solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; double start_moment = 0; double get_runtime() { return 1.0 * clock() / CLOCKS_PER_SEC; } void reset_timer() { start_moment = get_runtime(); } double timer_time() { return get_runtime() - start_moment; } void runtime() { cout << fixed << setprecision(5) << get_runtime() << '\n'; } template <class T> void read(vector<T> &a, long long n) { T x; a.clear(); for (long long i = 0; i < n; i++) { cin >> x; a.push_back(x); } } template <class T> void write(vector<T> &a) { for (T x : a) cout << x << ' '; cout << '\n'; } const long long INF = 1LL * 1001 * 1001 * 1001 * 1001 * 1001 * 1001; int T; long long a, b, c, d; long long first(long long x) { long long res = x * a; res -= (long long)x * c * b; long long cnt = min((long long)x - 1, (long long)c / d); res += (long long)c * b; res += (long long)c * cnt * b; res -= cnt * (cnt + 1) / 2 * d * b; return res; } int main() { ios_base::sync_with_stdio(0); cin >> T; while (T--) { cin >> a >> b >> c >> d; long long l = 1; long long r = 1000100; long long mx = -INF; while (r - l > 2) { long long m1 = l + (r - l) / 3; long long m2 = r - (r - l) / 3; if (first(m1) > first(m2)) { r = m2; } else { l = m1; } } for (long long i = l; i <= r; i++) { mx = max(mx, first(i)); } if (first(1000101) > mx) { cout << -1 << '\n'; } else { cout << mx << '\n'; } } }
5
#include <bits/stdc++.h> using namespace std; int d[300005], target[600005], pre[600005], last[300005], tot = 1, ans[300005]; bool visited[300005]; void add(int x, int y) { target[++tot] = y; pre[tot] = last[x]; last[x] = tot; } void dfs(int x) { visited[x] = 1; for (int i = last[x]; i; i = pre[i]) { int tar = target[i]; if (visited[tar]) continue; dfs(tar); if (d[tar]) ans[++ans[0]] = (i >> 1), d[x] ^= 1, d[tar] ^= 1; } } int main() { int n, m, sum = 0, sum2 = 0, x, y; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &d[i]); if (d[i] == -1) sum2++; else sum ^= d[i]; } if (sum == 1 && sum2 == 0) { puts("-1"); return 0; } for (int i = 1; i <= n; i++) if (d[i] == -1) { if (sum == 1 && sum2) d[i] = 1, sum2 = 0; else d[i] = 0; } for (int i = 1; i <= m; i++) scanf("%d%d", &x, &y), add(x, y), add(y, x); dfs(1); sort(ans + 1, ans + ans[0] + 1); printf("%d\n", ans[0]); for (int i = 1; i <= ans[0]; i++) printf("%d\n", ans[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; long long a[1010000], b[1010000], tms[1010000], C[1010000]; const long long mod = 1000000007; long long qpow(long long x, long long y, const long long& k) { long long t = 1; while (y) { if (y & 1) { t = t * x % k; } x = x * x % k; y >>= 1; } return t; } long long inv(long long x) { return qpow(x, mod - 2, mod); } signed main() { std::ios::sync_with_stdio(false); long long n, l, k; cin >> n >> l >> k; long long tk = l / n; for (long long i = 1; i <= n; ++i) { cin >> a[i]; b[i] = a[i]; } long long h = 0; sort(b + 1, b + n + 1); for (long long i = 1; i <= n; ++i) { if (b[i] != b[i - 1]) b[++h] = b[i]; ++tms[h]; } for (long long i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + h + 1, a[i]) - b; vector<vector<long long>> f(k + 5); for (auto& i : f) i = vector<long long>(h + 5); vector<vector<long long>> lst(k + 5); for (auto& i : lst) i = vector<long long>(h + 5); C[0] = 1; for (long long j = 0; j <= h; ++j) lst[0][j] = 1; for (long long i = 1; i <= min(k, tk); ++i) { for (long long j = 1; j <= h; ++j) { f[i][j] = lst[i - 1][j] * tms[j] % mod; lst[i][j] = (lst[i][j - 1] + f[i][j]) % mod; (f[i][j] *= (tk - i + 1) % mod) %= mod; } } long long rem = l % n; if (rem) { f[k + 1] = vector<long long>(rem + 5); for (long long j = 1; j <= rem; ++j) { for (long long r = 0; r < k; ++r) (f[k + 1][j] += lst[r][a[j]]) %= mod; } } long long ans = 0; for (auto& p : f) { for (auto& q : p) { ans = (ans + q) % mod; } } cout << ans << '\n'; return 0; }
2
#include<bits/stdc++.h> using namespace std; int main(){ string s,v; cin>>s>>v; int ans=INT_MAX; for(int i=0;i<=s.size()-v.size();i++){ int cnt=0; for(int j=i;j<i+v.size();j++){ if(v[j-i]!=s[j]) cnt++; } ans=min(ans,cnt); } cout<<ans<<endl; return 0; }
0
#include<algorithm> #include<iostream> #include<string> #include<vector> #include<queue> using namespace std; int main(){ int n; cin>>n; long long int x; long long int s=0; priority_queue<long long int,vector<long long int>,greater<long long int>>q; for(int i=0;i<n;i++){ cin>>x; s+=x; q.push(x); while(s<0){ s-=q.top(); q.pop(); } } long long int ans=q.size(); cout<<ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000003; const long long INF = mod * mod; const double eps = 1e-8; const double pi = acos(-1.0); struct query { int l, r; long long d; }; void solve() { int n; cin >> n; vector<long long> s(n), t(n); for (int i = 0; i < n; i++) { cin >> s[i]; } for (int i = 0; i < n; i++) cin >> t[i]; { long long sum = 0; for (int i = 0; i < n; i++) sum += s[i] - t[i]; if (sum != 0) { cout << "NO" << endl; return; } } vector<pair<long long, long long> > a; for (int i = 0; i < n; i++) a.push_back({s[i], i}); sort(a.begin(), a.end()); sort(t.begin(), t.end()); vector<query> ans; stack<pair<long long, long long> > st; for (int i = n - 1; i >= 0; i--) { long long dif = a[i].first - t[i]; if (dif < 0) { while (!st.empty()) { pair<long long, long long> p = st.top(); st.pop(); long long dm = min(-dif, p.first); ans.push_back({(int)a[i].second + 1, (int)p.second + 1, dm}); dif += dm; p.first -= dm; if (p.first > 0) st.push(p); if (dif == 0) break; } if (dif < 0) { cout << "NO" << endl; return; } } else if (dif > 0) { st.push({dif, a[i].second}); } } cout << "YES" << endl; cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].l << " " << ans[i].r << " " << ans[i].d << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main(){ int n, k; cin >> n >> k; vector<int> lig(n); for(int i=0; i<n; ++i) cin >> lig[i]; for(int i=0; i<k; ++i){ vector<int> nlig(n+1,0); for(int j=0; j<n; ++j){ int plus = max(0,j-lig[j]); int mai = min(n,j+lig[j]+1); ++nlig[plus]; --nlig[mai]; } for(int j=1; j<n; ++j) nlig[j] += nlig[j-1]; if(lig == nlig) break; lig = nlig; } for(int i=0; i<n; ++i){ if(i == n-1) cout << lig[i] << endl; else cout << lig[i] << " "; } }
0
#include <bits/stdc++.h> using namespace std; int const mxsz = 509; long long w[mxsz][mxsz]; bool ver[mxsz]; int main() { vector<int> er; vector<long long> res; int n; scanf("%d", &n); er.resize(n); for (int i = 0; i < n; ++i) { int x; for (int j = 0; j < n; ++j) { scanf("%d", &x); w[i][j] = x; } } for (int i = 0; i < n; ++i) scanf("%d", &er[i]); reverse(er.begin(), er.end()); for (int k = 0; k < n; ++k) { er[k]--; ver[er[k]] = 1; long long R = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { w[i][j] = min(w[i][j], w[i][er[k]] + w[er[k]][j]); if (ver[i] && ver[j]) R += w[i][j]; } res.push_back(R); } for (int i = n - 1; i >= 0; --i) printf("%I64d ", res[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const long long int infi = 1e18 + 5; long long int n; long long int sa[N], ea[N], sb[N], eb[N]; bool func() { set<long long int> left, right; vector<pair<long long int, long long int> > v; for (long long int i = 1; i <= n; i++) { v.push_back(make_pair(sa[i], i)); v.push_back(make_pair(ea[i] + 1, -i)); } left.insert(-infi); right.insert(infi); sort(v.begin(), v.end()); for (auto it : v) { if (it.second > 0) { long long int i = it.second; long long int l = max(*left.rbegin(), sb[i]); long long int r = min(*right.begin(), eb[i]); if (l > r) { return false; } left.insert(sb[i]); right.insert(eb[i]); } else { long long int i = -it.second; left.erase(sb[i]); right.erase(eb[i]); } } return true; } int main() { cin >> n; long long int i; for (i = 1; i <= n; i++) { cin >> sa[i] >> ea[i] >> sb[i] >> eb[i]; } if (!func()) { cout << "NO"; return 0; } for (i = 1; i <= n; i++) { swap(sa[i], sb[i]); swap(ea[i], eb[i]); } if (!func()) { cout << "NO"; return 0; } cout << "YES" << "\n"; }
4
#include <bits/stdc++.h> using namespace std; string s, t; int p = 0, q = 0; int lp = 0, lq = 0; int ls, lt, ks, kt; vector<int> a, b; void sw(int a0, int b0) { a.push_back(a0 + lp + 1); b.push_back(b0 + lq + 1); swap(lp, lq); lp += b0 - a0; lq += a0 - b0; } void init() { ls = s.length(), lt = t.length(); ks = 0, kt = 0; for (int i = 0; i < ls - 1; ++i) { if (s[i] != s[i + 1]) ++ks; } for (int i = 0; i < lt - 1; ++i) { if (t[i] != t[i + 1]) ++kt; } } void move_half() { int hks = (ks + 1) / 2, hkt = (kt + 1) / 2; for (int i = 0; i < ls - 1; ++i) { if (s[i] != s[i + 1]) --hks; if (hks == 0) { if (ks > 0) hks = i + 1; break; } } for (int i = 0; i < lt - 1; ++i) { if (t[i] != t[i + 1]) --hkt; if (hkt == 0) { if (kt > 0) hkt = i + 1; break; } } if (s[hks] == t[hkt]) { if ((ks % 2 == 0) || (kt % 2 == 1 && ks >= kt)) { while (s[hks] == s[hks + 1]) ++hks; ++hks; } else { while (t[hkt] == t[hkt + 1]) ++hkt; ++hkt; } } string temp = s.substr(0, hks) + t.substr(hkt, lt - hkt); s = t.substr(0, hkt) + s.substr(hks, ls - hks); t = temp; a.push_back(hks); b.push_back(hkt); init(); return; } int main() { cin >> s >> t; init(); if (abs(ks - kt) >= 2) { move_half(); } while (p < ls - 1 || q < lt - 1) { while (p < ls - 1 && s[p] == s[p + 1]) ++p; while (q < lt - 1 && t[q] == t[q + 1]) ++q; if (p >= ls - 1 && q >= lt - 1) break; char c1 = s[p], c2 = t[q]; if (p == ls || q == lt || c1 != c2) { if (p == ls) --p; if (q == lt) --q; sw(p, q); ++p, ++q; if (ks > 0) --ks; if (kt > 0) --kt; } else { if (q >= lt - 1 || ks > kt) { sw(p, -1); ++p; if (ks > 0) --ks; } else { sw(-1, q); ++q; if (kt > 0) --kt; } } } int n = a.size(); printf("%d\n", n); for (int i = 0; i < n; ++i) { printf("%d %d\n", a[i], b[i]); } return 0; }
6
#include <bits/stdc++.h> using namespace std; long int b[300013]; void Ok() { long int a, c, h, t, i = 0, j = 0, k, l, m, n; long int ans_1, ans_2, ans_3, cnt = 0, Max, Min, flag = 0, temp, sum = 0; double aa, bb, cc, dd, ee, ff, gg; char aaa[113][113], bbb[113], ccc[113], ddd; string spc, in; cin >> t; while (t--) { for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { cin >> aaa[i][j]; } } for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { if (aaa[j][i] == '1') aaa[j][i] = '2'; } } for (i = 1; i <= 9; i++) { for (j = 1; j <= 9; j++) { cout << aaa[i][j]; } cout << endl; } cout << endl; } return; } int main() { Ok(); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long A[100005], P[100005], N, S; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; S += A[i]; } sort(A, A + N); for (int i = 1; i < N; i++) { P[i] = P[i - 1] + (A[i] - A[i - 1]) * (i); S += 2 * P[i]; } long long g = gcd(N, S); N /= g; S /= g; cout << S << " " << N; }
1
#include <bits/stdc++.h> using namespace std; vector<int> raices; void raiz(long long n) { for (int i = 2; i < n; i++) { raices.push_back(i * i); } } long long bun(int n) { if (n == 0) { return 0; } return (2 * bun(n - 1) + 2); } long long paty(int n) { if (n == 0) { return 1; } return (2 * (paty(n - 1)) + 1); } bool puede(string a, string b) { int j = 0; for (int i = 0; i < a.size(); i++) { if (a[i] == b[j]) { j++; if (j == b.size()) { return true; } } } return false; } int main() { int n; cin >> n; cout << n << " "; for (int i = 1; i < n; i++) { cout << i << " "; } }
1
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int eps = 1e-10; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const int maxn = 100005; int n; long long a[maxn]; long long sum; int main(void) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", a + i); a[0] = 0; sum = 0; for (int i = 1; i <= n; i++) { if (a[i] > a[i - 1]) { sum += (n - a[i] + 1) * (a[i] - a[i - 1]); } else if (a[i] < a[i - 1]) { sum += (a[i - 1] - a[i]) * a[i]; } } printf("%lld\n", sum); return 0; }
5
#include<iostream> using namespace std; int main(){ int n; cin>>n; cout<<(n*n*n)<<endl; }
0
#include <bits/stdc++.h> using namespace std; int d[21][21], a[21], b; void DFS() { int x = 0, y = 0; for (int i = 1; i < b; i++) { if (a[i - 1] < a[i] && a[i] > a[i + 1]) x++; else if (a[i - 1] > a[i] && a[i] < a[i + 1]) y++; } if (x - 1 == y) d[b][x]++; if (b == 19) return; if (a[b] != 1) a[++b] = 1, DFS(), b--; if (a[b] != 2) a[++b] = 2, DFS(), b--; if (a[b] != 3) a[++b] = 3, DFS(), b--; if (a[b] != 4) a[++b] = 4, DFS(), b--; if (b <= 3) cout << b << "Done\n"; } int z[21][10] = { {}, {}, {}, {0, 14}, {0, 22}, {0, 16, 70}, {0, 6, 232}, {0, 1, 380, 353}, {0, 0, 396, 1786}, {0, 0, 284, 4500, 1782}, {0, 0, 142, 7414, 12122}, {0, 0, 48, 8849, 41160, 8997}, {0, 0, 10, 8028, 92226, 76878}, {0, 0, 1, 5658, 152140, 328222, 45425}, {0, 0, 0, 3112, 195420, 928494, 467292}, {0, 0, 0, 1322, 201744, 1947581, 2402980, 229347}, {0, 0, 0, 422, 170444, 3213988, 8205152, 2758896}, {0, 0, 0, 96, 118912, 4322956, 20852648, 16594160, 1157954}, {0, 0, 0, 14, 68640, 4846524, 41920210, 66357900, 15946870}, {0, 0, 0, 1, 32632, 4594423, 69183464, 197939352, 109824208, 5846414}, {0, 0, 0, 0, 12628, 3715462, 96046590, 468541040, 503245466, 90700276}}; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; cout << z[n][m]; return 0; a[0] = 1; DFS(); cout << "Done\n"; a[0] = 2; DFS(); cout << "Done\n"; a[0] = 3; DFS(); cout << "Done\n"; a[0] = 4; DFS(); cout << "Done\n"; for (int i = 0; i < 21; i++) for (int j = 0; j < 21; j++) cout << d[i][j] << ",\n"[j == 20]; }
5
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; long long n, m; long long a[N], ans[N]; set<long long> active; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i]; active.insert(i); } cin >> m; while (m--) { long long type; cin >> type; if (type == 1) { long long p, x; cin >> p >> x; auto it = active.lower_bound(p); if (it != active.end()) { long long idx = *it; ans[idx] += x; long long diff = 0; while (idx <= n && ans[idx] >= a[idx]) { diff = ans[idx] - a[idx]; ans[idx + 1] += diff; ans[idx] = a[idx]; active.erase(idx); idx++; } } } else { long long k; cin >> k; cout << ans[k] << "\n"; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<int> record; vector<int> dist; vector<int> deg; vector<vector<int>> edges; int idx; void dfs(int v, int p, int dep){ dist.push_back(dep); int r = record[idx]; if(0 <= p){ r--; edges.push_back({p}); deg[p]--; edges[p].push_back(v); }else{ edges.push_back({}); } deg.push_back(r); while(deg[v]){ idx++; if(record[idx]<0){ for(int i=0; i<dist.size(); i++){ if(dist[i] == dist[v] + record[idx] && 0 < deg[i]){ edges[i].push_back(v); edges[v].push_back(i); deg[i]--; deg[v]--; } } }else{ dfs(deg.size(), v, dep+1); } } } void solve(){ idx = 0; dfs(0, -1, 0); for(int i=0; i<edges.size(); i++){ auto edge = edges[i]; sort(edge.begin(), edge.end()); cout << i+1; for(auto v: edge) cout << " " << v+1; cout << endl; } } int main(){ int N; cin >> N; int cnt = 1; while (N--) { record.clear(); dist.clear(); deg.clear(); edges.clear(); int r; while(cin >> r, r) record.push_back(r); //cout << "Case:" << cnt << endl; solve(); cnt++; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 2 * 1e6 + 10; long long INF = 1e9 + 10; long long MOD = 998244353; int n; pair<int, int> a[MAXN]; pair<int, int> b[MAXN]; int func(int x, int cnt) { int res = 0; for (int pos = 0; pos < cnt; pos++) res += (1 << pos) * (int)((x >> pos) & 1); return res; } vector<pair<int, int> > gr[MAXN]; bool was[MAXN]; vector<int> ans; void dfs(int u, int e) { while (gr[u].size() > 0) { if (was[gr[u].back().second]) gr[u].pop_back(); else { was[gr[u].back().second] = 1; dfs(gr[u].back().first, gr[u].back().second); } } if (e == -1) return; if (b[e].first == u) { ans.push_back(e * 2 + 1); ans.push_back(e * 2 + 2); } else { ans.push_back(e * 2 + 2); ans.push_back(e * 2 + 1); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; for (int ANS = 20; ANS >= 0; ANS--) { for (int i = 0; i < MAXN; i++) gr[i].clear(); for (int i = 0; i < n; i++) { b[i].first = func(a[i].first, ANS); b[i].second = func(a[i].second, ANS); gr[b[i].first].push_back({b[i].second, i}); gr[b[i].second].push_back({b[i].first, i}); } bool bad = 0; for (int i = 0; i < MAXN; i++) { if ((gr[i].size() % 2) == 1) bad = 1; } if (bad) continue; fill(was, was + MAXN, 0); int start = 0; while (gr[start].size() == 0) start++; ans.clear(); dfs(start, -1); if (ans.size() == 2 * n) { cout << ANS << '\n'; for (auto it : ans) cout << it << ' '; return 0; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; char s[500010]; int A[500010], n; struct node { long long sum, suml, sumr, sum_; int L, R; friend node operator+(const node &a, const node &b) { node c; int t = a.R * 10 + b.L; int t1 = (t <= 18 && t > 9 ? 10 - abs(t - 9) : 0); c.sum = (a.sum * b.sum + a.suml * b.sumr % 998244353 * t1) % 998244353; c.suml = (a.sum * b.suml + a.suml * b.sum_ % 998244353 * t1) % 998244353; c.sumr = (a.sumr * b.sum + a.sum_ * b.sumr % 998244353 * t1) % 998244353; c.sum_ = (a.sumr * b.suml + a.sum_ * b.sum_ % 998244353 * t1) % 998244353; c.L = a.L; c.R = b.R; return c; } void init(int x) { L = R = x; sum = x + 1; suml = sumr = 1; sum_ = 0; } } G[(500010 << 2)]; struct Segment_Tree { long long sum[(500010 << 2)], suml[(500010 << 2)], sumr[(500010 << 2)], sum_[(500010 << 2)]; int L[(500010 << 2)], R[(500010 << 2)]; void Update(int l, int r, int p, int x) { if (l == r) { G[p].init(A[l]); return; } int mid = (l + r) >> 1; if (x <= mid) { Update(l, mid, (p << 1), x); } else { Update(mid + 1, r, (p << 1 | 1), x); } G[p] = G[(p << 1)] + G[(p << 1 | 1)]; } node Query(int l, int r, int p, int x) { if (l >= x) { return G[p]; } int mid = (l + r) >> 1; if (x <= mid) { return Query(l, mid, (p << 1), x) + G[(p << 1 | 1)]; } else { return Query(mid + 1, r, (p << 1 | 1), x); } } } T; struct Seg1 { int Mn[(500010 << 2)]; void Update(int l, int r, int p, int x) { if (l == r) { if (A[l] == 0) { Mn[p] = n + 1; } else { Mn[p] = l; } return; } int mid = (l + r) >> 1; if (x <= mid) { Update(l, mid, (p << 1), x); } else { Update(mid + 1, r, (p << 1 | 1), x); } Mn[p] = min(Mn[(p << 1)], Mn[(p << 1 | 1)]); } } T1; int main() { int m, i; scanf("%d%d%s", &n, &m, s + 1); for (i = 1; i <= n; i++) { A[i] = s[i] - '0'; T.Update(1, n, 1, i); T1.Update(1, n, 1, i); } while (m--) { int x, y; scanf("%d%d", &x, &y); A[x] = y; T.Update(1, n, 1, x); T1.Update(1, n, 1, x); int t = T1.Mn[1]; if (t > n) { printf("1\n"); } else { node tmp = T.Query(1, n, 1, t); printf("%lld\n", tmp.sum); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; #define EPS 1e-9 #define M 256 #define INF 1e9 int main(){ int N; while(cin >> N, N > 0){ int I[N+1],R[N+1],O[N+1]; for(int i = 0 ; i < N ; i++){ cin >> I[i]; } int S,A,C; double ans = INF; for(int s = 0 ; s <= 15 ; s++){ for(int a = 0 ; a <= 15 ; a++){ for(int c = 0 ; c <= 15 ; c++){ int x[M] = {0}; R[0] = s; for(int i = 1 ; i <= N ;i++){ R[i] = (a*R[i-1]+c) % M; O[i] = (I[i-1]+R[i]) % M; x[O[i]]++; } double H = 0; for(int i = 0 ; i < M ; i++){ double p = (double)x[i]/N; if(p > 0){ H -= p*log(p); } } if(H+EPS < ans){ ans = H; S = s; A = a; C = c; } } } } cout << S << " " << A << " " << C << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; template <class T> void printImpl(const vector<T>& coll) { copy(coll.begin(), coll.end(), ostream_iterator<T>(cout, " ")); cout << endl; } template <class T, int N> void printImpl(T (&coll)[N]) { copy(coll, coll + N, ostream_iterator<T>(cout, " ")); cout << endl; } template <class Key, class Value> void printImpl(const map<Key, Value>& data) { typename map<Key, Value>::const_iterator it; for (it = data.begin(); it != data.end(); ++it) { cout << it->first << ":" << it->second << endl; } } template <class T> void printImpl(const T& data) { cout << data << endl; } int main() { ios_base::sync_with_stdio(false); long long n, a, b, c; cin >> n >> a >> b >> c; if (a < b - c || n < b) { cout << n / a << endl; return 0; } long long bCount = (n - b) / (b - c) + 1; long long aCount = (n - bCount * (b - c)) / a; cout << aCount + bCount << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define ll long long #define mp make_pair #define pb push_back #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define rep(i, n) for (int i = 0; i < (int)(n); ++ i) int n; char s[55][25]; ll c[55]; bool ispal(char *s, int l, int r) { while (l < r) { if (s[l] != s[r]) return 0; l ++, r --; } return 1; } ll mem[55][25][55][25]; ll calc(int i, int ui, int j, int uj) { if (~mem[i][ui][j][uj]) return mem[i][ui][j][uj]; ll &ans = mem[i][ui][j][uj] = 1e18; if (ui == strlen(s[i]) && uj == strlen(s[j])) return ans = 0; if (ui == strlen(s[i])) { if (ispal(s[j], 0, strlen(s[j]) - uj - 1)) return ans = 0; rep(k, n) ans = min(ans, calc(k, 0, j, uj) + c[k]); return ans; } if (uj == strlen(s[j])) { if (ispal(s[i], ui, strlen(s[i]) - 1)) return ans = 0; rep(k, n) ans = min(ans, calc(i, ui, k, 0) + c[k]); return ans; } if (s[i][ui] == s[j][strlen(s[j]) - uj - 1]) return ans = calc(i, ui + 1, j, uj + 1); return ans; } int main() { scanf("%d", &n); rep(i, n) scanf("%s %lld", s[i], &c[i]); ll ans = 1e18; rep(i, n) if (ispal(s[i], 0, strlen(s[i]) - 1)) ans = min(ans, c[i]); memset(mem, -1, sizeof(mem)); rep(i, n) rep(j, n) ans = min(ans, calc(i, 0, j, 0) + c[i] + c[j]); if (ans >= 1e18) puts("-1"); else printf("%lld\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { cout.precision(10); cout << fixed; int N; cin >> N; vector<int> p(N); for (int i = 0; i < N; ++i) { cin >> p[i]; --p[i]; } vector<vector<int> > a(N); for (int i = 0; i < N; ++i) { int k; cin >> k; for (int j = 0; j < k; ++j) { int x; cin >> x; --x; a[x].push_back(i); } } int ans = 1e9; for (int b = 0; b < 3; ++b) { int c = b; int res = 0; vector<int> toUnlock(N); for (int i = 0; i < N; ++i) { for (size_t j = 0; j < a[i].size(); ++j) { ++toUnlock[a[i][j]]; } } int left = N; while (left > 0) { int changed = 1; while (changed) { changed = 0; for (int i = 0; i < N; ++i) { if (toUnlock[i] == 0 && p[i] == c) { toUnlock[i] = -1; ++res; changed = 1; --left; for (size_t j = 0; j < a[i].size(); ++j) { --toUnlock[a[i][j]]; } } } } if (left > 0) { ++res; c = (c + 1) % 3; } } ans = min(ans, res); } cout << ans << '\n'; return 0; }
1
#include<bits/stdc++.h> using namespace std; int main() { long long a,b,c,x,y,ans; cin>>a>>b>>c>>x>>y; ans=max(x,y)*2*c; ans=min(ans,min(x,y)*2*c+((x>y)?(x-y)*a:(y-x)*b)); ans=min(ans,a*x+b*y); cout<<ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = 998244353; ll n; vector<ll>c; vector<ll>to[10005]; vector<ll>ans; ll x=0; void dfs(ll v, ll p = -1){ ans[v]=c[x]; x++; for(ll i=0; i<to[v].size(); i++){ ll u = to[v][i]; if(u==p) continue; dfs(u,v); } } int main(){ cin >> n; for(int i=0; i<n-1; i++){ ll a,b; cin >> a >> b; a--; b--; to[a].push_back(b); to[b].push_back(a); } ll res = 0; c.resize(n); for(int i=0; i<n; i++){ cin >> c[i]; res+=c[i]; } sort(c.begin(),c.end()); reverse(c.begin(),c.end()); res-=c[0]; ans.resize(n); dfs(0); cout << res << endl; for(int i=0; i<n; i++) cout << ans[i] << " "; return 0; }
0
#include<iostream> using namespace std; int main(){ int n,m; int a,b,c; int i,j,k; int s=0; while(cin>>n){ if(!n)break; b=0,c=1e5,s=0; for(i=0;i<n;i++){ cin>>a; if(b<a)b=a; if(a<c)c=a; s+=a; } s-=b+c,n-=2; s/=n; cout<<s<<endl; } }
0
#include <bits/stdc++.h> using namespace std; const int MAX = 100; struct Mat { int ma[MAX][MAX]; void clear() { memset(ma, 0, sizeof(ma)); } } res[MAX]; int pow4[] = {1, 4, 16, 64, 256, 1024, 4096, 16384, 65536}; int mxsize; int FourBit(int val) { for (int i = 0;; i++) if (pow4[i] > val) return i - 1; } void Expand(int x, int y, int val, Mat &a) { int tp = val / 4; if (x == 1) a.ma[x - 1][y] += tp * 2; else if (x > 1) a.ma[x - 1][y] += tp; if (y == 1) a.ma[x][y - 1] += tp * 2; else if (y > 1) a.ma[x][y - 1] += tp; if (x + 1 == mxsize) mxsize++; a.ma[x + 1][y] += tp; if (y + 1 == mxsize) mxsize++; a.ma[x][y + 1] += tp; a.ma[x][y] += val % 4; } Mat Update(Mat m) { Mat ans[2]; int a = 0, b = 1, flag = 1, tp; ans[0] = m; while (flag) { ans[b].clear(); flag = 0; for (int i = 0; i < mxsize; i++) { for (int j = 0; j < mxsize; j++) { if ((tp = ans[a].ma[i][j]) >= 4) { flag = 1; Expand(i, j, tp, ans[b]); } else { ans[b].ma[i][j] += tp; } } } a ^= 1; b ^= 1; } return ans[a]; } Mat AddMat(Mat a, Mat b) { Mat c; c.clear(); for (int i = 0; i < mxsize; i++) for (int j = 0; j < mxsize; j++) c.ma[i][j] = a.ma[i][j] + b.ma[i][j]; return c; } void GetPow() { res[0].clear(); res[0].ma[0][0] = 1; mxsize = 1; for (int i = 1; i <= 15; i++) { res[i] = Update(AddMat(res[i - 1], res[i - 1])); } } int main() { int n, m; GetPow(); while (scanf("%d%d", &n, &m) != EOF) { Mat ans; ans.clear(); for (int i = 0, st = n; st; st >>= 1, i++) if (st & 1) ans = AddMat(ans, res[i]); ans = Update(ans); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); if (x < 0) x = -x; if (y < 0) y = -y; if (x >= MAX || y >= MAX) printf("0\n"); else printf("%d\n", ans.ma[x][y]); } } return 0; }
2
#include <iostream> #include <algorithm> using namespace std; int n, m[2], b[2][28], in, T = 0; char d[2][5][5][5]; int main(){ while(cin >> n && n){ if(T) cout << endl; for(int k=0;k<5;k++) for(int j=0;j<5;j++) for(int i=0;i<5;i++) cin >> d[0][i][j][k]; fill(b[0], b[2], 0); for(int i=0;i<2;i++){ cin >> m[i]; for(int j=0;j<m[i];j++){ cin >> in; b[i][in]++; } } for(int t=0;t<n;t++){ for(int x=0;x<5;x++){ for(int y=0;y<5;y++){ for(int z=0;z<5;z++){ int cnt = 0; for(int dx=-1;dx<2;dx++){ for(int dy=-1;dy<2;dy++){ for(int dz=-1;dz<2;dz++){ if(dx == dy && dz == dx && dz == 0) continue; int nx = x + dx; int ny = y + dy; int nz = z + dz; if(min(nx, min(ny, nz)) < 0 || max(nx, max(ny, nz)) > 4) continue; if(d[t%2][nx][ny][nz] == '1') cnt++; } } } d[(t+1)%2][x][y][z] = d[t%2][x][y][z]; if(d[t%2][x][y][z] == '1' && !b[1][cnt]) d[(t+1)%2][x][y][z] = '0'; if(d[t%2][x][y][z] == '0' && b[0][cnt]) d[(t+1)%2][x][y][z] = '1'; } } } } cout << "Case " << ++T << ":" << endl; for(int k=0;k<5;k++){ if(k) cout << endl; for(int j=0;j<5;j++){ for(int i=0;i<5;i++) cout << d[n%2][i][j][k]; cout << endl; } } } }
0
#include <bits/stdc++.h> using namespace std; int n, q, poz[100000 + 10]; long long ans1, ans2; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; poz[x] = i; } cin >> q; while (q--) { int x; cin >> x; ans1 += 1LL * poz[x]; ans2 += 1LL * (n - poz[x] + 1); } cout << ans1 << ' ' << ans2 << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, k, x, maxx, b[1000000], a[1000000], ans, kol, ii; int t = 0; bool ok; int main() { cin >> n >> k >> x; for (int i = 0; i < n; i++) cin >> a[i]; maxx = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) b[j] = a[j]; b[i] = x; k = i; for (int j = i; j < n; j++) { k++; b[k] = a[j]; } kol = 0; ii = 1; ok = false; ans = 1; while (ii <= k) { if (b[ii] == b[ii - 1]) ans++; if (b[ii] != b[ii - 1] || ii == k) { if (ans > 2) { kol += ans; if (ii == k && b[ii] == b[ii - 1]) { ok = true; } else { for (int j = ii; j <= k; j++) { ok = true; b[j - ans] = b[j]; } } if (ok) k -= ans; ii = 0; ok = false; } ans = 1; } ii++; } if (kol > maxx) { maxx = kol; } } if (maxx == 0) cout << 0 << endl; else cout << maxx - 1 << endl; }
2
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using ull = uint64_t; ll mi(ll a, ll b, ll p) { ll r = 1; while (b) { if (1 & b) r = r * a % p; a = a * a % p; b >>= 1; } return r; } ll exgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll d = exgcd(b, a % b, y, x); y -= a / b * x; } void print(ll n) { if (n < 0) { putchar('-'); n = -n; } if (n > 9) print(n / 10); putchar(n % 10 + '0'); } ll read() { char ch = getchar(); ll f = 1, x = 0; while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * f; } void init(){}; void solve() { long long n; string s; cin >> n; vector<long long> a, b, c(n); for (auto &t : c) cin >> t; cin >> s; for (long long i = 0; i < n; i++) { if (s[i] == 'B') a.push_back(c[i]); else b.push_back(c[i]); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long len = a.size(), p = a.size(); for (long long i = 0; i < a.size(); i++) { if (a[i] < i + 1) { cout << "NO\n"; return; } } for (long long i = p; i < n; i++) { if (b[i - p] > i + 1) { cout << "NO\n"; return; } } cout << "YES\n"; } signed main() { init(); ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class A, class B> A convert(B x) { stringstream ss; ss << x; A ret; ss >> ret; return ret; } const int oo = ~0u >> 2; const double eps = 1e-10, pi = acos(-1); const int ROW = 7, COL = 8, mp = 10, mc = 4 - 1, MAX_PTS = mp * 2 + 10, MAX_EDGES = ((MAX_PTS) * (MAX_PTS)) * 2, mo = 1000000007; const int fx[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; const char tml[5] = "BRWY"; struct Edge { int y, f, c, l; } E[MAX_EDGES]; int g[MAX_PTS], dis[MAX_PTS], fa[MAX_PTS], Q[MAX_PTS]; bool inq[MAX_PTS]; int tt, S(mp * 2 + 1), T(mp * 2 + 2); int a[ROW][COL]; bool v[ROW][COL], hori[ROW][COL], vert[ROW][COL]; int req[mp], pil[mp], num[mc + 1][mc + 1]; pair<int, int> ps_ty[mp]; char Now[ROW * 2 + 1][COL * 2 + 1], Ans[ROW * 2 + 1][COL * 2 + 1], TMP[ROW * 2 + 1][COL * 2 + 1]; int Best(oo); set<int> mem; void add(int x, int y, int f, int c) { E[++tt].y = y, E[tt].f = f, E[tt].c = c, E[tt].l = g[x], g[x] = tt; E[++tt].y = x, E[tt].f = 0, E[tt].c = -c, E[tt].l = g[y], g[y] = tt; } int diff(int x, int y) { pair<int, int> l = make_pair(min(ps_ty[x].first, ps_ty[x].second), max(ps_ty[x].first, ps_ty[x].second)); pair<int, int> r = make_pair(min(ps_ty[y].first, ps_ty[y].second), max(ps_ty[y].first, ps_ty[y].second)); if (l.first == r.second || l.second == r.first) swap(l.first, l.second); int ret = (l.first != r.first) + (l.second != r.second); return ret; } void work() { int enc(0); for (int i = 0; i <= mp - 1; ++i) enc = ((long long)enc * 29 + pil[i]) % mo; if (mem.find(enc) != mem.end()) return; mem.insert(enc); memset(&g, 0, sizeof(g)); ; tt = 1; for (int i = 0; i <= mp - 1; ++i) { add(S, i + 1, req[i], 0); add(i + mp + 1, T, pil[i], 0); for (int j = 0; j <= mp - 1; ++j) add(i + 1, j + mp + 1, oo, diff(i, j)); } int Cost(0); do { for (int i = 0; i <= T; ++i) dis[i] = oo, inq[i] = 0; int l(0), r(0); Q[r = (r + 1) % MAX_PTS] = S, dis[S] = 0, inq[S] = 1; while (l != r) { int x = Q[l = (l + 1) % MAX_PTS]; for (int i = g[x], y = E[i].y; i; i = E[i].l, y = E[i].y) if (E[i].f && dis[x] + E[i].c < dis[y]) { dis[y] = dis[x] + E[i].c, fa[y] = i; if (!inq[y]) inq[Q[r = (r + 1) % MAX_PTS] = y] = 1; } inq[x] = 0; } if (dis[T] == oo) break; int Aug = oo, x = T; while (x != S) Aug = min(Aug, E[fa[x]].f), x = E[fa[x] ^ 1].y; Cost += Aug * dis[T], x = T; while (x != S) E[fa[x]].f -= Aug, E[fa[x] ^ 1].f += Aug, x = E[fa[x] ^ 1].y; } while (1); if (Cost >= Best) return; int need[mp][mp], asn[mp], Cnt(0); for (int i = 0; i <= mp - 1; ++i) { asn[i] = 0; int SN(i * (mp + 2) * 2 + 7); for (int j = 0; j < mp; ++j, SN += 2) { need[j][i] = E[SN].f; if (E[SN].f) Cnt += E[SN].f * abs(E[SN].c); } } memcpy(&TMP, &Now, sizeof(Now)); for (int i = 0; i <= ROW - 1; ++i) for (int j = 0; j <= COL - 1; ++j) if (hori[i][j] || vert[i][j]) { int x, y; if (hori[i][j]) x = i, y = j + 1; else x = i + 1, y = j; int h(num[a[i][j]][a[x][y]]), t; if (need[h][h]) { --need[h][h], TMP[i * 2][j * 2] = tml[a[i][j]], TMP[x * 2][y * 2] = tml[a[x][y]]; continue; } while (!need[h][asn[h]]) ++asn[h]; --need[h][asn[h]], TMP[i * 2][j * 2] = tml[ps_ty[asn[h]].first], TMP[x * 2][y * 2] = tml[ps_ty[asn[h]].second]; int D = (ps_ty[asn[h]].first != a[i][j]) + (ps_ty[asn[h]].second != a[x][y]); if (D != diff(h, asn[h])) TMP[i * 2][j * 2] = tml[ps_ty[asn[h]].second], TMP[x * 2][y * 2] = tml[ps_ty[asn[h]].first]; } memcpy(&Ans, &TMP, sizeof(TMP)); Best = Cost; } void dfs(int x, int y) { if (x == ROW) { work(); return; } int nx = y == (COL - 1) ? x + 1 : x, ny = (y + 1) % COL; if (v[x][y]) { dfs(nx, ny); return; } if (y + 1 < COL && !v[x][y + 1]) { v[x][y] = v[x][y + 1] = 1, Now[x * 2][y * 2 + 1] = '-', hori[x][y] = 1; ++pil[num[a[x][y]][a[x][y + 1]]]; dfs(nx, ny); --pil[num[a[x][y]][a[x][y + 1]]]; v[x][y] = v[x][y + 1] = 0, Now[x * 2][y * 2 + 1] = '.', hori[x][y] = 0; } if (x + 1 < ROW && !v[x + 1][y]) { v[x][y] = v[x + 1][y] = 1, Now[x * 2 + 1][y * 2] = '|', vert[x][y] = 1; ++pil[num[a[x][y]][a[x + 1][y]]]; dfs(nx, ny); --pil[num[a[x][y]][a[x + 1][y]]]; v[x][y] = v[x + 1][y] = 0, Now[x * 2 + 1][y * 2] = '.', vert[x][y] = 0; } } int main() { for (int i = 0; i < ROW; ++i, scanf("\n")) for (int j = 0; j <= COL - 1; ++j) { char ch; scanf("%c", &ch); a[i][j] = strchr(tml, ch) - tml; } int tt(0); for (int i = 0; i <= mc; ++i) for (int j = mc; j >= i; --j) { num[i][j] = num[j][i] = tt; ps_ty[tt++] = make_pair(i, j); } for (int i = 0; i <= tt - 1; ++i) scanf("%d", req + i); memset(&Now, '.', sizeof(Now)); ; memset(&v, 0, sizeof(v)); ; memset(&pil, 0, sizeof(pil)); ; memset(&hori, 0, sizeof(hori)); ; memset(&vert, 0, sizeof(vert)); ; mem.clear(); dfs(0, 0); printf("%d\n", ROW * COL - Best); for (int i = 0; i < ROW * 2 - 1; printf("\n"), ++i) for (int j = 0; j <= COL * 2 - 2; ++j) printf("%c", Ans[i][j]); }
5
#include <bits/stdc++.h> using namespace std; int main(){ int a,b; cin >> a >> b; if(a>b)swap(a,b); for(int i=0;i<b;i++)cout << a; cout << endl; return 0; }
0
#include <bits/stdc++.h> int main() { int T, n; scanf("%d", &T); while (T--) { scanf("%d", &n); int ans = 0x3f3f3f3f, a, b; for (int x = 1, m; x * x <= n; ++x) if (m % x == 0) { m = x; if (m && (n - m) && m * (n / m - 1) < ans) { ans = m * (n / m - 1); a = m; b = n - a; } m = n / x; if (m && (n - m) && m * (n / m - 1) < ans) { ans = m * (n / m - 1); a = m; b = n - a; } } printf("%d %d\n", a, b); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; int pr[MAX]; int ht[MAX]; int tin[MAX]; int tout[MAX]; int rev[MAX]; vector<int> adj[MAX]; vector<int> val[MAX * 4]; vector<int> vech[MAX * 4]; int tim; void dfs(int s, int p) { tin[s] = (++tim); rev[tim] = s; for (int i = 0; i < adj[s].size(); i++) { int u = adj[s][i]; if (u != p) { ht[u] = ht[s] + 1; dfs(u, s); } } tout[s] = tim; } void merge(int pos) { int ptr1 = 0; int ptr2 = 0; int lft = 2 * pos; int rgt = pos + pos + 1; while (ptr1 < val[lft].size() && ptr2 < val[rgt].size()) { if (vech[lft][ptr1] == vech[rgt][ptr2]) { vech[pos].push_back(vech[lft][ptr1]); val[pos].push_back(min(val[lft][ptr1], val[rgt][ptr2])); ptr1++; ptr2++; } else if (vech[lft][ptr1] < vech[rgt][ptr2]) { vech[pos].push_back(vech[lft][ptr1]); val[pos].push_back(val[lft][ptr1]); ptr1++; } else { vech[pos].push_back(vech[rgt][ptr2]); val[pos].push_back(val[rgt][ptr2]); ptr2++; } } for (int i = ptr1; i < val[lft].size(); i++) { vech[pos].push_back(vech[lft][i]); val[pos].push_back(val[lft][i]); } for (int i = ptr2; i < val[rgt].size(); i++) { vech[pos].push_back(vech[rgt][i]); val[pos].push_back(val[rgt][i]); } for (int i = 1; i < val[pos].size(); i++) { val[pos][i] = min(val[pos][i - 1], val[pos][i]); } } void build(int pos, int l, int r) { if (l == r) { int s = rev[l]; vech[pos].push_back(ht[s]); val[pos].push_back(pr[s]); return; } int mid = (l + r) / 2; build(pos + pos, l, mid); build(pos + pos + 1, mid + 1, r); merge(pos); } int query(int pos, int l, int r, int ql, int qr, int y) { if (ql > r || qr < l) { return 1e9 + 5; } if (l >= ql && r <= qr) { int x = upper_bound(vech[pos].begin(), vech[pos].end(), y) - vech[pos].begin() - 1; if (x < 0) { return 1e9 + 5; } else { return val[pos][x]; } } int mid = (l + r) / 2; int a = query(pos + pos, l, mid, ql, qr, y); int b = query(pos + pos + 1, mid + 1, r, ql, qr, y); return min(a, b); } int main() { int n, r; scanf("%d %d", &n, &r); for (int i = 1; i <= n; i++) { scanf("%d", &pr[i]); } for (int i = 1; i < n; i++) { int a, b; scanf("%d %d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } dfs(r, 0); build(1, 1, n); int q; scanf("%d", &q); int last = 0; for (int i = 1; i <= q; i++) { int x, y; scanf("%d %d", &x, &y); x = ((x + last) % n) + 1; y = ((y + last) % n); last = query(1, 1, n, tin[x], tout[x], min(ht[x] + y, n)); printf("%d\n", last); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int Pm[2002][2002]; int main() { int n; cin >> n; vector<pair<int, int>> p(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; Pm[x + 1000][y + 1000] = 1; p[i] = {x, y}; } int ans = 0; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { int x1 = p[i].first; int y1 = p[i].second; int x2 = p[j].first; int y2 = p[j].second; if ((x1 + x2) % 2 == 0 && (y1 + y2) % 2 == 0) { if (Pm[(x1 + x2) / 2 + 1000][(y1 + y2) / 2 + 1000]) { ans++; } } } } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int const inf = 1e18; long long int const maxn = 1e6 + 5; long long int const mod = 1e9 + 7; long long int n, m; bool isValid(long long int u, long long int v) { return u >= 0 && u < n && v >= 0 && v < m; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int r, c, x, y; cin >> n >> m >> r >> c >> x >> y; r--; c--; string arr[n]; for (long long int i = 0; i < n; i++) cin >> arr[i]; long long int dist[n][m], vis[n][m]; for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) { dist[i][j] = inf; vis[i][j] = 0; } } dist[r][c] = 0; deque<pair<long long int, long long int>> q; q.push_back({r, c}); while (!q.empty()) { pair<long long int, long long int> val = q.front(); q.pop_front(); long long int i = val.first, j = val.second; vis[i][j] = 1; if (isValid(i, j - 1) && arr[i][j - 1] == '.') { if ((dist[i][j] + 1) < dist[i][j - 1]) { dist[i][j - 1] = dist[i][j] + 1; q.push_back({i, j - 1}); } } if (isValid(i, j + 1) && arr[i][j + 1] == '.') { if (dist[i][j] < dist[i][j + 1]) { dist[i][j + 1] = dist[i][j]; q.push_front({i, j + 1}); } } if (isValid(i - 1, j) && arr[i - 1][j] == '.') { if (dist[i][j] < dist[i - 1][j]) { dist[i - 1][j] = dist[i][j]; q.push_front({i - 1, j}); } } if (isValid(i + 1, j) && arr[i + 1][j] == '.') { if (dist[i][j] < dist[i + 1][j]) { dist[i + 1][j] = dist[i][j]; q.push_front({i + 1, j}); } } } long long int cnt = 0; for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) { if (vis[i][j] && dist[i][j] <= x) { long long int right_movement = dist[i][j] + (j - c); if (right_movement <= y) cnt++; } } } cout << cnt << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int num[1001] = {0}; int main() { int n, sum = 0; cin >> n; for (int i = 0; i < n; i++) cin >> num[i]; sort(num, num + n); sum = num[n - 1] - num[0] + 1 - n; cout << sum << endl; }
1
#include <bits/stdc++.h> using namespace std; int n; int b[100005]; int a[100005]; int help[100005]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &b[i]); if (n <= 2) { printf("0\n"); return 0; } for (int i = 0; i < n - 1; i++) a[i] = b[i + 1] - b[i]; int maxvalue = -0x3f3f3f3f; int minvalue = 0x3f3f3f3f; for (int i = 0; i < n - 1; i++) { maxvalue = max(maxvalue, a[i]); minvalue = min(minvalue, a[i]); } if (maxvalue - minvalue > 4) printf("-1\n"); else { bool flag; int ans = 0x3f3f3f3f; int res; for (int i = -1; i <= 1; i++) { for (int j = minvalue; j <= maxvalue; j++) { flag = true; help[0] = b[0] + i; for (int k = 0; k < n; k++) { if (abs(help[0] + k * j - b[k]) > 1) { flag = false; break; } } if (flag) { res = 0; for (int k = 0; k < n; k++) res += abs(help[0] + k * j - b[k]); ans = min(ans, res); } } } if (ans == 0x3f3f3f3f) printf("-1\n"); else printf("%d\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long max(long long a, long long b) { if (b <= a) return a; else return b; } long long min(long long a, long long b) { if (b <= a) return b; else return a; } long long fact(long long a) { long long f = 1; for (long long i = 1; i <= a; i++) f = (f * i); return f; } void FASTIO() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void solve(int t) { while (t--) { int n; cin >> n; for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { if (abs(i - j) <= 1) cout << 1 << " "; else cout << 0 << " "; } cout << "\n"; } } } int main() { FASTIO(); int t; cin >> t; solve(t); return 0; }
2
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, k; cin >> n >> k; long long int a[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; } long long int remain = 0; long long int ans = 0, prev; for (long long int i = 0; i < n; i++) { prev = ans; ans += (a[i] + remain) / k; if (i != n - 1 && (ans - prev) * k < remain) { ans++; remain = 0; } else if ((ans - prev) * k < remain) { remain = remain + a[i]; } else remain = -(ans - prev) * k + (a[i] + remain); } prev = ans; ans += (remain) / k; if (ans == prev && remain != 0) ans++; cout << ans << endl; }
4
#include<bits/stdc++.h> #define maxn 600005 #define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++) #define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--) #define mod 924844033 using namespace std; int n,sz[maxn],A[maxn],B[maxn]; vector<int>G[maxn]; void dfs(int u,int ff){ sz[u] = 1; int v; rep(i,0,G[u].size()-1) if((v=G[u][i]) ^ ff){ dfs(v,u); sz[u] += sz[v]; A[sz[v]] -- , A[n-sz[v]]--; } } int Wl,Wl2,w[maxn],lg[maxn],inv[maxn],invf[maxn],fac[maxn]; int Pow(int b,int k){ int r=1;for(;k;k>>=1,b=1ll*b*b%mod) if(k&1) r=1ll*r*b%mod; return r; } void init(int n){ for(Wl=1;n>=Wl<<1;Wl<<=1);int pw=Pow(5,(mod-1)/(Wl2=Wl<<1)); w[Wl] = inv[0] = inv[1] = fac[0] =fac[1] = invf[0] = invf[1] = 1; rep(i,Wl+1,Wl2) w[i] = 1ll * w[i-1] * pw % mod; per(i,Wl-1,1) w[i] = w[i<<1]; rep(i,2,Wl2) lg [i] = lg[i>>1] + 1, inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod, fac[i] = 1ll * fac[i-1] * i % mod ,invf[i] = 1ll * invf[i-1] * inv[i] % mod; } int upd(int x){ return x += x >> 31 & mod; } void NTT(int *A,int n,int tp){ static int r[maxn]={}; static unsigned long long ar[maxn]; if(tp ^ 1) reverse(A+1,A+n); rep(i,0,n-1) r[i] = r[i>>1] >>1|(i&1)<<lg[n]-1 , ar[i] = upd(A[r[i]]); for(int L=1;L<n;L<<=1) for(int s=0,L2=L<<1;s<n;s+=L2) for(int k=s,x=L,t;k<s+L;k++,x++) t = ar[k+L] * w[x] % mod , ar[k+L] = ar[k] - t + mod , ar[k] += t; rep(i,0,n-1) A[i] = ar[i] % mod; if(tp ^ 1) rep(i,0,n-1) A[i] = 1ll * A[i] * inv[n] % mod; } int main(){ scanf("%d",&n); rep(i,1,n-1){ int a,b; scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); } dfs(1,0); init(n << 1); rep(i,0,n) A[i] = 1ll * A[i] * fac[i] % mod , B[i] = invf[i]; reverse(A,A+n+1); NTT(A,Wl2,1),NTT(B,Wl2,1); rep(i,0,Wl2-1) A[i] = 1ll * A[i] * B[i] % mod; NTT(A,Wl2,-1); rep(i,1,n){ int ans = (1ll * fac[n] * invf[i] % mod * invf[n-i] % mod * n + A[n-i] * 1ll * invf[i]) % mod; printf("%d\n",(ans+mod)%mod); } }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int n, v, l, r, i; cin >> n >> l >> r; stack<int> s; vector<int> a(n); vector<int> c(n, INT_MAX); vector<int> d(n, INT_MIN); for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { while (!s.empty() && a[s.top()] > a[i]) { c[s.top()] = i; s.pop(); } s.push(i); } while (!s.empty()) s.pop(); for (i = n - 1; i >= 0; i--) { while (!s.empty() && a[s.top()] > a[i]) { d[s.top()] = i; s.pop(); } s.push(i); } for (i = 0; i < n; i++) { long long int a1 = c[i] - i - 1, a2 = i - d[i] - 1; if (a2 >= l && a1 >= r) { cout << i + 1; return 0; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long a[2000000]; void solve() { long long n; scanf("%d", &n); long long cnt = (1 << (n + 1)) - 1; for (long long i = 2; i <= cnt; i++) { scanf("%d", a + i); } long long ans = 0; for (long long t = (1 << n) - 1; t >= 1; t--) { long long x = a[t * 2]; long long y = a[t * 2 + 1]; a[t] += max(x, y); ans += 2 * max(x, y) - x - y; } cout << ans << endl; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
2
#include<set> #include<iostream> using namespace std; int main() { int T; cin >> T; set<int> s; while (T--) { int a; cin >> a; if (a==0) {int t; cin >> t;s.insert(t); cout << s.size() << endl; } else if (a==1) { int t; cin >> t; cout << (s.find(t) != s.end()) << endl; } else { int t; cin >> t; s.erase(t); } } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int a[n]; for(int i=0;i<n;i++) { cin>>a[i]; } int ans=0; for(int i=1;i<n-1;i++) { if((a[i-1]<a[i]&&a[i+1]>a[i])||\ a[i-1]>a[i]&&a[i+1]<a[i]) { ans++; } } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n; while (scanf("%lld", &n) == 1) { long long res; if (n == 1) res = 1; else if (n == 2) res = 2; else res = n * (n - 1) * (n - 2); if (n % 6 == 0) res = max(res / 2, (n - 1) * (n - 2) * (n - 3)); else if (n % 2 == 0 && n > 2) res = max(res / 2, n * (n - 1) * (n - 3)); printf("%lld\n", res); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int n, m, k; int comp_num = 0; char v[1005][1005]; int matrix[1005][1005]; int vis[1005][1005]; int ans[100005]; bool ok(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); } int dfs(int x, int y) { int a, b; int res = 0; vis[x][y] = 1; matrix[x][y] = comp_num; for (int i = 0; i < 4; i++) { a = x + dx[i]; b = y + dy[i]; if (ok(a, b) && v[a][b] == '*') { res++; } else if (ok(a, b) && v[a][b] == '.' && !vis[a][b]) { res += dfs(a, b); } } return res; } int main() { cin >> n >> m >> k; for (int(i) = 0; i < (n); i++) { for (int(x) = 0; x < (m); x++) cin >> v[i][x]; } for (int(i) = 0; i < (n); i++) { for (int(x) = 0; x < (m); x++) { if (v[i][x] == '.') { if (vis[i][x]) { continue; } comp_num++; ans[comp_num] = dfs(i, x); } } } ans[0] = 0; int a, b; while (k--) { cin >> a >> b; a--; b--; cout << ans[matrix[a][b]] << "\n"; } }
4
#define DEBUG 0 #include <bits/stdc++.h> using namespace std; #if DEBUG // basic debugging macros int __i__,__j__; #define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl #define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl #define printVar(n) cout<<#n<<": "<<n<<endl #define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl #define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;} #define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;} // advanced debugging class // debug 1,2,'A',"test"; class _Debug { public: template<typename T> _Debug& operator,(T val) { cout << val << endl; return *this; } }; #define debug _Debug(), #else #define printLine(l) #define printLine2(l,c) #define printVar(n) #define printArr(a,l) #define print2dArr(a,r,c) #define print2dArr2(a,r,c,l) #define debug #endif // define #define MAX_VAL 999999999 #define MAX_VAL_2 999999999999999999LL #define EPS 1e-6 #define mp make_pair #define pb push_back // typedef typedef unsigned int UI; typedef long long int LLI; typedef unsigned long long int ULLI; typedef unsigned short int US; typedef pair<int,int> pii; typedef pair<LLI,LLI> plli; typedef vector<int> vi; typedef vector<LLI> vlli; typedef vector<pii> vpii; typedef vector<plli> vplli; // ---------- END OF TEMPLATE ---------- #define MOD 1000000007 int H,L[1 << 17]; LLI P[1 << 17]; int l[1 << 18],r[1 << 18]; LLI inv(LLI n) { LLI r = 1; int e = MOD-2; while (e > 0) { if (e & 1) r *= n,r %= MOD; e >>= 1; n *= n,n %= MOD; } return r; } LLI prea[1 << 19],preb[1 << 19]; LLI query(vi &v,int l,int r,int t) { r = upper_bound(v.begin(),v.end(),r)-v.begin()-1; l = lower_bound(v.begin(),v.end(),l)-v.begin(); if (l <= r) { if (t == 0) return prea[r+1]-prea[l]; else return preb[r+1]-preb[l]; } else return 0; } LLI findAns2(vi a,vi b) { printArr(a,a.size()); printArr(b,b.size()); int i,j; vi v; vpii lca; LLI ans = 0; for (i = 0; i < a.size(); i++) v.pb(L[a[i]]); for (i = 0; i < b.size(); i++) v.pb(L[b[i]]); sort(v.begin(),v.end()); fill(prea,prea+v.size()+1,0); fill(preb,preb+v.size()+1,0); for (i = 0; i < a.size(); i++) { int p = lower_bound(v.begin(),v.end(),L[a[i]])-v.begin(); prea[p+1] += (P[a[i]]*P[L[a[i]]]) % MOD,prea[p] %= MOD; } for (i = 0; i < b.size(); i++) { int p = lower_bound(v.begin(),v.end(),L[b[i]])-v.begin(); preb[p+1] += (P[b[i]]*P[L[b[i]]]) % MOD,preb[p] %= MOD; } for (i = 0; i < v.size(); i++) { prea[i+1] = (prea[i+1]+prea[i]) % MOD; preb[i+1] = (preb[i+1]+preb[i]) % MOD; } printArr(v,v.size()); for (i = 1; i < v.size(); i++) { LLI p = 1,c = 1; for (j = H-2; j >= 0; j--) { if ((v[i]^v[i-1]) & (1 << j)) break; else { if (v[i] & (1 << j)) p *= c,c = 2*c+1,p %= MOD; else p *= c,c = 2*c,p %= MOD; } } debug p,c; LLI x = (query(v,l[c],r[c],0)*query(v,l[c],r[c],1)) % MOD; LLI y = (query(v,l[2*c],r[2*c],0)*query(v,l[2*c],r[2*c],1)) % MOD; LLI z = (query(v,l[2*c+1],r[2*c+1],0)*query(v,l[2*c+1],r[2*c+1],1)) % MOD; debug x,y,z; ans += ((x-y-z)*inv((((p*p) % MOD)*c) % MOD)) % MOD; } ans %= MOD; if (ans < 0) ans += MOD; printVar(ans); return ans; } int findAns(vi v,int t,LLI p) { if (v.size() == 1) return 0; int i; vi l,r; for (i = 0; i < v.size()/2; i++) l.pb(v[i]); for (i = v.size()/2; i < v.size(); i++) r.pb(v[i]); LLI ans = findAns(l,2*t,(p*t) % MOD)+findAns(r,2*t+1,(p*t) % MOD); LLI ans2 = (findAns2(l,r)*inv((((p*p) % MOD)*t) % MOD)) % MOD; printVar(ans2); return (ans+ans2) % MOD; } int main() { int i; vi v; scanf("%d",&H); for (i = 0; i < (1 << (H-1)); i++) scanf("%d",&L[i]),L[i]--,v.pb(i); int j; memset(l,-1,sizeof(l)); memset(r,-1,sizeof(r)); for (i = 0; i < (1 << (H-1)); i++) { P[i] = 1; int c = i+(1 << (H-1)); if (l[c] == -1) l[c] = r[c] = i; else l[c] = min(l[c],i),r[c] = max(r[c],i); for (j = 0; j < H; j++) { P[i] *= c,P[i] %= MOD,c /= 2; if (l[c] == -1) l[c] = r[c] = i; else l[c] = min(l[c],i),r[c] = max(r[c],i); } } printf("%d\n",findAns(v,1,1)); return 0; }
0
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll, ll>P; typedef pair<P, P>PP; ll dy[5] = {0, 1, 0, -1, 0}; ll dx[5] = {1, 0, -1, 0, 0}; ll change[5][5] = { {0, 1, 2, 3, 4}, {1, 2, 3, 0, 4}, {2, 3, 0, 1, 4}, {3, 0, 1, 2, 4}, {4, 4, 4, 4, 4} }; ll mp[33][33]; ll mn[33][33][5]; ll c[5]; void init(){ for(ll y = 0; y < 33; y++){ for(ll x = 0; x < 33; x++){ mp[y][x] = -1; for(ll i = 0; i < 5; i++){ mn[y][x][i] = -1145141919; } } } } int main(){ for(ll w, h; cin >> w >> h, w;){ init(); for(ll y = 1; y <= h; y++){ for(ll x = 1; x <= w; x++){ cin >> mp[y][x]; for(ll i = 0; i < 5; i++){ mn[y][x][i] = 1145141919; } } } for(ll i = 0; i < 4; i++)cin >> c[i]; priority_queue<PP, vector<PP>, greater<PP> >Q; Q.push(PP(P(0, 1), P(1, 0))); while(!Q.empty()){ ll cost = Q.top().first.first; ll x = Q.top().first.second; ll y = Q.top().second.first; ll d = Q.top().second.second; Q.pop(); if(mp[y][x] == -1)continue; if(mn[y][x][d] <= cost)continue; mn[y][x][d] = cost; if(y == h && x == w){ cout << cost << endl; break; } for(ll i = 0; i < 4; i++){ ll nxtC = cost; if(mp[y][x] != i)nxtC += c[i]; ll dd = change[d][i]; ll yy = y + dy[dd]; ll xx = x + dx[dd]; if(mn[yy][xx][dd] > nxtC){ Q.push(PP(P(nxtC, xx), P(yy, dd))); } } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template <typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } int odp[101]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) for (int k = i; k <= j; k++) odp[k]++; int mx = 0; for (int i = 1; i <= n; i++) mx = max(mx, odp[i]); cout << mx << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; string s; cin >> s; for (int i = 1; i <= t; i++) { if (t % i == 0) { reverse(s.begin(), s.begin() + i); } } cout << s; return 0; }
2
#include <bits/stdc++.h> using namespace std; double p[10000009], dp[10000009], ans[30]; int main() { int n, k; memset(dp, 0, sizeof(dp)); memset(ans, 0, sizeof(ans)); scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%lf", &p[i]); dp[0] = 1; for (int i = 0; i < (1 << n); i++) { double t = 0, l = 0; for (int j = 0; j < n; j++) { if (((1 << j) & i) == 0) { t += p[j]; l++; } } if (n - l > k) continue; for (int j = 0; j < n; j++) { if (p[j] < 0.0000000001) continue; if (((1 << j) & i) != 0) { dp[i] += dp[i - (1 << j)] * p[j] / (p[j] + t); ans[j] += dp[i - (1 << j)] * p[j] / (p[j] + t); } } } for (int i = 0; i < n; i++) printf("%.8lf ", ans[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long ar[90] = {0, 1, 1, 2}; long long x, y, ans, z; long long fn() { if (x < 0 && y <= 0) { if (z > y) return -1; return 0; } if (z <= y) return 0; long long cnt = 0, sum = 0, s = 0, p = 1, q = 2, i1 = 0, i2 = 1; if (x < 0) cnt = (abs(x) / y), x = x + cnt * y; long long a = x, b = y; for (int i = 0; i < 1000; i++) { sum = (a + b); cnt++; if (sum >= z || b >= z) return cnt; b = max(a, b); a = sum; } return -1; } int main() { int t, tc = 0; cin >> x >> y >> z; if (x > y) swap(x, y); ans = fn(); cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'H' || s[i] == 'Q' || s[i] == '9') { cout << "YES" << endl; return 0; } } cout << "NO" << endl; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { long long l, r; cin >> l >> r; long long ans = (r / 2) + 1; if (ans < l) { ans = l; } cout << (long long)r % ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; cout << "1" << " " << n - 1 << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long int n, s; cin >> n >> s; vector<int> v; long long int x = n, sm = 0; while (x) { v.push_back(x % 10); sm += (x % 10); x /= 10; } if (sm <= s) cout << 0 << endl; else { reverse(v.begin(), v.end()); if (v[0] < s) { sm = 0; int i = -1; while (sm <= s) { i++; if (i > v.size()) break; sm += v[i]; } if (sm > s) { sm -= v[i]; i--; while (sm + 1 > s && i >= 0) { sm -= v[i]; i--; } v[i]++; i++; for (; i < v.size(); i++) v[i] = 0; } } else { reverse(v.begin(), v.end()); v.push_back(1); reverse(v.begin(), v.end()); for (int j = 1; j < v.size(); j++) v[j] = 0; sm = 0; int i = 0; while (sm <= s && i < v.size()) { sm += v[i]; i++; } if (sm > s) { v[i - 1]++; for (; i < v.size(); i++) v[i] = 0; } } int co = v.size() - 1; long long int ans = 0; for (int i = 0; i < v.size(); i++, co++) { ans = (10 * ans) + v[i]; } cout << ans - n << endl; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; string s1, s2; int main() { double count = 0.00; cin >> s1; cin >> s2; int desired_position = 0; for (auto k : s1) { if (k == '+') desired_position++; else desired_position--; } int fixed_postion = 0, question_marks = 0; for (auto k : s2) { if (k == '+') fixed_postion++; else if (k == '-') fixed_postion--; else question_marks++; } for (int i = 0; i < (2 << question_marks); i++) { int temp = fixed_postion; for (int j = 0; j < question_marks; j++) { if (i & (1 << j)) { temp++; } else temp--; } if (temp == desired_position) count++; } cout << setprecision(12) << fixed << count / (2 << question_marks) << "\n"; return 0; }
2
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #include<queue> using namespace std; typedef pair<int,int> P; bool prime[1000001]; int n,m; int fie[1200][1200]; int dx[4]={1,0,-1,0}; int dy[4]={0,-1,0,1}; int numx[1000001],numy[1000001]; int dp[1201][1201]; void seive(){ for(int i=2;i<=1000000;i++)prime[i]=true; prime[0]=prime[1]=false; for(int i=2;i<=1000000;i++){ if(prime[i]){ for(int j=i*2;j<=1000000;j+=i)prime[j]=false; } } int x=501,y=501,i=1,cnt=0,d=0,cnt2=1; while(i<=1000000){ fie[x][y]=i; numx[i]=x; numy[i]=y; x+=dx[d]; y+=dy[d]; cnt++; if(cnt2==cnt){ d++; if(d==2)cnt2++; if(d==4)cnt2++,d=0; cnt=0; } i++; } } int main(void){ seive(); while(1){ scanf("%d%d",&m,&n); if(m+n==0)break; memset(dp,-1,sizeof(dp)); int mk=0; if(prime[n])mk++; dp[numx[n]][numy[n]]=mk; for(int i=0;i<=1100;i++){ for(int j=0;j<=1100;j++){ if(dp[j][i]>=0){ for(int k=-1;k<=1;k++){ if(fie[j+k][i+1]>=1 && fie[j+k][i+1]<=m){ int mc=0; if(prime[fie[j+k][i+1]])mc++; dp[j+k][i+1]=max(dp[j+k][i+1],dp[j][i]+mc); } } } } } int res2=0,res3=0; for(int i=0;i<=1100;i++){ for(int j=0;j<=1100;j++){ if(prime[fie[j][i]]){ if(dp[j][i]>=1 && (dp[j][i]>res2 ||(dp[j][i]==res2 && fie[j][i]>res3))){ res2=dp[j][i]; res3=fie[j][i]; } } } } printf("%d %d\n",res2,res3); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string S,T; cin >> S >>T; map<char,char> ma,ima; bool flag = true; for(int i = 0;i<S.size();i++){ char s = S[i]; char t = T[i]; if(ima.count(t)){ if(ima[t] != s) flag = false; } if(ma.count(s)){ if(ma[s] != t) flag = false; } ma[s] = t; ima[t] = s; } if(flag) cout << "Yes" << endl; else cout << "No"<<endl; }
0
#include <bits/stdc++.h> using namespace std; long long n, x, y, c; bool ch(long long m) { if (m == 0) return c == 1; double all = 1 + (m * (m + 1) / 2) * 4; double add = m - x + 1; if (add > 0) { add--; all -= (add * (add + 1) / 2) * 2 + add + 1; } add = m - y + 1; if (add > 0) { add--; all -= (add * (add + 1) / 2) * 2 + add + 1; } add = m - (n - y + 1) + 1; if (add > 0) { add--; all -= (add * (add + 1) / 2) * 2 + add + 1; } add = m - (n - x + 1) + 1; if (add > 0) { add--; all -= (add * (add + 1) / 2) * 2 + add + 1; } long long dx, dy; dx = abs(x - 1); dy = abs(y - 1); add = m - (dx + dy) - 1; if (add > 0) { all += add * (add + 1) / 2; } dx = abs(x - 1); dy = abs(n - y); add = m - (dx + dy) - 1; if (add > 0) { all += add * (add + 1) / 2; } dx = abs(n - x); dy = abs(y - 1); add = m - (dx + dy) - 1; if (add > 0) { all += add * (add + 1) / 2; } dx = abs(n - x); dy = abs(n - y); add = m - (dx + dy) - 1; if (add > 0) { all += add * (add + 1) / 2; } return all >= c; } int main() { ios::sync_with_stdio(0); cin >> n >> x >> y >> c; long long l = 0, h = 2e9, res = 0; while (l <= h) { long long m = (l + h) / 2; if (ch(m)) { res = m; h = m - 1; } else l = m + 1; } cout << res << endl; return 0; }
2
#include <bits/stdc++.h> int main() { int h1, h2, h3, m1, m2, m3, sum1, sum2; scanf("%d:%d", &h1, &m1); sum1 = h1 * 60 + m1; scanf("%d:%d", &h2, &m2); sum2 = h2 * 60 + m2; h3 = (sum1 + sum2) / 2 / 60; m3 = ((sum1 + sum2) / 2) % 60; printf("%02d:%02d", h3, m3); return 0; }
1
#include <cstdio> #include <vector> #include <algorithm> using namespace std; const int Max_N(131072 + 5); typedef long long int LL; void Get_Val(int &Ret) { Ret = 0; char ch; while (ch = getchar(), ch > '9' || ch < '0') ; do { (Ret *= 10) += ch - '0'; } while (ch = getchar(), ch >= '0' && ch <= '9'); } int N, Weight[Max_N]; vector<int> Son[Max_N]; void init() { Get_Val(N); for (int i = 2, fa;i <= N;++i) Get_Val(fa), Get_Val(Weight[i]), Son[fa].push_back(i); } struct node { node(const LL &_a = 0LL, const LL &_b = 0LL) : a(_a), b(_b) {} LL a, b; }; LL D; vector<node> Ans[Max_N], Tmp; inline bool comp(const node &v1, const node &v2) { return v1.a == v2.a ? v1.b < v2.b : v1.a < v2.a; } void dfs(const int &u) { Ans[u].clear(); if (Son[u].size()) { int a(Son[u][0]), b(Son[u][1]); dfs(a), dfs(b); Tmp.clear(); for (int i = 0, j = 0;i < Ans[a].size();++i) { while (j + 1 < Ans[b].size() && Ans[a][i].b + Ans[b][j + 1].a <= D) ++j; if (j < Ans[b].size() && Ans[a][i].b + Ans[b][j].a <= D) Tmp.push_back(node(Ans[a][i].a + Weight[u], Ans[b][j].b + Weight[u])); } for (int i = 0, j = 0;i < Ans[b].size();++i) { while (j + 1 < Ans[a].size() && Ans[b][i].b + Ans[a][j + 1].a <= D) ++j; if (j < Ans[a].size() && Ans[b][i].b + Ans[a][j].a <= D) Tmp.push_back(node(Ans[b][i].a + Weight[u], Ans[a][j].b + Weight[u])); } sort(Tmp.begin(), Tmp.end(), comp); if (Tmp.size()) { Ans[u].push_back(Tmp[0]); for (int i = 1;i < Tmp.size();++i) if (Tmp[i].b < Ans[u][Ans[u].size() - 1].b) Ans[u].push_back(Tmp[i]); } } else Ans[u].push_back(node(Weight[u], Weight[u])); } bool check(const LL &d) { D = d; dfs(1); return Ans[1].size() != 0; } void work() { LL l(0LL), r(1LL), mid; for (int s = 1;s <= N;++s) r += Weight[s]; while (l < r) { mid = l + ((r - l) >> 1LL); if (check(mid)) r = mid; else l = mid + 1LL; } printf("%lld", l); } int main() { init(); work(); return 0; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; int a[200010], v[200010]; bool bb; int finish; int dfs(int start, int lab) { v[start] = lab; if (v[a[start]] == lab) { finish = a[start]; return 1; } else { if (v[a[start]]) return 0; else { int ret = dfs(a[start], lab); if (ret == 0) return 0; else { if (start == finish) { bb = false; return ret + 1; } else if (bb) return ret + 1; else return ret; } } } } long long power(long long A, long long B, long long C) { long long ans = 1; while (B) { ans = ans * (B & 1 ? A : 1) % C; B /= 2; A = A * A % C; } return ans; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); int top = 0, num = 0; long long ans = 1; for (int i = 1; i <= n; i++) { if (!v[i]) { bb = true; long long m = dfs(i, ++top); if (!m) continue; num += m; ans = ans * (power(2, m, 1000000007) - 2) % 1000000007; } } ans = ans * power(2, n - num, 1000000007) % 1000000007; cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, i; cin >> n; long long int a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); reverse(a, a + n); for (i = 0; i < n; i++) cout << a[i] << " "; cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; stack<int> s; vector<int> v[30004]; int n, m, x, y; bool vis[30004]; void dfs(int pos) { vis[pos] = true; for (int i = 0; i < v[pos].size(); i++) if (!vis[v[pos][i]]) dfs(v[pos][i]); s.push(pos); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> x >> y; v[y].push_back(x); } for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i); while (!s.empty()) { cout << s.top() << " "; s.pop(); } cout << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 10, M = 1 << N; int n, m, k, q[N], e[N][N]; long long a[N][N], f[M]; long long det(int m) { long long ans = 1; for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { while (a[j][i]) { long long c = a[i][i] / a[j][i]; for (int k = i; k < m; k++) a[i][k] -= a[j][k] * c; swap(a[i], a[j]); ans = -ans; } } ans *= a[i][i]; } return ans; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1, u, v; i <= m; i++) { scanf("%d%d", &u, &v); u--; v--; e[u][v]++; e[v][u]++; } int nn = (1 << n); for (int s = 1; s < nn; s++) { long long ans = 1; m = 0; for (int i = 0; i < n; i++) if (!(s >> i & 1)) q[m++] = i; for (int i = 0, cc; i < n; i++) if (s >> i & 1) { for (int j = cc = 0; j < m; j++) cc += e[i][q[j]]; ans *= cc; } for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) a[i][j] = 0; for (int i = 0; i < m; i++) for (int j = 0; j < i; j++) { int w = e[q[i]][q[j]]; a[i][i] += w; a[j][j] += w; a[i][j] = a[j][i] = -w; } ans *= det(m - 1); if ((n + m + k) & 1) f[s] = -ans; else f[s] = ans; } for (int i = 0; i < n; i++) for (int s = 0; s < nn; s++) if (!(s >> i & 1)) f[s] += f[s | (1 << i)]; long long ans = 0; for (int s = 0; s < nn; s++) { int cc = 0; for (int i = 0; i < n; i++) if (s >> i & 1) cc++; if (cc == k) ans += f[s]; } printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long LINF = 1LL << 60; const int MAXN = 500001, INF = 1e9; int parent[MAXN], ranking[MAXN]; void UF_precalc(int n) { for (int v = 1; v <= n; v++) parent[v] = v; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } bool union_sets(int u, int v) { u = find_set(u); v = find_set(v); if (u != v) { if (ranking[u] == ranking[v]) ranking[u]++; if (ranking[u] < ranking[v]) swap(u, v); parent[v] = u; return 1; } return 0; } struct seg { int b, e, ind; }; bool cmp_b(seg X, seg Y) { return X.b < Y.b; } struct cmp_e { bool operator()(const seg X, const seg Y) { return X.e < Y.e; } }; int main() { int n; scanf("%d", &n); UF_precalc(n); vector<seg> segs(n); for (int i = 0; i < n; i++) { scanf("%d %d", &segs[i].b, &segs[i].e); segs[i].ind = i; } sort(segs.begin(), segs.end(), cmp_b); int unions = 0; set<seg, cmp_e> open; for (int cs = 0; cs < n; cs++) { int b = segs[cs].b; int e = segs[cs].e; int ind = segs[cs].ind; while (!open.empty() && (*open.begin()).e < b) open.erase(open.begin()); if (!open.empty()) for (auto it = open.begin(); it != open.end() && (*it).e < e; it++) if (!union_sets(ind, (*it).ind)) { printf("NO\n"); return 0; } else unions++; open.insert(segs[cs]); } if (unions == n - 1) printf("YES\n"); else printf("NO\n"); }
4
#include <bits/stdc++.h> using namespace std; int n, t; int allHouses = 0; int lastHouse = 0; string street; void precalc() { for (int i = 0; i < street.size(); ++i) if (street[i] == 'H') lastHouse = i; allHouses = std::count(street.begin(), street.end(), 'H'); } bool isSolution(int k) { int theEndIdx = 0; { int curK = k; while (theEndIdx < street.size() && (curK <= allHouses || lastHouse >= theEndIdx)) { if (street[theEndIdx++] == 'S') ++curK; } --theEndIdx; } vector<int> unfilled; unfilled.reserve(n); int totalCost = 0; int totalCost2 = 1000000000; int lastIdx = -1; int idx = lastIdx; int houses = 0; while (idx <= theEndIdx && houses < allHouses) { ++totalCost; ++idx; if (lastIdx >= idx) continue; lastIdx = idx; if (street[idx] == 'H') { if (k > 0) { ++houses; --k; } else { unfilled.push_back(idx); } } else if (street[idx] == 'S') { ++k; if (k == unfilled.size()) { const int cost = idx - unfilled.front(); totalCost2 = min(totalCost2, totalCost + (theEndIdx - idx) * 2 + cost); totalCost += cost - 1; idx = unfilled.front() - 1; houses += k; k = 0; if (houses == allHouses) ++totalCost; unfilled.clear(); } } } return min(totalCost, totalCost2) <= t && houses == allHouses; } int solve() { int left = -1; int right = n + 2; while (right - left > 1) { auto middle = (right + left) / 2; if (isSolution(middle)) { right = middle; } else { left = middle; } } if (left > n) { return -1; } else { return right; } } int main() { cin >> n >> t; cin >> street; precalc(); cout << solve() << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long getans(long long a, long long b) { long long x = min(a, b / 2); a -= x; b -= 2 * x; x += min(b, a / 2); return x; } void solve() { long long a, b; cin >> a >> b; if (a < b) swap(a, b); if (a >= 2 * b) { cout << min(a / 2, b) << endl; return; } long long y = (a - 2 * b) / (-3); long long x = (a - y) / 2; cout << x + y << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a[4], i, x, sum = 0; for (i = 0; i < 4; i++) cin >> a[i]; string s; cin >> s; for (i = 0; i < s.length(); i++) { x = (int)s[i] - 48; sum += a[x - 1]; } cout << sum; }
1
#include <bits/stdc++.h> using namespace std; const double eps = 1e-12; const int maxn = 100001; int n, k; double a[maxn], x, y; inline double dis(double a, double b, double c, double d) { return sqrt((a - c) * (a - c) + (b - d) * (b - d)); } inline void work1() { printf("%.10lf\n", min(dis(a[1], 0, x, y), dis(a[n], 0, x, y)) + a[n] - a[1]); } inline int dcmp(double a, double b) { if (fabs(a - b) < eps) return 1; return 0; } inline void work2() { double tmp = min(a[n] - a[k], a[k] - a[1]) + a[n] - a[1], ans; ans = a[k] - a[1] + a[n] - a[1] + dis(a[n], 0, x, y); ans = min(ans, a[n] - a[k] + a[n] - a[1] + dis(a[1], 0, x, y)); if (k > 1) ans = min(ans, a[n] - a[k] + dis(a[n], 0, x, y) + dis(a[k - 1], 0, x, y) + a[k - 1] - a[1]); if (k < n) ans = min(ans, a[k] - a[1] + dis(a[1], 0, x, y) + dis(a[k + 1], 0, x, y) + a[n] - a[k + 1]); int j = 0, K = 0; double ans1 = 1e18, ans2 = 1e18, ans3 = 1e18, ans4 = 1e18; for (int i = 1; i <= n; i++) if (a[i] <= x) j = i; for (int i = n; i >= 1; i--) if (a[i] > x) K = i; if (j && K) ans = min(ans, tmp - (a[K] - a[j]) + dis(x, y, a[j], 0) + dis(x, y, a[K], 0)); j = 0, K = 0; for (int i = 1; i <= n; i++) if (a[i] < x) j = i; for (int i = n; i >= 1; i--) if (a[i] >= x) K = i; if (j && K) ans = min(ans, tmp - (a[K] - a[j]) + dis(x, y, a[j], 0) + dis(x, y, a[K], 0)); for (int i = k; i < n; i++) ans = min(ans, min(a[i] - a[k] + a[i] - a[1] + dis(a[1], 0, x, y) + dis(x, y, a[i + 1], 0) + a[n] - a[i + 1], a[k] - a[1] + a[i] - a[1] + dis(a[i], 0, x, y) + dis(x, y, a[i + 1], 0) + a[n] - a[i + 1])); for (int i = 2; i <= k; i++) ans = min(ans, min(a[k] - a[i] + a[n] - a[i] + dis(a[n], 0, x, y) + dis(x, y, a[i - 1], 0) + a[i - 1] - a[1], a[n] - a[k] + a[n] - a[i] + dis(a[i], 0, x, y) + dis(x, y, a[i - 1], 0) + a[i - 1] - a[1])); printf("%.10lf\n", ans); } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%lf", &a[i]); double t = a[k]; scanf("%lf%lf", &x, &y); sort(a + 1, a + n + 1); if (k == n + 1) work1(); else { for (int i = 1; i <= n; i++) if (dcmp(a[i], t)) k = i; work2(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const double PI = 2 * acos(0.0); const int Numnodes = ((int)1e5) + 3; int main() { int N, K; cin >> N >> K; if (K == 10) { if (N == 1) return cout << -1 << endl, 0; for (long long int i = 0; i < (N - 1); i++) cout << 1; cout << 0; } else for (long long int i = 0; i < (N); i++) cout << K; }
1
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0'; } const int dlt = 310; const int U[4] = {1, -1, 0, 0}; const int V[4] = {0, 0, 1, -1}; const char ch[4] = {'R', 'L', 'U', 'D'}; const int N = 1000; queue<pair<int, int> > Q; inline int In_box(int x, int y) { return x >= 170 && x <= 470 && y >= 170 && y <= 470; } inline int In_full(int x, int y) { return x >= 0 && x <= 710 && y >= 0 && y <= 710; } struct point { int x, y; point() {} point(int _x, int _y) : x(_x), y(_y) {} } P[N]; int al, Sx, Sy, Tx, Ty, cnt, tree[N][N], vis[N][N], pre[N][N], ol, SSx, SSy, TTx, TTy; int O[110000], n, flag; char Ans[1200000]; int opt(char x) { if (x == 'R') return 0; else if (x == 'L') return 1; else if (x == 'U') return 2; else return 3; } void check() { int x = SSx, y = SSy, xx = TTx, yy = TTy; for (int i = 1; i <= al; i++) { x += U[opt(Ans[i])], y += V[opt(Ans[i])]; xx += U[opt(Ans[i])], yy += V[opt(Ans[i])]; if (tree[xx][yy]) xx -= U[opt(Ans[i])], yy -= V[opt(Ans[i])]; if (tree[x][y]) { printf("%d %d\n", x, y); puts("fail"); exit(0); } } if (x != Sx || y != Sy || xx != Tx || yy != Ty) { printf("%d %d %d %d\n", x, y, xx, yy); printf("%d %d %d %d\n", Sx, Sy, Tx, Ty); puts("fail"); exit(0); } } void sc() { check(); for (int i = 1; i <= al; i++) printf("%c", Ans[i]); exit(0); } void Go() { while (Q.size()) Q.pop(); Q.push(make_pair(Sx, Sy)); ++cnt; while (Q.size()) { int x = Q.front().first, y = Q.front().second; Q.pop(); if (x == Tx && y == Ty) break; for (int i = 0; i <= 3; i++) if (In_full(x + U[i], y + V[i]) && !tree[x + U[i]][y + V[i]] && vis[x + U[i]][y + V[i]] != cnt) { pre[x + U[i]][y + V[i]] = i; vis[x + U[i]][y + V[i]] = cnt; Q.push(make_pair(x + U[i], y + V[i])); } } } void Update() { int x = Tx, y = Ty; ol = 0; while (x != Sx || y != Sy) { O[++ol] = pre[x][y]; x -= U[O[ol]], y -= V[O[ol]]; } Sx = Tx, Sy = Ty; x = Tx, y = Ty; for (int i = ol; i >= 1; i--) { Ans[++al] = ch[O[i]]; if (!tree[x + U[O[i]]][y + V[O[i]]]) x += U[O[i]], y += V[O[i]]; } Tx = x, Ty = y; } void Up_Right() { if (Sy >= 170 && Sy <= 470) { while (min(Sy, Ty) < 500) Ans[++al] = 'U', Sy++, Ty++; while (min(Sx, Tx) < 500) Ans[++al] = 'R', Sx++, Tx++; } else { while (min(Sx, Tx) < 500) Ans[++al] = 'R', Sx++, Tx++; while (min(Sy, Ty) < 500) Ans[++al] = 'U', Sy++, Ty++; } } void Down_Left() { if (Sy >= 170 && Sy <= 470) { while (max(Sy, Ty) > 150) Ans[++al] = 'D', Sy--, Ty--; while (max(Sx, Tx) > 150) Ans[++al] = 'L', Sx--, Tx--; } else { while (max(Sx, Tx) > 150) Ans[++al] = 'L', Sx--, Tx--; while (max(Sy, Ty) > 150) Ans[++al] = 'D', Sy--, Ty--; } } void Right() { if (Sx <= Tx) return; int x = 0, y = 0; for (int i = 1; i <= n; i++) if (P[i].x > x) x = P[i].x, y = P[i].y; while (Tx > x + 1) Ans[++al] = 'L', Sx--, Tx--; while (Ty > y) Ans[++al] = 'D', Sy--, Ty--; while (Ty < y) Ans[++al] = 'U', Sy++, Ty++; while (Sx > x + 1) Ans[++al] = 'L', Sx--; if (Sx == Tx && Sy == Ty) sc(); } void Left() { if (Sx >= Tx) return; int x = 1000, y = 1000; for (int i = 1; i <= n; i++) if (P[i].x < x) x = P[i].x, y = P[i].y; while (Tx < x - 1) Ans[++al] = 'R', Sx++, Tx++; while (Ty > y) Ans[++al] = 'D', Sy--, Ty--; while (Ty < y) Ans[++al] = 'U', Sy++, Ty++; while (Sx < x - 1) Ans[++al] = 'R', Sx++; if (Sx == Tx && Sy == Ty) sc(); } void Up() { if (Sy <= Ty) return; int x = 0, y = 0; for (int i = 1; i <= n; i++) if (P[i].y > y) x = P[i].x, y = P[i].y; while (Ty > y + 1) Ans[++al] = 'D', Sy--, Ty--; while (Tx > x) Ans[++al] = 'L', Sx--, Tx--; while (Tx < x) Ans[++al] = 'R', Sx++, Tx++; while (Sy > y + 1) Ans[++al] = 'D', Sy--; if (Sx == Tx && Sy == Ty) sc(); } void Down() { if (Sy >= Ty) return; int x = 1000, y = 1000; for (int i = 1; i <= n; i++) if (P[i].y < y) x = P[i].x, y = P[i].y; while (Ty < y - 1) Ans[++al] = 'U', Sy++, Ty++; while (Tx > x) Ans[++al] = 'L', Sx--, Tx--; while (Tx < x) Ans[++al] = 'R', Sx++, Tx++; while (Sy < y - 1) Ans[++al] = 'U', Sy++; if (Sx == Tx && Sy == Ty) sc(); } int main() { scanf("%d%d%d%d%d", &Sx, &Sy, &Tx, &Ty, &n); if (Sx == Tx && Sy == Ty) { puts(""); return 0; } if (n == 0) { puts("-1"); return 0; } Sx += dlt; Sy += dlt; Tx += dlt; Ty += dlt; int x, y; SSx = Sx; SSy = Sy; TTx = Tx; TTy = Ty; for (int i = 1; i <= n; i++) scanf("%d%d", &x, &y), x += dlt, y += dlt, tree[x][y] = 1, P[i] = point(x, y); Go(); if (vis[Tx][Ty] != cnt) { puts("-1"); return 0; } while (1) { if (Sx == Tx && Sy == Ty) sc(); if (!In_box(Sx, Sy) && !In_box(Tx, Ty)) break; Go(); Update(); } Up_Right(); Right(); Up_Right(); Up(); Up_Right(); Down_Left(); Down(); Down_Left(); Left(); sc(); }
5
#include <stdio.h> int ball[10]; bool can(int n,int left,int right){ bool flag; if(n==10) return true; if(ball[n]>right && can(n+1,left ,ball[n])) return true; if(ball[n]>left && can(n+1,ball[n],right)) return true; return false; } int main(){ int N; scanf("%d",&N); for(int i=0;i<N;i++){ for(int j=0;j<10;j++){ scanf("%d",&ball[j]); } printf(can(0,0,0)?"YES\n":"NO\n"); } }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000") using namespace std; const int N = 100010; int main() { int n; scanf("%d", &n); printf("YES\n"); for (; n--;) { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); int M = 1000000010; int c = (x1 + M) % 2 * 2 + (y1 + M) % 2; printf("%d\n", c + 1); } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int dp[360361]; long long int k; bool reach(long long int from, long long int to) { if (from <= to) return false; long long int d = from - to; if (d == 1) return true; for (long long int i = (long long int)(from - to + 1); i < ((long long int)(k + 1)); i++) if (to % i == 0 && to + i > from) return true; return false; } long long int calc(long long int from, long long int to) { for (long long int i = (long long int)(from); i < ((long long int)(to + 1)); i++) dp[i] = 999999; dp[from] = 0; for (long long int i = (long long int)(from + 1); i < ((long long int)(to + 1)); i++) { for (long long int j = i - 1; j >= max(from, i - k + 1); j--) { if (reach(i, j)) dp[i] = min(dp[i], dp[j] + 1); } } return dp[to]; } int main() { long long int a, b; cin >> a >> b >> k; a -= 360360 * (b / 360360); b = b % 360360; if (a < 360360) { cout << calc(b, a) << endl; } else { long long int res = 0; res += calc(b, 360360); long long int nums = (a - (a % 360360) - 360360) / 360360; res += calc(0, 360360) * nums; res += calc(0, a % 360360); cout << res << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; signed main(){ string s;cin>>s; int ans=0; int n=s.size(); for(int i=0;i<n/2;i++){ if(s[i]!=s[n-i-1])ans++; } cout<<ans<<endl; }
0
#include <bits/stdc++.h> using namespace std; int get() { char c; int res = 0; while (c = getchar(), !isdigit(c)) ; do { res = (res << 3) + (res << 1) + (c - '0'); } while (c = getchar(), isdigit(c)); return res; } struct point { double x, y; int made; inline point(double _x = 0.0, double _y = 0.0) { x = _x; y = _y; } inline point operator-(const point &b) const { return point(x - b.x, y - b.y); } inline point operator+(const point &b) const { return point(x + b.x, y + b.y); } inline point operator|(const double &b) const { return point(x * b, y * b); } inline double operator^(const point &b) const { return x * b.y - y * b.x; } inline double operator*(const point &b) const { return x * b.x + y * b.y; } inline void input() { scanf("%lf%lf", &x, &y); } }; double A[700001], ans, tmp, tt; int l, r, n, k, i, j, jj, N, cnt; int main() { scanf("%d%d", &n, &k); N = 700000 / k; for (i = 1; i <= n; i++) { ans = 0; scanf("%d", &l); cnt = 0; for (j = 1; j <= k; j++) { scanf("%d", &r); tmp = 1.0 * (r - l) / N; for (jj = 0; jj <= N; jj++) { tt = 1.0 * l + tmp * jj; if (tt > A[cnt]) { ans += (tt - A[cnt]); A[cnt] = tt; } cnt++; } cnt--; l = r; } printf("%lf\n", ans / N); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int infint = INT_MAX; const long long infll = LLONG_MAX; int arr[600][600]; int score[600]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; for (int i = 0; i < n; i++) { int scr = 0, loc = 0; for (int j = 0; j < m; j++) { int v; cin >> v; arr[i][j] = v; if (v == 0) loc = 0; else loc++; scr = max(scr, loc); } score[i] = scr; } while (q--) { int y, x; cin >> y >> x; y--; x--; if (arr[y][x] == 1) { arr[y][x] = 0; } else { arr[y][x] = 1; } int mx = 0; int loc = 0; score[y] = 0; for (int i = 0; i < m; i++) { if (arr[y][i] == 1) loc++; else loc = 0; score[y] = max(score[y], loc); } for (int i = 0; i < n; i++) { mx = max(mx, score[i]); } cout << mx << endl; } return 0; }
2
#include <stdio.h> #include <algorithm> using namespace std; int main(){ int i,k,p[3],j[2]; for(i = 0;i < 3;i++) scanf("%d",&p[i]); for(i = 0;i < 2;i++) scanf("%d",&j[i]); sort(p,p + 3); sort(j,j + 2); printf("%d\n",p[0] + j[0] - 50); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; float s = 0, d; cin >> n; m = n; while (n--) { cin >> d; s = s + d; } float f = s / (m); printf("%lf", f); }
2
#include <bits/stdc++.h> using namespace std; template <class T> inline T BM(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T mdINV(T a, T M) { return BM(a, M - 2, M); } template <class T> inline T PW(T p, T e) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } template <class T> string NTS(T Number) { stringstream ss; ss << Number; return ss.str(); } template <class T> T stringtonumber(const string &Text) { istringstream ss(Text); T result; return ss >> result ? result : 0; } template <class T> bool ISLEFT(T a, T b, T c) { if (((a.first.first - b.first.first) * (b.second.second - c.second.second) - (b.first.first - c.first.first) * (a.second.second - b.second.second)) < 0.0) return 1; else return 0; } char a[2000000 + 2], b[2000000 + 2]; int main() { scanf("%s%s", a + 1, b + 1); int l = strlen(a + 1); int l1 = strlen(b + 1); int ar[l + 2]; for (int i = 1; i <= l; i++) scanf("%d", &ar[i]); int lo = 1, hi = l; int ans = 0; bool op[l + 2]; while (lo <= hi) { int mid = (lo + hi) / 2; int st = 1; memset(op, 0, sizeof op); for (int i = 1; i <= mid; i++) op[ar[i]] = 1; bool fl = 0; for (int i = 1; i <= l1; i++) { while (st <= l && (b[i] != a[st] || op[st] == 1)) st++; if (st > l) fl = 1; st++; } if (!fl) { lo = mid + 1; ans = mid; } else hi = mid - 1; } printf("%d", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, x, y; cin >> a >> b >> x >> y; int row1 = b * (a - (x + 1)); int row2 = b * (a - (a - (x))); int rowm = max(row1, row2); int col1 = a * (b - (y + 1)); int col2 = a * (b - (b - (y))); int colm = max(col1, col2); cout << max(rowm, colm) << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d = 1, i; cin >> a >> b; c = min(a, b); for (i = 1; i <= c; i++) { d = d * i; } cout << d; }
1
#include <bits/stdc++.h> using namespace std; const long long pinf = 1e18 + 4; const long long ninf = -pinf; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; long long d = 9 + 8 * (n + k); d = sqrt(d); long long ans = n - (d - 3) / 2; cout << ans; }
2
// abc161_e2.cc #include <bits/stdc++.h> using namespace std; int main() { int n, k, c; cin >> n >> k >> c; string s; cin >> s; vector<int> ans1, ans2; for (int i = 0; i < n; i++) { if (s[i] == 'o' && ans1.size() < k) { ans1.push_back(i); i += c; } } for (int i = n - 1; i >= 0; i--) { if (s[i] == 'o' && ans2.size() < k) { ans2.push_back(i); i -= c; } } for (int i = 0; i < k; i++) { if (ans1[i] == ans2[k - i - 1]) cout << ans1[i] + 1<< "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<string> v; int c[100][100]; int f[300]; int main() { f['W'] = 1; f['B'] = -1; int n, m; cin >> n >> m; v = vector<string>(n); for (int i = 0; i < n; ++i) cin >> v[i]; int ans = 0; for (int i = n - 1; i >= 0; --i) for (int j = m - 1; j >= 0; --j) if (c[i][j] != f[v[i][j]]) { ans++; int diff = f[v[i][j]] - c[i][j]; for (int x = 0; x <= i; ++x) for (int y = 0; y <= j; ++y) c[x][y] += diff; } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define fastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long int #define pb push_back #define ff first #define ss second #define all(v) (v).begin(), (v).end() #define mod (int)(998244353) #define PI 3.14159265358979323846264338327950L // ------------------ Debugging ------------------ #define trace(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);cout << endl; } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << "[ " << *it << " = " << a << " ] "; err(++it, args...); } template <typename Tk, typename Tv> ostream& operator<<(ostream& os, const pair<Tk, Tv> &p){os << "{" << p.first << ',' << p.second << "}";return os;} template <typename T> ostream& operator<<(ostream& os, const vector<T> &p){os << "[ "; for (T x: p) os << x << " "; os << "]" << endl; return os;} template <typename T> ostream& operator<<(ostream& os, const set<T> &p){os << "{ "; for (T x: p) os << x << " "; os << "}" << endl; return os;} template <typename Tk, typename Tv> ostream& operator<<(ostream& os, const map<Tk, Tv> &p){os << "{ "; for (pair<Tk, Tv> x: p) os << x << " "; os << "}" << endl; return os;} #define imie(args) cout << #args << ": "; cout << args; // ----------------------------------------------- signed main(){ fastIO int t = 1; cin >> t; while (t--){ int n; cin >> n; vector<pair<pair<int, int>, int>> ms; for (int i = 0; i < n; i++){ int h, w; cin >> h >> w; if (w < h) { swap(h, w); } ms.pb({{h, w}, i}); } // a < b sort(all(ms), [&](pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { if (a.ff.ff == b.ff.ff) { return a.ff.ss < b.ff.ss; } return a.ff.ff < b.ff.ff; }); vector<int> ans(n, -1); int small = 1e18, start = 0, ind = -1; for (int i = 1; i < n; i++) { if (ms[i].ff.ff > ms[i-1].ff.ff) { for (int j = start; j < i; j++) { if (small > ms[j].ff.ss) { small = ms[j].ff.ss; ind = ms[j].ss; } } start = i; } if (small < ms[i].ff.ss) { ans[ms[i].ss] = ind+1; } } for (int i : ans) { cout << i << " "; } cout << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res % p; } long long int modInverse(long long int n, long long int p) { return power(n, p - 2, p); } long long int nCrModPFermat(long long int n, long long int r, long long int p, long long int* fac) { if (r == 0 || n == r) return 1; long long int a = ((fac[n] % p) * (modInverse(fac[r], p) % p)) % p; long long int b = modInverse(fac[n - r], p) % p; return (a * b) % p; } void solve() { long long int n, m; cin >> n >> m; long long int* arr = new long long int[n]; for (int i = 0; i < n; i++) cin >> arr[i]; vector<long long int> v; long long int* freq = new long long int[101](); for (int i = 0; i < n; i++) { long long int req = m - arr[i]; long long int count = 0; for (int j = 1; j < 101; j++) { if (j * freq[j] <= req) { req -= (j * freq[j]); count += freq[j]; } else { count += (req / j); break; } } cout << i - count << " "; freq[arr[i]]++; } cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; t = 1; for (int y = 1; y <= t; y++) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string s, h; cin >> s; h = "hello"; int j = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == h[j]) { j++; } } if (j == 5) cout << "YES"; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef pair<int,int>P; int t[40000],u[40000],v[40000]; #define B 200 class UnionFind { vector<int>par, sz; public: UnionFind(int n) { par = sz = vector<int>(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; } } int find(int x) { if (par[x] == x)return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y)return; if (sz[x] > sz[y]) { par[y] = x; sz[x] += sz[y]; } else { par[x] = y; sz[y] += sz[x]; } } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sz[find(x)]; } }; multiset<int>E[40000]; int d[40000]; int main(){ int n,k;scanf("%d%d",&n,&k); rep(i,k){ scanf("%d%d%d",&t[i],&u[i],&v[i]); } set<P>se; for(int i=0;i<k;i+=B){ set<P>s2,s3; for(int j=i;j<k&&j-i<B;j++){ if(t[j]==2){ if(se.count(P(u[j],v[j]))){ se.erase(P(u[j],v[j])); s3.insert(P(u[j],v[j])); } } } UnionFind uf(n); for(auto p:se)uf.unite(p.first,p.second); rep(j,n)E[j].clear(); for(auto p:s3){ E[uf.find(p.first)].insert(uf.find(p.second)); E[uf.find(p.second)].insert(uf.find(p.first)); } for(int j=i;j<k&&j-i<B;j++){ if(t[j]==1){ s2.insert(P(u[j],v[j])); u[j]=uf.find(u[j]);v[j]=uf.find(v[j]); E[u[j]].insert(v[j]); E[v[j]].insert(u[j]); } else if(t[j]==2){ s2.erase(P(u[j],v[j])); u[j]=uf.find(u[j]);v[j]=uf.find(v[j]); E[u[j]].erase(E[u[j]].find(v[j])); E[v[j]].erase(E[v[j]].find(u[j])); } else{ memset(d,0,sizeof(d)); queue<int>que; d[uf.find(u[j])]=1; que.push(uf.find(u[j])); while(!que.empty()){ int p=que.front();que.pop(); for(int v:E[p]){ if(d[v]==0){ d[v]=1;que.push(v); } } } if(d[uf.find(v[j])]==1)puts("YES"); else puts("NO"); } } for(auto p:s2)se.insert(p); } }
0
#include <bits/stdc++.h> using namespace std; inline int read() { int k = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { k = k * 10 + ch - '0'; ch = getchar(); } return k * f; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline void writeln(int x) { write(x); puts(""); } int nedge = 0, p[200010], nex[200010], head[200010]; int a[100010], n, m, f[100010], rd[100010]; queue<int> q1, q2; inline void addedge(int a, int b) { p[++nedge] = b; nex[nedge] = head[a]; head[a] = nedge; } inline void work() { while (!q2.empty()) { int now = q2.front(); q2.pop(); for (int k = head[now]; k; k = nex[k]) { rd[p[k]]--; if (rd[p[k]]) continue; if (a[p[k]]) q1.push(p[k]); else q2.push(p[k]); } } } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= m; i++) { int x = read() + 1, y = read() + 1; addedge(y, x); rd[x]++; } for (int i = 1; i <= n; i++) if (!rd[i]) { if (a[i]) q1.push(i); else q2.push(i); } work(); int ans = 0; while (!q1.empty()) { ans++; while (!q1.empty()) { int now = q1.front(); q1.pop(); for (int k = head[now]; k; k = nex[k]) { rd[p[k]]--; if (rd[p[k]]) continue; if (a[p[k]]) q1.push(p[k]); else q2.push(p[k]); } } work(); } writeln(ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; pair<int, int> first[301]; inline double ccw(pair<int, int> A, pair<int, int> B, pair<int, int> C) { return ((double)(B.first - A.first) * (C.second - B.second) - (B.second - A.second) * (C.first - B.first)) * 0.5; } inline double chmax(double &A, double B) { if (B - A > 0.00000001) A = B; } int main() { int n; double maxArea = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", &first[i].first, &first[i].second); for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) { double maxMinus = -1, maxPlus = -1; for (int k = 1; k <= n; ++k) if (k != i && k != j) if (ccw(first[i], first[j], first[k]) < 0) chmax(maxMinus, -ccw(first[i], first[j], first[k])); else chmax(maxPlus, ccw(first[i], first[j], first[k])); if (maxPlus >= 0 && maxMinus >= 0) chmax(maxArea, maxMinus + maxPlus); } printf("%lf", maxArea); return 0; }
2
#include <bits/stdc++.h> long long ans[300009]; int valX[300009], valY[300009]; struct myc { int x, y, z; inline bool operator<(const myc &u) const { return y - x > u.y - u.x; } } v[300009]; int main() { int n, m; fscanf(stdin, "%d%d", &n, &m); long long sumX = 0, sumY = 0; for (int i = 1; i <= n; i++) { fscanf(stdin, "%d%d", &v[i].x, &v[i].y); sumX += v[i].x; sumY += v[i].y; v[i].z = i; valX[i] = v[i].x; valY[i] = v[i].y; } std::sort(v + 1, v + n + 1); long long sumaX = 0, sumaY = 0; for (int i = 1; i <= n; i++) { ans[v[i].z] = sumaX + 1LL * v[i].y * (i - 1) + (sumY - sumaY - v[i].y) + 1LL * v[i].x * (n - i); sumaX += v[i].x; sumaY += v[i].y; } for (int i = 1; i <= m; i++) { int a, b; fscanf(stdin, "%d%d", &a, &b); int r = std::min(valX[a] + valY[b], valX[b] + valY[a]); ans[a] -= r; ans[b] -= r; } for (int i = 1; i <= n; i++) fprintf(stdout, "%I64d ", ans[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m; int b[5005], c[5005]; double a[5005]; int main() { while (cin >> n >> m) { for (int i = 1; i <= n; i++) { cin >> b[i] >> c[i]; b[i] *= m; a[i] = b[i] * (1.0) / c[i]; } sort(a + 1, a + 1 + n); printf("%.8lf\n", a[1]); } return 0; }
1
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { x = 0; int f = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') { f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } int i, j, k, m, n; const int N = 5e3 + 10, mod = 1000000007; int dp[N][N]; char s[N], t[N]; int main() { scanf("%s%s", s + 1, t + 1); n = strlen(s + 1), m = strlen(t + 1); for (i = 1; i <= n; i++) dp[i][1] = (s[i] == t[1]); for (i = 1; i <= m; i++) dp[1][i] = dp[1][i - 1] + (s[1] == t[i]); for (i = 2; i <= n; i++) for (j = 2; j <= m; j++) dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1] * (s[i] == t[j]) + (s[i] == t[j])) % mod; int ans = 0; for (i = 1; i <= n; i++) ans = (ans + dp[i][m]) % mod; cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m, s[200005], tot, ta, tb; vector<pair<int, int> > v[200005]; bool vis[200005]; int ans[200005]; int sum[200005]; void dfs(int a) { vis[a] = 1; sum[a] = s[a]; for (int i = 0; i < v[a].size(); i++) if (vis[v[a][i].first] != 1) { dfs(v[a][i].first); sum[a] += sum[v[a][i].first]; if (v[a][i].second > 0) ans[v[a][i].second] = sum[v[a][i].first]; else ans[-v[a][i].second] = -sum[v[a][i].first]; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", s + i); tot += s[i]; } if (tot) printf("Impossible\n"); else { printf("Possible\n"); scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%d%d", &ta, &tb); v[ta].push_back({tb, i}); v[tb].push_back({ta, -i}); } dfs(1); for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); } return 0; }
6