solution
stringlengths
52
181k
difficulty
int64
0
6
#include<bits/stdc++.h> using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define mod 998244353 using ll=long long; const int INF=1000000000; const ll LINF=1001002003004005006ll; int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;} template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;} struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; template<typename T1,typename T2> ostream &operator<<(ostream &os,const pair<T1,T2>&p){ os<<p.first<<" "<<p.second; return os; } template<typename T> ostream &operator<<(ostream &os,const vector<T>&v){ for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" "); return os; } template<typename T1,typename T2> istream &operator>>(istream &is,pair<T1,T2>&p){ is>>p.first>>p.second; return is; } template<typename T> istream &operator>>(istream &is,vector<T>&v){ for(T &x:v)is>>x; return is; } template<ll Mod> struct ModInt{ long long x; ModInt():x(0){} ModInt(long long y):x(y>=0?y%Mod:(Mod-(-y)%Mod)%Mod){} ModInt &operator+=(const ModInt &p){ if((x+=p.x)>=Mod) x-=Mod; return *this; } ModInt &operator-=(const ModInt &p){ if((x+=Mod-p.x)>=Mod)x-=Mod; return *this; } ModInt &operator*=(const ModInt &p){ x=(int)(1ll*x*p.x%Mod); return *this; } ModInt &operator/=(const ModInt &p){ (*this)*=p.inverse(); return *this; } ModInt operator-()const{return ModInt(-x);} ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;} ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;} ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;} ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;} ModInt operator==(const ModInt &p)const{return x==p.x;} ModInt operator!=(const ModInt &p)const{return x!=p.x;} ModInt inverse()const{ int a=x,b=Mod,u=1,v=0,t; while(b>0){ t=a/b; swap(a-=t*b,b);swap(u-=t*v,v); } return ModInt(u); } ModInt pow(long long n)const{ ModInt ret(1),mul(x); while(n>0){ if(n&1) ret*=mul; mul*=mul;n>>=1; } return ret; } friend ostream &operator<<(ostream &os,const ModInt &p){return os<<p.x;} friend istream &operator>>(istream &is,ModInt &a){long long t;is>>t;a=ModInt<Mod>(t);return (is);} static int get_mod(){return Mod;} }; using mint=ModInt<mod>; template<int MAX> struct comcalc{ vector<mint> fact,finv,inv; comcalc():fact(MAX),finv(MAX),inv(MAX){ fact[0]=mint(1),fact[1]=mint(1),finv[0]=mint(1),finv[1]=mint(1),inv[1]=mint(1); for(int i=2;i<MAX;i++){ fact[i]=fact[i-1]*mint(i); inv[i]=mint(0)-inv[mod%i]*(mint(mod/i)); finv[i]=finv[i-1]*inv[i]; } } mint com(int n,int k){ if(n<k) return mint(0); if(n<0 or k<0) return mint(0); return fact[n]*(finv[k]*finv[n-k]); } mint fac(int n){ return fact[n]; } // 重複組み合わせ:n種類の物から重複を許し,k個選ぶ mint nHk(int n,int k){ return com(n+k-1,k); } // 玉n区別,箱k区別,各箱1個以上O(k) mint F12_dis_dis_one(int n,int k){ if(n<k)return mint(0); mint ret=0; for(int i=0;i<=k;i++){ mint add=com(k,i)*(mint(i).pow(n)); if((k-i)%2) ret-=add; else ret+=add; } return ret; } /* sum combination(n+x, x), x=l to r https://www.wolframalpha.com/input/?i=sum+combination%28n%2Bx+%2Cx%29%2C+x%3Dl+to+r&lang=ja check n+x < [COM_PRECALC_MAX] */ mint sum_of_comb(int n,int l,int r){ if(l>r)return mint(0); mint ret=mint(r+1)*com(n+r+1,r+1)-mint(l)*com(l+n,l); ret/=mint(n+1); return ret; } }; mint pow_mod(mint x,ll n){ return x.pow(n); } mint inv_mod(mint x){ return x.inverse(); } // O(n) mint fact_mod(ll n){ mint ret=1; for(int i=2;i<=n;i++) ret*=mint(i); return ret; } // O(r) mint comb_mod(ll n,ll r){ if(r>n-r) r=n-r; if(r==0) return 1; mint a=1,b=mint(fact_mod(r)).inverse(); for(int i=0;i<r;i++)a*=mint(n-i); return a*b; } const int MAX=4010000; using cominit=comcalc<MAX>; struct NTTmodint{ vector<mint> dw,idw; int max_base; mint root; NTTmodint(){ ll tmp=mod-1; max_base=0; while(tmp%2==0) tmp>>=1,max_base++; root=2; while(root.pow((mod-1)>>1).x==1) root+=1; dw.resize(max_base);idw.resize(max_base); for(int i=0;i<max_base;i++){ dw[i]=-root.pow((mod-1)>>(i+2)); idw[i]=mint(1)/dw[i]; } } void ntt(vector<mint> &a){ int n=(int)a.size(); for(int m=n;m>>=1;){ mint w=1; for(int s=0,k=0;s<n;s+=2*m){ for(int i=s,j=s+m;i<s+m;i++,j++){ auto x=a[i],y=a[j]*w; a[i]=x+y,a[j]=x-y; } w*=dw[__builtin_ctz(++k)]; } } } void intt(vector<mint> &a,bool f=true){ int n=(int)a.size(); for(int m=1;m<n;m*=2){ mint w=1; for(int s=0,k=0;s<n;s+=2*m){ for(int i=s,j=s+m;i<s+m;i++,j++){ auto x=a[i],y=a[j]; a[i]=x+y,a[j]=(x-y)*w; } w*=idw[__builtin_ctz(++k)]; } } if(f){ mint inv_sz=mint(1)/n; for(int i=0;i<n;i++) a[i]*=inv_sz; } } vector<mint> multiply(vector<mint> a,vector<mint> b){ int need=(int)a.size()+b.size()-1; int nbase=1; while((1<<nbase)<need) nbase++; int sz=1<<nbase; a.resize(sz,0);b.resize(sz,0); ntt(a);ntt(b); mint inv_sz=mint(1)/sz; for(int i=0;i<sz;i++) a[i]*=b[i]*inv_sz; intt(a,false); a.resize(need); return a; } }; signed main(){ cominit F; NTTmodint ntt; vector<mint> x(200000);// mul j=i to i, 2*iC2 x[0]=1; x[1]=1; for(int i=2;i<200000;i++){ x[i]=x[i-1]*F.com(i,2); } // k個からl個ペアを作る auto pairing=[&](int k,int l){ // cout<<k<<" "<<l<<" : "<<F.fac(k)/F.fac(k-2*l)/F.fac(l)/mint(2).pow(l)<<endl; return F.fac(k)/F.fac(k-2*l)/F.fac(l)/mint(2).pow(l); }; int n;cin>>n; map<int,int> mp; rep(i,2*n){ int x;cin>>x; mp[x]++; } vector<int> v; for(auto p:mp) v.push_back(p.second); vector<vector<mint>> fs; rep(i,(int)v.size()){ vector<mint> f; for(int j=0;j<=v[i];j+=2) f.push_back(pairing(v[i],j/2)); fs.push_back(f); } int m=(int)fs.size(); using P=pair<int,int>; // size, index of fs priority_queue<P,vector<P>,greater<P>> que; rep(i,m) que.push({(int)fs[i].size(),i}); // マージ while(que.size()>1){ auto [_ ,u]=que.top();que.pop(); auto [__,v]=que.top();que.pop(); // u<-v if(u>v) swap(u,v); fs[u]=ntt.multiply(fs[u],fs[v]); que.push({(int)fs[u].size(),u}); } auto [_, u]=que.top(); mint res=0; auto &conv_res=fs[u]; rep(i,(int)conv_res.size()){ // n-i個のペアを今作る. mint tmp=conv_res[i]*pairing(2*(n-i),(n-i)); if(i%2) res-=tmp; else res+=tmp; } cout<<res<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string str; int counter = 1; int inhand = 1; while (cin >> str) { for (int i = 1; i < str.length(); i++) { if (str[i] != str[i - 1] || inhand == 5) { counter++; inhand = 1; } else { inhand++; } } cout << counter << endl; } }
1
#include <bits/stdc++.h> #pragma GCC optimize("O3") std::pair<int, int> DR[] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {1, 1}, {1, -1}}; using namespace std; vector<int> vec[1 << 17]; int s[1 << 17], a[1 << 17]; int x, n; void dfs(int nod, int par) { if ((int)((vec[nod]).size()) == 1 && nod != 1) s[nod] = 1; for (auto it : vec[nod]) { if (it != par) { dfs(it, nod); s[nod] += s[it]; } } a[s[nod]]++; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); cin >> n; if (n == 1) return cout << 1, 0; for (int i = 2; i <= n; i++) { cin >> x; vec[i].push_back(x); vec[x].push_back(i); } dfs(1, 0); int sum = 0; int cnt = 0; for (int i = 1; i <= n; i++) { while (i > sum) { cnt++; sum += a[cnt]; } cout << cnt << ' '; } }
4
#include <bits/stdc++.h> namespace IO { char gc() { return getchar(); } template <typename Tp> bool get1(Tp &x) { bool neg = 0; char c = gc(); while (c != EOF && (c < '0' || c > '9') && c != '-') c = gc(); if (c == '-') c = gc(), neg = 1; if (c == EOF) return false; x = 0; for (; c >= '0' && c <= '9'; c = gc()) x = x * 10 + c - '0'; if (neg) x = -x; return true; } template <typename Tp> void printendl(Tp x) { if (x < 0) putchar('-'), x = -x; static short a[40], sz; sz = 0; while (x > 0) a[sz++] = x % 10, x /= 10; if (sz == 0) putchar('0'); for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]); puts(""); } } // namespace IO using IO::get1; using IO::printendl; const int inf = 0x3f3f3f3f; const long long Linf = 1ll << 61; const int maxn = 100111; const int mod = 998244353; int qpow(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = 1ll * ret * x % mod; x = 1ll * x * x % mod; y >>= 1; } return ret; } inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } int n, m, tot, coef; long long a[111], b[111]; int cnt[111], ans[111]; void dfs(int x, long long now) { if (x == tot) { cnt[__builtin_popcountll(now)]++; return; } dfs(x + 1, now); dfs(x + 1, now ^ a[x]); } int main() { get1(n) && get1(m); coef = 1; while (n--) { long long x; get1(x); for (int i = 0; i < tot; i++) if (a[i] & -a[i] & x) x ^= a[i]; if (!x) { add(coef, coef); continue; } for (int i = 0; i < tot; i++) if (a[i] & x & -x) a[i] ^= x; a[tot++] = x; } if (tot <= m / 2) { dfs(0, 0); for (int i = 0; i <= m; i++) ans[i] = 1ll * cnt[i] * coef % mod; } else { static int seq[111], have[111], c[111][111], ntot; for (int i = 0; i <= m; i++) { c[i][0] = 1; for (int j = 1; j <= m; j++) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; } for (int i = 0; i < tot; i++) have[__builtin_ctzll(a[i])] = 1; for (int i = 0; i < m; i++) if (!have[i]) { seq[ntot++] = i; if (coef & 1) coef += mod; coef >>= 1; } for (int i = 0; i < ntot; i++) { b[i] = 1ll << m - i; for (int j = 0; j < tot; j++) if ((a[j] >> seq[i]) & 1) b[i] |= 1ll << j; } tot = ntot; memcpy(a, b, sizeof(a)); dfs(0, 0); for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { int sum = 0; for (int k = 0; k <= j; k++) { int now = 1ll * c[j][k] * c[m - j][i - k] % mod; if (k % 2 == 0) add(sum, now); else add(sum, mod - now); } add(ans[i], 1ll * sum * cnt[j] % mod); } ans[i] = 1ll * ans[i] * coef % mod; } } for (int i = 0; i <= m; i++) printf("%d%c", ans[i], i == m ? '\n' : ' '); return 0; }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<ll, ll>; const ll MOD = 1e9 + 7, N = 1e5 + 10; ll c = 0; void test() { ll n, p, k; cin >> n >> p >> k; vector<ll> a(n); vector<ll> sm(k); vector<ll> pre(n + 1); pre[0] = 0; for (ll i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); for (ll i = 0; i < n; i++) { pre[i + 1] = pre[i] + a[i]; } auto kk = upper_bound(pre.begin(), pre.end(), p) - pre.begin(); ll ans = kk - 1; for (ll i = k - 1; i < n; i++) { sm[i % k] += a[i]; ll val = pre[(i + 1) % k]; val += sm[i % k]; if (val <= p) ans = max(ans, i + 1); } cout << ans << '\n'; return; } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); ll tt = 1; cin >> tt; for (ll i = 0; i < tt; i++) test(); return 0; }
2
#include<bits/stdc++.h> using namespace std; int main(){ string S; cin>>S; int ans=700; for(int i;i<3;i++) if(S.at(i)=='o') ans+=100; cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10, MAXM = 5e3 + 100, INF = 1e9; vector<int> adj[MAXN]; int mx[MAXM], mx_t, a[MAXN], parent[MAXM], l[MAXM], r[MAXM], n, m; pair<pair<int, int>, double> baze[MAXM]; double dp[MAXM][MAXM], p[MAXM]; void dfs(int v) { for (auto u : adj[v]) dfs(u); for (int i = 0; i <= m; i++) dp[i][v] = 1; for (auto u : adj[v]) for (int i = 0; i <= m; i++) dp[i][v] *= dp[min(m, mx[v] - mx[u] + i)][u]; for (int i = m; i > -1; i--) { dp[i][v] = dp[i][v] * (1 - p[v]); if (i > 0) dp[i][v] += p[v] * dp[i - 1][v]; } } int main() { fill(parent, parent + MAXM, -1); scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) { scanf("%d %d %lf", &baze[i].first.first, &baze[i].first.second, &baze[i].second); baze[i].first.first--; baze[i].first.second--; } sort(baze + 1, baze + m + 1); baze[0].first.first = 0; baze[0].first.second = n - 1; baze[0].second = 0; for (int i = 0; i < m + 1; i++) l[i] = baze[i].first.first, r[i] = baze[i].first.second, p[i] = baze[i].second; for (int i = 0; i < m + 1; i++) for (int j = l[i]; j <= r[i]; j++) mx[i] = max(mx[i], a[j]); for (int i = m; i > 0; i--) if (r[i] == r[i - 1] and l[i] == l[i - 1]) parent[i] = i - 1, adj[i - 1].push_back(i); for (int i = 1; i < m + 1; i++) { int l2 = -1, ind, r2; if (parent[i] == -1) { for (int j = m; j > -1; j--) if (i != j and l[j] <= l[i] and r[i] <= r[j]) { if ((l[j] > l2 or (l[j] == l2 and r[j] < r2)) and (l[i] != l[j] or r[i] != r[j] or j == 0)) { l2 = l[j]; r2 = r[j]; ind = j; } } parent[i] = ind; adj[ind].push_back(i); } } dfs(0); double ans = dp[0][0] * mx[0]; for (int i = 1; i <= m; i++) ans += (dp[i][0] - dp[i - 1][0]) * (i + mx[0]); printf("%.9lf", ans); return 0; }
3
#include<iostream> using namespace std; int main() { while(true){ int maxamnt = 0; char ans; for(int i = 0; i < 5; ++i){ int bn,an; cin >> bn; cin >> an; if( an + bn == 0 ) return 0; if( maxamnt < an+bn ){ maxamnt = an+bn; ans = 'A' + i; } } cout << ans << ' ' << maxamnt << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct hashLL { long long operator()(long long x) const { x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9); x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb); x = x ^ (x >> 31); return x; } }; struct Hashmap { long long toXor; unordered_map<long long, int, hashLL> m; Hashmap() { srand(time(0)); toXor = rand(); m.reserve(4096); m.max_load_factor(0.25); } int &operator[](const long long &key) { return m[key ^ toXor]; } unordered_map<long long, int, hashLL>::iterator find(const long long &key) { return m.find(key ^ toXor); } unordered_map<long long, int, hashLL>::iterator end() { return m.end(); } }; long long a[100001]; Hashmap mp; int n, k; int main() { std::ios::sync_with_stdio(false); cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; vector<long long> v; if (k == 0) v.push_back(0); else if (k == 1) v.push_back(1); else if (k == -1) { v.push_back(1); v.push_back(-1); } else { long long pp = 1; for (int i = 0;; i++) { v.push_back(pp); pp *= k; if (pp > 100000000000000 || pp < -100000000000000) break; } } long long c = 0; long long sum = 0; mp[sum]++; for (int i = 0; i < n; i++) { sum += a[i]; for (auto i : v) { c += mp[sum - i]; } mp[sum]++; } cout << c; printf("\n"); }
3
#include <bits/stdc++.h> int main() { int k, d, i; scanf("%d%d", &k, &d); if (d) { printf("%d", d); for (i = 1; i < k; i++) printf("0"); } else { if (k == 1) printf("0"); else printf("No solution"); } }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") const double PI = 3.14159265359; using namespace std; const int N = 5e5 + 10; int a[N], tr[N]; map<int, int> r; int n; int bitwise(int x) { return x & -x; } void add(int x, int c) { for (int i = x; i <= n; i += bitwise(i)) { tr[i] += c; } } int sum(int x) { int ans = 0; for (int i = x; i >= 1; i -= bitwise(i)) { ans += tr[i]; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { r.clear(); cin >> n; int maxn = 0; int flag = 0; int ans = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; maxn = max(a[i], maxn); r[a[i]]++; if (r[a[i]] > 1) flag = 1; } if (flag == 1) { cout << "YES" << "\n"; continue; } for (int i = 1; i <= n; i++) { ans += sum(n) - sum(a[i] - 1); add(a[i], 1); } if (ans % 2 == 0) { cout << "YES" << "\n"; } else { cout << "NO" << "\n"; } for (int i = 0; i <= maxn; i++) { tr[i] = 0; } } }
4
#include <bits/stdc++.h> using namespace std; long long n, l, r, a[120] = {0}, k = 0; bool fg1 = 1, fg2 = 0; int main() { std::ios::sync_with_stdio(false); cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i]; if (a[i] < a[i - 1]) fg1 = 0; } if (!fg1) { while (!fg2) { long long i, c = 0, l = -1, r = -1; for (i = 1; i <= n; i++) { if (a[i] < a[i - 1]) { l = i - 1; r = i; cout << l << " " << r << endl; long long t = a[i]; a[i] = a[i - 1]; a[i - 1] = t; fg2 = 0; } } if (l == -1) fg2 = 1; } return 0; } return 0; }
2
#include<bits/stdc++.h> #define gc getchar #define re register #define rep(i,s,t) for(re int i=s;i<=t;++i) #define reb(i,s,t) for(re int i=s;i>=t;--i) using namespace std; typedef long long ll; const int Mod=1e9+7,N=3010; int n,f[N],pre[N],suf[N]; string S; inline int read(){ int rt=0; char ch=gc(); while(ch<'0'||ch>'9')ch=gc(); while(ch>='0'&&ch<='9'){ rt=(rt<<3)+(rt<<1)+(ch&15); ch=gc(); } return rt; } int main(){ n=read(); cin>>S; f[1]=pre[1]=suf[1]=1; rep(i,2,n){ if(S[i-2]=='<')rep(j,1,i)f[j]=pre[j-1]; else rep(j,1,i)f[j]=suf[j]; rep(j,1,i)pre[j]=(pre[j-1]+f[j])%Mod; reb(j,i,1)suf[j]=(suf[j+1]+f[j])%Mod; } return not printf("%d",pre[n]); }
0
#include <bits/stdc++.h> using namespace std; int main() { long long a[50000 + 7], n, sum = 0, k, m = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } k = sum / n; for (int i = 0; i < n; i++) { if (a[i] < k) { a[i + 1] -= (k - a[i]); m += (k - a[i]); } if (a[i] > k) { a[i + 1] += (a[i] - k); m += (a[i] - k); } } cout << m << endl; }
2
#include <bits/stdc++.h> using namespace std; long long work(char c) { int i, j, tmp, k, l; if (c < '@' && '[' > c) i = 1; else i = 0; if ('`' < c && '{' > c) j = 1; else j = 0; if (c >= 'a') tmp = c - 'a' + 1; else tmp = c - 'A' + 1; return (i - j) * tmp; } int main() { char s[1000]; scanf("%s", s); int len = strlen(s); long long ans = 0; for (int i = 0; i < len; i++) { if (s[i] >= 'a' && s[i] <= 'z') ans -= s[i] - 'a' + 1; if (s[i] >= 'A' && s[i] <= 'Z') ans += s[i] - 'A' + 1; } cout << ans << endl; return 0; }
3
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <climits> #include <ctime> #include <queue> #include <stack> #include <algorithm> #include <list> #include <vector> #include <set> #include <map> #include <iostream> #include <deque> #include <complex> #include <string> #include <iomanip> #include <sstream> #include <bitset> #include <valarray> #include <unordered_map> #include <iterator> #include <assert.h> using namespace std; typedef long long int ll; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; #define REP(i,x) for(int i=0;i<(int)(x);i++) #define REPS(i,x) for(int i=1;i<=(int)(x);i++) #define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--) #define RREPS(i,x) for(int i=((int)(x));i>0;i--) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++) #define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++) #define ALL(container) (container).begin(), (container).end() #define RALL(container) (container).rbegin(), (container).rend() #define SZ(container) ((int)container.size()) #define mp(a,b) make_pair(a, b) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os; } template<class T> ostream& operator<<(ostream &os, const set<T> &t) { os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os; } template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);} template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);} namespace geom{ #define X real() #define Y imag() #define at(i) ((*this)[i]) #define SELF (*this) enum {TRUE = 1, FALSE = 0, BORDER = -1}; typedef int BOOL; typedef double R; const R INF = 1e8; R EPS = 1e-8; const R PI = 3.1415926535897932384626; inline int sig(const R &x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } inline BOOL less(const R &x, const R &y) {return sig(x-y) ? x < y : BORDER;} typedef complex<R> P; inline R norm(const P &p){return p.X*p.X+p.Y*p.Y;} inline R inp(const P& a, const P& b){return (conj(a)*b).X;} inline R outp(const P& a, const P& b){return (conj(a)*b).Y;} inline P unit(const P& p){return p/abs(p);} inline P proj(const P &s, const P &t){return t*inp(s, t)/norm(t);} inline int ccw(const P &s, const P &t, const P &p, int adv=0){ int res = sig(outp(t-s, p-s)); if(res || !adv) return res; if(sig(inp(t-s, p-s)) < 0) return -2; // p-s-t if(sig(inp(s-t, p-t)) < 0) return 2; // s-t-p return 0; // s-p-t } struct L : public vector<P>{ // line L(const P &p1, const P &p2){this->push_back(p1);this->push_back(p2);} L(){} P dir()const {return at(1) - at(0);} BOOL online(const P &p)const {return !sig(outp(p-at(0), dir()));} }; struct S : public L{ // segment S(const P &p1, const P &p2):L(p1, p2){} S(){} BOOL online(const P &p)const { if(!sig(norm(p - at(0))) || !sig(norm(p - at(1)))) return BORDER; // 座標の二乗とEPSの差が大きすぎないように注意 return !sig(outp(p-at(0), dir())) && inp(p-at(0), dir()) > EPS && inp(p-at(1), -dir()) > -EPS; //return !sig(abs(at(0)-p) + abs(at(1) - p) - abs(at(0) - at(1))); } }; struct C : public P{ C(){} C(const P& p, const R r):P(p), r(r){} R r; BOOL inside(const P& p)const { return less(norm(p-SELF), r*r);} }; struct F : public C{ R s, t; F(const C &c, R ss, R tt):C(c), s(ss), t(tt){ if(PI < s) s -= 2*PI; if(PI < t) t -= 2*PI; } BOOL inside(const P& p)const { P v = p - SELF; if(!sig(norm(v))) return BORDER; R a = arg(v); if(t < s){ if((!less(s, a) && !less(a, t)) || !less(norm(v), r*r)) return FALSE; return less(s, a) | less(a, t) | less(norm(v), r*r); }else{ if(!less(s, a) || !less(a, t) || !less(norm(v), r*r)) return FALSE; return less(s, a) | less(a, t) | less(norm(v), r*r); } } }; P crosspoint(const L &l, const L &m); struct G : public vector<P>{ G(size_type size=0):vector(size){} S edge(int i)const {return S(at(i), at(i+1 == size() ? 0 : i+1));} BOOL contains(const P &p)const { R sum = .0; REP(i, size()){ if(S(at(i), at((i+1)%size())).online(p)) return BORDER; // online sum += arg((at(i) - p) / (at((i+1)%size()) - p)); } return !!sig(sum); } R area()const { R sum = 0; REP(i, size()) sum += outp(at(i), at((i+1)%size())); return abs(sum / 2.); } P gp()const { P r(.0, .0); REP(i, size()){ const S &s = edge(i); r += (s[0]+s[1])*outp(s[0], s[1]); } return r / (6*area()); } G convex_hull(bool online = false) { if(size() < 2) return *this; sort(ALL(*this)); G r; r.resize((int)size()*2); int k=0; for(int i=0;i<size();r[k++]=at(i++)) while(k>1 && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--; int t = k; for(int i=(int)size()-1;i>=0;r[k++]=at(i--)) while(k>t && ccw(r[k-2], r[k-1], at(i)) < 1-online) k--; r.resize(k-1); return r; } G cut(const L &l)const { G g; REP(i, size()){ const S &s = edge(i); if(ccw(l[0], l[1], s[0], 0) >= 0) g.push_back(s[0]); if(ccw(l[0], l[1], s[0], 0) * ccw(l[0], l[1], s[1], 0) < 0) g.push_back(crosspoint(s, l)); } return g; } G Voronoi(const vector<P> &p, const int t)const { G g = *this; REP(i, p.size())if(i!=t){ const P m = (p[t]+p[i])*(R)0.5; g = g.cut(L(m, m+(p[i]-p[t])*P(0, 1))); } return g; } }; inline P proj(const P &s, const L &t){return t[0] + proj(s-t[0], t[1]-t[0]);} inline P reflect(const P &s, const L &t){return (R)2.*proj(s, t) - s;} inline S reflect(const S &s, const L &t){return S(reflect(s[0], t), reflect(s[1], t));} BOOL intersect(const S &s, const S &t){ const int p = ccw(t[0], t[1], s[0], 1) * ccw(t[0], t[1], s[1], 1); const int q = ccw(s[0], s[1], t[0], 1) * ccw(s[0], s[1], t[1], 1); return (p>0||q>0) ? FALSE : (!p||!q) ? BORDER : TRUE; } BOOL intersect(const S &s, const L &l){ if(l.online(s[0]) || l.online(s[1])) return BORDER; return (sig(outp(l.dir(), s[0]-l[0])) * sig(outp(l.dir(), s[1]-l[0])) <= 0); } R dist2(const L &l, const P &p){return norm(outp(l.dir(), p - l[0])) / norm(l.dir());} R dist2(const S &s, const P &p){ if(inp(p-s[0], s.dir()) < EPS) return norm(p - s[0]); if(inp(p-s[1], -s.dir()) < EPS) return norm(p - s[1]); return dist2((const L &)s, p); } R dist2(const S &s, const L &l){ return intersect(s, l) ? .0 : min(dist2(l, s[0]), dist2(l, s[1])); } R dist2(const S &s, const S &t){ return intersect(s, t) ? .0 : min(min(dist2(s, t[0]), dist2(t, s[0])), min(dist2(s, t[1]), dist2(t, s[1]))); } template <class T> R dist2(const G &g, const T& t){ // todo: 内部に完全に含まれる場合 R res = INF; REP(i, g.size()) res = min(res, dist2(g.edge(i), t)); return res; } template<class S, class T> R dist(const S& s, const T& t){return sqrt(dist2(s, t));} inline BOOL intersect(const C &a, const C &b){ return less((a.r-b.r)*(a.r-b.r), norm(a-b)) + less(norm(a-b), (a.r+b.r)*(a.r+b.r)) - 1; } inline BOOL intersect(const C &c, const L &l){ return less(dist2(l, c), c.r*c.r); } inline BOOL intersect(const C &c, const S &s){ int d = less(dist2(s, c), c.r*c.r); if(d != TRUE) return d; int p = c.inside(s[0]), q = c.inside(s[1]); return (p<0 || q<0) ? BORDER : p&q; } inline P crosspoint(const L &l, const L &m){ R A = outp(l.dir(), m.dir()), B = outp(l.dir(), l[1] - m[0]); if(!sig(abs(A)) && !sig(abs(B))) return m[0]; // same line if(abs(A) < EPS) assert(false); // !!!PRECONDITION NOT SATISFIED!!! return m[0] + B / A * (m[1] - m[0]); } #undef SELF #undef at } using namespace geom; int f = 0; namespace std{ bool operator<(const P &a, const P &b){return sig(a.X-b.X) ? a.X < b.X : a.Y+EPS < b.Y;} bool operator==(const P &a, const P &b){return abs(a-b) < EPS;} istream& operator>>(istream &is, P &p){R x,y;is>>x>>y;p=P(x, y);return is;} istream& operator>>(istream &is, L &l){l.resize(2);return is >> l[0] >> l[1];} istream& operator>>(istream &is, C &c){return is >> (P &)c >> c.r;} const R B = 200; const R Z = .5; ostream& operator<<(ostream &os, const C &c){return os << "circle("<<B+Z*(c.X)<<", "<<1000-B-Z*(c.Y)<<", "<<Z*(c.r)<<")";} ostream& operator<<(ostream &os, const P &p){return os << C(p, 2./Z);} ostream& operator<<(ostream &os, const S &s){return os << "line("<<B+Z*(s[0].X)<<", "<<1000-B-Z*(s[0].Y)<<", "<<B+Z*(s[1].X)<<", "<<1000-B-Z*(s[1].Y)<<")";} ostream& operator<<(ostream &os, const G &g){REP(i, g.size()) os << g.edge(i) << endl;return os;} } int n, m; vi path(S root, const vector<P>& pin){ vi res; if(!sig(root.dir().X)) return res; REPS(i, n){ P cp = crosspoint(root, L(pin[i], pin[i]+P(0, 1))); if(root.online(cp) == FALSE) continue; if(abs(root[0].X - cp.X) < EPS || abs(root[1].X - cp.X) < EPS) continue; res.push_back(pin[i].Y < cp.Y ? i : -i); } sort(ALL(res), [&](int i, int j){ return abs(root[0].X - pin[abs(i)].X) < abs(root[0].X - pin[abs(j)].X); }); return res; } int main(int argc, char *argv[]){ ios::sync_with_stdio(false); int T = 1; while(cin >> m >> n, n){ vector<P> root(m), pin(n+1); REP(i, m){ cin >> root[i]; root[i] *= polar((R)1, (R)1); } pin[0] = root[0]; REPS(i, n){ cin >> pin[i]; pin[i] *= polar((R)1, (R)1); } sort(pin.begin()+1, pin.end()); pin.push_back(root.back()); vector<int> st; st.push_back(0); REP(i, m-1){ S s(root[i], root[i+1]); vi isc = path(s, pin); FOR(it, isc){ if(st.back() == *it) st.pop_back(); else st.push_back(*it); } } st.push_back(n+1); vector<R> dp(st.size(), INF); vi prev(st.size()); dp[0] = .0; int save = 0; REP(i, st.size()){ if(i && st[i] == -st[i-1]){ save = i; dp[i] = dp[i-1]; prev[i] = prev[i-1]; continue; } for(int j=save;j<i;j++){ S r(pin[abs(st[j])], pin[abs(st[i])]); vi pt = path(r, pin); if([&](){ for(int k=1;k<=pt.size()&&j+k<i;k++) if(pt[k-1] != st[j+k]) return 0; return 1; }()){ if(chmin(dp[i], dp[j] + abs(r.dir()))){ prev[i] = j; } } } } printf("%.10f\n", (double)dp.back()); } return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MOD=1e9+7; const int INF=1e9; int main(){ ll s; cin>>s; ll x=(s+INF-1)/INF; ll y=x*INF-s; cout<<"0 0 1 1000000000 "<<x<<" "<<y<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int N = 200010; const int LN = 20; long long mod = 1e9 + 7; long long fmod(long long b, long long exp) { long long res = 1; while (exp) { if (exp & 1ll) res = (res * b) % mod; b = (b * b) % mod; exp /= 2ll; } return res; } int ver[2 * N]; long long bit[2 * N]; int st[N], en[N], A[N]; int f[N]; long long dp[N]; int par[N][LN]; int mark[N], lev[N]; vector<int> adj[N]; int tim = 0, n; void dfs(int i, int p = -1) { if (p + 1) { lev[i] = lev[p] + 1; par[i][0] = p; for (int j = 1; j < LN; j++) if (par[i][j - 1] + 1) par[i][j] = par[par[i][j - 1]][j - 1]; } st[i] = (++tim); ver[tim] = i; for (auto ve : adj[i]) if (p - ve) dfs(ve, i); en[i] = (++tim); ver[tim] = i; } int lca(int u, int v) { if (lev[u] < lev[v]) swap(u, v); for (int j = LN - 1; j >= 0; j--) if (par[u][j] + 1 && lev[par[u][j]] >= lev[v]) u = par[u][j]; if (u == v) return u; for (int j = LN - 1; j >= 0; j--) { if (par[u][j] + 1 && par[u][j] != par[v][j]) { u = par[u][j]; v = par[v][j]; } } return par[u][0]; } inline long long add(long long a, long long b) { a += b; if (a >= mod) a -= mod; return a; } void upd(int ind, int val) { for (int i = ind; i <= 2 * n; i += (i & -i)) bit[i] += val; } long long query(int ind) { long long res = 0; for (int i = ind; i > 0; i -= (i & -i)) res += bit[i]; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1, i, j, m, q, u, v, w, typ, e, k, x; memset(par, -1, sizeof(par)); cin >> n >> q; for (i = 1; i <= n - 1; i++) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, -1); int root; while (q--) { cin >> k >> x >> root; for (i = 1; i <= k; i++) { cin >> A[i]; mark[A[i]] = 1; upd(st[A[i]], 1); upd(en[A[i]] + 1, -1); } int ans_root = query(st[root]); for (i = 1; i <= k; i++) { int lp = lca(A[i], root); f[i] = query(st[A[i]]) + ans_root - 2 * query(st[lp]) + (mark[lp] == 1) - 1; } sort(f + 1, f + k + 1); for (i = 1; i <= k; i++) { upd(st[A[i]], -1); upd(en[A[i]] + 1, 1); mark[A[i]] = 0; } for (i = 0; i <= x; i++) dp[i] = 0; dp[0] = 1; int fl = 0; for (i = 1; i <= k; i++) if (f[i] >= x) fl = 1; if (fl) cout << "0\n"; else { dp[0] = 1; for (i = 1; i <= k; i++) { for (int j = min(i, x); j >= 0; j--) { if (j <= f[i]) dp[j] = 0; else dp[j] = add(((dp[j] * 1ll * (j - f[i])) % mod), dp[j - 1]); } } long long ans = 0; for (i = 1; i <= x; i++) { ans = add(ans, dp[i]); } cout << ans << "\n"; } } 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 n; cin >> n; if (n.length() >= 3) for (long long int i = 0; i < n.length() - 2; i++) { if ((n[i] != '.') && (n[i + 1] != '.') && (n[i + 2] != '.') && (n[i] != n[i + 1] && n[i + 1] != n[i + 2] && n[i + 2] != n[i])) { cout << "Yes"; return 0; } } cout << "No"; return 0; }
1
#include <iostream> using namespace std; int main() { int s,i,j,x,y,c[15][15],d[17][15]; while(cin >> x >> y) { if (x+y==0) break; for (i=0;i<y;i++) for (j=0;j<x;j++) cin >> c[i][j]; for (i=0;i<y+2;i++) for (j=0;j<x;j++) d[i][j]=(i==0); for (i=0;i<y-1;i++) for (j=0;j<x;j++) { s=d[i][j]; if (c[i][j]==0) { if (j>0) if (c[i+1][j-1]==0) d[i+1][j-1]+=s; if (j<x-1) if (c[i+1][j+1]==0) d[i+1][j+1]+=s; if (c[i+1][j]!=1) d[i+1][j]+=s; } if (c[i][j]==2 && c[i+2][j]!=1) d[i+2][j]+=s; } s=0; for (i=y-1;i<=y;i++) for (j=0;j<x;j++) s+=d[i][j]; if (y==1) { s=0; for (i=0;i<x;i++) s+=(c[0][i]==0);} cout << s << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int ara[2][2]; ara[0][0] = ara[0][1] = ara[1][0] = ara[1][1] = 0; int n, i, x, y, z; cin >> n; for (i = 0; i < n; i++) { cin >> x >> y >> z; ara[x - 1][0] += y; ara[x - 1][1] += z; } for (i = 0; i < 2; i++) { if (ara[i][0] >= ara[i][1]) cout << "LIVE" << endl; else cout << "DEAD" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; const int inf = 2e9 + 1; int n, w, a[N], b[N]; struct node { int val, id; }; inline bool operator<(node x, node y) { return x.val > y.val; } priority_queue<node> q1, q2, q3, q4, q5; int vis[N]; int main() { scanf("%d%d", &n, &w); for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i], &b[i]); for (int i = 1; i <= n; ++i) q1.push({a[i], i}); for (int i = 1; i <= n; ++i) q3.push({b[i], i}); long long ans = 0; for (int i = 1; i <= w; ++i) { int res = inf, id = 0; while (!q1.empty() && vis[q1.top().id]) q1.pop(); while (!q2.empty() && vis[q2.top().id] != 1) q2.pop(); while (!q3.empty() && vis[q3.top().id]) q3.pop(); while (!q4.empty() && vis[q4.top().id] != 1) q4.pop(); while (!q5.empty() && vis[q5.top().id] != 2) q5.pop(); if (!q1.empty()) { res = q1.top().val; id = 1; } if (!q2.empty() && q2.top().val < res) { res = q2.top().val; id = 2; } if (!q3.empty() && !q4.empty() && q3.top().val + q4.top().val < res) { res = q3.top().val + q4.top().val; id = 3; } if (!q3.empty() && !q5.empty() && q3.top().val + q5.top().val < res) { res = q3.top().val + q5.top().val; id = 4; } ans += res; if (id == 1) { int idx = q1.top().id; vis[idx] = 1; q1.pop(); q2.push({b[idx] - a[idx], idx}); q4.push({-a[idx], idx}); } else if (id == 2) { int idx = q2.top().id; vis[idx] = 2; q2.pop(); q5.push({a[idx] - b[idx], idx}); } else if (id == 3) { int idx = q3.top().id, idy = q4.top().id; vis[idx] = 2; vis[idy] = 0; q3.pop(); q4.pop(); q5.push({a[idx] - b[idx], idx}); q1.push({a[idy], idy}); q4.push({b[idy], idy}); } else { int idx = q3.top().id, idy = q5.top().id; vis[idx] = 2; vis[idy] = 1; q3.pop(); q5.pop(); q5.push({a[idx] - b[idx], idx}); q2.push({b[idy] - a[idy], idy}); q4.push({-a[idy], idy}); } } printf("%lld\n", ans); for (int i = 1; i <= n; ++i) printf("%d", vis[i]); return 0; }
5
#include <bits/stdc++.h> int n; int a[10100]; int b[10100]; int ans[1001000][2]; int ansl; int c[10100]; int d[500100][2]; int dl; void gelim() { int i, j, p; p = 0; for (i = 0; i < n; i++) { for (j = 0; j < p; j++) { if ((c[i] ^ c[j]) < c[i]) { c[i] ^= c[j]; d[dl][0] = i; d[dl][1] = j; dl++; } } if (c[i] != 0) { if (i == p) p++; else { c[p] ^= c[i]; d[dl][0] = p; d[dl][1] = i; dl++; c[i] ^= c[p]; d[dl][0] = i; d[dl][1] = p; dl++; p++; } j = p - 1; while (j != 0 && c[j - 1] < c[j]) { c[j] ^= c[j - 1]; d[dl][0] = j; d[dl][1] = j - 1; dl++; c[j - 1] ^= c[j]; d[dl][0] = j - 1; d[dl][1] = j; dl++; c[j] ^= c[j - 1]; d[dl][0] = j; d[dl][1] = j - 1; dl++; j--; } } } } int main() { int i, j; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < n; i++) { scanf("%d", &b[i]); } for (i = 0; i < n; i++) { c[i] = a[i]; } gelim(); for (i = 0; i < dl; i++) { ans[ansl][0] = d[i][0]; ans[ansl][1] = d[i][1]; ansl++; } dl = 0; for (i = 0; i < n; i++) { a[i] = c[i]; } for (i = 0; i < n; i++) { c[i] = b[i]; } gelim(); for (i = 0; i < n && a[i] != 0; i++) { for (j = i; j < n && a[j] != 0; j++) { if ((a[i] ^ c[i]) > ((a[i] ^ c[i]) ^ a[j])) { a[i] ^= a[j]; ans[ansl][0] = i; ans[ansl][1] = j; ansl++; } } if (a[i] != c[i]) { printf("-1"); return 0; } } if (c[i + 1] != 0) { printf("-1"); return 0; } for (i = dl - 1; i >= 0; i--) { ans[ansl][0] = d[i][0]; ans[ansl][1] = d[i][1]; ansl++; } printf("%d\n", ansl); for (i = 0; i < ansl; i++) { printf("%d %d\n", ans[i][0] + 1, ans[i][1] + 1); } }
6
#include <bits/stdc++.h> using namespace std; const long long MX = (1 << 17), MXL = 17; long long n, nxt[MXL][MX]; bool cw(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return 1ll * (b.first - a.first) * (c.second - b.second) < 1ll * (b.second - a.second) * (c.first - b.first); } int depth[MX]; pair<long long, long long> arr[MX]; int main() { scanf("%lld", &n); for (long long j = 1; j <= n; j++) scanf("%lld %lld", &arr[j].first, &arr[j].second); long long who = 0; nxt[0][n] = n + 1; depth[n] = 1; vector<int> v; v.push_back(n); int sz = 1; for (long long j = n - 1; j; j--) { while (sz >= 2 && cw(arr[v[sz - 2]], arr[v[sz - 1]], arr[j])) { v.pop_back(); --sz; } depth[j] = depth[v.back()] + 1; nxt[0][j] = v.back(); v.push_back(j); ++sz; } for (long long j = 1; j < MXL; j++) for (long long i = n; i; i--) nxt[j][i] = nxt[j - 1][nxt[j - 1][i]]; long long QN; scanf("%lld", &QN); while (QN--) { long long src, snk; scanf("%lld %lld", &src, &snk); if (depth[src] < depth[snk]) swap(src, snk); int d = depth[src] - depth[snk]; for (int j = MXL - 1; j >= 0; j--) if ((d & (1 << j))) { src = nxt[j][src]; } if (src == snk) goto pp; for (int j = MXL - 1; j >= 0; j--) if (nxt[j][src] != nxt[j][snk]) src = nxt[j][src], snk = nxt[j][snk]; src = nxt[0][src]; pp:; printf("%lld\n", src); } }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 7; const long long INF = 1e18 + 7; int n; int a[maxn]; long long dp[maxn][20]; long long k[20]; long long C[20]; int main() { scanf("%d", &n); int res = n; int num = 0; while (res != 1) num++, res >>= 1; for (int i = 1; i <= num; i++) { k[i] = (1 << (i - 1)) - 1; } for (int i = 1; i <= num; i++) { C[i] = C[i - 1] + k[num + 1 - i]; } for (int j = 0; j <= n; j++) for (int i = 0; i <= num; i++) dp[j][i] = INF; dp[n][0] = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int flag = 0; for (long long i = n - 1; i >= 0; i--) { if (a[i] == -1) { flag = i; break; } for (long long j = 0; j <= num; j++) { if (j != 0) dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + a[i]); if (n - 1 - i + 1 - j > C[j]) continue; dp[i][j] = min(dp[i + 1][j], dp[i][j]); } } long long ans = INF; for (int i = num; i >= max(0, num - flag); i--) { ans = min(ans, dp[flag + 1][i]); } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int m, n, k, sum, f[1005], g[505][505], dp[505][505]; char x; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= n; i++) { sum = 0; for (int j = 1; j <= m; j++) { cin >> x; if (x == '1') { sum++; f[sum] = j; } } for (int j = 0; j <= m; j++) g[i][j] = 0x3f3f3f; for (int k = 1; k <= sum; k++) { for (int j = 1; j <= sum - k + 1; j++) g[i][sum - k] = min(g[i][sum - k], f[j + k - 1] - f[j] + 1); } g[i][sum] = 0; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { dp[i][j] = 0x3f3f3f; for (int l = 0; l <= j; l++) { dp[i][j] = min(dp[i][j], dp[i - 1][j - l] + g[i][l]); } } } cout << dp[n][k] << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { std::cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const int fx[] = {+1, -1, +0, +0}; const int fy[] = {+0, +0, +1, -1}; long long int ans = 0; long long int n; signed main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; if (n == 0) { cout << 1 << '\n'; return 0; } cout << (long long int)4 * (long long int)floor(n * sqrt(2)); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t, n, i = 0; cin >> t; while (t--) { cin >> n; for (i = 0; i < n; i++) { cout << "1" << " "; } cout << endl; } }
1
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } using ll = long long; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> P; for (int i = 0; i < n; i++) { int p; cin >> p; P.push_back(p); } reverse((P).begin(), (P).end()); vector<int> golds; while (!P.empty() && (golds.empty() || golds.back() == P.back())) { golds.push_back(P.back()); P.pop_back(); } vector<int> silvers; while (!P.empty() && (int((silvers).size()) <= int((golds).size()) || silvers.back() == P.back())) { silvers.push_back(P.back()); P.pop_back(); } vector<int> bronzes; while (!P.empty() && (int((bronzes).size()) <= int((golds).size()) || bronzes.back() == P.back())) { bronzes.push_back(P.back()); P.pop_back(); } if (int((golds).size()) == 0 || int((silvers).size()) == 0 || int((bronzes).size()) == 0 || int((golds).size()) + int((silvers).size()) + int((bronzes).size()) > int((P).size())) { cout << "0 0 0\n"; continue; } vector<int> ans = {int((golds).size()), int((silvers).size()), int((bronzes).size())}; while (!P.empty()) { bronzes.push_back(P.back()); P.pop_back(); while (!P.empty() && P.back() == bronzes.back()) { bronzes.push_back(P.back()); P.pop_back(); } if (int((golds).size()) + int((silvers).size()) + int((bronzes).size()) <= int((P).size())) ans = {int((golds).size()), int((silvers).size()), int((bronzes).size())}; else break; } for (auto a : ans) cout << a << " "; cout << "\n"; } }
1
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "{" << p.first << "," << p.second << "}"; return os; } const int N = 3e5 + 5; const int oo = 1e9 + 7; int n; int a[N]; int id[N]; pair<int, int> upto[N]; vector<int> aa, bb; int d[3]; bool check(int i) { if (i > n) return aa.size() and bb.size(); if (i == n) { if (d[2] == oo or bb.back() + d[2] == a[n]) { bb.push_back(a[n]); return 1; } else if (d[1] + aa.back() == a[n]) { aa.push_back(a[n]); return bb.size(); } else return 0; } if (upto[i].first and (upto[i].second == d[2] or d[2] == oo) and (bb.empty() or a[i] - bb.back() == upto[i].second)) { for (; i <= n; i++) bb.push_back(a[i]); return 1; } if (a[i] == aa.back() + d[1]) { aa.push_back(a[i]); return check(i + 1); } else { if (d[2] == oo or (d[2] == a[i] - bb.back())) { if (bb.size() == 1) d[2] = a[i] - (bb.empty() ? 0 : bb.back()); bb.push_back(a[i]); return check(i + 1); } else return 0; } return 0; } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } upto[n] = {1, oo}; upto[n - 1] = {1, a[n] - a[n - 1]}; for (int i = n - 2; i >= 1; i--) { upto[i] = {upto[i + 1].first and a[i + 1] - a[i] == upto[i + 1].second, upto[i + 1].second}; } if (n == 2) { cout << a[1] << '\n' << a[2] << '\n'; return 0; } for (int i = 1; i <= 3; i++) { for (int j = i + 1; j <= 3; j++) { aa.clear(); bb.clear(); int x = j + 1; if (j == 3) bb.push_back(a[(i == 1) + 1]); aa.push_back(a[i]); aa.push_back(a[j]); d[1] = a[j] - a[i]; d[2] = oo; if (check(x)) { for (int i : aa) cout << i << " "; cout << "\n"; for (int i : bb) cout << i << " "; cout << "\n"; return 0; } } } cout << "No solution\n"; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) { int v = s[i] - '0'; if (v == 9 && i == 0) cout << 9; else { if (9 - v < v) cout << 9 - v; else cout << v; } } return 0; }
1
#include <bits/stdc++.h> int x[100005]; int main() { long long n, a, b, w, z; scanf("%I64d%I64d%I64d", &n, &a, &b); int i; for (i = 0; i < n; i++) { scanf("%I64d", &x[i]); w = x[i] * a / b; if (w * b % a == 0) z = w * b / a; else z = w * b / a + 1; printf("%I64d ", x[i] - z); } printf("\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; long long powmod(long long a, long long b, long long mod) { if (b == 0 || a == 1) { if (mod == 1) return 0; else return 1; } if (b % 2 == 0) { long long k = powmod(a, b / 2, mod); return (k * k) % mod; } else { long long k = powmod(a, b / 2, mod); return ((k * k) % mod * a) % mod; } } long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; if (a > b) return gcd(a % b, b); else return gcd(b % a, a); } int prime(int p) { for (int i = 2; i * i <= p; i++) { if (p % i == 0 && i < p) return i; } return 1; } long long sqr(long long i) { return i * i; } void r(long long &a) { cin >> a; } void r(long double &a) { cin >> a; } void r(long long &a, long long &b) { cin >> a >> b; } void r(long double &a, long double &b) { cin >> a >> b; } void r(long long &a, long long &b, long long &c) { cin >> a >> b >> c; } void r(long double &a, long double &b, long double &c) { cin >> a >> b >> c; } void r(long long &a, long long &b, long long &c, long long &d) { cin >> a >> b >> c >> d; } void r(long double &a, long double &b, long double &c, long double &d) { cin >> a >> b >> c >> d; } void r(long long &a, long long &b, long long &c, long long &d, long long &e) { cin >> a >> b >> c >> d >> e; } void r(long double &a, long double &b, long double &c, long double &d, long double &e) { cin >> a >> b >> c >> d >> e; } void r(long long &a, long long &b, long long &c, long long &d, long long &e, long long &f) { cin >> a >> b >> c >> d >> e >> f; } void r(long double &a, long double &b, long double &c, long double &d, long double &e, long double &f) { cin >> a >> b >> c >> d >> e >> f; } void r(vector<long long> &a) { for (long long i = 0; i < a.size(); i++) r(a[i]); } void r(vector<vector<long long>> &a) { for (long long i = 0; i < a.size(); i++) r(a[i]); } void w(long long a) { cout << a << "\n"; } void w(long double a) { cout << a << "\n"; } void w(char a) { cout << a; } void w(long long a, long long b) { cout << a << " " << b << "\n"; } void w(long double a, long double b) { cout << a << " " << b << "\n"; } void w(long long a, long long b, long long c) { cout << a << " " << b << " " << c << "\n"; } void w(long double a, long double b, long double c) { cout << a << " " << b << " " << c << "\n"; } void w(long long a, long long b, long long c, long long d) { cout << a << " " << b << " " << c << " " << d << "\n"; } void w(long double a, long double b, long double c, long double d) { cout << a << " " << b << " " << c << " " << d << "\n"; } void w(vector<long long> a) { for (long long i = 0; i < a.size(); i++) cout << a[i] << " "; cout << "\n"; } void w(vector<vector<long long>> a) { for (long long i = 0; i < a.size(); i++) w(a[i]); cout << "\n"; } void r(pair<long long, long long> &a) { cin >> a.first >> a.second; } void w(pair<long long, long long> a) { cout << a.first << " " << a.second << "\n"; } void r(vector<pair<long long, long long>> &a) { for (long long i = 0; i < a.size(); i++) r(a[i]); } void w(vector<pair<long long, long long>> a) { for (long long i = 0; i < a.size(); i++) w(a[i]); cout << "\n"; } void r(string &a) { cin >> a; } void r(char &a) { cin >> a; } void w(string a) { cout << a << "\n"; } void sort(vector<long long> &a) { sort(a.begin(), a.end()); } void sort(vector<pair<long long, long long>> &a) { sort(a.begin(), a.end()); } void rev(vector<long long> &a) { reverse(a.begin(), a.end()); } void rev(vector<pair<long long, long long>> &a) { reverse(a.begin(), a.end()); } void rev(string &a) { reverse(a.begin(), a.end()); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } void solve(int ppppppppp = 1) { long long a; r(a); vector<long long> m(a); r(m); sort(m); long long check = 1; for (long long i = 0; i < a; i++) if (m[i] % m[0] != 0) check = 0; if (check == 1) w(m[0]); else w(-1LL); return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tututu; tututu = 1; for (long long qwerty = 0; qwerty < tututu; qwerty++) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8; namespace std { bool operator<(const complex<double>& a, const complex<double>& b) { return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } } // namespace std int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int win(char a, char b) { if (a == b) return 2; if (a == 'R') { if (b == 'S') return 0; return 1; } if (a == 'S') { if (b == 'R') return 1; return 0; } if (b == 'S') { return 1; } return 0; } int main() { int n; cin >> n; string a, b; cin >> a >> b; int m = a.length(); int k = b.length(); int l = m * k / gcd(m, k); int ansa = 0; int ansb = 0; for (int i = (0); i < (int)(l); i++) { char la = a[i % m]; char lb = b[i % k]; ansa += (win(la, lb) == 0) * (n / l); ansb += (win(la, lb) == 1) * (n / l); } for (int i = (0); i < (int)(n % l); i++) { char la = a[i % m]; char lb = b[i % k]; ansa += (win(la, lb) == 0); ansb += (win(la, lb) == 1); } cout << ansb << " " << ansa << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; pair<long long, pair<long long, int> > p[n]; for (int i = 0; i < n; i++) cin >> p[i].first; for (int i = 0; i < n; i++) { cin >> p[i].second.first; p[i].second.second = i; } sort(p, p + n); multiset<long long> s; long long ans[n], x = 0; for (int i = 0; i < n; i++) { int t = p[i].second.second; long long c = p[i].second.first; ans[t] = c + x; if ((int)s.size() < k) { s.insert(c); x += c; } else { if (s.size() != 0 && c > *s.begin()) { x -= *s.begin(); s.erase(s.begin()); s.insert(c); x += c; } } } for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, cnt[101], s[100], cnt1, cnt2, cnt3, t; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &s[i]); cnt[s[i]]++; } for (int i = 1; i <= 100; i++) { if (cnt[i] == 1) cnt1++; else if (cnt[i] == 2) cnt2++; else if (cnt[i] >= 3) cnt3++; } if (cnt1 == 0) { printf("YES\n"); for (int i = 0; i < n; i++) printf("A"); } else if (cnt1 % 2 == 0) { printf("YES\n"); t = cnt1 / 2; for (int i = 0; i < n; i++) { if (cnt[s[i]] == 1 && t) { cnt[s[i]]--; printf("A"); t--; } else { printf("B"); } } } else if (cnt1 % 2 == 1) { if (cnt3 >= 1) { printf("YES\n"); t = cnt1 / 2; bool fin = false; for (int i = 0; i < n; i++) { if (cnt[s[i]] == 1 && t) { cnt[s[i]]--; printf("A"); t--; } else if (cnt[s[i]] >= 3 && !fin) { fin = true; printf("A"); } else { printf("B"); } } } else { printf("NO\n"); } } }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); map<long long, int> mp; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { long long ans, ans1 = 0; long long d1 = i, d2 = n / i; for (long long j = 1; j <= n; j += d2) ans1 += j; ans = (d2 * (d2 + 1) / 2) * i + d2 - n; mp[ans]++; mp[ans1]++; } } for (auto x : mp) printf("%lld ", x.first); return 0; }
3
#include<cstdio> #include<vector> #define N 1005 #define M 200005 using namespace std; int read(){ char c=getchar(); while(c<'0'||c>'9'){ continue; } int num=c^48; c=getchar(); while(c>='0'&&c<='9'){ num=num*10+(c^48); c=getchar(); } return num; } vector<int> edge[N]; int p1[N][N]; void dfs1(int now,int p){ for(vector<int>::iterator it=edge[now].begin();it!=edge[now].end();++it){ int v=*it; if(p1[p][v]){ continue; } p1[p][v]=p1[p][now]; dfs1(v,p); } } int p2[N][N]; void dfs2(int now,int p){ for(vector<int>::iterator it=edge[now].begin();it!=edge[now].end();++it){ int v=*it; if(p2[p][v]){ continue; } p2[p][v]=p2[p][now]; dfs2(v,p); } } int u[M],v[M]; int main(){ int n,m; n=read(); m=read(); for(int i=0;i<m;++i){ u[i]=read(); v[i]=read(); edge[u[i]].push_back(v[i]); } for(int i=1;i<=n;++i){ p1[i][i]=i; for(vector<int>::iterator it=edge[i].begin();it!=edge[i].end();++it){ int v=*it; if(p1[i][v]){ continue; } p1[i][v]=v; dfs1(v,i); } p2[i][i]=i; if(edge[i].begin()==edge[i].end()){ continue; } for(vector<int>::iterator it=--edge[i].end();;--it){ int v=*it; if(p2[i][v]){ if(it==edge[i].begin()){ break; } continue; } p2[i][v]=v; dfs2(v,i); if(it==edge[i].begin()){ break; } } } for(int i=0;i<m;++i){ int flag=0; if(p1[v[i]][u[i]]){ flag^=1; } if(p1[u[i]][v[i]]!=p2[u[i]][v[i]]){ flag^=1; } if(flag){ printf("diff\n"); }else{ printf("same\n"); } } }
0
#include <bits/stdc++.h> using namespace std; int main() { long long v; cin >> v; if (v == 2) cout << "2" << endl; else cout << "1" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T, typename TT> ostream &operator<<(ostream &s, pair<T, TT> t) { return s << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream &operator<<(ostream &s, vector<T> t) { s << "{"; for (int i = 0; i < t.size(); i++) s << t[i] << (i == t.size() - 1 ? "" : ","); return s << "}" << endl; } int main() { ios_base::sync_with_stdio(0); long long t1, t2, t0, x1, x2; cin >> t1 >> t2 >> x1 >> x2 >> t0; t1 -= t0; t2 -= t0; if (t1 == 0 && t2 == 0) { cout << x1 << " " << x2; return 0; } else if (t1 == 0 && t2 > 0) { cout << x1 << " " << 0; return 0; } else if (t1 < 0 && t2 > 0) { t1 = abs(t1); long long b = 10000000000000LL, b1 = 0, b2 = 1; for (int i = 1; i <= x2; i++) { long long g = i * t2; long long n1 = g / t1; if (n1 > x1) n1 = x1; long long n2 = i; if ((n1 + n2) * (-b1 * t1 + b2 * t2) >= (b1 + b2) * (-n1 * t1 + n2 * t2)) { if ((n1 + n2) * (-b1 * t1 + b2 * t2) > (b1 + b2) * (-n1 * t1 + n2 * t2)) { b1 = n1; b2 = n2; } else if (b1 + b2 < n1 + n2) { b1 = n1; b2 = n2; } } } cout << b1 << " " << b2; return 0; } else { cout << 0 << " " << x2; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; bool chance = true; while (n > 0 && m > 0) { n--, m--; chance = !chance; } if (chance) cout << "Malvika"; else cout << "Akshat"; }
1
#include<bits/stdc++.h> using namespace std; multiset<int> mp; int main() { int n;scanf("%d",&n); while (n--) {int w;scanf("%d",&w);mp.insert(w);} scanf("%d",&n); while (n--) { int w;scanf("%d",&w); if (mp.find(w)==mp.end()) {puts("NO");return 0;} mp.erase(mp.find(w)); } puts("YES"); }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, p, i, c; cin >> a >> b >> p; string s; cin >> s; for (i = s.length() - 2, c = 0; i >= 0; --i) { if (c != s[i]) { c = s[i]; p -= (c == 'A' ? a : b); if (p < 0) break; } } cout << i + 2 << endl; } return 0; }
2
#include <iostream> #include <cstdio> #include <cstring> #define N 200005 using namespace std; typedef long long ll; const int mod=1e9+7; struct edge{ int k,next; }e[N<<2]; int n,home[N],cnt=-1,a[N],tot,sum=0,s[N],top,to[N],H[N]; ll fac[N],inv[N]; ll quick_pow(ll a,ll b){ ll ans=1; for(;b;b>>=1,a=a*a%mod) if(b&1) ans=ans*a%mod; return ans; } ll C(int n,int m){ if(n<m) return 0; return fac[n]*inv[m]%mod*inv[n-m]%mod; } bool vis[N]; ll ans=1; void add(int *home,int x,int y){ cnt++; e[cnt].k=y; e[cnt].next=home[x]; home[x]=cnt; } void dfs(int k){ vis[k]=1;a[++tot]=k; for(int i=home[k];~i;i=e[i].next,sum++) if(!vis[e[i].k]) dfs(e[i].k); } int used[N],cir[N],cc; int fa[N]; void dfs(int k,int F){ used[k]=++cc;fa[k]=F; for(int i=home[k];~i;i=e[i].next) if(i!=F){ if(used[e[i].k]){ if(used[e[i].k]>used[k]) continue; int x=k; cir[e[i].k]=1; while(x!=e[i].k){ s[++top]=x; cir[x]=1; x=e[fa[x]].k; } s[++top]=e[i].k; continue; } else dfs(e[i].k,i^1); } } void dfs1(int k,int F){ used[k]=1; if(!cir[k]) to[k]=e[F].k; for(int i=home[k];~i;i=e[i].next) if(i!=F&&!used[e[i].k]){ dfs1(e[i].k,i^1); } } int vec,now,siz[N],IN[N]; ll f[N]; void dp(int k){ f[k]=1;now++;siz[k]=0; for(int i=H[k];~i;i=e[i].next){ dp(e[i].k); siz[k]+=siz[e[i].k]; f[k]=f[k]*C(siz[k],siz[e[i].k])%mod*f[e[i].k]%mod; } siz[k]++; } void solve(){ top=cc=0; dfs(a[1],-1); for(int i=1;i<=tot;i++) used[a[i]]=0; dfs1(s[1],-1); for(int i=1;i<=tot;i++) used[a[i]]=0; ll ret=0; int vv=vec,CNT=cnt; for(int w=-1;w<=1;w+=2){ cnt=CNT; for(int i=1;i<=tot;i++) H[a[i]]=-1,IN[a[i]]=0; for(int i=1;i<=top;i++){ int x=i+w; if(x==top+1) x=1; if(x==0) x=top; to[s[i]]=s[x]; } for(int i=1;i<=tot;i++){ for(int p=home[a[i]];~p;p=e[p].next)if(e[p].k<to[a[i]]){ add(H,a[i],e[p].k); IN[e[p].k]++; } } ll o=1;vec=vv; for(int i=1;i<=tot;i++) if(!IN[a[i]]){ now=0; dp(a[i]); o=o*C(vec,now)%mod*f[a[i]]%mod; vec-=now; } ret=(ret+o)%mod; } ans=ans*ret%mod; } int main(){ memset(home,-1,sizeof(home)); memset(H,-1,sizeof(H)); scanf("%d",&n); vec=n+n; fac[0]=inv[0]=1; for(int i=1;i<=200002;i++) fac[i]=fac[i-1]*i%mod,inv[i]=quick_pow(fac[i],mod-2); for(int i=1,x,y;i<=n+n;i++){ scanf("%d%d",&x,&y); add(home,x,y+n);add(home,y+n,x); } for(int i=1;i<=n;i++)if(!vis[i]){ tot=sum=0; dfs(i); if(sum!=tot+tot) return puts("0"),0; solve(); } printf("%lld",ans); return 0; }
0
#include <bits/stdc++.h> long long n, cnt; char c; int a[200]; int main() { scanf("%I64d%c", &n, &c); a['a'] = 4, a['b'] = 5, a['c'] = 6, a['d'] = 3, a['e'] = 2, a['f'] = 1; if (((n + 1) >> 1) & 1) cnt = n - 1; else cnt = n - 3; n = ((n - 1) >> 2 << 1) + (n & 1 ? 0 : 1); printf("%I64d", n * 6 + cnt + a[c]); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const double EPS = 1e-9; const long long MOD = (long long)(1e9 + 7); const long long MAXV = (long long)(2e5 + 10); const long long MAXE = (long long)(1e6 + 10); int V; int G[MAXV]; int H[MAXV]; int c; bool mark[MAXV]; int dfs(int u) { int v, w = 0; v = G[u]; if (!mark[v]) { mark[v] = true; w = dfs(v); } return H[u] + w; } int main() { scanf("%d", &V); for (int i = 1; i <= V; i++) { scanf("%d", &G[i]); } for (int i = 1; i <= V; i++) { scanf("%d", &H[i]); c += H[i]; } int aux; int sol = 0; int cc = 0; for (int i = 1; i <= V; i++) { if (!mark[i]) { cc++; mark[i] = true; aux = dfs(i); } } if (cc == 1) sol = 0; else sol = cc; if (c % 2 == 0) sol++; printf("%d\n", sol); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int INF = (1 << 29); vector<pair<int, int> > v; pair<int, int> rasp = {INF, -1}; int n, h, m, k, x, y; int main() { cin >> n >> h >> m >> k; for (int i = 1; i <= n; i++) { cin >> x >> y; v.push_back({y % (m / 2), i}); v.push_back({y % (m / 2) + (m / 2), i}); } sort(v.begin(), v.end()); int ind = 0; for (int i = 0; i < 2 * n; i++) { while (v[i].first - v[ind].first >= k) ind++; if (i >= n) rasp = min(rasp, {i - ind, v[i].first}); } cout << rasp.first << ' ' << rasp.second % (m / 2) << '\n'; int st = rasp.second - k, dr = rasp.second; for (int i = 0; i < 2 * n; i++) { if (st < v[i].first and v[i].first < dr) cout << v[i].second << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, a, b, c, d; long long ans; int main() { cin >> n >> a >> b >> c >> d; for (int x = 1; x <= n; ++x) { int y = x + b - c; int z = x + a - d; int w = a + y - d; if (1 <= y && y <= n && 1 <= z && z <= n && 1 <= w && w <= n) { ++ans; } } ans *= n; cout << ans << endl; }
2
#include <bits/stdc++.h> long long int const MOD = 1000000007; long long int const N = 1000005; long long int const LN = 20; long long int const inf = 8e18; using namespace std; long long int n, m, a[N]; long long int const MX = 10004; bitset<MX> ans; vector<long long int> v[N]; void build(long long int i, long long int s1, long long int e1, long long int l, long long int r, long long int val) { if (s1 > r || l > e1) return; if (l <= s1 && e1 <= r) { v[i].push_back(val); return; } long long int mid = (s1 + e1) / 2; build(2 * i, s1, mid, l, r, val); build(2 * i + 1, mid + 1, e1, l, r, val); } void dfs(long long int i, long long int s1, long long int e1, bitset<MX> ok) { bitset<MX> go = ok; for (auto w : v[i]) go |= (go << w); if (s1 == e1) { ans |= go; return; } long long int mid = (s1 + e1) / 2; dfs(2 * i, s1, mid, go); dfs(2 * i + 1, mid + 1, e1, go); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long int n, q; cin >> n >> q; for (long long int i = 1; i <= q; i++) { long long int l, r, x; cin >> l >> r >> x; build(1, 1, n, l, r, x); } bitset<MX> ok; ok[0] = 1; dfs(1, 1, n, ok); long long int p = 0; for (long long int i = 1; i <= n; i++) { if (ans[i]) p++; } cout << p << '\n'; for (long long int i = 1; i <= n; i++) { if (ans[i]) cout << i << " "; } return 0; }
5
#include <bits/stdc++.h> #pragma GCC optimize("-O3") using namespace std; const int N = 1e5 + 1; string s; int c[N]; vector<int> a; int n; void seive() { int cnt = 1; for (int i = 2; i <= n; i++) { if (!c[i]) { for (int j = i; j <= n; j += i) { if (!c[j]) c[j] = cnt; } cnt++; } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; seive(); for (int i = 2; i <= n; i++) cout << c[i] << ' '; cout << '\n'; }
3
#include <bits/stdc++.h> int lowbit(int x) { return x & -x; } struct artree { int data[200001], len; void ins(int i, int a) { for (; i <= len; i += lowbit(i)) data[i] += a; } int query(int i) { if (i < 0) return 0; int ans = 0; for (; i; i -= lowbit(i)) ans += data[i]; return ans; } } stat; struct fe { int b; fe *next; } tree[1000001]; int elen; void ins(int a, int b) { tree[elen] = (fe){b, tree[a].next}; tree[a].next = tree + elen++; } int deg[200001], bl[200001], lib[200001], dep[200001]; int ea[200001], eb[200001]; int main() { int n, m, i, j, k, last, r = 1, llen = 0; scanf("%d", &n); elen = n + 1; for (i = 1; i <= n - 1; ++i) { int a, b; scanf("%d%d", &a, &b); ea[i] = a, eb[i] = b; ins(a, b); ins(b, a); deg[a]++; deg[b]++; } for (i = 1; i <= n; ++i) if (deg[i] > 2) { r = i; break; } for (fe *j = tree[r].next; j; j = j->next) for (last = r, k = j->b;;) { dep[k] = dep[last] + 1; bl[k] = j->b; lib[k] = ++llen; if (deg[k] == 1) break; int k2 = k; if (tree[k].next->b == last) k = tree[k].next->next->b; else k = tree[k].next->b; last = k2; } for (i = 1; i <= n - 1; ++i) if (dep[eb[i]] > dep[ea[i]]) ea[i] = eb[i]; stat.len = n; scanf("%d", &m); for (i = 1; i <= m; ++i) { int opt, a, b; scanf("%d", &opt); if (opt == 1) { scanf("%d", &a); stat.ins(lib[ea[a]], -1); } if (opt == 2) { scanf("%d", &a); stat.ins(lib[ea[a]], 1); } if (opt == 3) { scanf("%d%d", &a, &b); if (dep[a] > dep[b]) a ^= b, b ^= a, a ^= b; if (bl[a] == bl[b]) { if (stat.query(lib[b]) - stat.query(lib[a])) printf("-1\n"); else printf("%d\n", dep[b] - dep[a]); } else { if (stat.query(lib[b]) - stat.query(lib[bl[b]] - 1) + stat.query(lib[a]) - stat.query(lib[bl[a]] - 1)) printf("-1\n"); else printf("%d\n", dep[a] + dep[b]); } } } return 0; }
4
#include <bits/stdc++.h> const int _ = 1e4 + 5; const long long INF = 1ll << 60; inline long long min(long long a, long long b) { return a < b ? a : b; } int n, c, s[_], t[_]; long long dp[2][_]; int main() { scanf("%d %d", &n, &c); for (int i = 1; i <= n; ++i) scanf("%d", s + i); for (int i = 1; i <= n; ++i) scanf("%d", t + i); for (int i = 1; i <= n; ++i) { memset(dp[i & 1], 0x3f, sizeof dp[i & 1]); dp[i & 1][0] = dp[(i - 1) & 1][0] + s[i]; for (int j = 1; j <= i; ++j) dp[i & 1][j] = min(dp[(i - 1) & 1][j] + 1ll * c * j + s[i], dp[(i - 1) & 1][j - 1] + t[i]); } long long ans = INF; for (int i = 0; i <= n; ++i) ans = min(ans, dp[n & 1][i]); printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1000005; const int X_NOTB = 0; const int X_BEG = 1; const int X_END = 2; const long long INF = 1000000000000000000LL; int mas[MAX_N]; long long dp[MAX_N][3]; set<int> st; long long n, a, b; void razlozh(int x) { int p = 2; while (p * p <= x) { if (x % p == 0) st.insert(p); while (x % p == 0) x /= p; p++; } if (x > 1) st.insert(x); return; } long long get_ans(int p) { for (int i = 0; i < MAX_N; i++) for (int j = 0; j < 3; j++) dp[i][j] = INF; dp[0][X_NOTB] = dp[0][X_END] = dp[0][X_BEG] = 0; for (int i = 0; i < n; i++) { if (mas[i] % p == 0) { dp[i + 1][X_NOTB] = dp[i][X_NOTB]; dp[i + 1][X_BEG] = min(dp[i][X_BEG], dp[i][X_NOTB]) + a; dp[i + 1][X_END] = min(dp[i][X_END], dp[i][X_BEG]); } else if ((mas[i] + 1) % p == 0 || (mas[i] - 1) % p == 0) { dp[i + 1][X_NOTB] = dp[i][X_NOTB] + b; dp[i + 1][X_BEG] = min(dp[i][X_BEG], dp[i][X_NOTB]) + a; dp[i + 1][X_END] = min(dp[i][X_END], dp[i][X_BEG]) + b; } else { dp[i + 1][X_NOTB] = INF; dp[i + 1][X_BEG] = min(dp[i][X_BEG], dp[i][X_NOTB]) + a; dp[i + 1][X_END] = INF; } } long long ans = min(dp[n][X_NOTB], dp[n][X_END]); if (dp[n][X_BEG] < ans) { if (dp[n][X_BEG] == n * a) { if (mas[0] % p == 0 || (mas[0] - 1) % p == 0 || (mas[0] + 1) % p == 0) ans = min(ans, (n - 1) * a + b); } else ans = dp[n][X_BEG]; } return ans; } int main() { scanf("%d %d %d", &n, &a, &b); for (int i = 0; i < n; i++) scanf("%d", &mas[i]); razlozh(mas[0] - 1); razlozh(mas[0]); razlozh(mas[0] + 1); razlozh(mas[n - 1] - 1); razlozh(mas[n - 1]); razlozh(mas[n - 1] + 1); long long ans = INF; for (set<int>::iterator i = st.begin(); i != st.end(); i++) { long long sum = get_ans(*i); ans = min(ans, sum); } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mm = 1000000007; int n, m, x, y, k, p; long long ans = 0, f[10100][8]; char a[4][10100]; void init() { scanf("%d", &n); for (int i = (1); i <= (3); ++i) scanf("%s", a[i] + 1); for (int i = (1); i <= (3); ++i) for (int j = (1); j <= (n); ++j) if (a[i][j] == 'O') { x = i; y = j; break; } } long long dp() { memset(f, 0, sizeof(f)); f[0][7] = 1; for (int i = (1); i <= (n); ++i) { m = 0; for (int j = (1); j <= (3); ++j) if (a[j][i] != '.') m |= 1 << (j - 1); for (int j = (0); j <= (7); ++j) if ((j & m) == m) { (f[i][j] += f[i - 1][7 ^ j | m]) %= mm; if (j == 3 && !m) (f[i][j] += f[i - 1][7]) %= mm; if (j == 6 && !m) (f[i][j] += f[i - 1][7]) %= mm; if (j == 7) { if (!(m & 3)) (f[i][j] += f[i - 1][m & 4 | 3]) %= mm; if (!(m & 6)) (f[i][j] += f[i - 1][m & 1 | 6]) %= mm; } } } return f[n][7]; } void work() { if (x == 1) p = 1; else if (x == 3) p = -1; else p = 0; for (int i = (1); i <= (7); ++i) { k = 0; if (i & 1) if (!(y > 2 && a[x][y - 1] == '.' && a[x][y - 2] == '.')) continue; else k++; if (i & 2) if (!(y < n - 1 && a[x][y + 1] == '.' && a[x][y + 2] == '.')) continue; else k++; if (i & 4) if (!(p && a[x + p][y] == '.' && a[x + p + p][y] == '.')) continue; else k++; if (i & 1) a[x][y - 1] = a[x][y - 2] = 'X'; if (i & 2) a[x][y + 1] = a[x][y + 2] = 'X'; if (i & 4) a[x + p][y] = a[x + p + p][y] = 'X'; if (k & 1) (ans += dp()) %= mm; else (ans -= dp()) %= mm; if (i & 1) a[x][y - 1] = a[x][y - 2] = '.'; if (i & 2) a[x][y + 1] = a[x][y + 2] = '.'; if (i & 4) a[x + p][y] = a[x + p + p][y] = '.'; } (ans += mm) %= mm; cout << ans; } int main() { init(); work(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, c, p[1000], t[1000], sp1 = 0, sp2 = 0, st = 0; cin >> n >> c; for (int i = 0; i < n; i++) { cin >> p[i]; } for (int i = 0; i < n; i++) { cin >> t[i]; } for (int i = 0; i < n; i++) { st += t[i]; sp1 += max(0, p[i] - st * c); } st = 0; for (int i = n - 1; i >= 0; i--) { st += t[i]; sp2 += max(0, p[i] - st * c); } if (sp1 > sp2) cout << "Limak"; else if (sp2 > sp1) cout << "Radewoosh"; else cout << "Tie"; }
1
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T, class T1> inline void gn(T &first, T1 &second) { gn(first); gn(second); } template <class T, class T1, class T2> inline void gn(T &first, T1 &second, T2 &z) { gn(first); gn(second); gn(z); } template <class T> inline void print(T first) { if (first < 0) { putchar('-'); return print(-first); } if (first < 10) { putchar('0' + first); return; } print(first / 10); putchar(first % 10 + '0'); } template <class T> inline void printsp(T first) { print(first); putchar(' '); } template <class T> inline void println(T first) { print(first); putchar('\n'); } template <class T, class U> inline void print(T first, U second) { printsp(first); println(second); } template <class T, class U, class V> inline void print(T first, U second, V z) { printsp(first); printsp(second); println(z); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } const int NN = 1000100; char s[NN * 7]; int nxt[NN][21], len[NN], tot[NN]; int st[NN]; int main() { int n, r, c, cur = 0; gn(n, r, c); for (int i = 1; i <= n; i++) { scanf("%s", s + cur); st[i] = cur; len[i] = strlen(s + cur); cur += len[i]; tot[i] = tot[i - 1] + len[i]; } for (int i = 1; i <= n; i++) { int j = max(i, nxt[i - 1][0]); while (j <= n && tot[j] - tot[i - 1] + j - i <= c) j++; nxt[i][0] = j; } for (int i = 0; i < 21; i++) nxt[n + 1][i] = n + 1; for (int i = 1; i < 21; i++) for (int j = 1; j <= n; j++) { nxt[j][i] = nxt[nxt[j][i - 1]][i - 1]; } int ans = 0, st = 0, ed = 0; for (int i = 1; i <= n; i++) { int k = r, u = i; while (k) u = nxt[u][(__builtin_ctz(k))], k ^= k & -k; if (u - i > ans) ans = u - i, st = i, ed = u; } if (!ans) return 0; int pre = 0; for (int i = st; i < ed; i++) { if (pre + len[i] > c) pre = 0, puts(""); if (pre) putchar(' '); for (int j = 0; j < len[i]; j++) putchar(s[::st[i] + j]); pre += len[i] + 1; } puts(""); return 0; }
2
#include <bits/stdc++.h> using namespace std; long long fastpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } int32_t main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n; cin >> n; bool outer = true; for (long long i = 0; i < n; i++) { bool inner = outer; for (long long i = 0; i < n; i++) { if (inner) cout << "B"; else cout << "W"; inner = !inner; } cout << "\n"; outer = !outer; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int const MAXN = 1e5 + 19; int tin[MAXN], tout[MAXN]; int up[MAXN][20], depth[MAXN]; vector<vector<int>> adj; int timer = 0, l; void dfs(int node, int p) { tin[node] = ++timer; up[node][0] = p; depth[node] = 1 + depth[p]; for (int i = 1; i <= l; i++) up[node][i] = up[up[node][i - 1]][i - 1]; for (auto child : adj[node]) { if (child != p) dfs(child, node); } tout[node] = ++timer; } bool is_ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; } int get_lca(int u, int v) { if (is_ancestor(u, v)) return u; if (is_ancestor(v, u)) return v; for (int i = l; i >= 0; i--) { if (!is_ancestor(up[u][i], v)) u = up[u][i]; } return up[u][0]; } struct nd { int lca, mx, mn; nd(){}; nd(int l, int x, int n) : lca(l), mx(x), mn(n){}; }; nd merge(nd l, nd r) { nd ret; if (l.lca == -1) return r; if (r.lca == -1) return l; ret.lca = get_lca(l.lca, r.lca); if (tin[r.mx] > tin[l.mx]) ret.mx = r.mx; else ret.mx = l.mx; if (tin[r.mn] < tin[l.mn]) ret.mn = r.mn; else ret.mn = l.mn; return ret; } nd tree[4 * MAXN]; void build(int node, int start, int end) { if (start == end) { tree[node] = nd(start, start, start); return; } int mid = (start + end) / 2; build(2 * node, start, mid); build(2 * node + 1, mid + 1, end); tree[node] = merge(tree[2 * node], tree[2 * node + 1]); } nd query(int node, int start, int end, int l, int r) { if (end < l or r < start or l > r) return nd(-1, 0, 1e9); if (l <= start && end <= r) return tree[node]; int mid = (start + end) / 2; nd L = query(2 * node, start, mid, l, r); nd R = query(2 * node + 1, mid + 1, end, l, r); return merge(L, R); } int main() { int n, q; ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> q; l = ceil(log2(n)); memset(up, -1, sizeof up); adj.resize(n + 1); for (int u = 2; u <= n; u++) { int v; cin >> v; v--; adj[u - 1].push_back(v); adj[v].push_back(u - 1); } dfs(0, 0); build(1, 0, n - 1); while (q--) { int l, r; cin >> l >> r; l--, r--; nd ans = query(1, 0, n - 1, l, r); nd path1 = merge(query(1, 0, n - 1, l, ans.mx - 1), query(1, 0, n - 1, ans.mx + 1, r)); nd path2 = merge(query(1, 0, n - 1, l, ans.mn - 1), query(1, 0, n - 1, ans.mn + 1, r)); if (depth[path1.lca] < depth[path2.lca]) cout << ans.mn + 1 << " " << depth[path2.lca] - 1 << "\n"; else cout << ans.mx + 1 << " " << depth[path1.lca] - 1 << "\n"; } }
5
#include<bits/stdc++.h> using namespace std; long long int n,m; int main(){ cin >> n >> m; vector<pair<long long int,long long int>> v(n); for(int i=0;i<n;i++){ cin >> v[i].first >> v[i].second; } sort(v.begin(),v.end()); long long int sum=0,ans=0,i=0,l=0; while(sum < m){ sum++; ans += v[i].first; l++; if(l == v[i].second){ i++; l=0; } } cout << ans; }
0
#include <bits/stdc++.h> using namespace std; int main(){ int n,k,q; cin>>n>>k>>q; vector<int> ok(n); for(int i=0; i<q; ++i){ int a; cin>>a; ++ok[a-1]; } for(int i=0; i<n; ++i){ if(k-q+ok[i]>0)cout<<"Yes"<<endl; else cout<<"No"<<endl; } }
0
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using li_int = list<int>; const int N = 100005; int n; pii a[4][N]; int nxt[4][N], pre[4][N]; void del(int p) { for (int i = 0; i < 4; i++) { int a = pre[i][p], b = nxt[i][p]; nxt[i][a] = b; pre[i][b] = a; } } bool dfs(vector<int> &left) { if (left.size() <= 1) return 1; for (int i = 0; i < 4; i++) { sort(left.begin(), left.end(), [&](int x, int y) { return a[i][x] < a[i][y]; }); int p = 0; for (auto &j : left) { nxt[i][p] = j; pre[i][j] = p; p = j; } nxt[i][p] = 0; } int num = left.size(); while (num > 1) { bool fi = 0; int head[4], target[4]; for (int i = 0; i < 4; i++) head[i] = nxt[i][0], target[i] = a[i][head[i]].second; for (int i = 1; i < num && !fi; i++) for (int j = 0; j < 4; j++) { head[j] = nxt[j][head[j]]; if (a[j][head[j]].first >= target[j]) { vector<int> s; for (int k = nxt[j][0]; k != head[j]; k = nxt[j][k]) s.push_back(k); for (auto &k : s) del(k); for (int k = 0; k < 4; k++) head[k] = nxt[k][0]; if (!dfs(s)) return 0; num -= i; for (int k = 0; k < 4; k++) nxt[k][0] = head[k]; fi = 1; break; } else target[j] = max(target[j], a[j][head[j]].second); } if (!fi) return 0; } return 1; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { int t1, t2, t3, t4; cin >> t1 >> t2 >> t3 >> t4; a[0][i] = pii(t1, t3); a[1][i] = pii(t2, t4); a[2][i] = pii(-t3, -t1); a[3][i] = pii(-t4, -t2); } vector<int> v; for (int i = 1; i <= n; i++) v.push_back(i); cout << (dfs(v) ? "YES" : "NO") << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long base = 10; const long long mod1 = 1e8 + 7, mod2 = 1e7 + 7; const long long maxn = 1e6 + 5; long long hash1[maxn], hash2[maxn], mu1[maxn], mu2[maxn]; string s; void show(long long x, long long y, long long z) { for (int i = 0; i < x; i++) { cout << s[i]; } cout << "+"; for (int i = x; i < y; i++) cout << s[i]; cout << "="; for (int i = y; s[i]; i++) cout << s[i]; cout << endl; } void check(long long x, long long y, long long z) { long long a = (hash1[x] + hash1[y] - hash1[x] * mu1[y - x] % mod1 + mod1) % mod1; long long b = (hash1[z] - hash1[y] * mu1[z - y] % mod1 + mod1) % mod1; long long aa = (hash2[x] + hash2[y] - hash2[x] * mu2[y - x] % mod2 + mod2) % mod2; long long bb = (hash2[z] - hash2[y] * mu2[z - y] % mod2 + mod2) % mod2; if ((s[x] != '0' || y - x == 1) && (s[y] != '0' || z - y > 1) || aa == 0 && bb == 0) if (a == b && aa == bb && x && y > x && y < z) { show(x, y, z); } } int main() { while (cin >> s) { mu1[0] = mu2[0] = 1; for (long long i = 0; s[i]; i++) { hash1[i + 1] = (hash1[i] * base + s[i] - '0') % mod1; hash2[i + 1] = (hash2[i] * base + s[i] - '0') % mod2; mu1[i + 1] = mu1[i] * 10 % mod1; mu2[i + 1] = mu2[i] * 10 % mod2; } long long len = s.size(); long long pj = s.size() / 3; for (int j = 1; j < len - 1; j++) { int y; if ((len - j) / 2 >= j) y = (len - j) / 2; else y = len - j * 2; if (y >= 0) { check(j, j + y, len); check(j, j + y + 1, len); check(j, j + y - 1, len); } } } }
6
#include <bits/stdc++.h> using namespace std; struct card { int u, d; }; card a[200000]; int b[300000], d[3000000], up[300000], down[300000]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d %d", &a[i].u, &a[i].d); b[2 * i] = a[i].u; b[2 * i + 1] = a[i].d; } sort(&b[0], &b[2 * n]); d[0] = b[0]; int k = 1; for (int i = 1; i < 2 * n; ++i) { if (b[i] != b[i - 1]) { d[k] = b[i]; k++; } } int L, R, M; for (int i = 0; i < n; ++i) { L = 0; R = k; while (L + 1 < R) { M = (L + R) / 2; if (d[M] > a[i].u) R = M; else L = M; } up[L]++; } for (int i = 0; i < n; ++i) { L = 0; R = k; while (L + 1 < R) { M = (L + R) / 2; if (d[M] > a[i].d) R = M; else L = M; } if (a[i].d != a[i].u) down[L]++; } int min = 0, now = -1; for (int i = 0; i < k; ++i) { if (up[i] >= (n + 1) / 2) { min = 0; now = i; break; } if (up[i] + down[i] >= (n + 1) / 2) { if ((now == -1) || ((n + 1) / 2 - up[i] < min)) { now = i; min = (n + 1) / 2 - up[i]; } } } if (now == -1) printf("-1"); else printf("%d", min); }
2
#include<bits/stdc++.h> using namespace std; const int N=1e6+5; char s[N]; int n,a[N],b[N]; int solve(int mx) { int add=0; for(int i=1;i<=n;i++) if(s[i]!='?') b[i]=s[i]=='1'?1:-1; else { if(a[i]+add+2<=mx) b[i]=1,add+=2; else b[i]=-1; } for(int i=1;i<=n;i++) b[i]+=b[i-1]; int mn=0;mx=0; for(int i=1;i<=n;i++) mn=min(mn,b[i]),mx=max(mx,b[i]); return mx-mn; } int main() { scanf("%s",s+1); n=strlen(s+1); for(int i=1;i<=n;i++) a[i]=a[i-1]+(s[i]=='1'?1:-1); for(int i=n-1;i>=1;i--) a[i]=max(a[i],a[i+1]); int ans=min(solve(a[1]),solve(a[1]+1)); ans=min(ans,solve(a[1]+2)); printf("%d\n",ans); }
0
#include <iostream> #include<string> using namespace std; int main() { string s;cin>>s; int c=0; for(int i=0;i<s.length();i++) { c+=(s[i]-'0'); } cout<<((c%9)?"No":"Yes"); }
0
#include <bits/stdc++.h> using namespace std; int inf = 1000000005; long long int llinf = 2000000000000000005LL; long long int mod = 1000000007; long long int mod9 = 1000000009; double PI = 3.1415926535897; double eps = 1e-15; int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}, dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; vector<bool> isprime; vector<int> primes; void seive(int n, bool wantlist = true) { isprime.resize(n + 1, true); isprime[0] = isprime[1] = false; int sq = sqrt(n + 1); for (int i = (2); i < (sq + 1); i++) { if (isprime[i]) { for (int j = i * i; j <= n; j += i) isprime[j] = false; } } for (int i = 2; wantlist && i <= n; i++) { if (isprime[i]) primes.push_back(i); } } template <class T> inline T gcd(T a, T b) { while (b != 0) { a %= b; swap(a, b); } return a; } template <class T> inline T lcm(T a, T b) { return a * b / gcd(a, b); } template <class T> inline bool is_square(T a) { return sqrt(a) == floor(sqrt(a)); } template <class T> inline vector<T> operator+(vector<T>& a, vector<T>& b) { assert(a.size() == b.size()); int n = a.size(); vector<T> c(n); for (int i = 0; i < n; i++) c[i] = a[i] + b[i]; return c; } int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } inline long long int bexp(long long int x, long long int n, long long int m = mod) { long long int res = 1; x %= m; while (n) { if (n & 1) res = res * x % m; x = x * x % m; n >>= 1; } return res; } inline bool ispalin(string& str) { int n = str.length(); for (int i = 0; i < n / 2; i++) if (str[i] != str[n - i - 1]) return false; return true; } int main() { string s; cin >> s; int n = s.length(), cnt = 0; bool poss = false; for (int i = (n - 1); i >= (0); i--) { cnt += s[i] == '0'; if (s[i] == '1') poss = poss || cnt >= 6; } if (poss) cout << "Yes" << endl; else cout << "No" << endl; }
1
#include <bits/stdc++.h> using namespace std; int main() { int k, t; cin >> k >> t; vector<int> a(t); for(int i = 0; i < t; i++) { cin >> a[i]; } int m = *max_element(a.begin(), a.end()); cout << max(0, m - 1 - (k - m)) << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); srand(time(0)); int n, first, x; cin >> n >> first >> x; vector<int> pom; for (int i = 1; i <= n; i++) if (i != first) pom.push_back(i); cout << "? " << first << endl; int t1, t2; cin >> t1 >> t2; pair<int, int> mx; if (t1 >= x) { cout << "! " << t1 << endl; exit(0); } else mx = {t1, t2}; for (int i = 0; i < 998 && !pom.empty(); i++) { int a = rand() % (int)(pom).size(); a <<= 2; a %= (int)(pom).size(); cout << "? " << pom[a] << endl; int v, nxt; cin >> v >> nxt; if (v <= x) { if (mx.first < v) { mx = {v, nxt}; } } swap(pom[a], pom.back()); pom.pop_back(); } int v, nxt; v = mx.first, nxt = mx.second; while (v < x) { if (nxt == -1) { cout << "! -1" << endl; return 0; } cout << "? " << nxt << endl; cin >> v >> nxt; } cout << "! " << v << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; template <class T> inline void umax(T &a, T b) { if (a < b) a = b; } template <class T> inline void umin(T &a, T b) { if (a > b) a = b; } template <class T> inline T abs(T a) { return a > 0 ? a : -a; } template <class T> inline T gcd(T a, T b) { return __gcd(a, b); } template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; } const int inf = 1e9 + 143; const long long longinf = 1e18 + 143; inline int read() { int x; scanf(" %d", &x); return x; } const int N = 3050; const int M = 1e5 + 100; int s[N][N]; int Right_border[N][N]; int Down_border[N][N]; int jump[N][N]; pair<pair<int, int>, pair<int, int> > p[M]; int main() { int n = read(); vector<pair<int, int> > cands; for (int i = 0; i < n; i++) { int x1 = read() + 5, y1 = read() + 5; int x2 = read() + 5, y2 = read() + 5; x2--; y2--; p[i] = make_pair(pair<int, int>(x1, y1), pair<int, int>(x2, y2)); cands.push_back(pair<int, int>(x1, y1)); for (int x = x1; x <= x2; x++) { for (int y = y1; y <= y2; y++) { s[x][y] = 1; } } for (int x = x1; x <= x2; x++) { Right_border[x][y1 - 1] = 1; Right_border[x][y2] = 1; } for (int y = y1; y <= y2; y++) { Down_border[x1 - 1][y] = 1; Down_border[x2][y] = 1; } } for (int i = N - 1; i >= 0; i--) { for (int j = 1; j < N; j++) { jump[i][j] = Down_border[i][j] ? i : jump[i + 1][j]; } } for (int i = 1; i < N; i++) { for (int j = 1; j < N; j++) { s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; Right_border[i][j] += Right_border[i - 1][j]; Down_border[i][j] += Down_border[i][j - 1]; } } random_shuffle((cands).begin(), (cands).end()); for (__typeof((cands).begin()) it = (cands).begin(); it != (cands).end(); it++) { int xa = it->first - 1, ya = it->second - 1; for (int d = 1; xa + d < N; d++) { int xb = xa + d; int yb = ya + d; if (s[xb][yb] - s[xa][yb] - s[xb][ya] + s[xa][ya] != d * d) break; if (Down_border[xa][yb] - Down_border[xa][ya] == d && Down_border[xb][yb] - Down_border[xb][ya] == d && Right_border[xb][ya] - Right_border[xa][ya] == d && Right_border[xb][yb] - Right_border[xa][yb] == d) { vector<int> ans; for (int i = 0; i < n; i++) if (p[i].first.first >= xa + 1 && p[i].second.first <= xb && p[i].first.second >= ya + 1 && p[i].second.second <= yb) { ans.push_back(i); } printf("YES %d\n", ans.size()); for (__typeof((ans).begin()) v = (ans).begin(); v != (ans).end(); v++) printf("%d ", *v + 1); return 0; } int nxb = jump[xb][ya + 1]; umax(d, nxb - xa - 1); } } puts("NO"); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; const int mod = 1e9 + 7; int ans; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; s.push_back('0'); reverse(s.begin(), s.end()); int n = s.size(); for (int i = 1; i < n; i++) { if (i < n - 1 || (s[i - 1] == '1')) { ans++; } if (s[i - 1] != s[i]) { if (s[i - 1] == '1') { s[i] = '1'; ans++; } else { if (i < n - 1) { ans++; } } } } cout << ans << '\n'; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> bool chmin(T &a, const T &b) { return a > b ? a = b, true : false; } template <typename T> bool chmax(T &a, const T &b) { return a < b ? a = b, true : false; } template <typename A, typename B> ostream &operator<<(ostream &cout, const pair<A, B> &p) { return cout << "(" << p.first << ", " << p.second << ")"; } const int maxN = 30 + 2; const int maxL = 100000 + 5; int n, ls[maxN]; string s[maxN]; vector<pair<int, int> > E[maxL]; vector<int> AlgoZ(string s) { int n = s.length(); vector<int> f(n, 0); int L = 0, R = 0; for (int i = 1; i < n; ++i) { if (L <= i && i < R) f[i] = min(f[i - L], R - i); while (i + f[i] < n && s[f[i]] == s[i + f[i]]) ++f[i]; if (i + f[i] > R) L = i, R = i + f[i]; } return f; } vector<int> G[maxL]; int vis[maxL]; int nowl, nowr; bool valid(int u) { return (nowl <= u && u < nowr) || u == ls[n]; } bool DFS(int u) { vis[u] = 1; for (int v : G[u]) if (valid(v)) { if (vis[v] == 0) { if (!DFS(v)) return false; } else if (vis[v] == 1) { return false; } else { assert(vis[v] == 2); } } vis[u] = 2; return true; } bool check(int L, int R) { nowl = L; nowr = R; memset(vis, 0, sizeof(vis)); for (int i = L; i < R; ++i) if (!vis[i]) { if (!DFS(i)) return false; } if (!vis[ls[n]] && !DFS(ls[n])) return false; return true; } void add(const vector<pair<int, int> > &E) { for (auto p : E) G[p.first].push_back(p.second); } void erase(const vector<pair<int, int> > &E) { for (auto p : E) G[p.first].erase(find(G[p.first].begin(), G[p.first].end(), p.second)); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; ls[0] = 0; for (int i = 1; i <= n; ++i) cin >> s[i], ls[i] = ls[i - 1] + s[i].length(); for (int i = 1; i <= n; ++i) G[ls[n]].push_back(ls[i - 1]); for (int i = 1; i <= n; ++i) { string t = s[i]; for (int j = 1; j <= n; ++j) t += s[j]; auto f = AlgoZ(t); for (int j = 1; j <= n; ++j) { for (int k = 0; k < (int)s[j].size(); ++k) { int leng = f[ls[j - 1] + k + s[i].size()]; chmin(leng, (int)s[i].size()); chmin(leng, (int)s[j].size() - k); if (leng == (int)s[i].size() && leng == (int)s[j].size() - k) if (k) E[i].emplace_back(ls[j - 1] + k, ls[n]); else ; else if (leng == (int)s[i].size() && leng < (int)s[j].size() - k) E[i].emplace_back(ls[j - 1] + k, ls[j - 1] + k + leng); else if (leng < (int)s[i].size() && leng == (int)s[j].size() - k) E[i].emplace_back(ls[j - 1] + k, ls[i - 1] + leng); } }; ; } int ans = 0; for (int l = 1, r = 0; l <= n; ++l) { chmax(r, l - 1); while (r + 1 <= n) { add(E[++r]); if (!check(ls[l - 1], ls[r])) { erase(E[r--]); break; } } ans += r - l + 1; if (l <= r) erase(E[l]); } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n; cin >> s; if (n % 2 == 1) { cout << 0; return 0; } int sum = 0; int sb = 0; int sa = 0; int i, j; for (i = 0; i < n; i++) if (s[i] == ')') sum--; else sum++; int res[1000006]; for (i = 0; i <= n; i++) res[i] = 0; int ming = 0; for (i = 0; i < n; i++) { if (ming < 0) break; if (s[i] == '(') sa = sum - sb - 1; else sa = sum - sb + 1; if (s[i] == '(') { if ((sb - 1 >= 0) && (ming >= 0) && (sa + sb - 1 == 0)) res[i]++; sb++; ming = min(ming, sb); } else { if ((sb + 1 >= 0) && (ming >= 0) && (sa + sb + 1 == 0)) res[i]++; sb--; ming = min(ming, sb); } } std::reverse(s.begin(), s.end()); for (i = 0; i < n; i++) if (s[i] == ')') s[i] = '('; else s[i] = ')'; int count = 0; sum = 0; sa = 0; sb = 0; for (i = 0; i < n; i++) if (s[i] == ')') sum--; else sum++; ming = 0; for (i = 0; i < n; i++) { if (ming < 0) break; if (s[i] == '(') sa = sum - sb - 1; else sa = sum - sb + 1; if (s[i] == '(') { if ((sb - 1 >= 0) && (ming >= 0) && (sa + sb - 1 == 0)) if (res[n - 1 - i] == 1) count++; sb++; ming = min(ming, sb); } else { if ((sb + 1 >= 0) && (ming >= 0) && (sa + sb + 1 == 0)) if (res[n - 1 - i]++) count++; sb--; ming = min(ming, sb); } } cout << count; }
5
#include <bits/stdc++.h> using namespace std; int n, k, sum = 0; int main() { cin >> n >> k; int qq = n; for (int i = 1; i <= n; i++) { sum = sum + i; qq--; if (sum - qq == k) break; } cout << qq; return 0; }
2
#include <bits/stdc++.h> using namespace std; void pre() {} void solve() { string s; cin >> s; vector<vector<long long int>> ans; bool flag = true; long long int n = s.size(); bool fl[n]; for (long long int i = 0; i < n; i++) { fl[i] = 0; } while (flag) { long long int i = 0; long long int j = n - 1; vector<long long int> v, w; while (i < n) { if (fl[i] == 0 && s[i] == '(') { v.push_back(i); } if (fl[j] == 0 && s[j] == ')') { w.push_back(j); } i++; j--; } if (v.size() == 0 || w.size() == 0) { break; } if (v[0] > w[0]) { break; } i = 0; vector<long long int> h; ans.push_back(h); long long int k = min(v.size(), w.size()); while (i < k && v[i] < w[i]) { flag = true; ans[ans.size() - 1].push_back(v[i]); ans[ans.size() - 1].push_back(w[i]); fl[v[i]] = 1; fl[w[i]] = 1; i++; } } cout << ans.size() << "\n"; for (long long int i = 0; i < ans.size(); i++) { sort(ans[i].begin(), ans[i].end()); cout << ans[i].size() << "\n"; for (long long int i : ans[i]) { cout << i + 1 << " "; } cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); pre(); long long int num = 1; for (long long int i = 0; i < num; i++) { solve(); } }
2
#include <bits/stdc++.h> using namespace std; double pi = 2 * acos(0.0); long long int power(long long int a, long long int b) { long long int res = 1; while (b) { if (b & 1) res = res * a; b = b / 2; a = (a * a); } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int tst; tst = 1; while (tst--) { bool flag = true; long long anscnt = 0, ansnum = 0; unsigned long long n; cin >> n; for (int i = 1; i <= 40; i++) { for (int j = 0; j <= 10; j++) { if ((power(i, 10 - j) * power(i + 1, j)) >= n) { anscnt = i; ansnum = 10 - j; flag = false; break; } } if (flag == false) break; } string str = "codeforces"; string ans = ""; for (int i = 0; i < ansnum; i++) { for (int j = 1; j <= anscnt; j++) { ans += str[i]; } } anscnt++; for (int i = ansnum; i <= 9; i++) { for (int j = 1; j <= anscnt; j++) { ans += str[i]; } } cout << ans; } }
2
#include <bits/stdc++.h> using namespace std; long long n; int dist[3005][3005]; pair<int, int> parent[3005][3005]; bool visited[3005][3005]; vector<int> adj[3005]; long long get_hash(long long i, long long j, long long k) { return i + 10000 * j + 100000000 * k; } unordered_set<long long> s; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long m, k; cin >> n >> m >> k; for (long long i = 0; i < m; i++) { long long u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (long long i = 1; i <= k; i++) { long long x, y, z; cin >> x >> y >> z; s.insert(get_hash(x, y, z)); } for (long long i = 0; i <= n; i++) for (long long j = 0; j <= n; j++) dist[i][j] = (long long)1e9; queue<pair<int, int> > bfs; bfs.push(make_pair(0, 1)); parent[0][1] = make_pair(-1, -1); dist[0][1] = 0; visited[0][1] = true; while (!bfs.empty()) { auto t = bfs.front(); long long c1 = dist[t.first][t.second]; for (auto k : adj[t.second]) { if (s.find(get_hash(t.first, t.second, k)) != s.end()) continue; if (!visited[t.second][k]) { parent[t.second][k] = t; assert(t.first != 0 || t.second != 0); dist[t.second][k] = c1 + 1; visited[t.second][k] = true; bfs.push(make_pair(t.second, k)); } } bfs.pop(); } int min1 = (long long)1e9, index1 = 0; for (long long i = 1; i < n; i++) { if (min1 > dist[i][n]) { min1 = min(dist[i][n], min1); index1 = i; } } if (min1 == (long long)1e9) { cout << "-1\n"; return 0; } cout << min1 << endl; stack<int> st; pair<int, int> temp = make_pair(index1, n); int itr = 0; while (temp != make_pair(-1, -1) && itr < min1) { st.push(temp.second); temp = parent[temp.first][temp.second]; } assert(st.top() == 1); while (!st.empty()) { cout << st.top() << " "; st.pop(); } }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 5; struct LBase { int n, basis[20]; LBase() { n = 0; } inline void insert(int x) { if (n == 20) return; for (int i = 0; i < n; i++) x = min(x, x ^ basis[i]); if (x != 0) { basis[n++] = x; for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--) swap(basis[k], basis[k - 1]); } } inline int getmax() { int ret = 0; for (int i = 0; i < n; i++) ret = max(ret, ret ^ basis[i]); return ret; } } base[maxn << 2]; inline LBase merge(LBase a, LBase b) { for (int i = 0; i < b.n; i++) { if (b.basis[i]) a.insert(b.basis[i]); } return a; } int a[maxn]; int n, q; void pushup(int rt) { base[rt] = merge(base[rt << 1], base[rt << 1 | 1]); } void build(int rt, int l, int r) { if (l == r) { base[rt].insert(a[l]); } else { int mid = l + r >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); pushup(rt); } } LBase query(int rt, int l, int r, int ql, int qr) { if (ql == l && r == qr) { return base[rt]; } else { int mid = l + r >> 1; if (qr <= mid) return query(rt << 1, l, mid, ql, qr); else if (ql > mid) return query(rt << 1 | 1, mid + 1, r, ql, qr); else { return merge(query(rt << 1, l, mid, ql, mid), query(rt << 1 | 1, mid + 1, r, mid + 1, qr)); } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); build(1, 1, n); scanf("%d", &q); while (q--) { int l, r; scanf("%d%d", &l, &r); int ans = query(1, 1, n, l, r).getmax(); printf("%d\n", ans); } return 0; }
6
#include <bits/stdc++.h> using namespace std; struct Node { int val, vald; }; const int MAXN = 300 * 1000 + 5, MOD = 1000 * 1000 * 1000 + 7; Node st[4 * MAXN]; int d[MAXN], l[MAXN], r[MAXN]; vector<int> g[MAXN], vt; void dfs(int v) { l[v] = vt.size(); vt.push_back(v); for (size_t i = 0; i < g[v].size(); i++) { d[g[v][i]] = d[v] + 1; dfs(g[v][i]); } r[v] = vt.size(); } void add(int v, int tl, int tr, int l, int r, int x, int xd) { if (l == tl && r == tr) { st[v].val = (st[v].val + x) % MOD; st[v].vald = (st[v].vald + xd) % MOD; return; } int tm = (tl + tr) / 2; if (l < tm) add(2 * v, tl, tm, l, min(r, tm), x, xd); if (r > tm) add(2 * v + 1, tm, tr, max(l, tm), r, x, xd); } int get(int v, int tl, int tr, int k) { if (tl + 1 == tr) return (st[v].val + (long long)st[v].vald * d[vt[k]]) % MOD; int tm = (tl + tr) / 2; if (k < tm) return (get(2 * v, tl, tm, k) + st[v].val + (long long)st[v].vald * d[vt[k]]) % MOD; return (get(2 * v + 1, tm, tr, k) + st[v].val + (long long)st[v].vald * d[vt[k]]) % MOD; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i < n; i++) { int p; cin >> p; p--; g[p].push_back(i); } dfs(0); int q; cin >> q; for (int i = 0; i < q; i++) { int type; cin >> type; if (type == 1) { int v, x, k; cin >> v >> x >> k; v--; add(1, 0, n, l[v], r[v], (x + (long long)k * d[v]) % MOD, -k); } else { int v; cin >> v; v--; cout << (get(1, 0, n, l[v]) + MOD) % MOD << '\n'; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; void generate(string s, int left, int right, vector<string>& arr, int needed) { if (arr.size() >= needed) { return; } if (left == 0 && right == 0) { arr.push_back(s); } if (left < right) generate(s + ')', left, right - 1, arr, needed); if (left > 0) generate(s + '(', left - 1, right, arr, needed); } void solve() { int n; cin >> n; int left = n, right = n; vector<string> a; generate("", left, right, a, n); for (auto& it : a) { cout << it << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) solve(); }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, k; cin >> n >> m >> k; if (m >= n && k >= n) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
1
#include <iostream> #include <vector> #include <map> #include <algorithm> #include <tuple> #include <queue> using namespace std; int H, W, sx, sy, gx, gy, col[13][13], G[130], cnts, bit[130]; char c[13][13]; long long J[130], JJ[130]; int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }; map<long long, int>Map[13][13]; void init() { H = 0; W = 0; sx = 0; sy = 0; gx = 0; gy = 0; cnts = 0; for (int i = 0; i < 13; i++) { for (int j = 0; j < 13; j++) { col[i][j] = 0; Map[i][j].clear(); c[i][j] = 0; } } for (int i = 0; i < 130; i++) { G[i] = 0; J[i] = 0; JJ[i] = 0; } } void dfs1(int px, int py) { if (px < 0 || py < 0 || px >= H || py >= W) return; if (col[px][py] >= 1 || c[px][py] != 'X') return; col[px][py] = cnts; G[cnts]++; for (int i = 0; i < 4; i++) { dfs1(px + dx[i], py + dy[i]); } } int main() { while (true) { init(); cin >> W >> H; if (H == 0 && W == 0) break; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> c[i][j]; if (c[i][j] == 'S') { sx = i; sy = j; c[i][j] = '.'; } if (c[i][j] == 'G') { gx = i; gy = j; c[i][j] = '.'; } } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (c[i][j] != 'X' || col[i][j] >= 1) continue; cnts++; dfs1(i, j); } } JJ[1] = 1; for (int i = 1; i <= cnts; i++) { J[i] = (G[i] / 2 + 1); if (i >= 2) JJ[i] = JJ[i - 1] * J[i - 1]; } queue<tuple<int, int, long long>> Q; Q.push(make_tuple(sx, sy, 0)); Map[sx][sy][0] = 1; int ans = (1 << 30); while (!Q.empty()) { int px = get<0>(Q.front()), py = get<1>(Q.front()); long long V = get<2>(Q.front()); Q.pop(); int cost = Map[px][py][V]; if (px == gx && py == gy) { ans = min(ans, cost); break; } long long VV = V; for (int i = 1; i <= cnts; i++) { bit[i] = VV % J[i]; VV /= J[i]; } for (int i = 0; i < 4; i++) { int vx = px + dx[i], vy = py + dy[i]; if (vx < 0 || vy < 0 || vx >= H || vy >= W || c[vx][vy] == '#') continue; long long I = V; bool die = false; if (col[vx][vy] >= 1) { I += JJ[col[vx][vy]]; if (bit[col[vx][vy]] + 1 == J[col[vx][vy]]) die = true; } if (die == true) continue; if (Map[vx][vy][I] == 0) { Map[vx][vy][I] = cost + 1; Q.push(make_tuple(vx, vy, I)); } } } cout << ans - 1 << endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,x; cin>>x>>a>>b; if(abs(x-a)>abs(x-b)){cout<<"B"<<endl;} else{cout<<"A"<<endl;} }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int i = 1, l = 1, r = n; while (i < n && a[i] > a[i - 1]) { i++; l = i; } while (i < n && a[i] < a[i - 1]) { i++; r = i; } reverse(a + l - 1, a + r); if (is_sorted(a, a + n)) { cout << "yes" << "\n"; cout << l << " " << r << "\n"; } else cout << "no" << "\n"; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n; cin >> n; cout << (int)sqrt(n) + (int)cbrt(n) - (int)cbrt(sqrt(n)) << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct point { long long value, max, min; }; void solution() { int n; cin >> n; vector<int> action(2 * n); for (int i = 0; i < 2 * n; i++) { string a; cin >> a; if (a == "+") { action[i] = 0; } else { int buff; cin >> buff; action[i] = buff; } } vector<int> values; vector<int> mass; for (int i = 2 * n - 1; i >= 0; i--) { if (action[i] == 0) { if (mass.size() == 0) { cout << "NO" << endl; return; } values.push_back(mass[mass.size() - 1]); mass.pop_back(); } else { mass.push_back(action[i]); } } vector<int> copy = values; set<int> val; bool flag = 0; for (int i = 0; i < 2 * n; i++) { if (action[i] == 0) { val.insert(values[values.size() - 1]); values.pop_back(); } else { if (action[i] != *val.begin()) { flag = 1; break; } else { val.erase(val.begin()); } } } reverse(copy.begin(), copy.end()); if (flag) { cout << "NO" << endl; return; } cout << "YES\n"; for (auto i : copy) { cout << i << ' '; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; n = 1; for (int i = 0; i < n; i++) { solution(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; ll md = 1000000007; ll exp(ll a, ll b) { ll r = 1ll; while (b > 0) { if (b & 1) { r = r * (a % md); r = (r + md) % md; } b >>= 1; a = (a % md) * (a % md); a = (a + md) % md; } return (r + md) % md; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll Min(ll a, ll b) { if (a < b) return a; return b; } ll Max(ll a, ll b) { if (a > b) return a; return b; } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; if (n == 3) { cout << "5"; return 0; } for (long long i = 1; i < 100; i += 2) { long long val = (i + 1) * (i + 1) / 4 + (i / 2) * (i / 2); if (val >= n) { cout << i; return 0; } } cout << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; inline int read() { register int res = 0; register char c = getchar(), f = 1; while (c < 48 || c > 57) { if (c == '-') f = 0; c = getchar(); } while (c >= 48 && c <= 57) res = (res << 3) + (res << 1) + (c & 15), c = getchar(); return f ? res : -res; } inline void write(long long x) { register char c[21], len = 0; if (!x) return putchar('0'), void(); if (x < 0) x = -x, putchar('-'); while (x) c[++len] = x % 10, x /= 10; while (len) putchar(c[len--] + 48); } const int N = 1e5 + 10, L = 20; struct node { int sz, a[L]; inline node(int x = -1) { if (~x) { sz = 1; for (int i = 0; i < L; ++i, x >>= 1) a[i] = x & 1; } else sz = 0, memset(a, 0, sizeof(a)); } inline node operator+(const node& p) const { node r = *this; r.sz += p.sz; for (int i = 0; i < L; ++i) r.a[i] += p.a[i]; return r; } inline node operator^(int x) const { node r = *this; for (int i = 0; i < L; ++i, x >>= 1) r.a[i] = x & 1 ? sz - a[i] : a[i]; return r; } } a[N], sum[N]; inline void write(const node& x) { long long r = 0; for (int i = 0; i < L; ++i) r += (long long)x.a[i] << i; write(r); } int n, m, rt, w[N], lc[N], rc[N], tag[N], tot = 0; inline void pu(int x) { sum[x] = sum[lc[x]] + sum[rc[x]] + a[x]; } int build(int l, int r) { if (l > r) return 0; int mid = (l + r) >> 1, x = mid; w[x] = ++tot; lc[x] = build(l, mid - 1), rc[x] = build(mid + 1, r); pu(x); return x; } inline void add(int x, int t) { a[x] = a[x] ^ t, sum[x] = sum[x] ^ t, tag[x] ^= t; } inline void pd(int x) { if (tag[x]) add(lc[x], tag[x]), add(rc[x], tag[x]), tag[x] = 0; } int merge(int x, int y) { if (!x || !y) return x | y; if (w[x] < w[y]) { pd(x), rc[x] = merge(rc[x], y), pu(x); return x; } else { pd(y), lc[y] = merge(x, lc[y]), pu(y); return y; } } void split(int rt, int k, int& x, int& y) { if (!rt) return x = y = 0, void(); pd(rt); if (sum[lc[rt]].sz < k) { x = rt; split(rc[x], k - sum[lc[x]].sz - 1, rc[x], y); pu(x); } else { y = rt; split(lc[y], k, x, lc[y]); pu(y); } } int op, l, r, x, y, z; int main() { n = read(); for (int i = 1; i <= n; ++i) sum[i] = a[i] = read(); m = read(); rt = build(1, n); while (m--) { op = read(), l = read(), r = read(); if (op == 1) { split(rt, r, x, z); split(x, l - 1, x, y); write(sum[y]), putchar('\n'); rt = merge(merge(x, y), z); } else { split(rt, r, x, z); split(x, l - 1, x, y); add(y, read()); rt = merge(merge(x, y), z); } } }
5
#include <bits/stdc++.h> using namespace std; long long a, b, c, d, l, r, m, ans, lgr, rgr; long long get_sum(long long n) { return (n * (n + 1) / 2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a >> b >> c >> d; for (long long i = a; i <= b; i++) { l = b - 1; r = c + 1; while (r - l > 1) { m = l + (r - l) / 2; if (i + m <= c) l = m; else r = m; } if (r + i < c) continue; lgr = r; if (lgr == c + 1) lgr--; rgr = c; lgr += i; rgr += i; if (lgr > d) ans += (rgr - lgr + 1) * (d - c + 1); else if (rgr <= d) { ans += get_sum(rgr - lgr + 1); ans += (lgr - c) * (rgr - lgr + 1); ans -= (rgr - lgr + 1); } else { ans += get_sum(d - lgr + 1); ans += (lgr - c) * (d - lgr + 1); ans -= (d - lgr + 1); ans += (d - c + 1) * (rgr - (d + 1) + 1); } } cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } int ans; long long n = 1000000000000000000, m; long long d; int nint() { int x; scanf("%d", &x); return x; } long long up(long long x) { x -= d - 1; if (x <= 0) x += n; return x; } long long getans(long long c) { cout << "? " << c << endl; long long x; cin >> x; return up(x); } int main() { long long n, m; cin >> n >> m; m--; for (int p = (n)-1; p >= 0; p--) { if ((1ll << p) == m + 1) { cout << p + 1 << endl; return 0; } if ((1ll << p) <= m) m -= (1ll << p); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int C = 26; void add(int &x, int y) { x += y; while (x >= MOD) x -= MOD; while (x < 0) x += MOD; } int fix(int x) { while (x >= MOD) x -= MOD; while (x < 0) x += MOD; return x; } int pw(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % MOD; b >>= 1; a = 1ll * a * a % MOD; } return ret; } const int MAXN = 5e5 + 10; int n, m, k, s; int r[MAXN], c[MAXN], safes[MAXN]; vector<pair<int, long long>> vec[MAXN]; int mn[MAXN], mx[MAXN], cnt[MAXN]; long long ans; void process(int row, int col, long long cost) { int pos = lower_bound(safes, safes + s, col) - safes; if (pos < s) vec[row + 1].push_back({safes[pos], cost + safes[pos] - col + 1}); if (pos - 1 >= 0) { pos--; vec[row + 1].push_back({safes[pos], cost + col - safes[pos] + 1}); } } void solve() { memset(mn, 63, sizeof(mn)); memset(mx, -1, sizeof(mx)); cin >> n >> m >> k >> s; k++; for (int i = 0; i < k; i++) { if (i < k - 1) cin >> r[i] >> c[i]; else r[i] = c[i] = 1; r[i]--, c[i]--, mn[r[i]] = min(mn[r[i]], c[i]), mx[r[i]] = max(mx[r[i]], c[i]), cnt[r[i]]++; } for (int i = 0; i < s; i++) cin >> safes[i], safes[i]--; sort(safes, safes + s); int rem = k; vec[0].push_back({0, 0}); ans = 1e18; long long xx = 0; for (int i = 0; rem && i < n; i++) { bool fl = rem == cnt[i]; rem -= cnt[i]; if (mx[i] >= 0) { vector<pair<int, long long>> vecTemp; sort(vec[i].begin(), vec[i].end()); for (int l = 0; l < (int)vec[i].size();) { int r = l; while (r < (int)vec[i].size() && vec[i][l].first == vec[i][r].first) r++; vecTemp.push_back(vec[i][l]); l = r; } vec[i] = vecTemp; for (auto x : vec[i]) { int col = x.first; long long cost = x.second + xx; if (mx[i] >= 0) { { long long temp = cost; if (mn[i] < col) temp += col - mn[i]; temp += mx[i] - min(col, mn[i]); if (fl) ans = min(ans, temp); process(i, mx[i], temp); } { long long temp = cost; if (mx[i] > col) temp += mx[i] - col; temp += max(col, mx[i]) - mn[i]; if (fl) ans = min(ans, temp); process(i, mn[i], temp); } } } xx = 0; } else vec[i + 1] = vec[i], xx++; } cout << ans << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int te = 1; for (int w = 1; w <= te; w++) { solve(); } return 0; }
4
#include<iostream> #include<algorithm> using namespace std; int main(){ long N,M; cin>>N>>M; long ans; if(N<M/2){ ans=N; ans+=(M-2*N)/4; } else{ ans=M/2; } cout<<ans<<endl; }
0
#define _USE_MATH_DEFINES #include <iostream> #include <iomanip> #include <algorithm> #include <functional> #include <vector> #include <cstdio> #include <cstring> #include <cmath> #include <cfloat> #include <map> #include <queue> #include <stack> #include <list> #include <string> #include <set> #include <complex> using namespace std; int main(){ int n; string s; while(cin>>n,n!=0){ cin>>s; while(n--){ int count=1; string a=""; char c =s[0]; for(int i=1;i<s.size();i++){ if(c==s[i]) count++; else{ a=a+to_string(count)+c; c=s[i]; count=1; } } a=a+to_string(count)+c; s=a; } cout<<s<<endl; } }
0
#include <bits/stdc++.h> using namespace std; mt19937 rnd(time(0)); const double eps = 1e-10; const long long N = 3e4 + 7; const long long MOD = 1e9 + 7; long long pw(long long b, long long p) { b %= MOD; long long res = 1; while (p > 0) { if (p & 1) res = res * b % MOD; b = b * b % MOD; p >>= 1; } return res; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m; cin >> n >> m; vector<string> s(n); for (long long i = 0; i < n; i++) cin >> s[i]; vector<long long> v; long long ans = 0, f = 0, ok = 0; for (long long j = 0; j < m; j++) { ok = 0; for (long long i = 1; i < n; i++) { if (s[i][j] < s[i - 1][j]) { if (f == j) { ans++; ok = 1; f = j + 1; break; } else { long long flag = 0; for (long long k = v.size() - 1; k >= 0; k--) { if (s[i][v[k]] > s[i - 1][v[k]]) { flag = 1; break; } } if (!flag) { ok = 1; ans++; break; } } } } if (!ok) { v.push_back(j); } } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long a, b, k; const long long c = 360360; long long f[2 * c]; long long dp(long long a, long long b) { f[0] = 0; for (long long i = 1; i <= a - b; ++i) { f[i] = f[i - 1] + 1; for (long long j = 2; j <= k; ++j) { if ((b + i) % j <= i) f[i] = min(f[i], f[i - (b + i) % j] + 1); } } return f[a - b]; } signed main() { cin >> a >> b >> k; if (a / c == b / c) cout << dp(a, b) << endl; else cout << dp(a % c, 0) + dp(c, b % c) + dp(c, 0) * (a / c - (b + c - 1) / c) << endl; return 0; }
3
#include <bits/stdc++.h> const int MAXN = 3000; const int MAX_INV = MAXN * MAXN + 5; int v[MAXN + 5]; int main(void) { int n; int inv = 0; scanf(" %d", &n); for (int i = 0; i < n; i++) { scanf(" %d", &v[i]); for (int j = i - 1; j >= 0; j--) { if (v[j] > v[i]) { inv++; } } } double res = inv & 1; res += 4 * (inv / 2); printf("%.12f\n", res); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long maxn = 55, inf = 1e9 + 1; int32_t main() { long long n; cin >> n; if (n & 1) { puts("YES"); for (long long i = 1; i <= n; i++) cout << 2 * i - (i & 1) << " "; for (long long i = 1; i <= n; i++) cout << 2 * i - !(i & 1) << " "; } else puts("NO"); }
1
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") const int Direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; const int Nmax = 200100; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; vector<int> w((int)1e6 + 1000); for (int i = 1; i <= n; i++) w[a[i]]++; int M = (int)1e6; for (int i = 0; i <= M + 998; i++) { w[i + 1] += w[i] / 2; w[i] %= 2; } int count = 0; for (int i = 0; i <= M + 998; i++) { assert(0 <= w[i] && w[i] <= 1); if (w[i] == 1) count++; } cout << count << endl; return 0; }
1
#include<stdio.h> int main() { int a, out[100002], n,i; while(~scanf("%d", &n)) { for(i=1; i<=n; i++) { scanf("%d", &a); out[a]=i; } for(i=1; i<=n; i++) printf("%d ", out[i]); } return 0; }
0
#include <bits/stdc++.h> using namespace std; mt19937 rnd; const int N = 5e3 + 10; const int inf = 1e9 + 10; int a[N], dp[N], new_dp[N], pref[N], suf[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int j = 0; j <= n; j++) { if (j <= a[1]) { dp[j] = j + (a[1] > j); } else { dp[j] = inf; } } pref[0] = dp[0]; for (int j = 1; j <= n; j++) { pref[j] = min(pref[j - 1], dp[j] - j); } suf[n] = dp[n]; for (int j = n - 1; j >= 0; j--) { suf[j] = min(suf[j + 1], dp[j]); } for (int i = 2; i <= n; i++) { new_dp[0] = suf[0] + (a[i] > 0); for (int j = 1; j <= n; j++) { if (j <= a[i]) { new_dp[j] = min(suf[j], pref[j] + j) + (a[i] > j); } else { new_dp[j] = inf; } } swap(dp, new_dp); pref[0] = dp[0]; for (int j = 1; j <= n; j++) { pref[j] = min(pref[j - 1], dp[j] - j); } suf[n] = dp[n]; for (int j = n - 1; j >= 0; j--) { suf[j] = min(suf[j + 1], dp[j]); } } cout << suf[0] << "\n"; return 0; }
5
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; int s=b-a; cout << s*(s+1)/2-b << endl; return 0; }
0