solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; template <class T, class T1> int chkmin(T &x, const T1 &y) { return x > y ? x = y, 1 : 0; } template <class T, class T1> int chkmax(T &x, const T1 &y) { return x < y ? x = y, 1 : 0; } const int MAXN = (1 << 11); int q; int n, m; int64_t a[MAXN][MAXN]; void read() { cin >> n >> m >> q; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) a[i][j] = s[j] - '0'; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { a[n + i][m + j] = a[i][j]; a[n + i][j] = a[i][j] ^ 1; a[i][m + j] = a[i][j] ^ 1; } n *= 2; m *= 2; } int pn(int i) { return (i / n) & 1; } int pm(int i) { return (i / m) & 1; } int get_par(int x, int y) { return (__builtin_popcount(x) % 2) ^ (__builtin_popcount(y) % 2); } int64_t sum(int x, int y) { if (x < 0 || y < 0) return 0; if (x < n && y < m) return a[x][y]; int64_t ret = 0; int M = (y / m) * m; int N = (x / n) * n; int p = get_par(x / n, y / m); if (p) ret += (x - N + 1) * 1ll * (y - M + 1) - a[x - N][y - M]; else ret += a[x - N][y - M]; ret += N * 1ll * (y + 1) / 2ll; ret += M * 1ll * (x + 1) / 2ll; ret -= N * 1ll * M / 2ll; return ret; } void solve() { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (i) a[i][j] += a[i - 1][j]; if (j) a[i][j] += a[i][j - 1]; if (i && j) a[i][j] -= a[i - 1][j - 1]; } for (int i = 0; i < q; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--, x2--, y2--; cout << sum(x2, y2) - sum(x1 - 1, y2) - sum(x2, y1 - 1) + sum(x1 - 1, y1 - 1) << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main(int argc, char* argv[]) { long long a, b; long long total = 0; cin >> a >> b; while (a > 0 && b > 0) { if (a > b) { total += a / b; a %= b; } else if (a < b) { total += b / a; b %= a; } else { total++; break; } } cout << total << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, x1, y1, x2, y2; long long int answ = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> x1 >> y1 >> x2 >> y2; answ += (x2 - x1 + 1) * (y2 - y1 + 1); } cout << answ; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n; int a[N], p[N]; int dp(int x) { if (p[x] != -1) return p[x]; for (int i = a[x]; x - i > 0 || x + i <= n; i += a[x]) { if (x - i > 0 && a[x - i] > a[x] && !dp(x - i)) return p[x] = 1; if (x + i <= n && a[x + i] > a[x] && !dp(x + i)) return p[x] = 1; } return p[x] = 0; } int main() { cin >> n; memset(p, -1, sizeof(int) * (n + 1)); for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { if (p[i] == -1) { dp(i); } cout << (p[i] ? 'A' : 'B'); ; } cout << endl; return 0; }
4
#include <bits/stdc++.h> std::map<int, std::list<int> > map; std::map<int, std::list<int> >::iterator it; short n, k; int i; std::list<int> in; int main() { scanf("%hd %hd", &n, &k); while (n--) { scanf("%d", &i); if (!map.count(i)) { in.clear(); in.push_back(i); map[i] = in; } for (it = map.begin(); map.size() < k && it != map.end(); ++it) if (it->second.back() != i && !map.count(it->first + i)) { in = it->second; in.push_back(i); map[it->first + i] = in; } } for (it = map.begin(); k-- && it != map.end(); ++it) { printf("%lu", it->second.size()); while (!it->second.empty()) { printf(" %d", it->second.front()); it->second.pop_front(); } puts(""); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, x; vector<int> g[maxn]; int d[2][maxn]; int cur; void dfs(int u, int par, int which) { d[which][u] = cur; cur++; for (int v : g[u]) if (v != par) dfs(v, u, which); cur--; } signed Main() { cin >> n >> x; int a, b; for (int i = 0; i < n - 1; i++) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } dfs(1, 0, 0); cur = 0; dfs(x, 0, 1); int ans = d[0][x]; for (int i = 1; i <= n; i++) if ((int)g[i].size() == 1) if (d[0][i] > d[1][i]) ans = max(ans, d[0][i]); cout << ans * 2 << '\n'; return 0; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); Main(); cout << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n <= 3) { cout << "NO"; return 0; } else cout << "YES" << endl; if (n % 2 == 0) { printf("1 * 2 = 2\n"); printf("2 * 3 = 6\n"); printf("6 * 4 = 24\n"); if (n == 4) return 0; int p = 4; n -= 2; while (n -= 2) { printf("%d - %d = %d\n", p + 2, p + 1, 1); printf("24 * 1 = 24\n"); p += 2; } } else { printf("5 * 3 = 15\n"); printf("2 * 4 = 8\n"); printf("15 + 8 = 23\n"); printf("23 + 1 = 24\n"); if (n == 5) return 0; int p = 5; n -= 3; while (n -= 2) { printf("%d - %d = %d\n", p + 2, p + 1, 1); printf("24 * 1 = 24\n"); p += 2; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; long double PI = 4 * atan(1); long long n, q, m, a[200001], t, l, r, b; pair<int, pair<int, int>> p[200001]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> q >> m; for (int i = 1; i < n + 1; i++) cin >> a[i]; for (int i = 0; i < q; i++) { cin >> p[i].first >> p[i].second.first >> p[i].second.second; } while (m--) { cin >> b; for (int i = q - 1; i >= 0; i--) { if (p[i].first == 1) { if (b >= p[i].second.first && b <= p[i].second.second) { if (b == p[i].second.first) b = p[i].second.second; else b--; } } else { if (b >= p[i].second.first && b <= p[i].second.second) { b = p[i].second.second - (b - p[i].second.first); } } } cout << a[b] << " "; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long int k, a, b; while (scanf("%lld %lld %lld", &k, &a, &b) != EOF) { if (a < k && b % k || b < k && a % k) printf("-1\n"); else printf("%lld\n", a / k + b / k); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, l; char z[120000]; int a[30]; int check(int x) { int i, j, last = -1; for (i = 1; i <= n; i++) if (z[i] <= 'a' + x - 1) last = i; if (last == -1) return 0; for (i = last + 1; i <= l - n + 1; i++) { if (z[i] <= 'a' + x - 1) last = i; if (i > last) { for (j = i + 1; j <= i + n - 1 && j <= l; j++) { if (z[j] <= 'a' + x - 1) last = j; } if (i > last) return 0; } } return 1; } int solve(int x) { int i, j, last = -1, sum, lp; sum = 0; lp = -1; x--; for (i = 1; i <= n; i++) { if (z[i] <= 'a' + x - 1) last = i; if (z[i] == 'a' + x) lp = i; } if (last == -1) last = lp, sum++; for (i = last + 1; i <= l - n + 1; i++) { if (z[i] <= 'a' + x - 1) last = i; if (z[i] == 'a' + x) lp = i; if (i > last) { for (j = i + 1; j <= i + n - 1 && j <= l; j++) { if (z[j] <= 'a' + x - 1) last = j; if (z[j] == 'a' + x) lp = j; } if (i > last) { sum++; last = lp; } } } return sum; } void work() { int i, j, ans, ansi; scanf("%d", &n); scanf("%s", z + 1); l = strlen(z + 1); for (i = 1; i <= 26; i++) { if (check(i) == 1) { ans = solve(i); ansi = i; break; } } for (i = 1; i <= l; i++) { a[z[i] - 'a' + 1]++; } for (i = 1; i < ansi; i++) for (j = 1; j <= a[i]; j++) printf("%c", 'a' + i - 1); for (i = 1; i <= ans; i++) printf("%c", 'a' + ansi - 1); printf("\n"); } int main() { work(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long y, k, n; cin >> y >> k >> n; long long cnt = 0; for (long long i = k; i <= n; i = i + k) { long long x = i - y; if (x > 0) { cnt++; cout << x << " "; } } if (cnt == 0) cout << "-1"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; int c = 0; cin >> n >> k; vector<int> S; for (int i = 0; i < n; i++) { cin >> c; S.push_back(c); } for (int i = k; i < S.size(); i++) { if (S[i - 1] != S[i]) { cout << "-1\n"; return 0; } } for (int j = k - 1; j >= 0; j--) { if (S[j] != S[j - 1]) { cout << j; return 0; } } }
2
#include <bits/stdc++.h> using namespace std; int main() { double n, m; scanf("%lf%lf", &n, &m); if (n == 1) printf("1\n"); else printf("%.10lf\n", 1 / n + (n - 1) / n * (m - 1) / (n * m - 1)); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int sz = 1005; int n, m, a[sz][sz]; long long dp1[sz][sz], dp2[sz][sz], dp3[sz][sz], dp4[sz][sz]; bool valid(int x, int y) { if (x >= 0 && x < n && y >= 0 && y < m) return 1; return 0; } long long solve1(int x, int y) { if (!valid(x, y)) return -1e14; if (x == 0 && y == 0) return a[x][y]; long long &ret = dp1[x][y]; if (ret != -1) return ret; return ret = max(solve1(x - 1, y), solve1(x, y - 1)) + a[x][y]; } long long solve2(int x, int y) { if (!valid(x, y)) return -1e14; if (x == n - 1 && y == m - 1) return a[x][y]; long long &ret = dp2[x][y]; if (ret != -1) return ret; return ret = max(solve2(x + 1, y), solve2(x, y + 1)) + a[x][y]; } long long solve3(int x, int y) { if (!valid(x, y)) return -1e14; if (x == 0 && y == m - 1) return a[x][y]; long long &ret = dp3[x][y]; if (ret != -1) return ret; return ret = max(solve3(x - 1, y), solve3(x, y + 1)) + a[x][y]; } long long solve4(int x, int y) { if (!valid(x, y)) return -1e14; if (x == n - 1 && y == 0) return a[x][y]; long long &ret = dp4[x][y]; if (ret != -1) return ret; return ret = max(solve4(x + 1, y), solve4(x, y - 1)) + a[x][y]; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) dp1[i][j] = dp2[i][j] = dp3[i][j] = dp4[i][j] = -1; solve1(n - 1, m - 1); solve2(0, 0); solve3(n - 1, 0); solve4(0, m - 1); long long maxi = 0; for (int i = 1; i < n - 1; ++i) for (int j = 1; j < m - 1; ++j) { long long t1 = dp3[i - 1][j] + dp4[i + 1][j] + dp2[i][j + 1] + dp1[i][j - 1]; long long t2 = dp3[i][j + 1] + dp4[i][j - 1] + dp2[i + 1][j] + dp1[i - 1][j]; maxi = max(maxi, max(t1, t2)); } return cout << maxi << endl, 0; }
4
#include <bits/stdc++.h> using namespace std; bool sortinrev(long long x, long long y) { return x > y; } void swap(long long* a, long long* b) { long long temp = *a; *a = *b; *b = temp; } long long div_ceil(long long a, long long b) { return (a + b - 1) / b; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } const int MOD = 1000 * 1000 * 1000 + 7; const long long INF = 1e18L + 5; long long fpow(long long a, long long b) { if (b == 0) return 1; if (b % 2 == 1) { return fpow(a, b - 1) * a % MOD; } else { long long b2 = fpow(a, b / 2) % MOD; return b2 * b2 % MOD; } } const int nax = 200005; const int N = int(1e6); bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } void solve() { long long n; cin >> n; long long m = n; while (!isPrime(m)) m++; cout << m << "\n1 " << n << "\n"; for (long long i = 0; i < n - 1; i++) cout << i + 1 << " " << i + 2 << "\n"; for (long long i = 0; i < m - n; i++) cout << i + 1 << " " << i + 1 + n / 2 << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; long long T = 1; while (t--) { cerr << "TEST-CASE #" << T << "\n"; solve(); T++; cerr << "\n"; } cerr << "\nI am Winning"; cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t, n, k, l, cn, m, w; string s; vector<int> v; cin >> t; for (int z = 0; z < t; z++) { l = 0; cn = 0; m = 0; cin >> n >> k; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { l++; v.push_back(i + 1); } } if (s.size() == 1 && s[0] == '0') { cn = 1; } else if (s.size() == 1 && s[0] == '1') { cn = 0; } else if (l == 0) { n = n - 1; cn = n / (k + 1); cn++; } else { m = 0; w = 0; if (s[0] == '0') { m = v[0] - 1; cn = m / (k + 1); } m = 0; w = 0; if (s[s.size() - 1] == '0') { m = s.size() - v[v.size() - 1]; w = m / (k + 1); cn += w; } if (v.size() >= 2) { m = 0; w = 0; for (int i = 0; i < v.size() - 1; i++) { m = v[i + 1] - (v[i] + 1) - k; w = m / (k + 1); cn += w; } } } cout << cn << endl; v.clear(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; using namespace chrono; long long int M = 1000000007; long long int min(long long int a, long long int b) { if (a < b) return a; return b; } long long int max(long long int a, long long int b) { return (a > b) ? a : b; } long long int modexpo(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = (res * a) % M; a = (a * a) % M; b /= 2; } return res; } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; void solve() { long long n, k; cin >> n >> k; vector<long long> arr(n); for (long long int i = 0; i < n; i++) cin >> arr[i]; ; string s; cin >> s; long long i = 0, j = 0; long long ans = 0; char prev = '0'; priority_queue<long long> st; while (i < n) { if (s[i] != prev) { long long t = min(st.size(), k); while (t--) { ans += st.top(); st.pop(); } while (!st.empty()) st.pop(); prev = s[i]; st.push(arr[i]); } else { st.push(arr[i]); } i++; } long long t = min(st.size(), k); while (t--) { ans += st.top(); st.pop(); } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAX = 0x7fffffff; const int INF = 0x7fffffff; const int mod = 1e9 + 7; int pairs[10][10]; int main() { ios_base::sync_with_stdio(false); string s; cin >> s; int pairs[10][10]; memset(pairs, 0, sizeof(int) * 100); for (int i = 0; i < s.size() - 1; i++) { pairs[s[i] - '0'][s[i + 1] - '0']++; } int ans[10][10]; for (int i = 0; i < 10; i++) { for (int j = i; j < 10; j++) { long long cnt = 0; bool did = true; for (int k = 0; k < 10; k++) { for (int l = 0; l < 10; l++) { int many = pairs[k][l]; if (many == 0) { continue; } int shortest = 1000; queue<pair<int, int> > q; q.push({k, 0}); int iters = 0; bool vis[10] = {false}; while (!q.empty()) { pair<int, int> front = q.front(); q.pop(); if (front.first == l && front.second != 0) { shortest = front.second; break; } if (vis[front.first]) { continue; } vis[front.first] = true; q.push({(front.first + i) % 10, front.second + 1}); q.push({(front.first + j) % 10, front.second + 1}); } if (shortest == 1000) { did = false; break; } cnt += (shortest - 1) * many; } if (!did) { break; } } if (!did) { ans[i][j] = -1; ans[j][i] = -1; } else { ans[i][j] = cnt; ans[j][i] = cnt; } } } for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { cout << ans[i][j] << " "; } cout << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T> void print(T& a) { for (auto x : a) cout << x << " "; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; map<string, string> m1, m2; for (int i = 0; i < n; i++) { string s1, s2; cin >> s1 >> s2; if (m2.find(s1) != m2.end()) { m1[m2[s1]] = s2; m2[s2] = m2[s1]; } else { m1[s1] = s2; m2[s2] = s1; } } cout << m1.size() << "\n"; for (auto x : m1) { cout << x.first << " " << x.second << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; constexpr int N = 3e3 + 1; int a[N], dp[N][N], nxt[N], prv[N], lst[N]; int main() { cin.tie(0), ios::sync_with_stdio(0); int t, n; cin >> t; while (t--) { cin >> n; memset(lst, -1, n * sizeof *lst); memset(prv, -1, n * sizeof *prv); memset(nxt, 127, n * sizeof *nxt); for (int i = 0; i < n; ++i) { cin >> a[i], --a[i]; if (lst[a[i]] != -1) { prv[i] = lst[a[i]]; nxt[lst[a[i]]] = i; } lst[a[i]] = i; } for (int le = 2; le <= n; ++le) for (int i = 0; i + le <= n; ++i) { int j = i + le - 1; dp[i][j] = dp[i + 1][j]; for (int z = nxt[i]; z <= j; z = nxt[z]) { dp[i][j] = max(dp[i][j], dp[i + 1][z - 1] + dp[z][j] + 1); } } cout << n - dp[0][n - 1] - 1 << '\n'; } return 0; }
9
#include <bits/stdc++.h> using namespace std; long long getMaxScore(int n, int m, long long k, vector<long long> a) { long long bestScore = 0; for (int s = 0; s < m; s++) { long long lowestSum = 0; long long curSum = 0; for (int i = 0; i < n; i++) { curSum += a[i]; long long modifier = 0; if (i >= s) { modifier = ((i - s) / m) + 1; } modifier *= k; if (i % m == s) { long long thisScore = (curSum - modifier) - lowestSum; bestScore = max(bestScore, thisScore); } long long thisOne = curSum - modifier; lowestSum = min(lowestSum, thisOne); } } return bestScore; } int main() { int n, m; long long k; scanf("%d%d%I64d", &n, &m, &k); vector<long long> a(n); for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); long long ans = getMaxScore(n, m, k, a); printf("%I64d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1); const long long INF = LLONG_MAX; const long long M = 1e5 + 10; const long long mod = 1e9 + 7; const long double eps = 1e-9; void read(long long t[], long long n) { for (int i = 1; i <= n; ++i) { cin >> t[i]; } return; } vector<pair<long long, long long> > G[M]; bool vis[M] = {false}; long long dist[M]; long long parent[M]; vector<long long> path; void dijkestra(vector<pair<long long, long long> > G[M], long long scr, int V) { for (int i = 1; i <= V; i++) dist[i] = INF; dist[scr] = 0; vis[scr] = true; parent[scr] = -1; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > q; q.push({0, scr}); while (!q.empty()) { long long a = q.top().second; vis[a] = 1; if (dist[a] < q.top().first) { q.pop(); continue; } q.pop(); for (auto b : G[a]) { if (dist[a] + b.second < dist[b.first] && !vis[b.first]) { dist[b.first] = dist[a] + b.second; parent[b.first] = a; q.push({dist[b.first], b.first}); } } } } void printPath(long long parent[], long long j) { if (parent[j] == -1) return; printPath(parent, parent[j]); printf("%d ", j); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long V, E, i; cin >> V >> E; for (int i = 0; i < E; ++i) { long long a, b, c; cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); } dijkestra(G, 1, V); if (dist[V] == INF) cout << -1; else { int index = V; while (true) { path.push_back(index); index = parent[index]; if (index == -1) break; } reverse(path.begin(), path.end()); for (auto x : path) cout << x << " "; cout << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long i, i1, j, k, t, n, m, res, check1, a, b, s, mat[1010][1010]; char x; vector<pair<long long, long long>> lst; vector<long long> v; map<vector<long long>, pair<long long, long long>> h; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> x; mat[i][j] = (long long)x; } } res = 0; for (j = 0; j < m; j++) { k = 1; lst.clear(); for (i = 0; i < 3; i++) { lst.push_back({-1, -1}); } for (i = 0; i < n; i++) { if (k > 1) { lst.pop_back(); } lst.push_back({k, mat[i][j]}); s = lst.size(); if (lst[s - 1].first == lst[s - 2].first && lst[s - 1].first <= lst[s - 3].first) { v = {lst[s - 1].second, lst[s - 2].second, lst[s - 3].second, i, k}; if (h[v].second == j - 1) { res += ++h[v].first; } else { h[v].first = 1; res++; } h[v].second = j; } if (i != n - 1) { if (mat[i + 1][j] == mat[i][j]) { k++; } else { k = 1; } } } } cout << res; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long double a, b, c, x1, x2, d; cin >> a >> b >> c; if (a == 0) { if (b != 0) { x1 = -c / b; cout << 1 << endl; cout.precision(10); cout << fixed << x1 << endl; } else if (c == 0) cout << -1 << endl; else cout << 0; } else { d = b * b - 4 * a * c; if (d < 0) cout << 0 << endl; else if (d == 0) { x1 = -b / (2 * a); cout << 1 << endl; cout.precision(10); cout << fixed << x1 << endl; } else { x1 = (-b + sqrt(d)) / (2 * a); x2 = (-b - sqrt(d)) / (2 * a); cout << 2 << endl; cout.precision(10); cout << fixed << min(x1, x2) << endl; cout << fixed << max(x1, x2) << endl; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long NMAX = 1e6 + 5, INF = 1e9 + 9; int cnt[1005]; int main() { int n, m, x, level = 0, ans = 0; cin >> n >> m; for (int i = 1; i <= m; ++i) { cin >> x; ++cnt[x]; bool flag = true; for (int j = 1; j <= n; ++j) if (cnt[j] == level) { flag = false; break; } if (flag) { ++level; ++ans; } } cout << ans; }
0
#include <bits/stdc++.h> using namespace std; long long dp[25][4], a[25]; long long dfs(int pos, int sta, int lim) { if (pos == -1) return 1; if (!lim && ~dp[pos][sta]) return dp[pos][sta]; int sup = lim ? a[pos] : 9; long long tmp = 0; for (int i = 0; i <= sup; i++) { if (i && sta == 3) continue; tmp += dfs(pos - 1, sta + (i ? 1 : 0), lim & (i == a[pos])); } if (!lim) dp[pos][sta] = tmp; return tmp; } long long solve(long long x) { int p = -1; for (; x; x /= 10) a[++p] = x % 10; return dfs(p, 0, 1); } int main() { int T; cin >> T; memset(dp, -1, sizeof dp); while (T--) { long long l, r; scanf("%lld%lld", &l, &r); printf("%lld\n", solve(r) - solve(l - 1)); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 5, inf = 1e9 + 100; long long a[N], lg[N], ll[N], rg[N], rl[N], dp[N]; vector<long long> j[N]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; for (long long i = 1; i <= n; ++i) { cin >> a[i]; dp[i] = inf; } vector<pair<long long, long long> > v; for (long long i = 1; i <= n; ++i) { while (v.size() && v.back().first < a[i]) v.pop_back(); if (v.size()) lg[i] = v.back().second; v.push_back({a[i], i}); } v.clear(); for (long long i = 1; i <= n; ++i) { while (v.size() && v.back().first > a[i]) v.pop_back(); if (v.size()) ll[i] = v.back().second; v.push_back({a[i], i}); } v.clear(); for (long long i = n; i >= 1; --i) { while (v.size() && v.back().first < a[i]) v.pop_back(); if (v.size()) rg[i] = v.back().second; v.push_back({a[i], i}); } v.clear(); for (long long i = n; i >= 1; --i) { while (v.size() && v.back().first > a[i]) v.pop_back(); if (v.size()) rl[i] = v.back().second; v.push_back({a[i], i}); } for (long long i = 1; i <= n; ++i) { if (rl[i] != 0) j[i].push_back(rl[i]); if (rg[i] != 0) j[i].push_back(rg[i]); if (ll[i] != 0) j[ll[i]].push_back(i); if (lg[i] != 0) j[lg[i]].push_back(i); } dp[1] = 0; for (long long i = 1; i <= n; ++i) for (auto to : j[i]) dp[to] = min(dp[to], dp[i] + 1); cout << dp[n]; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 1003, INF = 2000000009; struct ban { int x, i; ban() {} ban(int x, int i) { this->x = x; this->i = i; } }; int n, m; int s, f; pair<pair<int, int>, int> b[N * 30]; vector<ban> a[N]; int hi; bool c[N]; int pi[N], px[N]; bool dfs0(int x) { c[x] = true; if (x == f) return true; for (int i = 0; i < a[x].size(); ++i) { int h = a[x][i].x; if (a[x][i].i == hi) continue; if (!c[h]) { pi[h] = a[x][i].i; px[h] = x; if (dfs0(h)) return true; } } return false; } vector<int> path() { for (int i = 1; i <= n; ++i) c[i] = false; dfs0(s); vector<int> v; if (!c[f]) return v; for (int i = f; i != s; i = px[i]) { v.push_back(pi[i]); } reverse(v.begin(), v.end()); return v; } vector<int> vv; int tin[N], ti, fup[N]; void dfs(int x, int pi) { c[x] = true; tin[x] = ti++; fup[x] = tin[x]; for (int i = 0; i < a[x].size(); ++i) { int h = a[x][i].x; if (a[x][i].i == pi) continue; if (a[x][i].i == hi) continue; if (!c[h]) { dfs(h, a[x][i].i); fup[x] = min(fup[x], fup[h]); if (fup[h] > tin[x]) vv.push_back(a[x][i].i); } else { fup[x] = min(fup[x], tin[h]); } } } vector<int> bridges() { for (int i = 1; i <= n; ++i) c[i] = false; vv.clear(); dfs(s, 0); return vv; } int ans = INF; int anss; int ans1, ans2; int main() { cin >> n >> m; cin >> s >> f; for (int i = 1; i <= m; ++i) { int x, y, z; cin >> x >> y >> z; a[x].push_back(ban(y, i)); a[y].push_back(ban(x, i)); b[i] = make_pair(make_pair(x, y), z); } if (s == f) { cout << "-1" << endl; return 0; } vector<int> somepath = path(); if (somepath.empty()) { cout << 0 << endl << 0 << endl; return 0; } for (int i = 0; i < somepath.size(); ++i) { hi = somepath[i]; vector<int> p = path(); if (p.empty()) { if (b[somepath[i]].second < ans) { ans = b[somepath[i]].second; anss = 1; ans1 = somepath[i]; } continue; } set<int> s; for (int j = 0; j < p.size(); ++j) s.insert(p[j]); if (hi == 2) cerr << ""; vector<int> br = bridges(); for (int j = 0; j < br.size(); ++j) { if (s.find(br[j]) != s.end()) { if (b[somepath[i]].second + b[br[j]].second < ans) { ans = b[somepath[i]].second + b[br[j]].second; anss = 2; ans1 = somepath[i]; ans2 = br[j]; } } } } if (ans == INF) { cout << "-1" << endl; return 0; } cout << ans << endl; cout << anss << endl; if (anss == 1) cout << ans1 << endl; else cout << ans1 << ' ' << ans2 << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; long long k, d, t; long long T, T1, T2; long long v; double res; int main() { cin >> k >> d >> t; if (k % d == 0) { res = 1.0 * t; printf("%f\n", res); } else { T = k + d - k % d; T2 = d - k % d; T1 = k; t = t * 2; v = 2 * k + T2; res = t / v * T; t %= v; if (2 * T1 >= t) { res += 0.5 * t; } else { res += 1.0 * T1 + 1.0 * t - 2.0 * T1; } printf("%f\n", res); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 5, N = 2e5 + 5, mod = 1e9 + 7, M = 1e6 + 5; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { long long n, k; cin >> n >> k; long long a[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); for (int i = n - 1; i >= 1; i--) { if (k == 0) break; a[n] += a[i]; a[i] = 0; k--; } sort(a + 1, a + 1 + n); cout << a[n] - a[1] << endl; } }
0
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-10L; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; t = 1; while (t--) { long long r, x, y, xx, yy; cin >> r >> x >> y >> xx >> yy; long long d = (x - xx) * (x - xx) + (y - yy) * (y - yy); long double dd = sqrt(1.0L * d); r = r + r; long double res = dd / (1.0L * r); long long p = (long long)res; if (fabs(1.0L * p * 1.0L * r - dd) > eps) p++; cout << p << endl; return 0; } return 0; }
3
#include <bits/stdc++.h> using namespace std; void add(char *str, int len) { for (int i = 0; i < len; i++) if (str[i] == '9') str[i] = '0'; else str[i] += 1; } int getministr(char *str, int l) { int i, j, k; i = 0; j = 1; k = 0; while (i < l && j < l) { k = 0; while (str[i + k] == str[j + k] && k < l) k++; if (k == l) return i; if (str[i + k] > str[j + k]) if (i + k + 1 > j) i = i + k + 1; else i = j + 1; else if (j + k + 1 > i) j = j + k + 1; else j = i + 1; } if (i < l) return i; return j; } int main() { char str[2200]; char str2[1100]; int n; while (cin >> n) { cin.get(); scanf("%s", str); int pos; for (int i = 0; i <= n; i++) str[i + n] = str[i]; str[n * 2] = 0; pos = getministr(str, n); for (int i = 0; i < n; i++) str2[i] = str[i + pos]; str2[n] = 0; int j; bool flag = false; for (int i = 0; i < 10; i++) { add(str, 2 * n); pos = getministr(str, n); flag = false; for (j = 0; j < n && (!flag); j++) { if (str2[j] > str[j + pos]) flag = true; if (str2[j] < str[j + pos]) break; } if (flag) for (j = pos; j < pos + n; j++) str2[j - pos] = str[j]; } cout << str2 << endl; } }
3
#include <bits/stdc++.h> using namespace std; int n, m; void update(int *c, int x, int v) { while (x <= m) { c[x] += v; x += x & -x; } } long long sum(int *c, int x) { long long ret = 0; while (x > 0) { ret += c[x]; x -= x & -x; } return ret; } long long calc(int *c, int L, int R) { return sum(c, R) - sum(c, L - 1); } int c[3060 + 3060][3060], L[3060][3060], R[3060][3060], Ld[3060][3060]; char s[3060][3060]; vector<pair<int, int> > g[3060]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { if (s[i][j] == '.') L[i][j] = 0; else L[i][j] = L[i][j - 1] + 1; } for (int i = n; i > 0; --i) for (int j = m; j > 0; --j) { if (s[i][j] == '.') R[i][j] = 0; else R[i][j] = R[i][j + 1] + 1; if (s[i][j] == '.') Ld[i][j] = 0; else Ld[i][j] = Ld[i + 1][j - 1] + 1; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) g[j + R[i][j] - 1].push_back(make_pair(i, j)); long long ans = 0; for (int j = m; j > 0; --j) { for (int i = 0; i < g[j].size(); ++i) { int x = g[j][i].first; int y = g[j][i].second; update(c[x + y], y, 1); } for (int i = 1; i <= n; ++i) { int mi = min(L[i][j], Ld[i][j]); if (!mi) continue; ans += calc(c[i + j], j - mi + 1, j); } } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long N = 2e3 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t1; cin >> t1; while (t1--) { long long a, b, n, m; cin >> a >> b >> n >> m; if ((a + b) < (n + m)) { cout << "No" << endl; continue; } else { if (min(a, b) >= m) cout << "Yes" << endl; else cout << "No" << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7, N = 200000, BASE = 130; long long MOD(long long x) { x = x % INF; if (x < 0) x += INF; return x; } struct pair_ { long long f, s, ind; }; string s; long long n, m; vector<long long> hash_, power; void init() { power.resize(n); power[0] = 1; for (int i = 1; i < n; i++) { power[i] = MOD(power[i - 1] * BASE); } hash_.resize(n); hash_[0] = s[0]; for (int i = 1; i < n; i++) { hash_[i] = MOD(hash_[i - 1] + s[i] * power[i]); } } bool check(long long x, long long y, long long d) { if (x > y) { swap(x, y); } long long up = y - x; long long f = hash_[x + d]; if (x) { f -= hash_[x - 1]; } long long s = hash_[y + d]; if (y) { s -= hash_[y - 1]; } if (MOD(f * power[up]) == MOD(s)) { return true; } return false; } void program() { cin >> s; s += (int)31; n = s.size(); vector<int> ar(n); for (int i = 0; i < n; i++) { ar[i] = (int)s[i]; } int len = 1; while (len < n) { vector<pair_> tmp(n), tmp_(n); for (int i = 0; i < n; i++) { pair_ p; p.f = ar[i]; p.s = ar[(i + len) % n]; p.ind = i; tmp[i] = p; } map<pair<int, int>, int> sorted; vector<int> cnt(N), go(N); for (int i = 0; i < n; i++) { cnt[tmp[i].s]++; } for (int i = 1; i < N; i++) { go[i] = go[i - 1] + cnt[i - 1]; } for (int i = 0; i < n; i++) { int num = tmp[i].s; tmp_[go[num]] = tmp[i]; go[num]++; } cnt.assign(N, 0); go.assign(N, 0); for (int i = 0; i < n; i++) { cnt[tmp_[i].f]++; } for (int i = 1; i < N; i++) { go[i] = go[i - 1] + cnt[i - 1]; } for (int i = 0; i < n; i++) { int num = tmp_[i].f; tmp[go[num]] = tmp_[i]; go[num]++; } int count = 1; sorted[make_pair(tmp[0].f, tmp[0].s)] = 0; for (int i = 1; i < n; i++) { if (tmp[i].f != tmp[i - 1].f || tmp[i].s != tmp[i - 1].s) { sorted[make_pair(tmp[i].f, tmp[i].s)] = count; count++; } } for (int i = 0; i < n; i++) { pair_ p = tmp[i]; int num = sorted[make_pair(p.f, p.s)]; ar[p.ind] = num; } len *= 2; } vector<int> arr(n); for (int i = 0; i < n; i++) { arr[ar[i]] = i; } init(); vector<pair<int, int>> st; long long tmp = 0, ans = 0; st.push_back(make_pair(0, 0)); vector<int> cnt(n); for (int i = 0, j = 1; j < n; i++, j++) { long long p = arr[i], q = arr[j]; long long l = -1, r = min(n - p, n - q); while (l + 1 < r) { long long mid = (l + r) / 2; bool if_ = check(p, q, mid); if (if_) { l = mid; } else { r = mid; } } cnt[p] = max((long long)cnt[p], r); cnt[q] = max((long long)cnt[q], r); if (r > tmp) { tmp = r; st.push_back(make_pair(tmp, i)); } while (r < st.back().first) { auto b = st.back(); auto a = (long long)st[st.size() - 2].first; long long num = (i - b.second + 1); ans += num * num * (b.first - max(a, r)); st.pop_back(); tmp = a; if (r > a) { tmp = r; st.push_back(make_pair(r, b.second)); } } } int r = 0; while (r < st.back().first) { auto b = st.back(); auto a = st[st.size() - 2]; long long num = (n - 1 - b.second + 1); ans += num * num * (b.first - a.first); st.pop_back(); tmp = a.first; } for (int i = 0; i < n; i++) { ans += n - arr[i] - cnt[i] - 1; } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { program(); } }
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 6010; const int mod = 1000000007; int n; long long k[MAXN], k2[MAXN]; int main() { string asd; string asd2; cin >> asd; cin >> asd2; n = ((int)(asd2.size())); long long res = 0; for (int i = 0; i < ((int)(asd.size())); i++) { for (int j = 0; j < ((int)(asd2.size())); j++) { if (asd2[j] == asd[i]) { k2[j] = (k2[j] + k[j - 1] + 1) % mod; } k2[j + 1] = (k2[j + 1] + k2[j]) % mod; } res = (res + k2[n]) % mod; for (int j = 0; j < ((int)(asd2.size())); j++) k[j] = k2[j]; memset(k2, 0, sizeof k2); } cout << res << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 5; bool vis[maxn]; vector<int> v; int solve(int s) { int ans = 0; for (int i = 0; i < v.size(); i++) { ans += abs(v[i] - s); s += 2; } return ans; } int main() { ios::sync_with_stdio(false); int n; cin >> n; int p; for (int i = 0; i < n / 2; i++) { cin >> p; vis[p] = 1; v.push_back(p); } sort(v.begin(), v.end()); int cnt = min(solve(1), solve(2)); cout << cnt << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define fi first #define se second #define mk make_pair #define MOD 1000000007 int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; //t=1; while(t--) { ll n, c, ans = 1e18, pre = 0, even_min = 1e18, odd_min = 1e18; cin >> n; for (ll i = 0; i < n; ++i) { cin >> c; i % 2 ? odd_min = min(odd_min, c) : even_min = min(even_min, c); pre += c; if (i>0) ans = min(ans, pre + (n - 1 - i / 2) * even_min + (n - i + i / 2) * odd_min); } cout << ans << '\n'; } }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; long long ans = 1; set<int> buy = {-INT_MAX}, sell = {INT_MAX}; vector<int> open; for (int i = 0; i < n; i++) { string op; int p; cin >> op >> p; if (op == "ADD") { if (p < *buy.rbegin()) buy.insert(p); else if (p > *sell.begin()) sell.insert(p); else open.push_back(p); } else { int bs = *sell.begin(), bb = *buy.rbegin(); if (bs < p || bb > p) printf("0\n"), exit(0); else if (bs > p && p > bb) ans = ans * 2 % mod; buy.erase(p), sell.erase(p); for (int x : open) if (x > p) sell.insert(x); else if (x < p) buy.insert(x); open.clear(); } } printf("%lld\n", ans * (open.size() + 1) % mod); }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int G[100001]; G[0] = 0; G[1] = 0; G[2] = 0; int A[100001]; memset((A), 0, sizeof(A)); int xo; int l; for (int i = 3; i <= n; ++i) { int k = 2, o, opt; int reza = 1; int mm = 0; while (k + reza <= i) { o = i - reza; if (o % k == 0) { opt = o / k; xo = 0; for (l = opt; l < opt + k; l++) { xo ^= G[l]; } if (i == 15) { } A[xo] = 1; mm = max(mm, xo); } reza += k; k++; } for (int j = 0; j < 100001; ++j) { if (A[j] == 0) { G[i] = j; for (int p = 0; p < mm + 1; p++) A[p] = 0; break; } } } if (G[n] != 0) { int k = 2, o, opt; int reza = 1; while (k + reza <= n) { o = n - reza; if (o % k == 0) { opt = o / k; int xo = 0; for (int l = opt; l < opt + k; l++) { xo ^= G[l]; } if (xo == 0) { cout << k << endl; return 0; } } reza += k; k++; } cerr << "FATAL" << endl; exit(1); } else { cout << -1 << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 105, tt = 1e9 + 7; int n, lnk[maxn], son[2 * maxn], nxt[2 * maxn], Y[maxn], X[maxn], mat[maxn][maxn], F[maxn], fn[maxn]; struct edge { int x, y; } e[maxn]; int qsm(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = 1ll * ret * x % tt; y >>= 1, x = 1ll * x * x % tt; } return ret; } int gauss() { int ret = 1; for (int i = 1; i < n; i++) { if (mat[i][i] == 0) return 0; ret = 1ll * ret * mat[i][i] % tt; for (int j = i + 1; j < n; j++) { int tmp = 1ll * mat[j][i] * qsm(mat[i][i], tt - 2) % tt; for (int k = 1; k < n; k++) mat[j][k] = (mat[j][k] + tt - 1ll * mat[i][k] * tmp % tt) % tt; } } return ret; } int matrixt(int V) { memset(mat, 0, sizeof(mat)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i != j) mat[i][j] = (mat[i][j] + tt - 1) % tt, mat[i][i] = (mat[i][i] + 1) % tt; for (int i = 1; i < n; i++) { int x = e[i].x, y = e[i].y; mat[x][x] = (mat[x][x] + V - 1) % tt, mat[y][y] = (mat[y][y] + V - 1) % tt; mat[x][y] = (mat[x][y] + tt - (V - 1)) % tt, mat[y][x] = (mat[y][x] + tt - (V - 1)) % tt; } return gauss(); } void solve() { for (int i = 1; i <= n; i++) { int mul = Y[i]; for (int j = 1; j <= n; j++) if (j != i) mul = 1ll * mul * qsm((X[i] - X[j] + tt) % tt, tt - 2) % tt; memset(fn, 0, sizeof(fn)); fn[0] = 1; for (int j = 1; j <= n; j++) if (j != i) { for (int k = n; k >= 1; k--) fn[k] = (1ll * fn[k] * (tt - X[j]) % tt + fn[k - 1]) % tt; fn[0] = 1ll * fn[0] * (tt - X[j]) % tt; } for (int j = 0; j <= n; j++) F[j] = (F[j] + 1ll * fn[j] * mul % tt) % tt; } } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) scanf("%d%d", &e[i].x, &e[i].y); for (int i = 1; i <= n; i++) Y[i] = matrixt(i), X[i] = i; solve(); for (int i = 0; i < n; i++) printf("%d ", F[i]); puts(""); return 0; }
9
#include <bits/stdc++.h> using namespace std; char buf[1010]; int curLen; string inc(const string& s) { int i = s.length() - 1; string res = s; while (i >= 0 && s[i] == '1') { res[i] = '0'; --i; } if (i < 0) { res = "1" + res; } else { res[i] = '1'; } return res; } int main() { cout << fixed; cout.precision(14); int n; cin >> n; vector<pair<int, int> > lengths(n); for (int i = 0; i < n; i++) { lengths[i].second = i; cin >> lengths[i].first; } sort(lengths.begin(), lengths.end()); vector<string> res(n); curLen = lengths[0].first; buf[curLen] = 0; string s(curLen, '0'); res[lengths[0].second] = s; for (int i = 1; i < n; i++) { if (find(s.begin(), s.end(), '0') == s.end()) { cout << "NO\n"; return 0; } s = inc(s); int tmp = s.length(); s.resize(lengths[i].first); for (int j = tmp; j < lengths[i].first; ++j) { s[j] = '0'; } res[lengths[i].second] = s; } cout << "YES\n"; for (int i = 0; i < n; i++) { cout << res[i] << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long modexp(long long n, long long m) { if (m == 0) return 1; else if (m == 1) return n; else { long long p = modexp(n, m / 2); if (m % 2 == 1) return (((p * p) % 1000000007) * n) % 1000000007; else return (p * p) % 1000000007; } } long long exp(long long n, long long m) { if (m == 0) return 1; if (m == 1) return n; long long p = exp(n, m / 2); if (m % 2 == 1) return p * p * n; else return p * p; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long inv(long long n) { return modexp(n, 1000000007 - 2); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long factorial(long long fact[], long long n) { fact[0] = 1; fact[1] = 1; long long prod = 1, i; for (i = 2; i < n + 1; i++) { prod = (prod * i) % 1000000007; fact[i] = prod; } return prod; } long long longestPrefixSuffix(string s) { long long n = s.length(); long long lps[n]; lps[0] = 0; long long len = 0; long long i = 1; while (i < n) { if (s[i] == s[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } return lps[n - 1]; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t, n, m, q, i, j, k; string s, p; cin >> s >> p; if (p.length() > s.length()) cout << s << endl; else { n = s.length(); m = p.length(); long long maxlensuffix = longestPrefixSuffix(p); string pre = ""; if (maxlensuffix > 0) pre = p.substr(0, maxlensuffix); long long sn0 = 0, sn1 = 0, pn0 = 0, pn1 = 0; for (i = 0; i < n; i++) { if (s[i] == '1') sn1++; else sn0++; } for (i = 0; i < m; i++) { if (p[i] == '1') pn1++; else pn0++; } if (sn0 >= pn0 && sn1 >= pn1) { string ans = p; sn0 -= pn0; sn1 -= pn1; long long n1 = 0, n0 = 0; for (i = 0; i < maxlensuffix; i++) { if (pre[i] == '1') n1++; else n0++; } n1 = pn1 - n1; n0 = pn0 - n0; string suff = p.substr(maxlensuffix, m - maxlensuffix); long long occur = 0; if (n1 == 0 && n0 == 0) { } else if (n0 == 0) { occur = sn1 / n1; for (i = 0; i < occur; i++) ans += suff; } else if (n1 == 0) { occur = sn0 / n0; for (i = 0; i < occur; i++) ans += suff; } else { occur = min(sn1 / n1, sn0 / n0); for (i = 0; i < occur; i++) ans += suff; } for (i = 0; i < sn1 - occur * n1; i++) ans += '1'; for (i = 0; i < sn0 - occur * n0; i++) ans += '0'; cout << ans << endl; } else cout << s << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[110000]; bool ok(int a[], int n) { int c = a[0], b; if (a[0] == 0) b = 0; else b = a[1] / a[0]; if (c == 0) { for (int i = 1; i < n; i++) if (a[i]) return false; return true; } for (int i = 1; i < n; i++) { if (a[i] * a[0] != a[i - 1] * a[1]) return false; } return true; } bool ok(int a[], int n, int c, int b) { if (c == 0) { int skip = 0; for (int i = 0; i < n; i++) { if (a[i]) skip++; } return skip <= 1; } int last = 0, skip = 0; for (int i = 1; i < n; i++) { if (a[i] * a[0] != a[last] * a[1]) { skip++; continue; } last = i; } return skip <= 1; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); } if (ok(a, n)) { puts("0"); return 0; } if (ok(a + 1, n - 1)) { puts("1"); return 0; } swap(a[0], a[1]); if (ok(a + 1, n - 1)) { puts("1"); return 0; } swap(a[0], a[1]); int c = a[0], b; if (a[0] == 0) b = 0; else b = a[1] / a[0]; if (ok(a, n, c, b)) { puts("1"); } else { puts("2"); } return 0; }
7
#include <bits/stdc++.h> int f(int *a, int begin, int len, int m, int s) { int res = 0; for (int i = 0; i < len; i++) { if ((s & (1 << i)) == 0) continue; res = (res + a[begin + i]) % m; } return res; } int binary_search(int *a, int size, int val) { if (size == 0) return 0; int i = 0, j = size - 1; while (i < j) { int mid = (j - i) / 2 + i; if (a[mid] == val) return val; else if (a[mid + 1] > val) j = mid; else i = mid + 1; } return a[i]; } int main() { int n, m, a[36], lhs[1 << 18], rhs[1 << 18]; while (std::cin >> n >> m) { for (int i = 0; i < n; i++) std::cin >> a[i]; int lhsLen = 1 << (n / 2); for (int s = 0; s < lhsLen; s++) { lhs[s] = f(a, 0, n / 2, m, s); } int rhsLen = 1 << (n - n / 2); for (int s = 0; s < rhsLen; s++) { rhs[s] = f(a, n / 2, n - n / 2, m, s); } std::sort(rhs, rhs + rhsLen); int res = 0; for (int i = 0, val; i < lhsLen; i++) { val = binary_search(rhs, rhsLen, m - lhs[i] - 1); res = std::max(res, (lhs[i] + val) % m); val = binary_search(rhs, rhsLen, m * 2 - lhs[i] - 1); res = std::max(res, (lhs[i] + val) % m); } std::cout << res << std::endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int x; cin >> x; string s[x]; for (int i = 0; i < x; i++) { cin >> s[i]; } for (int i = 0; i < x; i++) { if (s[i].length() > 10) { int t = s[i].length() - 1; cout << s[i].at(0) << (s[i].length() - 2) << s[i].at(t) << endl; } else cout << s[i] << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long bananaz(long long x, long long y) { return (x + 1) * (y + 1) * (x + y) / 2; } int main() { ios_base::sync_with_stdio(false); long long m, b, x = 0, y, max = -1; cin >> m >> b; y = b; while (y >= 0) { if (bananaz(x, y) > max) max = bananaz(x, y); x++; while (y > -1.0 * x / m + b) y--; } cout << max << endl; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n][2], i; for (i = 0; i < n; i++) cin >> a[i][0]; for (i = 0; i < n; i++) cin >> a[i][1]; vector<long long> v; for (i = 0; i < n; i++) { if (a[i][1] == 0) v.push_back(a[i][0]); } sort(v.begin(), v.end(), greater<int>()); long long j = 0; for (i = 0; i < n; i++) { if (a[i][1] == 0) { cout << v[j] << " "; j++; } else cout << a[i][0] << " "; } cout << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const double pi = acos(-1); int n, i, j, k, t; int a[500002]; long long b[500002]; int main() { memset(b, -1, sizeof b); scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%d", a + i); a[n + i] = a[i]; } if (*max_element(a, a + n) == *min_element(a, a + n)) { if (a[0] != 0) return puts("NO") & 0; puts("YES"); for (i = 0; i < n; ++i) printf("%d ", 1); return puts("") & 0; } for (i = 2 * n - 1; i >= n; --i) { if (a[i] != 0 && a[i] > a[i - 1]) break; } k = i; b[k] = a[k]; for (i = 1; i < n; ++i) { b[k - i] = a[k - i]; if (a[k - i - 1] >= b[k - i]) { int mul = (a[k - i - 1] - b[k - i] + b[k - i + 1]) / b[k - i + 1]; b[k - i] += b[k - i + 1] * mul; } } for (i = 0; i < n; ++i) { if (b[i] != -1) break; b[i] = b[n + i]; } puts("YES"); for (i = 0; i < n; ++i) printf("%lld ", b[i]); puts(""); return 0; }
8
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c *x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug &operator<<(const c &) { return *this; } }; long long __pow(long long x, long long y, long long M) { long long res = 1; while (y > 0) { if (y & 1) res = (res * x) % M; y >>= 1; x = (x * x) % M; } return res % M; } double __pow(long long x, long long y) { double res = 1; while (y > 0) { if (y & 1) res = (res * x); y >>= 1; x = (x * x); } return res; } long long mi(long long x, long long M) { return __pow(x, M - 2, M); } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long add(vector<long long> a, long long M) { long long res = 0; for (auto x : a) res = (res + x) % M; return res; } long long mul(vector<long long> a, long long M) { long long res = 1; for (auto x : a) res = (res * x) % M; return res; } long long sub(long long a, long long b, long long M) { return (a - b + M) % M; } long long div(long long a, long long b, long long M) { return (a * mi(b, M)) % M; } void umax(long long &a, long long b) { a = max(a, b); } void umin(long long &a, long long b) { a = min(a, b); } const long long N = 2500 + 5, inf = 1e18, M = 1e9 + 7; long long Test, n, m, k, a[N][2], dp[N][4]; bool ok = 1; long long get(long long x, long long y) { if (x > 0 && y >= 0) return 1; if (x <= 0 && y > 0) return 2; if (x < 0 && y <= 0) return 3; return 4; } bool check(long long x1, long long y1, long long x2, long long y2) { return y2 * x1 - y1 * x2 < 0; } long long nC3(long long n) { if (n < 3) return 0; return (n * (n - 1) * (n - 2)) / 6; } long long nC4(long long n) { if (n < 4) return 0; return (n * (n - 1) * (n - 2) * (n - 3)) / 24; } void test_case() { cin >> n; long long ans = 0; for (long long i = 1; i <= n; i++) cin >> a[i][0] >> a[i][1]; for (long long i = 1; i <= n; i++) { vector<long long> rem; for (long long j = 1; j <= n; j++) { if (i == j) continue; rem.push_back(j); } long long cnt = 0; sort((rem).begin(), (rem).end(), [&](long long x, long long y) { long long q1 = get(a[x][0] - a[i][0], a[x][1] - a[i][1]), q2 = get(a[y][0] - a[i][0], a[y][1] - a[i][1]); if (q1 == q2) return (a[x][1] - a[i][1]) * (a[y][0] - a[i][0]) < (a[y][1] - a[i][1]) * (a[x][0] - a[i][0]); return q1 < q2; }); long long _cnt = 0; for (long long j = 0, l = 1; j < n - 1; j++) { if (l == j) l = (l + 1) % (n - 1); while (l != j && !check(a[rem[j]][0] - a[i][0], a[rem[j]][1] - a[i][1], a[rem[l]][0] - a[i][0], a[rem[l]][1] - a[i][1])) l = (l + 1) % (n - 1), _cnt++; cnt += nC3(_cnt) + nC3(n - 2 - _cnt); _cnt = max(0ll, _cnt - 1); } ans += nC4(n - 1) - cnt / 2; } cout << ans << '\n'; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); Test = 1; for (long long t = 1; t <= Test; t++) { test_case(); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = (1 << 18); long long dp[18][1 << 18][2][2], a[maxn], w[maxn]; void gmax(long long &a, long long b) { if (a < b) a = b; } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = (1), iend = (k); i <= iend; i++) scanf("%d", &a[i]), w[--a[i]] = 1; memset(dp, -0x3f, sizeof(dp)); for (int i = (0), iend = ((1 << (n - 1)) - 1); i <= iend; i++) { dp[1][i][w[i << 1]][w[i << 1 | 1]] = w[i << 1] | w[i << 1 | 1]; dp[1][i][w[i << 1 | 1]][w[i << 1]] = w[i << 1] | w[i << 1 | 1]; } for (int i = (2), iend = (n); i <= iend; i++) { int s = (1 << (n - i)) - 1; for (int sta = (0), staend = (s); sta <= staend; sta++) for (int l1 = (0), l1end = (1); l1 <= l1end; l1++) for (int l2 = (0), l2end = (1); l2 <= l2end; l2++) for (int r1 = (0), r1end = (1); r1 <= r1end; r1++) for (int r2 = (0), r2end = (1); r2 <= r2end; r2++) { long long t = dp[i - 1][sta << 1][l1][l2] + dp[i - 1][sta << 1 | 1][r1][r2]; gmax(dp[i][sta][l1 | r1][l2 | r2], t + 2 * (l2 | r2) + (l1 | r1)); if (l1 | r1) gmax(dp[i][sta][(l1 + r1) - 1][1], t + (l2 | r2) + 1 + 1); } } long long ans = 0; for (int l1 = (0), l1end = (1); l1 <= l1end; l1++) for (int l2 = (0), l2end = (1); l2 <= l2end; l2++) { gmax(ans, dp[n][0][l1][l2] + (l1 | l2)); } cout << ans; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int nr(0); string s; bool pirmas = false; cin >> s; if (s.length() < 4) { cout << "NO"; return 0; } for (int i = 0; i < s.length() - 1; i++) { if (s[i] == 'A' && s[i + 1] == 'B') { pirmas = true; nr = i + 2; break; } } if (pirmas == true && nr + 1 <= s.length()) for (int i = nr; i < s.length() - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'A') { cout << "YES"; return 0; } } nr = 0; pirmas = false; for (int i = 0; i < s.length() - 1; i++) { if (s[i] == 'B' && s[i + 1] == 'A') { pirmas = true; nr = i + 2; break; } } if (pirmas == true && nr + 1 <= s.length()) for (int i = nr; i < s.length() - 1; i++) { if (s[i] == 'A' && s[i + 1] == 'B') { cout << "YES"; return 0; } } cout << "NO"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (n == 1 && m == 1) { printf("1.0000000000000000"); return 0; } double res = 1.0 / n + double((n - 1) * (m - 1)) / n / (n * m - 1); printf("%.12f", res); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int times; cin >> times; while (times--) { string s, t, p; cin >> s >> t >> p; int hashs[26] = {0}, hasht[26] = {0}, hashp[26] = {0}; for (int i = 0; i < s.length(); i++) { hashs[(int)s[i] - 97]++; } for (int i = 0; i < t.length(); i++) { hasht[(int)t[i] - 97]++; } for (int i = 0; i < p.length(); i++) { hashp[(int)p[i] - 97]++; } int i; for (i = 0; i < 26; i++) { hasht[i] -= hashs[i]; if (hasht[i] < 0) { break; } } if (i != 26) { cout << "NO\n"; } else { for (i = 0; i < 26; i++) { if (hashp[i] < hasht[i]) { break; } } if (i != 26) { cout << "NO\n"; } else { int j, i, ind = 0; for (i = 0; i < s.length(); i++) { for (j = ind; j < t.length(); j++) { if (t[j] == s[i]) { ind = j + 1; break; } } if (j == t.length()) break; } if (i == s.length()) { cout << "YES\n"; } else { cout << "NO\n"; } } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int pt[1010]; int find(int x) { return pt[x] == x ? x : pt[x] = find(pt[x]); } int main() { int n, m; int x, y; int flag = 1; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) pt[i] = i; for (int i = 0; i < m; i++) { scanf("%d%d", &x, &y); int fx = find(x); int fy = find(y); if (fx == fy) flag = 0; else pt[fx] = fy; } if (flag && (m == n - 1)) printf("yes"); else printf("no"); return 0; }
2
#include <bits/stdc++.h> using namespace std; void solve() { int i, j, k, n, x, y, z; string s; cin >> s; n = s.size(); for (i = 1; i < n + 1; i++) { x = ((int)s[i - 1] - 48); if (x % 8 == 0) { cout << "YES\n" << x; return; } for (j = i + 1; j < n + 1; j++) { y = x * 10 + ((int)s[j - 1] - 48); if (y % 8 == 0) { cout << "YES\n" << y; return; } for (k = j + 1; k < n + 1; k++) { z = y * 10 + ((int)s[k - 1] - 48); if (z % 8 == 0) { cout << "YES\n" << z; return; } } } } cout << "NO"; } int main() { auto start = chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(false); int t = 1; while (t--) solve(); auto stop = chrono::high_resolution_clock::now(); auto durn = chrono::duration_cast<chrono::microseconds>(stop - start); }
3
#include <bits/stdc++.h> double pi = acos(-1); using namespace std; int data[100010]; int Times[100010]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> data[i]; Times[0] = 0; stack<int> killers; killers.push(0); for (int i = 1; i < n; i++) { int t = 0; while (killers.size() && data[killers.top()] <= data[i]) { t = max(Times[killers.top()], t); killers.pop(); } if (killers.size()) Times[i] = t + 1; else Times[i] = -1; killers.push(i); } int ans = -1; for (int i = 0; i < n; i++) ans = max(ans, Times[i]); cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; const int N = 100000 + 10; const long long MOD = 1000000000 + 7; const long long INF = 1000000010; const long long LOG = 25; int n, a[N], l[N], r[N], M[N], cnt[N]; long long sm; vector<int> num, Q[N], divi[N]; long long nCr(long long x) { return x * (x - 1) / 2; } void add(int x) { for (auto u : divi[x]) { sm -= nCr(cnt[u]) * M[u]; cnt[u]++; sm += nCr(cnt[u]) * M[u]; } } void minu(int x) { for (auto u : divi[x]) { sm -= nCr(cnt[u]) * M[u]; cnt[u]--; sm += nCr(cnt[u]) * M[u]; } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); M[1] = 1; for (int i = 1; i < N; i++) { for (int j = i * 2; j < N; j += i) M[j] -= M[i]; } cin >> n; for (int i = 1; i < N; i++) { for (int j = i; j < N; j += i) divi[j].push_back(i); } for (int i = 1; i <= n; i++) { cin >> a[i]; for (auto u : divi[a[i]]) num.push_back(u); } sort(num.begin(), num.end()); num.resize(unique(num.begin(), num.end()) - num.begin()); for (int i = 0; i < num.size(); i++) l[i] = 0, r[i] = num.size(); for (int lg = 0; lg < 17; lg++) { for (int i = 0; i <= num.size(); i++) Q[i].clear(); for (int i = 0; i < num.size(); i++) { if (r[i] - l[i] == 1) continue; Q[(l[i] + r[i]) >> 1].push_back(i); } memset(cnt, 0, sizeof cnt); sm = 0; for (int i = num.size() - 1; i >= 0; i--) { add(num[i]); long long x = sm; for (auto u : Q[i]) { add(num[u]); long long y = sm; minu(num[u]); if (y > x) l[u] = i; else r[u] = i; } } } long long ans = 0; for (int i = 0; i < num.size(); i++) { ans = max(ans, num[i] * 1ll * num[l[i]]); } cout << ans; return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], b[n]; int c[2001] = {0}; int h[2001] = {0}; for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; c[a[i]]++; h[b[i]]++; } int k = 0; for (int i = 0; i < n; i++) { if (a[i] == b[i] && h[b[i]] == 1) c[a[i]]--; } for (int i = 1; i < 2001; i++) { if (h[i]) { k += c[i]; } } if (k < n) cout << n - k; else cout << 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = int(2.1e5); vector<int> adj[MAXN]; vector<int> ans; int N, v[MAXN], g[MAXN]; void dfs(int x, int f, int c0, int c1) { int nc0, nc1; if (v[x] ^ (c1 % 2) == g[x]) { nc0 = c1, nc1 = c0; } else { ans.push_back(x); nc0 = c1 + 1, nc1 = c0; } for (int i = 0, j = adj[x].size(); i < j; ++i) { int y = adj[x][i]; if (y == f) continue; dfs(y, x, nc0, nc1); } } int main() { while (~scanf("%d", &N)) { ans.clear(); for (int i = 1; i <= N; ++i) adj[i].clear(); for (int i = 1; i < N; ++i) { int a, b; scanf("%d %d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= N; ++i) scanf("%d", v + i); for (int i = 1; i <= N; ++i) scanf("%d", g + i); dfs(1, -1, 0, 0); printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d\n", ans[i]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename T> void DEBUG(string label, T value) { cerr << "[" << label << " = " << value << "]\n"; } void solve() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; } int mid = n / 2 - 1; if (mid <= 2) { cout << "0 0 0" << endl; return; } int up = mid; while (up >= 0 && arr[mid + 1] == arr[up]) { up--; } int j = 1; while (j <= up && arr[j] == arr[0]) { j++; } if (j == up + 1) { cout << "0 0 0" << endl; return; } int gold = j; int i = j; int silver = 0; while (i <= up) { j = i + 1; while (j <= up && arr[j] == arr[i]) { j++; } int cnt = j - i; silver += cnt; i = j; if (silver > gold) break; } if (silver <= gold || i == up + 1 || up - i + 1 <= gold) { cout << "0 0 0" << endl; return; } cout << gold << " " << silver << " " << up - i + 1 << endl; } int main(int argv, char **argc) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; int n; while (t--) { cin >> n; int a[n + 5]; int minv = 2e9; int sum = 0; int ans = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] % 2 == 0) { sum++; } else { ans++; } } a[n + 1] = 1e9; sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { if (a[i + 1] - a[i] == 1) { minv = 1; } } if (sum % 2 == 0 && ans % 2 == 0) { cout << "YES" << endl; } else { if (minv == 1) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { int x = max(a, b); int y = min(a, b); if (!y) return x; else return gcd(y, x % y); } int main() { int num, temp, mx = -1; cin >> num; int tk = 0; for (int i = 0; (i) < (num); i++) { cin >> temp; tk = gcd(tk, temp); mx = max(mx, temp); } mx /= tk; int res = mx - num; if (res % 2) cout << "Alice\n"; else cout << "Bob\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; double dp[2005][2005]; int main() { int n, t; double p; while (cin >> n >> p >> t) { dp[0][0] = 1.0; for (int i = 1; i <= t; i++) { for (int j = 0; j <= i; j++) { if (j == 0) dp[i][j] = dp[i - 1][j] * (1.0 - p); else if (j == n) dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j]; else dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j] * (1.0 - p); } } double ans = 0.0; for (int j = 1; j <= n; j++) ans += (j * 1.0 * dp[t][j]); printf("%.6lf\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, k; int a[N]; vector<int> g[N]; int ans = 0, cnt = 0; int dp[N], big[N], bad[N]; void dfs(int u, int p, int x) { big[u] = 1, bad[u] = 0; if (a[u] < x) dp[u] = 0, bad[u] = 1; else dp[u] = 1; for (auto v : g[u]) { if (v == p) continue; dfs(v, u, x); bad[u] += bad[v]; big[u] += big[v]; } if (dp[u]) { int mx1 = 0, mx2 = 0; for (auto v : g[u]) { if (v == p) continue; if (dp[v] == big[v]) dp[u] += dp[v]; else { if (dp[v] > mx1) { mx2 = mx1, mx1 = dp[v]; } else if (dp[v] > mx2) mx2 = dp[v]; } } dp[u] += mx1; int now = dp[u] + mx2; if (bad[u] == cnt) now += n - big[u]; ans = max(ans, now); } } bool check(int x) { cnt = 0, ans = 0; for (int i = (1); i < (n + 1); i++) { if (a[i] < x) cnt++; } dfs(1, 0, x); return ans >= k; } int main() { cin >> n >> k; for (int i = (1); i < (n + 1); i++) cin >> a[i]; for (int i = (1); i < (n); i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } int s = 0, L = 1, R = 1e7; while (L <= R) { int mid = (L + R) / 2; if (check(mid)) { s = mid; L = mid + 1; } else R = mid - 1; } cout << s << "\n"; return 0; }
9
#include <bits/stdc++.h> class G2InversionsProblem { public: int n, k; int arr[31]; double dp[31][31][201] = {0}; void solve(std::istream& in, std::ostream& out) { out.precision(10); in >> n >> k; for (int i = 0; i < n; i++) { in >> arr[i]; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) dp[i][j][0] = 1.0; } } for (int t = 1; t <= k; t++) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int x = 0; x < n; x++) { for (int y = x; y < n; y++) { if ((i < x && y < j) || (y < i) || (x > j)) dp[i][j][t] += dp[i][j][t - 1] * 2.0 / n / (n + 1); else if (x <= i && y < j) dp[i][j][t] += dp[x + y - i][j][t - 1] * 2.0 / n / (n + 1); else if (i < x && j <= y) dp[i][j][t] += dp[i][x + y - j][t - 1] * 2.0 / n / (n + 1); else dp[i][j][t] += (1.0 - dp[x + y - j][x + y - i][t - 1]) * 2.0 / n / (n + 1); } } } } } double ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { ans += dp[i][j][k]; } } out << ans; } }; int main() { G2InversionsProblem solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int i, j, k, l, n, m, a[10000], N[30][30], P[30], p, mn, t; queue<pair<int, int> > q; int f, s; vector<int> v[10000]; int main() { cin >> t; while (t--) { cin >> n >> p; for (i = 1; i <= n; i++) { v[i].clear(); P[i] = 0; for (j = 1; j <= n; j++) { N[i][j] = 0; } } for (i = 1; i <= 2 * n + p; i++) { mn = 10000; for (j = 1; j <= n; j++) { for (k = 1; k <= n; k++) { if (j == k || N[j][k] == 1) continue; if (P[j] + P[k] < mn) { mn = P[j] + P[k]; f = j; s = k; } } } v[f].push_back(s); v[s].push_back(f); N[f][s] = 1; N[s][f] = 1; P[s]++; P[f]++; } for (i = 1; i <= n; i++) { for (j = 0; j < v[i].size(); j++) { if (i < v[i][j]) cout << i << " " << v[i][j] << endl; } } } }
3
#include <bits/stdc++.h> using namespace std; int n, cnt[8]; long long x; void checkmax(int &x, int y) { if (x < y) { x = y; } } void modify(int x) { switch (x) { case 4: cnt[2] += 2; break; case 6: cnt[2]++; cnt[3]++; break; case 8: cnt[2] += 3; break; case 9: cnt[3] += 2; break; default: cnt[x]++; break; } } void del(int x, int y) { switch (x) { case 7: cnt[7] -= y; cnt[5] -= y; cnt[3] -= 2 * y; cnt[2] -= 4 * y; break; case 5: cnt[5] -= y; cnt[3] -= y; cnt[2] -= 3 * y; break; case 3: cnt[3] -= y; cnt[2] -= y; break; default: cnt[2] -= y; break; } } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> x; while (x) { for (int i = x % 10; i >= 2; i--) { modify(i); } x /= 10; } for (int i = 1; i <= cnt[7]; i++) { cout << 7; } del(7, cnt[7]); for (int i = 1; i <= cnt[5]; i++) { cout << 5; } del(5, cnt[5]); for (int i = 1; i <= cnt[3]; i++) { cout << 3; } del(3, cnt[3]); for (int i = 1; i <= cnt[2]; i++) { cout << 2; } del(2, cnt[2]); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = (int)1e9 + 7; const int inf = (1 << 30) - 1; const int N = 505; long long binpow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long inv(long long x) { return binpow(x, mod - 2); } struct num { long long a, b; num() : a(0), b(0) {} num(long long x) : a(x % mod), b(0) {} num(long long a, long long b) : a(a % mod), b(b % mod) {} bool const operator==(const num &n) const { return a == n.a && b == n.b; } num const operator+(const num &n) const { return num(a + n.a, b + n.b); } num const operator-(const num &n) const { return num(a - n.a + mod, b - n.b + mod); } num const operator*(const num &n) const { return num(a * n.a + 5 * b * n.b, a * n.b + b * n.a); } num const operator*(long long k) const { return num(a * k, b * k); } num const operator/(long long k) const { k = inv(k); return num(a * k, b * k); } } powA[N], powB[N], powX[N]; const num E = num(1, 0); const num A = num(1, 1) / 2; const num B = num(1, mod - 1) / 2; const num X = num(0, inv(5)); int k; long long s[N][N], c[N][N], factK; void preprocess() { s[0][0] = c[0][0] = 1; for (int i = 1; i < N; i++) { s[i][0] = (mod - i + 1) * s[i - 1][0] % mod; c[i][0] = 1; for (int j = 1; j <= i; j++) { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; s[i][j] = (s[i - 1][j - 1] + mod + (mod - i + 1) * s[i - 1][j] % mod) % mod; } } factK = 1; powA[0] = powB[0] = powX[0] = E; for (int i = 1; i <= k; i++) { factK = factK * i % mod; powA[i] = powA[i - 1] * A; powB[i] = powB[i - 1] * B; powX[i] = powX[i - 1] * X; } } pair<num, num> get_progression(num C, long long n) { if (n == 0) return make_pair(E, E); if (n == 1) return make_pair(E + C, C); pair<num, num> p = get_progression(C, n / 2); if (n & 1) return make_pair(p.first * (E + C * p.second), p.second * p.second * C); return make_pair(p.first - p.second + p.second * p.first, p.second * p.second); } int get(long long n) { long long ans = 0; for (int j = 1; j <= k; j++) { for (int t = 0; t <= j; t++) { long long coef = s[k][j] * c[j][t] % mod; if ((j - t) & 1) coef = (mod - coef) % mod; num num_part = get_progression(powA[t] * powB[j - t], n).first; num_part = num_part * powX[j]; ans += coef * num_part.a % mod; ans %= mod; } } return ans * inv(factK) % mod; } int main() { long long l, r; cin >> k >> l >> r; preprocess(); long long ans = get(r + 2) + mod - get(l + 1); ans %= mod; printf("%d\n", (int)ans); return 0; }
10
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; const int N = 1e2 + 10; const long long mod = 1e9 + 7; const long long mod2 = 998244353; const long long inf = 8e18; const int LOG = 22; long long pw(long long a, long long b, long long M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); } long long Ans[N], tmp[N][N][N], dp[N][N][N], sub[N], tot[N], first[N], I[N], n; vector<int> G[N]; void dfs(int v, int P = 0) { sub[v] = 1; dp[v][1][1] = 1; for (auto u : G[v]) { if (u == P) continue; dfs(u, v); memset(tmp, 0, sizeof tmp); for (int i = 1; i <= sub[v]; i++) { for (int j = 1; j <= sub[v]; j++) { for (int i2 = 1; i2 <= sub[u]; i2++) { for (int j2 = 1; j2 <= sub[u]; j2++) { tmp[v][i + i2][j + j2 - 1] = (tmp[v][i + i2][j + j2 - 1] + dp[v][i][j] * dp[u][i2][j2]) % mod; tmp[v][i][j + j2] = (tmp[v][i][j + j2] + (dp[v][i][j] * dp[u][i2][j2] % mod) * i2) % mod; } } } } memcpy(dp[v], tmp[v], sizeof tmp[v]); sub[v] += sub[u]; } } long long E(long long r, long long n2) { if (r > n2 || r < 0) return 0ll; return ((first[n2] * I[r] % mod) * I[n2 - r]) % mod; } int main() { first[0] = I[0] = 1; for (int i = 1; i < N; i++) { first[i] = (first[i - 1] * i) % mod; I[i] = pw(first[i], mod - 2, mod); } scanf("%lld", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); G[a].push_back(b); G[b].push_back(a); } dfs(1, 0); for (int i = 1; i < N; i++) { for (int j = 1; j < N; j++) { Ans[i] = (Ans[i] + dp[1][j][i] * j) % mod; } } tot[n - 1] = 1; for (int i = n - 2; ~i; i--) { tot[i] = (pw(n, n - i - 2, mod) * Ans[n - i]) % mod; } for (int i = n - 1; ~i; i--) { for (int j = i + 1; j < n; j++) { tot[i] = (tot[i] - (E(i, j) * tot[j] % mod) + 2ll * mod) % mod; } } for (int i = 0; i < n; i++) { printf("%lld ", tot[i]); } return 0; }
9
#include <bits/stdc++.h> using namespace std; long long n, m; long long arr[100005], ans[100005]; int main() { long long i, x, sum; scanf("%lld %lld", &n, &m); sum = 0; for (i = 1; i <= m; i++) { scanf("%lld", &arr[i]); sum += arr[i]; } if (sum < n) return !printf("-1\n"); x = 0; for (i = 1; i <= m; i++) { ans[i] = x + 1; if (x + arr[i] > n) return !printf("-1\n"); sum -= arr[i]; x = max(x + 1, n - sum); } for (i = 1; i <= m; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; int m[30][30][5000]; int main() { string str; cin >> str; vector<int> fq(200); for (auto i : str) fq[i]++; int N = str.size(); int tt = 0; vector<vector<int>> ch(200, vector<int>(5000)); for (int x = 0; x < N; x++) { for (int y = 0; y < N; y++) { m[str[0] - 'a'][str[y] - 'a'][y]++; } rotate(str.begin(), str.begin() + 1, str.end()); } for (int x = 0; x < N; x++) { for (int y = 1; y < N; y++) { int fqq = m[str[0] - 'a'][str[y] - 'a'][y]; if (fqq == 1) { ch[str[0]][y]++; } } rotate(str.begin(), str.begin() + 1, str.end()); } for (int x = 'a'; x <= 'z'; x++) { tt += *max_element(ch[x].begin(), ch[x].end()); } cout << fixed << setprecision(18) << (tt / (long double)N); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int i, n; double k, time, sum = 0; cin >> n >> k; double a[n], b[n]; for (i = 0; i <= n - 1; i++) { cin >> a[i] >> b[i]; if (i != 0) sum = sum + sqrt(pow(a[i] - a[i - 1], 2.0) + pow(b[i] - b[i - 1], 2.0)); } time = sum / (50 * 1.0); time = time * k; cout << fixed; cout << setprecision(9) << time << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const ll mod = 1e9 + 7; const ll inf = 1e18; void vprint(vector<ll> A) { ll L = A.size(); for (ll i = (0); i < (L); ++i) { if (i) cout << ' '; cout << A[i]; } cout << endl; } bool is_palindrome(string &s) { ll L = s.size(); for (ll i = (0); i < (L / 2); ++i) { if (s[i] != s[L - 1 - i]) return false; } return true; } int main() { cin.tie(0); ios::sync_with_stdio(false); string s; cin >> s; ll N = s.size(); map<char, ll> mp; for (char c : s) { mp[c]++; } if (mp.size() == 1) { cout << ("Impossible") << endl; return 0; } if (N % 2 == 1) { for (auto pa : mp) { ll count = pa.second; if (count == N - 1) { cout << ("Impossible") << endl; return 0; } } } string t = s; for (ll i = (0); i < (N); ++i) { t = t.substr(1) + t[0]; if (is_palindrome(t) && t != s) { cout << (1) << endl; return 0; } } cout << (2) << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int inf = INT_MAX; int n; long long a, b; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> a >> b; int x, y; char tp; int val[8]; for (int i = 0; i < (8); i++) val[i] = inf; char type[8]; for (int i = 0; i < (8); i++) type[i] = 'z'; for (int i = 0; i < (n); i++) { cin >> tp >> x >> y; if (x == a && y > b && val[0] > y - b) val[0] = y - b, type[0] = tp; if (x - y == a - b && x > a && val[1] > x - a) val[1] = x - a, type[1] = tp; if (y == b && x > a && val[2] > x - a) val[2] = x - a, type[2] = tp; if (x + y == a + b && x > a && val[3] > x - a) val[3] = x - a, type[3] = tp; if (x == a && y < b && val[4] > b - y) val[4] = b - y, type[4] = tp; if (x - y == a - b && a > x && val[5] > a - x) val[5] = a - x, type[5] = tp; if (y == b && a > x && val[6] > a - x) val[6] = a - x, type[6] = tp; if (x + y == a + b && a > x && val[7] > a - x) val[7] = a - x, type[7] = tp; } bool flag = false; for (int i = 0; i < (8); i++) { if (i % 2 != 0 && (type[i] == 'Q' || type[i] == 'B')) flag = true; if (i % 2 == 0 && (type[i] == 'Q' || type[i] == 'R')) flag = true; } if (flag) cout << "YES" << "\n"; else cout << "NO" << "\n"; return 0; }
4
#include <bits/stdc++.h> int main() { int n, m, songs[100001]; std::cin >> n >> m; songs[0] = 0; for (unsigned i = 0; i != n; ++i) { int c, t; std::cin >> c >> t; songs[i + 1] = songs[i] + (c * t); } int song = 1; while (m--) { int moment; std::cin >> moment; while (moment > songs[song]) ++song; std::cout << song << std::endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; priority_queue<int, vector<int>, greater<int> > q; int main() { int n, a; long long s = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a); if (!q.empty() && q.top() < a) { s += (a - q.top()); q.pop(); q.push(a); q.push(a); } else q.push(a); } printf("%lld\n", s); return 0; }
8
#include <bits/stdc++.h> using namespace std; int n; long long re; long long k; int g[110000]; int check(int mid) { long long ans = 0; for (int i = 0; i < n; i++) ans += (long long)min(mid, g[i]); if (ans > k) return 0; else { return 1; } } int main() { int r = 0, l = 0; long long sum = 0; scanf("%d%I64d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &g[i]); r = max(r, g[i]); sum += (long long)g[i]; } if (sum < k) { printf("-1\n"); return 0; } if (sum == k) { return 0; } while (l <= r) { int mid = (l + r) / 2; if (check(mid)) l = mid + 1; else r = mid - 1; } sum = 0; for (int i = 0; i < n; i++) sum += (long long)min(r, g[i]); re = (k - sum); int i; for (i = 0; i < n && re; i++) { if ((g[i] > r)) { if (re) { g[i]--; re--; } } } for (int j = 0; j < n; j++) { if (g[(i + j) % n] > r) printf("%d ", (i + j) % n + 1); } printf("\n"); }
5
#include <bits/stdc++.h> using namespace std; class Solution { public: long long minPerimeter(long long a, long long b) { vector<pair<long long, long long>> reca; for (long long i = 1ll; i * i <= a; i += 1ll) if (a % i == 0ll) reca.push_back(make_pair(i, a / i)); vector<pair<long long, long long>> recb; for (long long i = 1ll; i * i <= b; i += 1ll) if (b % i == 0ll) recb.push_back(make_pair(i, b / i)); long long area = a + b; long long res = (1ll + area) * 2ll; for (long long i = 2ll; i * i <= area; i += 1ll) if (area % i == 0 && (ok(reca, i, area / i) || ok(recb, i, area / i))) res = (i + area / i) * 2ll; return res; } private: bool ok(const vector<pair<long long, long long>>& rec, long long a, long long b) { assert(a <= b); int l = -1, r = rec.size() - 1; while (l < r) { int mid = l + (r - l + 1) / 2; if (rec[mid].first <= a) l = mid; else r = mid - 1; } assert(l == r); if (l == -1) return false; return rec[l].second <= b; } }; int main() { long long a, b; cin >> a >> b; cout << Solution().minPerimeter(a, b) << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long h[1100], v[1100], mas[1100][1100]; int n, m; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%I64d", &mas[i][j]); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) h[i] += mas[i][j]; } for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) v[i] += mas[j][i]; int x, y; long long ans1 = (long long)1000000000 * 1000000000, ans2 = ans1; for (int i = 0; i <= n; i++) { long long t = 0; for (int j = 1; j <= n; j++) { if (i >= j) { t += (long long)h[j] * ((4 * (i - j) + 2) * (4 * (i - j) + 2)); } else { t += (long long)h[j] * (4 * (j - i - 1) + 2) * (4 * (j - i - 1) + 2); } } if (t < ans1) { ans1 = t; x = i; } } for (int i = 0; i <= m; i++) { long long t = 0; for (int j = 1; j <= m; j++) { if (i >= j) { t += (long long)v[j] * ((4 * (i - j) + 2) * (4 * (i - j) + 2)); } else { t += (long long)v[j] * (4 * (j - i - 1) + 2) * (4 * (j - i - 1) + 2); } } if (t < ans2) { ans2 = t; y = i; } } cout << ans1 + ans2 << endl; cout << x << " " << y << endl; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 4; int a[maxn]; int n, k, c; int cur, ans; int main() { cin >> n >> k; cin >> c; for (int i = 0; i < c; i++) cin >> a[i]; int i = 0; while (cur <= n && i < c) { if (a[i] - cur > k) { cur += k; ans++; } else { cur = a[i]; ans++; i++; } } while (1) { cur += k; if (cur > n) break; else ans++; } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1003; vector<int> g[maxn]; int n; bool ans = 1; bool dfs(int nod) { int aa = 0; for (auto x : g[nod]) { aa += dfs(x); } if (aa > 0 && aa < 3) ans = 0; if (aa == 0 && g[nod].size()) ans = 0; return (aa == 0); } int main() { if (0) { freopen("a.in", "r", stdin); freopen("a.out", "w", stdout); } cin >> n; for (int i = 1; i <= n - 1; i++) { int x; cin >> x; g[x].push_back(i + 1); } dfs(1); cout << ((ans == 1) ? "Yes" : "No"); return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, d, ans, s = 1, low; int main() { cin >> n >> d; n++; low = n - d; ans = n; while (n >= low) { while (n % (10 * s)) n -= s; if (n >= low) ans = n; s *= 10; } cout << ans - 1 << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const ll MOD = 1000000007; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } ll binpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b >>= 1; } return res; } ll modInv(ll a) { return binpow(a, MOD - 2); } const double PI = acos(-1); const double eps = -1e6; const int INF = 0x3f3f3f3f; const int NINF = 0xc0c0c0c0; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const ll NINFLL = 0xc0c0c0c0c0c0c0c0; const int mxN = 30001; int par[mxN][16]; int depth[mxN]; vector<int> spe[201]; int val[201]; vector<int> hang[mxN]; vector<pair<int, vector<pair<int, int>>>> rst; vector<pair<int, int>> res; int cnt[mxN]; bool flag[mxN]; int n, k; void killthyself() { cout << "-1\n"; exit(0); } void upd(int a, int p) { if (flag[a]) return; flag[a] = true; par[a][0] = p; depth[a] = depth[p] + 1; for (int lg = (1); lg < (16); ++lg) { par[a][lg] = par[par[a][lg - 1]][lg - 1]; } res.push_back({a, p}); } int walk(int a, int nh) { for (int i = (16) - 1; i >= (0); --i) { if (depth[par[a][i]] >= nh) { a = par[a][i]; } } return a; } int lca(int a, int b) { if (depth[a] < depth[b]) { b = walk(b, depth[a]); } else if (depth[a] > depth[b]) { a = walk(a, depth[b]); } if (a == b) return a; for (int i = (16) - 1; i >= (0); --i) { if (par[a][i] != par[b][i]) { a = par[a][i]; b = par[b][i]; } } return par[a][0]; } int dist(int a, int b) { return depth[a] + depth[b] - 2 * depth[lca(a, b)]; } int nthonpath(int first, int second, int d) { int imhavingfun = lca(first, second); if (depth[first] - depth[imhavingfun] >= d) { return walk(first, depth[first] - d); } else { d -= (depth[first] - depth[imhavingfun]); return walk(second, depth[imhavingfun] + d); } } int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cin.tie(0)->sync_with_stdio(0); cin >> n >> k; for (int i = (0); i < (n); ++i) { hang[i].push_back(i); } for (int i = (0); i < (k); ++i) { memset(cnt, 0, sizeof(cnt)); spe[i].resize(n); int mx = 0; for (int j = (0); j < (n); ++j) { cin >> spe[i][j]; if (spe[i][j] == 0) val[i] = j; mx = max(mx, spe[i][j]); cnt[spe[i][j]]++; } int mex = -1; while (cnt[mex + 1]) mex++; if (mex != mx) killthyself(); } int rt = val[0]; for (int i = (0); i < (16); ++i) { par[rt][i] = rt; } flag[rt] = 1; for (int i = (1); i < (k); ++i) { vector<pair<int, int>> chain; for (int j = (0); j < (n); ++j) { if (spe[i][j] + spe[0][j] == spe[0][val[i]]) chain.push_back({spe[0][j], j}); } sort(chain.begin(), chain.end()); int prv = -1; int tp = rt; for (int j = (0); j < (chain.size()); ++j) { if (chain[j].first != prv + 1) killthyself(); prv++; upd(chain[j].second, tp); tp = chain[j].second; } } for (int i = (0); i < (n); ++i) { if (!flag[i]) { rst.push_back({i, {}}); for (int j = (0); j < (k); ++j) { rst.back().second.push_back({spe[j][i], j}); } sort(rst.back().second.begin(), rst.back().second.end()); } } sort( rst.begin(), rst.end(), [&](pair<int, vector<pair<int, int>>> a, pair<int, vector<pair<int, int>>> b) { return a.second < b.second; }); for (int i = (0); i < (rst.size()); ++i) { int id = rst[i].first; vector<pair<int, int>> stuff = rst[i].second; int close = val[stuff[0].second]; pair<int, int> tohang = {0, close}; for (int j = (1); j < (stuff.size()); ++j) { int far = val[stuff[j].second]; int cc = spe[stuff[0].second][far]; int aa = stuff[0].first; int bb = stuff[j].first; if (aa + cc == bb) continue; int offtree = aa + bb - cc; if (offtree & 1) killthyself(); offtree >>= 1; tohang = max(tohang, {aa - offtree, nthonpath(close, far, aa - offtree)}); } tohang.first = stuff[0].first - tohang.first - 1; if (hang[tohang.second].size() > tohang.first + 1) { upd(id, hang[tohang.second][tohang.first]); } else { hang[tohang.second].push_back(id); if (hang[tohang.second].size() > tohang.first + 1) { upd(id, hang[tohang.second][tohang.first]); } else { killthyself(); } } } for (auto p : res) { cout << (p.first + 1) << " " << (p.second + 1) << "\n"; } return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { int t, z = 0, l, i = 0, len; int n, k, m = 0; scanf("%d", &n); long long a, pos[15], neg[15], ans, zero; memset(pos, 0, sizeof(pos)); memset(neg, 0, sizeof(neg)); ans = zero = 0; for (l = 0; l < n; l++) { scanf("%lld", &a); if (a > 0) pos[a]++; else if (a < 0) neg[-a]++; else zero++; } ans = (zero * (zero - 1)) / 2; for (l = 1; l < 11; l++) { ans += (pos[l] * neg[l]); } printf("%lld", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; int d[110][110][20]; bool t[110][110][20]; int v[110][110]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); ++k; char ch; scanf("%c", &ch); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { scanf("%c", &ch); v[i][j] = int(ch) - int('0'); }; scanf("%c", &ch); }; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { for (int o = 0; o < k; ++o) { d[i][j][o] = -100000; }; }; }; for (int i = 0; i < m; ++i) { d[n - 1][i][v[n - 1][i] % k] = v[n - 1][i]; }; for (int i = n - 2; i >= 0; --i) { for (int j = 0; j < m; ++j) { for (int o = 0; o < k; ++o) { if (j == 0 || (j != m - 1) && d[i + 1][j + 1][o] > d[i + 1][j - 1][o]) { d[i][j][(o + v[i][j]) % k] = d[i + 1][j + 1][o] + v[i][j]; t[i][j][(o + v[i][j]) % k] = true; } else { d[i][j][(o + v[i][j]) % k] = d[i + 1][j - 1][o] + v[i][j]; t[i][j][(o + v[i][j]) % k] = false; }; }; }; }; int ans = -1, pos; for (int i = 0; i < m; ++i) { if (d[0][i][0] > ans) { ans = d[0][i][0]; pos = i; }; }; printf("%d\n", ans); if (ans == -1) return 0; int ntt = 0; string ss(""); for (int i = 0; i < n - 1; ++i) { int tt = ntt; ntt = (d[i][pos][tt] - v[i][pos] + 1000 * k) % k; if (t[i][pos][tt]) { ss += 'L'; ++pos; } else { ss += 'R'; --pos; }; }; reverse(ss.begin(), ss.end()); printf("%d\n", pos + 1); cout << ss << endl; return 0; };
5
#include <bits/stdc++.h> using namespace std; bool cmp(pair<double, int> a, pair<double, int> b) { if (a.first == b.first) return a.second < b.second; return a.first > b.first; } int main() { long long n, t1, t2; double k; cin >> n >> t1 >> t2 >> k; vector<pair<double, long long> > ns; for (int i = 0; i < n; ++i) { long long a, b; cin >> a >> b; ns.push_back({max((a * t1 * (1 - k / 100) + b * t2), (b * t1 * (1 - k / 100) + a * t2)), i + 1}); } sort(ns.begin(), ns.end(), cmp); for (int j = 0; j < n; ++j) { cout << ns[j].second << " " << fixed << setprecision(2) << ns[j].first << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; } inline long long isqrt(long long k) { long long r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline long long icbrt(long long k) { long long r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(long double x, long double y) { return sign(x - y); } string strmax(string a, string b) { if (a == "") a = "-1"; if (b == "") b = "-1"; if (a == "-1") return b; if (b == "-1") return a; if (int((a).size()) > int((b).size())) return a; return b; } string work1(string s) { vector<int> v[3]; int sum = 0; for (int i = (0); i < (int((s).size())); i++) { int k = s[i] - '0'; if (i) { v[k % 3].push_back(i); } sum = (sum + k) % 3; } if (!sum) { return s; } if (int((v[sum]).size())) { string t = s; t.erase(t.begin() + v[sum][0]); return t; } sum = (sum + sum) % 3; if (int((v[sum]).size()) > 1) { string t = s; t.erase(t.begin() + v[sum][1]); t.erase(t.begin() + v[sum][0]); return t; } return "-1"; } string work2(string s, int step) { if (!int((s).size())) return "-1"; if (step == 5) return "-1"; s = s.substr(1); reverse((s).begin(), (s).end()); while (int((s).size()) > 1 && s[int((s).size()) - 1] == '0') s.pop_back(); reverse((s).begin(), (s).end()); return strmax(work1(s), work2(s, step + 1)); } void solve() { string s; cin >> s; string ans = work1(s); ans = strmax(ans, work2(s, 0)); for (int i = (0); i < (int((s).size())); i++) { if (s[i] == '0') { ans = strmax(ans, "0"); } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long MOD = (1e9 + 7); const int N = (5e5); map<long long, vector<pair<int, int> > > G; long long key[N + 2]; long long POT(long long x, long long y) { if (y == 0) return 1LL; if (y == 1) return x; long long ans = 1; if (y & 1) ans = x; long long val = POT(x, y / 2); ans *= val; ans %= MOD; ans *= val; ans %= MOD; return ans; } vector<long long> pa; int Find(int x) { return (pa[x] == x ? x : pa[x] = Find(pa[x])); } void Union(int x, int y) { int xx = Find(x); int yy = Find(y); pa[xx] = yy; } int main() { ios::sync_with_stdio(0); cin.tie(NULL); long long n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; i++) cin >> key[i]; int a, b; while (m--) { cin >> a >> b; long long p = key[a], q = key[b]; long long virus = p ^ q; G[virus].push_back(make_pair(a, b)); } map<long long, vector<pair<int, int> > >::iterator it; long long ans = 0; for (it = G.begin(); it != G.end(); it++) { vector<pair<int, int> > &v = (*it).second; map<int, int> cur; int nod = 0; for (int i = 0; i < v.size(); i++) { if (!cur.count(v[i].first)) cur[v[i].first] = nod++; if (!cur.count(v[i].second)) cur[v[i].second] = nod++; } pa.resize(nod); for (int i = 0; i < nod; i++) pa[i] = i; for (int i = 0; i < v.size(); i++) { Union(cur[v[i].first], cur[v[i].second]); } map<int, int>::iterator it; set<int> resp; for (it = cur.begin(); it != cur.end(); it++) resp.insert(Find((*it).second)); ans += POT(2, n - nod + (long long)resp.size()); ans %= MOD; ans += MOD; ans %= MOD; } ans += MOD; ans %= MOD; ans += (POT(2, n) * (POT(2, k) - G.size())) % MOD; ans %= MOD; ans += MOD; ans %= MOD; cout << ans << '\n'; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a[3], k = 0; for (int i = 0; i < 3; i++) { cin >> a[i]; k = k + a[i]; } int sum = k / 2; sort(a, a + 3); cout << min(a[0] + a[1], sum) << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; long long d[100][10001]; long long fact[101]; long long C[2][101]; long long power(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) (res *= a) %= MOD; (a *= a) %= MOD; n >>= 1; } return res; } int main() { ios::sync_with_stdio(0); int n, k; long long m; cin >> n >> m >> k; long long onest = (m + n - 1) / n; long long twost = m / n; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = (fact[i - 1] * i) % MOD; for (int i = 0; i <= n; i++) { long long now = (fact[n] * power((fact[i] * fact[n - i]) % MOD, MOD - 2)) % MOD; C[0][i] = power(now, onest); C[1][i] = power(now, twost); } for (int j = 0; j <= n; j++) d[0][j] = C[0][j]; for (int i = 1; i < n; i++) { for (int j = 0; j <= k; j++) { int border = min(n, j); int now = (m - i + n - 1) / n == twost; for (int x = 0; x <= border; x++) (d[i][j] += d[i - 1][j - x] * C[now][x]) %= MOD; } } cout << d[n - 1][k] << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int a[111], map[111]; while (~scanf("%d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4])) { sort(a, a + 5); a[5] = -1; int sum = 0; int j = 0; for (int i = 4; i >= 0; i--) { if (a[i] == a[i + 1]) { if (a[i] == a[i - 1]) { a[i] += a[i - 1] + a[i + 1]; a[i - 1] = a[i + 1] = 0; map[j++] = a[i]; a[i] = 0; } else { a[i] += a[i + 1]; a[i + 1] = 0; map[j++] = a[i]; a[i] = 0; } } } sort(map, map + j); int cou = 0; for (int i = 0; i < j - 1; i++) cou += map[i]; for (int i = 0; i < 5; i++) cou += a[i]; printf("%d\n", cou); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n, x = 0; string s; cin >> n; cin.get(); while (n--) { getline(cin, s); if (s[1] == '+') x++; else x--; } cout << x << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long bs = 300; long long le = 0, ri = -1; struct Query { long long l, r, k, qno; bool operator<(const Query &other) const { if (l / bs != other.l / bs) return l < other.l; return ((l / bs) & 1) ? (r < other.r) : (r > other.r); } }; const long long nax = 100005; long long timer = 0, tin[nax], tout[nax], col[nax], flat[nax], cnt[nax] = {0}, suff[nax] = {0}, ans[nax]; vector<long long> g[nax]; void dfs(long long u, long long p) { tin[u] = ++timer; flat[timer] = col[u]; for (auto &k : g[u]) if (k != p) dfs(k, u); tout[u] = timer; } void add(long long pos) { cnt[flat[pos]]++; suff[cnt[flat[pos]]]++; } void del(long long pos) { suff[cnt[flat[pos]]]--; cnt[flat[pos]]--; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m; cin >> n >> m; for (long long i = 1; i <= n; i++) cin >> col[i]; for (long long i = 0; i < n - 1; i++) { long long u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1, 1); vector<Query> qry; for (long long i = 0; i < m; i++) { long long u, k; cin >> u >> k; qry.push_back({tin[u], tout[u], k, i + 1}); } sort(qry.begin(), qry.end()); for (auto &q : qry) { while (le > q.l) le--, add(le); while (ri < q.r) ri++, add(ri); while (le < q.l) del(le), le++; while (ri > q.r) del(ri), ri--; ans[q.qno] = suff[q.k]; } for (long long i = 1; i <= m; i++) cout << ans[i] << '\n'; }
8
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double PI = acos(-1); const long long INFF = 1e18; const int INF = 1e9; const int mod = 1e9 + 7; const int MAX = 1e5 + 7; int a[61], b[61]; vector<int> na[MAX], nb[MAX]; bitset<61> ca, cb; vector<int> pt; long long cnt1[MAX], cnt2[MAX]; int main(void) { int n, m; scanf("%d %d", &n, &m); ; for (int i = (1); i < (n + 1); ++i) { scanf("%d", a + i); na[a[i] + 50000].push_back(i); } for (int i = (1); i < (m + 1); ++i) { scanf("%d", b + i); nb[b[i] + 50000].push_back(i); } for (int i = (1); i < (n + 1); ++i) for (int j = (1); j < (m + 1); ++j) pt.push_back(a[i] + b[j]); sort((pt).begin(), (pt).end()); pt.resize(unique((pt).begin(), (pt).end()) - pt.begin()); for (int j = 0; j < (((int)pt.size())); ++j) { for (int i = (1); i < (n + 1); ++i) { int v1 = pt[j] - a[i]; if (((int)nb[v1 + 50000].size())) cnt1[j] |= 1LL << i; for (int p : nb[v1 + 50000]) { cnt2[j] |= 1LL << p; } } } int ans = 0; for (int i = 0; i < (((int)pt.size())); ++i) for (int j = (i); j < (((int)pt.size())); ++j) { int v1 = __builtin_popcountll(cnt1[i] | cnt1[j]); int v2 = __builtin_popcountll(cnt2[i] | cnt2[j]); ans = max(ans, v1 + v2); } printf("%d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; int dp[51][510005][2], tot[51], pw[51]; struct tree { int vis[10]; int fail, dep; } AC[510005]; char s[1005], t[55]; int len, cnt, d, ans, res; void build(int x) { int now = 0; for (int i = x; i <= len; i++) { if (!AC[now].vis[s[i] - '0']) { AC[now].vis[s[i] - '0'] = ++cnt; AC[cnt].dep = AC[now].dep + 1; } now = AC[now].vis[s[i] - '0']; } } void getfail() { queue<int> Q; for (int i = 0; i < 10; i++) { if (AC[0].vis[i]) Q.push(AC[0].vis[i]); } while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = 0; i < 10; i++) { if (AC[u].vis[i]) { AC[AC[u].vis[i]].fail = AC[AC[u].fail].vis[i]; Q.push(AC[u].vis[i]); } else AC[u].vis[i] = AC[AC[u].fail].vis[i]; } } } void solve(int op) { res = 0; memset(dp, 0, sizeof(dp)); dp[0][0][1] = 1; pw[len] = 1; for (int i = len - 1; i >= 1; i--) pw[i] = 10ll * pw[i + 1] % 1000000007; for (int i = len - 1; i >= 1; i--) tot[i] = (tot[i + 1] + 1ll * pw[i + 1] * (t[i + 1] - '0') % 1000000007) % 1000000007; for (int i = 1; i <= len; i++) { for (int j = 0; j <= cnt; j++) { if (!dp[i - 1][j][0] && !dp[i - 1][j][1]) continue; for (int k = 0; k < 10; k++) (dp[i][AC[j].vis[k]][0] += dp[i - 1][j][0]) %= 1000000007; for (int k = 0; k < t[i] - '0'; k++) (dp[i][AC[j].vis[k]][0] += dp[i - 1][j][1]) %= 1000000007; (dp[i][AC[j].vis[t[i] - '0']][1] += dp[i - 1][j][1]) %= 1000000007; } for (int j = 0; j <= cnt; j++) { if (AC[j].dep >= d) { (res += 1ll * dp[i][j][0] * pw[i] % 1000000007) %= 1000000007; (res += 1ll * dp[i][j][1] * (tot[i] + 1) % 1000000007) %= 1000000007; dp[i][j][0] = dp[i][j][1] = 0; } } } (ans += 1ll * op * res % 1000000007) %= 1000000007; } int main() { scanf("%s", s + 1); len = strlen(s + 1); for (int i = 1; i <= len; i++) build(i); getfail(); scanf("%s", t + 1); len = strlen(t + 1); d = len / 2; t[len]--; int now = len; while (t[now] < '0') t[now] += 10, t[now - 1]--, now--; solve(1000000007 - 1); scanf("%s", t + 1); len = strlen(t + 1); solve(1); printf("%d\n", ans); return 0; }
12
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int N = 1e5 + 7; const int INF = 1e9 + 7; const int MOD = 1e9 + 7; long long Calc(long long x) { if (x > 1e7) return 1e18; return x * (x + 1) / 2 * 3; } int main() { long long n; cin >> n; if (n % 3 == 0) { long long l = 1, r = n, ans = 0; while (l <= r) { long long mid = l + r >> 1; if (Calc(mid * 3) <= n + mid * 3) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << endl; } else if (n % 3 == 2) { long long l = 1, r = n, ans = 0; while (l <= r) { long long mid = l + r >> 1; if (Calc(mid * 3 - 2) <= n + mid * 3 - 2) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << endl; } else { long long l = 1, r = n, ans = 0; while (l <= r) { long long mid = l + r >> 1; if (Calc(mid * 3 - 1) <= n + mid * 3 - 1) { ans = mid; l = mid + 1; } else r = mid - 1; } cout << ans << endl; } }
4
#include <bits/stdc++.h> using namespace std; int N, n, Q, M = 2, rnk[400010], lr[400010], sa[400010], ba[400010], he[400010], lg[400010], tmp, a0[200010], mn[400010][20], lm, sz, t[200010], c[400010], p1, p2, l1, r1, l2, r2, len; char s[200010]; void Sort() { for (int i = 1; i <= M; ++i) ba[i] = 0; for (int i = 1; i <= n; ++i) ba[rnk[i]]++; for (int i = 1; i <= M; ++i) ba[i] += ba[i - 1]; for (int i = n; i >= 1; --i) sa[ba[rnk[lr[i]]]--] = lr[i]; } void get_sa() { for (int i = 1; i <= n; ++i) rnk[i] = c[i], lr[i] = i; Sort(); for (int i = 1, tmp = 0; i < n; i <<= 1, M = tmp, tmp = 0) { for (int j = n; j >= n - i + 1; --j) lr[++tmp] = j; for (int j = 1; j <= n; ++j) sa[j] > i ? lr[++tmp] = sa[j] - i : 0; Sort(); for (int j = 1; j <= n; ++j) lr[j] = rnk[j]; rnk[sa[1]] = tmp = 1; for (int j = 2; j <= n; ++j) rnk[sa[j]] = (lr[sa[j]] == lr[sa[j - 1]] && lr[sa[j] + i] == lr[sa[j - 1] + i]) ? tmp : ++tmp; } } void get_height() { int tmp; for (int i = 1; i <= n; ++i) { tmp = sa[rnk[i] - 1], he[rnk[i]] = max(0, he[rnk[i - 1]] - 1); while (c[tmp + he[rnk[i]]] == c[i + he[rnk[i]]]) ++he[rnk[i]]; mn[rnk[i]][0] = he[rnk[i]]; } } int RMQ(const int& l, const int& r) { int tmp = lg[r - l + 1]; return min(mn[l][tmp], mn[r - (1 << tmp) + 1][tmp]); } int LCP(int x, int y) { if (x == y) return min(n - x + 1, n - y + 1); x = rnk[x], y = rnk[y]; if (x > y) swap(x, y); return RMQ(x + 1, y); } int Find1(const int& x) { int l = 1, r = sz + 1, mid; while (l < r) mid = l + r >> 1, a0[mid] >= x ? r = mid : l = mid + 1; return l; } int Find2(const int& x) { int l = 0, r = sz, mid; while (l < r) mid = l + r + 1 >> 1, a0[mid] <= x ? l = mid : r = mid - 1; return l; } int main() { scanf("%d%s", &N, s + 1); for (int i = 1; i <= N; ++i) { t[i] = (t[i - 1] ^ (s[i] == '1')); if (s[i] == '0') a0[++sz] = i; } n = (sz << 1); for (int i = 1; i <= sz; ++i) c[i] = t[a0[i]] + 1; for (int i = sz + 1; i <= n; ++i) c[i] = (t[a0[i - sz]] ^ 1) + 1; for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; get_sa(), get_height(); for (lm = 1; (1 << lm) <= n; ++lm) for (int i = 1; i + (1 << lm) - 1 <= n; ++i) mn[i][lm] = min(mn[i][lm - 1], mn[i + (1 << lm - 1)][lm - 1]); scanf("%d", &Q); while (Q--) { scanf("%d%d%d", &p1, &p2, &len), l1 = Find1(p1), r1 = Find2(p1 + len - 1), l2 = Find1(p2), r2 = Find2(p2 + len - 1); if (r1 - l1 != r2 - l2) { puts("No"); continue; } if (l1 > r1) { puts("Yes"); continue; } if (t[p1 - 1] != t[p2 - 1]) puts((LCP(l1, sz + l2) >= r1 - l1 + 1 ? "Yes" : "No")); else puts((LCP(l1, l2) >= r1 - l1 + 1 ? "Yes" : "No")); } return 0; }
8
#include <bits/stdc++.h> char a[2550][2550]; int cnt[2550][2550]; int up[10], down[10]; int N, M, K; long long ans = 0; int flag = 1; int cal_ones(int x1, int y1, int x2, int y2) { return cnt[x2][y2] - cnt[x2][y1] - cnt[x1][y2] + cnt[x1][y1]; } void divide_conquer(int x1, int y1, int x2, int y2, int flag) { if (x1 == x2 || y1 == y2) return; if (x2 == x1 + 1 && y2 == y1 + 1) { if (cal_ones(x1, y1, x2, y2) == K) ans++; return; } if (flag) { int mid = (x1 + x2) / 2; divide_conquer(x1, y1, mid, y2, !flag); divide_conquer(mid, y1, x2, y2, !flag); for (int i = y1; i < y2; i++) { up[0] = down[0] = mid; for (int k = 1; k <= K + 1; k++) { up[k] = x1; down[k] = x2; } for (int j = i + 1; j <= y2; j++) { for (int k = 1; k <= K + 1; k++) while (cal_ones(up[k], i, mid, j) >= k) up[k]++; for (int k = 1; k <= K + 1; k++) while (cal_ones(mid, i, down[k], j) >= k) down[k]--; for (int k = 0; k <= K; k++) ans += (up[k] - up[k + 1]) * (down[K - k + 1] - down[K - k]); } } } else { int mid = (y1 + y2) / 2; divide_conquer(x1, y1, x2, mid, !flag); divide_conquer(x1, mid, x2, y2, !flag); for (int i = x1; i < x2; i++) { up[0] = down[0] = mid; for (int k = 1; k <= K + 1; k++) { up[k] = y1; down[k] = y2; } for (int j = i + 1; j <= x2; j++) { for (int k = 1; k <= K + 1; k++) while (cal_ones(i, up[k], j, mid) >= k) up[k]++; for (int k = 1; k <= K + 1; k++) while (cal_ones(i, mid, j, down[k]) >= k) down[k]--; for (int k = 0; k <= K; k++) ans += (up[k] - up[k + 1]) * (down[K - k + 1] - down[K - k]); } } } } int main(void) { memset(a, 0, sizeof(a)); memset(cnt, 0, sizeof(cnt)); scanf("%d%d%d", &N, &M, &K); for (int i = 0; i < N; i++) scanf("%s", a + i); for (int i = 0; i <= N; i++) { for (int j = 0; j <= M; j++) { if (i == 0 || j == 0) cnt[i][j] = 0; else cnt[i][j] = cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1] + a[i - 1][j - 1] - '0'; } } divide_conquer(0, 0, N, M, 1); printf("%lld\n", ans); return 0; }
11