solution
stringlengths
52
181k
difficulty
int64
0
6
#include <cstdio> #include <algorithm> #define M 86401 using namespace std; int main(){ int i,n,v,tf,tt,h,m,s,l[M]; while(1){ scanf("%d",&n); if(!n){break;} fill(l,l+M,0);v=0; for(i=0;i<n;i++){ scanf("%d:%d:%d",&h,&m,&s); tf=h*3600+m*60+s; scanf("%d:%d:%d",&h,&m,&s); tt=h*3600+m*60+s; l[tf]++; l[tt]--; } for(i=1;i<M;i++){ l[i]+=l[i-1]; } for(i=0;i<M;i++){ v=max(v,l[i]); } printf("%d\n",v); } return 0; }
0
#include <bits/stdc++.h> using namespace std; void re(int& x); template <class T, class... Ts> void re(T& t, Ts&... ts); void pr(int x); void pr(const char* x); void ps(); template <class T, class... Ts> void ps(const T& t, const Ts&... ts); int e, s, n, m; int query[210000]; pair<int, int> sta[210000]; vector<pair<int, int> > ev; pair<int, int> ans[210000]; int has[10]; void solve() { re(e, s, n, m); for (int i = 0; i < (n); ++i) re(sta[i].first, sta[i].second); for (int i = 0; i < (m); ++i) re(query[i]); for (int i = 0; i < (n); ++i) { ev.emplace_back(max(-e, -sta[i].second - s), sta[i].first); ev.emplace_back(max(-e, -sta[i].second), -sta[i].first); } for (int i = 0; i < (m); ++i) { ev.emplace_back(max(-e, -query[i] - s), i + 200); } sort((ev).begin(), (ev).end()); pair<int, int> cur = {0, 0}; int la = -e; for (pair<int, int> p : ev) { if (p.first != la) { if (has[3]) { } else if (has[2] && cur.first != -1) cur.second += (p.first - la); else if (has[1] && cur.first != -1) cur.first += (p.first - la); else cur = {-1, -1}; } la = p.first; if (p.second < 200) { if (p.second < 0) has[(-p.second)]--; else has[p.second]++; } else { ans[p.second - 200] = cur; } } for (int i = 0; i < (m); ++i) ps(ans[i].first, ans[i].second); } int main() { solve(); } void re(int& x) { scanf("%d", &x); } template <class T, class... Ts> void re(T& t, Ts&... ts) { re(t); re(ts...); } void pr(int x) { printf("%d", x); } void pr(const char* x) { printf("%s", x); } void ps() { pr("\n"); } template <class T, class... Ts> void ps(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(" "); ps(ts...); }
5
#include <bits/stdc++.h> using namespace std; const long long INF = 1e15; const int N = 2e5 + 50; long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } long long modinverse(long long a, long long m) { return binpow(a, m - 2, m); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int k; cin >> k; int ans[105][105]; for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100; j++) { ans[i][j] = 0; } } int idx = 1; while (1) { long long val = idx * (idx - 1) * (idx - 2); val /= 6; if (val > k) { break; } else { idx++; } } idx--; for (long long i = 1; i <= idx; i++) { for (long long j = 1; j <= idx; j++) { if (j != i) ans[i][j] = 1; } } long long tot = (idx * (idx - 1) * (idx - 2)) / 6; idx++; while (tot < k) { int cnt = 0; for (long long j = 1; j <= idx; j++) { if (j == idx) { ans[j][j] = 0; } else { tot += cnt; cnt++; if (tot > k) break; ans[idx][j] = ans[j][idx] = 1; } } tot -= (cnt - 1); idx++; } cout << idx << "\n"; for (long long i = 1; i <= idx; i++) { for (long long j = 1; j <= idx; j++) cout << ans[i][j]; cout << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; const int N = 1e5 + 5; const int INF = 1e18; const int K = 320; int n, a[N], cnt[K][N], q, l, r, ty; deque<int> d[K]; void rebuild() { for (int i = 0; i < n; i++) { cnt[i / K][a[i]]++; d[i / K].push_back(a[i]); } } void upd(int l, int r) { int posr = r % K, posl = l % K, bl = l / K, br = r / K; if (bl == br) { int z = d[bl][posr]; d[bl].erase(d[bl].begin() + posr); d[bl].insert(d[bl].begin() + posl, z); return; } int z = d[br][posr]; d[br].erase(d[br].begin() + posr); cnt[br][z]--; for (int i = br; i > bl; i--) { int x = d[i - 1].back(); d[i].push_front(x); d[i - 1].pop_back(); cnt[i][x]++; cnt[i - 1][x]--; } d[bl].insert(d[bl].begin() + posl, z); cnt[bl][z]++; } int get(int l, int r, int x) { int posr = r % K, posl = l % K, bl = l / K, br = r / K; int res = 0; if (bl == br) { for (int i = posl; i <= posr; i++) { if (d[bl][i] == x) res++; } return res; } for (int i = posl; i < d[bl].size(); i++) { if (d[bl][i] == x) res++; } for (int i = bl + 1; i < br; i++) { res += cnt[i][x]; } for (int i = 0; i <= posr; i++) { if (d[br][i] == x) res++; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } rebuild(); int last = 0; cin >> q; for (int test = 1; test <= q; test++) { cin >> ty >> l >> r; l = (l + last - 1) % n; r = (r + last - 1) % n; if (l > r) swap(l, r); if (ty == 1) { upd(l, r); } else { int k; cin >> k; k = (last + k - 1) % n + 1; last = get(l, r, k); cout << last << "\n"; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long T; struct edge { int x1, y1, x2, y2; edge(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; vector<edge> v; void seal(int p, int x) { switch (x) { case 0: v.push_back(edge(p - 1, p + 2, p, p + 2)); v.push_back(edge(p - 1, p + 1, p, p + 1)); v.push_back(edge(p + 1, p - 1, p + 1, p)); v.push_back(edge(p + 2, p - 1, p + 2, p)); break; case 1: v.push_back(edge(p - 1, p + 1, p, p + 1)); v.push_back(edge(p + 1, p - 1, p + 1, p)); v.push_back(edge(p + 2, p - 1, p + 2, p)); break; case 2: v.push_back(edge(p - 1, p + 1, p, p + 1)); v.push_back(edge(p + 1, p - 1, p + 1, p)); break; case 3: v.push_back(edge(p - 1, p + 2, p, p + 2)); v.push_back(edge(p + 1, p - 1, p + 1, p)); v.push_back(edge(p + 2, p - 1, p + 2, p)); break; case 4: v.push_back(edge(p + 1, p - 1, p + 1, p)); v.push_back(edge(p + 2, p - 1, p + 2, p)); break; case 5: v.push_back(edge(p + 1, p - 1, p + 1, p)); break; } } int main() { scanf("%lld", &T); stack<int> s; while (T) { s.push(T % 6); T /= 6; } v.push_back(edge(1, 2, 2, 2)); v.push_back(edge(2, 1, 2, 2)); int n = 2; while (!s.empty()) { seal(n, s.top()); s.pop(); if (!s.empty()) { v.push_back(edge(n + 2, n, n + 3, n)); v.push_back(edge(n + 2, n + 1, n + 3, n + 1)); v.push_back(edge(n + 3, n - 1, n + 4, n - 1)); v.push_back(edge(n + 3, n, n + 4, n)); v.push_back(edge(n, n + 2, n, n + 3)); v.push_back(edge(n + 1, n + 2, n + 1, n + 3)); v.push_back(edge(n - 1, n + 3, n - 1, n + 4)); v.push_back(edge(n, n + 3, n, n + 4)); } n += 2; } printf("%d %d\n", n, n); printf("%lu\n", v.size()); for (auto e : v) { printf("%d %d %d %d\n", e.x1, e.y1, e.x2, e.y2); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1 * 1000 + 10; int n, m, flag, tedad[2], ja[maxn][4], a, b, color[2][2]; bool rang[2][4]; char graph[maxn][maxn]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> graph[i][j]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (graph[i][j] != '0') rang[i % 2][int(graph[i][j]) - 49] = 1; } } for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { if (rang[i][j] == 1) tedad[i]++; } if (tedad[i] > 2) { flag++; break; } } if (flag < 1) { for (int i = 0; i < 4; i++) { if (rang[0][i] == 1 && rang[1][i] == 1) { flag++; break; } } } if (flag < 1) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (graph[i][j] != '0' && ja[i][int(graph[i][j]) - 49] == 0) { ja[i][int(graph[i][j]) - 49] = (j % 2) + 1; for (int k = 0; k < 4; k++) { if (k != int(graph[i][j]) - 49) ja[i][k] = 2 - (j % 2); } } if (graph[i][j] != '0' && ja[i][int(graph[i][j]) - 49] != (j % 2) + 1) { flag++; break; } } if (flag > 0) break; } } if (flag == 0) { for (int i = 0; i < 4; i++) { if (rang[0][i] == 0 && rang[1][i] == 0) { if (tedad[0] < 2) { tedad[0]++; rang[0][i] = 1; } else { tedad[1]++; rang[1][i] = 1; } } if (rang[0][i] == 1) { color[0][a] = i + 1; a++; } else { color[1][b] = i + 1; b++; } } for (int i = 0; i < n; i++) { int c1, c2; if (ja[i][0] == 0) { c1 = color[i % 2][0]; c2 = color[i % 2][1]; } else { if (ja[i][color[i % 2][0] - 1] == 1) { c1 = color[i % 2][0]; c2 = color[i % 2][1]; } else { c1 = color[i % 2][1]; c2 = color[i % 2][0]; } } for (int j = 0; j < m; j++) { if (graph[i][j] != '0') cout << graph[i][j]; else { if (j % 2 == 0) cout << c1; else cout << c2; } } cout << endl; } return 0; } else { for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) rang[i][j] = 0; } tedad[0] = tedad[1] = 0; color[0][0] = color[0][1] = color[1][0] = color[1][1] = 0; for (int i = 0; i < n; i++) ja[i][0] = ja[i][1] = ja[i][2] = ja[i][3] = 0; for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { if (graph[i][j] != '0') rang[j % 2][int(graph[i][j]) - 49] = 1; } } for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { if (rang[i][j] == 1) tedad[i]++; } if (tedad[i] > 2) { flag++; break; } } if (flag < 2) { for (int i = 0; i < 4; i++) { if (rang[0][i] == 1 && rang[1][i] == 1) { flag++; break; } } } if (flag < 2) { for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { if (graph[i][j] != '0' && ja[j][int(graph[i][j]) - 49] == 0) { ja[j][int(graph[i][j]) - 49] = (i % 2) + 1; for (int k = 0; k < 4; k++) { if (k != int(graph[i][j]) - 49) ja[j][k] = 2 - (i % 2); } } if (graph[i][j] != '0' && ja[j][int(graph[i][j]) - 49] != (i % 2) + 1) { flag++; break; } } if (flag > 1) break; } } if (flag == 1) { for (int i = 0; i < 4; i++) { if (rang[0][i] == 0 && rang[1][i] == 0) { if (tedad[0] < 2) { tedad[0]++; rang[0][i] = 1; } else { tedad[1]++; rang[1][i] = 1; } } if (rang[0][i] == 1) { color[0][a] = i + 1; a++; } else { color[1][b] = i + 1; b++; } } for (int j = 0; j < m; j++) { int c1, c2; if (ja[j][0] == 0) { c1 = color[j % 2][0]; c2 = color[j % 2][1]; } else { if (ja[j][color[j % 2][0] - 1] == 1) { c1 = color[j % 2][0]; c2 = color[j % 2][1]; } else { c1 = color[j % 2][1]; c2 = color[j % 2][0]; } } for (int i = 0; i < n; i++) { if (graph[i][j] == '0') { if (i % 2 == 0) graph[i][j] = char(int(c1) + 48); else graph[i][j] = char(int(c2) + 48); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << graph[i][j]; cout << endl; } return 0; } else { cout << 0; return 0; } } return 0; }
5
#include <bits/stdc++.h> const int maxn = 100010; const int coor[4] = {1, 2, 0, -1}; long long P[maxn + 1][3]; long long lower[4], upper[4]; bool first[4]; int n; long long X, Y, Z; void init() { int i, j; scanf("%d", &n); for (i = 1; i <= n; ++i) for (j = 0; j < 3; ++j) scanf("%I64d", &P[i][j]); } bool ky(long long C, long long r) { int i, j, k; long long s, t; first[0] = first[1] = first[2] = first[3] = true; for (i = 1; i <= n; ++i) { for (j = 0; j < 4; ++j) { s = 0; for (k = 0; k < 3; ++k) { if (coor[j] == k) s -= P[i][k]; else s += P[i][k]; } if (j != 3) { t = (s - C - r) / 2; while (t + t < s - C - r) ++t; } else { t = (s - C - 3 * r) / 2; while (t + t < s - C - 3 * r) ++t; } if (first[j] || t > lower[j]) lower[j] = t; if (j != 3) { t = (s + C - r) / 2; while (t + t > s + C - r) --t; } else { t = (s + C - 3 * r) / 2; while (t + t > s + C - 3 * r) --t; } if (first[j] || t < upper[j]) upper[j] = t; first[j] = false; if (lower[j] > upper[j]) return false; } } if (lower[0] + lower[1] + lower[2] > upper[3]) return false; if (upper[0] + upper[1] + upper[2] < lower[3]) return false; for (i = 0; i < 3; ++i) { if (lower[0] + lower[1] + lower[2] >= lower[3]) break; t = lower[3] - (lower[0] + lower[1] + lower[2]); if (t <= upper[i] - lower[i]) { lower[i] += t; break; } lower[i] = upper[i]; } X = lower[0] + lower[1] + r; Y = lower[1] + lower[2] + r; Z = lower[2] + lower[0] + r; return true; } void work() { long long start, stop, mid; start = 0; stop = 7e18; while (start <= stop) { mid = (start + stop) / 2; if (ky(mid, 0)) stop = mid - 1; else if (ky(mid, 1)) stop = mid - 1; else start = mid + 1; } } void output() { printf("%I64d %I64d %I64d\n", X, Y, Z); } int main() { int t, T; scanf("%d", &T); for (t = 1; t <= T; ++t) { init(); work(); output(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { double a,l,x; while(cin >> a >> l >> x) { double b=sqrt(l*l-(a/2)*(a/2)); double c=(l+x)/2; double d=sqrt(c*c-(l/2)*(l/2)); printf("%.10f\n",a*b/2+l*d); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y, maxi = 0; long long arr[1000], arr1[1000]; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < n; i++) cin >> arr1[i]; for (int i = 0; i < n; i++) { x = arr[i]; y = arr1[i]; for (int j = i + 1; j < n; j++) { x = (x | arr[j]); y = (y | arr1[j]); maxi = max(maxi, (x + y)); } maxi = max(maxi, (x + y)); } cout << maxi << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, p = 1, q = 1, r = 1, s = 2; long long mx1 = 0, mx2 = 0; long long arr[2010][2010]; unordered_map<long long, long long> mp; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int t; cin >> t; arr[i][j] = t; mp[i + j + 5000] += t; mp[i - j] += t; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if ((i + j) % 2 == 0) { if (mx1 < mp[i + j + 5000] + mp[i - j] - arr[i][j]) { mx1 = mp[i + j + 5000] + mp[i - j] - arr[i][j]; p = i + 1; q = j + 1; } } else { if (mx2 < mp[i + j + 5000] + mp[i - j] - arr[i][j]) { mx2 = mp[i + j + 5000] + mp[i - j] - arr[i][j]; r = i + 1; s = j + 1; } } } } cout << mx1 + mx2 << endl << p << " " << q << " " << r << " " << s << " " << endl; return 0; }
3
#include<bits/stdc++.h> using namespace std; typedef long long LL; #define rep(i,a,b) for(int i=a;i<=b;i++) const int N=1e2+5; LL n,g,p[N],c[N],a[N],ans; int main() { cin>>n>>g; rep(i,1,n) { cin>>p[i]>>c[i]; a[i]=i; } LL ans=1e18; do { LL sum=0,tot=0; rep(i,1,n) { int j=a[i]; if(sum+p[j]*j*100+c[j]<=g) { sum+=p[j]*j*100+c[j]; tot+=p[j]; } else { LL ad=(g-sum)/(j*100); if((g-sum)%(j*100))ad++; tot+=min(ad,p[j]); ans=min(ans,tot); break; } } ans=min(ans,tot); }while(next_permutation(a+1,a+1+n)); cout<<ans; return 0; }
0
#include<stdio.h> #include<assert.h> #include<map> #include<cmath> #include<set> #include<queue> #include<algorithm> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll, ll> pll; const int MX = 200005; const int MM = 1000000007; ll gcd(ll a, ll b){ return b ? gcd(b, a%b) : a; } ll pw(ll a, ll b, ll M){ ll r = 1; while(b){ if( b&1 ) r = r*a % M; a = a*a % M; b /= 2; } return r; } pll ex_gcd(ll a, ll b){ if( b == 0 ) return pll(1, 0); else{ pll t = ex_gcd(b, a%b); // b * t.first + (a - b*a/b) * t.second return pll(t.second, t.first - a/b*t.second); } } ll get_answer(ll a, ll b, ll c, ll d){ ll g = gcd(b, d); pll t = ex_gcd(b, d); ll x = ((__int128) (c-a)/g * t.first * b + a) % (b*d/g); assert((c-a)%g == 0); if( x < 32 ) x += b*d/g; return x; } ll inv(ll A, ll B){ assert(gcd(A, B) == 1); pll t = ex_gcd(A, B); return (t.first%B + B) % B; } ll P(ll X){ ll r = 1; for(int i = 2; i*i <= X; i++){ if( X%i == 0 ){ X /= i, r *= (i-1); } while( X%i == 0 ){ X /= i, r *= i; } } if( X != 1 ) r *= (X-1); return r; } ll O = 1; ll F1(int A, int M){ if( A == 0 ) return M*(30/M+1); ll B = A, N = M; if( gcd(A, M) != 1 ){ while(gcd(A, N) != 1){ O *= gcd(A, N); N /= gcd(A, N); } ll x = F1(pw(A, O, N), N) * O; return x; } ll Pi = P(M), G = gcd(Pi, M); ll xi = F1(A%G, G); ll x = get_answer(pw(A, xi, M) * inv(O, M) % M, M, xi, Pi); return x; } void solve(){ int A, M; scanf("%d%d", &A, &M); O = 1; ll p = F1(A, M); printf("%lld\n", p); } int main() { int Q; scanf("%d", &Q); for(int i = 1; i <= Q; i++) solve(); }
0
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); i++) using namespace std; int main(){ int n, m; while(cin >> m >> n, m|n){ bool check[8000000] = {}; int cnt = 0, ans = 0; for(int i=m; i<8000000; i++){ if( check[i] == false ){ if( cnt < n ){ cnt++; for(int j = i; j<8e6; j+=i){ check[j] = true; } }else{ ans = i; break; } } } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; int n, m; char s[maxn + 3], t[maxn + 3]; int cnt1[maxn + 3], cnt2[maxn + 3]; int lst1[maxn + 3], lst2[maxn + 3]; int main(void) { scanf("%s%s", s + 1, t + 1); n = strlen(s + 1); m = strlen(t + 1); for (int i = 1; i <= n; ++i) { cnt1[i] = cnt1[i - 1] + (s[i] != 'A'); lst1[i] = s[i] == 'A' ? lst1[i - 1] : i; } for (int i = 1; i <= m; ++i) { cnt2[i] = cnt2[i - 1] + (t[i] != 'A'); lst2[i] = t[i] == 'A' ? lst2[i - 1] : i; } int q; scanf("%d", &q); while (q--) { int l1, r1, l2, r2; scanf("%d%d%d%d", &l1, &r1, &l2, &r2); int a = r1 - max(l1 - 1, lst1[r1]), b = r2 - max(l2 - 1, lst2[r2]); int c = cnt1[r1] - cnt1[l1 - 1], d = cnt2[r2] - cnt2[l2 - 1]; if (a < b) { putchar('0'); continue; } if ((a - b) % 3) { c += 2; } if (c <= d && !(d - c & 1) && !(r1 - l1 + 1 == a && d && a == b)) { putchar('1'); } else { putchar('0'); } } putchar('\n'); return 0; }
4
#include <iostream> #include <algorithm> using namespace std; int main() { int m; cin >> m; while(m--) { int A, y, n; cin >> A; cin >> y; cin >> n; int maxv = 0; for(int t = 0; t < n; ++t) { int mode, q; double p; cin >> mode >> p >> q; int a = A; int c = 0; for(int i = 0; i < y; ++i) { int b = (int)(a * p); if(mode) { a = a + b - q; } else { a = a - q; c += b; } } maxv = max(maxv, a+c); } cout<< maxv << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int in() { int32_t x; scanf("%d", &x); return x; } inline long long lin() { long long x; scanf("%lld", &x); return x; } inline string get() { char ch[2000010]; scanf("%s", ch); return ch; } inline void read(int *a, int n) { for (int i = 0; i < n; i++) a[i] = in(); } template <class blank> inline void out(blank x) { cout << x << "\n"; exit(0); } template <class blank> inline void smin(blank &a, blank b) { a = min(a, b); } template <class blank> inline void smax(blank &a, blank b) { a = max(a, b); } const int maxn = 1e5 + 10; const int maxm = 1e6 + 10; const int maxlg = 21; const int base = 29; const int mod = 1e9 + 7; const int inf = 1e9 + 10; const int SQ = 317; int dp[2][205][1005], a[maxn], dif[maxn]; int32_t main() { dp[0][0][0] = 1; int n = in(), k = in(); for (int i = 1; i <= n; i++) a[i] = in(); sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) dif[i] = a[i] - a[i - 1]; for (int i = 1; i <= n; i++) { int cur = i & 1; int pre = cur ^ 1; memset(dp[cur], 0, sizeof dp[cur]); for (int j = 0; j < i; j++) { for (int l = 0; l <= k - dif[i] * j; l++) { int l2 = l + dif[i] * j; dp[cur][j][l2] = (dp[cur][j][l2] + dp[pre][j][l]) % mod; dp[cur][j][l2] = (dp[cur][j][l2] + 1LL * dp[pre][j][l] * j % mod) % mod; dp[cur][j + 1][l2] = (dp[cur][j + 1][l2] + dp[pre][j][l]) % mod; if (j) dp[cur][j - 1][l2] = (dp[cur][j - 1][l2] + 1LL * dp[pre][j][l] * j % mod) % mod; } } } cout << accumulate(dp[n & 1][0], dp[n & 1][0] + k + 1, 0, [](const int &a, const int &b) { return (a + b) % mod; }) << "\n"; }
6
#include <bits/stdc++.h> using namespace std; int a[1001][1001]; int m, o1[100001], o2[100001], t[100001]; void f(int n) { int c = -1; if (n == 1) return; for (int i = 1; i <= n; i++) { bool b = 0; for (int j = 1; j <= n; j++) if (a[j][i]) { b = 1; break; } if (!b) { c = i; break; } } if (n != c) { for (int i = 1; i <= n; i++) swap(a[i][n], a[i][c]); m++; o1[m] = c; o2[m] = n; t[m] = 2; } int r = -1; for (int j = n; j >= 1; j--) { bool b = 0; for (int i = n - 1; i >= 1; i--) if (a[j][i]) { b = 1; break; } if (b) { r = j; break; } } if (r == -1) return; for (int i = 1; i <= n; i++) swap(a[n][i], a[r][i]); if (n != r) { m++; o1[m] = r; o2[m] = n; t[m] = 1; } f(n - 1); } int main() { int n, ans = 0; cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; a[x][y] = 1; } f(n); cout << m << endl; for (int i = 1; i <= m; i++) cout << t[i] << " " << o1[i] << " " << o2[i] << endl; }
3
#include <bits/stdc++.h> using namespace std; const int MAXK = 5000; int n; vector<int> cnt(MAXK + 1, 0); vector<vector<int>> primeExponential(MAXK + 1, vector<int>(MAXK + 1, 0)); void Preprocess() { for (int i = 2; i < MAXK + 1; i++) { for (int j = 0; j < MAXK + 1; j++) primeExponential[i][j] += primeExponential[i - 1][j]; int tmp = i; for (int x = 2; x <= sqrt(tmp); x++) { while (tmp % x == 0) { primeExponential[i][x]++; tmp /= x; } } if (tmp > 1) primeExponential[i][tmp]++; } } void Input() { cin >> n; for (int i = 0; i < n; i++) { int k; cin >> k; cnt[k]++; } } void Solve() { Preprocess(); vector<int> bestPD(MAXK + 1, 1); long long ans = 0, cur = 0; for (int i = 1; i < MAXK + 1; i++) { if (!cnt[i]) { bestPD[i] = 1; continue; } for (int j = 1; j < MAXK + 1; j++) { ans += 1LL * primeExponential[i][j] * cnt[i]; cur += 1LL * primeExponential[i][j] * cnt[i]; if (primeExponential[i][j]) bestPD[i] = j; } } while (*max_element(bestPD.begin(), bestPD.end()) > 1) { vector<int> frequency(MAXK + 1, 0); for (int i = 0; i < MAXK + 1; i++) frequency[bestPD[i]] += cnt[i]; int bestPrime = max_element(frequency.begin(), frequency.end()) - frequency.begin(); int bestGroup = frequency[bestPrime]; if (bestGroup * 2 <= n) break; if (bestPrime == 1) break; cur -= bestGroup; cur += (n - bestGroup); ans = min(ans, cur); for (int i = 0; i < MAXK + 1; i++) { if (bestPD[i] != bestPrime) bestPD[i] = 1; if (bestPD[i] == 1) continue; primeExponential[i][bestPD[i]] -= 1; while (bestPD[i] > 1 && primeExponential[i][bestPD[i]] == 0) bestPD[i]--; } } cout << ans << '\n'; } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(0); cin.tie(NULL); Input(); Solve(); return 0; }
4
#include<vector> #include<cmath> #include<map> #include<cstdlib> #include<iostream> #include<sstream> #include<fstream> #include<string> #include<algorithm> #include<cstring> #include<cstdio> #include<set> #include<stack> #include<bitset> #include<functional> #include<ctime> #include<queue> #include<deque> #include<complex> #include<cassert> using namespace std; #define pb push_back #define pf push_front typedef long long lint; typedef complex<double> P; #define mp make_pair #define fi first #define se second typedef pair<int,int> pint; #define All(s) s.begin(),s.end() #define rAll(s) s.rbegin(),s.rend() #define REP(i,a,b) for(int i=a;i<b;i++) #define rep(i,n) REP(i,0,n) //問題文および制約はちゃんと確認しよう! //サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも //とりあえずサンプルを読んでから解法を考えよう? int dp[(1<<21)+10][22]; int main() { memset(dp,0,sizeof(dp)); int n,K;cin>>n>>K;string s; rep(i,n+1){ cin>>s; rep(j,(1<<i)){ if(s[j]=='1') dp[(1<<i)+j][i]++; } } for(int i=n;i>0;i--) rep(j,(1<<i)) rep(l,i+1){ if(dp[(1<<i)+j][l]<1) continue; //cout<<i<<' '<<j<<' '<<l<<' '<<dp[(1<<i)+j][l]<<endl; rep(k,l){ if(k+1<i && ((j&(1<<k))>>k)==((j&(1<<(k+1)))>>(k+1))) continue; int to=(j>>(k+1)),bo=(j&((1<<k)-1)); //cout<<j<<' '<<k<<' '<<(to<<k)+bo<<endl; //if(i==4 && (to<<k)+bo==4) cout<<j<<' '<<k<<endl; if(i==3 && j==1){ //cout<<(to<<k)+bo<<' '<<k<<endl; } dp[(1<<(i-1))+(to<<k)+bo][k]+=dp[(1<<i)+j][l]; } //cout<<dp[4][2]<<endl; } for(int i=n;i>0;i--) rep(j,(1<<i)){ int sum=0; rep(k,i+1) sum+=dp[(1<<i)+j][k]; if(sum>=K){ string out=""; rep(l,i){ out+=('0'+(j%2));j/=2; } reverse(All(out)); cout<<out<<endl;return 0; } } cout<<""<<endl; }
0
#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0); int n, m; while (std::cin >> n >> m) { std::vector<int> f(1 + n), b(1 + m); for (int i = 1; i <= n; ++i) { std::cin >> f[i]; } for (int i = 1; i <= m; ++i) { std::cin >> b[i]; } std::vector<std::vector<int>> g(1 + n); for (int i = 1; i <= n; ++i) { g[f[i]].push_back(i); } std::vector<int> a(1 + m); int nSolutions = 1; for (int i = 1; nSolutions >= 1 && i <= m; ++i) { auto &to = g[b[i]]; if (to.empty()) { nSolutions = 0; } else if (to.size() == 1u) { a[i] = to.front(); } else { nSolutions = 2; } } if (nSolutions == 0) { std::cout << "Impossible\n"; } else if (nSolutions == 2) { std::cout << "Ambiguity\n"; } else { std::cout << "Possible\n"; for (int i = 1; i <= m; ++i) { std::cout << a[i] << " "; } std::cout << std::endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int inf = 10000000; long long arr[100005]; int main() { int n, k; cin >> n >> k; int a, b, c, d; for (int i = 0; i < (n); i++) { scanf("%d.%d.%d.%d", &a, &b, &c, &d); arr[i] += a; arr[i] <<= 8; arr[i] += b; arr[i] <<= 8; arr[i] += c; arr[i] <<= 8; arr[i] += d; } set<long long> ss; long long ans = 0; for (int i = 31; i > 0; i--) { ans |= (1ll << i); ss.clear(); for (int j = 0; j < (n); j++) ss.insert((ans & arr[j])); if (((int)(ss).size()) == k) { d = ans % 256; ans >>= 8; c = ans % 256; ans >>= 8; b = ans % 256; ans >>= 8; a = ans % 256; return printf("%d.%d.%d.%d\n", a, b, c, d) & 0; } } puts("-1"); return 0; }
3
#include <cstdio> #include <iostream> using namespace std; int ab(int x){ return (x>0)?x:-x; } int main(){ int tt; cin >> tt; for(int times=0; times<tt; ++times){ int m,n; cin >>m >>n; int sqsum=m*m+n*n; //printf("sqsum=%d\n", sqsum); bool prime=true; for(int i=-141; i<=141; ++i){ for(int j=-141; j<=141; ++j){ if( (i==0&&j==0) || (ab(i)==1&&j==0) || (i==0&&ab(j)==1) || (i==m&&j==n)|| (i==-n&&j==m)|| (i==-m&&j==-n)|| (i==n&&j==-m) ) continue; int tmp=i*i+j*j; if(sqsum%(i*i+j*j)==0 && ((i*m+j*n)%tmp==0)&&((i*n-j*m)%tmp==0)){ prime=false; //printf("i=%d, j=%d\n", i, j); break; } } if(!prime) break; } if(prime) printf("P\n"); else printf("C\n"); } }
0
#include <bits/stdc++.h> using namespace std; const int N = 2000007; const int mod = 1e9 + 7; int n; char s[N]; long long f[N][4], cnt = 1; inline long long read() { long long sum = 0, ff = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') ff = -1; ch = getchar(); } while (isdigit(ch)) sum = sum * 10 + ch - 48, ch = getchar(); return sum * ff; } void dp() { for (int i = 1; i <= n; i++) { f[i][1] = f[i - 1][1]; f[i][2] = f[i - 1][2]; f[i][3] = f[i - 1][3]; if (s[i] == 'a') f[i][1] = (f[i][1] + cnt) % mod; if (s[i] == 'b') f[i][2] = (f[i][2] + f[i - 1][1]) % mod; if (s[i] == 'c') f[i][3] = (f[i][3] + f[i - 1][2]) % mod; if (s[i] == '?') { f[i][1] = (3 * f[i][1] + cnt) % mod; f[i][2] = (3 * f[i][2] + f[i - 1][1]) % mod; f[i][3] = (3 * f[i][3] + f[i - 1][2]) % mod; cnt = cnt * 3 % mod; } } } int main() { n = read(); cin >> (s + 1); dp(); printf("%lld", f[n][3]); return 0; }
6
#include <bits/stdc++.h> using namespace std; int s[10] = {1, 3, 2, 6, 4, 5}; int main() { long n, i; cin >> n; if (n < 3) { cout << "-1" << endl; } else if (n == 3) { cout << "210" << endl; } else { int init = 20; while (init <= 210) { int x = init; int y = 0; i = 0; while (x > 0) { y += (x % 10) * s[i]; i++; x /= 10; } if (n % 6 == 0) y += s[5]; else y += s[(n % 6) - 1]; if (y % 7 == 0) break; init += 30; } cout << "1"; for (i = 1; i <= n - 4; i++) cout << "0"; if (init < 100) cout << "0"; cout << init << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; bool check(double x1, double y1, double x2, double y2, double x3, double y3) { double a = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); double b = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); double c = (x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2); double aa = sqrt(a), bb = sqrt(b), cc = sqrt(c); if ((a + b == c || a + c == b || b + c == a) && (aa + bb > cc && aa + cc > bb && bb + cc > aa)) return 1; return 0; } int main() { double x1, x2, x3, y1, y2, y3; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; if (check(x1, y1, x2, y2, x3, y3) == 1) { cout << "RIGHT" << endl; goto abc; } if (check(x1 + 1, y1, x2, y2, x3, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1 + 1, x2, y2, x3, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1, x2 + 1, y2, x3, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1, x2, y2 + 1, x3, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1, x2, y2, x3 + 1, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1, x2, y2, x3, y3 + 1) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1 - 1, y1, x2, y2, x3, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1 - 1, x2, y2, x3, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1, x2 - 1, y2, x3, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1, x2, y2 - 1, x3, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1, x2, y2, x3 - 1, y3) == 1) { cout << "ALMOST" << endl; goto abc; } if (check(x1, y1, x2, y2, x3, y3 - 1) == 1) { cout << "ALMOST" << endl; goto abc; } cout << "NEITHER" << endl; abc:; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, maxi = 0; scanf("%d", &n); string res; for (int i = 0; i < n; ++i) { string name; int x, y, a, b, c, d, e; cin >> name >> x >> y >> a >> b >> c >> d >> e; int sum = x * 100 - y * 50 + a + b + c + d + e; if (i == 0 || sum > maxi) { maxi = sum; res = name; } } cout << res << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long q, m, n, i, j, dummy, a, b, x, y, t, x1, y1; cin >> n >> m >> q; vector<vector<long> > R(100, vector<long>(100, 0)); vector<vector<long> > C(100, vector<long>(100, 0)); vector<vector<long> > E(100, vector<long>(100, 0)); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { R[i][j] = i; C[i][j] = j; } } for (dummy = 0; dummy < q; dummy++) { cin.ignore(); cin >> t >> b; if (t == 1) { x1 = R[b - 1][0]; y1 = C[b - 1][0]; for (j = 0; j < m - 1; j++) { R[b - 1][j] = R[b - 1][j + 1]; C[b - 1][j] = C[b - 1][j + 1]; } R[b - 1][m - 1] = x1; C[b - 1][m - 1] = y1; } if (t == 2) { x1 = R[0][b - 1]; y1 = C[0][b - 1]; for (i = 0; i < n - 1; i++) { R[i][b - 1] = R[i + 1][b - 1]; C[i][b - 1] = C[i + 1][b - 1]; } R[n - 1][b - 1] = x1; C[n - 1][b - 1] = y1; } if (t == 3) { x = b; cin >> y >> a; x1 = R[x - 1][y - 1]; y1 = C[x - 1][y - 1]; E[x1][y1] = a; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cout << E[i][j]; cout << " "; } cout << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; const int mod = 1e9 + 7; const char a[] = {'a', 'e', 'i', 'o', 'u'}; char s[maxn], t[maxn]; set<char> S; bool check(char a, char b) { bool fla = (S.find(a) == S.end()); bool flb = (S.find(b) == S.end()); return (fla ^ flb); } int main() { scanf("%s%s", s, t); int lens = strlen(s); int lent = strlen(t); if (lens != lent) { printf("No\n"); return 0; } S.clear(); for (int i = 0; i < 5; ++i) S.insert(a[i]); for (int i = 0; i < lens; ++i) if (s[i] != t[i]) { if (check(s[i], t[i])) { printf("No\n"); return 0; } } printf("Yes\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>; const int N = 1e6 + 5; int dsu[N]; int trace(int x) { return dsu[x] < 0 ? x : dsu[x] = trace(dsu[x]); } void join(int x, int y) { if ((x = trace(x)) == (y = trace(y))) return; if (dsu[x] > dsu[y]) swap(x, y); dsu[x] += dsu[y]; dsu[y] = x; } int solve(int n, int m) { fill(dsu, dsu + m, -1); for (int i = (0); i < (m); i++) { int x = (i + 2 * (n - 1)) % (2 * (m - 1)); if (x >= m) x = 2 * (m - 1) - x; join(i, x); x = (i - 2 * (n - 1)) % (2 * (m - 1)); if (x < 0) x += 2 * (m - 1); if (x >= m) x = 2 * (m - 1) - x; join(i, x); } int ans = 0; for (int i = (0); i < (m); i++) { ans += (dsu[i] < 0); } return ans; } int n, m; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; cout << solve(min(n, m), max(m, n)) << '\n'; }
3
#include <bits/stdc++.h> using namespace std; using li = long long; using ld = long double; using pii = pair<int, int>; using vi = vector<int>; using matr = vector<vi>; const int INF = 1e9 + 13; vector<int> hungary(vector<vector<int>> a) { int n = a.size() - 1; int m = n; vector<int> u(n + 1), v(m + 1), p(m + 1), way(m + 1); for (int i = 1; i <= n; ++i) { p[0] = i; int j0 = 0; vector<int> minv(m + 1, INF); vector<char> used(m + 1, false); do { used[j0] = true; int i0 = p[j0], delta = INF, j1; for (int j = 1; j <= m; ++j) if (!used[j]) { int cur = a[i0][j] - u[i0] - v[j]; if (cur < minv[j]) minv[j] = cur, way[j] = j0; if (minv[j] < delta) delta = minv[j], j1 = j; } for (int j = 0; j <= m; ++j) if (used[j]) u[p[j]] += delta, v[j] -= delta; else minv[j] -= delta; j0 = j1; } while (p[j0] != 0); do { int j1 = way[j0]; p[j0] = p[j1]; j0 = j1; } while (j0); } vector<int> ans(n + 1); for (int j = 1; j <= m; ++j) ans[j] = p[j]; return ans; } void solve() { int n, k; cin >> n >> k; vector<int> x(n), y(n); for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } vector<vector<int>> a(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < k - 1; j++) { a[i + 1][j + 1] = x[i] + y[i] * j; } for (int j = k - 1; j < n - 1; j++) { a[i + 1][j + 1] = y[i] * (k - 1); } a[i + 1][n] = x[i] + y[i] * (k - 1); } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) a[i][j] = -a[i][j]; } vector<int> res = hungary(a); vector<int> ans; for (int i = 0; i < k - 1; i++) { ans.push_back(res[i + 1]); } for (int i = k - 1; i < n - 1; i++) { ans.push_back(res[i + 1]); ans.push_back(-res[i + 1]); } ans.push_back(res[n]); cout << ans.size() << endl; for (auto x : ans) cout << x << ' '; cout << endl; } int main() { int t = 1; cin >> t; while (t--) solve(); }
6
#include <bits/stdc++.h> using namespace std; void printNumber(int number) { if (number < 10) cout << "0"; cout << number; } int main(void) { ios_base::sync_with_stdio(0); int h1, h2, m1, m2; char bs; cin >> h1 >> bs >> m1; cin >> h2 >> bs >> m2; int minutes1 = h1 * 60 + m1; int minutes2 = h2 * 60 + m2; int medium = (minutes1 + minutes2) / 2; int res_h = medium / 60; int res_min = medium % 60; printNumber(res_h); cout << ":"; printNumber(res_min); cout << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { deque<int> vec; int arr[200005]; int n, a; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a); vec.push_back(a); arr[i] = 0; } for (int i = 0; i < n; i++) { scanf("%d", &a); int j = 0, i1 = 0; if (arr[a - 1] == 1) { printf("0 "); } else if (vec.empty() != true) { int k = 0, f = 0; while (true) { arr[vec.front() - 1] = 1; if (vec.front() == a) { vec.pop_front(); break; } vec.pop_front(); k++; } printf("%d ", k + 1); } else printf("0 "); } }
2
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int a = 0, others = 0, temp; vector<char> c; for (int i = 0; i < s.size(); i++) { if (s[i] == 'a') { a++; temp = i; } else { others++; c.push_back(s[i]); } } int si = s.size(); if (a == si) cout << s << endl; else if (others % 2 == 1) printf(":(\n"); else if ((temp + (others / 2)) > (si - 1)) printf(":(\n"); else { int x = others / 2; int flag = 0; for (int i = 0, j = x; i < x && j < c.size(); i++, j++) { if (c[i] != c[j]) { flag = 1; break; } } if (flag == 1) printf(":(\n"); else { s.erase(si - x); cout << s << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<int> a, b; int main() { int n, m, c; cin >> n >> m >> c; a.resize(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } b.resize(m); for (int i = 0; i < m; ++i) { cin >> b[i]; } for (int i = 1; i < m; ++i) { b[i] = (b[i - 1] + b[i]) % c; } for (int i = 0; i < m; ++i) { a[i] = (a[i] + b[i]) % c; if (i - (n - m) - 1 >= 0) { a[i] = (a[i] - b[i - (n - m) - 1]) % c; if (a[i] < 0) { a[i] += c; } } } for (int i = m; i < n; ++i) { a[i] = (a[i] + b[m - 1]) % c; if (m - n + i - 1 >= 0 && m - n + i - 1 < m) { a[i] = (a[i] - b[m - n + i - 1]) % c; if (a[i] < 0) { a[i] += c; } } } for (int i = 0; i < n; ++i) { cout << a[i] << " "; } }
4
#include<cstdio> const int MAXN=20005,MAXV=30001; int ans[MAXN]; bool used[MAXV]; int gcd(int a,int b) {return b==0?a:gcd(b,a%b);} int main() { int n,sum=0,i,j; scanf("%d",&n); for(i=1,j=1;j<n;i++)//添加n-1个2、3的倍数 if(i%2==0||i%3==0) { ans[j++]=i; used[i]=true; sum+=i; } int r2=sum%2,r3=sum%3; for(i=1;j<=n;i++)//枚举最后一个数 if(!used[i]&&(i+r2)%2==0&&(i+r3)%3==0&&gcd(i,sum)!=1) ans[j++]=i; for(int i=1;i<n;i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); return 0; }
0
#include <bits/stdc++.h> using namespace std; constexpr int MAX_N = 2E3 + 5, MOD = 1E9 + 7; int n, m; string s[MAX_N]; int dp[MAX_N][MAX_N][2]; vector<int> row[MAX_N], col[MAX_N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; for (int i = 0; i < n; ++i) cin >> s[i]; if (n == 1) { cout << (count(s[0].begin(), s[0].end(), 'R') == 0) << endl; return 0; } if (m == 1) { cout << (count(s, s + n, "R") == 0) << endl; return 0; } dp[1][1][0] = 1; dp[1][1][1] = 1; for (int i = 0; i < n; ++i) for (int j = j = m - 1; j >= 0; --j) if (s[i][j] == 'R') row[i + 1].push_back(j + 1); for (int i = 0; i < m; ++i) for (int j = n - 1; j >= 0; --j) if (s[j][i] == 'R') col[i + 1].push_back(j + 1); for (int i = 2; i <= m; ++i) { if (row[1].size() <= m - i) dp[1][i][0] = dp[1][i - 1][1]; else dp[1][i][0] = (dp[1][i - 1][1] - dp[1][row[1][m - i] - 1][1] + MOD) % MOD; dp[1][i][1] = 1; } for (int i = 2; i <= n; ++i) { if (col[1].size() <= n - i) dp[i][1][1] = dp[i - 1][1][0]; else dp[i][1][1] = (dp[i - 1][1][0] - dp[col[1][n - i] - 1][1][0] + MOD) % MOD; dp[i][1][0] = 1; } for (int i = 2; i <= n; ++i) for (int j = 2; j <= m; ++j) { if (row[i].size() <= m - j) dp[i][j][0] = dp[i][j - 1][1]; else dp[i][j][0] = (dp[i][j - 1][1] - dp[i][row[i][m - j] - 1][1] + MOD) % MOD; if (col[j].size() <= n - i) dp[i][j][1] = dp[i - 1][j][0]; else dp[i][j][1] = (dp[i - 1][j][0] - dp[col[j][n - i] - 1][j][0] + MOD) % MOD; if (i != n || j != m) { dp[i][j][0] = (dp[i][j][0] + dp[i - 1][j][0]) % MOD; dp[i][j][1] = (dp[i][j][1] + dp[i][j - 1][1]) % MOD; } } cout << (dp[n][m][0] + dp[n][m][1]) % MOD << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main(void) { srand(time(0)); cout << fixed << setprecision(7); cerr << fixed << setprecision(7); int n, q; scanf("%d%d", &n, &q); static long long s[1 << 23]; for (int i = 1; i <= n; ++i) scanf("%I64d", &s[i]), s[i + n] = s[i + n + n] = s[i]; for (int i = 2; i <= n + n + n; ++i) s[i] += s[i - 1]; static int nxt[1 << 23]; for (int i = 1; i <= n; ++i) nxt[i] = i; for (int i = n + 1; i <= n + n + n; ++i) nxt[i] = nxt[i - n]; while (q--) { int v; scanf("%d", &v); if (s[n] <= v) { cout << "1\n"; continue; } static int a[1 << 22]; int cnt = 0; for (int i = 1; i <= n + n; ++i) { while (s[cnt] - s[i - 1] <= v) ++cnt; a[i] = cnt; } int ans = 0; cnt = 1; for (int i = 1; i <= n; ++i) cnt = nxt[a[cnt]]; const int lim = cnt + n; while (cnt < lim) ++ans, cnt = a[cnt]; cout << ans << '\n'; } cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, i, a[2000005] = {0}; int main() { cin >> n; while (n--) { cin >> i; a[i] = i; } for (int j = 1; j < 2000005; j++) if (a[j] == 0) a[j] = a[j - 1]; int sol = 0; for (int j = 2; j < 2000005; j++) if (a[j] == j) for (i = j - 1; i < 2000005; i += j) if (a[i] % j > sol and a[i] > j) sol = a[i] % j; cout << sol << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> v(n); for (auto &i : v) cin >> i; vector<vector<pair<long long, long long>>> g(n); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; --x; --y; g[x].push_back({y, i}); g[y].push_back({x, i}); } queue<int> q; vector<bool> s(n); for (int i = 0; i < n; i++) if (g[i].size() <= v[i]) { q.push(i); s[i] = true; } vector<int> d(n); for (int i = 0; i < n; i++) d[i] = g[i].size(); vector<int> ans; while (!q.empty()) { int c = q.front(); q.pop(); for (auto w : g[c]) if (d[w.first] > 0) { --d[w.first]; --d[c]; ans.push_back(w.second + 1); if (d[w.first] <= v[w.first] && !s[w.first]) { q.push(w.first); s[w.first] = true; } } } if (ans.size() == m) { cout << "ALIVE\n"; reverse(ans.begin(), ans.end()); for (int i = 0; i < m; i++) cout << ans[i] << " \n"[i == m - 1]; } else cout << "DEAD\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { string s; int a[123], k, m = 0, sum = 0; cin >> s >> k; for (int i = 'a'; i <= 'z'; i++) { cin >> a[i]; m = max(m, a[i]); } for (int i = 0; i < s.size(); i++) sum += (i + 1) * a[s[i]]; for (int i = s.size() + 1; i <= s.size() + k; i++) sum += i * m; cout << sum << endl; }
2
#include <bits/stdc++.h> using namespace std; struct solution_dag { vector<vector<int> > v; int path_12; solution_dag() { v = vector<vector<int> >(4); v[0].push_back(2); v[2].push_back(3); v[3].push_back(1); path_12 = 3; } void square(void) { int A = v[0][0]; v[0].clear(); int B = add_node(); int C = add_node(); int D = add_node(); v[0].push_back(D); v[D].push_back(B); v[D].push_back(C); v[B].push_back(A); v[C].push_back(A); path_12 += 2; } void plus_1(void) { int A = v[0][0]; int prev = A; for (int i = 0; i < path_12 - 2; i++) { int next = add_node(); v[prev].push_back(next); prev = next; } v[prev].push_back(1); } int add_node(void) { int res = v.size(); v.push_back(vector<int>()); return res; } }; const int MX = 1009; bool ans[MX][MX]; const int LG = 32; int k_digs[LG]; int main() { int k; cin >> k; for (int i = 0; i < LG; i++) { k_digs[i] = k % 2; k /= 2; } int it = LG - 1; while (k_digs[it] == 0) { it--; } it--; solution_dag D; while (it >= 0) { if (k_digs[it] == 1) { D.square(); D.plus_1(); } else { D.square(); } it--; } int n = D.v.size(); assert(n <= 1000); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ans[i][j] = false; for (int i = 0; i < n; i++) { for (size_t j = 0; j < D.v[i].size(); j++) { int k = D.v[i][j]; ans[i][k] = true; ans[k][i] = true; } } cout << n << '\n'; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << (ans[i][j] ? 'Y' : 'N'); cout << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; stack<char> st; bool player1 = false; st.push('#'); for (int i = 0; i < s.size(); i++) { if (st.top() == s[i]) { player1 = !player1; st.pop(); } else { st.push(s[i]); } } if (player1) { cout << "Yes"; } else { cout << "No"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; bool up = 0, lo = 0; int ans = INT_MAX; int n = s.size(); for (int i = 0; i < n; i++) { if (isupper(s[i])) up = 1; if (islower(s[i])) lo = 1; } if (up == 0 || lo == 0) { puts("0"); return 0; } int cnt = 0; ans = 0; for (int i = 0; i < n; i++) { if (islower(s[i])) cnt++; if (isupper(s[i])) { if (cnt > 0) { cnt--, ans++; } } } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const string schools[] = {"Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"}; map<char, int> id; vector<vector<size_t> > generate(vector<size_t> dist, char c) { if (c != '?') { dist[id[c]]++; return vector<vector<size_t> >(1, dist); } int minValue = *min_element(dist.begin(), dist.end()); vector<vector<size_t> > result; for (int i = 0; i < int(4); ++i) { if (dist[i] == minValue) { dist[i]++; result.push_back(dist); dist[i]--; } } return result; } int main() { id['G'] = 0; id['H'] = 1; id['R'] = 2; id['S'] = 3; int n; cin >> n; string s; cin >> s; set<vector<size_t> > variants; variants.insert(vector<size_t>(4)); for (int i = 0; i < int(s.size()); ++i) { set<vector<size_t> > news; for (set<vector<size_t> >::const_iterator it = variants.begin(); it != variants.end(); ++it) { vector<vector<size_t> > vars = generate(*it, s[i]); news.insert(vars.begin(), vars.end()); } variants = news; } vector<int> ans; for (set<vector<size_t> >::const_iterator it = variants.begin(); it != variants.end(); ++it) { int minValue = *min_element((*it).begin(), (*it).end()); for (int i = 0; i < int((*it).size()); ++i) { if ((*it)[i] == minValue) { ans.push_back(i); } } } sort(ans.begin(), ans.end()); ans.erase(unique(ans.begin(), ans.end()), ans.end()); for (int i = 0; i < int(ans.size()); ++i) { cout << schools[ans[i]] << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int power1[1000001], power2[1000001], lt, h1, h2, ans, base = 127, mod1 = 1000000007, mod2 = 1000000009; string t; vector<long long int> hash1, hash2; vector<pair<long long int, string> > v[1000001]; void dfs(long long int cur) { for (long long int i = 0; i < v[cur].size(); i++) { long long int x = v[cur][i].first; string s = v[cur][i].second; for (long long int j = 0; j < s.length(); j++) { hash1.push_back((hash1.back() * base + s[j] - 'a' + 1) % mod1); hash2.push_back((hash2.back() * base + s[j] - 'a' + 1) % mod2); if (hash1.size() - lt - 1 >= 0) { long long int temp_h1 = (((hash1.back() - hash1[hash1.size() - lt - 1] * power1[lt]) % mod1 + mod1) % mod1); long long int temp_h2 = (((hash2.back() - hash2[hash2.size() - lt - 1] * power2[lt]) % mod2 + mod2) % mod2); if (temp_h1 == h1 && temp_h2 == h2) ans++; } } dfs(x); for (long long int j = 0; j < s.length(); j++) { hash1.pop_back(); hash2.pop_back(); } } } int main() { ios::sync_with_stdio(false); ; cin.tie(0); ; long long int i, j, n, x, y; string s; cin >> n; power1[0] = 1; for (i = 1; i <= 100000; i++) power1[i] = (power1[i - 1] * base) % mod1; power2[0] = 1; for (i = 1; i <= 100000; i++) power2[i] = (power2[i - 1] * base) % mod2; for (i = 2; i <= n; i++) { cin >> x >> s; v[x].push_back(make_pair(i, s)); } cin >> t; lt = t.length(); for (i = 0; i < t.length(); i++) { h1 = (h1 * base + t[i] - 'a' + 1) % mod1; h2 = (h2 * base + t[i] - 'a' + 1) % mod2; } hash1.push_back(0); hash2.push_back(0); dfs(1); cout << ans; return 0; }
5
#include<bits/stdc++.h> using namespace std; using ll = long; int main(){ ll ans = 0 ,last = 0; for(ll i = 0; i < 5; i++){ ll a; cin >> a; ans += (a + 9) / 10 * 10; last = max(last, (1000 - a) % 10); } cout << ans - last; }
0
#include <bits/stdc++.h> using namespace std; long long how(int n) { long long c = 0; for (int i = 1; i <= n; i++) c += i; return c; } int main() { int n, mas[100000]; cin >> n; for (int i = 0; i < n; i++) cin >> mas[i]; int temp = 0; long long sum = n; for (int i = 1; i < n; i++) { if (mas[i] == mas[i - 1]) temp++; else { sum += how(temp); temp = 0; } } sum += how(temp); cout << sum; return 0; }
2
#include<iostream> #include<vector> #include<algorithm> #include<cstdio> #include<map> using namespace std; int main(){ string G[8]={ "AAA", "AA", "A", "B", "C", "D", "E", "NA", }; double a,b; while(cin>>a>>b){ int p=(int)(a*1000),q=(int)(b*1000); int gp,gq; if(p<35500)gp=0; else if(p<37500)gp=1; else if(p<40000)gp=2; else if(p<43000)gp=3; else if(p<50000)gp=4; else if(p<55000)gp=5; else if(p<70000)gp=6; else gp=7; if(q<71000)gq=0; else if(q<77000)gq=1; else if(q<83000)gq=2; else if(q<89000)gq=3; else if(q<105000)gq=4; else if(q<116000)gq=5; else if(q<148000)gq=6; else gq=7; int g=max(gp,gq); cout<<G[g]<<endl; } }
0
#include <bits/stdc++.h> using namespace std; const int INF = 100000; int d[101][101]; int main() { srand(clock()); ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) cin >> s[i]; int m; cin >> m; vector<int> a(m); for (int i = 0; i < m; i++) cin >> a[i], a[i]--; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { if (s[i][j] == '1') d[i][j] = 1; else d[i][j] = INF; } } } for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); vector<int> ans; ans.push_back(a[0]); int last = 0; for (int i = 1; i < m; i++) { if (d[a[last]][a[i]] != i - last) { ans.push_back(a[i - 1]); last = i - 1; } } ans.push_back(a[m - 1]); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] + 1 << ' '; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100 + 10; const int MAX_V = 200 + 10; const int MAX_S = 1 << 7; const int INF = 1000000; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; int N, M, K, S; int w[MAX_N][MAX_N]; int f[MAX_N][MAX_N][MAX_S]; int pre[MAX_N][MAX_N][MAX_S]; int qx[MAX_V]; int qy[MAX_V]; int in[MAX_N][MAX_N]; int cov[MAX_N][MAX_N]; void Init() { int x, y; scanf("%d%d%d", &N, &M, &K); S = 1 << K; for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) scanf("%d", &w[i][j]); for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) for (int k = 1; k < S; ++k) { f[i][j][k] = INF; pre[i][j][k] = INF; } for (int i = 0; i < K; ++i) { scanf("%d%d", &x, &y); f[x][y][1 << i] = w[x][y]; } } void SPFA(int s) { int qh = 0, qt = 0; for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) { qx[qt] = i; qy[qt++] = j; in[i][j] = true; } int nx, ny, px, py; for (; qh < qt;) { px = qx[qh]; py = qy[qh++]; for (int i = 0; i < 4; ++i) { nx = px + dx[i]; ny = py + dy[i]; if (nx > 0 && nx <= N && ny > 0 && ny <= M && f[nx][ny][s] > f[px][py][s] + w[nx][ny]) { f[nx][ny][s] = f[px][py][s] + w[nx][ny]; pre[nx][ny][s] = -i; if (!in[nx][ny]) { in[nx][ny] = true; qx[qt] = nx; qy[qt++] = ny; } } } in[px][py] = false; } } void DFS(int x, int y, int s) { cov[x][y] = true; int tmp = pre[x][y][s]; if (tmp == INF) return; if (tmp <= 0) DFS(x - dx[-tmp], y - dy[-tmp], s); else DFS(x, y, tmp), DFS(x, y, s - tmp); } void Solve() { for (int s = 1; s < S; ++s) { for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) for (int ns = (s - 1) & s; ns; ns = (ns - 1) & s) if (f[i][j][s] > f[i][j][ns] + f[i][j][s - ns] - w[i][j]) { f[i][j][s] = f[i][j][ns] + f[i][j][s - ns] - w[i][j]; pre[i][j][s] = ns; } SPFA(s); if (0) { printf("Now is state %d\n", s); for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) printf("%d ", f[i][j][s]); printf("\n"); } } } int ax, ay, ans = INF; for (int i = 1; i <= N; ++i) for (int j = 1; j <= M; ++j) if (f[i][j][S - 1] < ans) { ans = f[i][j][S - 1]; ax = i, ay = j; } printf("%d\n", ans); DFS(ax, ay, S - 1); for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) putchar(cov[i][j] ? 'X' : '.'); putchar('\n'); } } int main() { Init(); Solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 1000000000 + 7; const double EPS = 1e-10; const double PI = 2 * acos(0.0); const int maxn = 1000 + 66; int cnt[maxn]; bool prime[maxn]; long long primelist[maxn], prime_len; void GetPrime() { memset(prime, true, sizeof(prime)); prime_len = 0; long long i; for (i = 2; i <= maxn; i++) { if (prime[i]) primelist[prime_len++] = i; long long j; for (j = 0; j < prime_len; j++) { if (i * primelist[j] > maxn) break; prime[i * primelist[j]] = false; if (i % primelist[j] == 0) break; } } } long long mult_mod(long long a, long long b, long long mod) { a %= mod; b %= mod; long long ans = 0; long long temp = a; while (b) { if (b & 1) { ans += temp; if (ans > mod) ans -= mod; } temp <<= 1; if (temp > mod) temp -= mod; b >>= 1; } return ans; } long long pow_mod(long long a, long long n, long long mod) { long long ans = 1; long long temp = a % mod; while (n) { if (n & 1) ans = mult_mod(ans, temp, mod); temp = mult_mod(temp, temp, mod); n >>= 1; } return ans; } long long Work(long long n, long long p) { long long ans = 0; while (n) { ans += n / p; n /= p; } return ans; } long long GetComMod(long long n, long long m, long long p) { long long ans = 1; for (long long i = 0; i < prime_len && primelist[i] <= n; i++) { long long x = Work(n, primelist[i]); long long y = Work(n - m, primelist[i]); long long z = Work(m, primelist[i]); x -= (y + z); ans *= pow_mod(primelist[i], x, p); ans %= p; } return ans; } int main() { GetPrime(); int k; cin >> k; for (int i = 1; i <= k; i++) cin >> cnt[i]; long long res = 1, tempsum = cnt[1]; for (int i = 2; i <= k; i++) { res = (res % MOD * GetComMod(tempsum + cnt[i] - 1, cnt[i] - 1, MOD)) % MOD; tempsum += cnt[i]; } cout << res << endl; return 0; }
3
#include<bits/stdc++.h> using namespace std; int a,b; int main() { cin>>a>>b; if((a+b)%2==0)printf("%d\n",(a+b)/2); else puts("IMPOSSIBLE"); }
0
#include <bits/stdc++.h> using namespace std; inline long long getint() { long long _x = 0, _tmp = 1; char _tc = getchar(); while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar(); if (_tc == '-') _tc = getchar(), _tmp = -1; while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar(); return _x * _tmp; } long long ncase = 1; long long N, M; string str; const int mxn = 2e5 + 5; int v[mxn]; long long bit[mxn], tot; void pre() { return; } void init() { N = getint(); M = getint(); for (int i = 1; i <= N; ++i) v[i] = getint(), tot += v[i]; } inline int lb(int k) { return k & (-k); } void build() { for (int i = 1; i <= N; ++i) { for (int j = i; j <= N; j += lb(j)) bit[j] += v[i]; } } bool succ(int n) { long long sum = 0; for (; n; n -= lb(n)) sum += bit[n]; return sum > M; } void sol() { int n = N; long long ans = 0; build(); while (n) { while (tot > M) { int L = 0, R = N; while (R - L > 1) { int mid = (L + R) >> 1; succ(mid) ? R = mid : L = mid; } tot -= v[R]; --n; for (int i = R; i <= N; i += lb(i)) bit[i] -= v[R]; } if (n == 0) break; ans += ((M / tot) * n); M %= tot; } cout << ans << endl; return; } int main() { pre(); while (ncase--) { init(); sol(); } }
4
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #include<utility> #include<cmath> #include<sstream> using namespace std; #define all(a) a.begin(),a.end() typedef vector<int> vi; int main() { double a,b,c,d; const double r = 6378.1; while(cin>>a>>b>>c>>d&&b>=0) { a*=2*M_PI/360.0; b*=2*M_PI/360.0; c*=2*M_PI/360.0; d*=2*M_PI/360.0; double s=acos(cos(a)*cos(c)*cos(b-d)+sin(a)*sin(c)); cout<<(int)(abs(r*s)+0.5)<<endl; } }
0
#include <bits/stdc++.h> using namespace std; long long int fact[11]; void fact0() { fact[0] = fact[1] = 1; for (int i = 2; i <= 11; i++) fact[i] = fact[i - 1] * i; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); fact0(); string s1, s2; cin >> s1 >> s2; long long int plus, min, que; plus = min = que = 0; double x = 0.0; for (int i = 0; i < s1.length(); i++) { if (s1[i] == '+') plus++; else if (s1[i] == '-') min++; if (s2[i] == '+') plus--; else if (s2[i] == '-') min--; else if (s2[i] == '?') que++; } if ((min < 0 || plus < 0) || (que == 0 && (plus != 0 || min != 0))) printf("%.12lf\n", x); else if (!min && !plus) printf("%.12lf\n", x + 1); else { double r = fact[que] / ((double)fact[plus] * fact[min]); double res = (r) / (pow(2, que)); printf("%.12lf\n", res); } }
2
#include <bits/stdc++.h> using namespace std; long long n; vector<long long> a; long long solve(); long long calc(int u, int d, int lef); int main() { cin >> n; a.resize(n); for (auto& p : a) cin >> p; cout << solve() << endl; return 0; } long long solve() { long long res = 0; stack<long long> len, id; len.push(0); a.push_back(0); ++n; for (int i = 0; i < n; ++i) { int nextid = i; while (a[i] < len.top()) { nextid = id.top(); res = max(res, len.top() * (i - id.top())); len.pop(); id.pop(); } if (a[i] > len.top()) { len.push(a[i]); id.push(nextid); } } return res; }
0
#include <bits/stdc++.h> using namespace std; struct player { int score; int win[60]; void init() { score = 0; memset(win, 0, sizeof win); } }; map<string, player> m; map<string, player>::iterator it, winner; string winnername; int a[60] = {0, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1}; int T; int n; int main(int argc, const char* argv[]) { m.clear(); cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; i++) { string name; cin >> name; it = m.find(name); if (it == m.end()) { player newplayer; newplayer.init(); newplayer.score += a[i]; newplayer.win[i]++; m.insert(pair<string, player>(name, newplayer)); } else it->second.score += a[i], it->second.win[i]++; } } winner = m.begin(); for (it = m.begin(); it != m.end(); it++) if (it->second.score > winner->second.score) winner = it; else if (it->second.score == winner->second.score) for (int i = 1; i <= 50; i++) { if (it->second.win[i] > winner->second.win[i]) { winner = it; break; } else if (it->second.win[i] < winner->second.win[i]) break; } winnername = winner->first; cout << winnername << endl; winner = m.begin(); for (it = m.begin(); it != m.end(); it++) if (it->second.win[1] > winner->second.win[1]) winner = it; else if (it->second.win[1] == winner->second.win[1]) { if (it->second.score > winner->second.score) winner = it; else if (it->second.score == winner->second.score) { if (it->second.score > winner->second.score) winner = it; else if (it->second.score == winner->second.score) for (int i = 2; i <= 50; i++) { if (it->second.win[i] > winner->second.win[i]) { winner = it; break; } else if (it->second.win[i] < winner->second.win[i]) break; } } } winnername = winner->first; cout << winnername << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t; t = 1; cin >> t; while (t--) { ll n; cin >> n; ll a[n]; ll mna = INT_MAX, mnb = INT_MAX; for (ll i = 0; i < n; i++) cin >> a[i], mna = min(mna, a[i]); ll b[n]; for (ll i = 0; i < n; i++) cin >> b[i], mnb = min(mnb, b[i]); ll ans = 0; for (ll i = 0; i < n; i++) { if (a[i] >= mna || b[i] >= mnb) ans += (max(a[i] - mna, b[i] - mnb)); } cout << ans << "\n"; } }
2
#include<iostream> using namespace std; int main(){ char m; string a,b,ans; int i,j,c; while(scanf(" %c ",&m)!=EOF){ cin >> a >> b; ans.erase(); i = 0; j = 0; while(i<a.size() && j<b.size()){ if(a[i]==b[j]){ ans.push_back(a[i]); i++; j++; }else{ c = 1; if(a[i]=='('){ if(m=='u')ans.push_back(a[i]); while(c){ i++; if(m=='u')ans.push_back(a[i]); if(a[i]=='(')c++; if(a[i]==')')c--; } i++; }else{ if(m=='u')ans.push_back(b[j]); while(c){ j++; if(m=='u')ans.push_back(b[j]); if(b[j]=='(')c++; if(b[j]==')')c--; } j++; } } } cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; inline long long int modadd(long long int n, long long int m, long long int p = 1000000007) { return ((n + m) % p + p) % p; } inline long long int modsub(long long int n, long long int m, long long int p = 1000000007) { return ((n - m + p) % p + p) % p; } inline long long int modpro(long long int n, long long int m, long long int p = 1000000007) { return (((n % p) * (m % p)) % p + p) % p; } unsigned long long int powe(long long int first, long long int second) { unsigned long long int res = 1; while (second > 0) { if (second & 1) res = res * first; second = second >> 1; first = first * first; } return res; } long long int modpow(long long int first, long long int second, long long int p = 1000000007) { long long int res = 1; while (second > 0) { if (second & 1) res = modpro(res, first, p); second = second >> 1; first = modpro(first, first, p); } return res; } inline long long int modInverse(long long int n, long long int p = 1000000007) { if (n == 1) return 1; return modpow(n, p - 2, p); } inline long long int moddiv(long long int n, long long int m, long long int p = 1000000007) { return modpro(n, modInverse(m, p), p); } inline long long int modadd3(long long int first, long long int second, long long int z, long long int p = 1000000007) { return modadd(modadd(first, second, p), z, p); } inline long long int modadd4(long long int first, long long int second, long long int z, long long int w, long long int p = 1000000007) { return modadd(modadd(first, second, p), modadd(z, w, p), p); } inline long long int modnCr(long long int fac[], int n, int r, long long int p = 1000000007) { if (r == 0) return 1; return modpro(fac[n], modInverse(modpro(fac[r], fac[n - r], p), p), p); } template <typename T> inline T max3(T first, T second, T z) { return max(max(first, second), z); } template <typename T> inline T max4(T first, T second, T z, T w) { return max(max3(first, second, w), z); } template <typename T> inline T min3(T first, T second, T z) { return min(min(first, second), z); } template <typename T> inline T min4(T first, T second, T z, T w) { return min(min3(first, second, w), z); } template <typename T> void printArr(T *arr, int s, int n) { for (int i = s; i <= n; i++) { cout << arr[i] << " "; } cout << endl; } int a[1005]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int erer = 1; cin >> erer; for (int erer2 = (1); erer2 < (erer + 1); erer2++) { int n, m; cin >> n >> m; if (n % 2 == 0) { cout << n / 2 * m << endl; } else { cout << n / 2 * m + (m + 1) / 2 << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int read() { int s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + ch - '0', ch = getchar(); return f * s; } long long a, b, c, d; int main() { int T = read(); while (T--) { a = read(), b = read(), c = read(), d = read(); if (b * c < a) { puts("-1"); continue; } if (d >= c) { printf("%lld\n", a); continue; } long long wei = (a - 1) / b / d; printf("%lld\n", (wei + 1) * a - wei * (wei + 1) / 2 * b * d); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); int dcmp(double x) { if (fabs(x) <= eps) return 0; return (x > 0) ? 1 : -1; }; void file() { freopen("a.in", "r", stdin); } namespace Solver { int n, m, k; const int maxn = 1100000; int v[maxn]; int C[maxn]; int lowbit(int x) { return x & -x; }; int ask(int x) { int res = 0; for (int i = x; i; i -= lowbit(i)) res += C[i]; return res; } void add(int x, int v) { for (int i = x; i < maxn; i += lowbit(i)) C[i] += v; } void solve() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) { int val; scanf("%d", &val); v[val]++; } int cnt = 0; for (int i = 1; i < maxn; i++) { if (v[i]) { if ((1 + ask(i) - (i >= m ? ask(i - m) : 0)) >= k) cnt++; else add(i, 1); } } cout << cnt << endl; } }; // namespace Solver int main() { Solver::solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, k; int a[5111]; char flag[200000010]; int main() { int i, j, kk; int T; scanf("%d%d", &n, &k); for (i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); scanf("%d", &T); while (T--) { int x; int ans = k + 1; scanf("%d", &x); memset(flag, -1, sizeof(flag)); for (i = 0; i < n; i++) { for (j = 0; j <= k; j++) { flag[a[i] * j] = j; } } for (i = 0; i < n; i++) { for (j = 0; j <= k; j++) { if (x >= a[i] * j) if (~flag[x - a[i] * j]) ans = min(ans, j + flag[x - a[i] * j]); } } if (ans > k) ans = -1; printf("%d\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename F, typename S> ostream &operator<<(ostream &os, const pair<F, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; typename vector<T>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } return os << "}"; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; typename set<T>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename F, typename S> ostream &operator<<(ostream &os, const map<F, S> &v) { os << "["; typename map<F, S>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } struct CustomCompare { bool operator()(pair<int64_t, pair<int64_t, int64_t> > &a, pair<int64_t, pair<int64_t, int64_t> > &b) { if (a.first == b.first) return a.second.first > b.second.second; return a.first < b.first; } }; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int64_t t = 1; cin >> t; while (t--) { int64_t a, b, c, d; cin >> a >> b >> c >> d; if (b >= a) { cout << b << endl; continue; } if (d >= c) { cout << -1 << endl; continue; } int64_t ans = b + ceil(1.0 * (a - b) / (c - d)) * c; cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename TH> void _dbg(const char* sdbg, TH h) { cerr << sdbg << "=" << h << "\n"; } template <typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, t...); } template <class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } template <class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (first != t.first) return first < t.first; if (second != t.second) return second < t.second; return third < t.third; } }; template <class T> void ResizeVec(T&, vector<long long>) {} template <class T> void ResizeVec(vector<T>& vec, vector<long long> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } template <class A, class B, class C> ostream& operator<<(ostream& out, Triple<A, B, C> t) { return out << "(" << t.first << ", " << t.second << ", " << t.third << ")"; } template <class T> ostream& operator<<(ostream& out, vector<T> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } bool CmpX(pair<long long, long long> a, pair<long long, long long> b) { return a.second < b.second; } int main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); if (0) cout << fixed << setprecision(10); cin.tie(0); long long n, m, d; cin >> d >> n >> m; vector<pair<long long, long long> > stations; for (long long i = (1); i <= (m); ++i) { long long x, p; cin >> x >> p; stations.push_back({p, x}); } sort((stations).begin(), (stations).end(), CmpX); if (stations[0].second > n || d - stations.back().second > n) { cout << "-1\n"; return 0; } for (long long i = 0; i < (((long long)(stations).size()) - 1); ++i) { if (stations[i + 1].second - stations[i].second > n) { cout << "-1\n"; return 0; } } sort((stations).begin(), (stations).end()); set<long long> used; used.insert(0); used.insert(d); long long res = 0; for (auto stat : stations) { used.insert(stat.second); auto it = used.find(stat.second); it--; long long got = max(0ll, n - (stat.second - *it)); it++; it++; long long need = max(min(n, *it - stat.second) - got, 0ll); res += stat.first * need; } cout << res << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int M = 998244353, G = 3, N = 1 << 20; int Pw(int x, int y = M - 2) { return y ? ((y & 1) ? (long long)Pw((long long)x * x % M, y / 2) * x % M : Pw((long long)x * x % M, y / 2) % M) : 1; } int rt[N], mg[N], fac[N], Lm = 1; int rev[N], a[N], iv; void dft(int n) { for (int i = (0); i <= (n - 1); ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (n >> 1)), ((i < rev[i]) && (a[i] ^= a[rev[i]] ^= a[i] ^= a[rev[i]])); for (int i = 2; i <= n; i <<= 1) for (int j = 0, l = (i >> 1); j < n; j += i) for (int k = 0, pa, pb, *w = rt + l; k < l; k++, w++) pa = a[j + k], pb = (long long)a[j + k + l] * *w % M, a[j + k] = (pa + pb) % M, a[j + k + l] = (pa + M - pb) % M; for (int i = (0); i <= (n - 1); ++i) a[i] = (long long)a[i] * iv % M; } int n, deg[N], cnt[N], ns, x, y, all; int main() { cin >> n, fac[0] = deg[1] = 1; for (int i = (1); i <= (n); ++i) fac[i] = (long long)fac[i - 1] * i % M; for (int i = (1); i <= (n - 1); ++i) cin >> x >> y, deg[x] += 1, deg[y] += 1; for (int i = (1); i <= (n); ++i) if (deg[i] > 1) cnt[deg[i] - 1] += 1, all += 1; for (; Lm <= all; Lm <<= 1) ; int w = Pw(G, (M - 1) / Lm); mg[0] = 1, iv = Pw(Lm); for (int i = (1); i <= (Lm - 1); ++i) mg[i] = (long long)mg[i - 1] * w % M; for (int i = 1, S; i < Lm; i <<= 1) { S = Pw(G, M - 1 - (M - 1) / (i << 1)), rt[i] = 1; for (int j = (i + 1); j <= (i * 2 - 1); ++j) rt[j] = (long long)rt[j - 1] * S % M; } for (int i = (0); i <= (Lm - 1); ++i) a[i] = 1; for (int i = (1); i <= (n); ++i) if (cnt[i]) for (int j = 0, *w = mg; j < Lm; j++, w++) a[j] = (long long)a[j] * Pw(*w + i, cnt[i]) % M; dft(Lm); for (int i = (0); i <= (all); ++i) (ns += (long long)fac[n - i] * a[all - i] % M * ((i & 1) ? M - 1 : 1) % M) %= M; cout << ns << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)9e18; struct cmp { bool operator()(const long long &i, const long long &j) const { return i > j; } }; long long ans = 0; long long sumt = 0; long long sumd = 0; vector<vector<long long>> g; vector<long long> data1; long long dfs(long long v, long long pp) { if (data1[v] != -1) { long long e; if (data1[v] < pp) { cout << -1; exit(0); } long long t = data1[v] - pp; for (long long i = 0; i < g[v].size(); i++) { long long to = g[v][i]; long long zz = dfs(to, data1[v]); } if (t < 0) { cout << -1; exit(0); } return t; } else { if (g[v].size() == 0) return 0; vector<long long> A(g[v].size(), 0); for (long long i = 0; i < g[v].size(); i++) { long long to = g[v][i]; A[i] = dfs(to, pp); } long long mi = inf; for (long long i = 0; i < A.size(); i++) { if (A[i] < mi) mi = A[i]; } for (long long i = 0; i < A.size(); i++) { ans += A[i] - mi; } ans += mi; return 0; } } void dfs1(long long v) {} int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long sum = 0; long long n; cin >> n; data1.resize(n + 1); g.resize(n + 1); vector<long long> pr(n + 1); for (long long i = 2; i <= n; i++) { cin >> pr[i]; g[pr[i]].push_back(i); } for (long long i = 1; i <= n; i++) { cin >> data1[i]; } sumt = data1[1]; long long awd = dfs(1, data1[1]); ans += data1[1]; cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, temp, kqua = 0; cin >> a >> b >> c >> d; if (a > b) { temp = b; b = a; a = temp; } if (a <= c && c <= b) { temp = c; c = b; b = temp; } else if (a > c) { temp = c; c = b; b = a; a = temp; } if (b <= d + a) kqua = d - b + a; if (c <= d + b) kqua = kqua + d - c + b; cout << kqua; return 0; }
1
#include <bits/stdc++.h> #define maxn 505 using namespace std; int n; string s[maxn]; int col[maxn]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; } bool any_black = false; for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) if (s[i][j] == '#') { col[j]++; any_black = true; } } if (!any_black) { printf("-1\n"); return 0; } int ret = 1e9; for (int i = 0; i < n; i++) { int cur = 0; if (col[i] == 0) { cur++; } for (int j = 0; j < n; j++) if (s[i][j] == '.') { cur++; } for (int j = 0; j < n; j++) if (col[j] < n) { cur++; } ret = min(ret, cur); } printf("%d\n", ret); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int i, j, k, m, n, t; long long int x, y, a, b; cin >> t; while (t--) { string s; cin >> s; long long int z = 0, o = 0; n = s.length(); for (i = 0; i < n; i++) { if (s[i] == '0') z++; else o++; } if (o == 0 || z == 0) cout << s << endl; else { string s2; s2 += s[0]; long long int pos = 0; for (i = 1; i < n; i++) { if (s2[pos] == s[i]) { if (s[i] == '1') s2 += '0'; else s2 += '1'; pos++; } s2 += s[i]; pos++; } cout << s2 << endl; } } }
2
#include <bits/stdc++.h> struct Edge { int u, v, id; Edge(int u, int v, int id) : u(u), v(v), id(id) {} }; int b[100005], c[100005]; int map[200005]; std::vector<Edge> g[200005]; int in[200005]; int queue[200005]; int path[100005]; int used[100005]; int p[200005]; struct test { int l, r; test() {} test(int l, int r) : l(l), r(r) {} }; test keep[100005]; test gen[100005]; int getId(int u, int n) { int L = 1, R = n; int res = -1; while (L <= R) { int M = (L + R) / 2; if (map[M] <= u) { res = M; L = M + 1; } else R = M - 1; } return res; } int preWork(int n, int m) { std::sort(map + 1, map + 1 + n); int p = 1, size = 0; while (p <= n) { int np = p; while (np + 1 <= n && map[np + 1] == map[p]) np++; map[++size] = map[p]; p = np + 1; } for (int i = 1; i <= m; i++) { b[i] = getId(b[i], size); c[i] = getId(c[i], size); } return size; } int find(int u) { if (p[u] == u) return u; else return p[u] = find(p[u]); } void addEdge(int u, int v, int total) { p[find(u)] = find(v); in[u]++, in[v]++; g[u].push_back(Edge(u, v, total)); g[v].push_back(Edge(v, u, total)); } void popEdge(int u) { while (g[u].size() != 0) { int id = g[u][g[u].size() - 1].id; if (used[id]) g[u].pop_back(); else break; } } int findEuler(int s) { int t = -1, size = 0; queue[++t] = s; while (t != -1) { int u = queue[t--]; popEdge(u); if (g[u].size() == 0) path[++size] = u; else { queue[++t] = u; Edge edge = g[u][g[u].size() - 1]; g[u].pop_back(); used[edge.id] = 1; queue[++t] = edge.v; } } return size; } int isEuler(int n) { int root = find(1); for (int i = 2; i <= n; i++) if (find(i) != root) return 0; int odd = 0, find = -1; for (int i = 1; i <= n; i++) { if (in[i] % 2) odd++, find = i; } if (odd == 0) return findEuler(1); else if (odd == 2) return findEuler(find); else return 0; } int min(int a, int b) { return a > b ? b : a; } int max(int a, int b) { return a > b ? a : b; } int checkCmp(test u, test v) { if (u.l != v.l) return u.l < v.l; else return u.r < v.r; } int isOk(int n, int m) { if (n != m) return 0; else { std::sort(keep + 1, keep + 1 + n, checkCmp); std::sort(gen + 1, gen + 1 + n, checkCmp); int ok = 1; for (int i = 1; i <= n; i++) { if (keep[i].l != gen[i].l || keep[i].r != gen[i].r) ok = 0; } return ok; } } int main() { int n; scanf("%d", &n); int size = 0; int ok = 1; for (int i = 1; i <= n - 1; i++) { scanf("%d", &b[i]); map[++size] = b[i]; } for (int i = 1; i <= n - 1; i++) { scanf("%d", &c[i]); if (c[i] < b[i]) ok = 0; map[++size] = c[i]; } int count = preWork(size, n - 1); for (int i = 1; i <= count; i++) p[i] = i; for (int i = 1; i <= n - 1; i++) { addEdge(b[i], c[i], i); } int res = isEuler(count); if (ok && res != 0) { for (int i = 1; i <= res; i++) { printf("%d ", map[path[i]]); } printf("\n"); } else printf("-1\n"); return 0; }
5
#include<iostream> #include<vector> using namespace std; #define N 5100 vector<int> x[N]; int n,m,a,b,c,cnt; int V[N]; int main(){ for(int i=0;i<N;i++){ for(int j=0;j<i+5;j++){ x[i].push_back(0); V[i]=j; } } cin>>n>>m; for(int i=0;i<m;i++){ cin>>a>>b>>c;c++; x[a][b]++; x[a+c][b]--; x[a+c+1][b+1]++; x[a][b+1]--; x[a+c][b+c+1]++; x[a+c+1][b+c+1]--; } for(int i=0;i<N;i++){ for(int j=1;j<N;j++){ if(j<V[i]){ x[i][j]+=x[i][j-1]; } } } for(int j=0;j<N;j++){ for(int i=1;i<N;i++){ if(j<V[i]){ x[i][j]+=x[i-1][j]; } } } for(int i=0;i<=n;i++){ for(int j=0;j<=n-i;j++){ if(j+1<V[i+j+1]){ x[i+j+1][j+1]+=x[i+j][j]; } } } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(j<V[i]){ if(x[i][j]>=1){cnt++;} } } } cout<<cnt<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { int x; cin >> x; getchar(); string s; cin >> s; long long ans = 0; for (int i = 0; i < x; i++) { int y; y = s[i] - '0'; if (i == (x - 1)) { ans = ans + y; } else { if (y != 0) ans = ans + y + 1; } } cout << ans << endl; } }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5+10; int n, nim[N]; vector<int> g[N]; void dfs(int u, int f) { for(int v : g[u]) if(v != f) dfs(v,u); for(int v : g[u]) if(v != f) { nim[u] ^= (nim[v] + 1); } } int main() { scanf("%d", &n); for(int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); g[u].push_back(v); g[v].push_back(u); } dfs(1,-1); if(nim[1] == 0) puts("Bob"); else puts("Alice"); return 0; }
0
#pragma GCC optimize("Ofast") #include <cassert> #include <cstdio> #include <cstdlib> #include <string> #include <algorithm> #include <iostream> #include <queue> #include <vector> #include <bitset> #include <cmath> #include <limits> #include <iostream> #include <map> #include <set> #include <stack> #include <tuple> #include <iomanip> #include <functional> #include <complex> using namespace std; using ll=long long int; using pii=pair<int,int>; using pil=pair<int,ll>; using pli=pair<ll,int>; using pll=pair<ll,ll>; using psi=pair<string,int>; using pis=pair<int,string>; using psl=pair<string,ll>; using pls=pair<ll,string>; using pss=pair<string,string>; //辺 fromあり template<typename T > struct Edge { int from, to; T cost; Edge()=default; Edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} inline bool operator<(const Edge p)const noexcept{ return cost<p.cost; } inline bool operator>(const Edge p)const noexcept{ return cost>p.cost; } }; //辺 fromがない template<typename T > struct edge { int to; T cost; edge()=default; edge(int to, T cost) : to(to), cost(cost) {} }; template<typename T> using edges=vector<edge<T>>; template<typename T> using WeightGraph=vector<edges<T>>; using Graph=vector<vector<int>>; template<typename T> using vc=vector<T>; template<typename T> using vvc=vector<vector<T>>; template<typename T> using vvvc=vector<vector<vector<T>>>; template<typename T> using vvvvc=vector<vvvc<T>>; template<typename T> using vvvvvc=vector<vvvvc<T>>; template<class T,class U>inline constexpr bool chmin(T&a,const U b){if(a<=b)return false;a=b;return true;} template<class T,class U>inline constexpr bool chmax(T&a,const U b){if(a>=b)return false;a=b;return true;} #define bit(n,k) ( (n>>k)&1 ) inline void bin101(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout<<fixed<<setprecision(20); } template<typename T> inline void Yes(T flag){ if(flag) cout<<"Yes"<<endl; else cout<<"No"<<endl; } template<typename T> inline void YES(T flag){ if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; } //1-indexed vector cin template<typename T> inline void vin1(vector<T> &v){ for(int i=1;i<v.size();i++) cin>>v[i]; } //0-indexed vector cin template<typename T> inline void vin0(vector<T> &v){ for(int i=0;i<v.size();i++) cin>>v[i]; } //1-indexed vector<vector> cin template<typename T> inline void vin1(vector<vector<T>> &v){ for(int i=1;i<v.size();i++){ for(int j=1;j<v[i].size();j++) cin>>v[i][j]; } } //0-indexed vector<vector> cin template<typename T> inline void vin0(vector<vector<T>> &v){ for(int i=0;i<v.size();i++){ for(int j=0;j<v[i].size();j++) cin>>v[i][j]; } } //デバッグ template<typename T> inline void vout(const vector<T> &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i<sz;i++){ cout<<i<<" "<<v[i]<<'\n'; } cout<<"end\n"<<endl; } //デバッグ template<typename T> inline void vout(const vvc<T> &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i<sz;i++){ int ssz=v[i].size(); for(int j=0;j<ssz;j++){ cout<<i<<" "<<j<<" "<<v[i][j]<<'\n'; } } cout<<"\nend\n"<<endl; } //デバッグ(グリッド) template<typename T> inline void gvout(const vector<T> &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i<sz;i++){ if(i) cout<<" "; cout<<v[i]; } cout<<"\nend\n"<<endl; } //デバッグ(グリッド) template<typename T> inline void gvout(const vvc<T> &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i<sz;i++){ int ssz=v[i].size(); for(int j=0;j<ssz;j++){ if(j) cout<<" "; cout<<v[i][j]; } cout<<endl; } cout<<"end\n"<<endl; } //デバッグ template<typename T> inline void vout(const vvvc<T> &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i<sz;i++){ int ssz=v[i].size(); for(int j=0;j<ssz;j++){ int sssz=v[i][j].size(); for(int k=0;k<sssz;k++){ cout<<i<<" "<<j<<" "<<k<<" "<<v[i][j][k]<<'\n'; } } } cout<<"end\n"<<endl; } //pair cout template<typename T, typename U> inline ostream &operator<<(ostream &os,const pair<T,U> &p) { os<<p.first<<" "<<p.second; return os; } //pair cin template<typename T, typename U> inline istream &operator>>(istream &is,pair<T,U> &p) { is>>p.first>>p.second; return is; } //ソート template<typename T> inline void vsort(vector<T> &v){ sort(v.begin(),v.end()); } //逆順ソート template<typename T> inline void rvsort(vector<T> &v){ sort(v.rbegin(),v.rend()); } //要素数a 初期値x template<typename T> inline vector<T> vmake(int a,T x){ return vector<T>(a,x); } //data[a][b] 初期値x template<typename T> inline vector<vector<T>> vmake(int a,int b,T x){ return vector<vector<T>>(a, vector<T>(b,x)); } //data[a][b][c] 初期値x template<typename T> inline vector<vector<vector<T>>> vmake(int a,int b,int c,T x){ return vector<vector<vector<T>>> (a, vector<vector<T>>(b, vector<T>(c, x))); } //data[a][b][c][d] 初期値x template<typename T> inline vector<vector<vector<vector<T>>>> vmake(int a,int b,int c,int d,T x){ return vector<vector<vector<vector<T>>>> (a,vvvc<T>(b,vvc<T>(c,vc<T>(d,x)))); } //data[a][b][c][d][e] 初期値x template<typename T> inline vvvvvc<T> vmake(int a,int b,int c,int d,int e,T x){ return vvvvvc<T> (a,vvvvc<T>(b,vvvc<T>(c,vvc<T>(d,vc<T>(e,x))))); } //1ビットの数を返す inline int popcount(int x){ return __builtin_popcount(x); } //1ビットの数を返す inline int popcount(ll x){ return __builtin_popcountll(x); } //queのfront() pop() template<typename T> inline T pop(queue<T> &que){ assert(!que.empty()); T x=que.front(); que.pop(); return x; } //priority_que top() pop() template<typename T> inline T pop(priority_queue<T> &que){ assert(!que.empty()); T x=que.top(); que.pop(); return x; } //stack top() pop() template<typename T> inline T pop(stack<T> &st){ assert(!st.empty()); T x=st.top(); st.pop(); return x; } #define SZ(x) ( (int)x.size() ) #define pb push_back #define eb emplace_back /* 満たすものの個数を返す mode:0 x未満 mode:1 x以下の数 mode:2 x以上の数 mode:3 x超 mode:4 x */ template<typename T> inline int count_bound(vector<T> &v,T x,int mode){ switch(mode){ case 0: return lower_bound(v.begin(),v.end(),x)-v.begin(); case 1: return upper_bound(v.begin(),v.end(),x)-v.begin(); case 2: return v.end()-lower_bound(v.begin(),v.end(),x); case 3: return v.end()-upper_bound(v.begin(),v.end(),x); case 4: return upper_bound(v.begin(),v.end(),x)-lower_bound(v.begin(),v.end(),x); } } /* mode:0 xより小さい数で最大の数 mode:1 x以下の数で最大の数 mode:2 x以上の数で最小の数 mode:3 xより大きい数で最小の数 */ template<typename T> inline T value_bound(vector<T> &v,T x,int mode){ switch(mode){ case 0: return *(--lower_bound(v.begin(),v.end(),x)); case 1: return *(--upper_bound(v.begin(),v.end(),x)); case 2: return *lower_bound(v.begin(),v.end(),x); case 3: return *upper_bound(v.begin(),v.end(),x); } } constexpr int MAX=1<<30; constexpr ll INF=1LL<<62; constexpr ll MOD=1e9+7; constexpr int dx[]={1,0},dy[]={0,1}; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ int main(){ bin101(); int H,W; cin>>H>>W; auto a=vmake(H,W,0); vin0(a); //gvout(a); auto dp=vmake(H+W-1,H,H,-1); dp[0][0][0]=a[0][0]; for(int i=0;i<H+W-1;i++){ for(int s=0;s<H;s++){ for(int e=0;e<H;e++){ if(dp[i][s][e]==-1) continue; int sh=s,eh=e; int sw=i-sh,ew=i-eh; for(int k=0;k<2;k++){ for(int l=0;l<2;l++){ int snh=sh+dx[k],snw=sw+dy[k]; int enh=eh+dx[l],enw=ew+dy[l]; if(snh>=H || snw>=W || enh>=H || enw>=W) continue; int x; if(snh==enh) x=a[snh][snw]; else x=a[snh][snw]+a[enh][enw]; chmax(dp[i+1][snh][enh],dp[i][sh][eh]+x); } } } } } //vout(dp); cout<<dp[H+W-2][H-1][H-1]<<endl; }
0
#include <bits/stdc++.h> using namespace std; long double pi = 2.0 * acos(0.0); mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); long long int Dp[1005][1005] = {0}; long long int A[1000005]; long long int x, n; int fun(int ind, int sum) { if (sum == x) { return 1; } sum %= x; if (ind == n) { return 0; } if (Dp[ind][sum] != -1) { return Dp[ind][sum]; } int a, b; a = fun(ind + 1, sum + A[ind]); b = fun(ind + 1, sum); return Dp[ind][sum] = a + b; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int a, b, c, i, j, k, first, r, y, z; long long int m, p, q, t; memset(Dp, -1, sizeof(Dp)); first = 0; r = 0; cin >> n >> x; for (i = 0; i < n; i++) { cin >> A[i]; A[i] %= x; } for (i = 0; i < n; i++) if (A[i] == 0) first = 1; if (n > x + 3 || first == 1) { cout << "YES\n"; } else { r = fun(0, 0); if (r == 0) { cout << "NO\n"; } else { cout << "YES\n"; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<int> Zalgo(string S){ int n=S.size(), i=1, w=0, k; vector<int> Z(n); Z[0]=n; while(i<n){ while(i+w<n && S[w]==S[i+w]){ w++; } Z[i]=w; if(w==0){ i++; continue; } for(k=1; i+k<n && k+Z[k]<w; k++){ Z[i+k]=Z[k]; } i+=k; w-=k; } return Z; } int main(){ int N, ans=0; cin >> N; string S; cin >> S; for(int i=0; i<N-1; i++){ vector<int> Z(N-i); Z=Zalgo(S.substr(i)); for(int j=1; j<N-i; j++){ if(j>=Z[j]){ ans=max(ans, Z[j]); } } } cout << ans << "\n"; }
0
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") using namespace std; const int N = 110002; const int Lg = 16; int n, a[N], m; int l[Lg][N * 3], r[Lg][N * 3]; int tmin[Lg][N * 3 * 4], tmax[Lg][N * 3 * 4]; int x, y; void read(int &x) { char ch = getchar(); x = 0; for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 3) + (x << 1) + ch - '0'; } inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline void _read(int &sum) { char ch = nc(); sum = 0; while (!(ch >= '0' && ch <= '9')) ch = nc(); while (ch >= '0' && ch <= '9') sum = sum * 10 + ch - 48, ch = nc(); } inline int Min(int a, int b) { return a < b ? a : b; } inline int Max(int a, int b) { return a > b ? a : b; } int build(int t[], int a[], int node, int L, int r, int (*M)(int, int)) { int mid = (L + r) >> 1; return L == r ? t[node] = a[L] : t[node] = M(build(t, a, (node << 1), L, mid, M), build(t, a, (node << 1 | 1), mid + 1, r, M)); } int query(int t[], int node, int L, int r, int (*M)(int, int)) { if (L >= x && r <= y) return t[node]; int mid = (L + r) >> 1, ans = -1, v; if (x <= mid) { v = query(t, (node << 1), L, mid, M); ans = (ans == -1 ? v : M(ans, v)); }; if (y > mid) { v = query(t, (node << 1 | 1), mid + 1, r, M); ans = (ans == -1 ? v : M(ans, v)); }; return ans; } int main() { scanf("%d", &n); m = n * 3; for (int i = (0); i < (n); i++) scanf("%d", &a[i]); for (int i = (0); i < (m); i++) l[0][i] = max(i - a[i % n], 0), r[0][i] = min(i + a[i % n], m - 1); build(tmin[0], l[0], 1, 0, m - 1, Min); build(tmax[0], r[0], 1, 0, m - 1, Max); for (int i = (1); i < (Lg); i++) { for (int j = (0); j < (m); j++) { x = l[i - 1][j], y = r[i - 1][j]; l[i][j] = query(tmin[i - 1], 1, 0, m - 1, Min); r[i][j] = query(tmax[i - 1], 1, 0, m - 1, Max); } build(tmin[i], l[i], 1, 0, m - 1, Min); build(tmax[i], r[i], 1, 0, m - 1, Max); } for (int i = (n); i < (n << 1); i++) { int L = i, R = i, ans = 0; for (int j = (Lg - 1); j >= (0); j--) { x = L, y = R; int LL = query(tmin[j], 1, 0, m - 1, Min), RR = query(tmax[j], 1, 0, m - 1, Max); if (RR - LL + 1 < n) { ans += 1 << j; L = LL; R = RR; if (R - L + 1 == n - 1) break; } } if (n == 1) ans = -1; printf("%d ", ans + 1); } puts(""); return 0; }
4
#include <bits/stdc++.h> using namespace std; int Matrix[1002][2002]; int max(int a, int b) { if (a > b) return a; else return b; } int min(int a, int b) { if (a < b) return a; else return b; } int distance(int a, int b, int c) { int first, second; first = a < b ? b - a : a - b; second = b < c ? c - b : b - c; return first + second; } int main() { int n, k, p; int person[1005]; int key[2002]; cin >> n >> k >> p; for (int i = 1; i <= n; i++) cin >> person[i]; for (int i = 1; i <= k; i++) cin >> key[i]; sort(person + 1, person + n + 1); sort(key + 1, key + k + 1); for (int i = 1; i <= n; i++) { for (int j = i; j <= k; j++) { if (i == j) { Matrix[i][j] = max(Matrix[i - 1][j - 1], distance(person[i], key[i], p)); } else { Matrix[i][j] = min(Matrix[i][j - 1], max(Matrix[i - 1][j - 1], distance(person[i], key[j], p))); } } } int ans = 2e9; for (int i = n; i <= k; i++) ans = min(ans, Matrix[n][i]); cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > adj[100005]; priority_queue<pair<long long, long long> > pq; stack<long long> q; long long dist[100005], a, b, w, n, m, bef[100005]; bool processed[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a >> b >> w; adj[a].push_back(make_pair(w, b)); adj[b].push_back(make_pair(w, a)); } for (int i = 1; i <= n; i++) dist[i] = 1e18; memset(processed, 0, sizeof processed); memset(bef, -1, sizeof bef); dist[1] = 0; bef[1] = 0; pq.push(make_pair(0, 1)); while (!pq.empty()) { long long a = pq.top().second; pq.pop(); if (processed[a]) { continue; } processed[a] = true; for (auto u : adj[a]) { if (dist[u.second] > dist[a] + u.first) { dist[u.second] = dist[a] + u.first; bef[u.second] = a; pq.push(make_pair(-dist[u.second], u.second)); } } } if (bef[n] == -1) { cout << "-1" << '\n'; return 0; } long long x = n; bef[1] = 0; while (x != 0) { q.push(x); if (bef[x] == -1) { cout << "-1" << '\n'; return 0; } x = bef[x]; } long long qs = q.size(); cout << q.top(); q.pop(); for (int i = 2; i <= qs; i++) { cout << " " << q.top(); q.pop(); } cout << '\n'; }
3
#include <bits/stdc++.h> using namespace std; const int N = 200000; const int INF = 10000000; void setadd(set<pair<int, int> > &s, long long &S, int j, int i, int d) { set<pair<int, int> >::iterator it = s.upper_bound(make_pair(i, INF)); it--; bool F = 0; while (it != s.end() && it->second < d) { set<pair<int, int> >::iterator nx = it; nx++; S += ((nx != s.end() ? nx->first : j) - max(it->first, i)) * (d - it->second); if (it->first >= i) { s.erase(it); } it = nx; F = 1; } if (F) { s.insert(make_pair(i, d)); } } int main() { int n; scanf("%d", &n); static int a[N]; for (int i = 0; i < n; i++) { scanf("%d", a + i); } static int C[N + 1] = {0}, m1[N + 1], m2[N + 1], M1[N + 1], M2[N + 1]; for (int i = 1; i <= N; i++) { m1[i] = m2[i] = n; M1[i] = M2[i] = -1; } for (int i = 0; i < n; i++) { int j = a[i]; C[j]++; if (m1[j] == n) { m1[j] = i; } else if (m2[j] == n) { m2[j] = i; } M2[j] = M1[j]; M1[j] = i; } static int A[N] = {0}, B[N + 1] = {0}; static vector<pair<int, int> > V[N]; for (int d = 1; d <= N; d++) { int x1 = n, x2 = n, y1 = -1, y2 = -1; for (int j = d; j <= N; j += d) { if (x1 > m1[j]) { x2 = x1; x1 = m1[j]; } else if (x2 > m1[j]) { x2 = m1[j]; } if (x2 > m2[j]) { x2 = m2[j]; } if (y1 < M1[j]) { y2 = y1; y1 = M1[j]; } else if (y2 < M1[j]) { y2 = M1[j]; } if (y2 < M2[j]) { y2 = M2[j]; } } if (x2 + 1 < n) { B[x2 + 1] = max(B[x2 + 1], d); } if (y2 >= 0) { A[y2] = max(A[y2], d); } if (x1 + 1 < y1) { V[y1].push_back(make_pair(d, x1)); } } set<pair<int, int> > s; s.insert(make_pair(0, 0)); int D2 = 0; long long S = 0; for (int i = 0; i < n; i++) { if (D2 < B[i]) { D2 = B[i]; s.insert(make_pair(i, D2)); } S += (--s.end())->second; } long long ans = S; int D = 0; for (int j = n - 1; j > 0; j--) { S -= (--s.end())->second; if ((--s.end())->first == j) { s.erase(--s.end()); } D = max(D, A[j]); setadd(s, S, j, 0, D); for (int t = 0; t < V[j].size(); t++) { setadd(s, S, j, V[j][t].second + 1, V[j][t].first); } ans += S; } printf("%lld\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; bool ban = false; for (int i = 0; i < n; i++) { string s; cin >> s; int a, b; cin >> a >> b; if (a >= 2400 && b > a) ban = true; } if (ban) cout << "YES\n"; else cout << "NO\n"; return 0; }
1
#include <bits/stdc++.h> const long long maxn = 2e5 + 10; bool vis[maxn]; int pos; long long a[maxn], s[maxn], num[1000005], ans, cnt; void qprim(long long n) { long long i; pos = 0; for (i = 2; i <= n / i; i++) { if (n % i == 0) { s[pos++] = i; while (n % i == 0) n /= i; } } if (n != 1) s[pos++] = n; } long long add() { long long i, j, k, ret = 0; for (i = 1; i < (1 << pos); i++) { long long sum = 1, c = 0; for (j = 0; j < pos; j++) { if ((1 << j) & i) sum *= s[j], c++; } if (c % 2) ret += num[sum]; else ret -= num[sum]; num[sum]++; } ans += (cnt - ret); cnt++; return ans; } long long plus() { long long i, j, k, ret = 0; for (i = 1; i < (1 << pos); i++) { long long sum = 1, c = 0; for (j = 0; j < pos; j++) { if ((1 << j) & i) sum *= s[j], c++; } num[sum]--; if (c % 2) ret += num[sum]; else ret -= num[sum]; } cnt--; ans -= (cnt - ret); return ans; } int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); while (q--) { long long x; scanf("%I64d", &x); qprim(a[x]); if (vis[x]) { vis[x] = 0; printf("%I64d\n", plus()); } else { vis[x] = 1; printf("%I64d\n", add()); } } }
5
#include <bits/stdc++.h> using namespace std; int main() { double n; cin >> n; while (n--) { double ele; cin >> ele; if (ele < 4 && ele > 0) cout << "N\n"; else { double a = (ele + sqrt((ele * ele) - (4 * ele))) / 2; double b = (ele - sqrt((ele * ele) - (4 * ele))) / 2; double res = max(a, b); cout << setprecision(19) << "Y" << " " << ele - res << " " << res << endl; } } }
3
#include <vector> #include <list> #include <map> #include <set> #include <stack> #include <queue> #include <deque> #include <algorithm> #include <utility> #include <functional> #include <sstream> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <climits> #include <cassert> using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();} typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define EXIST(s,e) ((s).find(e)!=(s).end()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i) const double EPS = 1e-10; const double PI = acos(-1.0); struct tag{ string name; int x1,y1,x2,y2; vector<tag> children; tag *getClickedTag(int x,int y){ REP(i,children.size()){ tag *t=children[i].getClickedTag(x,y); if(t!=NULL){ return t; } } if(x1<=x&&x<=x2&&y1<=y&&y<=y2){ return this; }else{ return NULL; } } }; tag read(string &s, int &pos){ tag me; //タグ構造 ::= 開始タグ タグ値 {タグ構造}* 終了タグ //開始タグ ::= <タグ名> assert(s[pos]=='<'); pos++; while(s[pos]!='>'){ me.name.push_back(s[pos]); pos++; } assert(s[pos]=='>'); pos++; //タグ値 ::= 整数,整数,整数,整数 REP(i,4){ string num; while(isdigit(s[pos])){ num.push_back(s[pos]); pos++; } switch(i){ case 0:me.x1=toInt(num);break; case 1:me.y1=toInt(num);break; case 2:me.x2=toInt(num);break; case 3:me.y2=toInt(num);break; } assert(s[pos]==','||s[pos]=='<'); pos++; } pos--; while(s[pos+1]!='/'){ tag t=read(s,pos); me.children.push_back(t); } //終了タグ ::= </タグ名> assert(s[pos]=='<'); pos++; assert(s[pos]=='/'); pos++; while(s[pos]!='>'){ pos++; } assert(s[pos]=='>'); pos++; return me; } int main(){ int n; while(cin>>n,n){ string s; cin>>s; int pos=0; tag root=read(s,pos); REP(i,n){ int x,y; cin>>x>>y; tag *t=root.getClickedTag(x,y); if(t==NULL){ cout<<"OUT OF MAIN PANEL 1"<<endl; }else{ cout<<t->name<<" "<<t->children.size()<<endl; } } } }
0
#include <bits/stdc++.h> using namespace std; int n, X, dp[301][301][301]; string s; int main(){ cin>>s>>X;n=s.size(); for (int i=0; i<n; i++) for (int x=0; x<=X; x++)dp[x][i][i]=1; for (int x=0; x<=X; x++) for (int k=1; k<n; k++) for (int l=0, r=k; l+k<n; l++, r=l+k) dp[x][l][r]=max(max(dp[x][l][r-1], dp[x][l+1][r]), dp[max(x-min(1,abs(s[l]-s[r])), 0)][l+1][r-1]+ 2*min(1, (min(1, x)+(1-min(1,abs(s[l]-s[r])))))); cout<<dp[X][0][n-1]<<endl; return 0; }
0
#include <iostream> #include <iomanip> using namespace std; int main() { int candy, no; while (cin >> candy) { no = (candy % 39 != 0) ? candy % 39 : 39; cout << "3C"; cout << setw(2) << setfill('0') << no << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> bool get_int(T &x) { char t = getchar(); bool neg = false; x = 0; for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar()) ; if (t == '-') neg = true, t = getchar(); if (t == EOF) return false; for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0'; if (neg) x = -x; return true; } template <typename T> void print_int(T x) { if (x < 0) putchar('-'), x = -x; short a[20] = {}, sz = 0; while (x > 0) a[sz++] = x % 10, x /= 10; if (sz == 0) putchar('0'); for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]); } const int inf = 0x3f3f3f3f; const long long Linf = 1ll << 61; const double pi = acos(-1.0); struct matrix { long long a[70][70]; void clear(int x = 0) { for (int i = 0; i < 70; i++) for (int j = 0; j < 70; j++) a[i][j] = Linf * ((i != j) || x == 0); } matrix(int x = 0) { clear(x); } }; matrix operator*(const matrix &a, const matrix &b) { matrix c; for (int i = 0; i < 70; i++) for (int j = 0; j < 70; j++) for (int k = 0; k < 70; k++) c.a[i][j] = min(c.a[i][j], a.a[i][k] + b.a[k][j]); return c; } matrix tpw[30]; int id[1 << 8], tot, v[70], n, k, q, x, w[10]; matrix getpw(int x) { matrix ret(1); for (int i = 0; i < 30; i++) if ((((x) >> (i)) & 1)) ret = ret * tpw[i]; return ret; } int main() { (get_int(x) && get_int(k)); (get_int(n) && get_int(q)); for (int i = 0; i < k; i++) get_int(w[i]); vector<pair<int, pair<int, int> > > vs; for (int i = 0, x, w; i < q; i++) { (get_int(x) && get_int(w)); for (int j = 1; j <= k; j++) if (x - j > 0 && x - j <= n - ::x) vs.push_back(make_pair(x - j, make_pair(j - 1, w))); } memset(id, -1, sizeof(id)); for (int i = 0; i < (1 << k); i++) if (__builtin_popcount(i) == x) { id[i] = tot; v[tot++] = i; } matrix &tran = tpw[0]; for (int i = 0; i < tot; i++) { if ((((v[i]) >> (0)) & 1)) { for (int j = 0; j < k; j++) if ((((v[i] >> 1) >> (j)) & 1) == 0) { tran.a[i][id[(v[i] >> 1) | (1 << j)]] = w[j]; } } else tran.a[i][id[v[i] >> 1]] = 0; } for (int i = 1; i < 30; i++) tpw[i] = tpw[i - 1] * tpw[i - 1]; sort(vs.begin(), vs.end()); int last = 0; matrix all(1); for (int i = 0, nxt = 0; i < (int)vs.size(); i = nxt) { matrix now = tran; for (; nxt < (int)vs.size() && vs[nxt].first == vs[i].first; nxt++) { for (int j = 0; j < tot; j++) if ((((v[j]) >> (0)) & 1) && (((v[j] >> 1) >> (vs[nxt].second.first)) & 1) == 0) now.a[j][id[(v[j] >> 1) | (1 << vs[nxt].second.first)]] += vs[nxt].second.second; } all = all * getpw(vs[i].first - last - 1); all = all * now; last = vs[i].first; } all = all * getpw(n - x - last); printf("%I64d\n", all.a[id[(1 << x) - 1]][id[(1 << x) - 1]]); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; char S[1001]; cin >> t; int len; while (t--) { cin >> S; len = 0; for (int i = 0; S[i]; i++) { if (!S[i + 1]) { len = i + 1; } } if (len - 2 >= 0 && S[len - 1] == 'o' && S[len - 2] == 'p') { cout << "FILIPINO"; } else if (len - 4 >= 0 && (S[len - 4] == 'd' && S[len - 3] == 'e' && S[len - 2] == 's' && S[len - 1] == 'u') || (S[len - 4] == 'm' && S[len - 3] == 'a' && S[len - 2] == 's' && S[len - 1] == 'u')) { cout << "JAPANESE"; } else { cout << "KOREAN"; } cout << "\n"; } }
1
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 2e5 + 10; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n + 1]; map<long long, long long> f, make_pair; for (long long i = 0; i < n; i++) { cin >> a[i]; make_pair[a[i]] = i; } if (n == 1) { cout << a[0] << endl; continue; } long long k = n; for (long long i = 1; i <= n; i++) { for (long long j = make_pair[i]; j >= 1; j--) { if (f[j - 1] == 0 && a[j] < a[j - 1]) { swap(a[j], a[j - 1]); swap(make_pair[a[j]], make_pair[a[j - 1]]); f[j - 1] = 1; } } } for (long long i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; string DBG_DLM(int& i) { return (i++ == 0 ? "" : ", "); } template <class T> ostream& operator<<(ostream& os, vector<T> v); template <class T> ostream& operator<<(ostream& os, set<T> v); template <class T> ostream& operator<<(ostream& os, queue<T> q); template <class T> ostream& operator<<(ostream& os, priority_queue<T> q); template <class T, class K> ostream& operator<<(ostream& os, pair<T, K> p); template <class T, class K> ostream& operator<<(ostream& os, map<T, K> mp); template <class T, class K> ostream& operator<<(ostream& os, unordered_map<T, K> mp); template <int I, class TPL> void DBG(ostream& os, TPL t) {} template <int I, class TPL, class H, class... Ts> void DBG(ostream& os, TPL t) { os << (I == 0 ? "" : ", ") << get<I>(t); DBG<I + 1, TPL, Ts...>(os, t); } template <class T, class K> void DBG(ostream& os, pair<T, K> p, string delim) { os << "(" << p.first << delim << p.second << ")"; } template <class... Ts> ostream& operator<<(ostream& os, tuple<Ts...> t) { os << "("; DBG<0, tuple<Ts...>, Ts...>(os, t); os << ")"; return os; } template <class T, class K> ostream& operator<<(ostream& os, pair<T, K> p) { DBG(os, p, ", "); return os; } template <class T> ostream& operator<<(ostream& os, vector<T> v) { { int i = 0; os << "{"; { for (auto&& t : v) { os << DBG_DLM(i) << t; }; } os << "}"; return os; }; } template <class T> ostream& operator<<(ostream& os, set<T> s) { { int i = 0; os << "{"; { for (auto&& t : s) { os << DBG_DLM(i) << t; }; } os << "}"; return os; }; } template <class T> ostream& operator<<(ostream& os, queue<T> q) { { int i = 0; os << "{"; { for (; q.size(); q.pop()) { os << DBG_DLM(i) << q.front(); }; } os << "}"; return os; }; } template <class T> ostream& operator<<(ostream& os, priority_queue<T> q) { { int i = 0; os << "{"; { for (; q.size(); q.pop()) { os << DBG_DLM(i) << q.top(); }; } os << "}"; return os; }; } template <class T, class K> ostream& operator<<(ostream& os, map<T, K> m) { { int i = 0; os << "{"; { for (auto&& p : m) { os << DBG_DLM(i); DBG(os, p, "->"); }; } os << "}"; return os; }; } template <class T, class K> ostream& operator<<(ostream& os, unordered_map<T, K> m) { { int i = 0; os << "{"; { for (auto&& p : m) { os << DBG_DLM(i); DBG(os, p, "->"); }; } os << "}"; return os; }; } using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>; using vs = vector<string>; using vvs = vector<vs>; using vvvs = vector<vvs>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vvvpii = vector<vvpii>; template <class A, class B> bool amax(A& a, const B& b) { return b > a ? a = b, 1 : 0; } template <class A, class B> bool amin(A& a, const B& b) { return b < a ? a = b, 1 : 0; } ll ri() { ll l; cin >> l; return l; } string rs() { string s; cin >> s; return s; } template <class T, int N> struct matrix_ { T val[N][N]; matrix_(vector<vector<T>> ini) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) val[i][j] = ini[i][j]; } matrix_(int d = 0) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) val[i][j] = T((i == j) * d); } T& at(int row, int col) { return val[row][col]; } const T& at(int row, int col) const { return val[row][col]; } }; template <class T, int N> ostream& operator<<(ostream& os, const matrix_<T, N>& mat) { vector<vector<T>> vec(N, vector<T>(N)); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) vec[i][j] = mat.at(i, j); os << vec; return os; } using Mat = matrix_<int, 5>; const int inf = 268435456; const Mat MAX({{inf, inf, inf, inf, inf}, {inf, inf, inf, inf, inf}, {inf, inf, inf, inf, inf}, {inf, inf, inf, inf, inf}, {inf, inf, inf, inf, inf}}); const Mat UNIT({{0, inf, inf, inf, inf}, {inf, 0, inf, inf, inf}, {inf, inf, 0, inf, inf}, {inf, inf, inf, 0, inf}, {inf, inf, inf, inf, 0}}); static const Mat def = UNIT; struct SegTree0 { using V = Mat; const int n; vector<V> val; SegTree0(int _n) : n(p2(_n)), val(n * 2, def) {} V query(int a, int b) { return query(a, b, 0, 0, n); } void update(int i, V v) { i += n - 1; val[i] = v; while (i > 0) { i = (i - 1) / 2; val[i] = merge(val[i * 2 + 1], val[i * 2 + 2]); } } private: V query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return def; if (a <= l && r <= b) return val[k]; return merge(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } static int p2(int n, int m = 1) { return m >= n ? m : p2(n, m * 2); } static V merge(const V& l, const V& r) { Mat ret = MAX; for (int i = int(0); i < int(5); ++i) for (int k = int(i); k < int(5); ++k) for (int j = int(k); j < int(5); ++j) { ret.val[i][j] = min(ret.val[i][j], l.val[i][k] + r.val[k][j]); } return ret; } }; template <class V, class Merge> struct SegmentTree { const int n; const V unit_value; vector<V> val; SegmentTree(int _n) : n(1 << (33 - __builtin_clz(_n - 1))), unit_value(Merge::unit()), val(n, unit_value) {} V get(int i) const { return val[i + n / 2]; } void set(int i, const V& v) { val[i + n / 2] = v; } void build() { for (int i = n / 2 - 1; i > 0; i--) val[i] = Merge::merge(val[i * 2 + 0], val[i * 2 + 1]); } void update(int i, const V& v) { i += n / 2; val[i] = v; while (i > 1) { i >>= 1; val[i] = Merge::merge(val[i * 2 + 0], val[i * 2 + 1]); } } V query(int l, int r) const { l = max(0, min(n / 2, l)) + n / 2; r = max(0, min(n / 2, r)) + n / 2; V ret = unit_value; for (; l < r; l >>= 1, r >>= 1) { if (l & 1) ret = Merge::merge(ret, val[l++]); if (r & 1) ret = Merge::merge(ret, val[--r]); } return ret; } }; template <class V, class Merge> ostream& operator<<(ostream& os, const SegmentTree<V, Merge>& seg) { vector<V> vec; for (int i = 0; i < seg.n; i++) { vec.emplace_back(seg.get(i)); } os << vec; return os; } struct Merge { static Mat merge(const Mat& l, const Mat& r) { Mat ret = MAX; for (int i = int(0); i < int(5); ++i) for (int k = int(i); k < int(5); ++k) for (int j = int(k); j < int(5); ++j) { ret.val[i][j] = min(ret.val[i][j], l.val[i][k] + r.val[k][j]); } return ret; } static Mat unit() { return UNIT; } }; using SegTree = SegmentTree<Mat, Merge>; void Main() { int n = ri(), q = ri(); auto s = rs(); SegTree0 seg(n); for (int i = int(0); i < int(n); ++i) { const int d = s[i] - '0'; Mat mat(UNIT); if (d == 2) { mat.at(0, 0) = 1; mat.at(0, 1) = 0; } else if (d == 0) { mat.at(1, 1) = 1; mat.at(1, 2) = 0; } else if (d == 1) { mat.at(2, 2) = 1; mat.at(2, 3) = 0; } else if (d == 7) { mat.at(3, 4) = 0; } else if (d == 6) { mat.at(3, 3) = 1; mat.at(4, 4) = 1; } seg.update(i, mat); } for (int i = int(0); i < int(n); ++i) { } for (int _ = int(0); _ < int(q); ++_) { int l = ri() - 1, r = ri() - 1 + 1; Mat ret = seg.query(l, r); int ans = ret.at(0, 4); if (ans == inf) cout << "-1\n"; else cout << ans << "\n"; } } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }
5
#include <bits/stdc++.h> using namespace std; struct rec { int x, t; } a[100010]; int c[100010], b, n, i, j, m, v; bool operator<(rec a, rec b) { return a.t * v - a.x < b.t * v - b.x || a.t * v - a.x == b.t * v - b.x && a.t * v + a.x < b.t * v + b.x; } int main() { ios::sync_with_stdio(false); cin >> n; for (i = 1; i <= n; i++) cin >> a[i].x >> a[i].t; a[++n] = (rec){0, 0}; cin >> v; sort(a + 1, a + n + 1); for (i = n; i; i--) { b = -a[i].t * v - a[i].x; j = upper_bound(c + 1, c + m + 1, b) - c; if (!a[i].t && !a[i].x) cout << j - 1 << ' '; else c[j] = b, m = max(m, j); } cout << m << endl; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { string s; cin >> s; int t = s.size(); int as = 0; int bs = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'A') { as++; } else { bs++; if (as) { as--; bs--; } } } cout << (as + (bs % 2)) << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long double PI = acos(-1); const long long MAX = 1e6 + 10; const long long INF = 1e9 + 10; const long double EPS = 1e-9; const int M = 1e2 + 12; const int N = 5e5 + 10; int a[N], d[N]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (long long i = 0; i < n; ++i) cin >> a[i] >> d[i]; int ans1 = 0; for (long long i = 0; i < n; ++i) for (long long j = i + 1; j < n; ++j) { int ans = 0; pair<int, int> a1 = make_pair(a[i], -i), a2 = make_pair(a[i] + d[i], -i); pair<int, int> b1 = make_pair(a[j], -j), b2 = make_pair(a[j] + d[j], -j); int t = 0; if (a1 < b1 && b1 < a2 && a2 < b2) ans += 2; else if (b1 < a1 && a1 < b2 && b2 < a2) ans += 2; else if (a1 > b1 && b1 > a2 && a2 > b2) ans += 2; else if (b1 > a1 && a1 > b2 && b2 > a2) ans += 2; else if (min(a1, a2) < min(b1, b2) && max(b1, b2) < max(a1, a2)) ans++; else if (min(b1, b2) < min(a1, a2) && max(a1, a2) < max(b1, b2)) ans++; else if (b2 < a1 && a1 < b1 && b1 < a2) ans++; else if (a2 < b1 && b1 < a1 && a1 < b2) ans++; else if (a1 < b2 && b2 < a2 && a2 < b1) ans++; else if (b1 < a2 && a2 < b2 && b2 < a1) ans++; ans1 += ans; } cout << ans1; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t = 0; cin >> t; for (int i = 0; i < t; ++i) { int n = 0; cin >> n; int k = 2; while (true) { if (n % ((int)pow(2, k) - 1) == 0) { cout << n / ((int)pow(2, k) - 1) << "\n"; break; } else { ++k; } } } return 0; }
1
#include <algorithm> #include <deque> #include <cstdio> using namespace std; int main() { double i; deque<double> n; while(scanf("%lf", &i)!=EOF) n.push_back(i); sort(n.begin(), n.end()); printf("%.1f\n", *n.rbegin()-*n.begin()); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a >> b; int n; cin >> n; cout << a << " " << b << endl; for (int i = 0; i < n; i++) { string c, d; cin >> c >> d; if (c == a) a = d; else b = d; cout << a << " " << b << endl; } }
1
#include <bits/stdc++.h> using namespace std; int n, depth, pos[111]; char out[5000][5000]; string s, ans; int main() { cin >> n >> s; for (int i = 0; i < n; ++i) { if (s[i] == '[') { pos[i] = ++depth; } else { pos[i] = depth--; } } depth = 0; int curr = 0; int h = *max_element(pos, pos + n); for (int i = 0; i < n; ++i, ++curr) { if (i && s[i] == ']' && s[i - 1] == '[') { curr += 3; } int width = 2 * (h - pos[i]) + 1; depth = max(depth, pos[i] + width); if (s[i] == '[') { out[pos[i] - 1][curr + 1] = '-'; out[pos[i] + width][curr + 1] = '-'; } else { out[pos[i] - 1][curr - 1] = '-'; out[pos[i] + width][curr - 1] = '-'; } out[pos[i] - 1][curr] = out[pos[i] + width][curr] = '+'; for (int j = pos[i]; j < pos[i] + width; ++j) { out[j][curr] = '|'; } } for (int i = 0; i <= depth; ++i) { ans.clear(); for (int j = 0; j < curr; ++j) { ans += out[i][j] ? out[i][j] : ' '; } cout << ans << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct Treap { long long x, y; long long add_x, add_y; int height; Treap *left, *right, *parent; Treap(long long x, long long y, Treap *parent = 0) : x(x), y(y), parent(parent) { height = rand(); add_x = add_y = 0; left = right = 0; } void push() { if (!add_x && !add_y) { return; } if (left) { left->x += add_x; left->add_x += add_x; left->y += add_y; left->add_y += add_y; } if (right) { right->x += add_x; right->add_x += add_x; right->y += add_y; right->add_y += add_y; } add_x = add_y = 0; } void recalc() {} }; Treap *merge(Treap *x, Treap *y) { if (!x) return y; if (!y) return x; if (x->height < y->height) { x->push(); x->right = merge(x->right, y); if (x->right) x->right->parent = x; x->recalc(); return x; } else { y->push(); y->left = merge(x, y->left); if (y->left) y->left->parent = y; y->recalc(); return y; } } Treap *get_left(Treap *x) { if (!x) { return x; } if (!x->left) { return x; } x->push(); return get_left(x->left); } Treap *get_right(Treap *x) { if (!x) { return x; } if (!x->right) { return x; } x->push(); return get_right(x->right); } Treap *remove_left(Treap *x) { x->push(); if (x->left) { x->left = remove_left(x->left); return x; } if (x->right) { Treap *z = get_left(x->right); Treap *zz = new Treap(z->x, z->y); zz->right = remove_left(x->right); if (zz->right) { zz->right->parent = zz; } zz->parent = x->parent; return zz; } if (x->parent) { if (x->parent->left == x) { x->parent->left = 0; } else { x->parent->right = 0; } } return 0; } Treap *remove_right(Treap *x) { x->push(); if (x->right) { x->right = remove_right(x->right); return x; } if (x->left) { Treap *z = get_right(x->left); Treap *zz = new Treap(z->x, z->y); zz->left = remove_right(x->left); if (zz->left) { zz->left->parent = zz; } zz->parent = x->parent; return zz; } if (x->parent) { if (x->parent->right == x) { x->parent->right = 0; } else { x->parent->left = 0; } } return 0; } void split(Treap *t, Treap *&l, Treap *&r, long long diff) { if (!t) { l = r = 0; return; } t->push(); if (t->x - t->y >= diff) { split(t->left, l, t->left, diff); if (t->left) t->left->parent = t; r = t; } else { split(t->right, t->right, r, diff); if (t->right) t->right->parent = t; l = t; } } void readll(long long &v) { char ch = getchar(); while (ch < '0' || ch > '9') { ch = getchar(); } v = 0; while ('0' <= ch && ch <= '9') { v = v * 10 + ch - '0'; ch = getchar(); } } const int N = 1234567; pair<long long, int> e[N]; int main() { int n, m; long long C; scanf("%d %d", &n, &m); readll(C); int cnt = 0; for (int i = 0; i < 2 * n; i++) { readll(e[cnt].first); e[cnt].second = 1; cnt++; } for (int i = 0; i < 2 * m; i++) { readll(e[cnt].first); e[cnt].second = 2; cnt++; } sort(e, e + cnt); Treap *r = new Treap(0, 0, 0); int mask = 0; long long big_add = 0; for (int i = 0; i < cnt - 1; i++) { mask ^= e[i].second; if (mask == 0) { continue; } long long t = e[i + 1].first - e[i].first; if (t == 0) { continue; } if (mask == 1) { r->x += t; r->add_x += t; } if (mask == 2) { r->y += t; r->add_y += t; } if (mask == 3) { big_add += t; Treap *t1 = new Treap(0, 0, 0), *t2 = new Treap(0, 0, 0), *t3 = new Treap(0, 0, 0); split(r, t1, t2, -C); split(t2, t2, t3, C + 1); if (t1) t1->parent = 0; if (t2) t2->parent = 0; if (t3) t3->parent = 0; long long x3 = -C, y3 = 0; long long x4 = 0, y4 = -C; if (t1) { Treap *tmp = get_right(t1); x3 = tmp->x; y3 = tmp->x + C; } if (t3) { Treap *tmp = get_left(t3); x4 = tmp->y + C; y4 = tmp->y; } if (x3 > x4 || y3 > y4) { if (!t2) { t2 = new Treap(x3, y3); } else { Treap *tmp = get_left(t2); if (x3 > tmp->x || y3 > tmp->y) { t2 = merge(new Treap(x3, y3), t2); } } } if (x4 > x3 || y4 > y3 || (x3 == x4 && y3 == y4)) { if (!t2) { t2 = new Treap(x4, y4); } else { Treap *tmp = get_right(t2); if (x4 > tmp->x || y4 > tmp->y) { t2 = merge(t2, new Treap(x4, y4)); } } } t2->x += t; t2->add_x += t; t2->y += t; t2->add_y += t; Treap *t2l = get_left(t2); Treap *t2r = get_right(t2); while (t1) { Treap *tmp = get_right(t1); if (tmp->x <= t2l->x && tmp->y <= t2l->y) { t1 = remove_right(t1); } else { break; } } while (t3) { Treap *tmp = get_left(t3); if (tmp->x <= t2r->x && tmp->y <= t2r->y) { t3 = remove_left(t3); } else { break; } } r = merge(t1, merge(t2, t3)); } } cout << (big_add + (get_right(r))->x) << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long n, m, a[111111], b[111111], calc, w[111111], ans[111111], d[111111], last, pr; char c[111111]; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } int main() { cin >> n >> m; for (long long i = 1; i <= m; i++) { scanf("\n%c%I64d", &c[i], &b[i]); if (!w[b[i]]) { if (c[i] == '-') a[b[i]] = 1; } w[b[i]] = 1; } for (long long i = 1; i <= n; i++) { calc += a[i]; if (w[i] == 0) ans[i] = 1; } if (calc > 0) for (long long i = 1; i <= n; i++) d[i] = a[i]; else for (long long i = 1; i <= n; i++) d[i] = 1; for (long long i = 1; i <= m; i++) { if (c[i] == '+') { if (calc == 0) { if (last == 0) { last = 1; for (long long j = 1; j <= n; j++) if (j != b[i]) d[j] = 0; pr = b[i]; } else if (last == 1) { if (b[i] != pr) d[pr] = 0; } } if (calc > 0) d[b[i]] = 0; calc++; } else { if (calc > 1) d[b[i]] = 0; calc--; } } for (long long i = 1; i <= n; i++) { if (d[i] == 1) ans[i] = 1; } long long res = 0; for (long long i = 1; i <= n; i++) res += ans[i]; cout << res << endl; for (long long i = 1; i <= n; i++) if (ans[i]) cout << i << " "; }
2