func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; const int M = 100000 + 10; struct Node { int l, r, q; }; struct Tree { int l, r; int ans; int lazy; }; Tree tree[M * 3]; Node d[M]; int mex[M]; int n, m; void get_same(int s) { if (tree[s].l == tree[s].r) return; if (tree[s].lazy) { tree[s << 1].ans = tree[s << 1].ans | tree[s].ans; tree[s << 1 | 1].ans = tree[s << 1 | 1].ans | tree[s].ans; tree[s << 1].lazy = tree[s << 1 | 1].lazy = 1; tree[s].lazy = tree[s].ans = 0; } } void bulit(int s, int l, int r) { tree[s].l = l; tree[s].r = r; tree[s].ans = 0; tree[s].lazy = 0; if (l != r) { int mid = (l + r) >> 1; bulit(s << 1, l, mid); bulit(s << 1 | 1, mid + 1, r); } } void updata(int s, int l, int r, int z) { if (tree[s].l == l && tree[s].r == r) { tree[s].ans = tree[s].ans | z; tree[s].lazy = 1; return; } get_same(s); int mid = (tree[s].l + tree[s].r) >> 1; if (r <= mid) updata(s << 1, l, r, z); else if (l > mid) updata(s << 1 | 1, l, r, z); else { updata(s << 1, l, mid, z); updata(s << 1 | 1, mid + 1, r, z); } } void inse(int s) { get_same(s); if (tree[s].l == tree[s].r) { mex[tree[s].l] = tree[s].ans; return; } inse(s << 1); inse(s << 1 | 1); tree[s].ans = tree[s << 1].ans & tree[s << 1 | 1].ans; } int query(int s, int l, int r) { if (tree[s].l == l && tree[s].r == r) { return tree[s].ans; } int mid = (tree[s].l + tree[s].r) >> 1; if (r <= mid) return query(s << 1, l, r); else if (l > mid) return query(s << 1 | 1, l, r); else return query(s << 1, l, mid) & query(s << 1 | 1, mid + 1, r); } bool solve() { for (int i = 0; i < m; i++) { if (query(1, d[i].l, d[i].r) != d[i].q) return false; } return true; } int main() { while (scanf( %d%d , &n, &m) == 2) { bulit(1, 1, n); for (int i = 0; i < m; i++) { scanf( %d%d%d , &d[i].l, &d[i].r, &d[i].q); updata(1, d[i].l, d[i].r, d[i].q); } inse(1); if (solve()) { puts( YES ); for (int i = 1; i <= n; i++) { if (i != 1) printf( ); printf( %d , mex[i]); } printf( n ); } else { puts( NO ); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; char arr[105]; cin >> t; gets(arr); for (int i = 0; i < t; i++) { int flg1 = 0; int flg2 = 0; gets(arr); int len = strlen(arr); if (arr[0] == m && arr[1] == i && arr[2] == a && arr[3] == o && arr[4] == . ) { flg1 = 1; } if (arr[len - 1] == . && arr[len - 2] == a && arr[len - 3] == l && arr[len - 4] == a && arr[len - 5] == l ) { flg2 = 1; } if ((flg1 == 1 && flg2 == 1) || (flg1 == 0 && flg2 == 0)) { printf( OMG>.< I don t know! n ); } else if (flg1 == 1) { printf( Rainbow s n ); } else { printf( Freda s n ); } } return 0; }
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<stack> #include<ctime> #include<map> #define ll long long #define MAXN 20105 #define uns unsigned #define INF 0x3f3f3f3f #define MOD 1000000007ll #define lowbit(x) ((x)&(-(x))) using namespace std; inline ll read(){ ll x=0;bool f=1;char s=getchar(); while((s< 0 ||s> 9 )&&s>0){if(s== - )f^=1;s=getchar();} while(s>= 0 &&s<= 9 )x=(x<<1)+(x<<3)+s- 0 ,s=getchar(); return f?x:-x; } int n,q,a[MAXN],st[MAXN][20],dp[MAXN][20][35],lg[MAXN]; int A[35],B[35],tmp[35]; inline int maxx(int x,int y){return x+a[x]>y+a[y]?x:y;} inline int rmq(int l,int r){ if(l>r)return 0;r=min(r,n);int k=lg[r-l+1]; return maxx(st[l][k],st[r-(1<<k)+1][k]); } signed main() { n=read(),q=read(); for(int i=1;i<=n;i++)a[i]=read(),st[i][0]=i; lg[1]=0; for(int i=2;i<=n;i++)lg[i]=lg[i>>1]+1; for(int i=n;i>0;i--) for(int j=1;i+(1<<j)-1<=n;j++) st[i][j]=maxx(st[i][j-1],st[i+(1<<(j-1))][j-1]); for(int i=1;i<=n;i++) for(int k=0;k<=30;k++)dp[i][0][k]=i+a[i]+k; for(int j=0;j<lg[n];j++) for(int i=1;i<=n;i++) for(int k=0;k<=30;k++){ for(int l=k;l<=30;l++) dp[i][j+1][l]=max(dp[i][j+1][l],dp[ rmq(i,dp[i][j][k]) ][j][l-k]); for(int l=k;l<=30;l++) dp[i][j+1][l]=max(dp[i][j+1][l],dp[i][j+1][k]+l-k); } while(q--){ int l=read(),r=read(),k=read(); if(l==r){printf( 0 n );continue;} if(r<=l+a[l]+k){printf( 1 n );continue;} int ans=2; for(int i=0;i<=k;i++)A[i]=l+a[l]+i; for(int e=lg[r-l+1];e>=0;e--){ for(int i=0;i<=k;i++)tmp[i]=A[i],B[i]=rmq(l,A[i]); bool off=0; for(int i=0;i<=k&&!off;i++) for(int j=i;j<=k&&!off;j++) if(dp[B[i]][e][j-i]>tmp[j]){ tmp[j]=dp[B[i]][e][j-i]; if(tmp[j]>=r)off=1; } if(off)continue;ans+=(1<<e); for(int i=0;i<=k;i++)A[i]=tmp[i]; } printf( %d n ,ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, arr[25], ans = 0, c[3]; bool r1 = 1; cin >> n >> a >> b; c[0] = a, c[1] = b; for (int i = 0; i < n; i++) { cin >> arr[i]; } int mid = n / 2; if (2 * mid < n) mid++; for (int i = 0; i < mid; i++) { if (arr[i] == arr[n - i - 1] && arr[i] != 2) { continue; } else if (arr[i] == arr[n - i - 1] && arr[i] == 2) { if (i == n - i - 1) ans += min(a, b); else ans += 2 * min(a, b); } else if (arr[i] == 2 || arr[n - i - 1] == 2) { if (arr[i] == 2) ans += c[arr[n - i - 1]]; else ans += c[arr[i]]; } else r1 = 0; } if (r1) cout << ans << endl; else cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 10; const long long M = 1e9 + 7; int n, x, y, z; int last[N], to[2 * N], w[2 * N], num, nxt[2 * N]; long long a[N]; long long ans; void Add(int u, int v, int c) { nxt[++num] = last[u]; to[num] = v; w[num] = c; last[u] = num; } long long dp[N]; long long dfs(int u, int f) { long long max1, max2; max1 = max2 = 0; for (int i = last[u]; i; i = nxt[i]) { int v = to[i]; if (v == f) continue; dfs(v, u); long long now = dp[v] - w[i]; if (now >= max1) { max2 = max1; max1 = now; } else if (now > max2) max2 = now; } long long tot = max1 + max2 + a[u]; ans = max(ans, tot); return dp[u] = max(max1, max2) + a[u]; } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) scanf( %lld , a + i); for (int i = 1; i <= n - 1; ++i) { scanf( %d%d%d , &x, &y, &z); Add(x, y, z); Add(y, x, z); } dfs(1, 0); printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long int a, b, c, t, d, e, f; long long int max(long long int x, long long int y) { if (x > y) return x; else return y; } int main() { scanf( %I64d%I64d%I64d , &a, &b, &c); t = max(a, max(b, c)); d = max(t - 1, b); e = max(t - 1, c); f = max(t - 1, a); printf( %I64d n , d - b + e - c + f - a); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& a) { in >> a.first >> a.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> a) { out << a.first << << a.second; return out; } template <typename T, typename T1> T amax(T& a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T amin(T& a, T1 b) { if (b < a) a = b; return a; } const long long INF = 1e18; const int32_t M = 1e9 + 7; const int32_t MM = 998244353; const double inf = 2 * INF; const long long N = 0; vector<long long> tmp, v; long long pw(long long a, long long p) { long long result = 1; while (p > 0) { if (p & 1) result = a * result; a = a * a; p >>= 1; } return result; } long long sq(long long x) { long long ans = sqrt(x); while (ans * ans < x) { ans++; } while (ans * ans > x) { ans--; } return ans; } void precompute() { for (long long i = 3; i < 61; i++) { for (long long j = 2; j < INF; j++) { if (pow(j, i) >= inf) { break; } tmp.push_back(pw(j, i)); } } sort((tmp).begin(), (tmp).end()); (tmp).erase(unique((tmp).begin(), (tmp).end()), (tmp).end()); for (long long x : tmp) { long long t = sq(x); if (t * t == x) { continue; } v.push_back(x); } } void solve() { long long n; cin >> n; long long ans = upper_bound((v).begin(), (v).end(), n) - v.begin(); long long x = sq(n) - 1; ans += x; ans = n - 1 - ans; cout << ans << n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); precompute(); long long t = 1; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long srw[1000], scol[1000]; long long rows[1000001], cols[1000001]; int main() { int x, n, m, k, p, y, i, j; scanf( %d%d%d%d , &n, &m, &k, &p); for (i = 0; i < n; i++) for (j = 0; j < m; j++) { scanf( %d , &x); srw[i] += x; scol[j] += x; } multiset<long long, greater<long long> > rsm, csm; multiset<long long, greater<long long> >::iterator it; for (i = 0; i < n; i++) rsm.insert(srw[i]); for (j = 0; j < m; j++) csm.insert(scol[j]); for (i = 1; i <= k; i++) { it = rsm.begin(); long long val = *it; rsm.erase(it); rsm.insert(val - p * m); rows[i] = rows[i - 1] + val; it = csm.begin(); val = *it; csm.erase(it); csm.insert(val - p * n); cols[i] = cols[i - 1] + val; } long long ans = -99999999999999999; for (i = 0; i <= k; i++) ans = max(ans, cols[i] + rows[k - i] - 1LL * (k - i) * i * p); printf( %I64d , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, i, j, T, ti; long long MX = 4294967295; long long temp; long long loop(int n) { long long ret = 0; string str; int rn; cin >> str; m--; while (str != end ) { if (str == add ) { ret++; } else if (str == for ) { cin >> rn; temp = loop(rn); if (temp == -1) break; ret += temp; } else { cerr << WOW << str << endl; } if (m == 0) break; cin >> str; m--; } if (temp == -1) return -1; if (ret * n > MX) { return -1; } return ret * n; } int main() { ios::sync_with_stdio(false); cin >> m; temp = 0; temp = loop(1); if (temp == -1) { cout << OVERFLOW!!! << endl; } else { cout << temp << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &t) { t = 0; char ch = getchar(); int f = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } do { (t *= 10) += ch - 0 ; ch = getchar(); } while ( 0 <= ch && ch <= 9 ); t *= f; } const long long mod = 998244353; long long ksm(long long x, long long y) { if (x < 0) x += mod; if (x >= mod) x -= mod; long long res = 1; while (y) { if (y & 1) res = res * x % mod; x = x * x % mod; y >>= 1; } return res; } long long n, m, N, L, R, ans, x, y; int main() { read(n); read(m); read(L); read(R); N = n * m; x = R / 2 - (L - 1) / 2; y = R - L + 1 - x; if (N % 2 == 1) ans = ksm(x + y, N); else { ans = ksm(x + y, N) + ksm(x - y, N); ans %= mod; if (N % 2 == 0) ans = ans * ksm(2, mod - 2) % mod; } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &k) { k = 0; int flag = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) flag = -1; c = getchar(); } while (c >= 0 && c <= 9 ) { k = k * 10 + c - 0 ; c = getchar(); } k *= flag; } const int N = 1005; int n, m, k, s, t; long long pa, pb, f[N][N], A, B; void exgcd(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return; } exgcd(b % a, a, y, x); x -= b / a * y; } long long inv(long long a) { long long x, y; exgcd(a, 1000000007, x, y); return x; } int main() { cin >> k >> pa >> pb; A = pa * inv(pa + pb) % 1000000007; B = pb * inv(pa + pb) % 1000000007; for (int i = k; i >= 1; --i) for (int j = k - 1; j >= 0; --j) { if (i + j >= k) { f[i][j] = (i + j + pa * inv(pb)) % 1000000007; } else { f[i][j] = (A * f[i + 1][j] + B * f[i][i + j]) % 1000000007; } } cout << (f[1][0] + 1000000007) % 1000000007 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int a = 1; int b = N; for (int i = 2; N / i >= i; i++) if (N % i == 0) a = i, b = N / i; cout << a << << b << n ; }
#include <bits/stdc++.h> using namespace std; const long double PI = 3.1415926535897923846; const long long int MOD = 1000000007; const long long int N = 998244353; long long int power(long long int x, long long int n) { long long int res = 1; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } long long int modinverse(long long int a) { return power(a, MOD - 2); } void solve() { long long int n; cin >> n; vector<long long int> a(n); for (long long int i = 0; i < n; i++) { cin >> a[i]; } if (n % 2 == 0) { long long int xo = 0; for (long long int i = 0; i < n - 1; i++) xo = (xo ^ a[i]); if (a.back() - xo) { cout << NO n ; return; } } n -= (1 - n % 2); cout << YES n ; cout << n - 1 << endl; for (long long int i = 0; i + 2 < n; i += 2) { cout << i + 1 << << i + 2 << << i + 3 << endl; } for (long long int i = n - 3; i >= 0; i -= 2) { cout << i + 1 << << i + 2 << << i + 3 << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int test = 1; while (test--) { solve(); } cerr << Time : << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << ms n ; }
#include <bits/stdc++.h> using namespace std; int main() { int p[3500]; int n; cin >> n; bool b = 1; int j = 1; for (int i = 1; i <= n; i++) { cin >> p[i]; } sort(p + 1, p + n + 1); for (int i = 1; i <= n; i++) { if (p[i] != j) { cout << j; return 0; } else { j++; } } cout << p[n] + 1; return 0; }
#ifndef ONPC #define _FORTIFY_SOURCE 0 #pragma GCC optimize( Ofast ) #pragma GCC optimize( no-stack-protector ) #pragma GCC optimize( unroll-loops ) #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) #pragma GCC optimize( fast-math ) #endif #include <bits/stdc++.h> #define mp make_pair #define eb emplace_back #define x first #define y second template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pt; #ifdef ONPC mt19937 rnd(228); #else mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); #endif const ll MOD = 1000000007LL; const ll MOD2 = 998244353LL; const ld PI = acos(-1); #ifdef ONPC const int MAXN = 100; #else const int MAXN = 300100; #endif //#define int long long const ll INF = 1e18; void solve(){ int n; cin >> n; vector<int> p(n); for (int i = 1; i < n; i++){ cin >> p[i]; p[i]--; } vector<ll> a(n); for (int i = 1; i < n; i++){ cin >> a[i]; } vector<int> d(n, 0); for (int i = 1; i < n; i++){ if (d[i]) continue; vector<int> q; int j = i; while (j != 0) { q.eb(j); j = p[j]; } reverse(begin(q), end(q)); for (int k = 0; k < q.size(); k++){ d[q[k]] = k + 1; } } vector<vector<int>> v(n, vector<int>()); for (int i = 0; i < n; i++){ v[d[i]].eb(i); } vector<ll> dp(n, 0); for (int i = 1; i < n; i++){ if (v[i].empty()) break; ll mnc = INF, mxc = -INF; for (int j : v[i]){ chmin(mnc, a[j]); chmax(mxc, a[j]); } ll mx1 = -INF, mx2 = -INF; for (int j : v[i]){ chmax(mx1, dp[p[j]] + a[j]); chmax(mx2, dp[p[j]] - a[j]); } for (int j : v[i]){ dp[j] = dp[p[j]] + max(a[j] - mnc, mxc - a[j]); chmax(dp[j], mx1 - a[j]); chmax(dp[j], mx2 + a[j]); } } ll ans = 0; for (int i = 0; i < n; i++) chmax(ans, dp[i]); //for (int i = 0; i < n; i++) cerr << i << : << dp[i] << n ; cout << ans << n ; } /* #ifndef ONPC #define FILEIO #endif */ signed main() { #ifdef FILEIO freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int Q; cin >> Q; while (Q--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; if (str[0] > 96) { str[0] -= 32; } cout << str << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int dx[] = {0, 1, 0, -1, -1, 1, -1, 1}; long long int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1}; const long long int MAXN = 2e5 + 5; const long long int N = 2e5 + 5; long long int n, m; void LCSubsequence(string s1, string s2) { long long int n1 = s1.size(), n2 = s2.size(); vector<vector<long long int>> dp(n1 + 1, vector<long long int>(n2 + 1, 0)); long long int res = 0; for (long long int i = 0; i <= n1; i++) { for (long long int j = 0; j <= n2; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + (4 - 2); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) - 1; } if (dp[i][j] < 0) dp[i][j] = 0; res = max(res, dp[i][j]); } } cout << res << n ; } long long int goAns = 0; long long int go(long long int i, long long int j, string s1, string s2) { if (i >= n || j >= m) return 0; long long int x = 0, y = 0, res = 0; if (s1[i] == s2[j]) { res = (4 - 2) + max(0LL, go(i + 1, j + 1, s1, s2)); } else { x = -1 + go(i + 1, j, s1, s2); y = -1 + go(i, j + 1, s1, s2); res = max({0LL, x, y}); } goAns = max(goAns, res); return res; } void solve() { long long int x, y, z; cin >> n >> m; string s1, s2; cin >> s1 >> s2; LCSubsequence(s1, s2); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int T = 1; while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int n, ans = 0x3f3f3f3f; int a[100005], cnt; int b[100005], gc; int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); for (int i = 1; i <= 9; ++i) { cnt = 0; if (i == 1) b[1] = a[1] - 1, b[2] = a[2] - 1; if (i == 2) b[1] = a[1] - 1, b[2] = a[2] + 0; if (i == 3) b[1] = a[1] - 1, b[2] = a[2] + 1; if (i == 4) b[1] = a[1] + 0, b[2] = a[2] - 1; if (i == 5) b[1] = a[1] + 0, b[2] = a[2] + 0; if (i == 6) b[1] = a[1] + 0, b[2] = a[2] + 1; if (i == 7) b[1] = a[1] + 1, b[2] = a[2] - 1; if (i == 8) b[1] = a[1] + 1, b[2] = a[2] + 0; if (i == 9) b[1] = a[1] + 1, b[2] = a[2] + 1; cnt = fabs(b[1] - a[1]) + fabs(b[2] - a[2]); gc = fabs(b[2] - b[1]); if (b[2] >= b[1]) for (int i = 3; i <= n; ++i) { b[i] = a[i]; if (b[i] - gc - b[i - 1] > 1 || b[i] - gc - b[i - 1] < -1) { cnt = 0x3f3f3f3f; break; } else { if (b[i] == gc + b[i - 1]) continue; else if (b[i] == gc + b[i - 1] + 1) --b[i]; else if (b[i] == gc + b[i - 1] - 1) ++b[i]; cnt += fabs(a[i] - b[i]); } } if (b[2] < b[1]) { for (int i = 3; i <= n; ++i) { b[i] = a[i]; if (b[i] + gc - b[i - 1] > 1 || b[i] + gc - b[i - 1] < -1) { cnt = 0x3f3f3f3f; break; } else { if (b[i] == b[i - 1] - gc) continue; else if (b[i] == b[i - 1] - gc + 1) --b[i]; else if (b[i] == b[i - 1] - gc - 1) ++b[i]; cnt += fabs(a[i] - b[i]); } } } ans = min(cnt, ans); } if (ans == 0x3f3f3f3f) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> #pragma optimization_level 3 #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops,fast-math,O3 ) #pragma GCC target( avx,avx2,fma ) using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); int a, k, nc = 0, isz = 0; vector<unordered_set<int>> n, l; vector<int> cmp, vrt; void dfs(int v) { cmp[v] = nc; vrt.push_back(v); for (int i : l[v]) { if (cmp[i] == -1) dfs(i); else if (cmp[i] != nc) { assert(cmp[i] == 0); isz = 1; } } } int main() { cin.tie(0); cout.tie(0); cin.sync_with_stdio(0); cout.sync_with_stdio(0); ; cin >> a >> k; cmp = vector<int>(a, -1); l = vector<unordered_set<int>>(a, unordered_set<int>()); n = vector<unordered_set<int>>(a, unordered_set<int>()); for (int q = 0; q < k; q++) { int x, y; cin >> x >> y; x--, y--; n[x].insert(y); n[y].insert(x); } for (int q = 0; q < a; q++) { int sos = (a - 1) - n[q].size(); if (sos > a / 2) cmp[q] = 0; } for (int q = 0; q < a; q++) { if (cmp[q] != -1) continue; for (int w = 0; w < a; w++) { if (w == q || n[q].count(w)) continue; l[q].insert(w); } } for (int q = 0; q < a; q++) { if (cmp[q] != -1) continue; nc = q + 1; vrt.clear(); isz = 0; dfs(q); if (isz) { for (int i : vrt) cmp[i] = 0; } } map<int, int> mp; for (int i : cmp) mp[i]++; vector<int> u; for (pair<int, int> p : mp) u.push_back(p.second); sort(u.begin(), u.end()); cout << u.size() << n ; for (int i : u) cout << i << ; }
#include <bits/stdc++.h> using namespace std; string s; int main() { int n, i, j, k, l, m; cin >> n; cin >> s; int cnt, ans; char c, c1, c2, c3; ans = 0; for (i = 0; i < n - 1; i++) { cnt = 0; if (s[i] == s[i + 1] && i < n - 1) { c = s[i]; while (s[i] == c) { i++; cnt++; } ans += (cnt / 2); if (i < n) c2 = s[i]; if (c2 == R && c == B ) c1 = G ; else if (c2 == R && c == G ) c1 = B ; else if (c2 == B && c == G ) c1 = R ; else if (c2 == B && c == R ) c1 = G ; else if (c2 == G && c == B ) c1 = R ; else if (c2 == G && c == R ) c1 = B ; if (i >= n) { if (c == R ) c1 = B ; else if (c == B ) c1 = G ; else c1 = R ; } for (j = i - cnt + 1; j < i; j += 2) s[j] = c1; i--; } } printf( %d n , ans); cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string s; int x[100009], y[100009], z[100009], m, l, r; int main() { cin >> s; cin >> m; for (int i = 1; i <= s.size(); i++) { x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; if (s[i - 1] == x ) x[i]++; if (s[i - 1] == y ) y[i]++; if (s[i - 1] == z ) z[i]++; } for (int i = 1; i <= m; i++) { cin >> l >> r; if (min(x[r] - x[l - 1], min(y[r] - y[l - 1], z[r] - z[l - 1])) + 1 >= max(x[r] - x[l - 1], max(y[r] - y[l - 1], z[r] - z[l - 1]))) { cout << YES n ; continue; } if (r - l + 1 <= 2) { cout << YES n ; continue; } cout << NO n ; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[110]; for (int i = 0; i < n; i++) { cin >> a[i]; } int i, one, two, j, ans = 0; for (i = 0; i < k; i++) { for (one = 0, two = 0, j = i; j < n; j += k) { if (a[j] == 1) one++; else two++; } ans += min(one, two); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int n, a, b; int main() { cin >> n >> a >> b; int t = 1; int r = 0; if (a > b) { a ^= b ^= a ^= b; } while (t < n) { t <<= 1; ++r; } t >>= 1; if (a <= t && b > t) { cout << Final! << endl; return 0; } while (a > t || b <= t) { a -= a > t ? t : 0; b -= b > t ? t : 0; t >>= 1; --r; } cout << r << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mo = 1e9 + 7; const int maxi = 1e6 + 6; long long a[maxi], x[maxi], b[maxi]; int n; long long t; int sum[maxi]; int los[maxi]; int ll[maxi]; int main() { cin >> n >> t; for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); for (int i = 1; i <= n; i++) { scanf( %lld , &x[i]); sum[x[i] - 1]++; sum[i - 1]--; } for (int i = n; i > 0; i--) sum[i] += sum[i + 1]; for (int i = 1; i <= n; i++) if (x[i] < i || x[i] < x[i - 1]) return !printf( No n ); for (int i = 1; i <= n; i++) if (sum[i] > 0) b[i] = a[i + 1] + t; else b[i] = a[i] + t; for (int i = 2; i <= n; i++) if (b[i] <= b[i - 1]) b[i] = b[i - 1] + 1; for (int i = 2; i <= n; i++) if (a[i] + t > b[i - 1]) los[i] = 1; for (int i = 1; i <= n; i++) ll[i] = ll[i - 1] + los[i]; for (int i = 1; i <= n; i++) if (x[i] < n && x[i] - i > 0 && ll[x[i] + 1] - ll[i] == 0) return !printf( No n ); printf( Yes n ); for (int i = 1; i <= n; i++) printf( %lld , b[i]); printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<vector<int>> graph; ifstream in( buffcraft.in , ios::in); ofstream out( buffcraft.out , ios::out); int pl, pr, vl, vr, k; vector<int> p; void dfs(long long x) { if (x < 1000000000) { p.push_back(x); dfs(x * 10 + 4); dfs(x * 10 + 7); } } int main() { dfs(0); p.push_back(INT_MAX); sort(p.begin(), p.end()); cin >> pl >> pr >> vl >> vr >> k; double cnt = 0; double all = ((vr - vl) + 1.0) * ((pr - pl) + 1.0); for (int i = 1; i < p.size() - k; i++) { int lm = p[i - 1] + 1, l = p[i], r = p[i + k - 1], rp = p[i + k] - 1; if (pl <= l && pr >= lm && vl <= rp && vr >= r) cnt += (min(pr, l) - max(pl, lm) + 1.0) * (min(vr, rp) - max(vl, r) + 1.0); if (vl <= l && vr >= lm && pl <= rp && pr >= r) { cnt += (min(vr, l) - max(vl, lm) + 1.0) * (min(pr, rp) - max(pl, r) + 1.0); if (k == 1) cnt -= 1.0; } } cout << setprecision(10) << cnt / all; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 256; int n, a[MAXN], k, ans; list<int> L; set<int> s, S; multiset<int> M; int main() { cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; if (i >= k) s.insert(a[i]); } if (s.size() > 1) { cout << -1; return 0; } ans = k - 1; while (a[ans] == a[k]) --ans; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template <class t> using vvc = vc<vc<t>>; using pi = pair<ll, ll>; using vi = vc<ll>; template <class t, class u> ostream& operator<<(ostream& os, const pair<t, u>& p) { return os << { << p.first << , << p.second << } ; } template <class t> ostream& operator<<(ostream& os, const vc<t>& v) { os << { ; for (auto e : v) os << e << , ; return os << } ; } using uint = unsigned; using ull = unsigned long long; template <class t, size_t n> ostream& operator<<(ostream& os, const array<t, n>& first) { return os << vc<t>(first.begin(), first.end()); } template <ll i, class T> void print_tuple(ostream&, const T&) {} template <ll i, class T, class H, class... Args> void print_tuple(ostream& os, const T& t) { if (i) os << , ; os << get<i>(t); print_tuple<i + 1, T, Args...>(os, t); } template <class... Args> ostream& operator<<(ostream& os, const tuple<Args...>& t) { os << { ; print_tuple<0, tuple<Args...>, Args...>(os, t); return os << } ; } template <class t> void print(t x, ll suc = 1) { cout << x; if (suc == 1) cout << n ; if (suc == 2) cout << ; } ll read() { ll i; cin >> i; return i; } vi readvi(ll n, ll off = 0) { vi v(n); for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off; return v; } template <class T> void print(const vector<T>& v, ll suc = 1) { for (ll i = ll(0); i < ll(v.size()); i++) print(v[i], i == ll(v.size()) - 1 ? suc : 2); } string readString() { string s; cin >> s; return s; } template <class T> T sq(const T& t) { return t * t; } void yes(bool ex = true) { cout << Yes << n ; if (ex) exit(0); } void no(bool ex = true) { cout << No << n ; if (ex) exit(0); } void possible(bool ex = true) { cout << Possible << n ; if (ex) exit(0); } void impossible(bool ex = true) { cout << Impossible << n ; if (ex) exit(0); } constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; } const ll infLL = LLONG_MAX / 3; const ll inf = infLL; ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); } ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); } ll popcount(signed t) { return __builtin_popcount(t); } ll popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(ll i) { return i && (i & -i) == i; } ll mask(ll i) { return (ll(1) << i) - 1; } bool inc(ll first, ll second, ll c) { return first <= second && second <= c; } template <class t> void mkuni(vc<t>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } ll rand_int(ll l, ll r) { static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<ll>(l, r)(gen); } template <class t> void myshuffle(vc<t>& first) { for (ll i = ll(0); i < ll(ll(first.size())); i++) swap(first[i], first[rand_int(0, i)]); } template <class t> ll lwb(const vc<t>& v, const t& first) { return lower_bound(v.begin(), v.end(), first) - v.begin(); } vi waf(vi x) { sort(x.begin(), x.end()); vi y{0}; for (auto v : x) y.push_back(y.back() + v); return y; } signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); ll n; cin >> n; vi pri(n + 1, 1), tot(n + 1); vi al, od; iota(tot.begin(), tot.end(), 0); for (ll i = ll(2); i < ll(n + 1); i++) if (pri[i]) { for (ll j = i; j <= n; j += i) tot[j] = tot[j] / i * (i - 1); for (ll j = i * 2; j <= n; j += i) pri[j] = 0; } for (ll i = ll(2); i < ll(n + 1); i++) { al.push_back(tot[i]); if (i % 2) od.push_back(tot[i]); } void(0); void(0); al = waf(al); od = waf(od); ll k; cin >> k; ll ans = inf; if (k + 1 < ll(al.size())) chmin(ans, al[k + 1]); if (k < ll(od.size())) chmin(ans, od[k]); ans++; print(ans); }
#include <bits/stdc++.h> using namespace std; long long ret = 0; long long La, Ra, Ta; long long Lb, Rb, Tb; long long GCD = 0; pair<long long, long long> gcd(long long a, long long b) { if (b == 0) { GCD = a; return {1, 0}; } pair<long long, long long> nxt = gcd(b, a % b); long long x = nxt.first; long long y = nxt.second; long long k = a / b; return {y, x - k * y}; } int main() { cout << setprecision(10); ios::sync_with_stdio(0); cin.tie(0); cin >> La >> Ra >> Ta; cin >> Lb >> Rb >> Tb; pair<long long, long long> eq = gcd(Ta, Tb); long long L0 = La % GCD, R0 = L0 + (Ra - La); long long L1 = Lb % GCD, R1 = L1 + (Rb - Lb); La = L0; Ra = R0; Lb = L1; Rb = R1; for (long long offA = -2; offA <= 2; offA++) for (long long offB = -2; offB <= 2; offB++) { long long N_Ra = Ra + offA * Ta; long long N_La = La + offA * Ta; long long N_Rb = Rb + offB * Tb; long long N_Lb = Lb + offB * Tb; ret = max(ret, min(N_Ra, N_Rb) - max(N_La, N_Lb) + 1); } cout << ret << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; while (cin >> a >> b >> c) { if (b >= c) { b = c - 1; } else { c = b + 1; } if (a >= b) { a = b - 1; cout << a + b + c << endl; } else { b = a + 1; c = a + 2; cout << a + b + c << endl; } } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n, m, ans, l; int mark[MAXN], comp[MAXN], scc[MAXN], cyc[MAXN]; vector<int> edge[MAXN], adj[MAXN], radj[MAXN]; deque<int> dq; void dfs(int source) { mark[source]++; ans++, comp[source] = l; for (auto &u : edge[source]) if (!mark[u]) dfs(u); } void dfs1(int source) { mark[source]++; for (auto &u : adj[source]) if (!mark[u]) dfs1(u); dq.push_back(source); } void rev(int source) { mark[source]++; scc[l]++; for (auto &u : radj[source]) if (mark[u] == 1) rev(u); } int main() { cin >> n >> m; while (m--) { int x, y; cin >> x >> y; edge[x].push_back(y), edge[y].push_back(x); adj[x].push_back(y), radj[y].push_back(x); } for (int i = 1; i <= n; i++) if (!mark[i]) ++l, dfs(i), ans--; fill(mark, mark + MAXN, 0); l = 0; for (int i = 1; i <= n; i++) if (!mark[i]) dfs1(i); while (dq.size()) { int x = dq.back(); dq.pop_back(); if (mark[x] < 2 && !cyc[comp[x]]) { ++l, rev(x); if (scc[l] > 1) cyc[comp[x]] = true, ans++; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long int MAX3(long long int x, long long int y, long long int z) { long long int t = max(x, y); return max(t, z); } long long int power(long long int x, long long int n) { long long int i, ans = 1; for (i = 1; i <= (n); i += 1) { ans = ans * x; } return ans; } void solve() { long long int n, i, sum = 0; vector<long long int> a; cin >> n; for (i = 0; i < (n); i += 1) { long long int x; cin >> x; a.push_back(x); if (i % 2 == 0) sum += x; } vector<long long int> a1, a2; long long int m1 = 0, m2 = 0; for (i = 0; i < (n - 1); i += 1) { long long int t = -a[i] + a[i + 1]; i++; a1.push_back(t); } for (i = 1; i < (n - 1); i += 1) { long long int t = -a[i + 1] + a[i]; i++; a2.push_back(t); } long long int s = a1.size(); long long int max_so_far = 0; long long int max_ending_here = 0; for (i = 0; i < (s); i += 1) { max_ending_here += a1[i]; max_so_far = max(max_so_far, max_ending_here); if (max_ending_here < 0) max_ending_here = 0; } m1 = max_so_far; max_so_far = 0; max_ending_here = 0; s = a2.size(); for (i = 0; i < (s); i += 1) { max_ending_here += a2[i]; max_so_far = max(max_so_far, max_ending_here); if (max_ending_here < 0) max_ending_here = 0; } m2 = max_so_far; long long int ans = max(m1, m2); cout << ans + sum << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int> > a(n, vector<int>(n)); int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf( %d , &a[i][j]); if (i == j) sum += a[i][j]; } } sum &= 1; int q; scanf( %d , &q); const char *cc = 01 ; while (q--) { int t; scanf( %d , &t); if (t == 3) { printf( %c , cc[sum]); } else { scanf( %d , &t); sum ^= 1; } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; vector<vector<int>> g; vector<int> deg; ll ans; int n, m; void add_edge(int x, int y) { if (x > y) { int t = x; x = y; y = t; } g[x].push_back(y); deg[x]++; deg[y]++; } void init() { scanf( %d %d , &n, &m); deg.resize(n, 0); g.resize(n, vector<int>()); ans = 0; for (int i = 0; i < m; i++) { int x, y; scanf( %d%d , &x, &y); x--; y--; add_edge(x, y); } ans = 0; for (int i = 0; i < n; i++) ans += (ll)g[i].size() * (ll)(deg[i] - g[i].size()); } void work() { int Q; cout << ans << endl; scanf( %d , &Q); for (int qi = 0; qi < Q; qi++) { int x; scanf( %d , &x); x--; ans -= (ll)g[x].size() * (ll)(deg[x] - g[x].size()); ans += 0; for (int y : g[x]) { ll y_size = g[y].size(); ans -= (ll)y_size * (ll)(deg[y] - y_size); g[y].push_back(x); y_size++; ans += (ll)y_size * (ll)(deg[y] - y_size); } g[x].clear(); printf( %lld n , ans); } } int main() { init(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c *x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct deb { template <class c> deb &operator<<(const c &) { return *this; } }; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } const long long dx[8] = {0, 1, -1, 0, 1, 1, -1, -1}; const long long dy[8] = {1, 0, 0, -1, 1, -1, -1, 1}; const long long mod = 1e9 + 7; inline long long add(long long a, long long b) { return ((a % mod) + (b % mod)) % mod; } inline long long sub(long long a, long long b) { return ((a % mod) - (b % mod) + mod) % mod; } inline long long mul(long long a, long long b) { return ((a % mod) * (b % mod)) % mod; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } const double PI = acos(-1.0); const long long inf = 1e18; const long long maxn = 1e3 + 5; void solve() { long long n; cin >> n; vector<long long> arr(2 * n); cin >> arr; cin >> arr; sort(arr.begin(), arr.end()); long long s1 = 0; long long s2 = 0; for (long long i = 0; i < n; i++) s1 += arr[i]; for (long long i = n; i < 2 * n; i++) s2 += arr[i]; if (s1 != s2) { for (long long i = 0; i < 2 * n; i++) { cout << arr[i] << ; } cout << n ; } else { cout << -1 << n ; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; for (long long tt = 1; tt <= t; tt++) { solve(); } }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-9; const int inf = 2000000000; const long long infLL = 9000000000000000000; inline bool checkBit(long long n, int i) { return n & (1LL << i); } inline long long setBit(long long n, int i) { return n | (1LL << i); ; } inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long &a) { a %= 1000000007; (a < 0) && (a += 1000000007); } inline long long modMul(long long a, long long b) { a %= 1000000007, b %= 1000000007; normal(a), normal(b); return (a * b) % 1000000007; } inline long long modAdd(long long a, long long b) { a %= 1000000007, b %= 1000000007; normal(a), normal(b); return (a + b) % 1000000007; } inline long long modSub(long long a, long long b) { a %= 1000000007, b %= 1000000007; normal(a), normal(b); a -= b; normal(a); return a; } inline long long modPow(long long b, long long p) { long long r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); } inline long long modDiv(long long a, long long b) { return modMul(a, modInverse(b)); } inline long long power(long long bs, long long k) { long long x = 1LL, y = bs; if (k == 0) return 1LL; while (k > 0) { if (k % 2) x *= y; y *= y; k /= 2; } return x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed, ios::floatfield); ; long long n; cin >> n; if (n % 2) cout << black ; else { cout << white << n ; cout << 1 2 ; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAX = 1e3 + 20; long long inf = (long long)1e15; long long mod = (long long)1e9 + 7; long long n, cntr[MAX], cntc[MAX], l[MAX][MAX], used[MAX]; void swpr(long long ind1, long long ind2) { swap(cntr[ind1], cntr[ind2]); for (int i = 1; i <= n; i++) swap(l[ind1][i], l[ind2][i]); } void swpc(long long ind1, long long ind2) { swap(cntc[ind1], cntc[ind2]); for (int i = 1; i <= n; i++) swap(l[i][ind1], l[i][ind2]); } void solve() { cin >> n; for (int i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; l[x][y] = 1; cntr[x]++; cntc[y]++; } vector<pair<long long, pair<long long, long long>>> ans; long long need = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (cntr[j] == need && !used[j]) { used[i] = 1; need = 0; if (i != j) { swpr(i, j); ans.push_back({1, {i, j}}); } break; } } need++; } for (int i = 2; i <= n; i++) { long long start = (i - 1) - cntr[i] + 1; long long bord = start; while (bord <= n) { if (l[i][bord] == 1) { if (bord != start) { swpc(start, bord); ans.push_back({2, {start, bord}}); } start++; } bord++; } } cout << ans.size() << n ; for (int i = 0; i < ans.size(); i++) { cout << ans[i].first << << ans[i].second.first << << ans[i].second.second << n ; } } int main() { mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ios::sync_with_stdio(0); long long q; q = 1; while (q--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; size_t pos1 = s.find( AB ), pos2 = s.find( BA ); string tmp = s; s.erase(0, pos1 + 2); tmp.erase(0, pos2 + 2); bool check = false; if (pos1 == string::npos || pos2 == string::npos) { cout << NO ; } else { if (s.find( BA ) != string::npos || tmp.find( AB ) != string::npos) check = true; if (check) cout << YES ; else cout << NO ; } return 0; }
#include <bits/stdc++.h> using namespace std; string mask(int a) { string ans; int x = a; while (x) { if (x % 10 == 4 || x % 10 == 7) ans.insert(ans.begin(), x % 10 + 0 ); x /= 10; } return ans; } int main() { string b; int a; cin >> a >> b; a++; while (mask(a) != b) a++; cout << a; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f, mn1, mn2, res, ans1, ans2; cin >> a >> b >> c >> d >> e >> f; res = d; mn2 = min(b, min(c, d)); d = abs(mn2 - d); mn1 = min(a, d); ans1 = (mn2 * f) + (mn1 * e); mn1 = min(a, res); res = abs(mn1 - res); mn2 = min(b, min(c, res)); ans2 = (mn2 * f) + (mn1 * e); cout << max(ans1, ans2) << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int node[4][11][11][maxn]; int h[110]; char s[maxn], e[11]; int lowbit(int x) { return x & (-x); } void add(int ty, int pos, int x, int d) { for (; x < maxn; x += lowbit(x)) { for (int k = 1; k <= 10; k++) { node[ty][k][pos % k][x] += d; } } } int sum(int ty, int len, int pos, int x) { int ans = 0; for (; x > 0; x -= lowbit(x)) { ans += node[ty][len][pos][x]; } return ans; } int query(int l, int r, int ty, int len, int pos) { return sum(ty, len, pos, r) - sum(ty, len, pos, l - 1); } int main() { int n; h[ A ] = 0; h[ T ] = 1; h[ C ] = 2; h[ G ] = 3; scanf( %s%d , s, &n); int len = strlen(s); for (int i = 0; i < len; i++) { add(h[s[i]], i, i + 1, 1); } while (n--) { int a, b, c; scanf( %d , &a); if (a == 1) { scanf( %d%s , &b, e); add(h[s[b - 1]], b - 1, b, -1); add(h[e[0]], b - 1, b, 1); s[b - 1] = e[0]; } else { scanf( %d%d%s , &b, &c, e); len = strlen(e); int ans = 0; for (int i = 0; i < len; i++) { ans += query(b, c, h[e[i]], len, (i + b - 1) % len); } printf( %d n , ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long p[1000100], h[1000100], g[1000100], ans; int main() { int m, n, x, y; cin >> n >> m; for (int i = 0; i <= n; i++) p[i] = (i ? p[i - 1] * 9 : 1); while (m--) { scanf( %d%d , &x, &y); h[x] += p[y]; h[y] += p[x]; } for (int i = 1; i <= n; i++) g[i] = h[i] + p[i]; sort(h + 1, h + n + 1); for (int i = 1, cnt = 0; i <= n; i++) if (h[i] == h[i - 1]) ans += cnt++; else cnt = 1; sort(g + 1, g + n + 1); for (int i = 1, cnt = 0; i <= n; i++) if (g[i] == g[i - 1]) ans += cnt++; else cnt = 1; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; void die(string S) { puts(S.c_str()); exit(0); } int number[333][333]; int cnt[100100]; vector<int> V[100100]; int grid[333][333]; int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; vector<int> v; for (int i = 0; i < n * m; i++) { int x; cin >> x; v.push_back(x); number[i / m][i % m] = x; cnt[i] = 0; V[i].clear(); } sort((v).begin(), (v).end()); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { number[i][j] = lower_bound((v).begin(), (v).end(), number[i][j]) - v.begin(); cnt[number[i][j]]++; V[number[i][j]].push_back(i * m + j); } int pos = 0; for (int i = 0; i < n * m; i++) if (cnt[i]) { int endp = (pos + cnt[i] - 1); if (endp / m == pos / m) { sort((V[i]).rbegin(), (V[i]).rend()); for (int j = 0; j < cnt[i]; j++) grid[(pos + j) / m][(pos + j) % m] = V[i][j]; } else { sort((V[i]).begin(), (V[i]).end()); int x = m - pos % m; for (int j = x - 1; j >= 0; j--) grid[(pos + x - j - 1) / m][(pos + x - j - 1) % m] = V[i][j]; x = endp % m + 1; for (int j = cnt[i] - 1; j >= cnt[i] - x; j--) grid[(endp / m * m + cnt[i] - j - 1) / m] [(endp / m * m + cnt[i] - j - 1) % m] = V[i][j]; for (int j = m - pos % m; j < cnt[i] - x; j++) grid[(pos + j) / m][(pos + j) % m] = V[i][j]; for (int j = pos / m + 1; j < endp / m; j++) reverse(grid[j], grid[j] + m); } pos += cnt[i]; } int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) for (int k = j + 1; k < m; k++) if (grid[i][j] < grid[i][k]) ans++; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxN = 10 + 5; char a[maxN][maxN][maxN]; int mark[maxN][maxN][maxN]; int k, n, m; int dx[] = {-1, 1, 0, 0, 0, 0}; int dy[] = {0, 0, -1, 1, 0, 0}; int dz[] = {0, 0, 0, 0, -1, 1}; int cnt; void dfs(int x, int y, int z) { mark[x][y][z] = 1; cnt++; for (int i = 0; i < 6; i++) { int r = x + dx[i], c = y + dy[i], t = z + dz[i]; if (r < 0 || c < 0 || t < 0) continue; if (r >= k || c >= n || t >= m) continue; if (!mark[r][c][t] && a[r][c][t] == . ) dfs(r, c, t); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> k >> n >> m; for (int i = 0; i < k; i++) for (int j = 0; j < n; j++) for (int t = 0; t < m; t++) cin >> a[i][j][t]; int x, y; cin >> x >> y; x--; y--; dfs(0, x, y); cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; int n = s.size(); int cz = 0; int co = 0; int ct = 0; for (int i = 0; i < n; i++) { co += (s[i] == 1 ); ct += (s[i] == 2 ); cz += (s[i] == 0 ); } if (cz == 0 || ct == 0) { sort(s.begin(), s.end()); cout << s << n ; return 0; } if (co == 0) { cout << s << n ; return 0; } string aux = ; int idx = -1; for (int i = 0; i < n; i++) { if (s[i] != 1 ) aux += s[i]; } string res = ; int occ = -1; for (int i = 0; i < aux.size(); i++) { if (aux[i] == 2 && occ == -1) { while (co--) { res += 1 ; } res += aux[i]; occ = 1; } else { res += aux[i]; } } cout << res << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int mx = (int)2e5 + 5; int n, c[mx], ans[mx]; char a[mx], b[mx]; void solve() { int cnt = 0, temp = 0; for (int i = n - 1; i >= 0; i--) { c[cnt++] = (a[i] + b[i] - 2 * a + temp) % 26; temp = (a[i] + b[i] - 2 * a + temp) / 26; } c[cnt++] = temp; temp = 0; for (int i = cnt - 1; i >= 0; i--) { temp *= 26; ans[i] = (c[i] + temp) / 2; temp = (c[i] + temp) % 2; } for (int i = n - 1; i >= 0; i--) { printf( %c , ans[i] + a ); } cout << endl; } int main() { cin >> n; scanf( %s%s , a, b); solve(); }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; int c = 0; cin >> a >> b; while (a > 0 && b > 0) { if (a > b) { a -= 2; b -= 1; } else { a -= 1; b -= 2; } if (a > -1 && b > -1) c++; } cout << c; }
#include <bits/stdc++.h> using namespace std; const int mod = 998244353, MAX = 100003, INF = 1 << 30; long long rui(long long a, long long b) { if (b == 1) return a % mod; else if (b == 0) return 1; else if (b % 2 == 0) return (rui(a, b / 2) * rui(a, b / 2)) % mod; else return (rui(a, b - 1) * a) % mod; } int main() { int Q; cin >> Q; for (int q = 0; q < Q; q++) { int N, M; cin >> N >> M; vector<int> S[N], color(N, 0); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a--; b--; S[a].push_back(b); S[b].push_back(a); } long long ans = 1; for (int i = 0; i < N; i++) { if (color[i]) continue; int odd = 1, even = 0; color[i] = 1; queue<int> Q; Q.push(i); while (!Q.empty()) { int a = Q.front(); Q.pop(); for (int j = 0; j < S[a].size(); j++) { int b = S[a][j]; if (color[b] == 0) { color[b] = 3 - color[a]; if (color[b] % 2) odd++; else even++; Q.push(b); } else if (color[a] == color[b]) { ans = 0; } } } ans *= (rui(2, odd) + rui(2, even)) % mod; ans %= mod; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int lenA, lenB; int A[1000004]; int B[1000004]; int idxB[1000004]; int lenP = 0, idxP = 0; long long P[1000004 * 2]; int main() { scanf( %d %d , &lenA, &lenB); for (int i = 0, _n = (lenA); i < _n; ++i) scanf( %d , A + i); memset(idxB, -1, sizeof(idxB)); for (int j = 0, _n = (lenB); j < _n; ++j) { scanf( %d , B + j); idxB[B[j]] = j; } long long offset = 0; int res = 0; for (int k = 0, _n = (lenA * 2); k < _n; ++k) { long long p = idxB[A[k % lenA]]; if (p < 0) { lenP = 0; idxP = 0; continue; } p += offset; if (lenP > 0 && P[lenP - 1] >= p) { p += lenB; offset += lenB; } P[lenP++] = p; idxP = lower_bound(P + idxP, P + lenP, p - lenB + 1) - P; int len = lenP - idxP; res = max(res, len); } printf( %d n , res); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; long long d = (k / (n - 1)), r = (k % (n - 1)); if (r != 0) { cout << (d * n) + r << endl; } else { cout << (d * n) - 1 << endl; } } return 0; }
#include <bits/stdc++.h> int n, m; int main() { long long ans; int b, c, A, B, C, l; scanf( %d%d , &n, &m); n++; m++; ans = 0; for (c = m; c <= n - m; c++) { C = 2 * (n * n - n * c + c * c); for (b = n - m; b >= m; b--) { A = 2 * n - b - c; B = b * (n + c); l = (C - B) / A; if (m > l) l = m - 1; if (l >= n - m) break; ans += n - m - l; } } printf( %I64d n , ans * 3); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; struct node { int c, f, l, i; } a[maxn], b[maxn]; bool cmp1(node a, node b) { return a.f < b.f; } bool cmp2(node a, node b) { return a.c > b.c; } int main() { int n, d, s; scanf( %d%d%d , &n, &d, &s); int nn = n; int m = 0; int ss = s; for (int i = 1; i <= n; i++) { scanf( %d%d%d , &a[i].c, &a[i].f, &a[i].l); a[i].i = i; b[i] = a[i]; if (a[i].c != 0 && a[i].l >= d) { if (m == 0 || a[m].f > a[i].f) m = i; } } int ansf = 0; int ansc = 0; if (m != 0 && a[m].f <= s) { s = s - a[m].f; sort(a + 1, a + 1 + n, cmp2); int sumc = a[1].c; int sum = 1; for (int i = 2; i <= n; i++) { sum++; sumc = sumc - 1 + a[i].c; if (sumc == 0) break; } int j = 0; for (int i = 1; i <= n; i++) if (a[i].l >= d && a[i].i != m) { a[++j] = a[i]; } n = j; sort(a + 1, a + 1 + n, cmp1); for (int i = 1; i <= n; i++) { if (sum == nn) break; if (s >= a[i].f) { sum++; s -= a[i].f; } else break; } ansf = ss - s; ansc = sum; } n = nn; for (int i = 1; i <= n; i++) a[i] = b[i]; s = ss; int sumc = 0; int j = 0; for (int i = 1; i <= n; i++) if (a[i].c == 0 && a[i].l >= d) { a[++j] = a[i]; } n = j; sort(a + 1, a + 1 + n, cmp1); for (int i = 1; i <= n; i++) { if (s >= a[i].f) { sumc++; s -= a[i].f; } else break; } if (ansc < sumc) { ansf = ss - s; ansc = sumc; } else if (ansc == sumc && ansf > ss - s) { ansf = ss - s; ansc = sumc; } printf( %d %d n , ansc, ansf); return 0; }
#include <bits/stdc++.h> using namespace std; bool flag; int vis[(1 << 17)]; int n, m, k; vector<pair<int, int> > vt[(1 << 17)]; void dfs(int u) { for (int i = 0; i < vt[u].size(); i++) { int v = vt[u][i].first; int c = vt[u][i].second; if (vis[v] == -1) { if (c == 1) vis[v] = vis[u]; else vis[v] = !vis[u]; dfs(v); } if (c == 1 && (vis[v] != vis[u])) flag = true; if (c == 0 && (vis[v] == vis[u])) flag = true; } } int main() { scanf( %d%d , &n, &m); for (int i = 0; i < m; i++) { int u, v, c; scanf( %d%d%d , &u, &v, &c); vt[u].push_back(make_pair(v, c)); vt[v].push_back(make_pair(u, c)); } memset(vis, -1, sizeof(vis)); for (int i = 1; i <= n; i++) { if (vis[i] == -1) { vis[i] = 0; dfs(i); k++; } } long long ans = 1; if (flag) { puts( 0 ); return 0; } for (int i = 2; i <= k; i++) ans = ans * 2 % 1000000007; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int n, m, w[210], d[210][30010], f[30010], x[210][30010], u[30010], p; vector<int> a[30010], b[30010]; inline void GG() { printf( -1 n ); exit(0); } inline void line(int i, int j) { a[i].push_back(j); a[j].push_back(i); } inline void dfs(int i, int j, int k, int l) { if (d[k][i] != l) GG(); int u; for (u = 0; u < a[i].size(); u++) if (a[i][u] != j) dfs(a[i][u], i, k, l + 1); } inline void check() { int i; for (i = 1; i <= m; i++) dfs(w[i], 0, i, 0); } inline bool cmp(int i, int j) { return d[1][i] < d[1][j]; } int main() { int i, j, k, l, r; scanf( %d%d , &n, &m); for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { scanf( %d , &d[i][j]); if (d[i][j] == 0) if (w[i]) GG(); else w[i] = j; } if (!w[i]) GG(); for (j = 1; j < i; j++) if (w[i] == w[j]) GG(); } for (i = 2; i <= m; i++) { for (j = 1; j <= n; j++) if (d[i][j] + d[1][j] == d[i][w[1]]) x[i][d[i][j]] = j; for (j = 0; j <= d[i][w[1]]; j++) if (!x[i][j]) GG(); for (j = 0; j < d[i][w[1]]; j++) if (f[x[i][j]] && f[x[i][j]] != x[i][j + 1]) GG(); else f[x[i][j]] = x[i][j + 1]; } for (i = 1; i <= n; i++) if (f[i]) line(i, f[i]); check(); for (i = 1; i <= n; i++) if (!f[i] && i != w[1]) u[++p] = i; else b[i].push_back(i); if (p) { sort(u + 1, u + p + 1, cmp); for (i = 1; i <= p; i++) { for (j = 2, k = w[1]; j <= m; j++) { l = d[1][u[i]] + d[j][u[i]] - d[1][w[j]]; if ((l & 1) || l == 0) GG(); r = d[j][u[i]] - l / 2; r = x[j][r]; if (d[1][r] > d[1][k]) k = r; } j = d[1][u[i]] - d[1][k]; if (b[k].size() >= j) { line(b[k][j - 1], u[i]); if (b[k].size() == j) b[k].push_back(u[i]); } else GG(); } check(); } for (i = 1; i <= n; i++) for (j = 0; j < a[i].size(); j++) if (a[i][j] > i) printf( %d %d n , i, a[i][j]); return 0; }
#include <bits/stdc++.h> using namespace std; int n; int main() { pair<long long, long long> s, e; long long t; cin >> t; cin >> s.first >> s.second >> e.first >> e.second; map<char, int> mp; string str; cin >> str; for (int i = 0; i < t; ++i) { mp[str[i]]++; } char dir[] = { NSWE }; map<char, long long> nd; for (int j = 0; j < 4; ++j) { nd[dir[j]] = 0; } long long xdiff = abs(s.first - e.first); if (s.first < e.first) { nd[ E ] = xdiff; } else if (s.first > e.first) { nd[ W ] = xdiff; } long long ydiff = abs(s.second - e.second); if (s.second < e.second) { nd[ N ] = ydiff; } else if (s.second > e.second) { nd[ S ] = ydiff; } for (int i = 0; i < (int)str.size(); ++i) { if (nd[str[i]] > 0) { nd[str[i]]--; } int chck = 0; for (int j = 0; j < 4; ++j) chck += nd[dir[j]] == 0; if (chck == 4) { cout << i + 1 << endl; return 0; } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; class graph { int v; list<int>* adj; public: graph(int x) { this->v = x; adj = new list<int>[v]; } void add_edge(int a, int b) { adj[a].push_back(b); adj[b].push_back(a); } void count_colors(int a, int& n, int color[], int color_ar[]); }; void graph::count_colors(int a, int& n, int color[], int color_ar[]) { list<int>::iterator itr; int x = 0; } int main() { int n, m, c, x, y; cin >> n >> m; int color[100001] = {0}, Ar[100001] = {0}, g[100001] = {0}; for (int i = 0; i < n; i++) { cin >> color[i]; g[color[i]] = 1; } map<pair<int, int>, int> mp; for (int i = 0; i < m; i++) { cin >> x >> y; if (mp[make_pair(color[x - 1], color[y - 1])] == 0 && color[x - 1] != color[y - 1]) { Ar[color[x - 1]]++; } if (mp[make_pair(color[y - 1], color[x - 1])] == 0 && color[x - 1] != color[y - 1]) { Ar[color[y - 1]]++; } mp[make_pair(color[x - 1], color[y - 1])] = 1; mp[make_pair(color[y - 1], color[x - 1])] = 1; } int max1 = INT_MIN, color1; for (int i = 0; i < 100001; i++) { if (g[i] == 1) { if (Ar[i] > max1) { max1 = Ar[i]; color1 = i; } } } cout << color1; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int M = 1e9 + 7; int n; double v, v2, a[N], b[N]; double x, y; int id = 1; int main() { ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> v >> v2; for (int i = 0; i < n; ++i) { cin >> b[i]; a[i] = 1.0 * b[i] / v; } cin >> x >> y; for (int i = 1; i < n; ++i) { a[i] = a[i] + sqrt((b[i] - x) * (b[i] - x) + y * y) / v2; if (a[i] <= a[id]) { id = i; } } cout << id + 1 << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int maxn = 1e5 + 6; const long long int MOD = 1e9 + 7; vector<int> prim(1000005, 1); int fact[maxn]; long long int binomialCoeff(long long int n, long long int k) { long long int res = 1; if (k > n - k) k = n - k; for (long long int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } bool isVowel(char ch) { if (ch == a || ch == i || ch == e || ch == u || ch == o ) { return true; } return false; } long long int power(long long int x, long long int i, long long int mod) { long long int ans = 1; while (i > 0) { if (i & 1) ans = (ans * x) % mod; i >>= 1; x = (x * x) % mod; } return ans; } long long int modInverse(long long int x, long long int mod) { return power(x, mod - 2, mod); } int nCr(int n, int r) { if (n < r) { return 0; } return (((fact[n] * modInverse(fact[n - r], MOD)) % MOD) * modInverse(fact[r], MOD)) % MOD; } long long int power(int x, unsigned int y) { long long int temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } void erath(int n) { prim[1] = 0; prim[0] = 0; prim[2] = 1; for (int i = 2; i * i <= n; i++) { if (prim[i]) { for (int j = i * i; j <= n; j += i) { prim[j] = 0; } } } } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return (a / gcd(a, b)) * b; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long int n, m, k; int getmid(int l, int r) { return (l + (r - l) / 2); } struct segtree { int siz; vector<pair<long long int, long long int> > sums; void init(int n) { siz = 1; while (siz < n) siz *= 2; sums.assign(2 * siz, {LONG_MAX, 0}); } void set(int i, int v, int cur, int l, int r) { if (r - l == 1) { sums[cur].first = v; sums[cur].second = 1; return; } int m = getmid(l, r); if (i < m) { set(i, v, 2 * cur + 1, l, m); } else set(i, v, 2 * cur + 2, m, r); sums[cur].first = min(sums[2 * cur + 1].first, sums[2 * cur + 2].first); if (sums[2 * cur + 1].first == sums[cur].first && sums[2 * cur + 2].first == sums[cur].first) { sums[cur].second = sums[cur * 2 + 1].second + sums[cur * 2 + 2].second; } else if (sums[2 * cur + 1].first == sums[cur].first) { sums[cur].second = sums[cur * 2 + 1].second; } else { sums[cur].second = sums[cur * 2 + 2].second; } } void set(int i, int v) { set(i, v, 0, 0, siz); } pair<long long int, long long int> sum(int l, int r, int cur, int lx, int rx) { if (lx >= r || l >= rx) return {LONG_MAX, 0}; if (lx >= l && rx <= r) return {sums[cur].first, sums[cur].second}; int mid = getmid(lx, rx); pair<long long int, long long int> s1 = sum(l, r, 2 * cur + 1, lx, mid), s2 = sum(l, r, 2 * cur + 2, mid, rx); if (s1.first < s2.first) { return s1; } else if (s1.first > s2.first) return s2; else return {s1.first, s1.second + s2.second}; } pair<long long int, long long int> sum(int l, int r) { return sum(l, r, 0, 0, siz); } }; int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; int C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } void ans(int top) { cin >> n >> m >> k; long long int mx = max(n, m), mn = min(m, n); if ((mn + (mx - mn)) > k) { cout << -1 << endl; return; } if (m == n) { long long int add = k + 1 - n; if (add % 2 == 0) add -= 2; cout << (mn - 1 + add) << endl; return; } long long int rem = mx - mn; k -= mn; if (rem & 1) { cout << (mn + k - 1) << endl; } else { if (k & 1) k -= 2; cout << (mn + k) << endl; } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1, co = 1; cin >> t; while (t--) { ans(co); co++; } return 0; }
#include <bits/stdc++.h> using namespace std; struct edge { int v, w; }; long long MOD = 1000000007; long long _MOD = 1000000009; double EPS = 1e-10; long long extgcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = (a >= 0) ? 1 : -1; y = 0; return abs(a); } else { long long res = extgcd(b, a % b, y, x); y -= (a / b) * x; return res; } } long long mod_inverse(long long a, long long m) { long long x, y; extgcd(a, m, x, y); return (m + x % m) % m; } long long f(int l, int r, vector<long long>& h, vector<long long>& zi) { long long x = (h[r] - h[l]) * zi[l] % MOD; return (x + MOD) % MOD; } int main() { int N, M; cin >> N >> M; vector<pair<long long, char> > a, b; while (N--) { int l; char c; scanf( %d-%c , &l, &c); if (a.size() && a.back().second == c) a.back().first += l; else a.push_back(pair<long long, char>(l, c)); } while (M--) { int l; char c; scanf( %d-%c , &l, &c); if (b.size() && b.back().second == c) b.back().first += l; else b.push_back(pair<long long, char>(l, c)); } N = a.size(); M = b.size(); if (M == 1) { long long ans = 0; for (int i = 0; i < N; i++) if (a[i].second == b[0].second) ans += max(0LL, a[i].first - b[0].first + 1); cout << ans << endl; return 0; } vector<pair<long long, char> > v; for (pair<long long, char> x : a) v.push_back(x); for (pair<long long, char> x : b) v.push_back(x); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); vector<int> A(N), B(M); for (int i = 0; i < N; i++) A[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1; for (int j = 0; j < M; j++) B[j] = lower_bound(v.begin(), v.end(), b[j]) - v.begin() + 1; vector<long long> z(200001), zi(200001); z[0] = zi[0] = 1; for (int i = 1; i <= 200000; i++) { z[i] = z[i - 1] * 523541 % MOD; zi[i] = mod_inverse(z[i], MOD); } vector<long long> ha(N + 1), hb(M + 1); for (int i = 0; i < N; i++) ha[i + 1] = (ha[i] + A[i] * z[i]) % MOD; for (int j = 0; j < M; j++) hb[j + 1] = (hb[j] + B[j] * z[j]) % MOD; int cnt = 0; for (int i = 0; i + M <= N; i++) { if (!(a[i].second == b[0].second && a[i].first >= b[0].first)) continue; if (!(a[i + M - 1].second == b[M - 1].second && a[i + M - 1].first >= b[M - 1].first)) continue; if (f(i + 1, i + M - 1, ha, zi) != f(1, M - 1, hb, zi)) continue; cnt++; } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; const int INF = ((long long)1E9 + 7); template <class C> void mini(C& a, C b) { a = min(a, b); } template <class C> void maxi(C& a, C b) { a = max(a, b); } const int LG = 20; struct node { int par; vector<int> inc; bool monk; int aim; vector<pair<int, int> > mx; int up[LG]; int depth; int dlow, dup; node() : par(-1), inc(), monk(false), aim(-1), depth(0), dlow(0), dup(0) {} }; struct dart { bool down; int rev; int dist; int head; int far; list<int> pil; dart(int _head = 0, int _dist = 0) : down(false), rev(0), dist(_dist), head(_head), far(-INF), pil() {} }; const int MN = 100100; node T[MN]; dart D[2 * MN]; void assign(int i, int up, int d = 0) { T[i].par = up; T[i].depth = d; for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); it != (T[i].inc).end(); ++it) { if (*it == up) continue; D[*it].down = true; assign(D[*it].head, D[*it].rev, d + 1); } } void distUp(int i) { int mx = -INF; int up = T[i].par; for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); it != (T[i].inc).end(); ++it) { if (*it == up) continue; distUp(D[*it].head); maxi(mx, D[*it].far); } if (T[i].monk) maxi(mx, 0); if (up != -1) { D[D[up].rev].far = mx + D[up].dist; } } void distDown(int i) { int up = T[i].par; T[i].mx.push_back((up != -1) ? pair<int, int>(D[up].far, up) : pair<int, int>(-INF, -1)); T[i].mx.push_back((T[i].monk) ? pair<int, int>(0, -1) : pair<int, int>(-INF, -1)); T[i].mx.push_back(pair<int, int>(-INF, -1)); sort((T[i].mx).begin(), (T[i].mx).end(), greater<pair<int, int> >()); for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); it != (T[i].inc).end(); ++it) { if (*it == up) continue; if (D[*it].far > T[i].mx[2].first) { T[i].mx[2] = make_pair(D[*it].far, *it); sort((T[i].mx).begin(), (T[i].mx).end(), greater<pair<int, int> >()); } } for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); it != (T[i].inc).end(); ++it) { if (*it == up) continue; int j = (*it == T[i].mx[0].second); D[D[*it].rev].far = T[i].mx[j].first + D[*it].dist; distDown(D[*it].head); } } void goUp(int i) { int up = T[i].par; if (T[i].monk) { if (T[i].mx[0].first == T[i].mx[1].first) T[i].aim = i; else { int rv = D[T[i].mx[0].second].rev; D[rv].pil.insert(D[rv].pil.end(), i); } } for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); it != (T[i].inc).end(); ++it) { if (*it == up) continue; goUp(D[*it].head); int j = 0, k = 1; if (T[i].mx[0].second == *it) j = 1, k = 2; else if (T[i].mx[1].second == *it) k = 2; int a = T[i].mx[j].second; int b = T[i].mx[k].second; if (a == -1 || (b != -1 && D[a].far == D[b].far)) { for (__typeof((D[*it].pil).begin()) jt = ((D[*it].pil).begin()); jt != (D[*it].pil).end(); ++jt) { T[*jt].aim = i; } } else { D[D[a].rev].pil.splice(D[D[a].rev].pil.end(), D[*it].pil); } } } void goDown(int i) { int up = T[i].par; if (up != -1) { int j = 0, k = 1; if (T[i].mx[0].second == up) j = 1, k = 2; else if (T[i].mx[1].second == up) k = 2; int a = T[i].mx[j].second; int b = T[i].mx[k].second; if (a == -1 || (b != -1 && D[a].far == D[b].far)) { for (__typeof((D[up].pil).begin()) jt = ((D[up].pil).begin()); jt != (D[up].pil).end(); ++jt) { T[*jt].aim = i; } } else { D[D[a].rev].pil.splice(D[D[a].rev].pil.end(), D[up].pil); } } for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); it != (T[i].inc).end(); ++it) { if (*it != up) goDown(D[*it].head); } } void add(int i, int j) { if (T[i].depth < T[j].depth) swap(i, j); T[i].dlow++; T[j].dlow++; int dd = T[i].depth - T[j].depth; for (int k = 0; k < (LG); ++k) { if (dd & (1 << k)) { i = T[i].up[k]; } } for (int k = (LG - 1); k >= (0); --k) { if (T[i].up[k] != T[j].up[k]) { i = T[i].up[k]; j = T[j].up[k]; } } if (i != j) { i = T[i].up[0]; j = T[j].up[0]; } T[i].dup++; } int bst = 0; int cnt = 0; int final(int i) { int v = 0; for (__typeof((T[i].inc).begin()) it = ((T[i].inc).begin()); it != (T[i].inc).end(); ++it) { if (*it != T[i].par) v += final(D[*it].head); } v += T[i].dlow; v -= T[i].dup; if (!T[i].monk) { if (v > bst) { bst = v; cnt = 0; } if (v == bst) ++cnt; } return v - T[i].dup; } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < (m); ++i) { int x; cin >> x; --x; T[x].monk = true; } for (int i = 0; i < (n - 1); ++i) { int a, b, c; cin >> a >> b >> c; --a; --b; D[2 * i] = dart(b, c); T[a].inc.push_back(2 * i); D[2 * i + 1] = dart(a, c); T[b].inc.push_back(2 * i + 1); } for (int i = 0; i < (2 * (n - 1)); ++i) D[i].rev = i ^ 1; assign(0, -1); distUp(0); distDown(0); goUp(0); goDown(0); for (int i = 0; i < (n); ++i) { for (int j = 0; j < (LG); ++j) T[i].up[j] = -1; int up = T[i].par; if (up != -1) T[i].up[0] = D[up].head; } for (int j = 0; j < (LG - 1); ++j) { for (int i = 0; i < (n); ++i) { if (T[i].up[j] != -1) T[i].up[j + 1] = T[T[i].up[j]].up[j]; } } for (int i = 0; i < (n); ++i) { if (T[i].monk) add(i, T[i].aim); } final(0); cout << bst << << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(long long N, int pos) { return (bool)(N & (1LL << pos)); } int k, n; int ase[30]; string arr[2505]; vector<int> pos; vector<int> adj[5005]; vector<string> vec; int main() { cin >> k >> n; for (int i = 1; i <= k; i++) { cin >> arr[i]; } if (n == 1) { cout << -1 << endl; return 0; } if (n == 2) { swap(arr[1][0], arr[1][1]); string s1 = arr[1]; swap(arr[1][0], arr[1][1]); for (int i = 1; i <= k; i++) { if (arr[i] != arr[1]) { cout << -1 << endl; return 0; } } cout << s1 << endl; return 0; } if (k == 1) { swap(arr[1][0], arr[1][1]); cout << arr[1] << endl; return 0; } bool hobe = true, first = true; int nul = 0; for (int i = 2; i <= k; i++) { int cnt = 0; for (int j = 0; j < n; j++) { if (arr[1][j] != arr[i][j]) { if (first) pos.push_back(j); adj[i].push_back(j); cnt++; } } if (adj[i].size() == 0) nul++; if (cnt == 1 || cnt > 4) hobe = false; if (cnt > 0 && first) first = false; } if (nul == k - 1) { swap(arr[1][0], arr[1][1]); cout << arr[1] << endl; return 0; } if (!hobe) cout << -1 << endl; else { for (int i = 0; i < pos.size(); i++) { for (int j = 0; j < n; j++) { if (pos[i] == j) continue; swap(arr[1][pos[i]], arr[1][j]); vec.push_back(arr[1]); swap(arr[1][pos[i]], arr[1][j]); } } int mx = 0; for (int i = 0; i < n; i++) { ase[arr[1][i] - a ]++; mx = max(mx, ase[arr[1][i] - a ]); } if (mx > 1) vec.push_back(arr[1]); string ans = -1 ; for (int p = 0; p < vec.size(); p++) { int cnt = 0; for (int i = 2; i <= k; i++) { int mis = 0; char m1[5001], m2[5001]; for (int j = 0; j < n; j++) { if (vec[p][j] != arr[i][j]) { mis++; m1[mis] = vec[p][j]; m2[mis] = arr[i][j]; } } if (mis == 2 && m1[1] == m2[2] && m1[2] == m2[1]) cnt++; else if (mis == 0 && mx >= 2) cnt++; else break; } if (cnt == k - 1) { ans = vec[p]; break; } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0}, mp[100005], b; int cheak(int x, int y) { if (abs(x - y) == m || abs(x - y) == 1) return 1; else return 0; } void dfs(int x) { if (x == n * m) { b = 1; cout << YES << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) printf( %d , mp[i * m + j] + 1); printf( n ); } return; } for (int i = x; i < n * m; i++) { swap(mp[i], mp[x]); if ((x / m == 0 || cheak(mp[x], mp[(x / m - 1) * m + x % m]) == 0) && (x % m == 0 || cheak(mp[x], mp[x / m * m + x % m - 1]) == 0)) { dfs(x + 1); if (b == 1) return; swap(mp[i], mp[x]); } } } int main() { scanf( %d%d , &n, &m); for (int i = 0; i < n * m; i++) mp[i] = i; dfs(0); if (b == 0) printf( NO ); return 0; }
#include <bits/stdc++.h> using namespace std; struct coordinate { float x; float y; }; float check1(coordinate c1, coordinate c2, coordinate c3, coordinate c4, coordinate x1) { float ans1 = (x1.y - c1.y) + (x1.x - c1.x); float ans2 = (x1.y - c3.y) + (x1.x - c3.x); if (ans1 == 0 || ans2 == 0) return 0; else { float ans = ans1 / ans2; return ans; } } float check2(coordinate c1, coordinate c2, coordinate c3, coordinate c4, coordinate x1) { float m = (c3.y - c1.y) / (c3.x - c1.x); float ans1 = (x1.y - c1.y) - m * (x1.x - c1.x); float ans2 = (x1.y - c2.y) - m * (x1.x - c2.x); if (ans1 == 0 || ans2 == 0) return 0; else { float ans = ans1 / ans2; return ans; } } int main() { float n, d; cin >> n >> d; coordinate c1, c2, c3, c4; c1.x = d; c1.y = 0; c2.x = 0; c2.y = d; c3.x = n; c3.y = n - d; c4.x = n - d; c4.y = n; int t; cin >> t; while (t--) { coordinate x1; cin >> x1.x >> x1.y; float ans1 = check1(c1, c2, c3, c4, x1); float ans2 = check2(c1, c2, c3, c4, x1); if (ans1 < 0 && ans2 < 0) cout << YES << endl; else if (ans1 == 0 || ans2 == 0) cout << YES << endl; else cout << NO << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long mul_mod(long long a, long long b, long long MOD) { return a * b; } struct myhash { long long chash[401000][2]; long long mult[401000][2]; long long b1, M1, b2, M2; myhash(long long b1 = 1000000021, long long M1 = 1200000000000000013LL, long long b2 = 1000000009, long long M2 = 1200000000000000011LL) : b1(b1), M1(M1), b2(b2), M2(M2) { calc(); } void calc() { mult[0][0] = mult[0][1] = 1; for (long long i = 1; i < 401000; i++) { mult[i][0] = mul_mod(mult[i - 1][0], b1, M1); mult[i][1] = mul_mod(mult[i - 1][1], b2, M2); } } void compute(vector<long long> &st) { chash[0][0] = chash[0][1] = st[0] + 1; for (int i = 1; i < (int)st.size(); i++) { chash[i][0] = (mul_mod(chash[i - 1][0], b1, M1) + st[i] + 1); chash[i][1] = (mul_mod(chash[i - 1][1], b2, M2) + st[i] + 1); } } pair<long long, long long> substr(long long l, long long r) { if (!l) return make_pair(chash[r][0], chash[r][1]); long long p1 = (chash[r][0] - mul_mod(chash[l - 1][0], mult[r - l + 1][0], M1)); long long p2 = (chash[r][1] - mul_mod(chash[l - 1][1], mult[r - l + 1][1], M2)); return make_pair(p1, p2); } } h1, h2; long long n, m, a[401000]; vector<long long> diff; int main() { scanf( %lld %lld , &n, &m); for (int i = 0; i < n; i++) { scanf( %lld , &a[i]); if (i) diff.push_back((a[i] - a[i - 1] + m) % m); } diff.push_back((a[0] - a[n - 1] + m) % m); for (int i = 0, sz = diff.size(); i < sz; i++) diff.push_back(diff[i]); h1.compute(diff); reverse(diff.begin(), diff.end()); h2.compute(diff); reverse(diff.begin(), diff.end()); long long acum = 0; for (int i = 0; i < n; i++) acum += diff[i]; assert(acum % m == 0); vector<long long> ans; for (int i = n; i < diff.size(); i++) { acum += diff[i]; if (h1.substr(0, i) == h2.substr(diff.size() - i - 1, diff.size() - 1)) { ans.push_back((2LL * a[0] + acum) % m); } } sort(ans.begin(), ans.end()); ans.resize(unique(ans.begin(), ans.end()) - ans.begin()); printf( %d n , ans.size()); for (int i = 0; i < ans.size(); i++) { printf( %lld%c , ans[i], (i + 1 == ans.size()) ? n : ); } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; long long mpow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = res * x % mod; x = x * x % mod; n /= 2; } return res; } long long s2(long long n) { n %= mod; return n * (n - 1) % mod; } long long s3(long long n) { n %= mod; return n * (n - 1) % mod * (n - 2) % mod; } long long eval(vector<long long> p, long long t) { long long res = 0; for (int i = p.size() - 1; i >= 0; --i) { res = (res * t + p[i]) % mod; } return res; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); long long n; cin >> n; vector<long long> p(4); long long inv2 = mpow(2, mod - 2); long long inv3 = mpow(3, mod - 2); p[3] = (inv3) % mod; p[2] = (-(n + 2) % mod * inv2) % mod; p[1] = (-inv3 + (n + 2) % mod * inv2) % mod; p[0] = (-s3(n + 2) * inv3 + (n + 2) % mod * inv2 % mod * s2(n + 1)) % mod; long long s = eval(p, 1); for (long long x = 1; x * x <= n; ++x) { s = (s + 4 * eval(p, x * x % mod)) % mod; } vector<vector<long long> > pws(4, vector<long long>(1000003)); for (int i = 0; i < pws.size(); ++i) { for (long long j = 1; j < pws[i].size(); ++j) { long long j2 = j * j % mod; long long add = 1; for (int t = 0; t < i; ++t) add = add * j2 % mod; pws[i][j] = (pws[i][j - 1] + add) % mod; } } long long y = sqrt(n) + 2; for (long long x = 1; x * x < n; ++x) { while (x * x + y * y > n) --y; long long x2 = x * x % mod; long long s1 = 0; s1 = (s1 + y * p[0]) % mod; s1 = (s1 + (x2 * y % mod + pws[1][y]) * p[1]) % mod; s1 = (s1 + (x2 * x2 % mod * y % mod + 2 * x2 * pws[1][y] % mod + pws[2][y]) * p[2]) % mod; s1 = (s1 + (x2 * x2 % mod * x2 % mod * y % mod + 3 * x2 * x2 % mod * pws[1][y] % mod + 3 * x2 * pws[2][y] % mod + pws[3][y]) * p[3]) % mod; s = (s + 4 * s1) % mod; } cout << (s % mod + mod) % mod << endl; return 0; }
#include <bits/stdc++.h> using namespace std; bool cmp(int a, int b) { if (a > b) return true; else return false; } struct pack { long long t, T, x, cost; }; int main() { long long ans = 0, n, m; cin >> n >> m; vector<pack> data; for (int i = 0; i < (n); ++i) { pack temp; cin >> temp.t >> temp.T >> temp.x >> temp.cost; data.push_back(temp); } for (int i = 0; i < (n); ++i) { data[i].T = data[i].T - data[i].t; if (data[i].T > 0) { long long ans1; if (m > data[i].T) ans1 = data[i].cost + m * data[i].x; else ans1 = data[i].cost; if (m > data[i].T) { long long k = data[i].cost * ((m + data[i].T - 1) / data[i].T); ans1 = min(ans1, k); } if (m > 2 * data[i].T) { long long ans2 = m % data[i].T + data[i].T; ans1 = min(ans1, data[i].cost * (1 + (m - ans2) / data[i].T) + ans2 * data[i].x); } ans += ans1; } else { ans += data[i].cost + m * data[i].x; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long infl = 0x3f3f3f3f3f3f3f3fLL; template <class T> void rd(T &a) { char ch = getchar(); bool ng = 0; a = 0; for (; ch < 0 || ch > 9 ; ch = getchar()) if (ch == - ) ng = 1; for (; ch >= 0 && ch <= 9 ; ch = getchar()) a = (a << 1) + (a << 3) + (ch ^ 0 ); if (ng) a = ~a + 1; } template <class T> void wrt(T a) { if (a < 0) { putchar( - ); wrt(~a + 1); } else { if (a > 9) wrt(a / 10); putchar(a % 10 ^ 0 ); } } const int N = 5e5 + 5; int n, q, nxt[N], a[N], ans[N]; map<int, int> pos; vector<pair<int, int> > Q[N]; int fen[N]; void upd(int p, int x) { for (; p <= n; p += p & (-p)) fen[p] = min(fen[p], x); } int get(int p) { int res = inf; for (; p; p -= p & (-p)) res = min(res, fen[p]); return res; } int main() { rd(n), rd(q); for (int i = 1; i <= n; ++i) rd(a[i]); for (int i = n; i; --i) { if (pos.count(a[i])) nxt[i] = pos[a[i]]; else nxt[i] = -1; pos[a[i]] = i; } for (int i = 1; i <= n; ++i) fen[i] = inf; for (int i = 1; i <= q; ++i) { int l, r; rd(l), rd(r); Q[l].push_back(pair<int, int>(r, i)); } for (int i = n; i; --i) { if (~nxt[i]) upd(nxt[i], nxt[i] - i); for (int j = 0; j < (int)((Q[i]).size()); ++j) { int r = Q[i][j].first, id = Q[i][j].second; ans[id] = get(r); } } for (int i = 1; i <= q; ++i) { if (ans[i] >= inf) puts( -1 ); else wrt(ans[i]), puts( ); } return 0; }
#include <bits/stdc++.h> #define PI atan(1.0)*4 #define rp(i,s,t) for (register int i = (s); i <= (t); i++) #define RP(i,t,s) for (register int i = (t); i >= (s); i--) #define sc(x) scanf( %d ,&x) #define scl(x) scanf( %lld ,&x) #define ll long long #define ull unsigned long long #define mst(a,b) memset(a,b,sizeof(a)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pii pair<int,int> #define pll pair<ll,ll> #define pil pair<int,ll> #define m_p make_pair #define p_b push_back #define ins insert #define era erase #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3f #define dg if(debug) #define outval(a) cout << Debuging...| << #a << : << a << n ; using namespace std; int debug = 0; ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; } ll lcm(ll a,ll b){ return a/gcd(a,b)*b; } inline int read(){ int s=0,f=1; char ch=getchar(); while(ch< 0 ||ch> 9 ){ if(ch== - ) f=-1; ch=getchar(); } while(ch>= 0 &&ch<= 9 ){ s=s*10+ch- 0 ; ch=getchar(); } return s*f; } const int N = 1e6+7; const double eps = 1e-7; int a[N]; int f[N]; void solve(){ int n=read(),m=read(); rp(i,1,n) a[i]=read(); rp(i,1,n) f[i]=0; RP(i,n,1){ if(a[i]!=i) break; else f[i]=1; } int flag=1; rp(i,1,n){ if(f[i]==0){ flag=0; } } double ans=0.0; double cur=1; int ff=0; rp(i,1,m){ int r;double p; scanf( %d%lf ,&r,&p); // printf( %d %lf n ,r,p); if(r==n||f[r+1]){ ans+=cur*p; cur*=(1.0-p); if(fabs(p-1.0)<=eps) ff=1; } } if(ff||flag){ printf( 1.000000 n ); return ; } printf( %.6f n ,ans); } int main(){ //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef ONLINE_JUDGE #else freopen( in.txt , r , stdin); //debug = 1; #endif time_t beg, end; //if(debug) beg = clock(); int T=read(); while(T--) solve(); /* if(debug) { end = clock(); printf( time:%.2fs n , 1.0 * (end - beg) / CLOCKS_PER_SEC); } */ return 0; }
#include <bits/stdc++.h> const int N = 2e5 + 3; int n, m, l[N + N + N], r[N + N + N], k; char a[N + N + N], b[N]; int main() { int p, q; scanf( %d%d%s , &n, &m, a + 1); for (int i = 1; i <= n; i++) a[i + n] = a[i + n + n] = a[i]; for (int i = 1; i <= n; i++) b[i] = a[i]; for (int i = 1; i <= n; i++) if (a[i + n] == a[i + n - 1]) goto Brk; for (int i = 1 + (m & 1); i <= n + (m & 1); i++) putchar(a[i]); return 0; Brk:; k = 0; for (int i = 1; i <= n * 3; i++) if (a[i] == a[i - 1] && a[i] != a[i + 1]) r[++k] = i; k = 0; for (int i = 1; i <= n * 3; i++) if (a[i] != a[i - 1] && a[i] == a[i + 1]) l[++k] = i; for (int i = 1 + n; i <= n * 2; i++) if (a[i] != a[i - 1] && a[i] != a[i + 1]) { q = l[std::lower_bound(l + 1, l + 1 + k, i) - l]; p = r[std::lower_bound(r + 1, r + 1 + k, i) - r - 1]; if (q - i > m && i - p > m) b[i - n] = a[i + (m & 1)]; else if (q - i < i - p) b[i - n] = a[q]; else b[i - n] = a[p]; } for (int i = 1; i <= n; i++) putchar(b[i]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, t, k, x, d[200001] = {}, last_d = 1, last = 0, a; long long sum = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> t; if (1 == t) { cin >> a >> x; sum += a * x; if (last_d == 0) { last += x; } else if (last_d == a) { last += x; } else { d[a] -= x; } } else if (2 == t) { cin >> k; sum += k; d[last_d++] = k - last; last = k; } else { sum -= last; last -= d[--last_d]; } cout << setiosflags(ios::fixed) << setprecision(6) << ((double)sum) / ((double)last_d) << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, d; cin >> n >> d; int t[n], sum = 0, rest = (n - 1) * 10; for (int i = 0; i < n; i++) { cin >> t[i]; sum = sum + t[i]; } if ((rest + sum) > d) { cout << -1 ; return 0; } else { int bal = d - sum, max; max = bal / 5; cout << max; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /stack:100000000 ) using namespace std; const int N = 200; char s[N][N]; int main() { int C; cin >> C; int n = 3; while (n * (n - 1) * (n - 2) / 6 <= C) ++n; int m = --n; C -= n * (n - 1) * (n - 2) / 6; vector<int> add; while (C > 0) { ++n; for (int i = 2; i <= m; i++) { if (i * (i - 1) / 2 > C) { --i; add.push_back(i); C -= i * (i - 1) / 2; break; } } } assert(n <= 100); memset(s, 0 , sizeof(s)); for (int(i) = 0; (i) < (m); (i)++) for (int(j) = 0; (j) < (i); (j)++) s[i][j] = s[j][i] = 1 ; for (int i = m; i < n; i++) { for (int(j) = 0; (j) < (add[i - m]); (j)++) { s[i][j] = s[j][i] = 1 ; } } printf( %d n , n); for (int(i) = 0; (i) < (n); (i)++) { s[i][n] = 0; puts(s[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int64_t INF = 0x7FFFFFFF; const int MAX = 1; unordered_map<int, int> maps; unordered_map<int, int> cnt; int main(int argc, char* args[]) { int n; scanf( %d , &n); for (int(i) = (0), _n = (n - 1); (i) <= _n; (i)++) { int x; scanf( %d , &x); maps[x]++; } for (auto it : maps) { int x = it.second; for (int(i) = (1), _n = (x); (i) <= _n; (i)++) { cnt[i]++; } } int ans = 0; for (auto it : cnt) { ans += it.second - 1; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; bool lucky(char a) { if (a == 4 || a == 7 ) return true; else return false; } int main() { long long int a, b, c, d; vector<bool> res; cin >> a >> b >> c >> d; if (abs(c - d) > 1 || min(a, b) < max(c, d)) { puts( -1 ); return 0; } if (c > d) { for (long long int i = 0; i < ((long long int)(a - d)); i++) cout << 4; for (long long int i = 0; i < ((long long int)(d)); i++) cout << 74; for (long long int i = 0; i < ((long long int)(b - d)); i++) cout << 7; } else if (c < d) { cout << 7; for (long long int i = 0; i < ((long long int)(a - c)); i++) cout << 4; for (long long int i = 0; i < ((long long int)(c - 1)); i++) cout << 74; for (long long int i = 0; i < ((long long int)(b - c)); i++) cout << 7; cout << 4; } else { if (a + b < c + d + 1) { puts( -1 ); return 0; } for (long long int i = 0; i < ((long long int)(a - c)); i++) cout << 4; if (a - c > 0) { for (long long int i = 0; i < ((long long int)(c - 1)); i++) cout << 74; for (long long int i = 0; i < ((long long int)(b - c + 1)); i++) cout << 7; cout << 4; } else { cout << 7; for (long long int i = 0; i < ((long long int)(c)); i++) cout << 47; for (long long int i = 0; i < ((long long int)(b - 1 - c)); i++) cout << 7; } } puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; char buf[1 << 21], *p1 = buf, *p2 = buf; inline int qread() { register char c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++); register int x = 0, f = 1; while (c < 0 || c > 9 ) { if (c == - ) f = -1; c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++); } while (c >= 0 && c <= 9 ) { x = (x << 3) + (x << 1) + c - 48; c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++); } return x * f; } inline int Abs(const int& x) { return (x > 0 ? x : -x); } inline int Max(const int& x, const int& y) { return (x > y ? x : y); } inline int Min(const int& x, const int& y) { return (x < y ? x : y); } struct Segtree { long long sumv[800005], sumidx[800005]; inline void Modify(int p, int pl, int pr, int id) { if (pl == pr) { sumv[p]++; sumidx[p] += id; return; } int mid = pl + pr >> 1; if (mid >= id) Modify(p << 1, pl, mid, id); else Modify(p << 1 | 1, mid + 1, pr, id); sumv[p] = sumv[p << 1] + sumv[p << 1 | 1]; sumidx[p] = sumidx[p << 1] + sumidx[p << 1 | 1]; } inline int Kth(int p, int pl, int pr, int k) { if (pl == pr) return pl; int mid = pl + pr >> 1; if (k <= sumv[p << 1]) return Kth(p << 1, pl, mid, k); else return Kth(p << 1 | 1, mid + 1, pr, k - sumv[p << 1]); } inline long long Sumv(int p, int pl, int pr, int l, int r) { if (l > r) return 0; if (pl == l && pr == r) return sumv[p]; int mid = pl + pr >> 1; if (mid >= r) return Sumv(p << 1, pl, mid, l, r); else if (mid + 1 <= l) return Sumv(p << 1 | 1, mid + 1, pr, l, r); else return Sumv(p << 1, pl, mid, l, mid) + Sumv(p << 1 | 1, mid + 1, pr, mid + 1, r); } inline long long Sumidx(int p, int pl, int pr, int l, int r) { if (l > r) return 0; if (pl == l && pr == r) return sumidx[p]; int mid = pl + pr >> 1; if (mid >= r) return Sumidx(p << 1, pl, mid, l, r); else if (mid + 1 <= l) return Sumidx(p << 1 | 1, mid + 1, pr, l, r); else return Sumidx(p << 1, pl, mid, l, mid) + Sumidx(p << 1 | 1, mid + 1, pr, mid + 1, r); } }; Segtree sgt; int n, a[200005], idx[200005]; inline void Read() { n = qread(); for (int i = 1; i <= n; i++) idx[a[i] = qread()] = i; } inline long long Sum(int l, int k) { return 1ll * k * (l + l + k - 1) / 2; } inline void Solve() { long long inv = 0; for (int i = 1; i <= n; i++) { inv += sgt.Sumv(1, 1, n, idx[i] + 1, n); sgt.Modify(1, 1, n, idx[i]); int midv = sgt.Kth(1, 1, n, i + 1 >> 1); long long lsum = sgt.Sumidx(1, 1, n, 1, midv - 1), rsum = sgt.Sumidx(1, 1, n, midv + 1, n); printf( %lld , inv + Sum(midv - (i - 1 >> 1), i - 1 >> 1) - lsum + rsum - Sum(midv + 1, i >> 1)); } } int main() { Read(); Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int s[3000005], ps[3000005][5]; int n, ans; vector<int> temp; int po(int a, int b) { if (b == 0) return 1; return a * po(a, b - 1); } void prep() { int ptr1 = 1, ptr2 = 1, maxi = 0; ps[1][0] = 1; for (int i = (2); i <= (3000000); i++) { while (po(ptr1, 4) < i) ptr1++; while (po(ptr2 + 1, 2) <= i) ptr2++; for (int j = (0); j <= (4); j++) ps[i][j] = ps[i - 1][j]; while (ps[ptr2][s[i]] - ps[ptr1 - 1][s[i]] > 0) s[i]++; ps[i][s[i]]++; } } int f(long long x) { if (x <= 3000000) return s[x]; int ptr1 = ceil(pow(x, 0.25)), ptr2 = floor(pow(x, 0.5)); int ret = 0; while (ps[ptr2][ret] - ps[ptr1 - 1][ret] > 0) ret++; return ret; } int main() { prep(); scanf( %d , &n); for (int i = (1); i <= (n); i++) { long long x; scanf( %lld , &x); ans ^= f(x); } if (ans) printf( Furlo n ); else printf( Rublo n ); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int mi = 1000000007; map<pair<int, int>, int> mp; int minima[2000000]; int f(int a, int b) { if (mp.find(make_pair(a, b)) != mp.end()) { return mp[make_pair(a, b)]; } cout << ? << a << << b << endl; int s; cin >> s; chmin(mi, s); return mp[make_pair(a, b)] = s; } void SMAWK(vector<int> js, int ib, int ie, int id) { if (ib > ie) return; vector<int> js2; for (int q = 0, i = ib; q < js.size(); ++q) { while (!js2.empty() && f(i, js2.back()) >= f(i, js[q])) { js2.pop_back(); i -= id; } if (js2.size() != (ie - ib) / id) { js2.push_back(js[q]); i += id; } } SMAWK(js2, ib + id, ie, id * 2); for (int i = ib, q = 0; i <= ie; i += id * 2) { int jt = (i + id <= ie ? minima[i + id] : js.back()); int fm = 999999999 * 2, fq; for (; q < js.size(); ++q) { if (fm > (fq = f(i, js[q]))) { fm = fq; minima[i] = js[q]; } if (js[q] == jt) break; } } } void rowMinimaTM(int ib, int ie, int jb, int je) { vector<int> js; for (int j = jb; j <= je; ++j) js.push_back(j); SMAWK(js, ib, ie, 1); } int main() { int n, m; cin >> n >> m; rowMinimaTM(1, n, 1, m); cout << ! << mi << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int maxn = 2e5 + 5; long long int sz[maxn], dp[maxn], ans[maxn]; vector<long long int> g[maxn]; long long int n; long long int dp1(long long int node, long long int parent) { sz[node] = 1; for (auto& itr : g[node]) if (itr != parent) sz[node] += dp1(itr, node); return sz[node]; } long long int dp2(long long int node, long long int parent) { dp[node] = sz[node]; for (auto& itr : g[node]) if (itr != parent) dp[node] += dp2(itr, node); return dp[node]; } void dp3(long long int node, long long int parent, long long int fromParent = 0) { if (parent == -1) ans[node] = dp[node]; else ans[node] = n - 2 * sz[node] + ans[parent]; for (auto& itr : g[node]) if (itr != parent) dp3(itr, node); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (long long int i = 1; i < n; i++) { long long int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dp1(1, -1); dp2(1, -1); dp3(1, -1); long long int answer = 0; for (long long int i = 1; i <= n; i++) answer = max(answer, ans[i]); cout << answer; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[16] = {0}; int index = 0; for (int i = 1; i <= n; i++) { string s = to_string(i); bool lucky = true; for (int j = 0; j < (s.length()); j++) { if (s[j] != 4 && s[j] != 7 ) { lucky = false; break; } } if (lucky) { a[index] = i; index++; } } for (int i = 0; i < 16; i++) { if (a[i] != 0) { if (n % a[i] == 0) { cout << YES ; return 0; } } } cout << NO ; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; set<long long> ss; for (int i = 1; i <= 1e6; i++) { long long need = i * 2; long long cards = (i * (i + 1)) / 2; long long lft = cards - i; need += lft * 3; if (need > 1e9) break; ss.insert(-need); } while (t--) { long long n; cin >> n; long long cnt = 0; while (true) { set<long long>::iterator it = ss.lower_bound(-n); if (it == ss.end()) break; cnt++; n += *it; } cout << cnt << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; struct edge { int v, nxt, c; } e[1505 * 1505 * 2]; int p[1505], eid; void init() { memset(p, -1, sizeof p); eid = 0; } void insert(int u, int v, int c) { e[eid].v = v; e[eid].c = c; e[eid].nxt = p[u]; p[u] = eid++; } void add(int u, int v, int c) { insert(u, v, c); insert(v, u, 0); } int ch[10000005][2], cnt[10000005], tot, ys[1505], nxt[10000005], g[755][755], L[1505], R[1505], ha[10000005]; string st; queue<int> q; void insert(int id) { int p = 0, len = st.length(); L[id] = ha[0] + 1; for (int i = 0; i < len; i++) { ha[++ha[0]] = st[i] - a ; if (!ch[p][st[i] - a ]) ch[p][st[i] - a ] = ++tot; p = ch[p][st[i] - a ]; } cnt[p] = id; R[id] = ha[0]; } void build() { for (int i = 0; i < 2; i++) if (ch[0][i]) q.push(ch[0][i]); while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < 2; i++) { if (!ch[u][i]) ch[u][i] = ch[nxt[u]][i]; else nxt[ch[u][i]] = ch[nxt[u]][i], q.push(ch[u][i]); } } } int vis[10000005]; void run(int id) { int p = 0; vis[0] = 1; for (int i = L[id]; i <= R[id]; i++) { p = ch[p][ha[i]]; int pp = p, tt; while (pp) { if (cnt[pp] && cnt[pp] != id) { g[id][cnt[pp]] = 1; break; } pp = nxt[pp]; } int haha = pp; pp = p; while (pp) { if (cnt[pp] && cnt[pp] != id) { break; } tt = pp; pp = nxt[pp]; nxt[tt] = haha; } } } int dis[1505], S, T, n; int bfs() { memset(dis, -1, sizeof dis); dis[S] = 1; q.push(S); while (q.size()) { int u = q.front(); q.pop(); for (int i = p[u]; i + 1; i = e[i].nxt) { int v = e[i].v; if (dis[v] == -1 && e[i].c) { dis[v] = dis[u] + 1; q.push(v); } } } return dis[T] != -1; } int dfs(int u, int flow) { if (u == T) return flow; int ret = 0; for (int i = p[u]; i + 1; i = e[i].nxt) { int c = e[i].c, v = e[i].v; if (c && dis[v] == dis[u] + 1) { int tmp = dfs(v, min(flow, c)); ret += tmp, flow -= tmp; e[i].c -= tmp, e[i ^ 1].c += tmp; if (!flow) break; } } if (!ret) dis[u] = -1; return ret; } int Dinic() { int ret = 0; for (; bfs();) ret += dfs(S, 2147483646 / 2); return ret; } int main() { ios::sync_with_stdio(false); init(); cin >> n; T = 2 * n + 1; for (int i = 1; i <= n; i++) cin >> st, insert(i); build(); for (int i = 1; i <= n; i++) run(i); for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) g[i][j] |= (g[i][k] & g[k][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (g[i][j] && i != j) add(i, j + n, 1); for (int i = 1; i <= n; i++) add(S, i, 1), add(i + n, T, 1), vis[i + n] = 0; cout << n - Dinic() << endl; for (int i = 1; i <= n; i++) if (dis[i] != -1 && dis[i + n] == -1) cout << i << ; return 0; }
#include <bits/stdc++.h> const int MAXN = 1e5 + 5; enum { GRASS = G , WATER = W , LAVA = L }; enum { G = 5, W = 3, L = 1 }; long long a[MAXN]; char s[MAXN]; int n; bool water; long long grass, stamina, t; int main() { scanf( %d , &n); for (int i = 0; i < n; ++i) { scanf( %lld , &a[i]); } scanf( %s , s); water = false; grass = stamina = t = 0LL; for (int i = 0; i < n; ++i) { switch (s[i]) { case GRASS: grass += 2 * a[i]; stamina += 2 * a[i]; grass = std::min(stamina / 2, grass); t += G * a[i]; break; case WATER: water = true; stamina += 2 * a[i]; t += W * a[i]; break; case LAVA: if (stamina < 2 * a[i]) { t += ((water) ? W : G) * (a[i] - stamina / 2); stamina = 0; } else { stamina -= 2 * a[i]; } t += L * a[i]; grass = std::min(stamina / 2, grass); break; } } t -= (G - L) / 2 * grass; t -= (W - L) / 2 * (stamina / 2 - grass); printf( %lld n , t); return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int>> g; vector<int> w; vector<int> dp; void dfs(int node, int p) { dp[node] = w[node]; for (int u : g[node]) { if (u != p) { dfs(u, node); dp[node] += dp[u]; } } } int main() { cin >> n; int u, v, t; g.assign(n + 1, vector<int>()); w.assign(n + 1, 0); dp.assign(n + 1, 0); vector<int> ans; for (int i = 1; i < n; i++) { cin >> u >> v >> t; g[u].push_back(v); g[v].push_back(u); if (t == 2) { w[u] = 1; w[v] = 1; } } dfs(1, 0); for (int i = 1; i <= n; i++) { if (dp[i] == 1 && w[i] == 1) ans.push_back(i); } cout << ans.size() << endl; for (auto u : ans) cout << u << ; return 0; }
#include <bits/stdc++.h> using namespace std; int n; int a[300010]; long long ans = 0; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } sort(a + 1, a + 1 + n); for (int i = 1; i <= n / 2; ++i) { long long sum = a[i] + a[n - i + 1]; ans += sum * sum; } cout << ans; }
#include <bits/stdc++.h> using namespace std; vector<int> a; vector<int> b; int n; void get_input() { cin >> n; } void solve() { for (int i = 0; i < n; i++) { int kom; cin >> kom; a.push_back(kom); b.push_back(kom); } sort(b.begin(), b.begin() + n); int cnt = 0; for (int i = 0; i < n; i++) { if (b[i] != a[i]) { cnt++; } } cout << (cnt > 2 ? NO : YES ); } void output() {} int main() { ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; while (t--) { get_input(), solve(), output(); } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /stack:200000000 ) #pragma GCC optimize( Ofast ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) #pragma GCC optimize( unroll-loops ) using namespace std; const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; int main() { int n, k; scanf( %d%d , &n, &k); int v = 2 * k + 1; if (n <= k + 1) printf( 1 n1 n ); else if (n % v == 0) { printf( %d n , n / v); int ile = 0, pos = k + 1; while (ile < n) { printf( %d , pos); pos += v; ile += v; } printf( n ); } else if (n % v > k) { int r = n % v; int pos = 1 + r - (k + 1); int ile = r; printf( %d n%d , n / v + 1, pos); pos = ile + k + 1; while (ile < n) { printf( %d , pos); pos += v; ile += v; } printf( n ); } else { int res = n * n; for (int i = (k + 1); i <= (v); ++i) for (int j = (k + 1); j <= (v); ++j) if ((n - i - j) >= 0 && (n - i - j) % v == 0) res = min(res, 2 + (n - i - j) / v); printf( %d n , res); for (int i = (k + 1); i <= (v); ++i) for (int j = (k + 1); j <= (v); ++j) if ((n - i - j) >= 0 && (n - i - j) % v == 0 && 2 + (n - i - j) / v == res) { int pos = 1 + i - (k + 1); int ile = i; printf( %d %d , pos, n - (j - (k + 1))); n -= j; pos = ile + k + 1; while (ile < n) { printf( %d , pos); pos += v; ile += v; } printf( n ); return 0; } } return 0; }
#include <bits/stdc++.h> const int mod = 1000000007; struct uinon_set { int father[100003], n, value[100003]; int ans; void init() { ans = 0; for (int i = 1; i <= n; i++) { father[i] = i; value[i] = 0; } return; } int getfather(int i) { if (father[i] == i) return i; int tans = getfather(father[i]); value[i] = (value[i] + value[father[i]]) % mod; return father[i] = tans; } void insert(int root, int i, int xi) { int temp, tmp = getfather(root); temp = (value[root] + xi) % mod; father[tmp] = i; value[tmp] = temp; ans = (ans + temp) % mod; return; } } set; int main() { int n, t, x, y, i, j; scanf( %d , &n); set.n = n; set.init(); for (i = 1; i <= n; i++) { scanf( %d , &t); for (j = 0; j < t; j++) { scanf( %d%d , &x, &y); set.insert(x, i, y); } } printf( %d n , (set.ans + mod) % mod); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 5e5 + 5; bool vis[MAXN]; long long sp[MAXN]; long long f[MAXN]; bool is[MAXN]; long long ar[MAXN]; long long POWER[25]; long long F[25]; void sieve() { for (long long i = 0; i < MAXN; i++) vis[i] = false, sp[i] = 2; for (long long i = 3; i < MAXN; i += 2) { if (!vis[i]) { sp[i] = i; for (long long j = i * i; j < MAXN; j += 2 * i) { vis[j] = true; sp[j] = i; } } } } int main() { long long n, q; cin >> n >> q; for (long long i = 1; i <= n; i++) scanf( %lld , &ar[i]); sieve(); long long added = 0; POWER[0] = 1; for (long long i = 1; i < 15; i++) POWER[i] = POWER[i - 1] << 1; long long ans = 0; while (q--) { long long x; long long temp = 0; scanf( %lld , &x); long long cc = 0; long long val = ar[x]; while (val != 1) { long long div = sp[val]; F[cc++] = div; while (val % div == 0) val /= div; } for (long long i = 1; i < POWER[cc]; i++) { long long p = 1; long long counter = 0; for (long long j = 0; j < cc; j++) if (POWER[j] & i) counter++, p *= F[j]; if (counter & 1) temp += f[p]; else temp -= f[p]; } if (is[x]) { ans -= added - temp; if (ar[x] == 1) ans++; is[x] = false; for (long long i = 1; i < POWER[cc]; i++) { long long p = 1; for (long long j = 0; j < cc; j++) if (POWER[j] & i) p *= F[j]; f[p]--; } added--; } else { ans += added - temp; is[x] = true; for (long long i = 1; i < POWER[cc]; i++) { long long p = 1; for (long long j = 0; j < cc; j++) if (POWER[j] & i) p *= F[j]; f[p]++; } added++; } printf( %lld n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int x, y; string s, t; cin >> x >> y >> s >> t; int arr[y - x + 1]; int mx = -1, res; for (int i = 0; i < y - x + 1; i++) { int k = i; arr[i] = 0; for (int j = 0; j < x; j++, k++) { if (t[k] == s[j]) arr[i] += 1; } if (mx < arr[i]) { mx = arr[i]; res = i; } } cout << x - mx << endl; for (int i = res, j = 0; j < x; i++, j++) { if (t[i] != s[j]) cout << j + 1 << ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k, m; cin >> n >> k; m = n * (n - 1) / 2; if ((double)k > ((double)n - 1) / 2) { cout << -1; return 0; } else { cout << k * n << endl; for (int i = 1; i <= n; ++i) { for (int j = 0; j < k; ++j) { cout << i << << (i + j) % n + 1 << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long inf = 0x3f3f3f3f3f3f3f3f; const int maxn = 1005; int main() { int N; cin >> N; cout << 2 * N - 1 << << 2 << endl; cout << 1 << << 2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<pair<int, int>, int> > a(2 * n - 1); for (int i = 0; i < 2 * n - 1; i++) { cin >> a[i].first.first; cin >> a[i].first.second; a[i].second = i + 1; } sort(a.begin(), a.end()); long long s = 0, s1 = 0, tt = 0, tt1 = 0; for (int i = 0; i < 2 * n - 1; i++) { if (i % 2 == 0) { s += a[i].first.first; s1 += a[i].first.second; } tt += a[i].first.first; tt1 += a[i].first.second; } tt++; tt1++; tt /= 2; tt1 /= 2; cout << YES << endl; if (s >= tt && s1 >= tt1) { for (int i = 0; i < 2 * n - 1; i++) { if (i % 2 == 0) cout << a[i].second << ; } cout << endl; } else { for (int i = 0; i < 2 * n - 1; i++) { if (i % 2 == 1) { cout << a[i].second << ; } } cout << a[2 * n - 2].second << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T1> void print_list(const T1& a) { cerr << a << endl; } template <typename T1, typename T2> void print_list(const T1& a, const T2& b) { cerr << a << << b << endl; } template <typename T1, typename T2, typename T3> void print_list(const T1& a, const T2& b, const T3& c) { cerr << a << << b << << c << endl; } template <typename T1, typename T2, typename T3, typename T4> void print_list(const T1& a, const T2& b, const T3& c, const T4& d) { cerr << a << << b << << c << << d << endl; } template <typename T1, typename T2, typename T3, typename T4, typename T5> void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e) { cerr << a << << b << << c << << d << << e << endl; } template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e, const T6& f) { cerr << a << << b << << c << << d << << e << << f << endl; } template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> void print_list(const T1& a, const T2& b, const T3& c, const T4& d, const T5& e, const T6& f, const T7& g) { cerr << a << << b << << c << << d << << e << << f << << g << endl; } template <typename A, typename B> ostream& operator<<(ostream& out, const pair<A, B>& pp) { out << ( << pp.first << , << pp.second << ) ; return out; } template <typename A, typename B> istream& operator<<(istream& in, pair<A, B>& pp) { cerr << A pair wanted << endl; in >> pp.first >> pp.second; return in; } template <typename T> ostream& operator<<(ostream& out, const vector<T>& vect) { out << length = << vect.size() << endl; for (int i = 0; i < vect.size(); i++) out << vect[i] << ; out << endl; return out; } template <typename T> istream& operator>>(istream& in, vector<T>& vect) { vect.clear(); int n; cerr << A integer of length wanted << endl; in >> n; vect.resize(n); cerr << n << elements wanted << endl; for (int i = 0; i < n; i++) in >> vect[i]; return in; } template <typename T> ostream& operator<<(ostream& out, const vector<vector<T> >& vect) { out << row number= << vect.size() << endl; for (int i = 0; i < vect.size(); i++) { out << row # << i << : ; for (int j = 0; j < vect[i].size(); j++) out << << vect[i][j]; out << endl; } return out; } template <typename T> istream& operator>>(istream& in, vector<vector<T> >& vect) { vect.clear(); int n, m; cerr << Two integers wnated << endl; in >> n >> m; vect.resize(n); cerr << A matrix << n << X << m << wanted << endl; for (int i = 0; i < n; i++) { vect[i].resize(m); for (int j = 0; j < m; j++) in >> vect[i][j]; } return in; } template <typename T> inline void updateMax(T& a, const T& b) { a = max(a, b); } template <typename T> inline void updateMin(T& a, const T& b) { a = min(a, b); } template <typename T> inline vector<T> erase(vector<T> table, int ind) { assert(ind < table.size()); table.erase(table.begin() + ind); return table; } template <typename T> vector<T> unique(vector<T> table) { sort(table.begin(), table.end()); return vector<T>(table.begin(), unique(table.begin(), table.end())); } template <class T> vector<T> parse(const string& ss, const char* cut = ) { vector<T> re; for (int j = 0; j < ss.size(); j++) { string s; while (j < ss.size() && NULL == strchr(cut, ss[j])) s += ss[j++]; if (!s.empty()) { T tmp; istringstream is(s); is >> tmp; re.push_back(tmp); } } return re; } int countBit(int n) { int re = 0; while (n) re++, n ^= ((((n) ^ ((n)-1)) & n)); return re; } int MSB(int n) { if (n == 0) return 0; while (((((n) ^ ((n)-1)) & n)) != n) n ^= ((((n) ^ ((n)-1)) & n)); return n; } void initHash(int n, int hash[]) { for (int i = 0; i < n; i++) hash[((1) << (i))] = i; } void initBcnt(int n, int bcnt[]) { bcnt[0] = 0; for (int i = 1; i < ((1) << (n)); i++) bcnt[i] = bcnt[i ^ ((((i) ^ ((i)-1)) & i))] + 1; } template <typename T> T __gcd(T n, T m, T& a, T& b) { T a1 = 0, b1 = 1; a = 1, b = 0; while (m) { T c = n / m; T r = n - m * c; T t; t = a; a = a1; a1 = t - c * a1; t = b; b = b1; b1 = t - c * b1; n = m; m = r; } return n; } struct Node { int r, c, d; int v; Node(int x, int y, int z, int v1) : r(x), c(y), d(z), v(v1){}; bool operator<(const Node& other) const { return v > other.v; }; }; priority_queue<Node> heap; const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; const char dname[] = NWSE ; const string vv[] = { ......... , ....O.... , ..O...O.. , ..O.O.O.. , O.O...O.O , O.O.O.O.O , O.OO.OO.O }; const string hh[] = { ......... , ....O.... , O.......O , O...O...O , O.O...O.O , O.O.O.O.O , OOO...OOO }; char grid[2000][2000]; int dom[1000][1000]; long long int col1[1000], col2[1000]; long long int dp[1000]; const long long int mod = 1000000007; long long int buf[1000]; int main() { map<string, int> mem; for (int i = 0; i < 7; i++) { mem[vv[i]] |= 1; mem[hh[i]] |= 2; } int n, m; scanf( %d %d , &n, &m); for (int i = 0; i < 4 * n + 1; i++) { scanf( %s , grid[i]); } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { int x = 4 * i + 1; int y = 4 * j + 1; string s(9, . ); for (int a = 0; a < 3; a++) for (int b = 0; b < 3; b++) { s[a * 3 + b] = grid[x + a][y + b]; } assert(mem.count(s)); dom[i][j] = mem[s]; } for (int i = 0; i < m; i++) { col1[i] = 1 - (n & 1); for (int j = 1; j < n && col1[i]; j += 2) { if (0 == (dom[j][i] & 1) || 0 == (dom[j - 1][i] & 1)) { col1[i]--; break; } } } for (int i = 0; i + 1 < m; i++) { dp[0] = 1; for (int j = 1; j <= n; j++) { dp[j] = 0; if ((dom[j - 1][i] & 2) && (dom[j - 1][i + 1] & 2)) { dp[j] += dp[j - 1]; } if (j >= 2 && (dom[j - 1][i] & 1) && (dom[j - 2][i] & 1) && (dom[j - 1][i + 1] & 1) && (dom[j - 2][i + 1] & 1)) { dp[j] += dp[j - 2]; } dp[j] %= mod; } col2[i] = dp[n]; } dp[0] = 1; for (int i = 1; i <= m; i++) { dp[i] = 0; dp[i] += col1[i - 1] * dp[i - 1]; if (i >= 2) { dp[i] += (col2[i - 2] + mod - col1[i - 1] * col1[i - 2]) * dp[i - 2]; } dp[i] %= mod; } cout << dp[m] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, k; char s[1005], ans[1005]; map<long long, bool> dp[1005]; map<long long, char> pre[1005]; inline long long read() { long long q = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == - ) f = 0; ch = getchar(); } while (isdigit(ch)) q = (q << 3) + (q << 1) + (ch ^ 48), ch = getchar(); return f ? q : -q; } inline void print(long long x) { if (x < 0) putchar( - ), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); } signed main() { n = read(), k = read(), scanf( %s , s + 1), dp[1][0] = true; for (long long i = 2; i <= n + 1; ++i) for (long long j = -k + 1; j < k; ++j) if (dp[i - 1][j]) { if (!(s[i - 1] ^ L )) pre[i][j - 1] = L , dp[i][j - 1] = true; else if (!(s[i - 1] ^ W )) pre[i][j + 1] = W , dp[i][j + 1] = true; else if (!(s[i - 1] ^ D )) pre[i][j] = D , dp[i][j] = true; else pre[i][j + 1] = W , pre[i][j] = D , pre[i][j - 1] = L , dp[i][j + 1] = dp[i][j] = dp[i][j - 1] = true; } if (dp[n + 1][k] | dp[n + 1][-k]) { long long j = dp[n + 1][k] ? k : -k; for (long long i = n + 1; i > 1; --i) { char ch = pre[i][j]; ans[i - 1] = ch, ((!(ch ^ W )) ? --j : ((!(ch ^ L )) ? ++j : i)); } for (long long i = 1; i <= n; ++i) putchar(ans[i]); } else { putchar( N ), putchar( O ); return 0; }; return 0; }
#include <bits/stdc++.h> using namespace std; long long modpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) { ans = (ans * a) % 1000000007; } a = (a * a) % 1000000007; b = b / 2; } return ans; } int main() { cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; while (t--) { long long x, y; cin >> x >> y; if (x - y == 1) { cout << NO << endl; } else cout << YES << endl; } }
#include <bits/stdc++.h> using namespace std; list<int> l[100005]; list<int> son[100005]; int n, dad[100005], x, y, val[100005], res; void f(int a) { for (list<int>::iterator it = l[a].begin(); it != l[a].end(); it++) if ((*it) != dad[a]) { dad[*it] = a; f(*it); son[a].push_back(*it); } } pair<long long int, long long int> calc(int a) { pair<long long int, long long int> ret = pair<long long int, long long int>(0, 0); pair<long long int, long long int> tmp; for (list<int>::iterator it = son[a].begin(); it != son[a].end(); it++) if (*it != dad[a]) { tmp = calc(*it); ret = pair<long long int, long long int>(max(tmp.first, ret.first), max(tmp.second, ret.second)); } int change = ret.first - ret.second; if (val[a] + change > 0) ret.second += abs(val[a] + change); else ret.first += abs(val[a] + change); return ret; } int main() { scanf( %d , &n); for (int i = 0; i < n - 1; i++) { scanf( %d %d , &x, &y); l[x].push_back(y); l[y].push_back(x); } for (int i = 1; i <= n; i++) scanf( %d , &val[i]); f(1); pair<long long int, long long int> res = calc(1); cout << res.first + res.second << endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); if (fopen( input.txt , r )) { freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); } int n, a, b; cin >> n; bool visit[1000005] = {false}; int ans = -1; while (n--) { cin >> a >> b; while (visit[a] || a < ans) { a += b; } ans = max(ans, a); visit[a] = true; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int tt; cin >> tt; while (tt--) { int n; int diff = 0; cin >> n; int a[2 * n]; for (int i = 0; i < 2 * n; i++) { cin >> a[i]; if (a[i] & 1) diff++; else diff--; } if (diff == 0) { cout << 0 << n ; continue; } map<int, int> mp; mp[0] = 0; int cnt = 0; for (int i = n; i < 2 * n; i++) { if (a[i] & 1) cnt++; else cnt--; if (mp.find(cnt) == mp.end()) mp[cnt] = i - (n - 1); } cnt = 0; int ans; if (mp.find(diff) == mp.end()) ans = 2 * n; else ans = mp[diff]; for (int i = n - 1; i >= 0; i--) { if (a[i] & 1) cnt++; else cnt--; if (mp.find(diff - cnt) != mp.end()) { ans = min(ans, mp[(diff - cnt)] + n - i); } } cout << ans << n ; } }
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } int main() { int n, p, k, ai; cin >> n >> k >> p; vector<int> ods; vector<int> evns; for (int i = 0; i < n; i++) { cin >> ai; if (ai % 2 == 0) evns.push_back(ai); else ods.push_back(ai); } int rem = k - p; vector<vector<int> > ans; for (int i = 0; i < p; i++) { if (evns.size()) { vector<int> tmp; tmp.push_back(evns.back()); ans.push_back(tmp); evns.pop_back(); } else if (ods.size() >= 2) { vector<int> tmp; tmp.push_back(ods.back()); ods.pop_back(); tmp.push_back(ods.back()); ods.pop_back(); ans.push_back(tmp); } else { cout << NO ; return 0; } } for (int i = 0; i < rem; i++) { if (ods.size()) { vector<int> tmp; tmp.push_back(ods.back()); ans.push_back(tmp); ods.pop_back(); } else { cout << NO ; return 0; } } if (ods.size() % 2 == 1) { cout << NO ; return 0; } if (k == 1) { cout << YES << endl; int m = ans[0].size() + ods.size() + evns.size(); cout << m << ; for (int j = 0; j < ans[0].size(); j++) cout << ans[0][j] << ; for (int j = 0; j < ods.size(); j++) cout << ods[j] << ; for (int j = 0; j < evns.size(); j++) cout << evns[j] << ; return 0; } cout << YES << endl; for (int i = 0; i < ans.size(); i++) { if (ods.size()) { int m = ans[i].size() + ods.size(); cout << m << ; for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ; for (int j = 0; j < ods.size(); j++) cout << ods[j] << ; ods.clear(); cout << endl; } else if (evns.size()) { int m = ans[i].size() + evns.size(); cout << m << ; for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ; for (int j = 0; j < evns.size(); j++) cout << evns[j] << ; evns.clear(); cout << endl; } else { cout << ans[i].size() << ; for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ; cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int fa[101000][20], h[101000], lev[101000], ans[101000]; int dp[101000]; struct EDGE { int to, nxt; } box[2 * 101000]; int top, K; void add(int u, int v) { box[top].to = v; box[top].nxt = h[u]; h[u] = top++; } void DFS(int u, int pre, int t) { lev[u] = lev[pre] + 1; fa[u][0] = pre; if (!t) for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (int p = h[u]; p != -1; p = box[p].nxt) { int v = box[p].to; if (pre == v) continue; DFS(v, u, t); if (t) { dp[u] += dp[v]; ans[p / 2] = dp[v]; } } } int lca(int u, int v) { if (lev[u] < lev[v]) swap(u, v); int c = lev[u] - lev[v]; for (int i = 19; i >= 0; i--) { if (c & (1 << i)) { u = fa[u][i]; c -= (1 << i); } } if (u == v) return u; for (int i = 19; i >= 0; i--) { if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } } return fa[u][0]; } int main() { int n, k; int a, b; scanf( %d , &n); memset(h, -1, sizeof(h)); for (int i = 1; i < n; i++) { scanf( %d%d , &a, &b); add(a, b); add(b, a); } DFS(1, 1, 0); scanf( %d , &k); for (int i = 0; i < k; i++) { scanf( %d%d , &a, &b); int c = lca(a, b); dp[a]++; dp[b]++; dp[c] -= 2; } DFS(1, 1, 1); for (int i = 0; i < n - 1; i++) { printf( %d%c , ans[i], i == n - 2 ? n : ); } }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long extgcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long x1, y1; long long d = extgcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } bool isPowerOfTwo(long long n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } long long powm(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b >>= 1; } return res; } long long divide(long long a, long long b) { return (a % 1000000007) * powm(b, 1000000007 - 2) % 1000000007; } long long mul(long long a, long long b) { return ((a % 1000000007) * (b % 1000000007)) % 1000000007; } long long add(long long a, long long b) { return (a % 1000000007 + b % 1000000007) % 1000000007; } long long Totfun(long long n) { long long z = n; if (n % 2 == 0) { while (n % 2 == 0) { n /= 2; } z /= 2; } for (long long i = 3; i <= sqrt(n); i += 2) { if (isPrime(i) && n % i == 0) { while (n % i == 0) { n /= i; } z -= z / i; } } if (n > 1) { z -= z / n; } return z; } long long fact[100001]; long long nCr(long long n, long long r) { if (r > n || r < 0) return 0; long long z = fact[n]; z = mul(z, powm(fact[n - r], 1000000007 - 2)); z = mul(z, powm(fact[r], 1000000007 - 2)); return z; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, a = 0, b = 0, c = 0, z = 1; cin >> n; string s; cin >> s; for (long long i = 0; i < n; i++) { if (s[i] == a ) a = add(a, z); else if (s[i] == b ) b = add(b, a); else if (s[i] == c ) c = add(c, b); else { c = add(3 * c, b); b = add(3 * b, a); a = add(3 * a, z); z = mul(z, 3); } } cout << c << n ; return 0; }