solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int N, K; long long B; bool flag[100000 + 5]; struct Point { int x, y; Point(int _x = 0, int _y = 0) : x(_x), y(_y) {} }; vector<struct Point> a; bool cmp(const struct Point &a, const struct Point &b) { return a.x > b.x || (a.x == b.x && a.y < b.y); } int main() { scanf("%d %d", &N, &K); cin >> B; int ans = N; for (int i = 0, aa; i < N; i++) { scanf("%d", &aa); if (i + 1 < N) a.push_back(Point(aa, i + 1)); } sort(a.begin(), a.end(), cmp); long long sum = 0; int tt = N; for (int i = 0; i < K; i++) { sum += a[i].x; tt = min(tt, a[i].y); } if (sum > B) { ans = tt; } sum -= a[K - 1].x; for (int i = K; i < a.size(); i++) { if (sum + a[i].x > B) ans = min(ans, a[i].y); } printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); ; int a, i, c = 1, k, max; cin >> a; int b[a], x[a]; for (i = 0; i < a; i++) { cin >> b[i]; } max = 1; for (i = 1; i < a; i++) { if (b[i] >= b[i - 1]) { c++; } else { if (max <= c) max = c; c = 1; } if (max <= c) max = c; } cout << max << endl; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { int n, x; cin >> n >> x; set<int> st; for (int i = 1; i <= 300; i++) { st.insert(i); } for (int i = 1; i <= n; i++) { int x; cin >> x; if (st.count(x)) st.erase(x); } while (x--) { st.erase(*st.begin()); } int y = *st.begin(); cout << y - 1 << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e5 + 5; const int mod = 1e9 + 7; int32_t main() { 4; ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { ll n, m; cin >> n >> m; long long a[m], b[m]; long long id, mx = 0; vector<pair<long long, long long>> v; for (long long i = 0; i < m; i++) { cin >> a[i] >> b[i]; v.push_back({a[i], b[i]}); } sort(v.begin(), v.end(), greater<pair<ll, ll>>()); sort(a, a + m); vector<ll> pre(m); pre[0] = a[m - 1]; for (long long i = 1; i < m; i++) { pre[i] = pre[i - 1] + a[m - i - 1]; } ll ans = 0; for (long long i = 0; i < m; i++) { auto idx = lower_bound(a, a + m, v[i].second) - a; ll sum = 0; if (idx == m) { sum = v[i].first + (n - 1) * v[i].second; } else { idx = (m - idx - 1); if (n <= idx + 1) { sum = pre[n - 1]; } else { sum = pre[idx]; if (v[i].first >= v[i].second) { sum += (n - (idx + 1)) * v[i].second; } else sum += (n - (idx + 2)) * v[i].second + v[i].first; } } 4; ans = max(sum, ans); } cout << ans << "\n"; continue; } }
6
#include <bits/stdc++.h> using namespace std; int n, m, h; int U[100001]; vector<int> V[100001]; int C[100001], L[100001], second[100001], R[100001], t = 0, top = 0; int SCC[100001], out[100001], nSCC = 1; void DFS(int v) { L[v] = C[v] = ++t; second[top++] = v; for (int e : V[v]) { if (!C[e]) DFS(e); if (!R[e]) L[v] = (L[v] < L[e] ? L[v] : L[e]); } if (C[v] == L[v]) { for (int i = second[--top]; i != v; i = second[--top]) R[i] = nSCC; R[v] = nSCC++; } return; } int main() { scanf("%d %d %d", &n, &m, &h); for (int i = 1; i <= n; i++) scanf("%d", &U[i]); for (int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); if ((U[a] + 1) % h == U[b]) V[a].push_back(b); if ((U[b] + 1) % h == U[a]) V[b].push_back(a); } for (int i = 1; i <= n; i++) if (!C[i]) DFS(i); for (int i = 1; i <= n; i++) { SCC[R[i]] += 1; for (int e : V[i]) if (R[e] != R[i]) out[R[i]] += 1; } int min = 100001 + 1, minP = -1; for (int i = 1; i < nSCC; i++) { if (out[i] == 0) if (SCC[i] < min) min = SCC[i], minP = i; } printf("%d\n", min); for (int i = 1; i <= n; i++) { if (R[i] == minP) printf("%d ", i); } return 0; }
5
#include <bits/stdc++.h> int main() { int n, i, x = 0; char a[4]; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%s", &a); if (a[1] == '+') { x++; } else if (a[1] == '-') { x--; } } printf("%d", x); return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int read(int &i) { i = 0; char c = 0; bool flag = false; for (; (c < '0' || c > '9') && c != '-'; c = getchar()) ; while (c == '-') { flag = true; c = getchar(); } for (; c >= '0' && c <= '9'; c = getchar()) i = 10 * i + c - '0'; if (flag) i = -i; return i; } inline int write(int n) { if (n < 0) { putchar('-'); write(-n); } else if (n < 10) putchar(n + '0'); else { write(n / 10); putchar(n % 10 + '0'); } } inline int writeln(int n) { write(n); putchar('\n'); } const int INF = 1e9 + 7; const long long INFLL = 1e18 + 7; const int maxN = 4e5 + 9; int N, T; string s; void enter() { cin >> N >> T; cin >> s; } void process() { int i = 0, q; while (s[i] != '.') i++; while (i < N && s[i] < '5') i++; if (i == N) { cout << s; return; } i--; while (T > 0) { if (s[i] != '.') s[i]++; else { i--; q = i; while (i >= 0 && s[i] == '9') s[i--] = '0'; if (i == -1) cout << "1"; else s[i]++; break; } if (s[i] < '5') { q = i; break; } else { q = i; i--; } T--; } for (int j = (0), _b = (q); j <= _b; j++) cout << s[j]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); enter(); process(); }
4
#include <bits/stdc++.h> using namespace std; long long n, k, a[55]; int main() { a[0] = a[1] = 1; for (int i = 2; i <= 50; i++) a[i] = a[i - 1] + a[i - 2]; cin >> n >> k; long long c1 = 1, c2 = 2; while (n > 0) { if (k > a[n - 1]) { cout << c2 << " " << c1 << " "; k -= a[n - 1], n -= 2, c2 += 2, c1 += 2; } else { cout << c1 << " "; n--, c1++, c2++; } } cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int N = 18; int n, cnt; int a[200005], f[200005]; pair<int, int> d[300005][2], now[200005]; int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); } bool merge(int x, int y) { int fx = getf(x), fy = getf(y); if (fx == fy) return false; f[fx] = fy, cnt++; return true; } void upd(int x, pair<int, int> t) { if (t.second == d[x][0].second) chkmax(d[x][0].first, t.first); else if (!chkmax(d[x][0], t)) chkmax(d[x][1], t); } int main() { n = readint(); long long ans = 0; for (int i = 1; i <= n; i++) a[i] = readint(), f[i] = i, ans -= a[i]; while (1) { for (int i = 0; i < (1 << N); i++) d[i][0] = d[i][1] = make_pair(0, 0); for (int i = 1; i <= n; i++) upd(a[i], make_pair(a[i], getf(i))); for (int i = 0; i < N; i++) for (int j = 0; j < (1 << N); j++) if (j & (1 << i)) upd(j, d[j ^ (1 << i)][0]), upd(j, d[j ^ (1 << i)][1]); for (int i = 1; i <= n; i++) now[i] = make_pair(0, 0); for (int i = 0; i <= n; i++) { int tmp = a[i] ^ ((1 << N) - 1); if (d[tmp][0].second == getf(i)) chkmax(now[f[i]], make_pair(d[tmp][1].first + a[i], d[tmp][1].second)); else chkmax(now[f[i]], make_pair(d[tmp][0].first + a[i], d[tmp][0].second)); } for (int i = 0; i <= n; i++) if (f[i] == i && merge(i, now[i].second)) ans += now[i].first; if (cnt == n) break; } printf("%lld\n", ans); return 0; }
13
#include <bits/stdc++.h> using namespace std; const int inf = 10000000; const int MOD = 1000000007; int n, m, k, c = 1, q, d; set<char> se; vector<char> v, u, w; int a[100005]; int ind[100005]; map<int, int> mp; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; ind[i] = i; mp[a[i]] = i; } auto it = mp.rbegin(); vector<pair<int, int> > v; int mx = n; for (auto x : mp) { v.push_back({x.first, x.second}); } reverse(v.begin(), v.end()); int w = 1, last = -1; for (int i = 0; i < n; i++) { while (w < v[i].second) { w++; cout << endl; } if (w == last) cout << " "; if (w >= v[i].second) { last = w; cout << v[i].first; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long n, ans = 0, sum = 0; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < 31; i++) { sum = 0; for (long long j = 0; j < n; j++) { long long val = (a[j] > i ? -INF : a[j]); sum += val; if (sum < 0) sum = 0; ans = max(ans, sum - i); } } cout << ans; }
6
#include <bits/stdc++.h> const int S = 1 << 21; char ibuf[S], *iS, *iT, obuf[S], *oS = obuf, *oT = oS + S - 1; struct Flusher_ { ~Flusher_() { (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()); } } flusher_; template <class T> inline void read(T &x) { x = 0; register char c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), (iS == iT ? EOF : *iS++)) : *iS++); register bool f = 0; while (!isdigit(c)) f ^= c == '-', c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), (iS == iT ? EOF : *iS++)) : *iS++); while (isdigit(c)) x = x * 10 + c - '0', c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), (iS == iT ? EOF : *iS++)) : *iS++); if (f) x = -x; } template <class T> inline void readchar(T &x) { x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), (iS == iT ? EOF : *iS++)) : *iS++); while (!isalpha(x)) x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, S, stdin), (iS == iT ? EOF : *iS++)) : *iS++); } template <class T> inline void print(T x) { if (x < 0) (*oS++ = ('-'), oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()) : void()), x = -x; if (x > 9) print(x / 10); (*oS++ = (x % 10 + '0'), oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()) : void()); } template <class T> inline void print(T x, char c) { print(x), (*oS++ = (c), oS == oT ? (fwrite(obuf, 1, oS - obuf, stdout), oS = obuf, void()) : void()); } const int N = 2e5 + 10, sqn = 1200; int n, m, k, ans, cnt, a[N], b[N], s[N]; struct sequence { int siz, ans; std::vector<int> vec, bln, tag; std::vector<std::array<short, (1 << 14)>> val; inline int left(int k) { return k * sqn; } inline int right(int k) { return std::min((int)vec.size() - 1, (k + 1) * sqn - 1); } void modify(int k, int x) { ::ans += ans, cnt -= (vec.back() ^ tag.back()) != 0; for (int i = bln[k] + 1; i <= bln.back(); i++) { ans -= val[i][tag[i]]; tag[i] ^= x; ans += val[i][tag[i]]; } if (bln.back()) for (int i = left(bln[k]); i <= right(bln[k]); i++) val[bln[k]][vec[i]]--; for (int i = k; i <= right(bln[k]); i++) { ans -= vec[i] == tag[bln[k]]; vec[i] ^= x; ans += vec[i] == tag[bln[k]]; } if (bln.back()) for (int i = left(bln[k]); i <= right(bln[k]); i++) val[bln[k]][vec[i]]++; ::ans -= ans, cnt += (vec.back() ^ tag.back()) != 0; } void init() { for (int i = 1; i < vec.size(); i++) vec[i] ^= vec[i - 1]; for (int i = 0; i < vec.size(); i++) ans += vec[i] == 0; for (int i = 0; i < vec.size(); i++) bln.push_back(i / sqn); tag.resize(bln.back() + 1); if (bln.back()) { val.resize(bln.back() + 1); for (int k = 0; k <= bln.back(); k++) { for (int i = left(k); i <= right(k); i++) val[k][vec[i]]++; } } ::ans += vec.size() - ans, cnt += (vec.back() ^ tag.back()) != 0; } } f[N]; void output() { print(cnt ? -1 : ans, '\n'); } int main() { read(n), read(k), read(m); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 1; i <= n; i++) read(b[i]); for (int i = 1; i <= n + 1; i++) s[i] = a[i - 1] ^ b[i - 1] ^ a[i] ^ b[i]; for (int i = 1; i <= n + 1; i++) f[(i - 1) % k].vec.push_back(s[i]); for (int i = 0; i < k; i++) f[i].init(); output(); for (int c, p, x, v, i = 1; i <= m; i++) { readchar(c), read(p), read(x); if (c == 'a') v = a[p] ^ x, a[p] = x; if (c == 'b') v = b[p] ^ x, b[p] = x; f[p % k].modify(p / k, v); f[(p - 1) % k].modify((p - 1) / k, v); output(); } }
12
#include <bits/stdc++.h> using namespace std; const int Maxn = 2e5 + 1000; int Init[Maxn], First[Maxn], Last[Maxn], Arr[Maxn], Depth[Maxn], cnt, n, m; bool Mark[Maxn]; pair<int, int> Num[5 * Maxn]; vector<int> V[Maxn]; void Dfs(int Vert) { Mark[Vert] = true; Arr[++cnt] = Vert; First[Vert] = cnt; for (int i = 0; i < ((int)(V[Vert]).size()); i++) { int Child = V[Vert][i]; if (!Mark[Child]) { Depth[Child] = Depth[Vert] + 1; Dfs(Child); } } Last[Vert] = cnt + 1; } void Update(int id, int first, int second, int l, int r, int depth, int val) { if (first == l and second == r) { if (depth & 1) { Num[id].first += val; Num[id].second -= val; } else { Num[id].first -= val; Num[id].second += val; } return; } int Mid = (first + second) / 2; if (l < Mid) Update(2 * id, first, Mid, l, min(r, Mid), depth, val); if (r > Mid) Update(2 * id + 1, Mid, second, max(l, Mid), r, depth, val); } int Get_Ans(int id, int first, int second, int Index, int depth) { if (second == first + 1) return (depth & 1 ? Num[id].first : Num[id].second); int Sum = (depth & 1 ? Num[id].first : Num[id].second); int Mid = (first + second) / 2; if (Index < Mid) return Sum + Get_Ans(2 * id, first, Mid, Index, depth); else return Sum + Get_Ans(2 * id + 1, Mid, second, Index, depth); } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> Init[i]; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; V[x].push_back(y); V[y].push_back(x); } Dfs(1); int sts, vert, val; while (m--) { cin >> sts >> vert; if (sts == 1) { cin >> val; Update(1, 1, n + 1, First[vert], Last[vert], Depth[vert], val); } else cout << Init[vert] + Get_Ans(1, 1, n + 1, First[vert], Depth[vert]) << endl; } }
6
#include <bits/stdc++.h> using namespace std; int x2, y2, n, d; int sg[603][603]; int dx[603], dy[603]; bool use[403]; int &SG(int first, int second) { return sg[first + 200][second + 200]; } int main() { scanf("%d%d%d%d", &x2, &y2, &n, &d); for (int i = (0); i < (int)(n); i++) scanf("%d%d", dx + i, dy + i); for (int first = d; first >= -d; first--) for (int second = d; second >= -d; second--) { memset(use, 0, sizeof(use)); for (int i = (0); i < (int)(n); i++) { int a = first + dx[i], b = second + dy[i]; if (a * a + b * b > d * d) ; else use[SG(a, b)] = 1; } for (int i = (0); i < (int)(403); i++) if (!use[i]) { SG(first, second) = i; break; } } int ans = SG(x2, y2); puts(ans ? "Anton" : "Dasha"); }
5
#include <bits/stdc++.h> using namespace std; int n, k, ar[1 << 20]; long long dp[5005][5005]; int C; int cnt[5050]; int c1, c2; int done; int main() { ios_base::sync_with_stdio(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> ar[i]; cnt[i % k]++; } sort(ar, ar + n); C = 1e9; for (int i = 0; i < k; i++) { if (cnt[i] < C) C = cnt[i]; } for (int i = 0; i < k; i++) if (cnt[i] == C) c1++; else c2++; for (int i = 0; i <= c1; i++) for (int j = 0; j <= c2; j++) dp[i][j] = 1e18; dp[0][0] = 0; for (int i = 0; i <= c1; i++) for (int j = 0; j <= c2; j++) { done = i * C + j * (C + 1); if (i < c1 && C > 0) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + abs(ar[done + C - 1] - ar[done])); if (j < c2) dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + abs(ar[done + C] - ar[done])); } cout << dp[c1][c2] << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, a; int main() { scanf("%d%d", &n, &a); if (a % 2 == 1) { cout << (a + 1) / 2 << endl; } else { cout << n / 2 + 1 - a / 2 << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int L, v, li, ri; cin >> L >> v >> li >> ri; cout << L / v - (ri / v - (li - 1) / v) << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long N = 2e5, MAX = 2e5 + 5; const long long MOD = 1e9 + 7, mod = 1e9 + 7; long long dp[N] = {-1}; long long a[N], n; long long solv(long long i) { if (dp[i] != -1) return dp[i]; long long &ans = dp[i]; ans = 0; for (long long j = i % a[i]; j < n; j += a[i]) { if (a[j] > a[i]) ans = ans | !(solv(j)); } return ans; } void solve() { long long i, j, k, m, ans = MOD; cin >> n; for (i = 0; i < n + 3; i++) dp[i] = -1; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) cout << (solv(i) ? "A" : "B"); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long T = 1; while (T--) solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<long long> d(n); for (int i = 0; i < n; i++) { cin >> d[i]; } auto sum = a; partial_sum(a.begin(), a.end(), sum.begin()); auto get_sum = [&](int i, int j) { return sum[j] - (i ? sum[i - 1] : 0); }; long long ans = 0; if (k == 0) { for (int i = 0; i < n; i++) { ans = max(ans, get_sum(i, n - 1) - d[i]); } } else if (k == 1) { ans = max(ans, get_sum(0, n - 2) - *min_element(d.begin(), d.begin() + n - 1)); auto dd = d; sort(dd.begin(), dd.end()); ans = max(ans, get_sum(0, n - 1) - dd[0] - dd[1]); ans = max(ans, get_sum(0, n - 1) - a[1] - d[0]); ans = max(ans, get_sum(0, n - 1) - *min_element(a.begin() + 1, a.begin() + n) - d[0]); for (int i = 1; i < n; i++) { ans = max(ans, get_sum(i, n - 1) - d[i]); } } else { ans = max(ans, get_sum(0, n - 1) - *min_element(d.begin(), d.begin() + n - 1)); ans = max(ans, a[n - 1] - d[n - 1]); } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; void solve() { long long n, m, c = 0, i, tc = 0; cin >> n >> m; long long x = m % 10; long long m1 = 10 * m; if (n >= m1) { for (i = 1; i < 10; i++) { c += ((i * x) % 10); } tc = (n / m1) * c; n = n % m1; } for (i = 1; i * m <= n; i++) { tc += (i * x) % 10; } cout << tc << "\n"; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int main() { long long int a, b; cin >> a >> b; vector<pair<long long int, long long int> > vec, vec2; for (int i = -1000; i <= 1000; i++) { for (int j = -1000; j <= 1000; j++) { if (i && j && i * i + j * j == a * a) { vec.push_back(make_pair(i, j)); } } } for (int i = -1000; i <= 1000; i++) { for (int j = -1000; j <= 1000; j++) { if (i && j && i * i + j * j == b * b) { vec2.push_back(make_pair(i, j)); } } } for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < vec2.size(); j++) { if (vec[i].first * vec2[j].first + vec[i].second * vec2[j].second == 0 && vec[i].first != vec2[j].first && vec[i].second != vec2[j].second) { cout << "YES\n"; cout << 0 << " " << 0 << endl; cout << vec[i].first << " " << vec[i].second << endl; cout << vec2[j].first << " " << vec2[j].second << endl; return (0); } } } cout << "NO"; return (0); }
4
#include <bits/stdc++.h> using namespace std; int main() { int a, b, s, n, x; cin >> a >> b; n = a / b; s = a % b; x = n + s; int j = x; while (j != 0) { s = x % b; x = x / b; j = x; n += x; x += s; } cout << a + n; }
0
#include <bits/stdc++.h> using namespace std; int b[20][20], a[20], ans, n, q, N, st[20], c[262144], wz[1075637], top; long long dp[262144][20], da[20][262144], ta[262144], v[60000]; void FWT(long long *a, int s) { for (int h = 1; h < N; h <<= 1) for (int i = 0; i < N; i += h + h) for (int j = 0; j < h; ++j) a[h + i + j] += a[i + j] * s; } void dg(int w, int sm) { if (sm == n) { int no = 0; for (int i = 1; i <= w; ++i) no = (20ll * no + st[i]) % 1075637; wz[no] = ++top; for (int i = 0; i < N; ++i) ta[i] = 1; for (int p = 1; p <= w; ++p) for (int i = 0; i < N; ++i) ta[i] *= da[st[p]][i]; for (int i = 0; i < N; ++i) v[top] += ta[i] * (c[N - 1 - i] % 2 ? -1 : 1); return; } for (int i = st[w]; i <= n - sm; ++i) { if (sm + i < n && sm + i + i > n) continue; st[w + 1] = i; dg(w + 1, sm + i); } } void calc(int x, int s) { if (x == n) { int T = 0, no = 0; for (int i = 1, l = 0; i <= n; ++i) if (s & 1 << i - 1) ++l; else { if (1075637) st[++T] = l + 1; l = 0; } sort(st + 1, st + T + 1); for (int i = 1; i <= T; ++i) no = (20ll * no + st[i]) % 1075637; ta[N - 1 - s] = v[wz[no]]; return; } calc(x + 1, s); calc(x + 1, s | 1 << x - 1); } int main() { for (int i = 1; i < 262144; ++i) c[i] = c[i >> 1] + (i & 1); scanf("%d", &n); N = 1 << n; for (int i = 1; i <= n; ++i) { scanf("\n"); for (int j = 1; j <= n; ++j) b[i][j] = getchar() - 48; } for (int i = 1; i <= n; ++i) dp[1 << i - 1][i] = 1; for (int s = 1, S; s < 1 << n; ++s) for (int x = 1; x <= n; ++x) if (dp[s][x]) { for (int i = 1; i <= n; ++i) if (!(1 << i - 1 & s) && b[x][i]) S = s | 1 << i - 1, dp[S][i] = dp[S][i] + dp[s][x]; da[c[s]][s] += dp[s][x]; } for (int i = 1; i <= n; ++i) FWT(da[i], 1); st[0] = 1; dg(0, 0); for (int j = 0; j < N; ++j) ta[j] = 0; N >>= 1; calc(1, 0); FWT(ta, -1); for (int i = 0; i < 1 << n - 1; ++i) printf("%I64d ", ta[N - 1 - i]); }
12
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define inf 100000000000 #define mod 1000000007 ll power(ll x,ll n) { if(n==0) return 1; else if(n%2 == 0) //n is even return power((x*x),n/2); else //n is odd return (x*power((x*x),(n-1)/2)); } ll C(ll n, ll r) { if(r > n - r) r = n - r; // because C(n, r) == C(n, n - r) long long ans = 1; int i; for(i = 1; i <= r; i++) { ans =((ans) * (n - r + i)); ans =(ans)/i; } return ans; } void solve() { ll n,i,temp,arrxor=0,count=0; cin>>n; vector<ll> v1; for(i=0;i<n;i++) { cin>>temp; arrxor=arrxor^temp; v1.pb(temp); } if(arrxor==0) cout<<"YES"<<endl; else{ temp=0; for(i=0;i<n;i++) { temp=temp^v1[i]; if(temp==arrxor) { temp=0; count++; } } if(temp==0&&count>2) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--) solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int N = 1e5 + 5; const long long inf = 1e18; char s[N]; int i, j, aux[200]; int main() { scanf("%s", s); int len = strlen(s); int index = 123; for (i = 0; i < len; i++) aux[s[i]] = i; for (i = 122; i >= 97; i--) { for (j = aux[index]; j < len; j++) { if (s[j] == i) { printf("%c", char(i)); index = i; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; int computer[201], check[3][201]; vector<int> vv[201]; int bk[201]; int bfs(int st, int num) { queue<int> q; int ans = n; for (int i = 1; i <= n; i++) { if (check[num][i] == 0) q.push(i); } while (!q.empty()) { memset(bk, 0, sizeof(bk)); while (!q.empty()) { int head = q.front(); q.pop(); if (bk[head] == 5) { q.push(head); break; } bk[head]++; if (computer[head] != st) { q.push(head); continue; } for (int i = 0; i < vv[head].size(); i++) { int v = vv[head][i]; check[num][v]--; if (check[num][v] == 0) q.push(v); } } st = (st == 3 ? 1 : st + 1); if (!q.empty()) ans++; } return ans; } int main() { while (cin >> n) { memset(check, 0, sizeof(check)); for (int i = 1; i <= n; i++) vv[i].clear(); for (int i = 1; i <= n; i++) cin >> computer[i]; for (int i = 1; i <= n; i++) { int ki; cin >> ki; for (int j = 1; j <= ki; j++) { int aj; cin >> aj; vv[aj].push_back(i); check[0][i]++, check[1][i]++, check[2][i]++; } } int ans = 0x3f3f3f3f; for (int i = 1; i <= 3; i++) ans = min(ans, bfs(i, i - 1)); cout << ans << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = (int)5e5 + 7; const int inf = (int)1e9 + 7; int n; int a[303], f[303], cnt[303], sz; int c[303][303]; int d[303][303]; int ff[303]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); for (int j = 2; j * 1ll * j <= a[i] * 1ll; ++j) { while (a[i] % (j * j) == 0) { a[i] /= j * j; } } } for (int i = 0; i <= n; ++i) { c[i][0] = c[i][i] = 1; for (int j = 1; j < i; ++j) { c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % inf; } } for (int i = 1; i <= n; ++i) { if (!f[i]) { f[i] = ++sz; for (int j = i; j <= n; ++j) { if (a[i] == a[j]) { f[j] = f[i]; } } } ++cnt[f[i]]; } ff[0] = 1; for (int i = 1; i <= n; ++i) { ff[i] = ff[i - 1] * 1ll * i % inf; } d[1][0] = 1; int tot = 0; for (int i = 1; i <= sz; ++i) { for (int j = 0; j <= tot; ++j) { if (!d[i][j]) { continue; } for (int s = 1; s <= min(tot + 1, cnt[i]); ++s) { for (int e = 0; e <= j; ++e) { int nxt = (j - e) + (cnt[i] - s); int ways = d[i][j] * 1ll * ff[cnt[i]] % inf; ways = ways * 1ll * c[cnt[i] - 1][s - 1] % inf; ways = ways * 1ll * c[j][e] % inf; ways = ways * 1ll * c[tot + 1 - j][s - e] % inf; (d[i + 1][nxt] += ways) %= inf; } } } tot += cnt[i]; } printf("%d", d[sz + 1][0]); return 0; }
8
#include <bits/stdc++.h> using namespace std; bool uni[200010]; int sub[200010]; vector<int> adjList[200010]; int dfs(int node, int p = -1) { sub[node] = uni[node]; for (int nxt : adjList[node]) { if (nxt == p) continue; sub[node] += dfs(nxt, node); } return sub[node]; } int main(void) { int n, k; scanf("%d", &n); scanf("%d", &k); memset(uni, false, sizeof(uni)); for (int i = 0; i < 2 * k; i++) { int x; scanf("%d", &x); uni[x] = true; } for (int i = 0; i < n - 1; i++) { int fr, to; scanf("%d", &fr); scanf("%d", &to); adjList[fr].push_back(to); adjList[to].push_back(fr); } dfs(1); long long tot = 0; for (int i = 1; i <= n; i++) { tot += min(sub[i], 2 * k - sub[i]); } cout << tot; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int l = 1, r; char str[maxn]; int find(char ch) { for (int i = l; i <= r; i++) { if (str[i] == ch) { return i; } } return EOF; } int main() { gets(str + 1); r = strlen(str + 1); int point = find('.'); for (; str[l] == 48; l++) ; for (; (~point) && str[r] == 48; r--) ; if (str[r] == '.') { r--; } if (str[l] == '.') { int e = -1, pos = l + 1; for (; str[pos] == '0'; pos++, e--) ; putchar(str[pos]); if (pos != r) { putchar('.'); for (int i = pos + 1; i <= r; i++) putchar(str[i]); } printf("E%d", e); return 0; } int e = ((~point) ? point - 1 : r) - l; int _ = r; for (; _ > l && str[_] == 48; _--) ; putchar(str[l]); if (e && _ > l) { putchar('.'); } for (int i = l + 1; i <= _; i++) { if (str[i] != '.' || !e) { putchar(str[i]); } } if (e) { printf("E%d", e); } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int fast_expo(long long int x, long long int p) { if (p == 0) return 1; else if (p % 2 == 0) { long long int t = fast_expo(x, p / 2) % 998244353; return (t * t) % 998244353; } else return (x * (fast_expo(x, p - 1)) % 998244353) % 998244353; } int main() { int n; cin >> n; int ans = 0; vector<int> pre(n), a(n), suf(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { if (i && a[i] > a[i - 1]) pre[i] = pre[i - 1] + 1; else pre[i] = 1; ans = max(ans, pre[i]); } for (int i = n - 1; i >= 0; i--) { if (i + 1 < n && a[i] < a[i + 1]) suf[i] = suf[i + 1] + 1; else suf[i] = 1; ans = max(ans, suf[i]); } for (int i = 1; i < n - 1; i++) { if (a[i - 1] < a[i + 1]) ans = max(ans, pre[i - 1] + suf[i + 1]); } cout << ans << '\n'; }
3
#include <bits/stdc++.h> using namespace std; int N, M; vector<bool> arr[1000005]; vector<bool> brr[1000005]; pair<int, int> dq[4000005]; vector<int> dist[2][1000005]; void add(int n, bool b) { arr[n].push_back(b); brr[n].push_back(b); dist[0][n].push_back(INT_MAX >> 1); dist[1][n].push_back(INT_MAX >> 1); } void bfs(int t, int l, int r) { while (l <= r) { auto p = dq[l]; int i = p.first, j = p.second; l++; for (int a = -1; a <= 1; a++) { for (int b = -1; b <= 1; b++) { if (!t) { if (arr[i + a][j + b] && dist[t][i + a][j + b] > dist[t][i][j] + 1) { dist[t][i + a][j + b] = dist[t][i][j] + 1; dq[++r] = {i + a, j + b}; } } else { if (i + a >= 0 && i + a <= N + 1 && j + b >= 0 && j + b <= M + 1 && dist[t][i + a][j + b] > dist[t][i][j] + 1) { dist[t][i + a][j + b] = dist[t][i][j] + 1; dq[++r] = {i + a, j + b}; } } } } } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M; for (int j = 0; j <= M + 1; j++) { add(0, 0); add(N + 1, 0); } for (int i = 1; i <= N; i++) { add(i, 0); for (int j = 1; j <= M; j++) { char c; cin >> c; add(i, (c == 'X')); } add(i, 0); } int l = 1, r = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { if (arr[i][j]) { bool edge = 0; for (int a = -1; a <= 1; a++) { for (int b = -1; b <= 1; b++) { if (!arr[i + a][j + b]) { edge = 1; } } } if (edge) { dist[0][i][j] = 0; dq[++r] = {i, j}; } } } } bfs(0, l, r); int lft = 0, rht = N, ans = 0; while (lft <= rht) { int mid = lft + rht >> 1; l = 1, r = 0; for (int i = 0; i <= N + 1; i++) { for (int j = 0; j <= M + 1; j++) { dist[1][i][j] = INT_MAX >> 1; if (arr[i][j] && dist[0][i][j] >= mid) { dq[++r] = {i, j}; dist[1][i][j] = 0; } } } bfs(1, l, r); bool good = 1; for (int i = 0; i <= N + 1; i++) { for (int j = 0; j <= M + 1; j++) { if (!arr[i][j]) { if (dist[1][i][j] <= mid) { good = 0; break; } } else { if (dist[1][i][j] > mid) { good = 0; break; } } } } if (good) { lft = mid + 1; ans = mid; } else { rht = mid - 1; } } cout << ans << "\n"; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { if (arr[i][j] && dist[0][i][j] >= ans) { cout << 'X'; } else { cout << '.'; } } cout << "\n"; } }
7
#include <bits/stdc++.h> using namespace std; int main(void) { int a[8], i, g, j; string c[10]; string b, B, v; cin >> b; for (i = 0; i < 10; i++) cin >> c[i]; for (i = 0; i < 8; i++) { B.clear(); for (j = 0; j < 10; j++) B.push_back(b[i * 10 + j]); for (j = 0; j < 10; j++) if (B == c[j]) v.push_back(j + '0'); } cout << v; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int cnt1 = 0, cnt0 = 0; if (n % 2 != 0) { cout << 1 << endl; cout << s << endl; } else { for (int i = 0; i < n; i++) { if (s[i] == '0') { cnt0++; } else { cnt1++; } } if (cnt0 != cnt1) { cout << 1 << endl; cout << s; } else { cout << 2 << endl; for (int i = 0; i < n - 1; i++) { cout << s[i]; } cout << " " << s[n - 1]; } } }
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 3e5 + 5; const int INF = 1e9 + 7; struct note { int a, pos; } a[maxn], b[maxn]; bool cmp1(note t1, note t2) { return t1.a < t2.a; } bool cmp2(note t1, note t2) { return t1.a > t2.a; } int main() { int n; scanf("%d", &n); int x, y; int l1 = 0, l2 = 0; for (int q = 1; q <= n; q++) { scanf("%d%d", &x, &y); if (x > y) { a[l1++] = note{x, q}; } else { b[l2++] = note{x, q}; } } if (l1 > l2) { sort(a, a + l1, cmp1); printf("%d\n", l1); for (int q = 0; q < l1; q++) printf("%d ", a[q].pos); } else { sort(b, b + l2, cmp2); printf("%d\n", l2); for (int q = 0; q < l2; q++) printf("%d ", b[q].pos); } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long mod_exp(long long x, long long y, long long mm) { if (y == 0) return (1); else if (y % 2 == 0) return (mod_exp((x * x) % mm, y / 2, mm)); else return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm); } bool isPowerOfTwo(int n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } void solve() { int n; cin >> n; string s; cin >> s; int x = ((int)(s).size()); int cnt = 0; int flag = 0; for (int i = x - 1; i >= 0; i--) { if (s[i] == '0') break; else { cnt++; } } string ans; for (int i = 0; i < cnt; i++) { ans += '1'; } cnt = 0; for (int i = 0; i < x; i++) { if (s[i] == '1') break; else { cnt++; } } for (int i = 0; i < cnt; i++) { ans = '0' + ans; } for (int i = 0; i < x; i++) { if (s[i] == '1' && s[i + 1] == '0') { flag = 1; ans = '0' + ans; break; } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long a[100200]; pair<long long, long long> b[100200]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); reverse(a, a + n); int l = 0, r = 0; long long s = 0; while (r < n - 1) { if (a[r] > a[r + 1] + 1) r++; else { b[l].first = a[r]; b[l].second = a[r + 1]; r += 2; l++; } } for (int i = 0; i < l; i += 2) { s = s + (b[i].second * b[i + 1].second); } cout << s; return 0; }
4
#include <bits/stdc++.h> using namespace std; int f[4194304]; int n; int a[1000010]; int main() { int limit = (1 << 22) - 1; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); f[limit - a[i]] = a[i]; } for (int i = limit; i >= 0; i--) { if (!f[i]) continue; for (int j = 0; j <= 21; j++) { if (i & (1 << j)) { f[i ^ (1 << j)] = f[i]; } } } for (int i = 1; i <= n; i++) { if (f[a[i]]) printf("%d ", f[a[i]]); else printf("-1 "); } }
7
#include <bits/stdc++.h> using namespace std; int n, m, q, a, b, c, d, dp[50][50][50][50], A[50][50]; string s[50]; int main() { cin >> n >> m >> q; for (int i = 0; i <= n - 1; i++) cin >> s[i]; for (int i = 0; i <= n - 1; i++) for (int j = 0; j <= m - 1; j++) A[i + 1][j + 1] = (A[i + 1][j] + 1) * (s[i][j] == '0'); for (int w = 1; w <= m; w++) for (int h = 1; h <= n; h++) for (int i = 1; i <= n - h + 1; i++) for (int j = 1; j <= m - w + 1; j++) { dp[i][i + h - 1][j][j + w - 1] = dp[i][i + h - 1][j][j + w - 2] + dp[i][i + h - 2][j][j + w - 1] - dp[i][i + h - 2][j][j + w - 2]; for (int k = i + h - 1, C = w; k >= i; C = min(C, A[k][j + w - 1]), dp[i][i + h - 1][j][j + w - 1] += C, k--) ; } for (int i = 1; i <= q; i++) cin >> a >> b >> c >> d, cout << dp[a][c][b][d] << endl; }
5
#include <bits/stdc++.h> using namespace std; int n, k; string s; int cnt; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '#') { cnt++; } else { if (cnt >= k) { cout << "NO" << endl; return 0; } cnt = 0; } } if (cnt >= k) { cout << "NO" << endl; return 0; } else { cout << "YES" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; inline long long CinLL() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline int Cin() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f * x; } int n; int a[100005]; int line[100005]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = (1); i <= (n); i++) cin >> a[i]; for (int i = (1); i <= (n); i++) line[i] = a[i] + 1; for (int i = (2); i <= (n); i++) line[i] = max(line[i], line[i - 1]); for (int i = (n - 1); i >= (1); i--) line[i] = max(line[i + 1] - 1, line[i]); long long ans = 0LL; for (int i = (1); i <= (n); i++) { ans += (line[i] - 1 - a[i]); } cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { string S, S1, S2, S11, S21; getline(cin, S); int N = S.length(); int i, j, k; int t = 0, t3 = 0; for (i = 0; i < N; i++) { if (S[i] != S[N - i - 1] and t3 == 0) { t3 = 1; if (S[i + 1] == S[N - i - 1]) { t = 1; S1 = S; S1 += S1[N - 1]; for (j = N - 1; j > N - i - 1; j--) { S1[j + 1] = S1[j]; }; S1[N - i] = S1[i]; } if (S[i] == S[N - i - 2]) { t = 1; S2 = S; S2 += S2[N - 1]; for (j = N - 1; j > i - 1; j--) { S2[j + 1] = S2[j]; }; S2[i] = S2[N - i]; } if (t == 0) { t = 1; cout << "NA"; return 0; } } } S11 = S1 + '1'; S21 = S2 + '1'; if (t3 == 0) { S += S[N - 1]; for (j = N - 1; j >= (N - 1) / 2; j--) { S[j + 1] = S[j]; }; cout << S; return 0; }; int t1 = 0, t2 = 0, t5 = 0, t6 = 0; for (i = 0; i <= N; i++) { if (S1[i] != S1[N - i]) { t1 = 1; } } for (i = 0; i <= N; i++) { if (S2[i] != S2[N - i]) { t2 = 1; } } if (t1 == 0 and S11 != "1") { cout << S1; return 0; } if (t2 == 0 and S21 != "1") { cout << S2; return 0; } cout << "NA"; return 0; }
11
#include <bits/stdc++.h> using namespace std; long long prime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return i; } return n; } int main() { long long n, ok = 0; cin >> n; if (n % 2 != 0) { n -= prime(n); ok++; } cout << ok + n / 2 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; void rec(int n) { if (n == 1) return; int x = n; x--; while (n % x != 0) x--; cout << x << " "; return rec(x); } int main() { int n; cin >> n; cout << n << " "; rec(n); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, rsp, ans, taram; char first; int main() { cin >> n; cin >> first; if (first == 'U') { taram = 2; rsp++; } else taram = 1; for (int i = 2; i <= n; i++) { cin >> first; if (rsp * 2 == i - 1) { ans += (first == 'R' && taram == 2); ans += (first == 'U' && taram == 1); if ((first == 'R' && taram == 2) || (first == 'U' && taram == 1)) taram = taram % 2 + 1; } if (first == 'U') rsp++; } cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5, MAXS = 1e7, mod = 998244353, G = 3, invG = (mod + 1) / 3; int n; long long m; int cnte, h[MAXN], to[MAXN << 1], nx[MAXN << 1]; inline void adde(int u, int v) { cnte++; nx[cnte] = h[u]; to[cnte] = v; h[u] = cnte; } int fa[MAXN], siz[MAXN], son[MAXN]; void Dfs1(int u) { siz[u] = 1; for (int i = h[u]; i; i = nx[i]) { int v = to[i]; if (v == fa[u]) continue; fa[v] = u; Dfs1(v); siz[u] += siz[v]; if (siz[son[u]] < siz[v]) son[u] = v; } return; } int top[MAXN], seg[MAXN], rev[MAXN], ed[MAXN]; int buf[MAXN], g[MAXN]; int tmp[MAXS], *ans[MAXN << 2], *mul[MAXN << 2], *pt, len[MAXN << 2]; long long Fstpw(long long a, int b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; b >>= 1; a = a * a % mod; } return res; } void ntt(int *a, int n, int tp) { int bit = 0; while (1 << bit < n) bit++; static int rev[MAXN << 2]; for (int i = 1; i < n; i++) { rev[i] = rev[i >> 1] >> 1 | ((i & 1) << bit - 1); if (i < rev[i]) swap(a[i], a[rev[i]]); } for (int mid = 1; mid < n; mid <<= 1) { long long w = 1, w1 = Fstpw(tp == 1 ? G : invG, (mod - 1) / mid / 2); for (int j = 0; j < mid; j++, w = w * w1 % mod) for (int i = 0; i < n; i += mid * 2) { int x = a[i + j], y = w * a[i + j + mid] % mod; a[i + j] = (x + y) % mod; a[i + j + mid] = (x - y + mod) % mod; } } if (tp == -1) { long long t = Fstpw(n, mod - 2); for (int i = 0; i < n; i++) a[i] = a[i] * t % mod; } return; } int *NTT(int *a, int n, int *b, int m, int *res) { int siz = 1; while (siz <= n + m) siz <<= 1; static int f[MAXN << 2], g[MAXN << 2]; for (int i = 0; i < siz; i++) { f[i] = i <= n ? a[i] : 0; g[i] = i <= m ? b[i] : 0; } ntt(f, siz, 1); ntt(g, siz, 1); for (int i = 0; i < siz; i++) f[i] = 1ll * f[i] * g[i] % mod; ntt(f, siz, -1); int k = n + m; for (int i = 0; i <= k; i++) res[i] = f[i]; return res + k + 1; } void Dfs3(int k, int l, int r) { if (l == r) { int u = rev[l]; if (g[u]) { len[k] = siz[g[u]] + 1; ans[k] = pt; ans[k][0] = 0; ans[k][1] = 1; for (int i = 2; i <= len[k]; i++) ans[k][i] = buf[seg[g[u]] + i - 2]; pt += len[k] + 1; mul[k] = pt; memcpy(mul[k], ans[k], len[k] + 1 << 2); pt += len[k] + 1; } else { len[k] = 1; ans[k] = pt; ans[k][0] = 0; ans[k][1] = 1; pt += 2; mul[k] = pt; mul[k][0] = 0; mul[k][1] = 1; pt += 2; } return; } int mid = l + r >> 1; Dfs3(k << 1, l, mid); Dfs3(k << 1 | 1, mid + 1, r); len[k] = len[k << 1] + len[k << 1 | 1]; mul[k] = pt; pt = NTT(mul[k << 1], len[k << 1], mul[k << 1 | 1], len[k << 1 | 1], mul[k]); ans[k] = pt; pt = NTT(ans[k << 1 | 1], len[k << 1 | 1], mul[k << 1], len[k << 1], ans[k]); for (int i = 0; i <= len[k << 1]; i++) ans[k][i] = (ans[k][i] + ans[k << 1][i]) % mod; return; } void Dfs2(int u) { seg[u] = ++*seg; rev[*seg] = u; g[u] = 0; if (son[u]) { top[son[u]] = top[u]; Dfs2(son[u]); } else ed[top[u]] = u; for (int i = h[u]; i; i = nx[i]) { int v = to[i]; if (v == fa[u] || v == son[u]) continue; top[v] = v; Dfs2(v); g[u] = v; } if (top[u] == u) { pt = tmp; Dfs3(1, seg[u], seg[ed[u]]); memcpy(buf + seg[u], ans[1] + 1, siz[u] << 2); } return; } int main() { scanf("%d%lld", &n, &m); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); adde(u, v); adde(v, u); } Dfs1(1); top[1] = 1; Dfs2(1); int ans = 0; long long t = 1; for (int i = 1; i <= n; i++) { ans = (ans + t * buf[i]) % mod; t = (m + i) % mod * t % mod * Fstpw(i, mod - 2) % mod; } printf("%d\n", ans); return 0; }
13
#include <bits/stdc++.h> using namespace std; void solve() { const long long mxN = 1e5; int n, a, counter = 0; int ar[mxN] = {}; cin >> n; for (int i = 0; i < n; i++) { cin >> a; ar[i] = a; } for (int i = 0; i < n; i++) { if (ar[i] != ar[i + 1]) { counter++; } } cout << counter; } int main() { solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int rev(int n) { int res = 0; while (n) { res *= 10; res += n % 10; n /= 10; } return res; } bool prime(int n) { for (int i = 2; i * i <= n; ++i) { if (n % i == 0) return false; } return true; } int main() { stack<int> st; st.push(0); int carrots; cin >> carrots; int spread; int flour = 1; int egg = 1; while (carrots) { cin >> spread; st.push(spread); int t = st.top(); st.pop(); t *= flour; st.push(t); spread = st.top(); st.pop(); t = st.top(); st.pop(); t += spread; st.push(t); st.push(flour); t = st.top(); ++t; st.pop(); st.push(t); flour = st.top(); st.pop(); --carrots; } cout << st.top(); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; int n; int check[maxn]; int ver[maxn]; int f[maxn]; vector<int> e[maxn]; int ord; int in[maxn]; void dfs(int u) { check[u] = 1; for (int i = 0; i < e[u].size(); ++i) { int v = e[u][i]; if (!check[v]) dfs(v); } ver[ord--] = u; } int main() { scanf("%d\n", &n); ord = n; string s; getline(cin, s); int l; for (int i = 0; i < s.length(); ++i) { if (!(i > 0 && s[i - 1] == '=')) l = i + 1; else in[i + 1] = 1; if (s[i] == 'R') e[l].push_back(i + 2), in[i + 2] = 1; if (s[i] == 'L') e[i + 2].push_back(l), in[l] = 1; } for (int i = 1; i <= n; ++i) if (!in[i]) dfs(i); for (int i = 1; i <= n; ++i) if (ver[i]) { int u = ver[i]; if (!in[u]) f[u] = 1; for (int j = 0; j < e[u].size(); ++j) { int v = e[u][j]; f[v] = max(f[v], f[u] + 1); } } for (int i = 0; i < s.length(); ++i) { if (s[i] == '=') f[i + 2] = f[i + 1]; } for (int i = 1; i <= n; ++i) printf("%d ", f[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { string x = "qwertyuiopasdfghjkl;zxcvbnm,./", s; char a; cin >> a >> s; if (a == 'L') for (int i = 0; i < s.length(); i++) for (int j = 0; j < 30; j++) if (s[i] == x[j]) { s[i] = x[j + 1]; break; } if (a == 'R') for (int i = 0; i < s.length(); i++) for (int j = 0; j < 30; j++) if (s[i] == x[j]) { s[i] = x[j - 1]; break; } cout << s; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long t; string input; cin >> t; while (t--) { cin >> input; cout << input.size() - count(input.begin(), input.end(), '0') << endl; for (int i = input.size() - 1; i >= 0; i--) { if (input[i] != '0') { cout << input[i] + string(input.size() - i - 1, '0') + " "; } } cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAX_SZ = 12; const int MAX_VAL = 10; map<vector<int>, bool> seenDP; map<vector<int>, bool> winDP; bool cannotMove(vector<int> v) { int zero = 0; for (int out : v) zero += out == 0; if (zero >= 2) return true; vector<int> forced; for (int i = 1; i < v.size(); i++) { if (v[i - 1] == v[i]) forced.push_back(v[i]); } if (forced.size() > 1) return true; if (forced.size() == 1) { for (int i = 0; i < v.size(); i++) { if (v[i] == forced[0]) { v[i]--; break; } } sort(v.begin(), v.end()); for (int i = 1; i < v.size(); i++) { if (v[i] == v[i - 1]) return true; } return false; } for (int i = 0; i < v.size(); i++) { if (v[i] == 0) continue; if (i == 0 || v[i] - 1 != v[i - 1]) return false; } return true; } bool fast(vector<int> v) { sort(v.begin(), v.end()); if (cannotMove(v)) return false; int numOdd = 0; for (int out : v) numOdd += out % 2; if (v.size() % 4 == 0) return numOdd % 2 == 1; if (v.size() % 4 == 1) return numOdd % 2 == 1; if (v.size() % 4 == 2) return numOdd % 2 == 0; if (v.size() % 4 == 3) return numOdd % 2 == 0; assert(false); } bool win(vector<int> v) { sort(v.begin(), v.end()); bool allZero = v.back() == 0; if (allZero) return false; if (seenDP.count(v)) return winDP[v]; seenDP[v] = true; bool didWin = false; for (int i = 0; i < v.size(); i++) { vector<int> nv; nv = v; if (nv[i]) { nv[i]--; sort(nv.begin(), nv.end()); bool valid = true; for (int j = 1; j < nv.size(); j++) { if (nv[j - 1] == nv[j]) valid = false; } if (valid && !win(nv)) didWin = true; } } if (didWin) { winDP[v] = true; } return winDP[v]; } void dfs(vector<int> v) { if (v.size() == MAX_SZ) { win(v); return; } int init = 0; if (v.size()) init = v.back(); for (int a = init; a <= MAX_VAL; a++) { v.push_back(a); dfs(v); v.pop_back(); } } void solve() { vector<int> v; int n; cin >> n; v.resize(n); for (int i = 0; i < n; i++) cin >> v[i]; if (fast(v)) cout << "sjfnb\n"; else cout << "cslnb\n"; } void casesolve() { int t; cin >> t; for (int i = 1; i <= t; i++) { cout << "Case #" << i << " "; solve(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline void Swap(int& x, int& y) { x ^= y ^= x ^= y; } const int mod = 1e9 + 7, Inv2 = (mod + 1) / 2, Inv8 = (mod + 1) / 8, Inv24 = (mod + 1) / 24; int g[6][6]; inline int f1(int x) { int res = 0; if (x < 3) res = 0; else if (x & 1) res = ((3ll * x * x % mod * x + 2ll * x * x - 3ll * x - 18) % mod + mod) * Inv8 % mod; else res = ((3ll * x * x % mod * x + 2ll * x * x - 4ll * x - 16) % mod + mod) * Inv8 % mod; return res; } inline int fk(int x, int y) { int z = x + y, res = 0, w = (x + y) / 3; if (z % 3 == 1) { ++w; res = ((3ll * w * w % mod * w - 3ll * w * w + 2ll * w) % mod + mod) * Inv2 % mod; } if (z % 3 == 0) res = ((3ll * w * w % mod * w + 3ll * w * w + 2ll * w) % mod + mod) * Inv2 % mod; if (z % 3 == 2) res = ((3ll * w * w % mod * w + 9ll * w % mod * w + 10ll * w + 4) % mod + mod) * Inv2 % mod; return res; } inline int f2(int x, int y) { if (x > y) Swap(x, y); if (x < 3) return 0; if (y <= 5) return g[x][y]; int res = (f1(x / 2) + f1(y / 2)) % mod; res = (res - f2(x / 2, y / 2) + mod) % mod; res = (res + fk(x, y)) % mod; res = (res - fk(y / 2, y) + mod) % mod; res = (res - fk(x, x / 2) + mod) % mod; res = (res + fk(y / 2, x / 2)) % mod; return res; } inline int f3(int y) { int d = 0; if (~y & 1) { ++y; d = 1; } int res = ((2ll * y * y % mod * y - 9ll * y % mod * y - 26ll * y + 105) % mod + mod) * Inv24 % mod; if (d) { int q = (ll)((y - 2) / 4) * ((y + 1) / 2) % mod; q = (q + (ll)((y - 4) / 4) * (y / 2)) % mod; res = (res - q + mod) % mod; } return res; } inline int f4(int x, int y) { int res = f3(y), z = y - (x - 2) * 2; int q = f3(z), d = 0; if (~z & 1) { ++z; d = 1; } int q2 = (ll)(z / 2 - 1) * (z / 2 - 2) % mod; if (d) q2 = (q2 - (z - 5) / 2 + mod) % mod; q = (q + (ll)q2 * (x - 2)) % mod; res = (res - q + mod) % mod; return res; } inline int f(int x, int y) { if (x > y) Swap(x, y); if (x < 3) return 0; if (x * 2 <= y) return ((ll)f1(x) + f4(x, y) + f3(x)) % mod; else return ((ll)f2(x, y) + f3(y) + f3(x)) % mod; } int main() { g[3][3] = 4; g[3][4] = 7; g[3][5] = 9; g[4][4] = 12; g[4][5] = 17; g[5][5] = 26; int T = read(); while (T--) { int X = read(), Y = read(), N = read(), M = read(); int Ans = f(N, M); Ans = (Ans - f(X - 1, M) + mod) % mod; Ans = (Ans - f(N, Y - 1) + mod) % mod; Ans = (Ans + f(X - 1, Y - 1)) % mod; write(Ans); puts(""); } return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { long long int m, j = 0, n, g, b, i, k, c = 0, x; cin >> n; for (i = n; i < 1200; i++) { c = 0; m = i; while (m != 0) { c += m % 10; m = m / 10; } if (c % 4 == 0) { cout << i; break; } } } }
0
#include <bits/stdc++.h> using namespace std; bool mark[2][105]; char s[2][105][20]; int n, num[2][26]; int get(int x, int y) { int ret = -1; for (int i = 0; i < n; i++) if (!mark[1][i]) { int k = s[1][i][0] - 'A'; if (y == k || x == -1 && num[1][k] > num[0][k]) if (ret == -1 || strcmp(s[1][i], s[1][ret]) < 0) ret = i; } return ret; } int main() { scanf("%d", &n); for (int k = 0; k < 2; k++) for (int i = 0; i < n; i++) { scanf("%s", s[k][i]); num[k][s[k][i][0] - 'A']++; } for (int tt = 0; tt < n; tt++) { int i = -1; for (int j = 0; j < n; j++) if (!mark[0][j] && (i == -1 || strcmp(s[0][j], s[0][i]) < 0)) i = j; int k = s[0][i][0] - 'A'; printf("%s ", s[0][i]); int j; if (num[1][k] >= num[0][k]) j = get(0, k); else j = get(-1, k); printf("%s", s[1][j]); if (tt < n - 1) printf(", "); else printf("\n"); mark[1][j] = true; mark[0][i] = true; num[1][s[1][j][0] - 'A']--; num[0][k]--; } }
6
#include <bits/stdc++.h> const int M = 1000000000 + 7; int n, l, a[2][100]; int dp[30001][100][2], res = 0; int main() { scanf("%d %d", &n, &l); for (int i = 0; i < n; ++i) scanf("%d %d", &a[0][i], &a[1][i]); for (int i = 1; i <= l; ++i) for (int k = 0; k < n; ++k) for (int p = 0; p < (a[0][k] == a[1][k] ? 1 : 2); ++p) if (i == a[p][k]) dp[i][k][p] = 1; else if (i > a[p][k]) { dp[i][k][p] = 0; for (int j = 0; j < n; ++j) if (k != j) for (int q = 0; q < 2; ++q) if (a[(q + 1) % 2][j] == a[p][k]) dp[i][k][p] = (dp[i][k][p] + dp[i - a[p][k]][j][q]) % M; } for (int i = 0; i < n; ++i) res = (res + (dp[l][i][0] + dp[l][i][1]) % M) % M; printf("%d\n", res); return 0; }
5
#include <bits/stdc++.h> int main() { int a, b, c, d, min, e, f; long long int sum; scanf("%d%d%d%d", &a, &b, &c, &d); scanf("%d%d", &e, &f); if (f > e) { min = b; if (min > c) min = c; if (min > d) min = d; d = d - min; sum = min * f; if (a >= d) min = d; else min = a; sum = sum + min * e; } else { min = a; if (a > d) min = d; sum = min * e; min = d - min; if (min > b) min = b; if (min > c) min = c; sum = sum + min * f; } printf("%lld\n", sum); return 0; }
0
#include <bits/stdc++.h> using namespace std; struct Trie { int mark; Trie* son[53]; }; Trie* nul; Trie node[100000]; char s[10001], s1[10001]; int pn; Trie* root; int n; Trie* newnode() { Trie* temp; temp = &node[++pn]; temp->mark = 0; for (int i = 0; i < 53; i++) temp->son[i] = nul; return temp; } int f(char c) { if (c >= 'a' && c <= 'z') return c - 'a'; if (c >= 'A' && c <= 'Z') return c - 'A' + 26; if (c >= '0' && c <= '9') return 52; if (c == '+') return -1; if (c == '-') return -2; if (c == '*') return -3; if (c == '/') return -4; return -5; } int solve(int l, int r) { int z = 0, st, nt; int z2 = 0; int s2[10001]; int s3[10001]; Trie* u; for (int i = l; i <= r; i++) { if (s[i] == '(') { st = 1; nt = i; while (st > 0 || s[i] != ')') { i++; if (s[i] == '(') st++; if (s[i] == ')') st--; } s2[++z] = solve(nt + 1, i - 1); if (s2[z] == 1) return 1; else s2[z] = 0; } else { if (f(s[i]) < 0) s2[++z] = f(s[i]); else { if (root->son[f(s[i])] != nul) u = root->son[f(s[i])]; else u = root->son[f(s[i])] = newnode(); while (f(s[i + 1]) >= 0) { i++; if (u->son[f(s[i])] != nul) u = u->son[f(s[i])]; else u = u->son[f(s[i])] = newnode(); } if (u->mark == 1) return 1; s2[++z] = u->mark; } } } for (int i = 1; i <= z; i++) { if (s2[i] >= -2) s3[++z2] = s2[i]; else { if (s3[z2] == 3 || s2[i + 1] == 3) return 1; if (s2[i] == -3) { s3[z2] = 0; i++; } else { if (s2[i + 1] == 4) return 1; s3[z2] = 0; i++; } } } for (int i = 2; i <= z2; i += 2) { if (s3[i] == -2 && s3[i + 1] == 3) return 1; } int k; if (z == 1) k = s2[1]; else if (z2 == 1) k = 4; else k = 3; return k; } int work() { int z = 0; char c; while (scanf("%c", &c)) { if (c == '\n') break; if (c == ' ') continue; s[++z] = c; } s[z + 1] = '\0'; return solve(1, z); } int main() { nul = &node[0]; scanf("%d\n", &n); pn = 0; root = newnode(); Trie* u; char c; for (int i = 1; i <= n; i++) { while (scanf("%c", &c)) { if (c == 'e') break; } while (scanf("%c", &c)) { if (c == 'e') break; } while (scanf("%c", &c)) { if (c == ' ') continue; if (root->son[f(c)] == nul) u = root->son[f(c)] = newnode(); else u = root->son[f(c)]; break; } while (scanf("%c", &c)) { if (c == ' ') break; if (u->son[f(c)] == nul) u = u->son[f(c)] = newnode(); else u = u->son[f(c)]; } u->mark = work(); } if (work() != 1) puts("OK"); else puts("Suspicious"); return 0; }
9
#include <bits/stdc++.h> long long a, b, c, d, e, f, i = 4, r, z, x, y; int main() { std::cin >> a >> b >> c >> d >> e >> f; for (z = f * f + e * e; i--; x = c - a, y = d - (b = -b), r |= (!z && !x && !y) | (z && !((x * e + y * f) % z) && !((y * e - x * f) % z))) std::swap(a, b); puts(r ? "YES" : "NO"); }
6
#include <bits/stdc++.h> using namespace std; constexpr static int MAXN = 1e5 + 10; constexpr static int MAXVAL = 200; constexpr static int MOD = 998244353; int n; int a[MAXN]; int64_t cnt0[2][MAXVAL + 1], cnt1[2][MAXVAL + 1]; int64_t cnt0sum[2][MAXVAL + 1], cnt1sum[2][MAXVAL + 1]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int j = 1; j <= MAXVAL; j++) { if (a[0] == -1 || a[0] == j) cnt0[0][j] = 1; cnt0sum[0][j] = (cnt0sum[0][j - 1] + cnt0[0][j]) % MOD; } for (int i = 1; i < n; i++) { for (int j = 1; j <= MAXVAL; j++) { cnt0[i & 1][j] = 0; cnt1[i & 1][j] = 0; if (a[i] == -1 || a[i] == j) { cnt0[i & 1][j] += cnt0sum[(i - 1) & 1][j - 1] + cnt1sum[(i - 1) & 1][j - 1]; cnt0[i & 1][j] %= MOD; cnt1[i & 1][j] += cnt0[(i - 1) & 1][j]; cnt1[i & 1][j] += cnt1sum[(i - 1) & 1][MAXVAL] - cnt1sum[(i - 1) & 1][j - 1] + MOD; cnt1[i & 1][j] %= MOD; } cnt0sum[i & 1][j] = (cnt0sum[i & 1][j - 1] + cnt0[i & 1][j]) % MOD; cnt1sum[i & 1][j] = (cnt1sum[i & 1][j - 1] + cnt1[i & 1][j]) % MOD; } } int64_t total = 0; for (int j = 1; j <= MAXVAL; j++) total += cnt1[(n - 1) & 1][j]; total %= MOD; printf("%lld\n", total); }
5
#include <bits/stdc++.h> using namespace std; double eps = 1e-9; double pi = acos(-1); int fx[8] = {1, -1, 0, 0, 1, -1, -1, 1}; int fy[8] = {0, 0, 1, -1, 1, -1, 1, -1}; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define fast_io ios_base::sync_with_stdio(false); cin.tie(0); #define ll long long #define ull unsigned long long #define en "\n" #define ff first #define ss second #define sp(x) fixed << setprecision(x) #define VECT(v) vector<int>v #define SCAN(v) int temp; for(int i=0; i<n; i++) {cin>>temp; v.push_back(temp);} #define PRINT(v) for(int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << en; #define SORT(v) sort(v.begin(), v.end()); #define RSORT(v) sort(v.begin(), v.end(), greater<>()) #define CASEP(v) cout<<"Case "<<tc<<": "<<v<<"\n" #define DEBUG(v) cout << v << " "; cout << en; #define MIN(a, b) a < b ? a : b #define MAX(a, b) a > b ? a : b #define mem(a, b) memset(a, b, sizeof(a)) #define valid(nx, ny, row, col) nx >= 0 && nx < row && ny >= 0 && ny < col int main(void) { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif fast_io int t; cin >> t; while(t--) { int n, k; cin >> n >> k; if(n == 1) cout << k << en; else if(k == 1) cout << 1 << en; else { if(n % k == 0) { cout << 1 << en; } else if(n > 2 * k) { cout << 2 << en; } else { double a = MAX(n, k); double b = MIN(n, k); double tmp = ceil(a/b); cout << (int)tmp << en; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int module = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; vector<long long> p4(n, 1); for (int i = 1; i < n; ++i) p4[i] = p4[i - 1] * 4; cout << 4 * 2 * 3 * p4[n - 3] + 4 * (n - 3) * 3 * 3 * (n >= 4 ? p4[n - 4] : 0) << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T> int len(const T &c) { return (int)c.size(); } template <class T> void cmin(T &a, T b) { if (b < a) a = b; } template <class T> void cmax(T &a, T b) { if (b > a) a = b; } map<long long, long long> m; long long z, n, t1; int main() { cin >> n; for (int i(0), _n(n); i < _n; ++i) { cin >> t1; if (!t1) z++; else m[t1]++; } long long ans = 0; for (__typeof((m).begin()) i = (m).begin(); i != (m).end(); ++i) { ans += m[i->first] * (m[-i->first]); } ans /= 2; ans += z * (z - 1) / 2; cout << ans; }
2
#include <bits/stdc++.h> using namespace std; long long n, m, a[1000100], x, y, z, k, r, w, t; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; t = 0; k = 0; for (int i = 0; i < m; i++) { cin >> x; while (x > k + a[t]) { k += a[t]; t++; } cout << t + 1 << ' ' << x - k << endl; } return 0; }
1
#include <bits/stdc++.h> int top = 0; std::vector<int> ask[500005]; int x[500005], y[500005], z[500005], fa[500005], size[500005], tmp[500005], res[500005]; struct edge { int x, y, val; } e[500005]; struct state { int x, y, fx, fy; } st[500005]; inline int read() { register int x = 0, f = 1; register char s = getchar(); while (s > '9' || s < '0') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } inline void swap(int &x, int &y) { int tmp = y; y = x; x = tmp; } inline bool cmp1(const edge &x, const edge &y) { return x.val < y.val; } inline bool cmp2(const int &x, const int &y) { return z[x] < z[y]; } inline bool cmp3(const std::vector<int> &x, const std::vector<int> &y) { return z[x[1]] < z[y[1]]; } inline int find(int x) { while (x != fa[x]) x = fa[x]; return x; } inline void merge(int x, int y) { int fx = find(x), fy = find(y); if (fx != fy) { if (size[fx] > size[fy]) swap(fx, fy); st[++top] = (state){x, y, fx, fy}; size[fy] += size[fx]; fa[fx] = fy; } } int main() { int n = read(), m = read(), num = 0, cur = 1, Q = 0; for (register int i = 1; i <= n; ++i) { fa[i] = i; size[i] = 1; } for (register int i = 1; i <= m; ++i) { x[i] = read(); y[i] = read(); z[i] = read(); e[i].x = x[i]; e[i].y = y[i]; e[i].val = z[i]; } std::sort(e + 1, e + 1 + m, cmp1); Q = read(); for (register int t = 1; t <= Q; ++t) { int len = read(); res[t] = 1; for (register int i = 1; i <= len; ++i) tmp[i] = read(); std::sort(tmp + 1, tmp + 1 + len, cmp2); for (register int i = 1; i <= len; ++i) { if (z[tmp[i]] != z[tmp[i - 1]]) ask[++num].push_back(t); ask[num].push_back(tmp[i]); } } std::sort(ask + 1, ask + 1 + num, cmp3); for (register int i = 1; i <= num; ++i) { int flag = 1; while (cur <= m && e[cur].val < z[ask[i][1]]) { merge(e[cur].x, e[cur].y); ++cur; } for (register int j = 1; j < ask[i].size(); ++j) { int id = ask[i][j]; if (find(x[id]) == find(y[id])) { flag = 0; } else { merge(x[id], y[id]); } } for (register int j = ask[i].size() - 1; j >= 1; --j) { int id = ask[i][j]; while (top > 0 && st[top].x == x[id] && st[top].y == y[id]) { fa[st[top].fx] = st[top].fx; size[st[top].fy] -= size[st[top].fx]; --top; } } res[ask[i][0]] &= flag; } for (register int t = 1; t <= Q; ++t) printf("%s\n", res[t] ? "YES" : "NO"); return 0; }
7
#include <bits/stdc++.h> using namespace std; int v[100001]; vector<pair<int, int>> lefttt, rightttt; int main() { int n, d, b; int sumaLeft = 0; int sumaRight = 0; cin >> n >> d >> b; for (int i = 1; i <= n; ++i) { cin >> v[i]; } for (int i = 1; i <= n; ++i) { if (sumaLeft + v[i] >= ((n + 1) / 2) * b) { lefttt.push_back({i, ((n + 1) / 2) * b - sumaLeft}); v[i] -= (n / 2) * b - sumaLeft; break; } sumaLeft += v[i]; lefttt.push_back({i, v[i]}); v[i] = 0; } for (int i = n; i >= 1; --i) { if (sumaRight + v[i] >= (n / 2) * b) { rightttt.push_back({i, (n / 2) * b - sumaRight}); v[i] -= (n / 2) * b - sumaRight; break; } sumaRight += v[i]; rightttt.push_back({i, v[i]}); v[i] = 0; } int leftSize = lefttt.size(); int lefti = 0; int summ = 0; int cnt = 0; for (int i = 1; i <= (n + 1) / 2; ++i) { while (lefti < leftSize and summ < b) { summ += lefttt[lefti].second; lefti += 1; } if (abs(lefttt[lefti - 1].first - i) <= 1LL * i * d) { summ -= b; } else { cnt += 1; } } int rightSize = rightttt.size(); int righti = 0; int summr = 0; int cnt2 = 0; for (int i = n; i >= (n + 3) / 2; --i) { while (righti < rightSize and summr < b) { summr += rightttt[righti].second; righti += 1; } if (abs(i - rightttt[righti - 1].first) <= 1LL * (n - i + 1) * d) { summr -= b; } else { cnt2 += 1; } } cout << max(cnt, cnt2); return 0; }
7
#include <bits/stdc++.h> using namespace std; int cnt[10005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, a; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a; cnt[a]++; } long long ans = 0; for (int i = 0; i <= 1e4; i++) { for (int j = 0; j <= 1e4; j++) { if (__builtin_popcount(i ^ j) == k) ans += (1LL * cnt[i] * cnt[j]); } } if (k == 0) ans -= n; ans /= 2; cout << ans << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; int abs(int a, int b) { if (a < b) { return b - a; } else { return a - b; } } int main() { int t; int arr[100]; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; for (int j = 0; j < n; j++) { cin >> arr[j]; } int min_value = 2000; for (int j = 0; j < n; j++) { for (int k = j + 1; k < n; k++) { int cmin = abs(arr[j], arr[k]); if (cmin < min_value) { min_value = cmin; } } } cout << min_value << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1000010; int p[N], a[N], ll[N], lr[N], rr[N], f[N]; int ta, tp, n; char s[N]; void spe() { int t1 = 0, t2 = 0; for (int i = 1; i <= ta; ++i) if (a[i] < p[1]) ++t1; else ++t2; if (t1 > t2 || (t1 == t2 && p[1] - a[1] < a[ta] - p[1])) printf("%d %d\n", t1, p[1] - a[1]); else printf("%d %d\n", t2, a[ta] - p[1]); } bool check(int x) { int s1 = 0, s2 = 0, s3 = 0; for (int i = 1; i <= tp; ++i) { while (s1 < ta && a[s1 + 1] < p[i] - x) ++s1; ll[i] = s1; while (s2 < ta && a[s2 + 1] < p[i]) ++s2; lr[i] = s2; while (s3 < ta && a[s3 + 1] <= p[i] + x) ++s3; rr[i] = s3; } if (a[1] > p[1]) f[1] = rr[1]; else if (p[1] - a[1] > x) return 0; else f[1] = lr[1]; for (int i = 2; i <= tp; ++i) { f[i] = lr[i]; if (ll[i] > f[i - 1]) return 0; if (lr[i] <= f[i - 1]) f[i] = rr[i]; if (rr[i - 1] > f[i]) if (ll[i] <= f[i - 2]) f[i] = rr[i - 1]; } return f[tp] == ta; } int main() { scanf("%d%s", &n, s + 1); for (int i = 1; i <= n; ++i) if (s[i] == 'P') p[++tp] = i; else if (s[i] == '*') a[++ta] = i; if (tp == 1) { spe(); return 0; } int l = 1, r = n, ans; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } printf("%d %d\n", ta, ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { char c[] = {'a', 'b', 'c', 'd'}; int i = 0; while (n--) { cout << c[i++]; if (i > 3) i = 0; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; using mii = map<int, int>; using pii = pair<int, int>; using ll = int64_t; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; const pair<int, int> DD[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int mod = 1e9 + 7, mxN = 2e5 + 5, INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; template <typename... T> void print(T... args) { ((cout << args << " "), ...), cout << endl; } template <typename... T> void dbgout(string vars, T... args) { cout << endl; cout << "["; cout << vars << " = "; string delim = ""; (..., (cout << delim << args, delim = ", ")); cout << "]"; cout << endl; } template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& ot, pair<T1, T2>& p) { ot << p.first << ' ' << p.second; return ot; } template <typename T1, typename T2> bool cmax(T1& a, T2 b) { if (b > a) { a = b; return true; } return false; } template <typename T1, typename T2> bool cmin(T1& a, T2 b) { if (b < a) { a = b; return true; } return false; } template <typename T> istream& operator>>(istream& in, vector<T>& v) { for (T& x : v) in >> x; return in; } template <typename T> ostream& operator<<(ostream& ot, vector<T>& v) { for (T& x : v) ot << x << ' '; return ot; } template <typename T> ostream& operator<<(ostream& ot, set<T>& s) { for (T x : s) ot << x << ' '; return ot; } template <typename T1, typename T2> ostream& operator<<(ostream& ot, map<T1, T2>& M) { for (auto& [x, y] : M) ot << "(" << x << " : " << y << ")" << endl; return ot; } template <typename T> using pq = priority_queue<T, vector<T>, greater<T>>; ll mod_pow(ll x, ll y) { ll res = 1; x = x % mod; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } ll mod_inv(ll n) { ll ans = mod_pow(n, mod - 2); return ans; } ll mul(ll a, ll b) { a %= mod, b %= mod; return (a * b) % mod; } ll sub(ll a, ll b) { a %= mod, b %= mod; return (a - b + mod) % mod; } ll add(ll a, ll b) { a %= mod, b %= mod; return (a + b) % mod; } bool _prime(ll n) { if (n < 2) return 0; if (n < 4) return 1; if (n % 2 == 0 || n % 3 == 0) return 0; for (ll i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return 0; return 1; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename T, class first = function<T(const T&, const T&)>> class SparseTable { public: int n; vector<vector<T>> mat; first func; SparseTable(const vector<T>& a, const first& f) : func(f) { n = static_cast<int>(a.size()); int max_log = 32 - __builtin_clz(n); mat.resize(max_log); mat[0] = a; for (int j = 1; j < max_log; j++) { mat[j].resize(n - (1 << j) + 1); for (int i = 0; i <= n - (1 << j); i++) { mat[j][i] = func(mat[j - 1][i], mat[j - 1][i + (1 << (j - 1))]); } } } T get(int from, int to) const { assert(0 <= from && from <= to && to <= n - 1); int lg = 32 - __builtin_clz(to - from + 1) - 1; return func(mat[lg][from], mat[lg][to - (1 << lg) + 1]); } }; template <typename T> class fenwick { public: vector<T> fenw; int n; fenwick(int _n) : n(_n) { fenw.resize(n); } void modify(int x, T v) { while (x < n) { fenw[x] += v; x |= (x + 1); } } T get(int x) { T v{}; while (x >= 0) { v += fenw[x]; x = (x & (x + 1)) - 1; } return v; } }; void solve() { int n, q, L, R; cin >> n >> q; vi A(n), B(n); cin >> A >> B; vl dif(n + 1); for (int i = 0; i < (int)(n); i++) dif[i + 1] = dif[i] + B[i] - A[i]; SparseTable<ll> mn(dif, [](ll i, ll j) { return min(i, j); }); SparseTable<ll> mx(dif, [](ll i, ll j) { return max(i, j); }); while (q--) { cin >> L >> R; if (dif[R] - dif[L - 1] != 0) { print(-1); continue; } if (mn.get(L - 1, R) < dif[L - 1]) { print(-1); continue; } print(mx.get(L - 1, R) - dif[L - 1]); } } int main() { cout << fixed << setprecision(12); cin.tie(0)->sync_with_stdio(0), cin.exceptions(cin.failbit); solve(); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n), b(n); for (size_t i = 0; i < n; i++) cin >> a[i]; for (size_t i = 0; i < n; i++) cin >> b[i]; vector<long long> dp_a(n), dp_b(n); dp_a[n - 1] = a[n - 1]; dp_b[n - 1] = b[n - 1]; for (int i = n - 2; i >= 0; i--) { dp_a[i] = max(dp_a[i + 1], a[i] + dp_b[i + 1]); dp_b[i] = max(dp_b[i + 1], b[i] + dp_a[i + 1]); } cout << max(dp_a[0], dp_b[0]); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; const long long MOD = 1e9 + 7; const double PI = acos(-1.0); long long arr[MAX]; int fun(long long n) { int ans = 0; if (n == 0) return -1; while (1) { if (n & 1) return ans; else { ans++; n >>= 1; } } } int main() { int n; scanf("%d", &n); long long ma = 0; for (int i = 0; i < n; i++) { scanf("%lld", &arr[i]); ma = max(ma, arr[i]); } int v = 0; long long tmp = ma; while (tmp) { v++; tmp >>= 1; } int maxv = -1; int loc; for (int i = v - 1; i >= 0; i--) { loc = lower_bound(arr, arr + n, 1 << i) - arr; tmp = (1LL << 31) - 1; for (; loc < n; loc++) { if (arr[loc] & (1 << i)) tmp &= arr[loc]; } maxv = max(maxv, fun(tmp)); } if (maxv == -1) { bool first = true; for (int i = 0; i < n; i++) { if (first) { first = false; printf("%lld", arr[i]); } else printf(" %lld", arr[i]); } return 0; } int ans = 0; bool first = true; loc = lower_bound(arr, arr + n, 1 << maxv) - arr; for (int i = loc; i < n; i++) { if (arr[i] & (1 << maxv)) ans++; } printf("%d\n", ans); for (int i = loc; i < n; i++) { if (first) { first = false; if (arr[i] & (1 << maxv)) printf("%lld", arr[i]); } else if (arr[i] & (1 << maxv)) printf(" %lld", arr[i]); } printf("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; int euc(int a, int b) { if (a < b) { int t = a; a = b; b = t; } while (b != 0) { int r = a % b; a = b; b = r; } return a; } int main() { int a, b; scanf("%d %d", &a, &b); int nsd = euc(a, b); long long int lcm = ((long long int)a) * ((long long int)b) / nsd; int scoreA = (int)((long long int)(lcm / a)) - 1; int scoreB = (int)((long long int)(lcm / b)) - 1; if (a > b) scoreA++; else scoreB++; if (scoreA > scoreB) printf("Dasha"); else if (scoreA == scoreB) printf("Equal"); else { printf("Masha"); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; int solutions[40]; cin >> t; for (int i = 0; i < t; ++i) { int x; cin >> x; int num_bef; string a = std::to_string(x); int q = ((a[0] - '0') - 1) * 10; if (a.size() == 1) { q += 1; } if (a.size() == 2) { q += 3; } if (a.size() == 3) { q += 6; } if (a.size() == 4) { q += 10; } solutions[i] = q; } for (int j = 0; j < t; ++j) { cout << solutions[j] << endl; } }
0
#include <bits/stdc++.h> int main() { int n, i, t, s1 = 0, p = 1, s2 = 0, c = 0; scanf("%d", &n); while (n--) { scanf("%d", &t); int a[t]; for (i = 0; i < t; i++) { scanf("%d", &a[i]); s1 = s1 + a[i]; p = p * a[i]; if (a[i] == 0) { s2 = s2 + 1; c++; } else s2 = s2 + a[i]; } if (s1 != 0 && p != 0) printf("0\n"); else if (p != 0 && s1 == 0) printf("1\n"); else if (p == 0 && s1 == 0 || p == 0 && s1 != 0) { if (s2 == 0) printf("%d\n", c + 1); else printf("%d\n", c); } c = 0; p = 1; s1 = 0; s2 = 0; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; set<int> s; for (; n--;) { int x; cin >> x; s.insert(x); } vector<int> v(s.begin(), s.end()); if (v.size() < 2) return cout << "NO\n", 0; cout << v[1] << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 110; int a[N]; int main() { int n, d; scanf("%d %d", &n, &d); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); int m; scanf("%d", &m); int index = 0, resp = 0; while (m--) { if (index < n) resp += a[index++]; else resp -= d; } printf("%d\n", resp); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int x[4][4]; char c; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { cin >> c; x[i][j] = (c == '#' ? 1 : 0); } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int check = x[i][j] + x[i][j + 1] + x[i + 1][j] + x[i + 1][j + 1]; if (check == 4 || check == 0 || check % 2) { cout << "YES" << endl; return 0; } } } cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; long long a[505]; long long mn = 0x3f3f3f3f; long long ans; int check(int k) { long long ret = 0; for (int i = 1; i <= n; i++) { int c = a[i] % k; int d = a[i] / k; if (c != 0 && (c + d) < (k - 1)) { return 0; } ret += d + (c != 0); } ans = ret; return 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]), mn = min(mn, a[i]); for (int i = 1; i <= mn; i++) { int fl = check(mn / i + 1) || check(mn / i) || check(mn / i - 1); if (fl) break; } printf("%I64d", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; multimap<long long int, long long int> st; int main() { long long int n, m; cin >> n >> m; long long int arr[n]; set<long long int> s1, s2, res1, res2; for (long long int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] % 2 == 0) s1.insert(arr[i]); else s2.insert(arr[i]); } long long int l = n / 2 - s1.size(), r = n / 2 - s2.size(); for (long long int i = 1; i <= min(m, (long long int)200005); i++) if (i % 2 == 0 && s1.find(i) == s1.end()) res1.insert(i); else if (i % 2 != 0 && s2.find(i) == s2.end()) res2.insert(i); if (res1.size() < l || res2.size() < r) { cout << -1; exit(0); } cout << max(l, (long long int)0) + max(r, (long long int)0) << endl; for (long long int i = 0; i < n; i++) if (s1.find(arr[i]) != s1.end()) { s1.erase(arr[i]); if (l < 0) { arr[i] = *res2.begin(); res2.erase(res2.begin()); r--; l++; } } else if (s2.find(arr[i]) != s2.end()) { s2.erase(arr[i]); if (r < 0) { arr[i] = *res1.begin(); res1.erase(res1.begin()); l--; r++; } } else if (l > 0) { arr[i] = *res1.begin(); res1.erase(res1.begin()); l--; } else if (r > 0) { arr[i] = *res2.begin(); res2.erase(res2.begin()); r--; } for (long long int i = 0; i < n; i++) cout << arr[i] << " "; }
5
#include <bits/stdc++.h> using namespace std; struct st { bool operator()(string a, string b) { return a + b < b + a; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); ; int n; string s; multiset<string, st> S; cin >> n; for (int i = 0; i < n; i++) { cin >> s; S.insert(s); } for (auto x : S) cout << x; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int n, dp[4005][4005], t[4005][4005]; int main() { scanf("%d", &n); t[0][0] = 1; for (int i = 1; i <= n; i++) { t[i][0] = 1; for (int j = 1; j <= n; j++) t[i][j] = (t[i - 1][j - 1] + t[i - 1][j]) % MOD; } dp[1][1] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j <= i; j++) { dp[i][j] = 1LL * j * dp[i - 1][j] % MOD; if (j > 0) dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % MOD; } } int sol = 1; for (int i = 1; i < n; i++) { int ways = 0; for (int j = 1; j <= i; j++) ways = (ways + dp[i][j]) % MOD; int v = 1LL * t[n][n - i] * ways % MOD; sol = (sol + v) % MOD; } printf("%d\n", sol); return 0; }
5
#include <bits/stdc++.h> using namespace std; int x, y, n, m, a[100000], k; vector<long long> v; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> x >> y; if (v.size() != 0) { v.push_back(x * y + v[v.size() - 1]); } else { v.push_back(x * y); } } for (int i = 1; i <= m; i++) { cin >> x; while (x > v[k]) { k++; } cout << k + 1 << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long t, ans; for (cin >> t; t; t--) { string str; cin >> str; long long po = 0; long long num1 = 0, num2 = 0, len = str.length(); long long ans = len; for (int i = (0); i <= (len - 1); i++) { if (str[i] == 'A') num1++; if (str[i] == 'B') { if (num1) { num1--; ans -= 2; } else if (num2) { num2--; ans -= 2; } else { num2++; } } } cout << ans << endl; } }
1
#include <bits/stdc++.h> using namespace std; const int N = 305; namespace geometry { struct Vector { long long x, y; Vector(long long x = 0, long long y = 0) : x(x), y(y) {} bool operator<(const Vector &tmp) const { return x != tmp.x ? x < tmp.x : y < tmp.y; } bool operator==(const Vector &tmp) const { return x == tmp.x && y == tmp.y; } Vector operator-(const Vector &tmp) const { return Vector(x - tmp.x, y - tmp.y); } Vector operator*(const long long &k) const { return Vector(x * k, y * k); } }; long long dot(const Vector &A, const Vector &B) { return A.x * B.x + A.y * B.y; } long long length2(const Vector &A) { return dot(A, A); } } // namespace geometry using namespace geometry; int n, m, p[10]; int v[1005], tot; Vector a[10], b[1005]; vector<int> c[10][1005], g; bool dfs(int goal) { if (tot == m) return 0; for (auto x : c[p[tot]][goal]) if (!v[x]) { ++tot; if (!dfs(x)) return 0; } return g.push_back(goal), v[goal] = 1; } bool check(int x) { for (int i = 0; i < m; ++i) p[i] = i; do { g.clear(); tot = 0; int o = dfs(x); for (auto i : g) v[i] = 0; if (o) return 1; } while (next_permutation(p, p + m)); return 0; } int fg(long long x) { return x < 0 ? -1 : x == 0 ? 0 : 1; } const long double eps = 1e-12; int main() { ios::sync_with_stdio(false); cin >> m >> n; for (int i = 0, x, y; i < m; ++i) { cin >> x >> y; a[i].x = x, a[i].y = y; } for (int i = 0, x, y; i < n; ++i) { cin >> x >> y; b[i].x = x, b[i].y = y; } for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) { Vector v1 = b[j] - a[i]; for (int k = 0; k < n; ++k) { Vector v2 = b[k] - a[i]; if (fabs(1.0L * v2.x * v2.x * length2(v1) - 1.0L * v1.x * v1.x * length2(v2)) < eps && fabs(1.0L * v2.y * v2.y * length2(v1) - 1.0L * v1.y * v1.y * length2(v2)) < eps && fg(v1.x) == fg(v2.x) && fg(v1.y) == fg(v2.y) && length2(v2) < length2(v1)) c[i][j].push_back(k); } } int ans = 0; for (int i = 0; i < n; ++i) ans += check(i); cout << ans << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; void solve() { int n; cin >> n; char c, prev; cin >> c; prev = c; int cnt = 0; while (cin >> c) { if (c == prev) cnt++; prev = c; } cout << cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1005000; const int MD = 1000000007; vector<int> divs[MAXN]; int lp[MAXN]; vector<int> pr; int phi[MAXN]; int pil[MAXN]; int sum[MAXN][3]; int getPhi(int x) { int res = x; while (x != 1) { int div = lp[x]; res /= div; res *= (div - 1); while (x % div == 0) { x /= div; } } return res; } int getPil(int x) { int res = 0; for (vector<int>::iterator it = divs[x].begin(); it != divs[x].end(); ++it) { res = (res + 1ll * *it * phi[x / *it]) % MD; } res = (res + MD - x) % MD; return res; } int main() { ios_base::sync_with_stdio(false); for (int i = 0; i <= 1000000; ++i) { lp[i] = 0; } for (int i = 2; i <= 1000000; ++i) { if (lp[i] == 0) { lp[i] = i; pr.push_back(i); } for (int j = 0; j < int(pr.size()) && pr[j] <= lp[i] && 1000000 / i >= pr[j]; ++j) { lp[i * pr[j]] = pr[j]; } } divs[1].clear(); divs[1].push_back(1); for (int i = 2; i <= 1000000; ++i) { divs[i].clear(); if (lp[i] == i) { divs[i].push_back(1); divs[i].push_back(i); } else { int div = lp[i], cnt = 0; int cur_v = i; while (cur_v % div == 0) { ++cnt; cur_v /= div; } const vector<int> &from = divs[cur_v]; divs[i].reserve(from.size() * (cnt + 1)); for (vector<int>::const_iterator it = from.begin(); it != from.end(); ++it) { for (int cp = 1, j = 0; j <= cnt; cp *= div, ++j) { divs[i].push_back(cp * *it); } } } } for (int i = 1; i <= 1000000; ++i) { phi[i] = getPhi(i); pil[i] = (getPil(i) - i + 1 + MD) % MD; } sum[1][0] = sum[1][1] = sum[1][2] = 1; for (int i = 2, tmp = 0; i <= 1000000; ++i) { int add = (1ll * i * i + (tmp + pil[i]) * 2) % MD; sum[i][0] = (sum[i - 1][0] + add) % MD; sum[i][1] = (sum[i - 1][1] + 1ll * add * i) % MD; sum[i][2] = (sum[i - 1][2] + 1ll * add * i % MD * i) % MD; tmp = (tmp + 1ll * (i - 1) * (i - 1)) % MD; } int t; cin >> t; for (int i = 0; i < t; ++i) { int n, m; cin >> n >> m; if (n > m) { swap(n, m); } int res = sum[n][2]; res = (res + 1ll * (n + 1) * (m + 1) % MD * sum[n][0]) % MD; res = (res - 1ll * (n + m + 2) * sum[n][1] % MD + MD) % MD; cout << res << '\n'; } return 0; }
10
#include <bits/stdc++.h> using namespace std; long long int m, n, c, d, j, k, l, r, x, t, y, u, b, z, i; vector<long long int> v, w; set<long long int> s1; string s, p, q; map<long long int, pair<long long int, long long int> > mp; map<char, long long int> mk; int main() { cin >> n; long long int a[n + 1]; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= n; i++) { if (a[i] != i) { j = i; break; } } for (i = n; i > 0; i--) { if (a[i] != i) { k = i; break; } } for (i = 1; i < j; i++) v.push_back(a[i]); for (i = k; i >= j; i--) v.push_back(a[i]); for (i = k + 1; i <= n; i++) v.push_back(a[i]); for (i = 0; i < n; i++) { if (v[i] != i + 1) c = 1; } if (c) cout << 0 << " " << 0; else cout << j << " " << k; }
2
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:94777216") using namespace std; string st; int din[100001][26]; int ls[26]; int main() { cin >> st; int n = st.length(); for (int i = 0; i < 26; i++) ls[i] = -1; for (int i = 1; i <= n; i++) { for (int j = 0; j < 26; j++) din[i][j] = din[i - 1][j]; din[i][st[i - 1] - 'a']++; ls[st[i - 1] - 'a'] = i - 1; } int p = 0; for (int i = 25; i >= 0; i--) { if (ls[i] != -1) { bool b = 0; for (int j = 0; j < din[ls[i] + 1][i] - din[p][i]; j++) { cout << (char)('a' + i); b = 1; } if (b) p = ls[i] + 1; } if (p == n) return 0; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int t, i, j, k, m, n, x, y; t = 1; while (t--) { long long int flag = 0; x = -1; string s; cin >> s; n = s.length(); string F = ":("; for (i = 0; i < n; i++) { if (s[i] == 'a') { x = i; } } if (x == -1) { if (n & 1) { cout << F; } else { for (i = 0; i < n / 2; i++) { if (s[i] != s[n / 2 + i]) { cout << F; return 0; } } for (i = 0; i < n / 2; i++) { cout << s[i]; } } } else { string z = ""; for (i = 0; i < x; i++) { if (s[i] != 'a') { z += s[i]; } } string p = ""; for (i = x + 1; i < n; i++) { p += s[i]; } long long int len = z.length() + p.length(); if (len & 1 || p.length() < z.length()) { cout << F; return 0; } long long int diff = p.length() - z.length(); diff /= 2; for (i = 0; i < diff; i++) { z += p[i]; p[i] = '!'; } string pp = ""; for (i = 0; i < p.length(); i++) { if (p[i] != '!') { pp += p[i]; } } for (i = 0; i < z.length(); i++) { if (z[i] != pp[i]) { cout << F; return 0; } } for (i = 0; i <= x + diff; i++) { cout << s[i]; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 1; int k, s[maxn], t[maxn], res[maxn]; void Input() { cin >> k; char c; s[0] = t[0] = 0; for (int i = 1; i <= k; i++) { cin >> c; s[i] = int(c - 'a'); } for (int i = 1; i <= k; i++) { cin >> c; t[i] = int(c - 'a'); } } void Solve() { int d = 0; for (int i = k; i >= 1; i--) { if (s[i] + t[i] + d >= 26) { res[i] = s[i] + t[i] + d - 26; d = 1; } else { res[i] = s[i] + t[i] + d; d = 0; } } res[0] = d; d = 0; for (int i = 0; i <= k; i++) { if ((res[i] + d * 26) % 2 == 0) { res[i] = (res[i] + d * 26) / 2; d = 0; } else { res[i] = (res[i] + d * 26) / 2; d = 1; } } } void Output() { for (int i = 1; i <= k; i++) cout << char(res[i] + 'a'); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); Input(); Solve(); Output(); }
5
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } int n, e; const int MAXN = 2000005; vector<int> res; int arr[MAXN]; int pos[MAXN]; vector<int> p[MAXN]; bool seen[MAXN]; priority_queue<int> pq; set<int> places; void dfs(int u) { if (seen[u]) return; pq.push(-u); seen[u] = 1; places.insert(pos[u]); for (int j = 0; j < ((int)(p[u].size())); ++j) dfs(p[u][j]); } char s[MAXN]; int main() { scanf("%d", &n); res.resize(n); memset(seen, 0, sizeof(seen)); for (int i = 0; i < n; ++i) { scanf("%d", &arr[i]); res[i] = arr[i]; pos[arr[i]] = i; } scanf("%s", s); for (int i = 0; i < n - 1; ++i) { if (s[i] == '1') { int u = i, v = i + 1; p[arr[u]].push_back(arr[v]); p[arr[v]].push_back(arr[u]); } } for (int i = 0; i < n; ++i) { if (!seen[arr[i]]) { places.clear(); dfs(arr[i]); set<int>::iterator it = places.begin(); for (; it != places.end(); it++) { res[*it] = -pq.top(); pq.pop(); } } } int ok = 1; for (int i = 0; i < ((int)(res.size())) - 1; ++i) ok &= res[i] <= res[i + 1]; if (ok) cout << "YES\n"; else cout << "NO\n"; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:167772160000") using namespace std; vector<pair<int, int> > edges[310000]; bool ans[310000]; int ansCount; int num[310000]; int cnt[3]; bool vis[310000]; bool dfs(int a) { if (vis[a]) return false; vis[a] = true; bool ret = (num[a] == 2); for (int i = 0; i < (edges[a].size()); i++) { if (dfs(edges[a][i].first)) { ans[edges[a][i].second] = true; ansCount++; ret = !ret; } } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= (n); i++) { cin >> num[i]; num[i]++; cnt[num[i]]++; } for (int i = 1; i <= (m); i++) { int a, b; cin >> a >> b; edges[a].push_back(pair<int, int>(b, i)); edges[b].push_back(pair<int, int>(a, i)); } if (cnt[2] % 2 == 1 && cnt[0] == 0) { cout << -1; return 0; } int st = 1; if (cnt[0] > 0) while (num[st] != 0) st++; dfs(st); cout << ansCount << "\n"; for (int i = 1; i <= (m); i++) if (ans[i]) cout << i << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; void solve() { long long n, b, a, an = 0; cin >> n >> b >> a; long long x[n], cur = a; for (long long int i = 0; i < n; i++) cin >> x[i]; for (long long int i = 0; i < n; i++) { if (b == 0 && cur == 0) break; an++; if (x[i]) { if (cur == a) cur -= 2; else if (b) b--; else cur -= 2; cur++; } else { if (cur) cur--; else b--; } } cout << an; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long TESTS = 1; while (TESTS--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-10; const int INF = 1 << 30; const long long LLINF = 1ll << 60; const int MOD = 1e9 + 7; const int xx[8] = {1, 0, -1, 0, 1, -1, 1, -1}; const int yy[8] = {0, 1, 0, -1, -1, 1, 1, -1}; void fastio() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int main() { int n; scanf("%d", &n); int arr[n], maxi = 0, temp; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); if (arr[i] >= maxi) { temp = i; maxi = arr[i]; } } bool fl = true; for (int i = 0; i < temp; i++) { if (arr[i] >= arr[i + 1]) { fl = false; } } for (int i = temp; i < n - 1; i++) { if (arr[i] <= arr[i + 1]) { fl = false; } } if (fl) printf("YES\n"); else printf("NO\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 5; int ans[maxn * maxn], x[maxn * 5]; int main() { int n, m, k; int a, b; scanf("%d%d%d", &n, &m, &k); int num = 0; for (int i = 1; i <= k; i++) ans[num++] = 1; for (int i = 1; i <= n; i++) x[i] = k; int z; for (int i = n + 1; i <= m + n; i++) { if (!x[i]) { ans[num++] = i - 1; for (int j = i - 1; j < i - 1 + n; j++) x[j]++; } if (x[i] < k) { z = k - x[i]; for (int j = 1; j <= z; j++) ans[num++] = i; for (int j = i; j < i + n; j++) x[j] += z; } } if (x[n + m + 1] == 0) ans[num++] = n + m; printf("%d\n", num); for (int i = 0; i < num - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[num - 1]); }
5
#include <bits/stdc++.h> using namespace std; int a[33][33] = { 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1}; int main() { int x, y; cin >> x >> y; cout << a[x][y]; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int base = 1e6 + 3; long long f[20][20]; long long a[20]; long long FastPow(long long a, int n) { long long res = 1; for (; n; n >>= 1, a = a * a % base) if (n & 1) res = res * a % base; return res; } void Mul(int i, int x) { for (int j = (0), _b = (11); j <= _b; ++j) f[i][j] = f[i][j] * x % base; } void Sub(int i, int k) { for (int j = (0), _b = (11); j <= _b; ++j) f[i][j] = (f[i][j] - f[k][j] + base) % base; } int calc(int x) { long long res = 0; for (int i = (10), _b = (0); i >= _b; --i) res = (res * x + a[i]) % base; return res; } int main() { iostream::sync_with_stdio(false); cin.tie(0); for (int i = (0), _b = (10); i <= _b; ++i) { long long x = 1; for (int j = (0), _b = (10); j <= _b; ++j) { f[i][j] = x; x = x * i % base; } cout << "? " << i << endl; cin >> f[i][11]; } for (int i = (0), _b = (10); i <= _b; ++i) for (int j = (0), _b = (10); j <= _b; ++j) if (f[i][j] != 0) { Mul(i, FastPow(f[i][j], base - 2)); assert(f[i][j] == 1); for (int k = (0), _b = (10); k <= _b; ++k) if (k != i && f[k][j] != 0) { int save = f[k][j]; Mul(i, f[k][j]); Sub(k, i); Mul(i, FastPow(save, base - 2)); assert(f[k][j] == 0); } break; } for (int i = (0), _b = (10); i <= _b; ++i) for (int j = (0), _b = (10); j <= _b; ++j) if (f[i][j]) a[j] = f[i][11]; for (int i = 0, _b = (base); i < _b; ++i) if (calc(i) == 0) { return !(cout << "! " << i << endl); } cout << "! -1" << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 100005; int a[N]; int main() { ios::sync_with_stdio(0); int n, k; cin >> n >> k; for (int(i) = (0); i < (n); ++(i)) a[i] = i; k = min(k, n / 2); long long ans = 0; for (int(j) = (0); j < (n); ++(j)) { if (j < k) ans += (n - 1 - j); else ans += min(k, n - 1 - j); } cout << ans; }
2