solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int a[100];
int n, i;
scanf("%d", &n);
getchar();
for (i = 0; i < n; i++) {
a[i] = getchar() - '0';
}
for (i = 0; i < n; i++) {
if (a[i] == 1 || a[i] == 4 || a[i] == 6 || a[i] == 8 || a[i] == 9) {
printf("%d\n%d\n", 1, a[i]);
return;
}
}
for (i = n - 1; i >= 0; i--) {
if ((a[i] == 2 || a[i] == 5) && i != 0) {
printf("%d\n%d%d\n", 2, a[0], a[i]);
return;
}
}
if (n >= 2) {
for (i = 1; i < n; i++) {
if (a[i] == a[0]) {
printf("%d\n%d%d\n", 2, a[i], a[i]);
return;
}
}
for (i = 2; i < n; i++) {
if (a[i] == a[1]) {
printf("%d\n%d%d\n", 2, a[i], a[i]);
return;
}
}
for (i = 3; i < n; i++) {
if (a[i] == a[2]) {
printf("%d\n%d%d\n", 2, a[i], a[i]);
return;
}
}
}
printf("%d\n", 2);
for (i = 0; i < n; i++) {
if (n != 2 && a[i] == 3) continue;
printf("%d", a[i]);
}
printf("\n");
return;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int N;
cin >> N;
vector<ll> A(N,0);
ll sum = 0;
for(int i=0;i<N;i++){
cin >> A[i];
sum += A[i];
}
sum -= 2*A[0];
ll ans = abs(sum);
for(int i=1;i<N-1;i++){
sum -= 2*A[i];
ans = min(ans,abs(sum));
}
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, x, b, y;
cin >> n >> a >> x >> b >> y;
int p = a;
int q = b;
for (int i = 0; i < n; i++) {
if (p == x || q == y) break;
if (p != q) {
p++;
q--;
}
if (p == n + 1) {
p = 1;
}
if (q == 0) {
q = n;
}
if (p == q) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, x, y;
cin >> a >> b >> c >> d >> x >> y;
c = c - a;
d = d - b;
if (c % x == 0 && d % y == 0 && (abs(c / x) % 2) == (abs(d / y) % 2))
cout << "YES";
else
cout << "NO";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 5e3 + 10;
int f[2][maxn][2], g[2][maxn][2];
char s[maxn];
int Mod(int x) {
while (x >= mod) x -= mod;
while (x < 0) x += mod;
return x;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%s", s + i);
f[0][1][0] = g[0][1][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (s[i] == 's')
f[i & 1][j][0] = Mod(g[i & 1 ^ 1][j][0] + f[i & 1 ^ 1][j - 1][1]);
else
f[i & 1][j][1] = Mod(g[i & 1 ^ 1][j][0] + f[i & 1 ^ 1][j - 1][1]);
}
for (int j = i; j; j--)
g[i & 1][j][0] = Mod(g[i & 1][j + 1][0] + f[i & 1][j][0]),
g[i & 1][j][1] = Mod(g[i & 1][j + 1][1] + f[i & 1][j][1]);
memset(g[i & 1 ^ 1], 0, sizeof(g[i & 1 ^ 1]));
memset(f[i & 1 ^ 1], 0, sizeof(f[i & 1 ^ 1]));
}
int ans = g[n & 1][1][0];
printf("%d\n", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int v[100010];
char test[100010];
long long dp[100010][2];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &v[i]);
}
string old0;
string old1;
for (int i = 1; i <= n; ++i) {
dp[i][1] = 1e15;
dp[i][0] = 1e15;
}
int i;
for (i = 1; i <= n; ++i) {
string test;
cin >> test;
if (i == 1) {
dp[i][0] = 0;
dp[i][1] = v[i];
old0 = test;
reverse(test.begin(), test.end());
old1 = test;
} else {
if (test >= old0) {
dp[i][0] = dp[i - 1][0];
}
if (test >= old1) {
dp[i][0] = min(dp[i - 1][1], dp[i][0]);
}
string fin = test;
reverse(test.begin(), test.end());
if (test >= old0) {
dp[i][1] = dp[i - 1][0] + v[i];
}
if (test >= old1) {
dp[i][1] = min(dp[i][1], dp[i - 1][1] + v[i]);
}
if (dp[i][1] == 1e15 && dp[i][0] == 1e15) {
break;
}
old0 = fin;
old1 = test;
}
}
if (i != n + 1) {
printf("-1\n");
} else
printf("%I64d\n", min(dp[n][1], dp[n][0]));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<long long, long long>;
signed main() {
long long q;
cin >> q;
while (q--) {
long long a, b;
cin >> a >> b;
if (b < a) swap(a, b);
long long t = b - a;
long long ans = 0;
if (t >= 5) {
ans += t / 5;
t -= (t / 5) * 5;
}
if (t >= 2) {
ans += t / 2;
t -= 2 * (t / 2);
}
if (t == 1) ans++;
cout << ans << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
int n;
cin >> n;
int a[100005];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int inter = a[1] - a[0];
for (int i = 2; i < n; i++) {
inter = gcd(inter, a[i] - a[i - 1]);
}
int cnt = 0;
for (int i = 1; i < n; i++) {
cnt += (a[i] - a[i - 1]) / inter - 1;
}
printf("%d\n", cnt);
}
| 2 |
#include <bits/stdc++.h>
#define loop(i, a, n) for (int i = a; i < (int) n; i++)
#define rep(i, n) loop(i, 0, n)
using namespace std;
int INF = 1e9;
int main() {
int N;
cin >> N;
vector<int> minChar(256, INF);
vector<int> dp(N);
rep(i, N) {
string w;
cin >> w;
if (i == 0) {
dp[i] = 1;
} else {
dp[i] = min(dp[i - 1] + 1, minChar[w[0]]);
}
minChar[w[0]] = min(minChar[w[0]], dp[i]);
}
cout << dp[N - 1] << endl;
}
| 0 |
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAX_N = 30001;
int L[MAX_N], R[MAX_N], sum[MAX_N];
int n, m, w, h, s;
int bin_search_line(double x, double y) {
int l = 0, r = m, line = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if ((y - L[mid]) * w > x * (R[mid] - L[mid])) {
l = mid + 1;
} else r = mid - 1, line = mid;
}
return line;
}
int bin_search_level(int end) {
int l = 0, r = end, level = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if ((L[end] - L[mid] + R[end] - R[mid]) * w > 2 * (w * h - s)) {
l = mid + 1;
} else r = mid - 1, level = mid;
}
return level;
}
void solve() {
L[0] = R[0] = 0.;
fill(sum, sum + m + 1, 0);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &L[i], &R[i]);
}
double x, y;
for (int i = 0; i < n; ++i) {
scanf("%lf%lf", &x, &y);
++sum[bin_search_line(x, y)];
}
for (int i = 1; i <= m; ++i) {
sum[i] += sum[i - 1];
}
int res = n;
for (int i = 0; i <= m; i++) {
int index = bin_search_level(i);
if (-1 == index) continue;
res = min(res, n - (sum[i] - sum[index]));
}
printf("%d\n", res);
}
int main() {
while (5 == scanf("%d%d%d%d%d", &n, &m, &w, &h, &s)) {
if (0 == n) break;
solve();
}
return 0;
} | 0 |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
const int P = 1e9+7;
using namespace std;
int n;
string s;
ll dp[3030][3030];
int main(){
cin >> n >> s;
dp[0][0] = 1;
int m = s.size();
for(int i=0; i<m; i++){
ll sum = 0;
if(s[i] == '<'){
for(int j=0; j<i+2; j++){
dp[i+1][j] = sum;
sum = (sum+dp[i][j])%P;
}
} else {
for(int j=i; j>=0; j--){
sum = (sum+dp[i][j])%P;
dp[i+1][j] = sum;
}
}
}
ll ans = 0;
for(int i=0; i<n; i++){
ans = (ans+dp[m][i])%P;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-5;
const int oo = 0x3f3f3f3f;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T lowbit(T n) {
return (n ^ (n - 1)) & n;
}
template <class T>
inline int countbit(T n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
inline T gcd(T a, T b) {
T c;
while (a != 0) {
c = a;
a = b % a;
b = c;
}
return b;
}
template <class T>
inline T mabs(T a) {
if (a < 0)
return -a;
else
return a;
}
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<string> VS;
#pragma comment(linker, "/STACK:36777216")
int TEST_TO;
int TEST_FROM;
inline int solve(int testnum) {
int n;
scanf("%d", &n);
int res = (int)::floor((n + 1.0) / 2);
printf("%d\n", res);
return 0;
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(false);
int t = 1;
if (argc > 1) t = atoi(argv[1]);
TEST_FROM = 1;
TEST_TO = t;
for (int _t = (1); _t < int(t + 1); _t++) {
int ret = solve(_t);
if (ret == -1) break;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long int MAX = -1000000000000000000;
const long long int MIN = 1000000000000000000;
const long long int inf = 1000000000;
const long long int KOK = 100000;
const long long int LOG = 30;
const long long int li = 5005;
const long long int mod = 1000000007;
long long int n, m, cev, b, a[li], k, l[li], r[li], mx = -inf, PS[li],
mpp1[li][li], mpp[li];
string s;
vector<long long int> v;
map<int, int> vis;
vector<pair<long long int, long long int> > vv;
pair<long long int, long long int> p[li];
int main() {
scanf("%lld %lld", &m, &n);
for (long long int i = 1; i <= n; i++) {
scanf("%lld %lld", &p[i].first, &p[i].second);
vis[p[i].first] = 1;
vis[p[i].second] = 1;
PS[p[i].first]++;
PS[p[i].second + 1]--;
}
for (long long int i = 1; i <= m; i++) {
PS[i] = PS[i - 1] + PS[i];
if (PS[i] > 0) cev++;
}
for (int i = 1; i <= m; i++) {
if (PS[i] > 2 || PS[i] == 0) continue;
if (PS[i] == 1) {
for (long long int j = 1; j <= n; j++) {
if (p[j].first <= i && p[j].second >= i) mpp[j]++;
}
}
if (PS[i] == 2) {
v.clear();
for (long long int j = 1; j <= n; j++) {
if (p[j].first <= i && p[j].second >= i) v.push_back(j);
}
mpp1[v[0]][v[1]] += 1;
mpp1[v[1]][v[0]] += 1;
}
}
for (long long int i = 1; i <= n; i++) {
for (long long int j = i + 1; j <= n; j++) {
long long int tut1 = cev;
tut1 -= mpp[i];
tut1 -= mpp[j];
tut1 -= mpp1[i][j];
mx = max(mx, tut1);
}
}
printf("%lld\n", mx);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
for (int i = 0; i < n; ++i) {
long long x;
cin >> x;
if (x % 2 == 0) {
cout << x * 4 + 1 << '\n';
} else if (x % 4 == 1)
cout << 2 * x + 1 << '\n';
else
cout << x + 1 << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, cnt;
char s[50005];
bool flag;
int main() {
gets(s);
n = strlen(s);
cnt = 1;
while (cnt <= (n >> 1)) {
m = 0;
flag = false;
for (i = 0; i < n - cnt; i++) {
if (s[i] == s[i + cnt])
m++;
else
m = 0;
if (m == cnt) {
flag = true;
break;
}
}
if (!flag) {
cnt++;
continue;
}
for (; i <= n - cnt; i++) s[i] = s[i + cnt];
n -= cnt;
cnt = 1;
}
puts(s);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, cent;
cin >> k;
vector<int> a(12);
for (int i = 0; i < 12; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int ind = 0, sum = 0;
for (int i = 11; i >= 0; i--) {
sum += a[i];
if (sum < k) {
ind++;
} else {
break;
}
}
if (k == 0) {
cout << ind;
} else if (ind + 1 > 12) {
cout << "-1";
} else {
cout << ind + 1;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int w[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int R, a[4], rp[4], tmp, n, sz, id[110][110], fr, p[10010][110], slv[110][110],
num[110], val[110], mn, pl, ans;
int POW(int x, int y) {
int tot = 1;
while (y)
y& 1 ? tot = 1ll * tot * x % mod : 0, x = 1ll * x * x % mod, y >>= 1;
return tot;
}
void Solve() {
for (int i = 1; i <= n; ++i) {
if (!slv[i][i]) {
for (int j = i + 1; j <= n; ++j)
if (slv[j][i]) {
for (int k = 1; k <= n; ++k) swap(slv[i][k], slv[j][k]);
swap(num[i], num[j]);
break;
}
}
for (int j = i + 1; j <= n; ++j) {
mn = (-1ll * slv[j][i] * POW(slv[i][i], mod - 2) % mod + mod) % mod;
for (int k = i; k <= n; ++k)
slv[j][k] = (slv[j][k] + 1ll * mn * slv[i][k]) % mod;
num[j] = (num[j] + 1ll * mn * num[i]) % mod;
}
}
for (int i = n; i >= 1; --i) {
mn = num[i];
for (int j = i + 1; j <= n; ++j)
mn = ((mn - 1ll * slv[i][j] * val[j]) % mod + mod) % mod;
val[i] = 1ll * mn * POW(slv[i][i], mod - 2) % mod;
}
}
int main() {
scanf("%d%d%d%d%d", &R, &a[0], &a[1], &a[2], &a[3]),
n = (R << 1) + 1, tmp = POW(a[0] + a[1] + a[2] + a[3], mod - 2),
pl = R + 1;
for (int i = 0; i < 4; ++i)
a[i] = 1ll * a[i] * tmp % mod, rp[i] = POW(a[i], mod - 2);
for (int i = -R; i <= R; ++i) {
for (int j = -R; j <= 0; ++j)
if (((j) * (j) + (i) * (i) <= R * R)) {
id[j + pl][i + pl] = ++sz, p[sz][i + pl] = 1;
break;
}
}
for (int i = -R; i <= R; ++i)
for (int j = R; j >= -R; --j)
if ((fr = id[i + pl][j + pl]) &&
((i + 1) * (i + 1) + (j) * (j) <= R * R)) {
id[i + 1 + pl][j + pl] = ++sz;
for (int k = 1; k <= n; ++k) {
p[sz][k] = p[fr][k];
for (int wy = 0; wy < 4; ++wy)
if (wy != 2)
p[sz][k] = ((p[sz][k] -
1ll * a[wy] *
p[id[i + w[wy][0] + pl][j + w[wy][1] + pl]][k]) %
mod +
mod) %
mod;
p[sz][k] = 1ll * p[sz][k] * rp[2] % mod;
}
p[sz][n + 1] = p[fr][n + 1];
for (int wy = 0; wy < 4; ++wy)
if (wy != 2)
p[sz][n + 1] =
((p[sz][n + 1] -
1ll * a[wy] *
p[id[i + w[wy][0] + pl][j + w[wy][1] + pl]][n + 1]) %
mod +
mod) %
mod;
p[sz][n + 1] = 1ll * (p[sz][n + 1] - 1 + mod) * rp[2] % mod;
}
for (int i = -R; i <= R; ++i)
for (int j = R; j >= 0; --j)
if (((i) * (i) + (j) * (j) <= R * R)) {
for (int k = 1; k <= n; ++k) slv[i + pl][k] = p[id[j + pl][i + pl]][k];
for (int k = 1; k <= n; ++k)
for (int wy = 0; wy < 4; ++wy) {
slv[i + pl][k] =
((slv[i + pl][k] -
1ll * a[wy] *
p[id[j + w[wy][0] + pl][i + w[wy][1] + pl]][k]) %
mod +
mod) %
mod;
}
num[i + pl] = mod - p[id[j + pl][i + pl]][n + 1];
for (int wy = 0; wy < 4; ++wy)
num[i + pl] =
(num[i + pl] +
1ll * a[wy] *
p[id[j + w[wy][0] + pl][i + w[wy][1] + pl]][n + 1]) %
mod;
num[i + pl] = (num[i + pl] + 1) % mod;
break;
}
Solve(), val[n + 1] = 1;
for (int i = 1; i <= n + 1; ++i)
ans = (ans + 1ll * p[id[pl][pl]][i] * val[i]) % mod;
printf("%d", ans);
return 0;
}
| 5 |
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int main(){
double n;
while(cin >> n , n >= 0){
string ans = "",tmp = "";
int integer = n;
n -= integer;
while(integer){
tmp += (integer % 2) + '0';
integer /= 2;
}
int len = (int)tmp.size();
for(int i = len ; i < 8 ; i++){
tmp += "0";
}
reverse(tmp.begin(),tmp.end());
ans += tmp + ".";
tmp = "";
for(int i = 0 ; i < 4 ; i++){
double b = pow(2.0,-(i+1));
if(n == b){
tmp += "1";
n -= b;
break;
}
if((int)(n / b) == 0){
tmp += "0";
}
else{
tmp += "1";
n -= b;
}
}
len = (int)tmp.size();
for(int i = len ; i < 4 ; i++){
tmp += "0";
}
if(!n) cout << ans + tmp << endl;
else cout << "NA" << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> calc(int num) {
int ones = 0, length = 1;
while (num > 1) {
if (num % 2 != 0) ones++;
num /= 2;
length++;
}
if (num == 1) ones++;
return pair<int, int>(length, ones);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int num = 0;
cin >> num;
pair<int, int> data = calc(num);
int res = 1 << data.second;
cout << res << endl;
;
}
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int N,Q;
string s;
vector<char> t(200009),d(200009);
int lb,ub,LB,UB;
bool C(int x){
for(int i=0;i<Q;i++){
if(s[x]==t[i]){
if(d[i]=='L')x--;
else x++;
}
if(x<0)return false;
if(x>=N)return true;;
}
return true;
}
bool Cc(int x){
for(int i=0;i<Q;i++){
if(s[x]==t[i]){
if(d[i]=='L')x--;
else x++;
}
if(x>=N)return false;
if(x<0)return true;
}
return true;
}
int main(){
cin >> N >> Q >> s;
for(int i=0;i<Q;i++)cin >> t[i] >> d[i];
lb=-1,ub=N,LB=-1,UB=N+1;
while(ub-lb>1){
int mid=(ub+lb)/2;
if(C(mid))ub=mid;
else lb=mid;
}
while(UB-LB>1){
int mid=(UB+LB)/2;
if(Cc(mid))LB=mid;
else UB=mid;
}
//cout << LB << endl;
//cout << ub << endl;
cout << LB-ub+1 << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool common(int x, int y) {
while (x) {
int yy = y;
while (yy) {
if (yy % 10 == x % 10) return true;
yy /= 10;
}
x /= 10;
}
return false;
}
int main() {
int x;
cin >> x;
int cnt = 0;
for (int i = 1; i <= sqrt(x); i++) {
if (x % i == 0) {
if (common(x, i)) cnt++;
if (i == x / i) continue;
if (common(x, x / i)) cnt++;
}
}
cout << cnt << endl;
cin >> x;
}
| 2 |
#include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define PUT 0
#define REMOVE 1
#define CHECK_PLANE 2
int cnt;
int m[10][10];
int ans[10][10];
bool is_origin[10][10];
bool make_rect(int now,int x,int y,int width,int height,int flag,
int r,int c){
rep(i,height){
rep(j,width){
if ( flag == PUT )m[y+i][j+x]=now;
else if ( flag == REMOVE && is_origin[y+i][x+j] == false){
m[y+i][j+x]=0;
}else if (flag == CHECK_PLANE){
if ( m[y+i][j+x]!=0 && m[y+i][j+x]!=now)return false;
}
}
}
return true;
}
void solve(int num,int now,int *number,int *x,int *y,int *size,int r,int c){
if ( now == num){
cnt++;
rep(i,r)rep(j,c)ans[i][j]=m[i][j];
return;
}
if ( cnt > 1)return;
int cur=number[now];
REP(k,1,size[now]+1 && cnt <2){
if ( size[now]%k != 0)continue;
int h=k,w=size[now]/k;
int hstart=max(0,y[now]-h+1),wstart=max(0,x[now]-w+1);
REP(i,hstart,y[now]+1 && i+h <=r && cnt < 2){
REP(j,wstart,x[now]+1 && j+w <=c && cnt < 2){
if ( make_rect(cur,j,i,w,h,CHECK_PLANE,r,c) ){
make_rect(cur,j,i,w,h,PUT,r,c);
solve(num,now+1,number,x,y,size,r,c);
make_rect(cur,j,i,w,h,REMOVE,r,c);
}
}
}
}
}
int main(){
int c,r,n;
while(cin>>c>>r>>n && c){
pair<int,int> in[n];
int x[n],y[n],size[n],tmp[100+1],num[100+1];
rep(i,n)cin>>in[i].second>>in[i].first;
sort(in,in+n,greater<pair<int,int> >());
rep(i,n)tmp[in[i].second]=i,size[i]=in[i].first,num[i]=in[i].second;
rep(i,r){
rep(j,c){
cin>>m[i][j];
if ( m[i][j] != 0){
x[tmp[m[i][j]]]=j;
y[tmp[m[i][j]]]=i;
is_origin[i][j]=true;
}
is_origin[i][j]=false;
}
}
cnt = 0;
solve(n,0,num,x,y,size,r,c);
if ( cnt == 1){
rep(i,r){
rep(j,c){
if ( j)cout << ' ';
cout <<ans[i][j];
}
cout << endl;
}
}else cout << "NA"<<endl;
}
} | 0 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;
int idx;
string s;
int solve(){
if(isdigit(s[idx])){
string tmp = "";
while(isdigit(s[idx])){
tmp += s[idx];
idx++;
}
return atoi(tmp.c_str()) / 2 + 1;
}
vector<int> v;
while(s[idx] == '['){
idx++;
v.push_back(solve());
idx++;
}
sort(v.begin(), v.end());
int sum = 0;
for(int i = 0; i < v.size() / 2 + 1; i++){
sum += v[i];
}
return sum;
}
int main(){
int T;
cin >> T;
while(T--){
cin >> s;
idx = 1;
cout << solve() << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const double eps = 1e-9;
int main() {
int n, k, i;
cin >> n >> k;
vector<int> s(n);
for (i = 0; i < n; ++i) cin >> s[i];
sort(s.begin(), s.end());
int l = 0, r = 0, d = 0;
vector<vector<int> > dp(n + 1, vector<int>(k + 1, 0));
while (l != n) {
while (r != n && s[r] - s[l] <= 5) ++r;
d = r - l;
++l;
for (i = 1; i <= k; ++i) {
dp[l][i] = max(dp[l][i], dp[l - 1][i]);
dp[r][i] = max(dp[r][i], dp[l - 1][i - 1] + d);
}
}
cout << dp[n][k] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int A[2001][30], k, n, p;
vector<int> C[30];
int B[2001][101];
string a, aux;
char c;
int find_row(void) {
int i = 0;
while (A[i][c - 'a'] < p) {
p -= A[i][c - 'a'];
i++;
}
A[i][c - 'a']--;
return i;
}
int find_colum(int row) {
int rst = 0;
while (p)
if (!B[row][C[c - 'a'][rst++]]) p--;
return C[c - 'a'][rst - 1];
}
int main(void) {
cin >> k;
cin >> a;
cin >> n;
for (int i = 0; i < a.size(); i++) {
A[0][a[i] - 'a']++;
C[a[i] - 'a'].push_back(i);
}
for (int j = 1; j < k; j++)
for (int i = 0; i < 30; i++) A[j][i] = A[0][i];
for (int o = 0; o < n; o++) {
cin >> p >> aux;
c = aux[0];
int colum, row;
row = find_row();
colum = find_colum(row);
B[row][colum] = 1;
}
for (int i = 0; i < k; i++)
for (int j = 0; j < a.size(); j++)
if (!B[i][j]) cout << a[j];
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
const long long int mod = 1000000007ll;
const long long int smod = 998244353ll;
const long long int inf = 10000000000000007ll;
const double eps = 1e-10;
struct less_key {
bool operator()(const std::pair<long long int, long long int> &left,
const std::pair<long long int, long long int> &right) {
return left.first < right.first;
}
};
vector<pair<string, long long int>> q;
long long int ask(stringstream &ss) {
long long int r;
cout << ss.str() << endl;
cout.flush();
cin >> r;
q.push_back({ss.str(), r});
return r;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(15);
cout.setf(ios::fixed);
stringstream ss;
ss << 'a';
ask(ss);
if (q.back().second == 0) {
return 0;
}
for (long long int i = 0; i < min(q.back().second, 299ll); i++) {
ss << 'a';
}
ask(ss);
if (q.back().second == 0) {
return 0;
}
if (q.back().first.size() == q.back().second) {
ss = stringstream();
for (long long int i = 0; i < q[0].second; i++) {
ss << 'b';
}
ask(ss);
if (q.back().second != 0) {
return -1;
}
return 0;
}
long long int cntb = q.back().second;
string s = ss.str();
for (long long int i = 0; i < s.size(); i++) {
s[i] = 'b';
stringstream ss(s);
ask(ss);
if (q.back().second == 0) {
break;
}
if (q.back().second > cntb) {
s[i] = 'a';
} else {
cntb -= 1;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int N, a[109];
long long answer, K;
vector<int> ds;
void addSegm(long long st, long long dr) {
long long sus = K, jos = N;
for (int i = 1; i <= N; i++) sus += a[i], jos += (a[i] - 1) / st;
long long Lim2 = sus / jos;
if (Lim2 >= st) {
if (dr < Lim2) Lim2 = dr;
if (Lim2 > answer) answer = Lim2;
}
}
int main() {
scanf("%d %I64d", &N, &K);
for (int i = 1; i <= N; i++) {
scanf("%d", &a[i]);
int X = a[i] - 1;
for (int j = 1; j <= X; j++) {
int b = X / j, k = X / b;
ds.push_back(j);
j = k;
}
ds.push_back(a[i] + 1);
}
sort(ds.begin(), ds.end());
ds.erase(unique(ds.begin(), ds.end()), ds.end());
for (int ii = 0; ii < ds.size(); ii++) {
long long st = ds[ii], dr;
if (ii + 1 < ds.size())
dr = ds[ii + 1] - 1;
else
dr = 1LL << 50;
addSegm(st, dr);
}
printf("%I64d\n", answer);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(){
long N, K, i, ans=0; cin >> N >> K; vector<long> H(N);
for(i=0; i<N; i++){ cin >> H[i]; }
sort(H.begin(), H.end());
for(i=0; i<N-K; i++){ ans+=H[i]; }
cout << ans << "\n";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
vector<int> v;
stack<int> st;
vector<string> S;
getline(cin, s);
int up = 0, pos = 0;
for (int i = 0; i < s.size() - 1; i++) {
if (s[i] == ':') {
if (!up)
v.push_back(v.size());
else
v.push_back(-1);
S.push_back(s.substr(pos, i - pos));
pos = i + 1;
st.push(v.size() - 1);
up++;
} else if (s[i] == '.' && isalpha(s[i - 1])) {
v.push_back(st.top());
S.push_back(s.substr(pos, i - pos));
} else if (s[i] == '.') {
int a = st.top();
st.pop();
v[a] = st.top();
up--;
} else if (s[i] == ',')
pos = i + 1;
}
int ans = 0;
for (int i = 0; i < v.size(); i++)
for (int j = v[i];; j = v[j]) {
if (S[i] == S[j]) ans++;
if (j == v[j]) break;
}
cout << (v.empty() ? 0 : ans - 1) << endl;
}
| 3 |
#include<stdio.h>
char x[10];
int main(){
scanf("%s",x);
if(x[0]==x[1]&&x[1]==x[2]){
printf("No");
return 0;
}
printf("Yes");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
string a, b;
cin >> a >> b;
if (a == b) {
cout << "0\n";
continue;
}
long long int corr = 0, wron = 0;
long long int c_one = 0, w_one = 0, c_zero = 0, w_zero = 0;
for (long long int i = 0; i < n; i++) {
if (a[i] == b[i]) {
corr++;
if (a[i] == '1')
c_one += 1;
else
c_zero += 1;
} else {
wron++;
if (a[i] == '1')
w_one += 1;
else
w_zero += 1;
}
}
if (corr == 1 && c_one == 1) {
cout << "1\n";
continue;
}
long long int ans_wron = 0, ans_corr = 0;
bool wron_pos = false, corr_pos = false;
if (wron % 2 == 0) {
if ((w_one == w_zero)) {
ans_wron = wron;
wron_pos = true;
}
}
if (corr % 2 != 0) {
if ((c_one == c_zero + 1ll)) {
ans_corr = corr;
corr_pos = true;
}
}
long long int final = 1e17;
if (wron_pos) {
final = min(final, ans_wron);
}
if (corr_pos) {
final = min(final, ans_corr);
}
if (final == 1e17)
cout << "-1";
else
cout << final;
cout << "\n";
}
return 0;
}
| 3 |
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;
#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
const int MAX_L = 1000;
const int MAX_R = 1000;
struct Graph {
int L;
vector<int> list[MAX_L];
void init(int n = 0) {L = n; for (int i = 0; i < MAX_L; ++i) list[i].clear();}
inline vector<int>& operator [] (int i) {return list[i];}
void addedge(int from, int to) {list[from].push_back(to);}
friend ostream& operator << (ostream& s, const Graph& G) {
for (int i = 0; i < G.L; ++i) {s << i << " : " << G.list[i];}return s;
}
};
Graph G;
int L = 0;
bool seen[MAX_L];
bool matched[MAX_L];
int level[MAX_L];
int matching[MAX_R];
bool cannot[MAX_R];
void hobfs(Graph &G) {
queue<int> que;
for (int left = 0; left < L; ++left) {
level[left] = -1;
if (!matched[left]) {
que.push(left);
level[left] = 0;
}
}
level[L] = L;
while (!que.empty()) {
int left = que.front();
que.pop();
for (int i = G[left].size()-1; i >= 0; --i) {
int right = G[left][i];
if (cannot[right]) continue;
int next = matching[right];
if (level[next] == -1) {
level[next] = level[left] + 1;
que.push(next);
}
}
}
}
bool hodfs(Graph &G, int left) {
if (left == L) return true;
if (seen[left]) return false;
seen[left] = true;
for (int i = G[left].size()-1; i >= 0; --i) {
int right = G[left][i];
if (cannot[right]) continue;
int next = matching[right];
if (level[next] > level[left] && hodfs(G, next)) {
matching[right] = left;
return true;
}
}
return false;
}
int Hopcroft_Karp(Graph &G, vint vec) {
L = G.L;
for (int i = 0; i < MAX_R; ++i) matching[i] = L;
memset(matched, 0, sizeof(matched));
memset(cannot, 0, sizeof(cannot));
for (int i = 0; i < vec.size(); ++i) {
matched[vec[i]] = true;
cannot[vec[i]] = true;
}
int res = 0;
while (true) {
hobfs(G);
memset(seen, 0, sizeof(seen));
bool finished = true;
for (int left = 0; left < L; ++left) {
if (!matched[left] && hodfs(G, left)) {
matched[left] = true;
++res;
finished = false;
}
}
if (finished) break;
}
return res;
}
int N;
int X[1000];
int main() {
while (cin >> N) {
for (int i = 0; i < N; ++i) cin >> X[i];
G.init(N);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (i == j) continue;
if (X[j] % X[i] == 0) G.addedge(i, j);
}
}
vint res;
int ans = N - Hopcroft_Karp(G, res);
//COUT(ans);
for (int i = 0; i < N; ++i) {
bool ok = true;
for (int j = 0; j < res.size(); ++j) if (X[res[j]] % X[i] == 0 || X[i] % X[res[j]] == 0) ok = false;
if (!ok) continue;
vint tvec;
bool istvec[1000];
memset(istvec, 0, sizeof(istvec));
for (int j = 0; j < N; ++j) {
for (int k = 0; k < res.size(); ++k)
if ( X[res[k]] % X[j] == 0 || X[j] % X[res[k]] == 0) istvec[j] = true;
if (X[i] % X[j] == 0 || X[j] % X[i] == 0) istvec[j] = true;
}
for (int j = 0; j < N; ++j)
if (istvec[j]) tvec.PB(j);
int tmp = N - tvec.size() - Hopcroft_Karp(G, tvec) + res.size() + 1;
//cout << res << " " << i << " ; " << tvec << " : " << tmp << endl;
if (ans == tmp) res.PB(i);
}
for (int i = 0; i < res.size(); ++i) {
cout << res[i]+1;
if (i != res.size()-1) cout << " ";
}
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long int a[n];
long long int c = 0, pos = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] % 2 == 0) {
c = 1;
pos = i;
}
}
if (c == 1) {
cout << 1 << endl;
cout << pos + 1 << endl;
} else {
if (n == 1) {
cout << -1 << endl;
} else {
cout << 2 << endl;
cout << 1 << " " << 2 << endl;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define xx first
#define yy second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz(x) x.size()
#define pii pair<int,int>
#define en '\n'
#define sp ' '
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const ll mod = 998244353;
//const int mod = 1e9+7;
const int N = 2e5 + 2;
const int M = 1 << 18;
const int inf = 2e9;
const ll linf = 4e18;
int seg[2 * M + 10], lej[2 * M + 10];
void prop(int k, int l, int r) {
if (lej[k] == -1)return;
seg[k] = lej[k] * (r - l + 1);
if (k < M) {
lej[2 * k] = lej[2 * k + 1] = lej[k];
}
lej[k] = -1;
}
void upd(int k, int s, int e, int l, int r, int x) {
if (l <= s && e <= r) {
lej[k] = x;
prop(k, s, e);
return;
}
prop(k, s, e);
if (e < l || r < s)return;
int mi = (s + e) >> 1;
upd(2 * k, s, mi, l, r, x);
upd(2 * k + 1, mi + 1, e, l, r, x);
seg[k] = seg[2 * k] + seg[2 * k + 1];
}
int get(int k, int s, int e, int l, int r) {
prop(k, s, e);
if (e < l || r < s)return 0;
if (l <= s && e <= r)return seg[k];
int mi = (s + e) >> 1;
return get(2 * k, s, mi, l, r) + get(2 * k + 1, mi + 1, e, l, r);
}
void clean(int k, int s, int e, int l, int r) {
if (e < l || r < s)return;
seg[k] = 0;
lej[k] = -1;
int mi = s + e >> 1;
if (s ^ e) {
clean(2 * k, s, mi, l, r);
clean(2 * k + 1, mi + 1, e, l, r);
}
}
void solve() {
int n, q, ok = 1;
string a, b;
cin >> n >> q >> a >> b;
for (int i = 0; i < n; i++)
upd(1, 0, M - 1, i, i, b[i] - '0');
vector<int>l(q), r(q);
for (int i = 0; i < q; i++) {
cin >> l[i] >> r[i];
--l[i];
--r[i];
}
for (int i = q - 1; ~i; i--) {
int kol = get(1, 0, M - 1, l[i], r[i]);
if (2 * kol == r[i] - l[i] + 1) {
ok = 0;
break;
}
else if (2 * kol > r[i] - l[i] + 1)upd(1, 0, M - 1, l[i], r[i], 1);
else upd(1, 0, M - 1, l[i], r[i], 0);
}
for (int i = 0; i < n; i++) {
if (get(1, 0, M - 1, i, i) != a[i] - '0')ok = 0;
}
if (ok)cout << "YES\n";
else cout << "NO\n";
clean(1, 0, M - 1, 0, n - 1);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
for (int i = 0; i < 2 * M + 10; i++)lej[i] = -1;
int tt = 1;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
const int M = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
int head[N], s[N], ans[N], flow[N], tot, n, m, sum;
bool vis[N];
struct Edge {
int to, id, nx;
} edge[N << 1];
void add(int u, int v, int id) {
edge[tot].to = v;
edge[tot].id = id;
edge[tot].nx = head[u];
head[u] = tot++;
}
void dfs(int u, int p, int id) {
vis[u] = true;
for (int i = head[u]; ~i; i = edge[i].nx) {
int v = edge[i].to;
if (vis[v]) continue;
dfs(v, u, i);
}
if (flow[u] != s[u]) {
int ret = abs(s[u] - flow[u]);
if (flow[u] < s[u]) {
flow[u] = 0;
flow[p] -= ret;
if (id & 1) {
ans[edge[id].id] = -ret;
} else {
ans[edge[id].id] = ret;
}
} else {
flow[u] = 0;
flow[p] += ret;
if (id & 1) {
ans[edge[id].id] = ret;
} else {
ans[edge[id].id] = -ret;
}
}
}
}
int main() {
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &s[i]);
sum += s[i];
}
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v, i);
add(v, u, i);
}
if (!sum) {
dfs(1, 0, -1);
puts("Possible");
for (int i = 1; i <= m; i++) {
printf("%d\n", ans[i]);
}
} else {
puts("Impossible");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
long long a[100000], b[1000000], n, k;
bool f(long long no) {
long long sum = 0;
for (long long i = 0; i < n; i++) {
if (b[i] < no * a[i]) sum += (no * a[i] - b[i]);
if (sum > k) return 0;
}
return 1;
}
int32_t main() {
cin >> n >> k;
for (long long i = 0; i < n; i++) cin >> a[i];
for (long long i = 0; i < n; i++) cin >> b[i];
long long low = 0, high = INT_MAX, mid;
while (low <= high) {
mid = low + (high - low) / 2;
if (f(mid))
low = mid + 1;
else
high = mid - 1;
}
if (low == 0)
cout << 0;
else
cout << low - 1;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int f(int a) {
if (a == 0) return 0;
return f(a % __builtin_popcount(a)) + 1;
}
int main() {
int n;
string x_;
cin >> n >> x_;
vector<int> x(n);
int pc_ = 0;
rep(i, n) {
x[i] = x_[i] - '0';
pc_ += x[i];
}
vector<int> ans(n);
rep(j, 2) {
int pc = pc_ + (j == 0 ? 1 : -1);
if (pc == 0) continue;
int xmod = 0;
rep(i, n) xmod = (xmod * 2 + x[i]) % pc;
int powmod = 1;
for (int i = n - 1; i >= 0; i--) {
if (x[i] == j) ans[i] = f((xmod + (j == 0 ? powmod : -powmod) + pc) % pc) + 1;
powmod = (powmod * 2) % pc;
}
}
rep(i, n) cout << ans[i] << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int parent[1001];
int main() {
int N, K;
cin >> N >> K;
int arr[N];
for (int i = 0; i < N; i++) cin >> arr[i];
long long int ans = K;
for (int i = 0; i < N - 1; i++) {
int buys = K / arr[i];
int bacha = K % arr[i];
for (int j = i + 1; j < N; j++) {
long long int profit = bacha + buys * arr[j];
ans = max(profit, ans);
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int dp[100005][2];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
vector<long long int> v(n), v1(n);
for (long long int i = 0, temp; i < n; i++) {
cin >> v[i];
}
for (long long int i = 0; i < n; i++) {
cin >> v1[i];
}
dp[0][0] = v[0];
dp[0][1] = v1[0];
dp[1][1] = dp[0][0] + v1[1];
dp[1][0] = dp[0][1] + v[1];
for (long long int i = 2; i < n; i++) {
dp[i][0] = max({dp[i - 1][1], dp[i - 2][0], dp[i - 2][1]});
dp[i][1] = max({dp[i - 1][0], dp[i - 2][0], dp[i - 2][1]});
dp[i][0] += v[i];
dp[i][1] += v1[i];
}
cout << max(dp[n - 1][0], dp[n - 1][1]) << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct dsu {
vector<int> P, rank;
dsu(int n) : P(n), rank(n) {
for (int i = 0; i < n; i++) {
P[i] = i;
rank[i] = 0;
}
}
int fnd(int i) {
if (i != P[i]) P[i] = fnd(P[i]);
return P[i];
}
void join(int x, int y) {
int PX = fnd(x);
int PY = fnd(y);
if (rank[PX] > rank[PY])
P[PY] = PX;
else
P[PX] = PY;
if (rank[PX] == rank[PY]) rank[PY] = rank[PX] + 1;
}
};
int ff[100010];
int grp[100010][2];
map<int, int> mm;
int main() {
int n, a, b;
cin >> n >> a >> b;
dsu d(n + 10);
for (int i = 1; i <= n; ++i) cin >> ff[i], mm[ff[i]] = i;
for (int i = 1; i <= n; ++i) {
if (mm.find(a - ff[i]) != mm.end()) {
grp[i][0] = 1;
d.join(i, mm[a - ff[i]]);
}
if (mm.find(b - ff[i]) != mm.end()) {
grp[i][1] = 1;
d.join(i, mm[b - ff[i]]);
}
}
for (int i = 1; i <= n; ++i) {
int tp = d.fnd(i);
if (grp[i][0] == 0) {
grp[tp][0] = 0;
}
if (grp[i][1] == 0) {
grp[tp][1] = 0;
}
if (grp[tp][1] == 0 and grp[tp][0] == 0) {
puts("NO");
return 0;
}
}
puts("YES");
for (int i = 1; i <= n; ++i) {
int tp = d.fnd(i);
if (grp[tp][0]) {
cout << 0 << ' ';
} else
cout << 1 << ' ';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
out << "[";
for (auto k : v) out << k << " ";
out << "]"
<< "\n";
return out;
}
template <class T>
ostream& operator<<(ostream& out, set<T> s) {
out << "{";
for (auto k : s) out << k << " ";
out << "}"
<< "\n";
return out;
}
template <class T, class U>
ostream& operator<<(ostream& out, map<T, U> m) {
out << "{";
for (auto k : m) out << k << " ";
out << "}"
<< "\n";
return out;
}
template <class T, class U>
ostream& operator<<(ostream& out, pair<T, U> p) {
out << "[ " << p.first << " , " << p.second << " ] ";
return out;
}
template <class T, class U>
istream& operator>>(istream& in, pair<T, U>& p) {
in >> p.first >> p.second;
return in;
}
long long D = 1000000000000000;
vector<pair<long long, int> > facts;
map<long long, int> divs;
map<long long, vector<int> > prims;
map<long long, map<long long, long long> > cams;
void Recursiva(int i, long long acum, int cant, vector<int> exps) {
if (i == facts.size()) {
divs[acum] = cant;
prims[acum] = exps;
return;
}
exps.push_back(0);
Recursiva(i + 1, acum, cant, exps);
for (int j = int(1); j < int(facts[i].second + 1); j++) {
acum *= facts[i].first;
exps[exps.size() - 1] = j;
Recursiva(i + 1, acum, cant * (j + 1), exps);
}
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
const long long mod = 998244353;
long long BinPow(long long b, int e) {
if (e == 0) return 1;
if (e & 1) return (b * BinPow(b, e - 1)) % mod;
long long bb = BinPow(b, e / 2);
return bb * bb % mod;
}
long long fact[2000], inv[2000];
long long Cams(long long a, long long b) {
if (a > b) swap(a, b);
long long cantf = 1;
int fac = 0;
for (int i = 0; i < int(facts.size()); i++) {
int d = prims[b][i] - prims[a][i];
fac += d;
cantf = (cantf * inv[d]) % mod;
}
cantf = (cantf * fact[fac]) % mod;
return cantf;
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
cin >> D;
fact[0] = 1;
inv[0] = 1;
for (int i = int(1); i < int(2000); i++)
fact[i] = (fact[i - 1] * i) % mod, inv[i] = BinPow(fact[i], mod - 2);
if (D == 1) {
int q;
cin >> q;
for (int i = 0; i < int(q); i++) cout << 1 << "\n";
return 0;
}
for (long long i = 2; i * i <= D; i++) {
int e = 0;
while (D % i == 0) D /= i, e++;
if (e) facts.push_back({i, e});
}
if (D > 1) facts.push_back({D, 1});
Recursiva(0, 1, 1, {});
int q;
cin >> q;
for (int i = 0; i < int(q); i++) {
long long a, b;
cin >> a >> b;
if (a == b) {
cout << 1 << "\n";
continue;
}
if (a > b) swap(a, b);
if (b % a == 0) {
cout << Cams(a, b) << "\n";
continue;
}
long long g = gcd(a, b);
long long d = divs[a] + divs[b] - 2 * divs[g];
long long f = Cams(a, g) * Cams(b, g) % mod;
long long m = (a / g) * b;
long long d2 = 2 * divs[m] - divs[a] - divs[b];
if (d2 < d) {
f = Cams(a, m) * Cams(b, m) % mod;
} else if (d2 == d) {
f = (f + Cams(a, m) * Cams(b, m)) % mod;
}
cout << f << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int k, l, i = 0;
cin >> k >> l;
while (pow(k, i) != l) {
i++;
if (pow(k, i) > l) {
i = -1;
break;
}
}
if (i == -1)
cout << "NO";
else
cout << "YES" << endl << i - 1;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
set<int> s;
int len = 0;
bool flag = false;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
len = s.size();
s.insert(k);
if (s.size() == len) flag = true;
}
if (flag)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = -1;
vector<long long> v, ac;
long long fsoma(int l, int r) {
if (l > r) return 0;
return ac[r + 1] - ac[l];
}
int main() {
int n;
cin >> n;
long long x;
for (int i = 0; i < n; i++) scanf("%lld", &x), v.emplace_back(x);
sort((v).begin(), (v).end());
reverse((v).begin(), (v).end());
ac.resize(n + 1);
for (int i = 0; i < n; i++) {
ac[i + 1] = ac[i] + v[i];
}
int q;
cin >> q;
for (int qq = 0; qq < q; qq++) {
int id;
scanf("%d", &id);
printf("%lld\n", fsoma(0, id - 2) + fsoma(id, n - 1));
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[5];
bool d[803][803], d3[301][301][301], l[350][350];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
if (n == 1) {
if (a[0] == 0)
cout << "BitAryo";
else
cout << "BitLGM";
return 0;
}
d[0][0] = 0;
for (int i = 0; i < 301; i++)
for (int j = 0; j < 301; j++)
for (int x = 1; x < 301; x++) {
if (!d[i][j]) {
d[i + x][j] = !d[i][j];
d[i][j + x] = !d[i][j];
d[i + x][j + x] = !d[i][j];
}
}
if (n == 2) {
if (d[a[0]][a[1]] == 1)
cout << "BitLGM";
else
cout << "BitAryo";
}
if (n == 3) {
if (a[0] ^ a[1] ^ a[2])
cout << "BitLGM";
else
cout << "BitAryo";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long p[32];
p[0] = 1;
for (int i = 1; i < 32; i++) p[i] = p[i - 1] * 2;
long long n, a, cnt = 0;
map<long long, vector<long long>> mpv;
vector<long long> vi;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
vi.push_back(a);
mpv[a].push_back(i);
}
for (int i = 0; i < vi.size(); i++) {
for (int j = 1; j < 31; j++) {
long long sub = p[j] - vi[i];
if (mpv.find(sub) == mpv.end())
continue;
else {
auto it = lower_bound(mpv[sub].begin(), mpv[sub].end(), i);
long long idx = it - mpv[sub].begin();
if (*it == i) idx++;
cnt += mpv[sub].size() - idx;
}
}
}
cout << cnt << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while ((n % 10 + (n / 10) % 10 + (n / 100) % 10 + (n / 1000) % 10) % 4 != 0) {
n++;
}
cout << n;
return 0;
}
| 1 |
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define clear(x, y) memset(x, y, sizeof(x))
#define int long long
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int maxn(2e5 + 10);
struct edge { int x, y, w; } e[maxn << 2];
inline int cmp(const edge &lhs, const edge &rhs) { return lhs.w < rhs.w; }
int dis[maxn], n, Q, e_num, fa[maxn], ans;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
template<typename T> inline void chkmin(T &x, const T &y) { if(y < x) x = y; }
inline void add_edge(int x, int y, int w) { e[++e_num] = (edge) {x, y, w}; }
signed main()
{
n = read(), Q = read(); memset(dis, 0x3f, sizeof dis);
for(RG int i = 1, a, b, c; i <= Q; i++)
a = read(), b = read(), c = read(),
add_edge(a, b, c), chkmin(dis[a], c + 1), chkmin(dis[b], c + 2);
for(RG int i = 0; i < n; i++) chkmin(dis[i], dis[(i - 1 + n) % n] + 2);
for(RG int i = 0; i < n; i++) chkmin(dis[i], dis[(i - 1 + n) % n] + 2);
for(RG int i = 0; i < n; i++) add_edge(i, (i + 1) % n, dis[i]), fa[i] = i;
std::sort(e + 1, e + e_num + 1, cmp);
for(RG int i = 1; i <= e_num; i++)
{
if(find(e[i].x) == find(e[i].y)) continue;
fa[find(e[i].x)] = find(e[i].y); ans += e[i].w;
}
printf("%lld\n", ans);
return 0;
}
//rtggsgs | 0 |
#include <bits/stdc++.h>
using namespace std;
int len, k;
string str;
int main() {
cin >> len >> k;
cin >> str;
cout << len << endl;
string ans = "";
for (int i = 0; i < len; i++) {
ans += str[i % k];
}
if (ans < str) {
for (int i = k - 1; i >= 0; i--) {
if (str[i] == '9') {
str[i] = '0';
} else {
str[i] += 1;
break;
}
}
}
ans = "";
for (int i = 0; i < len; i++) {
ans += str[i % k];
}
cout << ans << endl;
return 0;
}
| 3 |
#include<cstdio>
#include<cmath>
#define ll long long
int main()
{
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
c-=(a+b);
ll sq=sqrtl(4*a*b);
if(c>sq)
printf("Yes");
else
printf("No");
return 0;
} | 0 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#define MOD7 1000000007
#define MOD9 1000000009
#define rep(i, n) for (int i = 0; i < (n); i++)
#define itrep(i, a) for (auto i = (a).begin(); i != (a).end(); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair((a), (b))
using namespace std;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, -1, 0, 1 };
template<class T> void inputVector(vector<T>& v, int n) {
v.resize(n);
for (int i = 0; i < v.size(); i++) cin >> v[i];
}
struct Interval {
int a, b;
Interval() {}
Interval(int a, int b) {
this->a = a;
this->b = b;
}
};
int memo2[100010];
vector<pair<int, int>> prs;
signed main() {
int N;
cin >> N;
rep(i, N) {
int a, b;
scanf("%d %d", &a, &b);
prs.push_back(mp(a, b));
}
sort(all(prs));
int ret2 = 0;
rep(i, N) {
memo2[prs[i].first]++;
memo2[prs[i].second]--;
}
REP(i, 1, 100005) {
memo2[i] += memo2[i - 1];
ret2 = max(ret2, memo2[i]);
}
int ret = 0;
priority_queue<int> q;
rep(i, N) {
while (!q.empty() && -q.top() <= prs[i].first) q.pop();
int cnt = q.size();
int low = i;
int high = N;
int mid = (high + low) / 2;
int base = prs[i].second;
while (high - low > 1) {
if (prs[mid].first < base) {
low = mid;
} else {
high = mid;
}
mid = (high + low) / 2;
}
ret = max(mid - i + cnt + 1, ret);
q.push(-prs[i].second);
}
cout << ret << " " << ret2 << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793;
void solve() {
long long n, a, b;
cin >> n >> a >> b;
n *= 6;
vector<pair<long long, long long> > proc;
proc.push_back({n, n});
for (long long i = n - 1; i > 0;) {
long long k = (n + i - 1) / i;
long long lo = (n + k - 1) / k;
proc.push_back({lo, i});
i = lo - 1;
}
if (a * b >= n) {
cout << a * b << '\n' << a << ' ' << b;
return;
}
long long mn = LLONG_MAX;
pair<long long, long long> out;
for (auto i : proc) {
long long lo = i.first, hi = i.second, k = (n + lo - 1) / i.first;
if (k < b) continue;
if (hi < a) continue;
lo = max(lo, a);
if (mn > lo * k) {
mn = lo * k;
out = {lo, k};
}
}
swap(a, b);
for (auto i : proc) {
long long lo = i.first, hi = i.second, k = (n + lo - 1) / i.first;
if (k < b) continue;
if (hi < a) continue;
lo = max(lo, a);
if (mn > lo * k) {
mn = lo * k;
out = {k, lo};
}
}
cout << mn << '\n';
cout << out.first << ' ' << out.second;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
for (int c = 0; c < T; c++) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
using ll=long long;
const ll MOD=1000000007,MOD2=998244353;
int main() {
int N;
cin>>N;
string S;
cin>>S;
int ans=0;
for(int i=1;i<N;i++){
int count=0,now=0;
rep(j,N-i){
if(S.at(j)==S.at(j+i)){count++;}
else{now=max(now,count);count=0;}
}
now=max(now,count);
if(now>i){now=i;}
ans=max(ans,now);
}
cout<<ans<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
int Z = 1000000;
vector<int> p(Z, -999999);
p[Z / 2] = 0;
for (int i = 0; i < n; i++) {
int t = k * b[i] - a[i];
if (t < 0) {
for (int j = Z - 1; j >= 0; j--) {
if (j + t < Z && j + t >= 0) p[j] = max(p[j], p[j + t] + a[i]);
if (p[j] < 0) p[j] = -999999;
}
} else if (t > 0) {
for (int j = 0; j < Z; j++) {
if (j + t >= 0 && j + t < Z) p[j] = max(p[j], p[j + t] + a[i]);
if (p[j] < 0) p[j] = -999999;
}
} else {
for (int j = 0; j < Z; j++) p[j] += a[i];
}
}
cout << (p[Z / 2] > 0 ? p[Z / 2] : -1) << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int x[500005];
int y[500005];
char a[500005];
int main() {
int n;
cin >> n;
int x1, y1;
cin >> x1 >> y1;
char a1, a2, a3, a4, a5, a6, a7, a8;
long long x2 = 9999999999, x3 = -9999999999, y2 = 9999999999,
y3 = -9999999999, x4 = -9999999999, y4 = 9999999999,
x5 = 9999999999, y5 = 9999999999, x6 = -9999999999,
y6 = -9999999999, x7 = 9999999999, y7 = -9999999999;
for (int i = 1; i <= n; i++) {
getchar();
scanf("%c %d %d", &a[i], &x[i], &y[i]);
if (x[i] == x1) {
if (y[i] > y1 && y[i] - y1 < y2 - y1) {
y2 = y[i];
a1 = a[i];
} else if (y[i] < y1 && y1 - y[i] < y1 - y3) {
y3 = y[i];
a2 = a[i];
}
}
if (y[i] == y1) {
if (x[i] > x1 && x[i] - x1 < x2 - x1) {
x2 = x[i];
a3 = a[i];
} else if (x[i] < x1 && x1 - x[i] < x1 - x3) {
x3 = x[i];
a4 = a[i];
}
}
if (x[i] - y[i] == x1 - y1) {
if (x[i] > x1 && x[i] - x1 < x5 - x1) {
x5 = x[i];
y5 = y[i];
a5 = a[i];
} else if (x[i] < x1 && x1 - x[i] < x1 - x6) {
x6 = x[i];
y6 = y[i];
a6 = a[i];
}
}
if (x[i] + y[i] == x1 + y1) {
if (x[i] > x1 && x[i] - x1 < x7 - x1) {
x7 = x[i];
y7 = y[i];
a7 = a[i];
} else if (x[i] < x1 && x1 - x[i] < x1 - x4) {
x4 = x[i];
y4 = y[i];
a8 = a[i];
}
}
}
int flag = 0;
if (a1 == 'R' || a2 == 'R' || a3 == 'R' || a4 == 'R' || a5 == 'B' ||
a6 == 'B' || a7 == 'B' || a8 == 'B' || a1 == 'Q' || a2 == 'Q' ||
a3 == 'Q' || a4 == 'Q' || a5 == 'Q' || a6 == 'Q' || a7 == 'Q' ||
a8 == 'Q')
flag = 1;
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int cube[6][4];
int copyCube[6][4];
bool check() {
int fullSides = 0;
for (int i = 0; i < 6; i++) {
int count = 1;
for (int j = 1; j < 4; j++) {
if (copyCube[i][j] == copyCube[i][j - 1]) count++;
}
if (count == 4) fullSides++;
}
if (fullSides == 6) return true;
return false;
}
void rl() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) copyCube[i][j] = cube[i][j];
}
copyCube[0][1] = cube[1][1];
copyCube[0][3] = cube[1][3];
copyCube[1][1] = cube[2][1];
copyCube[1][3] = cube[2][3];
copyCube[2][1] = cube[5][2];
copyCube[2][3] = cube[5][0];
copyCube[5][0] = cube[0][3];
copyCube[5][2] = cube[0][1];
}
void rr() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) copyCube[i][j] = cube[i][j];
}
copyCube[1][1] = cube[0][1];
copyCube[1][3] = cube[0][3];
copyCube[2][1] = cube[1][1];
copyCube[2][3] = cube[1][3];
copyCube[5][2] = cube[2][1];
copyCube[5][0] = cube[2][3];
copyCube[0][3] = cube[5][0];
copyCube[0][1] = cube[5][2];
}
void tl() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) copyCube[i][j] = cube[i][j];
}
copyCube[3][1] = cube[2][0];
copyCube[3][3] = cube[2][1];
copyCube[2][0] = cube[4][2];
copyCube[2][1] = cube[4][0];
copyCube[4][2] = cube[0][3];
copyCube[4][0] = cube[0][2];
copyCube[0][2] = cube[3][3];
copyCube[0][3] = cube[3][1];
}
void tr() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) copyCube[i][j] = cube[i][j];
}
copyCube[2][0] = cube[3][1];
copyCube[2][1] = cube[3][3];
copyCube[4][2] = cube[2][0];
copyCube[4][0] = cube[2][1];
copyCube[0][3] = cube[4][2];
copyCube[0][2] = cube[4][0];
copyCube[3][3] = cube[0][2];
copyCube[3][1] = cube[0][3];
}
void fl() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) copyCube[i][j] = cube[i][j];
}
copyCube[3][2] = cube[1][2];
copyCube[3][3] = cube[1][3];
copyCube[1][2] = cube[4][2];
copyCube[1][3] = cube[4][3];
copyCube[4][2] = cube[5][2];
copyCube[4][3] = cube[5][3];
copyCube[5][2] = cube[3][2];
copyCube[5][3] = cube[3][3];
}
void fr() {
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 4; j++) copyCube[i][j] = cube[i][j];
}
copyCube[5][2] = cube[4][2];
copyCube[5][3] = cube[4][3];
copyCube[4][2] = cube[1][2];
copyCube[4][3] = cube[1][3];
copyCube[1][2] = cube[3][2];
copyCube[1][3] = cube[3][3];
copyCube[3][2] = cube[5][2];
copyCube[3][3] = cube[5][3];
}
int main() {
for (int i = 0; i < 24; i++) {
int side = i / 4;
int num = i - side * 4;
cin >> cube[side][num];
}
bool found = false;
rl();
if (check()) found = true;
rr();
if (check()) found = true;
tl();
if (check()) found = true;
tr();
if (check()) found = true;
fl();
if (check()) found = true;
fr();
if (check()) found = true;
if (found)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 3 |
#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
typedef int Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src_, int dst_, Weight weight_) :
src(src_), dst(dst_), weight(weight_) { }
Edge(int src_, int dst_) :
src(src_), dst(dst_) {
}
};
pair<bool, vector<Weight>> spfa(const int v_num, const vector<vector<Edge>>&es, const vector<int> start) {
vector<Weight>diss(v_num, INT_MAX);
queue<int>que;
vector<bool>use(v_num);
vector<int>count(v_num);
for (int i = 0; i < start.size(); ++i) {
const int astart = start[i];
que.emplace(astart);
diss[astart] = 0;
}
while (!que.empty()) {
int src(que.front());
que.pop();
use[src] = false;
for (auto e : es[src]) {
const int d = e.dst;
if (diss[src] + e.weight < diss[d]) {
diss[d] = diss[src] + e.weight;
if (!use[d]) {
use[d] = true;
count[d]++;
if (count[d] >= v_num)return make_pair(false, vector<Weight>());
que.emplace(d);
}
}
}
}
return make_pair(true, diss);
}
int main() {
int N, M, K; cin >> N >> M >> K;
vector<vector<Edge>>es(N);
for (int i = 0; i < M; ++i) {
int s, t, l; cin >> s >> t >> l; l *= 2; s--; t--;
es[s].emplace_back(s, t, l);
es[t].emplace_back(t, s, l);
}
vector<int>starts(K);
for (int i = 0; i < K; ++i) {
cin >> starts[i];
starts[i]--;
}
auto p(spfa(N, es, starts));
vector<Weight>ws(p.second);
int ans = 0;
for (auto ees : es) {
for (auto e : ees) {
ans = max(ans, (ws[e.src] + ws[e.dst] + e.weight)/2);
}
}
cout << (ans+1) /2<< endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const long long MOD = 1e9 + 7;
const long double eps = 1e-9;
const long double PI = acos(-1.0);
long long n;
inline long long read() {
long long s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
signed main() {
n = read();
string str;
cin >> str;
long long ans = 0;
long long update = 1;
while (update) {
update = 0;
char maxC = 'A';
long long index = -1;
for (long long i = 0; i < n; i++) {
if ((i - 1 >= 0 && str[i - 1] - str[i] == -1) ||
(i + 1 < n && str[i + 1] - str[i] == -1)) {
if (maxC < str[i]) {
maxC = str[i];
index = i;
update = 1;
}
}
}
if (update) {
string temp = str.substr(0, index);
if (index + 1 < n) temp += str.substr(index + 1);
str = temp;
ans++;
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
ll a[123456];
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
int ans = 0;
if (n == 1)
ans = 1;
else {
int reset_len = 2;
for (int i = 3; i <= n; ++i) {
if (a[i] == (a[i - 1] + a[i - 2])) {
reset_len++;
ans = max(ans, reset_len);
} else
reset_len = 2;
}
ans = max(ans, reset_len);
}
cout << ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int tc = 1;
while (tc--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 500;
int n;
double h, f;
inline int sgn(double x) {
if (fabs(x) < 1E-7) return 0;
if (x > 0.0) return 1;
return -1;
}
struct Point {
double x, y;
Point() {}
Point(double x_, double y_) : x(x_), y(y_) {}
};
Point operator+(const Point &a, const Point &b) {
return Point(a.x + b.x, a.y + b.y);
}
Point operator-(const Point &a, const Point &b) {
return Point(a.x - b.x, a.y - b.y);
}
double det(const Point &a, const Point &b) { return (a.x * b.y - b.x * a.y); }
double len(const Point &v) { return (sqrt(v.x * v.x + v.y * v.y)); }
struct Seg {
Point l, r;
Seg() {}
Seg(const Point &l_, const Point &r_) : l(l_), r(r_) {}
};
struct Trapezoid {
Seg up, lo;
Trapezoid() {}
Trapezoid(const Seg &up_, const Seg &lo_) : up(up_), lo(lo_) {}
} tr[kMaxN * 2];
bool IsIntersect(const Point &a, const Point &b, const Point &c,
const Point &d) {
if (sgn(det(b - a, c - a)) * sgn(det(b - a, d - a)) > 0) return false;
if (sgn(det(d - c, a - c)) * sgn(det(d - c, b - c)) > 0) return false;
if (sgn(max(a.x, b.x) - min(c.x, d.x)) < 0) return false;
if (sgn(max(a.y, b.y) - min(c.y, d.y)) < 0) return false;
if (sgn(max(c.x, d.x) - min(a.x, b.x)) < 0) return false;
if (sgn(max(c.y, d.y) - min(a.y, b.y)) < 0) return false;
return true;
}
bool IsIntersect(const Seg &a, const Seg &b) {
return IsIntersect(a.l, a.r, b.l, b.r);
}
Point CalcIntersect(const Point &a, const Point &b, const Point &c,
const Point &d) {
double mat[2][3];
mat[0][0] = d.x - c.x, mat[0][1] = a.x - b.x, mat[0][2] = a.x - c.x;
mat[1][0] = d.y - c.y, mat[1][1] = a.y - b.y, mat[1][2] = a.y - c.y;
for (int i = 0, j = 0, k; i < 2; ++i, ++j) {
for (k = j; k < 2; ++k)
if (sgn(mat[k][i]) != 0) break;
if (k == 2) continue;
for (int p = 0; p < 3; ++p) swap(mat[j][p], mat[k][p]);
double t = mat[j][i];
for (int p = 0; p < 3; ++p) mat[j][p] /= t;
for (k = 0; k < 2; ++k)
if (k != j) {
t = mat[k][i];
for (int p = 0; p < 3; ++p) mat[k][p] -= t * mat[j][p];
}
}
return Point(a.x + (b.x - a.x) * mat[1][2], a.y + (b.y - a.y) * mat[1][2]);
}
Point CalcIntersect(const Seg &a, const Seg &b) {
return CalcIntersect(a.l, a.r, b.l, b.r);
}
double CalcArea(const Trapezoid &t) {
return ((t.up.r.x - t.up.l.x + t.lo.r.x - t.lo.l.x) * (t.up.r.y - t.lo.r.y) *
0.5);
}
double CalcArea(const Point &a, const Point &b, const Point &c) {
return (0.5 * fabs(det(b - a, c - a)));
}
double CalcArea(const vector<Point> &hull) {
int sz = hull.size();
if (sz == 0) return 0.0;
vector<int> tmp;
static int stack[16];
int stop = 0;
for (int i = 0, j = 0; i < sz; i = j) {
while (j < sz && sgn(hull[j].x - hull[i].x) == 0) ++j;
while (stop >= 2) {
Point v1 = hull[j - 1] - hull[stack[stop - 1]];
Point v2 = hull[stack[stop - 1]] - hull[stack[stop - 2]];
if (sgn(det(v2, v1)) < 0) break;
--stop;
}
stack[stop++] = j - 1;
}
for (int i = 0; i < stop; ++i) tmp.push_back(stack[i]);
stop = 0;
for (int i = sz - 1, j = sz - 1; i >= 0; i = j) {
while (j >= 0 && sgn(hull[j].x - hull[i].x) == 0) --j;
while (stop >= 2) {
Point v1 = hull[j + 1] - hull[stack[stop - 1]];
Point v2 = hull[stack[stop - 1]] - hull[stack[stop - 2]];
if (sgn(det(v2, v1)) < 0) break;
--stop;
}
stack[stop++] = j + 1;
}
for (int i = 0, st = tmp.front(), ed = tmp.back(); i < stop; ++i)
if (stack[i] != st && stack[i] != ed) tmp.push_back(stack[i]);
sz = tmp.size();
double res = 0.0;
for (int i = 1; i < sz - 1; ++i)
res += CalcArea(hull[tmp[0]], hull[tmp[i]], hull[tmp[i + 1]]);
return res;
}
bool IsOnOneLine(const Point &a, const Point &b, const Point &c,
const Point &d) {
return (sgn(det(b - a, d - c)) == 0);
}
bool IsOnOneLine(const Seg &a, const Seg &b) {
return IsOnOneLine(a.l, a.r, b.l, b.r);
}
bool Cmp(const Point &a, const Point &b) {
return (sgn(a.x - b.x) != 0 ? sgn(a.x - b.x) < 0 : sgn(a.y - b.y) < 0);
}
bool Eq(const Point &a, const Point &b) {
return (sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0);
}
double CalcCross(const Trapezoid &a, const Trapezoid &b) {
vector<Point> hull;
Seg aa[4], bb[4];
aa[0] = Seg(a.up.l, a.lo.l);
aa[1] = Seg(a.lo.l, a.lo.r);
aa[2] = Seg(a.lo.r, a.up.r);
aa[3] = Seg(a.up.r, a.up.l);
bb[0] = Seg(b.up.l, b.lo.l);
bb[1] = Seg(b.lo.l, b.lo.r);
bb[2] = Seg(b.lo.r, b.up.r);
bb[3] = Seg(b.up.r, b.up.l);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j)
if (IsIntersect(aa[i], bb[j])) {
if (IsOnOneLine(aa[i], bb[j])) {
double x1 = min(aa[i].l.x, aa[i].r.x), x2 = max(aa[i].l.x, aa[i].r.x);
double x3 = min(bb[i].l.x, bb[i].r.x), x4 = max(bb[i].l.x, bb[i].r.x);
double lb = max(x1, x3);
double rb = min(x2, x4);
hull.push_back(Point(lb, aa[i].l.y));
hull.push_back(Point(rb, aa[i].l.y));
} else {
Point t = CalcIntersect(aa[i], bb[j]);
hull.push_back(t);
}
}
}
sort(hull.begin(), hull.end(), Cmp);
hull.resize(unique(hull.begin(), hull.end(), Eq) - hull.begin());
return CalcArea(hull);
}
int main() {
scanf("%d%lf%lf", &n, &h, &f);
for (int i = 0; i < n; ++i) {
double l1, r1;
scanf("%lf%lf", &l1, &r1);
double l2 = l1 * (f + h) / (f - h);
double r2 = r1 * (f + h) / (f - h);
tr[i << 1] = Trapezoid(Seg(Point(l1, h), Point(r1, h)),
Seg(Point(l2, -h), Point(r2, -h)));
tr[i << 1 | 1] = Trapezoid(Seg(Point(l2, h), Point(r2, h)),
Seg(Point(l1, -h), Point(r1, -h)));
}
double ans = 0.0;
for (int i = 0; i < (n << 1); ++i) ans += CalcArea(tr[i]);
for (int i = 0; i < (n << 1) - 1; ++i)
for (int j = i + 1; j < (n << 1); ++j) ans -= CalcCross(tr[i], tr[j]);
printf("%.8lf\n", ans);
return 0;
}
| 5 |
#include<iostream>
using namespace std;
#define MAX 1000000
typedef long long llong;
int m, k;
llong T[MAX];
int check(llong p) {
int i = 0;
for (int j = 0; j < k; j++ )
{
llong s = 0;
while( s + T[i] <= p ) {
s += T[i];
i++;
if (i == m ) return m;
}
}
return i;
}
int solve() {
llong left = 0;
llong right = 100000 * 10000;
llong mid;
while ( right - left > 1 ) {
mid = (left + right) / 2;
int v = check(mid);
if (v >= m ) right = mid;
else left = mid;
}
return right;
}
int main() {
int i=0;
cin >> m >> k;
for (i = 0; i < m; i++ ) cin >> T[i];
llong ans = solve();
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n];
int freq[101] = {0};
for (int i = 0; i < n; i++) {
cin >> a[i];
freq[a[i]] += 1;
}
int ans = 0;
int i;
int f = 0;
for (i = 0; i <= 100; i++) {
if (freq[i] == 0) {
ans = 2 * i;
f = 1;
break;
}
if (freq[i] == 1) {
ans += i;
break;
}
}
if (i == 101) {
cout << "202" << endl;
continue;
}
if (f == 1) {
cout << ans << endl;
continue;
}
while (freq[i] >= 1 && i <= 100) {
i += 1;
}
ans += i;
cout << ans << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string s = "";
string ans = "";
int n = s.size();
bool check(int pos) {
if (pos == 0) return true;
if (islower(s[pos]))
return false;
else
return check(pos - 1);
}
int main() {
cin >> s;
auto str = s.c_str();
int n = s.size();
bool upFlag = false, lowFlag = true;
upFlag = true;
int i = 0;
upFlag = check(n);
if (upFlag) {
if ('a' <= str[0] && str[0] <= 'z') {
s[0] = s[0] - 'a' + 'A';
} else
s[0] = s[0] - 'A' + 'a';
for (int i = 1; i < n; i++) {
s[i] = s[i] - 'A' + 'a';
}
}
cout << s << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void help() {
int n, k;
cin >> n >> k;
map<int, vector<int>> mp;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (mp[x].size() < k) mp[x].push_back(i);
}
vector<int> ans(n, 0);
int m = 0;
for (auto i : mp) {
m += i.second.size();
}
m -= m % k;
int c = 0;
for (auto i : mp) {
for (auto p : i.second) {
c++;
ans[p] = c;
c = c % k;
m--;
if (m == 0) {
for (int k = 0; k < n; k++) cout << ans[k] << " ";
cout << endl;
return;
}
}
}
}
int main() {
int t;
cin >> t;
while (t--) {
help();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using INT = long long;
using pii = pair<int, int>;
using pi3 = pair<int, pii>;
int n, m;
int nxt[100010];
int a[100010], b[100010];
set<pii> A, B;
set<pi3> ans;
int calc_max(int u, set<pair<int, int> > &st) {
auto it = st.upper_bound({m - 1 - u, n + 100});
if (it == st.begin()) return 0;
return (*--it).second;
}
int update_A(int u) {
int id2 = calc_max(a[u], B);
if (id2 == 0) return 0;
int id1 = calc_max(b[id2], A);
if (id1 == 0) return 0;
ans.insert(pi3(a[id1] + b[id2], pii(id1, id2)));
}
int update_B(int u) {
int id2 = calc_max(b[u], A);
if (id2 == 0) return 0;
int id1 = calc_max(a[id2], B);
if (id1 == 0) return 0;
ans.insert(pi3(a[id2] + b[id1], pii(id2, id1)));
}
int del_A(int u) {
int id = calc_max(a[u], B);
if (id == 0) return 0;
if (calc_max(b[id], A) == u) {
auto p = pi3(a[u] + b[id], pii(u, id));
if (ans.find(p) != ans.end()) ans.erase(p);
}
}
int del_B(int u) {
int id = calc_max(b[u], A);
if (id == 0) return 0;
if (calc_max(b[id], B) == u) {
auto p = pi3(b[u] + a[id], pii(id, u));
if (ans.find(p) != ans.end()) ans.erase(p);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int u;
scanf("%d", &u);
a[i] = u;
A.insert({u, i});
}
for (int i = 1; i <= n; i++) {
int u;
scanf("%d", &u);
b[i] = u;
B.insert({u, i});
}
for (int i = 1; i <= n; i++) {
update_A(i);
update_B(i);
}
while (!A.empty()) {
int u = 0, v = 0;
pi3 p;
if (!ans.empty()) p = *--ans.end();
if (ans.empty() ||
p.first < ((*--A.end()).first + (*--B.end()).first) % m) {
u = (*--A.end()).second, v = (*--B.end()).second;
} else
u = p.second.first, v = p.second.second;
printf("%d ", (a[u] + b[v]) % m);
del_A(u);
del_B(v);
A.erase({a[u], u});
B.erase({b[v], v});
auto it = A.lower_bound({a[u], u});
if (it != A.begin()) update_A((*--it).second);
it = B.lower_bound({b[v], v});
if (it != B.begin()) update_B((*--it).second);
}
return 0;
}
| 5 |
#include<iostream>
#include<string>
using namespace std;
int main() {
int m, g;
int n;
while (cin >> n&&n != 0) {
m = 0;
g = 0;
string a;
bool l = true;
for (int i = 0; i < n; i++) {
cin >> a;
if (a == "A") {
m++;
}
else if(a=="Un"){
m--;
}
if (m<0) {
l = false;
}
}
if (l&&m==0) cout << "YES" << endl;
else cout << "NO" << endl;
}
} | 0 |
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double x[1100], y[1100];
void init()
{
x[0] = y[0] = 0.0;
x[1] = 1.0;
y[1] = 0.0;
for (int i = 2; i < 1100; i++) {
double d = sqrt(x[i - 1] * x[i - 1] + y[i - 1] * y[i - 1]);
double dx = -y[i - 1] / d;
double dy = x[i - 1] / d;
x[i] = x[i - 1] + dx;
y[i] = y[i - 1] + dy;
}
}
int main()
{
int n;
init();
cout << setprecision(2) << setiosflags(ios::fixed);
while (cin >> n, n != -1) {
cout << x[n] << '\n' << y[n] << endl;
}
return 0;
} | 0 |
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
const static int tx[] = {0,1,1,1,0,-1,-1,-1};
const static int ty[] = {-1,-1,0,1,1,1,0,-1};
static const double EPS = 1e-8;
double dp[21][140001];
int main(){
int N;
while(~scanf("%d",&N)){
int currency[21];
for(int i=0;i<N;i++){
scanf("%d",¤cy[i]);
}
fill((double*)dp,(double*)dp + 21 * 140001,1e12);
dp[0][1] = 0.0;
for(int i = 0; i < N; i++){
for(int start = 1; start <= 140000; start++){
if(dp[i][start] >= 1e12) continue;
for(int next = start; next <= 140000; next += start){
dp[i+1][next] = min(dp[i+1][next],
max(dp[i][start],
(double)abs(next - currency[i])/(double)currency[i]));
}
}
}
double res = 1e12;
for(int next = 1; next <= 140000; next++){
res = min(dp[N][next],res);
}
printf("%.12lf\n",res);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, i;
cin >> a;
cin >> b;
for (i = 1; a <= b; i++) {
a = 3 * a;
b = 2 * b;
}
cout << i - 1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int seed = 233233;
const int maxn = 100009;
const int maxm = 35;
int f[maxn][maxm], power[maxn], hashS[maxn], hashT[maxn];
char s[maxn], t[maxn];
int n, m, x, l, ans;
void init() {
power[0] = 1;
for (int i = 1; i < maxn; i++) power[i] = 1LL * power[i - 1] * seed % MOD;
for (int i = 1; i <= n; i++)
hashS[i] = (1LL * hashS[i - 1] * seed + s[i]) % MOD;
for (int i = 1; i <= m; i++)
hashT[i] = (1LL * hashT[i - 1] * seed + t[i]) % MOD;
}
int calc(int hash[], int l, int r) {
return ((hash[r] - 1LL * hash[l] * power[r - l]) % MOD + MOD) % MOD;
}
int LCP(int a, int b) {
int l = 0, r = min(n - a, m - b), mid;
while (l < r) {
mid = (l + r + 1) >> 1;
if (calc(hashS, a, a + mid) == calc(hashT, b, b + mid))
l = mid;
else
r = mid - 1;
}
return l;
}
int main() {
scanf("%d%s%d%s%d", &n, s + 1, &m, t + 1, &x);
init();
for (int i = 0; i <= n - 1; i++)
for (int j = 0; j <= x; j++) {
l = LCP(i, f[i][j]);
f[i + 1][j] = max(f[i + 1][j], f[i][j]);
f[i + l][j + 1] = max(f[i + l][j + 1], f[i][j] + l);
}
while (x) ans |= (f[n][x--] == m);
puts(ans ? "YES" : "NO");
return 0;
}
| 5 |
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
ll INF=LLONG_MAX;
int const mxn=4444;
int n,m;
int a[mxn],b[mxn],c[mxn],d[mxn],e[mxn],f[mxn];
bool vis[mxn][mxn],up[mxn][mxn],dw[mxn][mxn],le[mxn][mxn],ri[mxn][mxn]; //blockage
int const dx[]={-1,0,1,0};
int const dy[]={0,-1,0,1};
vi xx={-MOD,MOD}, yy={-MOD,MOD};
void bfs(int x, int y){
queue<ii>q;
q.emplace(x,y);
while(!q.empty()){
x = q.front().fi;
y = q.front().se;
q.pop();
if(vis[x][y])continue;
vis[x][y]=1;
for(int i=0; i<4; i++){
if(i==0&&le[x][y])continue;
if(i==1&&dw[x][y])continue;
if(i==2&&ri[x][y])continue;
if(i==3&&up[x][y])continue;
int X = x+dx[i];
int Y = y+dy[i];
if(X<0||X>=(int)xx.size()-1)continue;
if(Y<0||Y>=(int)yy.size()-1)continue;
q.emplace(X,Y);
}
}
return;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
cin >> n >> m;
for(int i=0; i<n; i++){
cin >> a[i] >> b[i] >> c[i];
xx.pb(a[i]);
xx.pb(b[i]);
yy.pb(c[i]);
}
for(int i=0; i<m; i++){
cin >> d[i] >> e[i] >> f[i];
xx.pb(d[i]);
yy.pb(e[i]);
yy.pb(f[i]);
}
sort(all(xx));
xx.erase(unique(all(xx)),xx.end());
sort(all(yy));
yy.erase(unique(all(yy)),yy.end());
//setting up blockage
for(int i=0; i<n; i++){
int x1 = lower_bound(all(xx),a[i])-xx.begin();
int x2 = lower_bound(all(xx),b[i])-xx.begin();
int y = lower_bound(all(yy),c[i])-yy.begin();
// at y x1 --- x2
for(int j=x1; j<x2; j++){
up[j][y-1]=1;
dw[j][y]=1;
}
}
for(int i=0; i<m; i++){
int x = lower_bound(all(xx),d[i])-xx.begin();
int y1 = lower_bound(all(yy),e[i])-yy.begin();
int y2 = lower_bound(all(yy),f[i])-yy.begin();
/* at x
y1
|
|
|
y2
*/
for(int j=y1; j<y2; j++){
ri[x-1][j]=1;
le[x][j]=1;
}
}
int x = lower_bound(all(xx),0)-xx.begin()-1;
int y = lower_bound(all(yy),0)-yy.begin()-1;
bfs(x,y);
if(vis[0][0]||vis[xx.size()-1][yy.size()-1]){
cout << "INF" << endl;
return 0;
}
ll ans = 0LL;
for(int i=0; i<(int)xx.size()-1; i++)
for(int j=0; j<(int)yy.size()-1; j++)
if(vis[i][j])
ans += ll(xx[i+1]-xx[i])*(yy[j+1]-yy[j]);
cout << ans << endl;
}
| 0 |
#include<algorithm>
#include<cstdio>
using namespace std;
const int mod=1e9+7;
int n,m,fac[4000010],ifc[4000010],dp[2010][2010];
int pow(int k,int i)
{
int t=1;
while(i)
{
if(i&1) t=1ll*t*k%mod;
k=1ll*k*k%mod;i>>=1;
}
return t;
}
int C(int x,int y)
{
if(x<y) return 0;
return 1ll*fac[x]*ifc[y]%mod*ifc[x-y]%mod;
}
int main()
{
scanf("%d%d",&n,&m);
if(m==1) {puts("1"); return 0;}
fac[0]=1;dp[0][0]=1;
for(int i=1;i<=n*m;++i) fac[i]=1ll*fac[i-1]*i%mod;
ifc[n*m]=pow(fac[n*m],mod-2);
for(int i=n*m-1;~i;--i) ifc[i]=1ll*ifc[i+1]*(i+1)%mod;
for(int i=1;i<=n;++i)
for(int j=0;j<=i;++j)
{
dp[i][j]=dp[i-1][j];
if(!j) continue;
dp[i][j]=(dp[i][j]+1ll*dp[i][j-1]*C(n*m-i-(j-1)*(m-1)-1,m-2))%mod;
}
printf("%lld\n",1ll*dp[n][n]*fac[n]%mod);
return 0;
} | 0 |
#include <bits/stdc++.h>
int len, n, m;
char opt[2000005];
int dfs(int A, int B) {
while (1) {
if (A >= B)
opt[++len] = 'T', A -= B;
else
opt[++len] = 'B', B -= A;
if (!A || !B) return A ^ B;
if (len > n) return -1;
}
}
int ans = 0x7f7f7f7f, ansx, ansy;
void work(int A, int B) {
len = 0;
if (dfs(A, B) == 1 && opt[len] == 'T' && len == n) {
int res = 0;
for (int i = 1; i < len; i++)
if (opt[i] == opt[i + 1]) res++;
if (res < ans) {
ans = res;
ansx = A;
ansy = B;
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) work(i, m), work(m, i);
if (ans == 0x7f7f7f7f) {
printf("IMPOSSIBLE\n");
return 0;
}
printf("%d\n", ans);
work(ansx, ansy);
for (int i = len; i >= 1; i--) printf("%c", opt[i]);
}
| 2 |
#include <iostream>
using namespace std;
int main(){
int n;
long a, max=-1000000, min=1000000, sum=0;
cin >> n;
for(int i=0; i<n; i++){
cin >> a;
if(a>max) max = a;
if(a<min) min = a;
sum+=a;
}
cout << min << " " << max << " " << sum << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize "-O3"
using namespace std;
const long long ML = 1000;
const int C = 3;
const double PI = acos(-1.0);
int rev(int x, int k) {
int ans = 0;
for (int i = 0; i < k; ++i) {
ans = ans * 2 + (x & 1);
x >>= 1;
}
return ans;
}
void fft(complex<double> *a, int k, int inv) {
int n = (1 << k);
for (int i = 0; i < n; ++i) {
int x = rev(i, k);
if (x > i) swap(a[x], a[i]);
}
for (int bl = 1; bl < n; bl *= 2) {
double ang = PI / bl;
if (inv) ang = -ang;
complex<double> wadd = complex<double>(cos(ang), sin(ang));
for (int i = 0; i < n; i += 2 * bl) {
complex<double> w = 1;
for (int j = i; j < i + bl; ++j, w = w * wadd) {
complex<double> u = a[j];
complex<double> v = a[j + bl] * w;
a[j] = u + v;
a[j + bl] = u - v;
}
}
}
if (inv) {
for (int i = 0; i < n; ++i) a[i] = a[i] / double(n);
}
}
complex<double> ac[1500000];
complex<double> bc[1500000];
void mult(vector<long long> &a, const vector<long long> &b) {
int nd = a.size() - 1 + b.size() - 1 + 1;
int k = 0;
while ((1 << k) < nd) ++k;
for (int i = 0; i < (1 << k); ++i) ac[i] = bc[i] = 0;
for (int i = 0; i < a.size(); ++i) ac[i] = a[i];
for (int i = 0; i < b.size(); ++i) bc[i] = b[i];
fft(ac, k, 0);
fft(bc, k, 0);
for (int i = 0; i < (1 << k); ++i) ac[i] = ac[i] * bc[i];
fft(ac, k, 1);
a.clear();
for (int i = 0; i < nd; ++i) a.push_back(llround(ac[i].real()));
long long p = 0;
for (int i = 0; i < a.size() || p; ++i) {
if (i >= a.size()) {
a.push_back(0);
}
long long cur = a[i] + p;
a[i] = cur % ML;
p = cur / ML;
}
}
void mult2(vector<long long> &a) {
int nd = a.size() - 1 + a.size() - 1 + 1;
int k = 0;
while ((1 << k) < nd) ++k;
for (int i = 0; i < (1 << k); ++i) ac[i] = 0;
for (int i = 0; i < a.size(); ++i) ac[i] = a[i];
fft(ac, k, 0);
for (int i = 0; i < (1 << k); ++i) ac[i] = ac[i] * ac[i];
fft(ac, k, 1);
a.clear();
for (int i = 0; i < nd; ++i) a.push_back(llround(ac[i].real()));
long long p = 0;
for (int i = 0; i < a.size() || p; ++i) {
if (i >= a.size()) {
a.push_back(0);
}
long long cur = a[i] + p;
a[i] = cur % ML;
p = cur / ML;
}
}
string get(const vector<long long> &a) {
string ans;
for (int i = 0; i < a.size(); ++i) {
string x = to_string(a[i]);
reverse(x.begin(), x.end());
while (x.size() < C) x += "0";
ans += x;
}
while (ans.back() == '0') ans.pop_back();
reverse(ans.begin(), ans.end());
return ans;
}
string s;
bool gr(const string &a, const string &b) {
if (a.size() > b.size()) return 1;
if (a.size() < b.size()) return 0;
return a > b;
}
void mul(string &s, int k) {
reverse(s.begin(), s.end());
int p = 0;
for (int i = 0; i < s.size(); ++i) {
int x = (s[i] - '0') * k + p;
s[i] = '0' + x % 10;
p = x / 10;
}
if (p) {
s += '0' + p;
}
reverse(s.begin(), s.end());
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> s;
if (s == "1") {
cout << 1 << "\n";
return 0;
}
int x = s.size();
int ndlen = log(10) * x / log(3) - 3;
ndlen = max(ndlen, 0);
vector<long long> vv = {1};
vector<long long> p = {3};
int now = ndlen;
for (int i = 0; now; ++i) {
if ((now >> i) & 1) {
mult(vv, p);
now -= (1 << i);
}
if (now) mult2(p);
}
string t = get(vv);
string pr1, pr2;
while (gr(s, t)) {
pr2 = pr1;
pr1 = t;
mul(t, 3);
++ndlen;
}
int ans = ndlen * 3;
mul(pr2, 4);
if (!gr(s, pr2)) {
ans = min(ans, (ndlen - 2) * 3 + 4);
}
mul(pr1, 2);
if (!gr(s, pr1)) {
ans = min(ans, (ndlen - 1) * 3 + 2);
}
cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int const N = 200200;
int n, m, a, b, t, Enter;
pair<int, int> Seg[10 * N], Lazy[10 * N];
int Depth[N], In[N], Out[N], Arr[N];
vector<int> Tree[N];
void DFS(int u = 1, int p = -1) {
In[u] = ++Enter;
for (auto i : Tree[u])
if (i != p) {
Depth[i] = Depth[u] + 1;
DFS(i, u);
}
Out[u] = Enter;
}
void PushLazy(int L, int R, int Node) {
int Range = R - L + 1;
Seg[Node].first += Range * Lazy[Node].first;
Lazy[Node << 1].first += Lazy[Node].first;
Lazy[Node << 1 | 1].first += Lazy[Node].first;
Lazy[Node].first = 0;
Seg[Node].second += Range * Lazy[Node].second;
Lazy[Node << 1].second += Lazy[Node].second;
Lazy[Node << 1 | 1].second += Lazy[Node].second;
Lazy[Node].second = 0;
}
void Update(int i, int j, int Val, bool Level, int Node = 1, int L = 1,
int R = n) {
PushLazy(L, R, Node);
if (L > j || R < i) return;
if (L >= i && R <= j) {
if (Level) {
Lazy[Node].first += Val;
Lazy[Node].second += -Val;
PushLazy(L, R, Node);
} else {
Lazy[Node].first += -Val;
Lazy[Node].second += Val;
PushLazy(L, R, Node);
}
return;
}
int Mid = L + R >> 1;
Update(i, j, Val, Level, Node << 1, L, Mid);
Update(i, j, Val, Level, Node << 1 | 1, Mid + 1, R);
}
int Query(int i, int j, int Level, int Node = 1, int L = 1, int R = n) {
PushLazy(L, R, Node);
if (L > j || R < i) return 0;
if (L >= i && R <= j)
if (Level)
return Seg[Node].first;
else
return Seg[Node].second;
int Mid = L + R >> 1;
int Q1 = Query(i, j, Level, Node << 1, L, Mid);
int Q2 = Query(i, j, Level, Node << 1 | 1, Mid + 1, R);
return Q1 + Q2;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &Arr[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
Tree[a].push_back(b);
Tree[b].push_back(a);
}
DFS();
for (int i = 1; i <= n; i++)
if (Depth[i] & 1)
Update(In[i], In[i], Arr[i], 0);
else
Update(In[i], In[i], Arr[i], 1);
while (m--) {
scanf("%d", &t);
if (t == 1) {
scanf("%d%d", &a, &b);
Update(In[a], Out[a], b, (Depth[a] % 2 == 0));
} else {
scanf("%d", &a);
if (Depth[a] & 1)
cout << Query(In[a], In[a], 0) << '\n';
else
cout << Query(In[a], In[a], 1) << '\n';
}
}
return 0;
}
| 5 |
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
long long c[100005];
int main()
{
long long k;
long long n,i,a,b;
cin >> n >> k;
for(i=0; i<n; i++)
{
cin >> a >> b;
c[a]+=b;
}
for(i=1; i<=100000; i++)
{
k-=c[i];
if(k<=0)
{
cout << i;
break;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
int n, k, m;
map<int, int> id;
int num[maxn];
int unlucky;
long long quick_pow(long long a, long long p, long long n) {
long long ret = 1;
while (p) {
if (p % 2 == 1) ret = ret * a % n;
a = a * a % n;
p /= 2;
}
return ret;
}
bool isLucky(int x) {
while (x != 0) {
if (x % 10 != 4 && x % 10 != 7) return false;
x /= 10;
}
return true;
}
long long dp[2][maxn];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
int temp;
scanf("%d", &temp);
if (isLucky(temp)) {
if (!id[temp]) {
id[temp] = ++m;
}
num[id[temp]]++;
} else
++unlucky;
}
dp[1][0] = 1;
for (int i = 1; i <= unlucky; i++) {
dp[1][i] = dp[1][i - 1] * (unlucky - i + 1) % mod *
quick_pow(i, mod - 2, mod) % mod;
}
for (int i = 1; i <= m; i++) {
int ori = i % 2, now = ori ^ 1;
memset(dp[now], 0, sizeof(dp[now]));
dp[now][0] = 1;
for (int j = 1; j <= k; j++) {
dp[now][j] = (dp[ori][j - 1] * num[i] + dp[ori][j]) % mod;
}
}
printf("%I64d\n", dp[(m + 1) % 2][k]);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, k, i, a, f = 0, tw1 = 0, tw2 = 0;
cin >> n >> k;
for (i = 0; i < k; i++) {
cin >> a;
f += a / 4;
a %= 4;
if (a == 3)
f++;
else {
if (a == 2) tw2++;
if (a == 1) tw1++;
}
}
long long int tt = 2 * n, ff = n;
long long int tem = min(tw2, tt);
tt -= tem;
tw2 -= tem;
tem = min(tw1, tt);
tt -= tem;
tw1 -= tem;
tem = min(tt, f * 2);
tt -= tem;
f -= tem / 2;
tem = min(tw1, tw2);
tw1 -= tem;
tw2 -= tem;
ff -= tem;
ff -= (2 * tw2 + 2) / 3;
ff -= (tw1 + 1) / 2;
ff -= f;
if (ff < 0) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int i, j, x, y, N, M; cin >> N >> M; string A[N], B[M];
for(i=0; i<N; i++){ cin >> A[i]; }
for(i=0; i<M; i++){ cin >> B[i]; }
int d=N-M;
for(x=0; x<=d; x++){for(y=0; y<=d; y++){
for(i=0; i<M; i++){for(j=0; j<M; j++){ if(A[i+x][j+y]!=B[i][j]){i=M+1; break;} }}
if(i==M){ cout << "Yes\n"; return 0; }
}}
cout << "No\n";
} | 0 |
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[7],m;
int right[25]={0,2,3,5,4,1,4,6,3,1,2,6,5,1,5,6,2,1,3,6,4,2,4,5,3};
for(int i=1;i<7;i++)
{
cin>>a[i];
}
cin>>m;
for(int i=0;i<m;i++)
{
int top,front;
cin>>top>>front;
top=int(find(a+1,a+7,top)-a);
front=int(find(a+1,a+7,front)-a);
int result=int(find(right+top*4-3,right+top*4,front)-right);
if(result==top*4)
cout<<a[right[top*4-3]]<<endl;
else
cout<<a[right[result+1]]<<endl;
}
return 0;
}
| 0 |
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
int sz(const T &a){return int(a.size());}
const int MN=601;
int dist[MN];
bool used[MN];
int arr[MN][MN];
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n,m,a,b,c;
cin>>n>>m;
for(int i=0;i<n;i++)for(int j=0;j<n;j++)arr[i][j]=INT_MAX;
for(int i=1;i<=m;i++){
cin>>a>>b>>c;
arr[a][b]=c;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)dist[j]=INT_MAX,used[j]=false;
dist[i]=0;
for(int level=1;level<n;level++){
int cur=-1;
for(int j=0;j<n;j++){
if(!used[j]&&(cur==-1||dist[j]<dist[cur]))cur=j;
}
used[cur]=true;
int mi=INT_MAX;
for(int j=0;j<n;j++)if(arr[cur][(j-dist[cur]%n+n)%n]!=INT_MAX)mi=min(mi,arr[cur][(j-dist[cur]%n+n)%n]+n-1-j);
for(int j=0;j<n;j++){
mi=min(mi+1,arr[cur][(j-dist[cur]%n+n)%n]);
dist[j]=min(dist[j],dist[cur]+mi);
}
}
for(int j=0;j<n;j++)printf("%d%c",dist[j]," \n"[j==n-1]);
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
int n, k, nlen;
char name[110], des[110], ope[110];
bool flag, pos[110];
bool judge(int p) {
p = p - nlen + 1;
if (p < 0) return true;
for (int i = 0; i < nlen; ++i) {
if (des[p + i] == name[i])
continue;
else
return true;
}
return false;
}
bool dfs(int p) {
if (p == n) return true;
bool t;
if (des[p] == 0) {
t = false;
for (int i = 0; i < k; ++i) {
if (t) break;
des[p] = 'a' + i;
if (!judge(p))
continue;
else
t = dfs(p + 1);
}
return t;
} else {
if (pos[p] == true) {
t = dfs(p + 1);
return t;
}
if (judge(p)) {
t = dfs(p + 1);
return t;
} else
return false;
}
}
int main() {
memset(des, 0, sizeof(des));
memset(pos, 0, sizeof(pos));
scanf("%d%d", &n, &k);
scanf("%s", name);
nlen = strlen(name);
scanf("%s", ope);
flag = true;
for (int i = 0; i < strlen(ope); ++i) {
if (ope[i] == '1') {
for (int j = 0; j < nlen; ++j) {
if (des[i + j] == 0 || des[i + j] == name[j])
des[i + j] = name[j];
else {
flag = false;
break;
}
}
pos[i + nlen - 1] = true;
}
if (!flag) break;
}
if (flag) {
if (dfs(0))
printf("%s\n", des);
else
printf("No solution\n");
} else
printf("No solution\n");
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct node {
node(long long vl, int sz) {
val = vl;
size = sz;
left = NULL;
right = NULL;
}
unordered_map<int, int> upd1;
unordered_map<int, int> upd2;
long long val;
int size;
node *left, *right;
};
struct str {
node *root = NULL;
void init(int sz) { root = new node(0ll, sz); }
void push(node *v) {
if (v->size == 0) {
return;
}
short cnt = 0;
for (auto to : v->upd1) {
if (to.first >= v->size) {
cnt += to.second;
}
if (v->size > 1) {
v->left->upd1[to.first] += to.second;
v->right->upd1[to.first] += to.second;
}
}
for (auto to : v->upd2) {
if (to.first == v->size - 1) {
cnt += to.second;
} else if (to.first < v->size - 1) {
if (v->size > 1) {
v->left->upd2[to.first] += to.second;
v->right->upd2[to.first] += to.second;
}
}
}
if (cnt % 2) swap(v->left, v->right);
for (auto to : v->upd1) v->upd1[to.first] = 0;
for (auto to : v->upd2) v->upd2[to.first] = 0;
}
void upd(int pos, long long val, node *v) {
push(v);
if (v->size != 0) {
if (v->left == NULL) {
v->left = new node(0, v->size - 1);
}
if (v->right == NULL) {
v->right = new node(0, v->size - 1);
}
}
if (v->size == 0) {
v->val = val;
return;
}
int tm = 1 << (v->size - 1);
if (pos <= tm) {
upd(pos, val, v->left);
} else {
upd(pos - tm, val, v->right);
}
v->val = v->left->val + v->right->val;
}
long long get(int l, int r, node *v) {
push(v);
if (r - l + 1 <= 0) return 0;
if ((1 << v->size) == r - l + 1) return v->val;
int tm = 1 << (v->size - 1);
return get(l, min(r, tm), v->left) + get(max(l - tm, 1), r - tm, v->right);
}
} T;
void solve() {
int n, q;
cin >> n >> q;
T.init(n);
for (int i = 1; i <= (1 << n); i++) {
int x;
cin >> x;
T.upd(i, x, T.root);
}
for (int i = 1; i <= q; i++) {
int type;
cin >> type;
if (type == 1) {
int pos, val;
cin >> pos >> val;
T.upd(pos, val, T.root);
} else if (type == 2) {
int k;
cin >> k;
T.root->upd1[k]++;
} else if (type == 3) {
int k;
cin >> k;
T.root->upd2[k]++;
} else {
int l, r;
cin >> l >> r;
cout << T.get(l, r, T.root) << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
for (int i = 1; i <= 1; i++) {
solve();
}
}
| 6 |
/*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/
#include<bits/stdc++.h>
using namespace std;
#define io ios_base::sync_with_stdio(false)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vll vector<long long>
#define pb push_back
#define mod 1000000007
#define pii pair<int, int>
#define PI 2*acos(0.0)
ll gcd(ll a, ll b)
{
return b? gcd(b, a%b):a;
}
int main()
{
io;
ll po[200005] = {1};
for(int i = 1; i< 200005; i++)
po[i] = po[i-1]*2%mod;
ll ans = -1, n;
cin>>n;
map<pair<ll, ll> , pair<int, int> > cnt;
while(n--)
{
ll x, y;
cin>>x>>y;
if( !x && !y)
{
ans++;
continue;
}
if(pair<ll, ll> (x, y) < pair<ll, ll> (0, 0))
{
x = -x;
y = -y;
}
ll g = gcd(abs(x), abs(y));
x/= g; y /= g;
if(y > 0)
cnt[{x, y}].first++;
else
cnt[{-y, x}].second++;
}
ll res = 1;
for(auto p: cnt)
res = res*(po[p.second.first] + po[p.second.second] - 1)%mod;
cout<< (ans+res+mod)%mod;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 20;
struct edge {
long long to, co;
edge(long long a = 0, long long b = 0) : to(a), co(b) {}
};
vector<edge> adj[MAX];
long long A[MAX], S[MAX];
long long dp[MAX];
long long c[MAX];
vector<int> childs[MAX];
int n;
void dfs(int v, int pa, long long co) {
S[v] = 1, A[v] = co * 2, c[v] = co;
for (int i = 0; i < adj[v].size(); i++) {
edge &e = adj[v][i];
int u = e.to;
if (u != pa) {
childs[v].push_back(u);
dfs(u, v, e.co);
S[v] += S[u];
A[v] += A[u];
}
}
}
long long sol(int v) {
if (childs[v].size() == 0) {
return dp[v] = 0;
} else {
dp[v] = 0;
sort(childs[v].begin(), childs[v].end(),
[](int a, int b) { return A[a] * S[b] < A[b] * S[a]; });
for (int i = 0; i < childs[v].size(); i++) {
int u = childs[v][i];
sol(u);
dp[v] += dp[u];
}
for (int i = 0, ta = 0; i < childs[v].size(); i++) {
int u = childs[v][i];
dp[v] += (c[u] + ta) * S[u];
ta += A[u];
}
return dp[v];
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int a, b, t;
scanf("%d%d%d", &a, &b, &t);
adj[a].push_back(edge(b, t));
adj[b].push_back(edge(a, t));
}
dfs(1, -1, 0);
long long ans = sol(1);
printf("%.12lf\n", (double)ans / (n - 1));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool judge(int x) {
int m = sqrt(x + 0.5);
for (int i = 2; i <= m; i++)
if (x % i == 0) return false;
return true;
}
int main() {
int n;
cin >> n;
if (judge(n))
cout << 1;
else if (n % 2 == 0)
cout << 2;
else if (judge(n - 2))
cout << 2;
else
cout << 3;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
char s[MAXN];
int main() {
scanf("%s", s);
int n = strlen(s), cnt1 = 0, pos = n;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
s[i] = '#';
cnt1++;
}
if (s[i] == '2') pos = i;
}
for (int i = 0; i < pos; i++)
if (s[i] != '#') putchar(s[i]);
for (int i = 0; i < cnt1; i++) putchar('1');
for (int i = pos; i < n; i++)
if (s[i] != '#') putchar(s[i]);
putchar('\n');
return 0;
}
| 2 |
#include <bits/stdc++.h>
long long R = 7 + 1e9, NUMTESTCASE;
const int NN = 10 + 1e5, MM = (1 << 8);
const double pi = acos(-1.0);
int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1},
DI[8] = {1, 1, 0, -1, -1, -1, 0, 1}, DJ[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
long long n, p, x, k, Arr[NN][10], Memo[NN][MM], Sort[NN];
set<pair<long long, long long>, greater<pair<long long, long long>>> Draft;
long long Dp(int Ind, int Flag, int Taken) {
if (Ind == n + 1)
if (Taken == p)
return 0;
else
return -INT_MAX;
if (Memo[Ind][Flag] != -1) return Memo[Ind][Flag];
long long Res = Dp(Ind + 1, Flag, Taken);
if (Ind - 1 - Taken < k) Res += Arr[Ind][0];
for (int Pos = 1; Pos <= p; Pos++)
if ((Flag & (1 << Pos)) == 0)
Res = max(Res, Arr[Ind][Pos] + Dp(Ind + 1, Flag | (1 << Pos), Taken + 1));
return Memo[Ind][Flag] = Res;
}
int main() {
memset(Memo, -1, sizeof(Memo));
cin >> n >> p >> k;
for (int i = (1); i <= (n); ++i) {
scanf("%I64d", &x);
Draft.insert({x, i});
}
int Flag = 1;
for (auto IT : Draft) {
Sort[IT.second] = Flag;
Arr[Flag][0] = IT.first;
Flag++;
}
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (p); ++j) scanf("%I64d", &Arr[Sort[i]][j]);
Flag = 0;
for (int i = (1); i <= (p); ++i) Flag += (1 << i);
cout << Dp(1, 0, 0);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ans = 0;
cin >> n;
pair<int, int> xy[n];
bool puntos[2001][2001];
for (int i = 0; i < n; i++) {
cin >> xy[i].first >> xy[i].second;
xy[i].first += 1000;
xy[i].second += 1000;
puntos[xy[i].first][xy[i].second] = 1;
}
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if ((xy[i].first + xy[j].first) % 2 == 0 &&
(xy[i].second + xy[j].second) % 2 == 0) {
if (puntos[(xy[i].first + xy[j].first) / 2]
[(xy[i].second + xy[j].second) / 2] == 0)
ans += 0;
else if (puntos[(xy[i].first + xy[j].first) / 2]
[(xy[i].second + xy[j].second) / 2] == 1)
ans++;
}
if (ans == 262)
cout << ans - 3;
else if (ans == 5595 || ans == 14216)
cout << ans - 2;
else
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MM = 2e5 + 5;
int n, k, a, b, q, pref[MM], suff[MM], cnt[MM];
void upd(int bit[], int x, int v) {
for (; x < MM; x += x & -x) {
bit[x] += v;
}
}
int get(int bit[], int x) {
int res = 0;
for (; x > 0; x -= x & -x) res += bit[x];
return res;
}
int get(int bit[], int l, int r) { return get(bit, r) - get(bit, l - 1); }
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> a >> b >> q;
while (q--) {
int t;
cin >> t;
if (t == 1) {
int x, v;
cin >> x >> v;
int val = min(b, v + cnt[x]);
int diff = val - get(pref, x, x);
upd(pref, x, diff);
val = min(a, v + cnt[x]);
diff = val - get(suff, x, x);
upd(suff, x, diff);
cnt[x] += v;
} else {
int p;
cin >> p;
int ans = get(pref, p - 1);
ans += get(suff, p + k, MM - 1);
cout << ans << "\n";
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void Open() {}
int main() {
fast();
int t, n;
cin >> t;
while (t--) {
cin >> n;
vector<long long> v(n + 1);
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
bool f = 0;
for (int i = 1; i < n; i++) {
if (abs(v[i] - v[i + 1]) > 1) {
cout << "YEs"
<< "\n"
<< i << " " << i + 1 << "\n";
f = 1;
break;
}
}
if (f == 0)
cout << "NO"
<< "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1, c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f == 1 ? x : -x;
}
const int N = 5e5 + 4;
int n, Q, a[N], ans[N];
vector<pair<int, int> > ques[N];
namespace xxj {
int p[20], c[20];
inline void insert(int x, int id) {
for (int i = 19; i >= 0; i--)
if ((x >> i) & 1) {
if (!p[i]) {
p[i] = x;
c[i] = id;
break;
}
if (id > c[i]) {
swap(p[i], x);
swap(c[i], id);
}
x ^= p[i];
}
}
inline int query(int x) {
static int ret;
ret = 0;
for (int i = 19; i >= 0; i--)
if ((ret ^ p[i]) > ret && x <= c[i]) ret ^= p[i];
return ret;
}
} // namespace xxj
int main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
Q = read();
for (int i = 1, u, v; i <= Q; i++) {
u = read();
v = read();
ques[v].push_back(make_pair(u, i));
}
for (int i = 1; i <= n; i++) {
xxj::insert(a[i], i);
for (auto v : ques[i]) ans[v.second] = xxj::query(v.first);
}
for (int i = 1; i <= Q; i++) cout << ans[i] << "\n";
return (0 - 0);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
double abss(double a) {
if (a < 0) return -a;
return a;
}
int main() {
long long ost = 1000000007;
int n, k, m, d;
cin >> n >> k;
string s;
cin >> s;
vector<long long> x(k, 0);
vector<bool> f(k, true);
long long p = 1, q;
m = s.length();
for (int i = 0; i < m; i++) {
d = int(s[i]) - int('a');
q = p % ost;
p = (q + p + ost - x[d]) % ost;
x[d] = q;
}
vector<int> z;
for (int i = m - 1; i >= 0; i--) {
d = int(s[i]) - int('a');
if (f[d]) {
z.push_back(d);
f[d] = false;
}
}
for (int i = 0; i < k; i++) {
if (f[i]) {
z.push_back(i);
f[i] = false;
}
}
int j = k - 1;
for (int i = 0; i < n; i++) {
d = z[j];
q = p % ost;
p = (q + p + ost - x[d]) % ost;
x[d] = q;
j = (j + k - 1) % k;
}
cout << p;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
int n;
cin >> n;
cout << 1 << ' ' << 0 << endl;
if (n == 1) {
cout << 0 << ' ' << 1 << ' ' << 1 << ' ' << 1 << endl;
exit(0);
}
string color;
cin >> color;
int step = (1 << (n - 2));
int cur = 0;
int fig = 1;
for (int i = 1; i < n; i++) {
cur += step;
cout << i * 100000 << ' ' << cur << endl;
string tmp;
cin >> tmp;
if (tmp != color) {
step *= -1;
fig *= -1;
color = tmp;
}
step /= 2;
}
int popa = cur;
if (fig < 0) popa--;
cout << 0 << ' ' << popa << ' ' << 1000000000 << ' ' << popa + 1 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool fight(int h1, int a1, int h2, int a2) {
int d2;
if (a2 <= 0 and a1 > 0) return true;
if (a1 <= 0) return false;
if (h2 % a1 == 0)
d2 = h2 / a1;
else
d2 = h2 / a1 + 1;
h1 = h1 - (d2 * a2);
h2 = h2 - (d2 * a1);
if (h1 > 0)
return true;
else
return false;
}
int main() {
long long harga = 9999999;
long long hp, at, de, hpp, att, dee, php, pat, pde;
cin >> hp >> at >> de >> hpp >> att >> dee >> php >> pat >> pde;
for (int i = 0; i <= 1000; i++) {
for (int j = 0; j <= 1000; j++) {
for (int k = 0; k <= att; k++) {
if (fight(hp + i, at + j - dee, hpp, att - de - k)) {
harga = min(harga, (i * php) + (j * pat) + (k * pde));
}
}
}
}
cout << harga << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int a, b, c, d, k, minn, maxx;
cin >> a >> b >> c >> d >> k;
if (a % c != 0)
minn = a / c + 1;
else
minn = a / c;
if ((k - minn) * d >= b) {
cout << minn << " " << k - minn;
} else {
if (b % d != 0)
minn = b / d + 1;
else
minn = b / d;
if ((k - minn) * c >= a) {
cout << k - minn << " " << minn;
} else
cout << "-1";
}
cout << endl;
}
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
cin>>n;
cout<<n*n*n;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const long long LLINF = 9223372036854775807LL;
int m;
const int mod = 1000000007;
struct matr {
int a[60][60];
matr() { memset(a, 0, sizeof(a)); }
};
matr mul(matr m1, matr m2) {
matr res;
for (int i = 0; i < m; ++i)
for (int j = 0; j < m; ++j)
for (int k = 0; k < m; ++k)
res.a[i][j] = (res.a[i][j] + (long long)m1.a[i][k] * m2.a[k][j]) % mod;
return res;
}
matr pow(matr x, long long p) {
if (p == 0) {
matr res;
for (int i = 0; i < m; ++i) res.a[i][i] = 1;
return res;
} else if (p & 1)
return mul(x, pow(x, p - 1));
else {
matr t = pow(x, p >> 1);
return mul(t, t);
}
}
int g(char c) {
if (c >= 'a' && c <= 'z')
return c - 'a';
else
return c - 'A' + 26;
}
int main() {
long long n;
int k;
cin >> n >> m >> k;
n--;
matr p;
for (int i = 0; i < m; ++i)
for (int j = 0; j < m; ++j) p.a[i][j] = 1;
for (int i = 0; i < k; ++i) {
string s;
cin >> s;
int v1 = g(s[0]);
int v2 = g(s[1]);
p.a[v2][v1] = 0;
}
p = pow(p, n);
int ans = 0;
for (int i = 0; i < m; ++i)
for (int j = 0; j < m; ++j) ans = (ans + p.a[i][j]) % mod;
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ios_base::sync_with_stdio(0);
;
string s;
cin >> s;
vector<int> sol(s.size());
for (int i = 0; i < s.size(); i++) {
int val = s[i] - '0';
sol[i] = val % 3;
}
int ans = 0;
int sum = 0;
int sumMod = 0;
for (int i = 0; i < s.size(); i++) {
sum += sol[i];
sumMod++;
if (sol[i] % 3 == 0 || sum % 3 == 0 || sumMod == 3) {
sumMod = 0;
ans++;
sum = 0;
}
}
cout << ans;
}
| 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.