solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main() { int q; string h = "http"; string f = "ftp"; string r = "ru"; string s; string b = ""; cin >> s; if (s[0] == 'h') { b += "http://"; if (s[4] == 'r' && s[5] == 'u') { b += s[4]; b += s[5]; for (int i = 6; i < s.size() - 1; i++) if (s[i] == 'r' && s[i + 1] == 'u') { b += "."; q = i; b += s[i]; b += s[i + 1]; break; } else b += s[i]; } else for (int i = 4; i < s.size() - 1; i++) if (s[i] == 'r' && s[i + 1] == 'u') { b += "."; q = i; b += s[i]; b += s[i + 1]; break; } else b += s[i]; } else { b += "ftp://"; if (s[3] == 'r' && s[4] == 'u') { b += s[3]; b += s[4]; for (int i = 5; i < s.size() - 1; i++) if (s[i] == 'r' && s[i + 1] == 'u') { b += "."; b += s[i]; b += s[i + 1]; q = i; break; } else b += s[i]; } else for (int i = 3; i < s.size() - 1; i++) if (s[i] == 'r' && s[i + 1] == 'u') { b += "."; b += s[i]; b += s[i + 1]; q = i; break; } else b += s[i]; } if (b.size() == s.size() + 4) { cout << b; return 0; } else { b += "/"; for (int i = q + 2; i < s.size(); i++) b += s[i]; } cout << endl << b; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long n, r, c1 = 1, sw = 0, i, j = 0; long long a[101000], b[101000]; cin >> n; for (i = 0; i < n; ++i) { cin >> a[i]; if (i > 0 && a[i] == a[i - 1]) c1++; else if (i > 0) { b[j] = c1; j++; c1 = 1; } } b[j] = c1; if (j == 0) { cout << "YES"; } else { for (i = 1; i <= j; ++i) { if (b[i] != b[i - 1]) sw = 1; } if (sw == 0) cout << "YES"; else cout << "NO"; } }
3
#include <bits/stdc++.h> using namespace std; const long long mod=1000000007; long long pw(long long x,int y){ long long a=1; while(y){ if(y&1)a=a*x%mod; x=x*x%mod; y/=2; } return a; } int N; int main(){ cin>>N; cout<<(pw(10,N)-(mod+pw(9,N)*2-pw(8,N))%mod+mod)%mod<<endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; cin >> n; map<int, int> home_color_count; map<int, int> away_color_count; map<int, vector<int>> team_to_color; for (int i = 0; i < n; i++) { int x = 0, y = 0; cin >> x >> y; team_to_color[i].push_back(x); team_to_color[i].push_back(y); home_color_count[x] += 1; away_color_count[y] += 1; } int ans = 0; for (auto item : team_to_color) { int home_color = item.second[0]; int away_color = item.second[1]; ans += home_color_count[away_color]; } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; using ll=long long; const ll MOD=1000000007LL; vector<ll> f,fi; ll mpow(ll x,ll n){ ll res=1; while(n>0){ if(n&1){ res*=x; res%=MOD; } x=x*x%MOD; n>>=1; } return res; } void init(int n){ f.resize(n+1); fi.resize(n+1); f[0]=1; for(ll i=1;i<=n;i++){ f[i]=f[i-1]*i; f[i]%=MOD; } fi[n]=mpow(f[n],MOD-2); for(ll i=n-1;i>=0;i--){ fi[i]=fi[i+1]*(i+1LL); fi[i]%=MOD; } } ll ncr(ll n,ll r){ if(n<r){ return 0; } if(r==0){ return 1; } return f[n]*fi[r]%MOD*fi[n-r]%MOD; } int main(){ ll n,A,B,C;cin>>n>>A>>B>>C; if(B%2){ cout<<0<<endl; return 0; } init(3*n+10); ll ans=0; for(ll b=0;b<=n/2;b++){ for(ll d=0;d<=n/3;d++){ ll c=B/2; ll a=A-b; ll x=C-b-3*d; if(x<0||a<0){ continue; } // cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<x<<endl; ll sum=a+b+c+d; ll s=ncr(sum,a)*ncr(sum-a,b)%MOD*ncr(sum-a-b,c)%MOD; if(c>0){ s*=ncr(x+c-1,x); s%=MOD; }else if(x>0){ s=0; } ans+=s; ans%=MOD; } } cout<<ans<<endl; }
0
#include <iostream> #include <cstdio> #include <cstring> #include <cassert> typedef long long lint; const int N=20; const int mod=1e9+7; int n,m,q,hbit[1<<N]; int limit[N+1][N+1],dp[2][1<<N]; template <class T> inline void apadd(T &x,const T y){ x+y<mod?x=x+y:x=x+y-mod; } inline int nxi(){ int x=0; char c; while((c=getchar())>'9'||c<'0'); while(x=x*10-48+c,(c=getchar())>='0'&&c<='9'); return x; } void init(){ for(int i=2; i<1<<n; ++i){ hbit[i]=hbit[i>>1]+1; } } int main(){ #ifdef LOCAL //freopen("d.in","r",stdin); #endif memset(limit,-1,sizeof(limit)); n=nxi(),m=nxi(),q=nxi(); --n; init(); for(int i=1; i<=q; ++i){ const int a=nxi(),b=nxi(),c=nxi(); limit[a][b]=c; } dp[0][0]=1; for(int i=1; i<=m; ++i){ for(int j=0; j<n; ++j){ int p=j&1; memset(dp[p^1],0,sizeof(dp[p^1])); for(int k=0; k<1<<n; ++k){ if(!dp[p][k]) continue; if((k>>j&1)==0&&limit[i][j+1]!=1){ //apadd(dp[p^1][k],dp[p][k]); dp[p^1][k]=(dp[p^1][k]+dp[p][k])%mod; } if(limit[i][j+1]!=0){ int lbit=((k>>j)&-(k>>j))<<j; int tgt=k^(1<<j)^lbit; dp[p^1][tgt]=(dp[p^1][tgt]+dp[p][k])%mod; } } } if(n&1){ memcpy(dp[0],dp[1],sizeof(dp[1])); } } lint ans=0; for(int i=0; i<1<<n; ++i){ ans+=dp[0][i]; } printf("%lld\n",ans%mod); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int T; cin >> T; while (T--) { int n; cin >> n; int i; vector<long long int> v; for (i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } sort(v.begin(), v.end()); long long int sum = 0, count = 0, rem = 0; for (i = 0; i < n; i++) { rem++; if (rem >= v[i]) { long long int temp = rem / v[i]; count = count + (temp); rem -= temp * v[i]; } } cout << count << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; char a[200001], b[200001]; long long ss[2][200001]; int main() { gets(a); gets(b); int s = strlen(b); for (int i = 0; i < s; i++) { int x = b[i] - 48; ss[x][i + 1] = ss[x][i] + 1; ss[(x + 1) % 2][i + 1] = ss[(x + 1) % 2][i]; } int s2 = s; s = strlen(a); long long res = 0; for (int i = 0; i < s; i++) { int x = a[i] - 48, y = s - i; res += ss[(x + 1) % 2][s2 - y + 1] - ss[(x + 1) % 2][i]; } printf("%I64d", res); }
2
#include <bits/stdc++.h> using namespace std; string s; char a[1001][1001] = {0}; int t = 0; int mx = 0; int openb(int v, int fl = false) { a[mx - v][t] = '+'; a[mx - v][t + 1] = '-'; for (int i = mx - v + 1; i <= mx - v + 1 + 2 * v - 1 - 1; i++) { a[i][t] = '|'; } a[mx - v + 1 + 2 * v - 1][t] = '+'; a[mx - v + 1 + 2 * v - 1][t + 1] = '-'; t++; if (fl) { t += 3; } return 0; } int closeb(int v) { a[mx - v][t] = '+'; a[mx - v][t - 1] = '-'; for (int i = mx - v + 1; i <= mx - v + 1 + 2 * v - 1 - 1; i++) { a[i][t] = '|'; } a[mx - v + 1 + 2 * v - 1][t] = '+'; a[mx - v + 1 + 2 * v - 1][t - 1] = '-'; t++; return 0; } int main() { int i, j, k, x, y, z, n, m, l, fl; cin >> n >> s; k = l = 0; for (i = 0; i < n; i++) { if (s[i] == '[') { k++; l = max(k, l); } if (s[i] == ']') { k--; } } k = l; mx = l; for (i = 0; i < n; i++) { if (s[i] == '[') { fl = false; if (s[i + 1] == ']') { fl = true; } openb(k, fl); k--; } if (s[i] == ']') { k++; closeb(k); } } for (i = 0; i <= 2 * mx; i++) { for (j = 0; j < t; j++) { if (a[i][j]) { cout << a[i][j]; } else { cout << " "; } } cout << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m; struct wall { int l, r, t; } w[100005]; int x[200005], xtot = 0; struct node { int x, i; } t[200005]; int ttot = 0; int cmp(const node& a, const node& b) { if (a.i == b.i) return a.x > b.x; return a.i < b.i; } int mi[200005]; multiset<int> se; node r[500005]; int rtot = 0; int main() { scanf("%d%d", &m, &n); x[xtot++] = 0; for (int i = 1; i <= n; i++) scanf("%d%d%d", &w[i].l, &w[i].r, &w[i].t), x[xtot++] = w[i].l, x[xtot++] = w[i].r; sort(x, x + xtot); xtot = unique(x, x + xtot) - x - 1; for (int i = 1; i <= n; i++) { int l = lower_bound(x, x + 1 + xtot, w[i].l) - x; int r = lower_bound(x, x + 1 + xtot, w[i].r) - x; t[++ttot] = (node){w[i].t, l}; t[++ttot] = (node){-w[i].t, r}; } sort(t + 1, t + 1 + ttot, cmp); for (int i = 0, cur = 1; i < xtot; i++) { while (cur <= ttot && t[cur].i == i) { if (t[cur].x < 0) { multiset<int>::iterator it = se.find(-t[cur].x); se.erase(it); } else { se.insert(t[cur].x); } cur++; } if (!se.empty()) mi[i + 1] = *se.begin(); else mi[i + 1] = -1; } for (int i = 1; i <= m; i++) { int q; scanf("%d", &q); r[++rtot] = (node){-2, q}; } for (int i = 1; i <= xtot; i++) if (~mi[i]) { r[++rtot] = (node){1, mi[i] - x[i]}; r[++rtot] = (node){-1, mi[i] - x[i - 1]}; } sort(r + 1, r + 1 + rtot, cmp); int cur = 0, ans = 0; for (int i = 1; i <= rtot; i++) { ans += cur * (r[i].i - r[i - 1].i); if (r[i].x == -2) printf("%d\n", ans); else cur += r[i].x; } return 0; }
4
#include <bits/stdc++.h> int main() { long long a, b; scanf("%lld %lld", &a, &b); m1: if (a <= 0 || b <= 0) goto end; if (a >= 2 * b) { a = a - 2 * b; if (a / 2 / b > 2) a = a - (a / 2 / b - 1) * b * 2; goto m1; } if (b >= 2 * a) { b = b - 2 * a; if (b / 2 / a > 2) b = b - (b / 2 / a - 1) * a * 2; goto m1; } else goto end; end: printf("%lld %lld\n", a, b); return 0; }
2
#include <bits/stdc++.h> using namespace std; int a[222222]; int n, k, def, j; long long l, di; int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%d", a + i); } def = 0; for (int i = 0; i < n; ++i) { j = i + def; di = l - (long long)j * (n - (i + 1)) * a[i]; if (di < k) { printf("%d\n", i + 1); --def; } else { l += a[i] * j; } } return 0; }
3
#include<iostream> using namespace std; char a[105][105]; int n, m; void dfs(int i,int j) { char s = a[i][j]; a[i][j] = '.'; if (a[i + 1][j] == s&&i + 1 < n) dfs(i + 1, j); if (a[i][j + 1] == s&&j + 1 < m) dfs(i, j + 1); if (a[i][j - 1] == s&&j - 1 >= 0) dfs(i, j - 1); if (a[i - 1][j] == s&&i - 1 >= 0) dfs(i - 1, j); } int main(void) { while (cin >> n >> m) { int temp = 0; if (!n && !m) break; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (a[i][j] != '.') { dfs(i, j); temp++; } cout << temp << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int p[n]; for (long long int i = 0; i < n; i++) cin >> p[i]; long long int k = n / 2; if (k < 5) { cout << "0 0 0" << endl; continue; } long long int g = 1, z = 1; for (long long int i = 1; i < k; i++) { if (p[i] == p[0]) { g++; z = i + 1; } } if (g > k / 3) { cout << "0 0 0" << endl; continue; } long long int s = 1, x = z + 1; for (long long int l = z + 1; l < k; l++) { if (p[l - 1] == p[l] || s <= g) { s++; x++; } else break; } long long int ss, co = 0; if (p[k - 1] == p[k]) { for (long long int j = k - 1; j >= x; j--) if (p[j] == p[k]) co++; } long long int b = k - x - co; if (b <= g) { cout << "0 0 0" << endl; continue; } cout << g << " " << s << " " << b << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; inline void OPEN(const string &s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } int main() { string a, b; cin >> a >> b; int n = a.length() / 2; int na = 0, nb = 0; for (int i = 0; i < (n); ++i) { int actiona, actionb; if (a[2 * i] == '[' && a[2 * i + 1] == ']') { actiona = 3; } if (a[2 * i] == '(' && a[2 * i + 1] == ')') { actiona = 1; } if (a[2 * i] == '8' && a[2 * i + 1] == '<') { actiona = 2; } if (b[2 * i] == '[' && b[2 * i + 1] == ']') { actionb = 3; } if (b[2 * i] == '(' && b[2 * i + 1] == ')') { actionb = 1; } if (b[2 * i] == '8' && b[2 * i + 1] == '<') { actionb = 2; } if ((actiona == 1 && actionb == 2) || (actiona == 2 && actionb == 3) || (actiona == 3 && actionb == 1)) { na++; } else if ((actionb == 1 && actiona == 2) || (actionb == 2 && actiona == 3) || (actionb == 3 && actiona == 1)) { nb++; } } if (na > nb) puts("TEAM 1 WINS"); if (na < nb) puts("TEAM 2 WINS"); if (na == nb) puts("TIE"); return 0; }
1
#include <bits/stdc++.h> using namespace std; string valid, str; char out[102]; int n, K, tab[102][26]; bool vis[102][102]; bool solve(int k, int match) { int sz = str.size(); if (match == sz && valid[k - sz] == '0') return false; if (match < sz && k >= sz && valid[k - sz] == '1') return false; if (k == n) return true; if (vis[k][match]) return false; vis[k][match] = true; for (int i = 0; i < K; i++) { out[k] = 'a' + i; if (solve(k + 1, tab[match][i])) return true; } return false; } int main() { cin >> n >> K; cin >> str >> valid; int sz = str.size(); for (int i = 0; i <= sz; i++) for (int j = 0; j < K; j++) { string have = str.substr(0, i); have += (char)('a' + j); for (int k = min(sz, i + 1); k >= 0; k--) if (have.substr(i + 1 - k) == str.substr(0, k)) { tab[i][j] = k; break; } } bool ret = solve(0, 0); if (!ret) cout << "No solution" << endl; else cout << out << endl; return 0; }
4
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define ll long long inline int read() { int x;char c; while((c=getchar())<'0'||c>'9'); for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0'; return x; } #define MN 100000 #define ND 2000000 void push(int); int cxn,cyn,u[MN+5]; ll ans,R,x[MN+5],y[MN+5],cx[MN+5],cy[MN+5]; struct segtree { struct node{int l,r,s,u;}t[ND+5]; int rt[MN+5],tn; void ins(int&k,int l,int r,int x,int z) { if(!k)k=++tn; ++t[k].s;t[k].u|=z; if(l==r)return; int mid=l+r>>1; if(x<=mid)ins(t[k].l,l,mid,x,z); else ins(t[k].r,mid+1,r,x,z); } void solve(int k,int l,int r) { if(!t[k].u)return; if(l==r&&!u[t[k].u])push(t[k].u); t[k].u=0; int mid=l+r>>1; if(l<r)solve(t[k].l,l,mid),solve(t[k].r,mid+1,r); } int query(int k,int l,int r,int ql,int qr) { if(l==ql&&r==qr){solve(k,l,r);return t[k].s;} int mid=l+r>>1,res; if(qr<=mid)res=query(t[k].l,l,mid,ql,qr); else if(ql>mid)res=query(t[k].r,mid+1,r,ql,qr); else res=query(t[k].l,l,mid,ql,mid)+query(t[k].r,mid+1,r,mid+1,qr); t[k].u=t[t[k].l].u|t[t[k].r].u; return res; } }A,B; inline int getx(ll x){return lower_bound(cx+1,cx+cxn+1,x)-cx;} inline int gety(ll y){return lower_bound(cy+1,cy+cyn+1,y)-cy;} void push(int v) { int xl,xr,yl,yr;u[v]=1; xl=getx(x[v]-R);xr=getx(x[v]+R); yl=gety(y[v]-R);yr=gety(y[v]+R+1)-1; if(xl<=cxn&&cx[xl]==x[v]-R&&yl<=yr)ans+=A.query(A.rt[xl],1,cyn,yl,yr); if(xr<=cxn&&cx[xr]==x[v]+R&&yl<=yr)ans+=A.query(A.rt[xr],1,cyn,yl,yr); if(xl<=cxn&&cx[xl]==x[v]-R)++xl;--xr; if(yl<=cyn&&cy[yl]==y[v]-R&&xl<=xr)ans+=B.query(B.rt[yl],1,cxn,xl,xr); if(yr<=cyn&&cy[yr]==y[v]+R&&xl<=xr)ans+=B.query(B.rt[yr],1,cxn,xl,xr); } int main() { int n,a,b,i,X,Y; n=read();a=read();b=read(); for(i=1;i<=n;++i) { x[i]=read();y[i]=read(); cx[i]=x[i]+=y[i];cy[i]=y[i]=x[i]-y[i]-y[i]; } R=max(abs(x[a]-x[b]),abs(y[a]-y[b])); sort(cx+1,cx+n+1);cxn=unique(cx+1,cx+n+1)-cx-1; sort(cy+1,cy+n+1);cyn=unique(cy+1,cy+n+1)-cy-1; for(i=1;i<=n;++i) { X=getx(x[i]);Y=gety(y[i]); A.ins(A.rt[X],1,cyn,Y,i); B.ins(B.rt[Y],1,cxn,X,i); } push(a); cout<<ans/2; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int two, three, five, six, thirtytwo = 0; cin >> two >> three >> five >> six; long long int use = min(min(two, five), six); long long int twofiftysix = 256 * use; if (two - use > 0) { long long int use2 = min(two - use, three); thirtytwo = 32 * use2; } cout << twofiftysix + thirtytwo << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<int> G[105000]; bool vis[105000]; int mark[105000]; int sta[105000], top; void dfs(int u, int fa) { vis[u] = true; int sz = G[u].size(); for (int i = 0; i < sz; i++) { int v = G[u][i]; if (!vis[v] && v != fa) dfs(v, u); } top = 0; for (int i = 0; i < sz; i++) { int v = G[u][i]; if (v != fa && mark[v] != 1) sta[top++] = v; } if (top & 1) sta[top++] = fa, mark[u] = 1; else mark[u] = 2; for (int i = 0; i < top; i += 2) { printf("%d %d %d\n", sta[i], u, sta[i + 1]); if (mark[sta[i]] == 2) mark[sta[i]] = 1; if (mark[sta[i + 1]] == 2) mark[sta[i + 1]] = 1; } } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); G[u].push_back(v); G[v].push_back(u); } if (m & 1) printf("No solution"); else { memset(vis, 0, sizeof(vis)); memset(mark, 0, sizeof(mark)); dfs(1, -1); } }
3
#include <bits/stdc++.h> using namespace std; using namespace std; int main() { string s; cin >> s; int pos_r = s.length() - 1; int pos_l = 0; string mid = ""; string ans; while (pos_r - pos_l >= 3) { bool fl = false; for (int i = pos_l; i <= pos_l + 1; i++) for (int j = pos_r - 1; j <= pos_r; j++) { if (s[i] == s[j] && !fl) { pos_l = i + 1; pos_r = j - 1; ans += s[i]; fl = true; } } } if (pos_r >= pos_l) mid = s[pos_r]; cout << ans << mid; reverse(ans.begin(), ans.end()); cout << ans; }
5
#include <stdio.h> int main(void) { char a[10]; scanf("%10c",a); printf("2018/01/%c%c",a[8],a[9]); return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; pair<long long, long long> st[1600005]; pair<long long, long long> lazy[1600005]; long long bm[305], inv[64]; int datas[400005]; vector<int> prime; pair<long long, long long> merge(pair<long long, long long> X, pair<long long, long long> Y) { X.first *= Y.first; X.first %= MOD; X.second |= Y.second; return X; } long long fexpo(int p, long long base) { if (p == 0) return 1; if (p == 1) return base % MOD; if (p % 2 == 1) return (fexpo(p - 1, base) * (base % MOD)) % MOD; long long h = fexpo(p / 2, base); return (h * h) % MOD; } void build(int pos, int L, int R) { if (L == R) { st[pos].first = datas[L]; st[pos].second = bm[datas[L]]; return; } int mid = (L + R) / 2; build(pos * 2, L, mid); build(pos * 2 + 1, mid + 1, R); st[pos] = merge(st[pos * 2], st[pos * 2 + 1]); } void relax(int &pos, int &L, int &R) { st[pos].first *= fexpo((R - L + 1), lazy[pos].first % MOD); st[pos].first %= MOD; st[pos].second |= lazy[pos].second; if (L != R) { lazy[pos * 2].first *= lazy[pos].first; lazy[pos * 2].first %= MOD; lazy[pos * 2].second |= lazy[pos].second; lazy[pos * 2 + 1].first *= lazy[pos].first; lazy[pos * 2 + 1].first %= MOD; lazy[pos * 2 + 1].second |= lazy[pos].second; } lazy[pos] = pair<long long, long long>(1, 0); } pair<long long, long long> query(int pos, int L, int R, int S, int E) { if (lazy[pos].second > 0) { relax(pos, L, R); } if (L > E || R < S) return pair<long long, long long>(1, 0); if (L >= S && R <= E) return st[pos]; int mid = (L + R) / 2; return merge(query(pos * 2, L, mid, S, E), query(pos * 2 + 1, mid + 1, R, S, E)); } void upd(int pos, int L, int R, int S, int E, pair<long long, long long> val) { if (lazy[pos].second > 0) { relax(pos, L, R); } if (L > E || R < S) return; if (L >= S && R <= E) { lazy[pos] = val; relax(pos, L, R); return; } int mid = (L + R) / 2; upd(pos * 2, L, mid, S, E, val); upd(pos * 2 + 1, mid + 1, R, S, E, val); st[pos] = merge(st[pos * 2], st[pos * 2 + 1]); } string pbm(long long inp) { string ret = ""; while (inp > 0) { if (inp % 2 == 1) ret.push_back('1'); else ret.push_back('0'); inp /= 2; } for (int a = 0; a < ret.size() / 2; a++) { swap(ret[a], ret[ret.size() - 1 - a]); } return ret; } long long hasil(pair<long long, long long> inp) { long long ret = 1; int ix = 0; while (inp.second > 0) { if (inp.second % 2 == 1) { ret *= inv[ix]; ret %= MOD; } ix++; inp.second /= 2; } ret *= inp.first; ret %= MOD; return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); for (int a = 0; a <= 1600000; a++) { lazy[a].first = 1; } for (int a = 2; a <= 300; a++) { bool check = 0; for (int b = 2; b < a; b++) { if (a % b == 0) check = 1; } if (!check) prime.push_back(a); } for (int a = 2; a <= 300; a++) { for (int b = 0; b < prime.size(); b++) { if (a % prime[b] == 0) { bm[a] |= (1LL << b); } } } for (int a = 0; a < prime.size(); a++) { inv[a] = fexpo(MOD - 2, prime[a]); inv[a] = ((1 - inv[a]) % MOD + MOD) % MOD; } int n, q; cin >> n >> q; for (int a = 1; a <= n; a++) { cin >> datas[a]; } build(1, 1, n); string S; int L, R, val; for (int a = 0; a < q; a++) { cin >> S >> L >> R; if (S == "TOTIENT") { cout << hasil(query(1, 1, n, L, R)) << endl; } else { cin >> val; upd(1, 1, n, L, R, pair<long long, long long>(val, bm[val])); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; bool vet[3]; int main() { long long n, a = 2e9, b = 2e9, c = 2e9, ab = 2e9, bc = 2e9, ac = 2e9, abc = 2e9; char fw; scanf("%lld", &n); for (long long i = 0, pr; i < n; i++) { scanf("%lld", &pr); scanf(" %c", &fw); vet[fw - 'A'] = true; while (1) { fw = getchar(); if (fw < 'A' || fw > 'C') break; vet[fw - 'A'] = true; } if (vet[0] == true) { a = min(a, pr); if (vet[1] == true) { ab = min(ab, pr); if (vet[2] == true) { abc = min(abc, pr); } } if (vet[2] == true) { ac = min(ac, pr); } } if (vet[1] == true) { b = min(b, pr); if (vet[2] == true) { bc = min(bc, pr); } } if (vet[2] == true) { c = min(c, pr); } vet[0] = vet[1] = vet[2] = false; ; } abc = min(abc, ab + c); abc = min(abc, ac + b); abc = min(abc, bc + a); abc = min(abc, a + b + c); if (abc == 2e9) printf("-1"); else printf("%lld", abc); return 0; }
2
#include <iostream> using namespace std; typedef long long ll; ll mod=1e9+7; int main(){ ll n; string s; cin >> n >> s; int check=0; int x[2*n]; for(int i=0;i<2*n;i++){ if(s[i]=='B'&&check%2==0||s[i]=='W'&&check%2==1){ check++; x[i]=0; } else{ check--; x[i]=1; } cerr << check << endl; } if(check){ cout << 0 << endl; return 0; } ll ans=1; for(ll i=1;i<=n;i++){ ans*=i; ans%=mod; } ll cnt=0; for(ll i=2*n-1;i>=0;i--){ if(x[i]) cnt++; else{ ans*=cnt; ans%=mod; cnt--; } } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 107; char s[maxn][maxn]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) scanf("%s", s[i]); if (n == 1) return puts("0"), 0; bool v[maxn]; int ans = 0; memset(v, 0, sizeof(v)); for (int i = 0; i < m; i++) { int j = 0; for (j = 0; j < n - 1; j++) { if (!v[j] && s[j + 1][i] < s[j][i]) { ans++; break; } } if (j == n - 1) { for (j = 0; j < n - 1; j++) { if (v[j]) continue; if (s[j][i] < s[j + 1][i]) v[j] = true; } v[j] = true; } } cout << ans << endl; return 0; }
3
//Love and Freedom. #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define ll long long #define inf 20021225 #define N 5010 #define mdn 1000000007 using namespace std; int read() { int s=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return f*s; } struct edge{int to,lt;}e[N<<1]; int in[N],cnt,sz[N],f[N],g[N][N],tmp[N],n; void add(int x,int y) { e[++cnt].to=y; e[cnt].lt=in[x]; in[x]=cnt; e[++cnt].to=x; e[cnt].lt=in[y]; in[y]=cnt; } void init() { f[0]=1; for(int i=2;i<=n;i+=2) f[i]=1ll*f[i-2]*(i-1)%mdn; } void upd(int &x,int y){x+=x+y>=mdn?y-mdn:y;} void dfs(int x,int fr) { sz[x]=1; g[x][1]=1; for(int i=in[x];i;i=e[i].lt) { int y=e[i].to; if(y==fr) continue; dfs(y,x); memcpy(tmp,g[x],sz[x]+1<<2); memset(g[x],0,sz[x]+1<<2); for(int sy=0;sy<=sz[y];sy++) for(int sx=0;sx<=sz[x];sx++) upd(g[x][sx+sy],1ll*g[y][sy]*tmp[sx]%mdn); sz[x]+=sz[y]; } for(int i=1;i<=sz[x];i++) upd(g[x][0],1ll*(mdn-1)*g[x][i]%mdn*f[i]%mdn); } int main() { n=read(); for(int i=1;i<n;i++) add(read(),read()); init(); dfs(1,1); printf("%d\n",mdn-g[1][0]); return 0; }
0
#include <bits/stdc++.h> using namespace std; int ans[10000]; int result[10000]; char text[10000][10]; char c; int p; int compare(const void *a, const void *b) { int aa = *(int *)a; int bb = *(int *)b; return aa - bb; } int main() { int pos = 0, len = -1, elemSize = 0; memset(text, 0, sizeof(text)); while (scanf("%c", &c) != -1) { if (c != '>' && c != '<' && c != '\0' && c != '\n' && c != ' ') text[len][pos++] = c; if (c == '<') len++, pos = 0; } memset(ans, 0, sizeof(ans)); memset(result, 0, sizeof(result)); p = -1; int step = -1; for (int i = 0; i <= len; i++) { if (strcmp(text[i], "table") == 0) p++, elemSize++; if (strcmp(text[i], "/table") == 0) { result[++step] = ans[p]; ans[p] = 0; p--; } if (strcmp(text[i], "td") == 0) ans[p]++; } qsort(result, elemSize, sizeof(result[0]), compare); for (int i = 0; i < elemSize; i++) printf("%d ", result[i]); printf("\n"); return 0; }
2
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) #define OP(m) cout << m << endl typedef long long ll; typedef unsigned long long ull; int main() { double xa1, ya1, xa2, ya2, xb1, yb1, xb2, yb2; while (cin >> xa1 >> ya1 >> xa2 >> ya2 >> xb1 >> yb1 >> xb2 >> yb2) { if (max(xa1, xb1) <= min(xa2, xb2) && max(ya1, yb1) <= min(ya2, yb2)) { OP("YES"); } else { OP("NO"); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2505; int n, g[N][N], an; bitset<2501> s[N]; struct Node { int val, x, y; void read(int x, int y) { this->x = x; this->y = y; scanf("%d", &val); g[x][y] = val; } bool operator<(const Node &c) const { return val < c.val; } } a[N * N]; int tmp[N * N], tn; bool check() { for (int i = 0; i < tn; i++) { int u = a[tmp[i]].x; int v = a[tmp[i]].y; if ((s[u] | s[v]).count() != n) return false; } for (int i = 0; i < tn; i++) { int u = a[tmp[i]].x; int v = a[tmp[i]].y; s[u][v] = 0; } return true; } bool judge() { for (int i = 1; i <= n; i++) { if (g[i][i]) return false; for (int j = i + 1; j <= n; j++) { if (g[i][j] != g[j][i]) return false; } } tmp[tn++] = 0; for (int i = 1; i < an; i++) { if (a[i].val != a[i - 1].val) { if (!check()) return false; tn = 0; tmp[tn++] = i; } else tmp[tn++] = i; } return check(); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { a[an++].read(i, j); s[i][j] = true; } } sort(a, a + an); printf("%s\n", judge() ? "MAGIC" : "NOT MAGIC"); return 0; }
6
#include <bits/stdc++.h> using namespace std; char s[50]; int main() { int n; long long num; char c; scanf("%d", &n); while (n--) { while ((c = getchar()) != '\'') ; scanf("%s", s); sscanf(s, "%I64d", &num); int len = strlen(s); if (len == 1) { if (s[len - 1] == '9') puts("1989"); else printf("199%c\n", s[len - 1]); } else if (len == 2) { if (s[1] == '9' && s[0] == '9') puts("1999"); else printf("20%s\n", s); } else if (len == 3) { if (s[len - 1] == '9' && s[len - 2] == '9') printf("2%s\n", s); else if (s[0] == '0') printf("3%s\n", s); else printf("2%s\n", s); } else if (len >= 4) { long long ai = 0, bi = 0; for (int i = 1; i <= len; i++) ai++, ai *= 10; for (int i = 1; i < len; i++) bi++, bi *= 10; bi += 1989; ai += 1988; if (num <= ai && num >= bi) printf("%I64d\n", num); else printf("1%s\n", s); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long inf = 1LL << 28; const long long mod = 1LL; char arr[210][210]; long long par[210]; bool col[210]; vector<long long> adjacency[210]; int main() { long long n, i, j, k, l; while (cin >> n) { for (i = 0; i < n; i++) adjacency[i].clear(), scanf(" %s", arr[i]); k = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) if (arr[i][j] == '.') { adjacency[i].push_back(j); k++; break; } } if (k == n) { for (i = 0; i < n; i++) printf("%I64d %I64d\n", i + 1, adjacency[i][0] + 1); return 0; } k = 0; for (i = 0; i <= n; i++) adjacency[i].clear(); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) if (arr[j][i] == '.') { adjacency[i].push_back(j); k++; break; } } if (k == n) { for (i = 0; i < n; i++) printf("%I64d %I64d\n", adjacency[i][0] + 1, i + 1); return 0; } printf("-1\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct opt { int att, def; }; opt arr[4]; bool oneWin() { if (arr[0].att > arr[2].def && arr[0].def > arr[2].att && arr[0].att > arr[3].def && arr[0].def > arr[3].att) return true; if (arr[1].att > arr[2].def && arr[1].def > arr[2].att && arr[1].att > arr[3].def && arr[1].def > arr[3].att) return true; return false; } bool twoWin() { bool flag[2]; flag[0] = flag[1] = false; for (int i = 0; i < 2; i++) { for (int j = 2; j < 4; j++) { if (arr[j].att > arr[i].def && arr[j].def > arr[i].att) flag[i] = true; } } if (flag[0] && flag[1]) return true; return false; } int main() { for (int i = 0; i < 2; i++) { int a, b, c, d; cin >> a >> b >> c >> d; arr[i * 2].att = b; arr[i * 2].def = c; arr[i * 2 + 1].att = d; arr[i * 2 + 1].def = a; } if (oneWin()) { cout << "Team 1" << endl; } else if (twoWin()) { cout << "Team 2" << endl; } else cout << "Draw" << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; #define fi first #define se second #define mp make_pair #define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = (int)1e6 + 3; int powr(int n, int k){ if(k == 0) return 1; int p = powr(n, k/2); p = (p * 1ll * p) % MOD; if(k % 2 == 1) p = (p * 1ll * n) % MOD; return p; } int fac[MOD]; void solve(){ int x, d, n; cin >> x >> d >> n; if(d == 0){ cout << powr(x, n) << "\n"; return; } x = (x * 1ll * powr(d, MOD - 2)) % MOD; if(x == 0){ cout << 0 << "\n"; return; } if(x + n - 1 >= MOD){ cout << 0 << "\n"; return; } int ans = (fac[x + n - 1] * 1ll * powr(fac[x - 1],MOD-2))%MOD; ans = (ans * 1ll * powr(d, n)) % MOD; cout << ans << "\n"; } int main(){ fastIO; fac[0]=1; for(int i = 1; i < MOD; i ++ ) fac[i]=(fac[i-1]*1ll*i)%MOD; int q; cin >> q; for(int t = 0 ;t < q; t ++ ) solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int num[maxn]; int e[maxn * 100]; int main() { int T; scanf("%d", &T); while (T--) { int cnt = 0, res = 0; memset(num, 0, sizeof(num)); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); num[x]++; if (num[x] % 2 == 0 && num[x]) e[cnt++] = x; } sort(e, e + cnt); int a = e[0], b = e[1]; for (int i = 2; i < cnt; i++) { if ((double)b / (double)a > (double)e[i] / (double)e[i - 1]) { a = e[i - 1]; b = e[i]; } } printf("%d %d %d %d\n", a, a, b, b); } return 0; }
3
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int N, M; int pSeg[1 << 19]; void update(int k, int x) { k += (1 << 18) - 1; pSeg[k] = x; while (k){ k = (k - 1) / 2; pSeg[k] = min(pSeg[k * 2 + 1], pSeg[k * 2 + 2]); } } int getMin(int a, int b, int k, int l, int r) { if (b <= l || r <= a){ return (1 << 30); } if (a <= l && r <= b){ return (pSeg[k]); } int lch = getMin(a, b, k * 2 + 1, l, (l + r) / 2); int rch = getMin(a, b, k * 2 + 2, (l + r) / 2, r); return (min(lch, rch)); } int main(void) { static int rSum[100000]; while (1){ scanf("%d %d", &N, &M); if (N + M == 0){ break; } for (int i = 0; i < 1 << 19; i++) pSeg[i] = M; memset(rSum, 0, sizeof(rSum)); for (int i = 0; i < N; i++){ int t; scanf("%d", &t); rSum[i + 1] = (rSum[i] + (t % M)) % M; } int ans = 0; for (int i = 0; i < N; i++){ update(rSum[i + 1], rSum[i + 1]); int lMin = getMin(0, rSum[i + 1], 0, 0, 1 << 18); int rMin = getMin(rSum[i + 1] + 1, M, 0, 0, 1 << 18); ans = max(max(ans, rSum[i + 1]), max(rSum[i + 1] - lMin, (rSum[i + 1] - rMin + M) % M)); } printf("%d\n", ans); } return (0); }
0
#include <bits/stdc++.h> using namespace std; int main() { int h, r; cin >> h >> r; if (h + r == 0) cout << "0\n"; else if (h + r < 0) cout << "-1\n"; else cout << "1\n"; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; const int logn = 30; const int inf = (int)1e9 + 5; const long long mod = (int)1e9 + 7; const long long base = 2204234849; const long long l_inf = (long long)4e18; const long double pi = acos(-1.0); const long double eps = 1e-12; int n, m, q; int a[105][105]; int data[10005][4]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.precision(12); cout << fixed; srand(566); cin >> n >> m >> q; for (int i = 0; i < q; i++) { cin >> data[i][0] >> data[i][1]; if (data[i][0] == 3) cin >> data[i][2] >> data[i][3]; } reverse(data, data + q); for (int i = 0; i < q; i++) { if (data[i][0] == 1) { for (int j = m; j > 1; j--) swap(a[data[i][1]][j], a[data[i][1]][j - 1]); } if (data[i][0] == 2) { for (int j = n; j > 1; j--) swap(a[j][data[i][1]], a[j - 1][data[i][1]]); } if (data[i][0] == 3) { a[data[i][1]][data[i][2]] = data[i][3]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) printf("%d ", a[i][j]); printf("\n"); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 10; int get2(int x) { for (int i = 1;; i++) if (x % i == 0) { int j = x / i; if ((i - j) % 2 == 0) return (j - i) / 2; } } void get(int *a, int n) { if (n == 1) { a[1] = 1; return; } if (n == 2) { a[1] = 3; a[2] = 4; return; } for (int i = 1; i <= n - 1; i++) a[i] = 2; a[n] = get2(4 * n - 4); } int a[maxn], b[maxn]; int main() { int n, m; scanf("%d%d", &n, &m); get(a, n); get(b, m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) printf("%d%c", a[i] * b[j], j == m ? '\n' : ' '); }
5
#include <bits/stdc++.h> using namespace std; long long a, b, w, x, c; long long ans; void deal() { if (c <= a) { ans = 0; return; } long long pt = (a * w - w * c + b) / (x - w); ans = pt; if (abs(pt * (x - w)) < abs(a * w - w * c + b)) ans++; return; } int main() { scanf("%I64d%I64d%I64d%I64d%I64d", &a, &b, &w, &x, &c); deal(); printf("%I64d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, dl, v1, v2, k, gr; double revs; double f(double x) { double ta, sl, t2, sa, rast; ta = x / v2; sl = ta * v1; t2 = (x - sl) / (v1 + v2); sa = x + t2 * v2; sl = sl + t2 * v1; revs = t2 * v2; rast = gr * x - (gr - 1) * revs; return rast; } int main() { ios_base::sync_with_stdio(0); cin >> n >> dl >> v1 >> v2 >> k; gr = (n + k - 1) / k; double l = 0, r = 10000000000, mid; for (int i = 1; i <= 10000; i++) { mid = (l + r) / 2; if (f(mid) < dl) l = mid; else r = mid; } double tt = (gr * l + (gr - 1) * revs) / v2; printf("%.10lf", tt); return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,inline,unroll-loops,fast-math") using namespace std; int t; vector<int> e[100020]; vector<pair<int, int> > o; void DFS(int x, int p, int t) { int i, r, c, k; o.push_back(make_pair(x, t)); k = (int)e[x].size() - (x != 1); r = t; for (i = c = 0; i < (signed)e[x].size(); i++) if (e[x][i] != p) { c++; if (t + c > ::t) { c--; o.push_back(make_pair(x, r - 1 - (k - c))); t = r - 1 - (k - c); c = 1; } DFS(e[x][i], x, t + c); o.push_back(make_pair(x, t + c)); } if (x != 1 && t + c != r - 1) o.push_back(make_pair(x, r - 1)); return; } int main(void) { int n, u, v; int i; scanf("%d", &n); for (i = 1; i < n; i++) { scanf("%d %d", &u, &v); e[u].push_back(v); e[v].push_back(u); } for (i = 1; i <= n; i++) t = max(t, (int)e[i].size()); DFS(1, 1, 0); printf("%d\n", (int)o.size()); for (i = 0; i < (signed)o.size(); i++) printf("%d %d\n", o[i].first, o[i].second); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int size; string s; cin >> size; cin >> s; int last = -1; int ans = 0; for (int i = 0; i < size; ++i) { if (s[i] == 'A') last = i; else if (last != -1) ans = max(ans, i - last); } cout << ans << '\n'; } }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, q; cin >> n >> q; for (int i = 1; i <= q; i++) { long long l, r; cin >> l >> r; if ((l + r) % 2 == 0) { if (n % 2 == 0) cout << (l - 1) * (n / 2) + (r + 1) / 2 << "\n"; else if (r % 2 == 1) cout << (l - 1) / 2 * n + (r + 1) / 2 << "\n"; else cout << (l - 2) / 2 * n + (n + 1) / 2 + (r + 1) / 2 << "\n"; } else { long long kol = (n * n + 1) / 2; if (n % 2 == 0) cout << kol + (l - 1) * (n / 2) + (r + 1) / 2 << "\n"; else if (r % 2 == 0) cout << kol + (l - 1) / 2 * n + (r + 1) / 2 << "\n"; else cout << kol + (l - 2) / 2 * n + n / 2 + (r + 1) / 2 << "\n"; } } }
2
#include <bits/stdc++.h> using namespace std; const int N = 300; const int mod = 998244353; const double Pi = acos(-1.0); const long long INF = 1e18; const int G = 3, Gi = 332748118; long long qpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } int n, m, k; int a[N], dp[N][N]; vector<int> g[N]; void dfs(int u, int pre) { dp[u][0] = a[u]; for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if (v != pre) dfs(v, u); } for (int j = 0; j <= k; ++j) { if (j == 0) { for (int i = 0; i < g[u].size(); ++i) { int v = g[u][i]; if (v == pre) continue; dp[u][j] += dp[v][max(0, k - j - 1)]; } } else { for (int i = 0; i < g[u].size(); ++i) { int v0 = g[u][i]; if (v0 == pre) continue; int temp = dp[v0][j - 1]; for (int i1 = 0; i1 < g[u].size(); ++i1) { int v1 = g[u][i1]; if (v1 == v0 || v1 == pre) continue; temp += dp[v1][max(j - 1, k - j - 1)]; } dp[u][j] = max(dp[u][j], temp); } } } for (int j = N - 1; j > 0; --j) { dp[u][j - 1] = max(dp[u][j - 1], dp[u][j]); } } int main() { scanf("%d%d", &n, &k); k++; for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); } dfs(1, 0); printf("%d\n", dp[1][0]); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() {int A, B, c{}; cin >> A >> B; for (int i{A}; i <= B; ++i) {auto s{to_string(i)}, t{s}; reverse(begin(t), end(t)); c += s == t;} cout << c;}
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; string s; int a[30] = {0}; int value; int length; int ct = 0; int i; while (t--) { cin >> s; for (i = 0; i < s.length() - 1; i++) { if (s[i] != s[i + 1]) break; } if (i == s.length() - 1) { cout << "-1" << "\n"; continue; } sort(s.begin(), s.end()); for (i = 0; i < s.length(); i++) cout << s[i]; cout << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = (1 << 12); const int mod = (int)1e9 + 7; int n, m; string a[MAXN]; void read() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; } int dp[MAXN][MAXN]; int cnt(int x1, int y1, int x2, int y2) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) dp[i][j] = 0; dp[x1][y1] = 1; if (a[x1][y1] == '#') dp[x1][y1] = 0; for (int x = x1; x <= x2; x++) for (int y = y1; y <= y2; y++) { if (x == x1 && y == y1) continue; dp[x][y] = 0; if (a[x][y] == '#') continue; if (x - 1 >= x1) dp[x][y] = (dp[x][y] + dp[x - 1][y]) % mod; if (y - 1 >= y1) dp[x][y] = (dp[x][y] + dp[x][y - 1]) % mod; } return dp[x2][y2]; } int64_t myabs(int64_t x) { return x > 0 ? x : -x; } void solve() { int64_t ans1 = (cnt(0, 1, n - 2, m - 1) * 1ll * cnt(1, 0, n - 1, m - 2)) % mod; int64_t ans2 = (cnt(0, 1, n - 1, m - 2) * 1ll * cnt(1, 0, n - 2, m - 1)) % mod; cout << (ans1 - ans2 + mod) % mod << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long C[16][16], dp[16][16], k; int num[16], t; long long solve(int len) { memset(dp, 0, sizeof(dp)); for (int i = 0; i <= num[0]; i++) dp[0][i] = C[len][i]; for (int i = 1; i < 16; i++) { for (int j = 0; j <= len; j++) { int ans = min(num[i], j); for (int k = 0; k <= ans; k++) { dp[i][j] += dp[i - 1][j - k] * C[len - j + k][k]; } } } return dp[15][len]; } int main() { for (int i = 0; i < 16; i++) C[i][0] = 1; for (int i = 1; i < 16; i++) for (int j = 1; j <= i; j++) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; cin >> k >> t; for (int i = 0; i < 16; i++) num[i] = t; int len = 1; for (;; len++) { long long tmp = 0; if (len == 1) tmp = 15; else { for (int j = 1; j < 16; j++) { num[j]--; tmp += solve(len - 1); num[j]++; } } if (k > tmp) k -= tmp; else break; } for (int i = len; i > 0; i--) { if (i == 1) { for (int j = 0; j < 16; j++) { if (j == 0 && len == 1) continue; if (num[j] != 0) k--; if (k == 0) { if (j < 10) cout << j; else cout << (char)(j + 'a' - 10); break; } } break; } for (int j = 0; j < 16; j++) { if (i == len && j == 0) continue; num[j]--; long long tmp = solve(i - 1); if (k > tmp) k -= tmp; else { if (j < 10) cout << j; else cout << (char)(j + 'a' - 10); break; } num[j]++; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; using ll=long long; #define int ll #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) #define gnr(i,a,b) for(int i=int(b)-1;i>=a;i--) #define per(i,b) gnr(i,0,b) #define pb push_back #define eb emplace_back #define a first #define b second #define bg begin() #define ed end() #define all(x) x.bg,x.ed #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;} template<class t,class u> void chmin(t&a,u b){if(a>b)a=b;} template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; using pi=pair<int,int>; using vi=vc<int>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.a<<","<<p.b<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } template<class t> void mkuni(vc<t>&v){ sort(all(v)); v.erase(unique(all(v)),v.ed); } ll read(){ll i;cin>>i;return i;} using ld=long double; using cm=complex<ld>; #define x real() #define y imag() const ld eps=1e-7; const ld PI=acos(ld(-1)); int sgn(ld a){return a<-eps?-1:(a>eps?1:0);} auto cmcmp=[](const cm&a,const cm&b){ if(sgn(a.x-b.x))return a.x<b.x; else return sgn(a.y-b.y)<0; }; ld dot(cm a,cm b){return a.x*b.x+a.y*b.y;} ld crs(cm a,cm b){return a.x*b.y-a.y*b.x;} int ccw(cm a,cm b){return sgn(crs(a,b));} int ccw(cm a,cm b,cm c){return ccw(b-a,c-a);} //AOJ1183 int qeq(ld a,ld b,ld c,ld&d,ld&e){ ld f=b*b-4*a*c; if(sgn(f)<0)return 0; ld g=sqrt(max(f,ld(0))); d=(-b+g)/(2*a); e=(-b-g)/(2*a); return sgn(f)+1; } //(-2)[a,-1](0)[b,1](2) int bet(cm a,cm b,cm c){ cm d=b-a; ld e=dot(d,c-a); if(sgn(e)<=0)return sgn(e)-1; return sgn(e-norm(d))+1; } ld tri2(cm a,cm b,cm c){ return crs(b-a,c-a); } //AOJ0153 //0-no,1-edge,2-in int cont(cm a,cm b,cm c,cm d){ if(ccw(a,b,c)==-1) swap(b,c); return min({ccw(a,b,d),ccw(b,c,d),ccw(c,a,d)})+1; } //AOJ1183 //arg between ab //assume given lengths are valid ld arg(ld a,ld b,ld c){ return acos(min(max((a*a+b*b-c*c)/(2*a*b),ld(-1)),ld(1))); } using ln=pair<cm,cm>; cm dir(ln a){return a.b-a.a;} cm eval(ln a,ld b){return a.a+dir(a)*b;} cm proj(ln a,cm b){ cm c=dir(a); return a.a+c*dot(c,b-a.a)/norm(c); } cm refl(ln a,cm b){ return ld(2)*proj(a,b)-b; } //AOJ0153 ld dsp(ln a,cm b){ cm c=proj(a,b); if(abs(bet(a.a,a.b,c))<=1)return abs(b-c); return min(abs(b-a.a),abs(b-a.b)); } int ccw(ln a,cm b){return ccw(a.a,a.b,b);} //AOJ1157 //0-no,1-yes(endpoint),2-yes(innner) int iss(ln a,ln b){ int c=ccw(a.a,a.b,b.a)*ccw(a.a,a.b,b.b); int d=ccw(b.a,b.b,a.a)*ccw(b.a,b.b,a.b); int e=max(c,d); if(e)return 1-e; int f=bet(a.a,a.b,b.a),g=bet(a.a,a.b,b.b); if(max(f,g)==-2||min(f,g)==2)return 0; return 1; } //AOJ1157 ld dss(ln a,ln b){ if(iss(a,b))return 0; return min({dsp(a,b.a),dsp(a,b.b),dsp(b,a.a),dsp(b,a.b)}); } using cr=pair<cm,ld>; //AOJ0153 //0-no,1-edge,2-in int cont(cr a,cm b){ return sgn(a.b-abs(b-a.a))+1; } //AOJ0153 //0-no,1-touch,2-cross int ids(cr a,ln b){ return sgn(a.b-dsp(b,a.a))+1; } //AOJ0129 (touch以外) //0-no(in),1-touch(in),2-cross,3-touch(out),4-no(out) int ics(cr a,ln b){ int c=ids(a,b); if(c<=1)return 4-c; return sgn(max(abs(b.a-a.a),abs(b.b-a.a))-a.b)+1; } //AOJ1183 int ccl(cr a,ln b,ld&c,ld&d){ cm e=dir(b); cm f=b.a-a.a; return qeq(norm(e),2*dot(e,f),norm(f)-a.b*a.b,c,d); } //AOJ0023 //0-apart,1-coinside,2-a<b,3-a<=b,4-a>b,5-a>=b,6-a touch b,7-a cross b int icc(cr a,cr b){ ld c=abs(a.a-b.a); if(sgn(c)==0&&sgn(a.b-b.b)==0)return 1; int d=sgn(c+a.b-b.b); if(d<=0)return d+3; int e=sgn(c+b.b-a.b); if(e<=0)return e+5; int f=sgn(a.b+b.b-c); if(f>=0)return f+6; return 0; } //two intersections r,l seen be a.a //assume two circles cross pair<ld,ld> ccc(cr a,cr b){ ld c=arg(b.a-a.a); ld d=arg(a.b,abs(b.a-a.a),b.b); return make_pair(c-d,c+d); } //area of circular segment ld cseg(ld a,ld b){ return a*a*(b-sin(b))/2; } signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(20); while(1){ ld uw=read(),uh=read(),a=read(),b=read(),ab=read(); if(uw==0)break; ld ar=sqrt(a/PI),br=sqrt(b/PI); bool sw=ar<br; if(sw)swap(ar,br); if(sgn(ar*2-min(uw,uh))>0){ cout<<"impossible"<<endl; continue; } ld lw=ar-br,up=ar+br; rep(_,60){ ld mid=(lw+up)/2; ld c,d; tie(c,d)=ccc(cr(cm(0,0),ar),cr(cm(mid,0),br)); ld e=cseg(ar,d-c); tie(c,d)=ccc(cr(cm(mid,0),br),cr(cm(0,0),ar)); e+=cseg(br,d-c); if(e<ab) up=mid; else lw=mid; } ld ax=ar,ay=ar; ld bx=ax,by=ay+up; if(sgn(by+br-uh)>0){ ld h=uh-ay-br; bx=ax+sqrt(up*up-h*h); by=ay+h; } if(sgn(bx+br-uw)<=0){ if(sw){ swap(ax,bx); swap(ay,by); swap(ar,br); } cout<<ax<<" "<<ay<<" "<<ar<<" "<<bx<<" "<<by<<" "<<br<<endl; }else cout<<"impossible"<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int inline read() { int num = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) { num = (num << 3) + (num << 1) + c - '0'; c = getchar(); } return num * neg; } const int maxn = 200010, mod = 998244353; int n, k, a, b, h[maxn], ans, sum, fac[maxn], inv[maxn]; int Pow(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = 1ll * ans * x % mod; x = 1ll * x * x % mod; y >>= 1; } return ans; } int C(int x, int y) { return 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod; } int main() { n = read(); k = read(); for (int i = 1; i <= n; i++) h[i] = read(); if (n == 1) { cout << 0; return 0; } for (int i = 1; i <= n; i++) { int to; if (i == n) to = 1; else to = i + 1; if (h[i] == h[to]) a++; else b++; } ans = Pow(k, a); fac[0] = 1; inv[0] = 1; for (int i = 1; i <= b; i++) fac[i] = 1ll * fac[i - 1] * i % mod; for (int i = 1; i <= b; i++) inv[i] = Pow(fac[i], mod - 2); for (int i = 1; i <= b; i++) { int S; if (i & 1) S = Pow(2, i - 1); else S = ((1ll * Pow(2, i) * inv[2] % mod - 1ll * C(i, i / 2) * inv[2] % mod) % mod + mod) % mod; sum = (sum + 1ll * Pow(k - 2, b - i) * C(b, b - i) % mod * S % mod) % mod; } cout << 1ll * ans * sum % mod; return 0; }
6
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846; string arr[1001]; int a[1001]; int main() { cin.tie(0); std::ios::sync_with_stdio(false); int n, m, x, t, c = 0, s7 = 0, s4 = 0; string s; cin >> s; for (int i = 0; i < s.size(); ++i) { if (s[i] == '4') s4++; else if (s[i] == '7') s7++; } if (!s4 && !s7) { cout << -1 << endl; return 0; } if (s4 >= s7) { cout << 4 << endl; } else if (s7 > s4) { cout << 7 << endl; } }
2
#include <bits/stdc++.h> using namespace std; using P = pair<int, int>; int main(){ string s; cin >> s; for(int i = 0; i < (1 << 3); i++) { string ans = ""; int t = s[0] - '0'; ans += s[0]; for(int j = 0; j < 3; j++) { if(i & (1 << j)) { ans += "+"; t += s[j+1] - '0'; } else { ans += "-"; t -= s[j+1] - '0'; } ans += s[j + 1]; } if(t == 7) { cout << ans << "=7" << endl; return 0; } } }
0
#include <bits/stdc++.h> using namespace std; int n, s[110000], L, R; int cnt[110000]; int tmpcnt[110000]; bool check(int r) { memset(tmpcnt, 0, sizeof(tmpcnt)); for (int i = L; i <= r; i++) tmpcnt[s[i]]++; for (int i = 1; i < n - i + 1; i++) { bool LinRange = (i >= L && i <= r); bool RinRange = ((n - i + 1) >= L && (n - i + 1) <= r); if (LinRange && RinRange) continue; else if (LinRange) { if (--tmpcnt[s[n - i + 1]] < 0) return false; } else if (RinRange) { if (--tmpcnt[s[i]] < 0) return false; } else { if (s[i] != s[n - i + 1]) return false; } } return true; } long long int calc() { int lowerBound = L, upperBound = n, ans = -1; while (lowerBound <= upperBound) { int mid = (lowerBound + upperBound) >> 1; if (check(mid)) { ans = mid; upperBound = mid - 1; } else lowerBound = mid + 1; } return (long long int)(n - ans + 1) * L; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &s[i]); L = 1, R = n; while (L <= R) { if (s[L] == s[R]) { L++; R--; } else break; } if (L > R) { printf("%I64d\n", (long long int)n * (long long int)(n - 1) / 2 + n); return 0; } int numOfOdd = 0; for (int i = L; i <= R; i++) cnt[s[i]]++; for (int i = 0; i < 110000; i++) if (cnt[i] & 1) numOfOdd++; if (numOfOdd > 1) { printf("0\n"); return 0; } long long int ans = 0; ans += calc(); reverse(s + 1, s + n + 1); ans += calc(); ans -= (long long int)L * L; printf("%I64d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long double epsylon = 1e-9; const std::string PROGRAM_NAME = "ivo"; int main() { int n; cin >> n; vector<string> a(n); getline(cin, a[0]); for (int i = 0; i < (int)a.size(); ++i) { getline(cin, a[i]); } vector<string> ans(2 * n - 1, string(2 * n - 1, 'x')); ans[n - 1][n - 1] = 'o'; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (a[i][j] == 'o') { for (int k = 0; k < n; ++k) { for (int l = 0; l < n; ++l) { if (a[k][l] == '.') { ans[n - 1 + k - i][n - 1 + l - j] = '.'; } } } } } } vector<string> actually(n, string(n, '.')); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (a[i][j] == 'o') { actually[i][j] = 'o'; } } } bool can = true; for (int i = 0; i < n && can; ++i) { for (int j = 0; j < n && can; ++j) { if (a[i][j] != 'o') { continue; } for (int k = 0; k < n && can; ++k) { for (int l = 0; l < n && can; ++l) { if (a[k][l] == 'o') { continue; } if (ans[n - 1 + k - i][n - 1 + l - j] == 'x') { actually[k][l] = 'x'; } } } } } for (int i = 0; i < n && can; ++i) { for (int j = 0; j < n && can; ++j) { if (a[i][j] == 'x' && actually[i][j] != 'x') { can = false; break; } } } if (!can) { cout << "NO" << endl; } else { cout << "YES" << endl; for (int i = 0; i < (int)ans.size(); ++i) { printf("%s\n", ans[i].c_str()); } } return 0; }
4
#include <bits/stdc++.h> using namespace std; inline void boost() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const long long maxn = 2e5 + 123; const long long inf = 1e9 + 123; const long long linf = 1e18 + 123; const long long mod = 1e9 + 7; const double eps = 1e-9; const double pi = acos(-1); int dx[8] = {0, 1, -1, 0, 1, -1, 1, -1}; int dy[8] = {1, 0, 0, -1, 1, 1, -1, -1}; int a[maxn], ans[maxn]; set<int> s; int main() { boost(); int q; cin >> q; while (q--) { int n; cin >> n; s.clear(); for (int i = 1; i <= n; i++) { cin >> a[i]; s.insert(i); } bool ok = 0; for (int i = 1; i <= n; i++) { if (a[i] != a[i - 1]) { ans[i] = a[i]; s.erase(a[i]); } else { ans[i] = (*s.begin()); s.erase(*s.begin()); if (ans[i] > a[i]) { ok = 1; break; } } } if (ok == 1) { cout << "-1\n"; continue; } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << "\n"; } exit(0); }
2
#include <bits/stdc++.h> int a[1 << 21], f[1 << 21][21], n, m, ans, opt; char s[21]; int main() { scanf("%d", &n); memset(f, 6, sizeof(f)), f[0][0] = 0; for (int i = 0; i < n; i++) { scanf("%s", s), m ? 0 : m = strlen(s); for (int j = 0; j < m; j++) a[i] = a[i] * 2 + s[j] - 48; } for (int i = 0; i < n; i++) if (i) { int res = 1 << 26, j; for (j = 0; (a[i - 1] & ~-(1 << m - j)) != a[i] >> j; j++) ; ans += j; for (int k = 0; k < m + 1; k++) res = res < f[a[i] >> k][m - k] + k - j ? res : f[a[i] >> k][m - k] + k - j; for (int k = 0; k < m + 1; k++) f[a[i - 1] & ~-(1 << k)][k] = f[a[i - 1] & ~-(1 << k)][k] < res ? f[a[i - 1] & ~-(1 << k)][k] : res; } for (int i = 0; i < 1 << m; i++) for (int j = 0; j < m + 1; j++) opt = opt < f[i][j] ? opt : f[i][j]; printf("%d\n", opt + ans + m); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); string s; cin >> s; map<char, bool> mp = {{'<', 1}, {'{', 1}, {'[', 1}, {'(', 1}, {']', 0}, {'}', 0}, {'>', 0}, {')', 0}}; map<char, char> m = {{'<', '>'}, {'{', '}'}, {'[', ']'}, {'(', ')'}}; stack<int> p; int ans = 0; for (auto c : s) { if (mp[c]) p.push(c); else { if (p.empty()) { cout << "Impossible\n"; return 0; } else if (c != m[p.top()]) ans++; p.pop(); } } if (p.empty()) cout << ans << endl; else cout << "Impossible\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long read() { long long f = 1, x = 0; char ss = getchar(); while (ss < '0' || ss > '9') { if (ss == '-') f = -1; ss = getchar(); } while (ss >= '0' && ss <= '9') { x = x * 10 + ss - '0'; ss = getchar(); } return f * x; } long long seed, vmax; long long rnd() { long long res = seed; seed = (seed * 7 + 13) % 1000000007; return res; } const int maxn = 100010; int n, m; long long a[maxn]; struct node { int ll, rr; mutable long long val; node(int L, int R = -1, long long V = 0) : ll(L), rr(R), val(V) {} bool operator<(const node& tt) const { return ll < tt.ll; } }; set<node> st; long long qpow(long long a, long long k, long long p) { long long res = 1; a %= p; while (k > 0) { if (k & 1) res = (res * a) % p; a = (a * a) % p; k >>= 1; } return res; } set<node>::iterator split(int pos) { set<node>::iterator it = st.lower_bound(node(pos)); if (it != st.end() && it->ll == pos) return it; --it; int ll = it->ll, rr = it->rr; long long val = it->val; st.erase(it); st.insert(node(ll, pos - 1, val)); return st.insert(node(pos, rr, val)).first; } void assign(int ll, int rr, long long val) { set<node>::iterator itr = split(rr + 1), itl = split(ll); st.erase(itl, itr); st.insert(node(ll, rr, val)); } void add(int ll, int rr, long long val) { set<node>::iterator itr = split(rr + 1), itl = split(ll); for (; itl != itr; ++itl) itl->val += val; } long long kth(int ll, int rr, int k) { vector<pair<long long, int> > vec; set<node>::iterator itr = split(rr + 1), itl = split(ll); for (; itl != itr; ++itl) vec.push_back(pair<long long, int>(itl->val, itl->rr - itl->ll + 1)); sort(vec.begin(), vec.end()); for (vector<pair<long long, int> >::iterator it = vec.begin(); it != vec.end(); ++it) { k -= it->second; if (k <= 0) return it->first; } return -1; } long long qsum(int ll, int rr, long long x, long long y) { long long res = 0; set<node>::iterator itr = split(rr + 1), itl = split(ll); for (; itl != itr; ++itl) res += (qpow(itl->val, x, y) * ((itl->rr - itl->ll + 1) % y)) % y, res %= y; return res; } int main() { n = read(); m = read(); seed = read(); vmax = read(); for (int i = 1; i <= n; ++i) { a[i] = (rnd() % vmax) + 1; st.insert(node(i, i, a[i])); } for (int i = 1; i <= m; ++i) { int op = (rnd() % 4) + 1; int ll = (rnd() % n) + 1, rr = (rnd() % n) + 1; long long x, y; if (ll > rr) swap(ll, rr); if (op == 3) x = (rnd() % (rr - ll + 1)) + 1; else x = (rnd() % vmax) + 1; if (op == 4) y = (rnd() % vmax) + 1; if (op == 1) add(ll, rr, x); else if (op == 2) assign(ll, rr, x); else if (op == 3) printf("%lld\n", kth(ll, rr, x)); else if (op == 4) printf("%lld\n", qsum(ll, rr, x, y)); } return 0; }
5
#include <bits/stdc++.h> #define rep(i,N) for(int i=0;i<(int)N;i++) #define rep1(i,N) for(int i=1;i<=(int)N;i++) #define pb push_back #define all(c) c.begin(),c.end() #define show(x) cout<<#x<<" "<<x<<endl using namespace std; typedef unsigned long long ll; typedef pair<ll,ll> P; ll rs[2][26]; set<P> st; int main(){ srand(unsigned(time(NULL))); rep(t,2) rep(i,26) rs[t][i]=rand(); string a,b; cin>>a>>b; int A=a.size(),B=b.size(); for(int l=min(A,B);l>0;l--){ st.clear(); ll v[2]={}; rep(t,2) rep(i,l) v[t]+=rs[t][a[i]-'a']; st.insert(P(v[0],v[1])); rep(i,A-l){ rep(t,2){ v[t]-=rs[t][a[i]-'a']; v[t]+=rs[t][a[i+l]-'a']; } st.insert(P(v[0],v[1])); } bool found=0; v[0]=v[1]=0; rep(t,2) rep(i,l) v[t]+=rs[t][b[i]-'a']; if(st.find(P(v[0],v[1]))!=st.end()) found=1; rep(i,B-l){ rep(t,2){ v[t]-=rs[t][b[i]-'a']; v[t]+=rs[t][b[i+l]-'a']; } if(st.find(P(v[0],v[1]))!=st.end()) found=1; } if(found){ cout<<l<<endl; return 0; } } puts("0"); }
0
#include <bits/stdc++.h> using namespace std; const long long N = 500005; long long n, cnt, head[N], diam, st[N], top, cir[N], dis[N]; long long tot, vis[N], huan[N], stw[N], w[N], ls[N], rs[N], lmx[N], rmx[N]; struct Edge { long long v, nx, s; } e[1000005]; inline long long read() { long long ret = 0, ff = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') ff = -ff; ch = getchar(); } while (isdigit(ch)) { ret = (ret << 3) + (ret << 1) + ch - '0'; ch = getchar(); } return ret * ff; } void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + 48); } void writeln(long long x) { write(x), puts(""); } void add(long long x, long long y, long long s) { e[++cnt].v = y; e[cnt].s = s; e[cnt].nx = head[x]; head[x] = cnt; } void dfs(long long now, long long fa) { st[++top] = now; vis[now] = 1; for (long long i = head[now]; i; i = e[i].nx) { long long v = e[i].v; if (v == fa) continue; if (vis[v]) { while (st[top] != v) { cir[++tot] = st[top]; w[tot - 1] = stw[top]; huan[cir[tot]] = 1; top--; } cir[++tot] = v; w[tot - 1] = stw[top]; w[tot] = e[i].s; huan[cir[tot]] = 1; return; } stw[top] = e[i].s; dfs(v, now); if (tot) return; } top--; vis[now] = 0; } void getd(long long now, long long fa) { for (long long i = head[now]; i; i = e[i].nx) { long long v = e[i].v; if (v == fa || huan[v]) continue; getd(v, now); diam = max(diam, dis[now] + dis[v] + e[i].s); dis[now] = max(dis[now], dis[v] + e[i].s); } } signed main() { n = read(); for (long long i = 1; i <= n; i++) { long long x = read(), y = read(), s = read(); add(x, y, s); add(y, x, s); } dfs(1, 0); for (long long i = 1; i <= tot; i++) getd(cir[i], 0); long long des = w[tot], s = 0, mx = -1e15; w[tot] = 0; for (long long i = 1; i <= tot; i++) { ls[i] = max(ls[i - 1], dis[cir[i]] + s); lmx[i] = max(lmx[i - 1], dis[cir[i]] + s + mx); mx = max(mx, dis[cir[i]] - s); s += w[i]; } s = 0, mx = -1e15; for (long long i = tot; i >= 1; i--) { s += w[i]; rs[i] = max(rs[i + 1], dis[cir[i]] + s); rmx[i] = max(rmx[i + 1], dis[cir[i]] + s + mx); mx = max(mx, dis[cir[i]] - s); } long long ans = lmx[tot]; for (long long i = 1; i < tot; i++) ans = min(ans, max(max(lmx[i], rmx[i + 1]), des + ls[i] + rs[i + 1])); write(max(ans, diam)); return 0; }
6
#include <bits/stdc++.h> using namespace std; long long n, k, a[55], cum[55]; set<long long> dp[55][55]; set<long long> maxand(long long i, long long k) { if (k == 1) { return {cum[n] - cum[i]}; } if (dp[i][k].empty() == false) { return dp[i][k]; } set<long long> ret, t; for (int x = i + 1; x <= n - k + 1; x++) { t = maxand(x, k - 1); for (auto j = t.begin(); j != t.end(); j++) { ret.insert(((cum[x] - cum[i]) & (*j))); } } return dp[i][k] = ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; cum[i] = cum[i - 1] + a[i]; } set<long long> ans = maxand(0, k); long long mx = 0; for (auto i = ans.begin(); i != ans.end(); i++) { mx = max(mx, (*i)); } cout << mx; }
4
#include <bits/stdc++.h> #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t; cin >> t; while (t--) { long long int n, p, i, c = 0, j; cin >> n >> p; for (i = 1; i <= n; i++) { for (j = i + 1; j <= n; j++) { if (c >= 2 * n + p) break; cout << i << ' ' << j << '\n'; c++; } } cout << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-5; int main() { int n, m; cin >> n >> m; int vote[2][100002]; int votenum[100002]; bool guilty[100002]; memset(vote, 0, sizeof(vote)); memset(votenum, 0, sizeof(votenum)); memset(guilty, false, sizeof(guilty)); int crimnum = 0; for (int i = 0; i < n; i++) { char sign; int num; cin >> sign >> num; if (sign == '+') { crimnum++; vote[0][num]++; votenum[i] = +num; } else { vote[1][num]++; votenum[i] = -num; } } int guilnum = 0; for (int i = 1; i <= n; i++) { if (crimnum - vote[0][i] + vote[1][i] == n - m) { guilty[i] = true; guilnum++; } } for (int i = 0; i < n; i++) { if (votenum[i] > 0) { if (guilnum == 1 && guilty[votenum[i]] == true) { cout << "Truth" << endl; } else if (guilty[votenum[i]] == false) { cout << "Lie" << endl; } else { cout << "Not defined" << endl; } } else { if (guilty[-votenum[i]] == false) { cout << "Truth" << endl; } else { if (guilnum > 1) { cout << "Not defined" << endl; } else { cout << "Lie" << endl; } } } } return 0; }
4
#include <iostream> #include <vector> #include <utility> #include <set> using namespace std; pair<int, int> dfs(int nowpoint, int nowcost, vector<vector<pair<int, int> > >& adjlist, vector<bool>& used){ used[nowpoint] = true; pair<int, int> ans(nowcost, nowpoint); for(size_t i = 0; i < adjlist[nowpoint].size(); i++) if(!used[adjlist[nowpoint][i].second]) ans = max(ans, dfs(adjlist[nowpoint][i].second, nowcost + adjlist[nowpoint][i].first, adjlist, used)); return ans; } int main(void){ while(true){ int n; cin >> n; if(n == 0){ break; } int input, sum = 0; vector<int> p(n, 0); vector<int> d(n, 0); for(int i = 1; i < n; i++){ cin >> input; p[i] = input - 1; } for(int i = 1; i < n; i++){ cin >> d[i]; sum += d[i]; } vector<vector<pair<int, int> > > adjlist(n); for(int i = 1; i < n; i++){ adjlist[i].push_back(make_pair(d[i], p[i])); adjlist[p[i]].push_back(make_pair(d[i], i)); } int leafedge = 0; set<int> leaflist; for(int i = 0; i < n; i++){ if(adjlist[i].size() == 1){ leaflist.insert(i); leafedge += adjlist[i][0].first; } } vector<vector<pair<int, int> > > inner = adjlist; for(int i = 0; i < n; i++){ if(leaflist.count(i) == 1){ inner[i].resize(0); } else{ for(size_t j = 0; j < inner[i].size(); j++){ if(leaflist.count(inner[i][j].second) == 1){ inner[i].erase(inner[i].begin() + j); --j; } } } } vector<bool> used(n, false), used2(n, false);; int start, diam; for(start = 0; start < n && inner[start].size() == 0; start++); if(start == n){ diam = 0; } else { start = dfs(start, 0, inner, used).second; diam = dfs(start, 0, inner, used2).first; } int ans = sum + (sum - leafedge) * 2 - diam; cout << ans << endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; int n,k; long long h[200009],tot; int main() { scanf("%d%d",&n,&k); for(int i=0;i<n;i++) scanf("%lld",h+i); sort(h,h+n); for(int i=0;i<n-k;i++) tot+=h[i]; printf("%lld\n",tot); return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mod = 1000000007; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int main() { int h, w, ai, aj, bi, bj; cin >> h >> w >> ai >> aj >> bi >> bj; vector<vector<pair<ll, ll>>> r(h, vector<pair<ll, ll>>(w, {1e9, 0})); queue<pair<int, int>> q; q.push({ai, aj}); r[ai][aj] = {0, 1}; while (!q.empty()) { auto p = q.front(); q.pop(); vector<pair<int, int>> next; for (int i = 0; i < 4; i++) { next.push_back({p.first + dy[i], p.second + dx[i]}); } next.push_back({p.first, 0}); next.push_back({p.first, w - 1}); next.push_back({0, p.second}); next.push_back({h - 1, p.second}); for (auto e : next) { if (e == p || e.first < 0 || h <= e.first || e.second < 0 || w <= e.second) { continue; } if (r[p.first][p.second].first + 1 == r[e.first][e.second].first) { r[e.first][e.second].second += r[p.first][p.second].second; r[e.first][e.second].second %= mod; } else if (r[p.first][p.second].first + 1 < r[e.first][e.second].first) { r[e.first][e.second].first = r[p.first][p.second].first + 1; r[e.first][e.second].second = r[p.first][p.second].second; q.push(e); } } } // for (auto ee : r) { // for (auto e : ee) { // cerr << e.first << "," << e.second << " "; // } // cerr << endl; // } cout << r[bi][bj].first << " " << r[bi][bj].second << endl; }
0
#include <bits/stdc++.h> int n, m; int a[100100]; int chk[100100]; int ans; int main() { int i, j; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } if (m == 1) { printf("%d", n); return 0; } std::sort(a, a + n); ans = n; j = 0; for (i = 0; i < n; i++) { while (j < i && a[i] / a[j] > m || (a[i] / a[j] == m && a[i] % a[j] != 0)) j++; if (a[i] / a[j] == m && a[i] % a[j] == 0) { if (chk[j] == 0) { ans--; chk[i] = 1; } } } printf("%d", ans); }
1
#include<bits/stdc++.h> #define range(i,a,b) for(int i = (a); i < (b); i++) #define rep(i,b) for(int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; //const int INF = 1e8; using namespace std; double p, q, a, n; const double base = 100; int dfs(int k, int n, int start, double sum){ int res = 0; if(n != 0){ range(i,start,a){ if(k * i > a) break; if(p * i - q < sum * i * q) continue; res += dfs(k * i, n - 1, i, sum + base / i); } } if(abs(p / q - sum) < 1e-10) res++; return res; } int main(){ while(cin >> p >> q >> a >> n, n) { p *= base; cout << dfs(1, n, 1, 0) << endl; } }
0
#include <bits/stdc++.h> int t1[1600001], t2[1600001], w[1600001], R[1600001], lim, n; long long x; const int P = 998244353; inline int mul(const int &a, const int &b) { return 1ll * a * b % P; } inline int sub(int a, const int &b) { a -= b; return (a < 0) ? a + P : a; } inline int add(int a, const int &b) { a += b; return (a >= P) ? a - P : a; } int qsm(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = mul(ans, a); a = mul(a, a); b >>= 1; } return ans; } void NTT(int *a) { for (int i = 0; i < lim; i++) if (i < R[i]) std::swap(a[i], a[R[i]]); for (int i = 1; i < lim; i <<= 1) for (int j = 0; j < lim; j += (i << 1)) for (int k = 0; k < i; k++) { const int Ny = mul(a[i + j + k], w[i + k]); a[i + j + k] = sub(a[j + k], Ny); a[j + k] = add(a[j + k], Ny); } } std::vector<int> operator*(std::vector<int> a, std::vector<int> b) { int size = a.size() + b.size() - 1; lim = 1; while (lim <= size) lim <<= 1; for (int i = 0; i < lim; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) ? lim >> 1 : 0); memset(t1, 0, lim << 2); memset(t2, 0, lim << 2); for (int i = a.size() - 1; ~i; --i) t1[i] = a[i]; for (int i = b.size() - 1; ~i; --i) t2[i] = b[i]; NTT(t1); NTT(t2); for (int i = 0; i < lim; i++) t1[i] = mul(t1[i], t2[i]); NTT(t1); std::reverse(t1 + 1, t1 + lim); int inv = qsm(lim, P - 2); a.resize(size); for (int i = 0; i < size; i++) a[i] = mul(t1[i], inv); return a; } void operator+=(std::vector<int> &a, std::vector<int> b) { if (a.size() < b.size()) a.resize(b.size()); for (int i = 0; i < b.size(); i++) a[i] = add(a[i], b[i]); } struct matrix { std::vector<int> num[2][2]; matrix operator*(matrix a) { matrix c; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { c.num[i][j].clear(); for (int k = 0; k < 2; k++) c.num[i][j] += num[i][k] * a.num[k][j]; } return c; } } mat[100001]; std::vector<int> ans[100001]; int head[100001], nxt[200001], b[200001], k, son[100001], light[100001], top[100001], size[100001], fa[100001]; void push(int s, int t) { nxt[++k] = head[s]; head[s] = k; b[k] = t; } void dfs1(int x, int f) { size[x] = 1; fa[x] = f; for (int i = head[x]; i; i = nxt[i]) if (b[i] != f) { dfs1(b[i], x); size[x] += size[b[i]]; if (size[son[x]] < size[b[i]]) son[x] = b[i]; } } void solve(int l, int r) { if (l == r) return; solve(l, (l + r) >> 1), solve(((l + r) >> 1) + 1, r); mat[l] = mat[((l + r) >> 1) + 1] * mat[l]; } void getans(int x) { int top = 0; for (int tem = x; tem; tem = son[tem]) { for (int j = head[tem]; j; j = nxt[j]) if (b[j] != fa[tem] && b[j] != son[tem]) getans(b[j]); } for (int tem = x; tem; tem = son[tem]) { ++top; if (ans[tem].size() < 1) ans[tem].resize(1); ans[tem][0] = add(ans[tem][0], 1); ans[tem].push_back(0); for (int i = ans[tem].size() - 1; i; i--) ans[tem][i] = ans[tem][i - 1]; ans[tem][0] = 0; mat[top].num[0][0] = mat[top].num[1][0] = ans[tem]; ans[tem].clear(); mat[top].num[0][1].clear(); mat[top].num[1][1].resize(1); mat[top].num[1][1][0] = 1; } solve(1, top); ans[fa[x]] = mat[1].num[1][0]; } int main() { scanf("%d%lld", &n, &x); for (int i = 1, wn; i < (800000); i <<= 1) { wn = qsm(3, (P - 1) / (i << 1)), w[i] = 1; for (int j = 1; j < i; j++) w[i + j] = mul(w[i + j - 1], wn); } for (int i = 1, u, v; i < n; i++) scanf("%d%d", &u, &v), push(u, v), push(v, u); dfs1(1, 0); getans(1); int fin = 0; for (int i = 1, Mul = 1; i < ans[0].size(); i++) { fin = add(fin, mul(ans[0][i], Mul)); Mul = mul(Mul, (x + i) % P); Mul = mul(Mul, qsm(i, P - 2)); } printf("%d\n", fin); }
6
#include <bits/stdc++.h> using namespace std; int a[2005]; int sz[2005]; int adj[2005][2005]; void push(int ind, int n) { for (int i = 0; i < n; ++i) { if (i != ind && a[ind] != a[i]) { adj[ind][sz[ind]++] = i; } } } int val[2005]; int f(int x, int y) { if (x > y) { swap(x, y); } if (a[x] > a[y]) { return 1; } else { return 0; } } void solve() { int n; scanf("%d ", &n); for (int i = 0; i < n; ++i) { scanf("%d ", &a[i]); a[i] = abs(a[i]); } set<pair<int, int> > s; int ret = 0; for (int i = 0; i < n; ++i) { push(i, n); for (int j = 0; j < sz[i]; ++j) { val[i] += f(i, adj[i][j]); } ret += val[i]; a[i] *= -1; for (int j = 0; j < sz[i]; ++j) { val[i] -= f(i, adj[i][j]); } a[i] *= -1; s.insert({val[i], i}); } assert(ret % 2 == 0); ret /= 2; while (s.size() > 0 && (*prev(s.end())).first > 0) { pair<int, int> v = *prev(s.end()); s.erase(v); ret -= v.first; v.first *= -1; s.insert(v); int id = v.second; for (int i = 0; i < sz[id]; ++i) { int vv = f(id, adj[id][i]); s.erase({val[adj[id][i]], adj[id][i]}); val[adj[id][i]] -= f(id, adj[id][i]); a[adj[id][i]] *= -1; val[adj[id][i]] += f(id, adj[id][i]); a[adj[id][i]] *= -1; val[id] *= -1; val[adj[id][i]] += f(id, adj[id][i]); a[adj[id][i]] *= -1; val[adj[id][i]] -= f(id, adj[id][i]); a[adj[id][i]] *= -1; val[id] *= -1; s.insert({val[adj[id][i]], adj[id][i]}); } val[id] *= -1; a[id] *= -1; } printf("%d\n", ret); } int main() { solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long x, y, sum; signed main() { cin >> x >> y; sum = 1; for (long long i = x + 1; i <= y; i++) { (sum *= i % 10) %= 10; if (sum == 0) break; } cout << sum << endl; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, v[100007], rest, ans, type, x; cin >> n >> k; rest = n + 1; for (int i = 0; i < k; i++) { cin >> x; if (n % x < rest) { rest = n % x; ans = n / x; type = i; } } cout << type + 1 << " " << ans; }
2
#include <bits/stdc++.h> using namespace std; int main() { int m, n, i, j, k, ans = 0; map<int, int> a; cin >> m; for (i = 1; i <= m; i++) { cin >> n; a[n]++; if (a[n] > a[ans]) { ans = n; } } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; int T[4 * 100005]; int Lz[4 * 100005]; long long update(int n, int l, int r, int i, int j, int val) { if (Lz[n]) { T[n] |= Lz[n]; if (l != r) { Lz[2 * n] |= Lz[n]; Lz[2 * n + 1] |= Lz[n]; } Lz[n] = 0; } if (i <= l and j >= r) { T[n] |= val; if (l != r) { Lz[2 * n] |= val; Lz[2 * n + 1] |= val; } Lz[n] = 0; return T[n]; } if (l > j or r < i) return T[n]; int mid = (l + r) / 2; long long x = update(2 * n, l, mid, i, j, val); long long y = update(2 * n + 1, mid + 1, r, i, j, val); T[n] = (x & y); return T[n]; } long long query(int n, int l, int r, int i, int j) { if (Lz[n]) { T[n] |= Lz[n]; if (l != r) { Lz[2 * n] |= Lz[n]; Lz[2 * n + 1] |= Lz[n]; } Lz[n] = 0; } if (i <= l and j >= r) { return T[n]; } if (l > j or r < i) return -1; int mid = (l + r) / 2; long long x = query(2 * n, l, mid, i, j); long long y = query(2 * n + 1, mid + 1, r, i, j); return (x & y); } int L[100005]; int R[100005]; int q[100005]; int main() { int n, m, x, cnt = 0; scanf(" %d %d", &n, &m); for (int i = 0; i < m; i++) { scanf(" %d %d %d", &L[i], &R[i], &q[i]); x = q[i]; cnt = 0; update(1, 0, n - 1, L[i] - 1, R[i] - 1, x); } for (int _i = 0; _i < m; _i++) { x = query(1, 0, n - 1, L[_i] - 1, R[_i] - 1); if (x != q[_i]) { puts("NO"); return 0; } } puts("YES"); for (int _i = 0; _i < n; _i++) { x = query(1, 0, n - 1, _i, _i); if (_i) printf(" "); printf("%d", x); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAGIC1 = 31; const int MAGIC2 = 127; const int INVM1 = 129032259; const int INVM2 = 913385835; const int MOD1 = 1e9 + 7; const int MOD2 = 1e9 + 9; long long power(long long a, long long n, long long MOD) { long long ret = 1, carry = a; while (n > 0) { if (n & 1) ret = ret * carry % MOD; carry = carry * carry % MOD; n >>= 1; } return ret; } map<pair<long long, long long>, int> base; int n, m, k; char s[2000005]; long long hash1[2000005], hash2[2000005]; int main() { scanf("%d %d", &n, &k); scanf("%s", s); n *= k; for (int i = 0; i < k; i++) s[i + n] += s[i]; hash1[0] = hash2[0] = 0; for (int i = 0; i < k; i++) hash1[0] = (hash1[0] * MAGIC1 + s[i] - 'a' + 1) % MOD1, hash2[0] = (hash2[0] * MAGIC2 + s[i] - 'a' + 1) % MOD2; long long pow1 = power(MAGIC1, k, MOD1), pow2 = power(MAGIC2, k, MOD2); for (int i = 1; i < n; i++) { hash1[i] = ((hash1[i - 1] * MAGIC1 + s[i + k - 1] - 'a' + 1 - pow1 * (s[i - 1] - 'a' + 1)) % MOD1 + MOD1) % MOD1; hash2[i] = ((hash2[i - 1] * MAGIC2 + s[i + k - 1] - 'a' + 1 - pow2 * (s[i - 1] - 'a' + 1)) % MOD2 + MOD2) % MOD2; } scanf("%d", &m); for (int i = 0; i < m; i++) { char ss[200005]; scanf("%s", ss); long long h1 = 0; long long h2 = 0; for (int j = 0; j < k; j++) h1 = (h1 * MAGIC1 + ss[j] - 'a' + 1) % MOD1, h2 = (h2 * MAGIC2 + ss[j] - 'a' + 1) % MOD2; base[make_pair(h1, h2)] = i; } for (int j = 0; j < k; j++) { vector<int> ans; set<pair<long long, long long> > vis; bool fail = false; for (int i = j; i < j + n; i += k) { pair<long long, long long> hash = make_pair(hash1[i], hash2[i]); if (vis.find(hash) != vis.end() || base.find(hash) == base.end()) { fail = true; break; } vis.insert(hash); ans.push_back(base[hash]); } if (fail) continue; printf("YES\n"); for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i] + 1); printf("\n"); return 0; } printf("NO\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; //point typedef complex<double> P; namespace std { bool operator < (const P& a, const P& b) { return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } bool cmp_y(const P &a, const P &b){ return a.imag() != b.imag() ? a.imag() < b.imag() : a.real() < b.real(); } } double cross(const P& a, const P& b) { return imag(conj(a)*b); } double dot(const P& a, const P& b) { return real(conj(a)*b); } P n_vector(P a){ double l = abs(a); return P(-a.imag(), a.real())/l; } // line struct L : public vector<P> { L(const P& a, const P& b) { push_back(a); push_back(b); } }; P projection(const L &l, const P &p) { double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]); return l[0] + t*(l[0]-l[1]); } double distanceLP(const L &l, const P &p) { return abs(p - projection(l, p)); } // circle struct C { P p; double r; C(const P& p, double r) : p(p), r(r) {} }; int intersectCC(const C& a, const C& b) { double dist = sqrt(norm(a.p-b.p)), r1 = a.r + b.r, r2 = abs(a.r - b.r); if(r1 < dist) return 4; //??\????????? if(dist == r1) return 3; //?????\ if(r2 < dist && dist < r1) return 2; //????????? if(dist == r2) return 1; //?????\ return 0; //?????? } vector<P> crossPointCL(C c, L l) { double d = distanceLP(l, c.p), r = c.r; P m = projection(l, c.p); P x = sqrt(r*r-d*d)/abs(l[1]-l[0])*(l[1]-l[0]); vector<P> ret(2,m); ret[0] -= x; ret[1] += x; sort(ret.begin(), ret.end()); //!!! return ret; } vector<P> crossPointCC(C a, C b) { double d = abs(a.p-b.p); double t = (a.r*a.r-b.r*b.r+d*d)/2/d, h = sqrt(a.r*a.r-t*t); P m = t/abs(b.p-a.p)*(b.p-a.p)+a.p; P n = n_vector(a.p-b.p); vector<P> ret(2, m); ret[0] -= h*n; ret[1] += h*n; sort(ret.begin(), ret.end()); //!!! return ret; } vector<P> tangentLC(P p, C c) { C c2 = C((p+c.p)/2.0, abs(p-c.p)/2.0); return crossPointCC(c, c2); } int main() { double x1, y1, r1, x2, y2; cin >> x2 >> y2 >> x1 >> y1 >> r1; C c1(P{x1, y1}, r1); P p(x2, y2); vector<P> ans = tangentLC(p, c1); cout << fixed << setprecision(15) << ans[0].real()+0 << " " << ans[0].imag()+0 << endl << ans[1].real()+0 << " " << ans[1].imag()+0 << endl; }
0
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, e = 0, o = 0; cin >> n; for (long long int i = 0; i < n; i++) { long long int a; cin >> a; if (a % 2 == 0) e++; else o++; } cout << min(e, o); }
1
#include <bits/stdc++.h> using namespace std; int n; int x[1000005]; int father[1000005]; int getfather(int x1) { return x1 == father[x1] ? x1 : father[x1] = getfather(father[x1]); } void merge(int a, int b) { int _a = getfather(a); int _b = getfather(b); father[_b] = father[_a]; } bool dian[1000005]; int main() { int m, n; cin >> n >> m; long long huan = 0; int a, b; for (int i = 1; i <= n; i++) father[i] = i; for (int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); dian[a] = dian[b] = true; if (a != b) { x[a]++; x[b]++; } else huan++; merge(a, b); } int t = -1; bool flag = true; for (int i = 1; i <= n; i++) if (t == -1 && dian[i]) t = getfather(i); else if (dian[i] && t != getfather(i)) flag = false; if (!flag) { cout << 0 << endl; return 0; } long long ans = 0; long long tmp = 0; for (int i = 1; i <= n; i++) { ans += 1LL * x[i] * (x[i] - 1) / 2; tmp += 1LL * x[i] * huan; } ans += tmp / 2; ans += 1LL * huan * (huan - 1) / 2; cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; vi a, b; inline void print(const vi &a) { printf("%d\n", (int)a.size()); for (auto &i : a) printf("%d", i); } int main() { int n, k; scanf("%d %d", &n, &k); a.resize(n); for (int i = 0; i < n; ++i) { char c; scanf(" %c", &c); a[i] = c - '0'; } b = a; for (int i = k; i < n; ++i) { b[i] = b[i - k]; } if (b >= a) { print(b); return 0; } for (int i = k - 1; i >= 0; --i) { if (a[i] != 9) { ++a[i]; for (int j = i + 1; j < k; ++j) { a[j] = 0; } for (int j = k; j < n; ++j) { a[j] = a[j - k]; } print(a); return 0; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int aa[100003]; int n, k, z, need, x; int main() { cin >> n >> k; for (auto i = 0; i < n; i++) { cin >> x; z += x / 10; aa[i] = x % 10; } sort(aa, aa + n, greater<int>()); for (auto i = 0; i < n; i++) { need = 10 - aa[i]; if (k >= need) { k = k - need; z++; } } z += k / 10; cout << min(z, 10 * n); return 0; }
3
#include <bits/stdc++.h> using namespace std; char s[2000005]; char s1[2000005]; int main() { int n; int sum; int p; while (~scanf("%d", &n)) { memset(s, 0, sizeof(s)); int max1 = 0; for (int c = 1; c <= n; c++) { scanf("%s", s1); int l = strlen(s1); scanf("%d", &sum); int t = -100; for (int i = 1; i <= sum; i++) { scanf("%d", &p); p--; for (int j = max(p, t); j < p + l; j++) s[j] = s1[j - p]; t = p + l; max1 = max(max1, p + l - 1); } } for (int i = 0; i <= max1; i++) { if (s[i] == 0) printf("a"); else printf("%c", s[i]); } printf("\n"); } }
3
#include <bits/stdc++.h> #define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++) using namespace std; typedef long long ll; typedef pair<ll, ll> pll; int main(void) { ll N, M; while(cin >> N >> M, N) { vector<vector<pll>> E(N); REP(i, 0, M) { ll t, s, d; cin >> t >> s >> d; s--; d--; E[s].push_back(pll(t, d)); } vector<bool> used(N, false); priority_queue<pll, vector<pll>, greater<pll>> q; q.push(pll(0, 0)); while(q.size()) { ll v = q.top().second, t = q.top().first; q.pop(); if(used[v]) continue; used[v] = true; for(pll p : E[v]) { ll nv = p.second, nt = p.first; if(nt >= t) { q.push(pll(nt, nv)); } } } ll ans = 0; REP(i, 0, N) if(used[i]) ans++; cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; using pll = pair<long long, long long>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long gcd_helper(long long a, long long b) { if (a % b == 0) return b; else return gcd_helper(b, a % b); } long long gcd(long long a, long long b) { if (a < 0) a *= -1; if (b < 0) b *= -1; if (a < b) swap(a, b); if (b == 0) return a; return gcd_helper(a, b); } long long powr(long long a, long long b, long long M) { if (b == 0) return 1; if (a == 0) return 0; long long ret = 1; while (b) { if (b & 1) { ret *= a; ret %= M; } a = (a * a) % M; b /= 2; } return ret; } long long find(long long x, vector<long long> &p) { if (p[x] != x) p[x] = find(p[x], p); return p[x]; } void unite(long long x, long long y, vector<long long> &p, vector<long long> &sz) { long long px = find(x, p); long long py = find(y, p); if (sz[px] < sz[py]) { swap(px, py); swap(x, y); } p[py] = px; sz[px] += sz[py]; } vector<vector<long long> > adj; vector<bool> vis; vector<string> s; vector<long long> t, lo; long long ans, n, m, tme; bool isnbr(long long i, long long j) { if (i < 0 || i >= n || j < 0 || j >= m) return false; if (s[i][j] == '#') { return true; } return false; } long long vindex(long long i, long long j) { long long ret = i * m + j; return ret; } void dfs(long long u, long long p = -1) { vis[u] = true; t[u] = lo[u] = ++tme; long long child = 0; for (long long v : adj[u]) { if (v == p) continue; if (vis[v]) lo[u] = min(lo[u], lo[v]); else { dfs(v, u); child++; lo[u] = min(lo[u], lo[v]); if (lo[v] >= t[u] && p != -1) { ans = 1; } } } if (p == -1 && child > 1) { ans = 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; s.resize(n); long long ctr = 0; for (long long i = 0; i < (n); i++) { cin >> s[i]; for (char ch : s[i]) { if (ch == '#') { ctr++; } } } if (ctr < 3) { cout << -1 << endl; return 0; } ans = 2; adj.resize(n * m, vector<long long>(0)); vis.resize(n * m, false); t.resize(n * m, 0); lo.resize(n * m, 0); tme = 0; long long u, v; long long rt = -1; for (long long i = 0; i < (n); i++) { for (long long j = 0; j < (m); j++) { if (s[i][j] == '#') { rt = u = vindex(i, j); if (isnbr(i - 1, j)) { v = vindex(i - 1, j); adj[u].push_back(v); } if (isnbr(i + 1, j)) { v = vindex(i + 1, j); adj[u].push_back(v); } if (isnbr(i, j - 1)) { v = vindex(i, j - 1); adj[u].push_back(v); } if (isnbr(i, j + 1)) { v = vindex(i, j + 1); adj[u].push_back(v); } } } } dfs(rt); cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 300005; int n; long long x1, x2; struct sss { long long val; int num; } a[maxn]; bool cmp(const sss &a, const sss &b) { return a.val > b.val; } void scanff() { scanf("%d%I64d%I64d", &n, &x1, &x2); for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i].val); a[i].num = i; } sort(a + 1, a + n + 1, cmp); } void work() { int now1, now2; bool flag = false; if (x1 < x2) { swap(x1, x2); flag = true; } for (now1 = 1; now1 <= n; now1++) { if (a[now1].val * now1 >= x1) break; } if (now1 > n) { printf("No\n"); return; } for (now2 = now1 + 1; now2 <= n; now2++) { if (a[now2].val * (now2 - now1) >= x2) break; } if (now2 > n) { int head = 1, tail = now1; for (;;) { if (tail + 1 <= n) { if (a[tail + 1].val * (tail - head + 1) >= x1) { tail++; head++; if (a[head - 1].val * (head - 1) >= x2) { printf("Yes\n"); if (!flag) { printf("%d %d\n", tail - head + 1, head - 1); for (int i = head; i <= tail; i++) { printf("%d ", a[i].num); } printf("\n"); for (int i = 1; i <= head - 1; i++) { printf("%d ", a[i].num); } return; } else { printf("%d %d\n", head - 1, tail - head + 1); for (int i = 1; i <= head - 1; i++) { printf("%d ", a[i].num); } printf("\n"); for (int i = head; i <= tail; i++) { printf("%d ", a[i].num); } return; } } } else { tail++; } } else break; } printf("No\n"); return; } printf("Yes\n"); if (!flag) { printf("%d %d\n", now1, now2 - now1); for (int i = 1; i <= now1; i++) { printf("%d ", a[i].num); } printf("\n"); for (int i = now1 + 1; i <= now2; i++) { printf("%d ", a[i].num); } } else { printf("%d %d\n", now2 - now1, now1); for (int i = now1 + 1; i <= now2; i++) { printf("%d ", a[i].num); } printf("\n"); for (int i = 1; i <= now1; i++) { printf("%d ", a[i].num); } } } int main() { scanff(); work(); return 0; }
4
#include<cstdio> #include<algorithm> using namespace std; inline int read() { int x;char c; while((c=getchar())<'0'||c>'9'); for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0'; return x; } #define MN 200000 struct edge{int nx,t;}e[MN*2+5]; int h[MN+5],en,d[MN+5],S[MN+5]; char s[MN+5]; long long ans; inline void ins(int x,int y) { e[++en]=(edge){h[x],y};h[x]=en; e[++en]=(edge){h[y],x};h[y]=en; } void dfs(int x,int fa) { S[x]=s[x]-'0'; for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa) { dfs(e[i].t,x); d[x]=max(d[x],d[e[i].t]+1); S[x]+=S[e[i].t]; } } void solve(int x,int fa,int df,int sf) { if(fa&&min(S[x]?d[x]:1e9,sf?df-1:1e9)<=min(d[x],df-1))++ans; int d1=0,d2=0,d3=s[x]^'0'?0:1e9; for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa) { if(d[e[i].t]>=d1)d2=d1,d1=d[e[i].t]+1; else if(d[e[i].t]>=d2)d2=d[e[i].t]+1; if(S[e[i].t])d3=min(d3,d[e[i].t]+1); } for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa) solve(e[i].t,x,max(df,d1==d[e[i].t]+1?d2:d1)+1,sf+S[x]-S[e[i].t]); if(df>d1)d2=d1,d1=df;else if(df>d2)d2=df; if(sf)d3=min(d3,df); if(d3<=d2)ans+=d2-d3+1; } int main() { int n=read(),i; for(i=1;i<n;++i)ins(read(),read()); scanf("%s",s+1); dfs(1,0);solve(1,0,0,0); printf("%lld",ans); }
0
#include <bits/stdc++.h> using namespace std; long long xorr(long long x) { if (x % 4 == 0) return x; if (x % 4 == 3) return 0; if (x % 4 == 1) return 1; return (x + 1); } int main() { long long ans = 0; int n; scanf("%d", &n); while (n--) { long long x, m; scanf("%lld", &x); scanf("%lld", &m); long long first = x - 1; long long l = x + m - 1; ans = ans ^ (xorr(first) ^ xorr(l)); } if (ans == 0) cout << "bolik"; else cout << "tolik"; }
3
#include<bits/stdc++.h> using namespace std; int a,b; string c; int main(){ cin>>a>>b; c=(a%3==0||b%3==0||(a+b)%3==0)?"Possible":"Impossible"; cout<<c<<endl; return 0; }
0
#include<iostream> #include<cstdio> using namespace std; int main(){ int T;scanf("%d",&T); while(T--){ int n,k1,k2,w,b; scanf("%d%d%d%d%d",&n,&k1,&k2,&w,&b); int x1=max(k1,k2),y1=min(k1,k2); int x2=max(n-k1,n-k2),y2=min(n-k1,n-k2); if(w<=y1+(x1-y1)/2&&b<=y2+(x2-y2)/2) puts("YES"); else puts("NO"); } return 0; } //A
1
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; using ul = unsigned long long; using vec = vector<int>; using vec2 = vector<vec>; vec s,t; int n; ul ans[510][510]; bool valid(vec x,vec y,vec2 &mp){ rep(i,n){ int m = mp[i][0]; if(s[i])rep(j,n)m|=mp[i][j]; else rep(j,n)m&=mp[i][j]; if(m!=x[i])return false; } rep(j,n){ int m = mp[0][j]; if(t[j])rep(i,n)m|=mp[i][j]; else rep(i,n)m&=mp[i][j]; if(m!=y[j])return false; } return true; } bool solve(vec x,vec y,ul k){ auto mp = vec2(n,vec(n,-1)); rep(i,n)rep(j,n){ if(x[i]^s[i])mp[i][j]=x[i]; if(y[j]^t[j]){ if((mp[i][j]^y[j])==1)return false; mp[i][j] = y[j]; } } int cx=n,cy = n; rep(i,n)cx-=x[i]^s[i]; rep(i,n)cy-=y[i]^t[i]; if(cx > 1 and cy > 1){ int cnt = 0; rep(i,n){ if(!(x[i]^s[i])){ int p = cnt++; rep(j,n)if(!(y[j]^t[j]))mp[i][j] = p++&1; } } } int c[2]={}; if(cx == 1){ rep(i,n)if(x[i]^s[i])c[x[i]]++; if(c[0] and c[1]){ rep(i,n)rep(j,n)if(mp[i][j]==-1)mp[i][j]=(i+j)&1; } else{ rep(i,n)rep(j,n){ if(mp[i][j]==-1){ mp[i][j]=(c[t[j]]?x[i]:t[j]); } } } } else if(cy == 1){ rep(i,n) if(y[i]^t[i])c[y[i]]++; if(c[0] and c[1]) { rep(i,n)rep(j,n)if(mp[i][j]==-1)mp[i][j] = (i+j)&1; } else{ rep(i,n)rep(j,n){ if(mp[i][j]==-1){ mp[i][j]=(c[s[i]]?y[j]:s[i]); } } } } if(!valid(x,y,mp))return false; rep(i,n)rep(j,n)ans[i][j] += k*mp[i][j]; return true; } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);cout<<fixed<<setprecision(15); cin>>n; vector<ul> u(n),v(n); s.resize(n);t.resize(n); rep(i,n)cin>>s[i];rep(i,n)cin>>t[i];rep(i,n)cin>>u[i];rep(i,n)cin>>v[i]; rep(_,64){ vec x(n),y(n); rep(i,n)x[i]=u[i]&1,y[i]=v[i]&1,u[i]>>=1,v[i]>>=1; if(!solve(x,y,1ULL<<_)){ cout<<-1<<endl; return 0; } } rep(i,n)rep(j,n){cout<<ans[i][j]<<" ";}cout<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; vector<vector<int> > neighbours(maxn); vector<pair<int, int> > path; void inline dfs(int u, int parent, int x, int d) { int y = x; path.push_back(make_pair(u + 1, y)); for (int i = 0; i < neighbours[u].size(); i++) { int v = neighbours[u][i]; if (v == parent) continue; if (y == d) { y = d - (int)neighbours[u].size(); path.push_back(make_pair(u + 1, y)); } dfs(v, u, y + 1, d); y++; path.push_back(make_pair(u + 1, y)); } if (y == (x + (int)neighbours[u].size() - 1)) { y = x - 1; path.push_back(make_pair(u + 1, y)); } } int main() { int n; cin >> n; int a, b; for (int i = 0; i < n - 1; i++) { cin >> a >> b; a--, b--; neighbours[a].push_back(b), neighbours[b].push_back(a); } int maxd = 0; for (int i = 0; i < n; i++) maxd = max(maxd, (int)neighbours[i].size()); dfs(0, -1, 0, maxd); cout << path.size() << endl; for (int i = 0; i < path.size(); i++) cout << path[i].first << ' ' << path[i].second << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long c, sum, ans = 0; cin >> c >> sum; int m = sum % c; int avg = sum / c; for (int i = 1; i <= m; i++) { ans = ans + (avg + 1) * (avg + 1); } for (int i = 1; i <= c - m; i++) { ans = ans + avg * avg; } cout << ans << endl; } return 0; }
1
#include <iostream> using namespace std; unsigned long long phi(unsigned long long n){ if(n == 1) return 1; unsigned long long r = n; for(long long i = 2; i <= n; i++){ if(!(n % i)){ r = r * (i - 1) / i; while (!(n % i)){ n /= i; } } } return r; } int main(){ unsigned long long n; cin >> n; cout << phi(n) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int a, b, x, y; cin >> a >> b >> x >> y; long long int r = max({b * (x), a * (y), (a - x - 1) * b, a * (b - y - 1)}); cout << r << endl; } }
1
#include <bits/stdc++.h> using namespace std; const int maxs = 100; const int maxn = 2e5 * 64; int n; char s[maxs]; vector<int> num; vector<char> sig; bool ins[maxs]; int ch[maxn][2]; int sum[maxn][2]; short typ[maxn]; int root, tot; void error() { puts("-1"); exit(0); } inline int newnode(int& now) { return now ? now : now = ++tot; } void insert(int deplim, int type) { int now = newnode(root); ++sum[now][type]; for (int i = 31; i >= deplim; --i) { now = newnode(ch[now][ins[i]]); ++sum[now][type]; } if (typ[now] == -1) typ[now] = type; else if (typ[now] != type) error(); } void init() { memset(typ, -1, sizeof(typ)); int deplim, type; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%s", s); int ret = 0; bool numf = 0; num.clear(); sig.clear(); int j = 0; while (s[j]) { if (isdigit(s[j])) numf = 1, ret = ret * 10 + s[j] - '0'; else { sig.push_back(s[j]); if (numf) num.push_back(ret), numf = 0, ret = 0; } ++j; } if (numf) num.push_back(ret); if (sig[0] == '+') type = 0; else type = 1; deplim = 32; if (num.size() == 5) deplim = num[4]; for (int j = 0; j < 4; ++j) for (int k = 7; ~k; --k) ins[((3 - j) << 3 | k)] = (num[j] >> k) & 1; insert(32 - deplim, type); } } vector<string> ans; string tmp; void calc(string); void dfs(int rt) { if (!sum[rt][1]) return; if (!sum[rt][0]) { ans.push_back(tmp); return; } if (typ[rt] != -1) error(); for (int i = 0; i < 2; ++i) { tmp += i + '0'; dfs(ch[rt][i]); tmp.pop_back(); } } void calc(string ai) { int l = ai.length(); while (ai.length() < 32) ai += '0'; for (int i = 0; i < 4; ++i) { int tmp = 0; for (int j = 0; j < 8; ++j) tmp = (tmp << 1) + ai[i << 3 | j] - '0'; if (i) cout << '.'; cout << tmp; } cout << '/' << l << endl; return; } int main() { init(); dfs(root); cout << ans.size() << endl; for (auto i : ans) calc(i); return 0; }
2
#include <bits/stdc++.h> #define REP(i,n)for (int i=0;i<(n);i++) #define PB push_back #define MP make_pair #define ALL(a) (a).begin(),(a).end() #define ll long long using namespace std; int d[5]={5,7,5,7,7}; int num[40]; int n; bool rec(int s){ int sum=0; int t[5]; REP(i,5)t[i]=d[i]; int ss=0; for(int i=s;i<n;i++){ if(t[ss]>0)t[ss]-=num[i]; else{ ss++; t[ss]-=num[i]; } } REP(i,5)if(t[i]!=0)return false; return true; } int main(){ while(1){ cin>>n; if(n==0)break; REP(i,n){ string s;cin>>s; num[i]=s.size(); } REP(i,n){ if(rec(i)){ cout<<i+1<<endl; break; } } } }
0
#include <bits/stdc++.h> using namespace std; void lines(int n, int l = 2) { char first = '#', last = '#', mid = '#'; if (l == 1) last = mid = '.'; if (l == 3) first = mid = '.'; cout << first; for (int i = 1; i < n - 1; i++) cout << mid; cout << last << endl; } int main() { int n, m, i, j, dir = 0; cin >> n >> m; for (i = 0; i < n; i++) { if (i % 2 == 0) lines(m); else { if (dir == 0) { lines(m, 3); dir = 1; } else { lines(m, 1); dir = 0; } } } return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4") using namespace std; using ll = long long; using ld = long double; using itn = int; using dd = double; mt19937 gen(41); const dd eps = 1e-7; const ll MAXN = 2097152; const ll INF = 1e9; const dd pi = acos(dd(-1)); ll gcd(ll a, ll b) { a = abs(a); b = abs(b); while (b) { a %= b; swap(a, b); } return a; } ll binpow(ll a, ll n, ll binpow_mod = 1e9 + 7) { ll res = 1; while (n) { if (n & 1) res *= a; a *= a; n >>= 1; a %= binpow_mod; res %= binpow_mod; } return res; } struct Point { ll x, y; bool z; void scan() { cin >> x >> y; } Point(ll X = 0, ll Y = 0, bool Z = 0) { x = X; y = Y; z = Z; } ll operator*(Point &a) { return x * a.y - y * a.x; } bool operator==(Point &a) { return x == a.x && y == a.y; } }; ll n, m, k; deque<ll> a; ll solve() { vector<ll> b((n + m - 1) / m, 0), c((n + m - 1) / m, -1e10); ll now = 0; for (int i = 0; i < n; i++) { now += a[i]; c[i / m] = max(c[i / m], now); if (i % m == m - 1) { b[i / m] = now; now = 0; } } ll ans = c[0] - k, sum = 0, min_sum = 0; for (int r = 0; r < b.size(); ++r) { sum += c[r] - k; ans = max(ans, sum - min_sum); sum = sum - c[r] + b[r]; min_sum = min(min_sum, sum); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); cin >> n >> m >> k; a.resize(n); for (int i = 0; i < n; i++) { cin >> a[i]; } ll res = 0; for (int k = 0; k < min(n, m); k++) { res = max(res, solve()); a.pop_front(); n = a.size(); } cout << res; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long powermod(long long _a, long long _b, long long _m) { long long _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a) % _m; _b /= 2; _a = (_a * _a) % _m; } return _r; } long long string_to_number(string s) { long long x = 0; stringstream convert(s); convert >> x; return x; } long long add(long long a, long long b) { long long x = (a + b) % 1000000007; return x; } long long mul(long long a, long long b) { long long x = (a * b) % 1000000007; return x; } long long sub(long long a, long long b) { long long x = (a - b + 1000000007) % 1000000007; return x; } long long divi(long long a, long long b) { long long x = a; long long y = powermod(b, 1000000007 - 2, 1000000007); long long res = (x * y) % 1000000007; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; if (n == 1) { int m; cin >> m; if (m % 2 == 1) cout << 3; else cout << 2; } else { int sum = 0; for (int i = 0; i < n; i++) { int x; cin >> x; sum += x; } int cnt = 0; for (int i = 1; i <= 5; i++) { if (((sum + i) % (n + 1)) != 1) cnt++; } cout << cnt; } return 0; }
1
#include <algorithm> #include <queue> #include <stack> #define REP(i,n) for(int i=0; i<(int)(n); i++) #include <iostream> #include <set> using namespace std; char makeChr(char c, int p){ const int len = ('Z' - 'A' + 1); return 'A' + (int)(c - 'A' + p + len * 100) % len; } int main(){ string s; while(cin >> s, s != "."){ vector<char> ans; int p = 0; stack<int> st; REP(i,s.size()){ if(s[i] == '+') p++; else if(s[i] == '-') p--; else if(s[i] == '[') st.push(ans.size()); else if(s[i] == ']'){ reverse(ans.begin() + st.top(), ans.end()); st.pop(); }else{ if(s[i] == '?') ans.push_back('A'); else ans.push_back(makeChr(s[i], p)); p = 0; } } cout << string(ans.begin(), ans.end()) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct Centroid_Decomposition { int n, root; vector<vector<int>> G, CDG; vector<int> sub, depth; vector<bool> visited; Centroid_Decomposition(int _n) { n = _n; G.resize(n + 1); CDG.resize(n + 1); sub.resize(n + 1); depth.resize(n + 1); visited.resize(n + 1); } void push(int a, int b) { G[a].push_back(b); G[b].push_back(a); } int dfs(int v, int p) { if (visited[v]) return 0; sub[v] = 1; for (int x : G[v]) if (x != p) sub[v] += dfs(x, v); return sub[v]; } int find_centroid(int v, int p, int t_size) { for (int x : G[v]) if (!visited[x] && x != p && sub[x] > t_size / 2) return find_centroid(x, v, t_size); return v; } void build(int v, int p) { int t_size = dfs(v, p); int centroid = find_centroid(v, p, t_size); visited[centroid] = true; if (p == -1) { p = centroid; root = centroid; depth[root] = 1; } else { CDG[centroid].push_back(p); CDG[p].push_back(centroid); } for (int x : G[centroid]) if (!visited[x]) build(x, centroid); } void init_depth(int v, int p) { for (int x : CDG[v]) if (x != p && x != v) { depth[x] = depth[v] + 1; init_depth(x, v); } } int operator[](int i) { return depth[i]; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; Centroid_Decomposition ranks(n); for (int i = 1; i <= n - 1; i++) { int a, b; cin >> a >> b; ranks.push(a, b); } ranks.build(1, -1); ranks.init_depth(ranks.root, -1); for (int i = 1; i <= n; i++) cout << (char)(64 + ranks[i]) << " "; }
3