func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; const int N = 5001; const int B = 27; int zhu[B], n, m; char s[N], t[N]; void solve() { memset(zhu, 0, sizeof(zhu)); n = strlen(s); m = strlen(t); for (int i = 0; i < n; ++i) zhu[s[i] - a ]++; int r = -1, p; if (n > m) p = m; else p = min(n, m) - 1; for (int i = t[0] - a + 1; i < 26; ++i) if (zhu[i]) r = 0; for (int i = 1; i <= p; ++i) { zhu[t[i - 1] - a ]--; if (zhu[t[i - 1] - a ] < 0) break; if (i == m) { r = m; continue; } for (int j = t[i] - a + 1; j < 26; ++j) if (zhu[j]) r = i; } if (r == -1) { printf( -1 n ); return; } else { memset(zhu, 0, sizeof(zhu)); for (int i = 0; i < n; ++i) zhu[s[i] - a ]++; for (int i = 0; i < r; ++i) { printf( %c , t[i]); zhu[t[i] - a ]--; } if (r < m) for (int i = t[r] - a + 1; i < 26; ++i) if (zhu[i]) { printf( %c , i + a ); zhu[i]--; break; } for (int i = 0; i < 26; ++i) while (zhu[i]--) printf( %c , i + a ); printf( n ); } } int main() { while (~scanf( %s%s , s, t)) solve(); } |
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int x = 0, f = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while ( 0 <= ch && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } int fac[2000010], inv[2000010], f[2000010]; inline int Power(int x, int y, int Q) { int ret = 1; while (y) { if (y & 1) ret = 1ll * ret * x % Q; x = (1ll * x * x) % Q; y >>= 1; } return ret; } int main() { int n = read(), m = read(), a = read(), Q = read(); int p = 1; for (int i = a; i != 1; i = (1ll * i * a % Q), p++) ; fac[1] = 1; for (int i = (2); i <= (max(n, m)); i++) fac[i] = 1ll * fac[i - 1] * i % p; inv[0] = inv[1] = 1; for (int i = (2); i <= (max(n, m)); i++) inv[i] = 1ll * (p - p / i) * inv[p % i] % p; for (int i = (2); i <= (max(n, m)); i++) inv[i] = 1ll * inv[i - 1] * inv[i] % p; f[0] = 1; for (int i = (1); i <= (m); i++) { f[i] = (f[i - 1] + (1ll * fac[m] * inv[m - i] % p * inv[i] % p)) % p; } for (int i = (m + 1); i <= (n); i++) f[i] = f[i - 1]; for (int i = n; i >= 1; i--) printf( %d , Power(a, f[i - 1], Q)); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, x; int dist[501][501]; bool color[501]; unsigned short num_red; unsigned short ans[501][501][501], best[501][501], sizes[501]; unsigned short inf = ~0; vector<unsigned short> edge[501]; void dfs(unsigned short& now, unsigned short& before) { for (int i = 0; i < edge[now].size(); i++) { unsigned short& next = edge[now][i]; if (next == before) continue; dfs(next, now); } for (int i = 1; i <= n; i++) if (dist[now][i] <= x) { sizes[now] = 1; ans[now][i][1] = !color[i]; for (int j = 0; j < edge[now].size(); j++) { unsigned short& next = edge[now][j]; if (next == before) continue; int num_node; if (num_red < sizes[now] + sizes[next]) num_node = num_red; else num_node = sizes[now] + sizes[next]; for (unsigned short k = num_node;; k--) { unsigned short temp = inf; int num_node_1 = k - sizes[now]; if (num_node_1 < 0) num_node_1 = 0; int num_node_2 = k; if (k > sizes[next]) num_node_2 = sizes[next]; for (unsigned short j = num_node_1; j <= num_node_2; j++) { if (temp > ans[now][i][k - j] + best[next][j]) temp = ans[now][i][k - j] + best[next][j]; } num_node_1++; for (unsigned short j = num_node_1; j <= num_node_2; j++) { if (temp > ans[now][i][k - j + 1] + ans[next][i][j] - !color[i]) temp = ans[now][i][k - j + 1] + ans[next][i][j] - !color[i]; } ans[now][i][k] = temp; if (!k) break; } sizes[now] += sizes[next]; } } for (unsigned short i = 1; i <= num_red; i++) { best[now][i] = inf; for (unsigned short j = 1; j <= n; j++) { best[now][i] = min(best[now][i], ans[now][j][i]); } } } int main() { num_red = 0; memset(ans, 0xff, sizeof ans); memset(best, 0xff, sizeof best); memset(dist, 0x3f3f, sizeof dist); cin >> n >> x; unsigned short col; for (int i = 1; i <= n; i++) { cin >> col; num_red += col; color[i] = col == 1; } unsigned short a, b; int cost; for (int i = 1; i < n; i++) { cin >> a >> b >> cost; edge[a].push_back(b); edge[b].push_back(a); dist[a][b] = dist[b][a] = cost; } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dist[i][j] = i == j ? 0 : min(dist[i][j], dist[i][k] + dist[k][j]); } } } unsigned short start = 1; unsigned short before = 0; dfs(start, before); if (best[1][num_red] == inf) cout << -1 << endl; else cout << best[1][num_red] << endl; return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:1024000000,1024000000 ) using namespace std; int main() { int T, i, j, k, ca = 0, n, m; int x1, y1, x2, y2; while (~scanf( %d%d%d%d , &x1, &y1, &x2, &y2)) { scanf( %d , &n); int ans = 0, a, b, c; while (n--) { scanf( %d%d%d , &a, &b, &c); long long first = 1LL * a * x1 + 1LL * b * y1 + c, second = 1LL * a * x2 + 1LL * b * y2 + c; if (first > 0 && second < 0) ans++; else if (first < 0 && second > 0) ans++; } printf( %d n , ans); } } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e9 + 7; vector<long long int> v; long long int calcmulti(string s, long long int l, long long int r) { long long int ans = 1; string z = ; for (long long int i = l; i <= r; i++) { if (s[i] == * ) { ans *= stoll(z); z = ; } else z += s[i]; } ans *= stoll(z); return ans; } long long int calc(string s, long long int ss, long long int st) { if (ss == st) return s[ss] - 0 ; long long int mid = (ss + st) / 2, mindist = MAXN; for (long long int i = mid; i <= st; i++) { if (s[i] == + ) { mindist = min(mindist, i); break; } } for (long long int i = mid; i >= ss; i--) { if (s[i] == + ) { mindist = min(mindist, i); break; } } if (mindist == MAXN) return calcmulti(s, ss, st); return calc(s, ss, mindist - 1) + calc(s, mindist + 1, st); } long long int change(string s, long long int ss, long long int st) { string z; long long int ans = calc(s, ss, st); for (long long int i = 0; i <= ss - 1; i++) z += s[i]; z += to_string(ans); for (long long int i = st + 1; i <= (long long int)s.length() - 1; i++) z += s[i]; return calc(z, 0, (long long int)z.length() - 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int ans = 0; string s; cin >> s; for (long long int i = 0; i < s.length(); i++) if (s[i] == * ) v.push_back(i); if (v.size() == 0) { cout << calc(s, 0, (long long int)s.length() - 1); return 0; } if (v.size() == 1) { cout << max(change(s, 0, v[0] - 1), max(change(s, v[0] + 1, (long long int)s.length() - 1), calc(s, 0, (long long int)s.length() - 1))); ; return 0; } ans = max(ans, calc(s, 0, (long long int)s.length() - 1)); for (long long int i = 0; i < (long long int)v.size(); i++) { ans = max(ans, change(s, 0, v[i] - 1)); ans = max(ans, change(s, v[i] + 1, (long long int)s.length() - 1)); for (long long int j = i + 1; j < (long long int)v.size(); j++) { ans = max(ans, change(s, v[i] + 1, v[j] - 1)); } } cout << ans; } |
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { unsigned n(0); cin >> n; vector<unsigned> array(n, 0); for (unsigned it(0); it != n; ++it) { array[it] = it + 1; } --n; for (unsigned it(n); it > 0; --it) { swap(array[it], array[it - 1]); } ++n; for (unsigned it(0); it != n; ++it) { cout << array[it] << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; cout << a[1] - a[0] << << a[n - 1] - a[0] << endl; for (int i = 1; i < n - 1; i++) cout << min(a[i + 1] - a[i], a[i] - a[i - 1]) << << max(a[n - 1] - a[i], a[i] - a[0]) << endl; cout << a[n - 1] - a[n - 2] << << a[n - 1] - a[0] << endl; } |
#include <bits/stdc++.h> using namespace std; long long tab[70]; void Tab() { for (int i = 0; i < 64; i++) tab[i] = pow(2, i); } long long Sum(long long n) { long long m = n + 1; if ((long long)(m) % 2 == 0) m /= 2; else n /= 2; long long sum = m * n; return sum; } long long Sum1(long long n) { long long sum1 = pow(2, n) - 1; return sum1; } int Find(long long n) { int i = 0; for (; i < 64; i++) { if (tab[i] > n) break; } return i; } int main() { Tab(); long long ans, sum, sum1; int t; long long n; cin >> t; while (t--) { ans = 0; cin >> n; sum = Sum(n); long long site = Find(n); sum1 = Sum1(site); ans = sum - sum1 - sum1; printf( %lld n , ans); } return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:200000000 ) using namespace std; const long long oo = 1LL << 60; const long long kNumMoves = 4; const long long kMoves[kNumMoves][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; struct Line { Line() : m(0), c(0) {} Line(long long slope, long long yOrd) : m(slope), c(yOrd) {} long long m; long long c; }; class ConvexHullOptimization { public: ConvexHullOptimization(int n) : cur(0), lines(n), count(0) {} void Add(long long m, long long c) { Line newLine(m, c); while (!KeepCurrentTop(newLine)) --count; lines[count++] = newLine; } void Reset() { cur = count = 0; } long long GetValue(long long x) { if (cur >= count) cur = count - 1; while ((cur < count - 1) && ((lines[cur].m * x + lines[cur].c) >= (lines[cur + 1].m * x + lines[cur + 1].c))) ++cur; return lines[cur].m * x + lines[cur].c; } private: bool KeepCurrentTop(const Line& l3) { if ((count - cur) < 2) return true; Line l2 = lines[count - 1]; Line l1 = lines[count - 2]; long double newCost = (l3.c - l1.c) * (long double)(l1.m - l2.m); long double oldCost = (l2.c - l1.c) * (long double)(l1.m - l3.m); return newCost > oldCost; } int cur; int count; vector<Line> lines; }; int n, m, p; vector<long long> d, t, accum; long long GetMinWaitingTime() { vector<vector<long long> > dp(m, vector<long long>(2, 0)); for (int i = 0; i < int(m); ++i) dp[i][0] = (i + 1) * t[i] - accum[i]; long long res = oo; ConvexHullOptimization chOpt(m + 1); for (int i = 1; i < int(p); ++i) { int curIdx = (i & 1); int prevIdx = curIdx ^ 1; chOpt.Reset(); chOpt.Add(-1, accum[0] + dp[0][prevIdx]); for (int k = 1; k < int(m); ++k) { dp[k][curIdx] = dp[k][prevIdx]; dp[k][curIdx] = min(dp[k][curIdx], (k + 1) * t[k] - accum[k] + chOpt.GetValue(t[k])); chOpt.Add(-(k + 1), accum[k] + dp[k][prevIdx]); } res = min(res, dp[m - 1][curIdx]); } return res; } int main() { cin >> n >> m >> p; d.resize(n, 0); for (int i = 0; i < int(n - 1); ++i) cin >> d[i + 1]; accum.resize(n, 0); for (int i = 0; i < int(n - 1); ++i) accum[i + 1] = accum[i] + d[i + 1]; t.resize(m); for (int i = 0; i < int(m); ++i) { int h; cin >> h >> t[i]; t[i] -= accum[--h]; } sort(t.begin(), t.end()); long long mn = t.front(); for (int i = 0; i < int(m); ++i) t[i] -= mn; accum.clear(); accum.resize(m, 0); accum[0] = t[0]; for (int i = 0; i < int(m - 1); ++i) accum[i + 1] = accum[i] + t[i + 1]; long long res = GetMinWaitingTime(); cout << res; int i; cin >> i; return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, in[5100], st[5100], f[5100], ans = 9999999999999; signed main() { cin >> n; for (long long i = 1; i <= n; i++) { cin >> in[i]; } memset(f, 0x7f, sizeof(f)); memcpy(st, in, sizeof(in)); sort(st + 1, st + n + 1); for (long long i = 1; i <= n; i++) f[i] = 0; for (long long i = 1; i <= n; i++) { for (long long o = 1; o <= n; o++) { f[o] = min(f[o - 1], f[o] + abs(in[i] - st[o])); } } for (long long i = 1; i <= n; i++) { ans = min(ans, f[i]); } cout << ans; } |
#include <bits/stdc++.h> using namespace std; double f[4][207][207]; struct node { int x, y; }; node a[207]; bool cmp(const node& x, const node& y) { return x.y > y.y; } int n, m, bm; int main() { while (scanf( %d %d %d , &n, &m, &bm) != EOF) { int i, j, k, r, w; int sum = 0; memset(f, 0, sizeof(f)); sum = bm; for (i = 1; i <= n; i++) { scanf( %d , &a[i].x); } for (i = 1; i <= n; i++) scanf( %d , &a[i].y); sort(a + 1, a + n + 1, cmp); double ans = 0; memset(f, 0, sizeof(f)); f[0][0][bm] = 1; for (i = 0; i < n; i++) { w = (i + 1) & 1; r = (i)&1; memset(f[w], 0, sizeof(f[w])); for (j = 0; j <= i; j++) for (k = 0; k <= 200; k++) { int h = k + a[i + 1].y; if (h < 0) continue; if (h > 200) h = 200; f[w][j + 1][h] += f[r][j][k] * (double)a[i + 1].x / 100; } for (j = 0; j <= i; j++) for (k = 0; k <= 200; k++) { int h = k + a[i].y; f[w][j][k] += f[r][j][k] * (1 - (double)a[i + 1].x / 100); } } w = n & 1; for (j = m; j <= n; j++) for (k = 0; k <= 200; k++) ans += f[w][j][k]; printf( %f n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; int x, cnt; int main() { cin >> x; while (x > 0) { if (x % 2 == 1) { cnt++; } x /= 2; } cout << cnt << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int num1 = n / 3; long long int num2 = n - (2 * num1); if (num1 % 3 != 0 && num2 % 3 != 0) cout << num1 << << num1 << << num2; else if (num1 - 1 > 0 && (num1 - 1) % 3 != 0 && (num2 + 2) % 3 != 0) cout << num1 - 1 << << num1 - 1 << << num2 + 2; else if (num2 - 2 > 0 && (num1 + 1) % 3 != 0 && (num2 - 2) % 3 != 0) cout << num1 + 1 << << num1 + 1 << << num2 - 2; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, c[5555], num[5555], ans[5555]; int main() { while (~scanf( %d , &n)) { for (int i = 1; i <= n; i++) scanf( %d , &c[i]); memset(num, 0, sizeof(num)); memset(ans, 0, sizeof(ans)); for (int i = 1; i <= n; i++) { memset(num, 0, sizeof(num)); num[c[i]]++, ans[c[i]]++; int Max = c[i]; for (int j = i + 1; j <= n; j++) { num[c[j]]++; if (num[c[j]] > num[Max] || num[c[j]] == num[Max] && c[j] < Max) Max = c[j]; ans[Max]++; } } for (int i = 1; i <= n; i++) printf( %d%c , ans[i], i == n ? n : ); } return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == - ) { ng = true; ch = getchar(); break; } if (ch >= 0 && ch <= 9 ) break; } while (1) { n = n * 10 + (ch - 0 ); ch = getchar(); if (ch < 0 || ch > 9 ) break; } return (ng ? -n : n); } template <typename T> inline T POW(T B, T P) { if (P == 0) return 1; if (P & 1) return B * POW(B, P - 1); else return (POW(B, P / 2) * POW(B, P / 2)); } template <typename T> inline T Gcd(T a, T b) { if (a < 0) return Gcd(-a, b); if (b < 0) return Gcd(a, -b); return (b == 0) ? a : Gcd(b, a % b); } template <typename T> inline T Lcm(T a, T b) { if (a < 0) return Lcm(-a, b); if (b < 0) return Lcm(a, -b); return a * (b / Gcd(a, b)); } long long Bigmod(long long base, long long power, long long MOD) { long long ret = 1; while (power) { if (power & 1) ret = (ret * base) % MOD; base = (base * base) % MOD; power >>= 1; } return ret; } bool isVowel(char ch) { ch = toupper(ch); if (ch == A || ch == U || ch == I || ch == O || ch == E ) return true; return false; } template <typename T> long long int isLeft(T a, T b, T c) { return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); } long long ModInverse(long long number, long long MOD) { return Bigmod(number, MOD - 2, MOD); } bool isConst(char ch) { if (isalpha(ch) && !isVowel(ch)) return true; return false; } int toInt(string s) { int sm; stringstream second(s); second >> sm; return sm; } int a[3][77]; int Ln[3]; int n; int dp[77][77][77][2]; int lol(int v, int k, int x, int f) { if ((v + k + x) == n) return 0; int &res = dp[v][k][x][f]; if (res != -1) return res; res = 20000; int np = v + k + x + 1; if (v < Ln[1]) { int tp = 0; int ps = a[1][v]; for (int i = x; i < Ln[0]; i++) { if (a[0][i] < ps) tp++; else break; } for (int i = k; i < Ln[2]; i++) { if (a[2][i] < ps) tp++; else break; } res = min(res, tp + lol(v + 1, k, x, 1)); } if (!f) { if (k < Ln[2]) { int tp = 0; int ps = a[2][k]; for (int i = x; i < Ln[0]; i++) { if (a[0][i] < ps) tp++; else break; } for (int i = v; i < Ln[1]; i++) { if (a[1][i] < ps) tp++; else break; } res = min(res, tp + lol(v, k + 1, x, 0)); } } if (x < Ln[0]) { int tp = 0; int ps = a[0][x]; for (int i = v; i < Ln[1]; i++) { if (a[1][i] < ps) tp++; else break; } for (int i = k; i < Ln[2]; i++) { if (a[2][i] < ps) tp++; else break; } res = min(res, tp + lol(v, k, x + 1, 0)); } return res; } map<char, int> mp; int main() { mp[ V ] = 1; mp[ K ] = 2; string s; cin >> n; cin >> s; for (int i = 1; i <= n; i++) { int tp = mp[s[i - 1]]; a[tp][Ln[tp]] = i; Ln[tp]++; } memset(dp, -1, sizeof(dp)); cout << lol(0, 0, 0, 0) << endl; return 0; } |
#include<bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define acarr(arr, n, type) type *arr=new type[n]; for(long long i=0; i<n; ++i) cin >> arr[i]; #define ll long long #define TF(ok) cout << (ok ? YES n : NO n ) #define ld long double #define vi vector<int> v1 #define pb push_back int solve(){ ll ans=0, tab=0; vector<int> v1(3); for (int i=0; i<3; i++){ cin >> v1[i]; ans+=v1[i]; } if (ans%9==0){ tab= ans/9; for (int i=0; i<3; i++){ if (v1[i]<tab){ cout << NO n ; return 0; } } cout << YES n ; } else{ cout << NO n ; } return 0; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int q; q=1; cin >> q; while(q--){ solve(); } return 0; } /* ~ Check for overflow ~ Division by 0 ~ Uninitialized ~ */ |
#include <bits/stdc++.h> using namespace std; int x, y; int main() { cin >> x >> y; if (x > 0 && y > 0) cout << 0 << x + y << << x + y << 0 n ; else if (x > 0 && y < 0) cout << 0 << -1 * (x + -1 * y) << << x + -1 * y << 0 n ; else if (x < 0 && y > 0) cout << -1 * (-1 * x + y) << 0 0 << -1 * x + y << n ; else cout << -1 * (-1 * x + -1 * y) << 0 0 << -1 * (-1 * x + -1 * y) << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = numeric_limits<long long>::max() / 4; struct MinCostMaxFlow { int N; vector<vector<long long> > cap, flow, cost; vector<int> found; vector<long long> dist, pi, width; vector<pair<int, int> > dad; MinCostMaxFlow(int N) : N(N), cap(N, vector<long long>(N)), flow(N, vector<long long>(N)), cost(N, vector<long long>(N)), found(N), dist(N), pi(N), width(N), dad(N) {} void AddEdge(int from, int to, long long cap, long long cost) { this->cap[from][to] = cap; this->cost[from][to] = cost; } void Relax(int s, int k, long long cap, long long cost, int dir) { long long val = dist[s] + pi[s] - pi[k] + cost; if (cap && val < dist[k]) { dist[k] = val; dad[k] = make_pair(s, dir); width[k] = min(cap, width[s]); } } long long Dijkstra(int s, int t) { fill(found.begin(), found.end(), false); fill(dist.begin(), dist.end(), INF); fill(width.begin(), width.end(), 0); dist[s] = 0; width[s] = INF; while (s != -1) { int best = -1; found[s] = true; for (int k = 0; k < N; k++) { if (found[k]) continue; Relax(s, k, cap[s][k] - flow[s][k], cost[s][k], 1); Relax(s, k, flow[k][s], -cost[k][s], -1); if (best == -1 || dist[k] < dist[best]) best = k; } s = best; } for (int k = 0; k < N; k++) pi[k] = min(pi[k] + dist[k], INF); return width[t]; } pair<long long, long long> GetMaxFlow(int s, int t) { long long totflow = 0, totcost = 0; while (long long amt = Dijkstra(s, t)) { totflow += amt; for (int x = t; x != s; x = dad[x].first) { if (dad[x].second == 1) { flow[dad[x].first][x] += amt; totcost += amt * cost[dad[x].first][x]; } else { flow[x][dad[x].first] -= amt; totcost -= amt * cost[x][dad[x].first]; } } } return make_pair(totflow, totcost); } }; const int N = 105; char s[N]; int b[N]; int main() { int n; cin >> n; scanf( %s , s + 1); int sum = 0; int cnt[26]; for (int i = 0; i < 26; i++) cnt[i] = 0; for (int i = 1; i <= n; i++) { scanf( %d , b + i); sum += b[i]; cnt[s[i] - a ]++; } MinCostMaxFlow mcmf(1 + 26 + n / 2 + 1 + 1); for (int i = 1; i <= 26; i++) { mcmf.AddEdge(1, i + 1, cnt[i - 1], 0); } for (int i = 1; i <= n / 2; i++) { mcmf.AddEdge(27 + i, 1 + 26 + n / 2 + 1, 2, 0); } for (int i = 1; i <= n / 2; i++) { for (int j = 0; j < 26; j++) { char k = a + j; if (s[i] == k && s[n - i + 1] == k) { mcmf.AddEdge(2 + j, 27 + i, 1, sum - max(b[i], b[n - i + 1])); } else if (s[i] == k) { mcmf.AddEdge(2 + j, 27 + i, 1, sum - b[i]); } else if (s[n - i + 1] == k) { mcmf.AddEdge(2 + j, 27 + i, 1, sum - b[n - i + 1]); } else { mcmf.AddEdge(2 + j, 27 + i, 1, sum); } } } auto e = mcmf.GetMaxFlow(1, 1 + 26 + n / 2 + 1); cout << n * sum - e.second << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; scanf( %d %d %d , &a, &b, &c); bool valid = false; int bo1, bo2, bo3; for (int i = 0; i <= a; i++) { bo1 = i; bo3 = a - i; bo2 = c - bo3; if (bo1 + bo3 != a || bo1 + bo2 != b || bo2 + bo3 != c) continue; if (bo1 < 0 || bo2 < 0 || bo3 < 0) continue; valid = true; break; } if (!valid) puts( Impossible ); else printf( %d %d %d n , bo1, bo2, bo3); return 0; } |
#include <bits/stdc++.h> const int mod = 998244353; const int N = 1e6 + 7; using namespace std; long long n, m, k, i, j, t, sig = 0, ans; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } map<pair<int, int>, set<long long>> mp; long long x[1005], y[1005]; struct node { int x, pos; } p[500007]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; long long ans = 0, sum = 0; for (i = 1; i <= n; i++) cin >> x[i] >> y[i]; for (i = 1; i <= n - 1; i++) { for (j = i + 1; j <= n; j++) { long long x1 = x[i], x2 = x[j], y1 = y[i], y2 = y[j]; int b = x1 - x2, a = y1 - y2; long long d = gcd(a, b); a /= d, b /= d; if (a < 0 || (!a && b < 0)) { a = -a, b = -b; } pair<int, int> line(a, b); long long c = a * x1 - b * y1; if (!mp[line].count(c)) { sum++; mp[line].insert(c); ans += sum - mp[line].size(); } } } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; for (int i = 0; i < q; i++) { int a, b, c; cin >> a; cin >> b; cin >> c; int p1, p2; cin >> p1; cin >> p2; int min_no = min(b, c); if (a <= 1) { cout << 0 << n ; continue; } if (b * 2 + c * 2 > a) { int p = 0; if (p2 >= p1) { if (c * 2 > a) { cout << (a / 2) * p2 << n ; continue; } else { p += c * p2; if ((a - c * 2) <= 1) { cout << p << n ; continue; } p += ((a - c * 2) / 2) * p1; cout << p << n ; continue; } } else if (p1 > p2) { if (b * 2 > a) { cout << (a / 2) * p1 << n ; continue; } else { p += b * p1; if ((a - b * 2) <= 1) { cout << p << n ; continue; } p += ((a - b * 2) / 2) * p2; cout << p << n ; continue; } } } else { int p = 0; p += (p2 * c) + (p1 * b); cout << p << n ; continue; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, h, t, u, v, tim, d[100100], w[100100], W[100100]; vector<int> e[100100], ans; int main() { scanf( %d%d%d%d , &n, &m, &h, &t); for (int i = 0; i < m; i++) scanf( %d%d , &u, &v), e[u].push_back(v), e[v].push_back(u); for (int i = 1; i < n + 1; i++) d[i] = e[i].size(); for (int i = 1; i < n + 1; i++) { if (d[i] <= h) continue; for (int j : e[i]) w[j] = i; for (int j : e[i]) { tim++; int p = d[i] - 1 - h, q = 0; ans.clear(); for (int k : e[j]) { if (k != i) if (p && w[k] == i) p--, q++, ans.push_back(k), W[k] = tim; else if (w[k] != i) q++, ans.push_back(k), W[k] = tim; if (q == t) break; } if (q == t) { puts( YES ); printf( %d %d n , i, j); int p = 0; for (int k : e[i]) if (k != j && W[k] != tim) { p++, printf( %d , k); if (p == h) break; } puts( ); for (int k : ans) printf( %d , k); return 0; } } } puts( NO ); } |
#include <bits/stdc++.h> int main() { int x, y; scanf( %d%d , &x, &y); double r = sqrt(x * x + y * y); int rmin = r; if ((double)rmin == r) { printf( black ); return 0; } if (x == 0 || y == 0) { printf( black ); return 0; } double tan = (double)y / (double)x; if (rmin % 2 == 1 && tan > 0) printf( white ); else if (rmin % 2 == 1 && tan < 0) printf( black ); else if (rmin % 2 == 0 && tan > 0) printf( black ); else if (rmin % 2 == 0 && tan < 0) printf( white ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int n, m, a, b, g; int dis[N]; long long ans, c[N]; long long sum(int u) { return 1LL * u * (u + 1) / 2; } long long query(int u) { return sum(u / g + 1) * g - 1LL * (u / g + 1) * ((g - ((u + 1) % g)) % g); } int gcd(int u, int v) { if (v == 0) { return u; } return gcd(v, u % v); } set<pair<int, int> > s; int main() { scanf( %d%d%d , &n, &a, &b); g = gcd(a, b); if (n > a + b - g) { ans += query(n) - query(a + b - g); n = a + b - g; } memset(dis, 0x3f, sizeof dis); dis[0] = 0; s.insert({0, 0}); while (!s.empty()) { auto u = *s.begin(); s.erase(s.begin()); if (dis[u.second] != u.first) { continue; } ++c[u.first]; if (u.second >= b && u.first < dis[u.second - b]) { dis[u.second - b] = u.first; s.insert({u.first, u.second - b}); } if (u.second + a <= n && max(u.first, u.second + a) < dis[u.second + a]) { dis[u.second + a] = max(u.first, u.second + a); s.insert({max(u.first, u.second + a), u.second + a}); } } for (int i = 1; i <= n; i++) { c[i] += c[i - 1]; } for (int i = 0; i <= n; i++) { ans += c[i]; } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, first, a[1111]; int b[1111], ub = -1, pos; vector<int> c[1111]; vector<int> d; int can[1111]; void go(int p, int v) { if (v == -1) return; if (v == first) ub = p; c[p].push_back(v); go(p, a[v]); } int main() { scanf( %d%d , &n, &first); --first; for (int i = 0; i < n; ++i) b[i] = -1; for (int i = 0; i < n; ++i) { scanf( %d , a + i); a[i]--; b[a[i]] = i; } for (int i = 0; i < n; ++i) { c[i].clear(); if (b[i] == -1) { go(i, i); } } assert(ub >= 0); d.clear(); for (int i = 0; i < c[ub].size(); ++i) { if (c[ub][i] == first) pos = c[ub].size() - i; } for (int i = 0; i < n; ++i) { if (i == ub) continue; if (c[i].size() != 0) d.push_back(c[i].size()); } sort(d.begin(), d.end()); for (int i = 0; i < n; ++i) can[i] = 0; can[0] = 1; for (int i = 0; i < d.size(); ++i) { for (int j = n - 1; j >= 0; --j) { if ((j + d[i] < n) && (can[j] == 1)) can[j + d[i]] = 1; } } for (int i = 0; i < n; ++i) { if (can[i]) cout << i + pos << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, i, maxi = 0; cin >> n; if (n == 1) { cout << 1 << endl; return 0; } vector<int> a(n, 0); vector<int> right(n, 0); vector<int> left(n, 0); vector<int> ans(n, 0); for (i = 0; i < n; i++) scanf( %d , &a[i]); for (i = 1; i < n; i++) if (a[i] > a[i - 1]) left[i] += left[i - 1] + 1; for (i = n - 2; i >= 0; i--) if (a[i] < a[i + 1]) right[i] += right[i + 1] + 1; for (i = 1; i < n - 1; i++) { if (a[i - 1] < a[i] && a[i] < a[i + 1]) ans[i] = left[i] + right[i] + 1; else if (a[i + 1] > a[i - 1] + 1) ans[i] = left[i - 1] + right[i + 1] + 3; else ans[i] = max(left[i - 1], right[i + 1]) + 2; } ans[0] = (a[0] < a[1]) ? right[0] + 1 : right[1] + 2; ans[n - 1] = (a[n - 1] > a[n - 2]) ? left[n - 1] + 1 : left[n - 2] + 2; for (i = 0; i < n; i++) if (ans[i] > maxi) maxi = ans[i]; cout << maxi << endl; } |
#include <bits/stdc++.h> using namespace std; int n, sum, as, b[100005], len, ia, la; pair<int, int> a[100005]; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i].first; a[i].second = i + 1; sum += a[i].first; } sort(a, a + n); la = n / 2; for (int i = 0; i < n / 2; ++i) as += a[i].first; for (int d = (sum - a[n - 1].first + 1) / 2; as < d; as -= a[ia++].first, as += a[la++].first) ; cout << la - ia << endl; for (int i = 0; i < n; ++i) { if (i < ia || i >= la) b[len++] = a[i].second; else { if (i - ia) cout << ; cout << a[i].second; } } cout << endl; cout << len << endl; for (int i = 0; i < len; ++i) { if (i) cout << ; cout << b[i]; } cout << endl; return 0; } |
#include <bits/stdc++.h> const double PI = acos(-1); int N; long long A; struct point { long long x, y; } p[100001]; struct line { double start, end; } q[100001]; inline long long sqr(long long x) { return x * x; } inline double tan_360(double x, double y) { if (x == 0) return PI * (y > 0 ? 0.5 : 1.5); if (y == 0) return PI * (x > 0 ? 0.0 : 1.0); double ans = atan(y / x); if (x > 0 && y > 0) return ans; if (x < 0 && y > 0) return ans + PI; if (x < 0 && y < 0) return ans + PI; return ans + PI * 2; } bool check(double mid) { int L = 0; for (int i = 1; i <= N; i++) if (sqrt(sqr(A + p[i].x) + sqr(p[i].y)) <= mid - sqrt(sqr(A - p[i].x) + sqr(p[i].y))) return true; for (int i = 1; i <= N; i++) { double R1 = mid, R2 = sqrt(sqr(A - p[i].x) + sqr(p[i].y)), dis = sqrt(sqr(A + p[i].x) + sqr(p[i].y)); if (R1 + R2 > dis && R1 + dis > R2 && R2 + dis > R1) { double mid_angle = tan_360(p[i].x + A, p[i].y), dif = acos((R1 * R1 + dis * dis - R2 * R2) / (2 * R1 * dis)); q[++L] = {mid_angle < dif ? mid_angle - dif + 2 * PI : mid_angle - dif, mid_angle + dif >= 2 * PI ? mid_angle + dif - 2 * PI : mid_angle + dif}; if (q[L].start > q[L].end) std::swap(q[L].start, q[L].end); } } std::sort(q + 1, q + L + 1, [](const line &a, const line &b) { return a.start < b.start || a.start == b.start && a.end < b.end; }); static int stack[100001]; int D = 0; for (int i = 1; i <= L; i++) { while (D && q[stack[D]].end < q[i].start) D--; if (D && q[stack[D]].end < q[i].end) return true; stack[++D] = i; } return false; } int main() { scanf( %d%lld , &N, &A); for (int i = 1; i <= N; i++) scanf( %lld%lld , &p[i].x, &p[i].y); double l = 0, r = A * 2; for (int T = 55; T--;) { double mid = (l + r) / 2; (check(mid) ? r : l) = mid; } printf( %.10lf n , l); return 0; } |
#include <bits/stdc++.h> using namespace std; const int M = 1000100; struct node { int w, flag; int a, b; int id; long long num; } a[M]; int n, pos[M], m, s[M]; bool cmp(const node &x, const node &y) { if (x.w == y.w) return x.flag > y.flag; return x.w < y.w; } int fa[M]; bool cmp1(const node &x, const node &y) { return x.id < y.id; } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= m; i++) { scanf( %d%d , &a[i].w, &a[i].flag); a[i].id = i; } sort(a + 1, a + 1 + m, cmp); for (int i = 1; i <= n; i++) fa[i] = i; int k = 1; for (int i = 1; i <= m; i++) if (a[i].flag) { a[i].a = k; pos[k] = i; a[i].num = (long long)(k - 1) * k / 2 + 1; a[i].b = ++k; } bool flag = 0; for (int i = 1; i <= m; i++) if (a[i].flag) if (a[i].num < (long long)i) { flag = 1; break; } if (flag) { printf( -1 n ); return 0; } pos[n] = m + 1; int h = 3, s1 = 1; for (int i = 1; i < n; i++) { for (int j = pos[i] + 1; j < pos[i + 1];) { a[j].a = s1; a[j].b = h; s1++; if (s1 == h - 1) { s1 = 1; h++; } j++; } } if (flag) { printf( -1 n ); return 0; } sort(a + 1, a + 1 + m, cmp1); for (int i = 1; i <= m; i++) printf( %d %d n , a[i].a, a[i].b); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 605; int nask(vector<int> tmp) { if (!tmp.size() || tmp.size() == 1) return 0; cout << ? << tmp.size() << endl; for (int i = (0), iend = ((int)tmp.size() - 1); i <= iend; i++) cout << tmp[i] << ; cout << endl; cout.flush(); int ans; cin >> ans; if (ans == -1) exit(0); return ans; } int dask(int u, vector<int> tar) { int cntsig = nask(tar); tar.push_back(u); int cnttot = nask(tar); return cnttot - cntsig; } int n; bool vis[maxn]; int bl[maxn]; int find(int u, vector<int> tmp) { vector<int> c1, c2; if (tmp.size() == 1) return tmp[0]; for (int i = (0), iend = ((int)(tmp.size()) - 1); i <= iend; i++) { if (c1.size() < tmp.size() / 2) c1.push_back(tmp[i]); else c2.push_back(tmp[i]); } if (dask(u, c1)) return find(u, c1); else return find(u, c2); } vector<int> half[2]; vector<int> side[maxn]; void dfs(int u) { half[bl[u]].push_back(u); while (1) { vector<int> tmp; for (int i = (1), iend = (n); i <= iend; i++) if (!vis[i]) tmp.push_back(i); if (dask(u, tmp) == 0) return; int tar = find(u, tmp); side[u].push_back(tar); side[tar].push_back(u); vis[tar] = 1; bl[tar] = bl[u] ^ 1; dfs(tar); } } int stk[maxn], top; vector<int> ans; void cfs(int u, int tar) { stk[++top] = u; if (u == tar) { for (int i = (1), iend = (top); i <= iend; i++) ans.push_back(stk[i]); return; } for (int i = (0), iend = (side[u].size() - 1); i <= iend; i++) { int v = side[u][i]; if (!vis[v]) { vis[v] = 1; cfs(v, tar); } } top--; } void ans_out(int u, int v) { memset(vis, 0, sizeof(vis)); vis[u] = 1; cfs(u, v); cout << N << ans.size() << endl; for (int i = (0), iend = ((int)(ans.size()) - 1); i <= iend; i++) cout << ans[i] << ; return; } int main() { ios::sync_with_stdio(0); cin >> n; vis[1] = 1; dfs(1); if (!nask(half[0]) && !nask(half[1])) { cout << Y << half[0].size() << endl; for (int i = (0), iend = ((int)(half[0].size()) - 1); i <= iend; i++) cout << half[0][i] << ; return 0; } else { for (int i = (0), iend = (1); i <= iend; i++) for (int j = (0), jend = ((int)(half[i].size()) - 1); j <= jend; j++) { vector<int> tmp; int u = half[i][j]; for (int k = (0), kend = ((int)(half[i].size()) - 1); k <= kend; k++) if (half[i][k] != u) tmp.push_back(half[i][k]); if (dask(u, tmp)) { for (int k = (0), kend = ((int)(tmp.size()) - 1); k <= kend; k++) { vector<int> gg; gg.push_back(u); gg.push_back(tmp[k]); if (nask(gg)) { ans_out(u, tmp[k]); return 0; } } } } } } |
#include <bits/stdc++.h> using namespace std; int n; int a[200010]; int prv[200010]; int f[200010][10]; void FillAll(int l, int r, int x, int y) { for (int i = x, j = l; i <= y; i++) { int ci = (r - j + 1) / (y - i + 1); ci = max(ci, 2); ci = min(ci, 5); for (int jj = j; jj < j + ci; jj++) a[jj] = i; j += ci; } } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , a + i); } for (int i = 1; i <= n; i++) prv[i] = a[i - 1] ? i - 1 : prv[i - 1]; memset(f, -1, sizeof f); f[0][5] = 0; int last = 0; for (int i = 1; i <= n; i++) if (a[i]) { last = i; int j = prv[i]; int space = i - j - 1; if (a[i] > a[j]) { for (int ki = 0; ki < min(5, space + 1); ki++) for (int kj1 = 1; kj1 <= 5 && f[i][ki + 1] == -1; kj1++) if (f[j][kj1] >= 0) { for (int kj2 = max(0, 2 - kj1); kj2 <= min(space - ki, 5 - kj1); kj2++) { int least = kj2 + 2 * (a[i] - a[j] - 1) + ki; int most = kj2 + 5 * (a[i] - a[j] - 1) + ki; if (least <= space && space <= most) { f[i][ki + 1] = kj2; break; } } } } if (a[i] == a[j]) { int ki = space + 1; for (int kj = 1; kj <= 5 - ki; kj++) if (f[j][kj] >= 0) f[i][ki + kj] = 0; } if (a[i] < a[j]) { printf( -1 ); return 0; } } if (last) { int had = last == n - 1; for (int i = last; i; i = prv[i]) { int j = prv[i]; if (a[i] != a[j]) { int lbound = max(1, 2 - had); int rbound = 5 - had; if (i == last) { lbound = 1 + (last == n); rbound = 5 - (last == n - 1); } bool isRet = 0; for (int ki = rbound; ki >= lbound; ki--) if (f[i][ki] >= 0) { for (int k = i - ki + 1; k <= i; k++) a[k] = a[i]; for (int k = j + 1; k <= j + f[i][ki]; k++) a[k] = a[j]; FillAll(j + f[i][ki] + 1, i - ki, a[j] + 1, a[i] - 1); had = f[i][ki]; isRet = 1; break; } if (!isRet) { printf( -1 ); return 0; } } else { for (int k = j + 1; k < i; k++) a[k] = a[i]; had += i - j; } } if (a[last] != a[last - 1]) { a[last + 1] = a[last]; last++; } if (last > n) { printf( -1 ); return 0; } } for (int i = last + 1; i <= n; i++) if (i == 1 || a[i - 1] == a[i - 2]) a[i] = a[i - 1] + 1; else a[i] = a[i - 1]; if (a[n] != a[n - 1]) a[n] = a[n - 1]; printf( %d n , a[n]); for (int i = 1; i <= n; i++) printf( %d , a[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int N = 1e6 + 5; long long p[N]; long long cnt[N]; vector<long long> a[N]; long long qmi(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; b >>= 1; a = a * a % mod; } return ans % mod; } int main() { long long n, k, z; cin >> n; for (int i = 1; i <= n; i++) { cin >> k; for (int j = 1; j <= k; j++) { cin >> z; a[i].push_back(z); cnt[z]++; } } long long ans = 0; for (int i = 1; i <= n; i++) { long long m = a[i].size(); long long temp = 0; for (int j = 0; j < m; j++) { temp += cnt[a[i][j]]; } long long cur = qmi((long long)n * n * m % mod, mod - 2) * temp % mod; ans = (cur + ans) % mod; ans %= mod; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int Iinf = 1e9; const long long LLinf = 1e18; const double eps = 1e-6; const long long mod = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); int s, x1, x2; cin >> s >> x1 >> x2; int t1, t2; cin >> t1 >> t2; int p, d; cin >> p >> d; if (d > 0) { if (x1 > x2) { cout << min(t1 * (s * 2 - p - x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); } else if (p > x2) { cout << min(t1 * (s * 2 - p + x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); } else if (p <= x1) { cout << min(t1 * (x1 - p + abs(x1 - x2)), t2 * abs(x1 - x2)); } else if (p > x1) { cout << min(t1 * (s * 2 - p + x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); } } else { if (x1 > x2) { if (p >= x1) { cout << min(t1 * (p - x1 + abs(x1 - x2)), t2 * (x1 - x2)); } else { cout << min(t1 * (p + s + s - x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); } } else { cout << min(t1 * (p + x1 + abs(x1 - x2)), t2 * abs(x1 - x2)); } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int n = 1 << 17; double sum[n * 2], lazy_mul[n * 2], lazy_add[n * 2]; void push(int i, int l, int r) { if (lazy_mul[i] != 1 || lazy_add[i] != 0) { sum[i] = sum[i] * lazy_mul[i] + lazy_add[i] * (r - l); if (i < n) { for (int j : {i * 2, i * 2 + 1}) { lazy_mul[j] *= lazy_mul[i]; lazy_add[j] *= lazy_mul[i]; lazy_add[j] += lazy_add[i]; } } lazy_mul[i] = 1; lazy_add[i] = 0; } } void fma(double mul, double add, int a, int b, int l = 0, int r = n, int i = 1) { push(i, l, r); if (a >= r || b <= l) return; if (a <= l && b >= r) { lazy_mul[i] = mul; lazy_add[i] = add; push(i, l, r); return; } int m = l + r >> 1; fma(mul, add, a, b, l, m, i * 2); fma(mul, add, a, b, m, r, i * 2 + 1); sum[i] = sum[i * 2] + sum[i * 2 + 1]; } double querySum(int a, int b, int l = 0, int r = n, int i = 1) { push(i, l, r); if (a >= r || b <= l) return 0; if (a <= l && b >= r) { return sum[i]; } int m = l + r >> 1; return querySum(a, b, l, m, i * 2) + querySum(a, b, m, r, i * 2 + 1); } int main() { fill_n(lazy_mul, n * 2, 1.); ios::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; for (int i = 0; i < n; i++) { int a; cin >> a; fma(1, a, i, i + 1); } while (q--) { int type; cin >> type; if (type == 1) { int l1, r1, l2, r2; cin >> l1 >> r1 >> l2 >> r2; l1--, l2--; double e1 = querySum(l1, r1) / (r1 - l1); double e2 = querySum(l2, r2) / (r2 - l2); double p1 = 1. / (r1 - l1); fma(1 - p1, p1 * e2, l1, r1); double p2 = 1. / (r2 - l2); fma(1 - p2, p2 * e1, l2, r2); } else { int l, r; cin >> l >> r; l--; cout << setprecision(20) << querySum(l, r) << n ; } } } |
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; int arr[n]; int a = 0, b = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] == 1) a++; else b++; } vector<pair<int, int>> v(m); for (int i = 0; i < m; i++) cin >> v[i].first >> v[i].second; for (int i = 0; i < m; i++) { if ((v[i].second - v[i].first + 1) % 2 == 1) cout << 0 << endl; else { int c = v[i].second - v[i].first + 1; int d = c / 2; if (d <= a && d <= b) cout << 1 << endl; else cout << 0 << endl; } } } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; int n, m, x, y, f[300010], a[300010][4]; void dfs(int x) { int cnt = 0; for (int i = 1; i <= a[x][0]; i++) cnt += f[x] == f[a[x][i]]; if (cnt > 1) { f[x] ^= 1; for (int i = 1; i <= a[x][0]; i++) if (f[x] == f[a[x][i]]) dfs(a[x][i]); } } int main() { cin >> n >> m; while (m--) { cin >> x >> y; a[x][++a[x][0]] = y; a[y][++a[y][0]] = x; } for (int i = 1; i <= n; i++) dfs(i); for (int i = 1; i <= n; i++) cout << f[i]; } |
#include <bits/stdc++.h> using namespace std; string str; int main() { cin >> str; bool chk = false; for (int i = 0; i <= str.length(); i++) { for (char ch = a ; ch <= z ; ch++) { string temp1 = str.substr(0, i) + ch + str.substr(i); string temp2 = temp1; reverse(temp2.begin(), temp2.end()); if (temp1.compare(temp2) == 0) { cout << temp1 << endl; chk = true; break; } } if (chk == true) { break; } } if (chk == false) { cout << NA n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int M = 55; const int K = 200010; const int LIT = 2500; const int INF = 1 << 30; const int dir[6][2] = {{2, 0}, {-2, 0}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}}; bool check(long long x, long long y, long long n) { long long dist = x * x + 3 * y * y; long long cmp = 4 * n * n; return dist <= cmp; } bool inside(long long x, long long y, long long n) { for (int j = 0; j < 6; j++) { long long xx = x + dir[j][0]; long long yy = y + dir[j][1]; if (!check(xx, yy, n)) return 0; } return 1; } void solve(int n) { long long x = 0, y = 0; long long res = 1; while (1) { y += 2; if (!inside(x, y, n)) break; res += 2; } while (y >= 0) { x += 3, y++; while (y >= 0) { if (inside(x, y, n)) { res += (y + 1) * 2; break; } y -= 2; } } cout << res << endl; } int main() { int n; while (cin >> n) { solve(n); } } |
#include <bits/stdc++.h> using namespace std; const int N = 100100; long long n, x; void printbin(long long a) { for (int i = 20; i >= 0; i--) { long long c = a & (1LL << i); cout << bool(c); } cout << n ; } int main() { cin >> n >> x; long long reversed = 0; reversed |= 1LL << 17; if (n == 1) { cout << YES n << x; return 0; } if (n == 2 && x == 0) { cout << NO n ; return 0; } if (n == 2) { cout << YES n << 0 << << x << n ; return 0; } cout << YES n ; long long value = 0; long long test = 0; for (int i = 1; i <= n - 3; i++) { cout << i << ; value ^= i; test ^= i; } reversed = x ^ value; reversed |= 1LL << 17; long long temp2 = 1LL << 17; if (reversed == 1LL << 17) { reversed |= 1LL << 18; long long temp = 1LL << 18; cout << reversed << << temp << << (temp ^ reversed) << n ; } else { cout << reversed << << temp2 << << 0 << n ; } return 0; } |
#include <bits/stdc++.h> namespace mist { const int W = 103, N = W * W, M = 404; int MOD; long long inv(int x) { return (x == 1 || x == -1) ? x : (-(MOD / x) * inv(MOD % x) % MOD); } struct calculator { int s[M][M]; int n; void reset(int init_n) { n = init_n; memset(s, 0, sizeof(s)); } void add(int x, int y) { s[x][y]--, s[y][x]--, s[x][x]++, s[y][y]++; } int calc() { n--; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) s[i][j] %= MOD; int sign = 1, row = 1, drow; for (int col = 1; col <= n; col++) { for (drow = row; drow <= n && !s[drow][col]; drow++) ; if (drow > n) return 0; if (drow != row) { sign *= -1; for (int i = 1; i <= n; i++) std::swap(s[row][i], s[drow][i]); } for (int i = row + 1; i <= n; i++) if (s[i][col]) { int x = -s[i][col] * inv(s[row][col]) % MOD; for (int j = 1; j <= n; j++) s[i][j] = (s[i][j] + (long long)x * s[row][j]) % MOD; } row++; } int ret = 1; for (int i = 1; i <= n; i++) ret = (long long)ret * s[i][i] % MOD; ret *= sign; return (ret + MOD) % MOD; } } C; struct dus { int q[N]; void reset(int n) { for (int i = 1; i <= n; i++) q[i] = i; } int ask(int p) { return p == q[p] ? p : q[p] = ask(q[p]); } bool uni(int u, int v) { return ask(u) == ask(v); } bool link(int u, int v) { if (uni(u, v)) return 0; q[ask(u)] = ask(v); return 1; } } d; char G[W][W]; int n, m; int mp(int x, int y) { return x * (m + 1) + y; } bool initialize() { scanf( %d%d%d , &n, &m, &MOD); for (int i = 1; i <= n; i++) scanf( %s , G[i] + 1); int total_number = (n + 1) * (m + 1); d.reset(total_number), d.reset(total_number); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (G[i][j] == / ) { int u = mp(i - 1, j), v = mp(i, j - 1), flag = 1; flag = d.link(u, v); if (!flag) return 0; } else if (G[i][j] == ) { int u = mp(i - 1, j - 1), v = mp(i, j), flag = 1; flag = d.link(u, v); if (!flag) return 0; } return 1; } int id[N], tot; int part0() { tot = 0; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) if (!((i + j) & 1) && d.q[mp(i, j)] == mp(i, j)) id[mp(i, j)] = ++tot; C.reset(tot); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (G[i][j] == * ) { int u, v; if ((i + j) & 1) u = mp(i - 1, j), v = mp(i, j - 1); else u = mp(i, j), v = mp(i - 1, j - 1); u = id[d.ask(u)], v = id[d.ask(v)]; C.add(u, v); } return C.calc(); } int part1() { tot = 0; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) if (((i + j) & 1) && d.q[mp(i, j)] == mp(i, j)) id[mp(i, j)] = ++tot; C.reset(tot); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (G[i][j] == * ) { int u, v; if ((i + j) & 1) u = mp(i, j), v = mp(i - 1, j - 1); else u = mp(i - 1, j), v = mp(i, j - 1); u = id[d.ask(u)], v = id[d.ask(v)]; C.add(u, v); } return C.calc(); } void solve() { if (!initialize()) return void(printf( 0 n )); int ans = (part0() + part1()) % MOD; ans = (ans + MOD) % MOD; printf( %d n , ans); } } // namespace mist int main() { mist::solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 5e2 + 5; const int mod = 1e9 + 7; const long long inf = 1e18; int n, ans, pos, pos1; int un[N], un1[N], cl[N], cl1[N], tr; string t, s; int calc(string e) { un[0] = cl[0] = 0, tr = 0; for (int i = 0; i < n; i++) { if (i) un[i] = un[i - 1], cl[i] = cl[i - 1]; if (e[i] == ( ) un[i]++; else { if (un[i]) un[i]--; else cl[i]++; } } if (cl[n - 1] == 0 and un[n - 1] == 0) tr = 1; un1[0] = cl1[0] = 0; for (int i = n - 1; i >= 0; i--) { un1[i] = un1[i + 1], cl1[i] = cl1[i + 1]; if (e[i] == ) ) un1[i]++; else { if (un1[i]) un1[i]--; else cl1[i]++; } } int jog = 0; for (int i = n - 1; i >= 1; i--) if (un[i - 1] == 0 and un1[i] == 0 and cl[i - 1] == cl1[i]) jog++; return jog + tr; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> s; ans = calc(s); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { t = s; swap(t[i], t[j]); if (ans < calc(t)) { ans = calc(t); pos = i, pos1 = j; } } } cout << ans << n << pos + 1 << << pos1 + 1; } |
#include <bits/stdc++.h> using namespace std; const int MAXS = 100000; inline long long get(long long n, long long ma, long long b) { return max(ma, (6 * n + b - 1) / b) * b; } int main() { long long n, a, b; cin >> n >> a >> b; if (a * b >= 6 * n) { cout << a * b << endl; cout << a << << b << endl; } else { long long best = b; for (int i = b; i <= MAXS; i++) if (get(n, a, best) > get(n, a, i)) best = i; long long best2 = a; for (int i = a; i <= MAXS; i++) if (get(n, b, best2) > get(n, b, i)) best2 = i; if (get(n, a, best) < get(n, b, best2)) { a = max(a, (6 * n + best - 1) / best); b = best; } else { a = best2; b = max(b, (6 * n + best2 - 1) / best2); } cout << a * b << endl; cout << a << << b << endl; } } |
#include <bits/stdc++.h> const int MAXN = 100; int R[2 * MAXN + 2]; int main() { int n, k, i; scanf( %d%d , &n, &k); for (i = 1; i <= 2 * n + 1; ++i) scanf( %d , R + i); for (i = 2; i <= 2 * n && k; ++i) if (R[i - 1] < R[i] - 1 && R[i] - 1 > R[i + 1]) { --R[i]; --k; } for (i = 1; i <= 2 * n + 1; ++i) printf( %d , R[i]); putchar( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 300010; pair<int, int> c[MAXN]; int n, x[2]; bool check(int pos, int fst, int snd) { int a = c[pos].first; int k = (x[fst] + a - 1) / a; if (pos + k >= n) { return false; } int val = c[pos + k].first; int dk = (x[snd] + val - 1) / val; if (pos + k + dk > n) { return false; } if (fst == 0) { printf( Yes n ); printf( %d %d n , k, dk); for (int i = 0; i < k; ++i) { printf( %d , c[pos + i].second + 1); } printf( n ); for (int i = 0; i < dk; ++i) { printf( %d , c[pos + k + i].second + 1); } printf( n ); } else { printf( Yes n ); printf( %d %d n , dk, k); for (int i = 0; i < dk; ++i) { printf( %d , c[pos + k + i].second + 1); } printf( n ); for (int i = 0; i < k; ++i) { printf( %d , c[pos + i].second + 1); } printf( n ); } return true; } int main() { scanf( %d%d%d , &n, &x[0], &x[1]); for (int i = 0; i < n; ++i) { scanf( %d , &c[i].first); c[i].second = i; } sort(c, c + n); for (int i = 0; i < n; ++i) { if (check(i, 0, 1) || check(i, 1, 0)) { return 0; } } printf( No n ); return 0; } |
#include <bits/stdc++.h> using namespace std; string s; int n, m, ans = 0; int main() { cin >> n >> m; while (n--) { cin >> s; } int flag = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] == B ) { flag = 1; } else if (flag) { flag = 0; ans++; } } cout << ans + flag << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; long long x2, y2; long long euler(long long a, long long b) { if (b == 0) { x2 = 1; y2 = 0; return a; } else { long long res = euler(b, a % b); long long temp = x2; x2 = y2; y2 = temp - (a / b) * y2; return res; } } int main() { int T; long long x, y, m, n, vx, vy; long long k1, k2; long long res, res1, res2, xx, yy; cin >> n >> m >> x >> y >> vx >> vy; if (!vx) { if ((!x) || (x == n)) { cout << x << << (vy > 0 ? m : 0) << endl; } else cout << -1 << endl; return 0; } if (!vy) { if ((!y) || (y == m)) { cout << (vx > 0 ? n : 0) << << y << endl; } else cout << -1 << endl; return 0; } k1 = (vx == 1 ? ((n - x) % n) : (x % n)); k2 = (vy == 1 ? ((m - y) % m) : (y % m)); long long g = euler(n, m); if (abs(k1 - k2) % g) { cout << -1 << endl; return 0; } long long lcm = m * n / g; res1 = (k2 * x2) % m; res1 = res1 * (n / g); res2 = (k1 * y2) % n; res2 = res2 * (m / g); res = (res1 + res2) % lcm; res = (res + lcm) % lcm; xx = x + res * vx; yy = y + res * vy; cout << ((xx % (2 * n)) + 2 * n) % (2 * n) << << ((yy % (2 * m)) + 2 * m) % (2 * m) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAX = 1e5 + 5; int n, s, h, m; vector<pair<int, int> > a; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin >> n >> s; for (int i = 0; i < n; i++) { cin >> h >> m; a.push_back(make_pair(h, m)); } bool done = false; if (a[0].first * 60 + a[0].second >= s + 1) cout << 0 0 ; else { for (int i = 0; i < n - 1; i++) { int gap = (a[i + 1].first - a[i].first) * 60 + (a[i + 1].second - a[i].second); if (gap >= 2 * (s + 1)) { done = true; if (a[i].second + s + 1 >= 60) { int mins = s + 1 - (60 - a[i].second); if (mins == 60) cout << a[i].first + 2 << 0 ; else cout << a[i].first + 1 << << mins; } else cout << a[i].first << << a[i].second + s + 1; break; } } if (!done) { if (a[n - 1].second + s + 1 >= 60) { int mins = s + 1 - (60 - a[n - 1].second); if (mins == 60) cout << a[n - 1].first + 2 << 0 ; else cout << a[n - 1].first + 1 << << mins; } else cout << a[n - 1].first << << a[n - 1].second + s + 1; } } return 0; } |
#include <bits/stdc++.h> #pragma GCC target( avx2 ) #pragma GCC optimization( O3 ) #pragma GCC optimization( unroll-loops ) using namespace std; template <class T> using vec = std::vector<T>; bool __hack = std::ios::sync_with_stdio(false); auto __hack1 = cin.tie(nullptr); struct Input { Input(istream &in) : in(&in) {} template <class T> T next() const { T x; *in >> x; return x; } int ni() const { return next<int>(); } istream *in; }; Input in(cin); class Output { private: ostream *out; template <typename T> void printSingle(const T &value) { *out << value; } public: Output(ostream &out) : out(&out) {} inline void print() {} template <typename T, typename... Ts> inline void print(const T &f, const Ts &...args) { printSingle(f); if (sizeof...(args) != 0) { *out << ; print(args...); } } template <typename... Ts> inline void println(const Ts &...args) { print(args...); *out << n ; } template <typename... Ts> inline void operator()(const Ts &...args) { println(args...); } template <typename T> ostream &operator<<(const T &x) { *out << x; return *out; } }; Output out(cout); namespace template_util { constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; } template <int N> struct bytetype {}; template <uint64_t N> struct minimal_uint : bytetype<bytecount(N)> {}; } // namespace template_util void solveOne() { auto n = in.ni(); auto m = in.ni(); vec<pair<int, int>> edges(m); for (int i = 0; i < (m); ++i) { auto x = in.ni() - 1; auto y = in.ni() - 1; if (x > y) swap(x, y); edges[i] = {x, y}; } vec<int> used(3 * n, 0); vec<int> takenEdges; for (int i = 0; i < (m); ++i) { if (!used[edges[i].first] && !used[edges[i].second]) { takenEdges.push_back(i); used[edges[i].first] = true; used[edges[i].second] = true; } if (takenEdges.size() == n) { out( Matching ); for (const auto &e : takenEdges) { out << e + 1 << ; } out(); return; } } out( IndSet ); int cnt = 0; for (int i = 0; i < (3 * n); ++i) { if (!used[i]) { out << i + 1 << ; ++cnt; } if (cnt == n) break; } out(); } void solve(istream &inStream, ostream &outStream) { in = Input(inStream); out = Output(outStream); auto tests = in.ni(); while (tests--) { solveOne(); } } int main() { solve(cin, cout); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } int sub(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } char S[1 << 20]; int B[1 << 20], W[1 << 20], X[1 << 20]; int func(int a, int b, char c) { if (a > b) return 0; int *p = (c == B ? B : (c == W ? W : X)); return p[b] - p[a - 1]; } int noBW[1 << 20][2]; int onlyW[1 << 20][2]; int onlyB[1 << 20][2]; int noW[1 << 20][2]; int pow2[1 << 20]; int main() { int n, k; scanf( %d%d%s , &n, &k, &S[1]); if (2 * k > n) { puts( 0 ); return 0; } for (int i = 1; i <= n; ++i) { B[i] = B[i - 1] + !!(S[i] == B ); W[i] = W[i - 1] + !!(S[i] == W ); X[i] = X[i - 1] + !!(S[i] == X ); } if (k == 1) { int res = 0; if (func(1, n, B ) == 0) res++; if (func(1, n, W ) == 0) res++; for (int i = 1; i + 1 <= n; ++i) if (func(1, i, B ) == 0 && func(i + 1, n, W ) == 0) res++; int tmp = 1; for (int i = 1; i <= n; ++i) if (S[i] == X ) tmp = add(tmp, tmp); printf( %d n , sub(tmp, res)); return 0; } noBW[0][0] = noBW[0][1] = 1; if (S[1] != W ) noBW[1][0] = 1; if (S[1] != B ) noBW[1][1] = 1; for (int i = 2; i <= n; ++i) { if (S[i] != W ) { noBW[i][0] = add(noBW[i][0], add(noBW[i - 1][0], noBW[i - 1][1])); if (i >= k && func(i - k + 1, i - 1, W ) == 0) noBW[i][0] = sub(noBW[i][0], noBW[i - k][1]); } if (S[i] != B ) { noBW[i][1] = add(noBW[i][1], add(noBW[i - 1][0], noBW[i - 1][1])); if (i >= k && func(i - k + 1, i - 1, B ) == 0) noBW[i][1] = sub(noBW[i][1], noBW[i - k][0]); } } for (int i = 1; i <= n; ++i) { if (S[i] != W ) { onlyW[i][0] = add(onlyW[i][0], add(onlyW[i - 1][0], onlyW[i - 1][1])); if (i >= k && func(i - k + 1, i - 1, W ) == 0) onlyW[i][0] = sub(onlyW[i][0], onlyW[i - k][1]); onlyB[i][0] = add(onlyB[i][0], add(onlyB[i - 1][0], onlyB[i - 1][1])); if (i >= k && func(i - k + 1, i - 1, W ) == 0) onlyB[i][0] = add(onlyB[i][0], noBW[i - k][1]); } if (S[i] != B ) { onlyB[i][1] = add(onlyB[i][1], add(onlyB[i - 1][0], onlyB[i - 1][1])); if (i >= k && func(i - k + 1, i - 1, B ) == 0) onlyB[i][1] = sub(onlyB[i][1], onlyB[i - k][0]); onlyW[i][1] = add(onlyW[i][1], add(onlyW[i - 1][0], onlyW[i - 1][1])); if (i >= k && func(i - k + 1, i - 1, B ) == 0) onlyW[i][1] = add(onlyW[i][1], noBW[i - k][0]); } } noW[n + 1][0] = 1; for (int i = n; i >= 1; --i) { if (S[i] != W ) { noW[i][0] = add(noW[i][0], add(noW[i + 1][0], noW[i + 1][1])); } if (S[i] != B ) { noW[i][1] = add(noW[i][1], add(noW[i + 1][0], noW[i + 1][1])); if (i + k <= n + 1 && func(i, i + k - 1, B ) == 0) noW[i][1] = sub(noW[i][1], noW[i + k][0]); } } pow2[0] = 1; for (int i = 1; i <= n; ++i) pow2[i] = add(pow2[i - 1], pow2[i - 1]); int res = 0; res = add(res, add(noBW[n][0], noBW[n][1])); res = add(res, add(onlyW[n][0], onlyW[n][1])); res = add(res, add(onlyB[n][0], onlyB[n][1])); for (int i = n - k + 1; i >= 1; --i) if (func(i, i + k - 1, W ) == 0) res = add(res, (long long)onlyW[i - 1][1] * add(noW[i + k][0], noW[i + k][1]) % MOD); printf( %d n , sub(pow2[func(1, n, X )], res)); return 0; } |
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, x; cin >> n >> m >> x; x--; if (n > m) { cout << 0 << endl; return 0; } vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, 0)); dp[0][0] = 1; for (int i = 0; i < m; i++) { vector<vector<long long>> ndp(n + 1, vector<long long>(n + 1, 0)); for (int j = 0; j <= n; j++) for (int k = 0; j + k <= n; k++) { if (i != x) (ndp[j][k] += dp[j][k]) %= mod; if (j + k + 1 <= n) (ndp[j + 1][k] += dp[j][k]) %= mod; if (i != x && j) (ndp[j - 1][k + 1] += dp[j][k]) %= mod; if (j + k + 1 <= n) (ndp[j][k + 1] += dp[j][k]) %= mod; } swap(dp, ndp); } long long ans = dp[0][n]; for (int i = 1; i <= n; i++) (ans *= i) %= mod; cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int n, m, k; int s; struct DSU { int p[505]; void init() { for (int i = 1; i <= n; i++) p[i] = i; } int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); } int count() { int count = 0; for (int i = 1; i <= n; i++) if (p[i] == i) count++; return count; } void link(int a, int b) { p[find(a)] = find(b); } } L[10050], R[10050]; DSU hebing(DSU L, DSU R) { DSU a = L; for (int i = 1; i <= n; i++) a.link(i, R.find(i)); return a; } int l[10050], r[10050]; int main() { cin >> n >> m; L[0].init(); for (int i = 1; i <= m; i++) { cin >> l[i]; cin >> r[i]; L[i] = L[i - 1]; L[i].link(l[i], r[i]); } R[m + 1].init(); for (int i = m; i >= 1; i--) { R[i] = R[i + 1]; R[i].link(l[i], r[i]); } cin >> k; while (k--) { int x1, y1; cin >> x1 >> y1; DSU h = hebing(L[x1 - 1], R[y1 + 1]); cout << h.count() << endl; } } |
#include <bits/stdc++.h> using namespace std; int main() { map<int, int, greater<int>> input; map<int, int, greater<int>> output; map<int, int, greater<int>> input_reverse; map<int, int, greater<int>> output_reverse; int N; scanf( %d , &N); ; int i = N; int index = 1; while (i--) { int x; scanf( %d , &x); input[index] = x; input_reverse[x] = index++; } index = 1; while (N--) { int x; scanf( %d , &x); output[index] = x; output_reverse[x] = index++; } map<int, int, greater<int>>::iterator it1; int val = 0; int count_val = 0; it1 = input.begin(); while (!input.empty()) { int x = (*it1).second; if (x != (*(output.begin())).second) { val = val + count_val + 1; count_val = 0; } else { count_val++; } it1++; input.erase((*(input_reverse.find(x))).second); output.erase((*(output_reverse.find(x))).second); } printf( %d n , val); } |
#include <bits/stdc++.h> namespace FastIO { bool IOerror = 0; inline char nc() { static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; if (p1 == pend) { p1 = buf; pend = buf + fread(buf, 1, 100000, stdin); if (pend == p1) { IOerror = 1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch == || ch == n || ch == r || ch == t ; } template <class T> inline bool read(T &x) { bool sign = 0; char ch = nc(); x = 0; for (; blank(ch); ch = nc()) ; if (IOerror) return false; if (ch == - ) sign = 1, ch = nc(); for (; ch >= 0 && ch <= 9 ; ch = nc()) x = x * 10 + ch - 0 ; if (sign) x = -x; return true; } template <class T, class... U> bool read(T &h, U &...t) { return read(h) && read(t...); } }; // namespace FastIO using namespace std; using namespace FastIO; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int INF = 0x3f3f3f3f; const int N = 1e5 + 10; struct node { int l, r, sum; bitset<1010> f; } tree[N * 15]; int root[N], cnt; int n, m, q; bitset<1010> base; void ins(int &x, int pre, int l, int r, int i, int j, int op) { x = ++cnt; tree[x] = tree[pre]; if (l == r) { if (op == 1) tree[x].f.set(j); if (op == 2) tree[x].f.reset(j); if (op == 3) tree[x].f ^= base; tree[x].sum = tree[x].f.count(); return; } int mid = (l + r) / 2; if (i <= mid) ins(tree[x].l, tree[pre].l, l, mid, i, j, op); else ins(tree[x].r, tree[pre].r, mid + 1, r, i, j, op); tree[x].sum = tree[tree[x].l].sum + tree[tree[x].r].sum; return; } signed main() { read(n, m, q); for (int i = 1; i <= m; i++) base.set(i); for (int i = 1; i <= q; i++) { int op, x, y; read(op); if (op <= 2) { read(x, y); ins(root[i], root[i - 1], 1, n, x, y, op); } if (op == 3) { read(x); ins(root[i], root[i - 1], 1, n, x, 0, op); } if (op == 4) { read(x); root[i] = root[x]; } printf( %d n , tree[root[i]].sum); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long L[100005]; long long P[100005][40]; long long T[100005]; vector<long long> graph[100005]; long long mx = 0, hh; void dfs(long long src, long long par) { if (src == -1) L[par] = 0; else L[par] = L[src] + 1; if (mx < L[par]) { hh = par; mx = max(mx, L[par]); } T[par] = src; long long len = graph[par].size(); for (long long i = 0; i < (long long)graph[par].size(); i++) { long long v = graph[par][i]; if (v != src) { dfs(par, v); } } } void dfs1(long long src, long long par) { if (src == -1) L[par] = 0; else L[par] = L[src] + 1; if (mx < L[par]) { hh = par; mx = max(mx, L[par]); } long long len = graph[par].size(); for (long long i = 0; i < (long long)graph[par].size(); i++) { long long v = graph[par][i]; if (v != src) { dfs1(par, v); } } } int main() { long long i, j, n, m, t, a, b, c, d, e, f, x, y; scanf( %lld , &t); for (long long cz = 0; cz < t; cz++) { scanf( %lld%lld%lld%lld%lld , &n, &a, &b, &c, &d); for (i = 0; i < n - 1; i++) { scanf( %lld%lld , &x, &y); graph[x].push_back(y); graph[y].push_back(x); } mx = 0; dfs(-1, a); long long cc, dd; cc = L[b]; for (i = 0; i <= n; i++) { L[i] = 0; } mx = 0; dfs1(-1, hh); if (c * 2 >= min(d, mx) || cc <= c) { printf( Alice n ); } else printf( Bob n ); for (i = 0; i <= n; i++) { graph[i].clear(); L[i] = 0; } } } |
#include <bits/stdc++.h> using namespace std; template <typename T> int size(T &a) { return (int)a.size(); } template <typename T> T sqr(T a) { return a * a; } const int INF = int(1e9); struct pt { int cnt; long long sumr; long long suml; long long f; int pr; int x; int lefm; int rim; pt *L, *R; }; const int MAXN = int(1e5); pt buf[MAXN * 2]; int sz = 0; pt *init(int x) { buf[sz].cnt = 1; buf[sz].L = buf[sz].R = NULL; buf[sz].x = x; buf[sz].pr = (rand() << 20) + rand(); buf[sz].suml = 0; buf[sz].sumr = 0; buf[sz].f = 0; buf[sz].lefm = buf[sz].rim = x; return &buf[sz++]; } int getcnt(pt *p) { return p == NULL ? 0 : p->cnt; } int getlefm(pt *p) { return p == NULL ? 2 * INF : p->lefm; } int getrim(pt *p) { return p == NULL ? -2 * INF : p->rim; } long long getf(pt *p) { return p == NULL ? 0 : p->f; } long long getsuml(pt *p) { return p == NULL ? 0 : p->suml; } long long getsumr(pt *p) { return p == NULL ? 0 : p->sumr; } void update(pt *&p) { if (p == NULL) return; p->cnt = getcnt(p->L) + getcnt(p->R) + 1; p->lefm = min(getlefm(p->L), p->x); p->rim = max(getrim(p->R), p->x); int n1 = getcnt(p->L); int n2 = getcnt(p->R); p->f = getf(p->L) + getf(p->R); p->f += getsumr(p->L) + (long long)n1 * (p->x - getrim(p->L)); p->f += getsuml(p->R) + (long long)n2 * (getlefm(p->R) - p->x); p->f += (long long)n1 * n2 * ((p->x - getrim(p->L)) + (getlefm(p->R) - p->x)); p->f += getsumr(p->L) * n2 + getsuml(p->R) * n1; p->suml = getsuml(p->L) + (p->x - p->lefm) + getsuml(p->R) + (long long)(n2 ? (getlefm(p->R) - p->lefm) : 0) * n2; p->sumr = getsumr(p->R) + (p->rim - p->x) + getsumr(p->L) + (long long)(n1 ? (p->rim - getrim(p->L)) : 0) * n1; } void merge(pt *a, pt *b, pt *&p) { if (a == NULL) { p = b; } else if (b == NULL) { p = a; } else if (a->pr < b->pr) { merge(a, b->L, b->L); p = b; update(p); } else { merge(a->R, b, a->R); p = a; update(p); } } void split(pt *t, pt *&a, pt *&b, int by) { if (t == NULL) { a = b = NULL; } else { if (t->x <= by) { split(t->R, t->R, b, by); a = t; update(a); } else { split(t->L, a, t->L, by); b = t; update(b); } } } int curx[MAXN]; void remove(pt *&t, int x) { pt *p, *q; pt *a, *b; split(t, p, q, x); split(p, a, b, x - 1); merge(a, q, t); } void insert(pt *&t, pt *i) { pt *p, *q; split(t, p, q, i->x); merge(p, i, t); merge(t, q, t); } long long query(pt *&t, int l, int r) { pt *p, *q, *a, *b; split(t, p, q, r); split(p, a, b, l - 1); long long res = getf(b); merge(a, b, t); merge(t, q, t); return res; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = (0); i < (n); ++i) cin >> curx[i]; pt *root = NULL; for (int i = (0); i < (n); ++i) { insert(root, init(curx[i])); } int q; cin >> q; for (int iter = (0); iter < (q); ++iter) { int t; cin >> t; if (t == 1) { int p, d; cin >> p >> d; --p; remove(root, curx[p]); curx[p] += d; insert(root, init(curx[p])); } else { int l, r; cin >> l >> r; cout << query(root, l, r) << n ; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int const N = 200001; int main() { long long n, k, d; scanf( %lld%lld%lld , &n, &k, &d); if (d < k) return puts( NO ), 0; long long dv = d / k; if (dv + 1 > n || (dv + (d % k == 0 ? 0 : 2)) > n) return puts( NO ), 0; vector<long long> an; puts( YES ); if (d % k == 0) { for (int j = 0; j < k; ++j) { if (j & 1) printf( 1 ); else printf( %lld , 1ll + dv); } return 0; } long long lst = 1; for (int j = 0; j < (d % k); ++j) { if (j & 1) printf( 1 ), lst = 1ll; else printf( %lld , 2ll + dv), lst = 2ll + dv; } for (int j = 0; j < k - (d % k); ++j) { if (j & 1) printf( %lld , lst); else printf( %lld , lst - dv >= 1 ? lst - dv : lst + dv); } } |
#include <bits/stdc++.h> using namespace std; long long n, m, p; int q[100010]; long long f[106][100010], D[100010], sum[100010], A[100010], g[100010]; int main() { scanf( %lld %lld %lld , &n, &m, &p); for (int i = 2; i <= n; i++) scanf( %lld , &D[i]), D[i] += D[i - 1]; long long H, T; for (int i = 1; i <= m; i++) { scanf( %lld %lld , &H, &T); A[i] = T - D[H]; } sort(A + 1, A + 1 + m); for (int i = 1; i <= m; i++) sum[i] = A[i] + sum[i - 1]; memset(f, 0x3f, sizeof(f)); f[0][0] = 0; for (int i = 1; i <= p; i++) { int l = 1, r = 1; q[1] = 0; for (int j = 1; j <= m; j++) g[j] = f[i - 1][j] + sum[j]; for (int j = 1; j <= m; j++) { while (l < r && (g[q[l + 1]] - g[q[l]]) <= A[j] * (q[l + 1] - q[l])) l++; f[i][j] = min(f[i - 1][j], g[q[l]] + A[j] * (j - q[l]) - sum[j]); if (g[j] >= 0x3f3f3f3f3f3f3f3fll) continue; while (l < r && (g[j] - g[q[r]]) * (q[r] - q[r - 1]) <= (g[q[r]] - g[q[r - 1]]) * (j - q[r])) r--; q[++r] = j; } } cout << f[p][m] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int s, x; cin >> s >> x; long long int i, c = 1, f = 0; long long int a = (s - x) / 2; if ((s - x) % 2 != 0) cout << 0; else { for (i = 0; i < 64; i++) { long long int x1 = ((x >> i) & 1); long long int a1 = ((a >> i) & 1); if (x1 == 0 && a1 == 1) f = 1; if (x1 == 1 && a1 == 0) c = c * 2; if (x1 == 1 && a1 == 1) { f = 1; c = 0; } } if (f == 1) cout << c; else cout << (c - 2); } } |
#include <bits/stdc++.h> using namespace std; int main() { int i, t, n, m, a[105], ans[105] = {0}; cin >> n >> m; for (__typeof(0) i = 0; i <= m - 1; i++) cin >> a[i]; for (__typeof(0) i = 0; i <= m - 1; i++) { int tem = a[i]; for (int j = tem; j <= n; j++) { if (ans[j] == 0) ans[j] = a[i]; } } for (__typeof(1) i = 1; i <= n; i++) cout << ans[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MX = 100000 + 10; long long n, arr[MX]; struct node { long long bit_[3]; node() { memset(bit_, 0, sizeof(bit_)); } }; vector<node> tree; void add(long long inode, long long ptr, long long num) { if (ptr < 0) return; long long q = (num >> ptr) & 1; if (tree[inode].bit_[q] == 0) { tree.push_back(node()); tree[inode].bit_[q] = (int)tree.size() - 1; } add(tree[inode].bit_[q], ptr - 1, num); } long long find_fit(long long x) { int ptr = 45; long long nd = 0; long long sol = 0; while (ptr >= 0) { sol *= 2; long long q = !((x >> ptr) & 1); if (q == 1) { if (tree[nd].bit_[1] != 0) { nd = tree[nd].bit_[1]; sol++; } else nd = tree[nd].bit_[0]; } else { if (tree[nd].bit_[0] != 0) nd = tree[nd].bit_[0]; else { nd = tree[nd].bit_[1]; sol++; } } ptr--; } return sol; } int main() { tree.push_back(node()); cin >> n; long long q = 0; add(0, 45, 0); for (int i = 0; i < n; i++) { cin >> arr[i]; q ^= arr[i]; add(0, 45, q); } q = 0; long long ans = 0; for (int i = n - 1; i >= 0; i--) { q ^= arr[i]; long long p = find_fit(q); ans = max(ans, p ^ q); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct node { long long y, z; long long x; int choice; }; node g[2000001]; int cmp(node head, node a) { if (head.y != a.y) return head.y < a.y; if (head.z != a.z) return head.z < a.z; return head.x > a.x; } struct info { int choice; long long x; } f[2000001]; int m, n; int has = 0; struct self { int x, y, z; } s[33]; long long ans; int choicex, choicey; int ipow(int i, int k) { if (k == 0) return 1; return i * ipow(i, k - 1); } int bs(long long y, long long z) { int l = 1, r = has, mid, ret = 0; while (l <= r) { mid = (l + r) >> 1; if (g[mid].y > y || (g[mid].y == y && g[mid].z >= z)) { ret = mid; r = mid - 1; } else l = mid + 1; } if (g[ret].y == y && g[ret].z == z) return ret; else return 0; } void rep(int i, int u) { if (i == 1) { int t = u % 3; if (t == 0) printf( LM n ); if (t == 1) printf( LW n ); if (t == 2) printf( MW n ); return; } rep(i - 1, u / 3); int t = u % 3; if (t == 0) printf( LM n ); if (t == 1) printf( LW n ); if (t == 2) printf( MW n ); } void p(int i, int u) { while (i <= m) { int t = u % 3; u /= 3; i++; if (t == 0) printf( LM n ); if (t == 1) printf( LW n ); if (t == 2) printf( MW n ); } } void print(int l, int r) { rep(n, l); p(n + 1, r); } int main() { ans = -1e14; scanf( %d , &m); for (int i = 1; i <= m; i++) scanf( %d%d%d , &s[i].x, &s[i].y, &s[i].z); if (m % 2 == 0) n = m / 2; else n = m / 2 + 1; for (int i = 0; i < ipow(3, n); i++) { int v = i; long long x = 0, y = 0, z = 0; for (int j = n; j >= 1; j--) { int k = v % 3, li = j; long long a = x, b = y; if (k == 0) { x += s[li].x; y += s[li].y; } if (k == 1) { x += s[li].x; z += s[li].z; } if (k == 2) { y += s[li].y; z += s[li].z; } v /= 3; } y -= x; z -= x; has++; g[has].x = x; g[has].y = y; g[has].z = z; g[has].choice = i; } sort(g + 1, g + has + 1, cmp); for (int i = 0; i < ipow(3, m - n); i++) { int v = i; long long x = 0, y = 0, z = 0; for (int j = n + 1; j <= m; j++) { int k = v % 3, li = j; if (k == 0) { x += s[li].x; y += s[li].y; } if (k == 1) { x += s[li].x; z += s[li].z; } if (k == 2) { y += s[li].y; z += s[li].z; } v /= 3; } y -= x; z -= x; int pos = bs(-y, -z); if (pos != 0) if (g[pos].x + x > ans) { ans = g[pos].x + x; choicex = g[pos].choice; choicey = i; } } if (ans != -1e14) print(choicex, choicey); else printf( Impossible n ); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int test; cin >> test; while (test--) { int n, m; cin >> n >> m; string S; cin >> S; vector<int> P(m); vector<int> cnt(n); for (int i = 0; i < m; i++) { cin >> P[i]; cnt[0]++; cnt[P[i]]--; } vector<int> res(26); for (int i = 0; i < n; i++) res[S[i] - a ]++; res[S[0] - a ] += cnt[0]; for (int i = 1; i < n; i++) { cnt[i] += cnt[i - 1]; res[S[i] - a ] += cnt[i]; } for (int i = 0; i < 26; i++) cout << res[i] << << flush; cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100005; struct node { long long x, s[8]; int id; } a[N]; int n, p, t; long long f[N][(1 << 8) + 5]; bool cmp(node a, node b) { return a.x > b.x; } int main() { scanf( %d%d%d , &n, &p, &t); for (int i = 1; i <= n; i++) { scanf( %lld , &a[i].x); a[i].id = i; } for (int i = 1; i <= n; i++) for (int j = 1; j <= p; j++) scanf( %lld , &a[i].s[j]); sort(a + 1, a + n + 1, cmp); memset(f, -1, sizeof(f)); f[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < (1 << p); j++) { f[i][j] = f[i - 1][j]; int sum = 0; for (int k = 0; k < p; k++) if (j & (1 << k)) sum++; int tmp = i - 1 - sum; if (tmp < t && f[i - 1][j] != -1) f[i][j] += a[i].x; for (int k = 0; k < p; k++) { if (((j & (1 << k))) && f[i - 1][j ^ (1 << k)] != -1) f[i][j] = max(f[i][j], f[i - 1][j ^ (1 << k)] + a[i].s[k + 1]); } } } printf( %lld , f[n][(1 << p) - 1]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; long long N, L, v1, v2, k; long long d; pair<double, double> get_it(double mid) { double end = mid * v2; double pass = mid; for (int i = 1; i < d; i++) { double add = (end - pass * v1) / (v2 + v1); end = (pass + add) * v1 + mid * v2; pass += add + mid; } return {pass, end}; } bool check(double mid) { return (get_it(mid).second >= L); } void test_case() { cin >> N >> L >> v1 >> v2 >> k; d = (N + k - 1) / k; double l = 0, r = INF; for (int i = 0; i < 100; i++) { double m = l + (r - l) / 2; if (check(m)) r = m; else l = m; } cout << fixed << setprecision(10); cout << get_it(l).first << n ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; while (tt--) { test_case(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int cd(int a, int b) { return a - a / b; } int main() { int cnt1, cnt2, x, y; cin >> cnt1 >> cnt2 >> x >> y; long long int l = 1, r = 1000000000009; while (l < r) { int mid = l + (r - l) / 2; if (cnt1 <= cd(mid, x) && cnt2 <= cd(mid, y) && cnt1 + cnt2 <= cd(mid, x * y)) r = mid; else l = mid + 1; } cout << r; return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll inf = 1e18; template <typename T> void printvector(const vector<T>& foo) { for (auto el : foo) cout << el << ; cout << endl; } bool calc(vector<int>& permuration, const vector<vector<int>>& sets, vector<bool>& used) { if (permuration.size() == sets.size() + 1) { for (auto el : permuration) cout << el << ; cout << n ; return true; } if (permuration.empty()) { for (int i = 0; i < sets.size(); i++) { permuration.push_back(sets[i][0]); permuration.push_back(sets[i][1]); used[i] = true; if (calc(permuration, sets, used)) return true; swap(permuration[0], permuration[1]); if (calc(permuration, sets, used)) return true; used[i] = false; permuration.pop_back(); permuration.pop_back(); } } vector<int> last_elements; for (size_t next = 0; next < sets.size(); next++) { if (used[next]) continue; const auto& row = sets[next]; if (row.size() > permuration.size() + 1) break; while (last_elements.size() < row.size() - 1) { last_elements.push_back( permuration[permuration.size() - last_elements.size() - 1]); } sort(last_elements.begin(), last_elements.end()); vector<int> diff; set_difference(row.begin(), row.end(), last_elements.begin(), last_elements.end(), back_inserter(diff)); if (diff.size() > 1) continue; used[next] = true; permuration.push_back(diff[0]); if (calc(permuration, sets, used)) return true; used[next] = false; permuration.pop_back(); return false; } return false; } int main() { int test; cin >> test; while (test--) { int n; cin >> n; vector<vector<int>> sets(n - 1); for (auto& row : sets) { int k; cin >> k; row.resize(k); for (auto& el : row) cin >> el; } sort(sets.begin(), sets.end(), [](const vector<int>& a, const vector<int>& b) { return a.size() < b.size(); }); vector<int> permutation; vector<bool> used(n, false); calc(permutation, sets, used); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 110000; int len, k, n; long long a[N]; long long resolve() { int pK = k; set<pair<long long, int>> av; set<pair<long long, int>> changed; long long sumP = 0; for (int i = 0; i < len; i++) { av.insert({a[i], i}); sumP += a[i]; } while (true) { if (!av.size()) break; auto lowest = *av.begin(); if (lowest.first >= 0) break; if (pK == 0) break; av.erase(av.begin()); changed.insert({-lowest.first, lowest.second}); sumP -= lowest.first; sumP += (-lowest.first); pK--; } long long best = sumP; for (int i = 1; i <= n - len; i++) { if (av.count({a[i - 1], i - 1})) { av.erase(av.find({a[i - 1], i - 1})); sumP -= a[i - 1]; } else { changed.erase(changed.find({-a[i - 1], i - 1})); sumP -= (-a[i - 1]); pK++; } int j = i + len - 1; av.insert({a[j], j}); sumP += a[j]; while (pK > 0) { if (av.size()) { auto x = *av.begin(); if (x.first < 0) { av.erase(av.begin()); changed.insert({-x.first, x.second}); sumP -= x.first; sumP += (-x.first); pK--; } else break; } else break; } while (pK == 0) { if (!av.size() || !changed.size()) break; auto x = *av.begin(); auto y = *changed.begin(); if (x.first > 0) break; if (x.first * -1 > y.first) { changed.erase(changed.begin()); av.erase(av.begin()); changed.insert({x.first * -1, x.second}); av.insert({y.first * -1, y.second}); sumP -= y.first; sumP += (-y.first); sumP -= x.first; sumP += (-x.first); } else break; } best = max(best, sumP); } return best; } int main() { scanf( %d %d , &n, &len); for (int i = 0; i < n; i++) scanf( %lld , a + i); scanf( %d , &k); long long ans = resolve(); for (int i = 0; i < n; i++) a[i] *= -1; ans = max(ans, resolve()); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k, l, c, d, p, nl, np; cin >> n >> k >> l >> c >> d >> p >> nl >> np; long long totlime = c * d; long long totdrink = k * l; totdrink = totdrink / nl; long long totsalt = p / np; long long mn = min(totlime, totdrink); mn = min(mn, totsalt); cout << mn / n << n ; } int main() { solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, ans, v[1009], m, c[1009]; vector<int> e; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> v[i]; for (int i = 2; i <= n; i++) { if (v[i] == 0 and v[i - 1] == 1 and v[i + 1] == 1) c[++m] = i + 1; } ans = m; for (int i = 1; i < m; i++) { if (c[i] + 2 == c[i + 1]) { ans--; c[i] = -1e9, c[i + 1] - 1e9; i++; } } cout << ans; } |
#include <bits/stdc++.h> using namespace std; int n, ys, ans; int main() { cin >> n; ys = 0; ans = 0; for (int i = n; i > 0; i /= 8) { ys = i % 8; if (ys == 1) ans++; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; using INT = long long; using pii = pair<int, int>; const int mod = 1e9 + 7; int power(int a, int n, int mod, int ans = 1) { for (; n; n >>= 1, a = (INT)a * a % mod) if (n & 1) ans = (INT)ans * a % mod; return ans; } int fac[3000111], rev[3000111]; int dp[3000111]; int C(int n, int k) { if (k < 0 or k > n) return 0; return (INT)fac[n] * rev[k] % mod * rev[n - k] % mod; } void pre() { for (int i = fac[0] = rev[0] = 1; i < 3000111; i++) { fac[i] = (INT)fac[i - 1] * i % mod; } rev[3000111 - 1] = power(fac[3000111 - 1], mod - 2, mod); for (int i = 3000111 - 2; i >= 0; i--) rev[i] = (INT)rev[i + 1] * (i + 1) % mod; } int main() { pre(); int n, q; cin >> n >> q; dp[0] = n; for (int i = 1; i <= n; i++) { dp[1] = (dp[1] + 3 * i) % mod; dp[2] = (C(i * 3, 2) + dp[2]) % mod; } int r3 = power(3, mod - 2, mod); for (int i = 3; i <= 3 * n; i++) { dp[i] = (C(3 * n, i - 2) + 3LL * C(3 * n, i - 1) + 3LL * C(3 * n, i) + C(3 * n, i + 1) - 3LL * dp[i - 1] - dp[i - 2]) % mod; dp[i] = (INT)dp[i] * r3 % mod; if (dp[i] < 0) dp[i] += mod; } while (q--) { int x; scanf( %d , &x); printf( %d n , dp[x]); } return 0; } |
#include <bits/stdc++.h> int a[(1 << 22) + 1]; int n, m; bool vis[(1 << 22) + 1][2]; bool yes[(1 << 22) + 1]; int mask; void dfs(int now, int p) { if (vis[now][p]) return; vis[now][p] = true; if (p == 0) dfs(now, 1); else { for (int j = 0; j < n; j++) { int jj = 1 << j; if (!(jj & now)) dfs(now | jj, 1); } if (yes[now ^ mask]) dfs(now ^ mask, 0); } } int main() { scanf( %d%d , &n, &m); int ans = 0; for (int i = 0; i < (1 << n); i++) yes[i] = vis[i][0] = vis[i][1] = false; for (int i = 0; i < m; i++) { scanf( %d , &a[i]); yes[a[i]] = true; } mask = (1 << n) - 1; for (int i = 0; i < m; i++) { if (!vis[a[i]][0]) { ans++; dfs(a[i], 0); } } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX_N = 3030; int head[MAX_N], current, n, siz[MAX_N][MAX_N], fa[MAX_N][MAX_N]; long long f[MAX_N][MAX_N]; struct edge { int to, nxt; } edges[MAX_N << 1]; void addpath(int src, int dst) { edges[current].to = dst, edges[current].nxt = head[src]; head[src] = current++; } void dfs(int u, int org) { siz[org][u] = 1; for (int i = head[u]; i != -1; i = edges[i].nxt) if (edges[i].to != fa[org][u]) fa[org][edges[i].to] = u, dfs(edges[i].to, org), siz[org][u] += siz[org][edges[i].to]; } long long dp(int u, int v) { if (u == v) return 0; if (f[u][v]) return f[u][v]; return (f[u][v] = max(dp(u, fa[u][v]), dp(v, fa[v][u])) + 1LL * siz[u][v] * siz[v][u]); } int main() { memset(head, -1, sizeof(head)); scanf( %d , &n); for (int i = 1, u, v; i <= n - 1; i++) scanf( %d%d , &u, &v), addpath(u, v), addpath(v, u); for (int i = 1; i <= n; i++) dfs(i, i); long long ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans = max(ans, dp(i, j)); printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> template <class T> inline void read(T &x) { x = 0; register char c = getchar(); register bool f = 0; while (!isdigit(c)) f ^= c == - , c = getchar(); while (isdigit(c)) x = x * 10 + c - 0 , c = getchar(); if (f) x = -x; } template <class T> inline void print(T x) { if (x < 0) putchar( - ), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 0 ); } template <class T> inline void print(T x, char c) { print(x), putchar(c); } template <class T> inline T gcd(T n, T m) { return m ? gcd(m, n % m) : n; } template <const int N> struct Prime : std::vector<int> { Prime() { std::vector<bool> vis(N); for (int i = 2; i < N; i++) { if (!vis[i]) this->push_back(i); for (int j : *this) { if (i * j >= N) break; vis[i * j] = true; if (!(i % j)) break; } } } }; const int N = 1e5 + 10, M = 1e7 + 10; int n, k, A, B, C, a[N], deg[N], v[N][8], l[N], f[M]; Prime<10000> prime; std::vector<int> pos; std::unordered_map<int, int> primeid; void affect(int *v, int l, int x) { std::function<void(int, int)> dfs = [&](int i, int s) { if (i == l) { if (s != 1) f[s] += x; return; } dfs(i + 1, s); dfs(i + 1, s * v[i]); }; dfs(0, 1); } int query(int *v, int l) { int res = 0; std::function<void(int, int)> dfs = [&](int i, int s) { if (i == l) { res += s < 0 ? -f[-s] : f[s]; return; } dfs(i + 1, s); dfs(i + 1, s * -v[i]); }; dfs(0, -1); return res; } int main() { read(n), read(k); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 0; i < prime.size(); i++) primeid[prime[i]] = i; for (int x, i = 1; i <= n; i++) { x = a[i]; for (int j = 0; prime[j] * prime[j] <= x; j++) if (x % prime[j] == 0) { v[i][l[i]++] = prime[j]; while (x % prime[j] == 0) x /= prime[j]; } if (x != 1) v[i][l[i]++] = x; } for (int i = 1; i <= n; i++) affect(v[i], l[i], 1); for (int i = 1; i <= n; i++) deg[i] = query(v[i], l[i]) - 1; for (int i = 1; i <= n; i++) affect(v[i], l[i], -1); int fulldeg = 0; for (int i = 1; i <= n; i++) fulldeg += deg[i] == n - 1; if (fulldeg >= k) { for (int i = 1; i <= n; i++) if (deg[i] == n - 1) pos.push_back(i); } else { for (int i = 1; i <= n; i++) if (n - 1 - deg[i] >= 2) { A = i; for (int j = 1; j <= n; j++) if (j != A && gcd(a[i], a[j]) == 1) { if (!B) { B = j; continue; } if (!C) { C = j; continue; } break; } break; } if (A) { if (!(B && C)) printf( Assert: Passing [%s] in LINE %d n , __FUNCTION__, 120), exit(0); auto getp = [&](int cur) { std::vector<int> res; for (int i = 1; i <= n && res.size() < cur; i++) if (i != A && i != B && i != C) { res.push_back(i); } return res; }; auto solve = [&](int cur) { int res = 0; std::vector<int> p = getp(cur); for (int i : p) affect(v[i], l[i], 1); for (int i : p) deg[i] = query(v[i], l[i]) - 1; for (int i : p) affect(v[i], l[i], -1); for (int i : p) res += deg[i] != cur - 1; return res; }; int l = 1, r = n - 3, mid, res = -1; while (l <= r) { mid = (l + r) >> 1; solve(mid) + 3 >= k ? (res = mid, r = mid - 1) : (l = mid + 1); } if (!(~res)) printf( Assert: Passing [%s] in LINE %d n , __FUNCTION__, 144), exit(0); int now = solve(res); std::vector<int> p = getp(res); for (int i : p) if (deg[i] != res - 1) pos.push_back(i); if (pos.size() < k) pos.push_back(A); if (pos.size() < k) pos.push_back(B); if (pos.size() < k) pos.push_back(C); } else { uint64_t all = 0; std::vector<int> match(n + 1); std::vector<uint64_t> hsh(n + 1); std::map<uint64_t, int> map; std::map<int, uint64_t> sta; std::mt19937_64 rng( 20040725 ^ std::chrono::steady_clock::now().time_since_epoch().count()); for (int i = 1; i <= n; i++) hsh[i] = rng(), all ^= hsh[i], map[hsh[i]] = i; for (int i = 1; i <= n; i++) for (int x = 1; x < (1 << l[i]); x++) { int s = 1; for (int j = 0; j < l[i]; j++) if ((x >> j) & 1) s *= v[i][j]; sta[s] ^= hsh[i]; } for (int i = 1; i <= n; i++) if (deg[i] == n - 2) { uint64_t tem = all; for (int x = 1; x < (1 << l[i]); x++) { int s = 1; for (int j = 0; j < l[i]; j++) if ((x >> j) & 1) s *= v[i][j]; tem ^= sta[s]; } match[i] = map[tem]; } for (int i = 1; i <= n; i++) if (!match[i] || i < match[i]) pos.push_back(i); } } if (!(pos.size() >= k)) printf( Assert: Passing [%s] in LINE %d n , __FUNCTION__, 186), exit(0); for (int i = 0; i < k; i++) print(pos[i], n [i + 1 == k]); } |
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const int toN = 58; int pn, p[10000]; int n, a[10000], omask[10000]; int dp[110][1 << 18]; int ans[110][1 << 18]; bool isPrime(int n) { if (n == 2) return true; for (int i = 2; i * i <= n; ++i) if (n % i == 0) return false; return true; } int main() { pn = 0; for (int i = 2; i <= toN; ++i) if (isPrime(i)) p[pn++] = i; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i <= n; ++i) { for (int m = 0; m < (1 << pn); ++m) dp[i][m] = inf; } dp[0][0] = 0; for (int i = 1; i <= toN; ++i) { omask[i] = 0; for (int j = 0; j < pn; ++j) if (i % p[j] == 0) omask[i] |= (1 << j); } for (int m = 0; m < (1 << pn); ++m) { for (int j = 1; j <= toN; ++j) if ((omask[j] & m) == 0) { for (int i = 0; i < n; ++i) if (dp[i][m] < inf) { int nm = (m | omask[j]); if (dp[i + 1][nm] > dp[i][m] + abs(j - a[i])) { dp[i + 1][nm] = dp[i][m] + abs(j - a[i]); ans[i + 1][nm] = j; } } } } int v = 0; for (int m = 0; m < (1 << pn); ++m) if (dp[n][v] > dp[n][m]) v = m; vector<int> ansv; for (int i = n; i >= 1; --i) { ansv.push_back(ans[i][v]); v = v - omask[ans[i][v]]; } reverse((ansv).begin(), (ansv).end()); for (__typeof(ansv.begin()) it = ansv.begin(); it != ansv.end(); ++it) cout << *it << ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const int INF = 0x3f3f3f3f; int dp[N]; struct node { int v, id; } a[N][2]; void up1(int sta, int cost, int i) { if (a[sta][0].v > cost) { a[sta][1] = a[sta][0]; a[sta][0] = {cost, i}; } else if (a[sta][1].v > cost) { a[sta][1] = {cost, i}; } } int num = -1, now, cs; pair<int, int> ans; void up2(node x, node y, int sta) { if (x.v == INF || y.v == INF) return; now = x.v + y.v; if (dp[sta] > num || (dp[sta] == num && cs > now)) { num = dp[sta]; cs = now; ans = {x.id, y.id}; } } int main() { int k, n, m, sta, v, cost; scanf( %d %d , &n, &m); for (int i = 1; i <= n; i++) { scanf( %d , &k); sta = 0; for (int j = 1; j <= k; j++) { scanf( %d , &v); sta |= 1 << (v - 1); } dp[sta]++; } int mx = 0; for (int i = 0; i < 9; i++) { for (int j = (1 << 9) - 1; j >= 0; j--) { if ((1 << i) & j) dp[j] = dp[j] + dp[j ^ (1 << i)]; mx = max(mx, dp[j]); } } for (int j = 0; j < (1 << 9); j++) a[j][0].v = a[j][1].v = INF; for (int i = 1; i <= m; i++) { scanf( %d %d , &cost, &k); sta = 0; for (int j = 1; j <= k; j++) { scanf( %d , &v); sta |= 1 << (v - 1); } up1(sta, cost, i); } for (int i = 0; i < (1 << 9); i++) { for (int j = 0; j < (1 << 9); j++) { if (i == j) up2(a[i][0], a[i][1], i); else up2(a[i][0], a[j][0], i | j); } } printf( %d %d n , ans.first, ans.second); return 0; } |
#include <bits/stdc++.h> using namespace std; int n; double vb, vs; double x[105]; double tagx, tagy; double cal(int p) { return sqrt((x[p] - tagx) * (x[p] - tagx) + tagy * tagy); } int cmp(double x) { if (x < (-1e-6)) return -1; else if (x > 1e-6) return 1; else return 0; } int main() { scanf( %d%lf%lf , &n, &vb, &vs); for (int i = 0; i < n; i++) scanf( %lf , &x[i]); scanf( %lf%lf , &tagx, &tagy); double ans = (double)(0x3f3f3f3f); int pos; double min_cal; for (int i = 1; i < n; i++) { double time = (x[i] / vb) + (cal(i) / vs); if (cmp(time - ans) == -1) { ans = time; pos = i; min_cal = cal(i); } else if (cmp(time - ans) == 0) { if (cmp(cal(i) - min_cal) == -1) { ans = time; pos = i; min_cal = cal(i); } } } printf( %d n , pos + 1); return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } int sub(int a, int b) { a -= b; return a < 0 ? a + mod : a; } int mul(int a, int b) { return (long long)a * b % mod; } int qpow(int a, int b) { int ret = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) ret = mul(ret, a); return ret; } const int inv2 = qpow(2, mod - 2); const int N = 1e6 + 5; int n; int hed[N], cnt, to[N], nxt[N]; inline void adde(int u, int v) { ++cnt; to[cnt] = v, nxt[cnt] = hed[u]; hed[u] = cnt; } int f[N][2]; int ans = 0; inline void dfs(int x, int pre) { int way0 = 1; for (int i = hed[x]; i; i = nxt[i]) { int v = to[i]; if (v == pre) continue; dfs(v, x); way0 = mul(way0, add(mul(inv2, f[v][0]), f[v][1])); } int way1 = sub(1, way0); f[x][0] = way0; f[x][1] = way1; ans = add(ans, way1); } int main() { cin >> n; for (int i = 1; i < n; i++) { int u, v; scanf( %d%d , &u, &v); adde(u, v), adde(v, u); } dfs(1, 0); cout << mul(ans, qpow(2, n)) << endl; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int MAXN = 2000 + 5; int dp[MAXN][MAXN] = {0}; int previous[MAXN] = {0}; void calculatePrevious(const string &s, const string &p) { int sSize = s.length(); int pSize = p.length(); for (int i = 0; i < sSize; ++i) { previous[i] = -2; int currentP = pSize - 1; int currentS = i; while (currentP >= 0 && currentS >= 0) { while (currentS >= 0 && s[currentS] != p[currentP]) { currentS--; } if (currentS >= 0) { currentP--; currentS--; } } if (currentP == -1) { previous[i] = currentS; } } } int main() { string s, p; cin >> s >> p; int sSize = s.length(); int pSize = p.length(); calculatePrevious(s, p); for (int i = 1; i <= sSize; ++i) { for (int j = 0; j < sSize; ++j) { dp[i][j] = dp[i - 1][j]; if (j > 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]); int prev = previous[i - 1]; if (prev != -2) { int toDelete = (i - 1) - prev - pSize; int newDelete = j - toDelete; int newIndex = prev + 1; if (newDelete >= 0 && newIndex >= newDelete) { dp[i][j] = max(dp[i][j], 1 + dp[newIndex][newDelete]); } } } } for (int i = 0; i < sSize; ++i) cout << dp[sSize][i] << ; cout << 0 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, j, i = 0, count = 0, max = 100000000; string s1, s2; cin >> n >> m >> s1 >> s2; vector<int> a, b; while (i + n <= m) { count = 0; for (j = 0; j < n; j++) { if (s1[j] != s2[i + j]) { a.push_back(j + 1); count++; } } if (count < max) { max = count; b = a; } i++; a.clear(); } cout << max << endl; for (i = 0; i < max; i++) cout << b[i] << ; cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; inline void proc_status() { ifstream t( /proc/self/status ); cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl; } template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline T read() { register T sum(0), fg(1); register char ch(getchar()); for (; !isdigit(ch); ch = getchar()) if (ch == - ) fg = -1; for (; isdigit(ch); ch = getchar()) sum = sum * 10 - 0 + ch; return sum * fg; } const int MOD = (int)1e9 + 7; const int MAXN = (int)1e6; int n; char s0[MAXN + 5]; inline void input() { scanf( %s , s0 + 1), n = strlen(s0 + 1); } namespace PAM { const int MAX_NODE = MAXN * 2, alpha = 26; int fail[MAX_NODE + 5], trans[MAX_NODE + 5][alpha], nxt[MAX_NODE + 5], cnt; int len[MAX_NODE + 5], delta[MAX_NODE + 5]; int rt0, rt1, lst; int S[MAX_NODE + 5], N = 0; inline void init() { S[0] = -1; cnt = 1, lst = 0; memset(trans[0], 0, sizeof trans[0]); memset(trans[1], 0, sizeof trans[1]); len[0] = 0, len[1] = -1; fail[0] = 1; } inline int new_node(int l) { len[++cnt] = l; fail[cnt] = 0; memset(trans[cnt], 0, sizeof trans[cnt]); return cnt; } inline int extend(int ch) { int p = lst; S[++N] = ch; while (S[N - len[p] - 1] != S[N]) p = fail[p]; int np = -1; if (!trans[p][ch]) { np = new_node(len[p] + 2); int t = fail[p]; while (S[N - len[t] - 1] != S[N]) t = fail[t]; fail[np] = trans[t][ch]; delta[np] = len[np] - len[fail[np]]; nxt[np] = delta[np] == delta[fail[np]] ? nxt[fail[np]] : fail[np]; trans[p][ch] = np; } else np = trans[p][ch]; return lst = np; } } // namespace PAM inline void solve() { static int s[MAXN + 5]; for (int i = 1; i <= n / 2; ++i) { s[i * 2 - 1] = s0[i] - a ; s[i * 2] = s0[n - i + 1] - a ; } static int nd[MAXN + 5]; PAM::init(); for (int i = 1; i <= n; ++i) nd[i] = PAM::extend(s[i]); static int f[MAXN + 5], g[MAXN + 5]; f[0] = 1; for (int i = 1; i <= n; ++i) for (int p = nd[i]; p; p = PAM::nxt[p]) { g[p] = f[i - PAM::len[PAM::nxt[p]] - PAM::delta[p]]; if (PAM::delta[p] == PAM::delta[PAM::fail[p]]) (g[p] += g[PAM::fail[p]]) %= MOD; if (i % 2 == 0) (f[i] += g[p]) %= MOD; } printf( %d n , (f[n] + MOD) % MOD); } int main() { input(); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const double eps = 1e-6; const int INF = 0x3f3f3f3f, N = 2000005, M = 2000005, MOD = 1e9 + 7; int sgn(double x) { return x < -eps ? -1 : (x < eps ? 0 : 1); } int Rand(int x) { return rand() * rand() % x + 1; } vector<int> G[N]; int dep[N]; double ans = 0; void dfs(int x, int _fa, int _dep) { dep[x] = _dep; ans += 1. / dep[x]; for (auto v : G[x]) if (v != _fa) { dfs(v, x, _dep + 1); } } int Solve() { int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; G[u].push_back(v), G[v].push_back(u); } dfs(1, 0, 1); cout << fixed << setprecision(12) << ans << endl; return 0; } void Pre() {} int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); Pre(); while (Solve()) ; return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:50000000 ) using namespace std; int tt[1000000], kix[1000000], num[1000000], degree[1000000], black[1000000], fix[1000000]; vector<int> q, g[1000000]; int pirveli, p, i, j, n, m, k, a, b; int par[100005][20]; long long ans[1000000]; void go(int x, int prev = -1) { fix[x] = 1; q.push_back(x); for (int i = 0; i < g[x].size(); i++) { int nxt = g[x][i]; if (nxt == prev) continue; if (fix[nxt] == 0) { go(nxt, x); continue; } if (num[nxt] != -1) continue; for (int j = q.size() - 1; j >= 0; j--) { num[q[j]] = nxt; if (q[j] == nxt) break; } } q.pop_back(); } void DFS(int v, int prev, int deg, int raod) { kix[v] = 1; degree[v] = deg; if (tt[v] != 1) raod++; black[v] = raod; par[v][0] = num[prev]; for (int j = 1; j <= 19; j++) if (par[v][j - 1] == -1) par[v][j] = -1; else par[v][j] = par[par[v][j - 1]][j - 1]; for (int i = 0; i < g[v].size(); i++) { int nxt = g[v][i]; if (nxt == prev) continue; DFS(nxt, v, deg + 1, raod); } } int LCA(int a, int b) { for (int i = 18; i >= 0; i--) { if (degree[a] - (1 << i) >= degree[b]) { a = par[a][i]; } } if (a == b) return a; for (int i = 18; i >= 0; i--) { if (par[a][i] != par[b][i]) { a = par[a][i]; b = par[b][i]; } } return par[a][0]; } int main() { cin >> n >> m; for (i = 0; i < m; i++) { cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } for (i = 0; i < n; i++) num[i] = -1; go(0); for (i = 0; i <= n; i++) for (j = 0; j <= 20; j++) par[i][j] = -1; for (i = 0; i < n; i++) { if (num[i] == -1 || num[i] == i) continue; for (j = 0; j < g[i].size(); j++) g[num[i]].push_back(g[i][j]); } for (i = 0; i < n; i++) if ((num[i] == -1 || num[i] == i)) { for (j = 0; j < g[i].size(); j++) { int nxt = g[i][j]; if (num[nxt] == i) { g[i][j] = g[i].back(); g[i].pop_back(); j--; } else if (num[nxt] == -1) continue; else g[i][j] = num[nxt]; } } if (num[0] == -1) pirveli = 0; else pirveli = num[0]; for (i = 0; i < n; i++) if (num[i] == -1) { num[i] = i; tt[i] = 1; } DFS(pirveli, -1, 0, 0); ans[0] = 1; for (i = 1; i <= n; i++) ans[i] = ans[i - 1] * 2 % 1000000007; cin >> k; while (k--) { cin >> a >> b; a = num[a - 1]; b = num[b - 1]; if (degree[a] < degree[b]) swap(a, b); p = LCA(a, b); p = num[p]; cout << ans[black[a] + black[b] - 2 * black[p] + (tt[p] == 0)] << endl; } } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long sum = 0, a; for (int i = 0; i < n; i++) { cin >> a; sum += (i * (a - 1) + a); } cout << sum; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long HASH = 1e16 + 7; const long long INF = 1e9; long long tavan(long long a, long long b) { return b ? (tavan(a * a, b >> 1) * (b & 1 ? a : 1)) : 1; } long long gcd(long long a, long long b) { return (a < b) ? gcd(b, a) : ((a % b == 0) ? b : gcd(b, a % b)); } bool isprime(long long x) { for (long long i = 2; i <= sqrt(x); i++) if (!(x % i)) return false; return true; } ifstream fin( input.txt ); ofstream fout( output.txt ); long long n, a[1100]; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int t = 0; t < n + 1000; t++) { bool ex = 1; for (int i = 0; i < n; i++) { if (i % 2 == 0) a[i]++; else a[i]--; if (a[i] == n) a[i] = 0; if (a[i] == -1) a[i] = n - 1; if (a[i] != i) ex = 0; } if (ex) { cout << YES ; return 0; } } cout << NO ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int Maxl = 26; const int Maxn = 30005; const int Maxstr = 200005; const int Inf = 2000000007; char str[Maxstr]; int t; int n, m, k; int freq[Maxl]; bool dp[Maxn]; int res; int main() { scanf( %d , &t); while (t--) { scanf( %d %d %d , &n, &m, &k); scanf( %s , str); fill(freq, freq + Maxl, 0); for (int i = 0; i < k; i++) freq[str[i] - A ]++; res = Inf; int mx = max(n, m); for (int i = 0; i < Maxl; i++) { int tot = 0; for (int j = 0; j < Maxl; j++) if (j != i) tot += freq[j]; int mn = tot; fill(dp, dp + mx + 1, false); dp[0] = true; for (int j = 0; j < Maxl; j++) if (j != i) for (int l = mx; l >= 0; l--) if (dp[l]) if (l + freq[j] > mx) mn = min(mn, l + freq[j]); else dp[l + freq[j]] = true; for (int l = 0; l <= mx; l++) if (dp[l]) { int a = l, b = tot - l; int mya = max(0, n - a), myb = max(0, m - b); if (mya + myb <= freq[i]) res = min(res, mya * myb); mya = max(0, n - b), myb = max(0, m - a); if (mya + myb <= freq[i]) res = min(res, mya * myb); } int a = mn, b = tot - mn; int mya = max(0, n - a), myb = max(0, m - b); if (mya + myb <= freq[i]) res = min(res, mya * myb); mya = max(0, n - b), myb = max(0, m - a); if (mya + myb <= freq[i]) res = min(res, mya * myb); } printf( %d n , res); } return 0; } |
#include <bits/stdc++.h> using namespace std; namespace { void Solve(int) { const set<string> bins = { AND , OR , XOR }; int n; cin >> n; vector<string> type(n); vector<int> lef(n, -1), rig(n, -1); for (int i = 0, __i = n; i < __i; ++i) { cin >> type[i] >> lef[i]; if (type[i] != IN ) --lef[i]; if (bins.count(type[i])) { cin >> rig[i]; --rig[i]; } } auto eval = [](const string& t, bool a, bool b) { if (t == AND ) return a and b; else if (t == OR ) return a or b; else return (bool)(a ^ b); }; vector<bool> val(n), flip(n); function<void(int)> dfs1 = [&](int at) { if (type[at] == IN ) { val[at] = lef[at]; } else { dfs1(lef[at]); if (bins.count(type[at])) { dfs1(rig[at]); val[at] = eval(type[at], val[lef[at]], val[rig[at]]); } else { val[at] = !val[lef[at]]; } } }; dfs1(0); function<void(int)> dfs2 = [&](int at) { if (type[at] == IN ) flip[at] = true; else if (type[at] == NOT ) { flip[at] = true; dfs2(lef[at]); } else { if (val[at] != eval(type[at], val[lef[at]] ^ 1, val[rig[at]])) { flip[at] = true; dfs2(lef[at]); } if (val[at] != eval(type[at], val[lef[at]], val[rig[at]] ^ 1)) { flip[at] = true; dfs2(rig[at]); } } }; dfs2(0); for (int i = 0, __i = n; i < __i; ++i) if (type[i] == IN ) cout << (val[0] ^ flip[i]); cout << n ; } void Init() { ios::sync_with_stdio(false); cin.tie(nullptr); } } // namespace int32_t main() { Init(); int tests = 1; for (int test = 1, __test = tests + 1; test < __test; ++test) Solve(test); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100000 + 1; const int INF = numeric_limits<int>::max() / 2; pair<int, int> P[MAX_N]; pair<int, int> prefix[MAX_N], suffix[MAX_N]; int N; long long square(int first) { return 1LL * first * first; } bool valid(long long M) { long long bestY, bestXY; int i = 1, j = 1; while (i <= N) { while (square(P[i].first - P[j].first) > M) j++; assert(j <= i); assert(square(P[i].first - P[j].first) <= M); int maxY = -INF; int minY = INF; bool changed = false; if (j > 1) { changed = true; minY = min(minY, prefix[j - 1].first); maxY = max(maxY, prefix[j - 1].second); } if (i < N) { changed = true; minY = min(minY, suffix[i + 1].first); maxY = max(maxY, suffix[i + 1].second); } if (changed) { bestY = square(maxY - minY); bestXY = max(square(maxY), square(minY)) + max(square(P[i].first), square(P[j].first)); } else bestY = 0, bestXY = 0; if (bestY <= M && bestXY <= M) return true; i++; } return false; } void makePrefixSuffix() { pair<int, int> p = {INF, -INF}; for (int i = 1; i <= N; ++i) { p.first = min(p.first, P[i].second); p.second = max(p.second, P[i].second); prefix[i] = p; } p = {INF, -INF}; for (int i = N; i >= 1; i--) { p.first = min(p.first, P[i].second); p.second = max(p.second, P[i].second); suffix[i] = p; } } long long binarySearch() { long long l = 0; long long r = 1LL * INF * INF >> 1; long long sol = -1; while (l <= r) { long long m = (l + r) / 2; if (valid(m)) { sol = m; r = m - 1; } else l = m + 1; } return sol; } int main() { ios_base::sync_with_stdio(false); cin >> N; for (int i = 1; i <= N; ++i) cin >> P[i].first >> P[i].second; sort(P + 1, P + N + 1); makePrefixSuffix(); long long sol1 = binarySearch(); for (int i = 1; i <= N; ++i) swap(P[i].first, P[i].second); sort(P + 1, P + N + 1); makePrefixSuffix(); long long sol2 = binarySearch(); cout << min(sol1, sol2) << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> a[100010]; int y[100010], res[100010]; int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) { int num; scanf( %d%d , &num, &y[i]); res[i] = 0; for (int k = 1; k * k <= num; k++) if (num % k == 0) { a[k].push_back(i); res[i]++; if (k * k != num) { a[num / k].push_back(i); res[i]++; } } } for (int i = 1; i <= 100000; i++) for (int j = 1; j < a[i].size(); j++) if (a[i][j] - a[i][j - 1] <= y[a[i][j]]) res[a[i][j]]--; for (int i = 0; i < n; i++) printf( %d n , res[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; struct P { long long first, second, sum; P(long long first = 0, long long second = 0, long long sum = 0) : first(first), second(second), sum(sum) {} bool operator<(const P &b) const { return first < b.first; } }; const long long MAX_V = 500100; const long long MAX_LOG_V = 25; vector<long long> G[MAX_V]; long long root, V; long long parent[MAX_LOG_V][MAX_V]; long long depth[MAX_V], ans[MAX_V]; struct vertex { long long v, di; vertex(long long v, long long di = 0) : v(v), di(di) {} bool operator<(const vertex &b) const { return di < b.di; } }; vector<vertex> depVec[MAX_V]; long long dfs_index = 0; void dfs(long long v, long long p, long long d) { parent[0][v] = p; depth[v] = d; depVec[d].push_back(vertex(v, ++dfs_index)); for (long long i = 0; i < G[v].size(); i++) if (G[v][i] != p) dfs(G[v][i], v, d + 1); } void init() { dfs(root, -1, 1); for (long long k = 0; k + 1 < MAX_LOG_V; k++) for (long long v = 1; v <= V; v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } long long lca(long long u, long long v) { if (depth[u] > depth[v]) swap(u, v); for (long long k = 0; k < MAX_LOG_V; k++) { if ((depth[v] - depth[u]) >> k & 1) v = parent[k][v]; if (depth[v] == depth[u]) break; } if (u == v) return u; for (long long k = MAX_LOG_V - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } void solve(const vector<vertex> &vec) { vector<P> stk(vec.size() + 1, P(0)); long long sn = 0; long long d = depth[lca(vec[0].v, vec[1].v)]; stk[sn++] = P(d, 1, d); ans[vec[1].v] += stk[0].sum; for (long long i = 2; i < vec.size(); i++) { long long d = depth[lca(vec[i - 1].v, vec[i].v)]; if (d < 0) { cout << d = << d << endl; } long long x = lower_bound(stk.begin(), stk.begin() + sn, P(d, -1)) - stk.begin(); if (x == sn) { stk[sn] = P(d, stk[sn - 1].second + 1, stk[sn - 1].sum + d); sn++; } else { long long cnt = stk[sn - 1].second - (x > 0 ? stk[x - 1].second : 0) + 1; if (cnt < 0) { cout << stk[sn-1].second = << stk[sn - 1].second << endl; cout << x = << x << endl; cout << sn = << sn << endl; cout << ~ = << (x > 0 ? stk[x - 1].second : 0) << endl; } stk[x] = P(d, stk[sn - 1].second + 1, (x > 0 ? stk[x - 1].sum : 0) + d * cnt); sn = x + 1; } ans[vec[i].v] += stk[sn - 1].sum; if (stk[sn - 1].sum < 0) { cout << vec[i].v = << vec[i].v << endl; for (long long j = 0; j < sn; j++) cout << stk[ << j << ] = << stk[sn - 1].first << << stk[sn - 1].second << << stk[sn - 1].sum << endl; exit(0); } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> V; for (long long i = 1; i <= V; i++) { long long p; cin >> p; if (p == 0) root = i; else G[p].push_back(i); } init(); for (long long i = 2; i <= V; i++) { if (depVec[i].empty()) break; if (depVec[i].size() > 1) { sort(depVec[i].begin(), depVec[i].end()); solve(depVec[i]); reverse(depVec[i].begin(), depVec[i].end()); solve(depVec[i]); } for (auto j : depVec[i]) { ans[j.v] += ans[parent[0][j.v]] + depth[j.v] - 1; } } for (long long i = 1; i <= V; i++) { cout << ans[i] << (i == V ? n : ); } } |
#include <bits/stdc++.h> using namespace std; const int Inf = 1e9 + 7; int dp[107][107]; int par[107][107]; string t = .,!? ; bool check(char c) { return t.find(c) == string::npos; } void solve() { int t; cin >> t; for (int k = 0; k < t; k++) { int n; cin >> n; vector<string> users; for (int i = 0; i < n; i++) { string p; cin >> p; users.push_back(p); } sort(users.begin(), users.end()); map<string, int> id; for (int i = 0; i < n; i++) id[users[i]] = i; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) dp[0][i] = 1; memset(par, -1, sizeof(par)); int m; cin >> m; string buf; getline(cin, buf); vector<string> message; for (int i = 0; i < m; i++) { string buf; getline(cin, buf); int pos = buf.find( : ); string user = buf.substr(0, pos); string msg = buf.substr(pos + 1); message.push_back(msg); vector<string> w; int p = 0, l = msg.size(); while (p < l) { while (p < l && !check(msg[p])) p++; int q = p; while (q < l && check(msg[q])) q++; string word = msg.substr(p, q - p); if (word != ) w.push_back(word); p = q; } int uid = id.count(user) == 0 ? -1 : id[user]; if (uid == -1) { for (int j = 0; j < n; j++) { if (find(w.begin(), w.end(), users[j]) != w.end()) continue; for (int jj = 0; jj < n; jj++) { if (dp[i][jj] == 1 && (i == 0 || j != jj)) { dp[i + 1][j] = 1; par[i + 1][j] = jj; } } } } else { for (int jj = 0; jj < n; jj++) { if (dp[i][jj] == 1 && (i == 0 || uid != jj)) { dp[i + 1][uid] = 1; par[i + 1][uid] = jj; } } } } vector<string> res; for (int i = 0; i < n; i++) if (dp[m][i] == 1) { int p = i, q = m; while (q > 0) { res.push_back(users[p]); p = par[q][p]; q--; } } if (res.empty()) { cout << Impossible << n ; } else { reverse(message.begin(), message.end()); for (int i = m - 1; i >= 0; --i) { ostringstream oss; oss << res[i]; oss << : ; oss << message[i]; cout << oss.str() << n ; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(9); cout << fixed; solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; long long f = 1; for (long long i = 0; i < 100000; i++) { n += (n % m); if (n % m == 0) { f = 0; break; } } if (!f) cout << Yes << endl; else cout << No << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long N = 2e5; const long long inf = 1e18; template <typename... T> void read(T&... args) { ((cin >> args), ...); } template <typename... T> void write(T&&... args) { ((cout << args << ), ...); } template <typename... T> void writeline(T&&... args) { ((cout << args << ), ...); cout << n ; } long long add(long long a, long long b) { long long res = (a + b) % mod; return (res < 0) ? res + mod : res; } long long mul(long long a, long long b) { long long res = (a * 1LL * b) % mod; return (res < 0) ? res + mod : res; } string binary(long long x) { string res = ; for (long long i = 0; i < 32; i++) res += (1 << i & x) ? 1 : 0 ; return res; } long long in(long long x, long long y, long long z) { if (y > z) swap(y, z); return (x >= y && x <= z); } long long valid(long long x, long long sum) { long long s = 0; while (x) { s += x % 10; x /= 10; } return s == sum; } void solve() { long long n; cin >> n; if (n == 1) { cout << -1; return; } writeline(n, n + 1, n * (n + 1)); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; inline long long read() { long long data = 0, w = 1; char ch = 0; while (ch != - && (ch < 0 || ch > 9 )) ch = getchar(); if (ch == - ) w = -1, ch = getchar(); while (ch >= 0 && ch <= 9 ) data = data * 10 + ch - 0 , ch = getchar(); return data * w; } long long n, c, p[1000005], a[1000005]; int main() { n = read(), c = read(); for (long long i = 1; i <= n; i++) p[i] = read(); long long sum = 0, ans = 0; for (long long i = 1; i <= n; i++) { long long s = read(); a[i] = s - p[i] + c * (n - i); sum += p[i]; } ans = sum; sort(a + 1, a + n + 1); for (long long i = 1; i <= n; i++) sum += a[i] - (i - 1) * c, ans = min(ans, sum); cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, used[100003], m[100003]; string s, ans = INFINITE ; void DFS(int v) { if (used[v] == 1) { ans = INFINITE ; return; } used[v] = 1; if (s[v] == < ) { if (v - m[v] > 0) DFS(v - m[v]); else { ans = FINITE ; return; } } else { if (v + m[v] <= n) DFS(v + m[v]); else { ans = FINITE ; return; } } } int main() { cin >> n >> s; s = + s; for (int k = 1; k <= n; k++) cin >> m[k]; DFS(1); cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll t; cin >> t; while (t--) { ll n, m, k; cin >> n >> m >> k; k--; k--; if (m > n * (n - 1) / 2 || m < n - 1) { cout << nO << n ; continue; } if (k > 1) { if (m > n - 2) { cout << yEs << n ; } else cout << nO << n ; } else if (k == 1) { if (m == n * (n - 1) / 2) { cout << yEs << n ; } else cout << nO << n ; } else if (k == 0) { if (n == 1) cout << yEs << n ; else cout << nO << n ; } else cout << nO << n ; } } |
#include <bits/stdc++.h> using namespace std; const int M = 10005; vector<int> g[M]; int main() { int n; scanf( %d , &n); for (int i = 0; i < n - 1; i++) { int a, b; scanf( %d %d , &a, &b); g[a].push_back(b); g[b].push_back(a); } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < g[i].size(); j++) { int v = g[i][j]; ans += ((int)g[v].size()) - 1; } } cout << ans / 2; return 0; } |
#include <bits/stdc++.h> using namespace std; long long fac[1000001]; int main() { long long n, ans = 0, c = 0, mx = 0, tmp, tmp2, lg; cin >> n; for (long long i = 2; i < n; i++) { tmp = n / i; tmp = tmp * (tmp + 1) / 2 - 1; ans += (tmp * 4); } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int N; char c[2000000]; int numWays() { if (N == 1) { if (c[0] == * ) return 1; else if (c[0] == 0 ) return 1; else if (c[0] == ? ) return 2; else return 0; } if (c[0] == 2 ) return 0; if (c[N - 1] == 2 ) return 0; for (int i = 0; i < N; i++) { if (c[i] == 0 ) { if (i > 0) { if (c[i - 1] == ? ) c[i - 1] = - ; else if (c[i - 1] == * ) return 0; } if (i + 1 < N) { if (c[i + 1] == ? ) c[i + 1] = - ; else if (c[i + 1] == * ) return 0; } } } for (int i = 0; i < N; i++) { int max = (i > 0) + ((i + 1) < N); if (c[i] == 0 + max) { if (i > 0) { if (c[i - 1] == ? ) c[i - 1] = * ; else if (c[i - 1] != * ) return 0; } if (i + 1 < N) { if (c[i + 1] == ? ) c[i + 1] = * ; else if (c[i + 1] != * ) return 0; } } } for (int i = 1; i + 1 < N; i++) { if (c[i] == 1 ) { int numq = (c[i - 1] == ? ) + (c[i + 1] == ? ); int numm = (c[i - 1] == * ) + (c[i + 1] == * ); int numf = 2 - numm - numq; if (numq == 0) { if (numf != 1) return 0; } else if (numq == 1) { if (numm == 0) { if (c[i - 1] == ? ) c[i - 1] = * ; else if (c[i + 1] == ? ) c[i + 1] = * ; else assert(false); } else if (numm == 1) { if (c[i - 1] == ? ) c[i - 1] = - ; else if (c[i + 1] == ? ) c[i + 1] = - ; else assert(false); } else assert(false); } } } for (int i = N - 2; i > 0; i--) { if (c[i] == 1 ) { int numq = (c[i - 1] == ? ) + (c[i + 1] == ? ); int numm = (c[i - 1] == * ) + (c[i + 1] == * ); int numf = 2 - numm - numq; if (numq == 0) { if (numf != 1) return 0; } else if (numq == 1) { if (numm == 0) { if (c[i - 1] == ? ) c[i - 1] = * ; else if (c[i + 1] == ? ) c[i + 1] = * ; else assert(false); } else if (numm == 1) { if (c[i - 1] == ? ) c[i - 1] = - ; else if (c[i + 1] == ? ) c[i + 1] = - ; else assert(false); } else assert(false); } } } int res = 1; for (int i = 0; i < N; i++) { if (c[i] == ? && (i == 0 || c[i - 1] != 1 )) { res *= 2; if (res >= MOD) res -= MOD; } } return res; } int main() { cin >> c; N = strlen(c); cout << numWays() << n ; } |
#include <bits/stdc++.h> using namespace std; constexpr int Mod = 998244353; int n, x, y; int gr[200005]; long long sol; int main() { cin >> n; sol = n; for (int i = 1; i < n; ++i) { cin >> x >> y; gr[x]++; gr[y]++; sol = 1LL * sol * gr[x] % Mod * gr[y] % Mod; } cout << sol << n ; return 0; } |