solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int convertToint(string str) { stringstream ss; ss << str; int num; ss >> num; return num; } string convertTostr(int x) { stringstream ss; ss << x; string st; st = ss.str(); return st; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int n; string ans = "a"; cin >> n; int arr[4] = {1, 2, 3, 4}; string arr2[4] = {"a", "b", "c", "d"}; for (int i = 2; i <= n; i++) { for (int j = 0; j < 4; j++) { if (i == arr[j]) { ans += arr2[j]; break; } else if (i == arr[j] + 4) { ans += arr2[j]; arr[j] = i; break; } } } cout << ans; return 0; }
2
#include<bits/stdc++.h> using namespace std; int main(){ int n,m,i; cin>>n>>m; int u[n],a[m]; memset(u,0,sizeof(u)); for(i=0;i<m;i++) scanf("%d",a+i); for(i=m-1;i>=0;i--) if(!u[a[i]-1]) printf("%d\n",a[i]),u[a[i]-1]=1; for(i=0;i<n;i++) if(!u[i]) printf("%d\n",i+1); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2050; const int MOD = 1e9 + 7; int n, k, s[N]; int dp[N][N][2]; int solve() { for (int i = 1; i <= n; i++) { if (s[i] == 2 || s[i] == 0) { for (int j = 0; j < N; j++) { int p = min(j + 2, 2049); dp[i][p][1] = (dp[i][p][1] + dp[i - 1][j][1]) % MOD; dp[i][p][0] = (dp[i][p][0] + dp[i - 1][j][0]) % MOD; } } if (s[i] == 4 || s[i] == 0) { for (int j = 0; j < N; j++) { int p = min(j + 4, 2049); if (j % 4) { if (j >= (1 << k)) dp[i][4][1] = (dp[i][4][1] + dp[i - 1][j][0]) % MOD; else dp[i][4][0] = (dp[i][4][0] + dp[i - 1][j][0]) % MOD; dp[i][4][1] = (dp[i][4][1] + dp[i - 1][j][1]) % MOD; } else { dp[i][p][0] = (dp[i][p][0] + dp[i - 1][j][0]) % MOD; dp[i][p][1] = (dp[i][p][1] + dp[i - 1][j][1]) % MOD; } } } } int ans = 0; for (int i = 0; i < 2050; i++) { ans = (ans + dp[n][i][1]) % MOD; if (i >= (1 << k)) ans = (ans + dp[n][i][0]) % MOD; } return ans; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &s[i]); memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; printf("%d\n", solve()); return 0; }
4
#include <bits/stdc++.h> using namespace std; int d[10001]; int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); d[a]++; d[b]++; } long long ans = 0; for (int i = 1; i <= n; i++) { ans += d[i] * (d[i] - 1) / 2; } printf("%I64d\n", ans); }
6
#include <bits/stdc++.h> using namespace std; int main() { int64_t h,w; cin >> h >> w; if(h==1 || w==1){ cout << 1 << endl; } else{ cout << ((h*w)+1)/2 << endl; } }
0
#include<bits/stdc++.h> using namespace std; int a,b,c; int main() { cin>>a>>b>>c; if(a==b&&b==c)cout<<1<<endl; else if(a==b||b==c||a==c)cout<<2<<endl; else cout<<3<<endl; }
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int64_t lol; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int64_t testc = 1; cin >> testc; while (testc--) { string x; cin >> x; long long temp = 0; for (auto i : x) { if (i == ':') { ++temp; } } for (long long i = 0; i < x.length(); i++) { if (x[i] == ':') { if (x[i + 1] == ':') { while (temp < 7) { ++temp; x = x.substr(0, i + 1) + ":" + x.substr(i + 1, x.size() - i - 1); } while (temp > 7) { --temp; x = x.substr(0, i) + x.substr(i + 1, x.size() - i - 1); } } } } x.push_back(':'); temp = 0; string out = ""; for (long long i = 1; i <= x.length(); i++) { if (x[i - 1] == ':') { long long zz = (i - temp - 1); long long zzz = zz; zz = 4 - zz; while (zz--) { out += '0'; } temp = i; for (long long ii = (i - zzz); ii < i; ii++) { out += x[ii - 1]; } out += ":"; } } out.pop_back(); cout << out << "\n"; } return EXIT_SUCCESS; }
2
#include <bits/stdc++.h> using namespace std; const int M = 1e5 + 10; const int MM = 2e3 + 10; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; ; const double eps = 1e-8; const double pi = acos(-1.0); int i, j, k, n, m, sum; int x; bool check(int mid1, int n, int b) { int m = (n - mid1) / b; if ((int)(n / m) >= b) return false; else return true; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &x); int flag = 0; if (x == 0) { printf("1 1\n"); continue; } for (int i = 1; i * i <= x; i++) { if (i * (int)(x / i) != x) continue; int x1 = i, x2 = x / i; if ((x1 + x2) % 2) continue; int a = (x1 + x2) / 2; int b = x2 - a; if (b == 0) continue; int lb = 0, ub = a / b; int mid; for (int k = 0; k < 12; k++) { mid = (ub + lb) / 2; if (check(mid, a, b)) lb = mid; else ub = mid; } int m = (a - mid) / b; if ((int)(a / m) == b) { printf("%d %d\n", a, m); flag = 1; break; } } if (!flag) printf("-1\n"); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 + 10; int n, k; int vec[MAXN], sec[MAXN], gec[MAXN], t[MAXN]; bool same() { for (int i = 0; i < n; i++) if (vec[i] != sec[i]) return false; return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> vec[i]; vec[i]--; } for (int i = 0; i < n; i++) { cin >> sec[i]; sec[i]--; } copy(vec, vec + n, gec); iota(vec, vec + n, 0); if (same()) { cout << "NO\n"; return 0; } bool fl = 0, ok = 0; int ttt = -1; for (int i = 0; i < k; i++) { { for (int j = 0; j < n; j++) t[j] = vec[gec[j]]; bool zoo = 1; for (int j = 0; j < n; j++) if (t[j] != sec[j]) zoo = 0; if (!zoo) ok = 1; zoo = 1; for (int j = 0; j < n; j++) t[gec[j]] = vec[j]; for (int j = 0; j < n; j++) if (t[j] != sec[j]) zoo = 0; if (!zoo) ok = 1; } for (int j = 0; j < n; j++) t[j] = vec[gec[j]]; copy(t, t + n, vec); if (same()) { ttt = i + 1; break; } } if (ttt != -1 && (ttt % 2 == k % 2)) if (ttt == k || ok) { cout << "YES\n"; return 0; } fl = 1; ok = 0; iota(vec, vec + n, 0); ttt = -1; for (int i = 0; i < k; i++) { { for (int j = 0; j < n; j++) t[j] = vec[gec[j]]; bool zoo = 1; for (int j = 0; j < n; j++) if (t[j] != sec[j]) zoo = 0; if (!zoo) ok = 1; zoo = 1; for (int j = 0; j < n; j++) t[gec[j]] = vec[j]; for (int j = 0; j < n; j++) if (t[j] != sec[j]) zoo = 0; if (!zoo) ok = 1; } for (int j = 0; j < n; j++) t[gec[j]] = vec[j]; copy(t, t + n, vec); if (same()) { ttt = i + 1; break; } } if (ttt != -1 && (ttt % 2 == k % 2)) if (ok) { cout << "YES\n"; return 0; } cout << "NO\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 320; int dir[8][2] = {{-1, 0}, {-1, +1}, {0, +1}, {+1, +1}, {+1, 0}, {+1, -1}, {0, -1}, {-1, -1}}; bool dp[MAXN][MAXN][8][32]; int n, t[32]; bool spot[MAXN][MAXN]; void Rec(int l, int x, int y, int d) { if (l > n) return; if (dp[x][y][d][l]) return; else dp[x][y][d][l] = true; for (int i = 1; i <= t[l]; ++i) { spot[x + i * dir[d][0]][y + i * dir[d][1]] = true; } int le = (d != 0) ? d - 1 : 7; int ri = (d != 7) ? d + 1 : 0; Rec(l + 1, x + dir[d][0] * t[l], y + dir[d][1] * t[l], le); Rec(l + 1, x + dir[d][0] * t[l], y + dir[d][1] * t[l], ri); } int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> t[i]; } Rec(1, 160, 160, 0); int cnt = 0; for (int i = 0; i < MAXN; ++i) { for (int j = 0; j < MAXN; ++j) { if (spot[i][j]) cnt++; } } cout << cnt << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long gcd(long long X, long long Y) { long long x = max(X, Y); long long y = min(X, Y); x = x % y; if (x == 0) { return y; } else return gcd(x, y); } long long fun(long long x, long long n, long long m) { long long c = x * (m - n) + 2 * n - m; return c; } int32_t main() { long long N, M, q; cin >> N >> M >> q; long long g = gcd(N, M); long long n = N / g; long long m = M / g; for (long long i = 0; i < q; i++) { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; long long f1 = fun(x1, n, m); long long f2 = fun(x2, n, m); long long s1, s2; if (y1 % f1 == 0) { s1 = y1 / f1 - 1; } else s1 = y1 / f1; if (y2 % f2 == 0) { s2 = y2 / f2 - 1; } else s2 = y2 / f2; if (s1 == s2) { cout << "YES"; } else { cout << "NO"; } cout << endl; } }
3
#include <bits/stdc++.h> using namespace std; int n, i, a[22], j, x[22][2], k = 0, s[11], b[5][5], s0 = 0; int rec(int x0, int y0) { if (!((x0 == n - 1) && (y0 == n))) { if (y0 == n) { y0 = x0; x0++; } if (x0 == n) { y0++; x0 = y0; } for (int i = 0; i < k; i++) { if (x[i][1]) { int xx = x[i][0]; b[x0][y0] = xx; x[i][1]--; s[x0] += xx; s[y0 + 5] += xx; if (x0 == y0) s[4] += xx; if (x0 + y0 + 1 == n) s[9] += xx; bool t = true; if (y0 == n - 1) if (s0 != s[x0]) t = false; if (x0 == n - 1) { if (s0 != s[y0 + 5]) t = false; } if (t) if (x0 <= y0) { if (rec(x0, y0 + 1)) return 1; } else if (rec(x0 + 1, y0)) return 1; x[i][1]++; s[x0] -= xx; s[y0 + 5] -= xx; if (x0 == y0) s[4] -= xx; if (x0 + y0 == n - 1) s[9] -= xx; } } } else { if (s0 != s[4]) return 0; if (s0 != s[9]) return 0; cout << s0 << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << b[i][j] << " "; cout << endl; } return 1; } return 0; } int main() { cin >> n; for (i = 0; i < n * n; i++) { cin >> a[i]; s0 += a[i]; bool t = true; for (j = 0; j < k; j++) if (a[i] == x[j][0]) { t = false; x[j][1]++; break; } if (t) { x[k][0] = a[i]; x[k][1] = 1; k++; } } s0 /= n; rec(0, 0); }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n; cin >> n; string s; cin >> s; set<char> st; for (auto i : s) st.insert(i); map<char, long long int> have; long long int ans = 1e18; long long int l = 0, r = 0; long long int cur = 0; while (r < n) { have[s[r]]++; if (have[s[r]] == 1) cur++; while (have[s[l]] > 1) { have[s[l]]--; l++; } if (cur == st.size()) ans = min(ans, r - l + 1); r++; } cout << ans << "\n"; }
3
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); int x, y; std::cin >> x >> y; if (x > 0 && y > 0) { int x1 = 0, y1, x2, y2 = 0; y1 = x + y; x2 = x + y; cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n"; } else if (x < 0 && y > 0) { int x1, y1 = 0, x2 = 0, y2; x1 = x - y; y2 = y - x; cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n"; } else if (x < 0 && y < 0) { int x1, y1 = 0, x2 = 0, y2; x1 = (x + y); y2 = (x + y); cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n"; } else { int x1 = 0, y1, x2, y2 = 0; y1 = y - x; x2 = x - y; cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n"; } return 0; }
1
#include <bits/stdc++.h> template <typename A, typename B> inline bool smax(A &a, const B &b) { return a < b ? a = b, 1 : 0; } template <typename A, typename B> inline bool smin(A &a, const B &b) { return b < a ? a = b, 1 : 0; } template <typename I> inline void read(I &x) { int f = 0, c; while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0; x = c & 15; while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); f ? x = -x : 0; } const int N = 5000 + 7; const long long INF = 0x3f3f3f3f3f3f3f3f; int n, s, e; int x[N], a[N], b[N], c[N], d[N]; long long dp[N][N]; inline void work() { for (int i = 1; i <= n; ++i) dp[i][0] = INF; for (int i = 1; i <= n; ++i) for (int j = 1; j <= i; ++j) { dp[i][j] = INF; if (i == s) { smin(dp[i][j], dp[i - 1][j - 1] + d[i]); if (j < i && (i == n || j > (i > e))) smin(dp[i][j], dp[i - 1][j] + c[i]); } else if (i == e) { smin(dp[i][j], dp[i - 1][j - 1] + b[i]); if (j < i && (i == n || j > (i > s))) smin(dp[i][j], dp[i - 1][j] + a[i]); } else { if (j < i - 1 && (i == n || j >= ((i > s) + (i > e)))) smin(dp[i][j], dp[i - 1][j + 1] + a[i] + c[i]); smin(dp[i][j], dp[i - 1][j - 1] + b[i] + d[i]); if (j < i && j > (i > s)) smin(dp[i][j], dp[i - 1][j] + b[i] + c[i]); if (j < i && j > (i > e)) smin(dp[i][j], dp[i - 1][j] + a[i] + d[i]); } } printf("%lld\n", dp[n][1]); } inline void init() { read(n), read(s), read(e); for (int i = 1; i <= n; ++i) read(x[i]); for (int i = 1; i <= n; ++i) read(a[i]), a[i] += x[i]; for (int i = 1; i <= n; ++i) read(b[i]), b[i] -= x[i]; for (int i = 1; i <= n; ++i) read(c[i]), c[i] += x[i]; for (int i = 1; i <= n; ++i) read(d[i]), d[i] -= x[i]; } int main() { init(); work(); fclose(stdin), fclose(stdout); return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long int n, k, q, i, j, a, b, x, y, c, d, m, count = 0, sum = 0, pro = 1, count1 = 0, flag = 0; string s; cin >> n >> m; for (i = 0; i < n; i++) { cin >> a >> b >> c >> d; if (b == c) { flag++; } } if (m % 2) cout << "NO\n"; else { if (flag > 0) cout << "YES\n"; else cout << "NO\n"; } } }
2
#include <bits/stdc++.h> template <class T> inline T sqr(T x) { return x * x; } #pragma comment(linker, "/STACK:64000000") using namespace std; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int n, m; string s[50]; int BP, DP = 0, CP = 3; struct point { int x, y; point(int X, int Y) { x = X; y = Y; } point() {} }; struct rect { point p[4]; int color; rect(point p0, point p1, point p2, point p3, int cur_color) { p[0] = p0; p[1] = p1; p[2] = p2; p[3] = p3; color = cur_color; } rect() {} }; point curp(0, 0); rect arr[50][50]; int dp[4][4]; bool used[50][50]; void init(int x, int y, rect r, int cur_color) { if (x < 0 || y < 0 || x == n || y == s[0].length() || cur_color != s[x][y] || used[x][y]) return; arr[x][y] = r; used[x][y] = 1; init(x + 1, y, r, cur_color); init(x, y + 1, r, cur_color); init(x - 1, y, r, cur_color); init(x, y - 1, r, cur_color); } int main() { dp[3][0] = 1; dp[3][2] = 0; dp[0][3] = 1; dp[0][1] = 2; dp[1][0] = 2; dp[1][2] = 3; dp[2][3] = 0; dp[2][1] = 3; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) cin >> s[i]; memset(used, 0, sizeof used); for (int i = 0; i < n; ++i) { for (int j = 0; j < s[0].length(); ++j) { if (used[i][j]) continue; int l = 0, h = 0; while (j + l < s[0].length() && s[i][j] == s[i][j + l]) l++; while (h + i < n && s[i][j] == s[h + i][j]) h++; l--; h--; init(i, j, rect(point(i, j), point(i, j + l), point(i + h, j + l), point(i + h, j), s[i][j]), s[i][j]); } } BP = s[0][0]; for (int i = 0; i < m; ++i) { curp = arr[curp.x][curp.y].p[dp[DP][CP]]; if (curp.x + dx[DP] < 0 || curp.y + dy[DP] < 0 || curp.x + dx[DP] >= n || curp.y + dy[DP] >= s[0].length() || s[curp.x + dx[DP]][curp.y + dy[DP]] == '0') { if (CP + 1 != 4 && CP + 1 == DP || CP + 1 == 4 && DP == 0) { CP += 2; CP %= 4; continue; } if (CP - 1 >= 0 && CP - 1 == DP || CP - 1 == -1 && DP == 3) { CP--; if (CP == -1) CP = 3; DP++; DP %= 4; continue; } } BP = s[curp.x + dx[DP]][curp.y + dy[DP]]; curp.x += dx[DP]; curp.y += dy[DP]; } cout << BP - '0'; return 0; }
4
#include <bits/stdc++.h> using namespace std; using namespace std; struct Info { int l, r, x; }; int res[333333]; struct Node { int l, r; int flag; } node[333333 * 6]; struct Seg { void pushdown(int x) { if (node[x].flag != -1) { node[(x) << 1].flag = node[(x) << 1 | 1].flag = node[x].flag; node[x].flag = -1; } } void build(int l, int r, int x = 1) { node[x].l = l, node[x].r = r; node[x].flag = -1; if (l == r) { return; } int mid = l + r >> 1; build(l, mid, (x) << 1); build(mid + 1, r, (x) << 1 | 1); } void biaoji(int l, int r, int val, int x = 1) { if (node[x].l >= l && node[x].r <= r) { node[x].flag = val; return; } int mid = (node[x].l + node[x].r) / 2; pushdown(x); if (l <= mid) biaoji(l, r, val, (x) << 1); if (r > mid) biaoji(l, r, val, (x) << 1 | 1); } int query(int v, int x = 1) { if (node[x].flag != -1 || node[x].l == node[x].r) { return node[x].flag; } int mid = (node[x].l + node[x].r) / 2; pushdown(x); if (v <= mid) return query(v, (x) << 1); if (v > mid) return query(v, (x) << 1 | 1); } } seg; int main() { memset((res), 0x3f, sizeof(res)); int n, m; scanf("%d%d", &(n), &(m)); seg.build(1, n); vector<Info> caozuo; for (int i = 0; i < m; i++) { int l, r, x; scanf("%d%d%d", &(l), &(r), &(x)); caozuo.push_back((Info){l, r, x}); } reverse(caozuo.begin(), caozuo.end()); for (int i = 0; i < m; i++) { int val = seg.query(caozuo[i].x); if (val == -1) { if (res[caozuo[i].x] == 0x3f3f3f3f) res[caozuo[i].x] = 0; } else { if (res[caozuo[i].x] == 0x3f3f3f3f) res[caozuo[i].x] = val; } seg.biaoji(caozuo[i].l, caozuo[i].r, caozuo[i].x); } for (int i = 1; i <= n; i++) if (res[i] == 0x3f3f3f3f) res[i] = seg.query(i); for (int i = 1; i <= n; i++) printf("%d ", res[i]); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1000000007; vector<int> rec[100008]; int dp[2][100008], a[100008]; int fun(int minn, int val) { int ll = 0, rr, ss = rec[val].size(); if (ss == 0) return maxn; else if (rec[val][0] >= minn) return rec[val][0] + 1; rr = ss; while (ll + 1 != rr) { int mid = (ll + rr) / 2; if (rec[val][mid] < minn) ll = mid; else rr = mid; } if (rr == ss) return maxn; return rec[val][rr] + 1; } int main() { int n, m, s, e, i, cc, ans; bool f; scanf("%d%d%d%d", &n, &m, &s, &e); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i <= 100000; i++) rec[i].clear(); for (i = 0; i < m; i++) { int tmp; scanf("%d", &tmp); rec[tmp].push_back(i); } memset(dp, 0, sizeof(dp)); cc = 1; ans = 0; f = true; while (f) { f = false; for (i = ans; i < n; i++) { if (i != 0 && dp[1 - cc][i - 1] != maxn) { dp[cc][i] = fun(dp[1 - cc][i - 1], a[i]); if (i != ans) dp[cc][i] = min(dp[cc][i], dp[cc][i - 1]); if (dp[cc][i] + i + 1 + (ans + 1) * e <= s) f = true; } else if (i == 0) { dp[cc][i] = fun(0, a[i]); if (i != ans) dp[cc][i] = min(dp[cc][i], dp[cc][i - 1]); if (dp[cc][i] + i + 1 + (ans + 1) * e <= s) f = true; } else dp[cc][i] = maxn; } if (f) ++ans; cc = 1 - cc; } printf("%d", ans); return 0; }
3
#include <bits/stdc++.h> int a[3], i; char s[101010]; int main() { scanf("%s", s); for (i = 0; s[i]; i++) a[(s[i] != '0') + (s[i] == '?')]++; if (a[0] + a[2] > a[1]) puts("00"); int half = i / 2, need0 = half, need1 = half + (i & 1), left = a[2]; bool ok = 1; left -= (need0 - a[0] > 0 ? need0 - a[0] : 0); ok &= left >= 0; left -= (need1 - a[1] > 0 ? need1 - a[1] : 0); ok &= left >= 0; i--; if (ok && (s[i] == '1' || (s[i] == '?' && a[1] != need1))) puts("01"); if (ok && (s[i] == '0' || (s[i] == '?' && a[0] != need0))) puts("10"); if (a[1] + a[2] > a[0] + 1) puts("11"); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int INF = ~0U >> 2; const int MOD = 1000000009; int ok[110], ch[110][4], dep[110], p[110], lnk[110], whi[110], f[1010][110][10]; char s[20]; queue<int> Q; void update(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } int fx(char c) { if (c == 'A') return 0; else if (c == 'C') return 1; else if (c == 'G') return 2; else return 3; } int main() { int n, m; cin >> n >> m; int tot = 0; memset(ch, -1, sizeof(ch)); dep[0] = 0; for (int i = 0; i < m; i++) { cin >> s; int t = 0; for (int i = 0; i < strlen(s); i++) { int x = fx(s[i]); if (ch[t][x] == -1) { ch[t][x] = ++tot; dep[tot] = dep[t] + 1; p[tot] = t; whi[tot] = x; } t = ch[t][x]; } ok[t] = dep[t]; } Q.push(0); while (!Q.empty()) { int t = Q.front(); Q.pop(); if (t == 0) lnk[t] = -1; else if (p[t] == 0) lnk[t] = 0; else { int k = lnk[p[t]]; for (; k != 0 && ch[k][whi[t]] == -1; k = lnk[k]) ; if (ch[k][whi[t]] != -1) k = ch[k][whi[t]]; lnk[t] = k; } for (int i = 0; i < 4; i++) if (ch[t][i] != -1) Q.push(ch[t][i]); if (t != 0) ok[t] = max(ok[t], ok[lnk[t]]); } f[0][0][0] = 1; for (int i = 0; i < n; i++) for (int j = 0; j <= tot; j++) for (int k = 0; k < 10; k++) if (f[i][j][k]) { for (int o = 0; o < 4; o++) { int t = j; for (; t != -1 && ch[t][o] == -1; t = lnk[t]) ; if (t == -1) continue; t = ch[t][o]; if (ok[t] > k) update(f[i + 1][t][0], f[i][j][k]); else if (k < 9) update(f[i + 1][t][k + 1], f[i][j][k]); } } int res = 0; for (int i = 0; i <= tot; i++) update(res, f[n][i][0]); cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, x, y; cin >> n >> m; vector<pair<int, int> > v; bool passed = true; while (m--) { cin >> x >> y; v.push_back(make_pair(x, y)); } sort(v.begin(), v.end()); for (int i = 0; i < v.size() && passed; i++) { if (v[i].first < n) { n += v[i].second; } else passed = false; } if (passed) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n, a[100005] = {0}; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int ans = min(n - 2, a[n - 2] - 1); cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int64_t a, v, b, w, t; cin >> a >> v >> b >> w >> t; if (abs(a - b) <= t*(v - w)) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long long lli; const int mod=1e9+7; void solve() { int n,k; cin>>n>>k; k--; if(n%2==0) { cout<<k%n+1<<endl; } else { cout<<(k+k/(n/2))%n+1<<endl; } } int main() { int t=1; cin>>t; while(t--) { solve(); } }
2
#include<iostream> #define ULL unsigned long long using namespace std; ULL transfer(ULL n,ULL m,ULL tar) { ULL y = (tar - 1) / n + 1; ULL x = tar - n * (y - 1); ULL x0 = 1 + m * (x - 1); ULL ans = (y - 1) + x0; return ans; } int main() { ULL t = 0; ULL n = 0, m = 0, tar = 0; cin >> t; for (ULL i = 1; i <= t; i++) { cin >> n >> m >> tar; cout << transfer(n, m, tar)<<endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; using lint = long long int; template<class T = int> using V = vector<T>; template<class T = int> using VV = V< V<T> >; template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); } template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; VV<> d; assign(d, n, n, (int) 1e9); for (int i = 0; i < n; ++i) d[i][i] = 0; for (int i = 0; i < m; ++i) { int s, t, _d; cin >> s >> t >> _d; d[s][t] = _d; } VV<> dp; assign(dp, 1 << n, n, (int) 1e9); dp[0][0] = 0; for (int bit = 0; bit < 1 << n; ++bit) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { dp[bit][i] = min(dp[bit][i], dp[bit ^ 1 << i][j] + d[j][i]); } int res = dp.back()[0] != 1e9 ? dp.back()[0] : -1; cout << res << '\n'; }
0
#include <bits/stdc++.h> using namespace std; struct node { int L, R, v; int Lf[12], Rf[12]; } Tr[100005 * 4]; int fa[12 * 100005], n, m, h, A[12][100005]; bool bao[12]; int find(int v) { if (fa[v] == v) return v; return fa[v] = find(fa[v]); } void build(int p, int l, int r) { Tr[p].L = l; Tr[p].R = r; if (l == r) { for (int i = 1; i <= n; i++) { if (A[i][l] != A[i - 1][l]) { Tr[p].v++; fa[m * (i - 1) + l] = m * (i - 1) + l; } else fa[m * (i - 1) + l] = m * (i - 2) + l; Tr[p].Lf[i] = find(m * (i - 1) + l); Tr[p].Rf[i] = find(m * (i - 1) + l); } return; } int mid = l + r >> 1; build(2 * p, l, mid); build(2 * p + 1, mid + 1, r); Tr[p].v = Tr[p * 2].v + Tr[p * 2 + 1].v; for (int i = 1; i <= n; i++) { if (A[i][mid] == A[i][mid + 1]) { int a = find((i - 1) * m + mid); int b = find((i - 1) * m + mid + 1); if (a != b) { fa[a] = b; Tr[p].v--; } } } for (int i = 1; i <= n; i++) { Tr[p].Lf[i] = find((i - 1) * m + l); Tr[p].Rf[i] = find((i - 1) * m + r); } } node solve(int p, int l, int r, int a, int b) { if (l == a && b == r) { return Tr[p]; } int mid = l + r >> 1; if (b <= mid) return solve(p * 2, l, mid, a, b); else if (a > mid) return solve(p * 2 + 1, mid + 1, r, a, b); else { int res = 0; node bao1 = solve(p * 2, l, mid, a, mid); node bao2 = solve(p * 2 + 1, mid + 1, r, mid + 1, b); memset(bao, 0, sizeof(bao)); for (int i = 1; i <= n; i++) { fa[bao1.Rf[i]] = bao1.Rf[i]; fa[bao2.Lf[i]] = bao2.Lf[i]; fa[bao1.Lf[i]] = bao1.Lf[i]; fa[bao2.Rf[i]] = bao2.Rf[i]; } for (int i = 1; i <= n; i++) { int a = find(bao1.Rf[i]); int b = find(bao2.Lf[i]); if (A[i][mid] == A[i][mid + 1]) { if (a != b) { fa[a] = b; res--; } } } node tmp; tmp.v = res; for (int i = 1; i <= n; i++) { tmp.Lf[i] = find(bao1.Lf[i]); tmp.Rf[i] = find(bao2.Rf[i]); } tmp.v += bao1.v + bao2.v; return tmp; } } int main() { int i, j, k, a, b; cin >> n >> m >> h; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { scanf("%d", &A[i][j]); } } build(1, 1, m); for (i = 1; i <= h; i++) { scanf("%d %d", &a, &b); printf("%d\n", solve(1, 1, m, a, b).v); } return 0; }
5
#include <bits/stdc++.h> using namespace std; inline int rdi() { int d; scanf("%d", &d); return d; } inline char rdc() { scanf(" "); return getchar(); } inline string rds() { string s; cin >> s; return s; } inline double rddb() { double d; scanf("%lf", &d); return d; } template <class T> inline bool setmin(T& a, T b) { return a > b ? a = b, true : false; } template <class T> inline bool setmax(T& a, T b) { return a < b ? a = b, true : false; } struct debugger { template <typename T> debugger& operator,(const T& x) { cerr << x << ","; return *this; } } dbgr; const int N = 32; priority_queue<int> q; int n, m, t, vs[N], a[N][N]; void dfs(int u, int m, int s) { if (((int)(q).size()) == t && s >= q.top()) return; if (u == n) { if (((int)(q).size()) == t) q.pop(); return q.push(s); } dfs(u + 1, m, s); for (int b = vs[u] & m; b; b &= b - 1) { int v = __builtin_ctz(b); dfs(u + 1, m ^ 1 << v, s + a[u][v]); } } int main() { n = rdi(), m = rdi(), t = rdi(); for (int repp = 0; repp < (m); ++repp) { int u = rdi() - 1, v = rdi() - 1; a[u][v] += rdi(), vs[u] |= 1 << v; } dfs(0, (1 << n) - 1, 0); printf("%d\n", q.top()); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { const double pi = 3.14159265358979323846; double r, h, v, e; cin >> r >> h >> v >> e; double x = pi; x = pi * r * r * h / 4; double t = x / (v - pi * r * r * e / 4); if (t < 0 || t > 10000) cout << "NO"; else { cout << "YES" << endl; cout << t; } }
1
#include <bits/stdc++.h> using namespace std; long double EPS = (long double)1e-11L; const double PI = 3.141592653589793238462; const int INF = 1e9 + 7; const long long LINF = 1e18 + 7; const int MOD = 1e9 + 7; const int S = 21; int n, m, s; vector<long long> bad, ans; vector<vector<long long> > T; vector<vector<long long> > mult(const vector<vector<long long> >& a, const vector<vector<long long> >& b) { vector<vector<long long> > c(s, vector<long long>(s)); for (int i = 0; i < (int)(s); ++i) { for (int j = 0; j < (int)(s); ++j) { for (int k = 0; k < (int)(s); ++k) { c[i][j] = (c[i][j] + a[i][k] * b[k][j] % MOD) % MOD; } } } return c; } vector<vector<long long> > binPow(int b) { vector<vector<long long> > res(s, vector<long long>(s)); for (int i = 0; i < (int)(s); ++i) { for (int j = 0; j < (int)(s); ++j) { res[i][j] = i == j; } } vector<vector<long long> > a = T; for (int i = 0; i < (int)(s); ++i) { if (bad[i]) { for (int j = 0; j < (int)(s); ++j) { a[j][i] = 0; } } } while (b) { if (b & 1) res = mult(res, a); a = mult(a, a); b >>= 1; } return res; } void upd(int dt) { vector<vector<long long> > res = binPow(dt); vector<long long> nAns(s); for (int i = 0; i < (int)(s); ++i) { for (int j = 0; j < (int)(s); ++j) { nAns[j] = (nAns[j] + ans[i] * res[i][j] % MOD) % MOD; } } ans = nAns; } int main() { int q; cin >> n >> m >> q; s = n * m; T.assign(s, vector<long long>(s)); for (int i = 0; i < (int)(n); ++i) { for (int j = 0; j < (int)(m); ++j) { int from = i * m + j; for (int q = 0; q < (int)(n); ++q) { for (int k = 0; k < (int)(m); ++k) { if ((i - q) * (i - q) + (j - k) * (j - k) <= 1) { int to = q * m + k; T[from][to] = 1; } } } } } int curt = 1; ans.resize(s), bad.resize(s); ans[0] = 1; for (int i = 0; i < (int)(q); ++i) { int tp, x, y, t; cin >> tp >> x >> y >> t; x--, y--; upd(t - curt); curt = t; int p = x * m + y; if (tp == 1) { cout << ans[p] << endl; continue; } if (tp == 2) { bad[p] = 1; ans[p] = 0; continue; } bad[p] = 0; } }
5
#include <bits/stdc++.h> using namespace std; long long num[15] = {9, 18, 27, 36, 45, 54, 63, 72, 81, 90}; char N[15]; long long n, cnt; long long output[10005]; long long cal(long long n) { long long sum = 0; while (n) { sum += n % 10; n /= 10; } return sum; } int main() { while (cin >> n) { sprintf(N, "%I64d", n); long long len = strlen(N); cnt = 0; for (long long i = num[len - 1]; i >= 0; i--) { if (cal(n - i) == i) { output[cnt++] = n - i; } } if (!cnt) printf("0\n"); else { printf("%I64d\n", cnt); for (long long i = 0; i < cnt; i++) { printf("%I64d\n", output[i]); } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; #define rep(i,m,n) for(int i=m;i<n;i++) ll mod=1e9+7; int main(){ int n,m; cin>>n>>m; ll a[n]; rep(i,0,n){ cin>>a[i]; } ll b[m]; rep(i,0,m){ cin>>b[i]; } ll x[n-1]; rep(i,0,n-1){ x[i]=a[i+1]-a[i]; } ll y[m-1]; rep(i,0,m-1){ y[i]=b[i+1]-b[i]; } ll c=0; rep(i,0,n-1){ c+=x[i]*(i+1)%mod*(n-1-i)%mod; c%=mod; } ll d=0; rep(i,0,m-1){ d+=y[i]*(i+1)%mod*(m-1-i)%mod; d%=mod; } cout<<c*d%mod<<endl; }
0
#include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; long long ans[3000003]; long long cnt, n; inline void read(long long &x) { x = 0; bool flag = false; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') flag = true; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } if (flag) x = -x; } void print(long long x) { if (x > 9) print(x / 10); putchar(x % 10 + '0'); } inline long long power(long long a, long long t) { long long res = 1; while (t) { if (t & 1) res = res * a % 998244353; a = a * a % 998244353; t >>= 1; } return res; } long long inv(long long n) { if (n == 1) return 1; if (n < 1) return 0; return (998244353 - 998244353 / n) * inv(998244353 % n) % 998244353; } inline void cal(long long k) { long long t = (n + 2 - k) * (n / k) / 2; ans[++cnt] = t; } signed main() { read(n); long long t = sqrt(n); for (long long i = 1; i <= t; ++i) { if (n % i != 0) continue; cal(i); if (i * i == n) continue; cal(n / i); } sort(ans + 1, ans + 1 + cnt); for (long long i = 1; i <= cnt; ++i) { print(ans[i]); putchar(' '); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int M = 1000 + 5; struct NODE { int pow; int dam; bool used; int index; } node[M]; int main() { int N, maxh, reg; cin >> N >> maxh >> reg; for (int i = 0; i < N; i++) { cin >> node[i].pow >> node[i].dam; node[i].used = false; node[i].index = i + 1; } int wintime = -1; int damage = 0; int curh = maxh; vector<pair<int, int> > ans; for (int t = 0; t <= 3000; t++) { int curh1 = curh - damage; curh1 = curh1 + reg; if (curh1 > maxh) { curh1 = maxh; } if (curh1 <= 0) { wintime = t; break; } curh = curh1; int index = -1; int maxd = 0; for (int i = 0; i < N; i++) { if (node[i].used == false) { if (100 * curh <= node[i].pow * maxh) { if (node[i].dam > maxd) { maxd = node[i].dam; index = node[i].index; } } } } if (index != -1) { damage = damage + node[index - 1].dam; node[index - 1].used = true; ans.push_back(make_pair(t, index)); } } if (wintime == -1) { cout << "NO" << endl; } else { cout << "YES" << endl; cout << wintime << " " << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].first << " " << ans[i].second << endl; } } return 0; }
2
#include<bits/stdc++.h> using namespace std; int main() { int q,t,f; char c; string s1,s2; cin>>s1>>q; while(q--) { cin>>t; if(t==1) swap(s1,s2); else { cin>>f>>c; if(f==1) s2.push_back(c); else s1.push_back(c); } } reverse(s2.begin(),s2.end()); cout<<s2+s1; return 0; }
0
#include<bits/stdc++.h> using namespace std; #define REP(i,s,n) for(int i=s;i<n;++i) #define rep(i,n) REP(i,0,n) #define SORT(c) sort((c).begin(),(c).end()) #define IINF INT_MAX #define LLINF LLONG_MAX #define DEBUG false typedef long long ll; typedef pair<int, int> ii; int main() { int n, m; while(cin >> n >> m, n ,m){ vector<int> table(n); vector<int> way(n, 1); rep(i, 3){ int num; cin >> num; rep(j, num){ int tmp; cin >> tmp; table[tmp-1] = i; } } vector<int> now(n, 0); int ans = 0; bool nn = false;//moved n-1th privious step while(1){ bool flag = true; rep(i, n){ if(table[i] != now[i]) flag = false; } if(flag) break; int num = IINF; if(!nn){ num = n-1; } else{ vector<int> maxi(3, -1); rep(i, n){ maxi[now[i]] = i; } rep(i, 3){ if(num > maxi[i] && maxi[i] > maxi[i+way[maxi[i]]]) num = maxi[i]; } } now[num] += way[num]; if(now[num] != 1) way[num] *= -1; nn = (num == n-1); ans++; } int tans = min(ans, (int)pow(3, n) - 1 - ans); if(tans > m) cout << -1 << endl; else cout << tans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 300010; int n; char s[maxn], t[maxn], ans[maxn]; int main() { scanf("%s %s", s + 1, t + 1), n = strlen(s + 1); sort(s + 1, s + n + 1); sort(t + 1, t + n + 1); int l1 = 1, r1 = n; int l2 = 1, r2 = n; for (int i = 1, cur = 0; i <= n; i++, cur ^= 1) { if (!cur) { if (s[l1] < t[r2]) { ans[i] = s[l1++]; } else { int cnt = (n + 1) / 2 - i / 2; l1 += cnt - 1; cnt = n / 2 - (i - 1) / 2; r2 -= cnt - 1; for (int j = n; j >= i; j--) { if (!((n - j) & 1)) ans[j] = s[l1--]; else ans[j] = t[r2++]; } break; } } else { if (t[r2] > s[l1]) { ans[i] = t[r2--]; } else { int cnt = (n + 1) / 2 - i / 2; l1 += cnt - 1; cnt = n / 2 - (i - 1) / 2; r2 -= cnt - 1; for (int j = n; j >= i; j--) { if (!((n - j) & 1)) ans[j] = t[r2++]; else ans[j] = s[l1--]; } break; } } } printf("%s\n", ans + 1); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; scanf("%d", &n); scanf("%d", &q); int b1 = 1, b2 = 2; while (q--) { int t; scanf("%d", &t); if (t == 1) { int move; scanf("%d", &move); if (move < 0) { b1 += move; b2 += move; if (b1 < 1) b1 = n + b1; if (b2 < 1) b2 = n + b2; } else if (move > 0) { b1 += move; b2 += move; if (b1 > n) b1 -= n; if (b2 > n) b2 -= n; } } else { if (b1 & 1) { b1++; if (b1 > n) b1 -= n; } else { b1--; if (b1 < 1) b1 = n + b1; } if (b2 & 1) { b2++; if (b2 > n) b2 -= n; } else { b2--; if (b2 < 1) b2 = n + b2; } } } int ans[n]; b1--; b2--; int ind1 = b1, v1 = 1, ind2 = b2, v2 = 2; for (int i = 0; i < n / 2; i++) { ans[ind1] = v1; ans[ind2] = v2; v1 += 2; v2 += 2; ind1 += 2; ind2 += 2; ind1 %= n; ind2 %= n; } for (int i = 0; i < n; i++) printf("%d ", ans[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const int MAXN = 100007; struct eT { void setd(int _u, int _v, int _w, int _l) { u = _u, v = _v, w = _w, last = _l; } int u, v, w, last; } edge[MAXN * 2]; class ftT { public: void clear(int n) { data.clear(); size = n + 3; data.resize(size); } void upd(int p, int x) { for (; p < size; p += p & -p) data[p] += x; } int sum(int p) { int ans = 0; for (; p; p ^= p & -p) ans += data[p]; return ans; } private: int size; vector<int> data; } ft; int n, ml, mw; long long ans; int ke, la[MAXN]; pair<int, int> aw; int size[MAXN]; int tl; vector<int> nw[MAXN], tw[MAXN]; bool vis[MAXN]; void init(); void input(); void work(); void dfs(int now, int fa, int nl, int nw); int getw(int now, int fa, int as); void divi(int now, int ns); long long getnum(vector<int> nw[], int nl); int main() { init(); input(); work(); } void init() { ios::sync_with_stdio(false); } void input() { scanf("%d%d%d", &n, &ml, &mw); int fa, nw; ke = 0; memset(la, -1, sizeof(la)); for (int i = 2; i <= n; ++i) { scanf("%d%d", &fa, &nw); edge[ke].setd(fa, i, nw, la[fa]); la[fa] = ke++; edge[ke].setd(i, fa, nw, la[i]); la[i] = ke++; } } void work() { divi(1, n); printf("%I64d\n", ans); } void divi(int now, int ns) { if (ns == 1) return; aw = make_pair(INF, -1); getw(now, -1, ns); now = aw.second; long long na = 0; int nl = 0, rl; for (int i = la[now]; ~i; i = edge[i].last) { if (!vis[edge[i].v]) { tl = 0; dfs(edge[i].v, now, 1, edge[i].w); if (tl > nl) { for (int j = nl + 1; j <= tl; ++j) nw[j].clear(); nl = tl; } for (int j = 1; j <= tl; ++j) { for (auto x : tw[j]) nw[j].push_back(x); } na -= getnum(tw, tl); for (int j = 1; j <= tl; ++j) tw[j].clear(); } } na += getnum(nw, nl); na >>= 1; for (int i = 1; i <= nl; ++i) na += nw[i].size(); for (int i = 1; i <= nl; ++i) nw[i].clear(); ans += na; vis[now] = true; for (int i = la[now]; ~i; i = edge[i].last) { if (!vis[edge[i].v]) divi(edge[i].v, size[edge[i].v]); } } int getw(int now, int fa, int as) { int ns = 1, ms = 0, v, ret; for (int i = la[now]; ~i; i = edge[i].last) { v = edge[i].v; if (v ^ fa && !vis[v]) { ret = getw(v, now, as); ms = (((ms) > (ret)) ? (ms) : (ret)); ns += ret; } } size[now] = ns; ms = (((ms) > (as - ns)) ? (ms) : (as - ns)); aw = (((aw) < (make_pair(ms, now))) ? (aw) : (make_pair(ms, now))); return ns; } void dfs(int now, int fa, int nl, int nw) { if (nl > ml || nw > mw) return; tw[nl].push_back(nw); tl = (((tl) > (nl)) ? (tl) : (nl)); for (int i = la[now]; ~i; i = edge[i].last) { if (edge[i].v ^ fa && !vis[edge[i].v]) { dfs(edge[i].v, now, nl + 1, nw + edge[i].w); } } } long long getnum(vector<int> nw[], int nl) { long long na = 0; map<int, int> mp; int kc = 0; for (int i = 1; i <= nl; ++i) for (auto x : nw[i]) mp[x] = 0; for (pair<const int, int> &x : mp) x.second = ++kc; mp.insert(make_pair(-INF, 0)); mp.insert(make_pair(INF, -1)); ft.clear(kc); int ri; for (int i = 1; i <= nl; ++i) { for (auto x : nw[i]) ft.upd(mp[x], 1); ri = ml - i; if (ri <= nl) for (auto x : nw[ri]) na += ft.sum((--mp.upper_bound(mw - x))->second); } ri = (((nl + 1) < (ml - nl)) ? (nl + 1) : (ml - nl)); for (int i = 1; i < ri; ++i) for (auto x : nw[i]) na += ft.sum((--mp.upper_bound(mw - x))->second); return na; }
5
#include <bits/stdc++.h> using namespace std; int fx[] = {1, -1, 0, 0}; int fy[] = {0, 0, 1, -1}; int main() { int n, m, i, x, cnt, j; cin >> n >> m; char a[n + 1][m + 1]; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { cin >> a[i][j]; } } for (i = 1, cnt = 0; i <= n; i++) { for (j = 1; j <= m; j++) { if (a[i][j] == 'P') { for (int k = 0; k < 4; k++) { int x = j + fx[k]; int y = i + fy[k]; if (y >= 1 && y <= n and x >= 1 && x <= m) { if (a[y][x] == 'W') { a[i][j] = '.'; a[y][x] = '.'; cnt++; break; } } } } } } printf("%d\n", cnt); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int MAXN = 1e5 + 5; const long long LINF = 1e18; const double pi = 3.1415926535; const double EPS = 1e-9; template <class T> using MinPQ = priority_queue<T, vector<T>, greater<T>>; template <class T> using MaxPQ = priority_queue<T>; int n; vector<int> d; vector<pair<int, int>> buildGraph(int curNode, vector<int> d) { if (d.empty()) return vector<pair<int, int>>(); vector<pair<int, int>> ans; for (int i = 0; i < d[0]; i++) { for (int j = curNode + i + 1; j <= curNode + d.back(); j++) { ans.emplace_back(curNode + i, j); } } int nextNode = d[0] + curNode; for (int i = 1; i < (int)d.size(); i++) d[i] -= d[0]; d.erase(d.begin()); if (!d.empty()) d.pop_back(); vector<pair<int, int>> tmp = buildGraph(nextNode, d); for (pair<int, int> e : tmp) ans.push_back(e); return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); scanf("%d", &n); d.resize(n); for (int i = 0; i < n; i++) scanf("%d", &d[i]); vector<pair<int, int>> ans = buildGraph(0, d); printf("%d\n", (int)ans.size()); for (pair<int, int> e : ans) printf("%d %d\n", e.first + 1, e.second + 1); printf("\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; int k; bool d[10]; string tmp; int cnt = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tmp; fill(d, d + 10, 0); bool flag = true; int len = tmp.length(); for (int j = 0; j < len; j++) { d[tmp[j] - '0'] = true; } for (int j = 0; j <= k; j++) { if (!d[j]) { flag = false; break; } } if (flag) cnt++; } cout << cnt << endl; return 0; }
1
/*jai_ganeshdeva*/ #include<bits/stdc++.h> using namespace std; #define pb push_back #define pu push // adds the value to the last of the queue #define lld long long int #define ldo long double #define ins insert /// used in set to insert the values #define adv advance /// used to increment the iterator #define mp make_pair #define fi first #define se second #define all(c) c.begin(),c.end() #define PI 3.1415926 #define INF (lld)1e16 #define vl vector<long long int > #define vpll vector< pair<lld ,lld> > #define vvl vector<vector<lld> > #define pll pair <lld,lld> lld p1=1e9 + 7,p2=998244353; void judge(){ #ifndef ONLINE_JUDGE // for getting input from input. freopen("input.txt", "r", stdin); // for writing output to output.txt // freopen("output.txt", "w", stdout); #endif } void fastio(){ ios_base::sync_with_stdio(false); cin.tie(NULL); } long long int modp(lld a,lld p,lld n) { // n=n%(p-1); if(n==0) return 1; if(n==1) return a%p; lld x=modp(a,p,n/2); x*=x; x%=p; return (n%2?(x*a)%p:x); } lld modInverse(lld n, lld p) { return modp(n, p, p-2); } // lld fac[500005]; // void getfac(lld p) // { // fac[0] = 1; // for (lld i=1 ; i<=500005; i++) // fac[i] = fac[i-1]*i%p; // } // long long int nCrmodp(lld n, lld r,lld p) // { // // Base case // if (r==0) // return 1; // return (fac[n]* modInverse(fac[r], p) % p * // modInverse(fac[n-r], p) % p) % p; // } struct segtree{ lld n,n2; vector<lld> v; void init(lld x) { lld a=1; while(a<=x) a*=2; n2=2*a; n=x; v.assign(n2,0); } void build(vector<lld> &w,lld curr,lld l,lld r) { if(l==r) { if(l<n) { v[curr]=w[l]; } return; } lld mid=l+(r-l)/2; build(w,2*curr+1,l,mid); build(w,2*curr+2,mid+1,r); v[curr]=v[2*curr+1]+v[2*curr+2]; } void build(vector<lld>&w) { build(w,0,0,n-1); } void update(lld l, lld r,lld pos,lld curr) { if(l==r) { v[curr]-=1; return; } lld mid=l+(r-l)/2; if(pos<=mid) { update(l,mid,pos,2*curr+1); } else update(mid+1,r,pos,2*curr+2); v[curr]=v[2*curr+1]+v[2*curr+2]; } void update(lld pos) { update(0,n-1,pos,0); } pll getVal(lld l,lld r,lld curr,lld k) { if(l==r) { return {l,k}; } lld mid=l+(r-l)/2; if(v[2*curr+1]>=k) { return getVal(l,mid,2*curr+1,k); } return getVal(mid+1,r,2*curr+2,k-v[2*curr+1]); } pll getVal(lld k) { return getVal(0,n-1,0,k); } }; lld l,r,mid,ans=0; lld n,i,j,k,g,m; lld x,y,n1,n2,h,z,c; bool compare(pair<pll,lld> p1,pair<pll,lld> p2) { if(p1.fi.fi!=p2.fi.fi) return p1.fi.fi<p2.fi.fi; return p1.fi.se>p2.fi.se; } // vector<pll> v; // vector<vector<lld> > w; // vector<lld> val; // vector<bool> visited(1e5); // lld DoJob(lld i) // { // } // void solve2(){ // cin>>n; // v.clear(); // w.clear(); // for(i=0;i<n;i++) // { // cin>>x>>y; // v.pb({x,y}); // } // w.assign(n,vector<lld>(1,-1)); // for(i=0;i<n-1;i++) // { // cin>>x>>y; // x-=1; // y-=1; // if(w[x][0]==-1) // { // w[x][0]=y; // } // else w[x].pb(y); // if(w[y][0]==-1) // { // w[y][0]=x; // } // else w[y].pb(x); // } // lld start=0; // val.assign(n,-1); // for(i=0;i<n;i++) // { // if(w[i].size()==1) // { // start=i; // break; // } // } // v[start]=v[i].fi; // lld a=DoJob(start); // } void solve(){ cin>>n; if(n==1) { cout<<"1"; return; } vector<lld> w(n+1,0); for(i=2;i<=n;i++) { if(w[i]==0) { w[i]=i; for(j=i*i;j<=n;j+=i) { w[j]=i; } } } vector<lld> w2(n+1,0); for(i=2;i<=n;i++) { lld a=i; lld b=1; while(a>1) { lld k=0; lld l=w[a]; while(a%l==0) { k+=1; a/=l; } b*=(k+1); } w2[i]=b; } vector<lld> v; v.pb(1); lld a=1; for(i=1;i<n;i++) { lld b=a+w2[i+1]; b%=p2; v.pb(b); a+=v[i]; a%=p2; } cout<<v[n-1]; // cout<<(3*modp(2,p2,n-2))%p2; } int main() { judge(); fastio(); lld t=1; // lld n; // cin>>t; // lld p1 = 1e9 + 7; // getfac(p1); for(int i=0;i<t;i++) { // cout<<"Case #"<<i+1<<": "; // cout<<" ----------------\n"; solve(); cout<<" \n"; } // cerr << "Time : " << ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; // exit(0); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int iIn1, iIn2, iIn3, iIn4; cin >> iIn1 >> iIn2 >> iIn3 >> iIn4; int x = ((iIn1 ^ iIn2) & (iIn3 | iIn4)) ^ ((iIn2 & iIn3) | (iIn1 ^ iIn4)); cout << x; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; const int inf = 0x3f3f3f3f; int t, n; int main() { int n; scanf("%d", &n); getchar(); char str[100005]; scanf("%s", str); int sl = strlen(str); int r = 0; int b = 0; int cr = 0, cb = 0; for (int i = 0; i < sl; i++) { if (i % 2 == 0) { if (str[i] != 'r') cr++; } else { if (str[i] != 'b') cb++; } } int cha = min(cr, cb); cha += (cr - cha) + cb - cha; cr = 0; cb = 0; for (int i = 0; i < sl; i++) { if (i % 2 == 0) { if (str[i] != 'b') cb++; } else { if (str[i] != 'r') cr++; } } int cha2 = min(cr, cb); cha2 += (cr - cha2) + cb - cha2; printf("%d\n", min(cha, cha2)); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c=0,d,e; cin>>a>>b; for(int i=0;i<a;i++) c+=b+i; if(b<0 && b+a-1<0) cout<<c-(b+a-1); else if(b>0) cout<<c-b; else cout<<c; }
0
#include <bits/stdc++.h> using namespace std; int a, b, c, d, e, x[100][100]; int main() { cin >> a; for (b = 1; b <= a + 1; b++) for (c = 1; c <= a * 2 + 1; c++) if (c <= a + 1) x[b][c] = c - a - 2 + b; else x[b][c] = a * 2 + 1 - c - a - 2 + b + 1; for (b = a + 2; b <= a * 2 + 1; b++) for (c = 1; c <= a * 2 + 1; c++) if (c <= a + 1) x[b][c] = c - a - 2 - b + a * 2 + 2; else x[b][c] = a * 2 + 1 - c - a - 2 - b + a * 2 + 3; for (b = 1; b <= a * 2 + 1; b++) { for (c = 1; c <= a * 2 + 1; c++) { if (x[b][c] >= 0 && c <= a) cout << x[b][c] << ' '; else if (x[b][c] > 0) cout << x[b][c] << ' '; else if (x[b][c] == 0) cout << 0; else if (x[b][c] < 0 && c <= a) cout << " "; } cout << endl; } }
2
#include <bits/stdc++.h> int ln[7]; char ll[7] = {"neit"}; int main() { char s[110]; scanf("%s", s); for (int j = 0; j < 4; j++) { for (int i = 0; s[i] != '\0'; i++) { if (ll[j] == s[i]) ln[j]++; } } if (ln[0] >= 3) { ln[0] -= 3; ln[0] /= 2; ln[0]++; } else ln[0] = 0; ln[1] /= 3; for (int i = 2; i >= 0; i--) if (ln[i + 1] < ln[i]) ln[i] = ln[i + 1]; printf("%d", ln[0]); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1500000; vector<int> prime; bool is_prime[MAX_N+1]; void siege(int n) { for (int i = 0; i <= n; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (int i = 2; i <= n; i++) { if (is_prime[i]) { prime.push_back(i); for (int j = 2*i; j <= n; j += i) is_prime[j] = false; } } } int main(void) { siege(MAX_N); int N; while (cin >> N, N) { if (is_prime[N]) { cout << 0 << endl; } else { int idx = lower_bound(prime.begin(), prime.end(), N) - prime.begin(); cout << prime[idx]-prime[idx-1] << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1005; const int INF = 0x3f3f3f3f; int main() { int n, a[MAXN], d, ans = INF, a1; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); sort(a, a + n); for (int i = 0; i <= 20000; ++i) { int mi = INF, ma = -INF; for (int j = 0; j < n; ++j) { mi = min(mi, j * i - a[j]); ma = max(ma, j * i - a[j]); } int no = (ma - mi + 1) / 2; if (ans > no) { ans = no; a1 = -mi - no; d = i; } } printf("%d\n%d %d", ans, a1, d); }
4
#include <bits/stdc++.h> using namespace std; int n; bool arr[100000], backup[100000]; vector<pair<int, int>> ans; void flip(int a, int b, vector<pair<int, int>> &ret = ans) { int c = b - a + b; arr[a] = !arr[a]; arr[b] = !arr[b]; arr[c] = !arr[c]; ret.push_back({a, b}); } bool solvesmall(int l, int r) { memcpy(backup, arr, sizeof backup); vector<pair<int, int>> candidates; for (int d = 1; d < 8; d++) { for (int i = l; i < r; i++) { int a = i, b = i + d, c = i + d + d; if (c <= r) candidates.push_back({a, b}); } } int score = (1 << 30); vector<pair<int, int>> res; for (int i = 0; i < (1 << candidates.size()); i++) { int cost = __builtin_popcount(i); vector<pair<int, int>> thisans; for (int j = 0; j < candidates.size(); j++) { if (i & (1 << j)) { auto [a, b] = candidates[j]; flip(a, b, thisans); } } bool good = true; for (int j = l; j <= r; j++) if (arr[j]) good = false; if (good && cost < score) { score = cost; res = thisans; } memcpy(arr + l, backup + l, (r - l + 1) * sizeof(bool)); } if (score == (1 << 30)) return false; for (auto c : res) ans.push_back(c); return true; } bool what(int l, int r) { while (r >= l) { if (r - l <= 8) return solvesmall(l, r); else if (!arr[l]) l++; else if (!arr[r]) r--; else if (arr[l + 1] && arr[l + 2]) flip(l, l + 1); else if (arr[r - 1] && arr[r - 2]) flip(r - 2, r - 1); else if (arr[l + 2]) flip(l, l + 2); else if (arr[r - 2]) flip(r - 4, r - 2); else if ((l + r) % 2 == 0) flip(l, (l + r) / 2); else if (arr[l + 1]) flip(l + 1, (l + 1 + r) / 2); else if (arr[r - 1]) flip(l, (l + r - 1) / 2); else flip(l, (l + r - 3) / 2); } return true; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; if (what(0, n - 1)) { cout << "YES" << endl; cout << ans.size() << endl; for (auto [a, b] : ans) cout << a + 1 << " " << b + 1 << " " << b - a + b + 1 << endl; } else { cout << "NO" << endl; } }
5
#include <bits/stdc++.h> #define int long long #define N 1001 using namespace std; const int INF = 1LL<<55; const int mod = (1e9)+7; const double EPS = 1e-8; const double PI = 6.0 * asin(0.5); template<class T> T Max(T &a,T b){return a=max(a,b);} template<class T> T Min(T &a,T b){return a=min(a,b);} typedef pair<int,int> P; int n; char G[N][N]; typedef pair<P,int>PP; int getIdx(const vector<PP>&V,const PP &a,const PP &b){ int f = (a.first.first + b.first.first)%4; int s = (a.first.second + b.first.second + 1)%2; for(int i=0;i<(int)V.size();i++) if(V[i].first == P(f,s)) return i; assert(0); } int judge(vector<PP> sta,int turn){ vector<P> v; for(PP p:sta) while(p.second--) v.push_back(p.first); assert((int)v.size()==2); P a = v[0], b = v[1]; return turn ^ a.second ^ b.second ^ (a.first==2 || a.first == 3) ^ (b.first==2 || b.first == 3); } map<vector<PP>,bool> mem; int dfs(vector<PP> sta,int turn){ int sum = 0; for(PP p:sta) sum += p.second; if(sum == 2) return judge(sta,turn); if(mem.count(sta)) return mem[sta]; int res = turn; for(PP &a:sta) for(PP &b:sta){ a.second--; b.second--; if(a.second < 0 || b.second < 0) {a.second++;b.second++;continue;} int idx = getIdx(sta,a,b); sta[idx].second++; int result = dfs(sta,!turn); a.second++, b.second++, sta[idx].second--; turn == 0? (res |= result):(res &= result); } return mem[sta] = res; } void visit(int pos,int num,vector<int> &used,vector<int> &par){ par[pos] = num; if(used[pos]++) return; for(int to=0;to<n;to++) if(G[pos][to]=='Y') visit(to,num,used,par); } vector<PP> prepare(){ vector<int> par(n),used(n,0); int cnt = 0; for(int i=0;i<n;i++) if(used[i] == 0) visit(i,cnt++,used,par); vector<P> A(cnt,P(0,0)); //nodes,edges; for(int i=0;i<n;i++){ A[par[i]].first++; for(int j=0;j<n;j++) A[par[i]].second+=G[i][j] == 'Y'; } for(P &a:A) a.first%=4, a.second=(a.second/2)%2; map<P,int> M; for(int i=0;i<4;i++) for(int j=0;j<2;j++) M[P(i,j)] = 0; for(P a:A) M[a]++,M[a]%=4; vector<PP> V; for(PP m:M) V.push_back(m); return V; } signed main(){ cin >> n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>G[i][j]; vector<PP> v = prepare(); int ans = dfs(v,0); cout<<(ans?"Taro":"Hanako")<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(){ long long H, W; cin >> H >> W; if(W == 1 || H == 1){ cout << 1 << endl; }else{ cout << (H * W + 1)/2 << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int MX = 100005; int n, p[MX], lvl[MX], sig[MX], ind; set<int> st[MX]; vector<int> res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; i++) { cin >> p[i]; lvl[i] = lvl[p[i]] + 1; st[p[i]].insert(i); if (lvl[i] > lvl[ind]) ind = i; } st[p[ind]].erase(ind); while (ind) { while (ind && st[p[ind]].empty()) { ind = p[ind]; st[p[ind]].erase(ind); } if (!ind) break; int a = *st[p[ind]].begin(); while (st[a].size()) { st[p[a]].erase(a); res.push_back(ind); p[ind] = a; a = *st[a].begin(); } st[p[a]].erase(a); res.push_back(ind); p[ind] = a; ind = a; } for (int i = 1; i < n; i++) sig[p[i]] = i; for (int i = 0, j = 0; i < n; i++, j = sig[j]) cout << j << " "; cout << '\n'; reverse(res.begin(), res.end()); cout << res.size() << '\n'; for (int x : res) cout << x << " "; cout << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; std::mt19937 rnd( (int)std::chrono::steady_clock::now().time_since_epoch().count()); long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } struct UF { int n; vector<int> par, sz, fst; void init(int _n) { n = _n; par = sz = fst = vector<int>(n); for (int i = (0); i < (n); ++i) par[i] = i, sz[i] = 1, fst[i] = i; } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); par[y] = x, sz[x] += sz[y], fst[x] = min(fst[x], fst[y]); } }; const int MAXN = 1000; int n; vector<int> v[MAXN]; int calc(vector<int> v) { int n = ((int)(v).size()); int mxval = v.back(); vector<int> pos(mxval + 1, n); for (int i = (0); i < (n); ++i) pos[v[i]] = i; for (int z = mxval - 1; z >= 0; --z) pos[z] = min(pos[z], pos[z + 1]); vector<int> hi(n, mxval + 1); set<int> can; for (int x = 0;; ++x) { vector<int> mxidx(n, n); UF uf; uf.init(n); vector<int> lo(n); bool any = false; for (int i = n - 1; i >= 0; --i) { { int dv = mxidx[i] == n ? 0 : v[mxidx[i]] - v[i]; lo[i] = dv; if (x - 1 >= 0) lo[i] = min(lo[i], hi[i]); if (lo[i] < hi[i]) any = true; if (lo[i] == 0 && lo[i] < hi[i]) can.insert(x); } { int l = max(lo[i], 1), r = hi[i] - 1; if (l > r) continue; int L = pos[max(v[i] - r, 0)], R = pos[max(v[i] - l + 1, 0)] - 1; if (L > R) continue; int at = L; while (at <= R) { if (mxidx[at] == n) { mxidx[at] = i; if (at - 1 >= 0 && mxidx[at - 1] != n) uf.unite(at - 1, at); if (at + 1 < ((int)(mxidx).size()) && mxidx[at + 1] != n) uf.unite(at, at + 1); } at = uf.find(at); at = uf.fst[at] + uf.sz[at]; } } } if (!any) break; hi = lo; } int ret = 0; while (can.count(ret)) ++ret; return ret; } bool solve() { int ret = 0; for (int i = (0); i < (n); ++i) ret ^= calc(v[i]); return ret != 0; } void run() { scanf("%d", &n); for (int i = (0); i < (n); ++i) { int cnt; scanf("%d", &cnt); v[i] = vector<int>(cnt); for (int j = (0); j < (cnt); ++j) scanf("%d", &v[i][j]); } printf("%s\n", solve() ? "YES" : "NO"); } int calcstupid(vector<int> v) { int n = ((int)(v).size()); auto mex = [&](set<int> &cur) { int ret = 0; while (cur.count(ret)) ++ret; return ret; }; vector<vector<int>> g(n, vector<int>(n, -1)); for (int i = n - 1; i >= 0; --i) { for (int j = (0); j <= (i); ++j) { set<int> cur; for (int k = (i + 1); k < (n); ++k) if (v[k] - v[i] > v[i] - v[j]) cur.insert(g[k][i]); g[i][j] = mex(cur); } } set<int> tmp; for (int i = (0); i < (n); ++i) tmp.insert(g[i][i]); return mex(tmp); } void stress() { int mxn = 100, mxval = max(mxn, 100000); for (int rep = (0); rep < (1000); ++rep) { int n = rnd() % mxn + 1; set<int> seen; vector<int> v(n); for (int i = (0); i < (n); ++i) while (true) { v[i] = rnd() % mxval + 1; if (seen.count(v[i])) continue; seen.insert(v[i]); break; } sort(v.begin(), v.end()); int have = calc(v); int want = calcstupid(v); if (have == want) { printf("."); continue; } printf("err have=%d want=%d\n", have, want); printf("v:"); for (int i = (0); i < (n); ++i) printf(" %d", v[i]); puts(""); break; } } void stressmem() { for (int rep = (0); rep < (10); ++rep) { int n = 100000; vector<int> v(n); for (int i = (0); i < (n); ++i) v[i] = i + 1; int have = calc(v); printf("have=%d\n", have); } } int main() { run(); return 0; }
5
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") struct edge { int u, v; edge() {} edge(int u, int v) : u(u), v(v) {} } e[100100]; int color[100100] = {0}, fa[100100], first[100100], vv[200200], nxt[200200], tot = 0, cnt = 0; long long ans = 1; int f(int x) { return fa[x] == x ? x : fa[x] = f(fa[x]); } void add(int u, int v) { vv[tot] = v; nxt[tot] = first[u]; first[u] = tot++; } void dfs(int u, int c) { color[u] = c; for (int i = first[u]; ~i; i = nxt[i]) { int v = vv[i]; if (!color[v]) dfs(v, 3 - c); else if (color[v] == color[u]) ans = 0; } } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; ++i) fa[i] = i; int u, v, type; while (m--) { scanf("%d%d%d", &u, &v, &type); if (type) { int fu = f(u), fv = f(v); fa[fu] = fv; } else e[cnt++] = edge(u, v); } memset(first, -1, sizeof(first)); for (int i = 0; i < cnt; ++i) { u = f(e[i].u); v = f(e[i].v); if (u == v) { ans = 0; continue; } add(u, v); add(v, u); } cnt = -1; for (int i = 1; i <= n; ++i) { int fu = f(i); if (color[fu]) continue; dfs(fu, 1); ++cnt; } while (cnt--) ans = ans * 2 % 1000000007LL; cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3 + 10; long long dp[maxn][maxn][3]; int a[maxn]; long long pre[maxn], suf[maxn]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + (a[i] == 1); for (int i = n; i >= 1; i--) suf[i] = suf[i + 1] + (a[i] == 2); long long ans = 0; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { dp[i][j][0] = dp[i][j - 1][0] + (a[j] == 2); dp[i][j][1] = max(dp[i][j - 1][0], dp[i][j - 1][1]) + (a[j] == 1); } } for (int i = 1; i <= n; i++) { for (int j = i - 1; j <= n; j++) { ans = max(ans, pre[i - 1] + dp[i][j][1] + suf[j + 1]); } } printf("%lld\n", ans); return 0; }
1
#include<iostream> #include<vector> #include<queue> typedef std::pair<int, int> P; int main(){ int n, r, l; std::cin >> n >> r >> l; std::vector<int>point(n, 0); std::vector<int>time(n, 0); std::priority_queue<P>que; int betime = 0; for (int i = 0; i < r; i++){ int d, t, x; std::cin >> d >> t >> x; d--; while (!que.empty()){ P res = que.top(); if (res.first == point[-res.second]){ time[-res.second] += (t - betime); break; } que.pop(); } point[d] += x; que.push(P(point[d], -d)); betime = t; } while (!que.empty()){ P res = que.top(); if (res.first == point[-res.second]){ time[-res.second] += (l - betime); break; } que.pop(); } int max = 0; for (int i = 1; i < n; i++){ if (time[i]>time[max])max = i; } std::cout << max + 1 << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); const long long inf = 1e17 + 7; long long n; vector<char> a, b; long long l, r; bool rev = false; void first(long long num) { vector<char> temp; for (long long i = a.size() - 1; i >= (long long)a.size() - num; i--) temp.emplace_back(a[i]); for (long long i = 0; i < (long long)a.size() - num; i++) temp.emplace_back(a[i]); a = temp; } void scan() { string second, t; cin >> n >> second >> t; for (char i : second) a.emplace_back(i); for (char i : t) b.emplace_back(i); l = n / 2; r = l - 1; } void solve() { scan(); vector<long long> res; bool bad = false; while (r - l + 1 < n || rev) { char let = 0; if (!rev && l - 1 >= 0) let = b[l - 1]; if (rev && r + 1 < n) let = b[r + 1]; long long id = n - 1; for (; id >= r - l + 1; id--) { if (a[id] == let) break; } if (let != 0 && id == r - l) { bad = true; break; } if (let == 0) id = r - l + 1; first(n - id); res.emplace_back(n - id); first(n - (n - id) - (r - l + 1)); res.emplace_back(n - (n - id) - (r - l + 1)); if (let != 0) { if (rev) r++; else l--; } first(r - l + 1); res.emplace_back(r - l + 1); rev = !rev; } if (bad) cout << -1; else { cout << res.size() << endl; for (long long i : res) cout << i << " "; } } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cout.precision(10); solve(); }
5
#include <bits/stdc++.h> using namespace std; int main() { string s; vector<char> v; cin >> s; v.push_back(s[0]); if (s.size() > 1) { v.push_back(s[1]); } for (int i = 2, j = 2; i < s.size(); i++) { if (v[j - 2] == v[j - 1] && s[i] != v[j - 1]) { v.push_back(s[i]); j++; } else if (v[j - 1] != v[j - 2]) { v.push_back(s[i]); j++; } } for (int i = 3; i < v.size(); i++) { if (v[i - 3] == v[i - 2] && v[i - 1] == v[i]) { v[i] = '0'; } } for (int i = 0; i < v.size(); i++) { if (v[i] != '0') { cout << v[i]; } } cout << endl; return 0; }
3
#include <bits/stdc++.h> int main() { int s, v1, v2, t1, t2, a, b; scanf("%d%d%d%d%d", &s, &v1, &v2, &t1, &t2); a = s * v1 + 2 * t1; b = s * v2 + 2 * t2; if (a > b) printf("Second\n"); else if (a < b) printf("First\n"); else printf("Friendship"); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(void) { int a[300000]; vector<int> b; int c; int p; int q; int r; int s; int n; int m; int x, y, z; int v; scanf("%d %d", &n, &m); x = -2; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] < 0) { c++; if (x != -2) b.push_back(i - x - 1); x = i; } } if (c > m) { printf("-1\n"); return 0; } else { m = m - c; sort(b.begin(), b.end()); s = 0; z = 0; for (int i = 0; i < c - 1; i++) { if (b[i] + s <= m) { z = z + 2; s = s + b[i]; } } if (s + n - x - 1 <= m) z++; printf("%d\n", 2 * c - z); } return 0; }
4
#include <stdio.h> int main(){ int i,n,pm,pj,pe; double avg,emav; char type; scanf("%d",&n); while(n!=0){ for(i=0;i<n;i++){ scanf("%d%d%d",&pm,&pe,&pj); avg = (pm+pe+pj) / 3.0; emav = (pm+pe) / 2.0; if(pm==100.0||pe==100.0||pj==100.0||emav>=90.0||avg>=80.0) type = 'A'; else if(avg>=70.0) type = 'B'; else if(avg>=50.0&&(pe>=80.0||pm>=80.0)) type = 'B'; else type = 'C'; printf("%c\n",type); } scanf("%d",&n); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 8 * 100 + 10; int ans[MAXN]; int arr[MAXN][MAXN]; bool row[MAXN], clm[MAXN]; int main() { int n; cin >> n; for (int i = 0; i < 2 * n; i++) for (int j = 0; j < i; j++) cin >> arr[i][j]; for (int k = 0; k < n; k++) { int mx = 0, x, y; for (int i = 0; i < 2 * n; i++) { if (!row[i]) for (int j = 0; j < i; j++) { if (!clm[j]) if (arr[i][j] > mx) { mx = arr[i][j]; x = i; y = j; } } } row[x] = true; row[y] = true; clm[y] = true; clm[x] = true; ans[x] = y + 1; ans[y] = x + 1; } for (int i = 0; i < n * 2; i++) cout << ans[i] << " "; cout << endl; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, x, ans; int a[200005], cnt[200005], lst[200005], pre[200005], nxt[200005], mina[400005], sum[200005]; bool vis[200005], inc[200005]; vector<int> b[200005], c[200005], v[200005]; void calc(int p) { mina[n] = 1; for (int i = 0; i < c[p].size(); i++) { int t = c[p][i]; sum[i + 1] = sum[i] + (a[t] == x ? -1 : 1); if (mina[sum[i + 1] + n]) { int l, r, s = c[p][mina[sum[i + 1] + n] - 1]; if (a[t] == x) r = nxt[t] - 1; else r = nxt[lst[t]] - 1; if (a[s] == x) l = pre[s] + 1; else l = lst[s] + 1; chkmax(ans, r - l + 1); } else mina[sum[i + 1] + n] = i + 2; } for (int i = 0; i <= c[p].size(); i++) mina[sum[i] + n] = 0; } int main() { n = readint(); for (int i = 1; i <= n; i++) a[i] = readint(); int maxa = 0, num = 0; for (int i = 1; i <= n; i++) cnt[a[i]]++, chkmax(maxa, cnt[a[i]]); for (int i = 1; i <= n; i++) if (maxa == cnt[i]) num++; if (num > 1) return printf("%d\n", n), 0; for (int i = 1; i <= n; i++) if (maxa == cnt[i]) x = i; for (int i = 1; i <= n + 1; i++) { lst[i] = lst[i - 1]; if (a[i - 1] == x) lst[i] = i - 1; } a[n + 1] = x; for (int i = 1; i <= n + 1; i++) if (a[i] == x) pre[i] = lst[i], nxt[lst[i]] = i; for (int i = 1; i <= n; i++) v[a[i]].push_back(i); for (int i = 1; i <= n; i++) { if (!v[i].size() || i == x) continue; int l = 0, r = 0; vector<int> gar(0); for (auto p : v[i]) { if (p > r) l = lst[p], r = nxt[l]; if (l) { b[l].push_back(i), gar.push_back(l); nxt[pre[l]] = nxt[l]; pre[nxt[l]] = pre[l]; l = pre[l]; } if (r <= n) { b[r].push_back(i), gar.push_back(r); nxt[pre[r]] = nxt[r]; pre[nxt[r]] = pre[r]; r = nxt[r]; } } reverse(gar.begin(), gar.end()); for (auto p : gar) nxt[pre[p]] = pre[nxt[p]] = p; } vector<int> cur(0); for (int i = 1; i <= n; i++) { if (a[i] == x) { for (auto r : b[i]) c[r].push_back(i), vis[r] = 1; vector<int> tmp(0); for (auto r : cur) { if (!vis[r]) { calc(r); c[r].clear(), inc[r] = 0; } else tmp.push_back(r); } swap(cur, tmp); for (auto r : b[i]) vis[r] = 0; } else { c[a[i]].push_back(i); if (!inc[a[i]]) cur.push_back(a[i]), inc[a[i]] = 1; } } for (int i = 1; i <= n; i++) if (c[i].size()) calc(i); printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using std::cin; using std::cout; const int maxn = 1e5 + 5; int n; std::vector<int> num; void printTree() { int cur = 0; for (int x : num) { int tmp = cur; cur += x; if (!tmp) continue; for (int y = tmp + 1; y <= cur; y++) printf("%d %d\n", tmp, y); } } int main() { cin >> n; if (n <= 5) { puts("-1"); } else { num.push_back(1); if (n % 2 == 1) { num.push_back(4); for (int i = 0; i < n - 7; i++) num.push_back(1); num.push_back(2); } else { num.push_back(3); for (int i = 0; i < n - 6; i++) num.push_back(1); num.push_back(2); } printTree(); } for (int i = 1; i < n; i++) { printf("%d %d\n", i, i + 1); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t, x, y, k = 0, l = 0; cin >> t; while (t--) { cin >> x >> y; if ((x > 0 && y >= 0) || (x > 0 && y < 0)) { k++; } else if ((x < 0 && y >= 0) || (x < 0 && y < 0)) { l++; } } if (k > 1) { if (l == 1) { cout << "Yes"; } else if (l == 0) { cout << "Yes"; } else { cout << "No"; } } else if (k == 1 && l == 1) { cout << "Yes"; } else if (l > 1) { if (k == 1) { cout << "Yes"; } else if (k == 0) { cout << "Yes"; } else { cout << "No"; } } }
1
#include <bits/stdc++.h> using namespace std; long long hh(long long p, long long n) { if (p == 1) return 1; if (p % 2) return (p + 1) / 2; if (n % 2 == 0) { return hh(p / 2, n / 2) + n / 2; } else { return hh(p / 2 + 1, (n + 1) / 2) + n / 2; } } int main() { long long n, q; cin >> n >> q; while (q--) { long long p, l, r; cin >> p; cout << hh(p, n) << "\n"; } return 0; }
2
#include <vector> #include <iostream> #include <utility> #include <algorithm> #include <string> #include <deque> #include <tuple> #include <queue> #include <functional> #include <cmath> #include <iomanip> #include <map> #include <numeric> #include <unordered_map> #include <unordered_set> #include <complex> #include <iterator> #include <array> #include <chrono> //cin.sync_with_stdio(false); //streambuf using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; template<class T, int s>using va = vector<array<T, s>>; template<class T, class T2> using umap = unordered_map<T, T2>; template<class T> using uset = unordered_set<T>; template<class T, class S> void cmin(T &a, const S&b) { if (a > b)a = b; } template<class T, class S> void cmax(T &a, const S&b) { if (a < b)a = b; } #define ALL(a) a.begin(),a.end() #define rep(i,a) for(int i=0;i<a;i++) #define rep1(i,a) for(int i=1;i<=a;i++) #define rrep(i,a) for(int i=a-1;i>=0;i--) #define rrep1(i,a) for(int i=a;i;i--) const ll mod = 1000000007; #ifndef INT_MAX const int INT_MAX = numeric_limits<signed>().max(); #endif template<class T>using heap = priority_queue<T, vector<T>, greater<T>>; template<class T>using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t & seed, const T & v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template<typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> & v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t& seed, Tuple const& tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t& seed, Tuple const& tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename ... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const& tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...> >::apply(seed, tt); return seed; } }; } ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } class unionfind { vector<int> par, rank, size_;//????????§??????????????¢???????????????????????????rank???????????????size????????? public: unionfind(int n) :par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x)return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y)return; if (rank[x] < rank[y])swap(x, y); par[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y])rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; ll gcd(ll a, ll b) { while (b) { ll c = a%b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b)*b; } int popcnt(unsigned long long a) { a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); } template<class T, class Func = function<const T(const T, const T)>> class segtree { vector<T> obj; int offset; Func updater; T e; int bufsize(int num) { int i = 1; for (; num >i; i <<= 1); offset = i - 1; return (i << 1) - 1; } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l)return e; if (a <= l && r <= b)return obj[k]; else return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } public: T query(int a, int b) {//[a,b) return query(a, b, 0, 0, offset + 1); } void updateall(int l = 0, int r = -1) { if (r < 0)r = offset + 1; l += offset, r += offset; do { l = l - 1 >> 1, r = r - 1 >> 1; for (int i = l; i < r; i++)obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]); } while (l); } void update(int k, T &a) { k += offset; obj[k] = a; while (k) { k = k - 1 >> 1; obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } segtree(int n, T e, const Func &updater_ = Func()) :obj(bufsize(n), e), e(e), updater(updater_) {} segtree(vector<T> &vec, T e, const Func &updater = Func()) :obj(bufsize(vec.size()), e), e(e), updater(updater) { copy(vec.begin(), vec.end(), obj.begin() + offset); updateall(); } typename vector<T>::reference operator[](int n) { return obj[n + offset]; } }; template<class T> class matrix { vector<vector<T>> obj; pair<int, int> s; public: matrix(pair<int, int> size, T e = 0) :matrix(size.first, size.second, e) {} matrix(int n, int m = -1, T e = 0) :obj(n, vector<T>(m == -1 ? n : m, e)), s(n, m == -1 ? n : m) {} static matrix e(int n) { matrix a = (n); for (int i = 0; i < n; i++)a[i][i] = 1; return a; } matrix& operator+=(const matrix &p) { if (s != p.s)throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++)obj[i][j] += p.obj[i][j]; return *this; } matrix operator+(const matrix &p) { matrix res(*this); return res += p; } matrix& operator-=(const matrix &p) { if (s != p.s)throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++)obj[i][j] -= p.obj[i][j]; return *this; } matrix operator-(const matrix &p) { matrix res(*this); return res -= p; } matrix& operator*=(T p) { for (auto &a : obj) for (auto &b : a)b *= p; return *this; } matrix operator*(T p) { matrix res(*this); return res *= p; } matrix operator*(const matrix &p) { if (s.second != p.s.first)throw runtime_error("matrix error"); matrix ret(s.first, p.s.second); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) for (int k = 0; k < p.s.second; k++)ret[i][k] += obj[i][j] * p.obj[j][k]; return ret; } matrix &operator*=(const matrix &p) { return *this = *this*p; } pair<int, int> size() const { return s; } matrix &mod(T m) { for (auto &a : obj) for (auto &b : a)b %= m; return *this; } typename vector<vector<T>>::reference operator[](int t) { return obj[t]; } }; template<class T> inline matrix<T> pow(matrix<T> &base, unsigned exp) { auto base_(base); if (base_.size().first != base_.size().second)throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1)res *= base_; if (!(exp /= 2))break; base_ *= base_; } return res; } template<class T> inline matrix<T> modpow(matrix<T> &base, unsigned exp, T m) { auto base_(base); if (base.size().first != base_.size().second)throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1)(res *= base_).mod(m); if (!(exp /= 2))break; (base_ *= base_).mod(m); } return res; } template<class T>int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } class Flow { int V; struct edge { int to, cap, rev, cost; }; vector<vector<edge>> G; vector<int> level, iter, h, dist, prevv, preve; public: Flow(int size) :G(size + 1), V(size + 1) { } void add_edge(int from, int to, int cap, int cost = 0) { G[from].push_back(edge{ to, cap, (int)G[to].size(),cost }); G[to].push_back(edge{ from,0,(int)G[from].size() - 1,-cost }); } void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t)return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { level.resize(V); iter.resize(V); int flow = 0; for (;;) { bfs(s); if (level[t] < 0)return flow; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, numeric_limits<int>::max()))>0) { flow += f; } } } typedef pair<int, int> P; int min_cost_flow(int s, int t, int f) { int res = 0; h.resize(V); dist.resize(V); prevv.resize(V); preve.resize(V); fill(h.begin(), h.end(), 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist.begin(), dist.end(), numeric_limits<int>::max()); dist[s] = 0; que.push({ 0,s }); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first)continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to]>dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push({ dist[e.to],e.to }); } } } if (dist[t] == numeric_limits<int>::max()) { return -1; } for (int v = 0; v < V; v++)h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d*h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } }; const ld eps = 1e-11, pi = acos(-1.0); typedef complex<ld> P; typedef vector<P> VP; ld dot(P a, P b) { return real(conj(a) * b); } ld cross(P a, P b) { return imag(conj(a) * b); } namespace std { bool operator<(const P &a, const P &b) { return abs(a.real() - b.real()) < eps ? a.imag() < b.imag() : a.real() < b.real(); } } struct L { P a, b; };//line->l,segment->s struct C { P p; ld r; }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > eps) return 1; // counter clockwise if (cross(b, c) < -eps) return -1; // clockwise if (dot(b, c) < 0) return 2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; // a--c--b on line } bool isis_ll(L l, L m) {//is intersect return abs(cross(l.b - l.a, m.b - m.a)) > eps; } bool isis_ls(L l, L s) { ld a = cross(l.b - l.a, s.a - l.a); ld b = cross(l.b - l.a, s.b - l.a); return (a * b < eps); } bool isis_lp(L l, P p) { return abs(cross(l.b - p, l.a - p)) < eps; } bool isis_ss(L s, L t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } P is_ll(L s, L t) { //intersect P sv = s.b - s.a, tv = t.b - t.a; return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } bool isis_sp(L s, P p) { return abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps; } P proj(L l, P p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } ld dist_lp(L l, P p) { return abs(p - proj(l, p)); } ld dist_ll(L l, L m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } ld dist_ls(L l, L s) { if (isis_ls(l, s)) return 0; return min(dist_lp(l, s.a), dist_lp(l, s.b)); } ld dist_sp(L s, P p) { P r = proj(s, p); if (isis_sp(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } ld dist_ss(L s, L t) { if (isis_ss(s, t)) return 0; ld a = min(dist_sp(s, t.a), dist_sp(t, s.a)); ld b = min(dist_sp(s, t.b), dist_sp(t, s.b)); return min(a, b); } VP is_cc(C c1, C c2) { VP res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); P diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * P(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * P(rc, -rs)); return res; } bool isis_vc(vector<C> vc) { VP crs; int n = vc.size(); rep(i, n)rep(j, i) for (P p : is_cc(vc[i], vc[j])) crs.push_back(p); rep(i, n) crs.push_back(vc[i].p); for (P p : crs) { bool valid = true; rep(i, n) if (abs(p - vc[i].p)>vc[i].r + eps) valid = false; if (valid) return true; } return false; } VP is_lc(C c, L l) { VP res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety; P nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } VP is_sc(C c, L l) { VP v = is_lc(c, l), res; for (P p : v) if (isis_sp(l, p)) res.push_back(p); return res; } vector<L> tangent_cp(C c, P p) {//????????\???? vector<L> ret; P v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (std::isnan(l)) { return ret; } P v1 = v * P(l / d, c.r / d); P v2 = v * P(l / d, -c.r / d); ret.push_back(L{ p, p + v1 }); if (l < eps) return ret; ret.push_back(L{ p, p + v2 }); return ret; } vector<L> tangent_cc(C c1, C c2) { vector<L> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { P center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { P out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<L> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { P v = c2.p - c1.p; v /= abs(v); P q1 = c1.p + v * P(0, 1) * c1.r; P q2 = c1.p + v * P(0, -1) * c1.r; ret.push_back(L{ q1, q1 + v }); ret.push_back(L{ q2, q2 + v }); } return ret; } ld area(const VP &p) {//??¢????? ld res = 0; int n = p.size(); rep(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } bool is_polygon(L l, VP &g) { int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i]; P b = g[(i + 1) % n]; if (isis_ss(l, L{ a, b })) return true; } return false; } int is_in_Polygon(const VP &g, P p) { bool in = false; int n = g.size(); for (int i = 0; i < n; i++) { P a = g[i] - p, b = g[(i + 1) % n] - p; if (imag(a) > imag(b)) swap(a, b); if (imag(a) <= 0 && 0 < imag(b)) if (cross(a, b) < 0) in = !in; if (abs(cross(a, b)) < eps && dot(a, b) < eps) return 0; // on } if (in) return 1; // in return -1; // out } VP ConvexHull(VP ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); VP ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } VP ConvexCut(const VP &ps, L l) { VP Q; for (int i = 0; i < (int)ps.size(); i++) { P A = ps[i], B = ps[(i + 1) % ps.size()]; if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0) Q.push_back(is_ll(L{ A, B }, l)); } return Q; } double dist2(P a) { return real(a)*real(a) + imag(a)*imag(a); } // Suffix Array ?????????O(|S|log^2|S|), ????´¢O(|T|log|S|), ?????????????§????O(|S|) class StringSearch { const int n; string S; public: vector<int> sa, rank; StringSearch(const string &S_) :n(S_.size()), S(S_), sa(n + 1), rank(n + 1) { for (int i = 0; i <= n; i++) { sa[i] = i; rank[i] = i < n ? S[i] : -1; } vector<int> tmp(n + 1); for (int k = 1; k <= n; k *= 2) { auto Compare_SA = [=](int i, int j) { if (this->rank[i] != this->rank[j]) return this->rank[i] < this->rank[j]; int ri = i + k <= n ? this->rank[i + k] : -1; int rj = j + k <= n ? this->rank[j + k] : -1; return ri < rj; }; sort(sa.begin(), sa.end(), Compare_SA); tmp[sa[0]] = 0; for (int i = 1; i <= n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (Compare_SA(sa[i - 1], sa[i]) ? 1 : 0); } for (int i = 0; i <= n; i++) { this->rank[i] = tmp[i]; } } } bool Contain(const string &T) { int a = 0, b = n; while (b - a > 1) { int c = (a + b) / 2; if (S.compare(sa[c], T.length(), T) < 0) a = c; else b = c; } return S.compare(sa[b], T.length(), T) == 0; } vector<int> LCPArray() { for (int i = 0; i <= n; i++) rank[sa[i]] = i; int h = 0; vector<int> lcp(n + 1); for (int i = 0; i < n; i++) { int j = sa[rank[i] - 1]; if (h > 0) h--; for (; j + h < n && i + h < n; h++) { if (S[j + h] != S[i + h]) break; } lcp[rank[i] - 1] = h; } return lcp; } }; int main() { int n; cin.sync_with_stdio(0); while (cin >> n, n) { va<int, 3> bl(n); rep(i, n)rep(j, 3)cin >> bl[i][j]; //va<double, 3> h; double ans = 1e9; int mina = 0; rep(i, n)if (ans > bl[i][2])ans = bl[i][2], mina = i; ans *= ans; rep(j, n) { int i = mina; if ((bl[i][0] - bl[j][0])*(bl[i][0] - bl[j][0]) + (bl[i][1] - bl[j][1])*(bl[i][1] - bl[j][1]) + bl[i][2] * bl[i][2] > bl[j][2] * bl[j][2]) { ans = 1e9; break; } } rep(i, n) { rep(j, i) { double l = sqrt((bl[i][0] - bl[j][0])*(bl[i][0] - bl[j][0]) + (bl[i][1] - bl[j][1])*(bl[i][1] - bl[j][1])); double a = ((bl[j][2] * bl[j][2] - bl[i][2] * bl[i][2]) / l + l) / 2; double x = (bl[i][0] * a + bl[j][0] * (l - a)) / l, y = (bl[i][1] * a + bl[j][1] * (l - a)) / l, z = bl[j][2] * bl[j][2] - a*a; if (z < 0 || a < 0 || a > l)continue; if (z < ans)ans = z; } } rep(i,n){ rep(j,i){ rep(k, j) { double x[2][3] = { {double(bl[i][0] - bl[j][0]),double(bl[i][1] - bl[j][1]),double(-bl[i][2] * bl[i][2] + bl[j][2] * bl[j][2] + bl[i][0] * bl[i][0] + bl[i][1] * bl[i][1] - bl[j][0] * bl[j][0] - bl[j][1] * bl[j][1])},{ double(bl[i][0] - bl[k][0]),double(bl[i][1] - bl[k][1]),double(-bl[i][2] * bl[i][2] + bl[k][2] * bl[k][2] + bl[i][0] * bl[i][0] + bl[i][1] * bl[i][1] - bl[k][0] * bl[k][0] - bl[k][1] * bl[k][1]) } }; double a, b, c; if (x[1][1] == 0) { if (x[0][1] == 0)continue; if (x[1][0] - (x[1][1] / x[0][1])*x[0][0] == 0)continue; a = (x[1][2] - x[1][1] / x[0][1] * x[0][2]) / (x[1][0] - (x[1][1] / x[0][1])*x[0][0])*0.5; b = (x[0][2] - 2 * a*x[0][0]) *0.5 / x[0][1]; } else { if (x[0][0] - (x[0][1] / x[1][1])*x[1][0] == 0)continue; a = (x[0][2] - x[0][1] / x[1][1] * x[1][2]) / (x[0][0] - (x[0][1] / x[1][1])*x[1][0])*0.5; b = (x[1][2] - 2 * a*x[1][0]) *0.5 / x[1][1]; } c = bl[i][2] * bl[i][2] - (a - bl[i][0])*(a - bl[i][0]) - (b - bl[i][1])*(b - bl[i][1]); if (c < 0)continue; if (c >= ans)continue; int cc = ccw(P(bl[i][0], bl[i][1]), P(bl[j][0], bl[j][1]), P(a, b)); if (cc == ccw(P(bl[j][0], bl[j][1]), P(bl[k][0], bl[k][1]), P(a, b)) && cc == ccw(P(bl[k][0], bl[k][1]), P(bl[i][0], bl[i][1]), P(a, b)))ans = c; //h.push_back({ a,b,c }); /*bool f = 1; rep(i, n) { if ((a - bl[i][0])*(a - bl[i][0]) + (b - bl[i][1])*(b - bl[i][1]) + c > bl[i][2]* bl[i][2] + 1e-7) { f = 0; break; } } if (f) { cmax(ans, sqrt(c)); }*/ } } } //sort(ALL(h), [](const array<double, 6> &a, const array<double, 6> &b) {return a[2] != b[2] ? a[2] > b[2]:a > b; }); /*double ans = 0; for (auto &&x : h) { bool f = 1; rep(i, n) { if (pow(x[0] - bl[i][0], 2) + pow(x[1] - bl[i][1], 2) + pow(x[2], 2) > pow(bl[i][2], 2)+1e-7) { f = 0; break; } } if (f) { cmax(ans, x[2]); } }*/ cout << fixed << setprecision(7) << sqrt(ans) << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e6 + 7; const int INF = (int)2e9 + 7; int cnt[MAXN]; string s; vector<int> pos; vector<pair<int, int>> all; int main() { cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') pos.push_back(i); else { if (pos.empty() || s[pos.back()] != '(') pos.clear(); else { while (!all.empty() && pos.back() <= all.back().first && all.back().second <= i) all.pop_back(); if (!all.empty() && all.back().second + 1 == pos.back()) all[all.size() - 1].second = i; else all.push_back(make_pair(pos.back(), i)); pos.pop_back(); } } } for (auto i : all) cnt[i.second - i.first + 1]++; cnt[0]++; for (int i = s.size(); i >= 0; i--) { if (cnt[i]) { printf("%d %d", i, cnt[i]); return 0; } } return 0; }
3
#include <bits/stdc++.h> int inp() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return sum; } int head[200010], nxt[400010], end[400010], value[400010], k, id[200010]; long long is[200010]; long long dis[200010]; struct Node { int x; long long d; bool is; bool operator<(const Node &b) const { return d > b.d; } }; bool cmp(int a, int b) {} std::priority_queue<Node> q; std::multiset<long long, std::greater<long long> > s; std::vector<int> vec; int cou = 0; void link(int a, int b, int v) { nxt[++cou] = head[a]; head[a] = cou; end[cou] = b; value[cou] = v; } void dij(int st) { q.push((Node){st, 0, true}); dis[st] = 0; for (int x = head[st]; x != -1; x = nxt[x]) is[end[x]] = value[x]; while (!q.empty() && q.top().d <= *s.begin()) { Node un = q.top(); q.pop(); if (un.d > dis[un.x]) continue; int u = un.x; if (!un.is) { s.insert(dis[u]); if (is[u] && is[u] <= *s.begin()) { s.erase(s.find(is[u])); } else s.erase(s.begin()); } for (int x = head[u]; x != -1; x = nxt[x]) { if (dis[end[x]] > dis[u] + value[x] && dis[u] + value[x] <= *s.begin()) { vec.push_back(end[x]); dis[end[x]] = dis[u] + value[x]; if (dis[end[x]] < *s.begin()) q.push((Node){end[x], dis[end[x]], (u == st)}); } } } while (!q.empty()) { q.pop(); } for (int i = 0; i < vec.size(); i++) dis[vec[i]] = 1e18; for (int x = head[st]; x != -1; x = nxt[x]) is[end[x]] = 0; dis[st] = 1e18; vec.clear(); } int main() { srand((unsigned)time(NULL)); memset(head, -1, sizeof(head)); int n = inp(); int m = inp(); k = inp() << 1; for (int i = 1; i <= n; i++) id[i] = i; std::random_shuffle(id + 1, id + n + 1); for (int i = 1; i <= m; i++) { int u = inp(); int v = inp(); int w = inp(); link(u, v, w); link(v, u, w); s.insert(w); s.insert(w); } for (int i = (m << 1); i < k; i++) s.insert(1e18); for (int i = (m << 1); i > k; i--) { s.erase(s.begin()); } for (int i = 1; i <= n; i++) dis[i] = 1e18; for (int i = 1; i <= n; i++) dij(id[i]); printf("%lld\n", *s.begin()); }
6
#include<bits/stdc++.h> using namespace std; using i64 = int64_t; int garden[12][12]={0}; void init(){ for(int i=0;i<=10;++i){ for(int j=0;j<=10;++j) garden[i][j] = 0; } } int main(){ int d, w; while(cin >> d >> w, d != 0){ init(); for(int i=0;i<d;++i){ for(int j=0;j<w;++j){ cin >> garden[i][j]; } } int ans = 0; for(int top=0;top<d;++top){ for(int left=0;left<w;++left){ for(int bottom=top+2;bottom<d;++bottom){ for(int right=left+2;right<w;++right){ int mini = 11; for(int i=left;i<=right;++i){ mini = min(mini, garden[top][i]); mini = min(mini, garden[bottom][i]); } for(int i=top;i<=bottom;++i){ mini = min(mini, garden[i][left]); mini = min(mini, garden[i][right]); } bool ok = true; int sum = 0; for(int i=top+1;i<bottom;++i){ for(int j=left+1;j<right;++j){ if(garden[i][j] >= mini){ ok = false; break; } sum += mini - garden[i][j]; } if(!ok)break; } if(ok)ans = max(ans, sum); } } } } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> const int N = 5e4 + 5; const int M = 4e5 + 5; int line[N]; int hang[N]; char s[105], p[105], t[105]; int sum[30]; using namespace std; int n, m, i, j; int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { memset(sum, 0, sizeof sum); scanf("%s", s); scanf("%s", t); scanf("%s", p); int mm = 0; int lens = strlen(s); int lent = strlen(t); int lenp = strlen(p); int flag1 = 0; for (j = 0; j < lent; j++) { if (t[j] == s[mm] && flag1 == 0) { mm++; if (mm == lens) flag1 = 1; } else { sum[t[j] - 'a']++; } } if (mm != lens) { printf("NO\n"); } else { for (j = 0; j < lenp; j++) { sum[p[j] - 'a']--; } int flag = 0; for (j = 0; j < 30; j++) { if (sum[j] > 0) { flag = 1; break; } } if (flag == 1) printf("NO\n"); else printf("YES\n"); } } }
3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(NULL); int t; cin >> t; while (t--) { bool ans = true; int n; cin >> n; string s; cin >> s; int l = 0, r = s.size() - 1; while (l < r) { int valL = s[l] - 'a', valR = s[r] - 'a'; if (!(abs(valL - valR) == 0 || abs(valL - valR) == 2)) ans = false; l++; r--; } if (ans) cout << "YES\n"; else cout << "NO\n"; } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[n][m], b[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> b[i][j]; } } int flag = 0; for (int i = 0; i < n; i++) { int row = i, col = 0; vector<int> v, g; while (col < m && row >= 0) { v.push_back(a[row][col]); g.push_back(b[row][col]); row--; col++; } sort(v.begin(), v.end()); sort(g.begin(), g.end()); for (int i = 0; i < v.size(); i++) { if (v[i] != g[i]) { cout << "NO" << '\n'; return 0; } } } for (int i = 1; i < m; i++) { int row = n - 1, col = i; vector<int> v, g; while (col < m && row >= 0) { v.push_back(a[row][col]); g.push_back(b[row][col]); row--; col++; } sort(v.begin(), v.end()); sort(g.begin(), g.end()); for (int i = 0; i < v.size(); i++) { if (v[i] != g[i]) { cout << "NO" << '\n'; return 0; } } } cout << "YES" << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, k; int a[100005]; set<long long> present; int ans; int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); for (int i = n - 1; i >= 0; i--) { if (present.find((long long)a[i] * k) == present.end()) { ans++; present.insert(a[i]); } } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> long long mpow(long long a, long long n, long long mod) { long long ret = 1; long long b = a; while (n) { if (n & 1) ret = (ret * b) % mod; b = (b * b) % mod; n >>= 1; } return (long long)ret; } using namespace std; string b[12]; int st[(int)(1e5 + 25)]; void solve() { string s; cin >> s; int sz = s.size(); int n; scanf("%d", &n); s = '#' + s; for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int i = 1; i <= sz; i++) { for (int j = 1; j <= n; j++) { int x = b[j].size(); if (i - x >= 0) { string other = s.substr(i - x + 1, x); if (other == b[j]) { st[i] = max(st[i], i - x + 1); } } } } for (int i = 1; i <= sz; i++) { st[i] = max(st[i], st[i - 1]); } int ans = 0, pos = 0; for (int i = 1; i <= sz; i++) { if (ans < i - st[i]) { ans = i - st[i]; pos = st[i]; } } cout << ans << " " << pos; } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int mark[200000], ans[200000]; int main() { long long int n, i, p, val1, val2; cin >> n; for (i = 0; i < n; i++) { cin >> p; mark[p]++; } ans[1] = mark[1]; val1 = 0; val2 = 0; for (i = 1; i < 200000; i++) { ans[i] = max(ans[i - 1], ans[i - 2] + i * mark[i]); } cout << ans[200000 - 1]; }
3
#include <bits/stdc++.h> int n, a, b; int solve(int countA, int countB) { if (countA == 0 && countB == 0) return 0; else { int answer = countA + countB; for (int cA = 0; cA <= countA; cA++) for (int cB = 0; cB <= countB; cB++) if (0 < cA + cB && cA * a + cB * b <= n) answer = std::min(answer, solve(countA - cA, countB - cB)); return 1 + answer; } } int main() { scanf("%d%d%d", &n, &a, &b); printf("%d", solve(4, 2)); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, d; cin >> n >> d; string s; cin >> s; int ans = 0; int i = 0; while (i != n - 1) { int t = d; while (s[i + t] != '1') { t--; if (t == 0) { cout << -1; return 0; } } ans++; i += t; } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1002; int n, p, k, a[MAXN], b[MAXN], ans; int n1, n2, f[2][MAXN][52][52]; inline void upd(int &x, int y) { if (y > x) x = y; } inline int getint() { int w = 0, q = 0; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') q = 1, c = getchar(); while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar(); return q ? -w : w; } inline void work() { n = getint(); p = getint(); k = getint(); n1 = getint(); for (int i = 1; i <= n1; i++) a[getint()]++; n2 = getint(); for (int i = 1; i <= n2; i++) b[getint()]++; int lim = n / p; if (n % p != 0) lim++; lim <<= 1; if (k >= lim) { for (int i = 1; i <= n; i++) ans += a[i] | b[i]; printf("%d", ans); return; } memset(f, -0x3f, sizeof(f)); f[0][0][0][0] = 0; int to, tag = 1; for (int i = 1; i <= n; i++) { to = tag; memset(f[to], -0x3f, sizeof(f[to])); tag ^= 1; for (int j = 0; j <= p; j++) { for (int ii = 0; ii <= k; ii++) { for (int jj = 0; jj <= k; jj++) { if (!ii && !jj) { upd(f[to][j][0][0], f[tag][j][0][0]); upd(f[to][j + 1][k - 1][0], f[tag][j][0][0] + a[i]); upd(f[to][j + 1][0][k - 1], f[tag][j][0][0] + b[i]); upd(f[to][j + 2][k - 1][k - 1], f[tag][j][0][0] + (a[i] | b[i])); } else if (!ii) { upd(f[to][j][0][jj - 1], f[tag][j][0][jj] + b[i]); upd(f[to][j + 1][k - 1][jj - 1], f[tag][j][0][jj] + (a[i] | b[i])); upd(f[to][j + 2][k - 1][k - 1], f[tag][j][0][jj] + (a[i] | b[i])); } else if (!jj) { upd(f[to][j][ii - 1][0], f[tag][j][ii][0] + a[i]); upd(f[to][j + 1][ii - 1][k - 1], f[tag][j][ii][0] + (a[i] | b[i])); upd(f[to][j + 2][k - 1][k - 1], f[tag][j][ii][0] + (a[i] | b[i])); } else upd(f[to][j][ii - 1][jj - 1], f[tag][j][ii][jj] + (a[i] | b[i])); } } } } tag ^= 1; for (int l = 0; l <= p; l++) for (int i = 0; i <= k; i++) for (int j = 0; j <= k; j++) ans = max(ans, f[tag][l][i][j]); printf("%d", ans); } int main() { work(); return 0; }
5
#include <bits/stdc++.h> using namespace std; mt19937 mt_rand( chrono::high_resolution_clock::now().time_since_epoch().count()); const int INF = 2e9; const long long ML = 4e18; long long Pow(long long x, long long p) { long long res = 1; while (p) { if (p & 1) res = (res * x) % 998244353; x = x * x % 998244353; p >>= 1; } return res; } long long count1(long long k) { if (k == 1) return 0; long long r = (k - 1) % 998244353 * Pow(2, k - 2) % 998244353; return r % 998244353; } long long count2(long long k) { if (k < 3) return 1; long long x = Pow(2, k - 3), y = x * 2 % 998244353; long long r = y + (k - 2) % 998244353 * x % 998244353; return r % 998244353; } long long a[100005], cnt[100005], sum[100005]; vector<long long> va[100005]; void solve() { int n; scanf("%d", &n); for (int i = (0); i < (n); ++i) { int x, y; scanf("%d%d", &x, &y); a[x] = y; } for (int i = (1); i < (100005); ++i) { for (int j = i; j < 100005; j += i) { if (a[j]) { cnt[i] = cnt[i] + a[j]; sum[i] = (sum[i] + a[j] * j) % 998244353; va[i].push_back(j); } } } vector<long long> ans(100005, 0); for (int i = (1); i < (100005); ++i) { if (cnt[i] < 2) continue; int x = count1(cnt[i]), y = count2(cnt[i]); for (long long j : va[i]) { ans[i] = (ans[i] + x * j % 998244353 * j % 998244353 * a[j] % 998244353 + y * j % 998244353 * ((sum[i] - j + 998244353) % 998244353) % 998244353 * a[j] % 998244353) % 998244353; } } for (int i = (100005 - 1); i >= (1); --i) { for (int j = i + i; j < 100005; j += i) { ans[i] = (ans[i] - ans[j] + 998244353) % 998244353; } } printf("%lld\n", ans[1]); } int main() { clock_t start, finish; start = clock(); solve(); finish = clock(); return 0; }
6
#include<bits/stdc++.h> using namespace std; typedef unsigned long long ull; char s[100001]; int main(){ ull i,j,h,w; cin>>h>>w; for(i=0;i<w+2;i++) cout<<"#"; cout<<endl; for(i=0;i<h;i++){ cin>>s; cout<<"#"<<s<<"#"<<endl; } for(i=0;i<w+2;i++) cout<<"#"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int32_t MOD = 1000000007; struct WEdge { int32_t u, v; int w; bool operator<(const WEdge& other) { return w < other.w; } WEdge(int32_t u, int32_t v, int w) : u(u), v(v), w(w) {} WEdge() {} void in() { cin >> u >> v >> w; } void trace() { if (0) cout << u << " " << v << " " << w << "\n"; } }; struct Edge { int32_t u, v; Edge(int32_t u, int32_t v) : u(u), v(v) {} void in() { cin >> u >> v; } }; struct UnionSet { map<int32_t, pair<int32_t, int32_t> > sets; void makeSet(int32_t x) { sets.emplace(x, make_pair(x, 0)); } void init(int32_t a, int32_t b) { for (int x = (a); x < (b); ++x) makeSet(x); } int find(int32_t x) { if (sets[x].first == x) return x; else return sets[x].first = find(sets[x].first); } void union_both(int32_t x, int32_t y) { int32_t parentX = find(x), parentY = find(y); int32_t rankX = sets[parentX].second, rankY = sets[parentY].second; if (parentX == parentY) return; else if (rankX < rankY) sets[parentX].first = parentY; else sets[parentY].first = parentX; if (rankX == rankY) sets[parentX].second++; } }; int32_t add(int32_t a, int32_t b) { int32_t r = a + b; if (r >= MOD) r -= MOD; return r; } int32_t sub(int32_t a, int32_t b) { int32_t r = a - b; if (r < 0) r += MOD; return r; } int32_t mul(int32_t a, int32_t b) { return (a * 1ll * b) % MOD; } int32_t pow_mod(int32_t a, int64_t n) { if (n == 0) return 1; else { int32_t tmp = pow_mod(a, n / 2); tmp = mul(tmp, tmp); if (n % 2 == 0) return tmp; else return mul(tmp, a); } } int32_t fact_mod(int32_t n) { int32_t r = 1; for (int32_t k = 1; k <= n; k++) r = mul(r, k); return r; } vector<int> children[200001]; int a[200001]; int64_t S[200001]; int64_t T[200001]; void dfs_init(int u, int pred) { S[u] = a[u]; T[u] = 0; for (auto v : children[u]) { if (v != pred) { dfs_init(v, u); S[u] += S[v]; T[u] += T[v] + S[v]; } } } int64_t M = -1; void dfs_maxt(int u, int pred, int64_t sa, int64_t ta) { M = max(M, ta + T[u]); sa += a[u]; ta += sa; int64_t ts = 0, tt = 0; for (auto v : children[u]) { if (v != pred) { ts += S[v]; tt += 2ll * S[v] + T[v]; } } for (auto v : children[u]) { if (v != pred) { dfs_maxt(v, u, sa + ts - S[v], ta + tt - T[v] - 2ll * S[v]); } } } int32_t main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = (0); i < (n); ++i) cin >> (a + 1)[i]; int u, v; for (int i = (0); i < (n - 1); ++i) { cin >> u >> v; children[u].push_back(v); children[v].push_back(u); } dfs_init(1, -1); dfs_maxt(1, -1, 0, 0); cout << M << "\n"; }
6
#include <bits/stdc++.h> const int MAXN = 100005; char s[MAXN]; using namespace std; struct Node { int* value[4][11]; } tree[MAXN]; void build_tree(int n) { for (int x = 1; x <= n; x++) for (int i = 0; i < 4; i++) for (int j = 1; j <= 10; j++) { tree[x].value[i][j] = new int[j]; for (int k = 0; k < j; k++) tree[x].value[i][j][k] = 0; } } void update(int pos, int i, int value, int n) { for (int x = pos; x <= n; x += ((x) & (-x))) for (int j = 1; j <= 10; j++) tree[x].value[i][j][(pos - 1) % j] += value; } int query(int x, int i, int j, int k) { int ret = 0; while (x) { ret += tree[x].value[i][j][k]; x -= ((x) & (-x)); } return ret; } inline int change(char c) { switch (c) { case 'A': return 0; case 'T': return 1; case 'G': return 2; case 'C': return 3; } } int main() { scanf("%s", s); int n = strlen(s); build_tree(n); for (int i = 0; i < n; i++) update(i + 1, change(s[i]), 1, n); int q; scanf("%d", &q); while (q--) { int op; scanf("%d", &op); if (op == 1) { int x; char c; scanf("%d %c", &x, &c); update(x, change(s[x - 1]), -1, n); update(x, change(c), 1, n); s[x - 1] = c; } else { int l, r; char e[15]; scanf("%d%d%s", &l, &r, e); int m = strlen(e); int ans = 0; for (int i = 0; e[i]; i++) { int temp = query(r, change(e[i]), m, (l + i - 1) % m) - query(l - 1, change(e[i]), m, (l + i - 1) % m); ans += temp; } printf("%d\n", ans); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; double power(float x, int y) { float temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else { if (y > 0) return x * temp * temp; else return (temp * temp) / x; } } bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } void solve() { long long int n; cin >> n; vector<long long int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; ; long long int ans = a[0]; long long int t = a[0]; for (long long int i = 1; i < n; i++) { t += a[i]; ans = min(ans, t); } cout << abs(ans) << endl; } int main() { long long int t; cin >> t; while (t--) solve(); }
2
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ int h,w,n; int x[100100],y[100100]; int wx[200100],wy[200100]; scanf("%d%d%d",&h,&w,&n); for(int i=0;i<n;i++){ scanf("%d%d",&x[i],&y[i]); wx[2*i] = wx[2*i+1] = x[i]; wy[2*i] = wy[2*i+1] = y[i]; } sort(wx,wx+2*n); sort(wy,wy+2*n); ll sumx[2]={}, sumy[2]={}; int med = n-1; for(int i=0;i<2*n;i++){ sumx[0] += abs(wx[i]-wx[med]); sumy[0] += abs(wy[i]-wy[med]); sumx[1] += abs(wx[i]-wx[med+1]); sumy[1] += abs(wy[i]-wy[med+1]); } ll ans = 1e18; pair<int,int> cord(0,0); for(int i=0;i<n;i++){ int dx = 0, dy = 0; if(x[i]<=wx[med])dx=1; if(y[i]<=wy[med])dy=1; int X = wx[med+dx], Y = wy[med+dy]; ll cur = sumx[dx] + sumy[dy] - abs(X-x[i]) - abs(Y-y[i]); if(ans > cur){ ans = cur; cord = make_pair(X,Y); }else if(ans == cur){ cord = min(cord, make_pair(X,Y)); } } printf("%lld\n", ans); printf("%d %d\n",cord.first, cord.second); }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, a[maxn], b[maxn], ans[maxn]; vector<int> g[maxn]; bool visited[maxn]; void dfs(int i, int status) { visited[i] = 1; ans[i] = status % 2 + 1; for (int x : g[i]) { if (!visited[x]) { dfs(x, status + 1); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } for (int i = 1; i < 2 * n; i += 2) { g[i].push_back(i + 1); g[i + 1].push_back(i); } for (int i = 1; i <= 2 * n; i++) { if (!visited[i]) { dfs(i, 0); } } for (int i = 0; i < n; i++) { cout << ans[a[i]] << ' ' << ans[b[i]] << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; int ans = a + b + c; int A = a, B = b, C = c; for (int i = 1; i <= 10000; i++) { int tmp1 = abs(i - a); for (int j = i; j <= 15000; j += i) { int tmp2 = abs(j - b); for (int k = j; k <= 15000; k += j) { int tmp3 = abs(k - c); int temp = tmp1 + tmp2 + tmp3; if (temp < ans) { ans = temp; A = i, B = j, C = k; } } } } cout << ans << endl; cout << A << " " << B << " " << C << endl; } }
4
#include <bits/stdc++.h> using namespace std; int cmpfunc(const void *a, const void *b) { return (*(int *)a - *(int *)b); } long long int gcd(long long int m, long long int n) { if (n == 0) return m; return gcd(n, m % n); } long long int fastpow(long long int a, long long int b, long long int m) { long long int r = 1; while (b > 0) { if (b % 2 == 1) r = (r * a) % m; b = b >> 1; a = (a * a) % m; } return r; } int prime(long long int x) { if (x == 1) return 0; if (x <= 3) return 1; if (x % 6 == 1 || x % 6 == 5) { long long int y = sqrt(x), i; for (i = 2; i <= y; i++) if (x % i == 0) return 0; return 1; } return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long int n, m; cin >> n >> m; long long int candidates[n + 1]; for (long long int i = 1; i <= n; i++) candidates[i] = 0; long long int a[m + 1][n + 1]; for (long long int i = 1; i <= m; i++) for (long long int j = 1; j <= n; j++) cin >> a[i][j]; long long int winner[m + 1]; for (long long int i = 1; i <= m; i++) { winner[i] = 0; } for (long long int i = 1; i <= m; i++) { long long int maxindex = 0; long long int max = -1; for (long long int j = 1; j <= n; j++) { if (a[i][j] > max) { max = a[i][j]; maxindex = j; } } winner[i] = maxindex; } long long int value[101]; for (long long int i = 1; i <= 100; i++) value[i] = 0; for (long long int i = 1; i <= m; i++) { value[winner[i]]++; } long long int max = 0; long long int maxindex = 0; for (long long int i = 1; i <= 100; i++) { if (value[i] > max) { max = value[i]; maxindex = i; } } cout << maxindex << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { string num; cin >> num; int len = num.size(); for (int i = 0; i < len;) { if ((num[i] != '1') && (num[i] != '4')) { cout << "NO" << endl; return 0; } else if (num[i] == '1' && num[i + 1] != '4') { i++; } else if (num[i] == '1' && num[i + 1] == '4' && num[i + 2] != '4') { i = i + 2; } else if (num[i] == '1' && num[i + 1] == '4' && num[i + 2] == '4') { i = i + 3; } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, x, y, a[100002]; int main() { cin >> n >> x >> y; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { int pd = 1; for (int j = max(1, i - x); j <= min(n, i + y); j++) if (a[j] < a[i]) pd = 0; if (pd) cout << i, exit(0); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; string s[60]; int xx2, yy2, xx1 = 110, yy1 = 110; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < n; j++) { if (s[i][j] - '0') { xx1 = min(i, xx1); xx2 = max(i, xx2); yy1 = min(j, yy1); yy2 = max(j, yy2); } } } int flag = 0; for (int i = xx1; i <= xx2; i++) for (int j = yy1; j <= yy2; j++) { if (i == xx1 || i == xx2) { if (j == yy1 || j == yy2) { if (s[i][j] != '1') flag = 1; } else if (s[i][j] != '2') flag = 1; } else { if (j == yy1 || j == yy2) { if (s[i][j] != '2') flag = 1; } else if (s[i][j] != '4') flag = 1; } } if (flag) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> void chkmin(T1 &x, T2 y) { if (x > y) x = y; } template <typename T1, typename T2> void chkmax(T1 &x, T2 y) { if (x < y) x = y; } inline void debug(int x) { fprintf(stderr, "ycx has aked ioi %d times\n", x); } namespace fastio { char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf; inline char getc() { return p1 == p2 && (p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2) ? -1 : *p1++; } inline void putc(char x) { (*p3++ = x); } template <typename T> void read(T &x) { x = 0; char c = getchar(); T neg = 0; while (!isdigit(c)) neg |= !(c ^ '-'), c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); if (neg) x = (~x) + 1; } template <typename T> void recursive_print(T x) { if (!x) return; recursive_print(x / 10); putc(x % 10 ^ 48); } template <typename T> void print(T x) { if (!x) putc('0'); if (x < 0) putc('-'), x = ~x + 1; recursive_print(x); } void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); } } // namespace fastio const int LOG = 60; const int MAXM = 30; const int MOD = 1e9 + 7; long long n; int m, d[LOG + 2], dc = -1; struct mat { int a[MAXM + 2][MAXM + 2]; mat() { memset(a, 0, sizeof(a)); } mat operator*(const mat &rhs) { mat ret; for (int i = 0; i <= m; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) ret.a[i][j] = (ret.a[i][j] + 1ll * a[i][k] * rhs.a[k][j]) % MOD; return ret; } } ans, mul, a[LOG + 2][MAXM + 2], suf[LOG + 2][MAXM + 2], pre[LOG + 2][MAXM + 2]; int main() { scanf("%lld%d", &n, &m); while (n) { d[++dc] = n % m; n /= m; } for (int i = 0; i <= dc; i++) { if (!i) { for (int j = 0; j < m; j++) { a[i][j].a[m][m] = 1; for (int k = 0; k < m; k++) if (k ^ j) a[i][j].a[k][k] = 1; for (int k = 0; k <= m; k++) a[i][j].a[j][k] = 1; } } else { for (int j = 0; j < m; j++) { if (!j) a[i][j] = suf[i - 1][0]; else a[i][j] = suf[i - 1][j] * pre[i - 1][j - 1]; } } pre[i][0] = a[i][0]; for (int j = 1; j < m; j++) pre[i][j] = pre[i][j - 1] * a[i][j]; suf[i][m - 1] = a[i][m - 1]; for (int j = m - 2; ~j; j--) suf[i][j] = a[i][j] * suf[i][j + 1]; } int sum = 0; ans.a[m][0] = 1; for (int i = 0; i <= m; i++) mul.a[i][i] = 1; for (int i = dc; ~i; i--) { while (d[i]) { d[i]--; mul = mul * a[i][sum]; sum++; if (sum == m) sum = 0; } } ans = mul * ans; int ret = 0; for (int i = 0; i <= m; i++) ret = (ret + ans.a[i][0]) % MOD; printf("%d\n", ret % MOD); return 0; }
5
#include <bits/stdc++.h> using namespace std; string res; vector<string> ele; vector<int> data; int main() { int n; scanf("%d", &n); string tmp; while (cin >> tmp) ele.push_back(tmp); if (ele.size() == 1 && ele[0] == "int") { cout << "int" << endl; return 0; } for (int i = 0; i < (int)ele.size(); i++) { if (ele[i] == "pair") { data.push_back(2); res += "pair<"; } else { if (data.size() == 0) { cout << "Error occurred" << endl; return 0; } data.back() -= 1; if (data.back() < 0) { cout << "Error occurred" << endl; return 0; } res += "int"; if (data.back() == 1) res += ","; else { while (data.back() == 0) { data.pop_back(); res += ">"; if (data.size() == 0) break; data.back()--; if (data.back() < 0) { cout << "Error occurred" << endl; return 0; } if (data.back() == 1) res += ","; } } } } if (data.size() != 0) { cout << "Error occurred" << endl; return 0; } cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; pair <int, int> p[200100]; int n[200100], d[200100]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) cin >> p[i].first >> p[i].second; sort (p, p + N); d[N] = 1; for (int i = N - 1; i >= 0; i--) { int c = i + 1; while (c < N && p[c].first < p[i].first + p[i].second) c = n[c]; d[i] = (d[i+1] + d[n[i] = c]) % 998244353; } cout << d[0] << "\n"; }
0
#include <bits/stdc++.h> using namespace std; long long binpow(long long a, long long n) { long long res = 1; while (n) { if (n & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; n >>= 1; } return res; } int main(int argc, char** argv) { string S; cin >> S; long long k; cin >> k; long long res = 0; long long L = ((int)(S).size()); for (long long i = 0; i < L; ++i) { if (S[i] == '0' || S[i] == '5') { res = (res + binpow(2, i)) % 1000000007; } } long long den = binpow(binpow(2, L) - 1, 1000000007 - 2); long long num = binpow(2, k * L) - 1; res = (res * num) % 1000000007; res = (res * den) % 1000000007; cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const double pi = 2 * acos(0.0); double inff = 1.0 / 0.0; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long int x, y, n, i, z, j, k, w, u, q, m, t, g; long long int fact[200011]; vector<long long int> adj[200011]; void pre_calc() { fact[0] = 1; for (i = (int)1; i < (int)n + 1; i++) { fact[i] = fact[i - 1] * i; if (fact[i] >= 998244353) fact[i] %= 998244353; } } long long int dfs(long long int src, long long int p) { long long int prod = 1; for (auto a : adj[src]) { if (a != p) { prod *= dfs(a, src); if (prod >= 998244353) prod %= 998244353; } } prod = (prod * fact[(int)adj[src].size()]); if (prod >= 998244353) prod %= 998244353; return prod; } void solve() { cin >> n; pre_calc(); for (i = (int)1; i < (int)n; i++) { cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } w = dfs(1, 0); cout << (w * n) % 998244353 << "\n"; } int main() { int TESTS = 1; ios::sync_with_stdio(false), cout.tie(0), cin.tie(0); ; while (TESTS--) { solve(); } }
2
#include <bits/stdc++.h> using namespace std; long long n; string a, b; int cnt[50005][26]; int main(void) { cin >> n; cin >> a >> b; long long N = n / 2; long long ans = 0; for (int i = 0; i < N; i++) { char a1 = a[i], a2 = a[n - i - 1]; char b1 = b[i], b2 = b[n - i - 1]; if (b1 == b2) { if (a1 == a2) continue; ans++; } else { if (a1 == b1) { if (a2 == b2) continue; ans++; } else if (a1 == b2) { if (a2 == b1) continue; ans++; } else { if (a2 == b1 || a2 == b2) ans++; else ans += 2; } } } if (n % 2 && a[n / 2] != b[n / 2]) ans++; cout << ans << endl; return 0; }
4