solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f; scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f); if (d == 0) puts("Hermione"); else if (c == 0) puts("Ron"); else if (b == 0) puts("Hermione"); else if (a == 0) puts("Ron"); else if (f == 0) puts("Hermione"); else if (e == 0) puts("Ron"); else puts(b * d * f > a * c * e ? "Ron" : "Hermione"); return 0; }
1
#include<bits/stdc++.h> using namespace std; int main(){ string s,t; cin>>s; cin>>t; string p = s + s; if (p.find(t) != std::string::npos) { std::cout << "Yes" << '\n'; } else{ cout<<"No"<<endl; } }
0
//If you can dream it, you can do it! //author@nowadays - 2021-07-13 11:01:41 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; vector<map<char, int>> a(m); for (int i = 0; i < n; ++i) { string s; cin >> s; for (int j = 0; j < m; ++j) { a[j][s[j]] ++; } } for (int i = 0; i < n - 1; ++i) { string s; cin >> s; for (int j = 0; j < m; ++j) { a[j][s[j]] --; } } string ans; for (int i = 0; i < m; ++i) { for (auto j: a[i]) { if (j.second > 0) { ans.push_back(j.first); break; } } } cout << ans << endl; } return 0; }
2
#include<iostream> using namespace std; int main() { int n,S; for(;cin>>n>>S,n;) { int power[n]; for(int i=0;i<n;i++) { cin>>power[i]; } int count=0; for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if(power[i]+power[j]>S) count++; } } cout<<count<<endl; } }
0
#include <bits/stdc++.h> int main() { int a; scanf("%d", &a), printf("%d\n", a & 1); }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; int n = t / 2; unsigned long long ans = 1, d; d = t * n; for (int i = 1; i <= t; i++) ans *= i; ans = ans / d; cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; bool visited[100]; int main() { long long r; cin >> r; for (long long i = 1; i * i < r; i++) { long long ec = (r - (i * i) - i - 1); if (ec % (2 * i) == 0 && ec > 0) { cout << i << " " << ec / (2 * i); return 0; } } cout << "NO"; }
1
#include <bits/stdc++.h> bool debug = 1; const double PI = acos(-1.0); const double eps = 1e-9; using namespace std; int ordem[30]; int v[30]; int v2[30]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; v2[i] = v[i]; } sort(v2, v2 + n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (v2[j] == v[i]) { ordem[i] = j; break; } } } int v3[30]; for (int i = 0; i < n; i++) { v3[i] = v2[(ordem[i] + 1) % n]; } for (int i = 0; i < n; i++) { printf("%d ", v3[i]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 100; const long long M = 1e6 + 10; struct node { long long id, l, r; node() { id = l = r = 0; }; node(long long L, long long R, long long I) { l = L; r = R; id = I; } } t[N * 4]; long long read() { long long ivy; scanf("%lld", &ivy); return ivy; } struct Tree { long long val[M * 4]; void add(long long x, long long l, long long r, long long p) { if (l == r) { val[x]++; return; } long long mid = l + r >> 1; if (p <= mid) add(x << 1, l, mid, p); else add(x << 1 | 1, mid + 1, r, p); val[x] = val[x << 1] + val[x << 1 | 1]; } long long ask(long long x, long long l, long long r, long long tl, long long tr) { if (l >= tl && r <= tr) return val[x]; long long mid = l + r >> 1, ans = 0; if (tl <= mid) ans += ask(x << 1, l, mid, tl, tr); if (tr > mid) ans += ask(x << 1 | 1, mid + 1, r, tl, tr); return ans; } } tree; long long n, q, A[N], cnt, ans[N]; bool cmp(node a, node b) { if (a.l != b.l) return a.l > b.l; if (a.r != b.r) return a.r < b.r; return a.id < b.id; } int main() { cin >> n >> q; cnt = n; for (long long l, r, i = 1; i <= n; i++) { l = read(); r = read(); t[i] = node(l, r, 0); } for (long long i = 1; i <= q; i++) { long long m = read(); for (long long j = 1; j <= m; j++) A[j] = read(); for (long long j = 1; j <= m; j++) if (A[j] - 1 > A[j - 1] && A[j] > 1) t[++cnt] = node(A[j - 1] + 1, A[j] - 1, i); if (A[m] < 1e6) t[++cnt] = node(A[m] + 1, 1e6, i); } sort(t + 1, t + 1 + cnt, cmp); for (long long i = 1; i <= cnt; i++) if (t[i].id) ans[t[i].id] += tree.ask(1, 1, 1e6, t[i].l, t[i].r); else tree.add(1, 1, 1e6, t[i].r); for (long long i = 1; i <= q; i++) cout << n - ans[i] << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<char>> a(n, vector<char>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> a[i][j]; } bool flag = false; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { bool allSame = true, no_intersection = true; for (int k = 0; k < m; k++) { if (a[i][k] != a[j][k]) allSame = false; if (a[i][k] == '#' && a[j][k] == '#') no_intersection = false; } if (!allSame && !no_intersection) { cout << "No" << endl; return 0; } } } cout << "Yes" << endl; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long ans = n; for (int k = n - 1; k > 0; k--) ans += (n + 1 - k) * k - (n - k); cout << ans << '\n'; }
2
#include <bits/stdc++.h> using namespace std; int x; int main() { scanf("%d", &x); printf("%d", x % 5 % 3 + 1); return 0; }
4
#include <bits/stdc++.h> using namespace std; bool hash[1000]; int n, m, last = -1; char k, f; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> k; if (last == k) { cout << "NO" << endl; return 0; } for (int j = 1; j < m; j++) { cin >> f; if (f != k) { cout << "NO" << endl; return 0; } } last = f; } cout << "YES" << endl; }
1
#include<iostream> using namespace std; int main(void) { int n; cin >> n; cout << (n<1200 ? "ABC" : (n<2800 ? "ARC" : "AGC")) << endl; }
0
#include<iostream> using namespace std; const int maxn = 3e5 + 50; int N, p[maxn]; bool check(int l, int r) { for (int i = l; i <= r; ++i) { if (p[i]<l || p[i]>r) { return false; } } int L = -1, R = -1; for (int i = l; i <= r; i += 2) { if (p[i] < i) { if (L > p[i]) return false; L = p[i]; } else { if (R > p[i]) return false; R = p[i]; } } return true; } bool sol() { for (int i = 2; i < N; ++i) { if (p[i - 1] != i - 1 && p[i] != i && p[i + 1] != i + 1) { return false; } } int l = 1; while (l <= N) { while (l <= N && p[l] == l) ++l; if (l > N) break; int r = l; while (r + 2 <= N && p[r + 1] == r + 1 && p[r + 2] != r + 2) r += 2; if (!check(l, r)) return false; l = r + 1; } return true; } int main() { scanf("%d", &N); for (int i = 1; i <= N; ++i) { scanf("%d", &p[i]); } puts(sol() ? "Yes" : "No"); return 0; }
0
#include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--) #define REP(i,n) for(int i=0;i<(n);i++) #define RREP(i,n) for(int i=n-1;i>=0;i--) #define PB push_back #define MP make_pair #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define CLR(a) memset(a,0,sizeof(a)) #define SET(a,c) memset(a,c,sizeof(a)) #define DEBUG(x) cout<<"#x"<<": "<<x<<endl using namespace std; typedef long long int ll; typedef vector<int> vi; typedef vector<ll> vl; const ll INF = INT_MAX/3; const ll MOD = 1000000007; const double EPS = 1e-14; const int dx[] = {1,0,-1,0} , dy[] = {0,1,0,-1}; typedef struct Btree { struct Btree *left; struct Btree *right; } Btree; Btree temp; Btree *createTree(string s){ // cout << "input: " << s << endl; int center = -1; stack< int > st; if( s.size() <= 1 ) { return NULL; } REP(i,s.length()){ if( s[i] == '(' ){ st.push(i); }else if ( s[i] == ')'){ st.pop(); }else if ( s[i] == ',' ){ if(st.size() == 1) center = i; } } if( center == -1) return NULL; Btree *ret = new Btree; ret->left = createTree(s.substr(1,center-1)); ret->right = createTree(s.substr(center+1,s.length())); return ret; } void dfs(int n,Btree *tree){ if( tree == NULL) return; REP(i,n) cout << " "; cout << n << endl; dfs(n+1,tree->left); dfs(n+1,tree->right); } string patternI(Btree *a,Btree *b ){ string left,right; left = ""; right = ""; if(a == NULL || b == NULL){ return ""; }else { left = patternI(a->left,b->left); right = patternI(a->right,b->right); } return "(" + left + "," + right + ")"; } string patternU(Btree *a,Btree *b ){ string left,right; left = ""; right = ""; if(a == NULL && b == NULL){ return ""; } else if(a == NULL && b != NULL) { left = patternU(NULL,b->left); right = patternU(NULL,b->right); }else if(a != NULL && b == NULL) { left = patternU(a->left,NULL); right = patternU(a->right,NULL); }else { left = patternU(a->left,b->left); right = patternU(a->right,b->right); } return "(" + left + "," + right + ")"; } int main(){ while(true){ char type; string t1,t2; cin >> type; if(cin.eof()) return 0; cin >> t1 >> t2; Btree *a = createTree(t1); Btree *b = createTree(t2); if( type == 'i'){ cout << patternI(a,b) << endl; }else if(type == 'u'){ cout << patternU(a,b) << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int a[maxn]; char S[maxn][10]; vector<int> F1, F2, F3, G1, G2, G3; bool p[maxn]; int f[maxn]; int main() { int n; scanf("%d", &n); int e = 0; for (int t = 1; t <= n; ++t) { scanf("%s%d", S[t], &a[t]); if (a[t]) ++e; int len = strlen(S[t]); f[t] = 0; if (S[t][0] != '0') for (int i = 0; i < len; ++i) { if (!(S[t][i] >= 'a' && S[t][i] <= 'z')) { f[t] = f[t] * 10 + S[t][i] - '0'; } else { f[t] = 0; break; } } if (f[t] > n) f[t] = 0; if (f[t] != 0) p[f[t]] = true; } for (int i = 1; i <= n; ++i) { if (a[i] == 0) { if (f[i] == 0) F1.push_back(i); else if (f[i] <= e) F2.push_back(i); } else { if (f[i] == 0) G1.push_back(i); else if (f[i] > e) G2.push_back(i); } } for (int i = 1; i <= e; ++i) if (!p[i]) F3.push_back(i); for (int i = e + 1; i <= n; ++i) if (!p[i]) G3.push_back(i); int ans = F1.size() + F2.size() + G1.size() + G2.size(); if (F3.empty() && G3.empty() && !(F1.empty() && F2.empty() && G1.empty() && G2.empty())) ans = ans + 1; printf("%d\n", ans); while (true) { if (F1.empty() && F2.empty() && G1.empty() && G2.empty()) break; if (!F3.empty() && (!G2.empty() || !G1.empty())) { if (!G2.empty()) { int x = G2[G2.size() - 1]; int y = F3[F3.size() - 1]; G2.pop_back(); F3.pop_back(); printf("move %s %d\n", S[x], y); G3.push_back(f[x]); } else { int x = G1[G1.size() - 1]; int y = F3[F3.size() - 1]; G1.pop_back(); F3.pop_back(); printf("move %s %d\n", S[x], y); } } else if (!G3.empty() && (!F2.empty() || !F1.empty())) { if (!F2.empty()) { int x = F2[F2.size() - 1]; int y = G3[G3.size() - 1]; F2.pop_back(); G3.pop_back(); printf("move %s %d\n", S[x], y); F3.push_back(f[x]); } else { int x = F1[F1.size() - 1]; int y = G3[G3.size() - 1]; F1.pop_back(); G3.pop_back(); printf("move %s %d\n", S[x], y); } } else { int x = F2[F2.size() - 1]; F2.pop_back(); F1.push_back(x); F3.push_back(f[x]); printf("move %s znfjgd\n", S[x]); S[x][0] = 'z'; S[x][1] = 'n'; S[x][2] = 'f'; S[x][3] = 'j'; S[x][4] = 'g'; S[x][5] = 'd'; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; unsigned long long mod = 1000000007; int main() { int commands, inp; cin >> commands; vector<pair<int, int> > values; for (int command = 1; command <= commands; command++) { cin >> inp; if (inp == 0) { vector<pair<int, int> > now = values; sort((values).begin(), (values).end()); reverse((values).begin(), (values).end()); while (int((values).size()) > 3) values.pop_back(); string todo[3] = {"pushStack", "pushQueue", "pushFront"}; int iamat = 0; for (int pos = 0; pos < int((now).size()); pos++) { bool match = false; for (int ind = 0; ind < int((values).size()); ind++) { if (now[pos].second == values[ind].second) match = true; } if (!match) cout << "pushBack\n"; else cout << todo[iamat++] << "\n"; } if (int((values).size()) == 3) cout << "3 popStack popQueue popFront\n"; else if (int((values).size()) == 2) cout << "2 popStack popQueue\n"; else if (int((values).size()) == 1) cout << "1 popStack\n"; else cout << "0\n"; values.clear(); } else values.push_back(make_pair(inp, command)); } if (!values.empty()) for (int pos = 0; pos < int((values).size()); pos++) cout << "pushBack\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n], x; for (x = 0; x < n; x++) cin >> a[x]; sort(a, a + n); for (x = n - 1; x >= 0; x--) cout << a[x] << " "; cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int prev = -1; int pprev = -1; int cnt = 0; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if ((prev == 3 && x == 2) || (prev == 2 && x == 3)) { printf("Infinite\n"); return 0; } if ((prev == 2 && x == 1)) cnt += 3; if (prev == 1 && x == 2) { if (pprev == 3) { cnt += 2; } else { cnt += 3; } } if ((prev == 3 && x == 1) || (prev == 1 && x == 3)) cnt += 4; pprev = prev; prev = x; } printf("Finite\n%d\n", cnt); return 0; }
1
#include <bits/stdc++.h> using namespace std; int n,m; bool yes[10]; int dp[10004],ans[10004],sz[10004]; int num[10]={0,2,5,5,4,5,6,3,7,6}; int main(){ memset (ans,-1,sizeof(ans)); cin>>n>>m; for (int i=0;i<m;i++){ int a;cin>>a; yes[a]=1; } ans[0]=0; for (int i=1;i<=n;i++) for (int j=9;j>=1;j--) if (i-num[j]>=0 && yes[j] && ans[i-num[j]]!=-1) if (sz[i-num[j]]+1>sz[i]){ sz[i]=sz[i-num[j]]+1; ans[i]=j; dp[i]=i-num[j]; } for (int i=n,j=sz[n];j>0;i=dp[i],j--){ cout<<ans[i]; } return 0; }
0
#include "iostream" #include "climits" #include "list" #include "queue" #include "vector" #include "string" #include "map" #include "unordered_map" #include "algorithm" #include "functional" #include "set" using namespace std; const long long int MOD = 1000000007; long long int N, M, K, H, W, L, R; struct Edge{ int to; long long int capa; long long int cost; unsigned long long int rev_index; }; class Min_Cost_Flow {//超頂点を忘れない int n; bool d; vector<vector<Edge>>edge; vector<long long int>potent; vector<long long int>dist; vector<int>pre_node; vector<int>pre_edge; public: Min_Cost_Flow(int node, bool directed) { node += 2; n = node; if (directed)d = true; else d = false; edge.resize(n); potent.resize(n); dist.resize(n); pre_node.resize(n); pre_edge.resize(n); return; } void Add_Edge(int s, int g, long long int cap, long long int cost) { if (!d) { edge[s].push_back({ g,cap,cost,edge[g].size() }); edge[g].push_back({ s,0,-cost ,edge[s].size() - 1 }); } else {////Fix!Fix!Fix!!!!!!! edge[s].push_back({ g,cap,cost,edge[g].size() }); edge[g].push_back({ s,cap,cost ,edge[s].size() - 1 }); } } long long int Solve(int s, int g, long long int f) { long long int ret = 0; for(auto &i:potent)i=0; while (f > 0) { // cout << f << endl; priority_queue<pair<long long int, int>, vector<pair<long long int, int>>, greater<pair<long long int, int>>>PQ; for (auto &i : dist)i = MOD * MOD; dist[s] = 0; PQ.push({ dist[s],s }); while (!PQ.empty()) { long long int c = PQ.top().first; int cn = PQ.top().second; // cout <<"jo"<< cn << endl; PQ.pop(); if (dist[cn] < c)continue; int cnt = 0; for (auto &i : edge[cn]) { // cout << i.to << endl; // cout << i.capa << " " << dist[i.to] << " " << dist[cn] << " " << i.cost << " " << potent[cn] << " "<<potent[i.to] << endl; if (i.capa > 0 && dist[i.to] > dist[cn] + i.cost + potent[cn] - potent[i.to]) { dist[i.to] = dist[cn] + i.cost + potent[cn] - potent[i.to]; pre_node[i.to] = cn; pre_edge[i.to] = cnt; PQ.push({ dist[i.to],i.to }); } cnt++; } } if (dist[g] == MOD * MOD)return -1; for (int i = 0; i < n; i++) potent[i] += dist[i]; long long int d = f; for (int i = g; i != s; i = pre_node[i]) { // cout << i << endl; d = min(d, edge[pre_node[i]][pre_edge[i]].capa); } f -= d; ret += d * potent[g]; for (int i = g; i != s; i = pre_node[i]) { auto &e = edge[pre_node[i]][pre_edge[i]]; e.capa -= d; edge[i][e.rev_index].capa += d; } // cout << ret << endl; } return ret; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; set<int>s; vector<pair<int, int>>v(N); long long int ans = 0; for (auto &i : v) { cin >> i.first >> i.second; ans += i.first + i.second; s.insert(i.first); s.insert(i.second); } map<int, int>m; int cnt = N; for (auto i : s) { m[i] = cnt; cnt++; } Min_Cost_Flow flow(N + s.size() + 10,false); for (int i = 0; i < N; i++) { flow.Add_Edge(i, m[v[i].first], 1, v[i].first); flow.Add_Edge(i, m[v[i].second], 1, v[i].second); flow.Add_Edge(i, cnt, 1, v[i].first + v[i].second); flow.Add_Edge(cnt + 1, i, 1, 0); } for (int i = N; i < cnt; i++)flow.Add_Edge(i, cnt + 2, 1, 0); flow.Add_Edge(cnt, cnt + 2, N, 0); cout << ans - flow.Solve(cnt + 1, cnt + 2, N) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(){ int n,c=0,a; cin>>n; for(int i=0;i<n;i++){ cin>>a; if(a%2==0){ if(a==2) c++; continue; } int j=sqrt(a)+1; if(j%2==0) j--; while(j>=3){ if(a%j==0) goto f; j=j-2; } c++; f:; } cout<<c<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const double eps = 1e-8; const double pi = acos(-1.0); int n, m; char a[110], b[110], c[110]; int d[26]; int main() { srand(time(NULL)); scanf("%d%d%s%s", &n, &m, a, b); int la = strlen(a), lb = strlen(b); int flag = 1; for (int i = 0; i < lb; i++) if (b[i] == '1') { for (int j = 0; j < la; j++) if (c[i + j] == 0 || c[i + j] == a[j]) { c[i + j] = a[j]; } else flag = 0; if (flag == 0) break; } if (flag) { for (int i = 0; i < m; i++) d[i] = i; for (int i = 0; i < lb; i++) if (b[i] == '0') { int done = 0; for (int j = 0; j < la; j++) if (c[i + j] == 0) { random_shuffle(d, d + m); for (int k = 0; k < m; k++) { if (d[k] + 'a' != a[j]) { c[i + j] = d[k] + 'a', done = 1; break; } } } else if (c[i + j] != a[j]) { done = 1; break; } if (!done) { flag = 0; break; } } } for (int i = 0; i < n; i++) if (c[i] == 0) c[i] = 'a'; if (flag) puts(c); else puts("No solution"); return 0; }
4
#include <bits/stdc++.h> using namespace std; int dist[2 * 100000 + 2 + 5], N, K; vector<int> G[2 * 100000 + 2 + 5]; void addedge(const string &wall, const string &wall2, int i, int offs) { int here = i + offs, there = i + N - offs; if (wall[i] == 'X') return; if (i != 0 && wall[i - 1] != 'X') G[here].push_back(here - 1); if (i == N - 1 || wall[i + 1] != 'X') G[here].push_back(here + 1); if (i + K >= N) { G[here].push_back(2 * N); } else if (wall2[i + K] != 'X') { G[here].push_back(there + K); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string L, R; cin >> N >> K >> L >> R; for (int i = 0; i < N; i++) { addedge(L, R, i, 0); addedge(R, L, i, N); } memset(dist, -1, sizeof(dist)); queue<int> q; dist[0] = 0; q.push(0); while (!q.empty()) { int fr = q.front(), d = dist[fr]; q.pop(); for (int to : G[fr]) { if (dist[to] == -1 && (to % N > d || to >= 2 * N)) { dist[to] = d + 1; q.push(to); } } } if (dist[2 * N] != -1 || dist[2 * N + 1] != -1) cout << "YES\n"; else cout << "NO\n"; }
4
#include <iostream> #include <algorithm> #include <cstring> using namespace std; typedef long long ll; const int mod=1000000007; int N,W; int w[201]; ll dp[2][10001][202]; int main(){ cin>>N>>W; for(int i=0;i<N;i++)cin>>w[i]; for(int j=0;j<=W;j++){ for(int k=0;k<=N;k++){ if(j<=W&&(k==N||w[k]+j>W))dp[N%2][j][k]=1; else dp[N%2][j][k]=0; } } for(int i=N-1;i>=0;i--){ int cur=(i+1)%2; int nxt=(i)%2; for(int j=0;j<=W;j++){ for(int k=0;k<=N;k++){ dp[nxt][j][k]=0; int nmwidx=k; if(k==N||w[k]>w[i])nmwidx=i; dp[nxt][j][k]+=dp[cur][j][nmwidx]; if(j+w[i]<=W)dp[nxt][j][k]+=dp[cur][j+w[i]][k]; dp[nxt][j][k]%=mod; } } } cout<<dp[0][0][N]<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int z = 998244353; void zf(string &s, int z[]) { z[0] = s.length(); int l = 0, r = 0; for (int i = 1; i < s.length(); i++) { if (i >= r) { l = r = i; while (r < s.length() && s[r - l] == s[r]) { r++; } z[i] = r - l; } else if (i + z[i - l] < r) { z[i] = z[i - l]; } else { l = i; while (r < s.length() && s[r - l] == s[r]) { r++; } z[i] = r - l; } } } int cs(string &a, int z[], int l1, int i, int j) { if (j - i != l1) { return j - i < l1 ? -1 : 1; } if (z[l1 + 1 + i] == l1) { return 0; } return a[l1 + 1 + i + z[l1 + 1 + i]] < a[z[l1 + 1 + i]] ? -1 : 1; } int main() { ios::sync_with_stdio(false); string a, l, r; cin >> a; cin >> l; cin >> r; string la = l + '#' + a; string ra = r + '#' + a; int zl[la.length()], zr[ra.length()]; zf(la, zl); zf(ra, zr); int dp[a.length() + 1]; dp[0] = 1; int i1 = 0, i2 = 0, dps = 0; for (int i = 1; i <= a.length(); i++) { while (i1 < i && cs(ra, zr, r.length(), i1, i) == 1) { if (a[i1] != '0') { dps = (dps - dp[i1] + z) % z; } i1++; } while (i2 < i && cs(la, zl, l.length(), i2, i) >= 0) { if (a[i2] != '0') { dps = (dps + dp[i2]) % z; } i2++; } dp[i] = dps; if (l == "0" && a[i - 1] == '0') { dp[i] = (dp[i] + dp[i - 1]) % z; } } printf("%d\n", dp[a.length()]); return 0; }
5
#include <iostream> using namespace std; int main(){ int n,a,b; cin >>n>>a>>b; cout <<min(a*n,b); }
0
#include <bits/stdc++.h> using namespace std; int n, b[5010]; long long tr[5010]; int gx1[5010][5010], gx2[5010][5010]; inline int lowbit(int x) { return (x & (-x)); } void xg(int x, int k) { for (int i = x; i <= n + 1; i += lowbit(i)) { tr[i] += k; } } long long getsum(int x) { long long res = 0; for (int i = x; i > 0; i -= lowbit(i)) { res += tr[i]; } return res; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> b[i]; b[i]++; } long long ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { gx1[i][j] = getsum(n + 1) - getsum(b[j]); } xg(b[i], 1); ans += getsum(n + 1) - getsum(b[i]); } memset(tr, 0, sizeof(tr)); for (int i = n; i > 0; i--) { for (int j = 1; j <= n; j++) { gx2[i][j] = getsum(b[j] - 1); } xg(b[i], 1); } if (ans == 0) { cout << "0 0"; return 0; } int maxx = 999999999, tt = 0; for (int i = 1; i < n; i++) { for (int j = i + 1; j <= n; j++) { int tot = ans + (gx1[i][j] - gx1[j][j] + gx2[i][j] - gx2[j][j]) + (gx2[j][i] - gx2[i][i] + gx1[j][i] - gx1[i][i]); if (b[i] > b[j]) tot++; if (maxx == tot) tt++; if (maxx > tot) maxx = tot, tt = 1; } } cout << maxx << " " << tt; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 305; int n, m, i, j, k; deque<char> a[N]; string s; vector<pair<pair<int, int>, pair<int, int> > > ans, s1, s2; inline void wk(vector<pair<pair<int, int>, pair<int, int> > >& v, bool fl) { static int b[N], c[N]; memset(b, 0, sizeof b); memset(c, 0, sizeof c); for (i = 1; i <= n; ++i) a[i].clear(); for (i = 1; i <= n; ++i) { cin >> s; if (fl) reverse(s.begin(), s.end()); for (j = 0; j < s.size(); ++j) a[i].push_back(s[j]); for (j = 2; j <= m; ++j) { cin >> s; if (fl) reverse(s.begin(), s.end()); for (k = s.size() - 1; k >= 0; --k) { if (i == 1) { if (s[k] == '0') v.push_back(make_pair(pair<int, int>(1, j), pair<int, int>(1, 1))); else v.push_back(make_pair(pair<int, int>(1, j), pair<int, int>(n, j))), ++c[j]; } else if (i == n) { if (s[k] == '1') v.push_back(make_pair(pair<int, int>(n, j), pair<int, int>(n, 1))); else v.push_back(make_pair(pair<int, int>(n, j), pair<int, int>(1, j))), ++b[j]; } else v.push_back(make_pair(pair<int, int>(i, j), pair<int, int>(i, 1))), a[i].push_front(s[k]); } } } for (i = 2; i <= m; ++i) { for (; b[i]--;) v.push_back(make_pair(pair<int, int>(1, i), pair<int, int>(1, 1))); for (; c[i]--;) v.push_back(make_pair(pair<int, int>(n, i), pair<int, int>(n, 1))); } for (i = 2; i < n; ++i) for (j = a[i].size() - 1; j >= 0; --j) if (a[i][j] == '0') v.push_back(make_pair(pair<int, int>(i, 1), pair<int, int>(1, 1))); else v.push_back(make_pair(pair<int, int>(i, 1), pair<int, int>(n, 1))); for (i = 0; i < a[1].size(); ++i) if (a[1][i] == '1') break; int z = 0; for (j = a[1].size() - 1; j >= i; --j) if (a[1][j] == '1') v.push_back(make_pair(pair<int, int>(1, 1), pair<int, int>(n, 1))); else v.push_back(make_pair(pair<int, int>(1, 1), pair<int, int>(1, 2))), ++z; for (; z--;) v.push_back(make_pair(pair<int, int>(1, 2), pair<int, int>(1, 1))); for (i = 0; i < a[n].size(); ++i) if (a[n][i] == '0') break; z = 0; for (j = a[n].size() - 1; j >= i; --j) if (a[n][j] == '0') v.push_back(make_pair(pair<int, int>(n, 1), pair<int, int>(1, 1))); else v.push_back(make_pair(pair<int, int>(n, 1), pair<int, int>(n, 2))), ++z; for (; z--;) v.push_back(make_pair(pair<int, int>(n, 2), pair<int, int>(n, 1))); std::cerr << v.size() << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; wk(s1, 0); wk(s2, 1); ans = s1; for (i = s2.size() - 1; i >= 0; --i) ans.push_back(make_pair(s2[i].second, s2[i].first)); cout << ans.size() << '\n'; for (i = 0; i < ans.size(); ++i) { cout << ans[i].first.first << ' ' << ans[i].first.second << ' '; cout << ans[i].second.first << ' ' << ans[i].second.second << '\n'; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 200000 + 7; const int M = 59; const int mid = M / 2; const int mod = 1e9 + 9; const int inf = 1e9 + 7; const long long linf = 1ll * inf * inf; const double pi = acos(-1); const double eps = 1e-7; const double ep = 1e-5; const int maxn = 1e5 + 7; const double PI = acos(-1); int n, m; vector<int> adj[N]; int d[N]; void DFS(int u, int r) { for (int j = (0); j < (adj[u].size()); j++) { int v = adj[u][j]; if (v != r) { d[v] = d[u] + 1; DFS(v, u); } } } void solve() { cin >> n >> m; for (int t = (0); t < (m); t++) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } DFS(1, -1); int mx = 1; for (int u = (1); u < (n + 1); u++) if (d[mx] < d[u]) mx = u; d[mx] = 0; DFS(mx, -1); for (int u = (1); u < (n + 1); u++) if (d[mx] < d[u]) mx = u; cout << d[mx]; } int main() { int T = 1; for (int i = (1); i < (T + 1); i++) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; const int MXN = 5010; long long n, k; int a[MXN], d; int main() { cin >> n >> k; while (n > 0) { a[d] = n % k; n /= k; if (d & 1) { if (a[d] > 0) { a[d] -= k; n++; } } d++; } printf("%d\n", d); for (int i = 0; i < d; ++i) printf("%d ", abs(a[i])); return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("tune=native") #pragma GCC optimize("unroll-loops") using namespace std; const int MAX = 2e5 + 5; const long long MOD = 1000000007; const long long MOD2 = 2010405347; const long long INF = 2e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 315; inline long long pw(long long x, long long y, long long md = MOD) { long long ret = 1; while (y) { if (y & 1) ret = ret * x % md; x = x * x % md, y >>= 1; } return ret; } inline void add(int &a, int b, int md = MOD) { a = a + b >= md ? a + b - md : a + b; } long long n, x[MAX], nw, y[MAX], z, ans, cnt; char c; void f(int nw) { if (y[nw] < 0) { --x[nw]; if (!x[nw + 1]) f(nw + 1); --x[nw + 1]; ++y[nw]; cout << nw << " -1\n"; ++cnt; if (cnt == 100000) exit(0); } else { ++x[nw]; if (x[nw + 1] == 9) f(nw + 1); ++x[nw + 1]; --y[nw]; cout << nw << " 1\n"; ++cnt; if (cnt == 100000) exit(0); } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = (1); i <= (n); ++i) cin >> c, x[i] = c - '0'; for (int i = (1); i < (n); ++i) { cin >> c; z = c - '0'; y[i] = z - (nw + x[i]); nw = y[i]; ans += abs(nw); } cin >> c; z = c - '0'; if (nw + x[n] != z) return cout << "-1\n", 0; cout << ans << '\n'; for (int i = (1); i < (n); ++i) while (y[i]) f(i); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MR = 3e5 + 10; int n, m, a, b, prv[MR], skoj[MR], p[MR], r[MR], iter, most[2][MR], znak[MR]; int base[MR]; vector<int> g[MR], k0, k1; queue<int> q; int done[MR]; int deg[MR]; bool removed[MR]; void makeset(int x) { p[x] = x; r[x] = 0; } void unionset(int x, int y) { if (r[x] > r[y]) p[y] = x; else { p[x] = y; if (r[x] == r[y]) r[y]++; } } int findset(int x) { if (p[x] != x) p[x] = findset(p[x]); return p[x]; } void przejdzSciezke(int nr, int nrstop) { if (nr == nrstop) return; int pom = skoj[nr]; while (true) { if (prv[pom] == -1) { przejdzSciezke(most[0][pom], pom); przejdzSciezke(most[1][pom], nrstop); skoj[most[0][pom]] = most[1][pom]; skoj[most[1][pom]] = most[0][pom]; break; } int pom1 = skoj[prv[pom]]; skoj[pom] = prv[pom]; skoj[prv[pom]] = pom; if (prv[pom] == nrstop) { break; } nr = prv[pom]; pom = pom1; } } bool search() { while (!q.empty()) { int akt = q.front(); q.pop(); for (int i = 0; i < (int)g[akt].size(); i++) if (!znak[g[akt][i]]) { znak[g[akt][i]] = 1; prv[g[akt][i]] = akt; znak[skoj[g[akt][i]]] = 2; q.push(skoj[g[akt][i]]); } else if (znak[g[akt][i]] == 2) { int p0 = base[findset(akt)], p1 = base[findset(g[akt][i])]; if (p0 == p1) continue; iter++; while (true) { if (done[p0] != iter) { done[p0] = iter; if (skoj[p0] != -1) { done[skoj[p0]] = 1; p0 = base[findset(prv[skoj[p0]])]; } } else if (skoj[p0] != -1) break; if (done[p1] != iter) { done[p1] = iter; if (skoj[p1] != -1) { done[skoj[p1]] = 1; p1 = base[findset(prv[skoj[p1]])]; } } else if (skoj[p1] != -1) break; if (skoj[p0] == -1 && skoj[p1] == -1) break; } if (skoj[p0] == -1 && skoj[p1] == -1 && p0 != p1) { przejdzSciezke(akt, p0); przejdzSciezke(g[akt][i], p1); skoj[akt] = g[akt][i]; skoj[g[akt][i]] = akt; return 1; } else { int pom0 = base[findset(akt)]; k0.clear(); while (true) { if (pom0 == p0 || pom0 == p1) break; most[0][pom0] = akt; most[1][pom0] = g[akt][i]; if (znak[skoj[pom0]] == 1) { znak[skoj[pom0]] = 2; q.push(skoj[pom0]); } k0.push_back(pom0); k0.push_back(skoj[pom0]); pom0 = base[findset(prv[skoj[pom0]])]; } int pom1 = base[findset(g[akt][i])]; k1.clear(); while (true) { if (pom1 == pom0) break; most[0][pom1] = g[akt][i]; most[1][pom1] = akt; if (znak[skoj[pom1]] == 1) { znak[skoj[pom1]] = 2; q.push(skoj[pom1]); } k1.push_back(pom1); k1.push_back(skoj[pom1]); pom1 = base[findset(prv[skoj[pom1]])]; } for (int i = 1; i < (int)k0.size(); i++) unionset(findset(k0[i - 1]), findset(k0[i])); for (int i = 1; i < (int)k1.size(); i++) unionset(findset(k1[i - 1]), findset(k1[i])); if (k0.size()) unionset(findset(pom0), findset(k0[0])); if (k1.size()) unionset(findset(pom0), findset(k1[0])); base[findset(pom0)] = pom0; } } } return 0; } int main() { int tests; scanf("%d", &tests); for (int c = 0; c < tests; c++) { scanf("%d%d", &n, &m); int ile = n; n *= 3; map<pair<int, int>, int> Mkr; for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); a--; b--; g[a].push_back(b); g[b].push_back(a); Mkr[make_pair(min(a, b), max(a, b))] = i; } set<pair<int, int>> S; for (int i = 0; i < n; i++) { deg[i] = g[i].size(); S.insert(make_pair(deg[i], i)); } int tmp = m; while (tmp) { auto it = S.end(); it--; for (int i : g[it->second]) if (!removed[i]) { S.erase(make_pair(deg[i], i)); deg[i]--; if (deg[i]) S.insert(make_pair(deg[i], i)); } tmp -= it->first; deg[it->second] = 0; removed[it->second] = 1; S.erase(it); } vector<int> IS; for (int i = 0; i < n; i++) if (!removed[i]) IS.push_back(i); else removed[i] = 0; if (IS.size() >= ile) { printf("IndSet\n"); for (int i : IS) { printf("%d ", i + 1); ile--; if (!ile) break; } printf("\n"); } else { for (int i = 0; i < n; i++) skoj[i] = -1; while (true) { while (!q.empty()) q.pop(); for (int i = 0; i < n; i++) { if (skoj[i] == -1) { znak[i] = 2; q.push(i); } else znak[i] = 0; done[i] = 0; makeset(i); base[i] = i; prv[i] = -1; } iter = 0; if (!search()) break; } vector<int> matching; for (int i = 0; i < n; i++) if (skoj[i] != -1) { matching.push_back(Mkr[make_pair(min(i, skoj[i]), max(i, skoj[i]))]); skoj[skoj[i]] = -1; skoj[i] = -1; } if (matching.size() >= ile) { printf("Matching\n"); for (int i : matching) { printf("%d ", i + 1); ile--; if (!ile) break; } printf("\n"); } else printf("Impossible\n"); } for (int i = 0; i < n; i++) g[i].clear(); } return 0; }
3
#include <bits/stdc++.h> typedef long long ll; typedef long long llong; typedef long double ld; typedef unsigned long long ull; using namespace std; /* ll pw(ll a, ll b) { ll ans = 1; while (b) { while (!(b & 1)) b >>= 1, a = (a * a) % MOD; ans = (ans * a) % MOD, --b; } return ans; } */ const int MAXN = 220000; int n; int was[MAXN]; vector<int> eds[MAXN]; int h[MAXN]; int sz[MAXN]; ll ans = 0; char s[MAXN]; int sm; void dfs1(int v) { was[v] = 1; if (s[v] == '1') sz[v] = 1; h[v] = 0; for (int u: eds[v]) { if (!was[u]) dfs1(u), sz[v] += sz[u], h[v] = max(h[v], h[u] + 1); } } void dfs2(int v, int hh) { vector<pair<int, int> > vv; vv.push_back(make_pair(0, s[v] - '0')); was[v] = 1; int mx1 = hh; int mx2 = 0; int cur = sm; if (s[v] == '1') --cur; for (int u: eds[v]) { if (was[u]) continue; if (h[u] + 1 > mx2) mx2 = h[u] + 1; if (mx2 > mx1) swap(mx2, mx1); vv.push_back(make_pair(h[u] + 1, (sz[u] ? 1 : 0))); cur -= sz[u]; } vv.push_back(make_pair(hh, (cur ? 1 : 0))); for (int u: eds[v]) { if (was[u]) continue; if (h[u] + 1 == mx1) dfs2(u, mx2 + 1); else dfs2(u, mx1 + 1); } int fl = 0; sort(vv.begin(), vv.end()); for (int i = 0; i < vv.size() - 1; ++i) { fl |= vv[i].second; if (fl) { if (i + 2 == vv.size()) { if (vv[i + 1].first > vv[i].first) ++ans; if (vv[i + 1].first > vv[i].first + 1) ++ans; } else { ans += vv[i + 1].first - vv[i].first; } } } } int main() { ans = 1; scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { int a, b; scanf("%d%d", &a, &b); --a, --b; eds[a].push_back(b); eds[b].push_back(a); } scanf(" %s", s); for (int i = 0; i < n; ++i) if (s[i] == '1') ++sm; dfs1(0); memset(was, 0, sizeof(was)); dfs2(0, 0); cout << ans << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, a[1000000]; int i; int p = 1; cin >> t; while (t--) { cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (i = 0; i < n - 1; i++) { if (a[i] != a[i + 1]) p++; } cout << p << endl; p = 1; } }
2
#include <bits/stdc++.h> using namespace std; long long MOD = 1791791791; double eps = 1e-12; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); template <typename T, typename U, typename V> struct Triple { T first; U second; V third; Triple(T first, U second, V third) : first(first), second(second), third(third) {} Triple() {} }; const long long p = 31, mod = 1791791791; vector<long long> ps, h; long long n; long long subHash(long long l, long long r) { return (h[r + 1] - (h[l] * ps[r - l + 1]) % mod + mod) % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; n = s.size(); ps.resize(n + 1); h.resize(n + 1); ps[0] = 1; h[0] = 0; for (long long i = 0; i < n; i++) { ps[i + 1] = (ps[i] * p) % mod; long long d = (s[i] - 'a' + 1); h[i + 1] = (h[i] * p + d) % mod; } vector<pair<long long, long long> > finds; for (long long i = 0; i < n - 1; i++) { if (h[i + 1] == subHash(n - i - 1, n - 1)) { finds.push_back({i + 1, h[i + 1]}); } } long long l = -1, r = finds.size(); long long ans = 0; while (l + 1 < r) { long long m = (l + r) / 2; bool done = false; long long need = finds[m].second; for (long long i = 1; i < n - finds[m].first; i++) { if (need == subHash(i, i + finds[m].first - 1)) { done = true; } } if (done) { l = m; ans = finds[m].first; } else { r = m; } } if (ans == 0) { cout << "Just a legend" << "\n"; } else { cout << s.substr(0, ans) << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int q; long long a, b, u, v, k; long long calc(long long u, long long v, long long k, long long o = 0) { if (u > v) swap(u, v); if (u < 1 || k <= 0) return 0; long long p = 1, res = 0; while (p * 2LL <= v) p *= 2LL; long long tmp = min(p, k); if (u <= p) { res = (tmp * (tmp + 1) / 2 % mod) * u % mod; res += (o * tmp % mod) * u % mod + calc(u, v - p, k - p, o + p); res %= mod; } else { res = (tmp * (tmp + 1) / 2 % mod) * p % mod; res += (o * tmp % mod) * p % mod; res += calc(u - p, v - p, k, o) + calc(p, v - p, k - p, o + p) + calc(u - p, p, k - p, o + p); } return res; } int main() { scanf("%d", &q); while (q-- > 0) { scanf("%lld %lld %lld %lld %lld", &a, &b, &u, &v, &k); long long res = (calc(u, v, k) + calc(a - 1, b - 1, k)) % mod; res = (res - calc(a - 1, v, k) - calc(u, b - 1, k) + mod * mod) % mod; printf("%lld\n", res); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, c = 0, j = 0, index[3], sum = 0, avg; cin >> n; long long int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } avg = sum / n; if (avg * n != sum) cout << "Unrecoverable configuration."; else { for (i = 0; i < n; i++) { if (a[i] == avg) c++; else { index[j] = i; j++; } if (j == 3) break; } if (c == n) cout << "Exemplary pages."; else if (c == n - 2) { if (a[index[1]] > a[index[0]]) cout << (a[index[1]] - a[index[0]]) / 2 << " ml. from cup #" << index[0] + 1 << " to cup #" << index[1] + 1 << "."; else cout << (a[index[0]] - a[index[1]]) / 2 << " ml. from cup #" << index[1] + 1 << " to cup #" << index[0] + 1 << "."; } else cout << "Unrecoverable configuration."; } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long a[100009]; int main() { long long n, x, y; int i; while (cin >> n >> x >> y) { int flag = 0; for (i = 1; i <= n - 1; i++) a[i] = 1, y--; a[n] = y; if (a[n] <= 0) flag = 1; long long sum = 0; for (i = 1; i <= n; i++) { sum = sum + a[i] * a[i]; } if (sum < x || flag) cout << "-1" << endl; else { for (i = 1; i <= n; i++) { cout << a[i] << endl; } } } }
1
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } long long c[50]; long long calc(long long u) { if (u == 0) return 0; if (u == 1) return 1; if (u == 2) return 3; if (u == 3) return 4; int i = 63 - __builtin_clzll(u); long long first = (1LL << i); long long sum = 0; sum += calc(u - first) + c[i] + first; return sum; } int main() { c[1] = 1; c[2] = 4; for (int i = 3; i < 50; i++) { c[i] = 2 * c[i - 1] + (1LL << (i - 1)); } long long n; scanf("%I64d", &n); cout << calc(n - 1) << endl; }
5
#include <iostream> #include <string> #include <cmath> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<iomanip> #define _USE_MATH_DEFINES #include <math.h> #include <functional> #include<complex> using namespace std; #define rep(i,x) for(ll i=0;i<x;i++) #define repn(i,x) for(ll i=1;i<=x;i++) typedef long long ll; const ll INF = 1e17; const ll MOD = 1000000007; const ll MAX = 4000001; const long double eps = 1E-14; ll max(ll a, ll b) { if (a > b) { return a; } return b; } ll min(ll a, ll b) { if (a > b) { return b; } return a; } ll gcd(ll a, ll b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } struct edge { ll ind; ll fr; ll to; ll d; }; class mint { long long x; public: mint(long long x = 0) : x((x% MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint& a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint& a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint& a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint& a) const { mint res(*this); return res += a; } mint operator-(const mint& a) const { mint res(*this); return res -= a; } mint operator*(const mint& a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint& operator/=(const mint& a) { return (*this) *= a.inv(); } mint operator/(const mint& a) const { mint res(*this); return res /= a; } friend ostream& operator<<(ostream& os, const mint& m) { os << m.x; return os; } }; mint pw(mint a, ll b) { if (b == 0) { return 1; } mint ret = pw(a, b >> 1); ret *= ret; if (b & 1) { ret *= a; } return ret; } typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<vector<vector<ll>>> vvvll; typedef vector<mint> vmint; typedef vector<vector<mint>> vvmint; typedef vector<vector<vector<mint>>> vvvmint; ////////////////////////////////////// int main() { ll N, M; cin >> N >> M; ll L = max(N, M); vvvll mat(L + 1); mat[0].assign(1, vll(1, 0)); repn(i, L) { ll K = 1 << i; mat[i].assign(K, vll(K, 0)); rep(x, K)rep(y, K) { ll t = mat[i - 1][x % (K / 2)][y % (K / 2)]; if (x >= K / 2 && y >= K / 2) { mat[i][x][y] = 1 - t; } else { mat[i][x][y] = t; } } //rep(x, K) { //rep(y, K)cout << mat[i][x][y]; //cout << endl; //} } vvll ans(1 << L, vll(1 << L, 0)); repn(x, (1 << L) - 1)repn(y, (1 << L) - 1) { ans[x][y] = (mat[L][x - 1][y] + mat[L][x][y - 1] + mat[L][x - 1][y - 1] +mat[L][x][y]) % 2; } repn(x, (1 << N) - 1) { repn(y, (1 << M) - 1)cout << ans[x][y]; cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; string s; cin >> s; int curr = 1, count = 0; for (int i = 0; i < s.length(); i++) { int index = int(s[i]) - 96; if (abs(index - curr) <= 13) { count += abs(index - curr); } else { count += 26 - abs(index - curr); } curr = index; } cout << count; return 0; }
1
#include<bits/stdc++.h> #define ll long long #define mod 1000000007 #define N 2009 #define M 4000009 #define cbn(x,y) ((ll)fac[x]*inv[y]%mod*inv[(x)-(y)]%mod) using namespace std; int n,m,tot,fac[M],inv[M],dp[N][N]; int main(){ scanf("%d%d",&n,&m); tot=n*m; if (m==1){ puts("1"); return 0; } int i,j; fac[0]=inv[0]=inv[1]=1; for (i=1; i<=tot; i++) fac[i]=(ll)fac[i-1]*i%mod; for (i=2; i<=tot; i++) inv[i]=mod-(ll)inv[mod%i]*(mod/i)%mod; for (i=2; i<=tot; i++) inv[i]=(ll)inv[i-1]*inv[i]%mod; dp[1][0]=fac[n]; for (i=1; i<=n; i++) for (j=0; j<=i; j++){ if (i<n) dp[i+1][j]=(dp[i+1][j]+dp[i][j])%mod; if (j<i) dp[i][j+1]=(dp[i][j+1]+(ll)dp[i][j]*cbn(tot-i-(m-1)*j-1,m-2))%mod; } printf("%d\n",dp[n][n]); return 0; }
0
#include <bits/stdc++.h> using namespace std; int64_t gcd(int64_t a, int64_t b){ return b==0 ? a : gcd(b, a%b); } int64_t lcm(int64_t a, int64_t b){ return a * (b/gcd(a, b)); } void fail(){ cout << 0 << endl; exit(0); } int main(){ int N, M; cin >> N >> M; vector<int> A(N); for(int i=0; i<N; i++){ cin >> A[i]; A[i] /= 2; } int64_t l = 1; for(int a : A){ l = lcm(l, a); if(l > 1e9) fail(); } for(int a : A) if((l/a) % 2 == 0) fail(); int num = M/l; int ans = (num+1)/2; cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> v) { os << "(" << v.first << ", " << v.second << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << " "; } os << v[i]; } return os; } template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) { for (int i = 0; i < (int)v.size(); i++) { if (i > 0) { os << endl; } os << v[i]; } return os; } string num2bit(long long num, long long len) { string bit = ""; for (int i = 0; i < (int)len; ++i) { bit += char('0' + (num >> i & 1)); } return bit; } template <typename Monoid> struct SegmentTree { using F = function<Monoid(Monoid, Monoid)>; int sz; vector<Monoid> seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid &x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid &x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } template <typename C> int find_subtree(int a, const C &check, Monoid &M, bool type) { while (a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <typename C> int find_first(int a, const C &check) { Monoid L = M1; if (a <= 0) { if (check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <typename C> int find_last(int b, const C &check) { Monoid R = M1; if (b >= sz) { if (check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(seg[--b], R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)n; ++i) cin >> a[i]; priority_queue<pair<long long, long long>> b; for (int i = 0; i < (int)n; ++i) b.push({a[i], i}); SegmentTree<long long> seg( n + 1, [](long long a, long long b) { return a + b; }, 0); long long res = 0; for (int i = n - 1; i >= 0; i--) { while (!b.empty()) { auto t = b.top(); if (t.first <= i) break; b.pop(); seg.update(t.second, 1); } long long L = i + 1; long long R = min(a[i], n); if (L >= R) continue; res += seg.query(L, R); } cout << res << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; void getint(int &v) { char ch, fu = 0; for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ; if (ch == '-') fu = 1, ch = getchar(); for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0'; if (fu) v = -v; } long long x, ans; int n, f[500010][2], a[500010], b[500010]; int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf("%lld", &x); while (x) a[i] += x % 2, x >>= 1; b[i] = b[i - 1] + a[i]; } f[0][0] = 1; for (int i = 1; i <= n; i++) { f[i][0] = f[i - 1][0]; f[i][1] = f[i - 1][1]; ++f[i][b[i] & 1]; } for (int i = 1; i <= n; i++) { int t = max(1, i - 119); int s = 0, mx = 0; for (int j = i; j >= t; j--) { mx = max(mx, a[j]); s += a[j]; if (s % 2 == 0 && mx <= s / 2) ++ans; } if (t - 2 >= 0) ans += f[t - 2][b[i] & 1]; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a > b) { return a; } return b; } long long int min(long long int a, long long int b) { if (a < b) { return a; } return b; } long long int n, k; long long int arr[200005]; long long int power(long long int x, long long int n) { long long int res = 1; while (n > 0) { if ((long long)n % 2 == 1) { res = (res * x); } x = (x * x); n = n / 2; } return res; } void solve() { long long int ctr[2 * k + 1]; long long int prefix[2 * k + 1]; memset(prefix, 0, sizeof(prefix)); memset(ctr, 0, sizeof(ctr)); for (int i = 0; i < n / 2; i++) { ctr[arr[i] + arr[n - i - 1]]++; long long int mn = min(arr[i], arr[n - i - 1]); long long int mx = max(arr[i], arr[n - i - 1]); mn += 1; mx += k; prefix[mn]++; prefix[mx + 1]--; } for (int i = 1; i < 2 * k + 1; i++) { prefix[i] += prefix[i - 1]; } long long int ans = LONG_MAX; for (int sum = 2; sum <= 2 * k; sum++) { ans = min(ans, (prefix[sum] - ctr[sum]) + (n / 2 - prefix[sum]) * 2); } cout << ans << "\n"; } int main() { int tc; cin >> tc; while (tc--) { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> arr[i]; } solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; string s; int n; int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> s; int n1 = 0, n0 = 0, n2 = 0; for (int i = 0; i < n; i++) { if (s[i] == '0') { n0++; } else if (s[i] == '1') { n1++; } else { n2++; } } for (int i = 0; i < n; i++) { if (n0 >= n / 3) break; if (s[i] == '1') { if (n1 > n / 3) { s[i] = '0'; n0++; n1--; } } if (s[i] == '2') { if (n2 > n / 3) { s[i] = '0'; n0++; n2--; } } } reverse(s.begin(), s.end()); for (int i = 0; i < n; i++) { if (n2 >= n / 3) { break; } if (s[i] == '1') { if (n1 > n / 3) { s[i] = '2'; n2++; n1--; } } if (s[i] == '0') { if (n0 > n / 3) { s[i] = '2'; n2++; n0--; } } } for (int i = 0; i < n; i++) { if (n0 == n / 3 || n1 == n / 3) { break; } if (s[i] == '0') { s[i] = '1'; n1++; n0--; } } reverse(s.begin(), s.end()); for (int i = 0; i < n; i++) { if (n2 == n / 3 || n1 == n / 3) { break; } if (s[i] == '2') { s[i] = '1'; n1++; n2--; } } cout << s << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; struct node { int l, r, x; node() {} node(int l, int r, int x) : l(l), r(r), x(x) {} }; vector<int> vec[100010]; int n, m; int vis[100010]; vector<node> res; int a[100010 * 100], now[100010], nx[100010 * 100], last; int sze[100010]; queue<int> q; void add(int x, int id) { a[++last] = x; nx[last] = now[id]; now[id] = last; } int main() { while (scanf("%d%d", &n, &m) != EOF) { int tot = 0; res.clear(); memset(now, 0, sizeof now); for (int i = 1, x, y; i <= n; ++i) { scanf("%d", &x); tot += x; vec[i].clear(); for (int j = 1; j <= x; ++j) { scanf("%d", &y); vec[i].push_back(y); } sze[i] = vec[i].size(); } if (tot % n == 0) { int x = tot / n; for (int i = 1; i <= n; ++i) if (sze[i] > x) for (auto it : vec[i]) add(i, it); for (int i = 1; i <= m; ++i) if (now[i]) q.push(i); for (int i = 1, front; i <= n; ++i) if (sze[i] < x) { for (auto it : vec[i]) vis[it] = 1; while (sze[i] < x) { front = q.front(); q.pop(); if (vis[front]) { q.push(front); continue; } int id = now[front]; for (; id; id = nx[id]) { if (sze[a[id]] <= x) continue; res.push_back(node(a[id], i, front)); vec[i].push_back(front); vis[front] = 1; --sze[a[id]]; ++sze[i]; id = nx[id]; break; } if (id) q.push(front); now[front] = id; } for (auto it : vec[i]) vis[it] = 0; } } else { int x = tot / n + 1; int need = n - tot % n; for (int i = 1; i <= n; ++i) if (sze[i] > x) for (auto it : vec[i]) add(i, it); for (int i = 1; i <= m; ++i) if (now[i]) q.push(i); for (int i = 1, front; i <= n; ++i) if (sze[i] < x) { if (sze[i] == x - 1 && need) { --need; continue; } for (auto it : vec[i]) vis[it] = 1; while (sze[i] < x) { if (sze[i] == x - 1 && need) { --need; break; } front = q.front(); q.pop(); if (vis[front]) { q.push(front); continue; } int id = now[front]; for (; id; id = nx[id]) { if (sze[a[id]] <= x) continue; res.push_back(node(a[id], i, front)); vec[i].push_back(front); vis[front] = 1; --sze[a[id]]; ++sze[i]; id = nx[id]; break; } if (id) q.push(front); now[front] = id; } for (auto it : vec[i]) vis[it] = 0; } } int len = res.size(); printf("%d\n", len); for (int i = 0; i < len; ++i) printf("%d %d %d\n", res[i].l, res[i].r, res[i].x); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; int abc[200009][26] = {0}, ans = 0, flag = 0, nouse[26] = {0}; string rem[200019]; int same[26][25] = {0}; int main() { for (int i = 0; i < 26; i++) nouse[i] = 1; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < s.size(); j++) { if (abc[i][(int)s[j] - 'a'] < 1) abc[i][(int)s[j] - 'a']++; } } for (int i = 0; i < 26; i++) { flag = 0; for (int j = 0; j < n; j++) if (abc[j][i] == 1 && flag == 0) { flag = 1; ans++; nouse[i] = 0; } } for (int i = 0; i < 26; i++) { for (int j = 0; j < n; j++) { if (abc[j][i] > 0) rem[j] += (i + 'a'); } } for (int i = 0; i < n; i++) { for (int j = 0; j < rem[i].size(); j++) { for (int k = 0; k < rem[i].size(); k++) { if (j != k && (int)rem[i][j] < rem[i][k]) same[(int)rem[i][j] - 'a'][(int)rem[i][k] - 'a' - 1] = 1; if (j != k && (int)rem[i][j] > rem[i][k]) same[(int)rem[i][j] - 'a'][(int)rem[i][k] - 'a'] = 1; } } } for (int x = 0; x < 26; x++) { for (int i = 0; i < 26; i++) { for (int j = 0; j < 25; j++) if (same[i][j] == 1) { int tr = j >= i ? j + 1 : j; for (int k = 0; k < 25; k++) if (same[tr][k] == 1) { if (tr > k && i > k || tr <= k && i <= k) same[i][k] = 1; else if (tr > k && i <= k && k != i) same[i][k - 1] = 1; else if (tr <= k && i > k && k + 1 != i) same[i][k + 1] = 1; } } } } for (int i = 0; i < 26; i++) { if (nouse[i] == 1) continue; for (int j = 0; j < 25; j++) { if (same[i][j] == 1) { ans--; int sr = j >= i ? j + 1 : j; nouse[sr] = 1; } } } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<string> vec_splitter(string second) { second += ','; vector<string> res; while (!second.empty()) { res.push_back(second.substr(0, second.find(','))); second = second.substr(second.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tt = clock(); int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int m; cin >> m; for (int i = 0; i < m; i++) { int b, c; cin >> b >> c; b--; if (b != 0 and b != n - 1) { a[b - 1] += c - 1; a[b + 1] += a[b] - c; a[b] = 0; } else if (b == 0) { a[b + 1] += a[b] - c; a[b] = 0; } else { a[b - 1] += c - 1; a[b] = 0; } } for (auto k : a) cout << k << "\n"; cerr << "TIME = " << (double)1.0 * (clock() - tt) / CLOCKS_PER_SEC << " seconds" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > pC, dC; bool cmp1(const pair<long long, long long> &i, const pair<long long, long long> &j) { if (i.first < j.first) return true; if (i.first == j.first) return (dC[i.second] < dC[j.second]); return false; } bool cmp2(const pair<long long, long long> &i, const pair<long long, long long> &j) { if (i.first < j.first) return true; if (i.first == j.first) return (pC[i.second] < pC[j.second]); return false; } int main() { std::ios_base::sync_with_stdio(false); long long N; cin >> N; pC.resize(N); dC.resize(N); vector<pair<long long, long long> > p(N), d(N); long long x1, x2; cin >> x1 >> x2; for (long long i = 0; i < N; ++i) { long long k, b; cin >> k >> b; pC[i] = p[i] = {k * x1 + b, i}; dC[i] = d[i] = {k * x2 + b, i}; } stable_sort(p.begin(), p.end(), cmp1); stable_sort(d.begin(), d.end(), cmp2); for (long long i = 0; i < N; ++i) { if (p[i].second != d[i].second) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
2
#include<bits/stdc++.h> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define EPS (1e-6) #define equals(a,b) (fabs((a)-(b)) < EPS) #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 using namespace std; class Point{ public: double x,y; Point(double x = 0,double y = 0): x(x),y(y){} Point operator + (Point p){return Point(x+p.x,y+p.y);} Point operator - (Point p){return Point(x-p.x,y-p.y);} Point operator * (double a){return Point(a*x,a*y);} Point operator / (double a){return Point(x/a,y/a);} Point operator * (Point a){ return Point(x * a.x - y * a.y, x * a.y + y * a.x); } bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); } bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; } }; struct Segment{ Point p1,p2; int index; Segment(Point p1 = Point(),Point p2 = Point(),int index=-1):p1(p1),p2(p2),index(index){} bool operator < (const Segment& s) const { return ( p2 == s.p2 ) ? p1 < s.p1 : p2 < s.p2; } bool operator == (const Segment& s) const { return ( s.p1 == p1 && s.p2 == p2 ) || ( s.p1 == p2 && s.p2 == p1 ); } }; typedef Point Vector; typedef Segment Line; typedef vector<Point> Polygon; ostream& operator << (ostream& os,const Point& a){ os << "(" << a.x << "," << a.y << ")"; } ostream& operator << (ostream& os,const Segment& a){ os << "( " << a.p1 << " , " << a.p2 << " )"; } double dot(Point a,Point b){ return a.x*b.x + a.y*b.y; } double cross(Point a,Point b){ return a.x*b.y - a.y*b.x; } double norm(Point a){ return a.x*a.x+a.y*a.y; } double abs(Point a){ return sqrt(norm(a)); } //rad ??????§?????????????????¢???????§???????????????????¨ Point rotate(Point a,double rad){ return Point(cos(rad)*a.x - sin(rad)*a.y,sin(rad)*a.x + cos(rad)*a.y); } // ????????????????¢???????????? double toRad(double agl){ return agl*M_PI/180.0; } // a => prev, b => cur, c=> next // prev ?????? cur ??????????£??? next ??????????????????§??????????±??????? double getArg(Point a,Point b,Point c){ double arg1 = atan2(b.y-a.y,b.x-a.x); double arg2 = atan2(c.y-b.y,c.x-b.x); double arg = fabs( arg1 - arg2 ); while( arg > M_PI ) arg -= 2.0 * M_PI; return fabs(arg); } int ccw(Point p0,Point p1,Point p2){ Point a = p1-p0; Point b = p2-p0; if(cross(a,b) > EPS)return COUNTER_CLOCKWISE; if(cross(a,b) < -EPS)return CLOCKWISE; if(dot(a,b) < -EPS)return ONLINE_BACK; if(norm(a) < norm(b))return ONLINE_FRONT; return ON_SEGMENT; } bool intersectLL(Line l, Line m) { return abs(cross(l.p2-l.p1, m.p2-m.p1)) > EPS || // non-parallel abs(cross(l.p2-l.p1, m.p1-l.p1)) < EPS; // same line } bool intersectLS(Line l, Line s) { return cross(l.p2-l.p1, s.p1-l.p1)* // s[0] is left of l cross(l.p2-l.p1, s.p2-l.p1) < EPS; // s[1] is right of l } bool intersectLP(Line l,Point p) { return abs(cross(l.p2-p, l.p1-p)) < EPS; } bool intersectSS(Line s, Line t) { return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 && ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0; } bool intersectSP(Line s, Point p) { return abs(s.p1-p)+abs(s.p2-p)-abs(s.p2-s.p1) < EPS; // triangle inequality } Point projection(Line l,Point p) { double t = dot(p-l.p1, l.p1-l.p2) / norm(l.p1-l.p2); return l.p1 + (l.p1-l.p2)*t; } Point reflection(Line l,Point p) { return p + (projection(l, p) - p) * 2; } double distanceLP(Line l, Point p) { return abs(p - projection(l, p)); } double distanceLL(Line l, Line m) { return intersectLL(l, m) ? 0 : distanceLP(l, m.p1); } double distanceLS(Line l, Line s) { if (intersectLS(l, s)) return 0; return min(distanceLP(l, s.p1), distanceLP(l, s.p2)); } double distanceSP(Line s, Point p) { Point r = projection(s, p); if (intersectSP(s, r)) return abs(r - p); return min(abs(s.p1 - p), abs(s.p2 - p)); } double distanceSS(Line s, Line t) { if (intersectSS(s, t)) return 0; return min(min(distanceSP(s, t.p1), distanceSP(s, t.p2)), min(distanceSP(t, s.p1), distanceSP(t, s.p2))); } Point crosspoint(Line l,Line m){ double A = cross(l.p2-l.p1,m.p2-m.p1); double B = cross(l.p2-l.p1,l.p2-m.p1); if(abs(A) < EPS && abs(B) < EPS){ vector<Point> vec; vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2); sort(vec.begin(),vec.end()); assert(vec[1] == vec[2]); //?????????????°?????????????????? return vec[1]; //return m.p1; } if(abs(A) < EPS)assert(false); return m.p1 + (m.p2-m.p1)*(B/A); } //cross product of pq and pr double cross3p(Point p,Point q,Point r) { return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); } //returns true if point r is on the same line as the line pq bool collinear(Point p,Point q,Point r) { return fabs(cross3p(p,q,r)) < EPS; } //returns true if point t is on the left side of line pq bool ccwtest(Point p,Point q,Point r){ return cross3p(p,q,r) > 0; //can be modified to accept collinear points } bool onSegment(Point p,Point q,Point r){ return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ; } // ------------------ double heron(Point A,Point B,Point C){ double a = abs(B-C); double b = abs(A-C); double c = abs(A-B); double s = ( a + b + c ) / 2; return sqrt( s * ( s - a ) * ( s - b ) * ( s - c ) ); } Line calcLine(Line line1,Line line2,Point p1,Point p2){ Point cp = crosspoint(line1,line2); double S = heron(p1,cp,p2); double a = abs(p1-cp); double b = abs(p2-cp); double arg_a = asin((2.0*S)/(a*b)); if( equals(2*S,a*b) ) arg_a = toRad(90); //arg_a = getArg(p1,cp,p2); //assert( !( cp == p1 || cp == p2 || p1 == p2 ) ); int res = ccw(cp,p1,p2); //assert( ( res == CLOCKWISE || res == COUNTER_CLOCKWISE )); //while( !( res == CLOCKWISE || res == COUNTER_CLOCKWISE )); Point base; if( res == COUNTER_CLOCKWISE ) base = p1; else base = p2; Point not_base = (base==p1)?p2:p1; //cout << base << " -> " << cp << " -> " << not_base << endl; arg_a = (toRad(180.0)-getArg(base,cp,not_base)); //puts("^^^^^"); //cout << "base = " << base << " | " << (arg_a*180/M_PI)<< endl; //cout << line1 << " and " << line2 << endl; //cout << cp << " | " << p1 << " | " << p2 << endl; Vector e = ( base - cp ) / abs( base - cp ); e = rotate(e,arg_a/2.0); Line tmp = Line(cp,cp+e*100); //cout << "return = " << tmp << endl; //puts("_____"); return tmp; } const string MANY = "Many"; const string NONE = "None"; #define all(x) (x.begin(),x.end()) void compute(vector<Line> &vec){ if( vec.size() <= 2 ) { cout << MANY << endl; return; } vector<Line> candidateLines; int n = vec.size(); rep(i,n) REP(j,i+1,n){ if( equals(cross(vec[i].p1-vec[i].p2,vec[j].p1-vec[j].p2),0.0) ) { Vector e = ( vec[i].p2 - vec[i].p1 ) / abs( vec[i].p2 - vec[i].p1 ); e = rotate(e,toRad(90)); Line line = Line(vec[i].p1,vec[i].p1+e*100); Point cp1 = crosspoint(line,vec[i]); Point cp2 = crosspoint(line,vec[j]); Point mp = ( cp1 + cp2 ) / 2.0; e = ( vec[i].p2 - vec[i].p1 ) / abs( vec[i].p2 - vec[i].p1 ); line = Line(mp,mp+e*100); line.index = candidateLines.size(); candidateLines.push_back(line); } else { //cout << vec[i] << " x " << vec[j] << endl; Point cp = crosspoint(vec[i],vec[j]); Point I = ( vec[i].p1 == cp ) ? vec[i].p2 : vec[i].p1; Point J = ( vec[j].p1 == cp ) ? vec[j].p2 : vec[j].p1; Vector e1 = ( I - cp ) / abs( I - cp ); Vector e2 = ( J - cp ) / abs( J - cp ); Line tmp = calcLine(vec[i],vec[j],cp+e1*100,cp+e2*100); //cout << "tmp = " << tmp << endl; int Index = candidateLines.size(); tmp.index = Index; candidateLines.push_back(tmp); tmp = calcLine(vec[i],vec[j],cp+e1*100,cp-e2*100); //cout << "tmp = " << tmp << endl; tmp.index = Index; candidateLines.push_back(tmp); } if( candidateLines.size() >= 50 ) break; } vector<Point> candidatePoints; rep(i,candidateLines.size()) REP(j,i+1,candidateLines.size()) { Line line1 = candidateLines[i]; Line line2 = candidateLines[j]; if( equals(cross(line1.p1-line1.p2,line2.p1-line2.p2),0.0) ) continue; Point cp = crosspoint(line1,line2); candidatePoints.push_back(cp); } vector<Point> &v = candidatePoints; sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); vector<Point> answer; rep(i,candidatePoints.size()){ Point p = candidatePoints[i]; //puts(""); //cout << "p = " << p << endl; double dist = -1; bool success = true; rep(j,vec.size()){ double tmp = distanceLP(vec[j],p); if( equals(dist,-1) ) dist = tmp; else if( !equals(dist,tmp) ) { success = false; /*break;*/ } //cout << dist << " ?? " << tmp << endl; } //cout << "success ?= " << success << endl; if( success ) answer.push_back(p); if( answer.size() >= 2 ) break; } if( answer.size() == 1 ) printf("%.10f %.10f\n",answer[0].x,answer[0].y); else if( answer.empty() ) cout << NONE << endl; else cout << MANY << endl; } int main(){ /* Point p = Point(0,1); cout << rotate(p,toRad(90)) << endl; */ int n; while( cin >> n, n ){ vector<Line> vec(n); rep(i,n) cin >> vec[i].p1.x >> vec[i].p1.y >> vec[i].p2.x >> vec[i].p2.y; compute(vec); } return 0; }
0
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define EPS 1e-9 #define INF 1070000000LL #define MOD 1000000007LL #define fir first #define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++) #define ite iterator #define mp make_pair #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define pb push_back #define sec second #define sz(x) ((int)x.size()) using namespace std; struct timer{ time_t start; timer(){start=clock();} ~timer(){cerr<<1.*(clock()-start)/CLOCKS_PER_SEC<<" secs"<<endl;} }; typedef istringstream iss; typedef long long ll; typedef pair<int,int> pi; typedef stringstream sst; typedef vector<int> vi; int N; pair<ll,ll> p[300010]; int main(){ cin.tie(0); ios_base::sync_with_stdio(0); cin>>N; rep(i,N)cin>>p[i].sec>>p[i].fir; sort(p,p+N); reverse(p,p+N); ll lo=-1,hi=N+1; while(lo+1<hi){ ll M=(lo+hi)/2; multiset<ll> S; multiset<ll>::ite it; ll sum=0; rep(i,M){ S.insert(p[i].sec); sum+=p[i].sec; } int ok=0; rep(i,N-M+1){ if(sum<=p[i+M-1].fir*M){ ok=1;break; } if(i==N-M)break; S.insert(p[i+M].sec); sum+=p[i+M].sec; it=S.lower_bound(*S.rbegin()); sum-=*it; S.erase(it); } if(ok)lo=M; else hi=M; } cout<<lo<<endl; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> string toString(T x) { if (x == 0) return "0"; bool negative = x < 0; string res; while (x) { res.push_back('0' + x % 10); x /= 10; } if (negative) res.push_back('-'); reverse(res.begin(), res.end()); return res; } void ioFromFile(string inputFileName, string outputFileName) { ifstream fin(inputFileName); ofstream fout(outputFileName); cin.rdbuf(fin.rdbuf()); cout.rdbuf(fout.rdbuf()); } int read() { int res; scanf("%d", &res); return res; } void write(int x) { printf("%d ", x); } template <typename T> T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } struct MyVec { int* arr; int n; MyVec() : n(0), arr(nullptr) {} void push(int x) { int* newArr = new int[n + 1]; for (int i = 0; i < n; i++) { newArr[i] = arr[i]; } newArr[n] = x; delete[] arr; arr = newArr; n++; } }; const int N = 10000000; bool isPrime[N + 1]; bool seen[N + 1]; int numa[N + 1]; int numb[N + 1]; MyVec primeDivisors[N + 1]; int main() { memset(isPrime, true, sizeof(isPrime)); for (int i = 2; i <= N; i++) { if (!isPrime[i]) continue; for (int j = i * 2; j <= N; j += i) { isPrime[j] = false; } } int n = read(), m = read(); vector<int> a(n); for (int i = 0; i < n; i++) { int x = read(); a[i] = x; seen[x] = true; } vector<int> b(m); for (int i = 0; i < m; i++) { int x = read(); b[i] = x; seen[x] = true; } for (int i = 2; i <= N; i++) { if (!isPrime[i]) continue; for (int j = i; j <= N; j += i) { if (seen[j]) primeDivisors[j].push(i); } } for (int i = 0; i < n; i++) { int x = a[i]; const MyVec& v = primeDivisors[x]; for (int di = 0; di < v.n; di++) { int d = v.arr[di]; while (x % d == 0) { numa[d]++; x /= d; } } } for (int i = 0; i < m; i++) { int x = b[i]; const MyVec& v = primeDivisors[x]; for (int di = 0; di < v.n; di++) { int d = v.arr[di]; while (x % d == 0) { numb[d]++; x /= d; } } } for (int i = 2; i <= N; i++) { int minn = min(numa[i], numb[i]); numa[i] -= minn; numb[i] -= minn; } vector<int> resa(n); for (int i = 0; i < n; i++) { int target = a[i]; int at = 1; for (int di = 0; di < primeDivisors[target].n; di++) { int d = primeDivisors[target].arr[di]; while (numa[d] > 0 && target % (at * d) == 0) { at *= d; numa[d]--; } } resa[i] = at; } vector<int> resb(m); for (int i = 0; i < m; i++) { int target = b[i]; int at = 1; for (int di = 0; di < primeDivisors[target].n; di++) { int d = primeDivisors[target].arr[di]; while (numb[d] > 0 && target % (at * d) == 0) { at *= d; numb[d]--; } } resb[i] = at; } write(n); write(m); for (int x : resa) write(x); for (int x : resb) write(x); return 0; }
3
#include <bits/stdc++.h> #pragma optimise GCC(-O2) using namespace std; mt19937_64 rang( chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); } long long INF = LLONG_MAX; const long long M = 1000000007; long long powm(long long, long long); long long foo(long long a) { long long ct = 1; long long tp = a; while (tp > 0) { tp -= ct; ct++; } return ct; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; long long hr = 0; long long mn = 0; hr += 10 * (s[0] - '0'); hr += (s[1] - '0'); mn += 10 * (s[3] - '0'); mn += (s[4] - '0'); long long a; cin >> a; mn += a; long long ex = mn / 60; mn = mn % 60; hr += ex; hr %= 24; if (hr > 9 && mn > 9) cout << hr << ":" << mn; if (hr > 9 && mn <= 9) cout << hr << ":" << '0' << mn; if (hr <= 9 && mn > 9) cout << '0' << hr << ":" << mn; if (hr <= 9 && mn <= 9) cout << '0' << hr << ":" << '0' << mn; } long long powm(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % M; a = (a * a) % M; b >>= 1; } return res; }
2
#include <bits/stdc++.h> using namespace std; int dist[100005], visited[100005], dp[100005], cnt = 0; vector<int> graph[100005], state[100005]; void dfs(int v) { visited[v] = 2; cnt++; int temp = 100005; for (int i = 0; i < graph[v].size(); i++) { int u = graph[v][i]; if (dist[u] == dist[v] - 1) temp = min(temp, dp[u]); } for (int i = 0, j = 0; i < graph[v].size() && j < state[v].size(); i++, j++) { int u = graph[v][i], z = state[v][j]; if (visited[u] == 1) { if ((dist[u] == dist[v] - 1) && dp[u] == temp) { if (z == 0) { if (dp[u] < dp[v]) { dfs(u); break; } } else { if (dp[u] == dp[v]) { dfs(u); break; } } } } } } int main() { ios_base::sync_with_stdio(false); int n, m, u, v, z; cin >> n >> m; int a[m], b[m], c[m], a1[m], b1[m], c1[m]; for (int i = 0; i < m; i++) { cin >> u >> v >> z; a[i] = u, b[i] = v, c[i] = z; graph[u].push_back(v); graph[v].push_back(u); state[u].push_back(z); state[v].push_back(z); } int s = 1; list<int> queue; visited[1] = 1; queue.push_back(s); while (!queue.empty()) { s = queue.front(); queue.pop_front(); for (int i = 0, j = 0; i < graph[s].size() && j < state[s].size(); i++, j++) { u = graph[s][i], v = state[s][j]; if (visited[u] == 0) { visited[u] = 1; dp[u] = dp[s] + (1 - v); dist[u] = dist[s] + 1; queue.push_back(u); } else { if (dist[u] == dist[s] + 1) { dp[u] = min(dp[u], dp[s] + (1 - v)); } } } } dfs(n); int j = 0; for (int i = 0; i < m; i++) { u = a[i], v = b[i], z = c[i]; if (visited[u] == 2 && visited[v] == 2) { if (z == 0) { a1[j] = u, b1[j] = v, c1[j] = 1; j++; } } else { if (z == 1) { a1[j] = u, b1[j] = v, c1[j] = 0; j++; } } } cout << j << "\n"; for (int i = 0; i < j; i++) { cout << a1[i] << " " << b1[i] << " " << c1[i] << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; string read(char x[]) { scanf("%s", x); return x; } const int M = 2e6 + 23; int n; int x, y, a, b; void solve() { scanf("%d", &n); int ans = 1; for (int i = 0; i < n; i++) { scanf("%d %d", &a, &b); if (x == a && y == b) continue; ans += max(0, min(a, b) - max(x, y) + 1); if (x == y) --ans; x = a, y = b; } printf("%d\n", ans); } int main() { solve(); }
2
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; int t[n],T = 0; for(int i = 0;i < n;i++){ cin >> t[i]; T += t[i]; } double v[2*T+1]; for(int i = 0;i < 2*T+1;i++){ v[i] = 1000000; } int tsum = 0; for(int i = 0;i < n;i++){ double vc; cin >> vc; for(int j = tsum;j <= tsum + 2*t[i];j++){ v[j] = min(v[j],vc); } tsum += 2*t[i]; } v[0] = 0; v[2*T] = 0; for(int i = 1;i < 2*T+1;i++){ v[i] = min(v[i-1]+0.5,v[i]); } for(int i = 2*T-1;i > 0;i--){ v[i] = min(v[i+1]+0.5,v[i]); } for(int i = 0;i <= 2*T;i++){ //cout << i/2 << ' ' << v[i] << endl; } double result = 0; for(int i = 0;i < 2*T;i++){ result += (v[i] + v[i+1])*0.25; } printf("%.16f\n", result); }
0
#include<bits/stdc++.h> using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<n;i++) #define debug(n,v) cout<<#v<<":";for(int i=0;i<n;i++)cout<<v[i]<<" ";cout<<endl; #define INF 1000000000 #define mod 1000000007 typedef long long ll; const ll LINF = 1001002003004005006ll; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } //まるでわからん。 int main(){ cout<<setprecision(10); double x[3],y[3]; rep(i,3){ cin>>x[i]>>y[i]; } double s=abs((x[0]-x[2])*(y[1]-y[2])-(x[1]-x[2])*(y[0]-y[2]))*0.5; double a=(x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1]); double b=(x[1]-x[2])*(x[1]-x[2])+(y[1]-y[2])*(y[1]-y[2]); double c=(x[2]-x[0])*(x[2]-x[0])+(y[2]-y[0])*(y[2]-y[0]); a=sqrt(a);b=sqrt(b);c=sqrt(c); double r=2*s/(a+b+c); double m=max({a,b,c}); double ans=m*r/(2.0*r+m); cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, c, x, y, z, p, m, i, j = 0; cin >> n; int arr[n * 2]; for (i = 0; i < n; i++) { cin >> arr[j] >> arr[j + 1]; j = j + 2; } cin >> x; for (i = 0; i < n * 2; i++) { if (arr[i] >= x) { break; } else { continue; } } i = i / 2; p = n - i; cout << p << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") const int mod = 998244353; int N1, N2, K; int A1[4009], B1[4009], A2[4009], B2[4009], dp[77][4009]; int CA1[4009], CB1[4009], DA1[4009], DB1[4009], CA2[4009], CB2[4009], DA2[4009], DB2[4009]; int cnt_c1, cnt_d1, cnt_c2, cnt_d2; long long ans1[77], ans2[77], nr[77][77]; vector<int> G1[4009], G2[4009]; int col1[4009], col2[4009]; void init() { for (int i = 0; i <= 75; i++) { for (int j = 0; j <= 75; j++) { if (i == 0 || j == 0) nr[i][j] = 1; else nr[i][j] = (nr[i - 1][j] + nr[i][j - 1]) % mod; } } } void dfs1(int pos, int dep) { if (col1[pos] >= 0) return; col1[pos] = dep; for (int i = 0; i < G1[pos].size(); i++) dfs1(G1[pos][i], dep ^ 1); } void dfs2(int pos, int dep) { if (col2[pos] >= 0) return; col2[pos] = dep; for (int i = 0; i < G2[pos].size(); i++) dfs2(G2[pos][i], dep ^ 1); } void solve_1() { for (int t = 1; t <= N1; t++) { for (int i = 0; i <= K; i++) { for (int j = 1; j <= N1; j++) dp[i][j] = 0; } dp[0][t] = 1; if (col1[t] == 0) { for (int i = 0; i < K; i++) { if ((i & 1) == 0) { for (int j = 0; j < cnt_c1; j++) { dp[i + 1][CB1[j]] += dp[i][CA1[j]]; if (dp[i + 1][CB1[j]] >= mod) dp[i + 1][CB1[j]] -= mod; } } if ((i & 1) == 1) { for (int j = 0; j < cnt_d1; j++) { dp[i + 1][DB1[j]] += dp[i][DA1[j]]; if (dp[i + 1][DB1[j]] >= mod) dp[i + 1][DB1[j]] -= mod; } } } } else { for (int i = 0; i < K; i++) { if ((i & 1) == 1) { for (int j = 0; j < cnt_c1; j++) { dp[i + 1][CB1[j]] += dp[i][CA1[j]]; if (dp[i + 1][CB1[j]] >= mod) dp[i + 1][CB1[j]] -= mod; } } if ((i & 1) == 0) { for (int j = 0; j < cnt_d1; j++) { dp[i + 1][DB1[j]] += dp[i][DA1[j]]; if (dp[i + 1][DB1[j]] >= mod) dp[i + 1][DB1[j]] -= mod; } } } } for (int i = 0; i <= K; i++) { ans1[i] += dp[i][t]; ans1[i] %= mod; } } } void solve_2() { for (int t = 1; t <= N2; t++) { for (int i = 0; i <= K; i++) { for (int j = 1; j <= N2; j++) dp[i][j] = 0; } dp[0][t] = 1; if (col2[t] == 0) { for (int i = 0; i < K; i++) { if ((i & 1) == 0) { for (int j = 0; j < cnt_c2; j++) { dp[i + 1][CB2[j]] += dp[i][CA2[j]]; if (dp[i + 1][CB2[j]] >= mod) dp[i + 1][CB2[j]] -= mod; } } if ((i & 1) == 1) { for (int j = 0; j < cnt_d2; j++) { dp[i + 1][DB2[j]] += dp[i][DA2[j]]; if (dp[i + 1][DB2[j]] >= mod) dp[i + 1][DB2[j]] -= mod; } } } } else { for (int i = 0; i < K; i++) { if ((i & 1) == 1) { for (int j = 0; j < cnt_c2; j++) { dp[i + 1][CB2[j]] += dp[i][CA2[j]]; if (dp[i + 1][CB2[j]] >= mod) dp[i + 1][CB2[j]] -= mod; } } if ((i & 1) == 0) { for (int j = 0; j < cnt_d2; j++) { dp[i + 1][DB2[j]] += dp[i][DA2[j]]; if (dp[i + 1][DB2[j]] >= mod) dp[i + 1][DB2[j]] -= mod; } } } } for (int i = 0; i <= K; i++) { ans2[i] += dp[i][t]; ans2[i] %= mod; } } } int main() { cin >> N1 >> N2 >> K; for (int i = 1; i <= N1 - 1; i++) { cin >> A1[i] >> B1[i]; G1[A1[i]].push_back(B1[i]); G1[B1[i]].push_back(A1[i]); } for (int i = 1; i <= N2 - 1; i++) { cin >> A2[i] >> B2[i]; G2[A2[i]].push_back(B2[i]); G2[B2[i]].push_back(A2[i]); } for (int i = 1; i <= N1; i++) col1[i] = -1; dfs1(1, 0); for (int i = 1; i <= N2; i++) col2[i] = -1; dfs2(1, 0); for (int i = 1; i <= N1 - 1; i++) { if (col1[A1[i]] == 0) { CA1[cnt_c1] = A1[i]; CB1[cnt_c1] = B1[i]; cnt_c1++; } else { DA1[cnt_d1] = A1[i]; DB1[cnt_d1] = B1[i]; cnt_d1++; } } for (int i = 1; i <= N1 - 1; i++) { if (col1[B1[i]] == 0) { CA1[cnt_c1] = B1[i]; CB1[cnt_c1] = A1[i]; cnt_c1++; } else { DA1[cnt_d1] = B1[i]; DB1[cnt_d1] = A1[i]; cnt_d1++; } } for (int i = 1; i <= N2 - 1; i++) { if (col2[A2[i]] == 0) { CA2[cnt_c2] = A2[i]; CB2[cnt_c2] = B2[i]; cnt_c2++; } else { DA2[cnt_d2] = A2[i]; DB2[cnt_d2] = B2[i]; cnt_d2++; } } for (int i = 1; i <= N2 - 1; i++) { if (col2[B2[i]] == 0) { CA2[cnt_c2] = B2[i]; CB2[cnt_c2] = A2[i]; cnt_c2++; } else { DA2[cnt_d2] = B2[i]; DB2[cnt_d2] = A2[i]; cnt_d2++; } } solve_1(); solve_2(); init(); long long ans = 0; for (int i = 0; i <= K; i++) { ans += (nr[i][K - i] * ans1[i] % mod) * ans2[K - i] % mod; ans %= mod; } cout << ans << endl; return 0; }
4
#include<iostream> using namespace std; int main(){ while(1){ int H,W; cin >> H >> W; if(H== 0 && W == 0)break; for(int i=1;i<=H;++i){ for(int j =1;j<=W;++j){ if((i + j) % 2 ==0){ cout <<"#"; } else{ cout << "."; } } cout << endl; } cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace ::std; struct info { int s; int e; int id; }; bool operator<(const info &in1, const info &in2) { return ((in1.s < in2.s) || ((in1.s == in2.s) && (in1.e > in2.e)) || ((in1.s == in2.s) && (in1.e == in2.e) && (in1.id < in2.id))); } class SegT { public: int op(int x, int y) { return max(x, y); } int up(int n) { int m = 1; n--; while (n) { n >>= 1; m <<= 1; } return m; } SegT(vector<int> &v) { ID = 0; sz = up(v.size()); P = new int[2 * sz]; Q = new int[2 * sz]; for (int i = 0; i < sz; i++) { P[sz + i] = (i < v.size()) ? v[i] : ID; Q[sz + i] = (i < v.size()) ? i : -1; } for (int i = sz - 1; i > 0; i--) { if (P[2 * i] < P[2 * i + 1]) { P[i] = P[2 * i + 1]; Q[i] = Q[2 * i + 1]; } else { P[i] = P[2 * i]; Q[i] = Q[2 * i]; } } } ~SegT() { delete[] P; } int range_val(int idx, int beg, int end, int s, int e, int &res) { if (s > end || e < beg) { res = -1; return ID; } if (beg >= s && end <= e) { res = Q[idx]; return P[idx]; } int mid = (beg + end) / 2; int res1; int res2; int u = range_val(2 * idx, beg, mid, s, e, res1); int v = range_val(2 * idx + 1, mid + 1, end, s, e, res2); if (u < v) { res = res2; return v; } else { res = res1; return u; } } int range_val(int s, int e, int &res) { return range_val(1, 0, sz - 1, s, e, res); } private: int ID; int sz; int *P; int *Q; }; int main() { int n, m; scanf("%d", &n); scanf("%d", &m); vector<info> P(n); for (int i = 0; i < n; i++) { int x, y; scanf("%d", &x); scanf("%d", &y); P[i].s = x; P[i].e = y; P[i].id = 1 + i; } std::sort(P.begin(), P.end()); vector<info> Q; Q.reserve(n); Q.push_back(P[0]); for (int i = 1; i < n; i++) if (P[i].e > Q.back().e) Q.push_back(P[i]); vector<int> v(Q.size()); for (int i = 0; i < Q.size(); i++) v[i] = Q[i].e - Q[i].s; SegT segT(v); int64_t ans = 0; int opt_i = -1; int opt_j = -1; for (int j = 0; j < m; j++) { int a, b, c; scanf("%d", &a); scanf("%d", &b); scanf("%d", &c); if (a >= Q.back().e) continue; if (b <= Q.front().s) continue; int x = -1; int y = -1; if (Q.front().s >= a) x = 0; else { if (Q.back().s < a) x = Q.size(); else { int beg = 0; int end = Q.size() - 1; while ((end - beg) > 1) { int mid = (beg + end) / 2; if (Q[mid].s >= a) end = mid; else beg = mid; } x = end; } } if (Q.back().e <= b) y = Q.size() - 1; else { if (Q.front().e > b) y = -1; else { int beg = 0; int end = Q.size() - 1; while ((end - beg) > 1) { int mid = (beg + end) / 2; if (Q[mid].e <= b) beg = mid; else end = mid; } y = beg; } } if (x > 0) { int rt = min(Q[x - 1].e, b); int64_t val = (int64_t)(rt - a) * (int64_t)c; if (val > ans) { ans = val; opt_i = Q[x - 1].id; opt_j = 1 + j; } } if ((1 + y) < Q.size()) { int lt = max(Q[y + 1].s, a); int64_t val = (int64_t)(b - lt) * (int64_t)c; if (val > ans) { ans = val; opt_i = Q[y + 1].id; opt_j = 1 + j; } } if (x >= 0 && x < Q.size() && y >= 0 && y < Q.size() && x <= y) { int idx; int len = segT.range_val(x, y, idx); int64_t val = (int64_t)len * (int64_t)c; if (val > ans) { ans = val; opt_i = Q[idx].id; opt_j = 1 + j; } } } std::cout << ans << std::endl; if (ans > 0) printf("%d %d\n", opt_i, opt_j); return 0; }
1
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for (int i=0; i<(int)(n); i++) #define all(vec) vec.begin(), vec.end() int main() { int n; scanf("%d", &n); vector<int> vec(n); rep(i,n) scanf("%d", &vec.at(i)); bool OK[2]; rep(j,2) { int i=0, m=1; OK[j] = false; while (i<m) { if (i == n-1) {OK[j] = true; break;} m = max(m, i+1+vec.at(i)/10); i++; } reverse(all(vec)); } if (OK[0] && OK[1]) printf("yes\n"); else printf("no\n"); }
0
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } typedef long long int ll; #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define endl "\n" const double EPS = 1e-7; const int INF = 1 << 30; const ll LLINF = 1LL << 60; const double PI = acos(-1); const int MOD = 1000000007; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; using State = string::const_iterator; //------------------------------------- string s; char pre(char c) { if(c == '?') { return c; } else { return (c == 'A' ? 'Z' : c - 1); } } char nxt(char c) { if(c == '?') { return c; } else { return (c == 'Z' ? 'A' : c + 1); } } string Cipher(State &); string String(State &); char Letter(State &); int main() { cin.tie(0); ios::sync_with_stdio(false); while(1) { cin >> s; if(s == ".") { break; } State now = s.begin(); string ans = Cipher(now); for(int i = 0; i < ans.size(); i++) { if(ans[i] == '?') { ans[i] = 'A'; } } cout << ans << endl; } } string Cipher(State &now) { string res = String(now); while(now != s.end() && *now != ']') { res += String(now); } return res; } string String(State &now) { string res; if(*now == '[') { res = Cipher(++now); reverse(ALL(res)); now++; return res; } else { res = Letter(now); return res; } } char Letter(State &now) { char res; if(*now == '+') { return nxt(Letter(++now)); } else if(*now == '-') { return pre(Letter(++now)); } else { return *(now++); } }
0
#include <bits/stdc++.h> using namespace std; int main() { string x; int m, n, i, j, q; cin >> x; int len = x.length(); int dp[len + 1]; dp[1] = 0; for (i = 2; i <= len; i++) { if (x[i - 1] == x[i - 2]) dp[i] = 1 + dp[i - 1]; else dp[i] = dp[i - 1]; } cin >> q; while (q--) { cin >> m >> n; cout << dp[n] - dp[m] << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; int main(){ int N,C,K; cin>>N>>C>>K; std::vector<int> T(N); for(int i=0;i<N;i++)cin>>T[i]; sort(T.begin(),T.end()); int ans=0; int busgo=-1; int busfm=-1; for(int i=0;i<N;i++){ if(busgo<T[i]||i>busfm+C-1){ busgo=T[i]+K; ans++; busfm=i; } } cout<<ans<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 7; const int M = 2e6 + 7; const int lim = 2e6; const int mod = 998244353; const int inf = 0x3f3f3f3f; int a[N], num[N]; bool vis[N]; void init() { vis[1] = 1; for (register int i = 2; i <= lim; i++) if (vis[i] == 0) for (register int j = i + i; j <= lim; j += i) vis[j] = 1; } int main() { init(); int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); num[a[i]]++; } if (num[1] > 1) { int ans = 0; for (int i = 1; i <= n; i++) { if (vis[a[i] + 1] == 0 && a[i] != 1) { ans = i; break; } } printf("%d\n", num[1] + (ans > 0 ? 1 : 0)); for (int i = 1; i <= num[1]; i++) printf("1 "); if (ans > 0) printf("%d\n", a[ans]); } else { for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (vis[a[i] + a[j]] == 0) { printf("2\n%d %d\n", a[i], a[j]); return 0; } } } for (int i = 1; i <= n; i++) { if (vis[a[i]] == 0) { printf("1\n%d\n", a[i]); return 0; } } printf("1\n%d\n", a[1]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1e9 + 7; const ll INF = 1e18 + 9; ll powmod(ll base, ll exp, const ll MOD) { ll ans = 1; while (exp) { if (exp & 1) ans = (ans * base) % MOD; base = (base * base) % MOD; exp >>= 1; } return ans; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ll modInverse(ll n, const ll p) { return powmod(n, p - 2, p); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int n, m; cin >> n >> m; vector<vector<int>> a(n, vector<int>(m)); vector<int> sum(n); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { cin >> a[j][i]; sum[j] += a[j][i]; } } vector<int> ans; for (int i = 0; i < m; ++i) ans.push_back(i); for (int i = 0; i < n - 1; ++i) { vector<pair<int, int>> by; int s = 0; for (int j = 0; j < m; ++j) { s += a[n - 1][j] - a[i][j]; by.emplace_back(a[i][j] - a[n - 1][j], j); } sort(by.begin(), by.end()); for (int j = 0; j <= m; ++j) { if (s <= 0) { if (j < int(ans.size())) { ans.clear(); for (int k = 0; k < j; ++k) ans.push_back(by[k].second); } break; } s += by[j].first; } } cout << ans.size() << "\n"; for (auto x : ans) cout << x + 1 << " "; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, k; vector<pair<int, int> > nei[500005]; long long dp0[500005], dp1[500005]; void Dfs(int v, int par) { multiset<long long> mx; long long sum = 0; for (int i = 0; i < nei[v].size(); ++i) { int to = nei[v][i].first; if (to == par) continue; Dfs(to, v); sum += dp0[to]; mx.insert(max(0LL, -dp0[to] + dp1[to] + nei[v][i].second)); } dp0[v] = dp1[v] = sum; int cnt = k; for (auto it = mx.rbegin(); it != mx.rend() && cnt--; ++it) dp0[v] += *it; cnt = k - 1; for (auto it = mx.rbegin(); it != mx.rend() && cnt--; ++it) dp1[v] += *it; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int q; cin >> q; while (q--) { cin >> n >> k; for (int v = 1; v <= n; ++v) { nei[v].clear(); dp0[v] = dp1[v] = 0; } for (int i = 1; i < n; ++i) { int u, v, w; cin >> u >> v >> w; nei[u].push_back(make_pair(v, w)); nei[v].push_back(make_pair(u, w)); } Dfs(1, -1); cout << dp0[1] << '\n'; } char I; cin >> I; }
3
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 50; int n, m; int res[N]; int fa[N], fb[N]; deque<int> Sa, Sb; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < n; i++) { int a; cin >> a; fa[a]++; } for (int i = 0; i < n; i++) { int a; cin >> a; fb[a]++; } for (int i = m - 1; i >= 0; i--) { while (fb[m - 1 - i]) { Sb.push_back(m - 1 - i); fb[m - 1 - i]--; } while (fa[i]) { if (Sb.empty()) Sa.push_back(i); else { res[Sb.back() + i]++; Sb.pop_back(); } fa[i]--; } } while (!Sa.empty()) { res[(Sa.front() + Sb.back()) % m]++; Sa.pop_front(); Sb.pop_back(); } for (int i = m - 1; i >= 0; i--) { while (res[i]) { cout << i << " "; res[i]--; } } }
5
#include <bits/stdc++.h> using namespace std; int t[300005][26], st[300005], h[300005], result[300005], depth[300005]; void count_size(int v) { st[v] = 1; for (int i = 0; i < 26; i++) { int x = t[v][i]; if (x) { h[x] = h[v] + 1; count_size(x); st[v] += st[x]; } } } vector<pair<int, int> > undo; int merge(int to, int from) { int ret = 0; for (int i = 0; i < 26; i++) { if (t[from][i]) { if (t[to][i] == 0) { ret += st[t[from][i]]; t[to][i] = t[from][i]; undo.emplace_back(to, i); } else { ret += merge(t[to][i], t[from][i]); } } } return ret; } void dfs(int v) { int mx = -1, bigChild = -1; for (int i = 0; i < 26; i++) { int x = t[v][i]; if (x && st[x] > mx) mx = st[x], bigChild = x; } for (int i = 0; i < 26; i++) { int x = t[v][i]; if (x) dfs(x); } if (bigChild != -1) { result[v] = mx; for (int i = 0; i < 26; i++) { int x = t[v][i]; if (x && x != bigChild) result[v] += merge(bigChild, x); } for (pair<int, int> p : undo) t[p.first][p.second] = 0; depth[h[v]] += st[v] - result[v]; undo.clear(); } } int main() { int n, a, b; char c; scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d %c", &a, &b, &c); t[a][c - 'a'] = b; } h[1] = 1; count_size(1); dfs(1); int mx = -1, lvl = -1; for (int i = 1; i <= n; i++) if (depth[i] > mx) mx = depth[i], lvl = i; printf("%d\n%d\n", n - mx, lvl); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int N, ans = 0; scanf("%d", &N); while (N--) { ans = 0; int b[12][12] = {0}, c[6] = {0}; char a[12]; int x = 0; for (int i = 0; i < 12; i++) scanf(" %c", &a[i]); for (int i = 0; i < 12; i++) { if (a[i] == 'X') { ans++; c[x] = 1; x++; break; } } for (int i = 0; i < 6; i++) { if (a[i] == 'X' && a[i + 6] == 'X') { ans++; c[x] = 2; x++; break; } } for (int i = 0; i < 4; i++) { if (a[i] == 'X' && a[i + 4] == 'X' && a[i + 8] == 'X') { ans++; c[x] = 3; x++; break; } } for (int i = 0; i < 3; i++) { if (a[i] == 'X' && a[i + 3] == 'X' && a[i + 6] == 'X' && a[i + 9] == 'X') { ans++; c[x] = 4; x++; break; } } for (int i = 0; i < 2; i++) { if (a[i] == 'X' && a[i + 2] == 'X' && a[i + 4] == 'X' && a[i + 6] == 'X' && a[i + 8] == 'X' && a[i + 10] == 'X') { ans++; c[x] = 6; x++; break; } } int y = 0; for (int i = 0; i < 12; i++) { if (a[i] == 'X') y++; } if (y == 12) { ans++; c[x++] = 12; } if (ans != 0) printf("%d ", ans); else printf("0\n"); for (int i = 0; i < ans; i++) { if (i != ans - 1) printf("%dx%d ", c[i], 12 / c[i]); else printf("%dx%d\n", c[i], 12 / c[i]); } } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template <class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } int N, M; bitset<200013> want; long long arr[200013]; long long prob[3013][3013]; long long choose[3013][3013]; long long tot, bad, good, ibad, igood; long long expo(long long a, long long e) { if (e == 0) return 1; if (e & 1) { return expo(a * a % 998244353, e >> 1) * a % 998244353; } return expo(a * a % 998244353, e >> 1); } long long modinv(long long x) { return expo(x, 998244353 - 2); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M; for (auto i = (0); i < (N); i++) { bool b; cin >> b; want[i] = b; } for (auto i = (0); i < (N); i++) { cin >> arr[i]; tot += arr[i]; if (!want[i]) bad += arr[i]; else good += arr[i]; } ibad = modinv(bad); igood = modinv(good); prob[0][0] = 1; for (auto i = (0); i < (M); i++) { for (auto j = (0); j < (i + 1); j++) { if (prob[i][j] == 0) continue; long long p = (bad - j) * modinv(tot - j + (i - j)) % 998244353; prob[i + 1][j + 1] += prob[i][j] * p; prob[i + 1][j + 1] %= 998244353; prob[i + 1][j] += prob[i][j] * (1 + 998244353 - p); prob[i + 1][j] %= 998244353; } } for (auto i = (0); i < (M + 1); i++) { choose[i][i] = choose[i][0] = 1; for (auto j = (1); j < (i); j++) { choose[i][j] = choose[i - 1][j] + choose[i - 1][j - 1]; if (choose[i][j] >= 998244353) choose[i][j] -= 998244353; } } long long ps = 0, ns = 0; for (auto j = (1); j < (M + 1); j++) { ps += prob[M][M - j] * j % 998244353 * igood; ps %= 998244353; } for (auto j = (1); j < (M + 1); j++) { ns += prob[M][j] * j % 998244353 * ibad; ns %= 998244353; } for (auto i = (0); i < (N); i++) { long long ans; if (want[i]) { ans = (ps + 1) * arr[i] % 998244353; } else { ans = (998244353 - ns + 1) * arr[i] % 998244353; } cout << ans << '\n'; } }
3
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 55; const int M = N * N; const int Mod = 1e9 + 7; int Dp[N][N][2 * M]; void add_self(int& x, int y) { if((x += y) >= Mod) x -= Mod; } int add(int x, int y) { return add_self(x, y), x; } int mul(int x, int y) { return (ll) x * y % Mod; } int main() { int n, m; cin >> n >> m; int nn = n * n; Dp[0][0][m + M] = 1; for(int i = 1; i <= n; i++) { for(int j1 = 0; i + j1 <= n; j1++) /// empty cells after 'i' { for(int k = -nn; k <= nn; k++) /// current oddness { int j2 = j1; Dp[i][j1][k + M] = 0; add_self(Dp[i][j1][k + M], mul(j1 + j2 + 1, Dp[i - 1][j1][k + i - i + M])); if(j1 > 0 && j2 > 0 && k - i - i >= -nn) add_self(Dp[i][j1][k + M], mul(j1 * j2, Dp[i - 1][j1 - 1][k - i - i + M])); if(k + i + i <= nn) add_self(Dp[i][j1][k + M], Dp[i - 1][j1 + 1][k + i + i + M]); } } } cout << Dp[n][0][0 + M] << endl; }
0
#include <bits/stdc++.h> using namespace std; int n, m, i, j, nf, ne, ns, seq[25]; vector<double> all; double rf, re, rs, df, de, ans, sum; bool chg() { int i; for (i = 1; i + 2 <= n; i += 2) { if (seq[i] > seq[i + 1]) { sort(seq + i + 2, seq + n + 1); reverse(seq + i + 2, seq + n + 1); return 1; } } return 0; } double crs(double l, double r, double ll, double rr) { return max(0.0, min(r, rr) - max(l, ll)); } int main() { cin >> nf >> ne >> ns >> rf >> re >> rs >> df >> de; rf = sqrt((rf) * (rf)-1); re = sqrt((re) * (re)-1); rs = sqrt((rs) * (rs)-1); double st = clock(); n = nf + ne + ns; for (i = 1; i <= nf; i++) seq[i] = 1; for (i = nf + 1; i <= nf + ne; i++) seq[i] = 2; for (i = nf + ne + 1; i <= n; i++) seq[i] = 3; do { if (chg()) continue; sum = 0; for (i = 1; i <= n; i++) if (seq[i] != 3) { if (seq[i] == 1) sum += 2 * rf * df; else sum += 2 * re * de; for (j = 1; j <= n; j++) if (seq[j] == 3) { if (seq[i] == 1) sum += crs((i + 1) / 2 - rf, (i + 1) / 2 + rf, (j + 1) / 2 - rs, (j + 1) / 2 + rs) * df; else sum += crs((i + 1) / 2 - re, (i + 1) / 2 + re, (j + 1) / 2 - rs, (j + 1) / 2 + rs) * de; } } ans = max(ans, sum); if ((clock() - st) > 1480) break; } while (next_permutation(seq + 1, seq + n + 1)); printf("%.10f\n", ans); return 0; }
5
#include <bits/stdc++.h> int init(int x, int y) { return (x > y) ? (x - y) : (y - x); } int main() { int a, b, i; int x[3] = {2, 3, 5}; int cnt_a[3], cnt_b[3]; while (scanf("%d%d", &a, &b) != EOF) { cnt_a[0] = cnt_a[1] = cnt_a[2] = 0; cnt_b[0] = cnt_b[1] = cnt_b[2] = 0; for (i = 0; i < 3; i++) { while (a % x[i] == 0) { cnt_a[i]++; a /= x[i]; } } for (i = 0; i < 3; i++) { while (b % x[i] == 0) { cnt_b[i]++; b /= x[i]; } } if (a != b && (a != 1 || b != 1)) printf("-1\n"); else { int ans = 0; for (i = 0; i < 3; i++) ans += init(cnt_a[i], cnt_b[i]); printf("%d\n", ans); } } return 0; }
2
#include <iostream> using namespace std; int n; int main() { cin>>n; int p = 0, q, ans = 0; for(int i=0;i<n;i++){ cin>>q; if(q > p) ans += q - p; p = q; } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int sum = 0; for (int i = 0; i < s.size(); i++) { if ((s[i] - '1' + 1) % 2 == 0) { sum += max(0, i); sum += 1; } } cout << sum << "\n"; }
1
#include <bits//stdc++.h> using namespace std; int main() { int x,a; cin>>x>>a; if(x<a)cout<<0; else cout<<10; return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T a) { return a >= 0 ? a : -a; } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T mod(T a, T b) { T ret = a % b; if (ret < 0) ret += b; return ret; } long long mulmod(long long a, long long b, long long c) { if (b == 0LL) return 0LL; long long ret = mulmod(a, b >> 1, c); ret = (ret + ret) % c; if (b & 1LL) ret = (ret + a) % c; return ret; } long long powmod(long long a, long long b, long long c) { if (b == 0LL) return 1LL; long long ret = powmod(a, b >> 1, c); ret = ret * ret % c; if (b & 1LL) ret = ret * a % c; return ret; } template <class T> inline void wt(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + '0'); else wt(x / 10), putchar(x % 10 + '0'); } template <class T> inline void rd(T& x) { bool f = 0; char ch; for (ch = getchar(); ch <= 32; ch = getchar()) ; if (ch == '-') f = 1, ch = getchar(); for (x = 0; ch > 32; ch = getchar()) x = (x << 3) + (x << 1) + ch - '0'; if (f) x = -x; } const long long N = 45000005; int tt; int u, v, w, n, m; int main() { vector<int> a; rd(n); for (register int i = 1; i <= n; ++i) rd(tt), a.push_back(tt); wt(*max_element(a.begin(), a.end()) ^ a.back()); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 400100; int n, m, x, y, d[N]; bool a[2][N]; void update(int col, int &cnt) { if (col < 0 || col + 1 == n) { return; } if (!a[0][col] && !a[1][col]) { cnt += 1 - d[col]; d[col] = 1; return; } if (a[0][col] && a[1][col] && !a[0][col + 1] && !a[1][col + 1]) { cnt += 1 - d[col]; d[col] = 1; return; } if (a[0][col] && a[1][col]) { cnt -= d[col]; d[col] = 0; return; } if (a[0][col] && !a[0][col + 1]) { cnt += 1 - d[col]; d[col] = 1; return; } if (a[1][col] && !a[1][col + 1]) { cnt += 1 - d[col]; d[col] = 1; return; } cnt -= d[col]; d[col] = 0; } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { d[j] = 0; a[i][j] = true; } } int cnt = 0; for (int i = 0; i < m; i++) { scanf("%d %d", &x, &y); x--; y--; a[x][y] ^= true; update(y - 1, cnt); update(y, cnt); if (cnt == 0) { puts("Yes"); } else { puts("No"); } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const int mod = 1e9 + 7; const int hash_bit = 13331; int t; int n; string str; int a[maxn]; void solve() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } sort(a + 1, a + 1 + n); long long ans = 1ll * a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4]; if (1ll * a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[1] > ans) ans = 1ll * a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[1]; if (1ll * a[n] * a[n - 1] * a[n - 2] * a[1] * a[2] > ans) ans = 1ll * a[n] * a[n - 1] * a[n - 2] * a[1] * a[2]; if (1ll * a[n] * a[n - 1] * a[1] * a[2] * a[3] > ans) ans = 1ll * a[n] * a[n - 1] * a[1] * a[2] * a[3]; if (1ll * a[n] * a[1] * a[2] * a[3] * a[4] > ans) ans = 1ll * a[n] * a[1] * a[2] * a[3] * a[4]; if (1ll * a[1] * a[2] * a[3] * a[4] * a[5] > ans) ans = 1ll * a[1] * a[2] * a[3] * a[4] * a[5]; cout << ans << endl; } int main() { scanf("%d", &t); while (t--) { solve(); } }
2
#include <iostream> #include <cstdio> #include <vector> #include <string> using namespace std; char ret(int i) { if (i < 10) return '0' + i; else return 'A' + (i-10); } int main(void){ int n, m, q; char c; while (cin>>n>>m>>q && n) { vector<vector<int> > s(q, vector<int>(n)); vector<vector<int> > b(q, vector<int>(m)); for (int i = 0; i < q; i++) { for (int j = 0; j < n; j++) { cin >> c; if (i == 0) s[i][j] = c-'0'; else { if (c == '0' && s[i-1][j] == 1) s[i][j] = 1; else if (c == '1' && s[i-1][j] == 1) s[i][j] = 0; else s[i][j] = c-'0'; } } for (int j = 0; j < m; j++) { cin >> c; b[i][j] = c-'0'; } } vector<vector<bool> > x(n, vector<bool>(m, true)); for (int i = 0; i < q; i++) { for (int j = 0; j < n; j++) { bool f; s[i][j] == 1 ? f = true : f = false; for (int k = 0; k < m; k++) { if (f) { if (b[i][k] == 0) x[j][k] = false; } else { if (b[i][k] == 1) x[j][k] = false; } } } } vector<char> ans(m, 'x'); for (int i = 0; i < m; i++) { c = '.'; for (int j = 0; j < n; j++) { if (x[j][i] == 1) { if (c == '.') c = ret(j); else c = '?'; } } if (c == '.') c = '?'; ans[i] = c; } for (int i = 0; i < m; i++) cout << ans[i]; cout << endl; } }
0
#include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN=6010,P=998244353; int n,s,a[MAXN],dp[MAXN],np[MAXN]; ll ans; int main () { scanf("%d%d",&n,&s); for (int i=1;i<=n;i++) { scanf("%d",&a[i]); for (int j=s;j>=1;j--) { dp[j+a[i]]=(dp[j+a[i]]+dp[j])%P; } dp[a[i]]=(dp[a[i]]+i)%P; np[i]=dp[s]; ans=(ans+(1ll*((np[i]-np[i-1]+P)%P)*(n-i+1)))%P; } printf("%lld\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<int> seen; vector<int> color; vector<vector<int> > e; int ans = 0; void dfs(int idx) { int to = -1; seen[idx] = 1; for (int i = 0; i < (int)e[idx].size(); i++) { to = e[idx][i]; if (seen[to] && color[idx] == color[to]) { ans++; return; } } for (int i = 0; i < (int)e[idx].size(); i++) { to = e[idx][i]; if (!seen[to]) { color[to] = color[idx] ^ 1; dfs(to); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; int from, to; e.resize(n); seen.resize(n, 0); color.resize(n, -1); for (int i = 0; i < m; i++) { cin >> from >> to; from--; to--; e[from].push_back(to); e[to].push_back(from); } for (int i = 0; i < n; i++) { if (!seen[i]) { dfs(i); } } ans += (n - ans) & 1; cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") using namespace std; namespace cst { const double pi = acos(-1.0); const int MIN_INT8 = -128; const int MIN_INT16 = -32768; const int MIN_INT = -2147483647 - 1; const long long MIN_LL = -9223372036854775807LL - 1; const int MAX_INT8 = 127; const int MAX_INT16 = 32767; const int MAX_INT = 2147483647; const long long MAX_LL = 9223372036854775807LL; } // namespace cst namespace t { template <typename T> inline const T gcd(T __m, T __n) { while (__n != 0) { T __t = __m % __n; __m = __n; __n = __t; } return __m; } template <typename T> inline const T max(const T &a, const T &b) { return a > b ? a : b; } template <typename T> inline const T min(const T &a, const T &b) { return a < b ? a : b; } template <typename T> inline const T abs(const T &a) { return a > 0 ? a : -a; } template <typename T> inline void in(T &x) { register T res = 0; register int neg = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') { neg = -1; } } for (; isdigit(c); c = getchar()) { res = res * 10 + c - '0'; } x = res * neg; } inline int read() { register int res = 0, neg = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') { neg = -1; } } for (; isdigit(c); c = getchar()) { res = res * 10 + c - '0'; } return res * neg; } inline long long read_ll() { register long long res = 0, neg = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') { neg = -1; } } for (; isdigit(c); c = getchar()) { res = res * 10 + c - '0'; } return res * neg; } } // namespace t const long long mod = 1e9 + 7; const long long maxn = 1e5 + 10; long long cnt = 1, n, m; long long a[maxn]; inline void ADD(long long &x, long long y) { (x += y) >= mod && (x -= mod); } struct Matrix { long long a[2][2]; Matrix() { a[0][0] = 0; a[0][1] = 0; a[1][0] = 0; a[1][1] = 0; } inline long long *operator[](long long x) { return a[x]; } inline const long long *operator[](long long x) const { return a[x]; } inline friend Matrix operator*(Matrix x, Matrix y) { Matrix ans; for (long long i = 0; i <= cnt; ++i) for (long long j = 0; j <= cnt; ++j) for (long long k = 0; k <= cnt; ++k) ADD(ans[i][j], x[i][k] * y[k][j] % mod); return ans; } inline friend Matrix operator+(Matrix x, Matrix y) { Matrix ans; for (long long i = 0; i <= cnt; ++i) for (long long j = 0; j <= cnt; ++j) ADD(ans[i][j], x[i][j]), ADD(ans[i][j], y[i][j]); return ans; } inline friend Matrix operator^(Matrix x, long long b) { Matrix ans; ans[0][0] = 1; ans[0][1] = 0; ans[1][0] = 0; ans[1][1] = 1; for (; b; b >>= 1, x = x * x) if (b & 1) ans = ans * x; return ans; } }; Matrix fir, bas, emp; inline void init() { bas[0][0] = 1; bas[0][1] = 1; bas[1][0] = 1; bas[1][1] = 0; fir[0][0] = 1; fir[0][1] = 1; fir[1][0] = 0; fir[1][1] = 0; emp[0][0] = 0; emp[0][1] = 0; emp[1][0] = 0; emp[1][1] = 0; } struct seg { Matrix tag, num; } q[maxn * 4]; inline void init(Matrix &a) { a[0][0] = 1; a[0][1] = 0; a[1][0] = 0; a[1][1] = 1; } inline bool judge(Matrix a) { if (a[0][0] != 1) return 0; if (a[0][1] != 0) return 0; if (a[1][0] != 0) return 0; if (a[1][1] != 1) return 0; return 1; } inline void pushup(long long now) { q[now].num = q[now * 2].num + q[now * 2 + 1].num; } inline void pushdown(long long now) { if (!judge(q[now].tag)) { Matrix cur = q[now].tag; q[now * 2].num = q[now * 2].num * cur; q[now * 2 + 1].num = q[now * 2 + 1].num * cur; q[now * 2].tag = q[now * 2].tag * cur; q[now * 2 + 1].tag = q[now * 2 + 1].tag * cur; init(q[now].tag); } } void build(long long now, long long l, long long r) { init(q[now].tag); if (l == r) { init(q[now].tag); q[now].num = fir * (bas ^ (a[l] - 1)); return; } long long mid = (l + r) / 2; build(now * 2, l, mid); build(now * 2 + 1, mid + 1, r); pushup(now); } void change(long long now, long long l, long long r, long long cl, long long cr, Matrix k) { if (r < cl || l > cr) return; if (l >= cl && r <= cr) { q[now].num = q[now].num * k; q[now].tag = q[now].tag * k; return; } pushdown(now); long long mid = (l + r) / 2; change(now * 2, l, mid, cl, cr, k); change(now * 2 + 1, mid + 1, r, cl, cr, k); pushup(now); } Matrix query(long long now, long long l, long long r, long long cl, long long cr) { if (r < cl || l > cr) return emp; if (l >= cl && r <= cr) return q[now].num; pushdown(now); long long mid = (l + r) / 2; return query(now * 2, l, mid, cl, cr) + query(now * 2 + 1, mid + 1, r, cl, cr); } int main() { n = t::read_ll(), m = t::read_ll(); init(); for (long long i = 1; i <= n; ++i) { a[i] = t::read_ll(); } build(1, 1, n); for (long long i = 1, opt, l, r, x; i <= m; ++i) { t::in(opt), t::in(l), t::in(r); if (opt == 1) { t::in(x); change(1, 1, n, l, r, bas ^ x); } else printf("%lld\n", query(1, 1, n, l, r)[0][1] % mod); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, l, k, k1, kol1 = 0, ans, ans1, a, b; cin >> n >> m; char g[n + 1][m + 1]; for (int i = 1; i <= n; i++) { int kol = 0; for (int j = 1; j <= m; j++) { cin >> g[i][j]; if (g[i][j] == '*') { kol++; l = j; if (kol == 1) ans = j; if (kol == 2) ans1 = j; } } if (kol == 2) { a = ans; b = ans1; } if (kol == 1 && kol1 == 0) { kol1++; k = i; k1 = l; } } if (k1 == a) cout << k << " " << b; else cout << k << " " << a; }
1
#include <bits/stdc++.h> using namespace std; bool gcd(int a, int b, int& x) { if (a == 0) { x = b; return false; } int x1, y1; bool d = gcd(b % a, a, x1); x = (b / a); return d; } long double a[210]; int mas[210]; long double pre[210][210][410]; int N; long double go(int l, int n, int space) { if (n < 0 && l <= 0 && space >= 0) return 1.0; if (n < 0) return 0.0; if (l < 0) l = 0; if (space > N) space = N; if (pre[l][n][space + 201] != -1) return pre[l][n][space + 201]; double t; if (mas[n] == -1) { t = a[n] * go(l - 1, n - 1, space - 1) + (1.0 - a[n]) * go(l, n - 1, space); pre[l][n][space + 201] = t; return t; } else { t = a[n] * go(l - 1, n - 1, space + mas[n]) + (1.0 - a[n]) * go(l, n - 1, space); pre[l][n][space + 201] = t; return t; } } int main() { int n, l, k, i, j, p, i2; cin >> n >> l >> k; N = n; for (i = 0; i < n; i++) { cin >> p; a[i] = (long double)p / 100.0; } for (i = 0; i < n; i++) { cin >> p; mas[i] = p; } for (i = 0; i <= l; i++) for (j = 0; j < n; j++) for (i2 = 0; i2 < 410; i2++) pre[i][j][i2] = -1; long double ans = go(l, n - 1, k); printf("%.8Lf", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> void chkmin(T1 &x, T2 y) { if (x > y) x = y; } template <typename T1, typename T2> void chkmax(T1 &x, T2 y) { if (x < y) x = y; } template <typename T> void read(T &x) { x = 0; char c = getchar(); T neg = 1; while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= neg; } const int MOD = 1e9 + 7; const int M = 1 << 15; const int MAGIC = 73741817; const int MAXK = 3e4; const int MAXP = 1 << 17; const int LOG_N = 16; const long double Pi = acos(-1); int qpow(int x, long long e) { int ret = 1; for (; e; e >>= 1, x = 1ll * x * x % MOD) if (e & 1) ret = 1ll * ret * x % MOD; return ret; } struct comp { long double x, y; comp() { x = y = 0; } comp(long double _x, long double _y) { x = _x; y = _y; } comp operator+(const comp &rhs) { return comp(x + rhs.x, y + rhs.y); } comp operator-(const comp &rhs) { return comp(x - rhs.x, y - rhs.y); } comp operator*(const comp &rhs) { return comp(x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x); } } A[MAXP + 5], B[MAXP + 5], C[MAXP + 5], D[MAXP + 5]; int E[MAXP + 5], F[MAXP + 5]; int LEN = 1, LOG = 0, rev[MAXP + 5]; void FFT(comp *a, int len, int type) { int lg = log2(len); for (int i = 0; i < len; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lg - 1)); for (int i = 0; i < len; i++) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int i = 2; i <= len; i <<= 1) { comp W(cos(2 * Pi / i), type * sin(2 * Pi / i)); for (int j = 0; j < len; j += i) { comp w(1, 0); for (int k = 0; k < (i >> 1); k++, w = w * W) { comp X = a[j + k], Y = w * a[(i >> 1) + j + k]; a[j + k] = X + Y; a[(i >> 1) + j + k] = X - Y; } } } if (type == -1) for (int i = 0; i < len; i++) a[i].x = (long long)(a[i].x / len + 0.5); } void polymul(int *a, int *b, int *c, int len) { for (int i = 0; i < len; i++) A[i].x = a[i] / M, B[i].x = a[i] % M, C[i].x = b[i] / M, D[i].x = b[i] % M; FFT(A, len, 1); FFT(B, len, 1); FFT(C, len, 1); FFT(D, len, 1); for (int i = 0; i < len; i++) { comp t1 = A[i] * C[i], t2 = B[i] * D[i]; B[i] = A[i] * D[i] + B[i] * C[i]; A[i] = t1; C[i] = t2; } FFT(A, len, -1); FFT(B, len, -1); FFT(C, len, -1); for (int i = 0; i < len; i++) c[i] = ((1ll * (long long)(A[i].x) % MOD * MAGIC % MOD + 1ll * (long long)(B[i].x) % MOD * M % MOD) % MOD + (long long)(C[i].x)) % MOD; for (int i = 0; i < len; i++) A[i] = B[i] = C[i] = D[i] = comp(0, 0); } long long n; int k; int fac[MAXK + 5], ifac[MAXK + 5]; int binom(int x, int y) { return 1ll * fac[x] * ifac[y] % MOD * ifac[x - y] % MOD; } void merge(int *a, int *b, int *c, long long x) { for (int i = 0; i <= k; i++) E[i] = 1ll * a[i] * ifac[i] % MOD * qpow(2, 1ll * x % (MOD - 2) * i % (MOD - 2)) % MOD; for (int i = 0; i <= k; i++) F[i] = 1ll * b[i] * ifac[i] % MOD; polymul(E, F, E, LEN); for (int i = 0; i <= k; i++) c[i] = 1ll * E[i] * fac[i] % MOD; for (int i = 0; i < LEN; i++) E[i] = F[i] = 0; } int a[MAXP + 5], b[MAXP + 5]; int main() { scanf("%lld%d", &n, &k); fac[0] = 1; if (n > k) return puts("0"), 0; for (int i = 1; i <= k; i++) fac[i] = 1ll * fac[i - 1] * i % MOD; ifac[k] = qpow(fac[k], MOD - 2); for (int i = k - 1; ~i; i--) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % MOD; while (LEN <= k + k) LEN <<= 1; a[0] = 1; for (int i = 1; i <= k; i++) b[i] = 1; long long ca = 0, cb = 1; for (int i = 0; i <= LOG_N; i++, merge(b, b, b, cb), cb <<= 1) if (n >> i & 1) ca |= cb, merge(a, b, a, cb); int ans = 0; for (int i = 1; i <= k; i++) ans = (ans + 1ll * a[i] * binom(k, i) % MOD) % MOD; printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> int main(void) { int i, j, ct, moves, tmp, p1, p2, p3, arr[1001], ans1[100000], ans2[100000]; scanf("%d", &ct); for (i = 1; i <= ct; scanf("%d", arr + i++)) ; for (p2 = 1, p3 = 2, i = 3; i <= ct; ++i) for (p1 = i;;) { if (arr[p1] > arr[p2]) { tmp = p1; p1 = p2; p2 = tmp; } if (arr[p2] > arr[p3]) { tmp = p2; p2 = p3; p3 = tmp; } if (arr[p1] > arr[p2]) { tmp = p1; p1 = p2; p2 = tmp; } if (arr[p1] == 0) { break; } for (j = arr[p2] / arr[p1]; j; j >>= 1) if (j & 1) { ans1[++moves] = p1; ans2[moves] = p2; arr[p2] -= arr[p1]; arr[p1] <<= 1; } else { ans1[++moves] = p1; ans2[moves] = p3; arr[p3] -= arr[p1]; arr[p1] <<= 1; } } if ((arr[p1] == 0) + (arr[p2] == 0) + (arr[p3] == 0) != 1) { puts("-1"); } else { for (printf("%d\n", moves), i = 1; i <= moves; ++i) { printf("%d %d\n", ans1[i], ans2[i]); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 401; int a[N], dp[N - 2][N][N]; int n, m, s, f, c, r; int main() { scanf("%d%d", &n, &m); for (int i = (1); i <= (n); ++i) scanf("%d", a + i); for (int s = (1); s <= (n); ++s) for (int f = (s); f <= (n); ++f) dp[0][s][f] = a[f] - a[s]; for (int r = (1); r <= (n - 2); ++r) for (int s = (1); s <= (n - r - 1); ++s) { int j = s + r; for (int f = (s + r + 1); f <= (n); ++f) { for (; j < f && max(dp[r - 1][s][j], a[f] - a[j]) > max(dp[r - 1][s][j + 1], a[f] - a[j + 1]); ++j) ; dp[r][s][f] = max(dp[r - 1][s][j], a[f] - a[j]); } } long long ans = 0; for (int i = (1); i <= (m); ++i) { scanf("%d%d%d%d", &s, &f, &c, &r); ans = max(ans, 1ll * dp[min(r, f - s - 1)][s][f] * c); } cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; vector<long long> vec; set<long long> set2; int main() { int n; long long k; cin >> n >> k; vec.resize(n); for (int i = 0; i < (int)n; i++) cin >> vec[i]; sort(vec.begin(), vec.end()); reverse(vec.begin(), vec.end()); set2.clear(); for (int i = 0; i < (int)vec.size(); i++) { if (set2.find(vec[i] * k) == set2.end()) set2.insert(vec[i]); } cout << set2.size() << endl; }
1
#include <bits/stdc++.h> struct point { double x, y; int id; inline bool operator<(const point& rhs) const { return (x != rhs.x) ? (x < rhs.x) : (y < rhs.y); } } q[200005], p[200005]; int n, m; int sta[200005], top; std::vector<int> v, id[200005]; inline double slope(int a, int b) { return ((p[a].y - p[b].y) / (p[a].x - p[b].x)); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { int a, b; scanf("%d%d", &a, &b); q[i] = (point){1. / a, 1. / b, i}; } std::sort(q + 1, q + n + 1); p[1] = q[1]; m = 1; for (int i = 2; i <= n; ++i) if (fabs(q[i].x - q[i - 1].x) > 1e-9) p[++m] = q[i]; else if (fabs(q[i].y - p[m].y) <= 1e-9) id[p[m].id].push_back(q[i].id); if (m == 1) { v.push_back(p[1].id); for (int i : id[p[1].id]) v.push_back(i); std::sort(v.begin(), v.end()); for (int i : v) printf("%d ", i); return 0; } sta[1] = 1, sta[2] = 2; top = 2; for (int i = 3; i <= m; ++i) { while (top > 1 && slope(sta[top - 1], sta[top]) > slope(sta[top - 1], i)) --top; sta[++top] = i; } v.push_back(p[sta[1]].id); for (int i : id[p[sta[1]].id]) v.push_back(i); for (int i = 2; i <= top; ++i) if (slope(sta[i - 1], sta[i]) < 0) { v.push_back(p[sta[i]].id); for (int j : id[p[sta[i]].id]) v.push_back(j); } else break; std::sort(v.begin(), v.end()); for (int i : v) printf("%d ", i); return 0; }
5
#include <iostream> using namespace std; int main(){ int n; while(cin >> n,n){ int i,aaa,ans=0; for(i=0;i<(n/4);i++){ cin >> aaa; ans += aaa; } cout << ans << endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; vector <int> adj[51]; int vi[51]; void dfs(int u,int x,int y) { vi[u]=1; for (int j=0;j<adj[u].size();j++) { if (!vi[adj[u][j]]) { if (u==x&&adj[u][j]==y) continue; dfs(adj[u][j],x,y); } } } int main() { int n,m; cin>>n>>m; int ans=0; memset(vi,0,sizeof(vi)); pair<int,int> a[m+1]; for (int i=1;i<=m;i++) { int x,y; cin>>x>>y; adj[x].push_back(y); adj[y].push_back(x); a[i].first=x; a[i].second=y; } for (int i=1;i<=m;i++) { int x=a[i].first; int y=a[i].second; memset(vi,0,sizeof(vi)); dfs(x,x,y); if (!vi[y]) ans++; } cout<<ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; inline long long IN() { long long x = 0, ch = getchar(), f = 1; while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar(); if (ch == '-') { f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } inline void Out(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) Out(x / 10), putchar(x % 10 + '0'); else putchar(x + '0'); } const int N = 5000005; long long P, n, m, a[N], b[N]; long long Solve() { long long MC = P * (P - 1) / 2 * n, FD = min(m * P - MC, a[n]), ret = 0; for (int i = 1; i <= n; i++) FD = min(FD, a[i] - i * P + m); long long now = FD; for (int i = n; i >= 1; i--) { if (i != n) now -= P; now = min(now, a[i]); if (now < MC + P || FD - now > m - P) return -1; ret += now; } return ret; } int main() { n = IN(), m = IN(); for (int i = 1; i <= n; i++) a[i] = IN(); long long Ans = 0; for (P = 1; P * n <= m; P++) Ans = max(Ans, Solve()); Out(Ans); }
5
#include <bits/stdc++.h> using namespace std; int const lim = 3e5 + 1; int const MLOG = 20, mod = 1e9 + 7, bas = 47; string s[lim + 3]; vector<int> adj[lim + 3]; int st[4 * lim + 3], poww[lim + 3]; void update(int i, int j, int index, int ind, int val) { if (i > ind || j < ind) return; if (i == j && i == ind) { st[index] = val; return; } int mid = (i + j) / 2; update(i, mid, index * 2 + 1, ind, val); update(mid + 1, j, index * 2 + 2, ind, val); st[index] = (0ll + st[index * 2 + 1] + ((1ll * st[index * 2 + 2] * poww[mid - i + 1]) % mod)) % mod; } int get(int i, int j, int index, int l, int r) { if (i > r || j < l) return 0; if (i >= l && j <= r) return st[index]; int mid = (i + j) / 2; int val1 = get(i, mid, index * 2 + 1, l, r); int val2 = get(mid + 1, j, index * 2 + 2, l, r); if (l > mid) return val2; if (r < mid + 1) return val1; int mn = min(mid - i + 1, mid - l + 1); return (0ll + val1 + ((1ll * val2 * poww[mn]) % mod)) % mod; } long long hashing = 0, ans = 0, sz = 0; void dfs(int u, int p, int ind) { for (int i = 0; i < adj[u].size(); i++) { int che = adj[u][i]; int tem = ind; for (int j = 0; j < s[che].size(); j++) { update(0, lim, 0, tem, s[che][j] - 'a' + 1); int val = 0; if (tem + 1 >= sz) val = get(0, lim, 0, tem - sz + 1, tem); if (val == hashing) ans++; tem++; } dfs(che, u, tem); } } int main() { poww[0] = 1; for (int i = 1; i < lim; i++) poww[i] = (1ll * poww[i - 1] * bas) % mod; int n; cin >> n; for (int i = 2; i <= n; i++) { int u; cin >> u >> s[i]; adj[u].push_back(i); } string inp; cin >> inp; sz = inp.size(); for (int i = 0; i < inp.size(); i++) { hashing += (1ll * (inp[i] - 'a' + 1) * poww[i]) % mod; hashing %= mod; } dfs(1, -1, 0); cout << ans << endl; }
5