solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int N = 3004; int n, m, poparcie[N], odw[N][N]; long long ans = LLONG_MAX; vector<pair<int, int> > G[N]; pair<int, pair<int, int> > T[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { int p, c; cin >> p >> c; poparcie[p]++; G[p].push_back(make_pair(c, poparcie[p])); T[i] = make_pair(c, make_pair(p, poparcie[p])); } for (int i = 1; i <= m; i++) sort(G[i].begin(), G[i].end()); sort(T + 1, T + n + 1); for (int i = 1; i <= n / 2 + 1; i++) { int r = poparcie[1]; long long F = 0; for (int j = 2; j <= m; j++) for (int k = 0; k <= poparcie[j] - i; k++) r++, (F += G[j][k].first), (odw[j][G[j][k].second] = i); if (r - i < 0) { for (int j = 1; j <= n; j++) { if (odw[T[j].second.first][T[j].second.second] < i && T[j].second.first != 1) (F += T[j].first), r++; if (r == i) break; } } ans = min(ans, F); } cout << ans << endl; getchar(); getchar(); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, s, f; char a[15], m[15]; cin >> a; strcpy(m, a); f = 0; n = 1; int len = strlen(a); for (i = 0; i < len; i++) if (a[i] != a[len - 1 - i]) { n = 0; break; } if (n && len % 2 == 0) { for (i = 0; i < len / 2; i++) cout << a[i]; cout << "a"; for (i = len / 2; i < len; i++) cout << a[i]; cout << endl; } else { for (i = 0; i <= len; i++) { s = 1; strcpy(a, m); for (j = len; j > i; j--) a[j] = a[j - 1]; a[i] = a[len - i]; for (j = 0; j < (len + 1) / 2; j++) if (a[j] != a[len - j]) { s = 0; break; } if (s) { f = 1; break; } } if (f) { for (i = 0; i <= len; i++) cout << a[i]; cout << endl; } else cout << "NA" << endl; } }
22
#include <bits/stdc++.h> using namespace std; struct Graph { unordered_map<int, unordered_set<int>> list; void add_edge(int u, int v) { list[u].insert(v); list[v].insert(u); } }; int dfs(Graph& g, int u, unordered_set<int>& visited, vector<int>& succ) { for (int v : g.list[u]) { if (visited.count(v) <= 0) { visited.insert(v); succ[u] += 1 + dfs(g, v, visited, succ); } } return succ[u]; } void dfs(Graph& g, unordered_map<int, int>& dists) { stack<int> s; unordered_set<int> visited; s.push(1); visited.insert(1); dists[1] = 0; while (!s.empty()) { int u = s.top(); s.pop(); bool has = false; for (int v : g.list[u]) { if (visited.count(v) <= 0) { visited.insert(v); s.push(v); dists[v] = dists[u] + 1; has = true; } } } } long long dfs(Graph& g, vector<bool>& marked) { vector<int> above(g.list.size() + 1); above[1] = 1; unordered_set<int> visited; stack<int> s; s.push(1); visited.insert(1); while (!s.empty()) { int u = s.top(); s.pop(); for (int v : g.list[u]) { if (visited.count(v) <= 0) { s.push(v); visited.insert(v); if (marked[v]) above[v] = above[u]; else above[v] = above[u] + 1; } } } long long sum = 0; for (int u = 1; u < g.list.size() + 1; ++u) { if (marked[u]) sum += above[u]; } return sum; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; Graph g; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; g.add_edge(u, v); } vector<int> succ(n + 1, 0); unordered_set<int> visited; visited.insert(1); dfs(g, 1, visited, succ); unordered_map<int, int> dists; dfs(g, dists); vector<pair<int, int>> vertices; for (auto& e : dists) { vertices.push_back({e.first, dists[e.first] - (succ[e.first] - 1)}); } sort(vertices.begin(), vertices.end(), [&](const pair<int, int>& a, const pair<int, int>& b) { return a.second > b.second; }); vector<bool> marked(n + 1); for (int i = 0; i < k; ++i) { marked[vertices[i].first] = true; } long long sum = dfs(g, marked); cout << sum << endl; }
8
#include <bits/stdc++.h> using namespace std; int main() { int i, n, j, k, l, m, t, c = 0, ans; scanf("%d", &n); char s[20]; char s1[20][20]; while (n--) { scanf("%s", s); l = strlen(s); vector<pair<int, int> > v; for (i = 1; i <= 12; i++) { if (12 % i == 0) { j = 12 / i; int x = 0; for (m = 0, x = 0; m < i; m++) { for (k = 0; k < j; k++, x++) { s1[m][k] = s[x]; } s1[m][k] = '\0'; } for (k = 0; k < j; k++) { c = 0; for (m = 0; m < i; m++) { if (s1[m][k] == 'O') { c = 1; } } if (c == 0) { int y = i; int z = j; v.push_back(make_pair(i, j)); break; } } } } sort(v.begin(), v.end()); printf("%d ", v.size()); for (i = 0; i < v.size(); i++) { printf("%dx%d", v[i].first, v[i].second); if (i != v.size() - 1) printf(" "); } puts(""); } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct UF { vector<int> e; UF(int n) : e(n, -1) {} int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); } bool join(int a, int b) { a = find(a), b = find(b); if (a == b) return false; if (e[a] > e[b]) swap(a, b); e[a] += e[b]; e[b] = a; return true; } }; char s[300030]; pair<int, int> a[300030]; vector<pair<int, int>> g[300030]; int t, n, k, m, x, d[300030], cnt[3][300030], ans; void dfs(int v, int p, int s) { if (s) d[v] = d[p]; else d[v] = 3 - d[p]; for (auto u : g[v]) if (!d[u.first]) dfs(u.first, v, u.second); } int main() { scanf("%d%d%s", &n, &k, s + 1); UF uf(k + 1); d[k + 1] = 2; for (int i = 1; i <= n; i++) s[i] -= '0'; for (int i = 1; i <= k; i++) for (int j = !scanf("%d", &m); j < m; j++) if (scanf("%d", &x), a[x].first) { a[x].second = i; g[a[x].first].emplace_back(i, s[x]); g[i].emplace_back(a[x].first, s[x]); } else a[x].first = i; for (int i = 1; i <= n; i++) if (a[i].first && a[i].second == 0) g[0].emplace_back(a[i].first, s[i]); for (int i = 0; i <= k; i++) if (!d[i]) dfs(i, k + 1, 0); for (int i = 1; i <= k; i++) cnt[d[i]][i] = 1; cnt[1][0] = 0x3f3f3f3f; for (int i = 1; i <= n; i++) { int u = uf.find(a[i].first), v = uf.find(a[i].second); if (uf.join(u, v)) { ans -= min(cnt[1][u], cnt[2][u]); ans -= min(cnt[1][v], cnt[2][v]); for (int j = 1; j <= 2; j++) cnt[j][u] = cnt[j][v] = cnt[j][u] + cnt[j][v]; ans += min(cnt[1][u], cnt[2][u]); } printf("%d\n", ans); } }
16
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; int n; long long x[maxn]; long long ans = 0x7FFFFFFFFFFFFFFF, cnt = 0; long long absl(long long a) { if (a < 0) return 0 - a; return a; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x[i]; } sort(x, x + n); for (int i = 1; i < n; i++) { if (absl(x[i - 1] - x[i]) < ans) { ans = absl(x[i - 1] - x[i]); cnt = 1; } else if (absl(x[i - 1] - x[i]) == ans) { cnt++; } } cout << ans << ' ' << cnt; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y, x_mid, y_mid; cin >> n; vector<pair<int, int> > v; for (int i = (0); i < (n); i++) { cin >> x >> y; v.push_back(make_pair(x, y)); } if (n % 2) { cout << "NO" << endl; return 0; } else { x_mid = v[0].first + v[n / 2].first; y_mid = v[0].second + v[n / 2].second; for (int i = (0); i < (n / 2); i++) { if (v[i].first + v[i + n / 2].first != x_mid || v[i].second + v[n / 2 + i].second != y_mid) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; } return 0; }
10
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; long double a[300000], t[300000]; bool check(long long i, long long j) { return (abs(t[i]) < abs(t[j])); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, tt; cin >> n >> tt; for (long long i = 1; i <= n; i++) cin >> a[i]; long double res = 0; vector<long long> vec1, vec2; for (long long i = 1; i <= n; i++) { cin >> t[i]; t[i] -= tt; if (abs(t[i]) < 1e-7) { res += a[i]; } else if (t[i] < 0) vec1.push_back(i); else vec2.push_back(i); } long double sum1 = 0, sum2 = 0; for (auto i : vec1) sum1 -= a[i] * t[i]; for (auto i : vec2) sum2 += a[i] * t[i]; if (sum1 > sum2) { sort(vec1.begin(), vec1.end(), check); for (auto i : vec1) { long double now = min(a[i], sum2 / abs(t[i])); res += now; sum2 -= abs(t[i]) * now; } for (auto i : vec2) res += abs(a[i]); } else { sort(vec2.begin(), vec2.end(), check); for (auto i : vec2) { long double now = min(a[i], sum1 / abs(t[i])); res += now; sum1 -= abs(t[i]) * now; } for (auto i : vec1) res += a[i]; } cout << fixed << setprecision(9) << res << '\n'; }
12
#include <bits/stdc++.h> using namespace std; const __int128 maxn = 2e5 + 100; const __int128 mod = 1e9 + 7; const __int128 inf = 1e18; long long s; __int128 n, k, x, a[maxn]; vector<__int128> c[maxn]; bool val(__int128 t) { x = 0; x += a[0]; x += (t + 1) * a[1]; if (n > 2 && t < 1e10) x += (t + 1) * (t + 2) / 2 * a[2]; else if (n > 2 && a[2]) return 1; for (__int128 i = 3; i < n; i++) { if (x >= k) return 1; if (t >= c[i].size() && a[i] > 0) return 1; else if (t < c[i].size()) x += c[i][t] * a[i]; } return (x >= k); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; for (__int128 i = 3; i < maxn; i++) { x = 1; __int128 j = 1; while (x < 1e18) { c[i].push_back(x); x = x * (i + j) / j; j++; } } cin >> s; n = s; cin >> s; k = s; for (__int128 i = 1; i <= n; i++) { cin >> s; a[n - i] = s; } if (*max_element(a, a + n) >= k) { cout << 0; return 0; } __int128 l = 0, r = 1e18, mid; while (r - l > 1) { mid = (l + r) / 2; if (val(mid - 1)) r = mid; else l = mid; } s = r; cout << s; return 0; }
16
#include <bits/stdc++.h> using namespace std; inline long long int modPow(long long int base, long long int exp, long long int modulus) { base %= modulus; long long int result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result; } inline string NumToStr(long long int Number) { stringstream ss; ss << Number; return ss.str(); } inline long long int StrToNum(const string &Text) { stringstream ss(Text); long long int result; return ss >> result ? result : 0; } inline long long int maximum(long long int a, long long int b, long long int c) { long long int d = (a >= b) ? a : b; long long int e = (d >= c) ? d : c; return e; } int main() { ios_base::sync_with_stdio(0); ; cin.tie(0); string s; cin >> s; vector<long long int> ab, ba; for (long long int i = 0; i < s.length() - 1; i++) { if (s[i] == 'A' && s[i + 1] == 'B') ab.push_back(i); if (s[i] == 'B' && s[i + 1] == 'A') ba.push_back(i); } if (ab.size() == 0 || ba.size() == 0) { cout << "NO"; exit(0); } long long int one, two; one = abs(ab[0] - ba[ba.size() - 1]); two = abs(ba[0] - ab[ab.size() - 1]); if (one > 1 || two > 1) cout << "YES"; else cout << "NO"; return 0; }
7
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx,avx2,fma,sse4") namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <typename A> inline bool read(A &x) { for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; else if (c == EOF) return 0; for (x = 0; c <= '9' && c >= '0'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (c & 15); x *= f; return 1; } inline bool read(char &x) { while ((x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) == ' ' || x == '\n' || x == '\r') ; return x != EOF; } inline bool read(char *x) { while ((*x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) == '\n' || *x == ' ' || *x == '\r') ; if (*x == EOF) return 0; while (!(*x == '\n' || *x == ' ' || *x == '\r' || *x == EOF)) *(++x) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); *x = 0; return 1; } template <typename A, typename... B> inline bool read(A &x, B &...y) { return read(x) && read(y...); } template <typename A> inline bool write(A x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putc(qu[qr--]); return 0; } inline bool write(char x) { putc(x); return 0; } inline bool write(const char *x) { while (*x) { putc(*x); ++x; } return 0; } inline bool write(char *x) { while (*x) { putc(*x); ++x; } return 0; } template <typename A, typename... B> inline bool write(A x, B... y) { return write(x) || write(y...); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io ::putc; using io ::read; using io ::write; int main() { int n; read(n); int arr[n]; for (auto &i : arr) read(i); int q, t; read(q); while (q--) { read(t); if (t == 1) { long long ans = 0, l, r; read(l, r); for (int i = l - 1; i < r; i++) { ans += arr[i]; } write(ans, '\n'); } else { int l, r, x; read(l, r, x); for (int i = l - 1; i < r; i++) { arr[i] ^= x; } } } }
12
#include <bits/stdc++.h> using namespace std; bool prime(long long int n) { for (int d = 2; d <= sqrt(n); d++) if (n % d == 0) return false; return true; } long long int putere(long long int x, long long int y, long long int mod) { int put = 1; while (y) { if (y % 2) put = (put * x) % mod; x = (x * x) % mod; y /= 2; } return put; } int main() { long long int n; cin >> n; if (n != 4 && !prime(n)) { cout << "NO"; return 0; } if (n == 4) { cout << "YES" << endl; cout << 1 << endl << 3 << endl << 2 << endl << 4; return 0; } if (n == 1) { cout << "YES" << endl << '1'; return 0; } cout << "YES" << '\n'; cout << '1' << '\n'; for (long long int i = 2; i < n; i++) cout << (i * putere(i - 1, n - 2, n)) % n << '\n'; cout << n; return 0; }
15
#include <bits/stdc++.h> using namespace std; template <class T> void read(T& x) { T res = 0, f = 1; char num = getchar(); while (!isdigit(num)) { if (num == '-') f = -1; num = getchar(); } while (isdigit(num)) { res = (res << 3) + (res << 1) + num - '0'; num = getchar(); } x = res * f; } void file() {} const int N = 1e6 + 5; int c[N], a[N]; void modify(int x, int v) { while (x < N) c[x] += v, x += (x & (-x)); } int query(int x) { int sum = 0; while (x) sum += c[x], x -= (x & (-x)); return sum; } signed main() { int n, q; read(n), read(q); for (int i = 1; i <= n; i++) { int x; read(x); modify(x, 1); } while (q--) { int x; read(x); if (x >= 1) { modify(x, 1); } else { x = abs(x); if (query(n) < x) continue; int l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; if (query(mid) >= x) r = mid; else l = mid + 1; } modify(l, -1); } } int ans = 0; for (int i = 1; i <= n; i++) { if (query(i)) { ans = i; break; } } printf("%d\n", ans); return 0; }
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; int main() { int n; int a[MAXN]; int vis[MAXN]; int sum[MAXN]; while (~scanf("%d", &n)) { memset(sum, 0, sizeof(sum)); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof(vis)); int dcolor = i; int maxx = 0; for (int j = i; j <= n; j++) { vis[a[j]]++; if (vis[a[j]] > maxx || (vis[a[j]] == maxx && dcolor > a[j])) { maxx = vis[a[j]]; dcolor = a[j]; } sum[dcolor]++; } } for (int i = 1; i <= n - 1; i++) printf("%d ", sum[i]); printf("%d\n", sum[n]); } return 0; }
7
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { for (; b; a %= b, swap(a, b)) ; return a; } const int INF = 987654321; int n, m; string a, b; int dp[5001][5001]; int solve(int ai, int bi) { if (ai >= n || bi >= m) return 0; int& ret = dp[ai][bi]; if (ret != -INF) return ret; ret = 0; if (a[ai] == b[bi]) { ret = 2; int res = solve(ai + 1, bi + 1) + 2; ret = max(ret, res); } int r1 = solve(ai + 1, bi) - 1; int r2 = solve(ai, bi + 1) - 1; ret = max(ret, r1); ret = max(ret, r2); return ret; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); fill(&dp[0][0], &dp[5000][5000] + 1, -INF); cin >> n >> m; cin >> a >> b; solve(0, 0); int res = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) res = max(res, dp[i][j]); cout << res; }
10
#include <bits/stdc++.h> using namespace std; int main() { string s, t; int n, a = 0, b = 0, f = 0; cin >> n >> s; for (int i = 0; i <= n; i++) if (s[i] == '_' || s[i] == '(' || s[i] == ')' || i == n) { if (!f) a = max(a, (int)t.length()); else if (t != "") b++; if (i < n) f = s[i] == '(' ? 1 : (s[i] == ')' ? 0 : f); t = ""; } else t += s[i]; cout << a << ' ' << b; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string second; cin >> second; long long int len = second.length(); if (len % 2 == 0) { long long int coun = 0; for (long long int i = 0; i < (len); ++i) if (second.at(i) == second.at(0)) coun++; if (coun == len) cout << "Impossible"; else { string s1; for (long long int i = 0; i < (len); ++i) { s1 = second.substr(i, len) + second.substr(0, i); if (s1 != second) { string st = s1; reverse(s1.begin(), s1.end()); if (s1 == st) { cout << 1; return 0; } } } cout << 2; } } else { long long int coun = 0; for (long long int i = 0; i < (len); ++i) if (second.at(i) == second.at(0)) coun++; if (coun >= len - 1) cout << "Impossible"; else cout << 2; } return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long arr[n]; long long brr[n]; long long ans1 = 0, ans2 = 0, finans = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n; i++) { cin >> brr[i]; } for (int i = 0; i < n; i++) { ans1 |= arr[i]; ans2 |= brr[i]; if (ans2 + ans1 < 0) ans2 = 0; finans = max(finans, ans2 + ans1); } cout << finans; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, i, d; scanf("%d", &n); if (n == 1) { printf("1"); return 0; } if (n == 2) { printf("1 2"); return 0; } if (n == 3) { printf("1 1 3"); return 0; } d = 1; while (n > 3) { for (i = 1; i <= n; i += 2) printf("%d ", d); d <<= 1; n >>= 1; } if (n == 2) printf("%d %d", d, d * 2); else printf("%d %d %d", d, d, 3 * d); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5 + 1; int n, q, x, y, tree[60][MaxN << 2]; void Modify(int x, int l, int r, int p, int v) { if (l > r || l > p || r < p) return; if (l == r) { for (int i = 0; i <= 59; i++) tree[i][x] = (i % v) ? 1 : 2; return; } int mid = (l + r) >> 1; if (p <= mid) Modify(x << 1, l, mid, p, v); else Modify(x << 1 | 1, mid + 1, r, p, v); for (int i = 0; i <= 59; i++) { int ans = tree[i][x << 1]; tree[i][x] = ans + tree[(i + ans) % 60][x << 1 | 1]; } } int Request(int x, int l, int r, int L, int R, int t) { if (l > r || l > R || r < L) return 0; if (L <= l && r <= R) return tree[t][x]; int mid = (l + r) >> 1; if (R <= mid) return Request(x << 1, l, mid, L, R, t); if (L > mid) return Request(x << 1 | 1, mid + 1, r, L, R, t); int ans = Request(x << 1, l, mid, L, R, t); return ans + Request(x << 1 | 1, mid + 1, r, L, R, (t + ans) % 60); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); Modify(1, 1, n, i, x); } char c[5]; scanf("%d", &q); for (int i = 1; i <= q; i++) { scanf("%s", &c); scanf("%d%d", &x, &y); if (c[0] == 'A') printf("%d\n", Request(1, 1, n, x, y - 1, 0)); if (c[0] == 'C') Modify(1, 1, n, x, y); } return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; if (s[s.length() - 1] == 'o') cout << "FILIPINO"; else if (s[s.length() - 1] == 'a') cout << "KOREAN"; else if (s[s.length() - 1] == 'u') cout << "JAPANESE"; if (t != 0) cout << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<vector<int> > data; int ara[3005], is[3005], total, par[3005], n; int root(int v) { return par[v] < 0 ? v : (par[v] = root(par[v])); } void union_set(int x, int y) { if ((x = root(x)) == (y = root(y))) return; if (par[y] < par[x]) swap(x, y); par[x] += par[y]; par[y] = x; } void func() { int i; total = 0; data.clear(); for (i = 0; i < 3005; i++) is[i] = 0; for (i = 0; i < n; i++) { if (is[i]) continue; vector<int> temp; int x = i; while (!is[x]) { is[x] = 1; temp.push_back(x); x = ara[x]; } data.push_back(temp); total++; } for (i = 0; i < n; i++) par[i] = ara[i]; for (i = 0; i < total; i++) par[data[i][0]] = -data[i].size(); return; } int main() { int i, t; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &ara[i]); ara[i]--; } int m; scanf("%d", &m); func(); if (m > n - total) { printf("%d\n", m - n + total); for (i = 0; i < m - n + total; i++) printf("%d %d ", data[0][0] + 1, data[i + 1][0] + 1); } else { t = n - total - m; int j; printf("%d\n", t); while (t) { int min_1 = 5000000, min_2 = 50000000, pos; for (i = 0; i < data.size(); i++) if (data[i].size() > 1 && data[i][0] < min_1) { min_1 = data[i][0]; pos = i; } for (i = 1; i < data[pos].size(); i++) { if (data[pos][i] < min_2) min_2 = data[pos][i]; } printf("%d %d ", min_1 + 1, min_2 + 1); t--; swap(ara[min_1], ara[min_2]); func(); } } printf("\n"); return 0; }
13
#include <bits/stdc++.h> using namespace std; vector<long long> v; void dfs(long long num, int len) { if (len == 10) { return; } v.push_back(num * 10 + 4); v.push_back(num * 10 + 7); dfs(num * 10 + 4, len + 1); dfs(num * 10 + 7, len + 1); } int main() { ios_base::sync_with_stdio(false); long long l, r; cin >> l >> r; dfs(0, 0); v.push_back(0); long long sum = 0; sort(v.begin(), v.end()); for (int i = 1; i < v.size(); i++) { long long maxNum = max(l, v[i - 1] + 1); long long minNum = min(r, v[i]); long long zero = 0; sum = sum + v[i] * max(minNum - maxNum + 1, zero); } cout << sum; ; return 0; }
3
#include <bits/stdc++.h> using namespace std; void solve() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a, b; cin >> n >> a >> b; n--; int ans = min(n - a, b) + 1; cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, b; cin >> n; b = (n - 4) % 6; if (n < 3) { cout << -1; } else if (n == 3) { cout << 210; } else { if (b == 0) { cout << 1; for (int i = 0; i < n - 4; i++) { cout << 0; } cout << "050"; } if ((n - 4) % 6 == 1) { cout << 1; for (int i = 0; i < n - 4; i++) { cout << 0; } cout << "080"; } if ((n - 4) % 6 == 2) { cout << 1; for (int i = 0; i < n - 4; i++) { cout << 0; } cout << "170"; } if ((n - 4) % 6 == 3) { cout << 1; for (int i = 0; i < n - 4; i++) { cout << 0; } cout << "020"; } if ((n - 4) % 6 == 4) { cout << 1; for (int i = 0; i < n - 4; i++) { cout << 0; } cout << "200"; } if ((n - 4) % 6 == 5) { cout << 1; for (int i = 0; i < n - 4; i++) { cout << 0; } cout << "110"; } } }
6
#include <bits/stdc++.h> using namespace std; int m[28][28]; char s[103]; int maxi, maxj; int main() { int n; scanf("%d", &n); getchar(); scanf("%c", &s[0]); for (int i = 1; i < n; i++) { scanf("%c", &s[i]); ++m[(int)s[i - 1] - (int)'A'][(int)s[i] - (int)'A']; } for (int i = 0; i < 28; i++) for (int j = 0; j < 28; j++) if (m[i][j] > m[maxi][maxj]) { maxi = i; maxj = j; } cout << (char)(maxi + (int)'A') << (char)(maxj + (int)'A'); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long N = 2e5; vector<long long> v; void ck(long long x) { v.clear(); while (x) { long long r = x % 16; v.push_back(r); x /= 16; } reverse(v.begin(), v.end()); } long long dp[12 + 2][(1 << 16) + 2][2][2]; long long dfs(long long i, long long msk, long long ck, long long f) { if (i == v.size()) return f; if (dp[i][msk][ck][f] != -1) return dp[i][msk][ck][f]; long long ret = 0; if (ck == 0) { if (!f) ret += dfs(i + 1, msk, 0, 0); else if (!(msk & (1 << 0))) ret += dfs(i + 1, msk | (1 << 0), 0, 1); for (long long j = 1; j < 16; j++) if (!(msk & (1 << j))) ret += dfs(i + 1, msk | (1 << j), 0, 1); } else { if (v[i] == 0) { if (!(msk & (1 << 0))) ret += dfs(i + 1, msk | (1 << 0), 1, 1); } else { if (!f) ret += dfs(i + 1, msk, 0, 0); else if (!(msk & (1 << 0))) ret += dfs(i + 1, msk | (1 << 0), 0, 1); for (long long j = 1; j < v[i]; j++) if (!(msk & (1 << j))) ret += dfs(i + 1, msk | (1 << j), 0, 1); long long j = v[i]; if (!(msk & (1 << j))) ret += dfs(i + 1, msk | (1 << j), 1, 1); } } return dp[i][msk][ck][f] = ret; } long long how(long long x) { ck(x); memset(dp, -1, sizeof dp); return dfs(0, 0, 1, 0); } void prlong(long long x) { ck(x); for (long long i = 0; i < v.size(); i++) { if (v[i] < 10) cout << v[i]; else cout << (char)(v[i] - 10 + 'a'); } cout << endl; exit(0); } long long c[12 + 2][200 + 2]; void pre() { c[2][1] = 10684262; c[2][2] = 21818416; c[2][3] = 32917177; c[2][4] = 44262765; c[2][5] = 55595204; c[2][6] = 66693165; c[2][7] = 78038807; c[2][8] = 89139198; c[2][9] = 100469239; c[2][10] = 111816120; c[2][11] = 122914223; c[2][12] = 134269578; c[2][13] = 145603427; c[2][14] = 156702159; c[2][15] = 168047800; c[2][16] = 179380159; c[2][17] = 190478181; c[2][18] = 201823788; c[2][19] = 212922519; c[2][20] = 224267412; c[2][21] = 235599831; c[2][22] = 246698541; c[2][23] = 258044152; c[2][24] = 269780437; c[2][25] = 281416441; c[2][26] = 296280998; c[2][27] = 309579622; c[2][28] = 321325881; c[2][29] = 333047046; c[2][30] = 345027197; c[2][31] = 356773364; c[2][32] = 368520257; c[2][33] = 380499710; c[2][34] = 392233770; c[2][35] = 404098466; c[2][36] = 415959757; c[2][37] = 427706651; c[2][38] = 439699964; c[2][39] = 451420486; c[2][40] = 463171190; c[2][41] = 475160646; c[2][42] = 486906872; c[2][43] = 498626738; c[2][44] = 510620047; c[2][45] = 522613361; c[2][46] = 534099948; c[2][47] = 546333503; c[2][48] = 558332881; c[2][49] = 569819609; c[2][50] = 585648774; c[2][51] = 597983741; c[2][52] = 609963160; c[2][53] = 621464362; c[2][54] = 633431318; c[2][55] = 645437098; c[2][56] = 657100413; c[2][57] = 668903165; c[2][58] = 680883315; c[2][59] = 692630874; c[2][60] = 704363821; c[2][61] = 716353279; c[2][62] = 728090918; c[2][63] = 739837175; c[2][64] = 751810100; c[2][65] = 763563491; c[2][66] = 775542916; c[2][67] = 787175732; c[2][68] = 799024572; c[2][69] = 811262581; c[2][70] = 822764405; c[2][71] = 834730006; c[2][72] = 846735643; c[2][73] = 860021788; c[2][74] = 874395030; c[2][75] = 886386589; c[2][76] = 898366708; c[2][77] = 910085237; c[2][78] = 921834159; c[2][79] = 933826755; c[2][80] = 945573646; c[2][81] = 957308010; c[2][82] = 969286829; c[2][83] = 981033663; c[2][84] = 992780554; c[2][85] = 1004753661; c[2][86] = 1016494405; c[2][87] = 1028477722; c[2][88] = 1040230483; c[2][89] = 1051963925; c[2][90] = 1063947095; c[2][91] = 1075941048; c[2][92] = 1087683968; c[2][93] = 1099666729; c[2][94] = 1111400425; c[2][95] = 1123134150; c[2][96] = 1135127321; c[2][97] = 1149386105; c[2][98] = 1163070787; c[2][99] = 1174791402; c[2][100] = 1186770221; c[2][101] = 1198504555; c[2][102] = 1210251212; c[2][103] = 1222230873; c[2][104] = 1233977796; c[2][105] = 1245712823; c[2][106] = 1257690946; c[2][107] = 1269425303; c[2][108] = 1281417960; c[2][109] = 1293166531; c[2][110] = 1304897885; c[2][111] = 1316877975; c[2][112] = 1328625324; c[2][113] = 1340365139; c[2][114] = 1352596941; c[2][115] = 1364378737; c[2][116] = 1376100658; c[2][117] = 1388070218; c[2][118] = 1400037167; c[2][119] = 1411546806; c[2][120] = 1423530778; c[2][121] = 1438753836; c[2][122] = 1451693779; c[2][123] = 1463196248; c[2][124] = 1475161146; c[2][125] = 1487141297; c[2][126] = 1498697638; c[2][127] = 1510640740; c[2][128] = 1522614507; c[2][129] = 1534348094; c[2][130] = 1546108292; c[2][131] = 1558087042; c[2][132] = 1569821538; c[2][133] = 1581566217; c[2][134] = 1593534595; c[2][135] = 1605280920; c[2][136] = 1617429429; c[2][137] = 1629213766; c[2][138] = 1641000456; c[2][139] = 1652990524; c[2][140] = 1664644268; c[2][141] = 1676460561; c[2][142] = 1688441288; c[2][143] = 1699992569; c[2][144] = 1712143804; c[2][145] = 1728118380; c[2][146] = 1740095962; c[2][147] = 1751765127; c[2][148] = 1763578803; c[2][149] = 1775545444; c[2][150] = 1787292318; c[2][151] = 1799040470; c[2][152] = 1811018040; c[2][153] = 1822752352; c[2][154] = 1834512412; c[2][155] = 1846491865; c[2][156] = 1858225593; c[2][157] = 1870217548; c[2][158] = 1882202761; c[2][159] = 1893945227; c[2][160] = 1905937240; c[2][161] = 1917671604; c[2][162] = 1929414230; c[2][163] = 1941386514; c[2][164] = 1953144784; c[2][165] = 1964878491; c[2][166] = 1976844135; c[2][167] = 1988849952; c[2][168] = 2001511563; c[2][169] = 2016510075; c[2][170] = 2028491580; c[2][171] = 2040469176; c[2][172] = 2051982610; c[2][173] = 2063962761; c[2][174] = 2075830294; c[2][175] = 2087443295; c[2][176] = 2099433413; c[2][177] = 2111249439; c[2][178] = 2123148467; c[2][179] = 2134890856; c[2][180] = 2146675283; c[2][181] = 2158868089; c[2][182] = 2170602514; c[2][183] = 2182362601; c[2][184] = 2194328495; c[2][185] = 2206075084; c[2][186] = 2217809156; c[2][187] = 2229788211; c[2][188] = 2241780828; c[2][189] = 2253281995; c[2][190] = 2265262149; c[2][191] = 2277254069; c[2][192] = 2291924674; c[2][193] = 2304913551; c[2][194] = 2316906199; c[2][195] = 2328885144; c[2][196] = 2340385516; c[2][197] = 2352366215; c[2][198] = 2364345672; c[2][199] = 2376092591; c[3][1] = 10030150; c[3][2] = 20103656; c[3][3] = 30157030; c[3][4] = 40244418; c[3][5] = 50297965; c[3][6] = 60385397; c[3][7] = 70439739; c[3][8] = 80526333; c[3][9] = 90614472; c[3][10] = 100668684; c[3][11] = 110755406; c[3][12] = 120809620; c[3][13] = 130896121; c[3][14] = 140950599; c[3][15] = 151038030; c[3][16] = 161116876; c[3][17] = 171178966; c[3][18] = 181265157; c[3][19] = 191319683; c[3][20] = 201407250; c[3][21] = 211460440; c[3][22] = 221547962; c[3][23] = 231602494; c[3][24] = 241688675; c[3][25] = 251776153; c[3][26] = 261829432; c[3][27] = 271989918; c[3][28] = 282063980; c[3][29] = 292616461; c[3][30] = 302977822; c[3][31] = 313117791; c[3][32] = 323260113; c[3][33] = 333334566; c[3][34] = 343475818; c[3][35] = 353567409; c[3][36] = 363691300; c[3][37] = 373801023; c[3][38] = 383908068; c[3][39] = 394043942; c[3][40] = 404144704; c[3][41] = 414264322; c[3][42] = 424373242; c[3][43] = 434480336; c[3][44] = 444588680; c[3][45] = 454721902; c[3][46] = 464810971; c[3][47] = 474945495; c[3][48] = 485052554; c[3][49] = 495162278; c[3][50] = 505299376; c[3][51] = 515377716; c[3][52] = 525519012; c[3][53] = 535593155; c[3][54] = 545767448; c[3][55] = 555878902; c[3][56] = 565984222; c[3][57] = 576499622; c[3][58] = 586890963; c[3][59] = 597037907; c[3][60] = 607145131; c[3][61] = 617253388; c[3][62] = 627393572; c[3][63] = 637468872; c[3][64] = 647611330; c[3][65] = 657719847; c[3][66] = 667826941; c[3][67] = 677935677; c[3][68] = 688044872; c[3][69] = 698180067; c[3][70] = 708292150; c[3][71] = 718399203; c[3][72] = 728508876; c[3][73] = 738617307; c[3][74] = 748724322; c[3][75] = 758864369; c[3][76] = 768947731; c[3][77] = 779081055; c[3][78] = 789189533; c[3][79] = 799297918; c[3][80] = 809469221; c[3][81] = 819546310; c[3][82] = 829687563; c[3][83] = 839763042; c[3][84] = 849903045; c[3][85] = 860382858; c[3][86] = 870773491; c[3][87] = 880955568; c[3][88] = 891033580; c[3][89] = 901173590; c[3][90] = 911280644; c[3][91] = 921389030; c[3][92] = 931529034; c[3][93] = 941605805; c[3][94] = 951745765; c[3][95] = 961854240; c[3][96] = 971962545; c[3][97] = 982070961; c[3][98] = 992180335; c[3][99] = 1002316827; c[3][100] = 1012427749; c[3][101] = 1022534801; c[3][102] = 1032643231; c[3][103] = 1042752902; c[3][104] = 1052860006; c[3][105] = 1062999967; c[3][106] = 1073083997; c[3][107] = 1083249728; c[3][108] = 1093358126; c[3][109] = 1103466468; c[3][110] = 1113605206; c[3][111] = 1123681908; c[3][112] = 1133823097; c[3][113] = 1143993474; c[3][114] = 1154643087; c[3][115] = 1164875683; c[3][116] = 1174953745; c[3][117] = 1185091122; c[3][118] = 1195169228; c[3][119] = 1205308922; c[3][120] = 1215416328; c[3][121] = 1225526002; c[3][122] = 1235664719; c[3][123] = 1245742464; c[3][124] = 1255881405; c[3][125] = 1265989875; c[3][126] = 1276098180; c[3][127] = 1286205320; c[3][128] = 1296326511; c[3][129] = 1306453590; c[3][130] = 1316563346; c[3][131] = 1326671823; c[3][132] = 1336778785; c[3][133] = 1346921504; c[3][134] = 1357028556; c[3][135] = 1367168551; c[3][136] = 1377246621; c[3][137] = 1387384044; c[3][138] = 1397493678; c[3][139] = 1407602065; c[3][140] = 1417740779; c[3][141] = 1427876682; c[3][142] = 1438525577; c[3][143] = 1448756589; c[3][144] = 1458870029; c[3][145] = 1469011242; c[3][146] = 1479089343; c[3][147] = 1489226775; c[3][148] = 1499333863; c[3][149] = 1509443493; c[3][150] = 1519551925; c[3][151] = 1529661611; c[3][152] = 1539800314; c[3][153] = 1549908703; c[3][154] = 1560015757; c[3][155] = 1570125435; c[3][156] = 1580233910; c[3][157] = 1590340914; c[3][158] = 1600481895; c[3][159] = 1610581777; c[3][160] = 1620731932; c[3][161] = 1630840372; c[3][162] = 1640947378; c[3][163] = 1651080615; c[3][164] = 1661165091; c[3][165] = 1671304158; c[3][166] = 1681382264; c[3][167] = 1691519685; c[3][168] = 1701657188; c[3][169] = 1711759211; c[3][170] = 1722407491; c[3][171] = 1732650232; c[3][172] = 1742790142; c[3][173] = 1752899821; c[3][174] = 1763006957; c[3][175] = 1773146966; c[3][176] = 1783223735; c[3][177] = 1793362404; c[3][178] = 1803472083; c[3][179] = 1813580471; c[3][180] = 1823687566; c[3][181] = 1833796307; c[3][182] = 1843935913; c[3][183] = 1854044346; c[3][184] = 1864152688; c[3][185] = 1874261038; c[3][186] = 1884402460; c[3][187] = 1894509476; c[3][188] = 1904649558; c[3][189] = 1914727222; c[3][190] = 1924866335; c[3][191] = 1934975574; c[3][192] = 1945083019; c[3][193] = 1955224317; c[3][194] = 1965299797; c[3][195] = 1975439798; c[3][196] = 1985517950; c[3][197] = 1995655283; c[3][198] = 2006276380; c[3][199] = 2016569050; c[4][1] = 10000760; c[4][2] = 20004106; c[4][3] = 30005636; c[4][4] = 40009442; c[4][5] = 50010968; c[4][6] = 60014778; c[4][7] = 70016361; c[4][8] = 80020114; c[4][9] = 90023745; c[4][10] = 100025446; c[4][11] = 110029200; c[4][12] = 120030832; c[4][13] = 130034534; c[4][14] = 140036168; c[4][15] = 150039866; c[4][16] = 160041504; c[4][17] = 170045254; c[4][18] = 180048957; c[4][19] = 190050532; c[4][20] = 200054284; c[4][21] = 210055864; c[4][22] = 220059638; c[4][23] = 230061197; c[4][24] = 240065010; c[4][25] = 250066953; c[4][26] = 260070343; c[4][27] = 270078258; c[4][28] = 280080967; c[4][29] = 290127491; c[4][30] = 300151151; c[4][31] = 310160647; c[4][32] = 320164604; c[4][33] = 330169581; c[4][34] = 340175777; c[4][35] = 350178516; c[4][36] = 360185246; c[4][37] = 370187504; c[4][38] = 380194121; c[4][39] = 390198618; c[4][40] = 400203055; c[4][41] = 410207553; c[4][42] = 420212049; c[4][43] = 430218662; c[4][44] = 440223159; c[4][45] = 450227596; c[4][46] = 460232092; c[4][47] = 470236590; c[4][48] = 480241026; c[4][49] = 490247699; c[4][50] = 500250071; c[4][51] = 510256632; c[4][52] = 520261189; c[4][53] = 530265567; c[4][54] = 540274531; c[4][55] = 550276685; c[4][56] = 560283408; c[4][57] = 570285610; c[4][58] = 580342938; c[4][59] = 590360914; c[4][60] = 600365405; c[4][61] = 610372019; c[4][62] = 620374339; c[4][63] = 630381009; c[4][64] = 640385264; c[4][65] = 650389945; c[4][66] = 660394442; c[4][67] = 670398880; c[4][68] = 680405436; c[4][69] = 690409514; c[4][70] = 700414484; c[4][71] = 710418982; c[4][72] = 720423419; c[4][73] = 730427915; c[4][74] = 740432558; c[4][75] = 750436791; c[4][76] = 760443523; c[4][77] = 770447900; c[4][78] = 780452456; c[4][79] = 790456894; c[4][80] = 800461390; c[4][81] = 810470237; c[4][82] = 820472559; c[4][83] = 830479173; c[4][84] = 840481553; c[4][85] = 850488107; c[4][86] = 860534741; c[4][87] = 870558168; c[4][88] = 880567900; c[4][89] = 890570222; c[4][90] = 900576834; c[4][91] = 910581337; c[4][92] = 920585768; c[4][93] = 930591802; c[4][94] = 940594704; c[4][95] = 950601374; c[4][96] = 960605813; c[4][97] = 970610308; c[4][98] = 980614746; c[4][99] = 990619252; c[4][100] = 1000623740; c[4][101] = 1010630234; c[4][102] = 1020634848; c[4][103] = 1030639345; c[4][104] = 1040643842; c[4][105] = 1050648279; c[4][106] = 1060654528; c[4][107] = 1070657213; c[4][108] = 1080666121; c[4][109] = 1090668444; c[4][110] = 1100675054; c[4][111] = 1110679546; c[4][112] = 1120683934; c[4][113] = 1130690660; c[4][114] = 1140692863; c[4][115] = 1150750142; c[4][116] = 1160765985; c[4][117] = 1170772658; c[4][118] = 1180777158; c[4][119] = 1190781591; c[4][120] = 1200788205; c[4][121] = 1210790585; c[4][122] = 1220797139; c[4][123] = 1230801694; c[4][124] = 1240806131; c[4][125] = 1250810628; c[4][126] = 1260815126; c[4][127] = 1270821681; c[4][128] = 1280826235; c[4][129] = 1290830672; c[4][130] = 1300835168; c[4][131] = 1310839606; c[4][132] = 1320844043; c[4][133] = 1330850715; c[4][134] = 1340852977; c[4][135] = 1350861885; c[4][136] = 1360866251; c[4][137] = 1370870875; c[4][138] = 1380875316; c[4][139] = 1390879812; c[4][140] = 1400886424; c[4][141] = 1410888806; c[4][142] = 1420895359; c[4][143] = 1430908260; c[4][144] = 1440964298; c[4][145] = 1450975152; c[4][146] = 1460977416; c[4][147] = 1470984086; c[4][148] = 1480986408; c[4][149] = 1490993020; c[4][150] = 1500997464; c[4][151] = 1511001956; c[4][152] = 1521006920; c[4][153] = 1531010891; c[4][154] = 1541017502; c[4][155] = 1551021999; c[4][156] = 1561026496; c[4][157] = 1571030933; c[4][158] = 1581035429; c[4][159] = 1591039909; c[4][160] = 1601046539; c[4][161] = 1611052791; c[4][162] = 1621057707; c[4][163] = 1631062263; c[4][164] = 1641066642; c[4][165] = 1651072965; c[4][166] = 1661075636; c[4][167] = 1671082308; c[4][168] = 1681084560; c[4][169] = 1691091182; c[4][170] = 1701095911; c[4][171] = 1711100115; c[4][172] = 1721157240; c[4][173] = 1731173239; c[4][174] = 1741179909; c[4][175] = 1751182231; c[4][176] = 1761188845; c[4][177] = 1771193404; c[4][178] = 1781197837; c[4][179] = 1791204391; c[4][180] = 1801206771; c[4][181] = 1811213325; c[4][182] = 1821217881; c[4][183] = 1831222323; c[4][184] = 1841226815; c[4][185] = 1851231313; c[4][186] = 1861237925; c[4][187] = 1871242419; c[4][188] = 1881249095; c[4][189] = 1891253531; c[4][190] = 1901258028; c[4][191] = 1911262465; c[4][192] = 1921269137; c[4][193] = 1931271459; c[4][194] = 1941278072; c[4][195] = 1951282453; c[4][196] = 1961287005; c[4][197] = 1971293671; c[4][198] = 1981296058; c[4][199] = 1991302612; c[5][1] = 10000008; c[5][2] = 20000096; c[5][3] = 30000114; c[5][4] = 40000208; c[5][5] = 50000226; c[5][6] = 60000320; c[5][7] = 70000339; c[5][8] = 80000432; c[5][9] = 90000522; c[5][10] = 100000544; c[5][11] = 110000637; c[5][12] = 120000656; c[5][13] = 130000749; c[5][14] = 140000768; c[5][15] = 150000861; c[5][16] = 160000880; c[5][17] = 170000973; c[5][18] = 180001065; c[5][19] = 190001085; c[5][20] = 200001178; c[5][21] = 210001197; c[5][22] = 220001290; c[5][23] = 230001309; c[5][24] = 240001402; c[5][25] = 250001427; c[5][26] = 260001514; c[5][27] = 270001751; c[5][28] = 280001787; c[5][29] = 290004429; c[5][30] = 300005186; c[5][31] = 310005454; c[5][32] = 320005544; c[5][33] = 330005659; c[5][34] = 340005766; c[5][35] = 350005863; c[5][36] = 360006040; c[5][37] = 370006069; c[5][38] = 380006246; c[5][39] = 390006348; c[5][40] = 400006451; c[5][41] = 410006553; c[5][42] = 420006656; c[5][43] = 430006833; c[5][44] = 440006935; c[5][45] = 450007038; c[5][46] = 460007140; c[5][47] = 470007244; c[5][48] = 480007345; c[5][49] = 490007523; c[5][50] = 500007550; c[5][51] = 510007727; c[5][52] = 520007828; c[5][53] = 530007932; c[5][54] = 540008183; c[5][55] = 550008212; c[5][56] = 560008388; c[5][57] = 570008417; c[5][58] = 580011412; c[5][59] = 590011977; c[5][60] = 600012085; c[5][61] = 610012261; c[5][62] = 620012290; c[5][63] = 630012466; c[5][64] = 640012559; c[5][65] = 650012671; c[5][66] = 660012774; c[5][67] = 670012876; c[5][68] = 680012989; c[5][69] = 690013082; c[5][70] = 700013257; c[5][71] = 710013360; c[5][72] = 720013462; c[5][73] = 730013566; c[5][74] = 740013668; c[5][75] = 750013771; c[5][76] = 760013947; c[5][77] = 770014050; c[5][78] = 780014152; c[5][79] = 790014256; c[5][80] = 800014357; c[5][81] = 810014610; c[5][82] = 820014638; c[5][83] = 830014815; c[5][84] = 840014843; c[5][85] = 850015019; c[5][86] = 860017595; c[5][87] = 870018357; c[5][88] = 880018686; c[5][89] = 890018715; c[5][90] = 900018891; c[5][91] = 910018992; c[5][92] = 920019096; c[5][93] = 930019199; c[5][94] = 940019302; c[5][95] = 950019478; c[5][96] = 960019574; c[5][97] = 970019683; c[5][98] = 980019786; c[5][99] = 990019889; c[5][100] = 1000019991; c[5][101] = 1010020152; c[5][102] = 1020020211; c[5][103] = 1030020373; c[5][104] = 1040020474; c[5][105] = 1050020578; c[5][106] = 1060020680; c[5][107] = 1070020783; c[5][108] = 1080021034; c[5][109] = 1090021063; c[5][110] = 1100021239; c[5][111] = 1110021337; c[5][112] = 1120021445; c[5][113] = 1130021622; c[5][114] = 1140021650; c[5][115] = 1150024590; c[5][116] = 1160025141; c[5][117] = 1170025317; c[5][118] = 1180025420; c[5][119] = 1190025521; c[5][120] = 1200025698; c[5][121] = 1210025727; c[5][122] = 1220025903; c[5][123] = 1230026006; c[5][124] = 1240026108; c[5][125] = 1250026211; c[5][126] = 1260026314; c[5][127] = 1270026419; c[5][128] = 1280026589; c[5][129] = 1290026695; c[5][130] = 1300026798; c[5][131] = 1310026902; c[5][132] = 1320027003; c[5][133] = 1330027172; c[5][134] = 1340027208; c[5][135] = 1350027461; c[5][136] = 1360027489; c[5][137] = 1370027665; c[5][138] = 1380027767; c[5][139] = 1390027870; c[5][140] = 1400028046; c[5][141] = 1410028076; c[5][142] = 1420028251; c[5][143] = 1430028574; c[5][144] = 1440031526; c[5][145] = 1450031917; c[5][146] = 1460031948; c[5][147] = 1470032124; c[5][148] = 1480032153; c[5][149] = 1490032329; c[5][150] = 1500032432; c[5][151] = 1510032535; c[5][152] = 1520032637; c[5][153] = 1530032739; c[5][154] = 1540032915; c[5][155] = 1550033018; c[5][156] = 1560033120; c[5][157] = 1570033223; c[5][158] = 1580033326; c[5][159] = 1590033429; c[5][160] = 1600033604; c[5][161] = 1610033641; c[5][162] = 1620033885; c[5][163] = 1630033989; c[5][164] = 1640034090; c[5][165] = 1650034194; c[5][166] = 1660034295; c[5][167] = 1670034473; c[5][168] = 1680034501; c[5][169] = 1690034678; c[5][170] = 1700034710; c[5][171] = 1710034882; c[5][172] = 1720037757; c[5][173] = 1730038373; c[5][174] = 1740038549; c[5][175] = 1750038578; c[5][176] = 1760038754; c[5][177] = 1770038857; c[5][178] = 1780038960; c[5][179] = 1790039136; c[5][180] = 1800039165; c[5][181] = 1810039341; c[5][182] = 1820039444; c[5][183] = 1830039547; c[5][184] = 1840039649; c[5][185] = 1850039752; c[5][186] = 1860039854; c[5][187] = 1870040031; c[5][188] = 1880040208; c[5][189] = 1890040311; c[5][190] = 1900040413; c[5][191] = 1910040516; c[5][192] = 1920040620; c[5][193] = 1930040722; c[5][194] = 1940040897; c[5][195] = 1950040927; c[5][196] = 1960041102; c[5][197] = 1970041206; c[5][198] = 1980041308; c[5][199] = 1990041485; c[6][1] = 10000000; c[6][2] = 20000001; c[6][3] = 30000001; c[6][4] = 40000002; c[6][5] = 50000002; c[6][6] = 60000003; c[6][7] = 70000003; c[6][8] = 80000004; c[6][9] = 90000005; c[6][10] = 100000005; c[6][11] = 110000006; c[6][12] = 120000006; c[6][13] = 130000007; c[6][14] = 140000007; c[6][15] = 150000008; c[6][16] = 160000008; c[6][17] = 170000009; c[6][18] = 180000010; c[6][19] = 190000010; c[6][20] = 200000011; c[6][21] = 210000011; c[6][22] = 220000012; c[6][23] = 230000012; c[6][24] = 240000013; c[6][25] = 250000013; c[6][26] = 260000014; c[6][27] = 270000017; c[6][28] = 280000017; c[6][29] = 290000097; c[6][30] = 300000107; c[6][31] = 310000110; c[6][32] = 320000111; c[6][33] = 330000112; c[6][34] = 340000113; c[6][35] = 350000114; c[6][36] = 360000116; c[6][37] = 370000116; c[6][38] = 380000118; c[6][39] = 390000119; c[6][40] = 400000120; c[6][41] = 410000121; c[6][42] = 420000122; c[6][43] = 430000124; c[6][44] = 440000125; c[6][45] = 450000126; c[6][46] = 460000127; c[6][47] = 470000128; c[6][48] = 480000129; c[6][49] = 490000131; c[6][50] = 500000131; c[6][51] = 510000133; c[6][52] = 520000134; c[6][53] = 530000135; c[6][54] = 540000138; c[6][55] = 550000138; c[6][56] = 560000140; c[6][57] = 570000140; c[6][58] = 580000224; c[6][59] = 590000232; c[6][60] = 600000233; c[6][61] = 610000235; c[6][62] = 620000235; c[6][63] = 630000237; c[6][64] = 640000238; c[6][65] = 650000239; c[6][66] = 660000240; c[6][67] = 670000241; c[6][68] = 680000242; c[6][69] = 690000243; c[6][70] = 700000245; c[6][71] = 710000246; c[6][72] = 720000247; c[6][73] = 730000248; c[6][74] = 740000249; c[6][75] = 750000250; c[6][76] = 760000252; c[6][77] = 770000253; c[6][78] = 780000254; c[6][79] = 790000255; c[6][80] = 800000256; c[6][81] = 810000259; c[6][82] = 820000259; c[6][83] = 830000261; c[6][84] = 840000261; c[6][85] = 850000263; c[6][86] = 860000342; c[6][87] = 870000352; c[6][88] = 880000356; c[6][89] = 890000356; c[6][90] = 900000358; c[6][91] = 910000359; c[6][92] = 920000360; c[6][93] = 930000361; c[6][94] = 940000362; c[6][95] = 950000364; c[6][96] = 960000365; c[6][97] = 970000366; c[6][98] = 980000367; c[6][99] = 990000368; c[6][100] = 1000000369; c[6][101] = 1010000371; c[6][102] = 1020000371; c[6][103] = 1030000373; c[6][104] = 1040000374; c[6][105] = 1050000375; c[6][106] = 1060000376; c[6][107] = 1070000377; c[6][108] = 1080000380; c[6][109] = 1090000380; c[6][110] = 1100000382; c[6][111] = 1110000383; c[6][112] = 1120000384; c[6][113] = 1130000386; c[6][114] = 1140000386; c[6][115] = 1150000470; c[6][116] = 1160000477; c[6][117] = 1170000479; c[6][118] = 1180000480; c[6][119] = 1190000481; c[6][120] = 1200000483; c[6][121] = 1210000483; c[6][122] = 1220000485; c[6][123] = 1230000486; c[6][124] = 1240000487; c[6][125] = 1250000488; c[6][126] = 1260000489; c[6][127] = 1270000490; c[6][128] = 1280000492; c[6][129] = 1290000493; c[6][130] = 1300000494; c[6][131] = 1310000495; c[6][132] = 1320000496; c[6][133] = 1330000498; c[6][134] = 1340000498; c[6][135] = 1350000501; c[6][136] = 1360000501; c[6][137] = 1370000503; c[6][138] = 1380000504; c[6][139] = 1390000505; c[6][140] = 1400000507; c[6][141] = 1410000507; c[6][142] = 1420000509; c[6][143] = 1430000513; c[6][144] = 1440000597; c[6][145] = 1450000602; c[6][146] = 1460000602; c[6][147] = 1470000604; c[6][148] = 1480000604; c[6][149] = 1490000606; c[6][150] = 1500000607; c[6][151] = 1510000608; c[6][152] = 1520000609; c[6][153] = 1530000610; c[6][154] = 1540000612; c[6][155] = 1550000613; c[6][156] = 1560000614; c[6][157] = 1570000615; c[6][158] = 1580000616; c[6][159] = 1590000617; c[6][160] = 1600000619; c[6][161] = 1610000619; c[6][162] = 1620000622; c[6][163] = 1630000623; c[6][164] = 1640000624; c[6][165] = 1650000625; c[6][166] = 1660000626; c[6][167] = 1670000628; c[6][168] = 1680000628; c[6][169] = 1690000630; c[6][170] = 1700000630; c[6][171] = 1710000632; c[6][172] = 1720000715; c[6][173] = 1730000723; c[6][174] = 1740000725; c[6][175] = 1750000725; c[6][176] = 1760000727; c[6][177] = 1770000728; c[6][178] = 1780000729; c[6][179] = 1790000731; c[6][180] = 1800000731; c[6][181] = 1810000733; c[6][182] = 1820000734; c[6][183] = 1830000735; c[6][184] = 1840000736; c[6][185] = 1850000737; c[6][186] = 1860000738; c[6][187] = 1870000740; c[6][188] = 1880000742; c[6][189] = 1890000743; c[6][190] = 1900000744; c[6][191] = 1910000745; c[6][192] = 1920000746; c[6][193] = 1930000747; c[6][194] = 1940000749; c[6][195] = 1950000749; c[6][196] = 1960000751; c[6][197] = 1970000752; c[6][198] = 1980000753; c[6][199] = 1990000755; c[7][1] = 10000000; c[7][2] = 20000000; c[7][3] = 30000000; c[7][4] = 40000000; c[7][5] = 50000000; c[7][6] = 60000000; c[7][7] = 70000000; c[7][8] = 80000000; c[7][9] = 90000000; c[7][10] = 100000000; c[7][11] = 110000000; c[7][12] = 120000000; c[7][13] = 130000000; c[7][14] = 140000000; c[7][15] = 150000000; c[7][16] = 160000000; c[7][17] = 170000000; c[7][18] = 180000000; c[7][19] = 190000000; c[7][20] = 200000000; c[7][21] = 210000000; c[7][22] = 220000000; c[7][23] = 230000000; c[7][24] = 240000000; c[7][25] = 250000000; c[7][26] = 260000000; c[7][27] = 270000000; c[7][28] = 280000000; c[7][29] = 290000001; c[7][30] = 300000001; c[7][31] = 310000001; c[7][32] = 320000001; c[7][33] = 330000001; c[7][34] = 340000001; c[7][35] = 350000001; c[7][36] = 360000001; c[7][37] = 370000001; c[7][38] = 380000001; c[7][39] = 390000001; c[7][40] = 400000001; c[7][41] = 410000001; c[7][42] = 420000001; c[7][43] = 430000001; c[7][44] = 440000001; c[7][45] = 450000001; c[7][46] = 460000001; c[7][47] = 470000001; c[7][48] = 480000001; c[7][49] = 490000001; c[7][50] = 500000001; c[7][51] = 510000001; c[7][52] = 520000001; c[7][53] = 530000001; c[7][54] = 540000001; c[7][55] = 550000001; c[7][56] = 560000001; c[7][57] = 570000001; c[7][58] = 580000002; c[7][59] = 590000002; c[7][60] = 600000002; c[7][61] = 610000002; c[7][62] = 620000002; c[7][63] = 630000002; c[7][64] = 640000002; c[7][65] = 650000002; c[7][66] = 660000002; c[7][67] = 670000002; c[7][68] = 680000002; c[7][69] = 690000002; c[7][70] = 700000002; c[7][71] = 710000002; c[7][72] = 720000002; c[7][73] = 730000002; c[7][74] = 740000002; c[7][75] = 750000002; c[7][76] = 760000002; c[7][77] = 770000002; c[7][78] = 780000002; c[7][79] = 790000002; c[7][80] = 800000002; c[7][81] = 810000002; c[7][82] = 820000002; c[7][83] = 830000002; c[7][84] = 840000002; c[7][85] = 850000002; c[7][86] = 860000003; c[7][87] = 870000003; c[7][88] = 880000003; c[7][89] = 890000003; c[7][90] = 900000003; c[7][91] = 910000003; c[7][92] = 920000003; c[7][93] = 930000003; c[7][94] = 940000003; c[7][95] = 950000003; c[7][96] = 960000003; c[7][97] = 970000003; c[7][98] = 980000003; c[7][99] = 990000003; c[7][100] = 1000000003; c[7][101] = 1010000003; c[7][102] = 1020000003; c[7][103] = 1030000003; c[7][104] = 1040000003; c[7][105] = 1050000003; c[7][106] = 1060000003; c[7][107] = 1070000003; c[7][108] = 1080000003; c[7][109] = 1090000003; c[7][110] = 1100000003; c[7][111] = 1110000003; c[7][112] = 1120000003; c[7][113] = 1130000003; c[7][114] = 1140000003; c[7][115] = 1150000004; c[7][116] = 1160000004; c[7][117] = 1170000004; c[7][118] = 1180000004; c[7][119] = 1190000004; c[7][120] = 1200000004; c[7][121] = 1210000004; c[7][122] = 1220000004; c[7][123] = 1230000004; c[7][124] = 1240000004; c[7][125] = 1250000004; c[7][126] = 1260000004; c[7][127] = 1270000004; c[7][128] = 1280000004; c[7][129] = 1290000004; c[7][130] = 1300000004; c[7][131] = 1310000004; c[7][132] = 1320000004; c[7][133] = 1330000004; c[7][134] = 1340000004; c[7][135] = 1350000004; c[7][136] = 1360000004; c[7][137] = 1370000004; c[7][138] = 1380000004; c[7][139] = 1390000004; c[7][140] = 1400000004; c[7][141] = 1410000004; c[7][142] = 1420000004; c[7][143] = 1430000004; c[7][144] = 1440000005; c[7][145] = 1450000005; c[7][146] = 1460000005; c[7][147] = 1470000005; c[7][148] = 1480000005; c[7][149] = 1490000005; c[7][150] = 1500000005; c[7][151] = 1510000005; c[7][152] = 1520000005; c[7][153] = 1530000005; c[7][154] = 1540000005; c[7][155] = 1550000005; c[7][156] = 1560000005; c[7][157] = 1570000005; c[7][158] = 1580000005; c[7][159] = 1590000005; c[7][160] = 1600000005; c[7][161] = 1610000005; c[7][162] = 1620000005; c[7][163] = 1630000005; c[7][164] = 1640000005; c[7][165] = 1650000005; c[7][166] = 1660000005; c[7][167] = 1670000005; c[7][168] = 1680000005; c[7][169] = 1690000005; c[7][170] = 1700000005; c[7][171] = 1710000005; c[7][172] = 1720000006; c[7][173] = 1730000006; c[7][174] = 1740000006; c[7][175] = 1750000006; c[7][176] = 1760000006; c[7][177] = 1770000006; c[7][178] = 1780000006; c[7][179] = 1790000006; c[7][180] = 1800000006; c[7][181] = 1810000006; c[7][182] = 1820000006; c[7][183] = 1830000006; c[7][184] = 1840000006; c[7][185] = 1850000006; c[7][186] = 1860000006; c[7][187] = 1870000006; c[7][188] = 1880000006; c[7][189] = 1890000006; c[7][190] = 1900000006; c[7][191] = 1910000006; c[7][192] = 1920000006; c[7][193] = 1930000006; c[7][194] = 1940000006; c[7][195] = 1950000006; c[7][196] = 1960000006; c[7][197] = 1970000006; c[7][198] = 1980000006; c[7][199] = 1990000006; c[8][1] = 10000000; c[8][2] = 20000000; c[8][3] = 30000000; c[8][4] = 40000000; c[8][5] = 50000000; c[8][6] = 60000000; c[8][7] = 70000000; c[8][8] = 80000000; c[8][9] = 90000000; c[8][10] = 100000000; c[8][11] = 110000000; c[8][12] = 120000000; c[8][13] = 130000000; c[8][14] = 140000000; c[8][15] = 150000000; c[8][16] = 160000000; c[8][17] = 170000000; c[8][18] = 180000000; c[8][19] = 190000000; c[8][20] = 200000000; c[8][21] = 210000000; c[8][22] = 220000000; c[8][23] = 230000000; c[8][24] = 240000000; c[8][25] = 250000000; c[8][26] = 260000000; c[8][27] = 270000000; c[8][28] = 280000000; c[8][29] = 290000000; c[8][30] = 300000000; c[8][31] = 310000000; c[8][32] = 320000000; c[8][33] = 330000000; c[8][34] = 340000000; c[8][35] = 350000000; c[8][36] = 360000000; c[8][37] = 370000000; c[8][38] = 380000000; c[8][39] = 390000000; c[8][40] = 400000000; c[8][41] = 410000000; c[8][42] = 420000000; c[8][43] = 430000000; c[8][44] = 440000000; c[8][45] = 450000000; c[8][46] = 460000000; c[8][47] = 470000000; c[8][48] = 480000000; c[8][49] = 490000000; c[8][50] = 500000000; c[8][51] = 510000000; c[8][52] = 520000000; c[8][53] = 530000000; c[8][54] = 540000000; c[8][55] = 550000000; c[8][56] = 560000000; c[8][57] = 570000000; c[8][58] = 580000000; c[8][59] = 590000000; c[8][60] = 600000000; c[8][61] = 610000000; c[8][62] = 620000000; c[8][63] = 630000000; c[8][64] = 640000000; c[8][65] = 650000000; c[8][66] = 660000000; c[8][67] = 670000000; c[8][68] = 680000000; c[8][69] = 690000000; c[8][70] = 700000000; c[8][71] = 710000000; c[8][72] = 720000000; c[8][73] = 730000000; c[8][74] = 740000000; c[8][75] = 750000000; c[8][76] = 760000000; c[8][77] = 770000000; c[8][78] = 780000000; c[8][79] = 790000000; c[8][80] = 800000000; c[8][81] = 810000000; c[8][82] = 820000000; c[8][83] = 830000000; c[8][84] = 840000000; c[8][85] = 850000000; c[8][86] = 860000000; c[8][87] = 870000000; c[8][88] = 880000000; c[8][89] = 890000000; c[8][90] = 900000000; c[8][91] = 910000000; c[8][92] = 920000000; c[8][93] = 930000000; c[8][94] = 940000000; c[8][95] = 950000000; c[8][96] = 960000000; c[8][97] = 970000000; c[8][98] = 980000000; c[8][99] = 990000000; c[8][100] = 1000000000; c[8][101] = 1010000000; c[8][102] = 1020000000; c[8][103] = 1030000000; c[8][104] = 1040000000; c[8][105] = 1050000000; c[8][106] = 1060000000; c[8][107] = 1070000000; c[8][108] = 1080000000; c[8][109] = 1090000000; c[8][110] = 1100000000; c[8][111] = 1110000000; c[8][112] = 1120000000; c[8][113] = 1130000000; c[8][114] = 1140000000; c[8][115] = 1150000000; c[8][116] = 1160000000; c[8][117] = 1170000000; c[8][118] = 1180000000; c[8][119] = 1190000000; c[8][120] = 1200000000; c[8][121] = 1210000000; c[8][122] = 1220000000; c[8][123] = 1230000000; c[8][124] = 1240000000; c[8][125] = 1250000000; c[8][126] = 1260000000; c[8][127] = 1270000000; c[8][128] = 1280000000; c[8][129] = 1290000000; c[8][130] = 1300000000; c[8][131] = 1310000000; c[8][132] = 1320000000; c[8][133] = 1330000000; c[8][134] = 1340000000; c[8][135] = 1350000000; c[8][136] = 1360000000; c[8][137] = 1370000000; c[8][138] = 1380000000; c[8][139] = 1390000000; c[8][140] = 1400000000; c[8][141] = 1410000000; c[8][142] = 1420000000; c[8][143] = 1430000000; c[8][144] = 1440000000; c[8][145] = 1450000000; c[8][146] = 1460000000; c[8][147] = 1470000000; c[8][148] = 1480000000; c[8][149] = 1490000000; c[8][150] = 1500000000; c[8][151] = 1510000000; c[8][152] = 1520000000; c[8][153] = 1530000000; c[8][154] = 1540000000; c[8][155] = 1550000000; c[8][156] = 1560000000; c[8][157] = 1570000000; c[8][158] = 1580000000; c[8][159] = 1590000000; c[8][160] = 1600000000; c[8][161] = 1610000000; c[8][162] = 1620000000; c[8][163] = 1630000000; c[8][164] = 1640000000; c[8][165] = 1650000000; c[8][166] = 1660000000; c[8][167] = 1670000000; c[8][168] = 1680000000; c[8][169] = 1690000000; c[8][170] = 1700000000; c[8][171] = 1710000000; c[8][172] = 1720000000; c[8][173] = 1730000000; c[8][174] = 1740000000; c[8][175] = 1750000000; c[8][176] = 1760000000; c[8][177] = 1770000000; c[8][178] = 1780000000; c[8][179] = 1790000000; c[8][180] = 1800000000; c[8][181] = 1810000000; c[8][182] = 1820000000; c[8][183] = 1830000000; c[8][184] = 1840000000; c[8][185] = 1850000000; c[8][186] = 1860000000; c[8][187] = 1870000000; c[8][188] = 1880000000; c[8][189] = 1890000000; c[8][190] = 1900000000; c[8][191] = 1910000000; c[8][192] = 1920000000; c[8][193] = 1930000000; c[8][194] = 1940000000; c[8][195] = 1950000000; c[8][196] = 1960000000; c[8][197] = 1970000000; c[8][198] = 1980000000; c[8][199] = 1990000000; c[9][1] = 10000000; c[9][2] = 20000000; c[9][3] = 30000000; c[9][4] = 40000000; c[9][5] = 50000000; c[9][6] = 60000000; c[9][7] = 70000000; c[9][8] = 80000000; c[9][9] = 90000000; c[9][10] = 100000000; c[9][11] = 110000000; c[9][12] = 120000000; c[9][13] = 130000000; c[9][14] = 140000000; c[9][15] = 150000000; c[9][16] = 160000000; c[9][17] = 170000000; c[9][18] = 180000000; c[9][19] = 190000000; c[9][20] = 200000000; c[9][21] = 210000000; c[9][22] = 220000000; c[9][23] = 230000000; c[9][24] = 240000000; c[9][25] = 250000000; c[9][26] = 260000000; c[9][27] = 270000000; c[9][28] = 280000000; c[9][29] = 290000000; c[9][30] = 300000000; c[9][31] = 310000000; c[9][32] = 320000000; c[9][33] = 330000000; c[9][34] = 340000000; c[9][35] = 350000000; c[9][36] = 360000000; c[9][37] = 370000000; c[9][38] = 380000000; c[9][39] = 390000000; c[9][40] = 400000000; c[9][41] = 410000000; c[9][42] = 420000000; c[9][43] = 430000000; c[9][44] = 440000000; c[9][45] = 450000000; c[9][46] = 460000000; c[9][47] = 470000000; c[9][48] = 480000000; c[9][49] = 490000000; c[9][50] = 500000000; c[9][51] = 510000000; c[9][52] = 520000000; c[9][53] = 530000000; c[9][54] = 540000000; c[9][55] = 550000000; c[9][56] = 560000000; c[9][57] = 570000000; c[9][58] = 580000000; c[9][59] = 590000000; c[9][60] = 600000000; c[9][61] = 610000000; c[9][62] = 620000000; c[9][63] = 630000000; c[9][64] = 640000000; c[9][65] = 650000000; c[9][66] = 660000000; c[9][67] = 670000000; c[9][68] = 680000000; c[9][69] = 690000000; c[9][70] = 700000000; c[9][71] = 710000000; c[9][72] = 720000000; c[9][73] = 730000000; c[9][74] = 740000000; c[9][75] = 750000000; c[9][76] = 760000000; c[9][77] = 770000000; c[9][78] = 780000000; c[9][79] = 790000000; c[9][80] = 800000000; c[9][81] = 810000000; c[9][82] = 820000000; c[9][83] = 830000000; c[9][84] = 840000000; c[9][85] = 850000000; c[9][86] = 860000000; c[9][87] = 870000000; c[9][88] = 880000000; c[9][89] = 890000000; c[9][90] = 900000000; c[9][91] = 910000000; c[9][92] = 920000000; c[9][93] = 930000000; c[9][94] = 940000000; c[9][95] = 950000000; c[9][96] = 960000000; c[9][97] = 970000000; c[9][98] = 980000000; c[9][99] = 990000000; c[9][100] = 1000000000; c[9][101] = 1010000000; c[9][102] = 1020000000; c[9][103] = 1030000000; c[9][104] = 1040000000; c[9][105] = 1050000000; c[9][106] = 1060000000; c[9][107] = 1070000000; c[9][108] = 1080000000; c[9][109] = 1090000000; c[9][110] = 1100000000; c[9][111] = 1110000000; c[9][112] = 1120000000; c[9][113] = 1130000000; c[9][114] = 1140000000; c[9][115] = 1150000000; c[9][116] = 1160000000; c[9][117] = 1170000000; c[9][118] = 1180000000; c[9][119] = 1190000000; c[9][120] = 1200000000; c[9][121] = 1210000000; c[9][122] = 1220000000; c[9][123] = 1230000000; c[9][124] = 1240000000; c[9][125] = 1250000000; c[9][126] = 1260000000; c[9][127] = 1270000000; c[9][128] = 1280000000; c[9][129] = 1290000000; c[9][130] = 1300000000; c[9][131] = 1310000000; c[9][132] = 1320000000; c[9][133] = 1330000000; c[9][134] = 1340000000; c[9][135] = 1350000000; c[9][136] = 1360000000; c[9][137] = 1370000000; c[9][138] = 1380000000; c[9][139] = 1390000000; c[9][140] = 1400000000; c[9][141] = 1410000000; c[9][142] = 1420000000; c[9][143] = 1430000000; c[9][144] = 1440000000; c[9][145] = 1450000000; c[9][146] = 1460000000; c[9][147] = 1470000000; c[9][148] = 1480000000; c[9][149] = 1490000000; c[9][150] = 1500000000; c[9][151] = 1510000000; c[9][152] = 1520000000; c[9][153] = 1530000000; c[9][154] = 1540000000; c[9][155] = 1550000000; c[9][156] = 1560000000; c[9][157] = 1570000000; c[9][158] = 1580000000; c[9][159] = 1590000000; c[9][160] = 1600000000; c[9][161] = 1610000000; c[9][162] = 1620000000; c[9][163] = 1630000000; c[9][164] = 1640000000; c[9][165] = 1650000000; c[9][166] = 1660000000; c[9][167] = 1670000000; c[9][168] = 1680000000; c[9][169] = 1690000000; c[9][170] = 1700000000; c[9][171] = 1710000000; c[9][172] = 1720000000; c[9][173] = 1730000000; c[9][174] = 1740000000; c[9][175] = 1750000000; c[9][176] = 1760000000; c[9][177] = 1770000000; c[9][178] = 1780000000; c[9][179] = 1790000000; c[9][180] = 1800000000; c[9][181] = 1810000000; c[9][182] = 1820000000; c[9][183] = 1830000000; c[9][184] = 1840000000; c[9][185] = 1850000000; c[9][186] = 1860000000; c[9][187] = 1870000000; c[9][188] = 1880000000; c[9][189] = 1890000000; c[9][190] = 1900000000; c[9][191] = 1910000000; c[9][192] = 1920000000; c[9][193] = 1930000000; c[9][194] = 1940000000; c[9][195] = 1950000000; c[9][196] = 1960000000; c[9][197] = 1970000000; c[9][198] = 1980000000; c[9][199] = 1990000000; c[10][1] = 10000000; c[10][2] = 20000000; c[10][3] = 30000000; c[10][4] = 40000000; c[10][5] = 50000000; c[10][6] = 60000000; c[10][7] = 70000000; c[10][8] = 80000000; c[10][9] = 90000000; c[10][10] = 100000000; c[10][11] = 110000000; c[10][12] = 120000000; c[10][13] = 130000000; c[10][14] = 140000000; c[10][15] = 150000000; c[10][16] = 160000000; c[10][17] = 170000000; c[10][18] = 180000000; c[10][19] = 190000000; c[10][20] = 200000000; c[10][21] = 210000000; c[10][22] = 220000000; c[10][23] = 230000000; c[10][24] = 240000000; c[10][25] = 250000000; c[10][26] = 260000000; c[10][27] = 270000000; c[10][28] = 280000000; c[10][29] = 290000000; c[10][30] = 300000000; c[10][31] = 310000000; c[10][32] = 320000000; c[10][33] = 330000000; c[10][34] = 340000000; c[10][35] = 350000000; c[10][36] = 360000000; c[10][37] = 370000000; c[10][38] = 380000000; c[10][39] = 390000000; c[10][40] = 400000000; c[10][41] = 410000000; c[10][42] = 420000000; c[10][43] = 430000000; c[10][44] = 440000000; c[10][45] = 450000000; c[10][46] = 460000000; c[10][47] = 470000000; c[10][48] = 480000000; c[10][49] = 490000000; c[10][50] = 500000000; c[10][51] = 510000000; c[10][52] = 520000000; c[10][53] = 530000000; c[10][54] = 540000000; c[10][55] = 550000000; c[10][56] = 560000000; c[10][57] = 570000000; c[10][58] = 580000000; c[10][59] = 590000000; c[10][60] = 600000000; c[10][61] = 610000000; c[10][62] = 620000000; c[10][63] = 630000000; c[10][64] = 640000000; c[10][65] = 650000000; c[10][66] = 660000000; c[10][67] = 670000000; c[10][68] = 680000000; c[10][69] = 690000000; c[10][70] = 700000000; c[10][71] = 710000000; c[10][72] = 720000000; c[10][73] = 730000000; c[10][74] = 740000000; c[10][75] = 750000000; c[10][76] = 760000000; c[10][77] = 770000000; c[10][78] = 780000000; c[10][79] = 790000000; c[10][80] = 800000000; c[10][81] = 810000000; c[10][82] = 820000000; c[10][83] = 830000000; c[10][84] = 840000000; c[10][85] = 850000000; c[10][86] = 860000000; c[10][87] = 870000000; c[10][88] = 880000000; c[10][89] = 890000000; c[10][90] = 900000000; c[10][91] = 910000000; c[10][92] = 920000000; c[10][93] = 930000000; c[10][94] = 940000000; c[10][95] = 950000000; c[10][96] = 960000000; c[10][97] = 970000000; c[10][98] = 980000000; c[10][99] = 990000000; c[10][100] = 1000000000; c[10][101] = 1010000000; c[10][102] = 1020000000; c[10][103] = 1030000000; c[10][104] = 1040000000; c[10][105] = 1050000000; c[10][106] = 1060000000; c[10][107] = 1070000000; c[10][108] = 1080000000; c[10][109] = 1090000000; c[10][110] = 1100000000; c[10][111] = 1110000000; c[10][112] = 1120000000; c[10][113] = 1130000000; c[10][114] = 1140000000; c[10][115] = 1150000000; c[10][116] = 1160000000; c[10][117] = 1170000000; c[10][118] = 1180000000; c[10][119] = 1190000000; c[10][120] = 1200000000; c[10][121] = 1210000000; c[10][122] = 1220000000; c[10][123] = 1230000000; c[10][124] = 1240000000; c[10][125] = 1250000000; c[10][126] = 1260000000; c[10][127] = 1270000000; c[10][128] = 1280000000; c[10][129] = 1290000000; c[10][130] = 1300000000; c[10][131] = 1310000000; c[10][132] = 1320000000; c[10][133] = 1330000000; c[10][134] = 1340000000; c[10][135] = 1350000000; c[10][136] = 1360000000; c[10][137] = 1370000000; c[10][138] = 1380000000; c[10][139] = 1390000000; c[10][140] = 1400000000; c[10][141] = 1410000000; c[10][142] = 1420000000; c[10][143] = 1430000000; c[10][144] = 1440000000; c[10][145] = 1450000000; c[10][146] = 1460000000; c[10][147] = 1470000000; c[10][148] = 1480000000; c[10][149] = 1490000000; c[10][150] = 1500000000; c[10][151] = 1510000000; c[10][152] = 1520000000; c[10][153] = 1530000000; c[10][154] = 1540000000; c[10][155] = 1550000000; c[10][156] = 1560000000; c[10][157] = 1570000000; c[10][158] = 1580000000; c[10][159] = 1590000000; c[10][160] = 1600000000; c[10][161] = 1610000000; c[10][162] = 1620000000; c[10][163] = 1630000000; c[10][164] = 1640000000; c[10][165] = 1650000000; c[10][166] = 1660000000; c[10][167] = 1670000000; c[10][168] = 1680000000; c[10][169] = 1690000000; c[10][170] = 1700000000; c[10][171] = 1710000000; c[10][172] = 1720000000; c[10][173] = 1730000000; c[10][174] = 1740000000; c[10][175] = 1750000000; c[10][176] = 1760000000; c[10][177] = 1770000000; c[10][178] = 1780000000; c[10][179] = 1790000000; c[10][180] = 1800000000; c[10][181] = 1810000000; c[10][182] = 1820000000; c[10][183] = 1830000000; c[10][184] = 1840000000; c[10][185] = 1850000000; c[10][186] = 1860000000; c[10][187] = 1870000000; c[10][188] = 1880000000; c[10][189] = 1890000000; c[10][190] = 1900000000; c[10][191] = 1910000000; c[10][192] = 1920000000; c[10][193] = 1930000000; c[10][194] = 1940000000; c[10][195] = 1950000000; c[10][196] = 1960000000; c[10][197] = 1970000000; c[10][198] = 1980000000; c[10][199] = 1990000000; } long long cnt[16], t; bool ok(long long x) { memset(cnt, 0, sizeof cnt); while (x) { long long r = x % 16; cnt[r]++; if (cnt[r] > t) return false; x /= 16; } return true; } const long long d = 1e7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long k; cin >> k >> t; if (t == 1) { long long lo = 1, hi = 5e10, md; while (hi - lo > 2) { md = (lo + hi) / 2; if (how(md) >= k) hi = md; else lo = md; } for (md = lo; md <= hi; md++) if (how(md) == k) break; prlong(md); } else { pre(); long long r = (k - 1) / d; long long st = c[t][r] + 1; k -= r * d; while (true) { k -= ok(st); if (k == 0) prlong(st); st++; } } return 0; }
17
#include <bits/stdc++.h> using namespace std; const long long INF = 5000000000000LL; int s[5000], e[5000], x[2 * 5000 + 1]; long long dp[2][2 * 5000 + 1]; int main() { ios::sync_with_stdio(0); int n, start; cin >> n >> start; x[0] = start; for (int i = 0; i < n; ++i) { cin >> s[i] >> e[i]; x[2 * i + 1] = s[i]; x[2 * i + 2] = e[i]; } int m = 2 * n + 1; sort(x, x + m); m = unique(x, x + m) - x; start = lower_bound(x, x + m, start) - x; fill(dp[0], dp[0] + m, INF); dp[0][start] = 0; for (int i = 0, r = 1; i < n; ++i, r ^= 1) { int p1 = lower_bound(x, x + m, s[i]) - x; int p2 = lower_bound(x, x + m, e[i]) - x; dp[r][p1] = dp[r][p2] = INF; long long best = INF; for (int j = 0; j < p1; ++j) { best = min(best, dp[r ^ 1][j] - x[j]); dp[r][j] = best + s[i]; } dp[r][p1] = min(best + s[i], dp[r ^ 1][p1]); best = INF; for (int j = m - 1; j > p2; --j) { best = min(best, dp[r ^ 1][j] + x[j]); dp[r][j] = best - e[i]; } dp[r][p2] = min(dp[r][p2], min(best - e[i], dp[r ^ 1][p2])); for (int j = p1 + 1; j < p2; ++j) dp[r][j] = dp[r ^ 1][j]; } long long ans = dp[n & 1][0]; for (int i = 1; i < m; ++i) ans = min(ans, dp[n & 1][i]); cout << ans << '\n'; return 0; }
13
#include <bits/stdc++.h> using namespace std; struct Point { int x; int y; }; bool onSegment(Point p, Point q, Point r) { if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) && q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y)) return true; return false; } int orientation(Point p, Point q, Point r) { int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); if (val == 0) return 0; return (val > 0) ? 1 : 2; } bool doIntersect(Point p1, Point q1, Point p2, Point q2) { int o1 = orientation(p1, q1, p2); int o2 = orientation(p1, q1, q2); int o3 = orientation(p2, q2, p1); int o4 = orientation(p2, q2, q1); if (o1 != o2 && o3 != o4) return true; if (o1 == 0 && onSegment(p1, p2, q1)) return true; if (o2 == 0 && onSegment(p1, q2, q1)) return true; if (o3 == 0 && onSegment(p2, p1, q2)) return true; if (o4 == 0 && onSegment(p2, q1, q2)) return true; return false; } bool isInside(Point polygon[], int n, Point p) { if (n < 3) return false; Point extreme = {10000000, p.y}; int count = 0, i = 0; do { int next = (i + 1) % n; if (doIntersect(polygon[i], polygon[next], p, extreme)) { if (orientation(polygon[i], p, polygon[next]) == 0) return onSegment(polygon[i], p, polygon[next]); count++; } i = next; } while (i != 0); return count & 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); Point sq[4], sqq[4]; for (int i = 0; i < 4; i++) { cin >> sq[i].x >> sq[i].y; } for (int i = 0; i < 4; i++) { cin >> sqq[i].x >> sqq[i].y; } vector<pair<Point, Point>> v, vv; for (int i = 0; i < 3; i++) { v.push_back({sq[i], sq[i + 1]}); } v.push_back({sq[3], sq[0]}); for (int i = 0; i < 3; i++) { vv.push_back({sqq[i], sqq[i + 1]}); } vv.push_back({sqq[3], sqq[0]}); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (doIntersect(v[i].first, v[i].second, vv[j].first, vv[j].second)) { cout << "YES"; return 0; } } } for (int i = 0; i < 4; i++) { if (isInside(sqq, 4, sq[i])) { cout << "YES"; return 0; } if (isInside(sq, 4, sqq[i])) { cout << "YES"; return 0; } } cout << "NO"; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int base = 29; struct BIT { int cnt[maxn]; void Init(void) { fill(begin(cnt), end(cnt), 0); } void upd(int i, int v) { for (; i; i -= i & -i) cnt[i] += v; } int sum(int i) { int res = 0; for (; i <= maxn - 5; i += i & -i) { res += cnt[i]; } return res; } } ft; void compress(vector<int> &a) { vector<int> b = a; sort(b.begin(), b.end()); for (auto &it : a) { it = lower_bound(b.begin(), b.end(), it) - b.begin() + 1; } } long long calc_inverse(vector<int> a) { long long res = 0; compress(a); for (auto it : a) { res += ft.sum(it + 1); ft.upd(it, 1); } return res; } int N; map<int, int> pos; set<int> si; signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; for (int i = 1; i <= N; ++i) { int x, y; cin >> x >> y; if (pos[x] == 0) pos[x] = x; if (pos[y] == 0) pos[y] = y; swap(pos[x], pos[y]); si.insert(x); si.insert(y); } vector<pair<int, int>> a; for (auto it : si) { a.push_back({pos[it], it}); } sort(a.begin(), a.end()); vector<int> b; for (auto it : a) b.push_back(it.second); long long res = 0; res += calc_inverse(b); vector<int> c; for (auto it : a) c.push_back(it.first); for (auto it : b) { int tmp = abs(it - pos[it]) - 1; if (tmp == 0) continue; int l = lower_bound(c.begin(), c.end(), min(it, pos[it]) + 1) - c.begin(); int r = lower_bound(c.begin(), c.end(), max(it, pos[it])) - c.begin(); tmp -= (r - l); res += tmp; } cout << res; }
13
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const int oo = 1 << 30; const long long loo = 1ll << 62; const int mods = 1e9 + 7; const int MAXN = 1000005; const int INF = 0x3f3f3f3f; inline int read() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } pair<int, int> b[MAXN]; map<int, int> Map; int n, m, a[MAXN], f[MAXN], Ans[MAXN], mxx[MAXN], mxy[MAXN]; int get(int x, int y) { return x * m + y; } int find(int x) { return f[x] == x ? f[x] : f[x] = find(f[x]); } void unions(int x, int y) { f[find(x)] = find(y); } void update(int r) { for (int l = r; l >= 0 && b[l].first == b[r].first; l--) Ans[b[l].second] = mxx[b[l].second / m] = mxy[b[l].second % m] = Ans[find(b[l].second)]; } signed main() { n = read(), m = read(); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[get(i, j)] = read(), f[get(i, j)] = get(i, j); for (int i = 0; i < n; i++) { Map.clear(); for (int j = 0; j < m; j++) { if (Map.find(a[get(i, j)]) != Map.end()) unions(Map[a[get(i, j)]], get(i, j)); Map[a[get(i, j)]] = get(i, j); } } for (int i = 0; i < m; i++) { Map.clear(); for (int j = 0; j < n; j++) { if (Map.find(a[get(j, i)]) != Map.end()) unions(Map[a[get(j, i)]], get(j, i)); Map[a[get(j, i)]] = get(j, i); } } for (int i = 0; i < n * m; i++) b[i] = make_pair(a[i], i); sort(b, b + n * m); for (int i = 0; i < n * m; i++) { if (i && b[i].first != b[i - 1].first) update(i - 1); int t = max(mxx[b[i].second / m], mxy[b[i].second % m]) + 1; upmax(Ans[find(b[i].second)], t); } update(n * m - 1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) printf("%d ", Ans[get(i, j)]); puts(""); } return 0; }
14
#include <bits/stdc++.h> using namespace ::std; int n, m; int a[105]; int pile[105]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); for (int i = 0; i < n; i++) { int j; for (j = m - 1; j >= 0; j--) if (pile[j] <= a[i]) break; if (j == -1) pile[m++] = 1; else pile[j]++; } printf("%d", m); return 0; }
6
#include <bits/stdc++.h> using namespace std; int Par[300]; bool Used[300]; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { int p; scanf("%d", &p); if (!Used[p]) { int Min = max(0, p - k + 1); int Last = p; int New; while (!Used[Last] && Last > Min) Last--; if (Used[Last]) { if (p - Par[Last] + 1 <= k) { int newP = Par[Last]; for (int j = Last + 1; j <= p; j++) { Used[j] = 1; Par[j] = newP; } } else { int newP = Last + 1; for (int j = Last + 1; j <= p; j++) { Used[j] = 1; Par[j] = newP; } } } else { int newP = Last; for (int j = Last; j <= p; j++) { Used[j] = 1; Par[j] = newP; } } } printf("%d ", Par[p]); } }
9
#include <bits/stdc++.h> using namespace std; const int N = 5E5 + 5, MX = 1E6 + 5, INF = 1E9 + 7; int n, q, u, v, a[N]; struct node { pair<int, int> mi; bool on; node(pair<int, int> _mi = {0, 1}, bool _on = false) : mi(_mi), on(_on) {} node operator+(const node &oth) const { pair<int, int> ans = {INF, 0}; if (on) { if (mi.first < ans.first) { ans = mi; } else if (mi.first == ans.first) { ans.second += mi.second; } } if (oth.on) { if (oth.mi.first < ans.first) { ans = oth.mi; } else if (oth.mi.first == ans.first) { ans.second += oth.mi.second; } } return node(ans, true); } }; struct segment_tree { node tr[4 * MX]; int lz[4 * MX]; void apply(int i, int v) { lz[i] += v; tr[i].mi.first += v; } void down(int i) { apply(i * 2, lz[i]); apply(i * 2 + 1, lz[i]); lz[i] = 0; } void build(int l, int r, int i) { if (l == r) { tr[i] = node(); return; } build(l, (l + r) / 2, i * 2); build((l + r) / 2 + 1, r, i * 2 + 1); tr[i] = tr[i * 2] + tr[i * 2 + 1]; } void update(int l, int r, int i, int L, int R, int v) { if (l > R || r < L || L > R) { return; } else if (L <= l && r <= R) { apply(i, v); return; } else { down(i); update(l, (l + r) / 2, i * 2, L, R, v); update((l + r) / 2 + 1, r, i * 2 + 1, L, R, v); tr[i] = tr[i * 2] + tr[i * 2 + 1]; } } void toggle(int l, int r, int i, int u) { if (l == r) { tr[i].on = !tr[i].on; } else { down(i); if (u <= (l + r) / 2) { toggle(l, (l + r) / 2, i * 2, u); } else { toggle((l + r) / 2 + 1, r, i * 2 + 1, u); } tr[i] = tr[i * 2] + tr[i * 2 + 1]; } } } seg; void update(int l, int r, int v) { if (l >= r) { swap(l, r); } seg.update(1, MX - 1, 1, l + 1, r, v); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> q; a[0] = MX - 1; for (int i = 1; i <= n; i++) { cin >> a[i]; seg.toggle(1, MX - 1, 1, a[i]); } for (int i = 0; i <= n; i++) { update(a[i], a[i + 1], 1); } while (q--) { cin >> u >> v; update(a[u - 1], a[u], -1); update(a[u], a[u + 1], -1); seg.toggle(1, MX - 1, 1, a[u]); a[u] = v; update(a[u - 1], a[u], 1); update(a[u], a[u + 1], 1); seg.toggle(1, MX - 1, 1, a[u]); cout << seg.tr[1].mi.second << '\n'; } }
25
#include <bits/stdc++.h> using namespace std; const int maxn = 66, maxm = 111111, mod = 1000000007; inline long long read() { char ch = getchar(); long long x = 0, f = 0; while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return f ? -x : x; } int m, n, w[maxn][maxn], id1[maxm], c1, id2[maxm], c2, fac[maxm], ans; long long e[maxn], ee[maxn]; unordered_map<long long, int> f[maxn][maxn]; inline void qmo(int &x) { x += (x >> 31) & mod; } int main() { m = read(); n = read(); while (n--) { int u = read(), v = read(), w = (read() - 1 + mod) % mod; if (!w) continue; if (!id1[u]) id1[u] = ++c1; u = id1[u]; if (!id2[v]) id2[v] = ++c2; v = id2[v]; ::w[u][v] = w; } for (int i = (c2); i >= (1); i--) { ee[i] = ee[i + 1]; for (int j = (1); j <= (c1); j++) if (w[j][i]) ee[i] |= 1ll << j; } f[0][0][0] = 1; for (int i = (1); i <= (c2); i++) for (int j = (0); j <= (i); j++) { for (auto k : f[i - 1][j]) qmo(f[i][j][k.first & ee[i]] += k.second - mod); if (j) for (int l = (1); l <= (c1); l++) if (w[l][i]) for (auto k : f[i - 1][j - 1]) if (!((k.first >> l) & 1)) qmo(f[i][j][(k.first | 1ll << l) & ee[i]] += 1ll * k.second * w[l][i] % mod - mod); } fac[0] = 1; for (int i = (1); i <= (m); i++) fac[i] = 1ll * fac[i - 1] * i % mod; for (int i = (0); i <= (min(c2, m)); i++) for (auto j : f[c2][i]) ans = (ans + 1ll * j.second * fac[m - i]) % mod; printf("%d\n", ans); }
23
#include <bits/stdc++.h> using namespace std; int n, m, arr[100][2], dp[201], suma; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d", &arr[i][0], &arr[i][1]); suma += arr[i][0]; } for (int i = 0; i < suma; i++) dp[i] = 1000000000; for (int i = 0; i < n; i++) for (int j = arr[i][0]; j <= suma; j++) if (dp[j] < 1000000000) dp[j - arr[i][0]] = min(dp[j - arr[i][0]], dp[j] + arr[i][1]); for (int i = 0; i <= suma; i++) if (i >= dp[i]) { printf("%d", i); return 0; } }
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long n, a = 0, b = 0, i = 0; string s, t; cin >> s >> t; n = s.length(); map<char, long long> m, m1; for (i = 0; i < t.size(); i++) ++m[t[i]]; for (i = 0; i < n; i++) { ++m1[s[i]]; } for (auto it : m1) { long long z = (long long)min(it.second, m[it.first]); a = a + z; m[it.first] = m[it.first] - z; m1[it.first] = m1[it.first] - z; } long long flag = 0; for (auto it : m1) { if (it.second != 0) { if (isupper(it.first)) { flag++; char c = it.first + 32; b = b + (long long)min(m1[it.first], m[c]); m1[c] = m1[it.first] - (long long)min(m1[it.first], m[c]); m[c] = m[c] - (long long)min(m1[it.first], m[c]); } else { char c = it.first - 32; b = b + (long long)min(m1[it.first], m[c]); m1[c] = m1[it.first] - (long long)min(m1[it.first], m[c]); m[c] = m[c] - (long long)min(m1[it.first], m[c]); } } } cout << a << " " << b; }
6
#include <bits/stdc++.h> using namespace std; const int maxN = 100 * 1000 + 5; int n, res; int x[maxN], y[maxN]; struct node { int x, y; node(int x = -1, int y = -1) : x(x), y(y) {} } a[maxN], b[maxN]; inline bool cmpX(const node &a1, const node &a2) { return (a1.x != a2.x) ? a1.x < a2.x : a1.y < a2.y; } inline bool cmpY(const node &a1, const node &a2) { return (a1.y != a2.y) ? a1.y < a2.y : a1.x < a2.x; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].x, &a[i].y); b[i] = a[i]; } sort(a + 1, a + n + 1, cmpY); sort(b + 1, b + n + 1, cmpX); int D = a[1].y - 1; int U = a[n].y + 1; int L = b[1].x - 1; int R = b[n].x + 1; res = 2 * (U - D + R - L); int d[5]; for (int i = 1; i <= 4; i++) d[i] = 1000 * 1000 * 1000; for (int i = 1; i <= n; i++) { int e1 = a[i].x + a[i].y - L - D; int e2 = a[i].x + U - L - a[i].y; int e3 = R + a[i].y - a[i].x - D; int e4 = R + U - a[i].x - a[i].y; d[1] = min(d[1], e1 - 1); d[2] = min(d[2], e2 - 1); d[3] = min(d[3], e3 - 1); d[4] = min(d[4], e4 - 1); } for (int i = 1; i <= 4; i++) res -= d[i]; cout << res << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, s, p, q; map<int, int> bm, sm; char a; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a >> p >> q; if (a == 'B') { bm[p] += q; } else { sm[p] += q; } } int c = 0; int z = sm.size(); int k = z - s; for (map<int, int>::reverse_iterator it = sm.rbegin(); it != sm.rend(); it++) { if (k <= 0) cout << "S " << it->first << ' ' << it->second << '\n'; k--; } c = 0; for (map<int, int>::reverse_iterator it = bm.rbegin(); it != bm.rend() && c < s; it++, c++) { cout << "B " << it->first << ' ' << it->second << '\n'; } }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; int pre = -1, u; cin >> n >> u; int a[n], k = 1, r = -1; a[0] = 1; pre = u; for (int i = 1; i < n; ++i) { int j; cin >> j; if (j == pre) a[i] = a[i - 1], r = i; else a[i] = 3 - a[i - 1], k = 2; pre = j; } if (u != pre && a[0] == a[n - 1]) { if (r > 0) { for (int i = r; i < n; ++i) a[i] = 3 - a[i]; } else a[n - 1] = 3, k = 3; } cout << k << endl; for (int i : a) cout << i << " "; cout << endl; } }
10
#include <bits/stdc++.h> using namespace std; long long int a[200001], b[200001]; long long int smallest_prime[2000001]; void seive() { long long int i, j, k, l, m, n, o, p; for (i = 1; i <= 2000000; i++) { smallest_prime[i] = 1; } smallest_prime[0] = smallest_prime[1] = 1; for (i = 2; i <= 2000000; i++) { if (smallest_prime[i] == 1) { for (j = 2 * i; j <= 2000000; j += i) { if (smallest_prime[j] == 1) smallest_prime[j] = i; } smallest_prime[i] = i; } } } vector<pair<long long int, long long int>> prime_divisors(long long int n) { long long int i, j, k, l, m, o, p, q, r, s, t; vector<pair<long long int, long long int>> vp; if (n <= 1) return vp; while (n != 1) { p = 0; k = smallest_prime[n]; while (n % k == 0) { n /= k; p++; } if (p > 0) vp.push_back(make_pair(k, p)); } if (n > 1) { vp.push_back({n, 1}); } return vp; } void solve() { long long int n, i, j, k, l, m, o, p, q, r, s, t; cin >> n; if (n == 1) { cout << 1 << " " << 0 << endl; return; } else if (smallest_prime[n] == n) { cout << n << " " << 0 << endl; return; } long long int ans = 0; long long int flag1 = 0; while (1) { vector<pair<long long int, long long int>> vp = prime_divisors(n); long long int flag = 0; for (i = 0; i < vp.size(); i++) { if (vp[i].second != 1) { flag = 1; } } if (flag == 0) break; for (i = 0; i < vp.size(); i++) { if ((vp[i].second) % 2 == 1 && flag) { n *= vp[i].first; flag1 = 1; } } n = sqrt(n); ans++; } if (flag1) ans++; cout << n << " " << ans << endl; } int main() { seive(); long long int t, n, m, k; t = 1; while (t--) { solve(); } return 0; }
7
#include <bits/stdc++.h> using namespace std; struct debugger { template <typename T> debugger& operator,(const T& v) { cerr << v << " "; return *this; } } dbg; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; string graph[2000]; int r, c, enx, eny, stx, sty; int level[2000][2000], vis[2000][2000]; void bfs() { memset(level, -1, sizeof(level)); memset(vis, 0, sizeof(vis)); queue<pair<int, int> > Q; Q.push(make_pair(enx, eny)); vis[enx][eny] = 1; level[enx][eny] = 0; while (!Q.empty()) { pair<int, int> u = Q.front(); Q.pop(); for (int i = 0; i < 4; i++) { int x = u.first + dx[i]; int y = u.second + dy[i]; if (x >= 0 && x < r && y >= 0 && y < c && !vis[x][y] && graph[x][y] != 'T') { vis[x][y] = 1; level[x][y] = level[u.first][u.second] + 1; Q.push(make_pair(x, y)); } } } } int main() { cin >> r >> c; for (int i = 0; i < r; i++) { cin >> graph[i]; for (int j = 0; j < c; j++) { if (graph[i][j] == 'S') { stx = i; sty = j; } if (graph[i][j] == 'E') { enx = i; eny = j; } } } bfs(); int ans = 0, p = level[stx][sty]; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if (isdigit(graph[i][j]) && level[i][j] != -1 && level[i][j] <= p) { ans += graph[i][j] - '0'; } } } printf("%d\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } 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 first = true; string res = "{"; for (const auto &x : v) { if (!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } template <class T> inline bool chmax(T &a, T b) { return a < b && (a = b, true); } template <class T> inline bool chmin(T &a, T b) { return a > b && (a = b, true); } using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f; constexpr ll MOD = 1e9 + 7; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; int n; vector<ll> a, b; ll test(ll given) { for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) { chmin(given, a[i]); given = b[i] - (a[i] - given); if (given < 0) return -1; } return given; } void solve() { cin >> n; a.resize(n); b.resize(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) { cin >> a[i]; } for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) { cin >> b[i]; } ll lo = -1, hi = a[0]; while (lo + 1 < hi) { ll mi = (lo + hi) / 2; if (test(mi) >= 0) { hi = mi; } else { lo = mi; } } 42; if (test(hi) >= hi) { puts("YES"); } else { puts("NO"); } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { solve(); } }
16
#include <bits/stdc++.h> using namespace std; map<int, int> mp2; map<int, int> mp1; map<int, int> mp; int n, B, C; int main() { cin >> n; int a[n], b[n - 1], c[n - 2]; for (int i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; } for (int i = 0; i < n - 1; i++) { cin >> b[i]; mp1[b[i]]++; } for (int i = 0; i < n - 2; i++) { cin >> c[i]; mp2[c[i]]++; } for (int i = 0; i < n; i++) { if (mp[a[i]] != mp1[a[i]]) { B = a[i]; } if (mp[a[i]] != mp2[a[i]] && mp2[a[i]] < mp1[a[i]]) { C = a[i]; } } cout << B << '\n' << C; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; cin >> s; int lo = 0, hi = 3 * n; auto works = [&](int t) { int togo = -1, seen = n + 1; for (int i = 0; i < (n); ++i) { if (s[i] == '*' && i > togo) seen = min(seen, i); if (s[i] == 'P') { int l = min(seen, i); seen = n + 1; if (i - l > t) return false; togo = max(i + t - 2 * (i - l), i + (t - (i - l)) / 2); } } return seen == n + 1; }; while (lo + 1 < hi) { int mi = (lo + hi) / 2; if (works(mi)) hi = mi; else lo = mi; } cout << hi << endl; }
10
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if(n%2==0) cout<<(n/2+1)*(n/2+1)<<endl; else cout<<2*(n/2+1)*(n/2+2)<<endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, p = 0; cin >> n; string s; cin >> s; bool ok = true; for (long long i = 0; i < s.length(); i++) { if (s[i] == '(') p++; else p--; if (p < -1) ok = false; } if (p != 0) ok = false; (ok) ? cout << "Yes" : cout << "No"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a, fl; fl = 0; for (int i = 0; i < n; i++) { cin >> a; if (a == 1) fl = 1; } if (fl == 1) cout << "-1" << endl; else cout << "1" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MX = 200020; const int N = 1000060; const int mod = (int)1e9 + 7; int bc[1111][1111]; int main() { cin.tie(0); cin.sync_with_stdio(0); int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); for (int i = n - 1; i >= 0; --i) { if (k % a[i] == 0) { cout << k / a[i] << endl; return 0; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int F[N]; string s, t; int n, m; vector<vector<int>> dp; int memo[26][N]; void init() { int i = 0, j = -1; F[0] = -1; while (i < m) { while (j >= 0 && t[i] != t[j]) j = F[j]; i++, j++; F[i] = j; } } int solve(int x, int y) { if (x == n) return 0; int &ret = dp[x][y + 1]; if (~ret) return ret; int add = 0; ret = 0; if (s[x] != '?') { y = memo[s[x] - 'a'][y]; y++; if (y == m) { add++; y = F[y]; } ret = solve(x + 1, y) + add; } else { for (int j = 0; j < 26; j++) { int to = memo[j][y]; ++to; if (to == m) { ret = max(ret, solve(x + 1, F[to]) + 1); } else { ret = max(ret, solve(x + 1, to)); } } } return ret; } int fill(int x, int y) { if (y < 0 || char('a' + x) == t[y]) return y; int &ret = memo[x][y]; if (ret != -2) return ret; ret = fill(x, F[y]); return ret; } int main() { cin >> s >> t; n = s.size(); m = t.size(); init(); for (int i = 0; i < 26; i++) for (int j = 0; j < m; j++) memo[i][j] = -2; for (int i = 0; i < 26; i++) for (int j = 0; j < m; j++) memo[i][j] = fill(i, j); dp = vector<vector<int>>(n + 2, vector<int>(m + 2, -1)); cout << solve(0, 0) << '\n'; return 0; }
15
#include <bits/stdc++.h> using namespace std; int tab[200007]; int main() { int q, i, j, n, k, ln; scanf("%d", &q); for (j = 0; j < q; j++) { ln = 0; scanf("%d %d", &n, &k); for (i = 0; i < n; i++) { scanf("%d", &tab[ln]); if (tab[ln] % 2 == 1) { tab[ln] = i + 1; ln++; } } if ((ln >= k) && ((ln - k) % 2 == 0)) { printf("YES\n"); for (i = (ln - k); i < ln - 1; i++) { printf("%d ", tab[i]); } printf("%d", n); } else { printf("NO"); } printf("\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MX = 2004; char s[MX][MX]; int d1[MX][MX], d2[MX][MX]; char ans[MX * 2]; int xi[] = {0, 1}; int yi[] = {1, 0}; struct node { int x, y; node() {} node(int x, int y) : x(x), y(y) {} }; void bfs(int n, int m) { queue<node> que; que.push(node(1, 1)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) d1[i][j] = INF, d2[i][j] = 0; d1[1][1] = 1, d2[1][1] = (s[1][1] == 'a'); for (int i = 1; i <= 2 * n - 1; i++) ans[i] = 'z'; ans[1] = (s[1][1] == 'a' || m > 0) ? 'a' : s[1][1]; while (!que.empty()) { node e = que.front(); que.pop(); int pos = e.x + e.y - 1; if (d1[e.x][e.y] - d2[e.x][e.y] > m && s[e.x][e.y] > ans[pos]) continue; for (int i = 0; i < 2; i++) { int xx = e.x + xi[i], yy = e.y + yi[i]; if (xx > n || yy > n) continue; int flag = (s[xx][yy] == 'a' && (d1[e.x][e.y] - d2[e.x][e.y]) <= m); if (d1[xx][yy] > d1[e.x][e.y] + 1) { d1[xx][yy] = d1[e.x][e.y] + 1; d2[xx][yy] = d2[e.x][e.y] + flag; que.push(node(xx, yy)); ans[pos + 1] = min(ans[pos + 1], (d1[xx][yy] - d2[xx][yy] <= m) ? 'a' : s[xx][yy]); } else if (d2[xx][yy] < d2[e.x][e.y] + flag) { d2[xx][yy] = d2[e.x][e.y] + flag; ans[pos + 1] = min(ans[pos + 1], (d1[xx][yy] - d2[xx][yy] <= m) ? 'a' : s[xx][yy]); } else { ans[pos + 1] = min(ans[pos + 1], s[xx][yy]); } } } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); bfs(n, m); printf("%s", ans + 1); return 0; }
11
#include <bits/stdc++.h> using namespace std; const int N = 10000; const long double pi = acos(-1.0); int n, f[200200], minf[200200], g[200200], o[200200]; int sum[200200]; pair<int, int> a[200200]; int read() { int x = 0, f = 1; char ch = getchar(); while ((ch < '0') || (ch > '9')) { if (ch == '-') f = -1; ch = getchar(); } while ((ch >= '0') && (ch <= '9')) { x = x * 10 + (ch - '0'); ch = getchar(); } return x * f; } int main() { n = read(); int maxin = 0, minout = 1000000007; for (register int i = 1; i <= n; i++) { a[i].first = read(); a[i].second = read(); maxin = max(maxin, a[i].second); minout = min(minout, a[i].first); } sort(a + 1, a + 1 + n); for (register int i = 1; i <= n; i++) o[i] = a[i].first; memset(f, 0x3f, sizeof(f)); memset(minf, 0x3f, sizeof(minf)); for (register int i = 1; i <= n; i++) { if (a[i].second < minout) { f[i] = a[i].second; g[i] = 1; } else { int pos = upper_bound(o + 1, o + 1 + n, a[i].second) - o - 1; f[i] = minf[pos] + a[i].second; g[i] = sum[pos]; } int tmp = f[i] - a[i].first; if (tmp == minf[i - 1]) sum[i] = (sum[i - 1] + g[i]) % 1000000007; else if ((tmp < minf[i - 1]) || (i == 1)) sum[i] = g[i]; else sum[i] = sum[i - 1]; minf[i] = min(minf[i - 1], tmp); } int m = 1000000007; for (register int i = 1; i <= n; i++) if (a[i].first > maxin) m = min(f[i], m); int ans = 0; for (register int i = 1; i <= n; i++) if ((a[i].first > maxin) && (f[i] == m)) ans = (ans + g[i]) % 1000000007; printf("%d", ans); return 0; }
15
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int MAXN = 300010; const int oo = 0x3f3f3f3f; int n, x, y; char a[MAXN]; int main() { n = read(); x = read(); y = read(); scanf("%s", a + 1); int flag = 0; long long sum1 = 0, sum2 = 0, sum3 = 0; for (int i = (1), i_end_ = (n); i <= i_end_; ++i) { if (a[i] == '0') flag = 1; else if (flag) { for (int j = (i), j_end_ = (n); j <= j_end_; ++j) if (a[j] == '0') { sum1++; flag = 0; i = j - 1; break; } } } flag = 0; for (int i = (n), i_end_ = (1); i >= i_end_; --i) { if (a[i] == '0') flag = 1; else if (flag) { for (int j = (i), j_end_ = (1); j >= j_end_; --j) if (a[j] == '0') { sum2++; flag = 0; i = j + 1; break; } } } flag = 0; for (int i = (1), i_end_ = (n); i <= i_end_; ++i) { if (a[i] == '0') flag = 1; else if (flag) sum3++, flag = 0; } if (flag) sum3++; cout << min({sum1 * x + y, sum2 * x + y, sum3 * y}); return 0; }
7
#include <bits/stdc++.h> using namespace std; long long poww(long long a, long long b, long long md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } const long long MAXN = 5000 + 10; const long long INF = 1e9; const long long MOD = 1e9 + 7; int vis[MAXN], n; pair<long long, long long> pos[MAXN]; int dist(int i, int j) { return abs(pos[i].first - pos[j].first) + abs(pos[i].second - pos[j].second); } bool dfs(int v, long long d) { for (int i = 1; i <= n; i++) { if (i == v || dist(i, v) <= d) continue; if (!vis[i]) { vis[i] = 3 - vis[v]; if (!dfs(i, d)) return false; } else if (vis[i] == vis[v]) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> pos[i].first >> pos[i].second; int L = 0, R = int(1e4 + 10); while (L < R) { int mid = (L + R) / 2; memset(vis, 0, sizeof vis); bool can = true; for (int i = 1; i <= n; i++) { if (!vis[i]) { vis[i] = 1; if (!dfs(i, mid)) { can = false; break; } } } if (can) R = mid; else L = mid + 1; } cout << L << '\n'; memset(vis, 0, sizeof vis); int C = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) { C++; vis[i] = 1; dfs(i, L); } } cout << poww(2, C, MOD) << '\n'; return 0; }
18
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int mxN = 200010; long long val; long long mxTree[mxN << 2], mnTree[mxN << 2], sumTree[mxN << 2]; bool lz[mxN << 2]; int N; void push(int n, int l, int r) { if (l == r || !lz[n]) return; int md = (l + r) >> 1; mxTree[n << 1] = mxTree[n << 1 | 1] = mnTree[n << 1] = mnTree[n << 1 | 1] = mxTree[n]; sumTree[n << 1] = mxTree[n << 1] * (md - l + 1ll); sumTree[n << 1 | 1] = mxTree[n << 1 | 1] * (r - md); lz[n] = false; lz[n << 1] = lz[n << 1 | 1] = true; } void upd(int a, int b, int n = 1, int l = 0, int r = N) { push(n, l, r); int md = (l + r) >> 1; if (mnTree[n] >= val) return; if (l == a && r == b) { if (l == r) { mxTree[n] = mnTree[n] = sumTree[n] = val; } else if (val > mxTree[n]) { mxTree[n] = mnTree[n] = val; sumTree[n] = val * (r - l + 1ll); lz[n] = true; } else { upd(a, min(b, md), n << 1, l, md); upd(max(a, md + 1), b, n << 1 | 1, md + 1, r); mxTree[n] = max(mxTree[n << 1], mxTree[n << 1 | 1]); mnTree[n] = min(mnTree[n << 1], mnTree[n << 1 | 1]); sumTree[n] = sumTree[n << 1] + sumTree[n << 1 | 1]; } return; } if (a <= md) upd(a, min(b, md), n << 1, l, md); if (b > md) upd(max(a, md + 1), b, n << 1 | 1, md + 1, r); mxTree[n] = max(mxTree[n << 1], mxTree[n << 1 | 1]); mnTree[n] = min(mnTree[n << 1], mnTree[n << 1 | 1]); sumTree[n] = sumTree[n << 1] + sumTree[n << 1 | 1]; } long long getSum(int a, int b, int n = 1, int l = 0, int r = N) { push(n, l, r); if (l == a && r == b) return sumTree[n]; int md = (l + r) >> 1; long long res = 0; if (a <= md) res += getSum(a, min(b, md), n << 1, l, md); if (b > md) res += getSum(max(a, md + 1), b, n << 1 | 1, md + 1, r); return res; } int lastOcc[mxN]; int fsOcc[mxN]; vector<long long> dvs[mxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); srand(time(NULL)); cin >> N; if (N == 1) { cout << "0\n"; return 0; } vector<long long> a(N); memset(fsOcc, -1, sizeof fsOcc); for (int i = 0; i < N; i++) { cin >> a[i]; for (long long j = 1; j * j <= a[i]; j++) { if (a[i] % j) continue; dvs[i].push_back(j); long long other = a[i] / j; lastOcc[j] = lastOcc[other] = i; if (fsOcc[j] == -1) fsOcc[j] = i; if (fsOcc[other] == -1) fsOcc[other] = i; if (j != other) dvs[i].push_back(other); } } for (int i = N - 1; i >= 1; i--) { long long mx = 0; for (auto d : dvs[i]) { if (lastOcc[d] > i) mx = max(mx, d); } push(1, 0, N); if (mx > mxTree[1]) { val = mx; upd(1, i); } } long long res = getSum(1, N - 1); for (int i = 0; i + 1 < N; i++) { long long mx = 0; for (auto d : dvs[i]) { if (fsOcc[d] < i) mx = max(mx, d); if (lastOcc[d] > i + 1 && fsOcc[d] == i) { val = d; upd(i + 2, lastOcc[d]); } } val = mx; upd(i + 2, N); res += getSum(i + 2, N); } cout << res << '\n'; return 0; }
20
#include <bits/stdc++.h> using namespace std; long long bgm(long long a, long long b, long long m) { a %= m; long long rem = 1; while (b) { if (b & 1) rem = (rem * a) % m; a = (a * a) % m; b >>= 1; } return rem; } long long in() { long long a; if (scanf("%lld", &a) == EOF) return EOF; return a; } long long dl() { double a; if (scanf("%lf", &a) == EOF) return EOF; return a; } const int MAX = 300005; char str[MAX]; int n, m; int cnt() { int ans = 0; for (int i = 0; i < n; i++) { int cnt = 0; while (i < n && str[i] == '.') cnt++, i++; if (cnt) ans += cnt - 1; } return ans; } int main() { n = in(), m = in(); scanf(" %s", str); int now = cnt(); while (m--) { int a = in() - 1; char c; scanf(" %c", &c); int ok = 0; if (a) ok = str[a - 1] == '.'; if (a < n - 1) ok += str[a + 1] == '.'; if (str[a] == c || (c != '.' && str[a] != '.')) ok = 0; if (c == '.' && str[a] != '.') now += ok; else if (c != '.' && str[a] == '.') now -= ok; str[a] = c; printf("%lld", (long long)(now)), puts(""); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int main() { int n; cin >> n; string w; cin >> w; int numChars[26]; memset(numChars, 0, 26 * sizeof(int)); int mx = 0; for (int j = 0; j < n; j++) { for (int i = j; i < n; i++) { numChars[w[i] - 'a']++; mx = max(mx, numChars[w[i] - 'a']); if (mx <= (i + 1 - j) / 2) { cout << "YES" << endl; cout << w.substr(j, i - j + 1); return 0; } } memset(numChars, 0, 26 * sizeof(int)); mx = 0; } cout << "NO"; return 0; }
2
#include <bits/stdc++.h> using namespace std; long n, m; char s[600][600]; long f[600][30][30] = {0}, nho1[600][30][30] = {0}, nho2[600][30][30] = {0}; int main() { cin >> n >> m; scanf("\n"); for (int i = 1; i <= n; i = i + 1) { scanf("%s\n", &s[i]); } for (int k = 1; k <= n; k = k + 1) { for (int i = 1; i <= 26; i = i + 1) for (int j = 1; j <= 26; j = j + 1) { if (i == j) f[k][i][j] = 2000000000; else { long dem = 0, min = 2000000000; for (int temp = 0; temp < m; temp = temp + 1) { if (temp % 2 == 0 && (i + 96) != s[k][temp]) dem = dem + 1; if (temp % 2 == 1 && (j + 96) != s[k][temp]) dem = dem + 1; } if (k == 1) f[k][i][j] = dem; else { for (int i1 = 1; i1 <= 26; i1 = i1 + 1) for (int j1 = 1; j1 <= 26; j1 = j1 + 1) { if (f[k - 1][i1][j1] < min && i1 != i && j1 != j && i1 != j1) { min = f[k - 1][i1][j1]; nho1[k][i][j] = i1; nho2[k][i][j] = j1; } } f[k][i][j] = min + dem; } } } } long min = 2000000000, nho3, nho4; for (int i = 1; i <= 26; i = i + 1) for (int j = 1; j <= 26; j = j + 1) { if (f[n][i][j] < min) { min = f[n][i][j]; nho3 = i; nho4 = j; } } cout << min << endl; for (int k = n; k >= 1; k = k - 1) { for (int temp = 0; temp < m; temp = temp + 1) { if (temp % 2 == 0) s[k][temp] = nho3 + 96; if (temp % 2 == 1) s[k][temp] = nho4 + 96; } long res1 = nho1[k][nho3][nho4], res2 = nho2[k][nho3][nho4]; nho3 = res1; nho4 = res2; } for (int i = 1; i <= n; i = i + 1) { printf("%s\n", s[i]); } return 0; }
12
#include <bits/stdc++.h> using namespace std; int n, m, ans = 1e9; int a[1001][1001]; int t[1001]; int di[4] = {-1, 0, 1, 0}; int dj[4] = {0, -1, 0, 1}; int d[4][1001][1001]; char c; deque<pair<int, int>> q; bool valid(int i, int j, int s) { return i >= 1 && i <= n && j >= 1 && j <= m && d[s][i][j] == -1 && a[i][j] != -1; } int main() { t['#'] = -1; t['.'] = 0; t['1'] = 1; t['2'] = 2; t['3'] = 3; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf(" %c", &c); a[i][j] = t[c]; } } memset(d, -1, sizeof d); for (int s = 1; s <= 3; s++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == s) { d[s][i][j] = 0; q.push_back({i, j}); } } } while (!q.empty()) { int i = q.front().first; int j = q.front().second; q.pop_front(); for (int k = 0; k < 4; k++) { if (valid(i + di[k], j + dj[k], s)) { if (a[i + di[k]][j + dj[k]] == 0) { d[s][i + di[k]][j + dj[k]] = d[s][i][j] + 1; q.push_back({i + di[k], j + dj[k]}); } else { d[s][i + di[k]][j + dj[k]] = d[s][i][j]; q.push_front({i + di[k], j + dj[k]}); } } } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (d[1][i][j] != -1 && d[2][i][j] != -1 && d[3][i][j] != -1) { ans = min(ans, d[1][i][j] + d[2][i][j] + d[3][i][j] - 2 * (a[i][j] == 0)); } } } if (ans == 1e9) { printf("-1"); } else { printf("%d", ans); } return 0; }
14
#include <bits/stdc++.h> using namespace std; inline int inp() { return 0; } inline int inp(bool& A) { if (cin >> A) return 1; return -1; } inline int inp(char& A) { return scanf(" %c", &A); } inline int inp(int& A) { return scanf("%d", &A); } inline int inp(float& A) { if (cin >> A) return 1; return -1; } inline int inp(long long& A) { return scanf("%I64d", &A); } inline int inp(double& A) { return scanf("%lf", &A); } inline int inp(char* A) { return scanf("%s", A); } inline int inp(string& A) { if (cin >> A) return 1; return -1; } template <class Front, class... Queue> inline int inp(Front& A, Queue&... B) { return inp(A) + inp(B...); } inline int oup() { return 0; } inline int oup(bool A) { if (cout << A) return 1; return -1; } inline int oup(char A) { return printf("%c", A); } inline int oup(int A) { return printf("%d", A); } inline int oup(float A) { if (cout << A) return 1; return -1; } inline int oup(long long A) { return printf("%I64d", A); } inline int oup(double A) { return printf("%lf", A); } inline int oup(char* A) { return printf(A); } inline int oup(const char* A) { return printf("%s", A); } inline int oup(string& A) { if (cout << A) return 1; return -1; } template <class Front, class... Queue> inline int oup(Front A, Queue... B) { return oup(A) + oup(B...); } template <class T> inline void remax(T& A, T B) { if (A < B) A = B; } template <class T> inline void remin(T& A, T B) { if (A > B) A = B; } string ToString(long long num) { string ret; do { ret += ((num % 10) + '0'); num /= 10; } while (num); reverse(ret.begin(), ret.end()); return ret; } long long ToNumber(string s) { long long r = 0, p = 1; for (int i = s.size() - 1; i >= 0; --i) r += (s[i] - '0') * p, p *= 10; return r; } long long Gcd(long long a, long long b) { while (a %= b ^= a ^= b ^= a) ; return b; } long long Power(long long base, long long power) { long long ret = 1; while (power) { if (power & 1) ret *= base; power >>= 1; base *= base; } return ret; } long long PowerMod(long long base, long long power, long long mod) { if (!power) return 1; if (power & 1) return (base * PowerMod(base, power - 1, mod)) % mod; return PowerMod((base * base) % mod, power >> 1, mod); } int Log(long long num, long long base) { int ret = 0; while (num) { ++ret; num /= base; } return ret; } int Count(long long mask) { int ret = 0; while (mask) { if (mask & 1) ++ret; mask >>= 1; } return ret; } vector<pair<int, int> > adj[100000]; vector<int> ans; int finished[100000]; bool vis[500000]; void dfs(int node = 0) { int sz = (int)adj[node].size(); while (finished[node] < sz) { int tmp = finished[node]; ++finished[node]; if (!vis[adj[node][tmp].second]) { vis[adj[node][tmp].second] = true; dfs(adj[node][tmp].first); } } ans.emplace_back(node + 1); } inline void run() { in:; int n, m; if (inp(n, m) != 2) return; vector<int> odds; int k = m, sz; for (int i = 0; i < m; ++i) { int a, b; inp(a, b); --a, --b; adj[a].emplace_back(make_pair(b, i)); adj[b].emplace_back(make_pair(a, i)); } for (int i = 0; i < n; ++i) { sz = (int)adj[i].size(); if (sz & 1) odds.emplace_back(i); } sz = (int)odds.size(); for (int i = 0; i < sz; i += 2) { adj[odds[i]].emplace_back(make_pair(odds[i + 1], k)); adj[odds[i + 1]].emplace_back(make_pair(odds[i], k)); ++k; } if (k & 1) { adj[0].emplace_back(make_pair(0, k)); adj[0].emplace_back(make_pair(0, k)); ++k; } dfs(); sz = (int)ans.size(); oup(k, '\n'); for (int i = 0; i + 1 < sz; ++i) if (i & 1) oup(ans[i + 1], ' ', ans[i], '\n'); else oup(ans[i], ' ', ans[i + 1], '\n'); for (int i = 0; i < n; ++i) adj[i].clear(); ans.clear(); memset(finished, 0, sizeof finished); memset(vis, false, sizeof vis); goto in; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); run(); return 0; }
18
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 99; int n, m, q, x, y, ans = 1, Fen[2][N]; void update(int x, int val, int s) { if (s) x = n + 1 - x, val = m + 1 - val; x++; while (x <= N) { Fen[s][x] = min(Fen[s][x], val); ; x += (x & -x); } } int Get(int x, int s) { if (s) x = n + 1 - x; x++; int res = N; while (x > 0) { res = min(res, Fen[s][x]); ; x -= (x & -x); } if (s) return m + 1 - res; return res; } void do_it(int x, int y) { int s = (x - 1) % 2; x = (x + 1) / 2, y = (y + 1) / 2; update(x, y, s); if (Get(x, 0) - 1 < Get(x, 1)) ans = 0; } int main() { for (int i = 0; i < N; i++) Fen[0][i] = Fen[1][i] = N; cin >> n >> m >> q; for (int io = 0; io < q; io++) { scanf("%d%d", &x, &y); ; do_it(x, y); cout << (ans ? "YES" : "NO") << endl; } }
19
#include <bits/stdc++.h> int read() { register int x = 0; register char ch = getchar(), f = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = !f; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0'); return f ? x : -x; } const int d[8][2] = {{-1, -2}, {-1, 2}, {-2, -1}, {-2, 1}, {1, 2}, {1, -2}, {2, -1}, {2, 1}}; struct node { int x, y; void get() { x = read(), y = read(); } } w, b; int n, m, dis[2][1005][1005], h, t; node pre[2][1005][1005]; node Q[1005 * 1005]; void bfs(int k, int x, int y) { memset(dis[k], 0x3f, sizeof dis[k]); h = 0, t = 1, Q[t] = (node){x, y}, dis[k][x][y] = 0; while (h < t) { node u = Q[++h]; for (register int i = 0; i < 8; ++i) { node v = u; v.x += d[i][0], v.y += d[i][1]; if (v.x >= 1 && v.x <= n && v.y >= 1 && v.y <= m && dis[k][v.x][v.y] == 0x3f3f3f3f) dis[k][v.x][v.y] = dis[k][u.x][u.y] + 1, pre[k][v.x][v.y] = u, Q[++t] = v; } } } node A, B; void check() { for (register int i = 0; i < 8; ++i) if (A.x + d[i][0] == B.x && A.y + d[i][1] == B.y) printf("%d %d\n", B.x, B.y), fflush(stdout), exit(0); } void move() { printf("%d %d\n", A.x, A.y), fflush(stdout), B.get(), check(); } void solve1(int k) { if (!k) printf("WHITE\n"), fflush(stdout), A = w, B = b, check(); else printf("BLACK\n"), fflush(stdout), A = b, B.get(), check(); if (!dis[k][A.x][A.y]) exit(0); while (1) { A = pre[k][A.x][A.y]; if (!dis[k][A.x][A.y]) printf("%d %d\n", A.x, A.y), fflush(stdout), exit(0); move(); } } void solve2(int k) { if (!k) printf("WHITE\n"), fflush(stdout), A = w, B = b, check(); else printf("BLACK\n"), fflush(stdout), A = b, B.get(), check(); while (1) { if (dis[k ^ 1][A.x][A.y] == 0) break; A = pre[k ^ 1][A.x][A.y], move(); } if (!k) --A.x, ++A.y, ++A.y, move(), --A.x, --A.x, --A.y, move(), ++A.x, ++A.x, --A.y; else ++A.x, ++A.y, ++A.y, move(), ++A.x, ++A.x, --A.y, move(), --A.x, --A.x, --A.y; printf("%d %d\n", A.x, A.y), fflush(stdout), exit(0); } int main() { n = read(), m = read(), w.get(), b.get(); bfs(0, n >> 1, m >> 1), bfs(1, (n >> 1) + 1, m >> 1); int colw = (w.x & 1) == (w.y & 1), colb = (b.x & 1) == (b.y & 1); if (colw != colb) { if (dis[0][w.x][w.y] <= dis[1][b.x][b.y]) solve1(0); else if (dis[1][w.x][w.y] >= dis[1][b.x][b.y] + 2) solve1(1); else solve2(0); } else { if (dis[1][b.x][b.y] <= dis[0][w.x][w.y] - 1) solve1(1); else if (dis[0][b.x][b.y] >= dis[0][w.x][w.y] + 1) solve1(0); else solve2(1); } }
21
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int p, n, t, i, a[101], maxd = 1; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); cin >> t; for (i = 0; i < n; i++) { if (a[i] + t >= a[n - 1]) { if (n - i > maxd) maxd = n - i; break; } else { p = upper_bound(a, a + n, a[i] + t) - a; if (p - i > maxd) maxd = p - i; } } cout << maxd; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int TC; cin >> TC; while (TC--) { int n, m, k; cin >> n >> m >> k; int l = n * m / 2 - k; if (!((k % 2 == n % 2 && n % 2 == 0 && (k + n - 1) / n * 2 < m) || (l % 2 == m % 2 && m % 2 == 0 && (l + m - 1) / m * 2 < n))) { cout << "NO\n"; continue; } cout << "YES\n"; vector<vector<char>> answer(n, vector<char>(m, '\0')); auto get_next_c = [&answer, n, m](int y, int x) -> char { char neighbors[4]{'\0', '\0', '\0', '\0'}; if (y != 0) neighbors[0] = answer[y - 1][x]; if (x != 0) neighbors[1] = answer[y][x - 1]; if (x != 0 && y + 1 != n) neighbors[2] = answer[y + 1][x - 1]; if (y != 0 && x + 1 != m) neighbors[3] = answer[y - 1][x + 1]; sort(neighbors, neighbors + 4); char next_c = 'a'; for (int i = 0; i < 4; ++i) if (neighbors[i] == next_c) ++next_c; return next_c; }; int y, x; if (k % 2 == n % 2 && n % 2 == 0 && (k + n - 1) / n * 2 < m) { for (int i = 0; i < k; ++i) { x = i / n * 2; y = i % n; char next_c = get_next_c(y, x); answer[y][x] = next_c; answer[y][x + 1] = next_c; } x = (k + 1) / n * 2; for (; x < answer[0].size(); ++x) { for (y = 0; y < answer.size(); y += 2) { if (answer[y][x]) continue; char next_c = get_next_c(y, x); answer[y][x] = next_c; answer[y + 1][x] = next_c; } } } else { for (int i = 0; i < l; ++i) { x = i % m; y = i / m * 2; char next_c = get_next_c(y, x); answer[y][x] = next_c; answer[y + 1][x] = next_c; } y = (l + 1) / m * 2; for (; y < answer.size(); ++y) { for (x = 0; x < answer[0].size(); x += 2) { if (answer[y][x]) continue; char next_c = get_next_c(y, x); answer[y][x] = next_c; answer[y][x + 1] = next_c; } } } for (int y = 0; y < answer.size(); ++y) { for (int x = 0; x < answer[y].size(); ++x) { if (answer[y][x]) cout << answer[y][x]; else cout << ' '; } cout << '\n'; } } }
13
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); int tot = 0; for (int i = 0; i < n; i++) { if (s[i] != 'Q') continue; for (int j = i + 1; j < n; j++) { if (s[j] != 'A') continue; for (int k = j + 1; k < n; k++) { if (s[k] != 'Q') continue; ++tot; } } } cout << tot << endl; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int q; cin >> q; string meh = "RGB"; while (q--) { int n, k; string cad; cin >> n >> k >> cad; int ans = n; for (int ini = 0; ini + k <= n; ++ini) { for (int off = 0; off < 3; ++off) { int aux = 0; for (int it = 0; it < k; ++it) aux += (cad[ini + it] != meh[(off + it) % 3]); ans = min(ans, aux); } } cout << ans << '\n'; } return 0; }
7
#include <bits/stdc++.h> using namespace std; template <class T> inline T modinv(T a, T n) { T i = n, v = 0, d = 1; while (a > 0) { T t = i / a, x = a; a = i % x; i = x; x = d; d = v - t * x; v = x; } return (v + n) % n; } long long modpow(long long n, long long k, long long mod) { long long ans = 1; while (k > 0) { if (k & 1) ans = (ans * n) % mod; k >>= 1; n = (n * n) % mod; } return ans % mod; } template <class T> string str(T Number) { string Result; ostringstream convert; convert << Number; Result = convert.str(); return Result; } int StringToNumber(const string &Text) { istringstream ss(Text); int result; return ss >> result ? result : 0; } template <class T> inline vector<pair<T, int> > FACTORISE(T n) { vector<pair<T, int> > R; for (T i = 2; n > 1;) { if (n % i == 0) { int C = 0; for (; n % i == 0; C++, n /= i) ; R.push_back(make_pair(i, C)); } i++; if (i > n / i) i = n; } if (n > 1) R.push_back(make_pair(n, 1)); return R; } template <class T> inline T TOTIENT(T n) { vector<pair<T, int> > R = FACTORISE(n); T r = n; for (int i = 0; i < R.size(); i++) r = r / R[i].first * (R[i].first - 1); return r; } template <class T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } double rnd(float d) { return floor(d + 0.49); } int main(int argc, char const *argv[]) { int n, v; cin >> n >> v; std::vector<int> ans; for (int i = 0; i < n; ++i) { int x; cin >> x; int f = 0; for (int j = 0; j < x; ++j) { int c; cin >> c; if (v > c) { f = 1; } } if (f) ans.push_back(i); } cout << ((int)(ans).size()) << "\n"; for (int i = 0; i < ((int)(ans).size()); ++i) { cout << ans[i] + 1 << " "; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int r[1000010]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { char s; long long a; cin >> s >> a; int sum = 0, t = 1; while (a > 0) { sum = sum + (a % 2) * t; a /= 10; t *= 2; } if (s == '+') r[sum]++; if (s == '-') r[sum]--; if (s == '?') cout << r[sum] << endl; } }
6
#include <bits/stdc++.h> const int N = 1010; const int M = 1010; double lf[N * M]; void init() { lf[0] = lf[1] = 0; for (int i = 2; i < N * M; i++) lf[i] = lf[i - 1] + log(i); } int n, m; double lC(int n, int m) { return lf[n] - lf[m] - lf[n - m]; } int main() { init(); while (~scanf("%d %d", &n, &m)) { double ans = 0; for (int i = 0; i <= m && i <= n; i++) { double e = lC(m, i) + lC((n - 1) * m, n - i) - lC(n * m, n); ans += (double)i * i / (double)n * exp(e); } printf("%.12f\n", ans); } }
13
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9 + 1; struct node { int mx1, mx2, cntmx1; long long sum; node() { mx1 = inf, mx2 = -1, cntmx1 = 1, sum = inf; } friend node combine(const node &l, const node &r) { node res; res.mx1 = max(l.mx1, r.mx1); if (l.mx1 > r.mx1) res.mx2 = max(l.mx2, r.mx1); else if (l.mx1 < r.mx1) res.mx2 = max(l.mx1, r.mx2); else res.mx2 = max(l.mx2, r.mx2); res.cntmx1 = (l.mx1 == res.mx1 ? l.cntmx1 : 0); res.cntmx1 += (r.mx1 == res.mx1 ? r.cntmx1 : 0); assert(res.cntmx1 > 0); res.sum = l.sum + r.sum; return res; } }; struct segtree { vector<node> val; int tsz; void upd(int v) { val[v] = combine(val[2 * v], val[2 * v + 1]); } vector<int> br_arr; segtree(int n) { tsz = 1; while (tsz < n) tsz *= 2; val.assign(2 * tsz, node()); for (int i = ((int)(tsz)-1); i >= 0; --i) if (i) upd(i); br_arr.assign(n, inf); } void push(int v) { for (int son = 2 * v; son <= 2 * v + 1; son++) if (val[v].mx1 < val[son].mx1) { val[son].sum -= (long long)(val[son].mx1 - val[v].mx1) * val[son].cntmx1; val[son].mx1 = val[v].mx1; } } void update(int v, int l, int r, int L, int R, int what) { if (l >= R || r <= L) return; if (val[v].mx1 <= what) return; if (l <= L && R <= r && val[v].mx2 < what) { assert(what < val[v].mx1); val[v].sum -= (long long)(val[v].mx1 - what) * val[v].cntmx1; val[v].mx1 = what; return; } push(v); int M = (L + R) / 2; update(2 * v, l, r, L, M, what); update(2 * v + 1, l, r, M, R, what); upd(v); } long long gosum(int v, int l, int r, int L, int R) { if (l >= R || r <= L) return 0; if (l <= L && R <= r) return val[v].sum; push(v); int M = (L + R) / 2; return gosum(2 * v, l, r, L, M) + gosum(2 * v + 1, l, r, M, R); } long long brut_sum(int l, int r) { long long ans = 0; for (int i = l; i < r; i++) ans += br_arr[i]; return ans; } void brut_remin(int l, int r, int x) { for (int i = l; i < r; i++) br_arr[i] = min(br_arr[i], x); } long long getsum(int l, int r) { long long ans = gosum(1, l, r, 0, tsz); return ans; } void remin_seg(int l, int r, int what) { update(1, l, r, 0, tsz, what); } }; void solve(int q) { const int n = (int)1e5; vector<segtree> data(4, segtree(n)); vector<set<int>> notalive(2); for (int i = 0; i < (int)(2); ++i) for (int j = 0; j < (int)(n); ++j) notalive[i].insert(j); auto transfer = [&](int side, int l, int r, int what) { while (true) { auto it = notalive[side].lower_bound(l); if (it == notalive[side].end() || *it >= r) break; const int cur = *it; if (!notalive[side ^ 1].count(cur)) { data[2].remin_seg(cur, cur + 1, 0); data[3].remin_seg(cur, cur + 1, 0); long long curans = 0; for (int i = 0; i < (int)(4); ++i) { long long addcur = data[i].getsum(cur, cur + 1); curans += (i < 2 ? addcur : -addcur); } } notalive[side].erase(it); } data[side + 2].remin_seg(l, r, what); }; for (int it = 0; it < (int)(q); ++it) { int t, l, r; cin >> t >> l >> r; if (t == 1) { int y; cin >> y; int side = (y < 0 ? 1 : 0); int ry = abs(y); data[side].remin_seg(l, r, ry); transfer(side, l, r, ry); } else { assert(t == 2); long long ans = 0; for (int i = 0; i < (int)(4); ++i) { long long cur = data[i].getsum(l, r); ans += (i < 2 ? cur : -cur); } cout << ans << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; while (cin >> n) solve(n); }
23
#include <bits/stdc++.h> using namespace std; signed main() { long long t; cin >> t; while (t--) { long long n; string str; cin >> n >> str; vector<long long> a(n), pref(n + 1, 0); map<long long, long long> m; for (long long i = 0; i < n; i++) { a[i] = str[i] - '0'; pref[i + 1] = pref[i] + a[i]; m[pref[i + 1] - (i + 1)]++; } long long ans = 0; for (long long i = 0; i < n; i++) { long long curr = pref[i + 1] - (i + 1); if (curr == 0) { ans += m[curr]; } else { if (m[curr] != 1) ans += m[curr] - 1; } if (m[curr] == 1) { m.erase(curr); } else m[curr]--; } cout << ans << "\n"; } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); ios::sync_with_stdio(false); int T; cin >> T; while (T--) { bool flag = false; int n; vector<int> v; cin >> n; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = 0; i < n - 1; i++) { if (abs(v[i + 1] - v[i]) > 1) { cout << "YES\n"; cout << i + 1 << " " << i + 2 << "\n"; flag = true; break; } } if (!flag) cout << "NO\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T> inline T __cin() { T ret; cin >> ret; return ret; } int main() { ios_base::sync_with_stdio(0); int n; string inp; cin >> n >> inp; string a = inp.substr(0, n); string b = inp.substr(n, n); sort(a.begin(), a.end()); sort(b.begin(), b.end()); int aa = 1, bb = 1; for (__typeof(((int)a.size())) i = 0; i < (((int)a.size())); i++) if (a[i] > b[i]) aa = 0; else if (a[i] < b[i]) bb = 0; else if (a[i] == b[i]) aa = bb = 0; if (aa || bb) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int q; cin >> q; long long int n, m, k, z, z1; for (int i = 0; i < q; i++) { cin >> n >> m >> k; if ((n + m) % 2 == 1) { k--; m--; z1 = max(n, m); if (z1 > k) { z1 = -1; } else { z1 += ((k - z1) / 2) * 2; if ((k - z1) % 2 == 1) { z1--; } } m++; n--; z = max(n, m); if (z > k) { z = -1; } else { z += ((k - z) / 2) * 2; if ((k - z) % 2 == 1) { z--; } } cout << max(z, z1) << endl; } else { z = max(n, m); if (z > k) { cout << -1 << endl; } else { z += ((k - z) / 2) * 2; if ((k - z) % 2 == 1) { z--; } cout << z << endl; } } } }
8
#include <bits/stdc++.h> using namespace std; string a[110]; int dp[2][110], c[2][110]; int main() { int N, M, ans = 0; cin >> N >> M; for (int i = N; i > 0; i--) { cin >> a[i]; } for (int f = 1; f <= N; f++) { for (int i = 0; i < M + 1 && a[f][i] == '0'; i++) c[0][f]++; for (int i = M + 1; i > 0 && a[f][i] == '0'; i--) c[1][f]++; } dp[0][1] = 2 * (M + 1 - c[1][1]); dp[1][1] = M + 1; ans = (M + 1 - c[1][1]); for (int f = 2; f <= N; f++) { dp[0][f] = min(1 + (M + 1 - c[1][f]) * 2 + dp[0][f - 1], 1 + M + dp[1][f - 1] + 1); dp[1][f] = min(1 + (M + 1 - c[0][f]) * 2 + dp[1][f - 1], 1 + M + dp[0][f - 1] + 1); if (c[0][f] != M + 1 && c[1][f] != M + 1 && f != 1) { ans = min(dp[0][f - 1] + 1 + (M + 1 - c[1][f]), dp[1][f - 1] + 1 + (M + 1 - c[0][f])); } } cout << ans << endl; return 0; }
8
#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--) { string p, h; cin >> p >> h; if (h.size() < p.size()) { cout << "NO\n"; continue; } vector<int> cnt_p(26, 0); for (int i = 0; i < p.size(); i++) { cnt_p[p[i] - 'a']++; } vector<int> cnt(26, 0); for (int i = 0; i < p.size() - 1; i++) { cnt[h[i] - 'a']++; } bool accept = false; for (int i = p.size() - 1; i < h.size(); i++) { cnt[h[i] - 'a']++; if (i >= p.size()) { cnt[h[i - p.size()] - 'a']--; } bool ok = true; for (int j = 0; j < 26; j++) { if (cnt[j] != cnt_p[j]) ok = false; } if (ok) { accept = true; break; } else { } } if (accept) { cout << "YES\n"; } else { cout << "NO\n"; } } }
2
#include <bits/stdc++.h> using namespace std; int isValid(int i, int j, int n) { if (i < 0 || j < 0 || i >= n || j >= n) { return false; } return true; } int main() { int n; cin >> n; if (n <= 2) { cout << 0 << endl; } else { char arr[1000][1000]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; } } int res = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int cnt = 0; if (arr[i][j] == 'X') { if (isValid(i - 1, j - 1, n)) { if (arr[i - 1][j - 1] == 'X') { cnt++; } } if (isValid(i - 1, j + 1, n)) { if (arr[i - 1][j + 1] == 'X') { cnt++; } } if (isValid(i + 1, j + 1, n)) { if (arr[i + 1][j + 1] == 'X') { cnt++; } } if (isValid(i + 1, j - 1, n)) { if (arr[i + 1][j - 1] == 'X') { cnt++; } } } if (cnt == 4) { res++; } } } cout << res << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int Maxn = 400 + 7; int t, h, r, n, m; long long dis[Maxn][Maxn], dp[Maxn][Maxn]; pair<int, int> ta[Maxn], he[Maxn]; bool mark[Maxn][Maxn], dor, ivan, vis[Maxn][Maxn]; queue<pair<int, int> > que; long long dfs(int v1, int v2) { vis[v1][v2] = true; mark[v1][v2] = true; long long maxx = 1; for (int i = 1; i <= min(n, v1); i++) { int u1 = v1 - i + he[i].first, u2 = v2 + he[i].second; if ((u1 + u2) <= r) { if (!mark[u1][u2]) dfs(u1, u2); if (vis[u1][u2]) dor = true; maxx = max(maxx, dp[u1][u2] + 1); } } for (int i = 1; i <= min(m, v2); i++) { int u1 = v1 + ta[i].first, u2 = v2 - i + ta[i].second; if ((u1 + u2) <= r) { if (!mark[u1][u2]) dfs(u1, u2); if (vis[u1][u2]) dor = true; maxx = max(maxx, dp[u1][u2] + 1); } } dp[v1][v2] = maxx; vis[v1][v2] = false; return maxx; } void bfs() { dis[h][t] = 0; que.push(make_pair(h, t)); while (!que.empty()) { int v1 = que.front().first, v2 = que.front().second; que.pop(); if (v1 == 0 && v2 == 0) { cout << "Ivan" << endl << dis[v1][v2]; ivan = true; } for (int i = 1; i <= min(n, v1); i++) { int u1 = v1 - i + he[i].first, u2 = v2 + he[i].second; if (dis[u1][u2] == -1 && u1 + u2 <= r) { dis[u1][u2] = dis[v1][v2] + 1; que.push(make_pair(u1, u2)); } } for (int i = 1; i <= min(m, v2); i++) { int u1 = v1 + ta[i].first, u2 = v2 - i + ta[i].second; if (dis[u1][u2] == -1 && u1 + u2 <= r) { dis[u1][u2] = dis[v1][v2] + 1; que.push(make_pair(u1, u2)); } } } } int main() { cin >> h >> t >> r; cin >> n; for (int i = 1, a, b; i <= n; i++) { cin >> a >> b; he[i] = make_pair(a, b); } cin >> m; for (int i = 1, a, b; i <= m; i++) { cin >> a >> b; ta[i] = make_pair(a, b); } for (int i = 0; i <= r; i++) { for (int j = 0; j <= r; j++) { dis[i][j] = -1; } } bfs(); if (ivan) return 0; int ans = dfs(h, t); if (dor) { cout << "Draw"; return 0; } cout << "Zmey" << endl << ans; }
13
#include <bits/stdc++.h> using namespace std; char a[100005]; char b[100005]; long long c[100005]; long long d[100005]; long long ans1[100005]; long long ans2[100005]; long long now = 0; void F2(long long n); void F1(long long n) { if (a[n] == '0' || a[n + 1] == '0') F2(n + 1); if (now >= 100000) return; a[n]--; a[n + 1]--; printf("%lld %d\n", n, -1); now++; if (now >= 100000) return; } void F2(long long n) { if (a[n] == '9' || a[n + 1] == '9') F1(n + 1); if (now >= 100000) return; a[n]++; a[n + 1]++; printf("%lld %d\n", n, 1); now++; if (now >= 100000) return; } int main() { long long i, ok = 1, con = 0, N, t; scanf("%lld", &N); scanf("%s %s", a + 1, b + 1); for (i = 1; i <= N; i++) { c[i] = (long long)a[i] - '0'; d[i] = (long long)b[i] - '0'; } for (i = 1; i < N; i++) { if (c[i] > d[i]) { t = c[i] - d[i]; c[i] -= t; c[i + 1] -= t; con += t; } else { t = d[i] - c[i]; c[i] += t; c[i + 1] += t; con += t; } } if (c[N] != d[N]) printf("-1\n"); else { printf("%lld\n", con); for (i = 1; i < N; i++) { while (a[i] > b[i]) { F1(i); if (now >= 100000) break; } while (a[i] < b[i]) { F2(i); if (now >= 100000) break; } if (now >= 100000) break; } } return 0; }
19
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; for (long long i = 2; i * i <= n; i++) { while (n % (i * i) == 0) { n = n / i; } } cout << n; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int inf = 1e9; const int mod = 1e9 + 7; int l[maxn]; int r[maxn]; int main() { int n, p; cin >> n >> p; long double ans = 0.0; for (int i = 0; i < n; i++) cin >> l[i] >> r[i]; for (int i = 0; i < n; i++) { int j = (i + 1) % n; long double e = 1.0; e *= (r[i] - l[i] + 1 - r[i] / p + (l[i] - 1) / p); e *= (r[j] - l[j] + 1 - r[j] / p + (l[j] - 1) / p); e /= (r[i] - l[i] + 1); e /= (r[j] - l[j] + 1); e = 1.0 - e; ans += e * 2000.0; } cout.precision(7); cout << fixed << ans << endl; }
9
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int maxn = 500000 + 5; const int INF = 0x3f3f3f3f; const int P = 1000000007; const double PI = acos(-1.0); template <typename T> inline T read(T& x) { x = 0; int _f = 0; char ch = getchar(); while (ch < '0' || ch > '9') _f |= (ch == '-'), ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x = _f ? -x : x; } const int N = (int)1e6 + 10; int i, j, tot, Prime[N]; bool isPrime[N]; void init() { int i, j; for (i = 2; i <= N - 10; i++) { if (!isPrime[i]) { Prime[++Prime[0]] = i; if (!tot && i > 1000) tot = Prime[0] - 1; } for (j = 1; i * Prime[j] <= N - 10 && j <= Prime[0]; j++) { isPrime[i * Prime[j]] = true; if (i % Prime[j] == 0) break; } } } int get(int x) { if (i == 1) return 1; for (int i = 1; i <= tot; i++) { if (x % Prime[i] == 0) { while (x % Prime[i] == 0) { x /= Prime[i]; } if (x == 1) { return Prime[i]; } } } return x; } int main() { init(); int x; read(x); int tmp = x, tmp2 = x, y; for (i = 1; i <= tot; i++) { if (x % Prime[i] == 0) { while (x % Prime[i] == 0) { x /= Prime[i]; } if (x == 1) { y = Prime[i]; break; } } } if (x != 1) y = x; x = tmp - y + 1; tmp = x; int ans = 0; for (i = x; i <= tmp2; i++) { int res, ttt; if (!isPrime[i]) ttt = i; else res = get(i), ttt = i - res + 1; if (ttt < 3) continue; if (ans == 0) ans = ttt; else ans = min(ans, ttt); } cout << ans << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int max_n = 100000; int n; vector<int> neigh[max_n]; int val[max_n]; int c; int s[max_n], s_ed; int b[2 * max_n], b_ed; void run_dfs(int v) { s[++s_ed] = v; val[v] = s_ed; b[++b_ed] = val[v]; for (int j = 0; j < int(neigh[v].size()); j++) { int w = neigh[v][j]; if (val[w] == -1) run_dfs(w); else while (val[w] < b[b_ed]) b_ed--; } if (val[v] == b[b_ed]) { b_ed--; c++; while (val[v] <= s_ed) val[s[s_ed--]] = c; } } void strong() { s_ed = b_ed = -1; for (int v = 0; v < n; v++) val[v] = -1; c = n - 1; for (int v = 0; v < n; v++) if (val[v] == -1) run_dfs(v); for (int v = 0; v < n; v++) if (val[v] != -1) val[v] -= n; } int rsize[max_n]; vector<int> rneigh[max_n]; bool rvis[max_n]; void rdfs(int v, int &tot_size, bool &over) { if (!rvis[v]) { rvis[v] = true; tot_size += rsize[v]; if (rsize[v] > 1) over = true; for (int j = 0; j < int(rneigh[v].size()); j++) rdfs(rneigh[v][j], tot_size, over); } } int main() { int m; scanf("%d %d", &n, &m); while (m--) { int u, v; scanf("%d %d", &u, &v); u--; v--; neigh[u].push_back(v); } strong(); for (int v = 0; v < n; v++) { rsize[val[v]]++; for (int j = 0; j < int(neigh[v].size()); j++) { int a = val[v]; int b = val[neigh[v][j]]; rneigh[a].push_back(b); rneigh[b].push_back(a); } } int ans = 0; for (int v = n - 1; v >= 0; v--) if (!rvis[v] && rsize[v] > 0) { int tot_size = 0; bool over = false; rdfs(v, tot_size, over); ans += tot_size - 1 + int(over); } printf("%d\n", ans); return 0; }
14
#include <bits/stdc++.h> using namespace std; int main() { long double n, t; cin >> n >> t; cout << n * pow(1.000000011, t); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100005; struct edge { int x, y, l, Num; void fill(int _x, int _y, int _l) { x = _x, y = _y, l = _l; } bool friend operator<(const edge A, const edge B) { return A.l > B.l; } } E[N], q[N]; int v[N], Res[N]; int n, Q; struct segment { int l, r, mid, len, full; segment() { l = r = mid = len = full = 0; } } Empty; segment combine(segment A, segment B) { segment Res; if (A.full < 0) return B; if (B.full < 0) return A; if (A.full && B.full) Res.full = 1; else if (A.full) Res = B, Res.l += A.len; else if (B.full) Res = A, Res.r += B.len; else { Res.mid = v[A.r + B.l] + A.mid + B.mid; Res.l = A.l, Res.r = B.r; } Res.len = A.len + B.len; return Res; } namespace segT { segment f[N << 2]; segment query(int x, int l, int r, int _l, int _r) { if (_l <= l && r <= _r) return f[x]; if (r <= _l || _r <= l) return Empty; int mid = (l + r) >> 1; return combine(query(x << 1, l, mid, _l, _r), query(x + x + 1, mid, r, _l, _r)); } void modify(int x, int l, int r, int p) { if (l == r - 1) { f[x].full = f[x].len = 1; return; } int mid = (l + r) >> 1; if (p < mid) modify(x << 1, l, mid, p); else modify(x + x + 1, mid, r, p); f[x] = combine(f[x << 1], f[x + x + 1]); } } // namespace segT namespace lsd { vector<int> e[N]; int que[N], dep[N], pos[N], grphd[N], fa[N], size[N], prf[N]; void preprocessing() { fill(fa, fa + N, -1); fill(pos, pos + N, -1); fill(prf, prf + N, -1); int x, hd, tl, Ti = 0; for (hd = 0, tl = 1, x = 0; hd < tl; x = que[++hd]) for (int j = 0; j < e[x].size(); ++j) if (e[x][j] != fa[x]) fa[que[tl++] = e[x][j]] = x, dep[e[x][j]] = dep[x] + 1; for (int i = n - 1; i >= 0; --i) if (++size[x = que[i]]) for (int j = 0; j < e[x].size(); ++j) { if (e[x][j] == fa[x]) continue; size[x] += size[e[x][j]]; if (prf[x] < 0 || size[e[x][j]] > size[prf[x]]) prf[x] = e[x][j]; } for (int i = 0; i < n; ++i) if (pos[que[i]] < 0) for (int x = que[i]; x >= 0; x = prf[x]) pos[x] = Ti++, grphd[x] = que[i]; } int query(int x, int y) { segment R1 = Empty, R2 = Empty, Temp; while (grphd[x] != grphd[y]) if (dep[grphd[x]] > dep[grphd[y]]) { Temp = segT ::query(1, 0, n, pos[grphd[x]], pos[x] + 1); R1 = combine(Temp, R1), x = fa[grphd[x]]; } else { Temp = segT ::query(1, 0, n, pos[grphd[y]], pos[y] + 1); R2 = combine(Temp, R2), y = fa[grphd[y]]; } if (dep[x] > dep[y]) { Temp = segT ::query(1, 0, n, pos[y] + 1, pos[x] + 1); R1 = combine(Temp, R1); } else { Temp = segT ::query(1, 0, n, pos[x] + 1, pos[y] + 1); R2 = combine(Temp, R2); } swap(R1.l, R1.r); Temp = combine(R1, R2); if (Temp.full) return v[Temp.len]; else return Temp.mid + v[Temp.l] + v[Temp.r]; } } // namespace lsd void preprocessing() { int x, y, l; cin >> n >> Q; for (int i = 1; i < n; ++i) cin >> v[i]; for (int i = 1; i < n; ++i) { cin >> x >> y >> l; E[i - 1].fill(--x, --y, l); lsd ::e[x].push_back(y), lsd ::e[y].push_back(x); } for (int i = 0; i < Q; ++i) { cin >> x >> y >> l; q[i].fill(x - 1, y - 1, l), q[i].Num = i; } sort(E, E + n - 1), sort(q, q + Q); lsd ::preprocessing(), Empty.full = -1; for (int i = 0; i < n - 1; ++i) if (lsd ::fa[E[i].x] == E[i].y) swap(E[i].x, E[i].y); } void solve() { int p = 0; for (int i = 0; i < Q; ++i) { while (p < n - 1 && E[p].l >= q[i].l) segT ::modify(1, 0, n, lsd ::pos[E[p++].y]); Res[q[i].Num] = lsd ::query(q[i].x, q[i].y); } for (int i = 0; i < Q; ++i) cout << Res[i] << endl; } int main() { ios ::sync_with_stdio(0); preprocessing(); solve(); return 0; }
23
#include <bits/stdc++.h> using namespace std; void PV(vector<long long> v) { for (long long i = 0; i < (long long)v.size(); i++) cout << v[i] << " "; cout << "\n"; } void PVV(vector<pair<long long, long long> > v) { for (long long i = 0; i < (long long)v.size(); i++) cout << v[i].first << " " << v[i].second << "\n"; } void PA(long long v[], long long n, long long x = 0) { for (long long i = x; i < n + x; i++) cout << v[i] << ' '; cout << "\n"; } void IN(long long a[], long long n, long long x = 0) { for (long long i = x; i < n + x; i++) cin >> a[i]; } inline void op() {} const long long M = 1e9 + 7; const long long IM = 3e16 + 37; const long long N = 2e5 + 10; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); op(); long long x0, y0, ax, ay, bx, by; cin >> x0 >> y0 >> ax >> ay >> bx >> by; long long xs, ys, t; cin >> xs >> ys >> t; vector<pair<long long, long long> > v; long long x = x0, y = y0; do { v.push_back({x, y}); x = x * ax + bx; y = y * ay + by; } while (x <= IM && y <= IM); long long ans = 0, l = v.size(); for (long long i = 0; i < l; i++) { x = xs, y = ys; long long ck = 0, a, b; for (long long j = i; j < l; j++) { tie(a, b) = v[j]; ck += abs(a - x) + abs(b - y); x = a, y = b; if (ck <= t) ans = max(ans, abs(j - i) + 1); else break; } x = xs, y = ys; ck = 0; for (long long j = i; j >= 0; j--) { tie(a, b) = v[j]; ck += abs(a - x) + abs(b - y); x = a, y = b; if (ck <= t) ans = max(ans, abs(j - i) + 1); else break; } } cout << (ans) << "\n"; return 0; }
9
#include <bits/stdc++.h> using namespace std; map<char, int> mp; int main() { string s; int l, i; char ch; cin >> l; cin >> s; for (i = 0; i < l; i++) { s[i] = tolower(s[i]); mp[s[i]]++; } for (ch = 'a'; ch <= 'z'; ch++) { if (mp[ch] == 0) { puts("NO"); return 0; } } puts("YES"); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) { int n; cin >> n; string s, t; cin >> s >> t; unordered_map<char, int> mp; if (s.length() != t.length()) { cout << -1 << "\n"; continue; } unordered_map<char, int> m; for (long long i = 0; i < (n); i++) mp[s[i]] += 1; int flag = 0; for (long long i = 0; i < (n); i++) { mp[t[i]] -= 1; if (mp[t[i]] < 0) { flag = 1; break; } } if (flag) { cout << -1 << "\n"; continue; } vector<long long> ans; for (int i = 0; i < n; i++) { if (t[i] == s[i]) continue; int pos; for (int j = i + 1; j < n; j++) { if (t[i] == s[j]) { pos = j; break; } } pos -= 1; for (int k = pos; k >= i; k -= 1) { ans.emplace_back(k); swap(s[k], s[k + 1]); } } cout << ans.size() << "\n"; for (auto i : ans) cout << i + 1 << " "; cout << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<int> aray[100001]; vector<int> euler; int dist[100001]; void dfs(int root, int count) { dist[root] = count; euler.push_back(root); for (int i = 0; i < aray[root].size(); i++) { int val = aray[root][i]; if (dist[val] == -1) { dfs(val, count + 1); euler.push_back(root); } } return; } bool valid(int a, int b) { if (a > b) return false; if (((a - b) % 2) == 0) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int t = n - 1, x, y; while (t--) { cin >> x >> y; aray[x].push_back(y); aray[y].push_back(x); } for (int i = 1; i <= n; i++) dist[i] = -1; dfs(1, 0); int first[n + 1]; for (int i = euler.size() - 1; i >= 0; i--) first[euler[i]] = i; int m = euler.size(); int s = sqrt(m); int box = ceil((double)m / s); int num[box]; for (int i = 0; i < box; i++) { num[i] = 1000000000; for (int j = i * s; j < min(m, (i + 1) * s); j++) num[i] = min(num[i], dist[euler[j]]); } int q; cin >> q; while (q--) { int arr[4], k; for (int i = 0; i < 4; i++) cin >> arr[i]; cin >> k; int count = 0; bool ans = false; vector<int> dia; for (int i = 0; i < 2; i++) { for (int j = 2; j < 4; j++) { int fir = first[arr[i]], sec = first[arr[j]]; int temp = min(fir, sec), tmep = max(fir, sec); fir = temp; sec = tmep; int bfir = fir / s, bsec = sec / s; int val = 100000000; if (bfir == bsec) { for (int i = fir; i <= sec; i++) val = min(val, dist[euler[i]]); } else { for (int i = bfir + 1; i < bsec; i++) val = min(val, num[i]); for (int i = fir; i < bfir * s + s; i++) val = min(val, dist[euler[i]]); for (int i = s * bsec; i <= sec; i++) val = min(val, dist[euler[i]]); } int distance = dist[arr[i]] + dist[arr[j]] - 2 * val; dia.push_back(distance); } } if (valid(dia[0] + dia[3] + 1, k)) ans = true; if (valid(dia[1] + dia[2] + 1, k)) ans = true; int fir = first[arr[2]], sec = first[arr[3]]; int temp = min(fir, sec), tmep = max(fir, sec); fir = temp; sec = tmep; int bfir = fir / s, bsec = sec / s; int val = 10000000; if (bfir == bsec) { for (int i = fir; i <= sec; i++) val = min(val, dist[euler[i]]); } else { for (int i = bfir + 1; i < bsec; i++) val = min(val, num[i]); for (int i = fir; i < bfir * s + s; i++) val = min(val, dist[euler[i]]); for (int i = s * bsec; i <= sec; i++) val = min(val, dist[euler[i]]); } int distance = dist[arr[2]] + dist[arr[3]] - 2 * val; if (valid(distance, k)) ans = true; if ((distance <= k) && ((distance - k) % 2) == 0) ans = true; if (ans) cout << "YES\n"; else cout << "NO\n"; } return 0; }
12
#include <bits/stdc++.h> using namespace std; long long dp[4005][105]; int n; long long f(long lett, long len) { int i, sum = 0; if (lett < 0 || len < 0) return 0; if (lett == 0 && len == 0) return 1; if (dp[lett][len] >= 0) return dp[lett][len]; for (i = 1; i <= 26; i++) { sum += f(lett - i, len - 1); sum %= 1000000007LL; } dp[lett][len] = sum; return sum; } int main() { int runs; memset(dp, -1, sizeof dp); cin >> runs; while (runs--) { string s; cin >> s; int sum = 0; n = s.size(); for (int i = 0; i < n; i++) sum += s[i] - 'a' + 1; cout << f(sum, n) - 1 << endl; } return 0; }
12
#include <bits/stdc++.h> const int mod = 1e9 + 7; const int N = 1e6 + 5; inline int max(int a, int b) { return a > b ? a : b; } int n, m, a, b, fac[N], inv[N], pw_n[N], pw_m[N], ans; inline int combn(int n, int m) { if (m < 0 || m > n) return 0; return (long long)fac[n] * inv[m] % mod * inv[n - m] % mod; } int main() { scanf("%d%d%d%d", &n, &m, &a, &b); fac[0] = inv[0] = inv[1] = pw_n[0] = pw_m[0] = 1; for (int i = 1; i <= max(n, m); ++i) fac[i] = (long long)fac[i - 1] * i % mod; for (int i = 2; i <= max(n, m); ++i) inv[i] = (long long)(mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i <= max(n, m); ++i) inv[i] = (long long)inv[i - 1] * inv[i] % mod; for (int i = 1; i <= n; ++i) pw_n[i] = (long long)pw_n[i - 1] * n % mod; for (int i = 1; i <= n; ++i) pw_m[i] = (long long)pw_m[i - 1] * m % mod; for (int i = 1; i < n - 1; ++i) { ans = (ans + (long long)combn(m - 1, i - 1) * combn(n - 2, i - 1) % mod * fac[i - 1] % mod * (i + 1) % mod * pw_n[n - i - 2] % mod * pw_m[n - i - 1] % mod) % mod; } ans = (ans + (long long)combn(m - 1, n - 2) * combn(n - 2, n - 2) % mod * fac[n - 2] % mod) % mod; printf("%d\n", ans); return 0; }
16
#include <bits/stdc++.h> using namespace std; const int N = 100010; int t[5], b[5], f[5][N][18], m, n, k; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) scanf("%d", &f[j][i][0]); } int len = int(log(n) / log(2)); for (int j = 0; j < m; j++) for (int q = 1; q <= len; q++) for (int i = 0; i - 1 + (1 << q) < n; i++) f[j][i][q] = max(f[j][i][q - 1], f[j][i + (1 << (q - 1))][q - 1]); int tot = 0, ans = 0; for (int l = 0, r = 0; r < n;) { int tot = 0; int p = (int)(log((r - l + 1)) / log(2)); for (int j = 0; j < m; j++) { t[j] = max(f[j][l][p], f[j][r - (1 << p) + 1][p]); tot += t[j]; } if (tot <= k) r++; else l++; if (l > r) r++; if (ans < r - l) { ans = r - l; for (int j = 0; j < m; j++) b[j] = t[j]; } } for (int i = 0; i < m - 1; i++) printf("%d ", b[i]); printf("%d\n", b[m - 1]); return 0; }
12
#include <bits/stdc++.h> using namespace std; int n; set<long long> s; long long a[201000], ans; void solve(long long x) { long long res = 0; for (int i = 0; i < n; i++) { if (a[i] < x) res += x - a[i]; else res += min(a[i] % x, x - a[i] % x); } ans = min(ans, res); } void add(long long x) { for (long long i = 2; i * i <= x; i++) while (x % i == 0) x /= i, s.insert(i); if (x > 1) s.insert(x); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); ans = n; srand(unsigned(time(0))); random_shuffle(a, a + n); for (int i = 1; i <= 30; i++) { long long x = a[i]; add(x); add(x - 1); add(x + 1); } for (auto i : s) solve(i); cout << ans << endl; }
17
#include <bits/stdc++.h> using namespace std; typedef long double ldb; const int maxn = 1 << 21; const int N = int(1e5); const long long mod = int(1e9) + 7; const long long inf = 1e18; const double PI = 3.1415926536; inline int Set(int n, int pos) { return n = n | 1 << pos; } inline bool check(int n, int pos) { return n & 1 << pos; } inline int Reset(int n, int pos) { return n = n & ~(1 << pos); } inline int lastbit(int n) { return n & (-n); } inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } inline int lcm(int a, int b) { return (a * b) / gcd(a, b); } inline long long power(long long a, long long b) { long long ans = 1; a = a % mod; while (b) { if (b & 1) ans = (ans * a) % mod; b >>= 1; a = (a * a) % mod; } return ans; } inline long long invmod(long long a) { long long ans = power(a, mod - 2); return ans; } inline long long Egcd(long long a, long long b, long long *x, long long *y) { if (a == 0) { *x = 0, *y = 1; return b; } long long x1, y1; long long gcd = Egcd(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } inline int bs(int arr[], int l, int r, int item) { if (l == r) { if (arr[l] == item) return r; else return -1; } if (item <= arr[(l + r) / 2]) return bs(arr, l, (l + r) / 2, item); else return bs(arr, (l + r) / 2 + 1, r, item); } inline void fastin(int &x) { x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } return; } int leap[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nleap[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int l, r, t = 0; for (int i = 1; i < n + 1; i++) { cin >> l >> r; t = max(t, l); if (t > r) cout << 0 << " "; else { cout << t << " "; t++; } } cout << endl; } }
4
#include <bits/stdc++.h> using namespace std; int k, a, b; vector<int> ans; int dp[201][201]; bool solve(int l, int kp, vector<int> v) { if (dp[l][kp] != -1) return dp[l][kp]; if (kp == k && l == 0) { ans = v; return true; } if (l <= 0 && kp != k) return false; v.push_back(l); bool x = false; for (int i = a; i <= b; i++) { x |= solve(l - i, kp + 1, v); } dp[l][kp] = x; return x; } int main(int argc, char const *argv[]) { cin >> k >> a >> b; string s; cin >> s; int len = s.length(); memset(dp, -1, sizeof(int) * 201 * 201); vector<int> v; solve(len, 0, v); if (ans.size() == 0) { cout << "No solution"; return 0; } ans.push_back(0); s = "x" + s; string str = ""; sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size() - 1; i++) { for (int j = ans[i] + 1; j <= ans[i + 1]; j++) { str += s[j]; } str += "\n"; } cout << str; return 0; }
6
#include <bits/stdc++.h> using namespace std; inline long long int inp() { long long int n = 0; int ch = getchar(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getchar(); n = n * sign; return n; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; double x1, y1, x2, y2, vmax, t, vx, vy, wx, wy, x, y; cin >> x1 >> y1 >> x2 >> y2 >> vmax >> t >> vx >> vy >> wx >> wy; double l = 0.0, r = 1e9, m; for (int i = 0; i < 400; i++) { m = (l + r) / 2; x = x1 + min(m, t) * vx + max(0.0, m - t) * wx; y = y1 + min(m, t) * vy + max(0.0, m - t) * wy; ((hypot(x - x2, y - y2) < m * vmax) ? r : l) = m; } cout << r << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& o, const vector<T>& v) { int b = 0; for (const auto& a : v) o << (b++ ? ", " : "") << a; return o; } template <typename T, typename U> ostream& operator<<(ostream& o, const pair<T, U>& p) { o << p.X << ' ' << p.Y; return o; } long long log(long long x) { return 64 - __builtin_clzll(x) - 1; } const int NEG = 0; const int POS = 1; long long A[100000]; long long DP[2][100000]; long long f(long long c, long long p) { long long& x = DP[c][p]; if (x != -1) return x; if (p == 0) { if (c == NEG) { return x = abs(A[p] - (-1)); } return x = abs(A[p] - 1); } long long a = abs(A[p] - (-1)) + f(NEG == c, p - 1); long long b = abs(A[p] - 1) + f(POS == c, p - 1); return x = min(a, b); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } for (int i = 0; i < 2; i++) { for (int j = 0; j < 100000; j++) { DP[i][j] = -1; } } cout << f(POS, n - 1) << endl; }
1
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("O3") #pragma GCC target("avx2") using namespace std; int main() { cin.sync_with_stdio(0); cin.tie(0); long long T; cin >> T; for (int t = (0); t <= ((int)(T)-1); ++t) { string s, ta, tb; cin >> s; for (long long i = 0; i < 5; ++i) { ta.push_back(s[2 * i]); tb.push_back(s[2 * i + 1]); } long long res = 10; { long long a = 0, b = 0; for (long long i = 0; i < 5; ++i) { if (a > b + (5 - i)) { res = min(res, 2 * i); } if (ta[i] != '0') { ++a; } if (a > b + (5 - i)) { res = min(res, 2 * i + 1); } if (tb[i] == '1') { ++b; } if (a > b + (5 - i - 1)) { res = min(res, 2 * i + 2); } } } { long long a = 0, b = 0; for (long long i = 0; i < 5; ++i) { if (b > a + (5 - i)) { res = min(res, 2 * i); } if (ta[i] == '1') { ++a; } if (b > a + (5 - i - 1)) { res = min(res, 2 * i + 1); } if (tb[i] != '0') { ++b; } if (b > a + (5 - i - 1)) { res = min(res, 2 * i + 2); } } } cout << res << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 105; const long long INF = 1e18; long long f[MAXN][MAXN], a[MAXN][MAXN]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) scanf("%lld", &a[i][j]); } srand((unsigned)time(NULL)); int c[MAXN]; int T = 5000; long long ans = INF; while (T--) { for (int i = 1; i <= n; i++) c[i] = rand() & 1, f[0][i] = INF; f[0][1] = 0; for (int i = 0; i < k; i++) { for (int j = 1; j <= n; j++) { f[i + 1][j] = INF; } for (int j = 1; j <= n; j++) for (int p = 1; p <= n; p++) { if (c[j] != c[p]) f[i + 1][p] = min(f[i + 1][p], f[i][j] + a[j][p]); } } ans = min(ans, f[k][1]); } printf("%lld", ans); return 0; }
15