solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; cin >> n >> m; vector<vector<long long> > dp(2 * m + 1, vector<long long>(n + 1, 0)); for (long long j = 1; j <= n; j++) dp[1][j] = 1; for (long long i = 1; i <= 2 * m; i++) dp[i][1] = 1; for (long long i = 2; i <= 2 * m; i++) { for (long long j = 2; j <= n; j++) { dp[i][j] = (dp[i][j - 1] + dp[i - 1][j]) % 1000000007; } } long long ans = 0; for (long long i = 1; i <= n; i++) ans = (ans + dp[2 * m][i]) % 1000000007; cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> const int N = 1e3 + 10; using namespace std; int n, a[N]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int ans = min(a[0], a[n - 1]); int x = 1e9; for (int i = 1; i < n - 2; i++) x = min(x, max(a[i], a[i + 1])); cout << min(x, ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; constexpr int maxn = 300000; int n, m, k, nk, par[maxn], lvl[maxn]; vector<int> G[maxn]; struct Cycle { int r, lo, hi; Cycle(int _r, int _lo, int _hi) : r(_r), lo(_lo), hi(_hi) {} int length() const { return lvl[lo] - lvl[hi] + 1; } bool operator<(const Cycle& b) const { return length() < b.length(); } friend ostream& operator<<(ostream& out, const Cycle c) { cout << c.r; auto y = c.lo; while (y != c.hi) { cout << " " << y; y = par[y]; } return out; } }; vector<Cycle> cycles; void print_path(int v) { cout << "PATH\n" << nk << "\n"; cout << Cycle(v, par[v], 0) << "\n"; exit(0); } void dfs(int a = 1, int p = 0) { par[a] = p; if (lvl[a] == nk) print_path(a); bool leaf = true; for (auto v : G[a]) { if (v == p or lvl[v]) continue; leaf = false; lvl[v] = lvl[a] + 1; dfs(v, a); } if (leaf) { sort((G[a]).begin(), (G[a]).end(), [&](int a, int b) { return lvl[a] > lvl[b]; }); auto lo = G[a][1], hi = G[a][2]; if ((lvl[a] - lvl[lo] + 1) % 3) cycles.emplace_back(a, par[a], par[lo]); else if ((lvl[a] - lvl[hi] + 1) % 3) cycles.emplace_back(a, par[a], par[hi]); else cycles.emplace_back(a, lo, par[hi]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; nk = (n + k - 1) / k; for (int u, v, i = 0; i < m; i++) { cin >> u >> v; G[u].emplace_back(v); G[v].emplace_back(u); } lvl[1] = 1; dfs(); cout << "CYCLES\n"; sort((cycles).begin(), (cycles).end()); for (int i = 0; i < k; i++) cout << cycles[i].length() << "\n" << cycles[i] << "\n"; }
3
#include <bits/stdc++.h> int B[2000]; using namespace std; int main() { int n, k, i, j, ans, pos, min; int A[2000]; scanf("%d %d", &n, &k); for (i = 1; i <= n; i++) scanf("%d", &A[i]); for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) { if (A[j] - A[i] != (j - i) * k && A[i] + (j - i) * k > 0) B[i]++; else if (A[i] - A[j] != (i - j) * k && A[i] + (j - i) * k <= 0) { B[i] = 1e8; break; } } pos = 0; min = 1e8; for (i = 1; i <= n; i++) if (min > B[i]) { min = B[i]; pos = i; } printf("%d\n", min); for (i = 1; i <= n; i++) { if (A[i] - A[pos] > (i - pos) * k) printf("- %d %d\n", i, A[i] - A[pos] - (i - pos) * k); if (A[i] - A[pos] < (i - pos) * k) printf("+ %d %d\n", i, (i - pos) * k + A[pos] - A[i]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int sum[3]; for (int i = 0; i < 3; i++) sum[i] = 0; for (int i = 0; i < n; i++) { int a[3]; cin >> a[0] >> a[1] >> a[2]; for (int i = 0; i < 3; i++) sum[i] += a[i]; } if (sum[0] == 0 && sum[1] == 0 && sum[2] == 0) cout << "YES"; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { long long n, m; cin >> n >> m; vector<long long> victories(n, 0); for (long long i = 0; i < m; ++i) { long long max_voices = -1, vinner_index = -1; for (long long j = 0; j < n; ++j) { long long n_voices; cin >> n_voices; if (n_voices > max_voices) { max_voices = n_voices; vinner_index = j; } } ++victories[vinner_index]; } long long max_victories = -1, vinner_index = -1; for (long long i = 0; i < n; ++i) { if (victories[i] > max_victories) { max_victories = victories[i]; vinner_index = i + 1; } } cout << vinner_index << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long arr[200]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long i = 1; for (; i < n + 1; i++) cin >> arr[i]; long long j; for (i = 1; i < n; i++) { for (j = 1; j <= (n - i); j++) { if (arr[j] > arr[j + 1]) { long long temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; cout << j << " " << j + 1 << endl; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; int sum = 0, prev; while (n > 1) { sum += n; prev = n; for (int i = 0; i < 16; i++) { if (n % primes[i] == 0) { n /= primes[i]; break; } } if (prev == n) n = 1; } cout << sum + 1; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int Maxn = 200020; int n, a[Maxn]; long long sum[Maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 0; i < n - 1; i++) { int cur; for (cur = 1; cur <= i;) { int val = i / cur; int ncur = i / val + 1; if (a[i + 2] < a[val + 1]) { sum[cur]++; sum[ncur]--; } cur = ncur; } if (a[i + 2] < a[1]) sum[cur]++, sum[n]--; } for (int i = 1; i < n; i++) sum[i] += sum[i - 1], printf("%I64d\n", sum[i]); ; }
6
#include <bits/stdc++.h> using namespace std; struct node { int v, num; node() {} node(int V, int Num) { v = V, num = Num; } }; queue<int> q; vector<node> G[200005]; vector<int> edge[200005]; int n, m, k; int dep[200005]; bool vis[200005]; void bfs() { q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i].v, num = G[u][i].num; if (!dep[v]) { q.push(v); dep[v] = dep[u] + 1; edge[v].push_back(num); } else if (dep[v] == dep[u] + 1) edge[v].push_back(num); } } } int tot, ans = 1; void dfs(int u) { if (u == n + 1) { for (int i = 1; i <= m; i++) printf("%d", vis[i]); printf("\n"); if (++tot == ans) exit(0); return; } for (int i = 0; i < edge[u].size(); i++) { int v = edge[u][i]; vis[v] = 1; dfs(u + 1); vis[v] = 0; } } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1, u, v; i <= m; i++) { scanf("%d %d", &u, &v); G[u].push_back(node(v, i)); G[v].push_back(node(u, i)); } bfs(); for (int i = 2; i <= n; i++) { if (ans * edge[i].size() > k) { ans = k; break; } else ans *= edge[i].size(); } printf("%d\n", ans); dfs(2); return 0; }
6
#include <stdio.h> typedef long long ll; int main(void) { ll i, j, now = 0, a, n; scanf("%lld", &n); for(i = 0; i < n; ++i) { scanf("%lld", &a); if(a == now + 1) now++; } printf("%lld", now ? n - now : -1); return 0; }
0
#include<queue> #include<string> #include<cstdio> #include<algorithm> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; int main(){ for(int n,m;scanf("%d%d",&n,&m),n;){ vector<int> G[15]; rep(i,m){ int u,v; scanf("%d%d",&u,&v); u--; v--; G[u].push_back(v); G[v].push_back(u); } int S_ini=0; rep(u,n){ int b; scanf("%d",&b); S_ini|=b<<u; } vector<int> sw[15]; rep(u,n){ int k; scanf("%d",&k); sw[u].resize(k); rep(i,k) scanf("%d",&sw[u][i]), sw[u][i]--; sort(sw[u].begin(),sw[u].end()); } if((S_ini&1<<0)==0){ puts("Help me!"); continue; } static int d[15][1<<15]; static char msg[15][1<<15][32]; static pair<int,int> pre[15][1<<15]; rep(u,n) rep(S,1<<n) d[u][S]=-1; d[0][S_ini]=0; queue< pair<int,int> > Q; Q.push(make_pair(0,S_ini)); while(!Q.empty()){ int u=Q.front().first,S=Q.front().second; Q.pop(); // スイッチ切り替え rep(i,sw[u].size()){ int v=sw[u][i]; if(d[u][S^1<<v]==-1 && v!=u){ d[u][S^1<<v]=d[u][S]+1; if(S&1<<v) sprintf(msg[u][S^1<<v],"Switch off room %d.",v+1); else sprintf(msg[u][S^1<<v],"Switch on room %d.",v+1); pre[u][S^1<<v]=make_pair(u,S); Q.push(make_pair(u,S^1<<v)); } } // 部屋移動 rep(i,G[u].size()){ int v=G[u][i]; if(d[v][S]==-1 && S&1<<v){ d[v][S]=d[u][S]+1; sprintf(msg[v][S],"Move to room %d.",v+1); pre[v][S]=make_pair(u,S); Q.push(make_pair(v,S)); } } } if(d[n-1][1<<n-1]!=-1){ printf("You can go home in %d steps.\n",d[n-1][1<<n-1]); vector<string> path; int u=n-1,S=1<<n-1; while(u!=0 || S!=S_ini){ path.push_back(msg[u][S]); pair<int,int> tmp=pre[u][S]; u=tmp.first; S=tmp.second; } reverse(path.begin(),path.end()); rep(i,path.size()) puts(path[i].c_str()); } else{ bool b=false; rep(S,1<<n) if(d[n-1][S]!=-1) b=true; puts(b?"You can not switch off all lights.":"Help me!"); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long num[300005]; long long tmp[300005]; struct node { int id; int a, b; long long ans; node() { ans = 0; } } p[300005]; vector<node> v[300005]; int main() { int n; scanf("%d", &n); int i, j; for (i = 1; i <= n; i++) { scanf("%d", &num[i]); } int m; scanf("%d", &m); for (i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); p[i].a = a; p[i].b = b; p[i].id = i; p[i].ans = 0; if (b > 600) { for (j = a; j <= n; j += b) { p[i].ans += num[j]; } } else v[b].push_back(p[i]); } for (i = 1; i <= 600; i++) { if (v[i].size() > 0) { for (j = n; j >= 1; j--) { tmp[j] = num[j]; if (i + j <= n) { tmp[j] += tmp[i + j]; } } for (j = 0; j < v[i].size(); j++) { p[v[i][j].id].ans = tmp[v[i][j].a]; } } } for (i = 1; i <= m; i++) { printf("%I64d\n", p[i].ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { string s; int ct = 0; cin >> s; int sz = s.size(); for (int i = 0; i < sz; i++) { if (s[i] == 'Q') { for (int j = i + 1; j < sz; j++) { if (s[j] == 'A') { for (int k = j + 1; k < sz; k++) { if (s[k] == 'Q') ct++; } } } } } cout << ct << endl; }
1
#include <bits/stdc++.h> using namespace std; int main(){ double a, b, c, C; double S,L,h; cin>>a>>b>>C; h = b*sin(C/180*M_PI); S = a * h / 2; c = sqrt((pow(a,2)+pow(b,2) - 2 * a * b * cos(C/180*M_PI))); L = a+b+c; printf("%10lf\n", S); printf("%10lf\n", L); printf("%10lf\n", h); }
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; const int MAX_N = 300010; int p[MAX_N]; vector <int> g[MAX_N]; vector <int> new_g[MAX_N]; int pairs[MAX_N]; int used[MAX_N]; vector <int> sets[MAX_N]; vector <int> order; void dfs(int v, bool &foundCycle) { used[v] = 1; for (int u: g[v]) { if (used[u] == 0) { dfs(u, foundCycle); } else if (used[u] == 1) { foundCycle = true; } } used[v] = 2; order.push_back(v); } void dfs2(int v, bool &foundCycle) { used[v] = 1; for (int u: new_g[v]) { if (used[u] == 0) { dfs2(u, foundCycle); } else if (used[u] == 1) { foundCycle = true; } } used[v] = 2; order.push_back(v); } int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) { int p; cin >> p; if (p > 0) { g[i].push_back(p); } } for (int i = 0; i < k; ++i) { int x, y; cin >> x >> y; g[y].push_back(x); pairs[x] = y; } bool foundCycle = false; for (int i = 1; i <= n; ++i) { if (used[i] == 0) { dfs(i, foundCycle); if (foundCycle) { break; } } } if (foundCycle) { cout << 0; return 0; } int setsAmount = 0; for (int i = 0; i < n; ++i) { int v = order[i]; if (p[v] == 0) { setsAmount++; p[v] = setsAmount; sets[setsAmount].push_back(v); int u = v; while (pairs[u] != 0) { u = pairs[u]; p[u] = setsAmount; sets[setsAmount].push_back(u); } } } for (int i = 1; i <= n; ++i) { used[i] = 0; for (int u: g[i]) { if (p[i] != p[u]) { new_g[p[i]].push_back(p[u]); } } } order.clear(); for (int i = 1; i <= setsAmount; ++i) { if (used[i] == 0) { dfs2(i, foundCycle); if (foundCycle) { break; } } } if (foundCycle) { cout << 0; } else { for (int set: order) { for (int x: sets[set]) { cout << x << " "; } } } }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n, m; int t[maxn], c[maxn]; int vis[maxn]; bool check(int x) { memset(vis, false, sizeof vis); int sum = 0; int cnt = 0; for (int i = x; i >= 1; --i) { if (t[i] != 0 && !vis[t[i]]) { vis[t[i]] = true; sum += c[t[i]]; cnt++; } else if (sum > 0) sum--; } if (cnt < m) return false; else if (sum > 0) return false; else return true; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &t[i]); for (int i = 1; i <= m; ++i) scanf("%d", &c[i]); int l = 1, r = n; if (!check(r)) { printf("-1\n"); } else { while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) r = mid - 1; else l = mid + 1; } if (check(l)) printf("%d\n", l); else if (check(r)) printf("%d\n", r); else printf("-1\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; bool vis[10000001]; int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } int lcm(int a, int b) { return ((a * b) / gcd(a, b)); } int main() { int n; scanf("%d", &n); int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a; scanf("%d", &a); if (i == j) sum += a; } } sum %= 2; int q; cin >> q; for (int i = 0; i < q; i++) { int t; scanf("%d", &t); if (t == 3) cout << sum; else { int y; scanf("%d", &y); sum = 1 - sum; } } }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; int b[n]; int sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; sum -= b[i]; } if (sum < 0) cout << "No"; else cout << "Yes"; }
1
#include <iostream> #include <algorithm> #include <string> #include <cstdio> using namespace std; //const double EPS = 1e-9; int n,t; long double dp[2001][2001]; string str; void solve(){ fill(dp[0],dp[2001],0.0); dp[0][0] = 1.0; for(int i=0;i<t;i++){ for(int j=0;j<n;j++){ //if(dp[i][j] < EPS) continue; for(int k=1;k<=6;k++){ dp[i+1][(j+k)%n] += dp[i][j] / 6.0; } } } double ans = 0.0; for(int i=0;i<n;i++){ double sum = 0.0; for(int j=0;j<n;j++){ if(str[(i+j)%n] == 'R') sum += dp[t][j]; } ans = max(ans,sum); } printf("%.9f\n",ans); } int main(){ while(cin >> n >> t && (n|t)){ cin >> str; solve(); } }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, i, j, k; long double t, s = 0, v = 0, ss, vv, EPS = 1E-10, x, a, b, c; cin >> n >> t; vector<long double> aa(n), tt(n); vector<pair<long double, long double>> pp; for (i = 0; i < n; i++) cin >> aa[i]; for (i = 0; i < n; i++) { cin >> tt[i]; s += tt[i] * aa[i]; v += aa[i]; pp.push_back({tt[i], aa[i]}); } sort(pp.rbegin(), pp.rend()); if (s / v >= t + EPS) { for (i = 0; i < n; i++) { ss = s - pp[i].first * pp[i].second; vv = v - pp[i].second; if (ss / vv >= t + EPS) { s = ss; v = vv; } else { a = 0; b = pp[i].second; while (abs(a - b) > EPS) { c = (a + b) / 2; ss = s - pp[i].first * c; vv = v - c; if (ss / vv >= t + EPS) a = c; else b = c; } s -= a * pp[i].first; v -= a; break; } } } else if (s / v <= t - EPS) { for (i = n - 1; i >= 0; i--) { ss = s - pp[i].first * pp[i].second; vv = v - pp[i].second; if (ss / vv <= t - EPS) { s = ss; v = vv; } else { a = 0; b = pp[i].second; while (abs(a - b) > EPS) { c = (a + b) / 2; ss = s - pp[i].first * c; vv = v - c; if (ss / vv <= t - EPS) a = c; else b = c; } s -= a * pp[i].first; v -= a; break; } } } cout << setprecision(10) << fixed << v << "\n"; return 0; }
5
#include<iostream> #include<iomanip> using namespace std; int main() { double a; double answer; while (cin >> a) { answer = a; for (int i = 2; i < 11; ++i) { answer+=(i % 2 ? a /= 3.0 : a *= 2.0); } cout <<setprecision(10)<< answer<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, mod = 1e9 + 7; vector<int> g[N], h[N]; int mark[N][2]; char ans[N]; void dfs(int v) { mark[v][0] = 1; for (int u : g[v]) { if (!mark[u][0]) dfs(u); else if (mark[u][0] == 1) { cout << -1; exit(0); } } mark[v][0] = 2; } void dfs2(int v) { mark[v][0] = 1; for (int u : g[v]) if (!mark[u][0]) dfs2(u); } void dfs3(int v) { mark[v][1] = 1; for (int u : h[v]) if (!mark[u][1]) dfs3(u); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); h[v].push_back(u); } for (int i = 0; i < n; i++) if (!mark[i][0]) dfs(i); memset(mark, 0, sizeof(mark)); int sum = 0; for (int i = 0; i < n; i++) { if (!mark[i][0] && !mark[i][1]) { ans[i] = 'A'; sum++; } else ans[i] = 'E'; if (!mark[i][0]) dfs2(i); if (!mark[i][1]) dfs3(i); } cout << sum << endl; for (int i = 0; i < n; i++) cout << ans[i]; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m, x, y, a[1001]; double F[1001][1001], ans; void Mix(double &a, double &b) { a = b = (a + b) / 2; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i < n; i++) for (int j = i + 1; j <= n; j++) F[i][j] = a[i] > a[j], F[j][i] = 1 - F[i][j]; for (int i = 0; i < m; i++) { scanf("%d%d", &x, &y); Mix(F[x][y], F[y][x]); for (int i = 1; i <= n; i++) if (i - x && i - y) Mix(F[i][x], F[i][y]), Mix(F[x][i], F[y][i]); } for (int i = 1; i < n; i++) for (int j = i + 1; j <= n; j++) ans += F[i][j]; printf("%.8lf", ans); }
4
#include <bits/stdc++.h> using namespace std; const int N = 123123; string a[N], b[N]; int p[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; for (int i = 0; i < n; i++) cin >> p[i]; for (int i = 0; i < n; i++) p[i]--; string prev = min(a[p[0]], b[p[0]]); for (int i = 1; i < n; i++) { string fst = a[p[i]], snd = b[p[i]]; if (fst > snd) swap(fst, snd); if (fst > prev) prev = fst; else if (snd > prev) prev = snd; else { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m, r; int a[31]; int b[31]; int main() { cin >> n >> m >> r; int mina = 0x7fffffff; int maxb = -0x7fffffff; for (int i = 1; i <= n; ++i) { cin >> a[i]; mina = min(mina, a[i]); } for (int i = 1; i <= m; ++i) { cin >> b[i]; maxb = max(maxb, b[i]); } int ans = r / mina * maxb + r - r / mina * mina; ans = max(ans, r); cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; using LL = long long; using LD = long double; struct d { LL a, b; }; d devices[100005]; LL n, p; bool f(LD med) { LD check = 0; for (int i = 0; i < n; i++) { check += max((long double)0.0, med * devices[i].a - devices[i].b) / med; } return check <= p; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> p; for (int i = 0; i < n; i++) { LL a, b; cin >> a >> b; devices[i].a = a; devices[i].b = b; } LD rateSum = 0; for (int i = 0; i < n; i++) rateSum += devices[i].a; if (p >= rateSum) { cout << "-1" << endl; return 0; } LD low = 0, high = 1.0 * LLONG_MAX; while (fabs(high - low) > 1e-9) { LD med = low + (high - low) / 2; if (f(med)) { low = med; } else { high = med; } } cout << fixed << setprecision(9) << low << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; char c[200001]; int a[200001]; void query() { long long n, m; cin >> n; for (int i = 1; i < n; ++i) { cin >> c[i]; } int cur, i = 1; cur = n; while (i < n) if (c[i] == '<') { int l = i, r = i; while (c[r] == '<' && r < n) ++r, --cur; ++r, --cur; for (int j = l; j < r; ++j) { a[j] = cur + j - l + 1; } i = r; } else { a[i] = cur; --cur; ++i; } if (i == n) { a[i] = cur; --cur; ++i; } for (int i = 1; i < n + 1; ++i) { cout << a[i] << " "; } cout << '\n'; cur = 1, i = 1; while (i < n) if (c[i] == '>') { int l = i, r = i; while (c[r] == '>' && r < n) ++r, ++cur; ++r, ++cur; for (int j = l; j < r; ++j) { a[j] = cur - j + l - 1; } i = r; } else { a[i] = cur; ++cur; ++i; } if (i == n) { a[i] = cur; ++cur; ++i; } for (int i = 1; i < n + 1; ++i) { cout << a[i] << " "; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; for (long long i = 1; i <= n; ++i) { query(); } return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; vector<long long> tree, mxi, tag, a; int T; void push(int i, int l, int r) { if (tag[i]) { tree[i] += tag[i]; if (l != r) { tag[((i << 1) + 1)] += tag[i]; tag[((i << 1) + 2)] += tag[i]; } tag[i] = 0; } } void pull(int i, int l, int r) { if (tree[((i << 1) + 1)] + tag[((i << 1) + 1)] >= tree[((i << 1) + 2)] + tag[((i << 1) + 2)]) { tree[i] = tree[((i << 1) + 1)] + tag[((i << 1) + 1)]; mxi[i] = mxi[((i << 1) + 1)]; } else { tree[i] = tree[((i << 1) + 2)] + tag[((i << 1) + 2)]; mxi[i] = mxi[((i << 1) + 2)]; } } void build(int i, int l, int r) { if (l == r) { tree[i] = a[l]; mxi[i] = l; return; } int m = (l + r) >> 1; build(((i << 1) + 1), l, m); build(((i << 1) + 2), m + 1, r); pull(i, l, r); } int ql, qr, x; long long v; void update(int i, int l, int r) { if (ql <= l && r <= qr) { tag[i] += v; return; } int m = (l + r) >> 1; if (ql <= m) update(((i << 1) + 1), l, m); if (qr > m) update(((i << 1) + 2), m + 1, r); pull(i, l, r); } void query(int i, int l, int r) { push(i, l, r); if (ql <= l && r <= qr) { if (tree[i] > v) { v = tree[i]; x = mxi[i]; } return; } int m = (l + r) >> 1; if (ql <= m) query(((i << 1) + 1), l, m); if (qr > m) query(((i << 1) + 2), m + 1, r); } int main() { cin.tie(0), ios::sync_with_stdio(0); int n; cin >> n; vector<tuple<long long, long long, long long>> arr(n); vector<long long> all(n); for (int i = 0; i < n; i++) { long long l, r, c; cin >> l >> r >> c; if (l > r) swap(l, r); arr[i] = tie(l, r, c); all[i] = r; } sort(arr.begin(), arr.end()); sort(all.begin(), all.end()); all.resize(unique(all.begin(), all.end()) - all.begin()); T = all.size(); a.resize(T, 0); for (int i = 0; i < n; i++) { get<1>(arr[i]) = lower_bound(all.begin(), all.end(), get<1>(arr[i])) - all.begin(); a[get<1>(arr[i])] += get<2>(arr[i]); } for (int i = 1; i < T; i++) a[i] += a[i - 1]; for (int i = 0; i < T; i++) a[i] -= all[i]; tree.resize(T * 4); tag.resize(T * 4, 0); mxi.resize(T * 4); build(0, 0, T - 1); long long ans = 0; int l = 1.5e9, r = l; for (int i = 0; i < n;) { int j = i + 1; while (j < n && get<0>(arr[i]) == get<0>(arr[j])) j++; for (int k = i; k < j; k++) { ql = lower_bound(all.begin(), all.end(), get<0>(arr[k])) - all.begin(); qr = T - 1; v = -1e9; query(0, 0, T - 1); if (v + get<0>(arr[k]) > ans) { ans = v + get<0>(arr[k]); l = get<0>(arr[k]); r = all[x]; } } for (int k = i; k < j; k++) { ql = get<1>(arr[k]); qr = T - 1; v = -get<2>(arr[k]); update(0, 0, T - 1); } i = j; } cout << ans << "\n"; cout << l << " " << l << " " << r << " " << r << "\n"; return 0; }
6
#include<bits/stdc++.h> #define ll long long using namespace std; const int N=2010; const ll inf=1e18; int n,sz[N];char s[N];ll ds[N],dz[N],sn[N]; vector<int>g[N]; void dfs(int u,int fa){ sz[u]=s[u]-'0'; ll sum=ds[u]=sn[u]=0,mx=0; for(int v : g[u]){ if(v==fa)continue; dfs(v,u); ds[u]+=ds[v]+sz[v];sz[u]+=sz[v]; if(ds[v]+sz[v]>=mx)sum+=mx,mx=ds[sn[u]=v]+sz[v]; else sum+=ds[v]+sz[v]; } dz[u] = dz[sn[u]] + ((mx>=sum) ? sum : ((mx+sum)>>1)); } int main(){ // freopen("E.in","r",stdin); // freopen("E.out","w",stdout); scanf("%d%s",&n,s+1); for(int i=1;i<n;++i){ int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } ll ans=inf; for(int i=1;i<=n;++i){ dfs(i,0); if(ds[i]&1)continue; if(dz[i]*2 >= ds[i])ans=min(ds[i]>>1,ans); } if(ans==inf)puts("-1");else cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, cnt, ans; int Q[200010], s1[200010], s2[200010], a[200010], pre[200010], nxt[200010]; char s[2][200010]; bool vis[200010]; int main() { int lst, x, y, tp1, tp2; scanf("%d%d", &n, &m); scanf("%s", s[0] + 1); scanf("%s", s[1] + 1); for (int i = 1; i <= n; i++) { if (s[0][i] == 'X' || s[1][i] == 'X') { vis[i] = 1; if (s[0][i] == '.' || s[1][i] == '.') { Q[++cnt] = i; if (s[0][i] == 'X') a[i] = 2; else a[i] = 1; } if ((s[0][i] == 'X' && s[1][i] == 'X') || (i > 1 && ((s[0][i] == 'X' && s[1][i - 1] == 'X') || (s[1][i] == 'X' && s[0][i - 1] == 'X')))) s1[i] = 1; } } for (int i = 1; i <= n; i++) s1[i] += s1[i - 1]; for (int i = 1; i <= cnt; i++) { if (s1[Q[i]] > s1[Q[i - 1]]) { s2[Q[i]] = 0; continue; } if (a[Q[i]] == a[Q[i - 1]]) s2[Q[i]] = Q[i] - Q[i - 1]; else s2[Q[i]] = Q[i] - Q[i - 1] + 1; } for (int i = 1; i <= n; i++) s2[i] += s2[i - 1]; lst = 0; for (int i = 1; i <= n; i++) { if (vis[i]) lst = i; pre[i] = lst; } lst = n + 1; for (int i = n; i; i--) { if (vis[i]) lst = i; nxt[i] = lst; } for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); if (x == y) { puts("0"); continue; } tp1 = tp2 = 1; if (x > n) { tp1++; x -= n; } if (y > n) { tp2++; y -= n; } if (y < x) { swap(tp1, tp2); swap(x, y); } if (s1[y] > s1[x]) { puts("-1"); continue; } if (!vis[x] && !vis[y] && (pre[x] == pre[y])) { ans = (tp1 == tp2) ? (y - x) : (y - x + 1); printf("%d\n", ans); continue; } ans = 0; if (!vis[x]) { ans += nxt[x] - x; if (a[nxt[x]] != tp1) ans++; x = nxt[x]; } if (!vis[y]) { ans += y - pre[y]; if (a[pre[y]] != tp2) ans++; y = pre[y]; } ans += s2[y] - s2[x]; printf("%d\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } long long seg[4 * N], a[N]; void build(int p, int s, int e) { if (s == e) { seg[p] = a[s]; return; } int m = (s + e) / 2; build(2 * p, s, m); build(2 * p + 1, m + 1, e); seg[p] = gcd(seg[2 * p], seg[2 * p + 1]); } long long query(int p, int s, int e, int l, int r) { if (s > r || e < l) return 0; if (l <= s && e <= r) return seg[p]; int m = (s + e) / 2; return gcd(query(2 * p, s, m, l, r), query(2 * p + 1, m + 1, e, l, r)); } void solve() { int n; scanf("%d", &n); long long a[n]; for (int i = 0; i < n; ++i) { scanf("%lld", a + i); } for (int i = 0; i < n - 1; ++i) { ::a[i] = abs(a[i] - a[i + 1]); } build(1, 0, n - 1); long long l = 0, ans = 1; for (int r = 0; r < n - 1; ++r) { while (r >= l && query(1, 0, n - 1, l, r) == 1) { ++l; } ans = max(ans, r - l + 2); } printf("%lld\n", ans); } int main() { int t = 1; scanf("%d", &t); while (t--) solve(); }
4
#include <bits/stdc++.h> using namespace std; const long long N = 400; long long dp[N][N]; long long did_dp[N][N]; vector<pair<long long, long long> > a; long long n; vector<long long> events; const long long INF = 1e18; void mmin(long long &a, long long b) { a = min(a, b); } void mmax(long long &a, long long b) { a = max(a, b); } map<long long, long long> anti_dict; long long find_event(long long coord) { return anti_dict[coord]; } long long dfs(long long ind, long long h_ind) { mmin(h_ind, ind); if (ind == 0) return 0; if (did_dp[ind][h_ind]) return dp[ind][h_ind]; did_dp[ind][h_ind] = 1; long long min_ind = ind - 1; for (long long i = 0; i < n; ++i) { if (a[i].first == events[ind - 1]) min_ind = find_event(a[i].first - a[i].second); } long long ret = dfs(ind - 1, min_ind); min_ind = h_ind; for (long long i = 0; i < n; ++i) if (a[i].first >= events[h_ind] && a[i].first < events[ind]) mmin(min_ind, find_event(a[i].first - a[i].second)); if (h_ind != ind) mmax(ret, events[ind] - events[h_ind] + dfs(h_ind, min_ind)); for (long long i = n - 1; i >= 0; --i) { if (a[i].first >= events[ind]) continue; if (a[i].first + a[i].second >= events[ind]) { mmax(ret, events[ind] - events[find_event(a[i].first)] + dfs(find_event(a[i].first), min_ind)); } mmin(min_ind, find_event(a[i].first - a[i].second)); } return dp[ind][h_ind] = ret; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; a.resize(n); for (long long i = 0; i < n; ++i) cin >> a[i].first >> a[i].second; sort(a.begin(), a.end()); set<long long> st; for (long long i = 0; i < n; ++i) { st.insert(a[i].first); st.insert(a[i].first - a[i].second); st.insert(a[i].first + a[i].second); } for (long long x : st) { anti_dict[x] = events.size(); events.push_back(x); } cout << dfs(events.size() - 1, events.size() - 1); }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, a[55][55]; while (cin >> n) { int maxn = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; if (a[i][j] > maxn) { maxn = a[i][j]; } } } if (maxn == 1) { cout << "Yes" << endl; continue; } int tf = 0; for (int ii = 1; ii <= n; ii++) { for (int jj = 1; jj <= n; jj++) { int x = 0; if (a[ii][jj] > 1) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (a[ii][j] + a[i][jj] == a[ii][jj]) { x = 1; break; } } if (x == 1) { break; } } } if (x == 0 && a[ii][jj] > 1) { tf = 1; break; } } } if (tf == 1) { cout << "No" << endl; } else { cout << "Yes" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, m, d; cin >> n >> k >> m >> d; long long ans = 0; for (int i = 1; i <= d; i++) { long long t = n / ((i - 1) * k + 1); if (t < 1) break; long long x = min(t, m); ans = max(x * i, ans); } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; long long pow_mod(long long a, long long b) { long long ans = 1; a %= mod; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1; } return ans; } int judge(char s) { if (s == 'A') return 0; else if (s == 'C') return 1; else if (s == 'G') return 2; return 3; } int n; string s; int cnt[10]; int main() { scanf("%d", &n); cin >> s; int len = s.length(); for (int i = 0; i < len; ++i) { cnt[judge(s[i])]++; } int maxx = -1; for (int i = 0; i < 4; ++i) { maxx = max(maxx, cnt[i]); } long long ans = 0; for (int i = 0; i < 4; ++i) { if (cnt[i] == maxx) ans++; } printf("%I64d\n", pow_mod(ans, (long long)n)); return 0; }
3
#include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(n); for(int i=0; i<n; ++i) cin >> a[i]; set<int> used; for(int i=n-1; i>=0; --i){ if(m > 0 && used.find(a[i]) == used.end()){ cout << a[i] << endl; used.insert(a[i]); -- m; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 9, oo = 1e9; bool a[105][105]; void solve() { int k, n = 3; cin >> k; while (n * (n - 1) * (n - 2) < k * 6) n++; if (n * (n - 1) * (n - 2) != 6 * k) n--; for (int i = 0; i < n; i++) { a[i][i] = 0; for (int j = i + 1; j < n; j++) { a[i][j] = 1; a[j][i] = 1; } } k -= n * (n - 1) * (n - 2) / 6; while (k) { int x = 0; while (++x * (x + 1) <= 2 * k) ; k -= --x * (x + 1) / 2; for (int i = 0; i < x; i++) { a[n][i + 1] = 1; a[i + 1][n] = 1; } a[n][0] = 1; a[0][n++] = 1; } cout << n << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << a[i][j]; cout << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) solve(); }
3
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(a),i##end=(b);i<=i##end;++i) #define per(i,a,b) for(int i=(a),i##end=(b);i>=i##end;--i) //mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); template<typename T>void chkmax(T&x,T y){if(x<y)x=y;} template<typename T>void chkmin(T&x,T y){if(x>y)x=y;} inline int read(){ #define nc getchar() int x=0;char c=nc;bool f=0; while(c<48)f|=c=='-',c=nc; while(c>47)x=x*10+(c^48),c=nc; return f?-x:x; #undef nc } typedef double db; typedef long long ll; typedef vector<int>vi; typedef pair<int,int>pii; const int maxn=1010,P=998244353; int Inc(int x,int y){return x+y<P?x+y:x+y-P;} int Dec(int x,int y){return x<y?x-y+P:x-y;} void Add(int &x,int y){x+=y;if(x>=P)x-=P;} void Sub(int &x,int y){x-=y;if(x<0)x+=P;} int qp(int a,int k){int res=1;for(;k;k>>=1,a=1ll*a*a%P)if(k&1)res=1ll*res*a%P;return res;} int n,m,valA[maxn],valB[maxn],F[maxn][maxn],G[maxn][maxn];char A[maxn],B[maxn]; void solve(){ scanf("%s%s",A+1,B+1),n=strlen(A+1),m=strlen(B+1); rep(i,1,n){ per(k,i,1){ valA[i]++; if(A[k]==A[k-1])break; } } rep(i,1,m){ per(k,i,1){ valB[i]++; if(B[k]==B[k-1])break; } } int ans=0; rep(i,1,n){ rep(j,1,m){ if(A[i]!=B[j]){ F[i][j]=valB[j]; G[i][j]=valA[i]; // per(k,j,1){ // F[i][j]++; // if(B[k]==B[k-1])break; // } // per(k,i,1){ // G[i][j]++; // if(A[k]==A[k-1])break; // } } // F[i][j]=valB[A[i]-'a'][j]; // G[i][j]=valA[B[j]-'a'][i]; if(A[i]!=A[i-1])Add(F[i][j],(1ll*F[i-1][j])%P); if(A[i]!=B[j])Add(F[i][j],(1ll*G[i-1][j])%P); if(B[j]!=B[j-1])Add(G[i][j],(1ll*G[i][j-1])%P); if(B[j]!=A[i])Add(G[i][j],(1ll*F[i][j-1])%P); // printf("(%d %d) %d %d\n",i,j,F[i][j],G[i][j]); Add(ans,Inc(F[i][j],G[i][j])); } } cout<<ans<<endl; } signed main(){ // int T=read(); // while(T--)solve(); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAX = 1000005; char s[MAX]; int z[MAX]; void zfun(int n) { int l = 0, r = 0; for (int i = 1; i < n; ++i) { if (i <= r) z[i] = min(z[i - l], r - i + 1); while (i + z[i] < n && s[i + z[i]] == s[z[i]]) z[i]++; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int n; cin >> s; n = strlen(s); zfun(n); int tmp = -1; for (int i = 1; i < n; ++i) { if (z[i] <= tmp && z[i] == n - i) { for (int j = i; j < i + z[i]; ++j) cout << s[j]; return 0; } tmp = max(tmp, z[i]); } cout << "Just a legend" << endl; return 0; }
2
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <set> #include <cmath> typedef long long ll; using namespace std; #define all(x) x.begin(), x.end() const ll INF = 1e9; ll n; ll xs, ys, xf, yf; ll x[5000], y[5000], r[5000]; double d[1111][1111]; double dist(int i, int j) { return sqrt((double)(x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> xs >> ys >> xf >> yf >> n; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i] >> r[i]; } x[0] = xs; y[0] = ys; x[n + 1] = xf; y[n + 1] = yf; n += 2; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = max((double)0, dist(i, j) - r[i] - r[j]); } } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } printf("%.9f", d[0][n - 1]); }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; const long long INF = 1000000000 + 9; const long double EPS = 1e-9; const long double PI = acos(-1); char s1[MAXN], s2[MAXN]; int k4, k7, len; void Input() { ifstream in("INPUT.TXT"); cin >> s1; cin >> s2; in.close(); } void Solution() { len = strlen(s1); for (int i = 0; i < len; i++) { if (s1[i] != s2[i] && s1[i] == '4') k4++; if (s1[i] != s2[i] && s1[i] == '7') k7++; } } void Output() { ofstream out("OUTPUT.TXT"); cout << k4 + k7 - min(k4, k7); cout << "\n"; out.close(); } int main() { Input(); Solution(); Output(); return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 1e2 + 10; int a[N], cont[N]; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; cont[a[i]]++; } int ans = 0; bool ok1 = true, ok2 = true; for (int i = 0; i <= 100; i++) { if (ok1 and cont[i] == 0) { ans += i; ok1 = false; } if (ok2 and cont[i] <= 1) { ans += i; ok2 = false; } } cout << ans << '\n'; for (int i = 0; i < n; i++) cont[a[i]] = 0; } return 0; }
1
#include<bits/stdc++.h> using namespace std; int main() { int N; cin>>N; int a=(N%10); if(a==2 || a==4||a==5||a==7||a==9) {cout<<"hon";} else if(a==3) {cout<<"bon";} else {cout<<"pon";} return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100005; inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * w; } int n, m1, m2; int fa1[N], fa2[N]; inline int find1(int u) { return fa1[u] == u ? u : fa1[u] = find1(fa1[u]); } inline int find2(int u) { return fa2[u] == u ? u : fa2[u] = find2(fa2[u]); } void merge1(int u, int v) { int x = find1(u), y = find1(v); if (x != y) fa1[x] = y; } void merge2(int u, int v) { int x = find2(u), y = find2(v); if (x != y) fa2[x] = y; } bool vis[N]; int main() { n = read(); m1 = read(); m2 = read(); for (int i = 1; i <= n; i++) fa1[i] = fa2[i] = i; int u, v; for (int i = 1; i <= m1; i++) { u = read(); v = read(); merge1(u, v); } for (int i = 1; i <= m2; i++) { u = read(); v = read(); merge2(u, v); } vector<int> ans1, ans2; for (int i = 1; i <= 1; i++) for (int j = 2; j <= n; j++) { int x1 = find1(i), y1 = find1(j); int x2 = find2(i), y2 = find2(j); if (x1 != y1 && x2 != y2) { ans1.push_back(i); ans2.push_back(j); fa1[x1] = y1; fa2[x2] = y2; vis[j] = 1; } } for (int i = 1; i <= 1; i++) for (int j = 2; j <= n; j++) { if (vis[j]) continue; int x1 = find1(i), y1 = find1(j); if (x1 != y1) { ans1.push_back(j); fa1[x1] = y1; } int x2 = find2(i), y2 = find2(j); if (x2 != y2) { ans2.push_back(j); fa2[x2] = y2; } } int sz = min(ans1.size(), ans2.size()); printf("%d\n", sz); for (int i = 0; i < sz; i++) printf("%d %d\n", ans1[i], ans2[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; int dp[100000][16][12]; inline bool check(int mask, int pos) { return (bool)(mask & (1 << pos)); } int m; int mod = 1000000007; int dp_func(int pos, int mask, int koyta) { if (check(mask, m)) mask = mask ^ (1 << m); if (koyta == 0) return 1; if (pos == -1) { if (koyta == 0) return 1; return 0; } if (dp[pos][mask][koyta] != -1) return dp[pos][mask][koyta]; int ret = 0; ret += dp_func(pos - 1, mask << 1, koyta); if (koyta) { long long g = 1LL * dp_func(pos - 1, (mask << 1) | (1 << 0), koyta - 1) * (__builtin_popcount(mask) + 1); if (g >= mod) g %= mod; ret += g; } return dp[pos][mask][koyta] = ret % mod; ; } int main() { memset(dp, -1, sizeof dp); int n, k; scanf("%d%d%d", &n, &k, &m); long long ret = 0; for (int i = n - 1; i >= 0; i--) { ret += dp_func(i - 1, (1 << 0), k - 1); } ret %= mod; cout << ret << endl; }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int l[MAXN], a[MAXN], r[MAXN], n; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); l[1] = 1; for (int i = 2; i <= n; i++) l[i] = min(l[i - 1] + 1, a[i]); r[n] = 1; for (int i = n - 1; i >= 1; i--) r[i] = min(r[i + 1] + 1, a[i]); int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, min(l[i], r[i])); printf("%d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int MAXN = 100005; int n, m, arr[MAXN]; int main() { cin >> n >> m; long long tot = 0; for (int x = 1; x <= n; x++) { cin >> arr[x]; tot = tot + arr[x]; } sort(arr + 1, arr + n + 1); long long nyak = arr[n]; long long ans = 0; for (int x = n; x >= 1; x--) { if (x != 1) { if (nyak > arr[x - 1]) { ans = ans + nyak - arr[x - 1]; nyak = arr[x - 1]; } else { ans++; nyak--; } } else { if (nyak > 0) { ans = ans + nyak; } else { ans++; } } } printf("%lld\n", tot - ans); }
2
#include <bits/stdc++.h> using namespace std; long long n, m, r, k; long long calc(long long row, long long col) { long long x = min(col, m - r + 1LL) - max(1LL, col - r + 1LL) + 1LL; long long y = min(row, n - r + 1LL) - max(1LL, row - r + 1LL) + 1LL; return x * y; } int main() { scanf("%lld%lld%lld%lld", &n, &m, &r, &k); priority_queue<pair<long long, pair<int, int> > > pque; int cmid = (m + 1) / 2; int rmid = (n + 1) / 2; pque.push(make_pair(calc(rmid, cmid), make_pair(rmid, cmid))); set<pair<int, int> > st; double res = 0.0; for (int i = 0; i < k; i++) { pair<long long, pair<int, int> > t = pque.top(); pque.pop(); while (t.second.first <= 0 || t.second.second <= 0 || st.find(t.second) != st.end()) t = pque.top(), pque.pop(); st.insert(t.second); res += t.first; int y = t.second.first; int x = t.second.second; pque.push(make_pair(calc(y - 1, x), make_pair(y - 1, x))); pque.push(make_pair(calc(y + 1, x), make_pair(y + 1, x))); pque.push(make_pair(calc(y, x - 1), make_pair(y, x - 1))); pque.push(make_pair(calc(y, x + 1), make_pair(y, x + 1))); } res /= (n - r + 1LL) * (m - r + 1LL); printf("%.9lf\n", res); return 0; }
4
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll max(ll a,ll b) {return a<b?b:a;} ll cmp(const ll &x,const ll &y) {return x>y;} ll a[4][110]; int main() { int n; ll W,w1,w,x,ans=0; scanf("%d%lld",&n,&W); scanf("%lld%lld",&w1,&x); a[0][1]=x;a[0][0]=1; for(int i=1;i<n;i++) { scanf("%lld%lld",&w,&x); int p=w-w1; a[p][++a[p][0]]=x; } for(int i=0;i<4;i++) sort(a[i]+1,a[i]+a[i][0]+1,cmp); for(int i=0;i<=a[0][0];i++) for(int j=0;j<=a[1][0];j++) for(int k=0;k<=a[2][0];k++) { ll res=(i+j+k)*w1+j+2*k; res=W-res;if(res<0) continue; res/=(w1+3);if(res>a[3][0]) res=a[3][0]; ll s=0; for(int ii=1;ii<=i;ii++) s+=a[0][ii]; for(int ii=1;ii<=j;ii++) s+=a[1][ii]; for(int ii=1;ii<=k;ii++) s+=a[2][ii]; for(int ii=1;ii<=res;ii++) s+=a[3][ii]; ans=max(ans,s); } printf("%lld\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAX_N = 260005, MAX_W = 500; struct GRIPPER { int m, p, r; long long d; } grip[MAX_N]; int x, y, n, X, Y, top; int p[MAX_N], r[MAX_N]; int L[1000], R[1000]; long long D[1000]; bool vis[MAX_N]; bool cmp_d(GRIPPER a, GRIPPER b) { return a.d < b.d; } bool cmp_m(GRIPPER a, GRIPPER b) { return a.m < b.m; } void do_it() { int st = 0, all = 1; while (st < all) { int P = p[st]; long long Rad = 1ll * r[st] * r[st]; st++; for (int i = 0; i < top; i++) { if (D[i] > Rad) { for (int j = L[i]; j <= R[i]; ++j) if (!vis[j] && grip[j].d <= Rad && grip[j].m <= P) { vis[j] = 1; p[all] = grip[j].p; r[all] = grip[j].r; all++; } break; } while (L[i] <= R[i] && grip[L[i]].m <= P) { if (!vis[L[i]]) { p[all] = grip[L[i]].p; r[all] = grip[L[i]].r; all++; vis[L[i]] = 1; } ++L[i]; } } } printf("%d\n", all - 1); return; } void init() { sort(grip + 1, grip + 1 + n, cmp_d); int i = 1; top = 0; while (i <= n) { int en = i + MAX_W - 1; if (en > n) en = n; L[top] = i; R[top] = en; D[top] = grip[en].d; sort(grip + L[top], grip + R[top] + 1, cmp_m); top++; i = en + 1; } } void input() { scanf("%d%d%d%d%d", &X, &Y, &p[0], &r[0], &n); for (int i = 1; i <= n; i++) { scanf("%d%d%d%d%d", &x, &y, &grip[i].m, &grip[i].p, &grip[i].r); grip[i].d = 1ll * (X - x) * (X - x) + 1ll * (Y - y) * (Y - y); } return; } int main() { input(); init(); do_it(); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int M = 1 << 20, INF = 2e9; int dp[M], n, m, cost[25][25]; char s[25][25]; void Min(int &a, int b) { if (a > b) a = b; } void rd(int &res) { char c; res = 0; while (c = getchar(), !isdigit(c)) ; do res = (res << 3) + (res << 1) + (c ^ 48); while (c = getchar(), isdigit(c)); } int find(int string) { for (int i = 0; i < n; i++) if (!(string & (1 << i))) return i; return n - 1; } int main() { rd(n); rd(m); for (int i = 0; i < n; i++) scanf("%s", s[i]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) rd(cost[i][j]); for (int string = 1; string < 1 << n; string++) dp[string] = INF; for (int string = 0; string + 1 < 1 << n; string++) { int had = find(string); for (int c = 0; c < m; c++) { Min(dp[string | (1 << had)], dp[string] + cost[had][c]); int sum = 0, mx = 0, nw = 0; for (int r = 0; r < n; r++) if (s[had][c] == s[r][c]) { sum += cost[r][c]; mx = max(mx, cost[r][c]); nw |= 1 << r; } Min(dp[string | nw], dp[string] + sum - mx); } } printf("%d", dp[(1 << n) - 1]); return 0; }
5
#define _USE_MATH_DEFINES #include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <cfloat> #include <climits> #include <cstring> #include <cmath> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<ll, int> ll_i; typedef pair<double, int> d_i; typedef pair<ll, ll> ll_ll; typedef pair<double, double> d_d; struct edge { int u, v; double w; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int dfs(vector<string>& a, vector<string>& b, string& c, string& d) { if (c == d) return 0; else if (c.length() >= d.length()) return INT_MAX / 2; int mini = INT_MAX / 2; for (int i = 0; i < a.size(); i++) { string s = c, t; while (!s.empty()) { if (s.length() >= a[i].length() && s.substr(0, a[i].length()) == a[i]) { t += b[i]; s.erase(s.begin(), s.begin() + a[i].length()); } else { t += s[0]; s.erase(s.begin(), s.begin() + 1); } } if (t != c) mini = min(mini, dfs(a, b, t, d) + 1); } return mini; } int main() { for (;;) { int n; cin >> n; if (n == 0) break; vector<string> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; string c, d; cin >> c >> d; int x = dfs(a, b, c, d); cout << (x < INT_MAX / 2 ? x : -1) << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int K = 10; const int N = 500500; int n; int a[N]; char b[N]; bool Res[K][2]; int f(int i, int x, int bit) { if (i == n) return x; bool cur = (a[i] & (1 << bit)); if (b[i] == '&') return f(i + 1, x & cur, bit); if (b[i] == '|') return f(i + 1, x | cur, bit); if (b[i] == '^') return f(i + 1, x ^ cur, bit); assert(0); } int main() { cin >> n; for (int i = 0; i < n; i++) scanf(" %c%d", b + i, a + i); for (int bit = 0; bit < K; bit++) { Res[bit][0] = f(0, 0, bit); Res[bit][1] = f(0, 1, bit); } int OrMask = 0; int AndMask = (1 << K) - 1; int XorMask = 0; for (int bit = 0; bit < K; bit++) { if (Res[bit][0] != Res[bit][1]) { if (Res[bit][0] == 1) XorMask |= (1 << bit); } else { if (Res[bit][0] == 0) AndMask ^= (1 << bit); else OrMask |= (1 << bit); } } puts("3"); cout << "| " << OrMask << endl; cout << "& " << AndMask << endl; cout << "^ " << XorMask << endl; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5; const int smax = 1e3; long long int ans, n, m, x, y, k; long long int a[maxn + 10]; long long int b[maxn + 10]; vector<int> g[maxn + 10]; set<int> se[maxn + 10]; string s; long int mat[smax + 10][smax + 10]; int main() { cin >> n >> m; memset(a, 0, sizeof a); memset(b, 0, sizeof b); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> mat[i][j]; if (mat[i][j] == 1) { a[i]++; b[j]++; } } } ans = 0; for (int i = 0; i < n; i++) { ans += ((1LL << a[i])) + ((1LL << m - a[i])) - 2; } for (int i = 0; i < m; i++) { ans += ((1LL << b[i])) + ((1LL << n - b[i])) - 2; } ans -= (n * m); cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n,l; char s[5010]; long long dp[5010][5010]; const long long mod=1e9+7; inline long long ksm(long long x,long long y,long long mod) { long long res=1; while(y) { if(y&1) res=res*x%mod; x=x*x%mod; y>>=1; } return res; } int main() { scanf("%d",&n); scanf("%s",s+1); l=strlen(s+1); dp[0][0]=1; for(int i=1;i<=n;++i) { for(int j=0;j<=i;++j) { dp[i][j]=(dp[i][j]+dp[i-1][j-1]*2)%mod; if(j==0) dp[i][j]=(dp[i][j]+dp[i-1][j])%mod; else dp[i][j-1]=(dp[i][j-1]+dp[i-1][j])%mod; } } long long ans=ksm(2,l,mod); ans=ksm(ans,mod-2,mod); ans=ans*dp[n][l]%mod; printf("%lld\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; void maxify(long long &a, long long b) { b > a && (a = b); } void minify(long long &a, long long b) { b < a && (a = b); } struct IOManager { template <typename T> inline bool read(T &x) { char c; bool flag = false; x = 0; while (~c && !isdigit(c = getchar()) && c != '-') ; c == '-' && (flag = true, c = getchar()); if (!~c) return false; while (isdigit(c)) x = (x << 3) + (x << 1) + c - '0', c = getchar(); return (flag && (x = -x), true); } inline bool read(char &c) { c = '\n'; while (~c && !(isprint(c = getchar()) && c != ' ')) ; return ~c; } inline long long read(char s[]) { char c; long long len = 0; while (~c && !(isprint(c = getchar()) && c != ' ')) ; if (!~c) return 0; while (isprint(c) && c != ' ') s[len++] = c, c = getchar(); return (s[len] = '\0', len); } template <typename T> inline IOManager operator>(T &x) { read(x); return *this; } template <typename T> inline void write(T x) { x < 0 && (putchar('-'), x = -x); x > 9 && (write(x / 10), true); putchar(x % 10 + '0'); } inline void write(char c) { putchar(c); } inline void write(char s[]) { long long pos = 0; while (s[pos] != '\0') putchar(s[pos++]); } template <typename T> inline IOManager operator<(T x) { write(x); return *this; } } io; struct BinaryIndexedTree { private: static const long long N = 200000; long long n, a[N]; public: void init(long long t) { n = t, memset(a, 0, sizeof a); } void add(long long p, long long v) { for (; p <= n; p += p & -p) a[p] += v; } long long get(long long p) { long long ret = 0; for (; p; p -= p & -p) ret += a[p]; return ret; } }; struct Set { private: static const long long N = 200000; long long a[N]; set<long long> s[N]; set<long long>::iterator it; public: void modify(long long p, long long v) { if (a[p]) s[a[p]].erase(p); a[p] = v; s[a[p]].insert(p); } long long prev(long long p) { it = s[a[p]].find(p); return it == s[a[p]].begin() ? *it : *(--it); } long long next(long long p) { it = ++s[a[p]].find(p); return it == s[a[p]].end() ? *(--it) : *it; } }; struct Solver { private: static const long long N = 200000; long long n, m, numq, top, pos[N], pre[N], ans[N]; BinaryIndexedTree tree; Set s; struct Query { long long type, x, y, w, id; bool operator<(const Query &q) const { return x == q.x ? type < q.type : x < q.x; } } q[N << 2], tmp[N << 2]; void add_query(long long type, long long x, long long y, long long w, long long id) { q[++numq] = (Query){type, x, y, w, id}; } void input() { io > n > m; for (long long i = 2; i <= n + 1; ++i) { long long t; io > t, s.modify(i, t); long long prev = s.prev(i); add_query(1, prev, i, i - prev, 0); } for (long long i = 1; i <= m; ++i) { long long o; io > o; if (o == 1) { long long p, x; io > p > x; ++p; long long prev = s.prev(p), next = s.next(p); if (prev < p) add_query(1, prev, p, prev - p, 0); if (next > p) add_query(1, p, next, p - next, 0); if (prev < p && next > p) add_query(1, prev, next, next - prev, 0); s.modify(p, x); prev = s.prev(p), next = s.next(p); if (prev < p && next > p) add_query(1, prev, next, prev - next, 0); if (prev < p) add_query(1, prev, p, p - prev, 0); if (next > p) add_query(1, p, next, next - p, 0); } else { long long l, r; io > l > r, ++l, ++r, ++top; add_query(2, r, r, 1, top); add_query(2, r, l - 1, -1, top); add_query(2, l - 1, r, -1, top); add_query(2, l - 1, l - 1, 1, top); } } } void process(long long l, long long r) { if (l == r) return; long long mid = l + r >> 1; process(l, mid), process(mid + 1, r); long long s = l, t = mid + 1, pnt = l; while (s <= mid && t <= r) { if (q[s] < q[t]) { if (q[s].type == 1) tree.add(q[s].y, q[s].w); tmp[pnt++] = q[s++]; } else { if (q[t].type == 2) ans[q[t].id] += q[t].w * tree.get(q[t].y); tmp[pnt++] = q[t++]; } } while (t <= r) { if (q[t].type == 2) ans[q[t].id] += q[t].w * tree.get(q[t].y); tmp[pnt++] = q[t++]; } for (long long i = l; i < s; ++i) if (q[i].type == 1) tree.add(q[i].y, -q[i].w); while (s <= mid) tmp[pnt++] = q[s++]; for (long long i = l; i <= r; ++i) q[i] = tmp[i]; } public: void solve() { input(), tree.init(n + 1), process(1, numq); for (long long i = 1; i <= top; ++i) io < ans[i] < '\n'; } } solver; signed main() { solver.solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &first, T2 second) { if (first > second) first = second; } template <typename T1, typename T2> inline void chkmax(T1 &first, T2 second) { if (first < second) first = second; } const string FileName = "input"; const int MAXN = 3e5 + 100; int n, k, b, c; vector<int> arr; vector<int> event[5]; long long sum, mod, ans = 1e18; multiset<long long> que; long long f(long long s, long long t) { return ((t - s) / 5) * b + ((t - s) % 5) * c; } int main(int argc, char const *argv[]) { srand(time(0)); ios::sync_with_stdio(0); cin >> n >> k >> b >> c; arr.resize(n); b = min(b, c * 5); for (int i = 0; i < n; ++i) { cin >> arr[i]; } sort(arr.begin(), arr.end()); for (int i = 0; i < n; ++i) { for (int j = 0; j < 5; ++j) { event[(((arr[i] + j) % 5) + 5) % 5].push_back(arr[i] + j); } } for (int j = 0; j < 5; ++j) { int L = 0; sum = 0, mod = 0; que.clear(); for (int i = 0; i < n; ++i) { while (L < n && arr[L] <= event[j][i]) { que.insert(-f(arr[L], event[j][i]) - mod); sum += f(arr[L], event[j][i]); ++L; } while (que.size() > k) { sum += *que.begin() + mod; que.erase(que.begin()); } if (que.size() == k) { ans = min(ans, sum); } if (i != n - 1) { assert(event[j][i] <= event[j][i + 1]); mod -= f(event[j][i], event[j][i + 1]); sum += f(event[j][i], event[j][i + 1]) * que.size(); } } } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; class dsu { public: vector<int> p; int n; dsu(int _n) : n(_n) { p.resize(n); iota(p.begin(), p.end(), 0); } inline int get(int x) { return (x == p[x] ? x : (p[x] = get(p[x]))); } inline bool unite(int x, int y) { x = get(x); y = get(y); if (x != y) { p[x] = y; return true; } return false; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; dsu d(2 * n); vector<int> x(n - 1), y(n - 1), w(n - 1); for (int i = 0; i < n - 1; ++i) { cin >> x[i] >> y[i] >> w[i]; x[i]--; y[i]--; if (w[i] == -1) continue; if (__builtin_popcount(w[i]) & 1) { d.unite(x[i] + n, y[i]); d.unite(x[i], y[i] + n); } else { d.unite(x[i] + n, y[i] + n); d.unite(x[i], y[i]); } } for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; u--; v--; if (w) { d.unite(u + n, v); d.unite(u, v + n); } else { d.unite(u + n, v + n); d.unite(u, v); } } bool ok = true; for (int i = 0; i < n; ++i) if (d.get(i) == d.get(i + n)) ok = false; if (ok) { cout << "YES" << '\n'; for (int i = 0; i < n; ++i) { if (d.get(i) == d.get(0) || d.get(i + n) == d.get(0)) continue; d.unite(i, 0); d.unite(i + n, n); } for (int i = 0; i < n - 1; ++i) { if (w[i] == -1) { if (d.get(x[i]) == d.get(y[i])) w[i] = 0; else w[i] = 1; } cout << x[i] + 1 << ' ' << y[i] + 1 << ' ' << w[i] << '\n'; } } else cout << "NO" << '\n'; } return 0; }
4
#include<cstdio> int main(){ int n,h,w; scanf("%d%d%d",&n,&h,&w); printf("%d",(n-h+1)*(n-w+1)); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, i, mis = 0, chs = 0, m, c; cin >> n; while (n--) { cin >> m >> c; if (m > c) { mis++; } else if (m < c) { chs++; } } if (mis > chs) { cout << "Mishka" << endl; } else if (chs > mis) { cout << "Chris" << endl; } else { cout << "Friendship is magic!^^" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int inf = 0x7fffffff; int n, k; int head[maxn], e; int d[maxn][3], pos[maxn][2], dist, core; bool vis[maxn]; struct Edge { int u, v; int next, len; } edge[maxn * 2]; void addedge(int u, int v, int l) { edge[e].u = u; edge[e].v = v; edge[e].len = l; edge[e].next = head[u]; head[u] = e++; } void dfs(int s) { int i, v; vis[s] = true; ; d[s][0] = d[s][1] = d[s][2] = 0; pos[s][0] = pos[s][1] = s; for (i = head[s]; i != -1; i = edge[i].next) { v = edge[i].v; if (!vis[v]) { dfs(v); if (d[s][0] < d[v][0] + edge[i].len) { d[s][2] = d[s][1]; d[s][1] = d[s][0]; pos[s][1] = pos[s][0]; d[s][0] = d[v][0] + edge[i].len; pos[s][0] = v; } else if (d[s][1] < d[v][0] + edge[i].len) { d[s][2] = d[s][1]; d[s][1] = d[v][0] + edge[i].len; pos[s][1] = v; } else if (d[s][2] < d[v][0] + edge[i].len) d[s][2] = d[v][0] + edge[i].len; } } } void dfs2(int s) { int i, v; vis[s] = true; if (dist > d[s][0]) { dist = d[s][0]; core = s; } for (i = head[s]; i != -1; i = edge[i].next) { v = edge[i].v; if (!vis[v]) { if (v != pos[s][0]) { if (d[v][0] < d[s][0] + edge[i].len) { d[v][2] = d[v][1]; d[v][1] = d[v][0]; pos[v][1] = pos[v][0]; d[v][0] = d[s][0] + edge[i].len; pos[v][0] = s; } else if (d[v][1] < d[s][0] + edge[i].len) { d[v][2] = d[v][1]; d[v][1] = d[s][0] + edge[i].len; pos[v][1] = s; } else if (d[v][2] < d[s][0] + edge[i].len) d[v][2] = d[s][0] + edge[i].len; } else { if (d[v][0] < d[s][1] + edge[i].len) { d[v][2] = d[v][1]; d[v][1] = d[v][0]; pos[v][1] = pos[v][0]; d[v][0] = d[s][1] + edge[i].len; pos[v][0] = s; } else if (d[v][1] < d[s][1] + edge[i].len) { d[v][2] = d[v][1]; d[v][1] = d[s][1] + edge[i].len; pos[v][1] = s; } else if (d[v][2] < d[s][1] + edge[i].len) d[v][2] = d[s][1] + edge[i].len; } dfs2(v); } } } int main() { scanf("%d%d", &n, &k); int i; e = 0; dist = inf; for (i = 1; i <= n; i++) { head[i] = -1; vis[i] = false; } for (i = 0; i < n - 1; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); addedge(a, b, c); addedge(b, a, c); } dfs(1); for (i = 1; i <= n; i++) vis[i] = false; dfs2(1); int l, r, ans, ex_ans; l = pos[core][0]; r = pos[core][1]; ans = d[core][0]; ex_ans = d[core][2]; k--; for (i = 0; i < k; i++) { if (l == -1 && r == -1) break; if (l == -1 && r != -1) { ans = d[r][1]; if (r == pos[r][1]) r = -1; else r = pos[r][1]; continue; } if (l != -1 && r == -1) { ans = d[l][1]; if (l == pos[l][1]) l = -1; else l = pos[l][1]; continue; } int ansl = max(d[l][1], d[pos[r][0]][1]); int ansr = max(d[r][1], d[pos[l][0]][1]); if (ansl <= ansr) { ans = ansl; ex_ans = max(ex_ans, d[l][2]); if (l == pos[l][1]) l = -1; else l = pos[l][1]; } else { ans = ansr; ex_ans = max(ex_ans, d[r][2]); if (r == pos[r][1]) r = -1; else r = pos[r][1]; } } printf("%d\n", max(ans, ex_ans)); return 0; }
5
#include <bits/stdc++.h> using namespace std; bool visited[50]; long long dx[] = {1, 0, -1, 0}; long long dy[] = {0, 1, 0, -1}; long long n, u, v; vector<long long> adj[50]; vector<pair<long long, long long> > vec(50); set<pair<long long, long long> > points; void dfs(long long u, long long dir, long long pow) { long long sz = adj[u].size(), i = 0, j = 0, temp = 1LL << pow; if (!visited[u] && sz <= 4) { visited[u] = true; while (i < sz) { if (!visited[adj[u][i]]) { while (j == dir) ++j; if (j < 4) { vec[adj[u][i]] = {vec[u].first + dx[j] * temp, vec[u].second + dy[j] * temp}; points.insert(vec[adj[u][i]]); dfs(adj[u][i], (j + 2) % 4, pow - 1), ++j; } } ++i; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); memset(visited, false, sizeof(visited)); cin >> n; for (int i = 1; i < n; ++i) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } vec[1] = {0, 0}; points.insert({0, 0}); dfs(1, -1, 50); if (points.size() == n) { cout << "YES" << "\n"; for (int i = 1; i <= n; ++i) { cout << vec[i].first << " " << vec[i].second << "\n"; } } else { cout << "NO" << "\n"; } cin.clear(); cin.ignore(); cin.get(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int dp[26][26]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string name; cin >> name; int f = name[0] - 'a'; int l = name[name.size() - 1] - 'a'; for (int i = 0; i < 26; i++) if (dp[i][f]) { dp[i][l] = max(dp[i][l], dp[i][f] + (int)name.size()); } dp[f][l] = max(dp[f][l], (int)name.size()); } int ans = 0; for (int i = 0; i < 26; i++) ans = max(ans, dp[i][i]); cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, mod = 1e9 + 7; int x[N]; long long powmod(long long b, long long p) { long long r = 1; for (; p; p >>= 1, b = b * b % mod) if (p & 1) r = r * b % mod; return r; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", x+i); } for (int i = 0; i + 1 < n; ++i) { x[i] = x[i+1] - x[i]; } long long fact = 1; --n; for (int i = 1; i <= n; ++i) fact = fact * i % mod; long long now = 0, ans = 0; for (int i = 0; i < n; ++i) { now = (now + fact * powmod(i+1, mod-2)) % mod; ans = (ans + now * x[i]) % mod; } printf("%lld\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long t, a, b, ans, k; cin >> t; for (int i = 0; i < t; ++i) { cin >> a >> b; ans = abs(a - b) / 10; if (a <= b) { a += ans * 10; } else { b += ans * 10; } if (a != b) { ans++; } cout << ans; cout << endl; } }
1
#include <bits/stdc++.h> using namespace std; const size_t DEEP = 40; int main() { int q; scanf("%d", &q); vector<int> pr(2); vector<vector<double>> f(2); f[1] = vector<double>(DEEP, 1.0); while (q--) { int type, v; scanf("%d%d", &type, &v); if (type == 1) { pr.push_back(v); f.push_back(vector<double>(DEEP, 1.0)); int x = pr.size() - 1; vector<int> prs(1, x); prs.reserve(DEEP + 1); for (size_t it = 1; it <= DEEP; ++it) { if (pr[prs.back()] == 0) break; prs.push_back(pr[prs.back()]); } for (int i = (int)prs.size() - 1; i > 1; --i) { int p = prs[i]; int xx = prs[i - 1]; for (size_t j = 0; j < DEEP; ++j) { double old; if (j == 0) old = 0.5; else old = f[xx][j - 1] + 0.5 * (1 - f[xx][j - 1]); f[p][j] /= old; } } for (size_t i = 0; i + 1 < prs.size(); ++i) { int x = prs[i]; int p = prs[i + 1]; for (size_t j = 0; j < DEEP; ++j) { double nw; if (j == 0) nw = 0.5; else nw = f[x][j - 1] + 0.5 * (1 - f[x][j - 1]); f[p][j] *= nw; } } } else { double ans = 0; for (size_t i = 1; i < DEEP; ++i) { ans += (f[v][i] - f[v][i - 1]) * i; } printf("%.12f\n", (double)ans); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5, inf = 1e9; int n, kk, s, x, k[N][2]; char ch[N]; int fa[N << 1], val[N << 1]; int find(int u) { return fa[u] == u ? fa[u] : fa[u] = find(fa[u]); } void uni(int a, int b) { if (find(a) == find(b)) return; val[find(b)] += val[find(a)]; fa[find(a)] = find(b); } int mix(int a) { return min(val[find(a)], val[find(a + kk)]); }; int main() { scanf("%d%d%s", &n, &kk, ch + 1); for (int i = 1; i <= kk; i++) { scanf("%d", &s); while (s--) { scanf("%d", &x); k[x][bool(k[x][0])] = i; } } for (int i = 1; i <= kk; i++) val[i] = 1, fa[i] = i, fa[i + kk] = i + kk; fa[kk + kk + 1] = kk + kk + 1; val[kk + kk + 1] = inf; int ans = 0; for (int i = 1; i <= n; i++) { if (k[i][0] == 0) goto qwq; if (ch[i] == '1') { if (k[i][1]) { if (find(k[i][0]) == find(k[i][1])) goto qwq; ans -= min(val[find(k[i][1])], val[find(k[i][1] + kk)]); ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]); uni(find(k[i][0]), find(k[i][1])), uni(find(k[i][0] + kk), find(k[i][1] + kk)); ans += min(val[find(k[i][1])], val[find(k[i][1] + kk)]); } else { ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]); uni(find(k[i][0]), find(kk + kk + 1)); ans += val[find(k[i][0] + kk)]; } } else { if (k[i][1]) { if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq; ans -= min(val[find(k[i][1])], val[find(k[i][1] + kk)]); ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]); uni(find(k[i][0]), find(k[i][1] + kk)), uni(find(k[i][1]), find(k[i][0] + kk)); ans += min(val[find(k[i][1] + kk)], val[find(k[i][0] + kk)]); } else { ans -= min(val[find(k[i][0])], val[find(k[i][0] + kk)]); uni(find(k[i][0] + kk), find(kk + kk + 1)); ans += val[find(k[i][0])]; } } qwq: printf("%d\n", ans); } return 0; }
3
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=300005,p=1000000007; int read(){ int f=1,g=0; char ch=getchar(); for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1; for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0'; return f*g; } int n,a[N],h[N]; bool check(int l,int r){ int x=0,y=0; for (int i=l;i<=r;i++){ if ((a[i]<l)||(a[i]>r)) return false; if (a[i]<i) {if (x>a[i]) return false;x=a[i];} else if (a[i]>i) {if (y>a[i]) return false;y=a[i];} } return true; } int main(){ n=read(); for (int i=1;i<=n;i++) a[i]=read(),h[i]=(a[i]==i); for (int i=2;i<n;i++) if (h[i-1]+h[i]+h[i+1]==0){puts("No");return 0;} int l=1,r; while (l<=n){ if (h[l]) {l++;continue;} for (r=l+1;(r<=n)&&(h[r]^h[r-1]);r++); if (h[r-1]) r--; if (!check(l,r-1)) {puts("No");return 0;} l=r; } puts("Yes"); return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } int m; int n; int w[1000]; int mod = 1000000000 + 7; long long st2[1500]; long long fact[1500]; long long step(long long x, long long p) { long long ans = 1; while (p) { if (p & 1) ans = ans * x % mod; x = x * x % mod; p /= 2; } return ans; } int main() { cin >> n >> m; for (int i = 0; i < (m); i++) { int x; cin >> x; w[x - 1] = 1; } fact[0] = st2[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i % mod; st2[i] = st2[i - 1] * 2 % mod; } int f = 0; int c = 0; int k = 0; vector<int> v; long long ans = 1; for (int i = 0; i < (n + 1); i++) { if (i == n || w[i]) { if (c) { v.push_back(c); k += c; if (f & i < n) ans = ans * st2[c - 1] % mod; } c = 0; f = 1; } else c++; } ans = ans * fact[k] % mod; for (int i = 0; i < (((int)(v).size())); i++) ans = ans * step(fact[v[i]], mod - 2) % mod; cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> #define syosu(x) fixed<<setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef pair<double,double> pdd; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<int,P> pip; typedef vector<pip> vip; const int inf=1<<30; const ll INF=1ll<<60; const double pi=acos(-1); const double eps=1e-8; const ll mod=1e9+7; const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1}; const int M=2000; int n,res=0; bitset<M> dpl[M],dpr[M],L[M],R[M]; int main(){ cin>>n; for(int i=1;i<n;i++) for(int j=0;j<i;j++){ char c; cin>>c; if(c=='1') L[i-j][j]=1; else R[i-j][i]=1; } for(int i=0;i<n;i++) dpl[0][i]=dpr[0][i]=1; for(int i=1;i<n;i++) for(int j=0;j<i;j++){ dpl[i]|=(dpl[j]&dpr[i-j-1]&L[i-j])<<(i-j); dpr[i]|=(dpl[j]&dpr[i-j-1]&R[j+1])>>(j+1); } for(int i=0;i<n;i++) if(dpl[i][i]&&dpr[n-i-1][i]) res++; cout<<res<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int INF = 1000000001; const long long int INFLL = 1000000000000000001; const long double EPS = 1e-6; const int MAXN = 500005; bool visited[MAXN]; int n, k; long long int DP[MAXN][2]; vector<pair<int, long long int>> adj[MAXN]; void Dfs(int u) { visited[u] = true; long long int result = 0; vector<pair<long long int, int>> changes; for (const pair<int, long long int>& x : adj[u]) { int v = x.first; long long int c = x.second; if (!visited[v]) { Dfs(v); result += DP[v][0]; changes.push_back(make_pair(DP[v][1] + c - DP[v][0], 1)); changes.push_back(make_pair(DP[v][1] - DP[v][0], 0)); } } sort(changes.begin(), changes.end()); reverse(changes.begin(), changes.end()); DP[u][0] = result; DP[u][1] = result; int psum = 0; for (int i = 0; i < (int)changes.size(); i++) { psum += changes[i].second; if (psum <= k - 1) DP[u][1] += max(0LL, changes[i].first); if (psum <= k) DP[u][0] += max(0LL, changes[i].first); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q; cin >> q; while (q--) { cin >> n >> k; for (int i = 0; i < n - 1; i++) { int u, v; long long int c; cin >> u >> v >> c; u--; v--; adj[u].push_back(make_pair(v, c)); adj[v].push_back(make_pair(u, c)); } Dfs(0); cout << DP[0][0] << "\n"; for (int i = 0; i <= n; i++) { adj[i].clear(); adj[i].shrink_to_fit(); DP[i][0] = 0; DP[i][1] = 0; visited[i] = false; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; struct sort_pred { bool operator()(const pair<int, int> &left, const pair<int, int> &right) { return left.second < right.second; } }; pair<int, int> tacka[222222]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> tacka[i].first >> tacka[i].second; } long long s = 0, brojac = 1, k = 0; sort(tacka, tacka + n); while (k < n - 1) { while (tacka[k].first == tacka[k + brojac].first && tacka[k].second == tacka[k + brojac].second) { if (k + brojac < n - 1) brojac++; else { brojac++; if (k == 0) { if (brojac % 2 == 0) { s = (brojac / 2) * (brojac - 1); cout << s; } else { s = ((brojac - 1) / 2) * brojac; cout << s; } return 0; } break; }; } if (brojac % 2 == 0) s -= (brojac / 2) * (brojac - 1); else s -= ((brojac - 1) / 2) * brojac; k += brojac; brojac = 1; } brojac = 1; k = 0; while (k < n - 1) { while (tacka[k].first == tacka[k + brojac].first) { if (k + brojac < n - 1) brojac++; else { brojac++; break; }; } if (brojac % 2 == 0) s += (brojac / 2) * (brojac - 1); else s += ((brojac - 1) / 2) * brojac; k += brojac; brojac = 1; } brojac = 1; k = 0; sort(tacka, tacka + n, sort_pred()); while (k < n - 1) { while (tacka[k].second == tacka[k + brojac].second) { if (k + brojac < n - 1) brojac++; else { brojac++; break; }; } if (brojac % 2 == 0) s += (brojac / 2) * (brojac - 1); else s += ((brojac - 1) / 2) * brojac; k += brojac; brojac = 1; } cout << s; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct Point { long long x, y; Point(long long x = 0, long long y = 0) : x(x), y(y) {} }; bool operator==(const Point &a, const Point &b) { return a.x == b.x && a.y == b.y; } bool operator!=(const Point &a, const Point &b) { return !(a == b); } Point operator-(const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); } long long det(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } long long dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } istream &operator>>(istream &in, Point &p) { return cin >> p.x >> p.y; } int main() { Point segments[4][2]; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 2; ++j) { cin >> segments[i][j]; } } bool found = false; for (int mask = 0; mask < (1 << 4); ++mask) { for (int i = 0; i < 4; ++i) { if (mask >> i & 1) { swap(segments[i][0], segments[i][1]); } } int permutation[4]; for (int i = 0; i < 4; ++i) { permutation[i] = i; } do { bool check = true; for (int iter = 0; iter < 4; ++iter) { int i = permutation[iter]; if (segments[i][0] == segments[i][1]) { check = false; } int type = -1; if (segments[i][0].x == segments[i][1].x) { type = 0; } if (segments[i][0].y == segments[i][1].y) { type = 1; } if ((iter & 1) != type) { check = false; } int j = permutation[(iter + 1) & 3]; if (segments[i][1] != segments[j][0]) { check = false; } } if (check) { found = true; } } while (next_permutation(permutation, permutation + 4)); for (int i = 0; i < 4; ++i) { if (mask >> i & 1) { swap(segments[i][0], segments[i][1]); } } } puts(found ? "YES" : "NO"); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long vy[4] = {-1, 0, 1, 0}, vx[4] = {0, 1, 0, -1}, inf = 1e18; const string ky = "URDL"; long long n, m, nn, yk[26], a[2069][2069], dp[2069][2069]; bitset<2069> vtd[2069], vtd2[2069]; pair<long long, long long> ca[4000069]; int main() { long long t, rr, i, j, r, k, y, x, yy, xx; pair<long long, pair<long long, long long>> mxe; char ch; for (i = 0; i < 4; i++) { yk[ky[i] - 'A'] = i; } scanf("%lld", &t); for (rr = 0; rr < t; rr++) { scanf("%lld%lld", &n, &m); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { scanf(" %c", &ch); a[i][j] = yk[ch - 'A']; vtd[i][j] = 0; } } mxe = {-inf, {-1, -1}}; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (!vtd[i][j]) { nn = 0; for (y = i, x = j; 1; y = yy, x = xx) { nn++; ca[nn] = {y, x}; if (vtd[y][x]) { break; } vtd[y][x] = 1; vtd2[y][x] = 1; yy = min(max(y + vy[a[y][x]], 1ll), n); xx = min(max(x + vx[a[y][x]], 1ll), m); } if (!vtd2[y][x]) { for (r = nn - 1; r; r--) { y = ca[r].first; x = ca[r].second; yy = ca[r + 1].first; xx = ca[r + 1].second; dp[y][x] = dp[yy][xx] + 1; } } else { for (r = 1; ca[r] != ca[nn]; r++) ; k = r; for (; r <= nn; r++) { y = ca[r].first; x = ca[r].second; dp[y][x] = nn - k; } for (r = k - 1; r; r--) { y = ca[r].first; x = ca[r].second; yy = ca[r + 1].first; xx = ca[r + 1].second; dp[y][x] = dp[yy][xx] + 1; } } for (r = 1; r <= nn; r++) { y = ca[r].first; x = ca[r].second; vtd2[y][x] = 0; } } mxe = max(mxe, {dp[i][j], {i, j}}); } } printf("%lld %lld %lld\n", mxe.second.first, mxe.second.second, mxe.first); } }
6
#include<iostream> #include<string> int main(void){ std::string s; int m,h; while(std::cin>>s){ if(s=="-")break; std::cin>>m; while(m-->0){ std::cin>>h; s=s.substr(h)+s.substr(0,h); } std::cout<<s<<std::endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t-- > 0) { int a, b, c, r; cin >> a >> b >> c >> r; if (a > b) swap(a, b); cout << b - a - max(0, (min(b, c + r) - max(a, c - r))) << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int mat[505][505]; int pre[505][505]; int dp[505][505]; int tot[505]; vector<pair<int, int> > comp[505]; int n, m, K; int maxi = 1e9; int doit(int pos, int left) { if (left < 0) return 1e9; if (pos == n + 1) return 0; if (dp[pos][left] != -1) return dp[pos][left]; int res = maxi; for (int i = (int)comp[pos].size() - 1; i >= 0; --i) { int per = comp[pos][i].first; int dur = comp[pos][i].second; res = min(res, doit(pos + 1, left - tot[pos] + per) + dur); } dp[pos][left] = res; return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> K; int ele; string s; memset(dp, -1, sizeof dp); for (int i = 1; i <= n; ++i) { cin >> s; int sum = 0; int temp; for (int j = 1; j <= m; ++j) { ele = s[j - 1] - '0'; sum += ele; mat[i][j] = sum; pre[i][j] = 1000; } tot[i] = sum; pre[i][0] = 0; for (int j = 1; j <= m; ++j) { for (int k = j; k <= m; ++k) { temp = mat[i][k] - mat[i][j - 1]; pre[i][temp] = min(pre[i][temp], k - j + 1); } } for (int j = 0; j <= m; ++j) { if (pre[i][j] != 1000) { comp[i].push_back({j, pre[i][j]}); } } } int res = doit(1, K); if (res == maxi) res = -1; cout << res << "\n"; return 0; }
4
#include <bits/stdc++.h> int main() { puts("2"); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n, m, i, j, k, t, ans; vector<long long> f; int main() { ios_base::sync_with_stdio(0); static const size_t npos = -1; cin >> n >> k; for (i = 1; i <= n; i++) { cin >> j; f.push_back(j); } sort(f.rbegin(), f.rend()); for (i = 0; i < n; i += k) { ans += (f[i] - 1) * 2; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MaxN = 25; int a[MaxN], sum[1 << 15]; int dp[MaxN][MaxN][1 << 15]; int n, leaf, x; bool f(int u, int leaf, int mask) { if (leaf < 0) return false; if (u == 0 && !leaf) return true; if (u == 0) return false; int &r = dp[u][leaf][mask]; if (r != -1) return r; r = false; for (int i = 0; i < (1 << (u - 1)); i++) if (!(i & mask) && sum[i] <= a[u] - 1 && __builtin_popcount(i) + a[u] - (sum[i] + 1) >= 2) r = max(r, (int)f(u - 1, leaf - (a[u] - (sum[i] + 1)), mask | i)); return r; } int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; int s = 0; memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { cin >> x; if (x == 1) ++leaf; else a[++s] = x; } if (n == 1 && leaf == 1) { cout << "YES" << endl; return 0; } if (leaf < n / 2) { cout << "NO" << endl; return 0; } n = s; sort(a + 1, a + 1 + n); for (int i = 0; i < (1 << n); i++) for (int j = 1; j <= n; j++) if ((1 << (j - 1) & i)) sum[i] += a[j]; cout << (f(n, leaf, 0) ? "YES" : "NO") << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int filter[3000000]; int prime[3000000]; vector<int> factors[2000010]; int n, m; vector<int> edgev[100010]; int value[100010]; int ans[100010]; int depth[100010]; vector<int> calfactor(int num) { vector<int> resu; while (filter[num]) { resu.push_back(filter[num]); num /= filter[num]; } resu.push_back(num); sort(resu.begin(), resu.end()); resu.erase(unique(resu.begin(), resu.end()), resu.end()); return resu; } void dfs(int cnt, int father) { depth[cnt] = depth[father] + 1; vector<int> cf = calfactor(value[cnt]); int cntans = -1; for (int i = 0; i < (int)cf.size(); i++) { if (cf[i] == 1) continue; if (factors[cf[i]].size()) { int temp = factors[cf[i]][factors[cf[i]].size() - 1]; if (cntans == -1 || depth[temp] > depth[cntans]) cntans = temp; } factors[cf[i]].push_back(cnt); } ans[cnt] = cntans; for (int i = 0; i < (int)edgev[cnt].size(); i++) { int v = edgev[cnt][i]; if (v == father) continue; dfs(v, cnt); } for (int i = 0; i < (int)cf.size(); i++) factors[cf[i]].pop_back(); } int main() { prime[0] = 0; for (int i = 2; i <= 2000000; i++) { if (!filter[i]) prime[++prime[0]] = i; for (int j = 1; j <= prime[0] && prime[j] * i <= 2000000; j++) { filter[prime[j] * i] = prime[j]; if (i % prime[j] == 0) break; } } scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &value[i]); for (int i = 1; i <= n - 1; i++) { int u, v; scanf("%d%d", &u, &v); edgev[u].push_back(v); edgev[v].push_back(u); } depth[0] = 0; dfs(1, 0); for (int i = 1; i <= m; i++) { int ope; scanf("%d", &ope); if (ope == 1) { int v; scanf("%d", &v); printf("%d\n", ans[v]); } else { int v, w; scanf("%d%d", &v, &w); value[v] = w; dfs(1, 0); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long n, m; long long power(long long x, long long y, long long m) { if (y == 1) return x % m; else if (y % 2) return (x * power(x, y - 1, m)) % m; long long val = power(x, y / 2, m) % m; return val * val; } int main() { cin >> n >> m; cout << (power(3, n, m) - 1 + m) % m << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; int n, fa[maxn + 2], cont[maxn + 2]; int mx1[maxn + 2], mx2[maxn + 2], diameter[maxn + 2]; bool vist[maxn + 2]; vector<int> G[maxn + 2]; vector<long long> dist[maxn + 2]; map<pair<int, int>, double> Map; void dfs1(int x, int pre, int col) { vist[x] = 1, fa[x] = col; int aa[3]; for (auto nx : G[x]) { if (nx == pre) continue; dfs1(nx, x, col); aa[0] = mx1[nx] + 1, aa[1] = mx1[x], aa[2] = mx2[x]; sort(aa, aa + 3); mx1[x] = aa[2], mx2[x] = aa[1]; } } void dfs2(int x, int pre, vector<long long> &ds, int di) { ds.push_back(max(di, mx1[x])); for (auto nx : G[x]) { if (nx == pre) continue; if (mx1[nx] + 1 == mx1[x]) dfs2(nx, x, ds, max(di, mx2[x]) + 1); else dfs2(nx, x, ds, max(di, mx1[x]) + 1); } } int BinSearch(vector<long long> &arr, long long key) { int l = 1, r = arr.size() - 1, res = 0; while (l <= r) { int mid = (l + r) >> 1; if (arr[mid] - arr[mid - 1] < key) res = mid, l = mid + 1; else r = mid - 1; } return res; } int main() { int m, T, u, v; scanf("%d%d%d", &n, &m, &T); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } for (int i = 1; i <= n; ++i) if (!vist[i]) { dfs1(i, 0, i); dfs2(i, 0, dist[i], 0); dist[i].push_back(0); sort(dist[i].begin(), dist[i].end()); diameter[i] = dist[i][dist[i].size() - 1]; for (int k = 1; k < dist[i].size(); ++k) dist[i][k] += dist[i][k - 1]; } while (T--) { scanf("%d%d", &u, &v); u = fa[u], v = fa[v]; if (u == v) { printf("%d\n", -1); continue; } if (Map.count(make_pair(u, v))) { printf("%f\n", Map[make_pair(u, v)]); continue; } if (dist[u].size() > dist[v].size()) swap(u, v); long long mxd = max(diameter[u], diameter[v]), sum = 0; for (int i = 1; i < dist[u].size(); ++i) { int index = BinSearch(dist[v], mxd - (dist[u][i] - dist[u][i - 1]) - 1); sum += (dist[v][dist[v].size() - 1] - dist[v][index]); sum += (dist[u][i] - dist[u][i - 1] + 1) * (dist[v].size() - 1 - index); sum += mxd * index; } double ans = double(sum) / (dist[u].size() - 1) / (dist[v].size() - 1); Map[make_pair(u, v)] = Map[make_pair(v, u)] = ans; printf("%.10f\n", ans); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int BASE = 100002; int N, M, K; set<pair<int, int> > S1[300002], S2[300002]; set<pair<int, int> > S; void add(int x, int y) { S.insert(make_pair(x, y)); S1[BASE + x - y].insert(make_pair(x, y)); S2[x + y].insert(make_pair(x, y)); } int block(int x, int y) { return (S.find(make_pair(x, y)) != S.end()); } int myabs(int x) { return (x < 0 ? -x : x); } int main() { cin.sync_with_stdio(false); cin >> N >> M >> K; for (int i = 0; i <= N + 1; ++i) { add(i, 0); add(i, M + 1); } for (int i = 0; i <= M + 1; ++i) { add(0, i); add(N + 1, i); } for (int i = 1, px, py; i <= K; ++i) { cin >> px >> py; add(px, py); } int pnx, pny, addx, addy; char wh[4]; cin >> pnx >> pny >> wh; if (wh[0] == 'N' && wh[1] == 'E') addx = -1, addy = 1; else if (wh[0] == 'N' && wh[1] == 'W') addx = -1, addy = -1; else if (wh[0] == 'S' && wh[1] == 'E') addx = 1, addy = 1; else addx = 1, addy = -1; if (addx == -1 && addy == -1) { set<pair<int, int> >::iterator it = S1[BASE + pnx - pny].lower_bound(make_pair(pnx, pny)); --it; pnx = it->first + 1; pny = it->second + 1; if ((block(it->first, it->second + 1) + block(it->first + 1, it->second)) % 2 == 0) addx = -addx, addy = -addy; else if (block(it->first, it->second + 1)) { addx = -addx; --pny; } else { addy = -addy; --pnx; } } else if (addx == 1 && addy == 1) { set<pair<int, int> >::iterator it = S1[BASE + pnx - pny].lower_bound(make_pair(pnx, pny)); pnx = it->first - 1; pny = it->second - 1; if ((block(it->first, it->second - 1) + block(it->first - 1, it->second)) % 2 == 0) addx = -addx, addy = -addy; else if (block(it->first, it->second - 1)) { addx = -addx; ++pny; } else { addy = -addy; ++pnx; } } else if (addx == -1 && addy == 1) { set<pair<int, int> >::iterator it = S2[pnx + pny].lower_bound(make_pair(pnx, pny)); --it; pnx = it->first + 1; pny = it->second - 1; if ((block(it->first, it->second - 1) + block(it->first + 1, it->second)) % 2 == 0) addx = -addx, addy = -addy; else if (block(it->first, it->second - 1)) { addx = -addx; ++pny; } else { addy = -addy; --pnx; } } else { set<pair<int, int> >::iterator it = S2[pnx + pny].lower_bound(make_pair(pnx, pny)); pnx = it->first - 1; pny = it->second + 1; if ((block(it->first, it->second + 1) + block(it->first - 1, it->second)) % 2 == 0) addx = -addx, addy = -addy; else if (block(it->first, it->second + 1)) { addx = -addx; --pny; } else { addy = -addy; ++pnx; } } int ix = pnx, iy = pny, iax = addx, iay = addy; long long result = 0; bool fnow = false, diff = false; while (true) { if (pnx == ix && pny == iy && addx == iax && addy == iay && fnow) break; if (addx == -1 && addy == -1) { set<pair<int, int> >::iterator it = S1[BASE + pnx - pny].lower_bound(make_pair(pnx, pny)); --it; result += myabs(it->first - pnx); pnx = it->first + 1; pny = it->second + 1; if ((block(it->first, it->second + 1) + block(it->first + 1, it->second)) % 2 == 0) { addx = -addx, addy = -addy; diff = true; } else if (block(it->first, it->second + 1)) { addx = -addx; --pny; } else { addy = -addy; --pnx; } } else if (addx == 1 && addy == 1) { set<pair<int, int> >::iterator it = S1[BASE + pnx - pny].lower_bound(make_pair(pnx, pny)); result += myabs(it->first - pnx); pnx = it->first - 1; pny = it->second - 1; if ((block(it->first, it->second - 1) + block(it->first - 1, it->second)) % 2 == 0) { addx = -addx, addy = -addy; diff = true; } else if (block(it->first, it->second - 1)) { addx = -addx; ++pny; } else { addy = -addy; ++pnx; } } else if (addx == -1 && addy == 1) { set<pair<int, int> >::iterator it = S2[pnx + pny].lower_bound(make_pair(pnx, pny)); --it; result += myabs(it->first - pnx); pnx = it->first + 1; pny = it->second - 1; if ((block(it->first, it->second - 1) + block(it->first + 1, it->second)) % 2 == 0) { addx = -addx, addy = -addy; diff = true; } else if (block(it->first, it->second - 1)) { addx = -addx; ++pny; } else { addy = -addy; --pnx; } } else { set<pair<int, int> >::iterator it = S2[pnx + pny].lower_bound(make_pair(pnx, pny)); result += myabs(it->first - pnx); pnx = it->first - 1; pny = it->second + 1; if ((block(it->first, it->second + 1) + block(it->first - 1, it->second)) % 2 == 0) { addx = -addx, addy = -addy; diff = true; } else if (block(it->first, it->second + 1)) { addx = -addx; --pny; } else { addy = -addy; ++pnx; } } fnow = true; } cout << result / (diff ? 2 : 1) << '\n'; }
5
#include <bits/stdc++.h> using namespace std; int N, M; char pc, c, x; int main(int argc, const char* argv[]) { cin >> N >> M; pc = '.'; for (int n = 0; n < N; n++) { cin >> c; if (c == pc) { cout << "NO" << endl; return 0; } for (int m = 1; m < M; m++) { cin >> x; if (x != c) { cout << "NO" << endl; return 0; } } pc = c; } cout << "YES" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m, s, t, tot, id, J, S; long long ans, tmp, all; int fa[200005][25]; int rmax[200005][25]; int head[200005]; int dep[200005]; int f[200005]; int flag[200005]; struct edge { int vet, next, len, id; } E[400005]; int a[200005]; int b[200005]; struct data { int x, y, z, w, id; } c[200005]; bool cmp(const data &x, const data &y) { return x.z < y.z; } int getfather(int x) { if (x != f[x]) f[x] = getfather(f[x]); return f[x]; } void dfs(int u, int dad) { for (int e = head[u]; e != -1; e = E[e].next) if (e != dad) { dep[E[e].vet] = dep[u] + 1; fa[E[e].vet][0] = u; rmax[E[e].vet][0] = E[e].id; dfs(E[e].vet, e ^ 1); } } void add(int u, int v, int c, int id) { E[++tot] = (edge){v, head[u], c, id}; head[u] = tot; } void init() { for (int j = 1; j <= 20; j++) { for (int i = 1; i <= n; i++) { fa[i][j] = fa[fa[i][j - 1]][j - 1]; int x = c[rmax[i][j - 1]].z; int y = c[rmax[fa[i][j - 1]][j - 1]].z; if (x > y) rmax[i][j] = rmax[i][j - 1]; else rmax[i][j] = rmax[fa[i][j - 1]][j - 1]; } } } int ask(int x, int y) { int ret = 0, j = 20; if (dep[y] < dep[x]) swap(x, y); while (j >= 0) { if (dep[fa[y][j]] >= dep[x]) { if (c[rmax[y][j]].z > c[ret].z) ret = rmax[y][j]; y = fa[y][j]; } j--; } j = 20; while (j >= 0) { if (fa[x][j] != fa[y][j]) { if (c[rmax[x][j]].z > c[ret].z) ret = rmax[x][j]; if (c[rmax[y][j]].z > c[ret].z) ret = rmax[y][j]; x = fa[x][j]; y = fa[y][j]; } j--; } if (x != y) { if (c[rmax[x][0]].z > c[ret].z) ret = rmax[x][0]; if (c[rmax[y][0]].z > c[ret].z) ret = rmax[y][0]; } return ret; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) f[i] = i; memset(head, -1, sizeof(head)); tot = -1; for (int i = 1; i <= m; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); for (int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); c[i] = (data){x, y, a[i], 0, i}; } scanf("%d", &s); sort(c + 1, c + 1 + m, cmp); for (int i = 1; i <= m; i++) { int r1 = getfather(c[i].x); int r2 = getfather(c[i].y); if (r1 != r2) { flag[i] = 1; add(c[i].x, c[i].y, c[i].z, i); add(c[i].y, c[i].x, c[i].z, i); f[r2] = r1; all += c[i].z; c[i].w = 1; } } ans = all; dfs(1, -1); init(); for (int i = 1; i <= m; i++) { if (flag[i] == 1) { long long tmp = all - s / b[c[i].id]; if (tmp < ans) { ans = tmp; J = i; } } else { int t = ask(c[i].x, c[i].y); long long tmp = all - c[t].z + c[i].z - s / b[c[i].id]; if (tmp < ans) { ans = tmp; J = i; S = t; } } } printf("%lld\n", ans); for (int i = 1; i <= m; i++) { if (i == J) printf("%d %d\n", c[i].id, c[i].z - s / b[c[i].id]); else if (c[i].w == 1 && i != S) printf("%d %d\n", c[i].id, c[i].z); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, t, k; int cost[22][22]; bool used[22]; int numFind; int anss[200005]; priority_queue<int> pq; void back(int num, int get) { if (num == n) { if (numFind >= t) { if (!pq.empty() && get < pq.top()) { pq.pop(); pq.push(get); } } else { pq.push(get); numFind++; } return; } back(num + 1, get); for (int i = 0; i < n; i++) { if (!used[i] && cost[num][i]) { if (numFind < t || get + cost[num][i] < pq.top()) { used[i] = true; back(num + 1, get + cost[num][i]); used[i] = false; } } } } int main() { cin >> n >> k >> t; int tmp1, tmp2; for (int i = 0; i < k; i++) { cin >> tmp1 >> tmp2; cin >> cost[tmp1 - 1][tmp2 - 1]; } back(0, 0); int i; for (i = 0; i < numFind; i++) { anss[i] = pq.top(); pq.pop(); } cout << anss[t - i] << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long binPow(long long a, long long b) { long long res = 1; while (b) { if (b % 2) res = (res * a) % mod; a = (a * a) % mod; b /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, a, b; cin >> n >> a >> b; if (a * b >= 6 * n) { cout << a * b << endl << a << ' ' << b; return 0; } for (long long i = 6 * n; i <= 6 * n + min(a, b); i++) { for (long long j = min(a, b); j <= sqrt(i); j++) { if (i % j == 0) { if (i / j >= max(a, b)) { cout << i << endl; if (min(a, b) == a) cout << j << ' ' << i / j; else cout << i / j << ' ' << j; return 0; } } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; void print(priority_queue<int, vector<int>, less<int> > pq, int res) { int cnt = 0; while (pq.size() && res > 0) { int u = pq.top(); res -= u; pq.pop(); ++cnt; } cout << cnt << " "; return; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); int n; cin >> n; int hi; cin >> hi; int sum = 0; priority_queue<int, vector<int>, less<int> > pq; for (int i = 0; i < n; i++) { int val; cin >> val; sum += val; int res = max(0, sum - hi); print(pq, res); pq.push(val); } return 0; }
3
#include <bits/stdc++.h> long long phi(long long a) { long long ans; ans = a; for (long long i = 2; i * i <= a; i++) { bool f(0); while (!(a % i) && a != 1) f = 1, a /= i; if (f) ans -= ans / i; } if (a != 1) ans -= ans / a; return ans; } bool pd; long long pow(long long a, long long b, long long m) { pd = 0; long long ans(1); while (b) { if (b & 1) { if (ans * a >= m) pd = 1; ans = ans * a % m; } if (a * a >= m && b != 1) pd = 1; a = a * a % m; b >>= 1; } return ans; } long long m, m1[1000000], n, a[1000000], k; long long tow(long long l, long long r, long long i) { if (l == r) { pd = 0; if (a[l] >= m1[i]) pd = 1; return a[l] % m1[i]; } if (m1[i] == 1) { pd = 1; return 0; } long long v1 = tow(l + 1, r, i + 1); if (pd) v1 += m1[i + 1]; return pow(a[l], v1, m1[i]); } int main() { scanf("%lld %lld", &n, &m); m1[1] = m; for (int i = 2; i <= n; i++) { m1[i] = phi(m1[i - 1]); } for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); scanf("%lld", &k); while (k--) { long long l, r; scanf("%lld %lld", &l, &r); printf("%lld\n", tow(l, r, 1)); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, x, t; cin >> n >> x >> t; cout << ((n+x-1)/x)*t << endl; }
0
#include <bits/stdc++.h> using namespace std; int vis[1001][1001]; char str[101]; int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int main() { int len, x = 500, y = 500, flag = 0; scanf("%s", str); len = strlen(str); vis[x][y] = 1; for (int i = 0; i < len; i++) { if (str[i] == 'L') { x--; if (vis[x][y]) { printf("BUG\n"); return 0; } vis[x][y] = 1; } else if (str[i] == 'U') { y--; if (vis[x][y]) { printf("BUG\n"); return 0; } vis[x][y] = 1; } else if (str[i] == 'R') { x++; if (vis[x][y]) { printf("BUG\n"); return 0; } vis[x][y] = 1; } else if (str[i] == 'D') { y++; if (vis[x][y]) { printf("BUG\n"); return 0; } vis[x][y] = 1; } int cnt = 0; for (int i = 0; i < 4; i++) { int xx = x + dir[i][0]; int yy = y + dir[i][1]; if (vis[xx][yy]) cnt++; } if (cnt != 1) { flag = 1; break; } } if (!flag) printf("OK\n"); else printf("BUG\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n; int ans; int tr[N]; int lowbit(int x) { return x & -x; } int sum(int x) { int res = 0; for (int i = x; i; i -= lowbit(i)) res += tr[i]; return res; } void init() { for (int i = 0; i <= n; i++) tr[i] = 0; } void add(int x, int c) { for (int i = x; i <= n; i += lowbit(i)) tr[i] += c; } void solve() { scanf("%d", &n); init(); vector<int> a, b; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a.push_back(x); } b = a; sort(b.begin(), b.end()); b.erase(unique(b.begin(), b.end()), b.end()); for (auto &it : a) it = lower_bound(b.begin(), b.end(), it) - b.begin() + 1; add(a[0], 1); long long ans = 0; for (int i = 1; i <= n - 1; i++) { int c = sum(a[i] - 1); int d = sum(n) - sum(a[i]); ans += min(c, d); add(a[i], 1); } cout << ans << endl; } int main() { int T; cin >> T; while (T--) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename T1> void dbg_pr(vector<T1> V, int add = 0, int start = -1, int end = -1) { cout << "{"; if (start < 0) start = 0; if (end < 0) end = V.size() - 1; for (int i = start; i <= end; i++) { cout << V[i] + add << ((i == end) ? "}" : " "); } } void dbg_pr(string V) { cout << "\"" << V << "\""; } template <typename T1> void dbg_pr(T1 V) { cout << V; } template <typename T1, typename T2> void dbg_pr(map<T1, T2> M) { cout << "{"; for (const auto m : M) { dbg_pr(m.first); cout << ":"; dbg_pr(m.second); cout << ","; } cout << "}"; } void debug_out() { cout << "\033[0m\n"; } template <typename T1, typename... T2> void debug_out(T1 A, T2... B) { cout << " "; dbg_pr(A); debug_out(B...); } int test = 1; void setupFastCin() { ios::sync_with_stdio(false); cin.tie(0); } template <typename T1> T1 chmin(T1 &x, const T1 v) { return x = min(x, v); } template <typename T1> T1 chmax(T1 &x, const T1 v) { return x = max(x, v); } template <typename T1> void pr(vector<T1> V, int add = 0, int start = -1, int end = -1) { if (start < 0) start = 0; if (end < 0) end = V.size() - 1; for (int i = start; i <= end; i++) { cout << V[i] + add << ((i == end) ? "\n" : " "); } } template <typename T1> T1 gcd(const T1 &a, const T1 &b) { if (a == 0 || b == 0) return a + b; return gcd(b, a % b); } int K; string S; long long DP[11][2000][2]; long long dp(int i, int mask, int k, int comp, long long p) { if (k > K) return LLONG_MAX; if (i == S.size()) { return 0; } if (DP[i][mask][comp] != -1) return DP[i][mask][comp]; int md = S[i] - '0'; long long sol = LLONG_MAX; for (int d = 0; d < 10; d++) { if (comp == 0 && d < md) continue; int newMask = mask; int newK = k; if ((mask & (1 << d)) == 0) { newMask += (1 << d); newK++; } if (newK > K) continue; int newC = comp; if (comp == 0) newC = d > md ? 1 : 0; long long s = dp(i + 1, newMask, newK, newC, p / 10); if (s == LLONG_MAX) continue; chmin(sol, d * p + s); break; } return DP[i][mask][comp] = sol; } void solve() { for (int i = 0; i < 11; i++) for (int j = 0; j < 1025; j++) DP[i][j][0] = DP[i][j][1] = -1; cin >> S >> K; long long p = pow(10ll, S.size() - 1); { cout << dp(0, 0, 0, 0, p) << "\n"; return; }; } int main() { setupFastCin(); int T = 1; cin >> T; for (int t = 0; t < T; t++) { solve(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int A[100010]; struct query { int t, x, c; int get_index(int i) { assert(1 <= i && i <= length()); if (t == 1) { return x; } else { i = (i - 1) % x + 1; return A[i]; } } int length() { return ((t == 1) ? 1 : (1 * x * c)); } }; query Q[100010]; int ans[100010]; int main() { int M, N; cin >> M; for (int i = 0; i < (M); ++i) { scanf("%d%d", &Q[i].t, &Q[i].x); if (Q[i].t == 2) scanf("%d", &Q[i].c); } cin >> N; long long last = 0; int last_q = -1; long long v = 0; int i = 0; cin >> v; while (i < N) { assert(last < v); while ((last + Q[(last_q + 1)].length()) < v) { int l = Q[(last_q + 1)].length(); int k = 1; for (k = 1; k <= l && last < 100000; ++k, ++last) { A[last + 1] = Q[(last_q + 1)].get_index(k); } last += (l - k + 1); ++last_q; } assert(last + Q[(last_q + 1)].length() >= v); while (last + Q[(last_q + 1)].length() >= v) { long long dist = v - last; ans[i] = Q[(last_q + 1)].get_index(dist); i++; if (i < N) { cin >> v; } else { break; } } } bool sp = false; for (int i = 0; i < (N); ++i) { assert(ans[i]); if (sp) printf(" "); sp = true; printf("%d", ans[i]); } printf("\n"); }
1
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, w = 0; char ch = getchar(); while (!isdigit(ch)) w |= ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return w ? -x : x; } inline long long ksm(long long x, long long y) { long long m = 1; while (y) { if (y & 1) m = (m * x) % 1000000007; x = (x * x) % 1000000007; y >>= 1; } return m; } long long n, d[100005], g[55][55][55], f[55][55], jc[55], inv[55], c[5], ans; inline long long calc(long long x, long long y) { return jc[x] % 1000000007 * inv[y] % 1000000007 * inv[x - y] % 1000000007; } inline void pre() { g[0][0][0] = 1; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n - i; j++) { if (i == j && i == 0) { continue; } if (i == 0) { for (int k = 2; k < j; k++) { g[0][i][j] = (g[0][i][j] + g[0][i][j - k - 1] * calc(j - 1, k) % 1000000007 * jc[k] % 1000000007 * inv[2] % 1000000007) % 1000000007; } } else { if (i >= 2) g[0][i][j] = (g[0][i][j] + g[0][i - 2][j] * (i - 1) % 1000000007) % 1000000007; if (j >= 1) { g[0][i][j] = (g[0][i][j] + g[0][i][j - 1] * j % 1000000007) % 1000000007; } } } } for (int i = 1; i < n; i++) for (int j = 1; j < n - i; j++) for (int k = 0; k <= j; k++) { int l = j - k; if (l >= 1) g[i][l][k] = (g[i][l][k] + g[i - 1][l - 1][k] * l % 1000000007) % 1000000007; if (k >= 1) g[i][l][k] = (g[i][l][k] + g[i - 1][l + 1][k - 1] * k % 1000000007) % 1000000007; } } int main() { n = read(); jc[0] = inv[0] = jc[1] = 1; for (int i = 2; i <= n; i++) jc[i] = jc[i - 1] * i % 1000000007; inv[n] = ksm(jc[n], 1000000007 - 2) % 1000000007; for (int i = n - 1; i >= 1; i--) inv[i] = inv[i + 1] * (i + 1) % 1000000007; for (int i = 1; i <= n; i++) d[i] = read(); pre(); f[d[1] + 1][d[1]] = 1; for (int i = d[1] + 2; i <= n; i++) { for (int j = 1; j <= i - d[1] - 1; j++) { c[0] = c[1] = 0; for (int k = 1; k < i - j; k++) { c[d[i - j - k + 1] == 3]++; f[i][j] = (f[i][j] + f[i - j][k] * g[j][c[0]][c[1]] % 1000000007) % 1000000007; } } } c[0] = c[1] = 0; for (int i = 1; i < n; i++) { c[d[n - i + 1] == 3]++; ans = (ans + f[n][i] * g[0][c[0]][c[1]] % 1000000007) % 1000000007; } ans %= 1000000007; printf("%lld", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int inf = 0x7fffffff; const double eps = 1e-12; const int mod = 1e9 + 7; const int maxn = 300005; using namespace std; int c[401]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> c[i]; int ans = -1; for (int i = 1; i < n; i++) { for (int j = i; j < n; j++) { int p = c[i - 1] - 1, cnt = 1, w = c[j]; for (int k = i; k <= j; k++) { cnt += p / c[k]; w += p / c[k] * c[k]; p %= c[k]; } p = w; for (int k = 0; k < n; k++) { cnt -= p / c[k]; p %= c[k]; } if (cnt < 0 && (ans == -1 || w < ans)) ans = w; } } printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int a[200005]; int b[200005]; bool ava[200005]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; ava[a[i]] = true; } for (int i = 0; i < n; ++i) { cin >> b[i]; } bool strt = true; int ind = -1; for (int i = n - 1; i >= 0; --i) { if (b[i] == 1) { ind = i; break; } if (b[i] != b[i - 1] + 1) { strt = false; break; } } if (ava[1]) { strt = true; } if (strt) { if (b[0] == 1) { cout << "0\n"; return 0; } int cur = 0; bool wrk = true; for (int x = (ind == -1 ? 1 : b[n - 1] + 1); x <= n; ++x) { if (!ava[x]) { wrk = false; break; } ava[b[cur]] = true; ++cur; } if (wrk) { if (ind == -1) { cout << n << "\n"; } else { cout << n - b[n - 1] << "\n"; } return 0; } } int diff = 0; for (int i = 0; i < n; ++i) { if (b[i] != 0) { diff = max(diff, i + 1 - b[i] + 1); } } cout << diff + n << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<int> v(2); int main() { ::ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; int a, b; cin >> n; for (int i = 0; i < n; ++i) { cin >> a >> b; if (a > 0) v[0]++; else v[1]++; } if (v[0] == 0 || v[0] == 1 || v[1] == 1 || v[1] == 0) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
1