solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main(void) { int a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; int l = b + a + c; return cout << l * l - a * a - c * c - e * e << '\n', 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 105; int a[N]; bool judge(int n) { int state = 1; for (int i = 2; i <= n; i++) { if (state == 1) { if (a[i] < a[i - 1]) state = 3; else if (a[i] == a[i - 1]) state = 2; } else if (state == 2) { if (a[i] < a[i - 1]) state = 3; else if (a[i] > a[i - 1]) return false; } else { if (a[i] >= a[i - 1]) return false; } } return true; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); judge(n) ? puts("YES") : puts("NO"); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<long long> pows2; map<long long, long long> mp; long long rec(long long n) { if (mp.find(n) != mp.end()) return mp[n]; if (n <= 1) return 0; long long ans = 0; int index = 0; for (int i = 0; i < 60; i++) { if (pows2[i] < n) { index = i; } else break; } ans = rec(pows2[index]) + rec(n - pows2[index]) + pows2[index]; mp[n] = ans; return ans; } int main() { ios::sync_with_stdio(false); pows2.push_back(1); for (int i = 0; i < 60; i++) pows2.push_back(2 * pows2.back()); long long n; cin >> n; long long ans = 0; cout << rec(n) << endl; return 0; }
5
/* Author: QAQAutomaton Lang: C++ Code: F.cpp Mail: [email protected] Blog: https://www.qaq-am.com/ */ #include<bits/stdc++.h> #define int long long #define debug(...) fprintf(stderr,__VA_ARGS__) #define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define all(x) x.begin(),x.end() #define x first #define y second using namespace std; typedef long long ll; typedef pair<int,int> pii; int inf; const double eps=1e-8; const double pi=acos(-1.0); template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;} template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;} template<class T>T sqr(T a){return a*a;} template<class T>T mmin(T a,T b){return a<b?a:b;} template<class T>T mmax(T a,T b){return a>b?a:b;} template<class T>T aabs(T a){return a<0?-a:a;} template<class T>int dcmp(T a,T b){return a>b;} template<int *a>int cmp_a(int x,int y){return a[x]<a[y];} #define min mmin #define max mmax #define abs aabs struct __INIT__{ __INIT__(){ memset(&inf,0x3f,sizeof(inf)); } }__INIT___; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) // print the remaining part inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc (char x) { *oS ++ = x; if (oS == oT) flush (); } template<typename A> inline bool read (A &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; return 1; } inline bool read (char &x) { while((x=gc())==' '||x=='\n' || x=='\r'); return x!=EOF; } inline bool read(char *x){ while((*x=gc())=='\n' || *x==' '||*x=='\r'); if(*x==EOF)return 0; while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc(); *x=0; return 1; } template<typename A,typename ...B> inline bool read(A &x,B &...y){ return read(x)&&read(y...); } template<typename A> inline bool write (A x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); return 0; } inline bool write (char x) { putc(x); return 0; } inline bool write(const char *x){ while(*x){putc(*x);++x;} return 0; } inline bool write(char *x){ while(*x){putc(*x);++x;} return 0; } template<typename A,typename ...B> inline bool write(A x,B ...y){ return write(x)||write(y...); } //no need to call flush at the end manually! struct Flusher_ {~Flusher_(){flush();}}io_flusher_; } using io :: read; using io :: putc; using io :: write; const int p=1e9+7; int f[55][55][55],c[55][55]; signed main(){ #ifdef QAQAutoMaton freopen("F.in","r",stdin); freopen("F.out","w",stdout); #endif int n; read(n); f[1][1][1]=1; for(int i=0;i<=n;++i){ c[i][0]=c[i][i]=1; for(int j=1;j<i;++j)c[i][j]=(c[i-1][j]+c[i-1][j-1])%p; } for(int i=1;i<n;++i){ for(int j=1;j<=i;++j){ for(int k=0;k<=j;++k){ for(int l=0;l<=j;++l){ for(int w=max(k-l,0LL);w+l-k+w+i<=n;++w){ if(w+l-k+w>0){ (f[w+l-k+w+i][w+l-k+w][w]+=f[i][j][k]*c[j][l]%p*c[w+l-k+w+i][i])%=p; } } } } } } int ans=0; for(int i=1;i<=n;++i){ for(int j=0;j<=i;++j) ans=(ans+f[n][i][j]*c[i][j])%p; } write(ans,'\n'); return 0; }
0
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int X,k,r[100010],Q; struct node{int t,a,id; }q[100010];int ans[100010]; bool cmp(node x,node y) {return x.t<y.t;} int pls(int x,int y){return max(0,min(X,x+y));} int main() { scanf("%d%d",&X,&k); for (int i=1;i<=k;i++) scanf("%d",&r[i]); scanf("%d",&Q); for (int i=1;i<=Q;i++) { scanf("%d%d",&q[i].t,&q[i].a);q[i].id=i; } sort(q+1,q+Q+1,cmp); int topr=0,topq=1,sum=0,down=0,up=X,add=0,js=-1; int cnt; while (topq<=Q) { if (r[topr+1]<q[topq].t&&topr<k) { topr++; add=js*(r[topr]-r[topr-1]); down=pls(down,add);up=pls(up,add); sum+=add;js=-js; } else { cnt=q[topq].t-r[topr]; ans[q[topq].id]=pls(max(down,min(up,q[topq].a+sum)),js*cnt); topq++; } } for (int i=1;i<=Q;i++) printf("%d\n",ans[i]); }
0
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1}; int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2}; int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1}; long long dp[2][5005]; long long a[5005], b[5004]; set<int> st; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; st.insert(a[i]); } int m = 0; for (auto x : st) { b[++m] = x; } for (int i = 0; i <= m; i++) { dp[1][i] = 1e18; } int xr = 0, nxr = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { dp[nxr][j] = min(dp[nxr][j - 1], dp[xr][j] + abs(a[i] - b[j])); } swap(xr, nxr); for (int j = 0; j <= m; j++) { dp[nxr][j] = 1e18; } } long long ans = dp[xr][m]; cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int N; int a, b; bool read() { if (scanf("%d", &N) < 1) { return 0; } scanf("%d%d", &a, &b); return 1; } const int MAXN = 2e3 + 10; namespace double_solver { double ans[MAXN]; double dp[MAXN]; double p_full[MAXN]; double f[MAXN][MAXN]; double pow_p[MAXN]; double pow_q[MAXN]; double solve() { const double p = a * 1.0 / b; const double q = 1 - p; pow_p[0] = pow_q[0] = 1; for (int i = 1; i < MAXN; ++i) { pow_p[i] = pow_p[i - 1] * p; pow_q[i] = pow_q[i - 1] * q; } memset(f, 0, sizeof f); for (int n = 0; n <= N; ++n) { for (int k = 0; k <= n; ++k) { if (k == 0) { f[n][k] = 1; continue; } f[n][k] += f[n - 1][k - 1] * pow_p[n - k]; if (n > k) { f[n][k] += f[n - 1][k] * pow_q[k]; } } } p_full[0] = 0; for (int n = 1; n <= N; ++n) { double bad = 0; for (int k = 1; k < n; ++k) { bad += f[n][k] * p_full[k]; } p_full[n] = 1 - bad; ; ; } memset(dp, 0, sizeof dp); memset(ans, 0, sizeof ans); ans[0] = 0; dp[0] = 0; for (int n = 2; n <= N; ++n) { dp[n] = 0; for (int k = 1; k < n; ++k) { dp[n] += f[n][k] * p_full[k] * (ans[k] + dp[n - k]); } double pairs = n * (n - 1) / 2; ans[n] = (dp[n] + pairs) / (1 - p_full[n]); dp[n] += f[n][n] * p_full[n] * ans[n]; ; ; } return ans[N]; } }; // namespace double_solver const int MOD = 998244353; void add(int& x, int y) { ((x += y) >= MOD) && (x -= MOD); } void sub(int& x, int y) { add(x, -y + MOD); } int mul(int x, int y) { return x * 1ll * y % MOD; } int mpow(int a, int p) { int res = 1; for (; p > 0; p >>= 1, a = mul(a, a)) { if (p & 1) { res = mul(res, a); } } return res; } int inv(int x) { int y = mpow(x, MOD - 2); assert(mul(x, y) == 1); return y; } namespace int_solver { int ans[MAXN]; int dp[MAXN]; int p_full[MAXN]; int f[MAXN][MAXN]; int pow_p[MAXN]; int pow_q[MAXN]; double solve() { const int p = mul(a, inv(b)); const int q = (1 - p + MOD) % MOD; pow_p[0] = pow_q[0] = 1; for (int i = 1; i < MAXN; ++i) { pow_p[i] = mul(pow_p[i - 1], p); pow_q[i] = mul(pow_q[i - 1], q); } memset(f, 0, sizeof f); for (int n = 0; n <= N; ++n) { for (int k = 0; k <= n; ++k) { if (k == 0) { f[n][k] = 1; continue; } add(f[n][k], mul(f[n - 1][k - 1], pow_p[n - k])); if (n > k) { add(f[n][k], mul(f[n - 1][k], pow_q[k])); } } } p_full[0] = 0; for (int n = 1; n <= N; ++n) { int bad = 0; for (int k = 1; k < n; ++k) { add(bad, mul(f[n][k], p_full[k])); } p_full[n] = 1; sub(p_full[n], bad); ; ; } memset(dp, 0, sizeof dp); memset(ans, 0, sizeof ans); ans[0] = 0; dp[0] = 0; for (int n = 2; n <= N; ++n) { dp[n] = 0; for (int k = 1; k < n; ++k) { int cur = mul(f[n][k], p_full[k]); int rec = ans[k]; add(rec, dp[n - k]); cur = mul(cur, rec); add(dp[n], cur); } const int pairs = n * (n - 1) / 2; ans[n] = dp[n]; add(ans[n], pairs); const int q_full = (1 - p_full[n] + MOD) % MOD; ans[n] = mul(ans[n], inv(q_full)); add(dp[n], mul(mul(f[n][n], p_full[n]), ans[n])); ; ; } return ans[N]; } }; // namespace int_solver int main() { while (read()) { int ans = int_solver::solve(); printf("%d\n", ans); } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long n; long long dpx[300005], dpy[300005], x[300005], y[300005], k; int main() { cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> x[i]; for (long long i = 1; i <= n; i++) cin >> y[i]; for (long long i = 1; i <= n; i++) { dpx[i] = max(0ll, dpx[i - 1] + x[i] - k * y[i]); if (dpx[i] > k) { cout << "No"; return 0; } } for (long long i = 1; i <= n; i++) { dpy[i] = max(0ll, dpy[i - 1] + y[i] - k * x[i]); if (dpy[i] > k) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; return 0; }
6
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; for(int i=a;i<a*102;i+=a){ if(i%b==c){ cout<<"YES"<<endl; return 0; } } cout<<"NO"<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int N = 1e5 + 5; int main() { int n, i, j, minA, maxA, minB, maxB, ans, x, y; scanf("%d", &n); minA = minB = INF; maxA = maxB = -INF; for (i = 0; i < n; i++) { scanf("%d%d", &x, &y); minA = min(minA, x + y - 1); maxA = max(maxA, x + y + 1); minB = min(minB, x - y - 1); maxB = max(maxB, x - y + 1); } ans = maxA - minA + maxB - minB; printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; struct node { long long num, lab; bool operator<(const node& a) const { return num > a.num; } } e[100005]; long long a[100005], w[100005], ans1[100005], ans2[100005]; priority_queue<node, vector<node>, less<node> > sp; int main() { long long n, m, i, j, ans = 0; scanf("%lld%lld", &n, &m); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = 1; i <= n; i++) scanf("%lld", &w[i]); for (i = 1; i <= n; i++) { if (m < a[i] % 100) { if (sp.empty()) { ans += (100 - a[i] % 100) * w[i]; m += 100 - a[i] % 100; ans1[i] = a[i] / 100 + 1; ans2[i] = 0; } else { sp.push((node){(100 - a[i] % 100) * w[i], i}); ans1[i] = a[i] / 100; ans2[i] = a[i] % 100; ans += sp.top().num; ans1[sp.top().lab]++; ans2[sp.top().lab] = 0; sp.pop(); m += 100 - a[i] % 100; } } else { if (a[i] % 100) sp.push((node){(100 - a[i] % 100) * w[i], i}); ans1[i] = a[i] / 100; ans2[i] = a[i] % 100; m -= a[i] % 100; } } printf("%lld\n", ans); for (i = 1; i <= n; i++) printf("%lld %lld\n", ans1[i], ans2[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; double gen(double x, double y, double z) { return log(log(x)) + z * log(y); } double gen(double x, double y) { return log(log(x)) + log(y); } vector<pair<double, string> > genall(double x, double y, double z) { vector<pair<double, string> > r; if (x > 1.) { r.push_back(pair<double, string>(gen(x, y, z), "x^y^z")); r.push_back(pair<double, string>(gen(x, z, y), "x^z^y")); r.push_back(pair<double, string>(gen(x, y * z), "(x^y)^z")); } if (y > 1.) { r.push_back(pair<double, string>(gen(y, x, z), "y^x^z")); r.push_back(pair<double, string>(gen(y, z, x), "y^z^x")); r.push_back(pair<double, string>(gen(y, x * z), "(y^x)^z")); } if (z > 1.) { r.push_back(pair<double, string>(gen(z, x, y), "z^x^y")); r.push_back(pair<double, string>(gen(z, y, x), "z^y^x")); r.push_back(pair<double, string>(gen(z, x * y), "(z^x)^y")); } return r; } int main() { double x, y, z; cin >> x >> y >> z; if (x <= 1. && y <= 1. && z <= 1.) { vector<pair<double, string> > r; if (x == 1.) { cout << "x^y^z" << endl; return 0; } if (y == 1.) { cout << "y^x^z" << endl; return 0; } if (z == 1.) { cout << "z^x^y" << endl; return 0; } auto v1 = genall(1. / x, y, z); auto v2 = genall(x, 1. / y, z); auto v3 = genall(x, y, 1. / z); r.insert(r.end(), v1.begin(), v1.end()); r.insert(r.end(), v2.begin(), v2.end()); r.insert(r.end(), v3.begin(), v3.end()); auto it = min_element(r.begin(), r.end(), [](pair<double, string> lhs, pair<double, string> rhs) { return lhs.first < rhs.first; }); cout << it->second << endl; } else { auto v = genall(x, y, z); auto it = max_element(v.begin(), v.end(), [](pair<double, string> lhs, pair<double, string> rhs) { return lhs.first < rhs.first; }); cout << it->second << endl; } return 0; }
4
#include <iostream> #include <string> using namespace std; int main() { string in; int n = 0; while (cin >> in) { n = 0; in += '\0'; for (int i = 0; i < in.size(); i++) { if (in[i] == 'M') n += 1000; if (in[i] == 'D') n += 500; if (in[i] == 'L') n += 50; if (in[i] == 'V') n += 5; if (in[i] == 'C') { if (in[i+1] == 'M' || in[i+1] == 'D') n -= 100; else n += 100; } if (in[i] == 'X') { if (in[i+1] == 'C' || in[i+1] == 'L') n -= 10; else n += 10; } if (in[i] == 'I') { if (in[i+1] == 'X' || in[i+1] == 'V') n -= 1; else n += 1; } } cout << n << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string t1, t2; cin >> t1; t2 = t1; reverse(t2.begin(), t2.end()); cout << t1 + t2; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long dp[2005][2005]; int lol[2005][2005]; int cnt[2005][2005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cnt[i][0] = 1; dp[i][0] = i; } for (int j = 1; j <= m; j++) { cnt[0][j] = 1; lol[0][j] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cnt[i][j] = (cnt[i][j - 1] + cnt[i - 1][j]) % 998244853; if (i <= j) lol[i][j] = (lol[i][j - 1] + lol[i - 1][j]) % 998244853; dp[i][j] = ((dp[i][j - 1] + dp[i - 1][j] - cnt[i][j - 1] + lol[i][j - 1] + cnt[i - 1][j]) % 998244853 + 998244853) % 998244853; } } cout << dp[n][m]; }
5
#include<bits/stdc++.h> using namespace std; #define ll long long #define M 20005 #define inf 998244353 struct edge{ int t,v,f,nxt; }e[M]; int head[M],tot=1; void add(int st,int t,int v,int f) { e[++tot].nxt=head[st],e[tot].t=t,e[tot].v=v,e[tot].f=f,head[st]=tot; e[++tot].nxt=head[t],e[tot].t=st,e[tot].v=-v,e[tot].f=0,head[t]=tot; } int n,s,t; ll ans; int d[M],vis[M],pre[M],F,L; ll dis[M]; bool spfa() { int tp=0; for(int i=1;i<=t;i++) vis[i]=0,dis[i]=-1e18; F=L=0; d[++L]=s,dis[s]=0; do{ int p=d[++F]; vis[p]=0; for(int i=head[p];i;i=e[i].nxt) { int to=e[i].t; if(!e[i].f) continue ; if(dis[to]<dis[p]+e[i].v) { if(to==t) tp=1; dis[to]=dis[p]+e[i].v,pre[to]=i^1; if(!vis[to]) d[++L]=to,vis[to]=1; } } }while(F<L); if(!tp) return 0; int p=t,Mf=inf; while(p!=s) { Mf=min(Mf,e[pre[p]^1].f); p=e[pre[p]].t; } p=t; while(p!=s) { e[pre[p]^1].f-=Mf; e[pre[p]].f+=Mf; ans+=1ll*e[pre[p]^1].v*Mf; p=e[pre[p]].t; } return 1; } void work() { while(spfa()) ; cout<<ans<<'\n'; } int main() { cin>>n; s=n*2+9,t=s+1; for(int i=1,x,y,c;i<=n;i++) { cin>>x>>y>>c; add(s,i,0,c); add(i,n*2+1,x+y,inf); add(i,n*2+2,x-y,inf); add(i,n*2+3,-x+y,inf); add(i,n*2+4,-x-y,inf); } for(int i=1,x,y,c;i<=n;i++) { cin>>x>>y>>c; add(i+n,t,0,c); add(n*2+5,i+n,x+y,inf); add(n*2+6,i+n,x-y,inf); add(n*2+7,i+n,-x+y,inf); add(n*2+8,i+n,-x-y,inf); } add(n*2+1,n*2+8,0,inf); add(n*2+2,n*2+7,0,inf); add(n*2+3,n*2+6,0,inf); add(n*2+4,n*2+5,0,inf); work(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long N; cin >> N; long long v[N]; for (long long i = 0; i < N; i++) cin >> v[i]; long long arr1[N - 1], arr2[N - 1]; for (long long i = 0; i < N - 1; i++) { arr1[i] = -1 * abs(v[i + 1] - v[i]); arr2[i] = abs(v[i + 1] - v[i]); if (i & 1) swap(arr1[i], arr2[i]); } long long max1[N - 1], max2[N - 1]; max1[0] = arr1[0]; max2[0] = arr2[0]; for (long long i = 1; i < N - 1; i++) max1[i] = max(max1[i - 1] + arr1[i], arr1[i]); for (long long i = 1; i < N - 1; i++) max2[i] = max(max2[i - 1] + arr2[i], arr2[i]); long long ans = 0; for (long long i = 0; i < N - 1; i++) ans = max(ans, max(max1[i], max2[i])); cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; map<string,set<int> > mp; string random2(){ string w; for(int i = 0 ; i < 100 ; i++) w += rand() % 26 + 'a'; return w; } string itos(int n){ stringstream ss; ss << n; return ss.str(); } bool inte(string s){ return '0' <= s[0] && s[0] <= '9'; } bool ope(string s){ return s == "+" || s == "*" || s == "/" || s == "-"; } bool var(string s){ return !inte(s) && !ope(s); } set<int> val(string s){ if( inte(s) ) return set<int>{atoi(s.c_str())}; return mp[s]; } set<int> operator + (set<int> a,set<int> b){ set<int> C; for( auto A : a ){ for( auto B : b ){ C.insert((A+B)%256); } } return C; } set<int> operator / (set<int> a,set<int> b){ set<int> C; for( auto A : a ){ for( auto B : b ){ C.insert((A/B)%256); } } return C; } set<int> operator * (set<int> a,set<int> b){ set<int> C; for( auto A : a ){ for( auto B : b ){ C.insert((A*B)%256); } } return C; } set<int> operator - (set<int> a,set<int> b){ set<int> C; for( auto A : a ){ for( auto B : b ){ C.insert((A-B+256)%256); } } return C; } bool calc(vector<string> e){ stack<string> s; for( int i = 0 ; i < e.size() ; i++){ if( inte(e[i]) ){ s.push(e[i]); } if( var(e[i]) ){ s.push(e[i]); } if( ope(e[i]) ){ string b = s.top(); s.pop(); string a = s.top(); s.pop(); set<int> B = val(b); set<int> A = val(a); set<int> R; if( e[i] == "+" ){ R = A+B; }else if( e[i] == "-" ){ R = A-B; }else if( e[i] == "*" ){ R = A*B; }else if( e[i] == "/" ){ if( B.count(0) ) return 0; R = A/B; } string name = random2(); mp[name] = R; s.push(name); } } return 1; } int main(){ int m; cin >> m; for(int i = 0 ; i < m ; i++){ string s; int x,y; cin >> s >> x >> y; for(int j = x ; j <= y ; j++) mp[s].insert(j); } int n; cin >> n; vector<string> e; for(int i = 0 ; i < n ; i++){ string s; cin >> s; e.push_back(s); } if( calc(e) ){ cout << "correct" << endl; }else{ cout << "error" << endl; } }
0
#include <bits/stdc++.h> using namespace std; int MOD; struct mint { int x; mint(int _x) : x((_x % MOD + MOD) % MOD) {} mint() : x(0) {} mint &operator=(const mint &rhs) { x = rhs.x; return *this; } mint pow(long long n) const { assert(0 <= n); mint x = *this, r = mint(1); while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } mint inv() const { return pow(MOD - 2); } mint &operator+=(const mint &rhs) { x += rhs.x; if (x >= MOD) x -= MOD; return *this; } mint &operator-=(const mint &rhs) { x -= rhs.x; if (x < 0) x += MOD; return *this; } mint &operator*=(const mint &rhs) { unsigned long long z = x; z *= rhs.x; x = (unsigned int)(z % MOD); return *this; } mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); } friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; } friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; } friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; } friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; } friend ostream &operator<<(ostream &os, const mint &m) { return os << m.x; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n >> MOD; vector<mint> dp(n + 5); vector<mint> suf(n + 5); dp[n] = suf[n] = 1; mint sum = 0; for (int i = n - 1; i >= 1; i--) { dp[i] = suf[i + 1]; for (int j = 2; j <= n; j++) { if (1LL * i * j > (long long)n) { break; } int l = i * j; int r = min(n, (i + 1) * j - 1); dp[i] += suf[l] - suf[r + 1]; } suf[i] = suf[i + 1] + dp[i]; } cout << dp[1] << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, t, e; int x[111111]; long long f[111111], g[111111]; int main(){ // freopen("input.inp", "r", stdin); scanf("%d%d%d",&n,&e,&t); for(int i = 1; i <= n; ++i) scanf("%d",&x[i]); int p = 0; g[0] = - 2 * x[1]; for(int i = 1; i <= n; ++i){ while (2ll * (x[i] - x[p + 1]) > t) ++p; f[i] = f[p] + t; if (p) f[i] = min(f[i], g[p - 1] + 2 * x[i]); g[i] = min(f[i] - 2 * x[i + 1], g[i - 1]); } printf("%lld",f[n] + e); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int MOD = 1e9 + 7; long long int power(long long int a, long long int n) { unsigned long long int result = 1, x = a; while (n > 0) { if (n % 2 == 1) result = (result * x) % MOD; x = (x * x) % MOD; n = n / 2; } return result; } vector<int> adj[500010]; int des[500010]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; while (t--) { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } vector<int> st; for (int i = 1; i <= n; i++) { cin >> des[i]; if (des[i] == 1) st.push_back(i); } for (int i = 1; i <= n; i++) { sort(adj[i].begin(), adj[i].end()); set<int> s; for (int j = 0; j < (int)adj[i].size(); j++) { if (des[adj[i][j]] < des[i]) s.insert(des[adj[i][j]]); if (des[i] == des[adj[i][j]]) { cout << -1 << endl; return 0; } } if ((int)s.size() != (des[i] - 1)) { cout << -1 << endl; return 0; } } if (st.size() == 0) { cout << -1 << endl; return 0; } vector<pair<int, int> > aaa(n); for (int i = 0; i < n; i++) { aaa[i].first = des[i + 1]; aaa[i].second = i + 1; } sort(aaa.begin(), aaa.end()); for (int i = 0; i < n; i++) cout << aaa[i].second << " "; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = int(1e9); const int N = 20; const int M = (1 << N); struct BracketSeqn { int balance; int lowestBalance; vector<int> queryAns; pair<int, bool> go(int x, bool f) { if (f) return make_pair(0, true); else return make_pair(x < queryAns.size() ? queryAns[x] : 0, x + lowestBalance < 0); } BracketSeqn(){}; BracketSeqn(string s) { vector<int> bal; int cur = 0; int n = s.size(); for (auto x : s) { if (x == '(') cur++; else cur--; bal.push_back(cur); } balance = bal.back(); lowestBalance = min(0, *min_element(bal.begin(), bal.end())); vector<vector<int>> negPos(-lowestBalance + 1); for (int i = 0; i < n; i++) { if (bal[i] > 0) continue; negPos[-bal[i]].push_back(i); } queryAns.resize(-lowestBalance + 1); for (int i = 0; i < queryAns.size(); i++) { int lastPos = int(1e9); if (i != -lowestBalance) lastPos = negPos[i + 1][0]; queryAns[i] = lower_bound(negPos[i].begin(), negPos[i].end(), lastPos) - negPos[i].begin(); } }; }; int dp[M][2]; char buf[M]; int total_bal[M]; int main() { int n; scanf("%d", &n); vector<BracketSeqn> bs; for (int i = 0; i < n; i++) { scanf("%s", buf); string s = buf; bs.push_back(BracketSeqn(s)); } for (int i = 0; i < (1 << n); i++) for (int j = 0; j < n; j++) if (i & (1 << j)) total_bal[i] += bs[j].balance; for (int i = 0; i < (1 << n); i++) for (int j = 0; j < 2; j++) dp[i][j] = -int(1e9); dp[0][0] = 0; for (int i = 0; i < (1 << n); i++) for (int f = 0; f < 2; f++) { if (dp[i][f] < 0) continue; for (int k = 0; k < n; k++) { if (i & (1 << k)) continue; pair<int, bool> res = bs[k].go(total_bal[i], f); dp[i ^ (1 << k)][res.second] = max(dp[i ^ (1 << k)][res.second], dp[i][f] + res.first); } } printf("%d\n", max(dp[(1 << n) - 1][0], dp[(1 << n) - 1][1])); }
6
#include <bits/stdc++.h> using namespace std; const int MAX = 5005; vector<int> llist[MAX]; int dpA[MAX], dpB[MAX]; int n, a, b, c, d, ofA, ofB, ofC, ofD, p[MAX], leaf[MAX]; void dfs(int u, int v) { p[u] = v; for (int i = 0; i < llist[u].size(); ++i) { int t = llist[u][i]; if (t == v) continue; dfs(t, u); leaf[u] += leaf[t]; } if (llist[u].size() == 1) leaf[u] = 1; } int trace(int u) { if (p[u] == 1) return u; return trace(p[u]); } int main() { int x; cin >> n >> a >> b >> c >> d; for (int i = 2; i <= n; ++i) { cin >> x; llist[x].push_back(i); llist[i].push_back(x); } dfs(1, 0); ofA = trace(a); ofB = trace(b); ofC = trace(c); ofD = trace(d); if (leaf[1] % 2 == 1) { cout << "NO" << endl; return 0; } dpA[leaf[ofA]] = 1; dpB[leaf[ofC]] = 1; for (int i = 0; i < llist[1].size(); ++i) { int t = llist[1][i]; if (t == ofA || t == ofB || t == ofC) continue; for (int j = leaf[1] - leaf[t]; j > 0; --j) { if (dpA[j] == 1) dpA[j + leaf[t]] = 1; } } for (int i = 0; i < llist[1].size(); ++i) { int t = llist[1][i]; if (t == ofB || t == ofA || t == ofD) continue; for (int j = leaf[1] - leaf[t]; j > 0; --j) { if (dpB[j] == 1) dpB[j + leaf[t]] = 1; } } int upper = leaf[1] / 2 - 1; int lowerA = leaf[1] / 2 + 1 - leaf[ofA] - leaf[ofB]; int lowerB = leaf[1] / 2 + 1 - leaf[ofC] - leaf[ofD]; bool resultA = false, resultB = false; for (int i = lowerA; i <= upper; ++i) { if (i >= 0 && dpA[i]) resultA = true; } for (int i = lowerB; i <= upper; ++i) { if (i >= 0 && dpB[i]) resultB = true; } if (resultA && resultB) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string a[150]; int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; int mxh = 0, mxw = 0, mnh = INT_MAX, mnw = INT_MAX; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == 'B') { if (i >= mxh && j >= mxw) { mxh = i; mxw = j; } if (i <= mnh && j <= mnw) { mnh = i; mnw = j; } } } } cout << (mnh + mxh + 2) / 2 << " " << (mxw + mnw + 2) / 2 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const int N = 10 + 5; const int MAX = 2147483647; int prime[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; int main() { int n, m, a, b, ans, fa, fb; scanf("%d %d %d %d", &n, &m, &a, &b); ans = 0; fa = a % m == 0 ? a / m : a / m + 1; fb = b % m == 0 ? b / m : b / m + 1; if (m == 1) { ans = 1; } else { if (fa == fb) ans = 1; else if (fa == fb - 1) if (a % m == 1) if (b % m == 0 || b == n) ans = 1; else ans = 2; else ans = 2; else if (a % m == 1) if (b % m == 0 || b == n) ans = 1; else ans = 2; else if (b % m == 0 || b == n) ans = 2; else if ((b - a + 1) % m == 0) ans = 2; else ans = 3; } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define oo 1000000010 const int N = 1510; int n , m , in[N]; vector< pair< vector< int > , int > > a , b; vector< int > from[N] , to[N]; bool done[N]; int edge_cnt = 0; vector< int > g[N]; vector< int > v1 , v2; inline void add(vector< int > &a,vector< int > &b){ from[edge_cnt] = a; to[edge_cnt] = b; for(int i = 0 ;i < (int)b.size();i++){ in[b[i]]++; } for(int i = 0 ;i < (int)a.size();i++){ g[a[i]].push_back(edge_cnt); } edge_cnt++; } vector< int > ans; int id[N]; int idx[N] , now[N]; queue < int > q; int main(){ scanf("%d%d",&n,&m); for(int i = 0 ;i < n;i++){ a.push_back(make_pair(vector< int > (m) , i)); for(int j = 0 ;j < m;j++){ scanf("%d",&a[i].first[j]); } } for(int i = 0 ;i < n;i++){ b.push_back(make_pair(vector< int > (m) , i)); for(int j = 0 ;j < m;j++){ scanf("%d",&b[i].first[j]); } } sort(a.begin(),a.end()); sort(b.begin(),b.end()); for(int i = 0 ;i < n;i++){ if(a[i].first != b[i].first){ puts("-1"); return 0; } idx[b[i].second] = a[i].second; now[b[i].second] = i; } for(int i = 1;i < n;i++){ v1.clear(); v2.clear(); for(int j = 0 ;j < m;j++){ if(a[now[i]].first[j] > a[now[i - 1]].first[j]) v1.push_back(j); else if(a[now[i]].first[j] < a[now[i - 1]].first[j]) v2.push_back(j); } add(v1 , v2); } for(int i = 0 ;i < m;i++){ if(in[i] == 0){ q.push(i); } } int node; while(!q.empty()){ node = q.front(); q.pop(); ans.push_back(node); for(int i = 0 ;i < (int)g[node].size();i++){ if(done[g[node][i]]) continue; done[g[node][i]] = true; for(int j = 0 ;j < (int)to[g[node][i]].size();j++){ in[to[g[node][i]][j]]--; if(in[to[g[node][i]][j]] == 0) q.push(to[g[node][i]][j]); } } } reverse(ans.begin(),ans.end()); for(int i = 0 ;i < (int)ans.size();i++){ id[ans[i]] = i + 1; } for(int i = 1 ;i < n;i++){ int col = -1; for(int j = 0 ;j < m;j++){ if(a[now[i]].first[j] != a[now[i - 1]].first[j]){ if(col == -1) col = j; if(id[j] > id[col]) col = j; } } if(col == -1 || id[col] == 0){ if(idx[i] < idx[i - 1]){ puts("-1"); return 0; } continue; } if(a[now[i]].first[col] < a[now[i - 1]].first[col]){ puts("-1"); return 0; } } printf("%d\n",(int)ans.size()); for(int i = 0 ;i < (int)ans.size();i++){ if(i) putchar(' '); printf("%d",ans[i] + 1); } puts(""); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long n, A, a[200005], sum, smol, lurg; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> A; for (long long i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } for (long long i = 1; i <= n; i++) { smol = A - sum + a[i]; lurg = A - n + 1; if (smol <= 0) smol = 1; if (lurg > a[i]) lurg = a[i]; cout << a[i] - (lurg - smol + 1) << ' '; } }
3
#include <bits/stdc++.h> using namespace std; void read(int& x) { scanf("%d", &x); } void read(long long& x) { scanf("%lld", &x); } template <typename T, typename... Args> void read(T& a, Args&... b) { read(a); read(b...); } int n; int a[100010]; int c[100010]; int cc[100010]; const int under = 340; int uc[100010], ucn; int ucc[100010]; inline void AddNum(int first) { if (!first) return; if (first <= under) ++cc[first]; } inline void RemNum(int first) { if (!first) return; if (first <= under) --cc[first]; } inline void Add(int first) { RemNum(c[first]); ++c[first]; AddNum(c[first]); } inline void Rem(int first) { RemNum(c[first]); --c[first]; AddNum(c[first]); } struct Query { int ql, qr, ai; } qry[100010]; const int WOW = 340; long long ans[100010]; int q; int asdf[under + 10]; int main() { read(n); for (int i = 1; i <= n; ++i) read(a[i]), ++ucc[a[i]]; read(q); for (int i = 1; i <= 100000; ++i) if (ucc[i] > under) uc[ucn++] = i; for (int i = 1; i <= q; ++i) { int a, b; read(a, b); qry[i] = {a, b, i}; } int cl = 1, cr = 1; Add(a[1]); sort(qry + 1, qry + q + 1, [&](const Query& a, const Query& b) { int ta = a.ql / WOW, tb = b.ql / WOW; if (ta != tb) return ta < tb; return a.qr < b.qr; }); for (int i = 1; i <= q; ++i) { int tl = qry[i].ql, tr = qry[i].qr; while (cr < tr) Add(a[++cr]); while (tl < cl) Add(a[--cl]); while (tr < cr) Rem(a[cr--]); while (cl < tl) Rem(a[cl++]); priority_queue<int> pq; long long ans = 0; int nokori = -1; for (int i = 1; i <= under; ++i) asdf[i] = cc[i]; for (int i = 1; i <= under; ++i) { int ccc = asdf[i]; if (ccc && nokori != -1) { --ccc; ans += (nokori + i); if (nokori + i > under) pq.push(-(nokori + i)); else ++asdf[nokori + i]; nokori = -1; } ans += (2ll * i) * (ccc / 2); if (2 * i > under) { for (int t = 0; t < ccc / 2; ++t) pq.push(-2 * i); } else { asdf[2 * i] += ccc / 2; } if (ccc % 2) nokori = i; } if (nokori != -1) pq.push(-nokori); for (int i = 0; i < ucn; ++i) if (c[uc[i]] > under) pq.push(-c[uc[i]]); while (pq.size() > 1llu) { int a = pq.top(); pq.pop(); int b = pq.top(); pq.pop(); ans -= a + b; pq.push(a + b); } ::ans[qry[i].ai] = ans; } for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; int ary[200]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> ary[i]; int a, b; cin >> a >> b; a--, b--; int x = a; int s1 = 0, s2 = 0; while (x != b) { s1 += ary[x]; x++; x %= n; } int y = b; while (y != a) { s2 += ary[y]; y++; y %= n; } if (s1 < s2) cout << s1 << endl; else cout << s2 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; bool isP(int n) { bool pr = 1; for (int j = 2; 1LL * j * j <= n && pr; j++) if (n % j == 0) pr = 0; return pr; } int main() { int n; cin >> n; if (isP(n)) cout << "1\n" << n; else if (isP(n - 2)) cout << "2\n2 " << (n - 2); else { cout << "3\n"; for (int i = n; i > 1; i -= 2) if (isP(i)) { cout << i << ' '; n -= i; break; } for (int i = 2; i <= 300; i++) { if (isP(i) && (n - i) > 1 && isP(n - i)) { cout << i << ' ' << (n - i); break; } } } return 0; }
4
#include <bits/stdc++.h> using namespace std; bool a[502][502]; int pre[502][502]; int main() { long long n, i, j, m, k, l, r; char c; cin >> n >> m >> k; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) cin >> c, a[i][j] = c - '0'; for (i = 1; i <= m; i++) for (j = 1; j <= n; j++) { pre[j][i] = pre[j - 1][i]; if (i == 1 || j == 1 || i == m || j == n) continue; if (a[j][i] && a[j - 1][i] && a[j + 1][i] && a[j][i - 1] && a[j][i + 1]) pre[j][i]++; } long long ans = 0, sum = 0; for (i = 1; i <= n; i++) { for (j = i + 2; j <= n; j++) { l = 1; ans = 0; for (r = 3; r <= m; r++) { ans += pre[j - 1][r - 1] - pre[i][r - 1]; if (ans >= k) sum += (m - r + 1); while (l <= r - 2 && ans >= k) { ans -= (pre[j - 1][l + 1] - pre[i][l + 1]); if (ans >= k) sum += (m - r + 1); l++; } } } } cout << sum; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[n + 1], b[m + 1]; for (int i = 1; i <= n; i++) cin >> a[i]; for (int j = 1; j <= m; j++) cin >> b[j]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i] == b[j]) { cout << a[i] << " "; b[j] = -1; break; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, s; cin >> a >> b >> s; if (a < 0) a *= -1; if (b < 0) b *= -1; long long int steps = a + b; if (s >= steps && (s - steps) % 2 == 0) cout << "Yes" << endl; else cout << "No" << endl; }
1
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int n, x[N], y[N], ans[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; int mnx = 2e9, mxx = -2e9, mny = 2e9, mxy = -2e9; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i]; mnx = min(mnx, x[i]); mxx = max(mxx, x[i]); mny = min(mny, y[i]); mxy = max(mxy, y[i]); } for (int i = 1; i <= n; i++) { ans[3] = max(ans[3], mxx - x[i] + mxy - y[i]); ans[3] = max(ans[3], mxx - x[i] + y[i] - mny); ans[3] = max(ans[3], x[i] - mnx + y[i] - mny); ans[3] = max(ans[3], x[i] - mnx + mxy - y[i]); } ans[3] *= 2; for (int i = 4; i <= n; i++) { ans[i] = (mxx - mnx + mxy - mny) * 2; } for (int i = 3; i <= n; i++) { cout << ans[i] << " "; } }
5
#include <bits/stdc++.h> using namespace std; mt19937 rnd(time(0)); #define ios ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0); #define int long long #define ld long double #define sz(a) (int)a.size() #define all(a) a.begin(),a.end() //ll p = 131; int mod = 1e9 + 7; int inf = 1000000000000000000; int ask(int t, int i, int j, int x) { cout << "? " << t << " " << i + 1 << " " << j + 1 << " " << x << endl; int res; cin >> res; return res; } void solve() { int n; cin >> n; int p1 = -1; for (int i = 0; i + 1 < n; i += 2) { int x = 1; int res = ask(2, i, i + 1, x); if (res == 1) { p1 = i; break; } if (res == 2) { if (ask(2, i + 1, i, x) == 1) { p1 = i + 1; break; } } } vector<int> ans(n, 1); if (p1 == -1) p1 = n - 1; for (int i = 0; i < n; ++i) { if (i != p1) { ans[i] = ask(1, p1, i, n - 1); } } cout << "! "; for (int i = 0; i < n; ++i) { cout << ans[i] << ' '; } cout << endl; } signed main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios; int t; cin >> t; while (t-- > 0) solve(); return 0; }
3
#include<bits/stdc++.h> #define itr(i, n) for(int i=1;i<=n;++i) using namespace std; const int SIZE = 5e5+1; vector<int> num_arr(SIZE), idx_arr(SIZE); stack<int> s, t; int memo[SIZE]; int n, k; void NIT(){ //Num Index Transform 数とインデックスを入れ替える itr(i, n) //両方とも要素を1からnにする idx_arr[num_arr[i]] = i; } void INT(){ //Index Num Transform itr(i, n) { num_arr[idx_arr[i]] = i; } } void divide_and_conquer_method(int left, int right){ if (left >= right) return; int median = (left + right) / 2; divide_and_conquer_method(left, median); divide_and_conquer_method(median+1, right); int Min = idx_arr[median]; s.push(Min); t.push(idx_arr[median]); for (int i = median-1; i >= left; --i){ Min = min(Min, idx_arr[i]); s.push(Min); t.push(idx_arr[i]); } int j = left; for (int i = median+1; i <= right; i++) { while (!(s.empty()) && s.top() - idx_arr[i] < k) { memo[j] = t.top(); ++j; s.pop(); t.pop(); } memo[j] = idx_arr[i]; j++; } while (!t.empty()) { memo[j] = t.top(); ++j; t.pop(); s.pop(); } for (int i = left; i <= right; i++){ idx_arr[i] = memo[i]; } } int main(){ cin >> n >> k; itr(i, n) cin >> num_arr[i]; NIT(); divide_and_conquer_method(1, n); INT(); itr(i, n){ cout << num_arr[i] << endl; } }
0
#include <bits/stdc++.h> using namespace std; long long cnt1, cnt2, x, y; bool ok(long long t) { long long ob = t / x + t / y - (t / (x * y)); long long per = t / x + t / y - ob; long long num1 = min(max(t / y - per, (long long)0), cnt1); long long num2 = min(max(t / x - per, (long long)0), cnt2); long long num3 = max(t - ob, (long long)0); return num1 + num2 + num3 >= cnt1 + cnt2; } int main() { cin >> cnt1 >> cnt2 >> x >> y; long long l = cnt1 + cnt2, r = (long long)1e18, mid; while (r - l > 1) { mid = (r + l) / 2; if (ok(mid)) r = mid; else l = mid; } if (ok(l)) cout << l; else cout << r; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, m, ans; string st; int main() { cin >> n >> m; cin >> st; ans = n * m - n; for (int k = 0; k < st.size(); ++k) { if (k > 0 && st[k] != st[k - 1]) ans += n * m - n; } long long c = 1; for (int k = 1; k < st.size(); ++k) { if (c == 1) { if (st[k] == st[k - 1]) continue; if (st[k] != st[k - 1]) { ++c; } continue; } if (st[k] == st[k - 2]) ++c; else { ans -= (c * (c - 1)) / 2; if (st[k] != st[k - 1]) c = 2; else c = 1; } } ans -= (c * (c - 1)) / 2; cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; void solve(long double d) { long double D = sqrt(d * (d - 4)); long double a = (d - D) / 2; long double b = (d + D) / 2; cout << "Y " << setprecision(15) << b << " " << setprecision(15) << a << endl; } int main() { int t; cin >> t; long double arr[t]; for (int i = 0; i < t; i++) { cin >> arr[i]; if (arr[i] == 0) cout << "Y 0.000000000 0.000000000" << endl; else if (arr[i] < 4) cout << "N" << endl; else solve(arr[i]); } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long n, m, k, l, h, a, b, c, d, e, f, ans = 0; int main() { cin >> n; for (int t = 1; t <= n; t++) { cin >> a >> b >> k; ans = k; if (a < b) swap(a, b); if (a > k) { cout << "-1" << endl; continue; } else { if ((k - a) % 2) ans--; if ((k - b) % 2) ans--; cout << ans << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; char s[100005]; char tmp[100005]; stack<char> st; int main() { scanf("%s", s); int l = strlen(s); int cnt = 0; for (int i = 0; i < l; i++) { if (!st.empty()) { char topp = st.top(); if (topp == s[i]) { cnt++; st.pop(); } else { st.push(s[i]); } } else { st.push(s[i]); } } if (cnt % 2 == 0) puts("No"); else puts("Yes"); }
2
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<long long> a(n), b(n); for (auto &x : a) { cin >> x; } for (auto &x : b) { cin >> x; } if (n == 1) { if (a[0] == b[0]) { cout << "SMALL\n0\n"; } else { cout << "IMPOSSIBLE\n"; } exit(0); } if (n == 2) { vector<pair<char, long long>> rez; long long ss = a[0] + a[1]; for (;;) { if (b[0] + b[1] < a[0] + a[1]) { cout << "IMPOSSIBLE\n"; exit(0); } if (a == b) { break; } vector<long long> b2 = {b[1], b[0]}; if (b2 == a) { rez.push_back({'R', 1}); break; } bool srt1 = (b[0] < b[1]), srt2 = (b2[0] < b2[1]); if (srt1) { if (b[0] < 1 || b[1] < 1 || b[1] <= a[1]) { cout << "IMPOSSIBLE\n"; exit(0); } long long to_sum = b[0] + (b[1] % b[0]); if (to_sum < ss) { long long cnt = (b[0] + b[1] - ss) / b[0]; if (b[0] + b[1] - b[0] * cnt != ss) { cout << "IMPOSSIBLE\n"; exit(0); } b[1] -= b[0] * cnt; rez.push_back({'P', cnt}); if (b == a) { break; } if (vector<long long>{b[1], b[0]} == a) { rez.push_back({'R', 1}); break; } else { cout << "IMPOSSIBLE\n"; exit(0); } } else { rez.push_back({'P', b[1] / b[0]}); b[1] %= b[0]; } } else if (srt2) { rez.push_back({'R', 1}); swap(b, b2); if (b[0] < 1 || b[1] < 1 || b[1] <= a[1]) { cout << "IMPOSSIBLE\n"; exit(0); } long long to_sum = b[0] + (b[1] % b[0]); if (to_sum < ss) { long long cnt = (b[0] + b[1] - ss) / b[0]; if (b[0] + b[1] - b[0] * cnt != ss) { cout << "IMPOSSIBLE\n"; exit(0); } b[1] -= b[0] * cnt; rez.push_back({'P', cnt}); if (b == a) { break; } if (vector<long long>{b[1], b[0]} == a) { rez.push_back({'R', 1}); break; } else { cout << "IMPOSSIBLE\n"; exit(0); } } else { rez.push_back({'P', b[1] / b[0]}); b[1] %= b[0]; } } else { cout << "IMPOSSIBLE\n"; exit(0); } } reverse((rez).begin(), (rez).end()); long long sum = 0, sum2 = 0; for (auto it : rez) { if (it.first == 'P') { sum += it.second; } sum2 += it.second; } if (sum > 2e5) { cout << "BIG\n" << sum << '\n'; } else { cout << "SMALL\n" << sum2 << '\n'; for (auto it : rez) { for (long long i = 0; i < it.second; i++) { cout << it.first; } } cout << '\n'; } exit(0); } long long mx = 0; for (auto &x : a) { mx = max(mx, x); } vector<char> rez; for (;;) { if (a == b) { break; } vector<long long> b2 = b; reverse((b2).begin(), (b2).end()); if (b2 == a) { rez.push_back('R'); break; } bool srt1 = true, srt2 = true; for (long long i = 1; i < n; i++) { if (b[i] <= b[i - 1]) { srt1 = false; } if (b2[i] <= b2[i - 1]) { srt2 = false; } } if (srt1) { for (long long i = 0; i < n; i++) { if (b[i] < 1) { cout << "IMPOSSIBLE\n"; exit(0); } } if (b.back() <= mx) { cout << "IMPOSSIBLE\n"; exit(0); } rez.push_back('P'); vector<long long> nb = {b[0]}; for (long long i = 1; i < n; i++) { nb.push_back(b[i] - b[i - 1]); } b = nb; } else if (srt2) { rez.push_back('R'); rez.push_back('P'); for (long long i = 0; i < n; i++) { if (b2[i] < 0) { cout << "IMPOSSIBLE\n"; exit(0); } } if (b2.back() <= mx) { cout << "IMPOSSIBLE\n"; exit(0); } vector<long long> nb = {b2[0]}; for (long long i = 1; i < n; i++) { nb.push_back(b2[i] - b2[i - 1]); } b = nb; } else { cout << "IMPOSSIBLE\n"; exit(0); } } long long sum = 0; for (auto it : rez) { if (it == 'P') { sum++; } } if (sum > 2e5) { cout << "BIG\n" << sum << '\n'; } else { cout << "SMALL\n" << (long long)(rez).size() << '\n'; reverse((rez).begin(), (rez).end()); for (auto &c : rez) { cout << c; } cout << '\n'; } cerr << clock() * 1.0 / CLOCKS_PER_SEC << '\n'; }
6
#include <bits/stdc++.h> using namespace std; inline int Get() { int res = 0, q = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') q = -1, ch = getchar(); while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar(); return res * q; } const double eps = 1e-12, pi = M_PI; const int oo = (int)2e9, mod = (int)1e9 + 7; const long long INF = (long long)1e17; long long n, m, K; long long Count(long long x) { long long res = 0; for (int i = (1), end = (n); i <= end; ++i) { res += min(m, x / (long long)i); } return res; } int main() { scanf( "%I64d" "%I64d" "%I64d" "\n", &n, &m, &K); long long l = 1, r = n * m; for (; l <= r;) { long long mid = (l + r) >> 1; long long low = Count(mid - 1), upr = Count(mid); if (K > low && K <= upr) { printf( "%I64d" "\n", mid); return 0; } else if (K <= low) r = mid - 1; else l = mid + 1; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, k = 0, ans = 1; cin >> a >> b; if (a == b) { cout << "1"; } else { while (k < 5 && a < b) { a++; ans *= (a % 10); if (ans % 10 == 0) break; k++; } cout << ans % 10; } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long modpow(long long a, long long p, long long mod) { long long ret = 1; while (p) { if (p & 1) ret = (ret * a) % mod; a = (a * a) % mod; p /= 2; } return ret; } long long power(long long a, long long p) { long long ret = 1; while (p) { if (p & 1) ret = (ret * a); a = (a * a); p /= 2; } return ret; } bool arr[10000001] = {0}; vector<int> vec; int main() { int i, j, tmp; ios_base::sync_with_stdio(0); cin.tie(NULL); int n, k, q, ii; cin >> n >> k; for (i = 0; i < n; i++) { cin >> tmp; arr[tmp]++; vec.push_back(tmp); } cin >> q; while (q--) { int ans = 100; cin >> tmp; int t = tmp; for (i = 0; i < k + 1; i++) { t = tmp; for (j = 0; j < (int)(vec.size()); j++) { t = tmp; t -= (i * vec[j]); if (t == 0) { ans = min(ans, i); continue; } if (t < 0) continue; for (ii = 0; ii < k - i + 1; ii++) { if ((ii) && (t % ii == 0) && ((t / ii) <= 10000000)) if (arr[t / ii] == 1) ans = min(ans, i + ii); } } } if (ans == 100) ans = -1; cout << ans << endl; } return 0; }
5
#include <iostream> using namespace std; int main() { int n,a,max=-1000000,min=1000000; cin >> n; long long int sum=0; for (int i=0;i<n;i++) { cin >> a; if (max<a) max=a; if (min>a) min=a; sum+=a; } cout << min << " " << max<< " " << sum << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = (1e6) + 7; int n, m, L, R, size, ansx[maxn], ansy[maxn]; int main() { scanf("%d%d", &n, &m); L = 1, R = m; for (int i = 1; i <= m >> 1; i++) { for (int j = 1; j <= n; j++) { ++size; ansx[size] = j, ansy[size] = L; ++size; ansx[size] = n - j + 1, ansy[size] = R; } ++L, --R; } if (m & 1) { L = 1, R = n; for (int i = 1; i <= n >> 1; i++) { ++size; ansx[size] = L, ansy[size] = (m + 1) >> 1; ++size; ansx[size] = R, ansy[size] = (m + 1) >> 1; ++L, --R; } if (L == R) ++size, ansx[size] = L, ansy[size] = (m + 1) >> 1; } for (int i = 1; i <= size; i++) printf("%d %d\n", ansx[i], ansy[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n,ans=0; scanf("%d",&n); ans+=min(9,n); ans+=min(900,max(0,n-99)); ans+=min(90000,max(0,n-9999)); printf("%d\n",ans); }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n; int i; for (i = 1; i < n; i++) cout << " " << i; cout << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int z, o, i, j, k; cin >> o >> z; if (o == z) { for (i = 0; i < o; i++) { cout << "10"; } return 0; } else if (o == z + 1) { for (i = 0; i < z; i++) { cout << "01"; } cout << "0"; return 0; } k = 0; if ((o + 1) > z) { k = 1; } if (z > 2 * (o + 1)) { k = 1; } if (k == 1) { cout << -1 << endl; return 0; } k = z - (o + 1); for (i = 0; i < o; i++) { if (k > 0) { cout << "110"; k--; } else { cout << "10"; } } if (k > 0) { cout << "11" << endl; } else { cout << "1" << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 3e6 + 10, MOD = 1e9 + 7; int a[N], has[N]; long long f[N]; int main() { int n, m; scanf("%d", &n); int maxn = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); has[a[i]]++; maxn = max(maxn, a[i]); } maxn = 3e6; for (int i = 1; i <= maxn; i++) { if (has[i]) { for (int j = i; j <= maxn; j += i) { f[j] += 1ll * has[i] * (has[j / i] - (i * i == j)); } } f[i] += f[i - 1]; } scanf("%d", &m); for (int i = 1; i <= m; i++) { int p; scanf("%d", &p); p = min(p, maxn + 1); printf("%I64d\n", 1ll * n * (n - 1) - f[p - 1]); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int n; vector<long long> piles; vector<long long> melt; vector<long long> melt_initial; vector<long long> cnt; vector<long long> alone; int main() { cin >> n; cnt.resize(n + 2, 0); alone.resize(n + 2, 0); for (int i = 0; i < n; i++) { long long temp; cin >> temp; piles.push_back(temp); } for (int i = 0; i < n; i++) { long long temp; cin >> temp; melt.push_back(temp); melt_initial.push_back(temp); } for (int i = 1; i < n; i++) melt[i] += melt[i - 1]; for (int i = 0; i < n; i++) { long long before_melt = i == 0 ? 0 : melt[i - 1]; long long max_melt = before_melt + piles[i]; if (melt_initial[i] > piles[i]) { alone[i] += piles[i]; continue; } int l = i, r = n - 1; while (r - l > 1) { int m = (l + r) / 2; if (melt[m] > max_melt) r = m; else l = m; } int index; if (melt[l] <= max_melt) index = l; if (melt[r] <= max_melt) index = r; cnt[i]++; cnt[index + 1]--; long long alone_left = piles[i]; if (i == 0) alone_left -= melt[index]; else alone_left -= melt[index] - melt[i - 1]; alone[index + 1] += alone_left; } long long delta = 0; for (int i = 0; i < n; i++) { delta += cnt[i]; long long current = alone[i] + melt_initial[i] * delta; cout << current << " "; } cout << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 1234567; int n, que[N]; char s[N], w[N]; int main() { scanf("%d %s %s", &n, s, w); if (strcmp(s, w) == 0) { puts("0"); return 0; } int ans = 1; int qh = 0, qt = 0; int j = n - 1; for (int i = n - 1; i >= 0; i--) { if (i == 0 || w[i] != w[i - 1]) { j = min(i, j); while (j >= 0 && s[j] != w[i]) { j--; } if (j < 0) { puts("-1"); return 0; } while (qh < qt && que[qh] - (qt - qh - 1) > i) { qh++; } if (i != j) { que[qt++] = j; ans = max(ans, qt - qh + 1); } } } printf("%d\n", ans); return 0; }
0
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> using namespace std; #define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define MOD 1000000007 #define mod 1000000007 #define mfor(i,a,b) for(int i=a;i<=b;i++) #define p_b push_back #define m_p make_pair #define all(v) v.begin(),v,end() typedef long long int ll; #define sp(x) setprecision(x) //cout<<fixed<<sp(len_of_decimal)<<ans<<endl; int dir8[2][8]={{0,0,1,-1,1,1,-1,-1},{1,-1,0,0,1,-1,-1,1}}; int dir4[2][4]={{0,0,1,-1},{1,-1,0,0}}; void solve() { ll n; cin>>n; vector<ll> gg={1,11,111,1111,11111,111111,1111111,11111111,111111111}; int ans=0; for(int i=0;i<gg.size();i++){ for(int j=1;j<=9;j++){ ll num= gg[i]*j; if(num<=n) ans++; } } cout<<ans<<endl; return; } int main() { SPEED int t; cin>>t; while(t--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; bool lengh(char str[10000], int& len); bool syntax(char str[10000], int& len); int main() { char str[10000]; cin.get(str, 10000, '\n'); cin.ignore(10000, '\n'); int len = strlen(str); if (!lengh(str, len) || !syntax(str, len)) { cout << "NO \n"; } else cout << "YES \n"; return 0; } bool lengh(char str[10000], int& len) { int r = 0, t1 = 0, t2 = 0; for (int i = 0; i < len; i++) { if (str[i] == '@') { for (int k = 0; k < len; k++) { if (str[k] == '/') { if (k - i - 1 > 32) return false; break; } else if (k == len - 1) { if (k - i > 32) return false; } } break; } } for (int i = 0; i < len; i++) { r++; if (str[i] == '@' || str[i] == '.' || str[i] == '/') { if (r < 2 || r > 17) return false; if (str[i] == '@') { t1++; for (int k = 0; k < len; k++) { if (str[k] == '/') { if (k < i) return false; } } } else if (str[i] == '/') t2++; else if (str[i] == '.') { for (int k = 0; k < len; k++) { if (str[k] == '@') { if (i < k) return false; } else if (str[k] == '/') { if (k < i) return false; } } } r = 0; } } if (r < 1 || r > 16) return false; if (t1 != 1 || t2 > 1) return false; return true; } bool syntax(char str[10000], int& len) { for (int i = 0; i < len; i++) { if ((64 <= int(str[i]) && int(str[i]) <= 90) || (97 <= int(str[i]) && int(str[i]) <= 122) || (46 <= int(str[i]) && int(str[i]) <= 57) || int(str[i]) == 95) { continue; } else return false; } return true; }
1
#include <bits/stdc++.h> using namespace std; int n; map<int, int> occ; set<pair<int, int> > st; set<pair<int, int> >::iterator stIter; vector<vector<int> > res; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int element; scanf("%d", &element); occ[element]++; } map<int, int>::iterator it; for (it = occ.begin(); it != occ.end(); it++) { pair<int, int> ele = make_pair(it->second, it->first); st.insert(ele); } int ans = 0; while (st.size() > 2) { stIter = st.end(); stIter--; pair<int, int> e1 = *stIter; set<pair<int, int> >::iterator tmp; tmp = stIter; stIter--; st.erase(tmp); pair<int, int> e2 = *stIter; tmp = stIter; stIter--; st.erase(tmp); pair<int, int> e3 = *stIter; st.erase(stIter); ans++; e2.first--; e1.first--; e3.first--; if (e2.first) st.insert(e2); if (e1.first) st.insert(e1); if (e3.first) st.insert(e3); vector<int> z; z.push_back(e3.second), z.push_back(e2.second), z.push_back(e1.second); sort(z.begin(), z.end()); res.push_back(z); } printf("%d\n", ans); for (int i = 0; i < res.size(); i++) { printf("%d %d %d\n", res[i][2], res[i][1], res[i][0]); } return 0; }
3
#include <bits/stdc++.h> using namespace std; bool isodd(long long int x) { return x % 2; } int main() { long long int n; cin >> n; vector<long long int> arr(n), ans; for (int i = n - 1; i != -1; i--) cin >> arr[i]; if (n == 1) { cout << arr[0]; return 0; } ans.push_back(arr[0]); int size = n / 2 - 1; pair<long long int, long long int> p[size]; int b = 1, e = n - 2; for (int i = 0; i < size; i++) { p[i] = make_pair(arr[e], arr[b]); b++, e--; } for (int i = 0; i < size; i++) { if (i % 2) ans.push_back(p[i].second); else ans.push_back(p[i].first); } if (n % 2 == 1) ans.push_back(arr[n / 2]); for (int i = size - 1; i != -1; i--) { if (i % 2) ans.push_back(p[i].first); else ans.push_back(p[i].second); } for (int i = 0; i < n - 1; i++) cout << ans[i] << " "; cout << arr[n - 1]; }
2
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 99999999999999999 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define NUM 100 int N,M; int boss[NUM],height[NUM],member_num[NUM]; int len_num[NUM+1]; bool check[NUM]; //自分のボスのindexを取得しつつ、経路圧縮を行う関数 int get_boss(int id){ if(boss[id] == id)return id; //自分が代表なら、自分の値を返す else{ return boss[id] = get_boss(boss[id]); //代表でないなら、自分が所属する組織の代表を返しつつ、経路圧縮 } } int is_same_group(int x,int y){ return get_boss(x) == get_boss(y); } void unite(int x,int y){ int boss_x = get_boss(x); int boss_y = get_boss(y); //既に同じグループなら何もしない if(boss_x == boss_y)return; //高さが高い方に吸収する if(height[x] > height[y]){ member_num[boss_x] += member_num[boss_y]; boss[boss_y] = boss_x; }else if(height[x] < height[y]){ member_num[boss_y] += member_num[boss_x]; boss[boss_x] = boss_y; }else{ //height[x] == height[y] member_num[boss_x] += member_num[boss_y]; boss[boss_y] = boss_x; height[x]++; } } int main(){ scanf("%d %d",&N,&M); for(int i = 0; i < N; i++){ boss[i] = i; height[i] = 0; member_num[i] = 1; check[i] = false; len_num[i+1] = 0; } int start,len; for(int loop = 0; loop < M; loop++){ scanf("%d %d",&start,&len); check[(start)%N] = true; for(int i = 1; i <= len-1; i++){ check[(start+i)%N] = true; } } for(int i = 0; i < N; i++){ if(check[i] == true && check[(i-1+N)%N] == true){ unite(i,(i-1+N)%N); } } for(int i = 0; i < N; i++){ if(check[i] == true && i == get_boss(i)){ len_num[member_num[i]] += 1; } } for(int i = N; i >= 1; i--){ if(len_num[i] == 0)continue; printf("%d %d\n",i,len_num[i]); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } long long cnt[1000000 + 10][30]; int main() { ios::sync_with_stdio(false); long long n, m; cin >> n >> m; string x, y; cin >> x >> y; if (x.size() < y.size()) { swap(x, y); swap(n, m); } int g = gcd(x.size(), y.size()); for (int i = 0; i < g; i++) { for (int j = i; j < x.size(); j += g) { int cur = (int)(x[j] - 'a'); cnt[i][cur]++; } } long long ans = 0; for (int i = 0; i < y.size(); i++) { int cur = (int)(y[i] - 'a'); ans += (x.size() / g) - cnt[i % g][cur]; } long long t = x.size(); cout << (m / (t / (long long)g)) * ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int const maxn = 1e6 + 1; int a[300005]; int b[300005]; int tree[4 * maxn]; int psh[4 * maxn]; inline void push(int i, int len) { tree[i] += psh[i]; if (len != 1) psh[2 * i + 1] += psh[i], psh[2 * i + 2] += psh[i]; psh[i] = 0; } void update(int i, int l, int r, int lq, int rq, int dx) { push(i, r - l); if (lq >= r || l >= rq) return; if (lq <= l && r <= rq) { psh[i] += dx; push(i, r - l); return; } int m = (r + l) / 2; update(2 * i + 1, l, m, lq, rq, dx); update(2 * i + 2, m, r, lq, rq, dx); tree[i] = max(tree[2 * i + 1], tree[2 * i + 2]); } int get_ans(int i, int l, int r) { push(i, r - l); if (r - l == 1) return l; int m = (r + l) / 2; if (tree[2 * i + 2] + psh[2 * i + 2] > 0) return get_ans(2 * i + 2, m, r); else return get_ans(2 * i + 1, l, m); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; multiset<int> Q; for (int i = 1; i <= n; ++i) { cin >> a[i]; Q.insert(a[i]); update(0, 1, maxn, 1, a[i] + 1, 1); } for (int i = 1; i <= m; ++i) { cin >> b[i]; update(0, 1, maxn, 1, b[i] + 1, -1); } int q, t, pos, x; cin >> q; for (int i = 1; i <= q; ++i) { cin >> t >> pos >> x; if (t == 1) { update(0, 1, maxn, 1, a[pos] + 1, -1); Q.erase(Q.find(a[pos])); a[pos] = x; Q.insert(a[pos]); update(0, 1, maxn, 1, a[pos] + 1, 1); } else { update(0, 1, maxn, 1, b[pos] + 1, 1); b[pos] = x; update(0, 1, maxn, 1, b[pos] + 1, -1); } if (tree[0] > 0) { auto it = Q.upper_bound(get_ans(0, 1, maxn)); it--; cout << (*it) << '\n'; } else cout << -1 << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; string x, y, z; int ans = 0; scanf("%d", &n); cin >> x; for (int i = 0; i < n; i++) { y += char(i % 2 + 48); z += char((1 - i % 2) + 48); } int cnt1 = 0; int cnt2 = 0; for (int i = 0; i < n; i++) { if (x[i] != y[i]) cnt1++; if (x[i] != z[i]) cnt2++; } printf("%d\n", min(cnt1, cnt2)); return 0; }
4
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const int N = 1e6 + 5; const int INF = 0xfffffff; template <typename T> void scan(T& x) { x = 0; char ch = getchar(); while (ch == ' ' || ch == '\n') ch = getchar(); do { x = 10 * x + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9'); } template <typename T> void print(T x) { if (x <= 9) putchar(x + '0'); else { print(x / 10); putchar((x % 10) + '0'); } } long long a[N]; void solve() { int n, p; long long sum1 = 0, sum2 = 0; scanf("%d%d", &n, &p); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum2 += a[i]; } long long ans = 0; for (int i = 0; i < n - 1; i++) { sum1 += a[i]; sum2 -= a[i]; ans = max(ans, sum1 % p + sum2 % p); } printf("%d\n", ans); } int main() { solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long prime(long long x) { long long mark = 0; { for (int i = 2; i <= sqrt(x); i++) { if (x % i == 0) { mark = 1; break; } } } return mark; } int main() { long long mark = 0; string s, s1; cin >> s; for (int i = 0; i < 5; i++) { cin >> s1; if (s1[0] == s[0] || s1[1] == s[1]) { mark = 1; } } if (mark == 1) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
1
#include <bits/stdc++.h> using namespace std; int been[500000]; int a[500000]; int n; long long ff[500000]; int cc; long long f(int x) { if (ff[x] != -0X3fffffff && x != 1) return ff[x]; long long r; long long xx = x; been[x] = cc; long long ans = 0; xx += a[x]; ans += a[x]; if (xx > n) { r = ans; goto loop; } ans += a[xx]; xx -= a[xx]; if (xx <= 0) { r = ans; goto loop; } if (been[xx] == cc) { r = -1; goto loop; } if (xx == 1) { r = -1; goto loop; } if (f(xx) == -1) { r = -1; goto loop; } r = f(xx) + ans; loop:; ff[x] = r; return ff[x]; } int main() { cin >> n; for (int i = 2; i <= n; i++) { ff[i] = -0X3fffffff; scanf("%d", &a[i]); } for (int i = 1; i <= n - 1; i++) { a[1] = i; cc = i; cout << f(1) << endl; } }
2
#include <bits/stdc++.h> using namespace std; void solve() { long long a, b, x, y, n; cin >> a >> b >> x >> y >> n; long long diff1 = a - x; long long diff2 = b - y; long long ta = a; long long tb = b; if (a - n <= x) { a = x; if (b - n + diff1 <= y) { b = y; } else { long long to = b - n + diff1; if (b - n + diff1 < y) { b = y; a = a - (to - y); } else { b = b - n + diff1; } } } else { a -= n; } long long prod1 = a * b; if (tb - n <= y) { tb = y; if (ta - n + diff2 <= x) { ta = x; } else { long long to = ta - n + diff2; if (ta - n + diff2 < x) { ta = x; tb = tb - (to - x); } else { ta = ta - n + diff2; } } } else { tb -= n; } long long prod2 = ta * tb; cout << min(prod1, prod2) << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int n, k, x; long long int a[200005]; long long int pre[200005], post[200005]; int main() { scanf("%lld", &n); scanf("%lld", &k); scanf("%lld", &x); long long int m = pow(x, k); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= n; i++) { pre[i] = (pre[i - 1] | a[i]); } for (int i = n; i >= 1; i--) { post[i] = (post[i + 1] | a[i]); } long long int ans = 0; for (int i = 1; i <= n; i++) { ans = max(ans, (pre[i - 1] | (a[i] * m) | post[i + 1])); } printf("%lld\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 5e5; int n, dp[30][30], cnt[N + 2], ans; string s[N + 2], tmp; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> tmp; cnt[i] = tmp.size(); s[i].push_back(tmp.front()); s[i].push_back(tmp.back()); } for (int i = 1; i <= n; i++) { int a = s[i][0] - 'a'; int b = s[i][1] - 'a'; for (int j = 0; j < 26; j++) { if (dp[j][a] == 0) continue; dp[j][b] = max(dp[j][b], dp[j][a] + cnt[i]); } dp[a][b] = max(dp[a][b], cnt[i]); } for (int i = 0; i < 26; i++) ans = max(ans, dp[i][i]); cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; string s, t; bool check(string ns) { int pos = 0; for (int i = 0; i < ns.size(); ++i) { if (ns[i] == t[pos]) ++pos; } return (pos == t.size()); } bool checksz(int m) { if (m == 0) return false; for (int i = 0; i + m <= s.size(); ++i) { string ns1 = s.substr(0, i); string ns2 = s.substr(i + m, (s.size() - (i + m) < 0) ? 0 : s.size() - (i + m)); if (check(ns1 + ns2)) { return true; } } return false; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> s >> t; int l = 0, r = s.size(), m; while (l < r) { m = (l + r + 1) / 2; if (checksz(m)) l = m; else r = m - 1; } cout << l << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; struct Node { int numComp; vector<pair<int, int> > lft; vector<pair<int, int> > rgt; }; Node dft; int n, K, m; vector<int> a[100010]; Node T[4 * 100010]; set<pair<int, int> > edges; int pr[100010]; vector<int> temp; int getParent(int u) { if (pr[u] == u) return u; if (pr[u] == 0) return u; temp.push_back(u); return pr[u] = getParent(pr[u]); } Node joinNode(Node T1, Node T2, int l, int r) { Node res; temp.clear(); for (auto x : T1.lft) { pr[x.first] = x.second; temp.push_back(x.first); } for (auto x : T1.rgt) { pr[x.first] = x.second; temp.push_back(x.first); } for (auto x : T2.lft) { pr[x.first] = x.second; temp.push_back(x.first); } for (auto x : T2.rgt) { pr[x.first] = x.second; temp.push_back(x.first); } res.numComp = T1.numComp + T2.numComp; int mid = (l + r) / 2; for (auto pu : T1.rgt) { int u = pu.first; for (int v : a[u]) { if (v <= mid || pr[v] == 0) continue; int du = getParent(u); int dv = getParent(v); if (du != dv) { res.numComp--; pr[du] = dv; temp.push_back(du); } } } for (auto x : T1.lft) { res.lft.push_back(pair<int, int>(x.first, getParent(x.first))); } for (auto x : T2.lft) { if (res.lft.size() == K) break; res.lft.push_back(pair<int, int>(x.first, getParent(x.first))); } for (auto x : T2.rgt) { res.rgt.push_back(pair<int, int>(x.first, getParent(x.first))); } for (auto x : T1.rgt) { if (res.rgt.size() == K) break; res.rgt.push_back(pair<int, int>(x.first, getParent(x.first))); } for (int i = 0; i < temp.size(); i++) { pr[temp[i]] = 0; } return res; } void build(int k, int l, int r) { if (l == r) { T[k].numComp = 1; T[k].lft.push_back(pair<int, int>(l, l)); T[k].rgt.push_back(pair<int, int>(l, l)); return; } int mid = (l + r) / 2; build(2 * k, l, mid); build(2 * k + 1, mid + 1, r); T[k] = joinNode(T[2 * k], T[2 * k + 1], l, r); } Node get(int k, int l, int r, int u, int v) { if (r < u || l > v) return dft; if (u <= l && r <= v) return T[k]; int mid = (l + r) / 2; Node T1 = get(2 * k, l, mid, u, v); Node T2 = get(2 * k + 1, mid + 1, r, u, v); return joinNode(T1, T2, l, r); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> K >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; if (u > v) swap(u, v); a[u].push_back(v); } build(1, 1, n); int q; cin >> q; for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; Node res = get(1, 1, n, l, r); cout << res.numComp << "\n"; } }
5
#include <bits/stdc++.h> using namespace std; int n; vector<pair<int, int>> st; void dfs(int cur, vector<vector<int>> &g, vector<int> &used, vector<int> &tps) { used[cur] = true; for (auto t : g[cur]) { if (!used[t]) { dfs(t, g, used, tps); } } tps.push_back(cur); } bool check(int k) { vector<vector<int>> g(n); vector<int> used(n); vector<int> tps; map<pair<int, int>, bool> rb; for (int i = 0; i < k; i++) { int u = st[i].first; int v = st[i].second; rb[{u, v}] = true; g[u].push_back(v); } for (int i = 0; i < n; i++) { if (!used[i]) { dfs(i, g, used, tps); } } reverse(tps.begin(), tps.end()); for (int i = 1; i < n; i++) { if (rb.count({tps[i - 1], tps[i]}) == 0) { return false; } } return true; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; st.push_back({u, v}); } if (!check(m)) { cout << -1 << endl; return 0; } int l = 0; int r = m; while (r - l > 1) { int m = (r + l) / 2; if (check(m)) r = m; else l = m; } cout << r << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, m; cin >> n >> m; vector<string> v1; vector<string> v2; for (int i = 0; i < m; i++) { string a, b; cin >> a >> b; v1.push_back(a); v2.push_back(b); } for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) { if (s == v1[j] || s == v2[j]) { if (v1[j].length() <= v2[j].length()) cout << v1[j] << " "; else cout << v2[j] << " "; } } } }
2
#include<bits/stdc++.h> using namespace std; int slove(int x,int y,int n); int main(void) { int N,Q; scanf("%d %d",&N,&Q); while(Q) { Q--; int x,y; scanf("%d %d",&x,&y); if(N==1) printf("%d\n",min(x,y)); else printf("%d\n",slove(x,y,N)); } return 0; } int slove(int x,int y,int n) { while(x!=y) { if(x > y) x = (x+n-2)/n; else if(y > x) y = (y+n-2)/n; } return x; }
0
#include <bits/stdc++.h> using namespace std; long long int MSS(long long int A[], long long int n) { long long int best = INT_MIN; long long int sum = 0; long long int i; for (i = 0; i < n; i++) { sum = max(A[i], A[i] + sum); best = max(best, sum); } return best; } long long int binarytodecimal(string n) { string num = n; long long int dec_value = 0; long long int base = 1; long long int len = num.length(); for (long long int i = len - 1; i >= 0; i--) { if (num[i] == '1') dec_value += base; base = base * 2; } return dec_value; } bool mark[10000007]; long long int nprime; long long int prime[1000007]; void sieve() { memset(mark, true, sizeof(mark)); mark[0] = mark[1] = false; for (long long int i = 2; i < 10000007; i++) { if (mark[i]) { prime[nprime++] = i; for (long long int j = 2 * i; j < 10000007; j += i) { mark[j] = false; } } } } long long int cmp(long long int a, long long int b) { return a > b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long int t, n, cnt = 0; cin >> t >> n; for (long long int tt = 1; tt <= t; tt++) { long long int mark[100] = {0}; string s; cin >> s; for (long long int i = 0; i < s.size(); i++) { mark[s[i] - '0'] = 1; } long long int mrk = 1; for (long long int i = 0; i <= n; i++) { if (mark[i] != 1) mrk = 0; } if (mrk == 1) cnt++; s.clear(); } cout << cnt << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; const int mod = 1e9 + 7; int n, ind[N]; char s[N]; int main() { while (~scanf("%d%s", &n, s)) { int c = 0, out = 0; int m = (n - 11) / 2; for (int i = 0; i <= n - 1; i++) if (s[i] == '8') { c++; ind[c] = i + 1; } else if (c <= m) out++; if (c <= m) puts("NO"); else { if (out > m) puts("NO"); else puts("YES"); } } return 0; }
2
#include <bits/stdc++.h> bool vis[111]; using namespace std; int main() { int n; string s; cin >> n; cin >> s; for (int i = 0; i < n; ++i) if (s[i] == '*') vis[i] = 1; for (int i = 0; i < n; ++i) { if (vis[i]) { for (int j = 1; i + j * 4 < n; ++j) { if (vis[i + j] && vis[i + j * 2] && vis[i + j * 3] && vis[i + j * 4]) { puts("yes"); return 0; } } } } puts("no"); return 0; }
1
#include<iostream> int main(){ int a, b; std::cin >> a >> b; std::cout << (a < b ? "a < b" : a>b ? "a > b" : "a == b") << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long MX = 2e5 + 5; long long s[MX]; long long a[MX]; long long f[MX]; long long n, m; const long long D = 1e9 + 7; long long p; long long q; long long pow2(long long x, long long y) { if (y == 0) return 1; long long ret = pow2(x, y / 2); ret *= ret; ret %= D; if (y % 2) ret *= x; return ret % D; } long long c(long long x, long long y) { return f[x] * pow2(f[y], D - 2) % D * pow2(f[x - y], D - 2) % D; } int main() { cin.tie(0); cout.tie(0); cin >> n >> m; q = pow2(2, n + 1); long long i, j; f[0] = 1; for (i = 1; i <= n; i++) { f[i] = f[i - 1] * i % D; cin >> a[i]; s[i] = s[i - 1] + a[i]; } s[n + 1] = m * 2; long long w = 0; for (i = 1; i <= n; i++) { for (j = max(w, m - s[i + 1] + 1); j <= min(m - s[i], i); j++) { p = (p + c(i, j) * i % D * pow2(2, n - i) % D) % D; } for (j = max(w, m - s[i + 1]); j <= min(m - s[i], i); j++) p = (p + c(i, j) * i % D * pow2(2, n - i) % D) % D; } cout << p * pow2(q, D - 2) % D; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long n, k; cin >> n >> k; if (n < k) cout << k; else cout << (n / k + 1) * k; }
1
#include <bits/stdc++.h> using namespace std; long long GCD(long long x, long long y) { if (y == 0) return x; return GCD(y, x % y); } long long LCM(long long x, long long y) { return (x * y) / GCD(x, y); } long long MPOW(long long a, long long b, long long m) { if (b == 0) return 1; long long x = MPOW(a, b / 2, m); x = (x * x) % m; if (b % 2 == 1) x = (x * a) % m; return x; } long long MINV(long long a, long long m) { return MPOW(a, m - 2, m); } long long MFACT(long long n, long long m) { if (m <= n) return 0; else if (n == 0) return 1; else return ((MFACT(n - 1, m) * n) % m); } inline long long max(long long a, long long b) { return (a > b) ? a : b; } inline long long min(long long a, long long b) { return (a < b) ? a : b; } inline void swap(long long &a, long long &b) { long long temp = a; a = b; b = temp; return; } inline long long max3(long long a, long long b, long long c) { return max(a, max(b, c)); } inline long long min3(long long a, long long b, long long c) { return min(a, min(b, c)); } inline bool sortbythis(pair<long long, long long> a, pair<long long, long long> b) { return (((a.first == b.first) && (a.second < b.second)) || (a.first < b.first)); } inline bool sortbythat(pair<long long, long long> a, pair<long long, long long> b) { return (((a.second == b.second) && (a.first < b.first)) || (a.second < b.second)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, o, e; cin >> n; e = n / 2; o = n - e; vector<long long> v(n); for (long long i = 0; i < n; ++i) { cin >> v[i]; if (v[i] == 0) continue; else if (v[i] % 2 == 0) e--; else o--; } if (n == 1) { cout << 0 << '\n'; return 0; } long long ans = 0; vector<long long> a, b, c, a1, b1; long long pos = 0, pos1 = 0; while (pos < n) { if (v[pos] != 0) { pos++; if (pos == n) break; if (v[pos] != 0 && v[pos] % 2 != v[pos - 1] % 2) ans++; } else { pos1 = pos + 1; while (pos1 < n && v[pos1] == 0) pos1++; if (pos == 0) { if (pos1 == n) c.push_back(pos1 - pos); else if (v[pos1] % 2 == 0) a1.push_back(pos1 - pos); else b1.push_back(pos1 - pos); } else if (pos1 == n) { if (v[pos - 1] % 2 == 0) a1.push_back(pos1 - pos); else b1.push_back(pos1 - pos); } else { if (v[pos - 1] % 2 == 0 && v[pos1] % 2 == 0) a.push_back(pos1 - pos); else if (v[pos - 1] % 2 == 1 && v[pos1] % 2 == 1) b.push_back(pos1 - pos); else c.push_back(pos1 - pos); } pos = pos1; } } sort(a.begin(), a.end(), greater<long long>()); sort(b.begin(), b.end(), greater<long long>()); sort(a1.begin(), a1.end(), greater<long long>()); sort(b1.begin(), b1.end(), greater<long long>()); while (a.size() > 0 && e - a.back() >= 0) { e -= a.back(); a.pop_back(); } while (a1.size() > 0 && e - a1.back() >= 0) { e -= a1.back(); a1.pop_back(); } while (b.size() > 0 && o - b.back() >= 0) { o -= b.back(); b.pop_back(); } while (b1.size() > 0 && o - b1.back() >= 0) { o -= b1.back(); b1.pop_back(); } ans += c.size() + a1.size() + b1.size() + 2 * (a.size() + b.size()); cout << ans << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } template <typename T = int> inline T nxt() { char c = nc(); T x = 0; int f = 1; for (; c > '9' || c < '0'; c = nc()) if (c == '-') f = -1; for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc()) ; x *= f; return x; } const int N = 100010; int cnt[N]; struct FT { vector<long long> s; FT(int n = N) : s(n){}; void update(int pos, long long val) { for (; pos < (int)(s).size(); pos |= pos + 1) s[pos] += val; } long long query(int pos) { long long sum = 0; for (; pos > 0; pos &= pos - 1) sum += s[pos - 1]; return sum; } }; struct RangeFT { FT ft1, ft2; void update(int l, int r, long long val) { ft1.update(l, val); ft1.update(r + 1, -val); ft2.update(l, (l - 1) * val); ft2.update(r + 1, -r * val); } long long query(int pos) { return ft1.query(pos + 1) * pos - ft2.query(pos + 1); } long long query(int l, int r) { return query(r) - query(l - 1); } }; struct query { int l, r; long long val; } Q[N]; int main() { cin.sync_with_stdio(0); cin.tie(0); int n = nxt(), m = nxt(), k = nxt(); RangeFT range_ft; for (int i = 0; i < (n); ++i) range_ft.update(i, i, nxt()); for (int i = 0; i < (m); ++i) { int l = nxt() - 1, r = nxt() - 1, v = nxt(); Q[i] = {l, r, v}; } for (int i = 0; i < (k); ++i) { int l = nxt() - 1, r = nxt() - 1; cnt[l]++, cnt[r + 1]--; } for (int i = 1; i < (N); ++i) cnt[i] += cnt[i - 1]; for (int i = 0; i < (N); ++i) { range_ft.update(Q[i].l, Q[i].r, Q[i].val * cnt[i]); } for (int i = 0; i < (n); ++i) cout << range_ft.query(i, i) << " "; }
3
#include <bits/stdc++.h> int main() { long long a, b, c = 0, product = 1; scanf("%I64d %I64d", &a, &b); while (a > 0 || b > 0) { c += ((b % 3 - a % 3 + 3) % 3) * product; a /= 3; b /= 3; product *= 3; } printf("%I64d\n", c); return 0; }
2
#include<bits/stdc++.h> using namespace std; int a[100010],s[100010]; int main() { int n,i,tot; scanf("%d",&n); for(i=1;i<=n;i++)scanf("%d",&a[i]); for(i=1;i<=n;i++)s[a[i]]++; tot=0; for(i=1;i<=100000;i++)if(s[i]>1)tot+=(s[i]-1); if(tot%2==0)printf("%d",n-tot); else printf("%d",n-tot-1); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 3e4 + 5; const int maxk = 505; int N, d; int dp[maxn][maxk], gem[maxn], vis[maxn][maxk]; int dfs(int pos, int prev) { int jj = prev - (d - 250); if (pos > 30000) return 0; if (vis[pos][jj]) return dp[pos][jj]; vis[pos][jj] = 1; int ans = 0; if (prev == 1) ans = max(ans, max(dfs(pos + 1, 1), dfs(pos + 2, 2))); else ans = max(ans, max(dfs(pos + prev - 1, prev - 1), max(dfs(pos + prev, prev), dfs(pos + prev + 1, prev + 1)))); return dp[pos][jj] = ans + gem[pos]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N >> d; for (int i = 1; i <= N; i++) { int p; cin >> p; gem[p]++; } cout << dfs(d, d) << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); int dx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; int dy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); } long long comb(long long a, long long b) { long long x = max(a - b, b), ans = 1; for (long long K = a, L = 1; K >= x + 1; K--, L++) { ans *= K; ans /= L; } return ans; } long long power(long long a, long long b, long long M) { long long result = 1; while (b > 0) { if (b & 1) result = result * a % M; a = a * a % M; b >>= 1; } return result; } void task() { int n, M; cin >> n >> M; cout << ((power(3, n, M) - 1) % M + M) % M << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); task(); return 0; }
3
#include <bits/stdc++.h> double a[200001]; int b[200001]; using namespace std; int main() { int T; double sum = 0; scanf("%d", &T); for (int i = 0; i < T; i++) { scanf("%lf", &a[i]); sum += a[i]; } double av = 0; av = sum / T; int num = 0, l = 0; ; for (int j = 0; j < T; j++) { if (a[j] == av) { num++; b[l++] = j + 1; } } if (num == 0) printf("0\n"); else { printf("%d\n", num); for (int j = 0; j < l - 1; j++) printf("%d ", b[j]); printf("%d\n", b[l - 1]); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int ar[] = {0, 0, 1, -1, 1, 1, -1, -1}; int ac[] = {1, -1, 0, 0, -1, 1, -1, 1}; int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; map<int, int> mp; for (int i = 0; i < n; i++) { int x; cin >> x; mp[x]++; } int ans = 0; for (int i = 0; i < k; i++) if (!mp[i]) ans++; cout << ans + mp[k]; return 0; }
1
#include "iostream" using namespace std; int main(){ int a; cin>>a; cout<<a+a*a+a*a*a<<endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> mal; vector<int> dev; for (int i = 0; i < n; i++) { int x; cin >> x; mal.push_back(x); } int m; cin >> m; for (int i = 0; i < m; i++) { int x; cin >> x; dev.push_back(x); } sort(mal.begin(), mal.end()); sort(dev.begin(), dev.end()); int i = 0; int j = 0; int res = 0; while (i < mal.size() && j < dev.size()) { if (abs(mal[i] - dev[j]) <= 1) { res++; i++; j++; } else { if (mal[i] < dev[j]) i++; else j++; } } cout << res; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, ans, count = 0; cin >> n >> k; int a[n]; map<int, int> m, m1; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; } for (auto i : m) for (int j = 1; j < i.second + 1; j++) m1[j] += i.second / j; for (auto i : m1) { if (i.second >= k) ans = i.first; } for (auto i : m) if (i.second >= ans) { for (int j = 1; j < (i.second / ans) + 1; j++) { if (count >= k) return 0; count++; cout << i.first << ' '; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long op[100005], k, n, s[100005]; int f1(long long s) { int l = 1, r = n + 1, mid; while (l < r) { if (r == l + 1) { return op[l] <= s ? l : -1; } mid = (l + r) >> 1; if (op[mid] <= s) l = mid; else r = mid; } } int f2(long long s) { int l = 0, r = n, mid; while (l < r) { if (r == l + 1) return op[r] >= s ? r : -1; mid = (l + r) >> 1; if (op[mid] >= s) r = mid; else l = mid; } } long long sv(long long at, long long x) { int l = f1(at), r = f2(at + x); long long ss = 0; if (l != -1) ss += at * l - s[l]; if (r != -1) ss += s[n] - s[r - 1] - 1ll * (at + x) * (n - r + 1); return ss; } bool check(long long x) { long long l = op[1], r = op[n], a, b; while (r - l >= 3) { a = l + (r - l) / 3; b = r - (r - l) / 3; if (sv(a, x) < sv(b, x)) r = b; else l = a; } for (long long i = l; i <= r; i++) if (sv(i, x) <= k) return 1; return 0; } int main() { scanf("%lld%lld", &n, &k); for (int i = 1; i <= n; i++) scanf("%lld", op + i); sort(op + 1, op + n + 1); for (int i = 1; i <= n; i++) s[i] = s[i - 1] + op[i]; long long l = -1, r = op[n] - op[1], mid; while (l < r) { if (r == l + 1) { printf("%lld", r); return 0; } mid = (l + r) >> 1; if (check(mid)) r = mid; else l = mid; } }
5
#include <bits/stdc++.h> using namespace std; int getNextX(int& x) { return x = (x * 37LL + 10007) % 1000000007; } void initAB(vector<int>& a, vector<int>& b, int n, int d, int x) { for (int i = (0); i < ((n)); ++i) a[i] = i + 1; for (int i = (0); i < ((n)); ++i) swap(a[i], a[getNextX(x) % (i + 1)]); for (int i = (0); i < ((n)); ++i) { if (i < d) b[i] = 1; else b[i] = 0; } for (int i = (0); i < ((n)); ++i) swap(b[i], b[getNextX(x) % (i + 1)]); } int main() { ios_base::sync_with_stdio(false); int n, d, x; cin >> n >> d >> x; vector<int> a(n), b(n); initAB(a, b, n, d, x); vector<int> p(n + 1); for (int i = (0); i < ((n)); ++i) p[a[i]] = i; vector<int> ones; for (int i = (0); i < ((n)); ++i) { if (b[i]) ones.push_back(i); } int const s = 30; for (int i = (0); i < ((n)); ++i) { int res = -1; for (int j = (0); j < ((s)); ++j) { int const target = n - j; if (p[target] <= i && b[i - p[target]]) { res = target; break; } } if (res == -1) { res = 0; for (int j = (0); j < (((int)ones.size())); ++j) { if (ones[j] > i) break; res = max(res, a[i - ones[j]]); } } cout << res << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[30]; bool mark[N]; bool dp[30][1000]; int n; void update() { dp[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j < 1000; j++) { dp[i][j] = dp[i - 1][j]; if (j >= a[i] && !mark[i]) dp[i][j] |= dp[i - 1][j - a[i]]; } } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); update(); int k = n; for (int i = 1; i <= n; i++) { int t = a[i] - 1; for (int j = i - 1; j >= 1; j--) { if (a[j] != a[i] - 1 && a[j] <= t && !mark[j] && dp[j - 1][t - a[j]]) { update(); mark[j] = 1; t -= a[j]; k--; } } if (t) k = 200; } if (k == 1) cout << "YES"; else cout << "NO"; return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 1e3 + 10; long long getans(long long x1, long long x2, long long d) { if (x1 < 0) x1 = -x1, x2 = -x2; if (x1 < x2) swap(x1, x2); long long o = (x1 + d - 1) / d; x1 -= d * o; x2 -= d * o; swap(x1, x2); return x2 / d - x1 / d; } int main() { int a, b, x1, y1, x2, y2; cin >> a >> b >> x1 >> y1 >> x2 >> y2; cout << max(getans(x1 + y1, x2 + y2, a * 2), getans(x1 - y1, x2 - y2, b * 2)) << endl; return 0; }
2
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) typedef long long ll; typedef pair<int, int> P; #define MAX 500005 int main(int, char**) { int n, l; cin >> n >> l; vector<int> a(n); rep(i,n) cin >> a[i]; deque<int> q; vector<int> ans; rep(i,n) { while (!q.empty() && a[q.back()] >= a[i]) q.pop_back(); q.push_back(i); if (i < l-1) continue; ans.push_back(a[q.front()]); if (q.front() == i-l+1) q.pop_front(); } int as = ans.size(); rep(i,as) { cout << ans[i]; if (i==as-1) cout << endl; else cout << " "; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b; cin >> a >> b; string prawdopodobienstwo[7] = {"0/0", "1/1", "5/6", "2/3", "1/2", "1/3", "1/6"}; cout << prawdopodobienstwo[max(a, b)]; }
1
#include <bits/stdc++.h> using namespace std; int min(int x, int y) { return x < y ? x : y; } int main() { int n, r; cin >> n; r = n % 10; cout << (r <= 10 - r ? n - r : n - r + 10) << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int arr[2000]; int main() { int n; cin >> n; int k = n / 2; if (n % 2) k++; cout << (n / 2 + 1) * k << endl; return 0; }
2
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:200000000") using namespace std; template <typename T> inline T Abs(T x) { return (x >= 0) ? x : -x; } template <typename T> inline T sqr(T x) { return x * x; } template <typename T> inline string toStr(T x) { stringstream st; st << x; string s; st >> s; return s; } template <typename T> inline int bit(T mask, int b) { return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0; } inline int nextInt() { int x; if (scanf("%d", &x) != 1) throw; return x; } inline long long nextInt64() { long long x; if (scanf("%lld", &x) != 1) throw; return x; } inline double nextDouble() { double x; if (scanf("%lf", &x) != 1) throw; return x; } const int INF = (int)1E9; const long long INF64 = (long long)1E18; const long double EPS = 1E-9; const long double PI = 3.1415926535897932384626433832795; const int MAXN = 300100; vector<int> l[MAXN]; int n, m, p; void update(map<int, int> &m, int x, int y, int &cnt) { if (m[x] == 0) cnt++; m[x] += y; if (m[x] == 0) cnt--; } vector<int> solve(const vector<int> &a, const vector<int> &b, int mod) { if (a.size() < b.size()) return vector<int>(); map<int, int> m; int cnt = 0, s = int(b.size()); for (int i = 0; i < (int)(s); i++) update(m, b[i], +1, cnt); for (int i = 0; i < (int)(s); i++) update(m, a[i], -1, cnt); vector<int> ans; if (cnt == 0) ans.push_back(mod); for (int i = s; i < int(a.size()); i++) { update(m, a[i - s], +1, cnt); update(m, a[i], -1, cnt); if (cnt == 0) ans.push_back(mod + p * (i - s + 1)); } return ans; } int main() { n = nextInt(); m = nextInt(); p = nextInt(); for (int i = 0; i < (int)(n); i++) { int x = nextInt(); l[i % p].push_back(x); } vector<int> b(m); for (int i = 0; i < (int)(m); i++) b[i] = nextInt(); vector<int> ans; for (int i = 0; i < (int)(p); i++) { vector<int> cur = solve(l[i], b, i); ans.insert(ans.end(), (cur).begin(), (cur).end()); } sort((ans).begin(), (ans).end()); cout << ans.size() << endl; for (int i = 0; i < (int)(ans.size()); i++) cout << ans[i] + 1 << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; int n; vector<long long> f(200100, 0); vector<long long> cf(200100, 0); long long rangeSum(int l, int r) { long long sec = 0; if (r >= 200100) { sec = n; } else { sec = cf[r - 1]; } return sec - cf[l - 1]; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; f[a]++; } cf[0] = f[0]; for (int i = 0; i < 200100; i++) { cf[i + 1] = cf[i] + f[i + 1]; } long long best = 0; for (int i = 1; i < 200100; i++) { if (f[i] == 0) continue; long long sum = 0; for (int j = i; j < 200100; j += i) { sum += j * rangeSum(j, j + i); } best = max(best, sum); } cout << best << endl; }
6
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int x, d, i = 0, j = 0; int a[100][2]; for (i = 0; i < n; i++) { for (j = 0; j < 2; j++) { scanf("%d", &a[i][j]); } } int l = 0, k = 0, z = 0; int flag = 0; for (k = 0; k < n; k++) { for (l = 0; l < n; l++) { if ((a[k][0] + a[k][1] == a[l][0]) && (a[l][0] + a[l][1] == a[k][0])) { flag = 1; } } } if (flag == 1) { printf("YES"); } else { printf("NO"); } return 0; }
1