solution
stringlengths
52
181k
difficulty
int64
0
6
#include<bits/stdc++.h> using namespace std; int main(){ int n,k; scanf("%d %d",&n,&k); if(k==1) printf("0"); else printf("%d",n-k); }
0
#include <bits/stdc++.h> using namespace std; int main() { long int n; cin >> n; long int p = n / 10; long int r = n; long int a = n % 10; n = n / 100; long int q = n * 10 + a; if (p >= q && p > r) cout << p; else if (q >= p && q > r) cout << q; else cout << r; }
1
#include <bits/stdc++.h> using namespace std; const int N = 111, M = 26, mod = 1e9 + 7; int dp[N][N * M]; int main() { dp[0][0] = 1; for (int i = 1; i < N; i++) { for (int j = 0; j <= i * 25; j++) { for (int k = 0; k <= 25; k++) { if (k <= j) dp[i][j] += dp[i - 1][j - k]; dp[i][j] %= mod; } } } int test; cin >> test; while (test--) { string str; cin >> str; int n = str.size(), sum = 0; for (int i = 0; i < n; i++) sum += str[i] - 'a'; cout << (dp[n][sum] + mod - 1) % mod << endl; } }
3
#include<stdio.h> int main() { int a,b,c; scanf("%d %d %d",&a,&b,&c); if(a+1>=c) printf("delicious"); else if(c>a+b) printf("dangerous"); else printf("safe"); }
0
#include <bits/stdc++.h> using namespace std; string A, B; int main(int argc, const char* argv[]) { cin >> A >> B; int j = 0; for (int i = 0; i < B.length(); i++) { if (B[i] == A[j]) j++; } cout << j + 1 << endl; return 0; }
1
#include <bits/stdc++.h> #define Int int64_t using namespace std; int main() { int N; cin >> N; vector<string> s(N); for (int i = 0; i < N; ++i) { cin >> s[i]; } vector<pair<int, int>> a, b; for (int i = 0; i < N; ++i) { int sum = 0, min_s = 1e8; for (auto c : s[i]) { if (c == '(') { ++sum; } else { --sum; } min_s = min(min_s, sum); } if (sum >= 0) { a.emplace_back(-min_s, i); } else { b.emplace_back(min_s - sum, i); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); int sum = 0; for (auto p : a) { for (auto c : s[p.second]) { if (c == '(') { ++sum; } else { --sum; } if (sum < 0) { cout << "No\n"; return 0; } } } for (auto p : b) { for (auto c : s[p.second]) { if (c == '(') { ++sum; } else { --sum; } if (sum < 0) { cout << "No\n"; return 0; } } } cout << (sum == 0 ? "Yes" : "No") << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int NUM = 7368800; int flg[NUM]; int main(){ for(int n, m; cin >> n >> m, n;){ for(int i = 0; i < NUM; i++)flg[i] = 0; for(int i = 0; i < m; i++){ for(int j = n; j < NUM; j += n)flg[j] = 1; for(;flg[n];n++); } cout << n << endl; } return 0; }
0
#include <iostream> #include <vector> int main() { int n; std::cin >> n; std::vector<int> vec(n); for (auto &v : vec)std::cin >> v; int q; std::cin >> q; for (auto i = 0; i < q; ++i) { int b, e, k; std::cin >> b >> e >> k; int count = 0; for (auto j = b; j < e; ++j) { if (vec[j] == k) ++count; } std::cout << count << std::endl; } }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; const unsigned int M = 1000000007; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; cout << (x2 - x1) * (y2 - y1) + 1 << "\n"; } return 0; }
3
#include<cstdio> #define popcnt __builtin_popcount typedef long long LoveLive; const int N=15,M=32768,md=1e9+7; int f[M],n,m,e[N]; int main(){ scanf("%d%d",&n,&m); for(int i=0;i<m;++i){ int u,v; scanf("%d%d",&u,&v); e[u-1]|=1<<(v-1); } const int C=1<<n; f[0]=1; for(int S=3;S<C;++S)if((S&1)==(S>>1&1)) for(int U=S;U;U=(U-1)&S)if((U&1)==(U>>1&1)){//sg=0 int T=S^U; int T_U=1,U_T=1; for(int i=0;i<n;++i){ if(U>>i&1)U_T=U_T*(1LL<<popcnt(e[i]&T))%md;else if(T>>i&1)T_U=T_U*((1LL<<popcnt(e[i]&U))-1)%md; } f[S]=(f[S]+(LoveLive)U_T*T_U%md*f[T])%md; } int ans=1; for(int i=1;i<=m;++i)ans=ans*2%md; ans=(ans-f[C-1]+md)%md; printf("%d",ans); return 0; }
0
#include <vector> #include <iostream> #include <functional> using namespace std; const int mod = 1000000007; int main() { int N, M; cin >> N >> M; vector<int> cs; for (int i = 2; i * i <= M; ++i) { int cnt = 0; while (M % i == 0) { M /= i; ++cnt; } if (cnt >= 1) cs.push_back(cnt); } if (M != 1) cs.push_back(1); vector<int> inv(32); inv[1] = 1; for (int i = 2; i < 32; ++i) inv[i] = (long long)inv[mod % i] * (mod - mod / i) % mod; function<int(int, int)> comb = [&](int n, int m) { int mul = 1; for (int i = n; i > n - m; --i) { mul = (long long)mul * i % mod; } for (int i = 1; i <= m; ++i) { mul = (long long)mul * inv[i] % mod; } return mul; }; int ans = 1; for (int i : cs) { int subans = comb(N + i - 1, i); ans = (long long)ans * subans % mod; } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, a4, a5, a6; cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6; if (a1 + a2 + a3 == a4 + a5 + a6) cout << "YES"; else if (a1 + a4 + a5 == a2 + a3 + a6) cout << "YES"; else if (a1 + a5 + a6 == a2 + a3 + a4) cout << "YES"; else if (a1 + a2 + a4 == a3 + a5 + a6) cout << "YES"; else if (a1 + a2 + a5 == a3 + a4 + a6) cout << "YES"; else if (a1 + a2 + a6 == a3 + a4 + a5) cout << "YES"; else if (a1 + a3 + a4 == a2 + a5 + a6) cout << "YES"; else if (a1 + a3 + a5 == a2 + a4 + a6) cout << "YES"; else if (a1 + a4 + a2 == a3 + a6 + a5) cout << "YES"; else if (a1 + a4 + a3 == a2 + a5 + a6) cout << "YES"; else if (a1 + a4 + a5 == a2 + a3 + a6) cout << "YES"; else if (a1 + a4 + a6 == a2 + a3 + a5) cout << "YES"; else if (a1 + a5 + a6 == a2 + a3 + a4) cout << "YES"; else if (a1 + a5 + a2 == a3 + a4 + a6) cout << "YES"; else if (a1 + a5 + a3 == a4 + a2 + a6) cout << "YES"; else if (a1 + a5 + a4 == a3 + a2 + a6) cout << "YES"; else if (a1 + a5 + a6 == a2 + a3 + a4) cout << "YES"; else if (a1 + a6 + a2 == a3 + a4 + a5) cout << "YES"; else if (a1 + a6 + a3 == a2 + a4 + a5) cout << "YES"; else if (a1 + a6 + a4 == a2 + a3 + a5) cout << "YES"; else if (a1 + a6 + a5 == a2 + a3 + a4) cout << "YES"; else cout << "NO"; cin >> a1; }
1
#include <bits/stdc++.h> using namespace std; int min(int a, int b) { if (a > b) return b; return a; } int ispali(string s) { string t = s; reverse(s.begin(), s.end()); if (t == s) return 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long n; cin >> n; string s; long long flag = 0, badstr = 0; ; for (int i = 0; i < n; i++) { cin >> s; long long len = s.length(); if (len % 2 == 1) flag = 1; else { long long count = 0; for (int j = 0; j < len; j++) { if (s[j] == '1') count++; } if (count % 2 == 1) badstr++; } } if (flag == 0 && badstr % 2 == 1) cout << n - 1 << "\n"; else cout << n << "\n"; } }
2
#include<iostream> #include<vector> using namespace std; int main(){ vector<long long> sum(1000001,0); for(int i=0;i<=1000;i++){ for(int j=0;j<=1000;j++){ sum[i+j]++; } } int n; while(cin >> n){ long long ans = 0; for(int i=0;i<=n;i++){ ans += sum[i] * sum[n-i]; } cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int x = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } set<int> S; int n; int a[100010]; inline bool check(int x) { S.clear(); S.insert(a[1]); for (int i = (3); i <= (n); i++) { if (S.size() && abs(a[i - 1] - a[i]) <= x) S.insert(a[i - 1]); while (S.size()) { set<int>::iterator it = S.begin(); if (abs(*it - a[i]) > x) S.erase(it); else break; } while (S.size()) { set<int>::iterator it = S.end(); it--; if (abs(*it - a[i]) > x) S.erase(it); else break; } } if (S.size()) return 1; return 0; } int main() { n = read(); n += 2; for (int i = (1); i <= (n); i++) a[i] = read(); int l = abs(a[1] - a[2]), r = 1000000000, t; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) r = (t = mid) - 1; else l = mid + 1; } cout << t; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> mp(k + 1, 0); for (int i = 0; i < n; i++) mp[a[i]]++; int o = 0, e = 0; int ctr = 0; for (auto it = 1; it < mp.size(); it++) { if (mp[it] % 2 == 1) { o++; if (o != 0 && o % 2 == 0) { ctr++; } } } cout << n - ctr << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; const int mod = 1e9 + 7; const double eps = 1e-9; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; long long n; bool pt[maxn]; void solve() { long long ans = 0; for (long long i = 2; i <= n; i++) { long long cnt = 0; for (long long j = i + i; j <= n; j += i) { cnt++; } ans += cnt * i; } printf("%lld\n", ans * 4); } int main() { cin >> n; solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long t, n, a, b; char s[200005]; long long k[200005]; int main() { cin >> t; while (t--) { cin >> n >> a >> b; cin >> s; long long m = 0; k[m] = -1; m++; long long suml = 0, sums = 0, sumz = 0, sump = n + 1; for (int i = 0; i < strlen(s); i++) { if (s[i] == '0' && i + 1 < strlen(s) && s[i + 1] == '1') { sump += 2; } if (s[i] == '1' && i + 1 < strlen(s) && s[i + 1] == '1') { sump++; } if (s[i] == '1') if (s[i] == '1' && i + 1 < strlen(s) && s[i + 1] == '0') { k[m] = i + 1; m++; sumz++; } if (s[i] == '0' && i + 1 < strlen(s) && s[i + 1] == '1') { k[m] = i; m++; sumz++; } } k[m] = n; m++; for (int i = 2; i < m - 2; i += 2) { if (2 * a >= (k[i + 1] - k[i]) * b) { sumz -= 2; sump += k[i + 1] - k[i]; } } cout << sump * b + sumz * 2 * a + (n - sumz) * a << endl; } }
3
#include <bits/stdc++.h> using namespace std; int index(int a[],int n){ int j=0; for(int i=0;i<n;i++){ if(i==0 && a[i]!=a[i+1] && a[i+1]==a[i+2]){ j=i; break; } else if(i==0 && a[i]!=a[i+1] && a[i+1]!=a[i+2]){ continue; } else if(a[i]!=a[i+1] && a[i]!=a[i-1]){ j=i; break; } else if(i==(n-1)){ j=n-1; break; } } return j; } int main() { int t,n; cin>>t; int b[t]; for(int i=0;i<t;i++){ cin>>n; int a[n]; for(int j=0;j<n;j++){ cin>>a[j]; } b[i]=index(a,n)+1; } for(int i=0;i<t;i++) cout<<b[i]<<endl; return 0; }
1
#include<stdio.h> int main() { int n,i,data[1000]; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &data[i]); } for (i = n; 1 < i; i--) { printf("%d ", data[i]); } printf("%d\n", data[i]); return 0; }
0
#include <bits/stdc++.h> using namespace std; int dx[] = {+1, -1, +0, +0}; int dy[] = {+0, +0, +1, -1}; int vis[20][20]; int main() { int test; cin >> test; while (test--) { long long n, m, k; cin >> n >> m >> k; string res = "YES", s[n + 1]; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = k - 1; i < n; i++) { for (int j = k - 1; j <= m - k; j++) { if (s[i][j] != '.') { int l = j, r = j, cnt = 0, h = i; vector<pair<int, int> > v1; while (l >= 0 && s[h][l] != '.' && s[h][r] != '.' && r < m && h >= 0) { v1.push_back({h, l}); v1.push_back({h, r}); l--; r++; h--; if (h < 0 || r >= m || l < 0) break; } if (v1.size() > 2 * k) { for (auto x : v1) { s[x.first][x.second] = '#'; } } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] == '*') res = "NO"; } } cout << res << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, mod = 1e9 + 7; const double eps = 1e-9; struct point { double x, y, z; }; point p[N]; double v1, v2; int n; double getdis(int id1, int id2) { double d1 = p[id1].x - p[id2].x; double d2 = p[id1].y - p[id2].y; double d3 = p[id1].z - p[id2].z; return sqrt(d1 * d1 + d2 * d2 + d3 * d3); } pair<pair<double, double>, double> getpos(double t) { for (int i = 1; i <= n; i++) { double dis = getdis(i - 1, i); if (dis / v2 <= t) { t -= dis / v2; continue; } return {{p[i - 1].x + double(p[i].x - p[i - 1].x) * t / (dis / v2), p[i - 1].y + double(p[i].y - p[i - 1].y) * t / (dis / v2)}, p[i - 1].z + double(p[i].z - p[i - 1].z) * t / (dis / v2)}; } if (t <= eps) return {{p[n].x, p[n].y}, p[n].z}; return {{-1, -1}, -1}; } bool check(double m) { auto xx = getpos(m); if (xx.first.first == -1) return true; p[n + 2].x = xx.first.first; p[n + 2].y = xx.first.second; p[n + 2].z = xx.second; return getdis(n + 1, n + 2) / v1 <= m; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i <= n; i++) cin >> p[i].x >> p[i].y >> p[i].z; cin >> v1 >> v2; cin >> p[n + 1].x >> p[n + 1].y >> p[n + 1].z; double l = 0, r = mod; for (int i = 0; i < 200; i++) { double m = (l + r) / 2; if (check(m)) r = m; else l = m; } if (getpos(r).first.first == -1) return cout << "NO", 0; cout << "YES\n"; cout << fixed << setprecision(6) << l << endl; auto xx = getpos(l); cout << xx.first.first << " " << xx.first.second << " " << xx.second; return 0; }
3
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int main(void) { int n,p[100],i,j,cnt,x; cnt=0; cin>>n; for(i=0;i<n;i++){ cin>>p[i]; } for(i=1;i<=n;i++){ cnt=0; for(j=0;j<n;j++){ if(p[j]>=i){ cnt=cnt+1; } } if(cnt>=i) x=i; } cout<<x<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double eps = 1e-10; const double pi = acos(-1.0); const long long INF = 1LL << 62; const int inf = 1 << 29; const long long mod = 1000000007; long long sum(long long n) { if (n & 1) return (n + 1) / 2 * n % mod; else return n / 2 * (n + 1) % mod; } int main(void) { long long a, b; cin >> a >> b; long long ans = 0; long long s = sum(a); for (long long i = 1; i < b; ++i) { long long d = (i * b) % mod * s % mod; long long m = (i * a) % mod; ans = (ans + d + m + mod) % mod; } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t; long long n, max, k; cin >> t; while (t--) { cin >> n >> k; long long ar[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n, greater<int>()); max = ar[0]; long int i = 1; while (k--) { if (ar[i] > 0) max = max + ar[i]; i++; } cout << max << endl; } }
2
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD=998244353; ll N; string S; ll dp[200020][2]; void add(ll &a,ll b){ a=(a+b)%MOD; } ll mod_pow(ll x,ll n){ x%=MOD; ll res=1; while(n>0){ if(n&1) res=res*x%MOD; x=x*x%MOD; n/=2; } return res; } int main(){ cin>>N; cin>>S; dp[0][0]=1; for(int i=0;i<N;i++){ if(S[i]=='0'){ add(dp[i+1][0],dp[i][0]); add(dp[i+1][1],dp[i][1]*2%MOD); }else{ add(dp[i+1][0],dp[i][0]); add(dp[i+1][1],dp[i][1]*2%MOD); add(dp[i+1][1],dp[i][0]); } } vector<pair<int,ll>> v; for(int i=1;i<=N;i++){ if(N%i>0) continue; int x=N/i; if(x%2==0) continue; string s=""; for(int j=0;j<i;j++) s+=S[j]; string t=""; for(int j=0;j<i;j++){ if(s[j]=='0') t+="1"; else t+="0"; } string r=""; for(int j=1;j<=x;j++){ if(j%2) r+=s; else r+=t; } if(S>=r) v.push_back(mkp(i,dp[i][1]+1)); else v.push_back(mkp(i,dp[i][1])); } ll ans=0; for(int i=0;i<v.size();i++){ int now=v[i].first; add(ans,v[i].second*2*now%MOD); for(int j=i+1;j<v.size();j++){ if(v[j].first%now==0) add(v[j].second,MOD-v[i].second); } } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> p; int a, b, c; cin >> a >> b >> c; p.push_back({a, b, c}); cin >> a >> b >> c; p.push_back({a, b, c}); if (p[0][0] != p[1][0] && p[0][1] != p[1][1] && p[0][2] != p[1][2]) { cout << "NO"; } else cout << "YES"; return 0; }
4
#include <bits/stdc++.h> using namespace std; char a[100011]; int n; int dp[100011][3][2]; int path[100011][3][2]; int solve(int idx, int rem, int f) { if (idx == n) { if (rem == 0 && f) return 0; else return (1 << 25); } if (dp[idx][rem][f] != -1) return dp[idx][rem][f]; int res = 1 + solve(idx + 1, rem, f); path[idx][rem][f] = 1; if (a[idx] != '0' || f) { int r2 = solve(idx + 1, (rem + (a[idx] - '0')) % 3, 1); if (r2 < res) path[idx][rem][f] = 2; res = min(res, r2); } return dp[idx][rem][f] = res; } void build(int idx, int rem, int f) { if (idx == n) return; if (path[idx][rem][f] == 1) { build(idx + 1, rem, f); } else { cout << a[idx]; build(idx + 1, (rem + (a[idx] - '0')) % 3, 1); } } int main() { scanf("%s", &a); n = strlen(a); memset(dp, -1, sizeof dp); int res = solve(0, 0, 0); if (res >= (1 << 25)) { bool ok = 0; for (int i = 0; i < n; i++) if (a[i] == '0') { ok = 1; break; } if (ok) cout << 0; if (!ok) printf("-1\n"); } else { build(0, 0, 0); } }
3
#include <bits/stdc++.h> using namespace std; int n, s, a[2005], dp[2005][2], ti[2005][2]; vector<pair<int, int> > v; pair<int, int> nxt[2005][2]; int solve(int i, bool start) { if (dp[i][start] != -1) return dp[i][start]; int t; if (start) t = v[0].first; else { if (a[i] == v.back().first) return 0; int ind = upper_bound(v.begin(), v.end(), make_pair(a[i], 1 << 30)) - v.begin(); t = v[ind].first; } ti[i][start] = t; vector<int> c; for (int j = 0; j < n; j++) if ((a[j] == t) || (j == i)) c.push_back(j); int ret = 1 << 30; for (int j = 0; j < c.size(); j++) { int x = c[j], y = c[(j + c.size() - 1) % c.size()]; int L = ((i - x + n) % n) + ((y - x + n) % n); int R = ((y - i + n) % n) + ((y - x + n) % n); if (a[y] == t) { if (solve(y, false) + L < ret) { ret = solve(y, false) + L; nxt[i][start].first = j; nxt[i][start].second = 0; } } if (a[x] == t) { if (solve(x, false) + R < ret) { ret = solve(x, false) + R; nxt[i][start].first = j; nxt[i][start].second = 1; } } } return dp[i][start] = ret; } int main() { scanf("%d %d", &n, &s); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); v.push_back(make_pair(a[i], i)); } sort(v.begin(), v.end()); s--; memset(dp, -1, sizeof dp); printf("%d\n", solve(s, true)); int i = s, start = true; while (true) { if ((!start) && (a[i] == v.back().first)) break; int t = ti[i][start]; vector<int> c; int ci; for (int j = 0; j < n; j++) { if (j == i) { c.push_back(j); ci = c.size() - 1; } else if (a[j] == t) c.push_back(j); } int j = nxt[i][start].first, dir = nxt[i][start].second; int x = c[j], y = c[(j + c.size() - 1) % c.size()]; if (dir == 0) { if (a[i] == t) printf("+0\n"); for (int k = ci; c[k] != x;) { int k_ = (k + c.size() - 1) % c.size(); int d = (c[k] - c[k_] + n) % n; printf("-%d\n", d); k = k_; } for (int k = ci; c[k] != y;) { int k_ = (k + 1) % c.size(); int d = (c[k_] - c[k] + n) % n; if (k == ci) printf("+%d\n", (c[k_] - x + n) % n); else printf("+%d\n", d); k = k_; } i = y; start = false; } else { if (a[i] == t) printf("+0\n"); for (int k = ci; c[k] != y;) { int k_ = (k + 1) % c.size(); int d = (c[k_] - c[k] + n) % n; printf("+%d\n", d); k = k_; } for (int k = ci; c[k] != x;) { int k_ = (k + c.size() - 1) % c.size(); int d = (c[k] - c[k_] + n) % n; if (k == ci) printf("-%d\n", (y - c[k_] + n) % n); else printf("-%d\n", d); k = k_; } i = x; start = false; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long long linf = 1ll * inf * inf; const int N = 1000000 + 7; const int M = 10000 + 7; const int multipleTest = 0; const double pi = acos(-1); template <typename T> void addmod(T& a, const long long& b, const int MOD = inf) { a = (a + b) % inf; if (a < 0) a += inf; } int n, k; int a[N]; vector<pair<int, int> > h; set<int> present; long long dp[N]; void solve() { cin >> n >> k; for (int i = (0), _b = (n); i < _b; ++i) { scanf("%d", a + i); h.push_back({-a[i], i + 1}); } sort(h.begin(), h.end()); dp[0] = 0; for (int i = 1; i <= n; ++i) { long long t = i / (k - 1); addmod(dp[i], i * t - t * (t + 1) / 2 * (k - 1)); } long long res = 0; for (auto& p : h) { long long u = p.first; int i = p.second; int lhs = 1, rhs = n; auto it = present.lower_bound(i); if (it != present.end()) rhs = (*it) - 1; if (it != present.begin()) { --it; lhs = (*it) + 1; } long long cur = 0; addmod(cur, dp[rhs - lhs + 1] - dp[i - lhs] - dp[rhs - i]); addmod(res, cur * -u); present.insert(i); } cout << res << '\n'; } int main() { int Test = 1; if (multipleTest) { cin >> Test; } for (int i = 0; i < Test; ++i) { solve(); } }
6
#include <bits/stdc++.h> using namespace std; const int nmax = 100010; int n, pi[nmax], sum[nmax]; char s[nmax]; vector<pair<int, int> > ans; int main() { gets(s + 1); n = strlen(s + 1); int q = 0; for (int i = 2; i <= n; ++i) { while (q && s[q + 1] != s[i]) q = pi[q]; if (s[q + 1] == s[i]) q++; pi[i] = q; } for (int i = n; i; --i) { sum[i]++; sum[pi[i]] += sum[i]; } for (int i = n; pi[i]; i = pi[i]) ans.push_back({pi[i], sum[pi[i]]}); ans.push_back({n, 1}); sort(ans.begin(), ans.end()); printf("%i\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%i %i\n", ans[i].first, ans[i].second); }
4
#include <cstdio> #include <cstdint> #include <vector> #include <algorithm> #include <utility> #include <numeric> #include <set> #include <map> #include <tuple> #include <string> constexpr intmax_t operator ""_jd(unsigned long long n) { return n; } constexpr uintmax_t operator ""_ju(unsigned long long n) { return n; } constexpr size_t operator ""_zu(unsigned long long n) { return n; } // constexpr ptrdiff_t operator ""_td(unsigned long long n) { return n; } template < typename Monoid, typename Container = std::vector<typename Monoid::first_type> > class basic_segment_tree { public: using size_type = size_t; using first_type = typename Monoid::first_type; using second_type = typename Monoid::second_type; using value_type = first_type; using binary_operation = typename Monoid::binary_operation; using external_binary_operation = typename Monoid::external_binary_operation; using container = Container; private: size_type M_base_size; binary_operation M_op1; external_binary_operation M_op2; container M_c; template <typename Predicate> size_type M_search_root(Predicate pred, value_type& x) const { size_type n = M_base_size; size_type l = n; size_type r = n+n; size_type v = r; std::vector<size_type> rs; x = M_op1.identity; while (l < r) { if (l & 1) { if (!pred(M_op1(x, M_c[l]))) return l; x = M_op1(x, M_c[l++]); } if (r & 1) rs.push_back(--r); l >>= 1; r >>= 1; } while (!rs.empty()) { size_type r = rs.back(); rs.pop_back(); if (!pred(M_op1(x, M_c[r]))) return r; x = M_op1(x, M_c[r]); } return v; } template <typename Predicate> size_type M_search_leaf(Predicate pred, size_type v, value_type& x) const { size_type n = M_base_size; while (v < n) { size_type c = v << 1; if (pred(M_op1(x, M_c[c]))) { x = M_op1(x, M_c[c]); c |= 1; } v = c; } return v - n; } public: basic_segment_tree() = default; basic_segment_tree(basic_segment_tree const&) = default; basic_segment_tree(basic_segment_tree&&) = default; basic_segment_tree(size_type n, first_type const& x = binary_operation().identity): M_base_size(n), M_op1(binary_operation()), M_op2(external_binary_operation()), M_c(n+n, x) { for (size_type i = n; i--;) M_c[i] = M_op1(M_c[i<<1|0], M_c[i<<1|1]); } template <typename InputIt> basic_segment_tree(InputIt first, InputIt last): M_op1(binary_operation()), M_op2(external_binary_operation()) { assign(first, last); } basic_segment_tree& operator =(basic_segment_tree const&) = default; basic_segment_tree& operator =(basic_segment_tree&&) = default; void assign(size_type n, value_type const& x) { M_base_size = n; M_c.assign(n+n, x); for (size_type i = n; i--;) M_c[i] = M_op1(M_c[i<<1|0], M_c[i<<1|1]); } template <typename InputIt> void assign(InputIt first, InputIt last) { container tmp(first, last); M_base_size = tmp.size(); M_c.resize(M_base_size); M_c.insert(M_c.end(), tmp.begin(), tmp.end()); for (size_type i = M_base_size; i--;) M_c[i] = M_op1(M_c[i<<1|0], M_c[i<<1|1]); } void modify(size_type i, second_type const& x) { i += M_base_size; M_c[i] = M_op2(M_c[i], x); while (i > 1) { i >>= 1; M_c[i] = M_op1(M_c[i<<1|0], M_c[i<<1|1]); } } void assign_at(size_type i, value_type const& x) { i += M_base_size; M_c[i] = x; while (i > 1) { i >>= 1; M_c[i] = M_op1(M_c[i<<1|0], M_c[i<<1|1]); } } value_type const& operator [](size_type i) const { return M_c[i + M_base_size]; } value_type accumulate(size_type l, size_type r) { first_type resl = M_op1.identity; first_type resr = resl; l += M_base_size; r += M_base_size; while (l < r) { if (l & 1) resl = M_op1(resl, M_c[l++]); if (r & 1) resr = M_op1(M_c[--r], resr); l >>= 1; r >>= 1; } return M_op1(resl, resr); } template <typename Predicate> std::pair<size_type, value_type> partition_point(Predicate pred) const { value_type value; size_type root = M_search_root(pred, value); size_type bound = M_search_leaf(pred, root, value); return {bound, value}; } }; template <typename Tp> struct range_prod_single_update { using first_type = Tp; using second_type = Tp; struct binary_operation { first_type identity{1}; first_type operator ()(first_type const& x, first_type const& y) const { return x * y; } }; struct external_binary_operation { first_type operator ()(first_type const&, second_type const& y) const { return y; } }; }; int main() { size_t n; scanf("%zu", &n); std::map<intmax_t, size_t> enc; std::vector<std::pair<intmax_t, intmax_t>> ta(n); for (auto& p: ta) { scanf("%jd %jd", &p.first, &p.second); enc[p.first]; } size_t q; scanf("%zu", &q); std::vector<std::pair<intmax_t, intmax_t>> lr(q); for (auto& p: lr) { scanf("%jd %jd", &p.first, &p.second); enc[p.first]; enc[p.second]; } size_t m = 0; for (auto& p: enc) p.second = m++; basic_segment_tree<range_prod_single_update<double>> st(m); for (size_t i = 0; i < n; ++i) { size_t it = enc.at(ta[i].first); double x = 1.0 - ta[i].second / 10.0; st.modify(it, x); } for (size_t i = 0; i < q; ++i) { size_t l = enc.at(lr[i].first); size_t r = enc.at(lr[i].second); double y = 1e9 * st.accumulate(l, r); printf("%.12f\n", y); } }
0
#include<cstdio> char A[1<<8|1]; int op[333333],cnt; void make(int S,int ty){ if(!S)return; int i=0;while(!(S>>i&1))i++; if(S==(1<<i)){ op[cnt++]=ty&2?-i-1:i+1; return; } if(ty&1){ op[cnt++]=ty&2?i+1:-i-1; make(S^1<<i,ty^3); op[cnt++]=ty&2?-i-1:i+1; make(S^1<<i,ty); } else{ make(S^1<<i,ty); op[cnt++]=ty&2?-i-1:i+1; make(S^1<<i,ty^3); op[cnt++]=ty&2?i+1:-i-1; } } int x[333333],y[333333],L; int main(){ int N;scanf("%d%s",&N,A); for(int S=1;S<1<<N;S++)if(A[S]=='1') for(int T=S;T;T=T-1&S)if(A[T]=='0')return puts("Impossible"),0; for(int S=1;S<1<<N;S++)if(A[S]=='0'){ bool fl=1; for(int T=S-1&S;T&&fl;T=T-1&S)if(A[T]=='0')fl=0; if(fl)make(S,0),make(S,1); } for(int i=0;i<cnt;i++){ int x1=op[i]>0?op[i]-1:-op[i],x2=op[i]>0?x1+1:x1-1; for(;x[L]!=x1;L++)x[L]<x1?x[L+1]=x[L]+1:x[L+1]=x[L]-1; x[++L]=x1;y[L]=1; x[++L]=x2;y[L]=1; x[++L]=x2;y[L]=0; } for(;x[L];L++)x[L+1]=x[L]-1; printf("Possible\n%d\n",L); for(int i=0;i<=L;i++)printf("%d %d\n",x[i],y[i]); }
0
#include <iostream> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) int m, n; bool tront[90][90]; int dfs(int y, int x) { int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0}; int rep = 0; tront[y][x] = false; REP(i, 4) { int nx = x + dx[i], ny = y + dy[i]; if (! (nx >= 0 && nx < m && ny >= 0 && ny < n)) continue; if (! tront[ny][nx]) continue; tront[ny][nx] = false; rep = max(rep, dfs(ny, nx)); tront[ny][nx] = true; } tront[y][x] = true; return rep + 1; } int main() { while (true) { int ans = 0; cin >> m >> n; if (n == 0 && m == 0) break; REP(i, n) REP(j, m) cin >> tront[i][j]; REP(i, n) REP(j, m) if (tront[i][j]) ans = max(ans, dfs(i, j)); cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxN = 300; bool matrix[maxN][maxN]; int nRows, nColumns; int leftStreak[maxN][maxN], downStreak[maxN][maxN]; int findRight() { for (int y = 0; y < nRows; y++) for (int x = nColumns - 1; x >= 0; x--) leftStreak[x][y] = (x != nColumns - 1) ? (matrix[x][y] == matrix[x + 1][y] ? leftStreak[x + 1][y] + 1 : 1) : 1; for (int x = 0; x < nColumns; x++) for (int y = nRows - 1; y >= 0; y--) downStreak[x][y] = (y != nRows - 1) ? (matrix[x][y] == matrix[x][y + 1] ? downStreak[x][y + 1] + 1 : 1) : 1; int answer = 0; for (int x = 0; x < nColumns; x++) for (int y = 0; y < nRows; y++) { if (matrix[x][y]) { int size = downStreak[x][y]; if (size < 2) continue; if (leftStreak[x][y] != size) continue; if (downStreak[x + size - 1][y] != size) continue; if (leftStreak[x][y + size - 1] != size) continue; if (x && y && matrix[x - 1][y - 1]) continue; if (x && y + size < nRows && matrix[x - 1][y + size]) continue; if (x + size < nColumns && y && matrix[x + size][y - 1]) continue; if (x + size < nColumns && y + size < nRows && matrix[x + size][y + size]) continue; if (x && (matrix[x - 1][y] || downStreak[x - 1][y] < size)) continue; if (y && (matrix[x][y - 1] || leftStreak[x][y - 1] < size)) continue; if (x + size < nColumns && (matrix[x + size][y] || downStreak[x + size][y] < size)) continue; if (y + size < nRows && (matrix[x][y + size] || leftStreak[x][y + size] < size)) continue; if (size > 2) { if (matrix[x + 1][y + 1] || leftStreak[x + 1][y + 1] != size - 2) continue; if (matrix[x + 1][y + 1] || downStreak[x + 1][y + 1] != size - 2) continue; if (matrix[x + 1][y + size - 2] || leftStreak[x + 1][y + size - 2] != size - 2) continue; if (matrix[x + size - 2][y + 1] || downStreak[x + size - 2][y + 1] != size - 2) continue; } answer++; } } return answer; } int plusStreak[maxN][maxN], rightStreak[maxN][maxN]; bool filled(int x, int y) { if (x < 0 || y < 0 || x >= nColumns || y >= nRows) return false; return matrix[x][y]; } int findStupid() { for (int y = nRows - 1; y >= 0; y--) for (int x = nColumns - 1; x >= 0; x--) rightStreak[x][y] = (x + 1 < nColumns && y + 1 < nRows) ? (matrix[x + 1][y + 1] == matrix[x][y] ? rightStreak[x + 1][y + 1] + 1 : 1) : 1; for (int y = nRows - 1; y >= 0; y--) for (int x = 0; x < nColumns; x++) plusStreak[x][y] = (x && y + 1 < nRows) ? (matrix[x - 1][y + 1] == matrix[x][y] ? plusStreak[x - 1][y + 1] + 1 : 1) : 1; int answer = 0; for (int x = 0; x < nColumns; x++) for (int y = 0; y < nRows; y++) if (matrix[x][y]) { int size = plusStreak[x][y]; if (size != rightStreak[x][y]) continue; if (size < 2) continue; if (rightStreak[x - size + 1][y + size - 1] != size) continue; if (plusStreak[x + size - 1][y + size - 1] != size) continue; if (matrix[x - 1][y] || plusStreak[x - 1][y] < size - 1) continue; if (matrix[x + 1][y] || rightStreak[x + 1][y] < size - 1) continue; if (matrix[x - size + 1][y + size] || rightStreak[x - size + 1][y + size] < size - 1) continue; if (matrix[x + size - 1][y + size] || plusStreak[x + size - 1][y + size] < size - 1) continue; if (y && matrix[x][y - 1]) continue; if (x - size >= 0 && matrix[x - size][y + size - 1]) continue; if (x + size < nColumns && matrix[x + size][y + size - 1]) continue; if (y + size * 2 - 1 < nRows && matrix[x][y + size * 2 - 1]) continue; if (matrix[x][y + 1] || rightStreak[x][y + 1] < size - 1) continue; if (matrix[x][y + 1] || plusStreak[x][y + 1] < size - 1) continue; if (size > 2) { if (matrix[x + size - 2][y + size - 1] || plusStreak[x + size - 2][y + size - 1] < size - 1) continue; if (matrix[x - size + 2][y + size - 1] || rightStreak[x - size + 2][y + size - 1] < size - 1) continue; } for (int t = 0; t < size; t++) { if (filled(x - t - 1, y - 1 + t)) goto fail; if (filled(x + t + 1, y - 1 + t)) goto fail; if (filled(x - size + t, y + size + t)) goto fail; if (filled(x + size - t, y + size + t)) goto fail; if (t > 0 && t < size - 1 && filled(x - t + 1, y + 1 + t)) goto fail; if (t > 0 && t < size - 1 && filled(x + t - 1, y + 1 + t)) goto fail; if (t > 0 && t < size - 1 && filled(x - size + t + 2, y + size + t - 2)) goto fail; if (t > 0 && t < size - 1 && filled(x + size - t - 2, y + size + t - 2)) goto fail; } answer++; fail:; } return answer; } void solve() { scanf("%d %d", &nRows, &nColumns); for (int y = 0; y < nRows; y++) { char buffer[maxN]; scanf("%s", buffer); for (int x = 0; x < nColumns; x++) matrix[x][y] = buffer[x] == '1'; } int nSquares = 0; nSquares += findRight(); nSquares += findStupid(); printf("%d\n", nSquares); } int main() { int nTests; scanf("%d", &nTests); for (int i = 0; i < nTests; i++) solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; int i; vector<pair<int, int>> v; v.reserve(10000); pair<int, int> cur; scanf("%d", &n); cur.first = 1, cur.second = 1; for (i = 0; i < n; i++) { v.push_back(cur); if (cur.first == cur.second) { cur.first++; } else { cur.second++; } } printf("%d\n", max(v[n - 1].first, v[n - 1].second)); for (vector<pair<int, int>>::iterator iter = v.begin(); iter != v.end(); iter++) { printf("%d %d\n", iter->first, iter->second); } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long getpow(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = (res * a) % 1000000007; a = (a % 1000000007 * a % 1000000007) % 1000000007; n >>= 1; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long i, j, k, m, n, t, l, r, x, z, y, p; cin >> n >> k; vector<long long> v[27]; char s[n + 9]; cin >> s; long long count = 1; for (i = 0; i < n; i++) { if (s[i] == s[i + 1]) count++; else { v[s[i] - 'a'].push_back(count); count = 1; } } long long mx = 0; for (i = 0; i < 26; i++) { p = 0; for (j = 0; j < v[i].size(); j++) { p += (v[i][j] / k); } if (p > mx) mx = p; } cout << mx; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m; int d[505][505]; int vis[505], match[505]; int ans; int in[505], out[505]; int dfs(int u, int x) { int i, j; for (i = 1; i <= n; i++) { if (i == u || vis[i] || !d[x][i]) continue; vis[i] = 1; if (match[i] == -1 || dfs(u, match[i])) { match[i] = x; return 1; } } return 0; } int choice(int u) { memset(match, -1, sizeof(match)); int i, edge, sum, l, ans = 0; edge = in[u] + out[u] - d[u][u]; sum = 2 * n - 1 - edge; for (i = 1, ans = 0; i <= n; i++) { if (i == u) continue; memset(vis, 0, sizeof(vis)); if (dfs(u, i)) ans++; } sum += m - edge - ans; sum += n - 1 - ans; return sum; } int main() { scanf("%d %d", &n, &m); int i, j, k; for (i = 0; i <= n; i++) for (j = 0, in[i] = 0, out[i] = 0; j <= n; j++) d[i][j] = 0; for (k = 0; k < m; k++) { scanf("%d %d", &i, &j); d[i][j] = 1; in[j]++; out[i]++; } for (i = 1, ans = 999999999; i <= n; i++) { int t = choice(i); ans = min(ans, t); } printf("%d\n", ans); }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; char a[maxn][maxn]; int n, s[maxn]; int main() { scanf("%d", &n); int r = 0, c = 0; int max1 = 0, sum = 0, biao = 1; for (int i = 1; i <= n; i++) { scanf("%d", s + i); if (i % 2 == 1) sum += s[i]; else sum -= s[i]; if (max1 < sum) { max1 = sum; biao = i; } if (r > sum) r = sum; c += s[i]; } r = max1 - r; sum = 0; for (int i = 1; i <= biao; i++) sum += s[i]; for (int i = 0; i <= 1000; i++) for (int j = 0; j <= 1000; j++) a[i][j] = ' '; int x = 0, y = sum + 1; for (int i = biao; i >= 1; i--) { for (int j = s[i]; j > 0; j--) if (i & 1) { x++; y--; a[x][y] = '/'; } else a[--x][--y] = '\\'; if (i & 1) x++; else x--; } x = 0, y = sum; for (int i = biao + 1; i <= n; i++) { for (int j = s[i]; j >= 1; j--) if (i & 1) a[--x][++y] = '/'; else a[++x][++y] = '\\'; if (i & 1) x--; else x++; } for (int i = 1; i <= r; i++) { for (int j = 1; j <= c; j++) cout << a[i][j]; cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main(){ int X,Y,Z,K; cin >> X >> Y >> Z >> K; vector<long> A(X),B(Y),C(Z),AB(X*Y),ABC(K*K); for(int i=0; i<X; i++){ cin >> A.at(i); } for(int i=0; i<Y; i++){ cin >> B.at(i); } for(int i=0; i<Z; i++){ cin >> C.at(i); } for(int i=0; i<X; i++){ for(int j=0; j<Y; j++){ AB.at(Y*(i+1)-j-1)=A.at(i)+B.at(j); } } sort(C.begin(),C.end()); sort(AB.begin(),AB.end()); for(int i=0; i<X*Y&&i<K; i++){ for(int j=0; j<Z&&j<K; j++){ ABC.at(K*(i+1)-j-1)=AB.at(X*Y-1-i)+C.at(Z-1-j); } } sort(ABC.begin(),ABC.end()); for(int i=0; i<K; i++){ cout << ABC.at(K*K-1-i) << endl; } }
0
#include <bits/stdc++.h> char a[150]; bool f1, f2, f3; int main() { int n; scanf("%s", a); for (n = 0; a[n]; n++) { if (a[n] >= 'A' && a[n] <= 'Z') f1 = 1; if (a[n] >= 'a' && a[n] <= 'z') f2 = 2; if (a[n] >= '0' && a[n] <= '9') f3 = 3; } puts(n >= 5 && f1 && f2 && f3 ? "Correct" : "Too weak"); return 0; }
1
#include <bits/stdc++.h> using namespace std; int arr[105]; int auction[105]; int main() { int n, m, x; cin >> n >> m; long long sum = 0; for (int i = 1; i <= n; i++) { cin >> arr[i]; sum += arr[i]; } for (int i = 1; i <= m; i++) { cin >> x; auction[i] = arr[x]; sum -= arr[x]; } sort(auction + 1, auction + m + 1); for (int i = m; i >= 1; i--) { if (sum > auction[i]) sum *= 2; else { sum += auction[i]; } } cout << sum << endl; return 0; }
3
#include <algorithm> #include <iostream> #include <vector> using namespace std; const int INF = (int)1e9 + 7; #define rep(i, n) for (int i = 0; i < n; i++) int n; vector<int> r; vector<vector<int>> spc; bool check() { vector<int> total(n); for (int k = 0; k < 3; k++) { vector<int> rank(n, 0); rep(i, n) rep(j, n) if (spc[k][i] < spc[k][j]) rank[i]++; rep(i, n) total[i] += r[rank[i]]; } vector<int> rank(n, 0); rep(i, n) rep(j, n) if (total[i] < total[j]) rank[i]++; return rank[0] <= 8 - 1; } int main() { while (cin >> n) { r.resize(n); spc.assign(3, vector<int>(n)); rep(i, n) cin >> r[i]; rep(i, n) cin >> spc[0][i] >> spc[1][i] >> spc[2][i]; int ans = INF; rep(k, 3) for (int p = 1; p <= 100; p++) { spc[k][0] += p; if (check()) ans = min(ans, p); spc[k][0] -= p; } if (ans == INF) cout << "Saiko" << endl; else cout << ans << endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; string a; int b; int main(){ scanf("%d",&b); cin>>a; if(b<3200)puts("red"); else cout<<a; }
0
#include <bits/stdc++.h> using namespace std; int n; int a[150010]; int ac[150010]; set<pair<int, int> > sw; int main() { cin >> n; for (int i = 0; i < (n); i++) cin >> a[i]; vector<int> cand; for (int i = 0; i < (n - 1); i++) { if (i & 1) { if (a[i] <= a[i + 1]) { cand.push_back(i); cand.push_back(i + 1); } else { ac[i + 1] = 1; } } else { if (a[i] >= a[i + 1]) { cand.push_back(i); cand.push_back(i + 1); } else { ac[i + 1] = 1; } } } for (int i = 0; i < (n); i++) ac[i + 1] += ac[i]; sort((cand).begin(), (cand).end()); cand.erase(unique((cand).begin(), (cand).end()), cand.end()); if (cand.size() >= 10) { puts("0"); return 0; } for (int p : cand) { for (int q = 0; q < (n); q++) if (q != p) { int s = p, t = q; if (s > t) swap(s, t); int num = (s > 0 ? ac[s - 1] : 0) + max(ac[n - 1] - ac[t + 1], 0) + max(ac[t - 1] - ac[s + 1], 0); swap(a[s], a[t]); if (s > 0) { if (s & 1) { if (a[s] > a[s - 1]) { ++num; } } else { if (a[s] < a[s - 1]) { ++num; } } } if (t < n - 1) { if (t & 1) { if (a[t] > a[t + 1]) { ++num; } } else { if (a[t] < a[t + 1]) { ++num; } } } if (s & 1) { if (a[s] > a[s + 1]) { ++num; } } else { if (a[s] < a[s + 1]) { ++num; } } if (t != s + 1) { if (t & 1) { if (a[t] > a[t - 1]) { ++num; } } else { if (a[t] < a[t - 1]) { ++num; } } } swap(a[s], a[t]); if (num == n - 1) { sw.insert(make_pair(s, t)); sw.insert(make_pair(t, s)); } } } cout << sw.size() / 2 << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ull unsigned long long #define all(x) x.begin(),x.end() #define MOD 1000000007 #define PI acos(-1) #define EPS 1e-9 #define debug cerr << #define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define case(tc) cout << "Case " << tc << ": "; #define close << "\n"; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); #ifdef necromancer freopen("../input.txt", "r", stdin); //freopen("../output.txt", "w", stdout); #endif int testcase; cin >> testcase; for (int tc = 0; tc < testcase; tc++) { string str; cin >> str; int invariant[2] = {-1,-1}; ll ans = 0; for (int i = 0; i < str.size(); ++i) { if(str[i] != '?'){ invariant[ (i%2)^(str[i]-'0') ] = i; // keep maximum invariant index } int mn = min(invariant[0],invariant[1]); ans += i-mn; } cout << ans close } }
3
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int N = 11005; pair<int, long long> seg[2 * N]; int n; long long m, l[2 * N], x[2 * N]; int bit[4][2 * N], f[2 * N]; int cnt, tot; int sum(int x, int* s) { int ret = 0; while (x) { ret += s[x]; x -= x & -x; } if (ret != 0) return 1; return 0; } void add(int x, int k, int* s) { while (x < 2 * N) { s[x] += k; x += x & -x; } } struct blk { int t; long long l, r; blk(int t = 0, long long l = 0, long long r = 0) : t(0), l(0), r(0) {} } b[N]; typedef int Matrix[3][3]; Matrix I[9] = {{{1, 1, 0}, {1, 1, 1}, {0, 1, 1}}, {{0, 0, 0}, {1, 1, 1}, {0, 1, 1}}, {{1, 1, 0}, {0, 0, 0}, {0, 1, 1}}, {{0, 0, 0}, {0, 0, 0}, {0, 1, 1}}, {{1, 1, 0}, {1, 1, 1}, {0, 0, 0}}, {{0, 0, 0}, {1, 1, 1}, {0, 0, 0}}, {{1, 1, 0}, {0, 0, 0}, {0, 0, 0}}, {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}, ans; void mul1(Matrix A, Matrix B) { Matrix C; memset(C, 0, sizeof(C)); for (int i = 0; i < 3; i++) { for (int k = 0; k < 3; k++) { if (A[i][k] == 0) continue; for (int j = 0; j < 3; j++) { C[i][j] = (1LL * A[i][k] * B[k][j] + C[i][j]) % MOD; } } } memcpy(A, C, sizeof(C)); } void mul2(Matrix A, Matrix B) { Matrix C; memset(C, 0, sizeof(C)); for (int i = 0; i < 3; i++) { for (int k = 0; k < 3; k++) { C[i][0] = (1LL * A[i][k] * B[k][0] + C[i][0]) % MOD; } } memcpy(B, C, sizeof(C)); } void fastpow(Matrix m, long long n, Matrix r) { Matrix tm; memcpy(tm, m, sizeof(tm)); while (n) { if (n & 1) { mul2(tm, r); } n >>= 1; mul1(tm, tm); } } vector<pair<int, long long>> Seg; int main() { ans[0][0] = ans[2][0] = 0; ans[1][0] = 1; scanf("%d%lld", &n, &m); x[tot++] = m, x[tot++] = 0; for (int i = 0; i < n; i++) { scanf("%d%lld%lld", &b[i].t, &b[i].l, &b[i].r); x[tot++] = --b[i].l; x[tot++] = b[i].r; } sort(x, x + tot); tot = unique(x, x + tot) - x; for (int i = 0; i < n; i++) { b[i].l = upper_bound(x, x + tot, b[i].l) - x; b[i].r = upper_bound(x, x + tot, b[i].r) - x; add(b[i].l, 1, bit[b[i].t]); add(b[i].r, -1, bit[b[i].t]); } for (int i = 1; i < tot; i++) { f[i] = sum(i, bit[1]) + sum(i, bit[2]) * 2 + sum(i, bit[3]) * 4; l[i] = x[i] - x[i - 1]; } l[1]--; int cnt = 0; f[0] = l[0] = -1; for (int i = 1; i < tot; i++) { if (f[i] != f[i - 1]) { seg[++cnt].first = f[i]; seg[cnt].second += l[i]; } else { seg[cnt].second += l[i]; } } for (int i = 1; i <= cnt; i++) { fastpow(I[seg[i].first], seg[i].second, ans); } printf("%d\n", ans[1][0]); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { double r, x, y, xx, yy, d, ans; cin >> r >> x >> y >> xx >> yy; d = sqrt(pow(xx - x, 2) + pow(yy - y, 2)); r *= 2; ans = d / r; if (ans != (int)ans) { cout << ((int)ans + 1); } else { cout << ans; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, op, x, p[2] = {0, 1}, a[1000000]; int main() { ios::sync_with_stdio(false); cin >> n >> m; while (m--) { cin >> op; if (op == 2) { if (p[1] & 1) p[0]++, p[1]--; else p[0]--, p[1]++; } else cin >> x, (p[0] += x) %= n, (p[1] += x) %= n; } for (int i = (p[0] + n) % n, j = 1; j <= n; a[i] = j, (i += 2) %= n, j += 2) ; for (int i = (p[1] + n) % n, j = 2; j <= n; a[i] = j, (i += 2) %= n, j += 2) ; for (int i = 0; i < n; i++) cout << a[i] << ' '; return 0; }
3
#include "iostream" using namespace std; int main(){ int a; while(cin >> a){ if(a == 0) break; int count = 0; for(int i = 1; i <= a; i ++){ int buf = i; while(buf % 5 == 0 ){ buf /= 5; count ++; } } cout << count << endl; } return 0; }
0
#include <iostream> using namespace std; int main(){ int n,t;cin>>n>>t; int mn = 1001; for(int i = 0; n > i; i++){ int q,w;cin>>q>>w; if(t >= w)mn = min(mn,q); } if(mn == 1001)cout << "TLE" << endl; else cout << mn << endl; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define fr(i,n) for(int i=0;i<n;i++) const ll MOD=1e9+7; ll n,a[100010],h[100010]={}; ll ad(ll x,ll y){ return (x+y)%MOD; } ll mlt(ll x,ll y){ return (x*y)%MOD; } ll pwr(ll x,ll y){ ll pw[32]={},w=1; pw[0]=x; fr(i,31) pw[i+1]=mlt(pw[i],pw[i]); fr(i,32){ w=mlt(w,1+(y%2)*(pw[i]-1)); y/=2; } return w; } ll inv(ll x){ return pwr(x,MOD-2); } int main(){ cin>>n; fr(i,n) cin>>a[i]; fr(i,n) h[i+1]=ad(h[i],inv(i+1)); ll ans=0; fr(i,n) ans=ad(ans,mlt(ad(h[i+1],h[n-i]-1),a[i])); fr(i,n) ans=mlt(ans,i+1); cout<<ans<<endl; }
0
#include <bits/stdc++.h> bool prizes[1000001]; int main() { int count; scanf("%d", &count); for (int i = 0; i < count; i++) { int prize; scanf("%d", &prize); prizes[prize] = true; } int lastPrize1 = 1; for (int i = 2; i <= 500000; i++) { if (prizes[i]) { lastPrize1 = i; } } int lastPrize2 = 1000000; for (int i = 999999; i >= 500001; i--) { if (prizes[i]) { lastPrize2 = i; } } int maxTime = std::max(lastPrize1 - 1, 1000000 - lastPrize2); printf("%d", maxTime); return 0; }
2
//Bokan ga bokka--nn!! //Daily Lunch Special Tanoshii !! //これは、頭が悪く競プロが世界で一番できないHIR180が //IOI2014日本代表になるまでのN日間の記録である。 #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <cassert> #include <string> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> using namespace std; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; typedef long long ll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 500000000 #define f first #define s second #define rep(i,x) for(int i=0;i<x;i++) int d[3005]; vector<P>edge[3005]; int main() { srand((unsigned int)time(NULL)); int n,m,k; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); edge[a].pb(mp(b,c)); edge[b].pb(mp(a,c)); } fill(d+1,d+n+1,INF); priority_queue<P,vector<P>,greater<P> >que; for(int i=0;i<k;i++) { int x; scanf("%d",&x); d[x]=0; que.push(mp(d[x],x)); } while(!que.empty()) { P p=que.top(); que.pop(); if(p.f!=d[p.s]) continue; for(int i=0;i<edge[p.s].size();i++) { if(p.f+edge[p.s][i].s<d[edge[p.s][i].f]) { d[edge[p.s][i].f]=p.f+edge[p.s][i].s; que.push(mp(d[edge[p.s][i].f],edge[p.s][i].f)); } } } int ret=0; for(int i=1;i<=n;i++) { for(int j=0;j<edge[i].size();j++) { ret=max(ret,d[i]+d[edge[i][j].f]+edge[i][j].s); } } printf("%d\n",(ret+1)/2); }
0
#include <bits/stdc++.h> using namespace std; using namespace std; using pll = pair<long long, long long>; map<long long, long long> M; long long cal(long long x, long long y, long long cost) { long long res = 0; while (x != y) { if (x > y) swap(x, y); M[y] += cost; res += M[y]; y >>= 1; } return max(res, 0LL); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int q; cin >> q; int id = 0; for (int i = 0; i < q; i++) { long long type, x, y; long long wt; cin >> type >> x >> y; if (type == 1) { cin >> wt; cal(x, y, wt); } else { cout << cal(x, y, 0LL) << endl; } } }
3
#include <bits/stdc++.h> using namespace std; char s[500000]; int main() { long long x, y; int n; while (~scanf("%d%lld%lld", &n, &x, &y)) { scanf("%s", s); int len = 0; int flag = 0; for (int i = 0; i < n; i++) { if (s[i] == '0') flag = 1; else if (s[i] == '1' && flag == 1) len++, flag = 0; } if (flag) len++; long long ans = min(len * y, y + x * (len - 1)); if (len == 0) ans = 0; printf("%lld\n", ans); } return 0; }
1
#include <bits/stdc++.h> using namespace std; short n, m; char a[1111][1111]; short kx[4] = {1, -1, 0, 0}, ky[4] = {0, 0, 1, -1}; bool vis[4][1001][1001]; short d[4][1001][1001]; bool sat; short dfs(short z, short u, short v) { if (vis[z][u][v]) return d[z][u][v]; vis[z][u][v] = true; if (a[u][v] == '*') d[z][u][v] = 1; else return 0; short uu = u + kx[z], vv = v + ky[z]; if (uu > 0 && vv > 0 && uu <= n && vv <= m) d[z][u][v] += dfs(z, uu, vv); return d[z][u][v]; } int g[1111][1111], h[1111][1111]; vector<short> d1, d2, d3; bool kt() { short u, v, z; for (int i = 0, ii = d1.size(); i < ii; i++) { u = d1[i], v = d2[i], z = d3[i]; g[u - z][v]++, g[u + z + 1][v]--; h[u][v - z]++, h[u][v + z + 1]--; } for (short i = 1; i <= n; i++) for (short j = 1; j <= m; j++) { g[i][j] += g[i - 1][j]; h[i][j] += h[i][j - 1]; u = g[i][j] + h[i][j]; if (u == 0) { if (a[i][j] == '*') return true; } else if (a[i][j] == '.') return true; } return false; } int main() { scanf("%hd%hd", &n, &m); for (short i = 1; i <= n; i++) for (short j = 1; j <= m; j++) scanf(" %c", &a[i][j]); short tmp; for (short i = 1; i <= n; i++) for (short j = 1; j <= m; j++) { if (a[i][j] == '*') sat = true; else continue; tmp = m + n; for (short z = 0; z < 4; z++) tmp = min(tmp, dfs(z, i, j)); tmp--; if (tmp > 0) d1.push_back(i), d2.push_back(j), d3.push_back(tmp); } if (sat) { if (d1.empty()) puts("-1"); else { if (kt()) puts("-1"); else { printf("%d\n", d1.size()); for (int i = 0, ii = d1.size(); i < ii; i++) printf("%hd %hd %hd\n", d1[i], d2[i], d3[i]); } } } else puts("0"); }
5
#include <bits/stdc++.h> using namespace std; const int MX = 500500; string s; int cnt[30][5400][30], ans; int main() { cin >> s; int n = s.length(); s = s + s; for (int i = 0; i < n; ++i) for (int j = i; j < n + i; ++j) cnt[s[i] - 'a'][j - i][s[j] - 'a']++; for (int i = 0; i < 26; ++i) { int tmp, mx = 0; for (int j = 0; j < n; ++j) { tmp = 0; for (int k = 0; k < 26; ++k) if (cnt[i][j][k] == 1) tmp++; mx = max(mx, tmp); } ans += mx; } cout << fixed << setprecision(20) << (double)ans / (double)n << endl; return 0; }
5
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; using ld = long double; const ld eps = 1e-9; int main() { while (true) { int W,H;cin>>W>>H; if(H==0)break; string command; int dx[4] = { -1,0,1,0 }; int dy[4] = { 0,1,0,-1 }; int way=3; int nx(0); int ny(H-1); while (cin >> command) { if (command == "STOP") { cout << nx + 1 <<" "<< H - ny << endl; break; } else if (command == "RIGHT") { way=(way+3)%4; } else if (command == "LEFT") { way=(way+1)%4; } else if (command == "FORWARD") { int k;cin>>k; while (k--) { const int nextx=nx+dx[way]; const int nexty=ny+dy[way]; if (nextx >= 0 && nextx < W&&nexty >= 0 && nexty < H) { nx=nextx; ny=nexty; } } } else if (command == "BACKWARD") { int k; cin >> k; way=(way+2)%4; while (k--) { const int nextx = nx + dx[way]; const int nexty = ny + dy[way]; if (nextx >= 0 && nextx < W&&nexty >= 0 && nexty < H) { nx = nextx; ny = nexty; } } way = (way + 2) % 4; } else { assert(false); } } } return 0; }
0
#include <cstdio> #include <iostream> #include <cstring> #include <string> using namespace std; const int maxn=5*10e5+10; int pre[maxn],suf[maxn]; void pre_fix(string s,int p[]){ p[0]=-1; int len=0,i=1; while(i<s.size()){ if(s[i]==s[len] || len==-1){ p[i+1]=len+1; i++,len++; } else len=p[len]; } } int main(){ string s1; while(cin>>s1){ string s2=""; int len=s1.size(); for(int i=len-1;i>=0;i--) s2+=s1[i]; pre_fix(s1,pre); pre_fix(s2,suf); bool flag=true; for(int i=1;i<len;i++) if(s1[i]!=s1[i-1]){ flag=false; break; } if(flag){ printf("%d\n%d",len,1); continue; } if(pre[len]==0 || len%(len-pre[len])) printf("%d\n%d",1,1); else{ printf("%d\n",2); int ans=0; for(int i=0;i<len;i++){ int k=len-i-1; if( (pre[i+1]==0 || (i+1)%(i+1-pre[i+1]) ) && (suf[k]==0 || k%(k-suf[k]))) ans++; } printf("%d",ans); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; ++i) { cin >> a[i]; } vector<vector<bool>> ops(N + 1, vector<bool>(N, false)); vector<pair<int, int>> intervals = {{0, N}}; for (int i = 0; i < N; ++i) { int ones = a[i]; int zeros = N + 1 - ones; vector<pair<int, int>> next_intervals; int j = 0; while (j < intervals.size() && intervals[j].second == intervals[j].first) { if (ones > 1) { ops[intervals[j].first][i] = true; ones--; } else { zeros--; } next_intervals.push_back(intervals[j]); j++; } int len = intervals[j].second - intervals[j].first + 1; int give_zeros = min(zeros, len - 1); int give_ones = len - give_zeros; assert(give_ones <= ones); for (int k = 0; k < give_ones; ++k) { ops[intervals[j].first + k][i] = true; } next_intervals.push_back( {intervals[j].first, intervals[j].first + give_ones - 1}); next_intervals.push_back( {intervals[j].first + give_ones, intervals[j].second}); ones -= give_ones; zeros -= give_zeros; for (int k = j + 1; k < intervals.size(); ++k) { for (int m = intervals[k].first; m <= intervals[k].second; ++m) { if (ones > 0) { ops[m][i] = true; ones--; } else { zeros--; } } next_intervals.push_back(intervals[k]); } intervals = next_intervals; } int i = 0; while (i < ops.size()) { bool allzero = true; for (int j = 0; j < N; ++j) if (ops[i][j]) allzero = false; if (allzero) { ops.erase(ops.begin() + i); } else { i++; } } cout << ops.size() << endl; for (int i = 0; i < ops.size(); ++i) { for (int j = 0; j < ops[i].size(); ++j) { cout << (ops[i][j] ? 1 : 0); } cout << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, f[1000005], cnt[105], dem, res; vector<int> P, X; int d, mul[100005]; void light_on(int &mask, int i) { mask |= 1 << (i); } int pt(int x) { int mask = 0; for (int i = 0; i < P.size(); ++i) { if (x % P[i] == 0) { int D = 0; while (x % P[i] == 0) { D++; x /= P[i]; } if (D % 2) light_on(mask, i); } } return mask; } bool ngto(int x) { for (int i = 2; i * i <= x; ++i) if (x % i == 0) return 0; return 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 2; i <= 70; ++i) if (ngto(i)) P.push_back(i); int G = (1 << (P.size())) - 1; for (int i = 1; i <= n; ++i) { int x; cin >> x; if (!cnt[x]) { X.push_back(x); } ++cnt[x]; } mul[0] = 1; for (int i = 1; i <= n; ++i) mul[i] = (1ll * mul[i - 1] * 2) % 1000000007; f[0] = 1; for (int i = 0; i < X.size(); ++i) { int x = X[i]; int s = cnt[x]; int mask = pt(x); int nw[1000005]; for (int j = 0; j <= G; ++j) nw[j] = 0; for (int j = 0; j <= G; ++j) { nw[j] = (nw[j] + (1ll * mul[s - 1] * f[j]) % 1000000007) % 1000000007; nw[j] = (nw[j] + (1ll * mul[s - 1] * f[j ^ mask]) % 1000000007) % 1000000007; } swap(f, nw); } cout << f[0] - 1; }
3
#include <bits/stdc++.h> using namespace std; void perdi() { puts("-1"); exit(0); } int d12, d13, d14, d23, d24, d34; char r[4][1 << 20]; int n, xx, yy; bool doit() { int x, y, z; if ((d14 + d24 - d12) % 2) return false; z = (d14 + d24 - d12) / 2; if ((-d24 + d12 - d34 + xx + yy) % 2) return false; x = (-d24 + d12 - d34 + xx + yy) / 2; y = d14 - x - z; if (x < 0 || y < 0 || z < 0 || x + y > d12 || x > xx || z > yy || y > d12 - xx) return false; for (int i = 0, ThxDem = x; i < ThxDem; ++i) r[3][i]++; for (int i = xx, ThxDem = xx + y; i < ThxDem; ++i) r[3][i]++; for (int i = d12, ThxDem = d12 + z; i < ThxDem; ++i) r[3][i]++; return true; } int main() { scanf("%d%d%d%d%d%d", &d12, &d13, &d14, &d23, &d24, &d34); memset(r, 'a', sizeof(r)); for (int i = 0, ThxDem = d12; i < ThxDem; ++i) r[1][i]++; if ((d13 + d23 - d12) % 2) perdi(); yy = (d13 + d23 - d12) / 2; xx = d13 - yy; if (xx < 0 || yy < 0 || xx > d12) perdi(); for (int i = 0, ThxDem = xx; i < ThxDem; ++i) r[2][i]++; for (int i = d12, ThxDem = d12 + yy; i < ThxDem; ++i) r[2][i]++; n = d12 + yy; while (d14 >= 0 && d24 >= 0 && d34 >= 0 && !doit()) { d14--; d24--; d34--; r[3][n++]++; } if (d14 < 0 || d24 < 0 || d34 < 0) perdi(); printf("%d\n", n); for (int i = 0, ThxDem = 4; i < ThxDem; ++i) r[i][n] = 0, puts(r[i]); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t, n, m, l, r, i; long long sum; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); sum = 0; for (i = 0; i < n; i++) { if (sum + i + 1 >= m) break; sum += i + 1; } l = i + 2; r = m - sum; for (i = n; i >= 1; i--) printf(i == l || i == r ? "b" : "a"); printf("\n"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; void read_file(bool outToFile = true) {} int n; const int nMax = 100000 + 2 + 9; double P[nMax]; double dp[nMax]; double ans; int main() { read_file(); while (scanf("%d", &n) != EOF) { ans = 0; for (int i = 1; i <= n; i++) scanf("%lf", &P[i]), ans += 1 * P[i]; dp[1] = 0; for (int i = 2; i <= n; i++) { dp[i] = 2 * P[i] * P[i - 1] + P[i] * dp[i - 1]; ans += dp[i]; } printf("%.9lf\n", ans); } }
4
#include <bits/stdc++.h> using namespace std; const int N = 100100; char a[N]; int b[N], c[N], d[N], e[N], sa[N], height[N], n, m, bg[N]; int rmq[N][22]; char p[N], ss[N]; int cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } void da() { int i, j, p, *x = c, *y = d, *t; for (i = 0; i < m; i++) b[i] = 0; for (i = 0; i < n; i++) b[x[i] = a[i]]++; for (i = 1; i < m; i++) b[i] += b[i - 1]; for (i = n - 1; i >= 0; i--) sa[--b[x[i]]] = i; for (j = 1, p = 1; p < n; j *= 2, m = p) { for (p = 0, i = n - j; i < n; i++) y[p++] = i; for (i = 0; i < n; i++) if (sa[i] >= j) y[p++] = sa[i] - j; for (i = 0; i < n; i++) e[i] = x[y[i]]; for (i = 0; i < m; i++) b[i] = 0; for (i = 0; i < n; i++) b[e[i]]++; for (i = 1; i < m; i++) b[i] += b[i - 1]; for (i = n - 1; i >= 0; i--) sa[--b[e[i]]] = y[i]; for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } } void callheight() { int i, j, k = 0; for (i = 1; i < n; i++) b[sa[i]] = i; n--; for (i = 0; i < n; height[b[i++]] = k) for (k ? k-- : 0, j = sa[b[i] - 1]; a[i + k] == a[j + k]; k++) ; } void initrmq(int n) { int i, k; for (i = 2; i <= n; ++i) rmq[i][0] = height[i]; for (k = 1; (1 << k) <= n; ++k) { for (i = 2; i + (1 << k) - 1 <= n; ++i) { rmq[i][k] = min(rmq[i][k - 1], rmq[i + (1 << (k - 1))][k - 1]); } } } int lcp(int aa, int bb) { aa = b[aa], bb = b[bb]; if (aa > bb) swap(aa, bb); ++aa; int k = (int)(log((bb - aa + 1) * 1.0) / log(2.0)); return min(rmq[aa][k], rmq[bb - (1 << k) + 1][k]); } void print() { int i; cout << "length : " << n << endl; cout << "s[n] = '$' " << endl; cout << "sorted suffix index(0 based) : " << endl; for (int i = 0; i <= n; i++) cout << sa[i] << " , "; cout << endl; cout << "rank of suffix " << endl; for (int i = 0; i <= n; i++) cout << b[i] << " , "; cout << endl; cout << "LCP between i and i-1 " << endl; for (int i = 0; i <= n; i++) cout << height[i] << " , "; cout << endl; } char s[2005]; int lst, md; int main() { int i, j, k, q; scanf("%s", s); strcat(a, s); strcat(a, "}"); md = strlen(a); scanf("%s", s); strcat(a, s); strcat(a, "$"); n = strlen(a); lst = n; m = 128; da(); callheight(); initrmq(n); int ans = n + 5; for (i = 1; i <= n + 1; i++) { int x = sa[i - 1]; int y = sa[i]; int z = height[i]; if (y < x) swap(x, y); if (z && (x < md - 1 && y >= md)) { if (height[i - 1] < z && height[i + 1] < z) { int xx = max(height[i - 1], height[i + 1]); if (z >= xx + 1) ans = min(ans, xx + 1); } } } if (ans == n + 5) ans = -1; printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[300005], n, flag[300005]; int main() { int i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]); printf("%d ", 1); int end = n, sum = 1; for (i = 1; i <= n; i++) { flag[a[i]] = 1; sum++; if (a[i] == end) { for (j = end; j >= 1; j--) { if (flag[j]) sum--; if (flag[j] == 0) break; } end = j; } printf("%d ", sum); } printf("\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main(){ int64_t a, b, c, d, _; cin >> a >> b >> _ >> c >> d >> _ >> _; int64_t ans = a/2*2 + b + c/2*2 + d/2*2; if(a && c && d){ a--; c--; d--; int64_t ans2 = a/2*2 + b + c/2*2 + d/2*2 + 3; ans = max(ans, ans2); } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; void pv(vector<long long int> arr, long long int size) { for (long long int i = 0; i < size; i++) { cout << arr[i] << ' '; } } void gv(vector<long long int> &arr) { long long int size = arr.size(); for (long long int i = 0; i < size; i++) { cin >> arr[i]; } } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; vector<long long int> arr(n); gv(arr); sort(arr.rbegin(), arr.rend()); deque<long long int> dq; dq.push_back(arr[0]); for (long long int i = 1; i < n; i++) { if (i % 2 == 1) { dq.push_back(arr[i]); } else { dq.push_front(arr[i]); } } for (long long int i = 0; i < n; i++) { cout << dq[i]; if (i != n - 1) cout << " "; } return 0; }
3
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> #include <cfloat> #include <ctime> #include <cassert> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> #include <numeric> #include <list> using namespace std; #ifdef _MSC_VER #define __typeof__ decltype template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; } #endif #define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define CLEAR(arr, val) memset(arr, val, sizeof(arr)) #define rep(i, n) for (int i = 0; i < n; ++i) template <class T> void max_swap(T& a, const T& b) { a = max(a, b); } template <class T> void min_swap(T& a, const T& b) { a = min(a, b); } typedef long long ll; typedef pair<int, int> pint; const double EPS = 1e-8; const double PI = acos(-1.0); const int dx[] = { 0, 1, 0, -1 }; const int dy[] = { 1, 0, -1, 0 }; bool valid_pos(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } const int mod = 100000007; const int MAX = 100000; void dpru(int* dp, int period) { static int aperi_dp[MAX + 10]; fill(aperi_dp, aperi_dp + (MAX + 10), 0); // aperi_dp[i] = sum[j = i-period .. i-1](aperi_dp[i - j]) aperi_dp[0] = 1; for (int i = 0; i < MAX; ++i) for (int j = 1; j <= period; ++j) (aperi_dp[i + j] += aperi_dp[i]) %= mod; fill(dp, dp + (MAX + 10), 0); // dp[i] = aperi_dp[i] + dp[i - period] // dp[0] = 0 for (int i = 1; i <= MAX; ++i) { (dp[i] += aperi_dp[i]) %= mod; (dp[i + period] += dp[i]) %= mod; } } int main() { ios::sync_with_stdio(false); static int dp3[MAX + 100], dp5[MAX + 100]; dpru(dp3, 3); dpru(dp5, 5); for (string s; cin >> s, s[0] != '#'; ) { s += '@'; ll res = 1; for (int i = 0, c = 0; s[i] != '@'; ++i) { ++c; if (s[i] != s[i + 1]) { int* dp; if (s[i] == '8' || s[i] == '0') dp = dp3; else dp = dp5; (res *= dp[c]) %= mod; c = 0; } } cout << res << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, M = 3010, mod = 998244353; long long a[N], w[N]; long long dp[M][M]; long long inverse(long long x); long long pwr(long long x, long long e); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; long long bad_wt = 0, good_wt = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { cin >> w[i]; if (a[i]) { good_wt += w[i]; } else { bad_wt += w[i]; } } for (int j = 0; j <= m and j <= bad_wt; ++j) { dp[m][j] = j; } long long tot_wt = good_wt + bad_wt; for (int i = m - 1; i >= 0; --i) { for (int j = 0; j <= m and j <= bad_wt; ++j) { long long p_bad = (bad_wt - j) * inverse(tot_wt - j + (i - j)); p_bad %= mod; long long p_good = (mod + 1 - p_bad) % mod; dp[i][j] = p_bad * dp[i + 1][j + 1] + p_good * dp[i + 1][j]; dp[i][j] %= mod; } } long long bad_visits = dp[0][0], good_visits = (m - bad_visits + mod) % mod; for (int i = 1; i <= n; ++i) { long long ans = w[i]; if (a[i]) { ans *= good_visits; ans %= mod; ans *= inverse(good_wt); ans %= mod; ans += w[i]; ans %= mod; } else { ans *= bad_visits; ans %= mod; ans *= inverse(bad_wt); ans %= mod; ans = (w[i] - ans + mod) % mod; } cout << ans << '\n'; } } long long inverse(long long x) { return pwr(x, mod - 2); } long long pwr(long long x, long long e) { long long res = 1; while (e) { if (e & 1) { res = (res * x) % mod; } x = (x * x) % mod; e /= 2; } return res; }
3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const long long N = 1e5; const long long mod = 1e9 + 7; const long double eps = 1E-7; long long n, mx, mn = 1e9, cnt, m, ans; void solve() { string s; cin >> s; string s1, s2; for (int i = 0; i < s.size(); i++) { if (s[i] % 2 == 0) s1 += s[i]; else s2 += s[i]; } merge(s1.begin(), s1.end(), s2.begin(), s2.end(), s.begin()); cout << s << endl; } int main() { ios_base::sync_with_stdio(0); long long T; cin >> T; while (T--) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; long long H[20000000], A[4005]; char s[4005]; long long bsearch(long long x, long long n) { int l = 0, r = n + 1, mid; while (r - l > 1) { mid = l + r >> 1; if (H[mid] <= x) l = mid; else r = mid; } return l; } int main() { int a, n, i, num, l, r, x, t; long long ans; scanf("%d%s", &a, s + 1), n = strlen(s + 1); for (i = 1; i <= n; i++) A[i] = s[i] - '0'; num = 0; for (l = 1; l <= n; l++) { x = 0; for (r = l; r <= n; r++) x += A[r], H[++num] = x; } sort(H + 1, H + 1 + num); ans = 0; for (l = 1; l <= n; l++) { x = 0; for (r = l; r <= n; r++) { x += A[r]; if (!x) { if (!a) ans += num; continue; } else { if (a % x) continue; t = a / x; } ans += bsearch(t, num) - bsearch(t - 1, num); } } printf("%I64d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 3; int n, m, k, ans, a[N], f[N]; set<int> s; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { while (!s.empty() && *s.begin() < a[i] - m + 1) s.erase(*s.begin()); s.insert(a[i]); while (s.size() >= k) { ans++; s.erase(*s.rbegin()); } } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e18; const int maxn = 3e5 + 10; const int mod = 1e9 + 7; long long n, m; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; if (n == 1 || m == 1) { if (m == 1) swap(n, m); if (m % 6 == 0) cout << m; if (m % 6 == 1) cout << m - 1; if (m % 6 == 2) cout << m - 2; if (m % 6 == 3) cout << m - 3; if (m % 6 == 4) cout << m - 2; if (m % 6 == 5) cout << m - 1; return 0; } if (n == 2 || m == 2) { if (m == 2) swap(n, m); if (m == 2) return cout << 0, 0; if (m == 3) return cout << 4, 0; if (m == 7) return cout << n * m - 2, 0; cout << n * m; return 0; } long long ans = n * m; if (ans % 2 == 1) ans--; cout << ans; }
2
#include <bits/stdc++.h> using namespace std; double n; int main() { cin >> n; for (double aa = 1; aa <= 10; aa++) for (double h = 1; h <= 10; h++) if (abs(aa * h / 2 / sqrt(aa * aa / 4 + h * h) - n) <= 0.0001) return cout << aa << ' ' << h << '\n', 0; return 0; }
5
#include <bits/stdc++.h> int a[105][105]; int main() { int n, i, j, k, t, h; int temp, ans; while (scanf("%d", &n) != EOF) { temp = 0; ans = 0; k = 0; t = 0; memset(a, 0, sizeof(a)); for (h = 0; h < n; h++) { scanf("%d%d", &i, &j); a[i][j] = 1; if (i > k) k = i; if (j > t) t = j; } for (i = 0; i <= k; i++) { for (j = 0; j <= t; j++) { if (a[i][j]) { temp++; break; } } } for (j = 0; j <= t; j++) { for (i = 0; i <= k; i++) { if (a[i][j]) { ans++; break; } } } printf("%d\n", ans > temp ? temp : ans); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m, t; cin >> n >> m; int visited[m + 1]; for (int i = 0; i <= m; i++) { visited[i] = 0; } for (int i = 0; i < n; i++) { int k; cin >> t; for (int j = 0; j < t; j++) { cin >> k; visited[k] = 1; } } for (int i = 1; i <= m; i++) { if (visited[i] == 0) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
1
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") long long dp[4040][4040], c[4040][4040]; void init() { dp[0][0] = dp[1][1] = 1; for (int i = 1; i < 4040; i++) { dp[i][1] = dp[i - 1][i - 1]; for (int j = 2; j <= i; j++) dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % 1000000007; } for (int i = 1; i < 4040; i++) { c[i][0] = c[i][i] = 1; for (int j = 1; j < i; j++) { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % 1000000007; } } } int main() { int n; init(); while (~scanf("%d", &n)) { long long ans = 0; for (int i = 0; i < n; i++) { ans += c[n][i] * dp[i][i]; ans %= 1000000007; } cout << ans << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; bool dfs(unsigned long long a, unsigned long long b) { if (a > b) { swap(a, b); } if (a == 0) { return false; } if (dfs(a, b % a)) { if (((b / a) % (a + 1)) % 2 == 0) { return true; } return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int i = 0; i < t; i++) { unsigned long long a, b; cin >> a >> b; if (dfs(a, b)) { cout << "First\n"; } else { cout << "Second\n"; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; map<string, int> M; int graph[10][10]; void init() { M["Anka"] = 0; M["Chapay"] = 1; M["Troll"] = 2; M["Dracul"] = 3; M["Snowy"] = 4; M["Hexadecimal"] = 5; M["Cleo"] = 6; } int aabs(int x) { if (x < 0) x = -x; return x; } int main() { int n; string str, a, b, junk; init(); memset(graph, 0, sizeof(graph)); cin >> n; getchar(); for (int i = (0); i < (n); i++) { getline(cin, str); istringstream ss(str); ss >> a >> junk >> b; graph[M[a]][M[b]] = 1; } int TA, TB, TC; cin >> TA >> TB >> TC; int best = -1, mindiff = 2000000000 + 7; for (int x = (1); x < (8); x++) { for (int y = (x + 1); y < (7); y++) { int z = 7; { int L = x, M = y - x, N = z - y; int Ga = TA / L, Gb = TB / M, Gc = TC / N; int best2, locbest = -1, cnt; best2 = max(aabs(Ga - Gb), max(aabs(Gb - Gc), aabs(Gc - Ga))); vector<int> V; for (int i = (0); i < (7); i++) V.push_back(i); do { cnt = 0; for (int i = (0); i < (x); i++) for (int j = (0); j < (x); j++) if (i != j) if (graph[V[i]][V[j]] == 1) cnt++; for (int i = (x); i < (y); i++) for (int j = (x); j < (y); j++) if (i != j) if (graph[V[i]][V[j]] == 1) cnt++; for (int i = (y); i < (z); i++) for (int j = (y); j < (z); j++) if (i != j) if (graph[V[i]][V[j]] == 1) cnt++; if (cnt > locbest) locbest = cnt; } while (next_permutation(V.begin(), V.end())); if (best2 < mindiff) mindiff = best2, best = locbest; else if (best2 == mindiff) best = max(locbest, best); } } } cout << mindiff << " " << best << endl; }
3
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, w = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * w; } inline long long min(long long x, long long y) { return x < y ? x : y; } inline long long max(long long x, long long y) { return x > y ? x : y; } struct node { long long to, nxt; } edge[200005 << 1]; long long n, tot; long long w[200005], h[200005], head[200005], f[200005][2]; vector<long long> tmp[200005]; inline void add(long long u, long long v) { edge[++tot].nxt = head[u]; edge[tot].to = v; head[u] = tot; } inline void addedge(long long u, long long v) { add(u, v), add(v, u); } void dfs(long long u, long long fath) { long long res = 0, otd = 0, ind = 0; for (long long i = head[u]; i; i = edge[i].nxt) { long long v = edge[i].to; if (v == fath) continue; dfs(v, u); if (h[v] == h[u]) tmp[u].push_back(f[v][1] - f[v][0]), res += f[v][0], otd++; else if (h[v] > h[u]) ind++, res += f[v][1]; else otd++, res += f[v][0]; } sort(tmp[u].begin(), tmp[u].end()); for (long long i = 0; i <= tmp[u].size(); i++) { f[u][0] = min(f[u][0], res + max(otd, ind + (u != 1)) * w[u]); f[u][1] = min(f[u][1], res + max(otd + (u != 1), ind) * w[u]); otd--, ind++; if (i < tmp[u].size()) res += tmp[u][i]; } } signed main(void) { n = read(); memset(f, 0x3f, sizeof(f)); for (long long i = 1; i <= n; i++) w[i] = read(); for (long long i = 1; i <= n; i++) h[i] = read(); for (long long i = 1; i < n; i++) { long long x = read(), y = read(); addedge(x, y); } dfs(1, 1); printf("%lld\n", min(f[1][0], f[1][1])); return 0; }
4
#include<bits/stdc++.h> using namespace std; int main(){ long long a,b,n; cin>>a>>b>>n; long long x=min(b-1,n); cout<<(a*x)/b - a*(x/b)<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef ostringstream OSS; typedef istringstream ISS; typedef long long LL; typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<string> VS; typedef vector<VS> VVS; typedef vector<bool> VB; typedef vector<VB> VVB; typedef vector<VVB> V3B; typedef vector<V3B> V4B; typedef vector<PII> VPII; #define Y first #define X second #define MP make_pair #define PB push_back #define EB emplace_back #define ALL(x) (x).begin(),(x).end() #define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY)) #define DUMP( x ) cerr << #x << " = " << ( x ) << endl template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; }; template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); }; const int INF = 0x3f3f3f3f; const LL INFL = 0x3f3f3f3f3f3f3f3fLL; const int DX[2][4]={{1,0,-1,0}, {-1, 0, 1, 0}}; const int DY[2][4]={{0,-1,0,1}, {0, -1, 0, 1}}; int W, H; VVS tss; PII find_start(VS &ts, char c) { for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { if (ts[y][x] == c) { return MP(y, x); } } } exit(1); } bool solve() { queue<vector<PII>> qs; V4B dones(H, V3B(W, VVB(H, VB(W)))); qs.push({find_start(tss[0], 'L'), find_start(tss[1], 'R')}); while (qs.size()) { auto q = qs.front(); PII a = q[0]; PII b = q[1]; qs.pop(); if (dones[a.Y][a.X][b.Y][b.X]) { continue; } else { dones[a.Y][a.X][b.Y][b.X] = true; } bool g1 = tss[0][a.Y][a.X] == '%'; bool g2 = tss[1][b.Y][b.X] == '%'; if (g1 && g2) { // ????????°??? return true; } else if (g1 ^ g2) { // ????????°??? continue; } for (int di = 0; di < 4; di++) { VPII ns(2); for (int i = 0; i < 2; i++) { int x = q[i].X + DX[i][di]; int y = q[i].Y + DY[i][di]; if (!RANGE(x, y, W, H) || tss[i][y][x] == '#') { x = q[i].X; y = q[i].Y; } ns[i].X = x; ns[i].Y = y; } qs.push(ns); } } return false; } int main(void) { while (cin >> W >> H, W) { tss = VVS(2, VS(H)); for (int y = 0; y < H; y++) { cin >> tss[0][y] >> tss[1][y]; } cout << (solve() ? "Yes" : "No") << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int dx[2] = {0, 1}, dy[2] = {1, 0}; map<char, int> m; int main() { int n; string s; cin >> n >> s; int x = 0, y = 0, cur = -1, ans = 0; m['U'] = 1; m['R'] = 0; for (int i = 0; i < n; i++) { char c = s[i]; x += dx[m[c]]; y += dy[m[c]]; if (x < y) { if (cur == 0) ans++; cur = 1; } else if (x > y) { if (cur == 1) ans++; cur = 0; } } cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; int numbers[100 + 8], saved[100 + 8]; int binReverse(int x) { char tmp[100 + 8]; memset(tmp, 0, sizeof tmp); int len = 0; while (x > 0) { if (x % 2 == 0) tmp[len++] = '0'; else tmp[len++] = '1'; x /= 2; } while (len < 8) tmp[len++] = '0'; int result = 0, step = 1; for (int i = len - 1; i >= 0; i--) { result += step * ((int)tmp[i] - 48); step *= 2; } return result; } int main() { char line[100 + 8]; gets(line); memset(saved, 0, sizeof saved); memset(numbers, 0, sizeof numbers); int len = strlen(line); for (int i = 0; i < len; i++) { saved[i + 1] = binReverse((int)line[i]); int tmp = (saved[i] - saved[i + 1]) % 256; if (tmp < 0) tmp += 256; printf("%d\n", tmp); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int A[1005][1005]; int main() { int N, M; cin >> N >> M; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { A[i][j] = 0; } } for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; A[x][y] = 1; A[y][x] = 1; } int pivot = 0; for (int i = 1; i <= N; i++) { int flag = 0; for (int j = 1; j <= N; j++) { if (A[i][j] == 1) { flag = 1; break; } } if (flag == 0) { pivot = i; } } cout << N - 1 << endl; for (int i = 1; i <= N; i++) { if (i != pivot) { cout << pivot << " " << i << endl; } } }
2
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int W, H, N; int maxh[101010]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> W >> H >> N; rep(i, 0, N) { int x, y; cin >> x >> y; chmax(maxh[x], y); } rrep(x, W - 2, 0) chmax(maxh[x], maxh[x + 1]); int ans = inf; rep(x, 0, W) chmin(ans, x + maxh[x + 1]); cout << ans << endl; }
0
#include <bits/stdc++.h> namespace IO { static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23; inline char myGetchar() { static char buf[IN_BUF], *ps = buf, *pt = buf; if (ps == pt) { ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin); } return ps == pt ? EOF : *ps++; } template <typename T> inline bool read(T &x) { bool op = 0; char ch = IO::myGetchar(); x = 0; for (; !isdigit(ch) && ch != EOF; ch = IO::myGetchar()) { op ^= (ch == '-'); } if (ch == EOF) { return false; } for (; isdigit(ch); ch = IO::myGetchar()) { x = x * 10 + (ch ^ '0'); } if (op) { x = -x; } return true; } inline int readStr(char *s) { int n = 0; char ch = IO::myGetchar(); for (; isspace(ch) && ch != EOF; ch = IO::myGetchar()) ; for (; !isspace(ch) && ch != EOF; ch = IO::myGetchar()) { s[n++] = ch; } s[n] = '\0'; return n; } inline void myPutchar(char x) { static char pbuf[OUT_BUF], *pp = pbuf; struct _flusher { ~_flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); } }; static _flusher outputFlusher; if (pp == pbuf + OUT_BUF) { fwrite(pbuf, 1, OUT_BUF, stdout); pp = pbuf; } *pp++ = x; } template <typename T> inline void print_(T x) { if (x == 0) { IO::myPutchar('0'); return; } std::vector<int> num; if (x < 0) { IO::myPutchar('-'); x = -x; } for (; x; x /= 10) { num.push_back(x % 10); } while (!num.empty()) { IO::myPutchar(num.back() ^ '0'); num.pop_back(); } } template <typename T> inline void print(T x, char ch = '\n') { print_(x); IO::myPutchar(ch); } inline void printStr_(const char *s, int n = -1) { if (n == -1) { n = strlen(s); } for (int i = 0; i < n; ++i) { IO::myPutchar(s[i]); } } inline void printStr(const char *s, int n = -1, char ch = '\n') { printStr_(s, n); IO::myPutchar(ch); } } // namespace IO using namespace IO; const int P = 998244353, I2 = (P + 1) >> 1; int n, m, fac[55], inv[55]; std::vector<long long> a, b, c; std::vector<int> id; std::vector<int> f, g; int qpow(int a, int b) { int s = 1; for (; b; b >>= 1) { if (b & 1) { s = 1ll * s * a % P; } a = 1ll * a * a % P; } return s; } bool insert(long long x) { for (int i = m - 1; ~i; --i) { if (x >> i & 1) { if (!a[i]) { a[i] = x; return true; } else { x ^= a[i]; } } } return false; } namespace BF { void dfs(int k, long long now) { if (k == (int)b.size()) { ++f[__builtin_popcountll(now)]; return; } dfs(k + 1, now); dfs(k + 1, now ^ b[k]); } } // namespace BF void dfs(int k, long long now) { if (k == (int)id.size()) { ++g[__builtin_popcountll(now)]; return; } dfs(k + 1, now); dfs(k + 1, now ^ (1ll << id[k]) ^ c[id[k]]); } int C(int n, int m) { if (m < 0 || m > n) { return 0; } return 1ll * fac[n] * inv[m] % P * inv[n - m] % P; } int main() { read(n), read(m); a.resize(m); for (int i = 0; i < n; ++i) { long long x; read(x); insert(x); } for (int i = m - 1; ~i; --i) { if (a[i]) { for (int j = m - 1; j > i; --j) { if (a[j] >> i & 1) { a[j] ^= a[i]; } } } } for (int i = 0; i < m; ++i) { if (a[i]) { b.push_back(a[i]); } else { id.push_back(i); } } if ((int)b.size() < m / 2) { f.resize(m + 1); BF::dfs(0, 0); for (int i = 0; i <= m; ++i) { f[i] = 1ll * f[i] * qpow(2, n - (int)b.size()) % P; print(f[i], ' '); } return 0; } c.resize(m); for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { c[j] |= (long long)(a[i] >> j & 1) << i; } } g.resize(m + 1); dfs(0, 0); fac[0] = 1; for (int i = 1; i <= m; ++i) { fac[i] = 1ll * fac[i - 1] * i % P; } inv[m] = qpow(fac[m], P - 2); for (int i = m; i; --i) { inv[i - 1] = 1ll * inv[i] * i % P; } std::vector<std::vector<int>> F(m + 1, std::vector<int>(m + 1)); for (int i = 0; i <= m; ++i) { for (int j = 0; j <= m; ++j) { for (int k = 0; k <= m; ++k) { int t = 1ll * C(i, k) * C(m - i, j - k) % P; if (k & 1) { F[j][i] = (F[j][i] + P - t) % P; } else { F[j][i] = (F[j][i] + t) % P; } } } } f.resize(m + 1); for (int i = 0; i <= m; ++i) { for (int j = 0; j <= m; ++j) { f[i] = (f[i] + 1ll * F[i][j] * g[j]) % P; } f[i] = 1ll * f[i] * qpow(I2, m - (int)b.size()) % P; f[i] = 1ll * f[i] * qpow(2, n - (int)b.size()) % P; print(f[i], ' '); } }
5
#include <bits/stdc++.h> using namespace std; struct fff { int num; int p; }; fff f[1000001]; struct merges { int num; int cnt; int ncnt; }; merges m[1000001]; long long sum[1000001]; bool cmp(fff a, fff b) { if (a.num == b.num) return a.p < b.p; return a.num < b.num; } int main() { int n, k; long long l; scanf("%d%I64d%d", &n, &l, &k); for (int i = 0; i < n; i++) { scanf("%d", &f[i].num); f[i].p = i; } sort(f, f + n, cmp); int tot = -1; for (int i = 0; i < n; i++) { if (i == 0 || f[i].num != f[i - 1].num) { tot++; m[tot].num = f[i].num; m[tot].cnt = 1; m[tot].ncnt = 0; } else m[tot].cnt++; if (l % n != 0 && f[i].p >= l % n) m[tot].ncnt++; } long long ans = l % 1000000007; sum[0] = m[0].cnt; for (int i = 1; i <= tot; i++) { sum[i] = sum[i - 1] + m[i].cnt; } long long duan = l / n; if (l % n) duan++; long long maxm = min((long long)k, duan); duan = duan % 1000000007; for (int i = 2; i <= maxm; i++) { for (int j = 0; j <= tot; j++) { long long now = (m[j].ncnt * sum[j]) % 1000000007; now = (now * (duan - i + 1000000007)) % 1000000007; ans = (ans + now) % 1000000007; now = ((m[j].cnt - m[j].ncnt) * sum[j]) % 1000000007; now = (now * (duan - i + 1 + 1000000007)) % 1000000007; ans = (ans + now) % 1000000007; sum[j] = (sum[j] * m[j].cnt) % 1000000007; if (j) sum[j] = (sum[j] + sum[j - 1]) % 1000000007; } } printf("%I64d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); i++) #define repp(i, l, r) for(int i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<<endl; #define CNO cout<<"No"<<endl; typedef long long LL; typedef long double LD; int n,m; vector<vector<int>> e; int ans=0; void dfs(int x, vector<pair<int,int>> &v){ vector<pair<int,int>> w = v; int k = w.size(); repp(i, x+1, n){ int mi=IINF; w=v; rep(j,k){ mi=min(mi,e[i][w[j].first]); w[j].second=min(w[j].second,e[i][w[j].first]); if(mi==0) break; } if(mi>0){ int tmp=mi; rep(j,k){ tmp+=w[j].second; } ans=max(ans,tmp); w.push_back({i,mi}); dfs(i,w); } } } int main(){ cin >> n >> m; int u,v,c; e=vector<vector<int>>(n,vector<int>(n,0)); rep(i,m){ cin >> u >> v >> c; u--,v--; e[u][v]=c; e[v][u]=c; } vector<pair<int,int>> aa(1,{0,IINF}); rep(i,n){ aa[0].first=i; dfs(i,aa); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; char c[1010]; int main() { cin >> n >> c; for (int i = 0; i < n; i++) { int l = i - 1, r = i, L = 1, R = 1; while (l >= 0 && c[l] != 'L') L += c[l--] == 'R'; while (r <= n - 2 && c[r] != 'R') R += c[r++] == 'L'; cout << max(L, R) << " "; } }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int n, a[maxn], lazy[maxn << 2], s[maxn], b[maxn]; vector<int> v1, v2; pair<int, int> seg[maxn << 2]; long long ans; void build(int l, int r, int id) { seg[id] = {maxn, r - l}; if (r - l == 1) return; int mid = (l + r) >> 1, left = id << 1, right = left | 1; build(l, mid, left); build(mid, r, right); return; } void update(int l, int r, int lo, int hi, int val, int id) { if (r <= lo || hi <= l) return; if (l >= lo && r <= hi) { lazy[id] += val; seg[id].first += val; return; } int mid = (l + r) >> 1, left = id << 1, right = left | 1; update(l, mid, lo, hi, val, left); update(mid, r, lo, hi, val, right); if (seg[left].first == seg[right].first) seg[id] = {seg[left].first, seg[left].second + seg[right].second}; else seg[id] = min(seg[left], seg[right]); seg[id].first += lazy[id]; return; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0, l, r; i < n; ++i) { cin >> l >> r; a[l - 1] = r - 1; } v1.push_back(-1); v2.push_back(-1); for (int i = 0; i < n; ++i) { while (v1.back() != -1 && a[v1.back()] < a[i]) v1.pop_back(); while (v2.back() != -1 && a[v2.back()] > a[i]) v2.pop_back(); b[i] = v1.back(); s[i] = v2.back(); v1.push_back(i); v2.push_back(i); } build(0, n, 1); for (int i = 0; i < n; ++i) { update(0, n, i, i + 1, -maxn, 1); int l = i - 1, r = i; while (l != s[i]) { l = s[l]; update(0, n, l + 1, r, a[r - 1] - a[i], 1); r = l + 1; } l = i - 1, r = i; while (l != b[i]) { l = b[l]; update(0, n, l + 1, r, a[i] - a[r - 1], 1); r = l + 1; } ans += seg[1].second * (!seg[1].first); update(0, n, 0, i + 1, -1, 1); } cout << ans << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n, m = 0, num, i, j; cin >> n; string s; while (n > 0) { m = max(n % 10, m); s.push_back(n % 10 + '0'); n /= 10; } reverse(s.begin(), s.end()); cout << m << endl; n = s.length(); for (j = 0; j < m; ++j) { num = 0; for (i = 0; i < n; ++i) { if (s[i] > '0') { num = num * 10 + 1; s[i]--; } else num = num * 10; } cout << num << " "; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n >> a >> b; int arr[n + 1]; for (int i = 1; i <= n; i++) cin >> arr[i]; sort(arr + 1, arr + n + 1); if (abs(arr[b] - arr[b + 1]) == 0) cout << 0 << endl; else cout << arr[b + 1] - arr[b] << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, k; cin >> n >> k; string ch; cin >> ch; long long int x = -1, x1 = -1, y2 = -1; vector<long long int> mark(n, 0); bool check = false, check1 = false; long long int idx1 = 0; for (long long int i = 0; i < ch.length(); i++) { if (mark[i] == 0) { mark[i] = 1; idx1 = i; long long int idx = i, x = ch[i]; long long int j = i; j = i + k; while (j < n) { long long int y = ch[j], y1; if (ch[j] > x) { idx = j; x = ch[j]; } ch[j] = ch[i]; y1 = ch[j]; if (y1 < y && (j < x1 || x1 == -1)) { check = true; if (y2 == -1) { y2 = j; } else { y2 = min(y2, j); } } if (y1 > y) { if (x1 == -1) x1 = j; else { x1 = min(x1, j); } if (j < y2) { check = false; } } mark[j] = 1; j += k; } } } if (check) { long long int idx3 = idx1; while (ch[idx1] == '9') { idx1--; } if (idx1 >= 0) { ch[idx1]++; long long int idx2 = idx1 + k; while (idx2 < n) { ch[idx2] = ch[idx1]; idx2 += k; } } for (long long int i = idx1 + 1; i <= idx3; i++) { long long int j = i; ch[j] = '0'; while (j < n) { ch[j] = '0'; j += k; } } cout << ch.length() << endl; cout << ch << endl; } else { cout << ch.length() << endl; cout << ch << endl; } }
3
#include <bits/stdc++.h> using std::pair; using std::vector; using Permutation = std::vector<size_t>; using Cycle = std::vector<size_t>; Permutation ReadInput(std::istream& in_stream = std::cin); void WriteError(std::ostream& out_stream = std::cout); void WriteAnswer(const Permutation& permutation, std::ostream& out_stream = std::cout); Permutation Sqrt(const Permutation& permutation); int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); try { auto input = ReadInput(); auto result = Sqrt(input); WriteAnswer(result); } catch (...) { WriteError(); } return 0; } Cycle CycleByNode(size_t node, vector<bool>* looked, const Permutation& permutation); Permutation Sqrt(const Permutation& permutation) { vector<bool> looked(permutation.size(), false); std::multimap<size_t, Cycle> cycle_by_len; for (size_t node = 0; node < permutation.size(); ++node) { if (!looked[node]) { Cycle cycle = CycleByNode(node, &looked, permutation); cycle_by_len.insert({cycle.size(), cycle}); } } for (size_t cycle_size = 0; cycle_size <= permutation.size(); cycle_size += 2) { if (cycle_by_len.count(cycle_size) % 2 == 1) { throw std::exception(); } } std::vector<Cycle> cycles; for (size_t cycle_size = 1; cycle_size <= permutation.size(); cycle_size += 2) { auto range = cycle_by_len.equal_range(cycle_size); for (auto it = range.first; it != range.second; ++it) { Cycle cycle; for (size_t i = 0; i != 0 || cycle.empty(); i = (i + (cycle_size + 1) / 2) % cycle_size) { cycle.push_back(it->second[i]); } cycles.push_back(cycle); } } for (size_t cycle_size = 0; cycle_size <= permutation.size(); cycle_size += 2) { auto range = cycle_by_len.equal_range(cycle_size); if (range.first != range.second) { for (auto fst = range.first, snd = ++range.first; fst != range.second && snd != range.second; ++fst, ((fst != range.second) ? ++fst : fst), ++snd, ((snd != range.second) ? ++snd : snd)) { cycles.push_back({}); bool flag = true; auto cmp = [&flag](size_t a, size_t b) { flag = !flag; return flag; }; std::merge(fst->second.begin(), fst->second.end(), snd->second.begin(), snd->second.end(), std::back_inserter(cycles.back()), cmp); } } } Permutation sqrt(permutation.size()); for (auto cycle : cycles) { for (auto it = cycle.begin(), next = cycle.begin() + 1; it != cycle.end() && next != cycle.end(); ++it, ++next) { sqrt[*it] = *next; } sqrt[cycle.back()] = cycle.front(); } return sqrt; } Cycle CycleByNode(size_t node, vector<bool>* looked, const Permutation& permutation) { Cycle cycle; for (; !looked->at(node); looked->at(node) = true, node = permutation[node]) { cycle.push_back(node); } return cycle; } void WriteError(std::ostream& out_stream) { out_stream << -1; } void WriteAnswer(const Permutation& permutation, std::ostream& out_stream) { for (auto elem : permutation) { out_stream << elem + 1 << ' '; } } Permutation ReadInput(std::istream& in_stream) { size_t perm_size; in_stream >> perm_size; Permutation permutation(perm_size); for (auto& elem : permutation) { in_stream >> elem; --elem; } return permutation; }
5
#include <bits/stdc++.h> using namespace std; int N, ans, psum[101], a; int main() { cin >> N; for (int i = 1; i <= N; i++) { cin >> a; psum[i] = psum[i - 1] + a; } for (int i = 0; i <= N; i++) { ans = max(ans, N - (((N - i) - (psum[N] - psum[i])) + psum[i])); } cout << ans << "\n"; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 1; vector<int> g[maxn]; int sz[maxn]; long long sumr[maxn], sumt[maxn]; long long cntt[maxn][2], cntr[maxn][2]; long long ans = 0; void dfs(int v, int p = 0) { sz[v] = 1; cntr[v][0] = 1; for (int x : g[v]) { if (x != p) { dfs(x, v); sz[v] += sz[x]; cntr[v][0] += cntr[x][1]; cntr[v][1] += cntr[x][0]; sumr[v] += sumr[x] + sz[x]; } } for (int x : g[v]) { if (x != p) { sumt[v] += (sumr[x] + sz[x]) * (sz[v] - sz[x] - 1); cntt[v][0] += cntr[x][0] * (cntr[v][1] - cntr[x][0]); cntt[v][0] += cntr[x][1] * (cntr[v][0] - cntr[x][1] - 1); cntt[v][1] += cntr[x][1] * (cntr[v][1] - cntr[x][0]); cntt[v][1] += cntr[x][0] * (cntr[v][0] - cntr[x][1] - 1); } } cntt[v][0] /= 2; cntt[v][1] /= 2; ans += sumr[v] + sumt[v]; } int main() { int n; cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } dfs(1); long long cur = 0; for (int i = 1; i <= n; i++) cur += cntr[i][1] + cntt[i][1]; ans += cur; ans /= 2; cout << ans << endl; }
5
// https: //onlinejudge.u-aizu.ac.jp/courses/lesson/8/ITP2/4/ITP2_4_A #include <iostream> #include <vector> #include <algorithm> int main() { int n; std::cin >> n; std::vector<int> A(n); for (auto &x : A) { std::cin >> x; } int q; std::cin >> q; for (int i = 0; i < q; i++) { int b, e, t; std::cin >> b >> e >> t; std::swap_ranges(A.begin() + b, A.begin() + e, A.begin() + t); } for (auto itr = A.begin(); itr != A.end(); itr++) { std::cout << *itr; if (itr + 1 != A.end()) { std::cout << " "; } } std::cout << std::endl; return 0; }
0