solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100 + 20; int a[MAX_N]; int P[MAX_N]; int mark[MAX_N]; int n, q; void dfs(int v, int r) { mark[v] = 1; if (v != r && !mark[v + 1] && P[v] < P[v + 1]) dfs(v + 1, r); } int main() { ios::sync_with_stdio(false); cin >> n; for (int i(0); i < int(n); i++) { cin >> a[i]; --a[i]; P[a[i]] = i; } cin >> q; while (q--) { int k, l, r; cin >> k >> l >> r; --l, r--; if (k == 2) { swap(P[a[l]], P[a[r]]); swap(a[l], a[r]); } else { int Sum = 0; for (int i(0); i < int(n); i++) mark[i] = 0; for (int i(l); i < int(r + 1); i++) if (!mark[i]) { ++Sum; dfs(i, r); } cout << Sum << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 1e6 + 5; int K; long long cnt[105]; vector<vector<long long>> iden; long long mm[N]; vector<vector<long long>> mul(vector<vector<long long>> A, vector<vector<long long>> B) { vector<vector<long long>> C(K + 1, vector<long long>(K + 1)); for (int i = int(0); i < int(K); ++i) for (int j = int(0); j < int(K); ++j) for (int k = int(0); k < int(K); ++k) C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod; return C; } vector<vector<long long>> binpow(vector<vector<long long>> A, long long p) { if (p == 0) return iden; if (p == 1) return A; vector<vector<long long>> ret = binpow(A, p >> 1); if (p & 1) return mul(ret, mul(ret, A)); return mul(ret, ret); } vector<long long> mul2(vector<vector<long long>> A, vector<long long> v) { vector<long long> ret(K + 1); for (int i = int(0); i < int(K); ++i) for (int j = int(0); j < int(K); ++j) ret[i] = (ret[i] + v[j] * A[i][j]) % mod; return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, p; cin >> n >> p >> K; vector<long long> ini(K + 1); for (int i = int(0); i < int(K + 1); ++i) { vector<long long> uwu; for (int j = int(0); j < int(K + 1); ++j) uwu.push_back((i == j)); iden.push_back(uwu); } iden[K][K] = 0; for (int i = int(0); i < int(n); ++i) { int k; cin >> k; ini[k % K]++; } for (int i = int(0); i < int(n); ++i) { int k; cin >> k; cnt[k % K]++; mm[i] = k; } vector<vector<long long>> base(K + 1, vector<long long>(K + 1)); for (int i = int(0); i < int(K); ++i) for (int j = int(0); j < int(K); ++j) { int val = (i - j + K) % K; base[i][j] = cnt[val]; } base = binpow(base, p - 2); vector<long long> vec = mul2(base, ini); long long ans = 0; for (int i = int(0); i < int(n); ++i) { int k; cin >> k; mm[i] = (mm[i] + k) % K; ans = (ans + vec[(K - mm[i]) % K]) % mod; } cout << ans << endl; return 0; }
2
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 1333 inline int read(){ int x=0,f=1; char c=getchar(); while(c<'0'||c>'9'){ if(c=='-')f=-1; c=getchar(); } while(c>='0'&&c<='9'){ x=(x<<1)+(x<<3)+c-'0'; c=getchar(); } return x*f; } int a[N][N],n,m; void Copy(int x,int y,int len){ for(int i=1;i<=len;++i){ for(int j=1;j<=len;++j){ a[i+x][j+y]=a[i][j]; } } } void dfs(int n){ if(n==1)return (void)(a[1][1]=1); dfs(n>>1); Copy(0,(n>>1)+1,n>>1); Copy((n>>1)+1,0,n>>1); Copy((n>>1)+1,(n>>1)+1,n>>1); a[(n>>1)+1][(n>>1)+1]=1; } int main(){ n=read(),m=read(); dfs((1<<max(n,m))-1); for(int i=1;i<=(1<<n)-1;++i){ for(int j=1;j<=(1<<m)-1;++j){ putchar(a[i][j]+'0'); } putchar('\n'); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 111; const double eps = 1e-8; const double pi = acos(-1.0); inline double sqr(double x) { return x * x; } inline int sgn(double x) { return fabs(x) < eps ? 0 : x > eps ? 1 : -1; } struct point { double x, y; point(double _x = 0, double _y = 0) : x(_x), y(_y) {} void input(void) { scanf("%lf%lf", &x, &y); } void output(void) { printf("%f %f\n", x, y); } bool operator==(const point &p) const { return sgn(x - p.x) == 0 && sgn(y - p.y) == 0; } bool operator<(const point &p) const { return sgn(y - p.y) < 0 || (sgn(y - p.y) == 0 && sgn(x - p.x) < 0); } point operator+(const point &p) const { return point(x + p.x, y + p.y); } point operator-(const point &p) const { return point(x - p.x, y - p.y); } point operator*(double k) { return point(x * k, y * k); } point operator/(double k) { return point(x / k, y / k); } double operator*(const point &p) const { return x * p.x + y * p.y; } double operator^(const point &p) const { return x * p.y - y * p.x; } double norm(void) { return hypot(x, y); } double norm2(void) { return sqr(x) + sqr(y); } double dist(const point &p) const { return hypot(x - p.x, y - p.y); } double dist2(const point &p) const { return sqr(x - p.x) + sqr(y - p.y); } }; struct line { point a, b; line() {} line(point _a, point _b) : a(_a), b(_b) {} point cross_point(line v) { double a1 = (v.b - v.a) ^ (a - v.a); double a2 = (v.b - v.a) ^ (b - v.a); return point((a.x * a2 - b.x * a1) / (a2 - a1), (a.y * a2 - b.y * a1) / (a2 - a1)); } }; struct polygon { int n; point p[N]; void input(void) { for (int i = 0; i < n; i++) p[i].input(); } double get_area(void) { double sum = 0; for (int i = 0; i < n; i++) sum += p[i] ^ p[(i + 1) % n]; return fabs(sum) / 2; } bool get_dir(void) { double sum = 0; for (int i = 0; i < n; i++) sum += p[i] ^ p[(i + 1) % n]; return sgn(sum) > 0; } }; struct polygons { vector<polygon> p; vector<pair<double, int> > e; polygons() { p.clear(); } void clear(void) { p.clear(); } void push(polygon q) { if (sgn(q.get_area())) p.push_back(q); } void ins(point st, point ed, point X, int i) { double r = fabs(ed.x - st.x) > eps ? (X.x - st.x) / (ed.x - st.x) : (X.y - st.y) / (ed.y - st.y); r = min(r, 1.0); r = max(r, 0.0); e.push_back(make_pair(r, i)); } double polygon_area_union(void) { double res = 0.0; int c0, c1, c2, c3, dp, k, l; size_t i, j; for (i = 0; i < p.size(); i++) if (!p[i].get_dir()) reverse(p[i].p, p[i].p + p[i].n); for (i = 0; i < p.size(); i++) for (k = 0; k < p[i].n; k++) { point &s = p[i].p[k], &t = p[i].p[(k + 1) % p[i].n]; if (!sgn(s ^ t)) continue; e.clear(); e.push_back(make_pair(0.0, 1)); e.push_back(make_pair(1.0, -1)); for (j = 0; j < p.size(); j++) if (i != j) for (l = 0; l < p[j].n; l++) { point a = p[j].p[l], b = p[j].p[(l + 1) % p[j].n], c = p[j].p[(l - 1 + p[j].n) % p[j].n]; c0 = sgn((t - s) ^ (c - s)); c1 = sgn((t - s) ^ (a - s)); c2 = sgn((t - s) ^ (b - s)); if (c1 * c2 < 0) ins(s, t, line(s, t).cross_point(line(a, b)), -c2); else if (!c1 && c0 * c2 < 0) ins(s, t, a, -c2); else if (!c1 && !c2) { c3 = sgn((t - s) ^ (p[j].p[(l + 2) % p[j].n] - s)); dp = sgn((t - s) * (b - a)); if (dp && c0) ins(s, t, a, dp > 0 ? c0 * ((j > i) ^ (c0 < 0)) : -(c0 < 0)); if (dp && c3) ins(s, t, b, dp > 0 ? -c3 * ((j > i) ^ (c3 < 0)) : c3 < 0); } } sort(e.begin(), e.end()); int ct = 0; double tot = 0.0, last; for (j = 0; j < e.size(); j++) { if (ct == 1) tot += e[j].first - last; ct += e[j].second; last = e[j].first; } res += (s ^ t) * tot; } return fabs(res) * 0.5; } }; int main(void) { polygons P; polygon tp; tp.n = 4; int n; double res = 0; scanf("%d", &n); if (n == 1) { puts("1"); return 0; } for (int i = 0; i < n; i++) { tp.input(); res += tp.get_area(); P.push(tp); } printf("%f\n", res / P.polygon_area_union()); return 0; }
5
#include <bits/stdc++.h> const long long int mod = 1e9 + 7; using namespace std; void myfun() { long long int n, i; cin >> n; map<long long int, long long int> mp; for (i = 0; i < n; i++) { long long int a; cin >> a; mp[a]++; } multiset<long long int> ss; for (auto it : mp) ss.insert(it.second); long long int ans = 1; for (i = 1; i <= n; i++) { long long int cur = i, sum = 0; multiset<long long int> sss; while (ss.size() > 0) { auto it = ss.lower_bound(cur); if (it == ss.end()) break; sum += cur; sss.insert(*it); ss.erase(it); cur = 2 * cur; } ans = max(ans, sum); for (auto it : sss) ss.insert(it); } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long int t = 1; while (t--) myfun(); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long nax = 2400000; char a[nax]; void solve() { cin >> a; for (long long x = 0; x < 10; x++) { for (long long y = 0; y < 10; y++) { long long b[10]; for (long long i = 0; i < 10; i++) b[i] = 1e9 + 7; for (long long q = 0; q < 10; q++) for (long long p = 0; p < 10; p++) if (p or q) b[(p * x + q * y) % 10] = min(b[(p * x + q * y) % 10], p + q - 1); long long res = 0; for (long long i = 0; a[i + 1]; i++) { if (b[(a[i + 1] - a[i] + 10) % 10] == 1e9 + 7) { res = -1; break; } res += b[(a[i + 1] - a[i] + 10) % 10]; } cout << res << ' '; } cout << '\n'; } } int32_t main() { long long t = 1; while (t--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; map<string, pair<long long, long long> > w, bs; map<string, vector<string> > pck; map<string, int> tp, vis; pair<long long, long long> go(string s) { if (vis.count(s)) return w[s]; vis[s] = 1; bool in = 0; pair<long long, long long> res(0, 0); for (int i = 0; i < pck[s].size(); i++) { in = 1; pair<long long, long long> tmp = go(pck[s][i]); if (tp[s] == 1) { if (i) res.first += bs[s].second; res.first += tmp.first; res.second = max(res.second, tmp.second); } else { if (i) res.second += bs[s].second; res.first = max(res.first, tmp.first); res.second += tmp.second; } } if (in) { res.first += 2 * bs[s].first; res.second += 2 * bs[s].first; } return w[s] = res; } int main() { int n; while (cin >> n) { cin.ignore(); w.clear(); bs.clear(); pck.clear(); tp.clear(); vis.clear(); while (n--) { string s; getline(cin, s); if (s.substr(0, 7) == "Widget ") { string name; int i; for (i = 7; s[i] != '('; i++) name += s[i]; long long x = 0, y = 0; i++; while (s[i] != ',') x = x * 10 + s[i] - '0', i++; i++; while (s[i] != ')') y = y * 10 + s[i] - '0', i++; w[name] = {x, y}; bs[name] = {0, 0}; tp[name] = 0; vis[name] = 1; } if (s.substr(0, 5) == "HBox " || s.substr(0, 5) == "VBox ") { w[s.substr(5, s.size() - 5)] = {0, 0}; tp[s.substr(5, s.size() - 5)] = (s[0] == 'H' ? 1 : 2); } int tmp = s.find(".pack("); if (tmp != -1) { string name, in; for (int i = 0; i < tmp; i++) name += s[i]; for (int i = tmp + 6; s[i] != ')'; i++) in += s[i]; pck[name].push_back(in); } tmp = s.find(".set_border("); if (tmp != -1) { string name; long long in = 0; for (int i = 0; i < tmp; i++) name += s[i]; for (int i = tmp + 12; s[i] != ')'; i++) in = in * 10 + s[i] - '0'; bs[name].first = in; } tmp = s.find(".set_spacing("); if (tmp != -1) { string name; long long in = 0; for (int i = 0; i < tmp; i++) name += s[i]; for (int i = tmp + 13; s[i] != ')'; i++) in = in * 10 + s[i] - '0'; bs[name].second = in; } } for (map<string, pair<long long, long long> >::iterator it = w.begin(); it != w.end(); it++) { it->second = go(it->first); cout << it->first << ' ' << it->second.first << ' ' << it->second.second << endl; } } return 0; }
4
#include<bits/stdc++.h> #define ll long long int using namespace std; int main() { stack<int>st[1000]; int n,tc,q,t; ll x; cin>>n>>tc; while(tc--) { cin>>q; if(q==0) { cin>>t>>x; st[t].push(x); } else if(q==1) { cin>>t; if(!st[t].empty()) { cout<<st[t].top()<<endl; } } else if(q==2) { cin>>t; if(!st[t].empty()) { st[t].pop(); } } } }
0
#include <bits/stdc++.h> using namespace std; int a[600002]; int main() { int n; scanf("%d", &n); vector<pair<int, int>> pairs; vector<int> smaller, bigger; for (int i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); pairs.push_back({x, y}); a[y] = i + 1; if (x < y) bigger.push_back(y); else smaller.push_back(y); } if (bigger.size() > smaller.size()) { sort(bigger.rbegin(), bigger.rend()); cout << bigger.size() << "\n"; for (long long i = 0; i < bigger.size(); i++) cout << a[bigger[i]] << " "; } else { sort(smaller.begin(), smaller.end()); cout << smaller.size() << "\n"; for (long long i = 0; i < smaller.size(); i++) cout << a[smaller[i]] << " "; } }
4
#include <bits/stdc++.h> using namespace std; long long a[100010], b[100010], csum[100010], n, adjustment1, adjustment2, tt[100010]; bool ok[100010]; using namespace std; const int maxN = 100010; struct Data { int sz; pair<long long, long long> val; int prior, l, r; }; struct Treap { Data node[maxN]; int null = 0; stack<int> fn; int root; void prep() { memset(node, 0, sizeof(node)); while (!fn.empty()) fn.pop(); for (int i = 1; i < maxN; i++) fn.push(i); root = 0; } int init(pair<long long, long long> val) { int id = fn.top(); fn.pop(); node[id].val = val, node[id].sz = 1, node[id].prior = rand(), node[id].l = node[id].r = null; return id; } void upd_sz(int t) { node[t].sz = node[node[t].l].sz + node[node[t].r].sz; } void Split(int t, int &l, int &r, pair<long long, long long> key) { if (!t) l = r = null; else if (node[t].val <= key) Split(node[t].r, node[t].r, r, key), l = t; else Split(node[t].l, l, node[t].l, key), r = t; upd_sz(t); } void Merge(int &t, int l, int r) { if (!l || !r) t = l ? l : r; else if (node[l].prior > node[r].prior) Merge(node[l].r, node[l].r, r), t = l; else Merge(node[r].l, l, node[r].l), t = r; upd_sz(t); } void Insert(int &t, int it) { if (!t) t = it; else if (node[it].prior > node[t].prior) Split(t, node[it].l, node[it].r, node[it].val), t = it; else Insert(node[t].val <= node[it].val ? node[t].r : node[t].l, it); upd_sz(t); } void Erase(int &t, pair<long long, long long> key) { int x, y, xx, xy; Split(t, x, y, key), Split(x, xx, xy, make_pair(key.first - 1, LLONG_MAX)), Merge(t, xx, y); fn.push(xy); } pair<long long, long long> findmin(int &t) { if (!t) return make_pair(1e18, 1e18); return min(node[t].val, findmin(node[t].l)); } }; Treap rt1, rt2; void func(bool r, long long a[]) { rt1.prep(), rt2.prep(); adjustment1 = 0, adjustment2 = 0; for (int i = 1; i <= n; i++) csum[i] = csum[i - 1] + a[i], adjustment1 += a[i]; for (int i = 1; i <= n; i++) { int v = rt2.init(make_pair(csum[i], i)); rt2.Insert(rt2.root, v); } for (int i = 1; i <= n; i++) { bool flag = 1; if (rt1.root != 0 && rt1.findmin(rt1.root).first + adjustment1 < 0) flag = 0; if (rt2.root != 0 && rt2.findmin(rt2.root).first - adjustment2 < 0) flag = 0; int ind = i; if (r) ind = n - i + 1; ok[ind] = max(ok[ind], flag); adjustment1 -= a[i]; rt1.Insert(rt1.root, rt1.init(make_pair(csum[i], i))); adjustment2 += a[i]; rt2.Erase(rt2.root, make_pair(csum[i], i)); } } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { cin >> b[i]; tt[i] = a[i] - b[i]; } func(0, tt); for (int i = 1, j = n; j > 1; i++, j--) tt[i] = a[j] - b[j - 1]; tt[n] = a[1] - b[n]; func(1, tt); int cnt = 0; for (int i = 1; i <= n; i++) cnt += ok[i]; cout << cnt << endl; for (int i = 1; i <= n; i++) { if (ok[i]) cout << i << " "; } cout << endl; }
5
#include <bits/stdc++.h> using namespace std; long long zero = 0; long long fexp(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % 1000000007; b /= 2; a = a * a % 1000000007; } return ans; } long long multiply(long long a, long long b, long long mod) { return ((a % mod) * (b % mod)) % mod; } template <typename ForwardIterator, typename T> ForwardIterator first_less_than(ForwardIterator first, ForwardIterator last, T value) { auto it = std::lower_bound(first, last, value); return (it == first ? last : --it); } const long long N = 1e6 + 5; long long level[N], visited[N], parent[N], happy[N]; std::vector<long long> adj[N]; long long ans = 0; void DFS(long long i) { visited[i] = 1; happy[i] = 1; for (auto u : adj[i]) { if (visited[u] == 0) { level[u] = level[i] + 1; parent[u] = i; DFS(u); happy[i] += happy[u]; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long t; t = 1; while (t--) { string s; cin >> s; long long n = s.size(); vector<long long> idx[26]; long long dp[26][26] = {0}; for (long long i = 0; i < n; i++) { for (long long j = 0; j < 26; j++) dp[j][s[i] - 'a'] += idx[j].size(); idx[s[i] - 'a'].push_back(i); } long long ans = 0; for (long long i = 0; i < 26; i++) { if (idx[i].size() > ans) { ans = idx[i].size(); } } for (long long i = 0; i < 26; i++) { for (long long j = 0; j < 26; j++) { if (ans < dp[i][j]) { ans = dp[i][j]; } } } cout << ans << "\n"; } cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl; return 0; }
3
#include <bits/stdc++.h> #define INF 5000000000000000000 #define ll long long #define pll pair<ll, ll> using namespace std; //=============ford_fulkerson============================ ll MAX_V = 202; struct edge {ll to, cap, rev;}; vector<vector<edge>> G(MAX_V); vector<bool> used(MAX_V, false); void add_edge(ll from, ll to, ll cap) { G.at(from).push_back((edge){to, cap, (ll)G.at(to).size()}); G.at(to).push_back((edge){from, 0ll, (ll)G.at(from).size() - 1}); } ll dfs(ll v, ll t, ll f) { if (v == t) { return f; } used.at(v) = true; for (ll i = 0; i < G.at(v).size(); ++i) { edge &e = G.at(v).at(i); if (!used.at(e.to) && e.cap > 0) { ll d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G.at(e.to).at(e.rev).cap += d; return d; } } } return 0; } ll max_flow(ll s, ll t) { ll flow = 0; while (true) { used = vector<bool>(MAX_V, false); ll f = dfs(s, t, INF); if (f == 0) { return flow; } flow += f; } } //================================================= int main() { ll X, Y, E; cin >> X >> Y >> E; for (ll i = 1; i <= X; ++i) { add_edge(0, i, 1); } for (ll i = 1; i <= Y; ++i) { add_edge(i + 100, 201, 1); } for (ll i = 0; i < E; ++i) { ll x, y; cin >> x >> y; x += 1; y += 1; add_edge(x, y + 100, 1); } cout << max_flow(0, 201) << "\n"; }
0
#include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PLL pair <LL, LL> #define PII pair <int, int> const int N = 1e6 + 7; const int MX = 1e9 + 7; const LL INF = 1e18 + 9LL; int n; char in[N]; int main(){ scanf("%s", in + 1); n = strlen(in + 1); LL ans = 0; for(int i = 1; i <= n; ++i) if(in[i] == '1') ans += (i + 1) / 2; int it = 1; int pref = 0; while(it <= n){ if(in[it] == '0'){ ans += pref; ++it; continue; } int t = it; while(t <= n && in[t] == '1') ++t; pref += (t - it + 1) / 2; it = t; } printf("%lld\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int v1, v2, t, d; int Max = 0; cin >> v1 >> v2 >> t >> d; Max = v1; for (int i = 2; i <= t; i++) { int vail = v1; for (int j = -d; j <= d; j++) { int v3 = v1 + j; int t2 = t - i; if (t2 * -1 * d + v3 <= v2) { vail = v3; } } Max += vail; v1 = vail; } cout << Max << endl; }
2
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int NN = 2102020; int sa[NN]; vector<int> trip[2]; int n, m1, m2, t, nn; int dp[NN][2]; vector<int> mov; vector<pii> shot; int is_trip(int u, int id) { int it = lower_bound(trip[id].begin(), trip[id].end(), u) - trip[id].begin(); if (it == trip[id].size()) return 0; return trip[id][it] == u; } void smax(int &u, int v) { if (u < v) u = v; } int last = 2e9 + 1; int calc(int u, int id) { assert(mov.size() < NN); assert(shot.size() < NN); if (u == 0) { if (!id) return 0; mov.push_back(0); return 0; } int f = is_trip(sa[u], id); int len = sa[u] - sa[u - 1]; if (dp[u - 1][id] >= 0 and dp[u - 1][id] + len - 1 >= f * t and dp[u][id] == dp[u - 1][id] - f * t + len) { if (f) { last = min(last - t, sa[u] - 1); shot.push_back({last, id}); } return calc(u - 1, id); } if (sa[u] != n + 1) mov.push_back(sa[u]); return calc(u, id ^ 1); } int main() { cin >> n >> m1 >> m2 >> t; for (int i = 0; i < m1; i++) { int first; scanf("%d", &first); trip[0].push_back(first); sa[nn++] = first; sa[nn++] = first + 1; } for (int i = 0; i < m2; i++) { int first; scanf("%d", &first); trip[1].push_back(first); sa[nn++] = first; sa[nn++] = first + 1; } sa[nn++] = 0; sort(sa, sa + nn); nn = unique(sa, sa + nn) - sa; sa[nn] = n + 1; memset(dp, -1, sizeof dp); dp[0][0] = dp[0][1] = 0; for (int i = 0; i < nn; i++) { int dis = sa[i + 1] - sa[i]; for (int j = 0; j < 2; j++) { int st = is_trip(sa[i + 1], j); if (dp[i][j] < 0) continue; if (dp[i][j] + dis - 1 < t * st) continue; smax(dp[i + 1][j], dp[i][j] - t * st + dis); if (not is_trip(sa[i + 1], j ^ 1)) { smax(dp[i + 1][j ^ 1], min(t, dp[i][j] - t * st + dis)); } } } if (dp[nn][0] < 0 and dp[nn][1] < 0) { puts("No"); return 0; } puts("Yes"); if (dp[nn][0] >= 0) calc(nn, 0); else if (dp[nn][1] >= 0) calc(nn, 1); reverse(mov.begin(), mov.end()); reverse(shot.begin(), shot.end()); cout << mov.size() << endl; for (auto u : mov) printf("%d ", u); puts(""); cout << shot.size() << endl; for (auto u : shot) printf("%d %d\n", u.first, u.second + 1); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, p, m; vector<vector<int> > g; vector<char> used; vector<long long> c, vs, es; pair<int, int> dfs(int v) { used[v] = true; pair<int, int> ans(1, (int)g[v].size()); for (int u : g[v]) { if (used[u]) { continue; } pair<int, int> a = dfs(u); ans.first += a.first; ans.second += a.second; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> p; g.resize(n); used.resize(n); c = vs = es = vector<long long>(p); for (int k = 0; k < p; k++) { cin >> c[k]; c[k]--; } for (int k = 0; k < m; k++) { int v, u; cin >> v >> u; v--; u--; g[v].push_back(u); g[u].push_back(v); } int i = -1; for (int k = 0; k < p; k++) { pair<int, int> ans = dfs(c[k]); vs[k] = ans.first; es[k] = ans.second / 2; if (i == -1 || vs[k] > vs[i]) { i = k; } } int rem = 0; for (int k = 0; k < n; k++) { if (!used[k]) { pair<int, int> ans = dfs(k); vs[i] += ans.first; es[i] += ans.second / 2; } } long long ans = 0; for (int k = 0; k < p; k++) { ans += vs[k] * (vs[k] - 1) / 2 - es[k]; } cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; int mod = 1000000007; const int inf = 1034567891; const long long LL_INF = 1234567890123456789ll; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <typename T> T GCD(T a, T b) { long long t; while (a) { t = a; a = b % a; b = t; } return b; } template <typename T> string toString(T a) { return to_string(a); } template <typename T> void toInt(string s, T& x) { stringstream str(s); str >> x; } inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) x += mod; return x; } inline int mul(int x, int y) { return (x * 1ll * y) % mod; } inline int powr(int a, long long b) { int x = 1 % mod; while (b) { if (b & 1) x = mul(x, a); a = mul(a, a); b >>= 1; } return x; } inline int inv(int a) { return powr(a, mod - 2); } typedef struct data { data* bit[2]; int cnt = 0; } trie; trie* head; void insert(long long x) { trie* node = head; for (int i = 30; i >= 0; i--) { int curbit = (x >> i) & 1; if (node->bit[curbit] == NULL) { node->bit[curbit] = new trie(); } node = node->bit[curbit]; node->cnt++; } } void remove(long long x) { trie* node = head; for (int i = 30; i >= 0; i--) { int curbit = (x >> i) & 1; node = node->bit[curbit]; node->cnt--; } } long long minXor(long long x) { trie* node = head; long long ans = 0; for (int i = 30; i >= 0; i--) { int curbit = (x >> i) & 1; if (node->bit[curbit] != NULL && node->bit[curbit]->cnt > 0) { node = node->bit[curbit]; } else { ans += (1ll << i); node = node->bit[!curbit]; } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<long long> vec1, vec2; long long v; for (int i = 0; i < n; i++) { cin >> v; vec1.push_back(v); } head = new trie(); for (int i = 0; i < n; i++) { cin >> v; vec2.push_back(v); insert(v); } for (int i = 0; i < n; i++) { long long ans = minXor(vec1[i]); remove(ans ^ vec1[i]); cout << ans << " "; } cout << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; int n, m, q, in[maxn + 5]; vector<int> g[maxn + 5]; long long res; long long count(int id) { return 1ll * g[id].size() * in[id]; } int main() { scanf("%d %d", &n, &m); for (int i = 1, u, v; i <= m; ++i) { scanf("%d %d", &u, &v); if (u > v) swap(u, v); g[u].push_back(v); in[v] += 1; } for (int i = 1; i <= n; ++i) res += count(i); printf("%lld\n", res); scanf("%d", &q); for (int i = 1, x; i <= q; ++i) { scanf("%d", &x); int sz = g[x].size(); for (int j = sz - 1; j >= 0; --j) { int v = g[x][j]; res -= count(x) + count(v); g[x].pop_back(); g[v].push_back(x); in[v] -= 1, in[x] += 1; res += count(x) + count(v); } printf("%lld\n", res); } return 0; }
6
#include <iostream> #include <string> #include <vector> #include <cmath> #include <algorithm> #include <cstdlib> #include <ctime> #include <cstdio> #include <functional> #include <set> #include <queue> #include <cctype> #include <climits> #include <stack> using namespace std; int main(){ int n; int r[1000],w[1000]; int sum_r=0,sum_w=0,idx=0; while(scanf("%d",&n),n){ sum_r=sum_w=idx=0; for(int i=0;i<n;i++){ scanf("%d %d",&r[i],&w[i]); sum_r+=r[i]; sum_w+=w[i]; if(r[idx]<r[i]) idx=i; } int r0=r[idx],r=sum_r-r0; w[idx]=0; if(r0 <= r){ cout<<sum_r+sum_w<<endl; }else{ int res=2*r0; //vector<vector<bool> > dp(1001,vector<bool>(1000,false)); // bool cur[1001]={}; bool *cur = (bool*)malloc( sizeof(bool)*(1000+2) ); cur[0]=true; for(int i=0;i<n;i++){ bool *next = (bool*)malloc( sizeof(bool)*(1000+2) ); for(int j=0;j<=r0-r;j++){ if(cur[j]){ next[j]=true; if(j+w[i]<=r0-r) next[j+w[i]]=true; } } swap(cur,next); } int opt=r0-r; while(!cur[opt]) opt--; res+=sum_w-opt; printf("%d\n",res); } } }
0
#include <bits/stdc++.h> using namespace std; template <typename T> int size(T& a) { return (int)a.size(); } template <typename T> T sqr(T a) { return a * a; } const int dx[4] = {+1, -1, 0, 0}; const int dy[4] = {0, 0, +1, -1}; const int MAXN = 1002; int n, m; char s[MAXN][MAXN]; bool u[MAXN][MAXN]; int c[MAXN][MAXN]; int g[MAXN][MAXN]; int Tm = 0; int T = 0; pair<int, int> q[MAXN * MAXN]; int sz; bool okey; bool good(int x, int y) { return x >= 0 && x < m && y >= 0 && y < n; } void dfs(int x, int y) { if (!good(x, y)) return; if (s[y][x] == '1') return; if (u[y][x]) return; u[y][x] = true; if (x == 0 || y == 0 || x == m - 1 || y == n - 1) okey = false; for (int dx = -1; dx <= 1; ++dx) for (int dy = -1; dy <= 1; ++dy) if (good(x + dx, y + dy) && s[y + dy][x + dx] == '1' && c[y + dy][x + dx] != T) { q[sz++] = make_pair(x + dx, y + dy); c[y + dy][x + dx] = T; } dfs(x + 1, y); dfs(x - 1, y); dfs(x, y + 1); dfs(x, y - 1); dfs(x + 1, y + 1); dfs(x + 1, y - 1); dfs(x - 1, y + 1); dfs(x - 1, y - 1); } int solve() { int cmp = 0; ++Tm; for (int i = (0); i < (sz); ++i) { int x = q[i].first; int y = q[i].second; if (g[y][x] == Tm) continue; ++cmp; g[y][x] = Tm; queue<pair<int, int> > qq; qq.push(make_pair(x, y)); while (size(qq)) { pair<int, int> p = qq.front(); qq.pop(); bool fst = true; int cnt = 0; for (int u = (0); u < (4); ++u) { int nx = p.first + dx[u]; int ny = p.second + dy[u]; if (good(nx, ny) && c[ny][nx] == T) ++cnt; if (good(nx, ny) && c[ny][nx] == T && g[ny][nx] != Tm && s[ny][nx] == '1') { if (fst) { g[ny][nx] = Tm; qq.push(make_pair(nx, ny)); } } } if (cnt != 2) return 0; } } if (cmp == 1 && sz >= 5) return sz; return 0; } int main() { int ans = 0; scanf("%d%d\n", &n, &m); for (int i = (0); i < (n); ++i) gets(s[i]); memset((u), (false), sizeof(u)); memset((c), (255), sizeof(c)); memset((g), (255), sizeof(g)); for (int i = (0); i < (n - 1); ++i) for (int j = (0); j < (m - 1); ++j) if (s[i][j] == '1' && s[i + 1][j] == '1' && s[i][j + 1] == '1' && s[i + 1][j + 1] == '1') ans = max(ans, 4); for (int i = (0); i < (n); ++i) for (int j = (0); j < (m); ++j) if (s[i][j] == '0' && !u[i][j]) { ++T; sz = 0; okey = true; dfs(j, i); if (!okey) continue; ans = max(ans, solve()); } cout << ans; }
4
/* -*- coding: utf-8 -*- * * 2961.cc: */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 100000; /* typedef */ /* global variables */ bool primes[MAX_N + 1]; int pnums[MAX_N], asums[MAX_N + 1]; /* subroutines */ int gen_primes(int maxp, int pnums[]) { memset(primes, true, sizeof(primes)); primes[0] = primes[1] = false; int p, pn = 0; for (p = 2; p * p <= maxp; p++) if (primes[p]) { pnums[pn++] = p; for (int q = p * p; q <= maxp; q += p) primes[q] = false; } for (; p <= maxp; p++) if (primes[p]) pnums[pn++] = p; return pn; } bool check(int pn, int pnums[], int n) { if (primes[n]) return false; int cnt = 0, e2 = 0; for (int i = 0; pnums[i] * pnums[i] <= n; i++) if (n % pnums[i] == 0) { cnt++; if (cnt >= 3 || e2 > 0) return true; int f = 1, &pi = pnums[i]; n /= pi; while (n % pi == 0) f++, n /= pi; if (f >= 4) return true; if (f >= 2) { e2 = 1; if (cnt >= 2) return true; } } return (n > 1 && (cnt >= 2 || e2 > 0)); } /* main */ int main() { int pn = gen_primes(MAX_N, pnums); //printf("pn=%d\n", pn); for (int n = 2; n <= MAX_N; n++) { bool c = check(pn, pnums, n); //printf("n=%d: c=%d\n", n, c); asums[n] = asums[n - 1] + (c ? 1 : 0); } int qn; scanf("%d", &qn); while (qn--) { int n; scanf("%d", &n); printf("%d\n", asums[n]); } return 0; }
0
#include <bits/stdc++.h> #define ll long long #define REP(i, n) for (ll (i) = 0; (i) < (n); (i)++) #define REPI(i, a, b) for (ll (i) = (a); (i) < (b); (i)++) #define int long long using namespace std; using P = pair<int, int>; using VI = vector<int>; using VVI = vector<VI>; using VVVI = vector<VVI>; const int INF = 1e7; int N, K; VVI edge; VI VP; priority_queue<P, vector<P>, greater<P>> pq; void solve() { while (!pq.empty()) { P p = pq.top(); pq.pop(); assert(p.first >= 0); for (int v: edge[p.second]) { if (VP[v] == -INF) { VP[v] = p.first + 1; pq.push(P{VP[v], v}); } else if (VP[v] != p.first + 1 && VP[v] != p.first -1) { cout << "No" << endl; return; } } } cout << "Yes" << endl; REP (i, N) { cout << VP[i] << endl; } } signed main() { cin >> N; edge = VVI(N); REP (i, N-1) { int a, b; cin >> a >> b; a--; b--; edge[a].push_back(b); edge[b].push_back(a); } cin >> K; VP = VI(N, -INF); REP (i, K) { int v, p; cin >> v >> p; v--; VP[v] = p; pq.push({p, v}); } solve(); }
0
#include <bits/stdc++.h> using namespace std; template <class A> void read(vector<A>& v); template <class A, size_t S> void read(array<A, S>& a); template <class T> void read(T& x) { cin >> x; } void read(double& d) { string t; read(t); d = stod(t); } void read(long double& d) { string t; read(t); d = stold(t); } template <class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template <class A> void read(vector<A>& x) { for (auto& a : x) read(a); } template <class A, size_t S> void read(array<A, S>& x) { for (auto& a : x) read(a); } const int mod = 1e9 + 7; long long int primeLimit = 1e5; const double PI = acos(-1); bool prime[100000]; string LongestPalindromicPrefix(string str) { string temp = str + '?'; reverse(str.begin(), str.end()); temp += str; long long int n = temp.length(); long long int lps[n]; fill(lps, lps + n, 0); for (long long int i = 1; i < n; i++) { long long int len = lps[i - 1]; while (len > 0 && temp[len] != temp[i]) { len = lps[len - 1]; } if (temp[i] == temp[len]) { len++; } lps[i] = len; } return temp.substr(0, lps[n - 1]); } bool binarySearchFloat(float lower, float higher, float maxErrorAllowed, long long int numberOfTimesWanttoRunCode) { float l = lower, r = higher; float eps = maxErrorAllowed; long long int iteration_count = numberOfTimesWanttoRunCode; for (long long int i = 0; i < iteration_count && l + eps < r; ++i) { } } void primefinder() { memset(prime, true, sizeof(prime)); prime[1] = false; prime[0] = false; for (long long int i = 2; i < primeLimit; i++) { if (prime[i]) { for (long long int j = i * i; j < primeLimit; j += i) { prime[j] = false; } } } } long long fast_pow(long long int x, long long int y) { long long res = 1; while (y > 0) { if (y & 1) res = res * x; y = y >> 1; x = x * x; } return res; } long long Fast_pow(long long int x, long long int y, long long int p) { long long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, k; cin >> n >> k; long long int a1, b1, a2, b2; cin >> a1 >> b1 >> a2 >> b2; if (n == 4 || k <= n) { cout << "-1\n"; return 0; } vector<long long int> ans1; ans1.push_back(a1); ans1.push_back(a2); unordered_set<long long int> visited; visited.insert(a1); visited.insert(a2); visited.insert(b1); visited.insert(b2); for (long long int i = 1; i <= n; i++) { if (visited.find(i) == visited.end()) { visited.insert(i); ans1.push_back(i); break; } } ans1.push_back(b2); for (long long int i = 1; i <= n; i++) { if (visited.find(i) == visited.end()) { visited.insert(i); ans1.push_back(i); } } ans1.push_back(b1); for (long long int i = 0; i < ans1.size(); i++) { cout << ans1[i] << " "; } cout << "\n"; visited.clear(); cout << a2 << " " << a1 << " " << ans1[2] << " "; for (long long int i = ans1.size() - 1; i >= 0; i--) { cout << ans1[i] << " "; if (ans1[i] == b2) break; } }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 20, mod = 1e9 + 7; int n, m, ans[maxn]; map<pair<int, int>, bool> adj; int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; adj[{x, y}] = 1; } ans[1] = 1; for (int i = 2; i <= n; i++) { ans[i] = i; int j = i - 1; while (j > 0 and adj[{ans[j], i}] == 1) { swap(ans[j], ans[j + 1]); j--; } } for (int i = 1; i <= n; i++) { cout << ans[i] << " "; } }
4
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; cout << s.substr(0, 3) << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; int c2 = 0, c3 = 0; while (n) { if (n % 10 == 4) c3++; else if (n % 10 == 7) c2++; n /= 10; } if ((c2 + c3) == 7 || (c2 + c3) == 4) cout << "YES"; else cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; char a[1100000], b[1100000]; int cnt[10][10]; int main() { int la, lb, i, j, k; long long ans = 0; scanf("%s%s", &a, &b); la = strlen(a); lb = strlen(b); for (i = 0; i < la; i++) { if (a[i] == 'R') a[i] = 0; if (a[i] == 'G') a[i] = 1; if (a[i] == 'B') a[i] = 2; } for (i = 0; i < lb; i++) { if (b[i] == 'R') b[i] = 0; if (b[i] == 'G') b[i] = 1; if (b[i] == 'B') b[i] = 2; } for (i = j = k = 0; i < la; i++) { while (k < lb - 1 && b[k] != a[i]) { k++; cnt[b[k - 1]][b[k]]++; } ans += k - j + 1; if (i && a[i] != a[i - 1]) ans -= cnt[a[i]][a[i - 1]]; if (k < lb - 1 && b[k] == a[i]) { k++; cnt[b[k - 1]][b[k]]++; } if (b[j] == a[i]) { if (j == lb - 1) break; j++; cnt[b[j - 1]][b[j]]--; } } printf("%I64d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int k; long long n; string a; cin >> n; if (n == 0) cout << "O-|-OOOO" << endl; while (n) { a = "OOOOO"; k = n % 10; if (k >= 5) { cout << "-O|"; a[k - 5] = '-'; } else { cout << "O-|"; a[k] = '-'; } cout << a << endl; n = n / 10; } return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int LOG = 19, maxN = 1 << LOG, mod = 998244353; template <typename T1, typename T2> inline void addMod(T1& a, T2 b) { a = (a + b) % mod; } template <typename T1, typename T2> inline void multMod(T1& a, T2 b) { a = a * b % mod; } int vis[maxN], nxt[maxN], prv[maxN], T[maxN]; long long dp[maxN]; bool ban[maxN]; void fail() { printf("0\n"); exit(0); } map<int, int> M; vector<pair<int, int> > kys; void solve() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int v = (1); v < (n + 1); v++) { int c; scanf("%d", &c); bool dupl = false; for (int i = (0); i < (c); i++) { scanf("%d", T + i); if (vis[T[i]] == v) dupl = true; vis[T[i]] = v; } if (dupl) { for (int i = (0); i < (c); i++) ban[T[i]] = true; continue; } for (int i = (0); i < (c - 1); i++) { int x = T[i]; if (nxt[x] != 0 and nxt[x] != T[i + 1]) ban[x] = ban[nxt[x]] = true; else nxt[x] = T[i + 1]; } for (int i = (1); i < (c); i++) { int x = T[i]; if (prv[x] != 0 and prv[x] != T[i - 1]) ban[x] = ban[prv[x]] = true; else prv[x] = T[i - 1]; } } for (int v = (1); v < (k + 1); v++) if (prv[v] == 0) { int d = 0; bool ok = true; for (int x = v; x != 0; x = nxt[x]) { d++; if (ban[x] or (x == v and d != 1)) { ok = false; break; } } if (ok) M[d]++; } for (auto it : M) kys.push_back(it); dp[0] = 1; for (int i = (1); i < (m + 1); i++) { for (int j = (0); j < (((int)(kys).size())); j++) { int ki, vl; tie(ki, vl) = kys[j]; if (ki <= i) dp[i] += dp[i - ki] * vl; } dp[i] %= mod; } printf("%lld\n", dp[m]); } int main() { int t = 1; for (int tid = (1); tid < (t + 1); tid++) { solve(); } return 0; }
6
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> using namespace std; using namespace std::chrono; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const int mod=998244353 ; const llint big=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-10; template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} //結局、メモ化すれば大丈夫 vector<vector<int>>ko; vector<int>p; map<tuple<int,int,int>,tuple<int,int,int>>memo; tuple<int,int,int> solve(int ter,int A,int B){ if(ko[ter].size()==0){return mt(p[ter],1,1);} if(memo.count(mt(ter,A,B))){return memo[mt(ter,A,B)];} SO(ko[ter]); int syo=mod,dai=0; int ret=-mod; do{ int nA=A; int gs=0,gd=0; for(auto it:ko[ter]){ int val,ss,sd; tie(val,ss,sd)=solve(it,-B,-nA); gs+=ss;gd+=sd;val*=-1; if(val>=B){nA=val;break;} maxeq(nA,val); } //if(ret!=nA&&ret!=-mod&&nA<B){cerr<<"bag"<<ret<<" "<<ter<<endl;} ret=nA; mineq(syo,gs); maxeq(dai,gd); }while(next_permutation(ko[ter].begin(),ko[ter].end())); memo[mt(ter,A,B)]=mt(ret,syo,dai); return mt(ret,syo,dai); } int main(void){ int n,i,j;cin>>n; p.res(n); ko.res(n); for(i=0;i<n;i++){cin>>p[i];} for(i=0;i<n;i++){ int d;cin>>d; while(d--){ int c;cin>>c;c--; ko[i].pub(c); } } int ret,syo,dai; tie(ret,syo,dai)=solve(0,-10001,10001); cout<<syo<<" "<<dai<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n, m, u, v, w, a[maxn], b[maxn], t; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); a[n + 1] = 1e9; for (int i = 1; i <= m; i++) { scanf("%d%d%d", &u, &v, &w); if (u == 1) t++, b[t] = v; } int ans = 1e9; sort(a + 1, a + n + 1); sort(b + 1, b + t + 1); int j = 1; for (int i = 1; i <= n + 1; i++) { while (j <= t && b[j] < a[i]) j++; ans = min(ans, i + t - j); } printf("%d\n", ans); }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int inches; inches = n / 3 + (n % 3 > 1); int feet; feet = inches / 12; inches %= 12; cout << feet << " " << inches << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAX_SIZE = 10000; vector<int> lucky; void dfs(long long x) { if (x >= 1000000000) return; if (x) lucky.push_back(x); dfs(x * 10 + 4); dfs(x * 10 + 7); } long long Inter(long long x1, long long y1, long long x2, long long y2) { return max(min(y1, y2) - max(x1, x2) + 1, 0ll); } void solve() { int i; dfs(0); lucky.push_back(0); lucky.push_back(1000000010); sort(lucky.begin(), lucky.end()); long long pl, pr, vl, vr, k, X = 0, Y = 0; cin >> pl >> pr >> vl >> vr >> k; Y = (vr - vl + 1) * (pr - pl + 1); for (i = 1; i + k < lucky.size(); i++) { X += Inter(pl, pr, lucky[i - 1] + 1, lucky[i]) * Inter(vl, vr, lucky[i + k - 1], lucky[i + k] - 1); X += Inter(vl, vr, lucky[i - 1] + 1, lucky[i]) * Inter(pl, pr, lucky[i + k - 1], lucky[i + k] - 1); if (k == 1) X -= Inter(vl, vr, lucky[i], lucky[i]) * Inter(pl, pr, lucky[i], lucky[i]); } printf("%.12lf\n", X * 1. / Y); } int main() { solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; long long k; cin >> n >> k; vector<long long> w(n); for (int i = 1; i < n; i++) { int foo; long long bar; cin >> foo >> bar; foo--; w[i] = w[foo] ^ bar; } sort(w.begin(), w.end()); vector<int> from(n), to(n); for (int i = 0; i < n; i++) { from[i] = 0; to[i] = n - 1; } vector<int> R(n), L(n); long long ans = 0; for (int bit = 61; bit >= 0; bit--) { int beg = 0; while (beg < n) { int end = beg; while (end + 1 < n && (w[end] ^ w[end + 1]) < (1LL << bit)) { end++; } R[beg] = end; L[end] = beg; beg = end + 1; } long long add = 0; for (int i = 0; i < n; i++) { if (from[i] > to[i]) { continue; } if (w[i] & (1LL << bit)) { if (w[to[i]] & (1LL << bit)) { add += to[i] - L[to[i]] + 1; } } else { if (!(w[from[i]] & (1LL << bit))) { add += R[from[i]] - from[i] + 1; } } } long long take = 0; if (add < k) { k -= add; ans += (1LL << bit); take = 1; } for (int i = 0; i < n; i++) { if (from[i] > to[i]) { continue; } if (((w[i] >> bit) & 1) == take) { if (w[from[i]] & (1LL << bit)) { to[i] = from[i] - 1; } else { to[i] = R[from[i]]; } } else { if (!(w[to[i]] & (1LL << bit))) { from[i] = to[i] + 1; } else { from[i] = L[to[i]]; } } } } cout << ans << '\n'; return 0; }
6
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; using ld = long double; const ld eps = 1e-9; string st; namespace cent { struct Edge { int src; int dst; int k; }; using Graph = vector<vector<Edge>>; class Centroid { private: int dfs(const Graph&g, const int now, const int from, vector<int>&ch_nums, const vector<int>&oks) { int sum = 1; for (auto &&e : g[now]) { if (e.dst == from || oks[e.dst] != -1)continue; else { sum += dfs(g, e.dst, e.src, ch_nums, oks); } } return ch_nums[now] = sum; }; int find_centroid(const int asize, const vector<vector<Edge>>&graph, const int pre_root, const int pre_from, const vector<int>&ch_nums, const vector<int>&oks) { for (auto&& e : graph[pre_root]) { if (e.dst == pre_from)continue; if (oks[e.dst] != -1)continue; if (ch_nums[e.dst]>asize / 2)return find_centroid(asize, graph, e.dst, e.src, ch_nums, oks); } return pre_root; } void dfs2(const vector<vector<Edge>>&graph, int now, int from,int pre_from,pair<vector<int>, vector<int>>&len_s, pair<int,pair<int,int>>p,int dd) { { if (p.first >= 0 && p.first == p.second.first) { len_s.first.push_back(p.first); } else if (p.first <= 0 && p.first == p.second.second) { len_s.second.push_back(p.first); } } for (auto &&e : graph[now]) { if(e.dst==pre_from||e.dst==from)continue; if(oks[e.dst]<=dd&&oks[e.dst]!=-1)continue; { auto np(p); np.first+=st[e.dst]=='('?1:-1; np.second.first=max(np.second.first,np.first); np.second.second=min(np.second.second,np.first); dfs2(graph,e.dst,now,pre_from,len_s,np,dd); } } } int cent(const vector<vector<Edge>>&graph, vector<int>&oks, const int root, const int from, vector<vector<int>>&centroid_edges, int& fst_centroid, int depth, vector<int>&ch_nums) { dfs(graph, root, from, ch_nums, oks); int cent_id = find_centroid(ch_nums[root], graph, root, from, ch_nums, oks); long long int nans=0; oks[cent_id] = depth; if (from != -1) { centroid_edges[from].push_back(cent_id); } else { fst_centroid = cent_id; } map<int, long long int>mp,mp2; for (auto e : graph[cent_id]) { if (e.dst == from)continue; if(oks[e.dst]!=-1)continue; int e_cent_id=cent(graph, oks, e.dst, e.src, centroid_edges, fst_centroid, depth + 1, ch_nums); pair<int,pair<int,int>>b_p; if (st[e.dst] == '(') { b_p=make_pair(1,make_pair(1,0)); } else { b_p=make_pair(-1,make_pair(0,-1)); } pair<vector<int>,vector<int>>xx; dfs2(graph,e.dst, -1, e.dst, xx, b_p,depth); //cout<<lens[e_cent_id].size()<<endl; map<int,int>n_mp,n_mp2; for (auto len :xx.first) { n_mp[len]++; mp[len]++; } for (auto len : xx.second) { n_mp2[len]++; mp2[len]++; } if (st[cent_id] == '(') { for (auto m : n_mp) { int need = -m.first; need--; if (n_mp2.find(need) != n_mp2.end()) { nans -= m.second*n_mp2[need]; } } } else { for (auto m : n_mp2) { int need=-m.first; need++; if (n_mp.find(need) != n_mp.end()) { nans-=m.second*n_mp[need]; } } } } mp[0]++;mp2[0]++; if (st[cent_id] == '(') { for (auto m : mp) { int need = -m.first; need--; if (mp2.find(need) != mp2.end()) { nans += m.second*mp2[need]; } } } else { for (auto m : mp2) { int need = -m.first; need++; if (mp.find(need) != mp.end()) { nans += m.second*mp[need]; } } } //cout<<root<<" "<<cent_id<<" "<<nans<<endl; ans+=nans; return cent_id; } public: long long int ans=0; vector<pair<vector<int>,vector<int>>>lens; vector<vector<int>> centroid_graph; vector<int>parents; vector<int>oks; map<pair<int, int>, long long int>mp; //fst:root snd:centroid_graph void init(const Graph&g) { lens.resize(g.size()); oks = vector<int>(g.size(), -1); int root = -1; centroid_graph.resize(g.size()); parents = vector<int>(g.size(), -1); vector<int>ch_nums(g.size()); cent(g, oks, 0, -1, centroid_graph, root, 0, ch_nums); for (int i = 0; i < centroid_graph.size(); ++i) { for (auto&& e : centroid_graph[i]) { parents[e] = i; } } return; } }centroid; } int main() { int N;cin>>N; cin>>st; using namespace cent; vector<vector<Edge>>graph(N); for (int i = 0; i < N-1; ++i) { int a,b;cin>>a>>b;a--;b--; graph[a].push_back(Edge{ a,b,st[b]=='(' }); graph[b].push_back(Edge{ b,a,st[a]=='(' }); } centroid.init(graph); cout<<centroid.ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long q, t, v, u, w; map<long long, long long> cost; cin >> q; while (q--) { cin >> t; if (t == 1) { cin >> v >> u >> w; while (v != u) { while (u > v) { cost[u] += w; u >>= 1; } while (v > u) { cost[v] += w; v >>= 1; } } } else { cin >> v >> u; long long res = 0; while (v != u) { while (u > v) { res += cost[u]; u >>= 1; } while (v > u) { res += cost[v]; v >>= 1; } } cout << res << endl; } } return 0; }
3
#include <iostream> #include <set> #include <utility> #include <vector> #include <algorithm> #define llint long long #define inf 1e18 using namespace std; typedef pair<llint, llint> P; struct BIT{ int size; vector<llint> bit; BIT(){size = 0;} BIT(int s){ size = s; bit.resize(size+1); init(); } void init(){ for(int i = 1; i <= size; i++) bit[i] = 0; } llint query(int i){ llint ret = 0; while(i > 0){ ret += bit[i]; i -= i&(-i); } return ret; } void add(int i, llint x){ while(i <= size){ bit[i] += x; i += i&(-i); } } }; llint n, k; llint a[100005]; P s[100005]; BIT bit(100005); int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for(int i = 1; i <= n; i++) cin >> a[i], a[i] -= k; s[0] = P(0, 1); for(int i = 1; i <= n; i++) s[i] = P(s[i-1].first+a[i], i+1); sort(s, s+n+1); llint ans = 0; for(int i = 0; i <= n; i++){ ans += bit.query(s[i].second); bit.add(s[i].second, 1); } cout << ans << endl; return 0; }
0
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) #define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i]; #define ft first #define sc second #define pb push_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(),(v).end() #define mod 1000000007 #define FS fixed<<setprecision(15) using namespace std; typedef long long ll; template<class T> using V=vector<T>; using Graph = vector<vector<int>>; using P=pair<ll,ll>; typedef unsigned long long ull; typedef long double ldouble; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const ll INF=1e18; const ull B=1000000007;//ハッシュの基数 //aはbに含まれているか? void contain(string a,string b){ int al=a.length(),bl=b.length(); if(al>bl) return; //Bのal乗を計算 ull t=1; for(int i=0;i<al;i++) t*=B; //aとbの最初のal文字に関するハッシュ値を計算 ull ah=0,bh=0; for(int i=0;i<al;i++) ah=ah*B+a[i]; for(int i=0;i<al;i++) bh=bh*B+b[i]; //bの場所を1つずつ進めながらハッシュ値をチェック for(int i=0;i+al<=bl;i++){ if(ah==bh) cout<<i<<'\n';//bのi文字目からのal文字が一致 if(i+al<bl) bh=bh*B+b[i+al]-b[i]*t; } } int main(){ string t,p; cin>>t>>p; contain(p,t); }
0
#include<bits/stdc++.h> using namespace std; #define ll long long #define f(i,x,n) for (int i = x;i < n;++i) int md = 1e9 + 7, d[401][401], a[401], b[401], p[401][401]; int main() { int n, c; scanf("%d%d", &n, &c); f(i, 1, n + 1)scanf("%d", a + i); f(i, 1, n + 1) { scanf("%d", b + i); f(j, a[i], b[i] + 1) { int t = 1; f(k, 0, 401) { p[i][k] += t; t = (ll)t*j%md; if (p[i][k] >= md)p[i][k] -= md; } } } d[0][0] = 1; f(i, 1, n + 1) { d[i][0] = (ll)(b[i] - a[i] + 1)*d[i - 1][0] % md; f(j, 1, c + 1)f(k, 0, j + 1) { d[i][j] += (ll)d[i - 1][j - k] * p[i][k] % md; if (d[i][j] >= md)d[i][j] -= md; } } printf("%d", d[n][c]); }
0
#include <bits/stdc++.h> using namespace std; int main(){ int N, M; cin>>N>>M; cout<<(N==M? "Yes\n": "No\n"); }
0
#include<bits/stdc++.h> using namespace std; #define int long long vector<int> ans; int S(int x) { int ret = 0; while(x) { ret += x % 10; x /= 10; } return ret; } bool verify(int x, int y) { return x * S(y) < y * S(x); } void insert(int x) { while(verify(x, ans.back())) ans.pop_back(); ans.push_back(x); } void init() { ans.push_back(1); for(int i = 2; i < 10000; i++) { insert(i); } for(int idx = 0, base = 10; idx < 12; idx++, base *= 10) { for(int i = 1000; i < 10000; i++) insert(i * base + (base - 1)); } } int32_t main() { cin.tie(0); cin.sync_with_stdio(0); init(); int K; cin >> K; for(int i = 0; i < K; i++) { cout << ans[i] << '\n'; } }
0
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const int n_ = 5000; long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long power(long long a, long long n, long long mod) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } long long n, m, dp[n_][n_], ans; int minB, b; string s; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; cin >> s; dp[0][0] = 1; for (int i = 1; i <= n - m; i++) { dp[i][0] = dp[i - 1][1]; for (int j = 1; j <= i; j++) { dp[i][j] = dp[i - 1][j + 1] + dp[i - 1][j - 1]; dp[i][j] %= MOD; } } int curDiff = 0; int desiredOpen = 0, desiredClose = 0; for (int i = 0; i < m; i++) { if (s[i] == '(') curDiff++; else { if (curDiff > 0) curDiff--; else { desiredOpen++; } } } desiredClose = curDiff; for (int c = 0; c <= n - m; c++) { for (int d = 0; d <= c; d++) { if (d >= desiredOpen && d - desiredOpen + desiredClose <= 2000) ans += 1LL * dp[c][d] * dp[n - m - c][d - desiredOpen + desiredClose] % MOD, ans %= MOD; } } cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, x, i, j; cin >> n >> x; long long int mm = 0; long long int mx = 0; for (i = 0; i < n; i++) { long long int a, b; cin >> a >> b; mm = max(mm, a - b); mx = max(mx, a); } if (x <= mx) { cout << "1\n"; return; } if (mm <= 0) { cout << "-1\n"; return; } long long int p = x - mx; long long int q = p / mm; if (p % mm != 0) { q++; } x -= (q * mm); if (x > 0) { q++; } cout << q << "\n"; } int main() { ios_base ::sync_with_stdio(0); cin.tie(NULL); int t; cin >> t; while (t > 0) { t--; solve(); } }
2
#include <bits/stdc++.h> using namespace std; const int kMaxN = 505, kMaxK = 21, kInf = 0x3f3f3f3f; const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, -1, 0, 1 }; int n, m, k, w[4][kMaxN][kMaxN], dp[kMaxK][kMaxN][kMaxN]; int resolve(int k, int x, int y) { if (~dp[k][x][y]) return dp[k][x][y]; if (k == 0) return dp[k][x][y] = 0; int &ans = dp[k][x][y]; ans = kInf; for (int d = 0; d < 4; ++d) { int nx = x + dx[d], ny = y + dy[d]; if (nx < 1 || nx > n || ny < 1 || ny > m) continue; ans = min(ans, w[d][x][y] + resolve(k - 1, nx, ny)); } return ans; } int main() { ios::sync_with_stdio(false); cin >> n >> m >> k; memset(dp, 0xff, sizeof dp); for (int i = 1; i <= n; ++i) { for (int j = 1, x; j < m; ++j) { cin >> x; w[1][i][j + 1] = w[3][i][j] = x; } } for (int i = 1; i < n; ++i) { for (int j = 1, x; j <= m; ++j) { cin >> x; w[0][i + 1][j] = w[2][i][j] = x; } } if (k & 1) { for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) cout << -1 << " \n"[j == m]; return 0; } k >>= 1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cout << (resolve(k, i, j) * 2) << " \n"[j == m]; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; void solve() { long long n, l, r; while (cin >> n >> l >> r) { vector<int> vmin, vmax; long long j = 1; long long minsum = 0, maxsum = 0; for (long long i = 0; i < l; i++) { minsum += j; j *= 2; } j = 1; for (long long i = 0; i < r; i++) maxsum += j, j *= 2; j /= 2; for (long long i = l; i < n; i++) minsum += 1; for (long long i = r; i < n; i++) maxsum += j; cout << minsum << ' ' << maxsum << endl; } } int main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); }; solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, d, count = 0; cin >> n >> d; string c; cin >> c; int f = 1; f = f + d - 1; int flag = 1; while (1) { if (c[f] == '1') { count++; if (f == n - 1) { cout << count; break; } flag = f; f += d; } else { f--; if (flag >= f) { cout << -1; break; } } } }
1
#include <bits/stdc++.h> typedef long long int ll; #define vaibhavv06 \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(0); #define int long long #define MM(a,b) memset(a,b,sizeof(a)) const ll MOD = 1000000007; using namespace std; const int nax = 1e5+5; vector<int> edges[nax]; int d1[nax],d2[nax]; void dfs(int u, int p,int d[]){ d[u]=d[p]+1; for(auto x:edges[u]){ if(x!=p) dfs(x,u,d); } } int32_t main(){ vaibhavv06; int n; cin>>n; int t,a; cin>>t>>a; for(int i=1;i<n;i++){ int a,b; cin>>a>>b; edges[a].push_back(b); edges[b].push_back(a); } d1[0]=-1; d2[0]=-1; dfs(t,0,d1); dfs(a,0,d2); int ans=0; for(int i=1;i<=n;i++){ if(d1[i]<d2[i]){ ans=max(ans,d2[i]-1); } } cout<<ans<<endl; }
0
#include<iostream> #include<algorithm> #include<string> #include<vector> #include<cmath> #include<map> #include<iomanip> #include<queue> #include<stack> #include<time.h> #define rep(i,n)for(int i=0;i<n;i++) #define int long long #define ggr getchar();getchar();return 0; #define prique priority_queue #define mod 1000000007 #define inf 1e15 using namespace std; typedef pair<int, int>P; void yes() { cout << "Yay!" << endl; } void no() { cout << ":(" << endl; } int gcd(int x, int y) { if (y == 0)return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int mod_pow(int x, int y, int m) { int res = 1; while (y) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int comb(int x, int y) { if (y > x)return 0; return kai(x, y)* mod_pow(kai(y, y), mod - 2, mod) % mod; } struct edge { int to, cost; }; signed main() { int n, m; cin >> n >> m; cout << n * m << endl; ggr }
0
#include <bits/stdc++.h> using namespace std; int main() { float n, x, y; cin >> n >> x >> y; float ans = (n / 100) * y; ans = ceil(ans); if (ans <= x) cout << "0" << endl; else { ans = ans - x; cout << ans << endl; } }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; int check(int n) { while (n) { int t = n % 10; if (t != 4 && t != 7) return 0; n /= 10; } return 1; } int n, a[100005]; int b[100005]; int c[100005]; vector<pair<int, int> > ans; bool cmp(int i, int j) { return a[i] < a[j]; } void slove(int &x, int y) { if (x == y) return; if (!check(a[x]) && !check(a[y])) puts("error"); swap(a[x], a[y]); ans.push_back(make_pair(x, y)); swap(c[x], c[y]); b[c[x]] = x; b[c[y]] = y; x = y; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], b[i] = i; sort(b + 1, b + 1 + n, cmp); int pos = -1; for (int i = 1; i <= n; i++) c[b[i]] = i; for (int i = 1; i <= n; i++) if (check(a[i])) { pos = i; break; } if (pos == -1) { int flag = 1; for (int i = 1; i < n; i++) if (a[i] > a[i + 1]) flag = 0; puts(flag ? "0" : "-1"); return 0; } for (int i = 1; i <= n; i++) { { slove(pos, i); slove(pos, b[i]); } } slove(pos, b[pos]); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i].first << " " << ans[i].second << endl; return 0; }
4
#include<bits/stdc++.h> using namespace std; int main() { string a,b; cin>>a>>b; for(int i=1;i<=a.size();i++) { a=a.substr(a.size()-1,1)+a.substr(0,a.size()-1); if(a==b) { cout<<"Yes"<<endl; return 0; } } cout<<"No"<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; t = 1; for (long long test = 1; test <= t; test++) { solve(); } return 0; } inline void solve() { long long n; cin >> n; if (n % 2 == 0) { cout << "white" << "\n"; cout << 1 << " " << 2 << "\n"; } else cout << "black" << "\n"; }
4
#include <iostream> #include <vector> #include <queue> using namespace std; struct state{ int node,del,dist; bool operator<(const state& r)const{ return dist>r.dist; } }; struct edge{ int to,dist; }; int main(){ int N,M,C; while(cin >> N >> M >> C && N){ vector<vector<edge>> g(N); for(int i=0;i<M;i++){ int a,b,c; cin >> a >> b >> c; a--; b--; g[a].push_back({b,c}); } int inf = 2e9; vector<vector<int>> dp(N,vector<int>(M+1,inf)); dp[0][0] = 0; priority_queue<state> Q; Q.push({0,0,0}); while(!Q.empty()){ state now = Q.top(); Q.pop(); if(dp[now.node][now.del]<now.dist) continue; for(auto& x:g[now.node]){ state ne = now; ne.node = x.to; ne.dist += x.dist; if(dp[ne.node][ne.del]>ne.dist){ dp[ne.node][ne.del] = ne.dist; Q.push(ne); } ne.dist = now.dist; ne.del++; if(ne.del>M) continue; if(dp[ne.node][ne.del]>ne.dist){ dp[ne.node][ne.del] = ne.dist; Q.push(ne); } } } for(int i=0;i<=M;i++){ if(dp[N-1][i]<=C){ cout << i << endl; break; } } } }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; using db = long double; const int N = 1e5 + 5, MOD = 1e9 + 7; using ii = pair<int, int>; int pwr[505]; int dp[2][505][505]; int n, k; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; pwr[0] = 1; for (int i = 1; i < 505; i++) { pwr[i] = (pwr[i - 1] + pwr[i - 1]) % MOD; } dp[1][0][0] = 1; for (int r = 1; r <= n; r++) { memset(dp[r & 1 ^ 1], 0, sizeof dp[r & 1 ^ 1]); for (int l = 0; l < r; l++) { for (int cnt = 0; cnt <= k; cnt++) { int val = dp[r & 1][l][cnt]; int lS = pwr[l]; int rS = (pwr[r - l] - 1); dp[r & 1 ^ 1][l][cnt] = (dp[r & 1 ^ 1][l][cnt] + 1ll * val * lS) % MOD; dp[r & 1 ^ 1][r][cnt + 1] = (dp[r & 1 ^ 1][r][cnt + 1] + 1ll * val * lS % MOD * rS) % MOD; } } } int ans = 0; for (int j = 0; j <= n; j++) { ans += dp[n & 1 ^ 1][j][k]; if (ans >= MOD) ans -= MOD; } cout << ans << '\n'; return 0; }
5
#include <cstdio> typedef long long ll; const int N=1010,p=1e9+7; char s[N][N]; int n,m,d,c,S,col,row; ll k; inline int qm(int x){return x<0?x+p:x;} template <typename _tp> inline int qpow(int A,_tp B){ int res(1);while(B){ if(B&1)res=1ll*res*A%p; A=1ll*A*A%p,B>>=1; }return res; } int main(){ scanf("%d%d%lld",&n,&m,&k); for(int i=1;i<=n;++i){ scanf("%s",s[i]+1); for(int j=1;j<=m;++j) if(s[i][j]=='#')++S; } for(int i=1;i<=n;++i)if(s[i][1]=='#'&&'#'==s[i][m])++row; for(int i=1;i<=m;++i)if(s[1][i]=='#'&&'#'==s[n][i])++col; if(!row&&!col){printf("%d\n",qpow(S,k-1));return 0;} if(!k||(row&&col)){puts("1");return 0;} if(row) for(int i=1;i<=n;++i) for(int j=1;j<m;++j) d+=(s[i][j]=='#'&&s[i][j+1]=='#'); else for(int i=1;i<n;++i) for(int j=1;j<=m;++j) d+=(s[i][j]=='#'&&s[i+1][j]=='#'); c=row?row:col; int ans=qm(qpow(S,k-1)-qpow(c,k-1)); ans=1ll*ans*d%p; ans=1ll*ans*qpow(qm(S-c),p-2)%p; printf("%d\n",qm(qpow(S,k-1)-ans)); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { long long n, h; cin >> n; if (n == 0) { cout << "1\n"; return 0; } if (n == 1) { cout << "4\n"; return 0; } h = n / sqrt(double(2.0)); if (h * h + (h + 1) * (h + 1) > n * n) { cout << h * 8 << "\n"; } else { cout << (h + h + 1) * 4 << "\n"; } return 0; }
1
#pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using db = double; using str = string; using pi = pair<int,int>; using pl = pair<ll,ll>; using pd = pair<double,double>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<double>; using vs = vector<string>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; #define tcT template<class T #define tcTUU tcT, class ...U #define tcTU tcT, class U #define tcTU3 tcTU, class C #define tcTU4 tcTU3, class D #define ts to_string #define rsz resize #define ins insert #define all(x) (x).begin(), (x).end() #define sz(v) ((int)(v).size()) #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define mt make_tuple #define eb emplace_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define fe first #define se second #define bs binary_search #define ft front() #define bk back() #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) const int di[4]={-1, 0, 1, 0}, dj[4]={0, 1, 0, -1}; const int di8[8]={-1, -1, 0, 1, 1, 1, 0, -1}, dj8[8]={0, 1, 1, 1, 0, -1, -1, -1}; const ld PI = acos((ld)-1); const ll INF = 1e18; const double eps=1e-11;//NOTES:eps mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll randint(ll a, ll b) { return uniform_int_distribution<ll>(a, b)(rng);} using ul = unsigned long long; ul modMul(ul a, ul b, const ul mod) { ll ret = a*b-mod*(ul)((ld)a*b/mod); return ret+((ret<0)-(ret>=(ll)mod))*mod; } ul modPow(ul a, ul b, const ul mod) { if (b == 0) return 1; ul res = modPow(a,b/2,mod); res = modMul(res,res,mod); return b&1 ? modMul(res,a,mod) : res; } bool prime(ul n) { // not ll! if (n < 2 || n % 6 % 4 != 1) return n-2 < 2; ul A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}, s = __builtin_ctzll(n-1), d = n>>s; trav(a,A) { // ^ count trailing zeroes ul p = modPow(a,d,n), i = s; while (p != 1 && p != n-1 && a%n && i--) p = modMul(p,p,n); if (p != n-1 && i != s) return 0; } return 1; } #define X fe #define Y se template<typename A, typename B> pair<A, B> operator+(const pair<A, B>& a, const pair<A, B>& b) { return mp(a.X + b.X, a.Y + b.Y); } template<typename A, typename B> pair<A, B> operator+=(pair<A, B>& a, const pair<A, B>& b) { a.X += b.X; a.Y += b.Y; return a; } template<typename A, typename B> pair<A, B> operator-(const pair<A, B>& a, const pair<A, B>& b) { return mp(a.X - b.X, a.Y - b.Y); } template<typename A, typename B> pair<A, B> operator-(const pair<A, B>& a) { return mp(-a.X, -a.Y); } template<typename A, typename B> pair<A, B>& operator++(pair<A, B>& a) { ++a.X; ++a.Y; return a; } template<typename A, typename B> pair<A, B>& operator--(pair<A, B>& a) { --a.X; --a.Y; return a; } template<typename A> vector<A>& operator++(vector<A>& a) { for (auto it = a.begin(); it != a.end(); ++it) ++* it; return a; } template<typename A> vector<A>& operator--(vector<A>& a) { for (auto it = a.begin(); it != a.end(); ++it) --* it; return a; } template<typename A, typename B> pair<A, B> operator++(pair<A, B>& a, int) { pair<A, B> b = a; ++a; return b; } template<typename A, typename B> pair<A, B> operator--(pair<A, B>& a, int) { pair<A, B> b = a; --a; return b; } template<typename A> vector<A>& operator++(vector<A>& a, int) { vector<A> b = a; ++a; return b; } template<typename A> vector<A>& operator--(vector<A>& a, int) { vector<A> b = a; --a; return b; } template<typename A, typename B> pair<A, B> operator-=(pair<A, B>& a, const pair<A, B>& b) { a.X -= b.X; a.Y -= b.Y; return a; } bool pow2(int i){ return i&&(i&-i)==i;} constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down ll half(ll x) { return fdiv(x,2); } bool inc(ll a,ll b,ll c){ return a<=b&&b<=c; } template<class t> int lwb(const vector<t>&v,const t&a){ return lower_bound(all(v),a)-v.begin(); } template<class T> inline T lowbit(T n){return (n^(n-1))&n;}//NOTES:lowbit( template <typename I> struct _reversed_struct { I &v_; explicit _reversed_struct(I &v) : v_{v} {} typename I::reverse_iterator begin() const { return v_.rbegin(); } typename I::reverse_iterator end() const { return v_.rend(); } }; template <typename I> _reversed_struct<I> reversed(I &v) { return _reversed_struct<I>(v); } template <typename T1, typename T2> typename vector<pair<T1, T2>>::iterator lower_bound(const vector<pair<T1, T2>> &v, const T1 &x) { return lower_bound(all(v), x, [](pair<T1, T2> a, pair<T1, T2> b) { return a.fe < b.fe; }); } template <typename T1, typename T2> typename vector<pair<T1, T2>>::iterator upper_bound(const vector<pair<T1, T2>> &v, const T1 &x) { return upper_bound(all(v), x, [](pair<T1, T2> a, pair<T1, T2> b) { return a.fe < b.fe; }); } #define L1(u, ...) [&](auto &&u) { return __VA_ARGS__; } #define L2(u, v, ...) [&](auto &&u, auto &&v) { return __VA_ARGS__; } #define sort_by(x, y) sort(all(x), [&](const auto &l, const auto &r) { return y; }) bool isUpperCase(char c){return c>='A' && c<='Z';}//NOTES:isUpperCase( bool isLowerCase(char c){return c>='a' && c<='z';}//NOTES:isLowerCase( bool isLetter(char c){return c>='A' && c<='Z' || c>='a' && c<='z';}//NOTES:isLetter( bool isDigit(char c){return c>='0' && c<='9';}//NOTES:isDigit( char toLowerCase(char c){return (isUpperCase(c))?(c+32):c;}//NOTES:toLowerCase( char toUpperCase(char c){return (isLowerCase(c))?(c-32):c;}//NOTES:toUpperCase( int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toInt( ll toInt64(string s){ll r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toInt64( double toDouble(string s){double r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toDouble( template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));}//NOTES:dist( double distR(double x1,double y1,double x2,double y2){return sqr(x1-x2)+sqr(y1-y2);}//NOTES:distR( template<class T> T cross(T x0,T y0,T x1,T y1,T x2,T y2){return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);}//NOTES:cross( int crossOper(double x0,double y0,double x1,double y1,double x2,double y2)//NOTES:crossOper( {double t=(x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);if (fabs(t)<=eps) return 0;return (t<0)?-1:1;} bool isIntersect(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)//NOTES:isIntersect( {return crossOper(x1,y1,x2,y2,x3,y3)*crossOper(x1,y1,x2,y2,x4,y4)<0 && crossOper(x3,y3,x4,y4,x1,y1)*crossOper(x3,y3,x4,y4,x2,y2)<0;} bool isMiddle(double s,double m,double t){return fabs(s-m)<=eps || fabs(t-m)<=eps || (s<m)!=(t<m);}//NOTES:isMiddle( #define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b))) constexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); } #define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower); #define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper); bool isPower(ll x, ll y) { ll res1 = log(y) / log(x); double res2 = log(y) / log(x); return (res1 == res2); } tcT> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } tcT> bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } void TIME(){ #ifdef __APPLE__ cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif } tcT> void remDup(vector<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); } tcTU> void remAll(vector<T>& v, U a){ v.erase(remove(all(v),a),v.end()); } tcTU> T fstTrue(T lo, T hi, U f) { //smallest while (lo < hi) { T mid = half(lo+hi); f(mid) ? hi = mid : lo = mid+1; } return lo; } tcTU> T lstTrue(T lo, T hi, U f) { //largest while (lo < hi) { T mid = half(lo+hi+1); f(mid) ? lo = mid : hi = mid-1; } return lo; } // TO_STRING tcTU3> string ts(tuple<T,U,C> p); tcTU4> string ts(tuple<T,U,C,D> p); string ts(char c) { return string(1,c); } string ts(bool b) { #ifdef __APPLE__ return b ? "true" : "false"; #else return ts((int)b); #endif } string ts(const char* s) { return (string)s; } string ts(string s) { return s; } tcT> string ts(complex<T> c) { stringstream ss; ss << c; return ss.str(); } string ts(vb v) { string res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); res += "}"; return res; } template<size_t SZ> string ts(bitset<SZ> b) { string res = ""; F0R(i,sz(b)) res += char('0'+b[i]); return res; } tcTU> string ts(pair<T,U> p); tcT> string ts(T v) { // containers with begin(), end() #ifdef __APPLE__ bool fst = 1; string res = "{"; for (const auto& x: v) { if (!fst) res += ", "; fst = 0; res += ts(x); } res += "}"; return res; #else bool fst = 1; string res = ""; for (const auto& x: v) { if (!fst) res += " "; fst = 0; res += ts(x); } return res; #endif } tcTU> string ts(pair<T,U> p) { #ifdef __APPLE__ return "("+ts(p.fe)+", "+ts(p.se)+")"; #else return ts(p.fe)+" "+ts(p.se); #endif } tcTU3> string ts(tuple<T,U,C> p) { #ifdef __APPLE__ return "("+ts(get<0>(p))+","+ts(get<1>(p))+","+ts(get<2>(p))+")"; #else return ts(get<0>(p))+" "+ts(get<1>(p))+" "+ts(get<2>(p)); #endif } tcTU4> string ts(tuple<T,U,C,D> p) { #ifdef __APPLE__ return "("+ts(get<0>(p))+","+ts(get<1>(p))+","+ts(get<2>(p))+","+ts(get<3>(p))+")"; #else return ts(get<0>(p))+" "+ts(get<1>(p))+" "+ts(get<2>(p))+" "+ts(get<3>(p)); #endif } //INPUT void re(string& d){getline(cin,d) ;getline(cin,d); } tcTU3> void re(tuple<T,U,C>& p); tcTU4> void re(tuple<T,U,C,D>& p); tcT> void re(complex<T>& c); tcTU> void re(pair<T,U>& p); tcT> void re(vector<T>& v); tcT> void rv(int n, vector<T>& x) { x.rsz(n); re(x); } tcT, size_t SZ> void re(array<T,SZ>& a); tcT> void re(T& x) { cin >> x; } void re(double& d) { string t; re(t); d = stod(t); } void re(ld& d) { string t; re(t); d = stold(t); } tcTUU> void re(T& t, U&... u) { re(t); re(u...); } tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; } tcTU> void re(pair<T,U>& p) { re(p.fe,p.se); } tcT> void re(vector<T>& x) { trav(a,x) re(a); } tcT, size_t SZ> void re(array<T,SZ>& x) { trav(a,x) re(a); } tcTU3> void re(tuple<T,U,C>& p){re(get<0>(p),get<1>(p),get<2>(p));} tcTU4> void re(tuple<T,U,C,D>& p){re(get<0>(p),get<1>(p),get<2>(p),get<3>(p));} // OUTPUT tcT> void pr(T x) { cout << ts(x); } tcTUU> void pr(const T& t, const U&... u) { pr(t); pr(u...); } void ps() { pr("\n"); } // print w/ spaces tcTUU> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); } struct chash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<int,int> x)const{ return operator()(uint64_t(x.first)<<32|x.second); } }; tcT> using V = vector<T>; tcT, size_t SZ> using AR = array<T,SZ>; tcT> using mpq = priority_queue<T, vector<T>, greater<T>>; tcT> using pq = priority_queue<T>; tcTU> using um = unordered_map<T,U,chash>; tcT> using us = unordered_set<T, chash>; tcT> using PR = pair<T,T>; #define one(x) memset(x,-1,sizeof(x)) #define zero(x) memset(x,0,sizeof(x)) // const int MOD = 1e9+7; const int MOD =998244353; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/rope> // using namespace __gnu_pbds; // using namespace __gnu_cxx; // #define fbo find_by_order // #define ook order_of_key // tcT> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // tcTU> using ht = gp_hash_table<T,U,chash>; // template<class key, class value, class cmp = std::less<key>> using omap = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>; // using cd = complex<ld>; // using vcd = vector<cd>; // int atMost(oset<pi>& T, int r) { // return T.ook({r,MOD}); } // int getSum(oset<pi>& T, int l, int r) { // return atMost(T,r)-atMost(T,l-1); } // fbo(k) returns iterator to kth element starting from 0; // ook(k) returns count of elements strictly smaller than k; template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } // primitive root for FFT int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int mint() { v = 0; } mint(ll _v) { v = (-MOD < _v && _v < MOD) ? _v : _v % MOD; if (v < 0) v += MOD; } friend bool operator==(const mint& a, const mint& b) { return a.v == b.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } friend void re(mint& a) { ll x; re(x); a = mint(x); } friend string ts(mint a) { return ts(a.v); } mint& operator+=(const mint& m) { if ((v += m.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& m) { if ((v -= m.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& m) { v = (ll)v*m.v%MOD; return *this; } mint& operator/=(const mint& m) { return (*this) *= inv(m); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; typedef mint<MOD,5> mi; // 5 is primitive root for both common mods typedef vector<mi> vmi; typedef pair<mi,mi> pmi; typedef vector<pmi> vpmi; vector<vmi> scmb; // small combinations void genComb(int SZ) { scmb.assign(SZ,vmi(SZ)); scmb[0][0] = 1; FOR(i,1,SZ) F0R(j,i+1) scmb[i][j] = scmb[i-1][j]+(j?scmb[i-1][j-1]:0); } vi invs, fac, ifac; // make sure to convert to LL before doing any multiplications ... void genFac(int SZ) { invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); invs[1] = fac[0] = ifac[0] = 1; FOR(i,2,SZ) invs[i] = MOD-(ll)MOD/i*invs[MOD%i]%MOD; FOR(i,1,SZ) { fac[i] = (ll)fac[i-1]*i%MOD; ifac[i] = (ll)ifac[i-1]*invs[i]%MOD; } } mi comb(int a, int b) { if (a < b || b < 0) return 0; return (ll)fac[a]*ifac[b]%MOD*ifac[a-b]%MOD; } ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} ll lcm(ll a,ll b) { return (a / gcd(a, b)) * b;} ll ncr(ll n, ll r){ ll p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; ll m = gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p;} // DEBUG void DBG() { cerr << "]" << endl; } tcTUU> void DBG(const T& t, const U&... u) { cerr << ts(t); if (sizeof...(u)) cerr << ", "; DBG(u...); } #ifdef __APPLE__ // chk -> fake assert #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); #else #define dbg(...) 0 #define chk(...) 0 #endif V<vi> readGraph(int n,int m){ V<vi>g(n); F0R(i,m){ int a,b; re(a,b); a--;b--; g[a].pb(b); g[b].pb(a); } return g; } V<vi> readTree(int n){ return readGraph(n,n-1); } void setPrec() { cout << fixed << setprecision(15); } void setIn(str s) { freopen(s.c_str(),"r",stdin); } void setOut(str s) { freopen(s.c_str(),"w",stdout); } void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } void setIO(str s = "") { unsyncIO(); setPrec(); // cin.exceptions(cin.failbit); // throws exception when do smth illegal // ex. try to read letter into int if (sz(s)) { setIn(s+".in");setOut(s+".out"); } // for USACO } const int mxN = 1e5 + 5; void solve(){ int n; re(n); ll ans=0; map<pi,int>m; F0R(i,n){ int x,y,u,v; re(x,y,u,v); int a=u-x,b=v-y,g=gcd(abs(a),abs(b)); a/=g; b/=g; pi z={a,b}; ans+=m[z]; m[-z]++; } ps(ans); } int main() { setIO(); int T = 1; re(T); FOR(_,1,T+1) { //pr("Case #",_,": "); solve(); } // you should actually read the stuff at the bottom } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
6
#include <bits/stdc++.h> using namespace std; int n, m, S1, S2, cnt1, cnt2, a[2005], ord1[2005], ord2[2005], id1[2005], id2[2005]; int s2[2005][2005], nxt1[2005][2005], nxt2[2005][2005]; long long dst1[2005], dst2[2005], s1[2005][2005], dp[2005][2005][2], mn[2005][2005][2]; bool vs1[2005]; struct Edge { int v, w; }; vector<Edge> e[2005]; bool cmp1(int x, int y) { return dst1[x] < dst1[y]; } bool cmp2(int x, int y) { return dst2[x] < dst2[y]; } void Dijkstra(int S, long long dst[]) { for (int i = 1; i <= n; ++i) vs1[i] = 0, dst[i] = 1e18; dst[S] = 0; for (int i = 1, t = 0; i <= n; ++i, t = 0) { for (int j = 1; j <= n; ++j) if (!vs1[j] && (!t || dst[j] < dst[t])) t = j; vs1[t] = 1; for (int j = 0, v; j < e[t].size(); ++j) v = e[t][j].v, dst[v] = min(dst[v], dst[t] + e[t][j].w); } } int main() { scanf("%d %d %d %d", &n, &m, &S1, &S2); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), ord1[i] = ord2[i] = i; for (int i = 1, u, v, w; i <= m; ++i) { scanf("%d %d %d", &u, &v, &w); e[u].push_back((Edge){v, w}); e[v].push_back((Edge){u, w}); } Dijkstra(S1, dst1); Dijkstra(S2, dst2); sort(ord1 + 1, ord1 + n + 1, cmp1); sort(ord2 + 1, ord2 + n + 1, cmp2); for (int i = 1; i <= n; ++i) { if (i == 1 || dst1[ord1[i]] != dst1[ord1[i - 1]]) ++cnt1; if (i == 1 || dst2[ord2[i]] != dst2[ord2[i - 1]]) ++cnt2; id1[ord1[i]] = cnt1; id2[ord2[i]] = cnt2; } for (int i = 1; i <= n; ++i) s1[id1[i]][id2[i]] += a[i], ++s2[id1[i]][id2[i]]; for (int i = cnt1; i >= 1; --i) for (int j = cnt2; j >= 1; --j) { s1[i][j] += s1[i + 1][j] + s1[i][j + 1] - s1[i + 1][j + 1]; s2[i][j] += s2[i + 1][j] + s2[i][j + 1] - s2[i + 1][j + 1]; if (s2[i][j] - s2[i + 1][j]) nxt1[i][j] = i; else nxt1[i][j] = i == cnt1 ? cnt1 : nxt1[i + 1][j]; if (s2[i][j] - s2[i][j + 1]) nxt2[i][j] = j; else nxt2[i][j] = j == cnt2 ? cnt2 : nxt2[i][j + 1]; dp[i][j][0] = s1[i][j] - mn[nxt1[i][j] + 1][j][1]; dp[i][j][1] = s1[i][j] - mn[i][nxt2[i][j] + 1][0]; mn[i][j][0] = min(mn[i][j + 1][0], dp[i][j][0] + s1[i][j]); mn[i][j][1] = min(mn[i + 1][j][1], dp[i][j][1] + s1[i][j]); } if (dp[1][1][0] > 0) puts("Break a heart"); else if (dp[1][1][0] < 0) puts("Cry"); else puts("Flowers"); return 0; }
4
#include <bits/stdc++.h> using namespace std; void solve() { string a, b; cin >> a >> b; long long n = a.size(); map<long long, long long> c1, c2; c1['4'] = 0; c1['7'] = 0; c2['4'] = 0; c2['7'] = 0; for (long long i = 0; i < a.length(); i++) { c1[a[i]]++; c2[b[i]]++; } long long ans = 0; long long nn, mm; for (auto x : c1) { nn = x.second; mm = c2[x.first]; long long diff = abs(mm - nn); if (diff > 0) break; } long long diff = abs(nn - mm); ans += diff; for (long long i = 0; i < a.length(); i++) { if (diff > 0) { if (a[i] == '7' && b[i] == '4') { diff--; a[i] = '4'; } else if (a[i] == '4' && b[i] == '7') { diff--; a[i] = '7'; } } else break; } long long c = 0; for (long long i = 0; i < n; i++) { if (a[i] != b[i]) c++; } ans += c / 2; cout << ans << "\n"; } int32_t main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
1
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> #include<iostream> #include<vector> #include<list> #include<cmath> #include<time.h> #include<unordered_map> #include<bits/stdc++.h> #define MAXN 2010101 using namespace std; typedef long long ll; typedef unsigned long long ull; const ll MOD=1000000000+7; int k, M=90; int a[100], b[100], c[100], zero[100]; void int2kits(int x, int *kits){ int p=0; while(x){ kits[p++]=x%k; x/=k; } } void add(int *f, int *s, int *t){ for(int i=0;i<M;i++) t[i]=(f[i]+s[i])%k; } void sub(int *f, int *s, int *t){ for(int i=0;i<M;i++) t[i]=(f[i]-s[i]+k)%k; } int kits2int(int *kits){ int res=0, tmp=1; for(int i=0;i<M;i++) { res+=kits[i]*tmp; tmp*=k; } return res; } bool tryout(int x){ printf("%d\n",x); fflush(stdout); int r; scanf("%d", &r); return r; } int main(){ int t; cin>>t; while(t--){ memset(a,0,sizeof(a)), memset(b,0,sizeof(b)), memset(c,0,sizeof(c)); int n; scanf("%d%d", &n, &k); for(int i=0;i<n;i++) { int2kits(i, a); if(i%2==0) add(c, a, b); else sub(c, a, b); if(tryout(kits2int(b))) break; sub(b, c, c); } } }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a, b, c; int cnt[100005]; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) cnt[i] = 0; for (int i = 1; i <= m; i++) { scanf("%d %d %d", &a, &b, &c); if (cnt[a] == 0 && cnt[b] != 1 && cnt[c] != 1) cnt[a] = 1; else if (cnt[a] == 0 && cnt[b] != 2 && cnt[c] != 2) cnt[a] = 2; else if (cnt[a] == 0 && cnt[b] != 3 && cnt[c] != 3) cnt[a] = 3; if (cnt[b] == 0 && cnt[a] != 1 && cnt[c] != 1) cnt[b] = 1; else if (cnt[b] == 0 && cnt[a] != 2 && cnt[c] != 2) cnt[b] = 2; else if (cnt[b] == 0 && cnt[a] != 3 && cnt[c] != 3) cnt[b] = 3; if (cnt[c] == 0 && cnt[a] != 1 && cnt[b] != 1) cnt[c] = 1; else if (cnt[c] == 0 && cnt[a] != 2 && cnt[b] != 2) cnt[c] = 2; else if (cnt[c] == 0 && cnt[a] != 3 && cnt[b] != 3) cnt[c] = 3; } for (int i = 1; i <= n; i++) printf("%d ", cnt[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e18; const int N = (int)2e5 + 99; int n, k; long long a[N], b[N]; bool moze(long long x) { vector<vector<long long> > ispr(k); vector<long long> cur(n); for (int i = 0; i < n; ++i) cur[i] = a[i]; for (int i = 0; i < n; ++i) { if (cur[i] / b[i] + 1 < k) ispr[cur[i] / b[i] + 1].push_back(i); cur[i] %= b[i]; } int tren = 0; for (int j = 0; j < k; ++j) { while (tren < k && ispr[tren].empty()) ++tren; if (tren <= j) return false; if (tren == k) return true; int i = ispr[tren].back(); if (cur[i] + x < b[i]) { cur[i] += x; continue; } ispr[tren].pop_back(); long long jos = (cur[i] + x) / b[i]; cur[i] = (cur[i] + x) % b[i]; if (tren + jos < k) ispr[tren + jos].push_back(i); } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; long long l = 0, d = inf; while (l <= d) { long long m = (l + d) / 2; if (moze(m)) d = m - 1; else l = m + 1; } cout << (l >= inf ? -1 : l); return 0; }
4
#include <bits/stdc++.h> using namespace std; string s; int n, sl[500010 + 1], i, d[500010 + 1], dmax = 0; string a[500010 + 1]; vector<int> ke[500010 + 1], res[500010 + 1]; void docdl() { cin >> s; } void build(int u) { while (sl[u] > 0 && i < n) { i++; ke[u].push_back(i); if (sl[i] > 0) build(i); sl[u]--; } } void dfs(int u) { for (int i = 0; i < ke[u].size(); i++) { int v = ke[u][i]; d[v] = d[u] + 1; dmax = max(dmax, d[v]); dfs(v); } } void xuli() { int k = 1; n = 1; for (int i = 1; i <= s.length(); i++) { if (s[i - 1] == ',') { k = 1 - k; if (k == 1) n++; } else { if (k == 1) a[n] += s[i - 1]; else sl[n] = sl[n] * 10 + (s[i - 1] - '0'); } } i = 0; sl[0] = 1000000; build(0); d[0] = 0; dfs(0); cout << dmax << '\n'; for (int i = 1; i <= n; i++) res[d[i]].push_back(i); for (int i = 1; i <= dmax; i++) { for (int j = 0; j < res[i].size(); j++) cout << a[res[i][j]] << ' '; cout << '\n'; } } int main() { ios_base::sync_with_stdio(false); docdl(); xuli(); }
5
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; const int M = 1000000007; const double PI = atan(1) * 4; const int oo = 2100000000; int main() { int n; cin >> n; vector<pair<int, int> > v(n), g(n); for (int i = 0; i < n; ++i) { scanf("%d%d", &v[i].first, &v[i].second); int x = v[i].first; int w = v[i].second; g[i].first = x + w; g[i].second = x - w; } sort((g).begin(), (g).end()); int ls = -1e9, ans = 0; for (int i = 0; i < n; ++i) { if (g[i].second >= ls) { ++ans; ls = g[i].first; } } cout << ans; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; const int K = 20; int go[K][N]; int tin[N], tout[N]; vector<int> g[N]; int timer = 0; int perm[N]; int where[N]; pair<int, int> t[4 * N]; int n; void dfs(int u, int p = 0) { go[0][u] = p; tin[u] = timer++; for (int i = 1; i < K; ++i) { go[i][u] = go[i - 1][go[i - 1][u]]; } for (auto v : g[u]) { if (v != p) { dfs(v, u); } } tout[u] = timer++; } bool anc(int a, int b) { return tin[a] <= tin[b] && tout[b] <= tout[a]; } int lca(int a, int b) { if (anc(a, b)) return a; for (int k = K - 1; k >= 0; --k) { if (!anc(go[k][a], b)) { a = go[k][a]; } } return go[0][a]; } pair<int, int> null = {-1, -1}; pair<int, int> empt = {-2, -2}; int d; bool on_path(int a, int b, int c) { return anc(d, c) && (anc(c, a) || anc(c, b)); } bool check(int l, int r, pair<int, int> a, pair<int, int> b) { d = lca(l, r); return on_path(l, r, a.first) && on_path(l, r, a.second) && on_path(l, r, b.first) && on_path(l, r, b.second); } pair<int, int> merge(pair<int, int> a, pair<int, int> b) { if (a == null || b == null) return null; if (a == empt) return b; if (b == empt) return a; if (check(a.first, a.second, a, b)) return a; if (check(b.first, b.second, a, b)) return b; if (check(a.first, b.first, a, b)) return {a.first, b.first}; if (check(a.second, b.second, a, b)) return {a.second, b.second}; if (check(a.first, b.second, a, b)) return {a.first, b.second}; if (check(a.second, b.first, a, b)) return {a.second, b.first}; return null; } void build(int l, int r, int v) { if (l == r) { t[v] = {where[l], where[l]}; } else { int m = (l + r) >> 1; build(l, m, 2 * v + 1); build(m + 1, r, 2 * v + 2); t[v] = merge(t[2 * v + 1], t[2 * v + 2]); } } void modify(int ind, int l, int r, int v) { if (ind < l || r < ind) return; if (l == r) { t[v] = {where[ind], where[ind]}; } else { int m = (l + r) >> 1; modify(ind, l, m, 2 * v + 1); modify(ind, m + 1, r, 2 * v + 2); t[v] = merge(t[2 * v + 1], t[2 * v + 2]); } } void update(int a, int b) { int pa = perm[a]; int pb = perm[b]; swap(where[perm[a]], where[perm[b]]); swap(perm[a], perm[b]); modify(pa, 0, n - 1, 0); modify(pb, 0, n - 1, 0); } int get(int l, int r, int v, pair<int, int> &pth) { if (l == r) { pth = merge(pth, t[v]); if (pth == null) return l - 1; return l; } int m = (l + r) >> 1; if (merge(pth, t[2 * v + 1]) != null) { pth = merge(pth, t[2 * v + 1]); return get(m + 1, r, 2 * v + 2, pth); } return get(l, m, 2 * v + 1, pth); } int mex() { pair<int, int> cur = empt; return get(0, n - 1, 0, cur) + 1; } void init() { cin >> n; for (int i = 0; i < n; ++i) { cin >> perm[i]; where[perm[i]] = i; } for (int i = 1; i < n; ++i) { int p; cin >> p; --p; g[p].push_back(i); } dfs(0); build(0, n - 1, 0); } void solve() { int q; cin >> q; while (q--) { int t; cin >> t; if (t == 1) { int a, b; cin >> a >> b; --a, --b; update(a, b); } else { cout << mex() << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20); init(); solve(); }
3
#include<iostream> #include<vector> #include<string> #include<bitset> #include<algorithm> #include<functional> #define NIL (-1) #define MAX 22 #define MAX_PIECES 10 #define BURIED 1 #define NONE 0 #define PZL 1 using namespace std; class CPiece{ public: class piece{ public: int si, sj; int H, W; short m[MAX][MAX]; piece(){ for(int i = 0; i < MAX; ++i) for(int j = 0; j < MAX; ++j) m[ i ][ j ] = NONE; } void Print() const{ for(int i = 1; i <= H; ++i){ for(int j = 1; j <= W; ++j) cout << (m[i][j]==PZL?'#':'.'); cout << '\n'; } } void DetermineSiSj(){ for(int i = 1; i <= H; ++i)for(int j = 1; j <= W; ++j)if(m[i][j]==PZL){si=i-1;sj=j-1;return ;} } bool operator==(const CPiece::piece &t) const { if( H == t.H && W == t.W ){ for(int i = 1; i <= H; ++i) for(int j = 1; j <= W; ++j) if( m[i][j] != t.m[i][j] ) return false; return true; }else return false; } }; vector<piece> rots; void Minimize(){ int offX = 0, offY = 0; for(int i = 1; i <= rots[0].H; ++i){ bool bClearThisLine = true; for(int j = 1; j <= rots[0].W; ++j){ if( rots[0].m[i][j] == PZL ) bClearThisLine = false; } if( !bClearThisLine ) break; else offY++; } for(int i = 1; i <= rots[0].W; ++i){ bool bClearThisLine = true; for(int j = 1; j <= rots[0].H; ++j){ if( rots[0].m[j][i] == PZL ) bClearThisLine = false; } if( !bClearThisLine ) break; else offX++; } for(int i = 1; i <= rots[0].H; ++i){ for(int j = 1; j <= rots[0].W; ++j){ this->rots[0].m[i][j] = this->rots[0].m[i+offY][j+offX]; } } rots[0].H -= offY; rots[0].W -= offX; offY = 0; offX = 0; for(int i = rots[0].H; i >= 1; --i){ bool bClearThisLine = true; for(int j = rots[0].W; j >= 1; --j){ if( rots[0].m[i][j] == PZL ) bClearThisLine = false; } if( !bClearThisLine ) break; else offY++; } for(int i = rots[0].W; i >= 1; --i){ bool bClearThisLine = true; for(int j = rots[0].H; j >= 1; --j){ if( rots[0].m[j][i] == PZL ) bClearThisLine = false; } if( !bClearThisLine ) break; else offX++; } this->rots[0].H -= offY; this->rots[0].W -= offX; } void CreateRotations(){ rots[0].DetermineSiSj(); for(int i = 1; i <= 3; ++i){ bool bExist = false; piece next; TurnRight90( rots[rots.size()-1], next ); next.H = rots[rots.size()-1].W; next.W = rots[rots.size()-1].H; next.DetermineSiSj(); if( rots.end() == find( rots.begin(), rots.end(), next ) ) rots.push_back( next ); } //cout << rots.size() << endl; } int CountPuzzleCells() const { int ret = 0; for(int i = 1; i <= rots[0].H; ++i) for(int j = 1; j <= rots[0].W; ++j) if( rots[0].m[i][j] == PZL ) ++ret; return ret; } bool operator>(const CPiece &t) const { int area = this->rots[0].H * this->rots[0].W; int area_t = t.rots[0].H * t.rots[0].W; if( area == area_t ) return this->CountPuzzleCells() > t.CountPuzzleCells(); else return area > area_t; } private: void TurnRight90( piece &from, piece &t ){ double offsetX = (from.W+1) / 2.0; double offsetY = (from.H+1) / 2.0; for(double i = 1 - offsetY; i <= from.H - offsetY + 0.1; i += 1.0){ for(double j = 1 - offsetX; j <= from.W - offsetX + 0.1; j += 1.0){ double ti = j; double tj = -i; t.m[ int(ti + offsetX) ][ int(tj + offsetY) ] = from.m[ int(i + offsetY) ][ int(j + offsetX) ]; } } } }; class CPieces{ public: vector< CPiece > vPieces; bitset< MAX_PIECES > bUse; bitset< MAX_PIECES > bUsed; }; class CPuzzle{ public: int H, W; short m[MAX][MAX]; short chk[MAX][MAX]; bool inRangeHeight(int i){ return 1 <= i && i <= H; } bool inRangeWidth(int j){ return 1 <= j && j <= W; } void Initialize(){ for(int i = 0; i < MAX; ++i) for(int j = 0; j < MAX; ++j){ m[ i ][ j ] = BURIED; chk[MAX][MAX] = 0; } } void Print(){ for(int i = 1; i <= H; ++i){ for(int j = 1; j <= W; ++j) cout << (m[i][j]==BURIED?'#':'.'); cout << '\n'; } } bool CheckAndPutPiece( int i, int j, unsigned int choose, int rot, CPieces &ps ){ int tmpm[MAX][MAX]; bool bOk = true; CPiece::piece &pce = ps.vPieces[choose].rots[rot]; for(int ci = 0; ci < MAX; ++ci) for(int cj = 0; cj < MAX; ++cj) tmpm[ci][cj]=m[ci][cj]; PutPiece( i, j, pce ); for(int ci = 1; ci <= H; ++ci){ for(int cj = 1; cj <= W; ++cj){ if( m[ci][cj] == NONE ){ bool bFound = false; for(unsigned int k = 0; k < ps.vPieces.size() && !bFound; ++k){ if( k != choose && ps.bUse[k] && !ps.bUsed[k] ){ for(unsigned int l = 0; l < ps.vPieces[k].rots.size() && !bFound; ++l){ if( TryPutPiece( ci - ps.vPieces[k].rots[l].si, cj - ps.vPieces[k].rots[l].sj, ps.vPieces[k].rots[l] ) ) { CheckPiece( ci - ps.vPieces[k].rots[l].si, cj - ps.vPieces[k].rots[l].sj, ps.vPieces[k].rots[l] ); } } } } } } } for(int ci = 1; ci <= H && bOk; ++ci) for(int cj = 1; cj <= W && bOk; ++cj) if( m[ci][cj] == NONE && chk[ci][cj] == 0 ) bOk = false; if( !bOk ){ for(int ci = 0; ci < MAX; ++ci) for(int cj = 0; cj < MAX; ++cj) m[ci][cj] = tmpm[ci][cj]; return false; }else return true; } void ResetChk(){ for(int i = 0; i < MAX; ++i) for(int j = 0; j < MAX; ++j) chk[i][j] = 0; } bool TryPutPiece( int i, int j, CPiece::piece &p ){ CPiece::piece &pce = p; if( !inRangeHeight(i) || !inRangeWidth(j) || !inRangeHeight(i+pce.H-1) || !inRangeWidth(j+pce.W-1) ) return false; for(int ci = i; ci <= i + pce.H - 1; ++ci) for(int cj = j; cj <= j + pce.W - 1; ++cj) if( m[ci][cj] == BURIED && pce.m[ci-i+1][cj-j+1] == PZL ) return false; return true; } bool Complete(){ for(int i = 1; i <= H; ++i) for(int j = 1; j <= W; ++j) if( m[i][j] == NONE ) return false; return true; } int CountEmptyCells(){ int ret = 0; for(int i = 1; i <= H; ++i) for(int j = 1; j <= W; ++j) if( m[i][j] == NONE ) ++ret; return ret; } private: void CheckPiece( int i, int j, CPiece::piece &p){ CPiece::piece &pce = p; if( !inRangeHeight(i) || !inRangeWidth(j) || !inRangeHeight(i+pce.H-1) || !inRangeWidth(j+pce.W-1) ) return ; for(int ci = i; ci <= i + pce.H - 1; ++ci) for(int cj = j; cj <= j + pce.W - 1; ++cj) if( m[ci][cj] == NONE && pce.m[ci-i+1][cj-j+1] == PZL ) chk[ci][cj] = 1; } void PutPiece( int i, int j, CPiece::piece &p ){ CPiece::piece &pce = p; for(int ci = i; ci <= i + pce.H - 1; ++ci) for(int cj = j; cj <= j + pce.W - 1; ++cj) if( m[ci][cj] == NONE && pce.m[ci-i+1][cj-j+1] == PZL ) m[ci][cj] = BURIED; } }; bool DFS( CPuzzle &p, CPieces &ps, bool &bAbort ){ int choose = NIL; //p.Print();cout<<endl; for(unsigned int i = 0; i < ps.vPieces.size(); ++i){ if( ps.bUse[i] && !ps.bUsed[i] ){ choose = i; break; } } if( choose == NIL ){ if( p.Complete() ) return true; else return false; }else if( p.Complete() ){ bAbort = true; return true; } for(unsigned int k = 0; k < ps.vPieces[choose].rots.size(); ++k){ /*cout << "Considering : "<<ps.vPieces[choose].rots[k].si <<"," <<ps.vPieces[choose].rots[k].sj<<endl; ps.vPieces[choose].rots[k].Print(); cout << endl;*/ for(int i = 1; i <= p.H; ++i){ for(int j = 1; j <= p.W; ++j){ if( p.m[i][j] == NONE ){ if( p.TryPutPiece( i - ps.vPieces[choose].rots[k].si, j - ps.vPieces[choose].rots[k].sj, ps.vPieces[choose].rots[k] ) ) { CPuzzle pzl_tmp = p; bool bResult; /*cout << "Chosen: " << choose << " (i,j) = (" << i << "," << j << ")\n"; cout << "Piece :\n"; ps.vPieces[choose].rots[k].Print(); cout << endl;*/ pzl_tmp.ResetChk(); if( pzl_tmp.CheckAndPutPiece( i - ps.vPieces[choose].rots[k].si, j - ps.vPieces[choose].rots[k].sj, choose, k, ps ) ) { ps.bUsed.set( choose ); bResult = DFS( pzl_tmp, ps, bAbort ); if( bResult ) return true; ps.bUsed.reset( choose ); } } } } } } return false; } int main(){ while( true ){ int n; int p, pnum; CPuzzle pzl; CPieces ps; pzl.Initialize(); cin >> pzl.H >> pzl.W; if( pzl.H == 0 && pzl.W == 0 ) break; for(int i = 1; i <= pzl.H; ++i){ string s; cin >> s; for(int j = 1; j <= pzl.W; ++j){ if( s[j - 1] == '#' ) pzl.m[ i ][ j ] = BURIED; else pzl.m[ i ][ j ] = NONE; } } cin >> n; for(int i = 0; i < n; ++i){ CPiece tmp; CPiece::piece t; cin >> t.H >> t.W; for(int j = 1; j <= t.H; ++j){ string s; cin >> s; for(int k = 1; k <= t.W; ++k){ if( s[k - 1] == '#' ) t.m[ j ][ k ] = PZL; else t.m[ j ][ k ] = NONE; } } tmp.rots.push_back( t ); tmp.Minimize(); tmp.CreateRotations(); ps.vPieces.push_back( tmp ); } cin >> p; for(int i = 0; i < p; ++i){ cin >> pnum; bool bAbort = false; int cntPzl = 0; int cntPss = 0; CPuzzle pzl_tmp = pzl; CPieces ps_tmp; ps_tmp.bUse.reset(); ps_tmp.bUsed.reset(); for( int j = 0; j < pnum; ++j ){ int t; cin >> t; ps_tmp.vPieces.push_back( ps.vPieces[t-1] ); cntPss += ps.vPieces[t-1].CountPuzzleCells(); ps_tmp.bUse.set( j ); } cntPzl = pzl_tmp.CountEmptyCells(); if( cntPzl != cntPss ){ cout << "NO\n"; continue; } /* ツパツズツδ仰づ個妥・ツつォツつ「ツ渉づ可青ョツ療ア */ sort( ps_tmp.vPieces.begin(), ps_tmp.vPieces.end(), greater<CPiece>() ); if( DFS( pzl_tmp, ps_tmp, bAbort ) ) cout << (bAbort ? "NO\n" : "YES\n"); else cout << "NO\n"; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; long long ans; int n, w; int pos[maxn], v[maxn]; int seq[maxn]; pair<double, double> k[maxn]; map<double, int> rec; int bit[maxn]; void update(int p) { while (p <= n) { bit[p]++; p += (p & (-p)); } return; } int calc(int p) { int ans = 0; while (p > 0) { ans += bit[p]; p -= (p & (-p)); } return ans; } bool cmp(pair<double, double> a, pair<double, double> b) { if (a.first != b.first) return a.first < b.first; else return a.second > b.second; } int main() { scanf("%d%d", &n, &w); for (int i = 1; i <= n; i++) { scanf("%d%d", &pos[i], &v[i]); k[i].first = -(double)pos[i] / (v[i] + w); k[i].second = -(double)pos[i] / (v[i] - w); } sort(k + 1, k + n + 1, cmp); for (int i = 1; i <= n; i++) rec[k[i].second] = 0; int tot = 0; for (map<double, int>::iterator ii = rec.begin(); ii != rec.end(); ii++) ii->second = ++tot; for (int i = 1; i <= n; i++) seq[i] = rec[k[i].second]; for (int i = 1; i <= n; i++) { ans += (long long)(i - calc(seq[i] - 1) - 1); update(seq[i]); } printf("%I64d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; #define ll long long #define vi vector<int> #define pii pair<int,int> #define vii vector<pair<int,int>> ll MOD = 1e9+7; ll inv(ll a, ll b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; } ll fac[100001]; ll C(ll n, ll m){ return (((fac[n]*inv(fac[m],MOD))%MOD)*(inv(fac[n-m], MOD)))%MOD; } int main(){ ios::sync_with_stdio(0); int n, k; ll ans = 0; cin >> n >> k; ll a[n], b[n], t = n-1; for (int i = 0 ; i < n ; ++i){ cin >> a[i]; b[i] = a[i]; } sort(a, a+n); fac[0] = 1; for (int i = 1 ; i < 100001 ; ++i) fac[i] = (fac[i-1]*i)%MOD; for(int i = 0;i<n;i++){ if(i>=k-1)ans+=a[i]*C(i,k-1),ans%=MOD; if(n-i-1>=k-1)ans-=a[i]*C(n-i-1,k-1),(ans+=MOD)%=MOD; } cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; map<string, int> m; void solve() { m["H"] = 1; m["HE"] = 1; m["LI"] = 1; m["BE"] = 1; m["B"] = 1; m["C"] = 1; m["N"] = 1; m["O"] = 1; m["F"] = 1; m["NE"] = 1; m["NA"] = 1; m["MG"] = 1; m["AL"] = 1; m["SI"] = 1; m["P"] = 1; m["S"] = 1; m["CL"] = 1; m["AR"] = 1; m["K"] = 1; m["CA"] = 1; m["SC"] = 1; m["TI"] = 1; m["V"] = 1; m["CR"] = 1; m["MN"] = 1; m["FE"] = 1; m["CO"] = 1; m["NI"] = 1; m["CU"] = 1; m["ZN"] = 1; m["GA"] = 1; m["GE"] = 1; m["AS"] = 1; m["SE"] = 1; m["BR"] = 1; m["KR"] = 1; m["RB"] = 1; m["SR"] = 1; m["Y"] = 1; m["ZR"] = 1; m["NB"] = 1; m["MO"] = 1; m["TC"] = 1; m["RU"] = 1; m["RH"] = 1; m["PD"] = 1; m["AG"] = 1; m["CD"] = 1; m["IN"] = 1; m["SN"] = 1; m["SB"] = 1; m["TE"] = 1; m["I"] = 1; m["XE"] = 1; m["CS"] = 1; m["BA"] = 1; m["LA"] = 1; m["HF"] = 1; m["TA"] = 1; m["W"] = 1; m["RE"] = 1; m["OS"] = 1; m["IR"] = 1; m["PT"] = 1; m["AU"] = 1; m["HG"] = 1; m["TL"] = 1; m["PB"] = 1; m["BI"] = 1; m["PO"] = 1; m["AT"] = 1; m["RN"] = 1; m["FR"] = 1; m["RA"] = 1; m["AC"] = 1; m["RF"] = 1; m["DB"] = 1; m["SG"] = 1; m["BH"] = 1; m["HS"] = 1; m["MT"] = 1; m["DS"] = 1; m["RG"] = 1; m["CN"] = 1; m["NH"] = 1; m["FL"] = 1; m["MC"] = 1; m["LV"] = 1; m["TS"] = 1; m["OG"] = 1; m["LA"] = 1; m["CE"] = 1; m["PR"] = 1; m["ND"] = 1; m["PM"] = 1; m["SM"] = 1; m["EU"] = 1; m["GD"] = 1; m["TB"] = 1; m["DY"] = 1; m["HO"] = 1; m["ER"] = 1; m["TM"] = 1; m["YB"] = 1; m["LU"] = 1; m["AC"] = 1; m["TH"] = 1; m["PA"] = 1; m["U"] = 1; m["NP"] = 1; m["PU"] = 1; m["AM"] = 1; m["CM"] = 1; m["BK"] = 1; m["CF"] = 1; m["ES"] = 1; m["FM"] = 1; m["MD"] = 1; m["NO"] = 1; m["LR"] = 1; string p; int dp[12]; char str[12]; scanf("%s", str + 1); memset(dp, 0, sizeof dp); n = strlen(str + 1); dp[0] = 1; for (int i = 1; i <= n; ++i) { p.clear(); p += str[i]; if (m[p]) dp[i] |= dp[i - 1]; p.clear(); if (i >= 1) p += str[i - 1], p += str[i], dp[i] |= (m[p] * dp[i - 2]); } puts(dp[n] ? "YES" : "NO"); } signed main() { solve(); }
6
#include <stdio.h> int main(){printf("%s\n",scanf("%*d/0%*[1-4]/%*d%*s")<0?"Heisei":"TBD");}
0
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(false); int n; int a[3001], b[3001], c[3001]; long long d0[3001], d1[3001]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) cin >> c[i]; d0[0] = a[0]; d1[0] = b[0]; for (int i = 1; i < n; i++) { d0[i] = a[i] + d1[i - 1] > b[i] + d0[i - 1] ? a[i] + d1[i - 1] : b[i] + d0[i - 1]; d1[i] = b[i] + d1[i - 1] > c[i] + d0[i - 1] ? b[i] + d1[i - 1] : c[i] + d0[i - 1]; } cout << d0[n - 1] << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, a[16]; bool flag; void dfs(int x, int ans) { if (x == n) { if (ans % 360 == 0) { flag = 1; } return; } dfs(x + 1, ans + a[x]); if (ans >= a[x]) { dfs(x + 1, ans - a[x]); } else { dfs(x + 1, 360 + ans - a[x]); } return; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } dfs(0, 0); if (flag) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long pow(long long b, long long e, long long m) { long long t = 1; for (; e; e >>= 1, b = b * b % m) e & 1 ? t = t * b % m : 0; return t; } template <class T> inline bool chkmin(T &a, T b) { return a > b ? a = b, true : false; } template <class T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; } template <class T> inline T sqr(T x) { return x * x; } template <typename T> T gcd(T x, T y) { for (T t; x; t = x, x = y % x, y = t) ; return y; } template <class edge> struct Graph { vector<vector<edge> > adj; Graph(int n) { adj.clear(); adj.resize(n + 5); } Graph() { adj.clear(); } void resize(int n) { adj.resize(n + 5); } void add(int s, edge e) { adj[s].push_back(e); } void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); } vector<edge> &operator[](int t) { return adj[t]; } }; const int maxn = 220000; int bit[maxn]; int top = 1; void bit_modify(int k, int v) { for (; k < maxn; k += k & -k) bit[k] += v; } int bit_query(int k) { int t = bit[k]; for (; k &= k - 1; t += bit[k]) ; return t; } int main() { ios_base::sync_with_stdio(false); int n, t, a, x, k; cin >> n; double sum = 0; cout << setprecision(15); for (int i = 1; i <= n; ++i) { cin >> t; if (t == 1) { cin >> a >> x; sum += (double)a * x; bit_modify(1, x); bit_modify(a + 1, -x); } else if (t == 2) { cin >> k; sum += k; ++top; bit_modify(top, k); bit_modify(top + 1, -k); } else if (t == 3) { int lx = bit_query(top); bit_modify(top, -lx); bit_modify(top + 1, lx); sum -= lx; --top; } cout << sum / top << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; string a; bool is() { for (int i = 0; i < a.size() / 2; i++) { if (a[i] != a[a.size() - 1 - i]) return false; } return true; } void ins(int n, char b) { a = a.substr(0, n) + b + a.substr(n, a.size()); } void ele(int n) { a = a.substr(0, n) + a.substr(n + 1, a.size()); } bool solve() { for (int i = 0; i <= a.size(); i++) { for (char j = 'a'; j <= 'z'; j++) { ins(i, j); if (is()) return true; ele(i); } } return false; } int main() { cin >> a; if (solve()) cout << a << endl; else cout << "NA" << endl; return 0; }
5
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } void koncz() { cout << "0" << endl; exit(0); } long long n; vector<long long> ak; vector<long long> graj(vector<long long> x) { cout << ((long long)(x).size()) << " "; for (long long el : x) { cout << el + 1 << " "; ak[el] = 1; } cout << endl; long long y; cin >> y; y--; vector<long long> res; for (long long i = (0); i <= ((long long)(((long long)(x).size())) - 1); i++) { if (ak[y] == 1) { res.push_back(y); ak[y] = 0; } y++; if (y == n) y = 0; } return res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); if (0) cout << fixed << setprecision(6); cin >> n; if (n <= 3) { koncz(); } long long naj = 0; long long siz = 1; for (long long i = 1; i < n; i++) { long long res = n - (n + i - 1) / i - i + 1; if (res > naj) { naj = res; siz = i; } } (siz, naj); ak.resize(n); vector<long long> todo; for (long long i = (0); i <= ((long long)(n - 1) - 1); i++) { if (i % siz != siz - 1) { todo.push_back(i); } } (todo); vector<long long> ost; while (((long long)(todo).size())) { vector<long long> x = ost; while (((long long)(x).size()) != siz && ((long long)(todo).size())) { x.push_back(todo.back()); todo.pop_back(); } ost = graj(x); } koncz(); }
6
#include <bits/stdc++.h> using namespace std; int n, m, na, nd; int aa[111], ad[111], b[111]; int not_broke() { int ans = 0; for (int i = 1; i <= na; ++i) { if (m - i + 1 < 1) continue; int res = 0, tot = 0; for (int k = 1; k <= i; ++k) res += b[m - i + k] - aa[k], tot += b[m - i + k] >= aa[k]; if (tot == i) ans = max(ans, res); } return ans; } int broke() { if (n > m) return 0; for (int i = 1; i <= nd; ++i) { int fd = 0; for (int j = 1; j <= m; ++j) { if (b[j] > ad[i]) { for (int k = j; k <= m - 1; ++k) b[k] = b[k + 1]; m--; fd = 1; break; } } if (!fd) return 0; } for (int i = 1; i <= na; ++i) if (aa[i] > b[m - na + i]) return 0; int ans = 0; for (int i = 1; i <= m; ++i) ans += b[i]; for (int i = 1; i <= na; ++i) ans -= aa[i]; return ans; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { char st[11]; int x; scanf("%s%d", st, &x); if (st[0] == 'D') ad[++nd] = x; else aa[++na] = x; } for (int i = 1; i <= m; ++i) scanf("%d", &b[i]); sort(aa + 1, aa + 1 + na); sort(b + 1, b + 1 + m); sort(ad + 1, ad + 1 + nd); int ans = not_broke(); ans = max(ans, broke()); cout << ans << endl; }
4
#include<bits/stdc++.h> #define pb push_back #define mp make_pair #define fi first #define se second #define SZ(x) ((int)x.size()) #define ref(i,x,y)for(int i=x;i<=y;++i) using namespace std; typedef long long LL; typedef pair<int,int> PII; int n,m,a[101][101],id[10010];int vis[101]; int s[101][101],link[101],LK[101]; bool dfs(int x){ if(vis[x])return 0;vis[x]=1; ref(i,1,n)if(s[x][i]){ if(!link[i]||dfs(link[i])){ link[i]=x; return 1; } } return 0; } int main(){ scanf("%d%d",&n,&m); ref(i,1,n)ref(j,1,m)scanf("%d",&a[i][j]); ref(i,1,n*m)id[i]=(i-1)/m+1; ref(i,1,m){ memset(s,0,sizeof s); ref(I,1,n)ref(J,i,m)s[I][id[a[I][J]]]++; memset(link,0,sizeof link); ref(I,1,n){ memset(vis,0,sizeof vis); dfs(I); } ref(I,1,n)LK[link[I]]=I; ref(I,1,n){ ref(J,i,m)if(LK[I]==id[a[I][J]]){ swap(a[I][i],a[I][J]); break; } } } ref(i,1,n){ ref(j,1,m)cout<<a[i][j]<<" "; cout<<endl; } ref(i,1,m){ ref(j,1,n){ ref(k,j,n)if(id[a[k][i]]==j){ swap(a[j][i],a[k][i]); break; } } } ref(i,1,n){ ref(j,1,m)cout<<a[i][j]<<" "; cout<<endl; } }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 20; int n; vector<int> adj[MAXN]; long long c[MAXN], f[MAXN], g[MAXN]; void get(int v, int p = -1) { long long m1 = 0, m2 = 0; for (int u : adj[v]) if (u != p) { get(u, v); m1 = max(m1, f[u]); m2 = min(m2, g[u]); } c[v] += m1; c[v] += m2; f[v] = m1; g[v] = m2; if (c[v] < 0) { f[v] -= c[v]; c[v] = 0; } else { g[v] -= c[v]; c[v] = 0; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 0; i < n; i++) cin >> c[i]; get(0); cout << f[0] - g[0] << endl; return 0; }
4
#include <iostream> using namespace std; int main(){ string s; cin >> s; cout << s.substr(0,4) << ' ' << s.substr(4) << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int w, m; cin >> w >> m; bool f = true; int cnt = 0; if (w == 1) { if (m <= 100) cout << "YES"; else cout << "NO"; return 0; } while (((m + 1) % w == 0 || (m - 1) % w == 0 || m % w == 0) && m > 1) { if (m % w == 0) { m /= w; cnt++; } if ((m + 1) % w == 0) { m = (m + 1) / w; cnt++; continue; } if ((m - 1) % w == 0) { m = (m - 1) / w; cnt++; continue; } } if (m <= 1) cout << "YES"; else cout << "NO"; }
3
#include <bits/stdc++.h> using namespace std; const int N = 205; int n; int ans[N]; char str[N]; int main() { scanf("%d", &n); scanf("%s", str + 1); for (register int i = 1; i <= n; ++i) for (register int j = i + 1; j <= n; ++j) for (register int k = j + 1; k <= n; ++k) if (str[i] > str[j] && str[j] > str[k]) { puts("NO"); return 0; } ans[1] = 0; for (register int i = 2; i <= n; ++i) { for (register int j = 1; j < i; ++j) if (str[j] > str[i]) { ans[i] = ans[j] ^ 1; break; } } puts("YES"); for (register int i = 1; i <= n; ++i) printf("%d", ans[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, q, m, b[1000000]; struct arr { int x, y; }; struct stp { long long ans, l, r; vector<arr> pre, suf; } z; struct segment_tree { stp a[1000000]; stp merge(stp x, stp y) { z.l = x.l, z.r = y.r; z.ans = x.ans + y.ans; for (int i = 0, j = x.suf.size(); i < y.pre.size(); i++) { while (j && (x.suf[j - 1].y | y.pre[i].y) >= m) --j; if (j != x.suf.size()) z.ans += (x.suf[j].x - x.l + 1) * ((i + 1 < y.pre.size() ? y.pre[i + 1].x : y.r + 1) - y.pre[i].x); } z.pre = x.pre; for (int i = 0; i < y.pre.size(); i++) if ((z.pre.back().y | y.pre[i].y) != z.pre.back().y) z.pre.push_back({y.pre[i].x, z.pre.back().y | y.pre[i].y}); z.suf = y.suf; for (int i = 0; i < x.suf.size(); i++) if ((z.suf.back().y | x.suf[i].y) != z.suf.back().y) z.suf.push_back({x.suf[i].x, z.suf.back().y | x.suf[i].y}); return z; } void build(int l, int r, int k) { a[k].l = l, a[k].r = r; if (l == r) { a[k].ans = b[l] >= m; a[k].pre.push_back({l, b[l]}); a[k].suf.push_back({l, b[l]}); return; } int mid = (l + r) >> 1; build(l, mid, k << 1); build(mid + 1, r, (k << 1) | 1); a[k] = merge(a[k << 1], a[(k << 1) | 1]); } void update(int l, int r, int k, int x, int y) { if (l == r) { a[k].ans = y >= m; a[k].pre[0] = {l, y}; a[k].suf[0] = {l, y}; return; } int mid = (l + r) >> 1; if (x <= mid) update(l, mid, k << 1, x, y); if (x > mid) update(mid + 1, r, (k << 1) | 1, x, y); a[k] = merge(a[k << 1], a[(k << 1) | 1]); } stp find(int l, int r, int k, int x, int y) { if (l >= x && r <= y) return a[k]; int mid = (l + r) >> 1; if (y <= mid) return find(l, mid, k << 1, x, y); if (x > mid) return find(mid + 1, r, (k << 1) | 1, x, y); return merge(find(l, mid, k << 1, x, y), find(mid + 1, r, (k << 1) | 1, x, y)); } } st; int main() { scanf("%d%d%d", &n, &q, &m); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); st.build(1, n, 1); for (int i = 1; i <= q; i++) { int bo, x, y; scanf("%d%d%d", &bo, &x, &y); if (bo & 1) st.update(1, n, 1, x, y); else printf("%lld\n", st.find(1, n, 1, x, y).ans); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007, ar = 200005; priority_queue<int> q; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int x, k; cin >> x >> k; vector<int> vec; for (int w = 0; w < x; w++) { int a; cin >> a; vec.push_back(a); } sort(vec.begin(), vec.end()); if (k == 0) if (vec[0] > 1) cout << 1 << endl; else cout << -1 << endl; else if (k == x) cout << vec[vec.size() - 1] << endl; else if (abs(vec[k - 1] - vec[k]) == 0) cout << -1 << endl; else cout << vec[k - 1] << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[6], l, s; int main() { int i; for (i = 0; i < 6; i++) scanf("%d", &a[i]); l = a[0] + a[1] + a[2]; s = l * l - a[0] * a[0] - a[2] * a[2] - a[4] * a[4]; printf("%d\n", s); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e7 + 1; int arr[N]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; int mx = 0, cnt; for (int i = 0; i < n; i++) { cnt = 1; for (int j = i; j > 0; j--) { if (arr[j] < arr[j - 1]) break; cnt++; } for (int j = i; j < n - 1; j++) { if (arr[j] < arr[j + 1]) break; cnt++; } mx = max(mx, cnt); } cout << mx << endl; }
2
#include<bits/stdc++.h> using namespace std; int main() { int A,B; cin>>A>>B; if(A>=13) cout<<B; else if(A<=5) cout<<0; else cout<<B/2; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { float d, h, u, e; cin >> d >> h >> u >> e; if (u < e * 3.14159 * d * d / 4) cout << "NO"; else cout << "YES" << "\n" << h / (u / (3.14159265 * d * d / 4) - e); }
1
#include <bits/stdc++.h> using namespace std; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x < 0 ? -x : x; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T> bool chmin(T& x, const T& y) { if (x > y) { x = y; return true; } return false; } template <typename T> bool chmax(T& x, const T& y) { if (x < y) { x = y; return true; } return false; } vector<vector<int>> e; int main(int, char**) { ios_base::sync_with_stdio(false); cin.tie(NULL); srand(421); int n, m; cin >> n >> m; vector<vector<int>> e(n); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u; --v; e[u].push_back(v); e[v].push_back(u); } vector<vector<int>> dists(n, vector<int>(n, -1)); for (int i = 0; i < n; ++i) { auto& d = dists[i]; queue<int> q; q.push(i); d[i] = 0; while (q.size()) { int x = q.front(); q.pop(); for (int y : e[x]) { if (d[y] == -1) { d[y] = d[x] + 1; q.push(y); } } } } set<int> st; vector<int> v(3); for (int i = 0; i < 3; ++i) { do { v[i] = rand() % n; } while (st.count(v[i])); st.insert(v[i]); } int target = -1; for (;;) { bool ok = false; for (int i = 0; i < 3; ++i) { if (i) { cout << " "; } cout << v[i] + 1; if (target == v[i]) { ok = true; } } cout << endl; cout.flush(); if (ok) { break; } cin >> target; --target; for (int i = 0; i < 3; ++i) { if (target == v[i]) { ok = true; } } if (ok) { break; } vector<int> p = {0, 1, 2}; sort((p).begin(), (p).end(), [&](const int a, const int b) { return dists[target][a] < dists[target][b]; }); set<int> st; for (auto& i : p) { int x = v[i]; int opt = (int)1e+9; int t = x; random_shuffle((e[x]).begin(), (e[x]).end()); for (auto& y : e[x]) { if (st.count(y)) { continue; } if (chmin(opt, dists[target][y])) { t = y; } } v[i] = t; st.insert(t); } } cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; #define int long long int set<string> se; int32_t main(){ int n;cin>>n; string s,tmp;cin>>s;se.insert(s); for(int i=1;i<n;i++){ cin>>tmp;if(se.count(tmp)||tmp[0]!=s[s.length()-1]){ cout<<"No"; return 0; } se.insert(tmp); s = tmp; } cout<<"Yes"; }
0
#include <cstring> #include <string> #include <time.h> #include <cassert> #include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <limits.h> #include <bitset> #include <list> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iostream> #include <stdio.h> #include <fstream> using namespace std; #pragma warning(disable : 4996) typedef long double ld; typedef double db; typedef long long ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<ll> vl; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<double, double> pd; #define gets(x) fgets(x, sizeof(x), stdin) #define FIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define For(i, a, b) for(ll i=a; i<b; i++) #define inc(i, a, b) for(ll i=a; i<=b; i++) #define dec(i, a, b) for(ll i=a; i>=b; i--) #define pb push_back #define mp make_pair #define sz(a) ((int)(a.size())) #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define ZERO(a) memset(a,0,sizeof(a)) #define X first #define Y second #define ub upper_bound #define lb lower_bound #define minh priority_queue <ll, vector<ll>, greater<ll>> #define minph priority_queue <pl, vector<pl>, greater<pl>> #define maxh priority_queue <ll> #define M_PI 3.14159265358979323846 #define eps (double)(0.000000001) const ll N = 1000007, M = 1e7, md = 1e9 + 7, inf = 1e9 + 5, linf = 2e18 + 5, MX = 3e5, aA = 'a' - 'A'; stack <int> stk; ll a[N], b[N], p[N], w, h, x, y, z, l, r, cnt, res, num, sum, n, m, k; string s, t; vi g[N]; bool c[N]; bool chk(ll x) { ll sx = sqrt(x); return (x == sx * sx); } int main() { FIO; cin >> m; while (m--) { cin >> n; bool x = 0; if (n % 4 == 0) { y = n / 4; x |= chk(y); } if (n % 2 == 0) { y = n / 2; x |= chk(y); } cout << (x ? "YES" : "NO") << '\n'; } }
2
#include <bits/stdc++.h> using namespace std; long long dp[101][101][101]; char txt[101]; int sz; long long MD = 1000000007; long long norm(long long &x) { return x %= MD; } void tran(int t) { int i, j, k; auto _1 = dp[t - 2]; auto _0 = dp[t - 1]; auto _2 = dp[t]; for (i = 0; i <= sz; i++) { for (j = i; j <= sz; j++) { _2[i][j] = 0; for (k = i; k <= j; k++) { long long u; norm(u = _0[i][k] * _1[k][j]); norm(_2[i][j] += u); } } } } void init() { int i, j; for (i = 0; i <= sz; i++) { for (j = i; j <= sz; j++) { dp[0][i][j] = dp[1][i][j] = 0; } } for (i = 0; i <= sz; i++) { dp[0][i][i] = dp[1][i][i] = 1; } dp[0][0][0] = dp[1][0][0] = dp[0][sz][sz] = dp[1][sz][sz] = 2; for (i = 0; i < sz; i++) { dp[txt[i] - '0'][i][i + 1] = 1; } } int main() { int n, i; scanf("%d%d", &sz, &n); scanf("%s", txt); init(); for (i = 2; i <= n; i++) { tran(i); } printf("%lld\n", dp[n][0][sz]); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, k; string str; cin >> n >> k; cin >> str; long long l = 0, r = 0; long long cntb = 0; long long ans1 = 0, ans2 = 0; while (r < n and l <= r) { if (str[r] == 'b') { cntb++; if (cntb <= k) { ans1 = max(ans1, r - l + 1); r++; } else { while (str[l] == 'a') l++; l++; r++; } } else { ans1 = max(ans1, r - l + 1); r++; } } ans2 = 0; r = 0, l = 0; long long cnta = 0; while (r < n and l <= r) { if (str[r] == 'a') { cnta++; if (cnta <= k) { ans2 = max(ans2, r - l + 1); r++; } else { while (str[l] == 'b') l++; l++; r++; } } else { ans2 = max(ans2, r - l + 1); r++; } } cout << max(ans1, ans2); }
3
#include <bits/stdc++.h> const int MOD = 1000000007; int modpow(int a, int e) { int ret = 1; while (e > 0) { if (e & 1) ret = (1ll * ret * a) % MOD; a = (1ll * a * a) % MOD; e >>= 1; } return ret; } std::vector<int> fact, invfact; int binco(int n, int k) { if (k < 0 || k > n) return 0; int xd = (1ll * fact[n] * invfact[k]) % MOD; return (1ll * xd * invfact[n - k]) % MOD; } int pref[2005][2005]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); const int N = 100000; fact.resize(N); invfact.resize(N); fact[0] = invfact[0] = 1; for (int i = (1); i < (N); ++i) fact[i] = (1ll * fact[i - 1] * i) % MOD, invfact[i] = modpow(fact[i], MOD - 2); int n, m, r; std::cin >> n >> m >> r; std::vector<std::pair<std::pair<int, int>, int> > A; for (int i = 0; i < (n); ++i) { int x, y, b; std::cin >> x >> y >> b; A.push_back(std::make_pair(std::make_pair(x, y), b)); } auto dist = [&](std::pair<int, int> a, std::pair<int, int> b) { return std::max(std::abs(a.first - b.first), std::abs(a.second - b.second)); }; for (auto& i : (A)) { pref[i.first.first][i.first.second]++; } for (int i = 0; i < (2005); ++i) { for (int j = 0; j < (2005); ++j) { if (i > 0) pref[i][j] += pref[i - 1][j]; if (j > 0) pref[i][j] += pref[i][j - 1]; if (i > 0 && j > 0) pref[i][j] -= pref[i - 1][j - 1]; } } std::vector<int> cnt(n); int ans = 0; for (int i = 0; i < (n); ++i) { std::pair<int, int> xx = A[i].first; for (auto& pr : (A)) { if (dist(xx, pr.first) <= r) cnt[i]++; } } auto g1 = [&](int res, int a) { int xd = binco(res + a, m); xd -= binco(res, m); if (xd < 0) xd += MOD; return xd; }; auto g2 = [&](int res, int a, int b) { int xd = binco(res + a + b, m); xd -= binco(res + a, m) + binco(res + b, m); xd %= MOD; xd += binco(res, m); xd %= MOD; if (xd < 0) xd += MOD; return xd; }; auto przec = [&](std::pair<int, int> a, std::pair<int, int> b) { std::pair<int, int> x = std::make_pair(std::max(a.first - r, b.first - r), std::min(a.first + r, b.first + r)); std::pair<int, int> y = std::make_pair(std::max(a.second - r, b.second - r), std::min(a.second + r, b.second + r)); if (x.first > x.second || y.first > y.second) return 0; if (x.second > 2000) x.second = 2000; if (y.second > 2000) y.second = 2000; if (x.first < 1) x.first = 1; if (y.first < 1) y.first = 1; return pref[x.second][y.second] - pref[x.first - 1][y.second] - pref[x.second][y.first - 1] + pref[x.first - 1][y.first - 1]; }; for (int i = 0; i < (n); ++i) for (int j = 0; j < (n); ++j) { if (i == j) { int c = cnt[i]; int rest = n - c; ans = (ans + 1ll * (1ll * g1(rest, c) * A[i].second % MOD) * A[j].second) % MOD; } else { int p = przec(A[i].first, A[j].first); int a = cnt[i] - p; int b = cnt[j] - p; assert(a >= 0 && b >= 0); assert(p >= 0); int rest = n - a - b - p; assert(rest >= 0); ans = (ans + 1ll * (1ll * g2(rest, a, b) * A[i].second % MOD) * A[j].second) % MOD; ans = (ans + 1ll * (1ll * g1(n - p, p) * A[i].second % MOD) * A[j].second) % MOD; } } std::cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n; cin >> n; vector<int> v(n + 1, 0); for (int i = 0; i < n; i++) { int temp; cin >> temp; ++v[temp]; } sort(v.rbegin(), v.rend()); int ind = v[0] + 1, ans = 0; for (int i = 0; i < n; i++) { if (ind == 0 || v[i] == 0) break; if (v[i] >= ind) v[i] = ind - 1; ind = v[i]; ans += v[i]; } cout << ans << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main(){ cin.tie(0); ios::sync_with_stdio(false); int cs[26]={}; char c; while(cin>>c){ c=tolower(c); if(isalpha(c))cs[c-'a']++; } for (int i = 0; i < 26; i++){ printf("%c : %d\n",i+'a',cs[i]); } }
0
#include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "vector" using namespace std; #define MAXN 2016 #define P 1000000007 int n; vector<int> G[MAXN]; int A[MAXN] , F[MAXN] , d[MAXN] , s[MAXN] , siz[MAXN]; void dfs( int u , int fa ) { if( A[u] ) s[u] = d[u] , siz[u] = 1; else siz[u] = s[u] = 0; int mxp = 0; for( int v : G[u] ) if( v != fa ) { d[v] = d[u] + 1 , dfs( v , u ) , s[u] += s[v] , siz[u] += siz[v]; mxp = ( !mxp || s[v] > s[mxp] ) ? v : mxp; } int sum = s[u] - siz[u] * d[u] , mx = s[mxp] - siz[mxp] * d[u]; if( sum >= 2 * mx ) F[u] = sum / 2; else F[u] = sum - mx + min( F[mxp] , ( 2 * mx - sum ) / 2 ); } int main() { cin >> n; for( int i = 1 ; i <= n ; ++ i ) scanf("%1d",&A[i]); for( int i = 1 , u , v ; i < n ; ++ i ) { scanf("%d%d",&u,&v); G[u].push_back( v ) , G[v].push_back( u ); } int res = 0x3f3f3f3f + 2; for( int i = 1 ; i <= n ; ++ i ) { d[i] = 0, dfs( i , i ); if( ( ~s[i] & 1 ) && F[i] == s[i] / 2 ) res = min( res , s[i] / 2 ); } cout << ( res == 0x3f3f3f3f + 2 ? -1 : res ) << endl; }
0
#include<bits/stdc++.h> #define range(i,a,b) for(int i = (a); i < (b); i++) #define rep(i,b) for(int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; //const int INF = 1e8; using namespace std; const int M = 1000000007; int main(){ int n; long long t; cin >> n >> t; vector<long long> a(n); rep(i,n) cin >> a[i]; sort(all(a)); long long ans = 1; for(auto it = a.begin(); it != a.end(); it++){ ans *= (upper_bound(a.begin(), it, *it) - lower_bound(a.begin(), it, (*it) - t)) + 1; ans %= M; } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, s; cin >> n >> s; int sol = 100; bool ok = 0; for (int i = 1; i <= n; ++i) { int x, y; cin >> x >> y; if (x < s) { if (y) sol = min(sol, y); } if (x < s || (x == s && y == 0)) ok = 1; } if (!ok) cout << "-1\n"; else cout << 100 - sol << "\n"; return 0; }
1
#include <iostream> #include <vector> using namespace std; int c = 0; void merge(int a[], int l, int m, int r) { int n1 = m - l; int n2 = r - m; vector<int> L(n1 + 1), R(n2 + 1); for (int i = 0; i < n1; i++) L[i] = a[l + i]; for (int i = 0; i < n2; i++) R[i] = a[m + i]; L[n1] = 1000000001; R[n2] = 1000000002; int i = 0, j = 0; for (int k = l; k < r; k++) { if (L[i] <= R[j]) { a[k] = L[i]; i++; } else { a[k] = R[j]; j++; } c++; } } void mergeSort(int a[], int l, int r) { if (l + 1 < r) { int m = (l + r) / 2; mergeSort(a, l, m); mergeSort(a, m, r); merge(a, l, m, r); } } int main() { int n = 0, a[500000] = { 0 }; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; mergeSort(a, 0, n); for (int i = 0; i < n - 1; i++) cout << a[i] << " "; cout << a[n - 1] << endl; cout << c << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int i = 1, j; int x = 0; while (1) { if (i % 2 == 1) { a -= i; if (a < 0) { cout << "Vladik\n"; return 0; } } else { b -= i; if (b < 0) { cout << "Valera\n"; return 0; } } i++; } return 0; }
1