solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; const int maxn = 600010; const int INF = 1 << 30; const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; int n, m, k; vector<int> g[maxn], w[maxn]; map<pair<int, int>, int> mapp; queue<int> que; int d[maxn]; int r[maxn], c[maxn]; bool inq[maxn]; void link(int u, int v, int c) { if (u == -1 || v == -1) return; g[u].push_back(v); w[u].push_back(c); } int getid(bool col, int x) { if (col) { if (x <= m && x >= 1) return k + 10 + x; else return -1; } else { if (x <= n && x >= 1) return k + 10 + m + x; else return -1; } } int spfa(int s, int t) { for (int i = 0; i < maxn; i++) d[i] = INF; memset(inq, false, sizeof(inq)); d[s] = 0; que.push(s); while (!que.empty()) { int u = que.front(); que.pop(); inq[u] = false; for (int i = 0; i < (int)g[u].size(); i++) { int v = g[u][i]; if (d[v] > d[u] + w[u][i]) { d[v] = d[u] + w[u][i]; if (!inq[v]) { que.push(v); inq[v] = true; } } } } return d[t] != INF ? d[t] : -1; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= k; i++) { scanf("%d%d", &r[i], &c[i]); mapp[make_pair(r[i], c[i])] = i; link(getid(0, r[i]), i, 0); link(getid(0, r[i] - 1), i, 0); link(getid(0, r[i] + 1), i, 0); link(i, getid(0, r[i]), 1); link(i, getid(0, r[i] - 1), 1); link(i, getid(0, r[i] + 1), 1); link(getid(1, c[i]), i, 0); link(getid(1, c[i] - 1), i, 0); link(getid(1, c[i] + 1), i, 0); link(i, getid(1, c[i]), 1); link(i, getid(1, c[i] - 1), 1); link(i, getid(1, c[i] + 1), 1); } for (int i = 1; i <= k; i++) { for (int j = 0; j < 4; j++) { if (mapp[make_pair(r[i] + dx[j], c[i] + dy[j])] != 0) link(i, mapp[make_pair(r[i] + dx[j], c[i] + dy[j])], 0); } } if (mapp[make_pair(n, m)] == 0) { mapp[make_pair(n, m)] = k + 2; link(getid(0, n), k + 2, 0); link(getid(1, m), k + 2, 0); } int ans = spfa(mapp[make_pair(1, 1)], mapp[make_pair(n, m)]); printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; double PI = acos(-1); double EPS = 1e-7; int INF = 1000000000; int MOD = 1000000007; int MAXINT = 2147483647; long long INFLL = 1000000000000000000LL; long long MAXLL = 9223372036854775807LL; int mx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}; int my[8] = {0, 0, -1, 1, -1, 1, -1, 1}; vector<int> lol[2]; vector<pair<int, int> > comb; map<int, int> co[2]; int maks = -1; long long ans = 0; long long C(int N, int P) { if (P > N) return 0; long long ret = 1; for (int(a) = (0); (a) <= (P - 1); (a)++) { ret *= (long long)(N - a); } for (int(a) = (1); (a) <= (P); (a)++) { ret /= (long long)a; } return ret; } void MAX(int v, long long x) { if (x <= 0) return; if (maks < v) { maks = v; ans = x; } else if (maks == v) ans += x; } int main() { int n, m; scanf("%d%d", &n, &m); for (int(a) = (1); (a) <= (m); (a)++) { int t, d; scanf("%d%d", &t, &d); co[t][d]++; comb.push_back(make_pair(d, t)); lol[t].push_back(d); } sort(comb.begin(), comb.end()); sort(lol[0].begin(), lol[0].end()); sort(lol[1].begin(), lol[1].end()); if ((int)lol[0].size() >= 2) { int val = lol[0].back() - lol[0][0]; int numl = co[0][lol[0][0]]; int numr = co[0][lol[0].back()]; int sz = (int)lol[0].size(); int other = sz - numl - numr; if (other < 0) MAX(val, C(sz, 3)); else { MAX(val, C(numl, 2) * C(numr, 1)); MAX(val, C(numl, 1) * C(numr, 2)); MAX(val, C(numl, 1) * C(numr, 1) * C(other, 1)); } } if ((int)lol[1].size() >= 2) { int val = lol[1].back() - lol[1][0]; int numl = co[1][lol[1][0]]; int numr = co[1][lol[1].back()]; int sz = (int)lol[1].size(); int other = sz - numl - numr; if (other < 0) MAX(val, C(sz, 3)); else { MAX(val, C(numl, 2) * C(numr, 1)); MAX(val, C(numl, 1) * C(numr, 2)); MAX(val, C(numl, 1) * C(numr, 1) * C(other, 1)); } } int left = 0; for (int(a) = (0); (a) <= ((int)lol[0].size() - 1); (a)++) { int val = n; while (left < (int)lol[1].size() && lol[1][left] < lol[0][a]) left++; int same = co[1][lol[0][a]]; int right = (int)lol[1].size() - left - same; MAX(val, left * right); MAX(val, left * same); MAX(val, same * right); MAX(val, C(same, 2)); } left = 0; for (int(a) = (0); (a) <= ((int)lol[1].size() - 1); (a)++) { int val = n; while (left < (int)lol[0].size() && lol[0][left] < lol[1][a]) left++; int same = co[0][lol[1][a]]; int right = (int)lol[0].size() - left - same; MAX(val, left * right); MAX(val, left * same); MAX(val, same * right); MAX(val, C(same, 2)); } int ls[2] = {-1, -1}; int sisa[2] = {(int)lol[0].size(), (int)lol[1].size()}; int wait = -1; for (int(a) = (0); (a) <= ((int)comb.size() - 1); (a)++) { if (wait != -1 && comb[wait].first != comb[a].first) { for (int(b) = (wait); (b) <= (a - 1); (b)++) { ls[comb[b].second] = comb[b].first; } wait = -1; } sisa[comb[a].second]--; if (ls[!comb[a].second] != -1) { MAX(n - (comb[a].first - ls[!comb[a].second]), (long long)co[!comb[a].second][ls[!comb[a].second]] * sisa[comb[a].second]); } if (wait == -1) wait = a; } ls[0] = -1; ls[1] = -1; sisa[0] = (int)lol[0].size(); sisa[1] = (int)lol[1].size(); wait = -1; for (int(a) = ((int)comb.size() - 1); (a) >= (0); (a)--) { if (wait != -1 && comb[wait].first != comb[a].first) { for (int(b) = (wait); (b) >= (a + 1); (b)--) { ls[comb[b].second] = comb[b].first; } wait = -1; } sisa[comb[a].second]--; if (ls[!comb[a].second] != -1) { MAX(n - (ls[!comb[a].second] - comb[a].first), (long long)co[!comb[a].second][ls[!comb[a].second]] * sisa[comb[a].second]); } if (wait == -1) wait = a; } printf("%I64d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; long long n, m; long long arr[4000][4000]; long long M(long long a) { return (a + 1000000007) % 1000000007; } long long f(long long a, long long b, long long x, long long y) { if (arr[a][b] == 0 || arr[x][y] == 0) return 0; long long tab[n][m]; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) tab[i][j] = arr[i][j]; } if (a == 0) { for (long long i = 0; i < n; i++) tab[i][0] = 0; } else { for (long long i = 0; i < m; i++) tab[0][i] = 0; } tab[a][b] = 1; for (long long i = a; i <= x; i++) { for (long long j = b; j <= y; j++) { if (i == 0 || j == 0) continue; if (tab[i][j] != 0) tab[i][j] = M(tab[i - 1][j] + tab[i][j - 1]); } } if (tab[x][y] == -1) return 0; return tab[x][y]; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { char c; cin >> c; if (c == '.') arr[i][j] = -1; else arr[i][j] = 0; if ((i == 0 || j == 0) && arr[i][j] == -1) arr[i][j] = 1; } } for (long long i = 0; i < n; i++) { if (arr[i][0] == 0) { for (long long j = i; j < n; j++) arr[j][0] = 0; break; } } for (long long i = 0; i < m; i++) { if (arr[0][i] == 0) { for (long long j = i; j < m; j++) arr[0][j] = 0; break; } } long long a = (M(M(f(0, 1, n - 2, m - 1) * f(1, 0, n - 1, m - 2)) - M(f(0, 1, n - 1, m - 2) * f(1, 0, n - 2, m - 1)))); cout << a << endl; }
4
#include <bits/stdc++.h> void solve(long long n, long long k, long long val) { if (n - 2 * k + 1 <= val) { if (val % 2) { printf("."); } else { printf("X"); } } else { printf("."); } } int main() { long long n, k; int q; scanf("%I64d%I64d%d", &n, &k, &q); for (int i = 0; i < q; i++) { long long val; scanf("%I64d", &val); if (k == 0) { printf("."); } else if (k == n) { printf("X"); } else if (k == 1) { if (val == n) { printf("X"); } else { printf("."); } } else { if (n >= 2 * k) { if (n % 2) { if (val == n) { printf("X"); } else { solve(n - 1, k - 1, val); } } else { solve(n, k, val); } } else { if (val >= n - (2 * k - n) + 1) { printf("X"); } else { solve(n - (2 * k - n), k - (2 * k - n), val); } } } } printf("\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int a[1000000]; int main() { long long int n, d; cin >> n >> d; for (long long int i = 0; i < n; i++) { cin >> a[i]; } long long int ans = 0; for (long long int i = 0, j = 0; i < n; i++) { while (j < n && a[j] - a[i] <= d) { ans += ((j - i) * (j - i - 1)) / 2; j++; } } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 113; const long long INF = 2e18 + 113; int c, v, mx, add, l; int ans = 1; int main() { cin >> c >> v >> mx >> add >> l; c = max(0, c - v); v = min(mx, v + add); while (c) { c += l; ++ans; c = max(0, c - v); v = min(mx, v + add); } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; vector<long long> lucky; void gen(long long num) { if (num > 44444444444) return; if (num > 0) lucky.push_back(num); gen(num * 10 + 4); gen(num * 10 + 7); } int main() { gen(0); sort(lucky.begin(), lucky.end()); long long l, r; cin >> l >> r; long long prv = l, ans = 0; for (long long i = 0; i < lucky.size(); i++) { if (lucky[i] < prv) continue; if (lucky[i] >= r) { ans += (r - prv + 1) * lucky[i]; break; } ans += (lucky[i] - prv + 1) * lucky[i]; prv = lucky[i] + 1; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int l = s.length(); long long maxx = 1; for (int i = 0; i < l; i++) { maxx *= (s[i] - '0'); } if (l == 1) { cout << s << endl; return 0; } for (int i = 1; i < l; i++) { long long ans = 1; for (int j = 0; j <= i - 2; j++) ans *= max(s[j] - '0', 1); ans *= max((s[i - 1] - '0' - 1), 1); for (int j = i; j < l; j++) ans *= 9; maxx = max(maxx, ans); } cout << maxx << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const double MAXN = 10000000; const int MAX2 = 1e8 + 1; long long absl(long long a, long long b) { return (a < b) ? (b - a) : (a - b); } long long power(long long a, long long b) { long long res = 1ll; a = a; while (b > 0) { if (b & 1) { res = (res * a); b--; } a = (a * a); b >>= 1; } return res; } long long fermat_inv(long long y) { return power(y, MOD - 2ll); } long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } template <typename T> T nxt() { T x; cin >> x; return x; } int n; struct pt { long long s; int i, j; }; int mn = 1; int mx; int cnt1 = 0; int st; void dfs(int i, int par, vector<int> v1[], int w) { if (int((v1[i]).size()) == 1 && i != st) { if (w % 2 != 0) mn = 3; if (w == 2) cnt1 += 1; } else { int cnt = 0; for (auto i1 : v1[i]) { if (i1 != par) { if (int((v1[i1]).size()) == 1 && w + 1 > 2) cnt += 1; dfs(i1, i, v1, w + 1); } } if (cnt > 1) mx -= cnt - 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; for (int tt = 1; tt <= t; ++tt) { n = nxt<int>(); vector<int> v1[n + 1]; for (int i1 = 0; i1 <= n - 2; i1++) { int x = nxt<int>(); int y = nxt<int>(); v1[x].push_back(y); v1[y].push_back(x); } int i; mx = n - 1; for (int i1 = 1; i1 <= n; i1++) { if (int((v1[i1]).size()) == 1) { i = i1; break; } } st = i; dfs(i, -1, v1, 0); if (cnt1 >= 1) mx -= cnt1; cout << mn << " " << mx; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxN = 105; int a[maxN][maxN], cot[maxN], dong[maxN], d_dong[maxN], d_cot[maxN]; int n, m; void update(int i, int j) { dong[i] += -2 * a[i][j]; cot[j] += -2 * a[i][j]; a[i][j] = -a[i][j]; } int check_dong() { for (int i = 1; i <= n; i++) if (dong[i] < 0) return i; return -1; } int check_cot() { for (int i = 1; i <= m; i++) if (cot[i] < 0) return i; return -1; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%i", &a[i][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cot[j] += a[i][j], dong[i] += a[i][j]; do { int dong = check_dong(); if (dong != -1) { for (int j = 1; j <= m; j++) update(dong, j); ++d_dong[dong]; continue; } int cot = check_cot(); if (cot != -1) { for (int i = 1; i <= n; i++) update(i, cot); ++d_cot[cot]; continue; } if (cot == -1 && dong == -1) break; } while (true); int ans = 0; for (int i = 1; i <= n; i++) if (d_dong[i] % 2 == 1) ++ans; cout << ans << " "; for (int i = 1; i <= n; i++) if (d_dong[i] % 2 == 1) printf("%i ", i); cout << "\n"; ans = 0; for (int i = 1; i <= m; i++) if (d_cot[i] % 2 == 1) ans++; cout << ans << " "; for (int i = 1; i <= m; i++) if (d_cot[i] % 2 == 1) printf("%i ", i); fclose(stdin); return 0; }
4
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); } template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); } template <class T> inline T ison(T n, T pos) { return (bool)(n & ((T)1 << pos)); } template <class T> inline T gcd(T a, T b) { while (b) { a %= b; swap(a, b); } return a; } template <typename T> string NumberToString(T Number) { ostringstream second; second << Number; return second.str(); } inline int nxt() { int aaa; scanf("%d", &aaa); return aaa; } inline long long int lxt() { long long int aaa; scanf("%lld", &aaa); return aaa; } inline double dxt() { double aaa; scanf("%lf", &aaa); return aaa; } template <class T> inline T bigmod(T p, T e, T m) { T ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; } return (T)ret; } long long int ar[1000010]; vector<pair<char, long long int> > v; int getLast(int l, int r, char c) { for (int i = r; i >= l; i--) { if (v[i].first == c) { return i; } } return -1; } int getFirst(int l, int r, char c) { for (int i = l; i <= r; i++) { if (v[i].first == c) return i; } return v.size() + 10; } string g = "hard"; long long int dp[100010][5]; long long int go(int pos, int cnt) { if (cnt >= 4) return (long long int)3e17; if (pos == v.size()) { if (cnt == 4) { return (long long int)3e17; } return 0; } long long int& res = dp[pos][cnt]; if (res != -1) return res; res = (long long int)3e17; res = min(res, go(pos + 1, cnt) + v[pos].second); res = min(res, go(pos + 1, cnt + (v[pos].first == g[cnt]))); return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { cin >> ar[i]; } for (int i = 0; i < n; i++) { if (s[i] == 'h' || s[i] == 'r' || s[i] == 'a' || s[i] == 'd') { v.push_back(make_pair(s[i], ar[i])); } } long long int ans = 0; memset(dp, -1, sizeof(dp)); ans = go(0, 0); cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; struct node { int i, f, g; node(int k) : i(k) {} }; vector<node> a[1000 + 10]; int i, j, k, m, n, l; bool cmp(node a, node b) { return a.i < b.i; } void getF(int i) { j = k = 0; while (j < ((int)(a[i]).size()) && k < ((int)(a[i + 1]).size())) { if (a[i][j].i < a[i + 1][k].i) a[i][j++].f = k; else k++; } while (j < ((int)(a[i]).size())) a[i][j++].f = k; for (int j = 0; j < (((int)(a[i]).size()) - 1); ++j) a[i][j].f = a[i][j + 1].f - a[i][j].f; } void getG(int i) { j = k = 0; while (j < ((int)(a[i]).size()) && k < ((int)(a[i - 1]).size())) { if (a[i][j].i < a[i - 1][k].i) a[i][j++].g = k; else k++; } while (j < ((int)(a[i]).size())) a[i][j++].g = k; for (int j = 0; j < (((int)(a[i]).size()) - 1); ++j) a[i][j].g = a[i][j + 1].g - a[i][j].g; } int main() { while (~scanf("%d", &n)) { int ans = 0; for (int i = (1); i <= (n); ++i) { a[i].clear(); scanf("%d", &m); for (int j = 0; j < (m); ++j) { scanf("%d", &k); node p(k); a[i].push_back(p); } sort(a[i].begin(), a[i].end(), cmp); } a[n + 1] = a[1]; a[0] = a[n]; for (int i = (1); i <= (n); ++i) { getF(i); getG(i); for (int j = 0; j < (((int)(a[i]).size()) - 1); ++j) if (a[i][j].f != a[i][j].g) ans++; } cout << ans << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; struct pnt { int x, y, idx; bool operator<(const pnt &p) const { return pair<long long, long long>(x, y) < pair<long long, long long>(p.x, p.y); } } a[2005]; struct line { int dx, dy, i1, i2; }; vector<line> v; int n, rev[2005]; long long p, q; long long ccw(pnt a, pnt b, pnt c) { int dx1 = b.x - a.x; int dy1 = b.y - a.y; int dx2 = c.x - a.x; int dy2 = c.y - a.y; return abs(1ll * dx1 * dy2 - 1ll * dy1 * dx2); } void report(int x, int y, int z) { puts("Yes"); for (auto &i : {x, y, z}) { printf("%d %d\n", a[i].x, a[i].y); } exit(0); } void solve(int c1, int c2, long long l) { int s = c2, e = n - 1; while (s != e) { int m = (s + e + 1) / 2; if (ccw(a[c1], a[c2], a[m]) <= l) s = m; else e = m - 1; } if (ccw(a[c1], a[c2], a[s]) == l) { report(c1, c2, s); } s = 0, e = c1; while (s != e) { int m = (s + e) / 2; if (ccw(a[c1], a[c2], a[m]) <= l) e = m; else s = m + 1; } if (ccw(a[c1], a[c2], a[s]) == l) { report(c1, c2, s); } } int main() { cin >> n >> p; p <<= 1; for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } sort(a, a + n); for (int i = 0; i < n; i++) { a[i].idx = i; rev[i] = i; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { v.push_back({a[j].x - a[i].x, a[j].y - a[i].y, a[i].idx, a[j].idx}); } } sort(v.begin(), v.end(), [&](const line &a, const line &b) { long long cw = 1ll * a.dx * b.dy - 1ll * b.dx * a.dy; if (cw != 0) return cw > 0; return pair<long long, long long>(a.i1, a.i2) < pair<long long, long long>(b.i1, b.i2); }); long long ret = 0; for (int i = 0; i < v.size(); i++) { int c1 = rev[v[i].i1], c2 = rev[v[i].i2]; if (c1 > c2) swap(c1, c2); solve(c1, c2, p); swap(a[c1], a[c2]); swap(rev[v[i].i1], rev[v[i].i2]); } cout << "No" << endl; }
4
#include <bits/stdc++.h> using namespace std; const int N = 105; int n, L; int v[N]; char S[N]; char Str[N][N]; char Lower(char ch) { if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 'a'; return ch; } char Uper(char ch) { if (ch >= 'a' && ch <= 'z') return ch - 'a' + 'A'; return ch; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%s", Str[i] + 1); scanf("%s", S + 1); L = strlen(S + 1); memset(v, 0, sizeof(v)); for (int i = 1; i <= L; i++) { for (int j = 1; j <= n; j++) if (i >= strlen(Str[j] + 1)) { int m = strlen(Str[j] + 1); int l = i - m; bool check = true; for (int k = 1; k <= m; k++) { l++; if (Lower(Str[j][k]) != Lower(S[l])) check = false; } if (check) { l = i - m; for (int k = 1; k <= m; k++) { l++; v[l] = 1; } } } } char ch; cin >> ch; for (int i = 1; i <= L; i++) if (v[i]) { if (Lower(S[i]) == Lower(ch)) { if (S[i] >= 'a' && S[i] <= 'z') S[i] = 'a' + (Lower(ch) == 'a'); else S[i] = 'A' + (Lower(ch) == 'a'); } else { if (S[i] >= 'A' && S[i] <= 'Z') S[i] = Uper(ch); else S[i] = Lower(ch); } } printf("%s", S + 1); }
1
#include <bits/stdc++.h> using namespace std; int main() { long long k; cin >> k; long long len = 1, num = 9; while (k > len * num) { k -= len * num; ++len, num *= 10; } num /= 9; long long bel = num + (k - 1) / len; for (int n = len - 1 - (k - 1) % len; n > 0; n--) bel /= 10; cout << bel % 10 << '\n'; }
1
#include<bits/stdc++.h> #define N 600005 using namespace std; int n,m,l,r,c[N]; struct node{int l,r,len;}a[N]; inline bool cmp(node aa,node bb){return aa.len<bb.len;} inline int lowbit(int x){return x&-x;} inline void add(int x,int y){for(int i=x;i<=m;i+=lowbit(i))c[i]+=y;} inline int sum(int x){int ans=0;for(int i=x;i;i-=lowbit(i))ans+=c[i];return ans;} int main(){ scanf("%d%d",&n,&m);int tot=0; for (int i=1;i<=n;i++){ scanf("%d%d",&l,&r); a[i].l=l;a[i].r=r; a[i].len=r-l+1; } sort(a+1,a+n+1,cmp);int j=1; for (int i=1;i<=m;i++){ for (;j<=n;j++){ if (a[j].len>i) break; add(a[j].l,1); if (a[j].r+1<=m) add(a[j].r+1,-1); } int ans=n-j+1; for (int j=i;j<=m;j+=i) ans=ans+sum(j); printf("%d\n",ans); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; int p = 0; while (p * (p+1) / 2 < n) p++; for (int i = 1; i <= p; i++) { if (p * (p+1) / 2 == n + i) continue; cout << i << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; string remex(string s) { string t = ""; for (int i = 0; s[i]; i++) if (s[i] == '*' || s[i] == '?') continue; else t += s[i]; return t; } void solve() { string s; cin >> s; int k; cin >> k; int cnt1 = 0, cnt2 = 0; for (int i = 0; s[i]; i++) { if (s[i] == '?') cnt1++; else if (s[i] == '*') cnt2++; } int slwe = s.length() - (cnt1 + cnt2); if (k == slwe) { cout << remex(s) << "\n"; } else if (k > slwe) { if (cnt2) { string t = ""; for (int i = 0; s[i]; i++) { if (s[i] == '*') { while (slwe < k) { t += s[i - 1]; slwe++; } for (int j = i; s[j]; j++) if (s[j] != '*' && s[j] != '?') t += s[j]; break; } else if (s[i] == '?') { } else { t += s[i]; } } cout << t << "\n"; } else { cout << "Impossible\n"; } } else if (k >= slwe - (cnt1 + cnt2)) { string t = ""; int rem = 0; for (int i = 0; s[i]; i++) { if (s[i] == '?' || s[i] == '*') continue; if (s[i + 1] == '?' || s[i + 1] == '*') { rem++; if (slwe - rem == k) { for (int j = i + 1; s[j]; j++) if (s[j] != '*' && s[j] != '?') t += s[j]; break; } continue; } t += s[i]; } cout << t << "\n"; } else if (k < slwe - (cnt1 + cnt2)) { cout << "Impossible\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; while (t--) solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); ios::sync_with_stdio(false); int n; cin >> n; int a; vector<int> v; v.push_back(0); for (int i = 0; i < n; i++) { cin >> a; v.push_back(a); } int f1 = 0, f2 = 0, b1 = 0, b2 = 0; for (int i = 1; i <= n; i++) { b2 += v[i]; i++; } for (int i = 2; i <= n; i++) { b1 += v[i]; i++; } int ans = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) b1 = b1 - v[i] + v[i - 1]; else b2 = b2 - v[i] + v[i - 1]; if (b1 == b2) ans++; } cout << ans; return 0; }
2
#include <bits/stdc++.h> const int N = (1 << 22); using namespace std; int a[N], dp[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < (n); i++) { cin >> a[i]; dp[a[i]] = a[i]; } int yek = (1 << 22) - 1; for (int i = 0; i < (N); i++) for (int j = 0; j < (22); j++) if ((i & (1 << j)) && dp[i - (1 << j)]) dp[i] = dp[i - (1 << j)]; for (int i = 0; i < (n); i++) cout << (dp[yek ^ a[i]] ? dp[yek ^ a[i]] : -1) << ' '; }
5
#include <bits/stdc++.h> using namespace std; long long add(long long a, long long b, long long c) { long long res = a + b; return (res >= c ? res - c : res); } long long mod_neg(long long a, long long b, long long c) { long long res; if (abs(a - b) < c) res = a - b; else res = (a - b) % c; return (res < 0 ? res + c : res); } long long mul(long long a, long long b, long long c) { long long res = (long long)a * b; return (res >= c ? res % c : res); } template <typename T> T power(T e, T n) { T x = 1, p = e; while (n) { if (n & 1) x = x * p; p = p * p; n >>= 1; } return x; } template <typename T> T mdpower(T e, T n, T m) { T x = 1, p = e; while (n) { if (n & 1) x = mul(x, p, m); p = mul(p, p, m); n >>= 1; } return x; } template <typename T> T extended_euclid(T a, T b, T &x, T &y) { T xx = 0, yy = 1; y = 0; x = 1; while (b) { T q = a / b, t = b; b = a % b; a = t; t = xx; xx = x - q * xx; x = t; t = yy; yy = y - q * yy; y = t; } return a; } template <typename T> T mod_inverse(T a, T n) { T x, y, z = 0; T d = extended_euclid(a, n, x, y); return (d > 1 ? -1 : mod_neg(x, z, n)); } const long long N = 300005; long long arr[N]; long long dp[N]; long long dp1[N]; long long subtree[N]; vector<long long> v[N]; void dfs(long long s, long long par) { for (auto it : v[s]) { if (it == par) continue; dfs(it, s); } subtree[s] = v[s].size() - 1; } void dfs1(long long s, long long par) { dp[s] = arr[s]; for (auto it : v[s]) { if (it == par) continue; dfs1(it, s); dp[s] += dp[it] + subtree[it]; } dp[s] += subtree[s]; } void dfs2(long long s, long long par) { dp1[s] = dp1[par] + dp[par] - dp[s] - 1; for (auto it : v[s]) { if (it == par) continue; dfs2(it, s); } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; map<long long, long long> mp; long long ans = INT_MAX; for (long long i = 1; i <= n; i++) { cin >> arr[i]; mp[arr[i]]++; } for (long long i = 1; i <= n - 1; i++) { long long a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } for (long long i = 1; i <= n; i++) { long long node = i; long long ans1 = arr[node]; mp[arr[node]]--; if (mp[arr[node]] == 0) { mp.erase(mp.find(arr[node])); } for (auto it : v[node]) { ans1 = max(ans1, arr[it] + 1); mp[arr[it]]--; if (mp[arr[it]] == 0) mp.erase(mp.find(arr[it])); } if (mp.size() > 0) { auto jt = mp.end(); jt--; ans1 = max(ans1, jt->first + 2); } ans = min(ans, ans1); for (auto it : v[node]) { mp[arr[it]]++; } mp[arr[node]]++; } cout << ans << "\n"; cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; }
3
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll N; map<ll, ll> S; ll Q; ll R; int main() { cin >> N; for (ll i = 0; i < N; ++i) { ll a; cin >> a; ++S[a]; R += a; } cin >> Q; for (ll q = 0; q < Q; ++q) { ll b, c; cin >> b >> c; R += (c - b) * S[b]; S[c] += S[b]; S[b] = 0; cout << R << endl; } }
0
#include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { if (!b) return a; return gcd(b, a % b); } map<string, int> mp; string minn, maxx; int temp[6000]; struct num { int def, ope; string s, v1, v2, val; int id1, id2; } f[5010]; int getbit(int n, int bit, int def) { int sum = 0; for (int i = 0; i < n; i++) { if (f[i].def == 1) temp[i] = (f[i].val[bit] - '0'); else { if (f[i].def == 2) { if (f[i].ope == 1) { sum += def & def; temp[i] = def & def; } else if (f[i].ope == 2) { sum += def | def; temp[i] = def | def; } else if (f[i].ope == 3) { sum += def ^ def; temp[i] = def ^ def; } } if (f[i].def == 3) { if (f[i].ope == 1) { sum += def & temp[f[i].id2]; temp[i] = def & temp[f[i].id2]; } else if (f[i].ope == 2) { sum += def | temp[f[i].id2]; temp[i] = def | temp[f[i].id2]; } else if (f[i].ope == 3) { sum += def ^ temp[f[i].id2]; temp[i] = def ^ temp[f[i].id2]; } } else if (f[i].def == 4) { if (f[i].ope == 1) { sum += def & temp[f[i].id1]; temp[i] = def & temp[f[i].id1]; } else if (f[i].ope == 2) { sum += def | temp[f[i].id1]; temp[i] = def | temp[f[i].id1]; } else if (f[i].ope == 3) { sum += def ^ temp[f[i].id1]; temp[i] = def ^ temp[f[i].id1]; } } else if (f[i].def == 5) { if (f[i].ope == 1) { sum += temp[f[i].id1] & temp[f[i].id2]; temp[i] = temp[f[i].id1] & temp[f[i].id2]; } else if (f[i].ope == 2) { sum += temp[f[i].id1] | temp[f[i].id2]; temp[i] = temp[f[i].id1] | temp[f[i].id2]; } else if (f[i].ope == 3) { sum += temp[f[i].id1] ^ temp[f[i].id2]; temp[i] = temp[f[i].id1] ^ temp[f[i].id2]; } } } } return sum; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { string name, tmp1, tmp2; cin >> name >> tmp1 >> tmp2; if (isdigit(tmp2[0])) { f[i].val = tmp2; f[i].def = 1; mp[name] = i; } else { string tmp3; cin >> tmp3 >> f[i].v2; if (tmp2[0] != '?' && f[i].v2[0] != '?') { if (tmp3[0] == 'A') { f[i].v1 = tmp2; f[i].def = 5; f[i].ope = 1; f[i].id1 = mp[f[i].v1]; f[i].id2 = mp[f[i].v2]; mp[name] = i; } else if (tmp3[0] == 'O') { f[i].v1 = tmp2; f[i].def = 5; f[i].ope = 2; f[i].id1 = mp[f[i].v1]; f[i].id2 = mp[f[i].v2]; mp[name] = i; } else if (tmp3[0] == 'X') { f[i].v1 = tmp2; f[i].def = 5; f[i].ope = 3; f[i].id1 = mp[f[i].v1]; f[i].id2 = mp[f[i].v2]; mp[name] = i; } } else if (tmp2[0] == '?' && f[i].v2[0] == '?') { if (tmp3[0] == 'A') { f[i].ope = 1; mp[name] = i; f[i].def = 2; } else if (tmp3[0] == 'O') { f[i].ope = 2; mp[name] = i; f[i].def = 2; } else if (tmp3[0] == 'X') { f[i].ope = 3; mp[name] = i; f[i].def = 2; } } else if (tmp2[0] == '?' && f[i].v2[0] != '?') { if (tmp3[0] == 'A') { f[i].ope = 1; f[i].def = 3; f[i].id2 = mp[f[i].v2]; } else if (tmp3[0] == 'O') { f[i].ope = 2; f[i].def = 3; f[i].id2 = mp[f[i].v2]; } else if (tmp3[0] == 'X') { f[i].ope = 3; f[i].def = 3; f[i].id2 = mp[f[i].v2]; } mp[name] = i; } else if (tmp2[0] != '?' && f[i].v2[0] == '?') { f[i].v1 = tmp2; mp[name] = i; if (tmp3[0] == 'A') { f[i].ope = 1; f[i].def = 4; f[i].id1 = mp[f[i].v1]; } else if (tmp3[0] == 'O') { f[i].ope = 2; f[i].def = 4; f[i].id1 = mp[f[i].v1]; } else if (tmp3[0] == 'X') { f[i].ope = 3; f[i].def = 4; f[i].id1 = mp[f[i].v1]; } } } } for (int i = 0; i < m; i++) { int X = getbit(n, i, 0); int Y = getbit(n, i, 1); if (X > Y) { minn.push_back('1'); maxx.push_back('0'); } else if (X < Y) { minn.push_back('0'); maxx.push_back('1'); } else if (X == Y) { minn.push_back('0'); maxx.push_back('0'); } } cout << minn << endl << maxx << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; int n; vector<int> a[200]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed; cin >> n; int k = (1 + sqrt(1 + 8 * n)) / 2, x = 1; cout << k << endl; for (int i = 1; i <= k; i++) { for (int j = i + 1; j <= k; j++) { a[i].push_back(x); a[j].push_back(x); x++; } } for (int i = 1; i <= k; i++) { int m = a[i].size(); for (int j = 0; j < m; j++) { cout << a[i][j] << (j == m - 1 ? '\n' : ' '); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long int; using dl = double; const int N = 2e5 + 10; ll aarray[200000 + 10]; ll magic[101][101]; vector<ll> primes; bool fk[1000001]; int main() { ios_base::sync_with_stdio(false); string str, ptr, ps[1001], ss[1001], ans; ll n, m, a, j, k, i, p, four = 0, seven = 0, f = 0; cin >> str >> ptr; ans += str[0]; for (i = 1; i < str.length(); i++) { if (str[i] < ptr[0]) ans += str[i]; else break; } ans += ptr[0]; cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 3e2, M = 3e5 + 3e2, inf = 2e9 + 10; inline int fastmin(int x, int y) { return (((x - y) >> 31) & (x ^ y)) ^ y; } int ANS[M], NEW[N], A[N], UNQ[N], DF[N], DS[N], n, q, sz, l, r, a; vector<int> FIXR[N], FIXRID[N]; void addf(int ind, int d) { for (ind = (n - ind + 1); ind <= n; ind += (ind & -ind)) DF[ind] = min(DF[ind], d); } int getf(int ind) { int sm = inf; for (ind = (n - ind + 1); ind; ind -= (ind & -ind)) sm = min(DF[ind], sm); return sm; } void adds(int ind, int d, int id = 1, int l = 1, int r = sz) { DS[id] = d; if (r == l) return; int mid = ((l + r) >> 1); if (ind <= mid) adds(ind, d, (id << 1), l, mid); else adds(ind, d, (id << 1 | 1), mid + 1, r); } int gets(int s, int e, int id = 1, int l = 1, int r = sz) { if (UNQ[l] >= s && UNQ[r] <= e) return (DS[id]); if (l == r) return (0); int mid = ((l + r) >> 1); int rt = 0; if (s <= UNQ[mid]) rt = max(rt, gets(s, e, (id << 1), l, mid)); if (UNQ[mid + 1] <= e) rt = max(rt, gets(s, e, (id << 1 | 1), mid + 1, r)); return (rt); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &A[i]), UNQ[i] = A[i]; sort(UNQ + 1, UNQ + n + 1); sz = unique(UNQ + 1, UNQ + n + 1) - UNQ - 1; for (int i = 1; i <= n; i++) NEW[i] = lower_bound(UNQ + 1, UNQ + sz + 1, A[i]) - UNQ; scanf("%d", &q); for (int i = 1; i <= q; i++) scanf("%d %d", &l, &r), FIXR[r].push_back(l), FIXRID[r].push_back(i); memset(DF, 63, sizeof(DF)); for (int i = 2; i <= n; i++) { adds(NEW[i - 1], i - 1); l = -inf, r = inf, a = i - 1; while (l <= r) { a = gets(l, r); if (!a) break; addf(a, abs(A[a] - A[i])); if (A[a] >= A[i]) r = (A[i] + A[a] - 1) >> 1; if (A[a] <= A[i]) l = (A[i] + A[a] + 2) >> 1; } for (int j = 0; j < (int)FIXR[i].size(); j++) ANS[FIXRID[i][j]] = getf(FIXR[i][j]); } for (int i = 1; i <= q; i++) printf("%d\n", ANS[i]); }
6
#include <bits/stdc++.h> using namespace std; #define int long long typedef long long ll; typedef unsigned long long ull; typedef unsigned __int128 HASH; typedef pair<int,int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll,int> plli; typedef pair<double, int> pdbi; typedef pair<int,pii> pipii; typedef pair<ll,pll> plpll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) #define rrep(i,n) for (int i=(n);i>0;i--) #define rrep2(i,a,b) for (int i=(a);i>b;i--) #define pb push_back #define fi first #define se second #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const int INF = 1<<30; const ll mod = 1000000000 + 7; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const double pi = 3.141592653589793; #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod int n, q, s, t; int a[200000 + 5], diff[200000 + 5]; signed main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n >> q >> s >> t; cin >> a[0]; rep2(i, 1, n + 1) { cin >> a[i]; diff[i] = a[i] - a[i - 1]; } int ans = 0; rep2(i, 1, n + 1) { if (diff[i] >= 0) ans -= diff[i] * s; else ans += -diff[i] * t; } rep(i, q) { int l, r, x; cin >> l >> r >> x; if (diff[l] >= 0) ans += diff[l] * s; else ans -= -diff[l] * t; if (diff[r + 1] >= 0) ans += diff[r + 1] * s; else ans -= -diff[r + 1] * t; diff[l] += x; if (diff[l] >= 0) ans -= diff[l] * s; else ans += -diff[l] * t; if (r + 1 != n + 1) { diff[r + 1] -= x; if (diff[r + 1] >= 0) ans -= diff[r + 1] * s; else ans += -diff[r + 1] * t; } cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; class A { public: void solveOne(istream &in, ostream &out) { int n, v; in >> n >> v; set<int> s; for (int i = 0; i < n; ++i) { int x; in >> x; for (int j = 0; j < x; ++j) { int y; in >> y; if (v > y) { s.insert(i + 1); } else continue; } } out << s.size() << "\n"; for (auto i : s) out << i << " "; out << "\n"; } void solve(std::istream &in, std::ostream &out) { int tc = 1; for (int i = 0; i < tc; ++i) { solveOne(in, out); } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); A a; std::istream &in(std::cin); std::ostream &out(std::cout); a.solve(in, out); return 0; }
1
//yukicoder@cpp14 //author:luckYrat(twitter:@luckYrat_) //<ここに一言> //せんげん! #include <iostream> #include <cmath> #include <algorithm> #include <iomanip> #include <string> #include <vector> #include <set> #include <stack> #include <queue> #include <map> #include <bitset> #include <cctype> #include <utility> #include <climits> //なまえくーかん! using namespace std; using ll = long long; using P = pair<ll,ll>; //てーすう! const int mod = 1000000007; const int inf = (1<<30)-1; const ll linf = (1LL<<62LL)-1; const double EPS = (1e-10); //でふぁいん! #define anyfill(n,s) setw(n) << setfill(s) #define loop(s) for(int i = 0; s > i; i++) #define rep(i,q) for(int i = 0; (q) > i; i++) #define repp(i,n,q) for(int i = n; (q) > i; i++) #define dep(i,q) for(int i = (q); 0 < i; i--) //みじかく! #define pb push_back #define fir first #define scn second #define ednl endl //いぇすのー! #define YesNo(a) (a?"Yes":"No") #define YESNO(a) (a?"YES":"NO") #define yesno(a) (a?"yes":"no") //きんぼーnほーこー!! P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}}; P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; /* 確認ポイント cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる 計算量は変わらないが楽できるシリーズ min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる */ template <typename T> T gcd(T a,T b){ if(a%b==0)return b; else return gcd(b,a%b); } template <typename T> T lcm(T a,T b){ return a/gcd(a,b)*b; } int dp[200][200]; struct z{ int a,b,c; }; __attribute__((constructor)) void initial() { cin.tie(0); ios::sync_with_stdio(false); } int main(){ int n;cin>>n; vector<int> A(n); for(int i = 0; n > i; i++){ cin>>A[i]; } sort(A.begin(),A.end()); vector<int> Y; for(int i = 1; A[n-1] >= i*i; i++){ if(!(A[n-1]%i)){ Y.push_back(i); if(i*i!=A[n-1])Y.push_back(A[n-1]/i); } } sort(Y.begin(),Y.end()); int ans = 0; for(int i = 0; n > i; i++){ ans += *lower_bound(Y.begin(),Y.end(),A[i])-A[i]; } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto &x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } inline int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } inline int sub(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } inline int mul(int a, int b) { return (int)((long long)a * b % MOD); } inline int binpow(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = mul(res, a); a = mul(a, a); b /= 2; } return res; } inline int inv(int a) { return binpow(a, MOD - 2); } int gcd(int a, int b, int &x, int &y) { if (a == 0) { x = 0, y = 1; return b; } int x1, y1; int d = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } const int N = 1e3 + 5, M = 55, INF = 1e7; int dp[N][M][M], nxt_s[M][26], nxt_t[M][26]; void precalc(string &s, int nxt[M][26]) { int n = s.size(); vector<int> pi(n); for (int i = 2; i < n; ++i) { int j = pi[i - 1]; while (j > 0 && s[j + 1] != s[i]) j = pi[j]; if (s[j + 1] == s[i]) j++; pi[i] = j; } for (int i = 0; i < n; ++i) for (char c = 'a'; c <= 'z'; ++c) { int j = i; while (j > 0 && s[j + 1] != c) j = pi[j]; if (s[j + 1] == c) j++; nxt[i][c - 'a'] = j; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string cs, s, t; cin >> cs >> s >> t; int size = cs.size(), n = s.size(), m = t.size(); cs = '#' + cs; s = '#' + s; t = '#' + t; precalc(s, nxt_s); precalc(t, nxt_t); for (int i = 0; i <= size; ++i) for (int j = 0; j <= n; ++j) for (int k = 0; k <= m; ++k) dp[i][j][k] = -INF; dp[0][0][0] = 0; for (int i = 0; i < size; ++i) for (int j = 0; j <= n; ++j) for (int k = 0; k <= m; ++k) for (char c = 'a'; c <= 'z'; ++c) if (cs[i + 1] == c || cs[i + 1] == '*') { int nxs = nxt_s[j][c - 'a'], nxt = nxt_t[k][c - 'a']; dp[i + 1][nxs][nxt] = max(dp[i + 1][nxs][nxt], dp[i][j][k] + (nxs == n) - (nxt == m)); } int ans = -INF; for (int j = 0; j <= n; ++j) for (int k = 0; k <= m; ++k) ans = max(ans, dp[size][j][k]); cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int t, d, l; vector<int> x; int solve(); int main() { while(1) { cin >> t >> d >> l; if(t + d + l == 0) break; x.assign(t, 0); for(int i = 0; i < t; ++i) cin >> x[i]; cout << solve() << endl; } return 0; } int solve() { int ans = 0, lim = -1; for(int i = 0; i < t; ++i) { if(lim >= i) ++ans; if(x[i] >= l) lim = i + d; } return ans; }
0
#include <bits/stdc++.h> using namespace std; class knight { public: long long int pow, ind, coin; }; bool cmp1(knight k1, knight k2) { return k1.pow < k2.pow; } bool cmp2(knight k1, knight k2) { return k1.ind < k2.ind; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; knight* arr = new knight[n]; for (int i = 0; i < n; ++i) { cin >> arr[i].pow; arr[i].ind = i; } for (int i = 0; i < n; ++i) cin >> arr[i].coin; sort(arr, arr + n, cmp1); multiset<long long int> coins; for (int i = 0; i < n; ++i) { long long int maxC = 0; int j = 0; for (auto it = coins.rbegin(); j < k && it != coins.rend(); ++it, ++j) maxC += *it; coins.insert(arr[i].coin); arr[i].pow = maxC + arr[i].coin; } sort(arr, arr + n, cmp2); for (int i = 0; i < n; ++i) cout << arr[i].pow << " "; delete[] arr; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long dp[1012][1012][2]; int k, t, lmax; char s[1200], in[1200]; long long dfs(int pos, int last, int st, int state) { if (pos < 1) { return st == 1; } if (!state && dp[pos][last][st] != -1) return dp[pos][last][st]; int u = state ? s[pos] - '0' : 9; long long ans = 0; for (int i = 0; i <= u; i++) { if (i == 4 || i == 7) { ans += dfs(pos - 1, pos, (last - pos <= k && last) | st, state && i == u); } else ans += dfs(pos - 1, last, st, state && i == u); } ans %= 1000000007LL; if (state == 0) dp[pos][last][st] = ans; return ans; } int main() { scanf("%d%d", &t, &k); memset(dp, -1, sizeof(dp)); int q; while (t--) { scanf("%s", in); lmax = strlen(in); q = 1; for (int i = lmax - 1; i >= 0; i--) s[q++] = in[i]; long long s1 = dfs(lmax, 0, 0, 1); long long ans = 0; int p = -3000; for (int i = 0; i < lmax; i++) { if (in[i] == '4' || in[i] == '7') { if (i - p <= k) { ans = 1; break; } else p = i; } } scanf("%s", in); lmax = strlen(in); q = 1; for (int i = lmax - 1; i >= 0; i--) s[q++] = in[i]; long long s2 = dfs(lmax, 0, 0, 1); long long q = s2 - s1 + ans; if (q < 0) q += 1000000007; printf("%lld\n", q); } }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; int n, k; vector<int> a; vector<int> group; vector<int> id; int main(int argc, char *argv[]) { cin >> n >> k; a.resize(n + 1); group.resize(k + 1); id.resize(k + 1); int sum = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; sum += a[i]; } int done = 0; unordered_set<int> interesting; int idx = 0; for (int i = 0; i < sum; ++i) { for (int j = 1; j <= k; ++j) { if (group[j] == 1) done++; group[j] = max(0, group[j] - 1); if (group[j] == 0 && idx < n) { idx++; group[j] = a[idx]; id[j] = idx; } } if (done < 1) continue; int d = (int)((double)(100.0 * done) / n + 0.5); for (int j = 1; j <= k; ++j) { if (group[j] > 0 && a[id[j]] - group[j] + 1 == d) interesting.insert(id[j]); } } cout << (int)interesting.size() << endl; return 0; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") const int INF = INT_MAX; const int mod = 1e9 + 7; const long long LINF = LLONG_MAX; using namespace std; int main() { int n; cin >> n; vector<long long> p(n); for (int i = 0; i < (n); i++) cin >> p[i]; for (int i = (1); i < (n); i++) { long long y1 = p[i] - p[0]; vector<bool> u(n); u[0] = true; for (int j = (1); j < (n); j++) { long long y2 = p[j] - p[0]; if (i * y2 == y1 * j) u[j] = true; } long long fp = -1, sp = -1; long long sy, sx; for (int j = (1); j < (n); j++) { if (!u[j]) { if (fp == -1) fp = j, u[fp] = true; else if (sp == -1) sp = j, u[sp] = true, sy = p[sp] - p[fp], sx = sp - fp; else { long long y2 = p[j] - p[fp]; if (sx * y2 == sy * (j - fp)) u[j] = true; } } } bool yes = true; for (int i = 0; i < (n); i++) if (!u[i]) yes = false; if (sp != -1 && sx * y1 != sy * i) yes = false; if (fp == -1) { { cout << "No" << endl; return 0; } } if (yes) { cout << "Yes" << endl; return 0; } } vector<bool> u(n); u[0] = true; long long fp = -1, sp = -1, sy, sx; for (int j = (1); j < (n); j++) { if (!u[j]) { if (fp == -1) fp = j, u[fp] = true; else if (sp == -1) sp = j, u[sp] = true, sy = p[sp] - p[fp], sx = sp - fp; else { long long y2 = p[j] - p[fp]; if (sx * y2 == sy * (j - fp)) u[j] = true; } } } bool yes = true; for (int i = 0; i < (n); i++) if (!u[i]) yes = false; if (yes) { cout << "Yes" << endl; return 0; } cout << "No" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { char mapp[110]; int vis[110]; int sum; while (scanf("%s", mapp) != EOF) { memset(vis, 0, sizeof vis); int len = strlen(mapp); int sum = 0; for (int i = 0; i < len - 1; i++) { if (mapp[i] == 'V' && mapp[i + 1] == 'K') { vis[i] = vis[i + 1] = 1; sum++; i++; } } for (int i = 0; i < len - 1; i++) { if ((mapp[i] == 'V' || mapp[i + 1] == 'K') && vis[i] == 0 && vis[i + 1] == 0) { sum++; break; } } printf("%d\n", sum); } }
1
#include <bits/stdc++.h> using namespace std; const long long inf = 1e10 + 7; const long double eb = 1e-9; const long long mod = 1e9 + 7; template <class T> T add(T v1, T v2, long long mo = mod) { return (v1 % mo + v2 % mo) % mo; } template <class T> T subtract(T v1, T v2, long long mo = mod) { return (v1 % mo - v2 % mo) % mo; } template <class T> T mult(T v1, T v2, long long mo = mod) { return (v1 % mo * v2 % mo) % mo; } template <class T, class P> void build_st(vector<T> &a, vector<P> &segT, long long start, long long end, long long tidx) { if (start == end) { segT[tidx] = a[start]; return; } long long mid = start + (end - start) / 2; build_st(a, segT, start, mid, tidx * 2); build_st(a, segT, mid + 1, end, 2 * tidx + 1); segT[tidx] = segT[tidx * 2] + segT[tidx * 2 + 1]; } template <class T, class P> void update_st(vector<T> &a, vector<T> &segT, long long start, long long end, long long tidx, long long idx, T val) { if (start == end) { segT[tidx] = a[idx] = val; return; } long long mid = start + (end - start) / 2; if (idx > mid) update_st(a, segT, mid + 1, end, 2 * tidx + 1, idx, val); else update_st(a, segT, start, mid, 2 * tidx, idx, val); segT[tidx] = segT[2 * tidx] + segT[2 * tidx + 1]; } template <class T> T query_st(vector<T> &segT, long long start, long long end, long long tidx, long long left, long long right) { if (start > right || end < left) return 0; if (start >= left && end <= right) return segT[tidx]; long long mid = start + (end - start) / 2; T val1 = query_st(segT, start, mid, 2 * tidx, left, right); T val2 = query_st(segT, mid + 1, end, 2 * tidx + 1, left, right); return val1 + val2; } template <class T> void in(vector<T> &a, long long n, long long g = 0) { for (long long i = g; i < n + g; i++) cin >> a[i]; } template <class T> void pv(vector<T> &a) { for (auto i : a) cout << i << " "; cout << "\n"; } template <class T> void pvv(vector<vector<T>> &a) { for (auto i : a) { for (auto j : i) cout << j << " "; cout << "\n"; } } string to_binary(long long n) { string ans = ""; while (n) { ans += (n & 1) + '0'; n >>= 1; } return ans; } long long to_decimal(string s) { long long ans = 0; long long bot = 1; for (long long i = 0; i < s.length(); i++) { ans += (bot * (s[i] - '0')); bot <<= 1; } return ans; } void solver() { long long l, r; cin >> l >> r; if (l == r) { cout << 0 << "\n"; return; } string s = to_binary(l); string p = to_binary(r); while (s.length() < p.length()) { s += "0"; } long long i = s.length() - 1; string ans = ""; while (i >= 0 && s[i] == p[i]) { ans = '0' + ans; i--; } while (i >= 0) { ans = "1" + ans; i--; } cout << to_decimal(ans); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(15); long long t = 1; while (t--) { solver(); } }
4
#include <bits/stdc++.h> using namespace std; const int N = 105; int a[N], num[N]; int main() { int n; cin >> n; bool flag = false; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] != 0) flag = true; } if (flag) { cout << "YES" << endl; int sum = 0, cnt = 0; num[cnt++] = 1; for (int i = 1; i <= n; i++) { while (a[i] == 0) i++; sum += a[i]; if (sum == 0) { sum = a[i]; num[cnt++] = i - 1; num[cnt++] = i; } } num[cnt++] = n; cout << cnt / 2 << endl; for (int i = 0; i < cnt; i += 2) printf("%d %d\n", num[i], num[i + 1]); } else cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long int arr[262144 << 1]; arr aa, bb; long long int *f = aa + 1; long long int *ff = bb + 1; long long int *g; struct Node { Node *le, *ri; long long int del, sum, base; Node() : le(0), ri(0), del(0), sum(0), base(0) {} } * A, *B; Node *buildtree(int l, int r) { Node *ret = new Node(); if (l == r) { ret->base = g[l]; ret->sum = g[l] * ff[l] % 1000000000; } else { int m = l + r >> 1; ret->le = buildtree(l, m); ret->ri = buildtree(m + 1, r); ret->base = (ret->le->base + ret->ri->base) % 1000000000; ret->sum = (ret->le->sum + ret->ri->sum) % 1000000000; } return ret; } inline void up(Node *nd) { nd->sum = (nd->le->sum + nd->ri->sum) % 1000000000; } inline void del1(Node *nd, long long int dd) { nd->del = (nd->del + dd) % 1000000000; nd->sum = (nd->sum + dd * nd->base % 1000000000) % 1000000000; } inline void down(Node *nd) { if (nd->del) { del1(nd->le, nd->del); del1(nd->ri, nd->del); nd->del = 0; } } int Xl, Xr; long long int Xv; void cha(Node *nd, int l, int r) { if (l == r) { nd->sum = Xv * nd->base % 1000000000; return; } int m = l + r >> 1; down(nd); if (Xl <= m) cha(nd->le, l, m); else cha(nd->ri, m + 1, r); up(nd); } void jia(Node *nd, int l, int r) { if (l > Xr || r < Xl) return; if (l >= Xl && r <= Xr) { del1(nd, Xv); return; } int m = l + r >> 1; down(nd); jia(nd->le, l, m); jia(nd->ri, m + 1, r); up(nd); } long long int quer(Node *nd, int l, int r) { if (l > Xr || r < Xl) return 0; if (l >= Xl && r <= Xr) return nd->sum; int m = l + r >> 1; down(nd); return (quer(nd->le, l, m) + quer(nd->ri, m + 1, r)) % 1000000000; } int main() { int n, m, i; scanf("%d%d", &n, &m); f[-1] = 1; f[0] = 0; for (i = 1; i <= n + 2; i++) f[i] = (f[i - 1] + f[i - 2]) % 1000000000; for (i = 1; i <= n; i++) scanf("%I64d", ff + i); g = f; A = buildtree(1, n); g = f - 1; B = buildtree(1, n); for (i = -1; i <= n + 2; i++) if (i & 1) ff[i] = f[i]; else ff[i] = (1000000000 - f[i]) % 1000000000; int cmd; long long int ans; for (i = 1; i <= m; i++) { scanf("%d", &cmd); if (cmd == 1) { scanf("%d%I64d", &Xl, &Xv); cha(A, 1, n); cha(B, 1, n); } if (cmd == 2) { scanf("%d%d", &Xl, &Xr); ans = (ff[Xl - 2] * quer(A, 1, n) % 1000000000 + ff[Xl - 1] * quer(B, 1, n) % 1000000000) % 1000000000; printf("%I64d\n", ans); } if (cmd == 3) { scanf("%d%d%I64d", &Xl, &Xr, &Xv); jia(A, 1, n); jia(B, 1, n); } } return 0; }
5
#include <bits/stdc++.h> #include <math.h> #define rep(i,n) for (int i = 0; i < (n); ++i) #define PI 3.14159265359 using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; ll s = a[0] + a[1], x = 0; for (int i = 2; i < n; i++) x ^= a[i]; if ((s - x) & 1) {cout << -1 << endl; return 0; } ll d = (s - x) / 2; if ((d & x) || (d < 0) || (d > a[0])) { cout << -1 << endl; return 0; } ll m = 1; m = m<<40; ll a0 = d; rep(_, 41) { if (m & x) { if (a0 + m <= a[0]) a0 += m; } m = m>>1; } if (a0 <= 0) { cout << -1 << endl; return 0; } cout << a[0] - a0 << endl; return 0; } /* */
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { long long l, r, d; cin >> l >> r >> d; if (d < l) cout << d << "\n"; else { cout << ((r / d) + 1) * d << "\n"; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int d[70][70][70], ans[70][70][70]; int m, n, r; const int INF = 1 << 30; void floyd(int t) { for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) d[t][i][j] = min(d[t][i][j], d[t][i][k] + d[t][k][j]); } int main() { cin >> n >> m >> r; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) scanf("%d", &d[i][j][k]); floyd(i); } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { ans[1][i][j] = INF; for (int k = 1; k <= m; k++) ans[1][i][j] = min(ans[1][i][j], d[k][i][j]); } for (int i = 2; i <= n; i++) { for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) { ans[i][j][k] = INF; for (int t = 1; t <= n; t++) ans[i][j][k] = min(ans[i][j][k], ans[i - 1][j][t] + ans[1][t][k]); } } int a, b, c; while (r--) { scanf("%d %d %d", &a, &b, &c); c++; if (c >= n) c = n; printf("%d\n", ans[c][a][b]); } return 0; }
4
#include <bits/stdc++.h> struct STUD { int id; bool notified; int msg_left; }; int main() { int n; int sum = 0; scanf("%d", &n); STUD* stud = new STUD[n]; for (int i = 0; i < n; i++) { stud[i].id = i + 1; scanf("%d", &stud[i].msg_left); sum += stud[i].msg_left; stud[i].notified = false; } if ((stud[0].msg_left == 0) || (sum + 1 < n)) { printf("-1"); return 0; } STUD temp; for (int j = 0; j < n; j++) for (int i = 1; i < n - 1; i++) if (stud[i].msg_left < stud[i + 1].msg_left) { temp = stud[i]; stud[i] = stud[i + 1]; stud[i + 1] = temp; } int last = 1; printf("%d\n", n - 1); for (int i = 0; i < n; i++) { while ((stud[i].msg_left > 0) && (last < n)) { stud[i].msg_left--; printf("%d %d\n", stud[i].id, stud[last].id); stud[last].notified = true; last++; } } delete stud; return 0; }
2
#include <bits/stdc++.h> using namespace std; int H, W; string s[509]; int main() { cin >> H >> W; for(int i = 0; i < H; i++) { cin >> s[i]; for(int j = 0; j < W; j++) { cout << (j != W - 1 && (s[i][j] == '#' || j == 0 || i % 2 == 0) ? '#' : '.'); } cout << endl; } cout << endl; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { cout << (j != 0 && (s[i][j] == '#' || j == W - 1 || i % 2 == 1) ? '#' : '.'); } cout << endl; } }
0
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #include <climits> #define rep(i, m, n) for(int i=int(m);i<int(n);i++) #define EACH(i, c) for (auto &(i): c) #define all(c) begin(c),end(c) #define EXIST(s, e) ((s).find(e)!=(s).end()) #define SORT(c) sort(begin(c),end(c)) #define pb emplace_back #define MP make_pair #define SZ(a) int((a).size()) //#define LOCAL 0 //#ifdef LOCAL //#define DEBUG(s) cout << (s) << endl //#define dump(x) cerr << #x << " = " << (x) << endl //#define BR cout << endl; //#else //#define DEBUG(s) do{}while(0) //#define dump(x) do{}while(0) //#define BR //#endif //改造 typedef long long int ll; using namespace std; #define INF (1 << 30) #define INFl (ll)5e15 #define DEBUG 0 //デバッグする時1にしてね #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 //ここから編集する void solve(int n,ll I,ll J){ ll maxL = 1; ll maxH = (1LL << n); ll curL = 1; ll curH = I; vector<int> cmd(n,1);//0がL,1がR // ll cmd = (1 << n) - 1;// while(maxH != 1){ maxL *= 2LL; maxH /= 2LL; if(curH > maxH){ curH -= maxH; }else{ curL = maxL - curL + 1; curH = maxH - curH + 1; } } ll range = (1LL << n); ll targetL = J; ll nowL = curL; // for(int i = n - 1; i >= 0; i--){ for(int i = 0; i < n; i++){ range >>= 1; // if((targetL <= range) ^ (nowL <= range)){ // cmd[i] = 0; // } if(range == 0){ // cout << "err" << endl; } if(targetL <= range){ if(nowL <= range){ }else{ nowL -= range; cmd[i] = 0; } }else{ if(nowL <= range){ targetL -= range; cmd[i] = 0; }else{ nowL -= range; targetL -= range; } } } rep(i,0,n){ // cout << (cmd[i]) ? "R" : "L"; if(cmd[i] == 1){ cout << "R"; }else{ cout << "L"; } } cout << endl; return; } int main() { cin.tie(0); ios::sync_with_stdio(false); while(true){ int n; ll I,J; cin >> n >> I >> J; if(n == 0) break; solve(n,I,J); } return 0; }
0
#include <bits/stdc++.h> using namespace ::std; int main() { int t; cin >> t; while (t--) { long long int l, r; cin >> l >> r; cout << (r / l + 1) * (r / l + 1) * (r % l) + (l - r % l) * (r / l) * (r / l) << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k, sum, t1, t2; string s; cin >> n >> k; cin >> s; sum = k / 2; t1 = sum; t2 = sum; for (int i = 0; i < n; i++) { if ((s[i] == '(') && (t1 > 0)) { t1--; cout << '('; } else if ((s[i] == ')') && (t2 > 0) && (t2 > t1)) { t2--; cout << ')'; } } return 0; }
3
#include<bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(0);cin.tie(0); int n,t=0; cin>>n; long long int a[n+5],ans[2]={0}; for(int i=0 ; i<n ; i++) cin>>a[i]; sort(a,a+n,greater<int>() ); for(int i=0 ; i<n ; i++) { if(a[i]==a[i+1]) ans[t++]=a[i],i++; if(t==2) break; } cout<<ans[0]*ans[1]<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; string s; int mark[N], dp[N][30], c; vector<int> adj[N]; void dfs(int v) { mark[v] = 1; dp[v][s[v - 1] - 'a'] = 1; for (int u : adj[v]) { if (mark[u] == 1) { c = 1; continue; } if (mark[u] == 0) dfs(u); for (int j = 0; j < 26; j++) dp[v][j] = max(dp[v][j], dp[u][j] + (s[v - 1] - 'a' == j)); } mark[v] = 2; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m >> s; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); } for (int i = 1; i <= n; i++) if (mark[i] == 0) dfs(i); if (c) return cout << -1, 0; int ans = 0; for (int i = 1; i <= n; i++) for (int j = 0; j < 26; j++) ans = max(ans, dp[i][j]); cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, cnt = 0; string st1, st2; set<pair<string, string> > v; cin >> n; cnt = n; for (i = 0; i < n; i++) { cin >> st1 >> st2; v.insert(make_pair(st1, st2)); } cout << v.size() << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int x, m, max = -1, fristRow = -1, FristCoulmn = 0, lastRow = 0, lastColumn = 0; cin >> x >> m; char arr[1000][1000]; for (int i = 0; i < x; i++) { for (int j = 0; j < m; j++) { cin >> arr[i][j]; if (arr[i][j] == '*') { if (fristRow == -1) { fristRow = i; FristCoulmn = j; lastRow = i; lastColumn = j; } if (i > lastRow) lastRow = i; if (j > lastColumn) lastColumn = j; if (FristCoulmn > j) FristCoulmn = j; } } } for (int i = fristRow; i <= lastRow; i++) { for (int j = FristCoulmn; j <= lastColumn; j++) { cout << arr[i][j]; } cout << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k, a[210]; cin >> n >> k; for (int(i) = 0; (i) < (n); i++) cin >> a[i]; int res = -(1e9); for (int(R) = 0; (R) < (n); R++) for (int(L) = 0; (L) < (R + 1); L++) { vector<int> v; int sum = 0; for (int i = L; i <= R; i++) { v.push_back(a[i]); sum += a[i]; } sort((v).begin(), (v).end()); if ((int)(v).size() > k) v.resize(k); vector<int> w; for (int(i) = 0; (i) < (n); i++) { if (i < L || i > R) w.push_back(a[i]); } sort((w).begin(), (w).end()); reverse((w).begin(), (w).end()); if ((int)(w).size() > k) w.resize(k); for (int(i) = 0; (i) < (min((int)(v).size(), (int)(w).size())); i++) { if (v[i] < w[i]) { sum -= v[i]; sum += w[i]; } } res = max(res, sum); } cout << res << endl; return 0; }
1
#include<complex> #include<stdio.h> #define F(a,b,c) acos((b*b+c*c-a*a)/2/b/c) int main(){int n;double d,e,f,g,h,i;for(scanf("%d",&n);n--;){scanf("%lf%lf%lf%lf%lf%lf",&d,&g,&e,&h,&f,&i);std::complex<double> a(d,g),b(e,h),c(f,i);float A=abs(b-c),B=abs(c-a),C=abs(a-b),L=F(A,B,C),s=sin(2*L),t=sin(2*F(B,C,A)),u=sin(2*F(C,A,B)),v=s+t+u;printf("%.3f %.3f %.3f\n",(d*s+e*t+f*u)/v,(g*s+h*t+i*u)/v,A/sin(L)/2);}}
0
#include <bits/stdc++.h> const double PI = acos(-1.0); using namespace std; int n, k, u[4005][4005], RSQ[4005][4005], COST[4005][4005], DP[805][4005]; char* buffer = new char[10000]; void dp(int k, int l, int r, int opt_l, int opt_r) { if (l > r) return; int m = (l + r) / 2, idx = -1, mn = 1000000000; for (int i = opt_l; i <= opt_r && i <= m; i++) { if (mn > DP[k - 1][i] + COST[i + 1][m]) { mn = DP[k - 1][i] + COST[i + 1][m]; idx = i; } } DP[k][m] = mn; dp(k, l, m - 1, opt_l, idx); dp(k, m + 1, r, idx, opt_r); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc = 1, i, j; scanf("%d%d", &n, &k); gets(buffer); for (i = 1; i <= n; i++) { gets(buffer); for (j = 1; j <= n; j++) u[i][j] = buffer[(j - 1) * 2] - '0'; } for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) RSQ[i][j] = RSQ[i][j - 1] + u[i][j]; for (i = 1; i <= n; i++) for (j = i + 1; j <= n; j++) COST[i][j] = COST[i][j - 1] + RSQ[j][j] - RSQ[j][i - 1]; for (i = 1; i <= n; i++) DP[1][i] = COST[1][i]; for (i = 2; i <= k; i++) dp(i, 1, n, 1, n); printf("%d\n", DP[k][n]); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long powmod(long long a, long long x, long long mod) { if (x == 0) return a == 0 ? 0 : 1 % mod; if (x % 2) return a * powmod(a, x - 1, mod) % mod; return powmod(a * a % mod, x / 2, mod); } long long invmodp(long long a, long long mod) { return powmod(a, mod - 2, mod); } long long calc(long long k, long long l, long long r, long long p) { if (p == 2) return 1 - k % 2; const long long x = powmod(k % p, powmod(2, l, p - 1), p); if (x == 1) { if (k % 2) return 2; else return powmod(2, r - l + 1, p); } else { const long long y = (powmod(x, powmod(2, r - l + 1, p - 1), p) - 1 + p) % p; const long long z = y * invmodp((x - 1 + p) % p, p) % p; if (k % 2) { const long long g = powmod(2, r - l, p); return z * invmodp(g, p) % p; } return z; } } int main() { int T; cin >> T; while (T--) { long long k, l, r, p; cin >> k >> l >> r >> p; cout << calc(k, l, r, p) << endl; } return 0; }
4
#include <iomanip> #include <iostream> #include <algorithm> using namespace std; int n, w, v; int main() { while (cin >> n >> w, w) { int m = 0, p = 0, d = 0, c[100] = { 0 }; for (int i = 0; i < n; i++) cin >> v, m = max(m, v / w), c[v / w]++; for (int i = 0; i <= m; i++) d += (m - i) * c[i], p = max(p, c[i]); cout << fixed << setprecision(15) << 1.0 * d / m / p + 0.01 << endl; } return 0; }
0
//#define LOCAL #include <fstream> #include <iostream> #include <cmath> #include <algorithm> #include <string> #define rep(i,n) for(int i=0; i<n; i++) using namespace std; int main() { #ifdef LOCAL ifstream in("input.txt"); cin.rdbuf(in.rdbuf()); #endif int n, S; int r[20010]; while (true) { cin >> n >> S; if (n == 0 && S == 0) break; rep(i,n) cin >> r[i]; sort(r, r + n); int ans = 0; rep(i,n) { int needle = S + 1 - r[i]; int* pos = lower_bound(r + i + 1, r + n, needle); ans += (int)((r + n) - pos); } cout << ans << endl; } }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define mp make_pair #define PI pair<ll,ll> #define poly vector<ll> #define mem(a) memset((a),0,sizeof(a)) #define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++) #define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--) #define pb push_back #define fi first #define se second #define SZ(x) ((int)(x.size())) inline char gc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } #define gc getchar inline ll read(){ ll x = 0; char ch = gc(); bool positive = 1; for (; !isdigit(ch); ch = gc()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = gc()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(ll a){ if(a<0){ a=-a; putchar('-'); } if(a>=10)write(a/10); putchar('0'+a%10); } inline void writeln(ll a){write(a); puts("");} inline void wri(ll a){write(a); putchar(' ');} inline ull rnd(){ return ((ull)rand()<<30^rand())<<4|rand()%4; } const int N=5005; int dp[N]; int main(){ #ifdef Brollan freopen("1.in","r",stdin); #endif int n=read(),mod=read(); dp[n]=1; int t=(n-1)/2; For(i,1,(n+1)/2){ Rep(j,n-i,1)dp[j]=(dp[j]+dp[j+i])%mod; } For(i,1,n/2){ Rep(j,n-i,1)dp[j]=(dp[j]+dp[j+i])%mod; } ll ans=0; For(i,1,n)ans+=dp[i]; cout<<ans%mod<<endl; }
0
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int a[3]; for (long long int i = 0; i < 3; i++) cin >> a[i]; long long int ans = 0; sort(a, a + 3); if (a[0] + a[1] <= a[2]) ans = a[0] + a[1]; else { long long int x = min(a[0], (a[0] + a[1] - a[2] + 1) / 2); ans += x; a[0] = a[0] - x; a[1] = a[1] - x; ans += min(a[2], a[0] + a[1]); } cout << ans << "\n"; } return 0; }
1
#include <cstdio> using namespace std; long long v[2005][2005]; const int mod=1e9+7; int main() { long long n,k; scanf("%lld%lld",&n,&k); v[0][0]=1; for(int i=1;i<=n;i++){ v[i][0]=1; for(int j=1;j<=i;j++) v[i][j]=(v[i-1][j-1]+v[i-1][j])%mod; } for(int i=1;i<=k;i++) printf("%lld\n",v[n-k+1][i]*v[k-1][i-1]%mod); return 0; }
0
#include<bits/stdc++.h> #define ll long long #define lc (id * 2) #define rc (id * 2 + 1) #define mid ((l + r) >> 1) #define fi first #define se second using namespace std; const int maxN = 2e5 + 5; int p[maxN]; struct SegmentTree { int e, st[maxN * 4]; void build(int id, int l, int r) { if (l == r){ st[id] = l * 2 - e; return; } build(lc, l, mid); build(rc, mid+1, r); st[id] = p[st[lc]] < p[st[rc]] ? st[lc] : st[rc]; } int query(int id, int l, int r, int x, int y) { if (l == x && y == r) return st[id]; if (y <= mid) return query(lc, l, mid, x, y); if (mid < x) return query(rc, mid+1, r, x, y); int L = query(lc, l, mid, x, mid); int R = query(rc, mid+1, r, mid+1, y); return p[L] < p[R] ? L : R; } } Tree[2]; struct Node { int l, r, p, q, a, b; bool operator < (const Node &x) const { return a > x.a; } } tmp; priority_queue<Node> Q; int n; void calc(int l, int r) { Node res; res.l = l; res.r = r; if (l&1) { res.p = Tree[1].query(1, 1, n/2, (l+1)/2, (r+1)/2); res.q = Tree[0].query(1, 1, n/2, (res.p+2)/2, (r+1)/2); } else { res.p = Tree[0].query(1, 1, n/2, (l+1)/2, r/2); res.q = Tree[1].query(1, 1, n/2, (res.p+2)/2, (r+1)/2); } res.a = p[res.p]; res.b = p[res.q]; Q.push(res); } int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); cin >> n; Tree[0].e = 0; Tree[1].e = 1; for(int i = 1; i <= n; ++i) cin >> p[i]; Tree[0].build(1, 1, n/2); Tree[1].build(1, 1, n/2); calc(1, n); while (!Q.empty()) { tmp = Q.top(); Q.pop(); cout << tmp.a << " " << tmp.b << " "; if (tmp.l < tmp.p) calc(tmp.l, tmp.p-1); if (tmp.p + 1 < tmp.q) calc(tmp.p+1, tmp.q-1); if (tmp.q < tmp.r) calc(tmp.q+1, tmp.r); } return 0; }
0
#include <bits/stdc++.h> const int INF = 0x3f3f3f3f; long long int getint() { long long int ans = 0; int f = 1; char c; while ((c = getchar()) < '0' || c > '9') if (c == '-') f = -f; while (c >= '0' && c <= '9') { ans = ans * 10 + c - '0'; c = getchar(); } return ans * f; } int lowbit(const int &a) { return ((a) & (-a)); } using namespace std; const int MAXN = 100000; int hsh[MAXN + 10], a[MAXN + 10], cnt; int L[MAXN + 10], R[MAXN + 10]; bool cmp(const int &i, const int &j) { return a[i] < a[j]; } int sum(const int c[], int pos) { int ret = 0; while (pos > 0) { ret += c[pos]; pos -= lowbit(pos); } return ret; } void update(int c[], int pos, int num) { while (pos <= cnt) { c[pos] += num; pos += lowbit(pos); } } int main() { int n = getint(); long long int ans = 0; long long int k = getint(); for (int i = 1; i <= n; ++i) { a[i] = getint(); hsh[i] = i; } sort(hsh + 1, hsh + n + 1, cmp); cnt = 1; for (int i = 1; i <= n; ++i, ++cnt) { while (i < n && a[hsh[i]] == a[hsh[i + 1]]) a[hsh[i]] = cnt, ++i; a[hsh[i]] = cnt; } --cnt; int l = 1, r = 2; long long int num = 0; update(L, a[l], 1); for (int i = 2; i <= n; ++i) { update(R, a[i], 1); num += a[i] < a[1]; num += sum(R, cnt) - sum(R, a[i]); } ans = (num > k); while (l < n) { if (l == r) { update(R, a[r], -1); num -= sum(R, a[r] - 1); num -= sum(L, cnt) - sum(L, a[r]); ++r; if (num > k) ++ans; } while (r < n) { long long int q = sum(R, a[r] - 1) + sum(L, cnt) - sum(L, a[r]); if (num - q <= k) break; num -= q; ++ans; update(R, a[r], -1); ++r; } ++l; update(L, a[l], 1); num += sum(R, a[l] - 1); num += sum(L, cnt) - sum(L, a[l]); ans += r - l; } printf("%I64d\n", (long long int)n * (n - 1) / 2 - ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, t; string a; map<string, int> tb; map<pair<int, string>, int> dp; void solve(int ix, string pre) { if (dp.find(make_pair(ix, pre)) != dp.end()) return; dp[make_pair(ix, pre)] = 1; int st; st = ix - 1; if (st > 4) { string temp = a.substr(st, 2); if (pre != temp) { if (tb.find(temp) == tb.end()) tb[temp] = 1; solve(st - 1, temp); } st = ix - 2; if (st > 4) { temp = a.substr(st, 3); if (pre != temp) { if (tb.find(temp) == tb.end()) tb[temp] = 1; solve(st - 1, temp); } } } return; } int main() { cin >> a; n = a.size(); tb.clear(); dp.clear(); solve(n - 1, ""); cout << tb.size() << endl; for (map<string, int>::iterator it = tb.begin(); it != tb.end(); it++) cout << (it->first) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; bool dis(long long int n) { string s = to_string(n); for (long long int i = 0; i < s.length() - 1; i++) { for (long long int j = i + 1; j < s.length(); j++) { if (s[i] == s[j]) return false; } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; n++; while (!dis(n)) { n++; } cout << n << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int i, v[n + 1], x, y, ans = 0; for (i = 1; i <= n; i++) { cin >> v[i]; } for (i = 0; i < m; i++) { cin >> x >> y; ans += min(v[x], v[y]); } cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c ; cin>>a>>b>>c; if(((b*10)+c )%4==0){cout<< "YES" ;} else {cout<< "NO" ; } return 0; }
0
#include <bits/stdc++.h> static inline unsigned long long rdtsc() { unsigned long long d; __asm__ __volatile__("rdtsc" : "=A"(d)); return d; } using namespace std; const int inf = (int)1.01e9; const double eps = 1e-9; const int N = 2; int mod; struct matrix { int a[N][N]; matrix() { memset(a, 0, sizeof a); } matrix(int x) { memset(a, 0, sizeof a); for (int i = 0; (i) < (N); ++i) a[i][i] = x; } inline void out() { fprintf(stderr, "---\n"), fflush(stderr); for (int i = 0; (i) < (N); ++i) for (int j = 0; (j) < (N); ++j) fprintf(stderr, "%d%c", a[i][j], " \n"[j + 1 == N]), fflush(stderr); } }; inline matrix operator*(matrix a, matrix b) { matrix c; for (int i = 0; (i) < (N); ++i) for (int j = 0; (j) < (N); ++j) for (int k = 0; (k) < (N); ++k) c.a[i][j] = (c.a[i][j] + a.a[i][k] * (long long)b.a[k][j]) % mod; return c; } const int NN = 1 << 16; matrix t[2 * NN]; inline matrix get(int l, int r) { l += NN; r += NN; matrix resl(1), resr(1); while (l <= r) { if ((l & 1) == 1) resl = resl * t[l]; if ((r & 1) == 0) resr = t[r] * resr; l = (l + 1) >> 1; r = (r - 1) >> 1; } return resl * resr; } inline matrix mpow(matrix a, long long b) { matrix res(1); while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } const int maxn = (int)2e5 + 10; int a[maxn]; pair<long long, int> e[maxn]; map<long long, int> M; long long y[maxn * 2]; int yc = 0; matrix ooops[maxn]; int n; inline int getVal(long long x) { if (M.find(x) != M.end()) return M[x]; return a[x % n]; } void Do(matrix &res, long long l, long long r) { if (l > r) return; long long bl = l / n; long long br = r / n; if (bl == br) { res = res * get(l % n, r % n); } else { res = res * get(l % n, n - 1); res = res * mpow(get(0, n - 1), (br - bl - 1)); res = res * get(0, r % n); } } int main() { long long k; scanf("%I64d%d", &k, &mod); scanf("%d", &n); for (int i = 0; (i) < (n); ++i) scanf("%d", &a[i]); a[n] = a[0]; for (int i = 0; (i) < (n); ++i) { t[i + NN].a[1][0] = 1; t[i + NN].a[0][1] = a[i]; t[i + NN].a[1][1] = a[i + 1]; } for (int i = NN - 1; i > 0; --i) t[i] = t[2 * i] * t[2 * i + 1]; int m; scanf("%d", &m); for (int i = 0; (i) < (m); ++i) scanf("%I64d%d", &e[i].first, &e[i].second); for (int i = 0; (i) < (m); ++i) M[e[i].first] = e[i].second; for (int i = 0; (i) < (m); ++i) y[yc++] = e[i].first - 1, y[yc++] = e[i].first; sort(y, y + yc); yc = unique(y, y + yc) - y; for (int i = 0; (i) < (yc); ++i) { ooops[i].a[1][0] = 1; ooops[i].a[0][1] = getVal(y[i]); ooops[i].a[1][1] = getVal(y[i] + 1); } matrix res(1); long long x = -1; for (int i = 0; (i) < (yc); ++i) { if (y[i] > k - 1) break; Do(res, x + 1, y[i] - 1); res = res * ooops[i]; x = y[i]; } Do(res, x + 1, k - 1); printf("%d\n", res.a[1][0] % mod); return 0; }
1
#include <bits/stdc++.h> using namespace std; int povernut(int x) { int res = 0; while (x) { res = res * 10 + x % 10; x /= 10; } return res; } int main(int argc, char *argv[]) { int a, b; scanf("%d%d", &a, &b); int res = a + povernut(b); printf("%d", res); return 0; }
1
#include <iostream> using namespace std; string s; int a[27]; int main() { cin >> s; for(int i = 0; i < s.length(); i++) a[s[i] - 'a']++; for(int i = 0; i < 27; i++) if (a[i] % 2) { cout << "No"; return 0; } cout << "Yes"; return 0; }
0
#include <iostream> #include <map> #include <algorithm> using namespace std; int main(void){ pair<int,char> A[5]; for(int f=0;f!=1;){ for(int i=0,a,b;i<5;++i){ cin>>a>>b; if(a==0&&b==0){f=1;break;} A[i].first=a+b; A[i].second=(char)(i+'A'); if(i==4){ sort(&A[0],&A[5],greater<pair<int,char> >()); cout<<A[0].second<<" "<<A[0].first<<endl; } } } return 0; }
0
#include <bits/stdc++.h> #pragma warning(disable : 4996) int main() { int a, b, n; scanf("%d%d%d", &a, &b, &n); double min_time; for (int i = 0; i < n; i++) { int x, y, v; double time; scanf("%d%d%d", &x, &y, &v); time = (double)(sqrt(pow((double)(x - a), 2) + pow((double)(y - b), 2)) / v); if (i == 0) min_time = time; else if (min_time > time) min_time = time; } printf("%lf", min_time); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long r, g, b, res = 0, demores = 0, dr, dg, db; cin >> r >> g >> b; int k = 0; res = r / 3 + g / 3 + b / 3; for (int i = 0; i < 3; i++) { if (r > 0 and g > 0 and b > 0) { demores = (r / 3) + (g / 3) + (b / 3) + k; res = max(res, demores); r--; g--; b--; k++; } } cout << res << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int dp[100][100]; vector<long long> can; int test; int r(long long a, long long b) { if ((a == 0) || (b == 0)) return 0; int k = r(b % a, a); if (k == 0) return 1; long long v = (b % a); v += a; long long c = (b - v) / a; if (a & 1) { int res = (c & 1); return res; } c %= (a + 1); if (c == a) return 1; int res = (c & 1); return res; } int brut(int a, int b) { if (dp[a][b] != -1) return dp[a][b]; if ((a == 0) || (b == 0)) return 0; int res = brut(b % a, a); if (res == 0) return dp[a][b] = 1; int v = a; while (b - v >= 0) { int nb = b - v; res = min(res, brut(min(nb, a), max(nb, a))); v *= a; } res ^= 1; return dp[a][b] = res; } int main() { int t; scanf("%d", &t); while (t--) { long long a, b; cin >> a >> b; if (a > b) swap(a, b); int res = r(a, b); if (res) printf("First\n"); else printf("Second\n"); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; bool is_prime(long long x) { if (x <= 1) return false; for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } bool is_palindrome(string s1) { int l = s1.length(); for (int i = 0; i < l / 2; i++) if (s1[i] != s1[l - i - 1]) return false; return true; } unsigned long long C(long long n, long long k) { if (k == 0) return 1; return (n * C(n - 1, k - 1)) / k; } long long modular_pow(long long base, long long exponent, int modulus) { long long result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } long long binaryToDec(string number) { long long result = 0, pow = 1; for (int i = number.length() - 1; i >= 0; --i, pow <<= 1) result = (result + (number[i] - '0') * pow) % 1000003; return result; } long long binaryToDec2(string number) { long long result = 0, pow = 1; int l = number.length(); for (int i = 0; i < l; ++i, pow <<= 1) result = (result + (number[i] - '0') * pow) % 1000003; return result; } string decimalToBin(int number) { if (number == 0) return "0"; if (number == 1) return "1"; if (number % 2 == 0) return decimalToBin(number / 2) + "0"; else return decimalToBin(number / 2) + "1"; } unsigned long long GCD(unsigned long long a, unsigned long long b) { return b == 0 ? a : GCD(b, a % b); } unsigned long long LCM(unsigned long long i, unsigned long long j) { return (i * j) / GCD(i, j); } int cntMask(int mask) { int ret = 0; while (mask) { if (mask % 2) ret++; mask /= 2; } return ret; } int getBit(int mask, int i) { return ((mask >> i) & 1) == 1; } int setBit(int mask, int i, int value = 1) { return (value) ? mask | (1 << i) : (mask & ~(1 << i)); } unsigned long long mystoi(string s) { unsigned long long ans = 0; unsigned long long po = 1; for (int i = s.length() - 1; i >= 0; i--) { ans += (s[i] - '0') * po; po *= 10; } return ans; } string conv(int i) { string t = ""; while (i) { t += '0' + (i % 10); i /= 10; } return t; } bool hasZero(int i) { if (i == 0) return true; while (i) { if (i % 10 == 0) return true; i /= 10; } return false; } bool isSortedAc(int a[], int n) { for (int i = 0; i < int(n - 1); i++) if (a[i] > a[i + 1]) return false; return true; } bool isSortedDec(int a[], int n) { for (int i = 0; i < int(n - 1); i++) if (a[i] < a[i + 1]) return false; return true; } bool cmp(unsigned long long i, unsigned long long j) { return i > j; } void C() { int n, p; string s; cin >> n >> p >> s; p--; int st = 1000000 + 5, f = -1; int ans = 0; if (p >= n / 2) { for (int i = n / 2; i < n; i++) { if (s[i] != s[n - i - 1]) { int t1 = abs(s[i] - s[n - i - 1]); int t2 = abs(abs(s[i] - 'a') + abs(26 - (s[n - i - 1] - 'a'))); int t3 = abs(abs(s[n - 1 - i] - 'a') + abs(26 - (s[i] - 'a'))); int t4 = min(t1, min(t2, t3)); ans += t4; st = min(st, i); f = max(f, i); } } } else { for (int i = n / 2 - 1; i >= 0; i--) { if (s[i] != s[n - i - 1]) { int t1 = abs(s[i] - s[n - i - 1]); int t2 = abs(abs(s[i] - 'a') + abs(26 - (s[n - i - 1] - 'a'))); int t3 = abs(abs(s[n - 1 - i] - 'a') + abs(26 - (s[i] - 'a'))); int t4 = min(t1, min(t2, t3)); ans += t4; st = min(st, i); f = max(f, i); } } } if (st == 1000000 + 5 && f == -1) { cout << 0 << endl; return; } int temp = abs(f - st); temp += min(abs(p - st), abs(p - f)); ans += temp; cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); C(); return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize(fast - math) using namespace std; long long powmod(long long a, long long b, long long mod) { if (b == 0 || a == 1) { if (mod == 1) return 0; else return 1; } if (b % 2 == 0) { long long k = powmod(a, b / 2, mod); return (k * k) % mod; } else { long long k = powmod(a, b / 2, mod); return ((k * k) % mod * a) % mod; } } long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; if (a > b) return gcd(a % b, b); else return gcd(b % a, a); } int prime(int p) { for (int i = 2; i * i <= p; i++) { if (p % i == 0 && i < p) return i; } return 1; } long long sqr(long long i) { return i * i; } void r(long long &a) { cin >> a; } void r(long double &a) { cin >> a; } void r(long long &a, long long &b) { cin >> a >> b; } void r(long double &a, long double &b) { cin >> a >> b; } void r(long long &a, long long &b, long long &c) { cin >> a >> b >> c; } void r(long double &a, long double &b, long double &c) { cin >> a >> b >> c; } void r(long long &a, long long &b, long long &c, long long &d) { cin >> a >> b >> c >> d; } void r(long double &a, long double &b, long double &c, long double &d) { cin >> a >> b >> c >> d; } void r(long long &a, long long &b, long long &c, long long &d, long long &e) { cin >> a >> b >> c >> d >> e; } void r(long double &a, long double &b, long double &c, long double &d, long double &e) { cin >> a >> b >> c >> d >> e; } void r(long long &a, long long &b, long long &c, long long &d, long long &e, long long &f) { cin >> a >> b >> c >> d >> e >> f; } void r(long double &a, long double &b, long double &c, long double &d, long double &e, long double &f) { cin >> a >> b >> c >> d >> e >> f; } void r(vector<long long> &a) { for (long long i = 0; i < a.size(); i++) r(a[i]); } void r(vector<vector<long long>> &a) { for (long long i = 0; i < a.size(); i++) r(a[i]); } void w(long long a) { cout << a << "\n"; } void w(int a) { cout << a << "\n"; } void w(long double a) { cout << a << "\n"; } void w(char a) { cout << a; } void w(long long a, long long b) { cout << a << " " << b << "\n"; } void w(long double a, long double b) { cout << a << " " << b << "\n"; } void w(long long a, long long b, long long c) { cout << a << " " << b << " " << c << "\n"; } void w(long double a, long double b, long double c) { cout << a << " " << b << " " << c << "\n"; } void w(long long a, long long b, long long c, long long d) { cout << a << " " << b << " " << c << " " << d << "\n"; } void w(long double a, long double b, long double c, long double d) { cout << a << " " << b << " " << c << " " << d << "\n"; } void w(vector<long long> a) { for (long long i = 0; i < a.size(); i++) cout << a[i] << " "; cout << "\n"; } void w(vector<vector<long long>> a) { for (long long i = 0; i < a.size(); i++) w(a[i]); cout << "\n"; } void r(pair<long long, long long> &a) { cin >> a.first >> a.second; } void w(pair<long long, long long> a) { cout << a.first << " " << a.second << "\n"; } void r(vector<pair<long long, long long>> &a) { for (long long i = 0; i < a.size(); i++) r(a[i]); } void w(vector<pair<long long, long long>> a) { for (long long i = 0; i < a.size(); i++) w(a[i]); cout << "\n"; } void r(string &a) { cin >> a; } void r(char &a) { cin >> a; } void w(string a) { cout << a << "\n"; } void sort(vector<long long> &a) { sort(a.begin(), a.end()); } void sort(vector<pair<long long, long long>> &a) { sort(a.begin(), a.end()); } void rev(vector<long long> &a) { reverse(a.begin(), a.end()); } void rev(vector<pair<long long, long long>> &a) { reverse(a.begin(), a.end()); } void rev(string &a) { reverse(a.begin(), a.end()); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } void solve(int ppppppppp = 1) { long long n, k; r(n, k); long long pow1 = 1; for (long long i = 0; i < k; i++) pow1 *= 10; w(lcm(pow1, n)); return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tututu; tututu = 1; for (long long qwerty = 0; qwerty < tututu; qwerty++) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<int> A; vector<int> C; vector<long long> DP(6000, 0); vector<long long> DP2(6000, 0); int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int u; cin >> u; A.push_back(u); } C = A; sort(C.begin(), C.end()); for (int i = 0; i < n; i++) DP[i] = abs(A[0] - C[i]); for (int i = 1; i < n; i++) DP[i] = min(DP[i - 1], DP[i]); for (int i = 1; i < n; i++) { DP2 = DP; DP[0] = DP2[0] + abs(A[i] - C[0]); for (int j = 1; j < n; j++) DP[j] = min(DP[j - 1], DP2[j] + abs(C[j] - A[i])); } cout << DP[n - 1] << endl; cin >> n; return 0; }
3
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 99999999999999999 #define MOD 1000000007 //#define EPS 0.000000001 using namespace std; #define EPS 0.000000001 #define NUM 105 struct Point{ Point(){ x = y = 0; } Point(int arg_x,int arg_y){ x = arg_x; y = arg_y; } ll x,y; }; typedef Point Vector; struct Data{ Data(ll arg_Time,ll arg_Vx,ll arg_Vy){ Time = arg_Time; Vx = arg_Vx; Vy = arg_Vy; } ll Time,Vx,Vy; }; struct Info{ Point pos_First; string name; vector<Data> V; }; struct State{ State(int arg_robo_A,int arg_robo_B,double arg_Time){ robo_A = arg_robo_A; robo_B = arg_robo_B; Time = arg_Time; } bool operator<(const struct State &arg) const{ return Time > arg.Time; //時刻の昇順(PQ) } int robo_A,robo_B; double Time; }; int N; ll T,R; bool is_adj[NUM][NUM],is_Same[NUM]; Info info[NUM]; priority_queue<State> Q; int calc_dist(Point A,Point B){ return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y); } void calc_Cross_Time(int robo_A,int robo_B){ Point pos_A = info[robo_A].pos_First; Point pos_B = info[robo_B].pos_First; Vector v_A = Vector(info[robo_A].V[0].Vx,info[robo_A].V[0].Vy); Vector v_B = Vector(info[robo_B].V[0].Vx,info[robo_B].V[0].Vy); Vector next_v_A,next_v_B; int index_A = 0,index_B = 0; ll current_Time = 0,next_Time; while(true){ next_v_A = v_A; next_v_B = v_B; //次のイベントはどちらが早いか、または同時か if( (index_A < info[robo_A].V.size()-1 && info[robo_A].V[index_A].Time < info[robo_B].V[index_B].Time)){ //Aが早い next_Time = info[robo_A].V[index_A].Time; next_v_A.x = info[robo_A].V[index_A+1].Vx; next_v_A.y = info[robo_A].V[index_A+1].Vy; index_A++; }else if((index_B < info[robo_B].V.size()-1 && info[robo_B].V[index_B].Time < info[robo_A].V[index_A].Time)){ //Bが早い next_Time = info[robo_B].V[index_B].Time; next_v_B.x = info[robo_B].V[index_B+1].Vx; next_v_B.y = info[robo_B].V[index_B+1].Vy; index_B++; }else{ //同時 next_Time = info[robo_A].V[index_A].Time; if(index_A+1 <= info[robo_A].V.size()-1){ next_v_A.x = info[robo_A].V[index_A+1].Vx; next_v_A.y = info[robo_A].V[index_A+1].Vy; index_A++; } if(index_B+1 <= info[robo_B].V.size()-1){ next_v_B.x = info[robo_B].V[index_B+1].Vx; next_v_B.y = info[robo_B].V[index_B+1].Vy; index_B++; } } //判別式 ll a = (v_A.x-v_B.x)*(v_A.x-v_B.x)+(v_A.y-v_B.y)*(v_A.y-v_B.y); ll b = 2*((pos_A.x-pos_B.x)*(v_A.x-v_B.x)+(pos_A.y-pos_B.y)*(v_A.y-v_B.y)); ll c = (pos_A.x-pos_B.x)*(pos_A.x-pos_B.x)+(pos_A.y-pos_B.y)*(pos_A.y-pos_B.y)-R*R; ll D = b*b-4*a*c; if(a != 0 && D >= 0){ //解があるならそれを求める:current_Time-next_Timeの間にあれば、それをQにpush double A = a,B = b, C = c; double CURRENT = current_Time,NEXT = next_Time; double t_1 = CURRENT+(-B-sqrt(B*B-4*A*C))/(2*A); double t_2 = CURRENT+(-B+sqrt(B*B-4*A*C))/(2*A); if(t_1 > CURRENT+EPS && t_1+EPS < NEXT){ Q.push(State(robo_A,robo_B,t_1)); } if(fabs(t_1-t_2) > EPS && t_2 > CURRENT+EPS && t_2+EPS < NEXT){ Q.push(State(robo_A,robo_B,t_2)); } } if(index_A == info[robo_A].V.size()-1 && index_B == info[robo_B].V.size()-1){ break; } pos_A.x += v_A.x*(next_Time-current_Time); pos_A.y += v_A.y*(next_Time-current_Time); pos_B.x += v_B.x*(next_Time-current_Time); pos_B.y += v_B.y*(next_Time-current_Time); current_Time = next_Time; v_A = next_v_A; v_B = next_v_B; } } void func(){ for(int i = 0; i < NUM; i++){ info[i].V.clear(); } int base_robot = 0; ll Time,Vx,Vy; for(int i = 0; i < N; i++){ cin >> info[i].name; scanf("%lld %lld %lld",&Time,&info[i].pos_First.x,&info[i].pos_First.y); while(true){ scanf("%lld %lld %lld",&Time,&Vx,&Vy); info[i].V.push_back(Data(Time,Vx,Vy)); if(Time == T)break; } } is_Same[base_robot] = true; for(int i = 1; i < N; i++){ is_Same[i] = false; } for(int i = 0; i < N; i++){ for(int k = 0; k < N; k++){ is_adj[i][k] = (i==k); } } //最初の位置関係 for(int a = 0; a < N-1; a++){ for(int b = a+1; b < N; b++){ //最初の距離 ll first_dist = calc_dist(info[a].pos_First,info[b].pos_First); if(first_dist <= R*R){ is_adj[a][b] = true; is_adj[b][a] = true; } } } for(int mid = 0; mid < N; mid++){ for(int start = 0; start < N; start++){ if(!is_adj[start][mid])continue; for(int goal = 0; goal < N; goal++){ if(!is_adj[mid][goal])continue; is_Same[goal] = is_Same[start]|is_Same[mid]; } } } for(int a = 0; a < N-1; a++){ for(int b = a+1; b < N; b++){ calc_Cross_Time(a,b); } } while(!Q.empty()){ if(is_adj[Q.top().robo_A][Q.top().robo_B]){ is_adj[Q.top().robo_A][Q.top().robo_B] = false; is_adj[Q.top().robo_B][Q.top().robo_A] = false; }else{ is_adj[Q.top().robo_A][Q.top().robo_B] = true; is_adj[Q.top().robo_B][Q.top().robo_A] = true; if(is_Same[Q.top().robo_A] == is_Same[Q.top().robo_B]){ Q.pop(); continue; } int robo_id; if(is_Same[Q.top().robo_A]){ robo_id = Q.top().robo_B; }else{ robo_id = Q.top().robo_A; } is_Same[robo_id] = true; queue<int> calc_Q; calc_Q.push(robo_id); while(!calc_Q.empty()){ for(int i = 0; i < N; i++){ if(is_adj[calc_Q.front()][i] == true && is_Same[i] ==false){ is_Same[i] = true; calc_Q.push(i); } } calc_Q.pop(); } } Q.pop(); } vector<string> ans; for(int i = 0; i < N; i++){ if(is_Same[i]){ ans.push_back(info[i].name); } } sort(ans.begin(),ans.end()); for(int i = 0; i < ans.size(); i++){ printf("%s\n",ans[i].c_str()); } } int main(){ while(true){ scanf("%d %lld %lld",&N,&T,&R); if(N == 0 && T == 0 && R == 0)break; func(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long a, m, t; long long fai(long long n) { long long sum = n; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) sum = sum / i * (i - 1); while (n % i == 0) n /= i; } if (n > 1) sum = sum / n * (n - 1); return sum; } long long gcd(long long x, long long y) { if (y == 0) return x; else return gcd(y, x % y); } int main() { cin >> t; while (t--) { cin >> a >> m; if (gcd(a, m) == 1) cout << fai(m) << endl; else cout << fai(m / gcd(a, m)) << endl; } }
4
#include <bits/stdc++.h> long long p, n, h[500000], r[5000000], e[500000]; char s[500000]; inline bool ispalind(long long a, long long b) { return (h[b] - h[a - 1] * e[b - a + 1]) * e[a - 1] == r[b] - r[a - 1]; } const long long fac = 129581259982959; signed main() { scanf("%lld%s", &p, s + 1); n = strlen(s + 1); e[0] = 1; for (long long i = 1; i <= n; i++) e[i] = fac * e[i - 1]; h[0] = r[0] = 1; long long cur = n; for (long long i = 1; i <= n; i++) { h[i] = h[i - 1] * fac + s[i]; r[i] = r[i - 1] + e[i - 1] * s[i]; if ((i >= p && ispalind(i - p + 1, i)) || (i > p && ispalind(i - p, i))) { cur = i; break; } } bool ok = false; for (long long i = cur; i > 0; i--) { for (char c = s[i] + 1; c <= 'z'; c++) { s[i] = c; h[i] = h[i - 1] * fac + s[i]; r[i] = r[i - 1] + e[i - 1] * s[i]; if ((i >= p && ispalind(i - p + 1, i)) || (i > p && ispalind(i - p, i))) continue; ok = true; break; } if (ok) { cur = i; break; } } if (!ok) { puts("Impossible"); return 0; } for (long long i = cur + 1; i <= n; i++) for (char c = 'a'; c <= 'z'; c++) { s[i] = c; h[i] = h[i - 1] * fac + s[i]; r[i] = r[i - 1] + e[i - 1] * s[i]; if ((i >= p && ispalind(i - p + 1, i)) || (i > p && ispalind(i - p, i))) continue; break; } puts(s + 1); return 0; }
4
#include <bits/stdc++.h> using namespace std; string manacher(string s) { long long n = s.size(); vector<long long> d1(n); for (long long i = 0, l = 0, r = -1; i < n; i++) { long long k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } vector<long long> d2(n); for (long long i = 0, l = 0, r = -1; i < n; i++) { long long k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1); while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) { k++; } d2[i] = k--; if (i + k > r) { l = i - k - 1; r = i + k; } } long long j1 = -1; for (long long i = 0; i < n; i++) { if (d1[i] == i + 1) { j1 = i; } } long long j2 = -1; for (long long i = 0; i < n; i++) { if (i == d2[i] && (d2[i] != 0)) { j2 = i; } } string r1, r2; if (j2 != -1) { r1 = s.substr(0, j2 * 2); } if (j1 != -1) { r2 = s.substr(0, 2 * j1 + 1); } if (r1.size() > r2.size()) return r1; else return r2; } int main() { long long tc; cin >> tc; while (tc--) { string a; cin >> a; long long n = a.size(); long long j = -1; string st; for (long long i = 0; i < n / 2; i++) { if (a[i] != a[n - 1 - i]) { j = i; break; } else { st += a[i]; } } if (j == -1) { cout << a << "\n"; continue; } string en = st; reverse(en.begin(), en.end()); string md; for (long long i = j; i <= n - j - 1; i++) { md += a[i]; } string x = manacher(md); string mdr = md; reverse(mdr.begin(), mdr.end()); string y = manacher(mdr); string result = ""; if (st.size() > 0) result += st; if (x.size() > y.size()) result += x; else result += y; if (en.size() > 0) result += en; cout << result << "\n"; } }
4
#include <bits/stdc++.h> using namespace std; pair<long long, int> a[200005]; vector<int> v[200005]; long long hm[200005]; priority_queue<int> pq; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i].first); } for (int i = 1; i <= n; i++) { scanf("%d", &a[i].second); } sort(a + 1, a + n + 1); int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i].first != a[i - 1].first) { v[++cnt].push_back(a[i].second); hm[cnt] = a[i].first; } else { v[cnt].push_back(a[i].second); } } long long ans = 0, sum = 0; for (int i = 1; i <= cnt; i++) { while (!pq.empty() && hm[i - 1] < hm[i]) { sum -= pq.top(); pq.pop(); ans += sum; hm[i - 1]++; } int sizee = v[i].size() - 1; for (int j = 0; j < sizee; j++) { sum += v[i][j]; pq.push(v[i][j]); } if (pq.empty()) continue; if (v[i][sizee] >= pq.top()) { ans += sum; } else { sum -= pq.top(); pq.pop(); sum += v[i][sizee]; pq.push(v[i][sizee]); ans += sum; } hm[i]++; } int num = 0; while (!pq.empty()) { ans += (long long)pq.top() * num; pq.pop(); num++; } printf("%I64d\n", ans); return 0; }
4
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <complex> #include <cstring> #include <cstdlib> #include <string> #include <cmath> #include <cassert> #include <queue> #include <set> #include <map> #include <valarray> #include <bitset> #include <stack> using namespace std; #define REP(i,n) for(int i=0;i<(int)n;++i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1<<29; enum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT }; template <class T> class dice { public: int id[6]; dice() {} dice(T *v) { id[TOP] = 2; id[FRONT] = 0; id[LEFT] = 4; id[RIGHT] = 1; id[BACK] = 5; id[BOTTOM] = 3; REP(i,6) var[i] = v[i]; } T& operator[] (int f) { return var[id[f]]; } const T& operator[] (int f) const { return var[id[f]]; } void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); } void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); } void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); } vector<dice> all_rolls() { vector<dice> ret; for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k) for (int i = 0; i < 4; roll_z(), ++i) ret.push_back(*this); return ret; } private: void roll(FACE a, FACE b, FACE c, FACE d) { int tmp = id[a]; id[a] = id[b]; id[b] = id[c]; id[c] = id[d]; id[d] = tmp; } T var[6]; }; int n; vector<dice<int> > vd[4]; dice<int> nowd[4]; int solve(int now) { if (now == n) { int res = 0; REP(i, 6) { int table[24] = {}; REP(j, n) { table[nowd[j][i]]++; } res += n - *max_element(table, table+24); } // REP(i, n) { // REP(j, 6) // cout << nowd[i].var[j] << " "; // cout << endl; // } // cout << res << endl; return res; } int res = INF; FOR(it, vd[now]) { nowd[now] = *it; res = min(res, solve(now+1)); } return res; } int main() { while(cin>>n,n) { dice<int> di[n]; map<string, int> mp; int num = 0; REP(i, n) { int a[6]; REP(j, 6) { string s; cin >> s; int id; if (mp.count(s)) id = mp[s]; else id = mp[s] = num++; a[j] = id; } di[i] = dice<int>(a); } REP(i, n) vd[i] = di[i].all_rolls(); if (n == 1) { cout << 0 << endl; continue; } cout << solve(0) << endl; } }
0
#include <bits/stdc++.h> using namespace std; int K, n[4], t[4]; int main() { ios::sync_with_stdio(false); cin >> K; for (int i = 1; i <= 3; ++i) { cin >> n[i]; } for (int i = 1; i <= 3; ++i) { cin >> t[i]; } multiset<int> mySet[4]; for (int i = 1; i <= 3; ++i) { for (int j = 1; j <= n[i]; ++j) { mySet[i].insert(0); } } for (int k = 1; k <= K; ++k) { int cand = *mySet[1].begin(); int sum = t[1]; for (int i = 2; i <= 3; ++i) { int currCand = *mySet[i].begin(); int x = cand + sum; if (x < currCand) { cand += currCand - x; } sum += t[i]; } sum = 0; for (int i = 1; i <= 3; ++i) { sum += t[i]; mySet[i].erase(mySet[i].begin()); mySet[i].insert(cand + sum); } } auto it = mySet[3].end(); --it; cout << *it << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int extgcd(int a, int b, int &x, int &y) { int d = a; x = 1; y = 0; if (b) d = extgcd(b, a % b, y, x), y -= a / b * x; return d; } int modinv(int a) { int x, y; extgcd(a, MOD, x, y); return (x + MOD) % MOD; } int main() { int n, k, a[2010], b[2010]; long long c[2010]; cin >> n >> k; for (int i = 0; i < (int)(n); ++i) cin >> a[i]; if (k == 0) copy(a, a + n, b); else { --k; fill_n(b, n, 0); c[0] = 1; for (int i = 1; i < (int)(n); ++i) c[i] = c[i - 1] * (k + i) % MOD * modinv(i) % MOD; for (int i = 0; i < (int)(n); ++i) for (int j = 0; j < (int)(i + 1); ++j) b[i] = (b[i] + a[j] * c[i - j]) % MOD; } for (int i = 0; i < (int)(n); ++i) cout << b[i] << (i == n - 1 ? "\n" : " "); }
5
#include <bits/stdc++.h> using namespace std; vector<long long> graph[500005]; bool visited[500005]; void dfs(long long s) { visited[s] = true; for (long long i = 0; i < graph[s].size(); i++) { long long u = graph[s][i]; if (visited[u] == false) { dfs(u); } } cout << s << " "; } int main() { std::ios::sync_with_stdio(false); long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; graph[a].push_back(b); } for (long long i = 1; i <= n; i++) if (visited[i] == false) dfs(i); }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m, div = 0, d, s; cin >> n >> m; div = n / m; s = div + 1; if (m % n == 0) for (int i = 0; i < m; i++) cout << div << " "; else { d = n - (m * div); for (int i = 0; i < m; i++) { if (i >= d) cout << div << " "; else if (i < d) cout << s << " "; } } return 0; }
1
#include<bits/stdc++.h> using namespace std; int n; string s[1005]; int main() { scanf("%d%*d",&n); for(int i=0;i<n;i++) cin>>s[i]; sort(s,s+n); for(int i=0;i<n;i++) { cout<<s[i]; } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; cout<<( (n==0) ? 1: 0); }
0
#include<bits/stdc++.h> #define _ ios_base::sync_with_stdio(0);cin.tie(0); #define REP(i,n) for(int i=0;i<(int)(n);i++) using namespace std; int main(){ _; int n,m; while(cin>>n>>m,(n|m)!=0){ map<int,int> h,w; vector<int> v; int tmp; REP(i,n){ cin>>tmp; v.push_back(0); for(int &a:v){ a+=tmp; h[a]++; } } v.clear(); REP(i,m){ cin>>tmp; v.push_back(0); for(int &a:v){ a+=tmp; w[a]++; } } int sum=0; for(auto hh:h){ if(w[hh.first]>0){ sum+=w[hh.first]*hh.second; } } cout<<sum<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; for (int z = 0; z < t; ++z) { long long n; cin >> n; long long k = 0; long long cur = 0; long long m = (n + 2) / 3; while (cur + (1ll << (2 * k)) < m) { cur += (1ll << (2 * k)); k++; } m -= cur; vector<int> b; m--; while (m > 0) { b.push_back(m % 2); m /= 2; } while (b.size() < 2 * k) b.push_back(0); reverse(b.begin(), b.end()); vector<int> b1(2 * k), b2(2 * k); for (int i = 0; i < k; ++i) { if (b[i * 2] == 0 && b[i * 2 + 1] == 0) { b1[i * 2] = 0; b1[i * 2 + 1] = 0; b2[i * 2] = 0; b2[i * 2 + 1] = 0; } else if (b[i * 2] == 0 && b[i * 2 + 1] == 1) { b1[i * 2] = 1; b1[i * 2 + 1] = 0; b2[i * 2] = 1; b2[i * 2 + 1] = 1; } else if (b[i * 2] == 1 && b[i * 2 + 1] == 0) { b1[i * 2] = 1; b1[i * 2 + 1] = 1; b2[i * 2] = 0; b2[i * 2 + 1] = 1; } else if (b[i * 2] == 1 && b[i * 2 + 1] == 1) { b1[i * 2] = 0; b1[i * 2 + 1] = 1; b2[i * 2] = 1; b2[i * 2 + 1] = 0; } } long long ans; if (n % 3 == 1) { ans = 1; for (int i = 0; i < 2 * k; ++i) { ans *= 2; ans += b[i]; } } else if (n % 3 == 2) { ans = 2; for (int i = 0; i < 2 * k; ++i) { ans *= 2; ans += b1[i]; } } else if (n % 3 == 0) { ans = 3; for (int i = 0; i < 2 * k; ++i) { ans *= 2; ans += b2[i]; } } cout << ans << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const bool testcase = 0; const long long int mod1 = 1000000007; const long long int mod2 = 998244353; inline long long int md(long long int x, long long int M = mod1) { x %= M; return ((x < 0) ? (x + M) : x); } bool is_prime(long long int n) { if (n < 2) return false; long long int rt = sqrt(n); if (rt < 2) return true; if (n % 2 == 0) return false; if (rt < 3) return true; if (n % 3 == 0) return false; for (int i = 5; i <= rt; i += 4 - i % 6 / 2) { if (rt < i) return true; if (n % i == 0) return false; } return true; } template <typename TAIL> inline void inp(TAIL& T) { cin >> T; } template <typename HEAD, typename... TAIL> inline void inp(HEAD& H, TAIL&... T) { cin >> H; inp(T...); } template <typename T> inline bool in_range(T a, T l, T r) { return (a >= l && a <= r); } template <typename T, typename U> inline istream& operator>>(istream& in, pair<T, U>& a) { in >> a.first >> a.second; return in; } template <typename T> inline istream& operator>>(istream& in, vector<T>& a) { for (auto& x : a) in >> x; return in; } void solve(); int main() { std::chrono::time_point<std::chrono::high_resolution_clock> start, end; start = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(0); cin.tie(NULL); int _T_ = 1; if (testcase) { cin >> _T_; } while (_T_--) { solve(); } end = std::chrono::high_resolution_clock::now(); long long int elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start) .count(); cerr << "\nElapsed Time: " << elapsed_time << "ms\n"; return 0; } const long long int N = 5555; vector<long long int> adj[N]; vector<long long int> sz(N, 0); vector<long long int> deno; vector<bool> vis(N, false); long long int dfs(long long int i) { vis[i] = true; sz[i] = 1; for (auto it : adj[i]) { if (!vis[it]) { sz[i] += dfs(it); } } return sz[i]; } void solve() { long long int n; cin >> n; for (int i = 0; i < n - 1; ++i) { long long int u, v; cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } long long int sum = 0; set<pair<long long int, long long int>> st; for (int i = 0; i < n; ++i) { if (adj[i].size() != 1) { sum = 0; for (int i = 0; i < n; i++) { vis[i] = false; sz[i] = 0; } dfs(i); for (auto it : adj[i]) { if (i == 2) { 29; } deno.push_back(sz[it]); sum += sz[it]; } 29; long long int m = deno.size(); vector<vector<bool>> dp(m + 1, vector<bool>(sum + 1, false)); for (int i = 1; i <= m; i++) { dp[i][0] = 1; } for (int i = 1; i <= m; ++i) { for (int j = 1; j <= sum; ++j) { if (j >= deno[i - 1]) { dp[i][j] = (dp[i - 1][j] || dp[i - 1][j - deno[i - 1]]); } else { dp[i][j] = dp[i - 1][j]; } } } for (int i = 1; i <= sum; i++) { if (dp[m][i]) { long long int s1 = i; long long int s2 = n - i - 1; if (s1 > 0 && s2 > 0) { st.insert({s1, s2}); st.insert({s2, s1}); } } } deno.clear(); } } cout << st.size() << '\n'; for (auto it : st) { cout << it.first << ' ' << it.second << '\n'; } }
5
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG int64_t mod=1000000007; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using Graph = vector<vector<int>>; int main() { int N,M; cin>>N>>M; Graph G(N); rep(i,M){ int a,b; cin>>a>>b; a--; b--; G[a].push_back(b); G[b].push_back(a); } vector<int> dist(N, -1); queue<int> q; dist[0] = 0; q.push(0); while(!q.empty()){ int v=q.front(); q.pop(); for(auto nv:G[v]){ if (dist[nv] != -1) continue; dist[nv]= v+1; q.push(nv); } } cout<<"Yes"<<endl; rep(i,N-1){ cout<<dist[i+1]<<endl; } }
0
#include <iostream> #include <string> using namespace std; int main() { string cipher; while (getline(cin, cipher)) { for (int i = 0; i < 26;i++) { for (unsigned int n = 0;n < cipher.length();n++) { if (cipher.at(n) == 'z') { cipher.at(n) = 'a'; } else if (cipher.at(n) >= 'a'&&cipher.at(n) <= 'y') { cipher.at(n)++; } } if (cipher.find("the") != string::npos || cipher.find("this") != string::npos || cipher.find("that") != string::npos) { cout << cipher << endl; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 0, 1}; int dy[] = {0, 1, -1, 0}; long long mo = 1e9; const long double P = 3.141592653589793238L; void trust_yourself() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long gcd(long long x, long long y) { if (x == 0) return y; return gcd(y % x, x); } long long fast_power(long long x, long long y, long long mo) { long long p = 1; while (y) { if (y & 1) p = (p * x) % mo; y = (y >> 1); x = (x * x) % mo; } return p; } long long convert_to_num(string x) { long long num = 0; for (int i = 0; i < x.size(); i++) { num *= 2; num += x[i] - '0'; } return num; } long long com(long long n, long long r) { long long p = 1, p1 = 1; for (long long i = r + 1; i <= n; i++) { p = p * i; p /= p1; p1++; } return p; } int main() { trust_yourself(); int n, m; long long b[200001] = {0}, sum = 0; multiset<int> s; cin >> n >> m; for (int i = 0, a; i < n; i++) { cin >> a; s.insert(a); } for (int x, y; m--;) { cin >> x >> y; b[x - 1] += 1; if (y < n) b[y] += -1; } for (int i = 1; i < n; i++) { b[i] += b[i - 1]; } sort(b, b + n); for (; n--;) { auto it = --s.end(); sum += (*it * b[n]); s.erase(it); } cout << sum; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long debug_counter = 0; void logic() { long long n; cin >> n; long long index = 1; while (n - index > 0) { n = n - index; index++; } cout << n; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long t; { logic(); } }
1
#include <bits/stdc++.h> using namespace std; char d[1000001]; vector<int> seg; int main() { scanf(" %s", d); int n, i; n = strlen(d); int p = 0; char r = d[p]; while (d[(p + n - 1) % n] == r) p = (p + n - 1) % n; int e = p; while (d[(p + 1) % n] == r) p = (p + 1) % n; seg.push_back((p - e + 1 + n) % n); p++; while (p != e) { r = d[p]; int s = p; while (d[(p + 1) % n] == r) p = (p + 1) % n; seg.push_back((p - s + 1 + n) % n); p = (p + 1) % n; } int ans = 0; i = 0; while (i < seg.size()) { if (seg[i] > 1) { ans++; i++; } else { int c = 0; while (i < seg.size() && seg[i] == 1) { i++; c++; } ans += c / 2; } } printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; constexpr long double m_pi = 3.1415926535897932L; constexpr long long MOD = 1000000007; constexpr long long INF = 1LL << 61; constexpr long double EPS = 1e-10; template <typename T> using vector2 = vector<vector<T>>; template <typename T> using vector3 = vector<vector2<T>>; string operator*(const string& s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { int to, rev; long long cap; Edge(int _to, long long _cap, int _rev) { to = _to; cap = _cap; rev = _rev; } }; void add_edge(vector<vector<Edge>>& G, int from, int to, long long cap, bool revFlag, long long revCap) { G[from].push_back(Edge(to, cap, (long long)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (long long)G[from].size() - 1)); } long long max_flow_dfs(vector<vector<Edge>>& G, long long v, long long t, long long f, vector<bool>& used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge& e = G[v][i]; if (!used[e.to] && e.cap > 0) { long long d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } long long max_flow(vector<vector<Edge>>& G, long long s, long long t) { long long flow = 0; for (;;) { vector<bool> used(G.size()); for (long long(i) = (long long)(0); i < (long long)(used.size()); i++) used[i] = false; long long f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(vector<vector<Edge>>& G, long long s, vector<long long>& d, vector<long long>& negative) { d.resize(G.size()); negative.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i] = INF; for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) negative[i] = false; d[s] = 0; for (long long(k) = (long long)(0); k < (long long)(G.size() - 1); k++) { for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } for (long long(k) = (long long)(0); k < (long long)(G.size() - 1); k++) { for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(vector<vector<Edge>>& G, long long s, vector<long long>& d) { d.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i] = INF; d[s] = 0; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q; q.push(make_pair(0, s)); while (!q.empty()) { pair<long long, long long> a = q.top(); q.pop(); if (d[a.second] < a.first) continue; for (long long(i) = (long long)(0); i < (long long)(G[a.second].size()); i++) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(vector<vector<Edge>>& G, vector<vector<long long>>& d) { d.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i].resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(d[i].size()); j++) { d[i][j] = ((i != j) ? INF : 0); } } for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { chmin(d[i][G[i][j].to], G[i][j].cap); } } for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G.size()); j++) { for (long long(k) = (long long)(0); k < (long long)(G.size()); k++) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(vector<vector<Edge>>& graph, vector<long long>& order) { int n = graph.size(), k = 0; vector<long long> in(n); for (auto& es : graph) for (auto& e : es) in[e.to]++; priority_queue<long long, vector<long long>, greater<long long>> que; for (long long(i) = (long long)(0); i < (long long)(n); i++) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto& e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class Lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; Lca() {} Lca(const vector<vector<Edge>>& g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const vector<vector<Edge>>& g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto& e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const vector<vector<Edge>>& g, int v, vector<vector<int>>& scc, stack<int>& S, vector<int>& inS, vector<int>& low, vector<int>& num, int& time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; for (decltype((g[v]).begin()) e = (g[v]).begin(); e != (g[v]).end(); ++e) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(vector<int>()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const vector<vector<Edge>>& g, vector<vector<int>>& scc) { const int n = g.size(); vector<int> num(n), low(n); stack<int> S; vector<int> inS(n); int time = 0; for (long long(u) = (long long)(0); u < (long long)(n); u++) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; long long num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } long long size(int x) { return -data[root(x)]; } long long numSet() { return num; } }; template <typename T, typename F> class SegmentTree { private: T identity; F merge; long long n; vector<T> dat; public: SegmentTree(F f, T id, vector<T> v) : merge(f), identity(id) { int _n = v.size(); n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); for (long long(i) = (long long)(0); i < (long long)(_n); i++) dat[n + i - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } SegmentTree(F f, T id, int _n) : merge(f), identity(id) { n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); } void set_val(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int l, int r) { T left = identity, right = identity; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0) left = merge(left, dat[l]); if ((r & 1) == 0) right = merge(dat[r - 1], right); l = l / 2; r = (r - 1) / 2; } return merge(left, right); } }; class SumSegTree { public: long long n, height; vector<long long> dat; SumSegTree(long long _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<long long>(2 * n - 1, 0); } void add(long long i, long long x) { i += n - 1; dat[i] += x; while (i > 0) { i = (i - 1) / 2; dat[i] += x; } } long long sum(long long l, long long r) { long long ret = 0; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0) ret += dat[l]; if ((r & 1) == 0) ret += dat[r - 1]; l = l / 2; r = (r - 1) / 2; } return ret; } }; class RmqTree { private: long long _find(long long a, long long b, long long k, long long l, long long r) { if (r <= a || b <= l) return INF; if (a <= l && r <= b) return dat[k]; else { long long s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); long long s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); return min(s1, s2); } } public: long long n, height; vector<long long> dat; RmqTree(long long _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<long long>(2 * n - 1, INF); } void update(long long i, long long x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } long long find(long long a, long long b) { return _find(a, b, 0, 0, n); } }; void divisor(long long n, vector<long long>& ret) { for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void prime_factorization(long long n, vector<pair<long long, long long>>& ret) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } long long mod_pow(long long x, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long mod_inv(long long x, long long mod) { return mod_pow(x, mod - 2, mod); } class Combination { public: vector<long long> fact; vector<long long> inv; long long mod; long long mod_inv(long long x) { long long n = mod - 2LL; long long res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long nCr(long long n, long long r) { if (n < r) return 0; if (n < mod) return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; long long ret = 1; while (n || r) { long long _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } long long nPr(long long n, long long r) { return (fact[n] * inv[n - r]) % mod; } long long nHr(long long n, long long r) { return nCr(r + n - 1, r); } Combination(long long _n, long long _mod) { mod = _mod; long long n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; for (long long(i) = (long long)(0); i < (long long)(n - 1); i++) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n); inv[n - 1] = mod_inv(fact[n - 1]); for (int i = n - 1; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; long long popcount(long long x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; long long ans = 1; auto check = [&](long long a) { if (a > n) return 0LL; long long cnt = 1; for (long long(i) = (long long)(1); i < (long long)(61); i++) { long long tmp = (a << i); if (tmp > n) break; cnt += min(1LL << i, n - tmp + 1); } return cnt; }; long long l = 0, r = n / 2 + 1; while (r - l > 1) { long long mid = (r + l) / 2; long long t = mid * 2; long long cnt = check(t) + check(t + 1); if (cnt >= k) l = mid; else r = mid; } chmax(ans, l * 2); l = -1, r = n / 2 + 1; while (r - l > 1) { long long mid = (r + l) / 2; long long t = mid * 2 + 1; long long cnt = check(t); if (cnt >= k) l = mid; else r = mid; } chmax(ans, l * 2 + 1); cout << ans << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int l = 0, r = k - 1, i = l; long long int sum = 0, newsum = 0; while (i <= r) { sum = sum + arr[i]; i++; } newsum = sum; l++; r++; while (r != n) { newsum = newsum - arr[l - 1] + arr[r]; sum = sum + newsum; l++; r++; } long long int x = n - k + 1; float y = sum / (float)x; printf("%0.10f", y); }
2
#include<map> #include<cstdio> int main(){ int n; scanf("%d", &n); long long ans = 1; const long long mod = 1e9 + 7; std::map<int, long long> factors; for(int i = 2; i <= n; i++){ for(int j = 2, d = i; d > 1; j++){ while(d % j == 0){ factors[j]++; d /= j; } } } for(auto p : factors) ans = ans *(1 + p.second) % mod; printf("%ld\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; struct Node { int ty, x, y, v, id; bool operator<(const Node& q) const { if (x == q.x) return ty < q.ty; return x < q.x; } }; Node a[N * 10], temp[N * 10]; int C[N], ans[N], p[N], q[N], _hash[N], rec[N]; int n, cnt; int lowbit(int x) { return x & -x; } void add(int x, int d) { while (x <= n) { C[x] += d; x += lowbit(x); } } int sum(int x) { int ret = 0; while (x) { ret += C[x]; x -= lowbit(x); } return ret; } void clear(int x) { while (x <= n) { if (C[x]) C[x] = 0; else break; x += lowbit(x); } } void cdq(int l, int r) { if (l == r) return; int mid = (l + r) >> 1; cdq(l, mid); cdq(mid + 1, r); int pl = l, pr = mid + 1, p = l; while (pl <= mid && pr <= r) { if (a[pl] < a[pr]) { if (a[pl].ty == 0) add(a[pl].y, a[pl].v); temp[p++] = a[pl++]; } else { if (a[pr].ty == 1) ans[a[pr].id] += sum(a[pr].y) * a[pr].v; temp[p++] = a[pr++]; } } while (pl <= mid) temp[p++] = a[pl++]; while (pr <= r) { if (a[pr].ty == 1) ans[a[pr].id] += sum(a[pr].y) * a[pr].v; temp[p++] = a[pr++]; } for (int i = l; i <= r; i++) { clear(a[i].y); a[i] = temp[i]; } } int main() { int m, tot, ty, x, y, xx, yy, cnt; scanf("%d %d", &n, &m); tot = 0; for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= n; i++) scanf("%d", &q[i]); for (int i = 1; i <= n; i++) _hash[p[i]] = i; for (int i = 1; i <= n; i++) q[i] = _hash[q[i]]; for (int i = 1; i <= n; i++) a[++tot] = (Node){0, i, q[i], 1, 0}; cnt = 0; for (int i = 1; i <= m; i++) { scanf("%d", &ty); if (ty == 1) { scanf("%d %d %d %d", &x, &y, &xx, &yy); cnt++; a[++tot] = (Node){1, xx - 1, x - 1, 1, cnt}; a[++tot] = (Node){1, xx - 1, y, -1, cnt}; a[++tot] = (Node){1, yy, x - 1, -1, cnt}; a[++tot] = (Node){1, yy, y, 1, cnt}; } else { scanf("%d %d", &x, &y); a[++tot] = (Node){0, x, q[x], -1, 0}; a[++tot] = (Node){0, y, q[y], -1, 0}; swap(q[x], q[y]); a[++tot] = (Node){0, x, q[x], 1, 0}; a[++tot] = (Node){0, y, q[y], 1, 0}; } } cdq(1, tot); for (int i = 1; i <= cnt; i++) printf("%d\n", ans[i]); return 0; }
5