solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long double eps = 1e-16; const int maxn = 110000; vector<int> a[1000010][2]; int last = 0; vector<int> res; bool rec(int r) { int x = ++last; if (a[x][0].size()) { if (a[x][0][0] <= last) { return false; } if (!rec(a[x][0].back())) { return false; } } res.push_back(x); if (a[x][1].size()) { if (a[x][1][0] <= last) { return false; } if (!rec(max(r, a[x][1].back()))) { return false; } } else if (last < r) { return rec(r); } return true; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int x, y; char s[10]; scanf("%d%d%s", &x, &y, &s); a[x][s[0] != 'L'].push_back(y); } for (int i = 1; i <= n; i++) { for (int j = 0; j < 2; j++) { sort(a[i][j].begin(), a[i][j].end()); } } if (!rec(n)) { printf("IMPOSSIBLE"); } else { for (int i = 0; i < n; i++) { printf("%d ", res[i]); } } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int main() { int t; cin >> t; while (t--) { char s[N]; int num0 = 0, num1 = 0, cnt = 0, p = 0; cin >> s; char c = s[0]; for (int i = 0; s[i]; i++) { if (s[i] == '0') { num0++; } else { num1++; } if (s[i] != c) { cnt++; if (c == '0') { p++; } } c = s[i]; } if (s[strlen(s) - 1] == '0') { p++; } if (p >= 2) { cout << 2 << endl; } else if (p == 1 || num0 == strlen(s)) { cout << 1 << endl; } else if (num1 == strlen(s)) { cout << 0 << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long n, x, y, z, m, p, q, r; long long ara[300000]; long long gcd(long long n, long long m) { if (m == 0) return n; return gcd(m, n % m); } int main() { cin >> n; long long gc = 0; for (long long i = 1; i <= n; i++) scanf("%I64d", &ara[i]), gc = gcd(gc, ara[i]); if (gc > 1) { cout << "YES" << endl << 0 << endl; return 0; } long long ans = 0; for (long long i = 1; i < n; i++) { if (ara[i] % 2 == 0) continue; if (ara[i + 1] % 2 == 1) { ans += 1; ara[i] = ara[i + 1] = 2; } else { ans += 2; ara[i] = ara[i + 1] = 2; } } if (ara[n] % 2 == 1) { ans += 2; } cout << "YES" << endl << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007; const int MM = 5220000; long long int m, n, k, p1, p2, p3, p, t, l, r, f, dem[12000], a[MM], lp[MM], pr[10000]; int sieve(long long int* pb, long long int* pr); long long int powmod(long long int a1, long long int b1, long long int modx) { long long int res = 1; a1 %= modx; for (; b1; b1 >>= 1) { if (b1 & 1) res = res * a1 % modx; a1 = (a1 * a1) % modx; } return res; } long long int nc2(long long int x) { return ((x * (x - 1)) / 2) % mod; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t >> l >> r; int u = sieve(dem, pr); lp[1] = 1; for (int i = 0; i < u; i++) { lp[pr[i]] = pr[i]; for (long long int j = pr[i] * pr[i]; j <= r; j += pr[i]) { if (!lp[j]) lp[j] = pr[i]; } } for (int i = 0; i <= r; i++) { if (lp[i] == 0) lp[i] = i; if (lp[i] == i) { a[i] = nc2(i); } } for (int i = 2; i <= r; i++) { if (a[i] == 0) { a[i] = (a[i / lp[i]] + (i / lp[i]) * nc2(lp[i])); if (a[i] > mod) a[i] %= mod; } } long long int ans = 0; long long int o = 1; for (int i = (int)l; i <= r; i++) { ans = (ans + (o * a[i])); o = (o * t) % mod; if (ans > mod) ans %= mod; } cout << ans % mod; return 0; } int sieve(long long int* pb, long long int* pr) { int MPR = 9002; for (int i = 0; i < MPR; i++) pb[i] = 1; for (int i = 2; i < (MPR); i++) if (pb[i] == 1) for (int j = i * i; j <= MPR; j += i) pb[j] = 0; int cur = 0; for (int i = 2; i < (int)(MPR); i++) if (pb[i]) pr[cur++] = i; return cur; }
5
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double pi = 3.141592653589793238462; inline long long sq(long long x) { return x * x; } struct point { long long x, y; int p; }; long long sqdist(point a, point b) { return sq(a.x - b.x) + sq(a.y - b.y); } long long ccw(point a, point b, point c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); } struct comp { point O; bool colinear; comp(point _o, bool c) : O(_o), colinear(c) {} bool operator()(point a, point b) { long long x1 = a.x - O.x; long long y1 = a.y - O.y; long long x2 = b.x - O.x; long long y2 = b.y - O.y; if (x1 * x2 < 0) return x1 < x2; long long cross = x1 * y2 - x2 * y1; if (colinear && cross == 0) { if (y1 * y2 < 0) return y1 < y2; return sqdist(O, a) < sqdist(O, b); } if (x1 == 0 && y1 > 0) return false; if (x2 == 0 && y2 > 0) return true; return cross > 0; } }; point tele[10]; point monsters[1005]; vector<point> pts[10]; int beg[10][1005], exact[10][1005]; int n, k; bool mycomp(int a, int b) { return tele[a].p < tele[b].p; } bitset<1005> afraid; unordered_set<int> killed; bool go(int target, list<int> &perm) { if (perm.empty()) return false; int where = perm.front(); perm.pop_front(); int tpos = exact[where][target]; int spos = beg[where][target]; while (spos < tpos) { if (killed.count(pts[where][spos].p)) { spos++; } else if (!go(pts[where][spos++].p, perm)) { return false; } } killed.insert(target); afraid[target] = true; return true; } int main() { cin >> k >> n; for (int i = 0; i < k; i++) { cin >> tele[i].x >> tele[i].y; tele[i].p = i; } for (int i = 0; i < n; i++) { cin >> monsters[i].x >> monsters[i].y; monsters[i].p = i; } for (int i = 0; i < k; i++) { pts[i].reserve(n); copy(monsters, monsters + n, back_inserter(pts[i])); sort((pts[i]).begin(), (pts[i]).end(), comp(tele[i], true)); } for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { exact[i][j] = lower_bound((pts[i]).begin(), (pts[i]).end(), monsters[j], comp(tele[i], true)) - pts[i].begin(); beg[i][j] = lower_bound((pts[i]).begin(), (pts[i]).end(), monsters[j], comp(tele[i], false)) - pts[i].begin(); } } vector<int> idx(k); for (int target = 0; target < n; target++) { if (afraid[target]) continue; for (int i = 0; i < k; i++) idx[i] = i; do { list<int> perm((idx).begin(), (idx).end()); killed.clear(); if (go(target, perm)) { break; } } while (next_permutation((idx).begin(), (idx).end(), mycomp)); } cout << afraid.count() << endl; return 0; }
9
#include <bits/stdc++.h> using vi = std::vector<long long int>; using vvi = std::vector<vi>; using pii = std::pair<long long int, long long int>; using vpii = std::vector<pii>; using vvpii = std::vector<vpii>; using namespace std; const long long int N = 2e6 + 10; const long long int inf = 1e18 + 10; const long double Pi = 3.14159265; long long int ans = inf; long long int f(long long int n) { if (n < 0) return -1; long long int ans = 0; while (n) { ans += n % 10; n /= 10; } return ans; } void solve() { long long int n, k; cin >> n >> k; ans = inf; long long int num = 0; for (long long int j = 1; j <= 9; j++) for (long long int i = j; i < inf; i = (i * 10) + 9) { for (long long int cnt = i - 30; cnt <= i; cnt++) { if (cnt < 0) continue; num = 0; for (long long int K = 0; K <= k; K++) num += f(cnt + K); if (num == n) ans = min(cnt, ans); } } if (ans == inf) cout << -1; else cout << ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); ; long long int xx = 0; long long int t; cin >> t; while (t--) { ; solve(); cout << "\n"; } return 0; }
7
#include <bits/stdc++.h> using namespace std; int n, m, k, a[55], p, cnt; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); if (m <= k) printf("0"); else { sort(a, a + n); p = n - 1; cnt = k; while (p >= 0) { cnt--; cnt += a[p--]; if (cnt >= m) { printf("%d", n - p - 1); return 0; } } printf("-1"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long n, k, ll; long long mod = 1e9 + 7, qq; long long s[5100][5100], ans; long long ksm(long long a, long long q) { long long ans = 1, b = q, v = a; while (b) { if (b & 1) { ans *= v; ans %= mod; } v *= v; v %= mod; b >>= 1; } return ans; } int main() { s[0][0] = 1; for (int x = 1; x <= 5000; x++) { for (int y = 1; y <= x; y++) { s[x][y] = (s[x - 1][y - 1] % mod + s[x - 1][y] % mod * y % mod) % mod; } } cin >> n >> k; qq = ksm(2, n); ll = 1; long long fan = ksm(2, mod - 2); for (int x = 0; x <= k; x++) { ans = (ans + 1ll * ll * s[k][x] % mod * qq % mod) % mod; ll = ll * (n - x) % mod; qq = 1ll * qq * fan % mod; } if (!k) { ans--; } cout << ans << endl; }
8
#include <bits/stdc++.h> using namespace std; int p[200000 + 100], n, ans[200000 + 100]; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 2; i <= n; i++) cin >> p[i]; int idx = 1, i = p[n]; ans[0] = n; while (1) { ans[idx++] = i; if (i == 1) break; i = p[i]; } for (int i = idx - 1; i >= 0; i--) cout << ans[i] << " "; }
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; void *wmem; char memarr[96000000]; template <class T> inline void walloc1d(T **arr, int x, void **mem = &wmem) { static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void *)(((char *)(*mem)) + skip[((unsigned long long)(*mem)) & 15]); (*arr) = (T *)(*mem); (*mem) = ((*arr) + x); } inline void rd(int &x) { int k; int m = 0; x = 0; for (;;) { k = getchar(); if (k == '-') { m = 1; break; } if ('0' <= k && k <= '9') { x = k - '0'; break; } } for (;;) { k = getchar(); if (k < '0' || k > '9') { break; } x = x * 10 + k - '0'; } if (m) { x = -x; } } inline void rd(double &x) { scanf("%lf", &x); } inline void wt_L(char a) { putchar(a); } inline void wt_L(double x) { printf("%.15f", x); } template <class T> struct LHeap { int *hp; int *place; int size; T *val; void malloc(int N) { hp = (int *)std::malloc(N * sizeof(int)); place = (int *)std::malloc(N * sizeof(int)); val = (T *)std::malloc(N * sizeof(T)); } void walloc(int N, void **mem = &wmem) { walloc1d(&hp, N, mem); walloc1d(&place, N, mem); walloc1d(&val, N, mem); } void free() { std::free(hp); std::free(place); std::free(val); } void init(int N) { int i; size = 0; for (i = 0; i < (N); i++) { place[i] = -1; } } void up(int n) { int m; while (n) { m = (n - 1) / 2; if (val[hp[m]] <= val[hp[n]]) { break; } swap(hp[m], hp[n]); swap(place[hp[m]], place[hp[n]]); n = m; } } void down(int n) { int m; for (;;) { m = 2 * n + 1; if (m >= size) { break; } if (m + 1 < size && val[hp[m]] > val[hp[m + 1]]) { m++; } if (val[hp[m]] >= val[hp[n]]) { break; } swap(hp[m], hp[n]); swap(place[hp[m]], place[hp[n]]); n = m; } } void change(int n, T v) { T f = val[n]; val[n] = v; if (place[n] == -1) { place[n] = size; hp[size++] = n; up(place[n]); } else { if (f < v) { down(place[n]); } else if (f > v) { up(place[n]); } } } int pop(void) { int res = hp[0]; place[res] = -1; size--; if (size) { hp[0] = hp[size]; place[hp[0]] = 0; down(0); } return res; } }; int N; int L; int X; int Y; double S; double w[400000]; double s[400000]; double a[400000]; int sz; int main() { int Lj4PdHRW; wmem = memarr; int i; int k; double gn; double nd; double y; double res = 0; LHeap<double> hp; i = 0; rd(N); rd(L); for (Lj4PdHRW = 0; Lj4PdHRW < (N); Lj4PdHRW++) { rd(X); rd(Y); rd(S); if (i < X) { w[sz] = X - i; s[sz++] = 0; } w[sz] = Y - X; s[sz++] = S; i = Y; } if (i < L) { w[sz] = L - i; s[sz++] = 0; } for (i = 0; i < (sz); i++) { a[i] = 1; } hp.walloc(sz); hp.init(sz); for (k = 0; k < (sz); k++) { while (hp.size) { i = hp.hp[0]; if (s[i] <= s[k]) { break; } gn = w[i] / s[i] - w[i] / (s[i] + a[i]) * (1 - a[i]); nd = w[k] / (s[k] + 2) + w[k] / (s[k] + a[k]) * (1 - a[k]); if (nd < gn) { a[k] = 2; y = w[i] / (s[i] + a[i]) * (1 - a[i]) + nd; a[i] = (w[i] - y * s[i]) / (w[i] + y); break; } else { a[i] = 0; y = w[k] / (s[k] + a[k]) * (1 - a[k]) - gn; a[k] = (w[k] - y * s[k]) / (w[k] + y); hp.pop(); } } if (a[k]) { hp.change(k, -s[k]); } } for (i = 0; i < (sz); i++) { res += w[i] / (s[i] + a[i]); } wt_L(res); wt_L('\n'); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int max6 = 1e6 + 6; char s[3][max6]; int f[max6], g[max6]; int main() { scanf("%s\n%s", s[1] + 1, s[2] + 1); int n = strlen(s[1] + 1); int m = strlen(s[2] + 1); int j = 1; for (int i = 1; i <= m; ++i) { while (j <= n && s[1][j] != s[2][i]) j++; f[i] = j; j++; } j = n; for (int i = m; i >= 1; --i) { while (j >= 1 && s[1][j] != s[2][i]) j--; g[i] = j; j--; } int res = 0; for (int i = 1; i <= m; ++i) cerr << f[i] << " " << g[i] << "\n"; for (int i = 0; i <= m; ++i) res = max(res, g[i + 1] - f[i] - 1); res = max(res, n - f[m]); res = max(res, g[1] - 1); cout << res; }
4
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int INF = 2e9; const long long INFLL = 1e18; const int MAX_N = 2010; int N, M; long long dp[MAX_N + 1][MAX_N + 1][2]; long long sum[MAX_N + 1][MAX_N + 1][2]; string str[MAX_N + 1]; int cnt[MAX_N + 1][MAX_N + 1][2]; int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N; i++) { cin >> str[i]; for (int j = 0; j < M; j++) { if (str[i][j] == 'R') { cnt[i][j][0] = cnt[i][j][1] = 1; } } } for (int i = N - 1; i >= 0; i--) { for (int j = M - 1; j >= 0; j--) { cnt[i][j][0] += cnt[i + 1][j][0]; cnt[i][j][1] += cnt[i][j + 1][1]; } } for (int i = N - 1; i >= 0; i--) { for (int j = M - 1; j >= 0; j--) { if (i == N - 1 && j == M - 1) { dp[i][j][0] = dp[i][j][1] = 1; } else if (i == N - 1) { if (cnt[i][j + 1][1] == 0) { dp[i][j][1] = 1; } } else if (j == M - 1) { if (cnt[i + 1][j][0] == 0) { dp[i][j][0] = 1; } } else { int t = cnt[i + 1][j][0]; if (0) cout << t << " "; dp[i][j][0] = (sum[i + 1][j][0] - sum[N - t][j][0] + MOD) % MOD; if (0) cout << t << " "; t = cnt[i][j + 1][1]; dp[i][j][1] = (sum[i][j + 1][1] - sum[i][M - t][1] + MOD) % MOD; } sum[i][j][0] = dp[i][j][1]; if (i != N - 1) sum[i][j][0] += sum[i + 1][j][0]; sum[i][j][1] = dp[i][j][0]; if (j != M - 1) sum[i][j][1] += sum[i][j + 1][1]; if (0) cout << i << " " << j << " " << dp[i][j][0] << " " << dp[i][j][1] << " " << sum[i][j][0] << " " << sum[i][j][1] << " " << cnt[i][j][0] << " " << cnt[i][j][1] << endl; } } if (N == 1 && M == 1) { if (str[0][0] == 'R') { printf("0"); } else { printf("1"); } return 0; } cout << (dp[0][0][0] + dp[0][0][1]) % MOD; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n, v, k1 = 0, k2 = 0; int how_many1 = 0; vector<int> molt[99 + 1]; scanf("%d", &n); short b[n << 1]; for (int i = 0; i < (n << 1); i++) { scanf("%d", &v); molt[v].push_back(i); } for (int v = 10; v <= 99; v++) if (molt[v].size() >= 2) { k1++; k2++; how_many1++; b[molt[v][0]] = 1; b[molt[v][1]] = 2; } for (int v = 10; v <= 99; v++) if (molt[v].size() == 1) { if (k1 <= k2) { k1++; how_many1++; b[molt[v][0]] = 1; } else { k2++; b[molt[v][0]] = 2; } } for (int v = 10; v <= 99; v++) { for (int i = 2; i < molt[v].size(); i++) { if (how_many1 < n) { b[molt[v][i]] = 1; how_many1++; } else { b[molt[v][i]] = 2; } } } printf("%d\n", k1 * k2); for (int i = 0; i < 2 * n; i++) printf("%d ", b[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long n, k, a[555][555], D[250250], area = 0, cur = 0, A[250250], S[555][555], N; long long dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}, mom[555][555]; void over(int i, int j) { if (i < 1 || j < 1 || i > n || j > n) return; if (a[i][j] == 0 || mom[i][j] != 0) return; mom[i][j] = N; for (int k = 0; k < 4; ++k) { over(i + dx[k], j + dy[k]); } } void add(int i, int j) { if (i < 1 || j < 1 || i > n || j > n) return; if (a[i][j] == 1) { ++D[mom[i][j]]; if (D[mom[i][j]] == 1) { cur += A[mom[i][j]]; } } } void del(int i, int j) { if (i < 1 || j < 1 || i > n || j > n) return; if (a[i][j] == 1) { --D[mom[i][j]]; if (D[mom[i][j]] == 0) { cur -= A[mom[i][j]]; } } } int main() { cin >> n >> k; getchar(); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { a[i][j] = getchar() == '.'; S[i][j] = (1 - a[i][j]) + S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1]; } getchar(); } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (a[i][j] == 1 && mom[i][j] == 0) { ++N; over(i, j); } if (mom[i][j] > 0) { ++A[mom[i][j]]; } } } for (int h = k; h <= n + 1; ++h) { cur = 0; memset(D, 0, sizeof D); for (int j = 1; j <= k; ++j) { for (int i = h; h - i + 1 <= k + 2; --i) { add(i, j); } } for (int j = k + 1; j <= n + 1; ++j) { for (int i = h; h - i + 1 <= k + 2; --i) { add(i, j); del(i, j - (k + 2)); } del(h, j); del(h - (k + 1), j); del(h, j - (k + 1)); del(h - (k + 1), j - (k + 1)); if (cur + (S[h - 1][j - 1] - S[h - 1][j - (k + 1)] - S[h - (k + 1)][j - 1] + S[h - (k + 1)][j - (k + 1)]) > area) { area = cur + (S[h - 1][j - 1] - S[h - 1][j - (k + 1)] - S[h - (k + 1)][j - 1] + S[h - (k + 1)][j - (k + 1)]); } add(h, j); add(h - (k + 1), j); add(h, j - (k + 1)); add(h - (k + 1), j - (k + 1)); } } cout << area << "\n"; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, x; cin >> n; if (n % 2 == 0) { x = 2; for (int i = 0; i < n / 2 - 1; i++) { x = x + (i + 1) * 2 + 2; } } else { x = 1; for (int i = 0; i < (n - 1) / 2; i++) { x = x + i * 2 + 3; } } cout << x << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; #define ll long long #define mp(a,b) make_pair(a,b) #define F first #define S second #define pb(x) push_back(x) #define N 200005 #define MOD 1000000007 int main() { int t; cin>>t; for(int q=0;q<t;q++){ ll n,m,i,x,y; cin>>n; vector<pair<ll,ll> >v(n); vector<ll>l,r; for(i=0;i<n;i++){ cin>>v[i].F>>v[i].S; l.pb(v[i].F); r.pb(v[i].S); } ll ans=n; sort(l.begin(),l.end()); sort(r.begin(),r.end()); for(i=0;i<n;i++){ x=upper_bound(l.begin(),l.end(),v[i].S)-l.begin(); x=n-x; y=lower_bound(r.begin(),r.end(),v[i].F)-r.begin(); ans=min(ans,x+y); } cout<<ans<<"\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int test = 0; test < t; test++) { int n, M; cin >> n >> M; vector<vector<pair<int, int> > > v(26, vector<pair<int, int> >()); map<pair<int, int>, int> m; char c; for (int i = 0; i < n; i++) { for (int j = 0; j < M; j++) { cin >> c; if (c != '.') { m[make_pair(i, j)] = (int)c - 'a'; v[c - 'a'].emplace_back(i, j); } } } bool good = true; for (int i = 0; i < 26; i++) { if (v[i].size() > 1) { if (v[i][0].first == v[i][1].first) { for (int j = 1; j < v[i].size(); j++) { if (v[i][j].first != v[i][j - 1].first or v[i][j].second < v[i][j - 1].second) { good = false; } else { for (int from = v[i][j - 1].second; from <= v[i][j].second; from++) { if (m.find(make_pair(v[i][j].first, from)) == m.end()) { good = false; } else { if (m[make_pair(v[i][j].first, from)] < i) { good = false; } } } } } } else { for (int j = 1; j < v[i].size(); j++) { if (v[i][j].second != v[i][j - 1].second or v[i][j].first < v[i][j - 1].first) { good = false; } else { for (int from = v[i][j - 1].first; from <= v[i][j].first; from++) { if (m.find(make_pair(from, v[i][j].second)) == m.end()) { good = false; } else { if (m[make_pair(from, v[i][j].second)] < i) { good = false; } } } } } } } } int last = -1; for (int i = 0; i < 26; i++) { if (!v[i].empty()) { last = i; } } if (last == -1) { cout << "YES" << endl; cout << 0 << endl; continue; } if (good) { cout << "YES" << endl; cout << last + 1 << endl; for (int i = 0; i <= last; i++) { if (v[i].empty()) { cout << v[last][0].first + 1 << " " << v[last][0].second + 1 << " "; cout << v[last][0].first + 1 << " " << v[last][0].second + 1 << endl; } else { cout << v[i][0].first + 1 << " " << v[i][0].second + 1 << " "; cout << v[i][v[i].size() - 1].first + 1 << " " << v[i][v[i].size() - 1].second + 1 << endl; } } } else { cout << "NO" << endl; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; template <typename T> inline T gcd(T a, T b) { while (b != 0) swap(b, a %= b); return a; } int main() { string s; cin >> s; int n = (int)s.length(); long long ans = (1 << n) - 2; for (int i = n - 1; i >= 0; --i) { if (s[i] == '7') ans += (1 << (n - i - 1)); } cout << ans + 1; }
1
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 9; const long long mod = 998244353; int a[N], stk[N], stk2[N], top, top2, t[N], b[N], c[N], ans[N][3], ans2[N][4]; set<int> s; bool cmp(int u, int v) { return a[u] < a[v]; } bool cmp2(int u, int v) { return a[u] > a[v]; } int main() { int n, q; scanf("%d%d", &n, &q); for (int i = (1); i < (n + 1); ++i) scanf("%d", &a[i]); s.insert(n + 1); int r1 = 0, r2 = 0; for (int i = (n + 1) - 1; i >= (1); --i) { while (top) { int p = stk[top]; if (a[p] > a[i]) { --top; --t[p]; if (!t[p]) s.insert(p); r1 = 0; } else { break; } } while (top2) { int p = stk2[top2]; if (a[p] < a[i]) { --top2; --t[p]; if (!t[p]) s.insert(p); r2 = 0; } else { break; } } b[i] = i + max(r1, r2) + 1; ans[i][0] = i, ans[i][1] = b[i] - 1, ans[i][2] = b[i]; int s1 = lower_bound(stk + 1, stk + 1 + top, i, cmp) - stk - 1, s2 = lower_bound(stk2 + 1, stk2 + 1 + top2, i, cmp2) - stk2 - 1; if (s1 && s2) { c[i] = *s.lower_bound(max(stk[s1], stk2[s2])); if (c[i] <= n) { int u = lower_bound(stk + 1, stk + 1 + top, c[i], greater<int>()) - stk, v = lower_bound(stk2 + 1, stk2 + 1 + top2, c[i], greater<int>()) - stk2; ans2[i][0] = i; ans2[i][1] = min(stk[u], stk2[v]); ans2[i][2] = max(stk[u], stk2[v]); ans2[i][3] = c[i]; } } else c[i] = n + 1; stk[++top] = i; stk2[++top2] = i; ++r1, ++r2; t[i] += 2; if (i < n && b[i] > b[i + 1]) { b[i] = b[i + 1]; for (int j = (0); j < (3); ++j) ans[i][j] = ans[i + 1][j]; } if (i < n && c[i] > c[i + 1]) { c[i] = c[i + 1]; for (int j = (0); j < (4); ++j) ans2[i][j] = ans2[i + 1][j]; } } while (q--) { int l, r; scanf("%d%d", &l, &r); if (c[l] <= r) { printf("4\n"); for (int i = (0); i < (4); ++i) printf("%d ", ans2[l][i]); } else if (b[l] <= r) { printf("3\n"); for (int i = (0); i < (3); ++i) printf("%d ", ans[l][i]); } else printf("0"); printf("\n"); } }
11
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; int res = 0; if (a) { res++; a--; } if (b) { res++; b--; } if (c) { res++; c--; } if (a >= b && a >= c) { if (a && b) { res++; a--; b--; } if (a && c) { res++; a--; c--; } if (b && c) { res++; b--; c--; } } else if (b >= a && b >= c) { if (a && b) { res++; a--; b--; } if (b && c) { res++; b--; c--; } if (a && c) { res++; a--; c--; } } else { if (b && c) { res++; b--; c--; } if (a && c) { res++; a--; c--; } if (a && b) { res++; a--; b--; } } if (a && b && c) { res++; } cout << res << endl; } }
0
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& out, const pair<T1, T2> p) { out << '(' << p.first << ',' << p.second << ')'; return out; } template <class T1, class T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <class T> istream& operator>>(istream& in, vector<T>& v) { for (T& x : v) in >> x; return in; } template <class T> ostream& operator<<(ostream& out, const vector<vector<T>>& v) { for (const vector<T>& x : v) out << x << '\n'; return out; } template <class T> ostream& operator<<(ostream& out, const vector<T>& v) { for (const T& x : v) out << x << ' '; return out; } long long gcd(long long a, long long b) { if (b > a) swap(a, b); return (b ? gcd(b, a % b) : a); } using ll = long long; using pii = pair<int, int>; using pll = pair<long long, long long>; using tiii = pair<pair<int, int>, int>; using vi = vector<int>; using vl = vector<long long>; using vvi = vector<vector<int>>; using vvl = vector<vector<long long>>; const int h = 1000000007; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cerr.setstate(ios::failbit); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; string s; cin >> s; if (k & 1) { cout << "NO\n"; continue; } bool ok = true; for (int i = (0); i < (k); i++) { set<int> st; for (int j = i; j < n; j += k) { if (s[j] != '?') st.insert(s[j]); } if (st.size() > 1) { ok = false; break; } else if (st.size() == 1) { for (int j = i; j < n; j += k) { s[j] = *st.begin(); } } } if (!ok) { cout << "NO\n"; continue; } int z0 = count(s.begin(), s.begin() + k, '0'); int z1 = count(s.begin(), s.begin() + k, '1'); cout << (z0 <= k / 2 and z1 <= k / 2 ? "YES" : "NO") << '\n'; } }
3
#include <bits/stdc++.h> using namespace std; int n; vector<string> v, v1; bool check; int main() { cin >> n; v.resize(n); v1.resize(n); for (int i = 0; i < n; i++) { cin >> v[i]; v1[i] = v[i]; if (i && v[i].size() < v[i - 1].size()) check = 1; } if (check) { cout << "NO"; return 0; } for (int i = 0; i < v[0].size(); i++) { if (v[0][i] == '?') { if (i) v[0][i] = '0'; else v[0][i] = '1'; } } for (int i = 0; i < n - 1; i++) { if (v[i + 1].size() > v[i].size()) { for (int j = 0; j < v[i + 1].size(); j++) { if (v[i + 1][j] == '?') { if (j) v[i + 1][j] = '0'; else v[i + 1][j] = '1'; } } } else { int ind = -1; for (int j = 0; j < v[i].size(); j++) { if (v[i + 1][j] == '?') { v[i + 1][j] = v[i][j]; if (v[i][j] != '9') ind = j; } if (j < v[i].size() && v[i + 1][j] > v[i][j]) { for (j++; j < v[i].size(); j++) if (v[i + 1][j] == '?') v[i + 1][j] = '0'; } if (j < v[i].size() && v[i][j] > v[i + 1][j]) { if (ind > -1) { v[i + 1][ind]++; for (j = ind + 1; j < v[i].size(); j++) if (v1[i + 1][j] == '?') v[i + 1][j] = '0'; } else { cout << "NO"; return 0; } } } check = 1; for (int j = 0; j < v[i].size(); j++) if (v[i][j] != v[i + 1][j]) check = 0; if (check) { if (ind == -1) { cout << "NO"; return 0; } else { v[i + 1][ind]++; for (int j = ind + 1; j < v[i].size(); j++) { if (v1[i + 1][j] == '?') v[i + 1][j] = '0'; } } } } } cout << "YES" << endl; for (int i = 0; i < n; i++) cout << v[i] << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long res, p2[510], preq[510][510], f; int n, k, r; int main() { cin >> n >> k; res = 0; p2[0] = 1; for (int i = 1; i <= n; i++) { p2[i] = (p2[i - 1] * 2LL) % 998244353LL; } for (int i = 1; i < 510; i++) { for (int j = 1; j < 510; j++) { preq[i][j] = 0; } } for (int i = 0; i < 510; i++) { preq[0][i] = -1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { for (int v = 1; v <= i - j + 1; v++) { f = ((p2[v - 1] - preq[v - 1][j]) * p2[i - v - j + 1]) % 998244353LL; preq[i][j] = (preq[i][j] + f + 998244353LL) % 998244353LL; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if ((i * j) < k) { long long pi = (preq[n][i] - preq[n][i + 1] + 998244353LL) % 998244353LL; long long pj = (preq[n][j] - preq[n][j + 1] + 998244353LL) % 998244353LL; res = (res + (long long)(pi * pj)) % 998244353LL; } } } res = (res * 499122177LL) % 998244353LL; cout << res << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 2000010; int in[N / 10]; int a[N]; void add(int x, int k) { while (x <= N) { a[x] += k; x += x & -x; } } int ask(int x) { int ans = 0; while (x) { ans += a[x]; x ^= x & -x; } return ans; } int main() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; ++i) cin >> in[i], in[i] += m + 1; sort(in + 1, in + 1 + n); int ans = 0; for (int i = 1; i <= n; ++i) { add(in[i], 1); if (ask(in[i]) - ask(in[i] - m) >= k) { ++ans; add(in[i], -1); } } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; const unsigned long long m = 1e9 + 7; int l1, r1, x1; unsigned long long pp(unsigned long long a, unsigned long long b) { if (b == 0) return 1; if (b % 2 == 0) { return ((pp(a, b / 2) % m) * (pp(a, b / 2) % m)) % m; } else { return (a * (((pp(a, b / 2) % m) * (pp(a, b / 2) % m)) % m)) % m; } } int main() { unsigned long long x, k; cin >> x >> k; if (x == 0) { cout << 0; return 0; } if (k == 0) { cout << (2 * (x % m)) % m; return 0; } unsigned long long t = (((pp(2, k + 1)) * (x % m)) % m - pp(2, k) + 1 + 2 * m) % m; cout << t; return 0; }
4
#include <bits/stdc++.h> using namespace std; int quick_pow(int a, int b, int MOD) { a %= MOD; int res = 1; while (b) { if (b & 1) res = (res * a) % MOD; b /= 2; a = (a * a) % MOD; } return res; } const int maxn = 300000 + 10; const int MOD = 1e9 + 7; int L[maxn], R[maxn], l[maxn], r[maxn]; vector<int> p, vec[maxn * 2]; multiset<int> S; int cmp(int i, int j) { if (L[i] == L[j]) return R[i] < R[j]; return L[i] < L[j]; } int getHash(int x) { return upper_bound(p.begin(), p.end(), x) - p.begin(); } int main() { int i, j, n, k; while (scanf("%d%d", &n, &k) != EOF) { p.clear(); for (i = 1; i <= n; i++) { scanf("%d%d", &L[i], &R[i]); p.push_back(L[i]); p.push_back(R[i]); } sort(p.begin(), p.end()); p.erase(unique(p.begin(), p.end()), p.end()); int m = p.size(); for (i = 0; i <= m; i++) { vec[i].clear(); } for (i = 1; i <= n; i++) { l[i] = getHash(L[i]); r[i] = getHash(R[i]); vec[l[i]].push_back(r[i]); } int res = 0, ansl = 0, ansr = 0; S.clear(); for (i = 1; i <= m; i++) { for (j = 0; j < vec[i].size(); j++) S.insert(vec[i][j]); while (S.size() > k) S.erase(S.begin()); if (S.size() == k && *S.begin() >= i) { int temp = p[*S.begin() - 1] - p[i - 1] + 1; if (res < temp) { res = temp; ansr = p[*S.begin() - 1]; ansl = p[i - 1]; } } } printf("%d\n", res); if (res == 0) for (i = 1; i <= k; i++) printf("%d%c", i, " \n"[i == k]); else { for (i = 1; k; i++) { if (L[i] <= ansl && R[i] >= ansr) { printf("%d ", i); k--; } } puts(""); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { priority_queue<int> pq; int n, x; scanf("%d", &n); long long res = 0; for (int i = 0; i < n; i++) { scanf("%d", &x); if (!pq.empty() && -pq.top() < x) res += x - (-pq.top()), pq.pop(), pq.push(-x); pq.push(-x); } printf("%lld\n", res); return 0; }
8
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int N = 2e5 + 5; vector<int> v[N]; int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; int ans = 1e9; vector<int> a(n); for (int i = 0; i < n; i++) { int t; cin >> t; int c = 0; while (t) { v[t].push_back(c); t >>= 1; c++; } } for (int i = 0; i < N; i++) { sort(v[i].begin(), v[i].end()); if (v[i].size() >= k) { int cur = 0; for (int j = 0; j < k; j++) { cur += v[i][j]; } ans = min(ans, cur); } } cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; char a[100010]; int b[26]; int main() { int n; memset(a, 0, sizeof(a)); while (scanf("%d %s", &n, a) != EOF) { bool flag = 1; memset(b, 0, sizeof(b)); for (int i = 0; a[i]; i++) { b[a[i] - 'a']++; } int k = 0; memset(a, 0, sizeof(a)); for (int i = 0; i < 26; i++) { if (b[i] % n != 0) { flag = 0; } else { int x = b[i] / n; while (x) { a[k++] = char('a' + i); x--; } } } if (flag) { for (int i = 0; i < n; i++) printf("%s", a); cout << endl; } else cout << "-1" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { if (y == 0) return x; else return gcd(y, x % y); } long long expo(long long n, long long m) { long long r = 1; while (m > 0) { if (m % 2) r = (r * n) % 1000000007; n = (n * n) % 1000000007; m = m / 2; } return r % 1000000007; } int n, m, i, x, y, j; int a[100005], l[100005], r[100005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> a[i]; r[i] = l[i] = i; } for (i = n; i > 1; i--) { if (a[i] >= a[i - 1]) { r[i - 1] = r[i]; } if (a[n - i + 1] >= a[n - i + 2]) { l[n - i + 2] = l[n - i + 1]; } } for (i = 1; i <= m; i++) { cin >> x >> y; if (r[x] >= l[y]) { cout << "Yes" << endl; } else cout << "No" << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; int a, b; vector<pair<int, int>> dominos; scanf("%d", &n); for (register int i = 0; i < n; ++i) { scanf("%d %d", &a, &b); dominos.push_back({a, b}); } int fh = 0; int sh = 0; bool possible = false; for (register int i = 0; i < n; ++i) { fh += dominos[i].first; sh += dominos[i].second; if (!possible) { if ((dominos[i].first + dominos[i].second) % 2) { possible = true; } } } if ((fh + sh) % 2) { printf("-1\n"); } else { if (fh % 2 && sh % 2) { if (possible) { printf("1\n"); } else { printf("-1\n"); } } else { printf("0\n"); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int num[200010], n, m, many, ans = 1; int qpow(int x, int y) { int tmp = 1; while (y) { if (y & 1) tmp = 1ll * x * tmp % 998244353; y >>= 1, x = 1ll * x * x % 998244353; } return tmp; } int main() { scanf("%d%d%d", &n, &m, &many); for (int i = 1; i <= m; i++) scanf("%d", &num[i]); sort(num + 1, num + m + 1); for (int i = 1; i <= m; i++) ans = 1ll * ans * ((1ll * qpow(many, num[i] - num[i - 1]) * (qpow(many, num[i] - num[i - 1]) - 1) % 998244353 * qpow(2, 998244353 - 2) % 998244353 + qpow(many, num[i] - num[i - 1])) % 998244353) % 998244353; ans = 1ll * ans * qpow(many, (n - 2 * num[m])) % 998244353, printf("%d\n", ans); }
7
#include <bits/stdc++.h> using namespace std; int n; int a[600]; queue<pair<int, char> > moves; int comi = 0; bool tenta(int ini, int fim) { int indMaior = ini; int ultMaior = ini; bool continuo = true; for (int i = ini + 1; i <= fim; i++) { if (a[i] > a[indMaior]) { indMaior = ultMaior = i; continuo = true; } else if (a[i] == a[indMaior] && continuo) ultMaior = i; else continuo = false; } if (indMaior == ini && ultMaior == fim) { return false; } while (indMaior > ini) { moves.push(make_pair(indMaior - comi + 1, 'L')); indMaior--; ultMaior = indMaior; fim--; } while (ultMaior < fim) { moves.push(make_pair(ultMaior - comi + 1, 'R')); indMaior = ultMaior; fim--; } while (indMaior > ini) { moves.push(make_pair(indMaior - comi + 1, 'L')); indMaior--; ultMaior = indMaior; fim--; } return true; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int k; scanf("%d", &k); bool ok = true; int pos = 0; int j; int b[600]; for (int i = 0; i < k; i++) { scanf("%d", &b[i]); if (!ok) continue; int soma = 0; for (j = pos; j < n; j++) { soma += a[j]; if (soma >= b[i]) break; } if (soma != b[i]) ok = false; else { if (pos != j) ok = tenta(pos, j); comi += j - pos; pos = j + 1; } } if (!ok || pos < n) printf("NO\n"); else { printf("YES\n"); while (!moves.empty()) { printf("%d %c\n", moves.front().first, moves.front().second); moves.pop(); } } }
5
#include <bits/stdc++.h> using namespace std; int a[111][2]; int dp[111][2][3004]; int main() { int N, L; cin >> N >> L; for (int i = 1; i <= N; i++) { cin >> a[i][0] >> a[i][1]; } dp[0][0][0] = 1; int ans = 0; for (int l = 1; l <= L; l++) { for (int i = 1; i <= N; i++) { for (int t1 = 0; t1 < 2 - (a[i][0] == a[i][1]); t1++) { for (int j = 0; j <= N; j++) { if (i == j) continue; for (int t2 = 0; t2 < 2; t2++) { if (l < a[i][t1]) continue; if (!j || a[i][t1] == a[j][t2 ^ 1]) { dp[i][t1][l] += (dp[j][t2][l - a[i][t1]]); dp[i][t1][l] %= 1000000007; } } } if (l == L) { ans = (ans + dp[i][t1][l]) % 1000000007; } } } } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; struct trie { trie* ch[2]; long long b; trie() { for (int i = 0; i < 2; i++) ch[i] = NULL; b = 0; } void add(long long x, long long i) { b++; if (i == -1) { return; } long long z = (long long)1 << i; z = z & x; if (z != 0) z = 1; if (ch[z] == NULL) ch[z] = new trie(); ch[z]->add(x, i - 1); } void del(long long x, long long i) { b--; if (i == -1) { return; } long long z = (long long)1 << i; z = z & x; if (z != 0) z = 1; ch[z]->del(x, i - 1); if (ch[z]->b == 0) { ch[z] = NULL; } } long long cal(long long x, long long i) { if (i == -1) { return 0LL; } long long z = (long long)1 << i; z = z & x; if (z != 0) z = 1; long long r = 0; if (ch[!z] != NULL) { r = (long long)1 << i; r = r | ch[!z]->cal(x, i - 1); } else { r = 0; r = r | ch[z]->cal(x, i - 1); } return r; } }; long long dp[100100]; int main() { int n; cin >> n; dp[0] = 0; long long a[n + 1]; for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); dp[i] = dp[i - 1] ^ a[i]; } trie root; long long maxx = 0; for (int i = 0; i <= n; i++) { root.add(dp[i], 50); maxx = max(maxx, dp[i]); } maxx = max(maxx, root.cal(0, 50)); long long r = 0; for (int i = n; i >= 1; i--) { r = r ^ a[i]; root.del(dp[i], 50); maxx = max(maxx, root.cal(r, 50)); } cout << maxx; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1050; const int INF = 1000000007; template <class T> void byebye(T _rpl) { cout << _rpl << endl; exit(0); } int nextint() { int x; scanf("%d", &x); return x; } long long nextll() { long long x; scanf("%lld", &x); return x; } template <class T> T emax(T& t1, T t2) { if (t1 < t2) t1 = t2; return t1; } template <class T> T emin(T& t1, T t2) { if (t1 > t2) t1 = t2; return t1; } int read() { int f = 1, ret = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'); c = getchar(); } return ret * f; } void write(int x) { if (x < 0) { putchar('-'); write(-x); return; } if (x < 10) { putchar(x + 48); return; } write(x / 10); putchar(x % 10 + 48); } int n; int s[maxn]; int cnt[maxn]; int main() { int t = read(); while (t--) { int ans = -1; n = read(); for (int i = 0; i < n; ++i) s[i] = read(); for (int i = 1; i <= 1024; ++i) { fill(cnt, cnt + maxn, 0); bool flag = true; for (int j = 0; j < n; ++j) { if ((s[j] ^ i) > 1024) { flag = false; break; } cnt[s[j]]++, cnt[s[j] ^ i]--; } for (int j = 0; j < n; ++j) if (cnt[s[j]] != 0) flag = false; if (flag) { ans = i; break; } } printf("%d\n", ans); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int num[10] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1}; int main() { int n; scanf("%d", &n); if (n > 18 * 2) printf("-1\n"); else { for (int i = 0; i < n / 2; i++) printf("8"); if (n % 2 == 0) printf("\n"); else printf("4\n"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int i, j, k, l, m, n; cin >> n; string s; if (n == 1 || n == 2) { cout << -1; return 0; } else if (n == 3) { cout << 210; return 0; } long long int a[7] = {0, 200, 110, 50, 80, 170, 20}; n--; for (i = 1; i <= 6; i++) { if ((n - i) % 6 == 0) { cout << 1; for (j = 1; j <= n - 3; j++) { cout << 0; } if (i == 1) { cout << "200"; } else if (i == 2) { cout << "110"; } else if (i == 3) { cout << "050"; } else if (i == 4) { cout << "080"; } else if (i == 5) { cout << "170"; } else if (i == 6) { cout << "020"; } return 0; } } cout << -1; }
3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, mod = 1e9 + 7, inv2 = (mod + 1) / 2; int ksm(int b, int n) { int res = 1; while (n) { if (n & 1) res = 1ll * res * b % mod; b = 1ll * b * b % mod; n >>= 1; } return res; } struct segment { int l, r, type; bool operator<(const segment& b) const { if (r == b.r) return l > b.l; return r < b.r; } } a[N], b[N], c[N]; int f[N], s[N], sum[2][N]; void merge(int n, int m) { int i = 1, j = 1, k = 1; while (i <= n && j <= m) { if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } while (i <= n) c[k++] = a[i++]; while (j <= m) c[k++] = b[j++]; } int Find(int i, int val) { int p = 0, l = 1, r = i; for (int cnt = 0; cnt <= 100; ++cnt) { int mid = (l + r) >> 1; if (c[mid].r > val) p = mid, r = mid - 1; else l = mid + 1; } return p; } signed main() { ios::sync_with_stdio(false); int k, n, m; cin >> k >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i].l >> a[i].r, a[i].l--, a[i].type = 0; for (int i = 1; i <= m; ++i) cin >> b[i].l >> b[i].r, b[i].l--, b[i].type = 1; sort(a + 1, a + n + 1), sort(b + 1, b + m + 1); for (int i = 1; i <= n; ++i) a[i].l = max(a[i - 1].l, a[i].l); for (int i = 1; i <= m; ++i) b[i].l = max(b[i - 1].l, b[i].l); merge(n, m); int ans = 1; for (int i = 1; i <= n + m; ++i) { int t1 = mod - ksm(inv2, c[i].r - c[i].l), t2 = mod - ksm(inv2, c[i].r); int p = Find(i, c[i].l); f[i] = (1ll * t1 * ((p == 0 ? 0 : s[p - 1]) + 1) + 1ll * t2 * (p == i ? 0 : (mod + sum[c[i].type][i - 1] - sum[c[i].type][p - 1]))) % mod; s[i] = (s[i - 1] + f[i]) % mod; sum[!c[i].type][i] = sum[!c[i].type][i - 1]; sum[c[i].type][i] = (sum[c[i].type][i - 1] + 1ll * f[i] * ksm(2, c[i].r)) % mod; ans = (ans + f[i]) % mod; } cout << 1ll * ans * ksm(2, k) % mod; return 0; }
10
#include <bits/stdc++.h> using namespace std; int n; int a[1000001]; int d[2097152]; unordered_map<int, int> t; vector<int> v[1000001]; int m; vector<pair<int, int> > queries[1000001]; int ans[1000001]; int dd[1000001]; void change(int i, int x) { d[i] = x; while (i / 2 > 0) { i /= 2; d[i] = d[i * 2] ^ d[i * 2 + 1]; } } int rmq(int v, int vl, int vr, int l, int r) { if (vl > r || vr < l) { return 0; } if (l <= vl && vr <= r) { return d[v]; } return rmq(v * 2, vl, (vl + vr) / 2, l, r) ^ rmq(v * 2 + 1, (vl + vr) / 2 + 1, vr, l, r); } int main() { cin >> n; int ss = 1; while (ss < n) { ss *= 2; } for (int i = 0; i < n; i++) { scanf("%d", &a[i]); v[t[a[i]]].push_back(i); t[a[i]] = i + 1; dd[i] = a[i]; } cin >> m; for (int i = 0; i < m; i++) { int l, r; scanf("%d %d", &l, &r); l--; r--; queries[l].push_back(make_pair(r, i)); } for (int i = 1; i < n; i++) { dd[i] ^= dd[i - 1]; } for (int i = 0; i < n; i++) { for (int j = 0; j < (int)v[i].size(); j++) { change(ss + v[i][j], a[v[i][j]]); } for (int j = 0; j < (int)queries[i].size(); j++) { ans[queries[i][j].second] = rmq(1, 1, ss, i + 1, queries[i][j].first + 1); ans[queries[i][j].second] ^= dd[queries[i][j].first] ^ (i != 0 ? dd[i - 1] : 0); } } for (int i = 0; i < m; i++) { printf("%d\n", ans[i]); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int col[200005], dp[200005], ans[200005]; vector<vector<int> > g; bool vis[200005]; void dfs1(int v) { vis[v] = true; for (int i = 0; i < g[v].size(); i++) { if (!vis[g[v][i]]) { dfs1(g[v][i]); dp[v] += max(0, dp[g[v][i]]); } } if (col[v] == 1) { dp[v]++; } else { dp[v]--; } } void dfs2(int v, int p) { vis[v] = true; ans[v] = dp[v]; for (int i = 0; i < g[v].size(); i++) { if (!vis[g[v][i]]) { dp[v] -= max(0, dp[g[v][i]]); dp[g[v][i]] += max(0, dp[v]); dfs2(g[v][i], v); dp[g[v][i]] -= max(0, dp[v]); dp[v] += max(0, dp[g[v][i]]); ; } } } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> col[i]; } g.resize(n + 1); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs1(1); for (int i = 1; i <= n; i++) { vis[i] = false; } dfs2(1, 1); for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> sq[5000]; stack<int> st; int dfn[5000], low[5000], n, cnt = 0, tim = 0, color[5000]; bool in[5000]; void tarjan(int u) { tim++; dfn[u] = low[u] = tim; int i, len = sq[u].size(); st.push(u); in[u] = 1; for (i = 0; i < len; i++) { int v = sq[u][i]; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (in[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { int nowtop = -1; cnt++; while (nowtop != u) { nowtop = st.top(); st.pop(); in[nowtop] = 0; color[nowtop] = cnt; } } } int main() { scanf("%d", &n); int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { int p; scanf("%d", &p); if ((i != j) && (p > 0)) sq[i].push_back(j); } } memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(in, 0, sizeof(in)); memset(color, 0, sizeof(color)); for (i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); if (cnt == 1) printf("YES"); else printf("NO"); return 0; }
7
#include <bits/stdc++.h> char s[(int)1e6]; int p[(int)1e6]; int main() { int n, i, len; long long bp, tm, mx; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &p[i]); } getchar(); gets(s); bp = 0; len = strlen(s); for (i = 0; i < len; i++) { if (s[i] == 'B') bp += p[i]; } mx = bp; tm = bp; for (i = 0; i < len; i++) { if (s[i] == 'A') bp += p[i]; else bp -= p[i]; if (bp > mx) mx = bp; } bp = tm; for (i = len - 1; i >= 0; i--) { if (s[i] == 'A') bp += p[i]; else bp -= p[i]; if (bp > mx) mx = bp; } printf("%I64d\n", mx); return 0; }
3
#include <bits/stdc++.h> using namespace std; void debug_out() { cout << '\n'; } template <typename T, typename... R> void debug_out(const T &f, const R &...r) { cout << f << " "; debug_out(r...); } template <typename A, typename B> ostream &operator<<(ostream &out, const pair<A, B> &a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename Tuple, size_t N> struct tuple_print { static void print(const Tuple &t, std::ostream &os) { tuple_print<Tuple, N - 1>::print(t, os); os << ", " << std::get<N - 1>(t); } }; template <typename Tuple> struct tuple_print<Tuple, 1> { static void print(const Tuple &t, std::ostream &os) { os << "(" << std::get<0>(t); } }; template <typename... Args> ostream &operator<<(ostream &os, const tuple<Args...> &t) { tuple_print<decltype(t), sizeof...(Args)>::print(t, os); return os << ")"; } template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) { out << "["; bool first = true; for (auto &v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "]"; return out; } template <typename T, class Cmp> ostream &operator<<(ostream &out, const set<T, Cmp> &a) { out << "{"; bool first = true; for (auto &v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "}"; return out; } template <typename U, typename T, class Cmp> ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) { out << "{"; bool first = true; for (auto &p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0; } out << "}"; return out; } template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } const long long mod = 1e9 + 7; const int N = 1e6 + 100; int T; long long n; long long b[N], tot[N], c[100]; int main() { scanf("%lld", &n); long long maxx = 0, pos = 0; for (int i = 1; i <= n; i++) { scanf("%lld", &b[i]); long long x = b[i], cnt = 0; for (; !(x & 1); x >>= 1, ++cnt) ; tot[i] = cnt; ++c[cnt]; if (c[cnt] > maxx) maxx = c[cnt], pos = cnt; } printf("%lld\n", n - maxx); for (int i = 1; i <= n; i++) if (tot[i] != pos) printf("%lld ", b[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int main() { mp.clear(); int a, b, m, r0; cin >> a >> b >> m >> r0; int tmp = (a * r0 + b) % m; while (mp.find(tmp) == mp.end()) { mp[tmp]++; tmp = (a * tmp + b) % m; } int ans = tmp; tmp = (a * r0 + b) % m; int cnt = 0; while (ans != tmp) { cnt++; tmp = (a * tmp + b) % m; } cout << mp.size() - cnt << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 9; int n, nxt[MAX], a[MAX]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0; for (int l = 0; l < n; l++) { for (int i = n - 2, j = n - 1; i >= 0; i--) { while (i < j - a[j]) j--; nxt[i] = j; } for (int i = 0; i < n - 1; i = nxt[i]) ans++; rotate(a, a + n - 1, a + n); } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; template <class T, class T1> int chkmin(T &x, const T1 &y) { return x > y ? x = y, 1 : 0; } template <class T, class T1> int chkmax(T &x, const T1 &y) { return x < y ? x = y, 1 : 0; } const int MAXN = (1 << 20); vector<string> query(int l, int r) { cout << "? " << l << " " << r << '\n' << flush; vector<string> ret; for (int i = 0; i < (r - l + 1) * (r - l + 2) / 2; i++) { string tmp; cin >> tmp; sort(tmp.begin(), tmp.end()); ret.push_back(tmp); } return ret; } int n; void read() { cin >> n; } bool cmp(pair<int, map<char, int> > x, pair<int, map<char, int> > y) { return x.first < y.first; } void solve() { if (n == 1) { cout << "? " << 1 << " " << 1 << '\n' << flush; string ans; cin >> ans; cout << "! " << ans << '\n' << flush; return; } auto a = query(1, n); auto b = query(2, n); map<string, int> prefixes; for (auto it : a) prefixes[it]++; for (auto it : b) prefixes[it]--; vector<pair<int, map<char, int> > > ret; for (auto it : prefixes) { if (it.second <= 0) continue; map<char, int> cnt; for (auto x : it.first) { cnt[x]++; } ret.push_back({((int)it.first.size()), cnt}); } sort(ret.begin(), ret.end(), cmp); string ans = ""; map<char, int> last = {}; for (auto it : ret) { char found = -1; for (auto c : it.second) { if (c.second > last[c.first]) { found = c.first; break; } } ans.push_back(found); last[found]++; } cout << "! " << ans << '\n' << flush; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, b, p, cnt = 0; cin >> n >> b >> p; int cnt1 = n; while (n > 1) { if (n % 2 == 0) { cnt += n * b + (n / 2); n /= 2; } else if (n % 2 == 1) { cnt += (n - 1) * b + (n / 2); n /= 2; n++; } } cout << cnt << " " << cnt1 * p << endl; }
1
#include<bits/stdc++.h> using namespace std; #define ll long long #define f(i,n) for(int i=0;i<n;i++) #define endl ("\n") #define pb push_back #define mp make_pair #define pl pair<ll, ll> #define vl vector<ll> #define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL); #define N 100005 #define M 1000000007 void SieveOfEratosthenes(ll n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (ll p = 2; p * p <= n; p++) { if (prime[p] == true) { for (ll i = p * p; i <= n; i += p) prime[i] = false; } } for (ll p = 2; p <= n; p++) if (prime[p]) cout << p << " "; } int main() { fastIO ll n,q; cin>>n>>q; ll cnt0=0,cnt1=0; ll a[n]; f(i,n){cin>>a[i]; if(a[i])cnt1++; else cnt0++; } while(q--){ ll x,y; cin>>x>>y; if(x==1){ if(a[y-1]==0){ cnt1++; cnt0--; } else { cnt1--; cnt0++; } a[y-1]=1-a[y-1]; } else { if(cnt1>=y)cout<<1<<endl; else cout<<0<<endl; } } }
0
#include <bits/stdc++.h> using namespace std; int T, len, a[20], num[20]; bool vis[20][40][4][2], yet[20][2]; long long n, k, inf[20], f[20][40][4][2]; bool J(int x) { return x == 4 || x == 7; } int count(int x) { int s = 0; for (; x; x /= 10) s += J(x % 10); return s; } long long get(int o, int t1, int t2, int r) { if (vis[o][t1 + 11][t2 + 2][r]) return f[o][t1 + 11][t2 + 2][r]; long long &now = f[o][t1 + 11][t2 + 2][r]; now = inf[len - o + 3]; if (o > len && t1 == 0) { if (t2 == 1) now = 3; if (t2 == -1) now = 5; if (t2 == -2 || t2 == 0) now = !r; return now; } if (o > len && t1 > 0) return now; if (o > len + 2) return now; vis[o][t1 + 11][t2 + 2][r] = 1; for (int i = 0; i <= 9; i++) { int nr = (i > a[o] || (i == a[o] && r)); if (o > len && t1 < 0 && !J(i)) continue; if (t2 == -2) now = min(get(o + 1, t1 - J(i) + J(a[o]), t2, nr) * 10 + i, now); if (t2 == -1) { if (J(i)) now = min(get(o + 1, t1 - J(i) + J(a[o]), -2, nr) * 10 + i, now); else if (i == 9) now = min(get(o + 1, t1 - J(i) + J(a[o]), t2, nr) * 10 + i, now); } if (t2 == 1) { if (i < 9 && J(i + 1)) now = min(get(o + 1, t1 - J(i) + J(a[o]), -2, nr) * 10 + i, now); if (i == 9) now = min(get(o + 1, t1 - J(i) + J(a[o]), t2, nr) * 10 + i, now); } if (t2 == 0) { if (i < 9 && !J(i) && !J(i + 1)) now = min(get(o + 1, t1 - J(i) + J(a[o]), -2, nr) * 10 + i, now); if (i == 9) now = min(get(o + 1, t1 - J(i) + J(a[o]), t2, nr) * 10 + i, now); } } return now; } long long dfs(int o, long long k) { if (o > len) return 1; long long now(inf[len - o + 3]); if (k < 10) { for (int i = 0; i <= 9; i++) { int ok = 1, t1 = -2, t2 = -2; if (i == a[o]) continue; for (int j = 0; j < k && k; j++) { int x = i + j, y = a[o] + j; if (x < 10) { if (t1 == -2) t1 = J(y % 10) - J(x) + count(num[o + 1] + y / 10) - count(num[o + 1]); else if (t1 != J(y % 10) - J(x) + count(num[o + 1] + y / 10) - count(num[o + 1])) ok = 0; } else { if (t2 == -2) t2 = J(y % 10) - J(x % 10) + count(num[o + 1] + y / 10) - count(num[o + 1]) - t1; else if (t2 != J(y % 10) - J(x % 10) + count(num[o + 1] + y / 10) - count(num[o + 1]) - t1) ok = 0; } } if (ok) now = min(get(o + 1, t1, t2, (i > a[o] || i == a[o])) * 10 + i, now); } } long long t = dfs(o + 1, 1 + (k + a[o] - 1) / 10); now = min(t * 10 + a[o], now); return now; } void init() { inf[0] = 1; for (int i = 1; i <= 19; i++) inf[i] = inf[i - 1] * 10; memset(num, 0, sizeof(num)); memset(f, 0, sizeof(f)); memset(vis, 0, sizeof(vis)); memset(a, 0, sizeof(a)); } int cl(long long x) { int t = 0; for (; ++t, x /= 10;) ; return t; } int main() { T = 1; while (T--) { init(); scanf( "%I64d" "%I64d", &n, &k); for (len = 0; a[++len] = n % 10, num[len] = n, n /= 10;) ; len = max(cl(k), len); printf( "%I64d" "\n", dfs(1, k)); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; long long n = s.size(); vector<long long> freq(26, 0); for (long long int i = 0; i < n; i++) { freq[s[i] - 'a']++; } vector<long long int> primes; vector<bool> isprime(n + 1, 1); isprime[1] = false; for (long long int i = 2; i < n; i++) { if (!isprime[i]) continue; primes.push_back(i); for (long long j = i * i; j < n; j += i) { isprime[j] = 0; } } string res = ""; for (long long int i = 0; i < n; i++) res += '!'; bool pos = 1; long long idx = 0, mx = 0; for (long long int j = 0; j < 26; j++) { if (freq[j] > mx) { mx = freq[j]; idx = j; } } for (long long i = 0; i < primes.size() && primes[i] * 2 <= n; i++) { for (long long k = primes[i] - 1; k < n; k += primes[i]) { if (res[k] != '!') { if (res[k] == 'a' + idx) continue; else pos = false; break; } if (freq[idx] == 0) { pos = false; break; } res[k] = (char)('a' + idx); freq[idx]--; } } if (!pos) { cout << "NO"; } else { cout << "YES" << "\n"; for (long long int i = 0; i < n; i++) { if (res[i] == '!') { for (long long int j = 0; j < 26; j++) { if (freq[j] > 0) { res[i] = (char)('a' + j); freq[j]--; break; } } } } cout << res; } }
2
#include <bits/stdc++.h> using namespace std; long long calculateDays(vector<long long> &caffeine, long long mid, long long total_sum) { long long n = caffeine.size(); long long k = 0, sum1 = 0; long long j = 0; while (j < n) { for (long long m = 0; m <= mid; m++) { sum1 += max(caffeine[j] - k, (long long)0); j++; if (j >= n) { break; } } k++; if (sum1 >= total_sum) { break; } } return sum1; } int main() { long long n, m; cin >> n >> m; vector<long long> caffeine(n); for (long long i = 0; i < n; i++) { cin >> caffeine[i]; } sort(caffeine.begin(), caffeine.end(), greater<long long>()); long long total_sum = 0, sum = 0; for (long long i = 0; i < n; i++) { sum += max(caffeine[i] - i, (long long)0); total_sum += caffeine[i]; } if (total_sum < m) { cout << -1 << endl; } else if (total_sum == m) { cout << n << endl; } else { long long low = 0, high = n, mid = (high + low) / 2; while (low < high) { long long ans = calculateDays(caffeine, mid, m); if (ans >= m) { if (mid - 1 >= 0) { while (calculateDays(caffeine, mid - 1, m) >= m) { mid = mid - 1; if (mid == 0) { break; } } } cout << mid + 1 << endl; break; } else if (ans < m) { low = mid + 1; mid = (high + low) / 2; } else { high = mid - 1; mid = (low + mid) / 2; } } } }
4
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); long double fixAngle(long double A) { return A > 1 ? 1 : (A < -1 ? -1 : A); } double dcmp(double a, double b) { return fabs(a - b) < 1e-7 ? 0 : a > b ? 1 : -1; } int main() { int m, n; cin >> m >> n; long double e = 0; for (int i = 1; i <= m; i++) { e += i * (powl((double)i / m, n) - powl((double)(i - 1) / m, n)); } cout << setprecision(20) << fixed << e << "\n"; }
4
#include <bits/stdc++.h> int main() { int t; int n; int i; long long int h, x; scanf("%d", &t); while (t--) { h = 0; scanf("%d", &n); for (i = 1; i <= 9; i++) { x = i; while (x <= n) { h++; x = x * 10 + i; } } printf("%lld\n", h); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long X = 222222, N = 111111; bool isp[X]; vector<long long> primes; long long f[N], g[N]; long long n, m; long long powmod(long long x, long long n, long long mod) { long long ans = 1; while (n) { if (n & 1) { ans = ans * x % mod; } x = x * x % mod; n >>= 1; } return ans; } void calcg(long long d) { fill(g, g + d, 0); for (long long i = (0); i <= (n - 1); i++) { long long ii = i % d; g[ii] += f[i]; } } long long evalg(long long r, long long d, long long p) { long long ans = 0, po = 1; for (long long i = (0); i <= (d - 1); i++) { ans = (ans + g[i] * po) % p; po = po * r % p; } return ans; } long long findr(long long p, long long k) { long long r; do { r = powmod(rand() % (p - 1) + 1, k, p); } while (r == 1 || r > p - 2); return r; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); srand(time(NULL)); cin >> n >> m; string s, t; cin >> s >> t; for (long long i = (0); i <= (n - 1); i++) { f[i] = s[i] - t[i]; } fill(isp + 2, isp + X, true); for (long long i = (2); i <= (X - 1); i++) { if (isp[i]) { primes.push_back(i); for (long long j = 2 * i; j < X; j += i) { isp[j] = false; } } } for (long long i = 1;; i++) { long long d = primes[i]; calcg(d); for (long long k = (m + d - 2) / d; d * k + 1 < X; k++) { long long p = d * k + 1; if (!isp[p]) continue; long long rb = findr(p, k), r = 1; for (long long i = (1); i <= (d); i++) { r = r * rb % p; if (!evalg(r, d, p)) { cout << p << " " << r << "\n"; return 0; } } } } return 0; }
11
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1.0); const double esp = 0.00000001; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long ngcd(long long* a, int n) { return n == 1 ? *a : gcd(a[n - 1], ngcd(a, n - 1)); } long long Pow(long long a, long long n, long long mod) { long long res = 1ll; a %= mod; while (n) { if (n & 1) res = res * a % mod; n >>= 1; a = a * a % mod; } return res % mod; } const long long mod = 998244353; const long long MOD = 1000000007; const int maxn = 2005; char s[2][maxn]; int main() { while (~scanf("%s %s", s[0], s[1])) { int len[2]; len[0] = strlen(s[0]); len[1] = strlen(s[1]); int res = len[1]; for (int i = 0; i < len[0]; ++i) { int tmp = 0, x = i, y = 0; for (; x < len[0] && y < len[1]; ++x, ++y) if (s[0][x] == s[1][y]) ++tmp; res = min(res, len[1] - tmp); } for (int i = 0; i < len[1]; ++i) { int tmp = 0, x = i, y = 0; for (; x < len[1] && y < len[0]; ++x, ++y) if (s[1][x] == s[0][y]) ++tmp; res = min(res, len[1] - tmp); } printf("%d\n", res); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m; vector<tuple<int, int, int>> edge; class BipGraph { int m, n; list<int> *adj; int *pairU, *pairV, *dist; public: BipGraph(int m, int n); void addEdge(int u, int v); bool bfs(); bool dfs(int u); int hopcroftKarp(); }; int BipGraph::hopcroftKarp() { pairU = new int[m + 1]; pairV = new int[n + 1]; dist = new int[m + 1]; for (int u = 0; u <= m; u++) pairU[u] = 0; for (int v = 0; v <= n; v++) pairV[v] = 0; int result = 0; while (bfs()) { for (int u = 1; u <= m; u++) if (pairU[u] == 0 && dfs(u)) result++; } return result; } bool BipGraph::bfs() { queue<int> Q; for (int u = 1; u <= m; u++) { if (pairU[u] == 0) { dist[u] = 0; Q.push(u); } else dist[u] = INT_MAX; } dist[0] = INT_MAX; while (!Q.empty()) { int u = Q.front(); Q.pop(); if (dist[u] < dist[0]) { list<int>::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) { int v = *i; if (dist[pairV[v]] == INT_MAX) { dist[pairV[v]] = dist[u] + 1; Q.push(pairV[v]); } } } } return (dist[0] != INT_MAX); } bool BipGraph::dfs(int u) { if (u != 0) { list<int>::iterator i; for (i = adj[u].begin(); i != adj[u].end(); ++i) { int v = *i; if (dist[pairV[v]] == dist[u] + 1) { if (dfs(pairV[v]) == true) { pairV[v] = u; pairU[u] = v; return true; } } } dist[u] = INT_MAX; return false; } return true; } BipGraph::BipGraph(int m, int n) { this->m = m; this->n = n; adj = new list<int>[m + 1]; } void BipGraph::addEdge(int u, int v) { adj[u].push_back(v); } bool check(int d) { BipGraph g(n, n); for (auto e : edge) { if (get<2>(e) <= d) { g.addEdge(get<0>(e), get<1>(e)); } } return g.hopcroftKarp() == n; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; long long int u, v, d; for (long long int i = 0; i < m; i++) { cin >> u >> v >> d; edge.push_back({u, v, d}); } int left = 1; int right = pow(10, 9); while (left < right) { int mid = (left + right) / 2; if (check(mid)) { right = mid; } else { left = mid + 1; } } if (check(left)) { cout << left << endl; } else { cout << -1 << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, q, head[130000], _edge, ans, fa[130000], son[130000], bro[130000], deep[130000], num[130000], h_son[130000], top[130000], dfsx[130000], _dfsx, vfa[130000], vson[130000], vbro[130000], node[130000], root; bitset<130000> vis; struct EDGE { int a, b, next; EDGE(int a = 0, int b = 0, int next = 0) : a(a), b(b), next(next) {} } edge[2 * 130000]; bool cmp(int a, int b) { return dfsx[a] < dfsx[b]; } void adde(int a, int b) { edge[++_edge] = EDGE(a, b, head[a]); head[a] = _edge; } void addt(int a, int b) { bro[b] = son[a]; son[a] = b; fa[b] = a; } void vaddt(int a, int b) { ans += deep[b] - deep[a]; vbro[b] = vson[a]; vson[a] = b; vfa[b] = a; } void read() { int ls1, ls2; scanf("%d%d", &n, &q); for (int i = 1; i < n; ++i) scanf("%d%d", &ls1, &ls2), adde(ls1, ls2), adde(ls2, ls1); return; } void dfs1(int a, int c) { deep[a] = c; for (int i = head[a]; i; i = edge[i].next) { int b = edge[i].b; if (fa[a] == b) continue; addt(a, b); dfs1(b, c + 1); num[a] += num[b]; if (num[h_son[a]] < num[b]) h_son[a] = b; } ++num[a]; } void dfs2(int a, int t) { dfsx[a] = ++_dfsx; top[a] = t; if (h_son[a]) dfs2(h_son[a], t); for (int b = son[a]; b; b = bro[b]) if (b != h_son[a]) dfs2(b, b); } void vdfs(int a, int v, int &ans_node, int &ans_v) { if (!a || vis[a]) return; vis[a] = 1; if (v > ans_v) ans_node = a, ans_v = v; if (v == ans_v && ans_node > a) ans_node = a; for (int b = vson[a]; b; b = vbro[b]) { vdfs(b, v + deep[b] - deep[a], ans_node, ans_v); } vdfs(vfa[a], v + deep[a] - deep[vfa[a]], ans_node, ans_v); } int LCA(int a, int b) { while (top[a] != top[b]) { if (deep[top[b]] < deep[top[a]]) swap(a, b); b = fa[top[b]]; } return deep[a] < deep[b] ? a : b; } void vbuild() { sort(node + 1, node + 1 + q, cmp); deque<int> sta; sta.push_back(node[1]); for (int i = 2; i <= q; ++i) { int lca = LCA(sta.back(), node[i]); while (deep[sta.back()] > deep[lca]) { int b = sta.back(); sta.pop_back(); if (sta.empty() || deep[sta.back()] < deep[lca]) sta.push_back(lca); vaddt(sta.back(), b); } sta.push_back(node[i]); } while (sta.size() > 1) { int b = sta.back(); sta.pop_back(); vaddt(sta.back(), b); } root = sta.back(); } int main() { int ls1 = 0, ls2 = -1; read(); dfs1(1, 0); dfs2(1, 1); for (int i = 1; i <= q; ++i) scanf("%d", node + i); vbuild(); vdfs(root, 0, ls1, ls2); root = 1e6; vis = 0; vdfs(ls1, 0, root, ls2); printf("%d\n%d\n", min(root, ls1), 2 * ans - ls2); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { FILE *inF, *outF; inF = fopen("in", "r"); outF = fopen("out", "w"); inF = stdin; outF = stdout; int t; fscanf(inF, "%d", &t); while (t--) { int n, q; long long dp[300001][2] = {}; fscanf(inF, "%d %d", &n, &q); for (int i = 1; i <= n; i++) { int num; fscanf(inF, "%d", &num); dp[i][0] = max(dp[i - 1][1] + num, dp[i - 1][0]); dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - num); } fprintf(outF, "%lld\n", max(dp[n][0], dp[n][1])); } return 0; }
2
#include <bits/stdc++.h> using namespace std; ifstream fin("AAtest.in.txt"); struct vaar { long long su, mit, pal, ko; }; long long n, m, a[100005], c, l, k, b, pal; vector<vaar> v[35]; vaar va; int main() { ios::sync_with_stdio(0); cin >> n >> m >> k >> b; for (int i = 0; i < m; i++) cin >> a[i]; sort(a, a + m); c = 1; while (l < n) c *= 2, l++; for (int i = 0; i < m; i++) a[i] += c - 1; va.su = b; va.mit = 1; va.pal = 1; va.ko = a[0]; v[0].push_back(va); for (int i = 1; i < m; i++) { if (v[0][v[0].size() - 1].ko == a[i]) v[0][v[0].size() - 1].su += b, v[0][v[0].size() - 1].mit++; else { va.su = b; va.mit = 1; va.pal = 1; va.ko = a[i]; v[0].push_back(va); } } pal = 1; for (int i = 0; i < n; i++) { pal *= 2; for (int j = 0; j < v[i].size(); j++) if (j == 0 || v[i][j].ko / 2 != v[i][j - 1].ko / 2) { va.su = v[i][j].su; va.mit = v[i][j].mit; va.ko = v[i][j].ko / 2; va.pal = 1; v[i + 1].push_back(va); } else { v[i + 1][v[i + 1].size() - 1].su += v[i][j].su; v[i + 1][v[i + 1].size() - 1].mit += v[i][j].mit; v[i + 1][v[i + 1].size() - 1].pal = 2; } for (int j = 0; j < v[i + 1].size(); j++) { if (v[i + 1][j].pal == 1) v[i + 1][j].su += k; v[i + 1][j].su = min(v[i + 1][j].su, b * pal * v[i + 1][j].mit); } } cout << v[n][0].su; }
4
#include <bits/stdc++.h> class Segtree { int32_t** segtree; int32_t size_; void build(int32_t node, int32_t left, int32_t right, std::vector<int32_t>& data) { segtree[node] = new int32_t[right - left]; if (right - left == 1) { segtree[node][0] = data[left]; return; } int32_t m = (left + right) / 2; build(node * 2 + 1, left, m, data); build(node * 2 + 2, m, right, data); std::merge(segtree[node * 2 + 1], segtree[node * 2 + 1] + m - left, segtree[node * 2 + 2], segtree[node * 2 + 2] + right - m, segtree[node]); } public: void fill(std::vector<int32_t>& data) { segtree = new int32_t*[4 * data.size()]; for (int32_t i = 0; i < 4 * data.size(); i++) segtree[i] = nullptr; build(0, 0, data.size(), data); size_ = data.size(); } int32_t query(int32_t node, int32_t left, int32_t right, int32_t query_left, int32_t query_right, int32_t val) { if (left >= query_right || right <= query_left) return 0; if (left >= query_left && right <= query_right) return std::lower_bound(segtree[node], segtree[node] + right - left, val) - segtree[node]; int32_t m = (left + right) / 2; return query(node * 2 + 1, left, m, query_left, query_right, val) + query(node * 2 + 2, m, right, query_left, query_right, val); } void destroy() { for (int32_t i = 0; i < 4 * size_; i++) delete[] segtree[i]; delete[] segtree; } }; int main() { int32_t n, m; std::cin >> n >> m; std::string* field = new std::string[n]; for (int32_t i = 0; i < n; i++) { std::cin >> field[i]; for (int32_t j = 0; j < m; j++) field[i].push_back(rand() % 2 == 1 ? 'z' : '.'); } int32_t** available_left = new int32_t*[n]; for (int32_t i = 0; i < n; i++) { available_left[i] = new int32_t[m]; int32_t begin = -1; int32_t left_passed = 0; for (int32_t j = 0; j <= m; j++) { if (j < m && field[i][j] == 'z') { left_passed++; if (begin == -1) begin = j; } else { if (begin != -1) for (int32_t k = begin; k < j; k++) available_left[i][k] = k - begin + 1; begin = -1; } } } int32_t** available_diag = new int32_t*[n]; for (int32_t i = 0; i < n; i++) { available_diag[i] = new int32_t[m]; } for (int32_t i = 0; i < n; i++) { std::pair<int32_t, int32_t> begin = {-1, -1}; std::pair<int32_t, int32_t> cur = {i, 0}; for (int32_t j = 0; cur.first >= 0 && cur.second < m; j++) { if (field[cur.first][cur.second] == 'z') { if (begin.first == -1) begin = cur; } else { if (begin.first != -1) for (std::pair<int32_t, int32_t> k = begin; k != cur;) { available_diag[k.first][k.second] = begin.first - k.first + 1; k.first--; k.second++; } begin = {-1, -1}; } cur.first--; cur.second++; } if (begin.first != -1) { for (std::pair<int32_t, int32_t> k = begin; k != cur;) { available_diag[k.first][k.second] = begin.first - k.first + 1; k.first--; k.second++; } } } for (int32_t i = 1; i < m; i++) { std::pair<int32_t, int32_t> begin = {-1, -1}; std::pair<int32_t, int32_t> cur = {n - 1, i}; for (int32_t j = 0; cur.first >= 0 && cur.second < m; j++) { if (field[cur.first][cur.second] == 'z') { if (begin.first == -1) begin = cur; } else { if (begin.first != -1) for (std::pair<int32_t, int32_t> k = begin; k != cur;) { available_diag[k.first][k.second] = begin.first - k.first + 1; k.first--; k.second++; } begin = {-1, -1}; } cur.first--; cur.second++; } if (begin.first != -1) { for (std::pair<int32_t, int32_t> k = begin; k != cur;) { available_diag[k.first][k.second] = begin.first - k.first + 1; k.first--; k.second++; } } } int32_t** ends_right = new int32_t*[n]; for (int32_t i = 0; i < n; i++) { ends_right[i] = new int32_t[m]; int32_t begin = -1; for (int32_t j = m - 1; j >= -1; j--) { if (j >= 0 && field[i][j] == 'z') { if (begin == -1) begin = j; } else { for (int32_t k = begin; k > j; k--) ends_right[i][k] = begin; begin = -1; } } } std::vector<int32_t>* ens_per_diag = new std::vector<int32_t>[n + m - 1]; int32_t** index_in_diag = new int32_t*[n + m - 1]; for (int32_t i = 0; i < n; i++) { index_in_diag[i] = new int32_t[m]; for (int32_t j = 0; j < m; j++) { index_in_diag[i][j] = ens_per_diag[i + j].size(); ens_per_diag[i + j].push_back(ends_right[i][j]); } } Segtree* trees = new Segtree[n + m - 1]; int64_t answer = 0; std::vector<std::pair<int32_t, int32_t> > all_cells; for (int32_t i = 0; i < n; i++) for (int32_t j = 0; j < m; j++) { if (field[i][j] == 'z') all_cells.emplace_back(i, j); } std::sort(all_cells.begin(), all_cells.end(), [](std::pair<int32_t, int32_t> p1, std::pair<int32_t, int32_t> p2) { return p1.first + p1.second < p2.first + p2.second; }); for (int32_t i = 0; i < all_cells.size(); i++) { if (i > 0 && all_cells[i - 1].first + all_cells[i - 1].second < all_cells[i].first + all_cells[i].second) trees[all_cells[i - 1].first + all_cells[i - 1].second].destroy(); if (i == 0 || all_cells[i - 1].first + all_cells[i - 1].second < all_cells[i].first + all_cells[i].second) trees[all_cells[i].first + all_cells[i].second].fill( ens_per_diag[all_cells[i].first + all_cells[i].second]); int32_t go_down = std::min(available_left[all_cells[i].first][all_cells[i].second], available_diag[all_cells[i].first][all_cells[i].second]); answer += index_in_diag[all_cells[i].first + go_down - 1] [all_cells[i].second - go_down + 1] - index_in_diag[all_cells[i].first][all_cells[i].second] + 1; answer -= trees[all_cells[i].first + all_cells[i].second].query( 0, 0, ens_per_diag[all_cells[i].first + all_cells[i].second].size(), index_in_diag[all_cells[i].first][all_cells[i].second], index_in_diag[all_cells[i].first + go_down - 1] [all_cells[i].second - go_down + 1] + 1, all_cells[i].second); } std::cout << answer; return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int mod = (int)1e9 + 7; const long long INF = 1000000000000000000LL; const int WASTE = (int)2e5; int power(int a, int b, int m) { int i = 1; while (b) { if (b % 2) i = (i * a) % mod; b /= 2; a = (a * a) % mod; } return i; } const int N = 1e5 + 5; int ans = 0, t[N]; bool visit[N], p[N]; vector<int> v[N], topo; void dfs(int x) { visit[x] = 1; for (auto y : v[x]) if (!visit[y]) dfs(y); topo.push_back(x); } void dfs1(int x) { visit[x] = 1; int m = p[x]; for (auto y : v[x]) { if (!visit[y]) dfs1(y); bool ss = 0; if (p[x] && !p[y]) ss = 1; m = max(m, t[y] + ss); } t[x] = m; ans = max(ans, t[x]); } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; memset(p, 0, sizeof(p)); memset(visit, 0, sizeof(visit)); for (int x = 0; x < n; x++) cin >> p[x]; while (m--) { int x, y; cin >> x >> y; v[x].push_back(y); } for (int x = 0; x < n; x++) if (!visit[x]) dfs(x); reverse(topo.begin(), topo.end()); memset(visit, 0, sizeof(visit)); for (int x : topo) if (!visit[x]) dfs1(x); cout << ans; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { long long int n; cin >> n; vector<pair<long long int, long long int> > v; for (int i = 0; i < n; i++) { long long int x; cin >> x; v.push_back({x, i + 1}); } sort(v.begin(), v.end()); long long int ma, mi; ma = v[0].second; mi = v[0].second; if (n == 1) { cout << "1"; cout << endl; continue; } for (int i = 0; i < n; i++) { ma = max(ma, v[i].second); mi = min(mi, v[i].second); long long int d = ma - mi + 1; long long int num = v[i].first; if (d == num) { cout << "1"; } else { cout << "0"; } } cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n; for (int i = 0; i < n + 1; i++) cin >> a >> b; cout << (n - 4) / 2; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; string s1, s2; bool check[123], a[123][123]; int q[27], first = 1, last, res; char l[730], r[730]; void init() { memset(check, true, sizeof(check)); first = 1; last = 0; } void push(int u) { last++; q[last] = u; } void pop(int &u) { u = q[first]; first++; } void build(int u, int v) { a[u][v] = true; a[v][u] = true; } bool bfs(int s, int f) { init(); push(s); check[s] = false; while (first <= last) { int u; pop(u); for (int v = 'a'; v <= 'z'; v++) if (a[u][v] && check[v]) { if (v == f) return true; check[v] = false; push(v); } } return false; } int main() { cin >> n; cin >> s1 >> s2; for (int i = 0; i < n; i++) { if (a[s1[i]][s2[i]] == false && bfs(s1[i], s2[i]) == false && s1[i] != s2[i]) { res++; l[res] = s1[i]; r[res] = s2[i]; build(s1[i], s2[i]); } } cout << res << endl; for (int i = 1; i <= res; i++) cout << l[i] << " " << r[i] << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[100005]; int main() { int n, m, c; cin >> n >> m >> c; for (int i = 1; i <= m; i++) { int x, ans; cin >> x; if (x <= c / 2) { for (int j = 1; j <= n; j++) { if (x < a[j] || a[j] == 0) { a[j] = x; ans = j; break; } } } else { for (int j = n; j >= 1; j--) { if (x > a[j] || a[j] == 0) { a[j] = x; ans = j; break; } } } cout << ans << endl; } }
6
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double PI = acos(-1.0); const int T = 5e3 + 3; const long long MOD = 1e9 + 7; long long dp[2][T]; long long l[T]; long long r[T]; long long calc(int j, bool ok) { return (dp[ok][r[j]] - dp[ok][l[j] - 1] - (dp[ok][j] - dp[ok][j - 1] + MOD) + MOD + MOD) % MOD; } int main() { ios_base::sync_with_stdio(false); memset(l, INF, sizeof l); long long n, a, b, k; cin >> n >> a >> b >> k; for (int i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { if (abs(i - j) < abs(i - b)) { l[i] = min(l[i], j); r[i] = max(r[i], j); } } } dp[0][0] = 0; for (int i = 1; i <= n; i++) dp[0][i] += dp[0][i - 1] + (i != b); bool ok = 0; for (int i = 1; i <= k; i++) { for (int j = 1; j <= n; j++) { if (j == b) continue; dp[!ok][j] = calc(j, ok); } for (int i = 1; i <= n; i++) dp[!ok][i] = (dp[!ok][i] + dp[!ok][i - 1]) % MOD; ok = !ok; } cout << (dp[ok][a] - dp[ok][a - 1] + MOD) % MOD << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXLEN = 20; int hex2dec(char c) { if (c >= '0' && c <= '9') return c - '0'; else return c - 'a' + 10; } long long dp[MAXLEN][16]; long long dp2[16][(1 << 16) + 10]; int mx_hex_dig[1 << 16]; bool valid[1 << 16]; long long solve(string s) { int mx = 0; int n = s.length(); long long res = 0; int msk = 0; for (int i = 0; i < n; i++) { int x = hex2dec(s[i]); for (int j = 0; j < x; j++) { if (i >= n - 4) { int cmsk = msk + (j << (4 * (n - 1 - i))); res += dp2[mx][cmsk + (1 << (4 * (n - 1 - i)))] - dp2[mx][cmsk]; } else { int mxUsedDigit = max(mx, j); res += dp[n - i - 1][mxUsedDigit]; } } if (i >= n - 4) msk += (x << (4 * (n - 1 - i))); else mx = max(mx, x); } return res; } int main() { for (int j = 0; j < (1 << 16); j++) { mx_hex_dig[j] = 0; for (int k = 0; k < 4; k++) { int hex_dig = (j >> (k * 4)) & 15; mx_hex_dig[j] = max(mx_hex_dig[j], hex_dig); } valid[j] = ((j >> mx_hex_dig[j]) & 1) > 0; } for (int i = 0; i < 16; i++) for (int j = 0; j < (1 << 16); j++) { dp2[i][j + 1] = dp2[i][j]; if (i > mx_hex_dig[j]) dp2[i][j + 1] += (j >> i) & 1; else dp2[i][j + 1] += valid[j]; } for (int i = 0; i < 16; i++) dp[4][i] = dp2[i][(1 << 16)]; for (int i = 5; i < MAXLEN; i++) for (int j = 0; j < 16; j++) for (int p = 0; p < 16; p++) dp[i][j] += dp[i - 1][max(j, p)]; int q; cin >> q; while (q--) { string l, r; cin >> l >> r; int idx = (int)r.length() - 1; while (idx >= 0 && r[idx] == 'f') { r[idx] = '0'; idx--; } if (idx == -1) r = "1" + r; else if (r[idx] == '9') r[idx] = 'a'; else r[idx]++; cout << solve(r) - solve(l) << endl; } }
10
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const double eps = 1e-8; const int inf = 0x3f3f3f3f; const int N = 2e5 + 10; long long ans[10000] = {1, 0, -1, -1, -1, -1, -1, -1, -1, -1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 1, 0, -1, -1, -1, -1, -1, -1, -1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 2, -1, -1, -1, -1, -1, -1, -1, -1, 6, -1, 1, 0, -1, -1, -1, -1, -1, -1, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 9, 4, 2, -1, -1, -1, -1, -1, -1, -1, 19, 9, -1, 1, 0, -1, -1, -1, -1, -1, 29, 5, -1, -1, -1, -1, -1, -1, -1, -1, 39, 19, 3, -1, -1, -1, -1, -1, -1, -1, 49, 6, -1, -1, -1, -1, -1, -1, -1, -1, 59, 29, -1, 2, -1, -1, -1, -1, -1, -1, 69, 7, 4, 9, 1, 0, -1, -1, -1, -1, 79, 39, -1, -1, -1, -1, -1, -1, -1, -1, 89, 8, -1, -1, -1, -1, -1, -1, -1, -1, 99, 49, 5, 3, -1, -1, -1, -1, -1, -1, 199, 18, -1, 19, 9, -1, -1, -1, -1, -1, 299, 59, -1, 8, 2, -1, -1, -1, -1, -1, 399, 28, 6, -1, -1, 1, 0, -1, -1, -1, 499, 69, -1, 4, -1, -1, -1, -1, -1, -1, 599, 38, -1, 29, 8, -1, -1, -1, -1, -1, 699, 79, 7, 18, 19, 9, -1, -1, -1, -1, 799, 48, -1, 7, 3, -1, -1, -1, -1, -1, 899, 89, -1, 5, -1, -1, -1, -1, -1, -1, 999, 58, 17, 39, 7, 2, -1, -1, -1, -1, 1999, 189, -1, 28, 18, -1, 1, 0, -1, -1, 2999, 68, -1, 17, 29, -1, -1, -1, -1, -1, 3999, 289, 27, 6, 4, 7, 9, -1, -1, -1, 4999, 78, -1, 49, 6, -1, -1, -1, -1, -1, 5999, 389, -1, 38, 17, -1, 8, -1, -1, -1, 6999, 88, 37, 27, 28, 3, -1, -1, -1, -1, 7999, 489, -1, 16, 39, -1, 7, -1, -1, -1, 8999, 98, -1, 59, 5, -1, 2, -1, -1, -1, 9999, 589, 47, 48, 16, 5, 6, 1, 0, -1, 19999, 198, -1, 37, 27, -1, 19, 9, -1, -1, 29999, 689, -1, 26, 38, -1, 5, 8, -1, -1, 39999, 298, 57, 69, 49, 4, 18, 7, -1, -1, 49999, 789, -1, 58, 15, -1, 4, 6, -1, -1, 59999, 398, -1, 47, 26, -1, 17, 5, -1, -1, 69999, 889, 67, 36, 37, 15, 3, 4, -1, -1, 79999, 498, -1, 79, 48, -1, 16, 3, -1, -1, 89999, 989, -1, 68, 59, -1, 29, 2, -1, -1, 99999, 598, 77, 57, 25, 14, 15, 19, 1, 0, 199999, 1989, -1, 46, 36, -1, 28, 18, -1, 1, 299999, 698, -1, 89, 47, -1, 14, 17, -1, 2, 399999, 2989, 87, 78, 58, 25, 27, 16, -1, 3, 499999, 798, -1, 67, 69, -1, 13, 15, -1, 4, 599999, 3989, -1, 56, 35, -1, 26, 14, -1, 5, 699999, 898, 97, 189, 46, 24, 39, 13, -1, 6, 799999, 4989, -1, 88, 57, -1, 25, 12, -1, 7, 899999, 998, -1, 77, 68, -1, 38, 29, -1, 8, 999999, 5989, 197, 66, 79, 35, 24, 28, 11, 9, 1999999, 1998, -1, 289, 45, -1, 37, 27, -1, 10, 2999999, 6989, -1, 188, 56, -1, 23, 26, -1, 11, 3999999, 2998, 297, 87, 67, 34, 36, 25, -1, 12, 4999999, 7989, -1, 76, 78, -1, 49, 24, -1, 13, 5999999, 3998, -1, 389, 89, -1, 35, 23, -1, 14, 6999999, 8989, 397, 288, 55, 45, 48, 22, -1, 15, 7999999, 4998, -1, 187, 66, -1, 34, 39, -1, 16, 8999999, 9989, -1, 86, 77, -1, 47, 38, -1, 17, 9999999, 5998, 497, 489, 88, 44, 33, 37, 21, 18, 19999999, 19989, -1, 388, 189, -1, 46, 36, -1, 19, 29999999, 6998, -1, 287, 65, -1, 59, 35, -1, 20, 39999999, 29989, 597, 96, 76, 55, 45, 34, -1, 21, 49999999, 7998, -1, 589, 87, -1, 58, 33, -1, 22, 59999999, 39989, -1, 488, 188, -1, 44, 32, -1, 23, 69999999, 8998, 697, 387, 289, 54, 57, 49, -1, 24, 79999999, 49989, -1, 196, 75, -1, 43, 48, -1, 25, 89999999, 9998, -1, 689, 86, -1, 56, 47, -1, 26, 99999999, 59989, 797, 588, 187, 65, 69, 46, 31, 27, 199999999, 19998, -1, 487, 288, -1, 55, 45, -1, 28, 299999999, 69989, -1, 296, 389, -1, 68, 44, -1, 29, 399999999, 29998, 897, 789, 85, 64, 54, 43, -1, 30, 499999999, 79989, -1, 688, 186, -1, 67, 42, -1, 31, 599999999, 39998, -1, 587, 287, -1, 53, 59, -1, 32, 699999999, 89989, 997, 396, 388, 75, 66, 58, -1, 33, 799999999, 49998, -1, 889, 489, -1, 79, 57, -1, 34, 899999999, 99989, -1, 788, 95, -1, 65, 56, -1, 35, 999999999, 59998, 1997, 687, 286, 74, 78, 55, 41, 36, 1999999999, 199989, -1, 496, 387, -1, 64, 54, -1, 37, 2999999999, 69998, -1, 989, 488, -1, 77, 53, -1, 38, 3999999999, 299989, 2997, 888, 589, 85, 63, 52, -1, 39, 4999999999, 79998, -1, 787, 195, -1, 76, 69, -1, 40, 5999999999, 399989, -1, 596, 386, -1, 89, 68, -1, 41, 6999999999, 89998, 3997, 1989, 487, 84, 75, 67, -1, 42, 7999999999, 499989, -1, 988, 588, -1, 88, 66, -1, 43, 8999999999, 99998, -1, 887, 689, -1, 74, 65, -1, 44, 9999999999, 599989, 4997, 696, 295, 185, 87, 64, 51, 45, 19999999999, 199998, -1, 2989, 486, -1, 73, 63, -1, 46, 29999999999, 699989, -1, 1988, 587, -1, 86, 62, -1, 47, 39999999999, 299998, 5997, 987, 688, 94, 189, 79, -1, 48, 49999999999, 799989, -1, 796, 789, -1, 85, 78, -1, 49, 59999999999, 399998, -1, 3989, 395, -1, 188, 77, -1, 50, 69999999999, 899989, 6997, 2988, 586, 285, 84, 76, -1, 51, 79999999999, 499998, -1, 1987, 687, -1, 187, 75, -1, 52, 89999999999, 999989, -1, 896, 788, -1, 83, 74, -1, 53, 99999999999, 599998, 7997, 4989, 889, 194, 186, 73, 61, 54, 199999999999, 1999989, -1, 3988, 495, -1, 289, 72, -1, 55, 299999999999, 699998, -1, 2987, 686, -1, 185, 89, -1, 56, 399999999999, 2999989, 8997, 996, 787, 385, 288, 88, -1, 57, 499999999999, 799998, -1, 5989, 888, -1, 184, 87, -1, 58, 599999999999, 3999989, -1, 4988, 989, -1, 287, 86, -1, 59, 699999999999, 899998, 9997, 3987, 595, 294, 93, 85, -1, 60, 799999999999, 4999989, -1, 1996, 786, -1, 286, 84, -1, 61, 899999999999, 999998, -1, 6989, 887, -1, 389, 83, -1, 62, 999999999999, 5999989, 19997, 5988, 988, 485, 285, 82, 71, 63, 1999999999999, 1999998, -1, 4987, 1989, -1, 388, 189, -1, 64, 2999999999999, 6999989, -1, 2996, 695, -1, 284, 188, -1, 65, 3999999999999, 2999998, 29997, 7989, 886, 394, 387, 187, -1, 66, 4999999999999, 7999989, -1, 6988, 987, -1, 193, 186, -1, 67, 5999999999999, 3999998, -1, 5987, 1988, -1, 386, 185, -1, 68, 6999999999999, 8999989, 39997, 3996, 2989, 585, 489, 184, -1, 69, 7999999999999, 4999998, -1, 8989, 795, -1, 385, 183, -1, 70, 8999999999999, 9999989, -1, 7988, 986, -1, 488, 92, -1, 71, 9999999999999, 5999998, 49997, 6987, 1987, 494, 384, 289, 81, 72, 19999999999999, 19999989, -1, 4996, 2988, -1, 487, 288, -1, 73, 29999999999999, 6999998, -1, 9989, 3989, -1, 293, 287, -1, 74, 39999999999999, 29999989, 59997, 8988, 895, 685, 486, 286, -1, 75, 49999999999999, 7999998, -1, 7987, 1986, -1, 589, 285, -1, 76, 59999999999999, 39999989, -1, 5996, 2987, -1, 485, 284, -1, 77, 69999999999999, 8999998, 69997, 19989, 3988, 594, 588, 283, -1, 78, 79999999999999, 49999989, -1, 9988, 4989, -1, 484, 192, -1, 79, 89999999999999, 9999998, -1, 8987, 995, -1, 587, 389, -1, 80, 99999999999999, 59999989, 79997, 6996, 2986, 785, 393, 388, 91, 81, 199999999999999, 19999998, -1, 29989, 3987, -1, 586, 387, -1, 82, 299999999999999, 69999989, -1, 19988, 4988, -1, 689, 386, -1, 83, 399999999999999, 29999998, 89997, 9987, 5989, 694, 585, 385, -1, 84, 499999999999999, 79999989, -1, 7996, 1995, -1, 688, 384, -1, 85, 599999999999999, 39999998, -1, 39989, 3986, -1, 584, 383, -1, 86, 699999999999999, 89999989, 99997, 29988, 4987, 885, 687, 292, -1, 87, 799999999999999, 49999998, -1, 19987, 5988, -1, 493, 489, -1, 88, 899999999999999, 99999989, -1, 8996, 6989, -1, 686, 488, -1, 89, 999999999999999, 59999998, 199997, 49989, 2995, 794, 789, 487, 191, 90, 1999999999999999, 199999989, -1, 39988, 4986, -1, 685, 486, -1, 181, 2999999999999999, 69999998, -1, 29987, 5987, -1, 788, 485, -1, 182, 3999999999999999, 299999989, 299997, 9996, 6988, 985, 684, 484, -1, 183, 4999999999999999, 79999998, -1, 59989, 7989, -1, 787, 483, -1, 184, 5999999999999999, 399999989, -1, 49988, 3995, -1, 593, 392, -1, 185, 6999999999999999, 89999998, 399997, 39987, 5986, 894, 786, 589, -1, 186, 7999999999999999, 499999989, -1, 19996, 6987, -1, 889, 588, -1, 187, 8999999999999999, 99999998, -1, 69989, 7988, -1, 785, 587, -1, 188, 9999999999999999, 599999989, 499997, 59988, 8989, 1985, 888, 586, 291, 189, 19999999999999999, 199999998, -1, 49987, 4995, -1, 784, 585, -1, 190, 29999999999999999, 699999989, -1, 29996, 6986, -1, 887, 584, -1, 281, 39999999999999999, 299999998, 599997, 79989, 7987, 994, 693, 583, -1, 282, 49999999999999999, 799999989, -1, 69988, 8988, -1, 886, 492, -1, 283, 59999999999999999, 399999998, -1, 59987, 9989, -1, 989, 689, -1, 284, 69999999999999999, 899999989, 699997, 39996, 5995, 2985, 885, 688, -1, 285}; int main() { int n, m, q, k; cin >> q; while (q--) { cin >> n >> k; cout << ans[(n - 1) * 10 + k]; cout << '\n'; } }
7
#include <bits/stdc++.h> using namespace std; using lli = long long int; const int MOD = 1000000007; const int MOD1 = 998244353; const int maxn = 100010; const int lim = (int)1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n = 0; lli A = 0ll; cin >> n >> A; vector<int> d(n); for (auto &i : d) cin >> i; lli sum = accumulate(d.begin(), d.end(), 0ll); for (int i = 0; i < n; ++i) { lli res = 0ll, mx = sum - d[i], r = A - mx; if (r > 1ll) res += (r - 1ll); lli mi = n - 1ll; r = A - mi; if (r < d[i]) res += (d[i] - r); cout << res << " "; } }
4
#include <bits/stdc++.h> using namespace std; bool isGreater(int x, int y) { cout << 2 << ' ' << 1 << ' ' << x << ' ' << y << '\n'; cout.flush(); int answer; cin >> answer; return answer == 1; } long long getArea(int x, int y) { cout << 1 << ' ' << 1 << ' ' << x << ' ' << y << '\n'; cout.flush(); long long answer; cin >> answer; return answer; } int main() { int n; cin >> n; int first = 1; int second = 2; for (int i = 3; i <= n; ++i) { if (isGreater(i, second)) { second = i; } } vector<pair<long long, int>> areas; pair<long long, int> best = {-1, -1}; for (int i = 1; i <= n; ++i) { if (i == first or i == second) continue; areas.push_back({getArea(second, i), i}); if (best.first < areas.back().first) { best = areas.back(); } } vector<pair<long long, int>> left, right; for (auto x : areas) { if (x == best) continue; if (isGreater(x.second, best.second)) left.push_back(x); else right.push_back(x); } sort(left.begin(), left.end()); sort(right.begin(), right.end()); reverse(right.begin(), right.end()); cout << 0 << ' '; cout << first << ' ' << second << ' '; for (auto x : left) cout << x.second << ' '; cout << best.second << ' '; for (auto x : right) cout << x.second << ' '; cout.flush(); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 20; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const long long LLINF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1.0); const double RATE = 0.995; const double eps = 1e-15; int to[4][2] = {1, 0, 0, 1}; string g[MAXN]; vector<bool> used[MAXN]; int n, m; bool dfs(int x, int y) { if (x == n - 1 && y == m - 1) { return true; } for (int i = 0; i < 2; ++i) { int nx = x + to[i][0]; int ny = y + to[i][1]; if (nx < n && nx >= 0 && ny < m && ny >= 0 && g[nx][ny] == '.' && !used[nx][ny]) { used[nx][ny] = true; if (dfs(nx, ny)) { g[x][y] = '#'; return true; } } } return false; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> g[i]; for (int j = 0; j < m; ++j) { used[i].push_back(false); } } if (!dfs(0, 0)) { puts("0"); return 0; } for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) used[i][j] = false; if (!dfs(0, 0)) { puts("1"); } else puts("2"); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 10; int a[MAXN], l[MAXN], r[MAXN]; void solve() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } l[1] = 1; for (int i = 2; i <= n; i++) { l[i] = (a[i] >= a[i - 1] ? l[i - 1] + 1 : 1); } r[n] = 1; for (int i = n - 1; i >= 1; i--) { r[i] = (a[i] >= a[i + 1] ? r[i + 1] + 1 : 1); } int ans = -1; for (int i = 1; i <= n; i++) { ans = max(ans, l[i] + r[i] - 1); } printf("%d\n", ans); } int main() { solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int mod = 1e9 + 7; long long int powmod(long long int a, long long int b) { long long int res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } int nxt() { int x; scanf("%d", &x); return x; } int main() { int tc = nxt(); while (tc--) { int n = nxt(); map<int, int> m, c; for (int i = 0; i < n; i++) { int a = nxt(), b = nxt(); m[a]++; if (b) c[a]++; } int high = 0; vector<pair<int, int> > order; for (auto x : m) { high = max(high, x.second); order.push_back({x.second, c[x.first]}); } sort((order).begin(), (order).end()); reverse((order).begin(), (order).end()); int ptr = 0; int a1 = 0, a2 = 0; multiset<int> cnt; for (int height = high; height > 0; height--) { while (ptr < order.size() && order[ptr].first >= height) { cnt.insert(order[ptr].second); ptr++; } if (cnt.size()) { a1 += height; int tmp = *cnt.rbegin(); a2 += min(height, tmp); cnt.erase(cnt.find(tmp)); } } printf("%d %d\n", a1, a2); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int x, y; cin >> x >> y; int a = y, b = y, c = y; int op = 1; a = b + c - 1; int flag = 0; if (a >= x) { a = x; flag = 1; } while (flag == 0) { if (op % 3 == 1) { b = a + c - 1; if (b >= x) { b = x; flag = 1; } op++; } else if (op % 3 == 2) { c = a + b - 1; if (c >= x) { c = x; flag = 1; } op++; } else { a = b + c - 1; if (a >= x) { a = x; flag = 1; } op++; } } if (a != x) op++; if (b != x) op++; if (c != x) op++; cout << op << endl; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; int mob[maxn]; vector<int> v1[maxn]; bool isPrime[maxn]; bool sq[maxn]; int fact[maxn], rfact[maxn]; int pw(int a, int b) { if (b == 0) return 1; int temp = pw(a, b / 2) % 1000000007; if (b % 2) { return (1ll * (1ll * a * temp) % 1000000007 * temp) % 1000000007; } return (1ll * temp * temp) % 1000000007; } void init() { memset(isPrime, 1, sizeof(isPrime)); isPrime[1] = 0; isPrime[0] = 0; for (int i = 1; i < maxn; i++) { for (int j = i; j < maxn; j += i) { v1[j].push_back(i); if (j != i and i != 1) { isPrime[j] = 0; } } } for (int i = 2; i * i < maxn; i++) { int k = i * i; for (int j = k; j < maxn; j += k) { sq[j] = 1; } } mob[1] = 1; for (int i = 2; i < maxn; i++) { if (sq[i]) { mob[i] = 0; continue; } int parity = 0; for (auto val : v1[i]) { if (isPrime[val]) parity++; } mob[i] = (parity % 2 == 1) ? -1 : 1; } fact[0] = 1; fact[1] = 1; rfact[1] = 1; rfact[0] = 1; for (int i = 2; i <= maxn - 1; i++) { fact[i] = (1ll * i * fact[i - 1]) % 1000000007; rfact[i] = pw(fact[i], 1000000007 - 2); } } void solve() { int n, f; scanf("%d", &n); scanf("%d", &f); int ans = 0; for (auto val : v1[n]) { int k = val; if (k < f) continue; int z = k - f; int temp = (1ll * (1ll * fact[k - 1] * rfact[z]) % 1000000007 * rfact[f - 1]) % 1000000007; if (mob[n / val] == 1) { ans = (0ll + ans + temp) % 1000000007; } else if (mob[n / val] == -1) { ans = (0ll + ans - temp + 1000000007) % 1000000007; } } printf("%d\n", ans); } int main() { init(); int t = 1; scanf("%d", &t); while (t--) { solve(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 110, M = 1010; int i, j, k, l, m, n, o, p, K, Sx, Sy, Tx, Ty, w[N][N]; char s[N][N], c[N]; struct Point { int x, y; Point() {} Point(int X, int Y) { x = X; y = Y; } } P[N], Ans; inline void Init() { int x1, y1, x2, y2; cin >> n >> m >> K; for (i = 1; i <= n; i++) { getchar(); scanf("%s", s[i]); for (j = m; j >= 1; j--) s[i][j] = s[i][j - 1]; } scanf("%d%d%s%d%d", &x1, &y1, c, &x2, &y2); Sx = x1; Sy = y1; Tx = x2; Ty = y2; l = strlen(c); for (i = l; i >= 1; i--) c[i] = c[i - 1]; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) if ((s[i][j] >= 'a') && (s[i][j] <= 'z')) { P[s[i][j] - 'a' + 1] = Point(i, j); w[i][j] = 1; } else if ((s[i][j] >= '0') && (s[i][j] <= '9')) w[i][j] = s[i][j] - '0'; } inline Point Solve() { int x, y, i, nx, ny, k, o, p, now; x = Sx; y = Sy; i = 0; now = 0; while (1) { if (i < l) { k = c[i + 1] - 'a' + 1; nx = P[k].x; ny = P[k].y; } else { nx = Tx; ny = Ty; } if (nx == x) { o = 0; if (ny > y) p = 1; else p = -1; } else { p = 0; if (nx > x) o = 1; else o = -1; } while ((x != nx) || (y != ny)) { if (now >= K) return Point(x, y); now += w[x][y]; if (now > K) return Point(x, y); x += o; y += p; } i++; if (i == l + 1) break; } return Point(x, y); } int main() { Init(); Ans = Solve(); printf("%d %d\n", Ans.x, Ans.y); fclose(stdin); fclose(stdout); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 4000 + 10; const int MOD = 1e9 + 7; int dp[MAXN][MAXN], N; int main() { while (scanf("%d", &N) != EOF) { dp[0][0] = 1; for (int i = 1; i <= N; i++) { dp[i][0] = dp[i - 1][i - 1]; for (int j = 1; j <= i; j++) dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % MOD; } cout << dp[N][N - 1] << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) { cout << "white" << endl; cout << 1 << " " << 2 << endl; } else cout << "black" << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n, k; char s[10]; int ask(long long l, long long r) { printf("%lld %lld\n", l, r); fflush(stdout); scanf("%s", s); return s[0] == 'Y'; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { scanf("%lld%lld", &n, &k); long long l = 1, r = n; while (1) { if (r - l <= 5 * k) { long long tmp = l + rng() % (r - l + 1); if (ask(tmp, tmp)) { return 0; } else { l = max(1ll, l - k); r = min(n, r + k); } continue; } long long mid = (l + r) / 2; if (ask(l, mid)) { l = max(1ll, l - k); r = min(n, mid + k); } else { l = max(1ll, mid + 1 - k); r = min(n, r + k); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 128; const int P = 1000000007; int dp[N][8][2]; int ans[N][N], tmp[N][N], mul[N][N]; int main() { for (int i = 0; i <= N - 1; i++) { dp[i][0][0] = 0; dp[i][0][1] = 1; for (int j = 1; j <= 7; j++) { dp[i][j][0] = dp[i][j - 1][0] + dp[i][j - 1][1]; if ((i >> (j - 1)) & 1) dp[i][j][1] = dp[i][j - 1][0]; else dp[i][j][1] = dp[i][j][0]; } } ans[0][0] = 1; for (int c = 1; c <= 7; c++) { int val; cin >> val; int M = (1 << c) - 1; for (int i = 0; i <= M / 2; i++) ans[0][M / 2 + 1 + i] = ans[0][i], ans[0][i] = 0; for (int i = 0; i <= M; i++) for (int j = 0; j <= M; j++) mul[i][j] = dp[i & j][c][1]; for (; val; val >>= 1) { if (val & 1) { for (int i = 0; i <= M; i++) for (int j = 0; j <= M; j++) tmp[i][j] = 0; for (int i = 0; i <= M; i++) for (int j = 0; j <= M; j++) for (int k = 0; k <= M; k++) { tmp[i][k] = (tmp[i][k] + (long long)ans[i][j] * mul[j][k]) % P; } for (int i = 0; i <= M; i++) for (int j = 0; j <= M; j++) ans[i][j] = tmp[i][j]; } for (int i = 0; i <= M; i++) for (int j = 0; j <= M; j++) tmp[i][j] = 0; for (int i = 0; i <= M; i++) for (int j = 0; j <= M; j++) for (int k = 0; k <= M; k++) { tmp[i][k] = (tmp[i][k] + (long long)mul[i][j] * mul[j][k]) % P; } for (int i = 0; i <= M; i++) for (int j = 0; j <= M; j++) mul[i][j] = tmp[i][j]; } } cout << ans[0][N - 1] << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 120005; int n, a[N], b[N], c[N], l[N], r[N], num[N]; bool check(int x) { num[a[x]]--; num[b[x]]++; num[c[x]]++; if (num[b[x]] <= 9 && num[c[x]] <= 9) return 1; num[a[x]]++; num[b[x]]--; num[c[x]]--; return 0; } int main() { scanf("%d", &n); for (int i = 1; i <= 4 * n; i++) { scanf("%d%d%d", &a[i], &b[i], &c[i]); l[i] = i - 1; r[i] = i + 1; } l[1] = 4 * n; r[4 * n] = 1; for (int i = 1; i <= n; i++) num[i] = 4; puts("YES"); for (int i = 1, j = 1; i <= 4 * n; i++) { for (; !check(j);) j = r[j]; r[l[j]] = r[j]; l[r[j]] = l[j]; printf("%d ", j); j = r[j]; } }
9
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double pi = acos(-1.0); const int maxn = (int)1e5 + 10; const int mod = (int)1e9; int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } long int lr[160][160], rl[160][160], dp[160][160][3], m; int call(int i, int j, int flg, int w) { if (w == 0) return 0; if (dp[i][j][flg] != -1) return dp[i][j][flg]; if (flg == 0 && lr[i][j - 1] > 0) return 1001001001; if (flg == 1 && rl[i][j + 1] > 0) return 1001001001; long mn = 1001001001, p, v, l, k; if (flg == 0) { p = 0; for (k = j; k < m && lr[i][k] < lr[i][m]; k++) p++; l = p + 1; if (w - lr[i][m] == 0) return p; for (; k <= m; k++) { v = l + call(i + 1, k, 1, w - lr[i][m]); l++; mn = (mn < v ? mn : v); } } else { p = 0; for (k = j; k > 0 && rl[i][k] < rl[i][1]; k--) p++; l = p + 1; if (w - rl[i][1] == 0) return p; for (; k > 0; k--) { v = l + call(i + 1, k, 0, w - rl[i][1]); l++; mn = (mn < v ? mn : v); } } dp[i][j][flg] = mn; return mn; } int main() { int w = 0, n, i, j; memset(dp, -1, sizeof(dp)); string str; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> str; for (j = 1; j <= m; j++) if (str[j - 1] == 'W') lr[i][j] = lr[i][j - 1] + 1; else lr[i][j] = lr[i][j - 1]; for (j = m; j > 0; j--) if (str[j - 1] == 'W') rl[i][j] = rl[i][j + 1] + 1; else rl[i][j] = rl[i][j + 1]; w = w + lr[i][m]; } w = call(1, 1, 0, w); cout << w << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; long long modexp(long long a, long long ex, long long md) { long long ans = 1; while (ex > 0) { if (ex & 1ll) ans = (ans * a) % md; ex >>= 1ll; a = (a * a) % md; } return ans; } vector<long long> mods; int main() { ios::sync_with_stdio(false); cin.tie(0); ; long long a, b, p, x; cin >> a >> b >> p >> x; for (long long k = 0; k < p - 1; k++) { long long c = modexp(a, k, p); long long q = modexp(c, p - 2, p); q = (q * b) % p; long long z = (q * (p - 1) * (p - 1)) % (p * (p - 1)); z = (z + k * p) % (p * (p - 1)); mods.push_back(z); } long long s = p * (p - 1); sort(mods.begin(), mods.end()); mods.resize(unique(mods.begin(), mods.end()) - mods.begin()); long long ans = 0; for (long long y : mods) { if (x < y) continue; ans += (x - y) / s + 1; } cout << ans << endl; }
6
#include <bits/stdc++.h> using namespace std; template <class T> T sqr(T x) { return x * x; } template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } template <class T> inline T bigmod(T p, T e, T M) { if (e == 0) return 1; if (e % 2 == 0) { long long int t = bigmod(p, e / 2, M); return (T)((t * t) % M); } return (T)((long long int)bigmod(p, e - 1, M) * (long long int)p) % M; } template <class T> inline T bigexp(T p, T e) { if (e == 0) return 1; if (e % 2 == 0) { long long int t = bigexp(p, e / 2); return (T)((t * t)); } return (T)((long long int)bigexp(p, e - 1) * (long long int)p); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } int dx4[] = {1, 0, -1, 0}; int dy4[] = {0, 1, 0, -1}; int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}; int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1}; int nx8[] = {1, 1, -1, -1, 2, 2, -2, -2}; int ny8[] = {2, -2, 2, -2, 1, -1, 1, -1}; int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; long long int n, res, rem; int main() { scanf("%I64d", &n); res = (long long int)n / 3LL; res *= 2LL; rem = n % 3; if (rem != 0) res++; cout << res; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long N = 100005; long long n, m, a[N], tot, ans, k; struct node { long long p, data, flag; } opt[N]; vector<node> v[N]; struct Node { long long id, flag; double val; } c[N]; bool cmp(const node &x, const node &y) { return x.data > y.data; } bool cmp1(const Node &x, const Node &y) { return x.val > y.val; } bool cmp2(const Node &x, const Node &y) { return x.flag < y.flag; } int main() { scanf("%lld%lld%lld", &k, &n, &m); for (long long i = 1; i <= k; i++) scanf("%lld", &a[i]); long long x, y, z; for (long long i = 1; i <= n; i++) { scanf("%lld%lld%lld", &x, &y, &z); if (x == 1 && z > a[y]) { if (opt[y].data < z) { opt[y].data = z; opt[y].p = i; opt[y].flag = x; } } if (x == 2 && z > 0) v[y].push_back((node){i, z, 2}); if (x == 3 && z > 1) { c[++tot].id = i; c[tot].val = (double)z; c[tot].flag = 3; } } for (long long i = 1; i <= k; i++) { if (opt[i].data > 0) { v[i].push_back((node){opt[i].p, opt[i].data - a[i], 1}); } sort(v[i].begin(), v[i].end(), cmp); for (long long j = 0; j < v[i].size(); j++) { node tmp = v[i][j]; c[++tot].id = tmp.p; c[tot].flag = tmp.flag; c[tot].val = (double(a[i] + tmp.data) / a[i]); a[i] += tmp.data; } } sort(c + 1, c + tot + 1, cmp1); ans = min(m, tot); sort(c + 1, c + ans + 1, cmp2); printf("%lld\n", ans); for (long long i = 1; i <= ans; i++) printf("%lld ", c[i].id); return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, i, s, a[1000]; int main() { cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); for (i = 1; i <= n; i++) if (a[i] != 0 && a[i] != a[i + 1]) s++; cout << s; }
0
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7, mod1 = 1e9 + 9; const long long N = 200005, LN = 18; long long logs[N], mx[N][20], mn[N][20], a[N], b[N]; long long n; void pre() { logs[1] = 0; for (long long i = 2; i <= (n); i++) { logs[i] = logs[i >> 1] + 1; } for (long long i = 1; i <= (n); i++) mx[i][0] = a[i]; for (long long i = 1; i <= (n); i++) mn[i][0] = b[i]; for (long long j = 1; j <= (18); j++) { for (long long i = 1; i <= (n); i++) { if ((i + (1 << j) - 1) > n) break; mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]); mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]); } } } long long calmax(long long l, long long r) { long long k = logs[r - l + 1]; return max(mx[l][k], mx[r - (1 << k) + 1][k]); } long long calmin(long long l, long long r) { long long k = logs[r - l + 1]; return min(mn[l][k], mn[r - (1 << k) + 1][k]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; cin >> n; for (long long i = 1; i <= (n); i++) cin >> a[i]; for (long long i = 1; i <= (n); i++) cin >> b[i]; pre(); long long ans = 0; for (long long i = 1; i <= (n); i++) { long long lo = i, hi = n; while (lo < hi) { long long mi = lo + hi >> 1; if (calmax(i, mi) < calmin(i, mi)) lo = mi + 1; else hi = mi; } if (calmax(i, lo) != calmin(i, lo)) continue; long long tm = lo; lo = i, hi = n; while (lo < hi) { long long mi = lo + hi + 1 >> 1; if (calmax(i, mi) > calmin(i, mi)) hi = mi - 1; else lo = mi; } ans += (lo - tm + 1); } cout << ans; return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, x; long long k; int main() { cin >> n; while (n--) { cin >> k >> x; cout << (k - 1) * 9 + x << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int MOD = 1e9 + 7; const double eps = 1e-9; int m[11]; double pos[11], v[11]; void collision(int i, int j) { double v1 = ((m[i] - m[j]) * v[i] + 2 * m[j] * v[j]) / (m[i] + m[j]); double v2 = ((m[j] - m[i]) * v[j] + 2 * m[i] * v[i]) / (m[i] + m[j]); v[i] = v1, v[j] = v2; } double time2col(int i, int j) { if (abs(v[i] - v[j]) < eps) return 1e18; if (abs(pos[i] - pos[j]) < eps) return 1e18; double res = (pos[j] - pos[i]) / (v[i] - v[j]); return res < 0 ? 1e18 : res; } int main() { cout << fixed; cout.precision(10); int n; double t; cin >> n >> t; for (int i = 0; i < n; ++i) { cin >> pos[i] >> v[i] >> m[i]; } double currt = 0.0; while (currt < t - eps) { double mint = t - currt; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { double t2col = time2col(i, j); if (t2col < mint - eps) mint = t2col; } } for (int i = 0; i < n; ++i) pos[i] += v[i] * mint; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (abs(pos[i] - pos[j]) < eps) collision(i, j); } } currt += mint; } for (int i = 0; i < n; ++i) cout << pos[i] << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; const int N = 1001; bitset<N> n6[N] = {0, 14 * 2, 28 * 2, 40 * 2, 48 * 2, 37 * 2, 3 * 2}, g[N]; int main(int argc, char const *argv[]) { int n; scanf("%d", &n); if (n == 4) return puts("-1"), 0; if (n % 2 == 0) { for (int i = 7; i <= n; i += 2) { for (int j = 1; j < i; ++j) { n6[i][j] = n6[j][i + 1] = 1; } n6[i + 1][i] = 1; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { printf("%d ", (int)n6[i][j]); } putchar('\n'); } } else { for (int i = 2; i <= n; i += 2) { for (int j = 1; j < i; ++j) { g[i][j] = g[j][i + 1] = 1; } g[i + 1][i] = 1; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { printf("%d ", (int)g[i][j]); } putchar('\n'); } } return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007; long long int cnt[200005]; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); long long int t = 1; while (t--) { long long int n, b, i = 0, m = 0, maxm = 0, k = 0, j = 0, flag = 0, x, summ = 0, q = 0; long double a; cin >> n >> a >> b >> k; long long int a1 = a; std::vector<long long int> h(n); for (i = 0; i < n; i++) { cin >> h[i]; h[i] = h[i] % (a1 + b); if (h[i] == 0) h[i] += a + b; h[i] = ceil(h[i] / a) - 1; } sort(h.begin(), h.end()); i = 0; maxm = 0; for (i = 0; i < n; i++) { if (k - h[i] < 0) break; maxm++; k = k - h[i]; } cout << maxm << endl; } }
3
#include <bits/stdc++.h> using namespace std; const int N = 1234; int n; char a[N]; void solve() { scanf("%d %s", &n, a); int ans = n; for (int i = 0; i < n; ++i) { int l = i + 1; int r = n - i; if (a[i] == '1') ans = max(ans, max(2 * l, 2 * r)); } printf("%d\n", ans); } int main() { int qq; scanf("%d", &qq); while (qq--) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long inf = 2e18 + 5; vector<long long> vec; void init() { for (long long i = 2; i <= 1e6; i++) { long long ret = i * i; for (int j = 3; j <= 63; j++) { if (inf / i < ret) break; ret *= i; if (ret > ((long long)sqrt(ret)) * ((long long)sqrt(ret))) vec.push_back(ret); } } sort(vec.begin(), vec.end()); unique(vec.begin(), vec.end()) - vec.begin(); } int main() { init(); int t; scanf("%d", &t); while (t--) { long long l, r; scanf("%lld%lld", &l, &r); long long ans = (upper_bound(vec.begin(), vec.end(), r) - vec.begin()) - (lower_bound(vec.begin(), vec.end(), l) - vec.begin()); printf("%lld\n", ans + (long long)sqrt(r) - (long long)sqrt(l - 1)); } return 0; }
6
#include <bits/stdc++.h> using namespace std; using vi = vector<long long>; using vs = vector<string>; using vvi = vector<vi>; using pii = pair<long long, long long>; using vpii = vector<pii>; using mii = map<long long, long long>; using umii = unordered_map<long long, long long>; template <class T> bool maxi(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool mini(T &a, T b) { return a < b ? (a = b, true) : false; } template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (long long i = 0; i < static_cast<long long>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n, m; cin >> n >> m; vi a(n); for (long long i = (0); (1) > 0 ? i < (n) : i > (n); i += (1)) cin >> a[i]; sort((a).begin(), (a).end()); reverse((a).begin(), (a).end()); long long sum = 0; long long cnt = 0; for (long long i = (0); (1) > 0 ? i < (n) : i > (n); i += (1)) { sum += a[i]; cnt++; if (sum >= m) break; } cout << cnt; cerr << "\n\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
0
#include <bits/stdc++.h> struct Range { struct It { int num, step; int operator*() { return num; } void operator++() { num += step; } bool operator!=(const It& other) { return num != other.num; } }; Range(int ee) : b(0), e(ee) {} Range(int bb, int ee) : b(bb), e(ee) {} It begin() { return {b, (b < e ? 1 : -1)}; } It end() { return {e, 0}; } int b, e; }; template <typename T> inline T& UMAX(T& x, T y) { if (x < y) x = y; return x; } template <typename T> inline T& UMIN(T& x, T y) { if (y < x) x = y; return x; } template <typename T, typename... Args> struct ArithmiticPromotion { typedef decltype(T() + typename ArithmiticPromotion<Args...>::type()) type; }; template <typename T, typename U> struct ArithmiticPromotion<T, U> { typedef decltype(T() + U()) type; }; template <typename T> struct ArithmiticPromotion<T, T> {}; template <typename T> struct ArithmiticPromotion<T> {}; template <typename T, typename U> typename ArithmiticPromotion<T, U>::T MAX(T a, U b) { return a < b ? b : a; } template <typename T, typename... Args> typename ArithmiticPromotion<T, Args...>::T MAX(T a, Args... args) { return MAX(a, MAX(args...)); } template <typename T, typename U> typename ArithmiticPromotion<T, U>::T MIN(T a, U b) { return a < b ? a : b; } template <typename T, typename... Args> typename ArithmiticPromotion<T, Args...>::T MIN(T a, Args... args) { return MIN(a, MIN(args...)); } template <typename T> struct ScanfSpecifier {}; template <> struct ScanfSpecifier<char*> { static constexpr const char* value = "%s"; }; template <> struct ScanfSpecifier<int> { static constexpr const char* value = "%d"; }; template <> struct ScanfSpecifier<double> { static constexpr const char* value = "%lf"; }; template <> struct ScanfSpecifier<float> { static constexpr const char* value = "%f"; }; template <> struct ScanfSpecifier<char> { static constexpr const char* value = "%c"; }; template <> struct ScanfSpecifier<const char*> { static constexpr const char* value = "%s"; }; template <> struct ScanfSpecifier<unsigned long> { static constexpr const char* value = "%lu"; }; template <> struct ScanfSpecifier<unsigned int> { static constexpr const char* value = "%u"; }; template <> struct ScanfSpecifier<long long int> { static constexpr const char* value = "%lld"; }; template <typename T> int RD(T& arg) { return std::scanf(ScanfSpecifier<T>::value, &arg); } template <int S> int RD(char (&arg)[S]) { return std::scanf("%s", arg); } int RD(char* arg) { return std::scanf("%s", arg); } template <> int RD<char>(char& arg) { return std::scanf(" %c", &arg); } template <typename T, typename... Args> int RD(T& arg1, Args&... args) { return RD(arg1) + RD(args...); } template <typename T> T RD() { T ret; RD(ret); return ret; } template <typename It> void RDV(It begin, It end) { while (begin != end) RD(*begin++); } template <typename C> void RDV(C& c) { RDV(std::begin(c), std::end(c)); } template <typename T> void WT(T arg) { std::printf(ScanfSpecifier<T>::value, arg); } template <typename T, typename U> void WT(std::pair<T, U> arg) { std::printf("("); WT(arg.first); std::printf(", "); WT(arg.second); std::printf(")"); } template <typename... Args> void WT(Args... args) { int alc = 0; int dummy[] = {((alc++ ? std::printf(" ") : 0), WT(args), 0)...}; } template <typename... Args> void WTL(Args... args) { WT(args...); std::printf("\n"); } template <typename It> void WTV(It begin, It end) { int alc = 0; while (begin != end) (alc++ ? std::printf(" ") : 0), WT(*begin++); } template <typename C> void WTV(const C& c) { WTV(std::begin(c), std::end(c)); } template <typename It> void WTVL(It begin, It end) { WTV(begin, end); std::printf("\n"); } template <typename C> void WTVL(const C& c) { WTVL(std::begin(c), std::end(c)); } template <unsigned int Z> struct ZpZ { static const unsigned int P = Z; using U64 = unsigned long long int; U64 value; ZpZ(long long int n) { if (std::abs(n) >= P) n %= P; if (n < 0) n += P; value = n; } ZpZ() : value(0) {} ZpZ& operator+=(ZpZ p) { if ((value += p.value) >= P) value -= P; return *this; } ZpZ& operator-=(ZpZ p) { if (value >= p.value) value -= p.value; else value += P - p.value; return *this; } ZpZ& operator*=(ZpZ p) { value = value * p.value % P; return *this; } ZpZ& operator/=(ZpZ p) { value = value * p.inverse().value % P; return *this; } bool operator==(ZpZ p) { return value == p.value; } bool operator!=(ZpZ p) { return value != p.value; } ZpZ operator+(ZpZ r) const { return ZpZ(value) += r; } ZpZ operator-(ZpZ r) const { return ZpZ(value) -= r; } ZpZ operator*(ZpZ r) const { return ZpZ(value) *= r; } ZpZ operator/(ZpZ r) const { return ZpZ(value) /= r; } explicit operator long long int() const { return value; } explicit operator int() const { return value; } ZpZ operator-() { return P - value; } ZpZ inverse() { int a = value, b = P, u = 1, v = 0; while (b) { int t = a / b; std::swap(a -= t * b, b); std::swap(u -= t * v, v); } if (u < 0) u += P; return u; } ZpZ pow(U64 e) { U64 ret = 1, base = value; while (e) { if ((e & 1) && ((ret *= base) >> 32)) ret %= P; if ((base *= base) >> 32) base %= P; e >>= 1; } return ret; } }; template <unsigned int P> void WT(ZpZ<P> arg) { WT((int)arg); } const int MOD = 1e9 + 7; using ZZ = ZpZ<MOD>; ZZ min(ZZ a, ZZ b) { return std::min(a.value, b.value); } ZZ max(ZZ a, ZZ b) { return std::max(a.value, b.value); } using RG = Range; using namespace std; ZZ dp[100009][11][3]; vector<int> adj[100009]; int N, M; int K, X; void f(int u, int p) { for (int to : adj[u]) if (to != p) f(to, u); ZZ now[11] = {}; now[0] = K - 1; for (int to : adj[u]) if (to != p) { ZZ tmp[11] = {}; for (int i : RG(X + 1)) for (int j : RG(X + 1)) if (i + j <= X) tmp[i + j] += now[i] * (dp[to][j][0] + dp[to][j][1] + dp[to][j][2]); for (int i : RG(X + 1)) now[i] = tmp[i]; } for (int i : RG(X + 1)) dp[u][i][0] = now[i]; memset(now, 0, sizeof(now)); now[1] = 1; for (int to : adj[u]) if (to != p) { ZZ tmp[11] = {}; for (int i : RG(X + 1)) for (int j : RG(X + 1)) if (i + j <= X) tmp[i + j] += now[i] * dp[to][j][0]; for (int i : RG(X + 1)) now[i] = tmp[i]; } for (int i : RG(X + 1)) dp[u][i][1] = now[i]; memset(now, 0, sizeof(now)); now[0] = M - K; for (int to : adj[u]) if (to != p) { ZZ tmp[11] = {}; for (int i : RG(X + 1)) for (int j : RG(X + 1)) if (i + j <= X) tmp[i + j] += now[i] * (dp[to][j][0] + dp[to][j][2]); for (int i : RG(X + 1)) now[i] = tmp[i]; } for (int i : RG(X + 1)) dp[u][i][2] = now[i]; } int main() { RD(N, M); for (int i : RG(N - 1)) { int u, v; RD(u, v); adj[u].push_back(v); adj[v].push_back(u); } RD(K, X); f(1, 0); ZZ ans = 0; for (int i : RG(X + 1)) ans += dp[1][i][0] + dp[1][i][1] + dp[1][i][2]; WTL(ans); }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 5; long long s[maxn], a[maxn], l, r, n, q, b[maxn], m; bool cmp(long long a, long long b) { return a < b; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; } sort(s + 1, s + n + 1, cmp); for (int i = 1; i < n; i++) { a[i] = s[i + 1] - s[i]; } sort(a + 1, a + n, cmp); for (int i = 1; i < n; i++) { b[i] = b[i - 1] + a[i]; } cin >> q; m = n - 1; for (int i = 1; i <= q; i++) { cin >> l >> r; long long tp = lower_bound(a + 1, a + n, (r - l + 1)) - a; tp--; long long ans = b[tp] + (m - tp) * (r - l + 1) + (r - l + 1); cout << ans << ' '; } }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, a[105]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (a[0] == a[(n / 2) - 1] && a[n / 2] == a[n - 1] && a[n / 2] != a[(n / 2) - 1]) cout << "YES\n" << a[0] << " " << a[n - 1] << endl; else cout << "NO\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long mod = (1e9 + 7); long long power(long long n, long long p) { long long ans = 1; while (p) { if (p & 1) { ans = (ans * n) % mod; } p >>= 1; n = (n * n) % mod; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; for (long long i = 1; i < sqrt(n); i++) { long long a = (i * (i + 1)) / 2; long long lo = 1, hi = n; while (lo <= hi) { long long mid = (lo + hi) / 2; long long x = (mid * (mid + 1)) / 2; if (n == a + x) { cout << "YES"; return 0; } else if (a + x > n) hi = mid - 1; else lo = mid + 1; } } cout << "NO"; }
2
#include <bits/stdc++.h> using namespace std; const int logN = 32, M = (int)5e4 + 5; int n, m, k, tk; struct tree { int cov, s; tree *l, *r; } mem[M * logN * 3], *newtree = mem; tree *root = newtree++; int L, R, D; void update(tree *cur, int beg, int end) { if (cur->cov) cur->s = end ^ beg; else cur->s = (cur->l ? cur->l->s : 0) ^ (cur->r ? cur->r->s : 0); } void cover(tree *&cur, int beg, int end) { if (R <= beg || L >= end) return; if (!cur) cur = newtree++; if (L <= beg && R >= end) cur->cov += D; else { int mid = beg + end >> 1; cover(cur->l, beg, mid); cover(cur->r, mid, end); } update(cur, beg, end); } struct data { int x, l, r, d; bool operator<(const data &A) const { return x < A.x; } } seq[M * 2], *newdata = seq; int main() { scanf("%d%d%d", &n, &m, &tk); for (k = 0; (1 << k) <= tk; k++) ; k--; while (m--) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); a--, b--; *newdata++ = (data){a, b, d, 1}; *newdata++ = (data){c, b, d, -1}; } sort(seq, newdata); int ans = 0; for (int i = 0; i < newdata - seq; i++) { if (i) { int s = seq[i].x ^ seq[i - 1].x, sum = 0, now = root->s; s ^= s >> 1; now ^= now >> 1; for (int j = 29; j >= 0; j--) { if (now >> (j + 1) & 1) now ^= 3 << j; if (s >> j & 1) sum ^= now; } ans ^= sum; } L = seq[i].l, R = seq[i].r, D = seq[i].d; cover(root, 0, n); } for (int i = 29; i >= k; i--) { if (ans >> (i + 1) & 1) ans ^= 3 << i; } puts(ans ? "Hamed" : "Malek"); }
12