solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; long long int dp[2][151][11255], q[151]; int main() { ios::sync_with_stdio(false); ; cin.tie(0); ; long long int i, j, n, k, req, s, x, y, ans = 2000000000000000000; cin >> n >> req >> s; for (i = 1; i <= n; i++) cin >> q[i]; s = min(s, 11250LL); for (i = 0; i < 2; i++) { for (j = 0; j <= req; j++) { for (k = 0; k <= s; k++) dp[i][j][k] = 2000000000000000000; } } dp[0 & 1][0][0] = 0; for (i = 1; i <= n; i++) { for (j = 0; j <= req; j++) { for (k = 0; k <= s; k++) { dp[i & 1][j][k] = dp[(i - 1) & 1][j][k]; if (j - 1 >= 0 && (i - j) >= 0 && k - (i - j) >= 0) dp[i & 1][j][k] = min(dp[i & 1][j][k], dp[(i - 1) & 1][j - 1][k - (i - j)] + q[i]); } } } ans = 2000000000000000000; for (i = 0; i <= s; i++) ans = min(ans, dp[n & 1][req][i]); cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long sum = n; while (true) { long long cnt = 2; while (n % cnt != 0) { cnt++; if (cnt == (long long)sqrt(n) + 1) { cnt = n; break; } } n /= cnt; sum += n; if (n == 1) break; } cout << sum << endl; }
2
#include "bits/stdc++.h" using namespace std; int main(){ string t,p; cin>>t>>p; stack<char> st; vector<int> b; vector<int> f; for(int i=0;i<p.size();i++)st.push(p[i]); for(int i=t.size()-1;i>=0;i--){ if(st.size()&&t[i]==st.top()){ f.push_back(i); st.pop(); } } if(st.size()){ cout<<"no"<<endl; return 0; } while(st.size())st.pop(); for(int i=p.size()-1;i>=0;i--)st.push(p[i]); for(int i=0;i<t.size();i++){ if(st.size()&&t[i]==st.top()){ b.push_back(i); st.pop(); } } reverse(b.begin(),b.end()); if(b==f&&b.size()==p.size()){ cout<<"yes"<<endl; }else{ cout<<"no"<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long inf = 2e9 + 10; template <typename T1, typename T2> static void relax(T1 &x, T2 y) { if (x > y) x = y; } template <typename T1, typename T2> static void gain(T1 &x, T2 y) { if (x < y) x = y; } long long powmod(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int TC; const int maxn = 1e7 + 5; int n; long long x, y; long long dp[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); TC = 1; for (int(tt) = (int)(1); (tt) <= (int)(TC); ++(tt)) { cin >> n >> x >> y; dp[1] = x; for (int(i) = (int)(2); (i) <= (int)(n); ++(i)) { long long cst = dp[i - 1]; if (i & 1) dp[i] = min(dp[(i + 1) / 2] + x + y, cst + x); else dp[i] = min(dp[i / 2] + y, cst + x); } cout << dp[n] << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main(){ long long a, b, c, d, k, l, g; cin >> a >> b >> c >> d; k=c; l=d; if(c<d){ k=d; l=c; } g=l%k; while(g!=0){ l=k; k=g; g=l%k; } cout << b-a+1-b/c+(a-1)/c-b/d+(a-1)/d+b/(c/k*d)-(a-1)/(c/k*d) << endl; }
0
#include <bits/stdc++.h> using namespace std; struct node { int val; int p; } node[106]; bool cmp(const struct node x, const struct node y) { return y.val - x.val > 0; } int main() { int n, d; int var; cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> node[i - 1].val; node[i - 1].p = i; } sort(node, node + n, cmp); int ans[106], ansp = 0; for (int i = 0; i < n; i++) { if (d >= node[i].val) ans[ansp++] = node[i].p; else break; d -= node[i].val; } cout << ansp << endl; for (int i = 0; i < ansp; i++) cout << ans[i] << " "; return 0; }
1
#include <bits/stdc++.h> #define rep(i,l,n) for(int i=l;i<n;i++) #define rer(i,l,n) for(int i=l;i<=n;i++) #define all(a) a.begin(),a.end() #define o(a) cout<<a<<endl #define pb(a) push_back(a) #define mk(a,b) make_pair(a,b) #define fi first #define se second using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; int main(){ int n; vi v(2001); rer(i,0,1000){ rer(j,0,1000){ v[i+j]++; } } while(cin>>n){ int ans=0; /*sort(all(v)); rep(i,0,v.size()){ ans+=upper_bound(all(v),n-v[i])-lower_bound(all(v),n-v[i]); }*/ /*rer(i,0,min(1000,n)){ rer(j,0,min(1000,n)){ if((n-i-j)<0) continue; ans+=upper_bound(all(v),n-i-j)-lower_bound(all(v),n-i-j); } }*/ rer(i,0,v.size()){ rer(j,0,v.size()){ if(i+j==n) ans+=(v[i]*v[j]); } } o(ans); } }
0
#include <bits/stdc++.h> using namespace std; using pi = pair<int, int>; const int N = 55; char a[N][N]; pi dir[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; int n, m; pi s; bool follow(vector<int> v) { pi cur = s; for (int i : v) { cur.first += dir[i].first; cur.second += dir[i].second; if (cur.first < 1 || cur.first > n || cur.second < 1 || cur.second > m) return 0; char c = a[cur.first][cur.second]; if (c == '#') return 0; if (c == 'E') return 1; } return 0; } char buff[1000]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("\n%s", a[i] + 1); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (a[i][j] == 'S') s = pi(i, j); scanf("\n%s", buff); vector<int> v; for (int i = 0; buff[i] != 0; ++i) v.push_back(buff[i] - '0'); sort(dir, dir + 4); int res = 0; do { res += follow(v); } while (next_permutation(dir, dir + 4)); printf("%d\n", res); }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n, k, i; cin >> n >> k; long long int a[n]; map<long long int, long long int> m; for (i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; } sort(a, a + n); long long int ans[200005] = {}; for (i = 0; i < n; i++) { if (k <= m[a[i]]) { cout << 0; return 0; } long long int x = a[i], y = 0; while (x) { x /= 2; y++; if (m.find(x) == m.end() || m[x] < k) { m[x]++; ans[x] += y; } } } long long int mins = LLONG_MAX; for (i = 0; i < 200005; i++) { if (m.find(i) != m.end() && m[i] >= k) mins = min(mins, ans[i]); } cout << mins; }
6
#include <bits/stdc++.h> using namespace std; int euclid(int a, int b, int &x, int &y) { if (!a) { x = 0; y = 1; return b; } int _x, _y; int g = euclid(b % a, a, _x, _y); x = _y - b / a * _x; y = _x; return g; } void relax(int &obj, int C3, int C4, int C5, int _k3, int k4, int _k5, int &K3, int &K4, int &K5) { int f = abs(C3 * _k3 - C4 * k4) + abs(C4 * k4 - C5 * _k5); if (f < obj) { obj = f; K3 = _k3, K4 = k4, K5 = _k5; } } int main() { int n, S, a[333]; cin >> n >> S; for (int i = 0; i < n; ++i) { cin >> a[i]; } int C[11] = {0}; for (int _n(n), i(0); i < _n; i++) C[a[i]]++; int K3 = 1011111111, K4 = 1011111111, K5 = 1011111111, F = 1011111111; int ek3 = 1011111111, ek5 = 1011111111; const int D = euclid(C[3], C[5], ek3, ek5); for (int k4 = 0; k4 * C[4] <= S; ++k4) { const int REM = S - k4 * C[4]; if (REM % D) continue; const int k3 = ek3 * (REM / D), k5 = ek5 * (REM / D); int idx[] = {(k4 - k3 + C[5] / D - 1) / (C[5] / D), (k5 - k4 + C[3] / D - 1) / (C[3] / D), (k5 - k4 * C[4] / C[5] + C[3] / D - 1) / (C[3] / D) + 2, (k4 * C[4] / C[3] - k3 + C[5] / D - 1) / (C[5] / D) + 2}; for (int o = 0; o < 4; ++o) { for (int it = 0, j = idx[o]; it < 5; --j, ++it) { int _k3 = k3 + j * (C[5] / D); int _k5 = k5 - j * (C[3] / D); if (_k3 < 0) continue; if (_k3 > k4) continue; if (_k5 < k4) continue; relax(F, C[3], C[4], C[5], _k3, k4, _k5, K3, K4, K5); } } } if (F == 1011111111) { cout << -1 << endl; } else { printf("%d %d %d\n", K3, K4, K5); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, count = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { cin >> n; if (n == 1) { count = abs(2 - i) + abs(2 - j); cout << count << endl; return 0; } } } cout << count << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define N 3010 #define INF 2147483647 inline int read() { int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x; } int tot=1; int fir[N],nex[N<<1],got[N<<1]; inline void AddEdge(int x,int y) { nex[++tot]=fir[x],fir[x]=tot,got[tot]=y; } char s[N]; int siz[N],dis[N],f[N],a[N]; inline void dfs(int x,int fa) { siz[x]=a[x],dis[x]=f[x]=0; int p=0; for (int i=fir[x];i;i=nex[i]) { if (got[i]==fa) continue; dfs(got[i],x),dis[got[i]]+=siz[got[i]]; dis[x]+=dis[got[i]],siz[x]+=siz[got[i]]; if (dis[p]<dis[got[i]]) p=got[i]; } if (!p) return; if (dis[x]>=2*dis[p]) f[x]=dis[x]/2; else f[x]=dis[x]-dis[p]+min(f[p],(2*dis[p]-dis[x])/2); } int main() { int n=read(); scanf("%s",s+1); for (int i=1;i<=n;i++) a[i]=s[i]-'0'; for (int i=1;i<=n-1;i++) { int x=read(),y=read(); AddEdge(x,y),AddEdge(y,x); } int ans=INF; for (int i=1;i<=n;i++) { dfs(i,0); if (f[i]*2==dis[i]) ans=min(ans,f[i]); } printf("%d\n",ans==INF?-1:ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[110] = {4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165}; int main() { int n; cin >> n; cout << a[n - 1] << endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; int main(){ int N, m; cin >> N >> m; vector< bool > shop(N + 1, 0); for(int i = 0; i < m; ++i){ int c, d; cin >> c >> d; fill( shop.begin() + c, shop.begin() + d, true); } cout << N + 1 + count(shop.begin(), shop.end(), true) * 2 << endl; }
0
#include <bits/stdc++.h> using namespace std; const int MX = 1e3; int sum[MX + 2]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, q, l, r; string str, str2; cin >> n >> m >> q; cin >> str >> str2; for (long long i = 0; i < n - m + 1; i++) { sum[i] = (str.substr(i, m) == str2); } while (q-- > 0) { cin >> l >> r; l--, r--; r = r - m + 1; int ans = 0; for (long long i = l; i <= r; i++) ans += sum[i]; cout << ans << '\n'; } }
2
#include <bits/stdc++.h> using namespace std; int main(){ int t;cin>>t; while(t--){ int n;cin>>n; string s;cin>>s; bool flag=false; int count=0,pal=0; for(int i=0;i<n/2;i++){ if(s[i]!=s[n-1-i]){ flag=true; pal++; } } for(int i=0;i<n;i++){ if(s[i]=='0'){ count++; } } if(flag==false){ if(count==1 || count%2==0){ cout<<"BOB"<<endl; } else{ cout<<"ALICE"<<endl; } } else{ if(count==2 && pal==1){ cout<<"DRAW"<<endl; } else{ cout<<"ALICE"<<endl; } } } }
2
#include <bits/stdc++.h> using namespace std; int main(void){ long long N,K; cin >> N >> K; cout << min(N % K,K-N%K) << endl; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 10010, mod = 1000000007; int q, l, t, m; long long a, b; long long f(long long x) { return a + (x - 1) * b; } long long s(long long l, long long r) { return (f(l) + f(r)) * (r - l + 1) / 2; } int main() { scanf("%I64d%I64d%d", &a, &b, &q); while (q--) { scanf("%d%d%d", &l, &t, &m); long long L = l - 1, R = 3e6; while (L < R) { long long M = L + R + 1 >> 1; if (f(M) > t || s(l, M) > (long long)t * m) R = M - 1; else L = M; } printf("%I64d\n", L < l ? -1 : L); } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, capacity, cost; Edge(int from, int to, int capacity, int cost) : from(from), to(to), capacity(capacity), cost(cost){}; }; vector<vector<int>> adj, cost, capacity; const int INF = 2e9; void shortest_paths(int n, int v0, vector<int>& d, vector<int>& p) { d.assign(n, INF); d[v0] = 0; vector<bool> inq(n, false); queue<int> q; q.push(v0); p.assign(n, -1); while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; for (int v : adj[u]) { if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) { d[v] = d[u] + cost[u][v]; p[v] = u; if (!inq[v]) { inq[v] = true; q.push(v); } } } } } int min_cost_flow(int N, vector<Edge>& edges, int s, int t) { adj.assign(N, vector<int>()); cost.assign(N, vector<int>(N, 0)); capacity.assign(N, vector<int>(N, 0)); for (Edge e : edges) { adj[e.from].push_back(e.to); adj[e.to].push_back(e.from); cost[e.from][e.to] = e.cost; cost[e.to][e.from] = -e.cost; capacity[e.from][e.to] = e.capacity; } int flow = 0; int cost = 0; vector<int> d, p; while (1) { shortest_paths(N, s, d, p); if (d[t] == INF) break; int f = 2000000000; int cur = t; while (cur != s) { f = min(f, capacity[p[cur]][cur]); cur = p[cur]; } flow += f; cost += f * d[t]; cur = t; while (cur != s) { capacity[p[cur]][cur] -= f; capacity[cur][p[cur]] += f; cur = p[cur]; } } return cost; } int ab[76][2]; int main() { int T; int i, j; int n, k; int s, t; scanf("%d", &T); while (T--) { vector<Edge> edges; vector<int> res; scanf("%d %d", &n, &k); s = n + n, t = n + n + 1; for (i = 0; i < n; i++) { scanf("%d %d", &ab[i][0], &ab[i][1]); } for (i = 0; i < n; i++) { edges.emplace_back(s, i, 1, 0); edges.emplace_back(i + n, t, 1, 0); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i >= k - 1 && i < n - 1) edges.emplace_back(j, i + n, 1, -(k - 1) * ab[j][1]); else if (i == n - 1) edges.emplace_back(j, i + n, 1, -(ab[j][0] + (k - 1) * ab[j][1])); else edges.emplace_back(j, i + n, 1, -(ab[j][0] + i * ab[j][1])); } } min_cost_flow(n + n + 2, edges, s, t); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (!capacity[j][i + n]) { if (i >= k - 1 && i < n - 1) { res.push_back(j + 1); res.push_back(-j - 1); } else res.push_back(j + 1); } } } printf("%d\n", 2 * n - k); for (auto o : res) printf("%d ", o); printf("\n"); } }
6
#include <bits/stdc++.h> using namespace std; void input(vector<long long int> &arr) { for (int i = 0; i < arr.size(); i++) cin >> arr[i]; } void output(vector<long long int> arr) { for (int i = 0; i < arr.size(); i++) cout << arr[i] << " "; cout << "\n"; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int power(long long int a, long long int b) { if (b == 0) return 1; if (b == 1) return a; long long int smallans = power(a, b / 2); long long int myans = (smallans * smallans) % 1000000007; if ((b & 1)) myans = (myans * a) % 1000000007; return myans; } long long int multiply(long long int a, long long int b) { long long int ans = ((a % 1000000007) * (b % 1000000007)) % 1000000007; return ans; } long long int add(long long int a, long long int b) { long long int ans = ((a % 1000000007) + (b % 1000000007)) % 1000000007; return ans; } long long int subtract(long long int a, long long int b) { long long int ans = ((a % 1000000007) - (b % 1000000007)) % 1000000007; if (ans < 0) ans += 1000000007; return ans; } long long int divide(long long int a, long long int b) { return multiply(a, power(b, 1000000007 - 2)); } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; void update(vector<long long int> &tree, long long int index, long long int val) { index++; while (index < tree.size()) { tree[index] = (tree[index] + val) % 1000000007; index += index & (-index); } } long long int calculate(vector<long long int> &tree, long long int index) { long long int sum = 0; index++; while (index > 0) { sum = (sum + tree[index]) % 1000000007; index -= index & (-index); } return sum; } long long int findpar(long long int p, vector<long long int> &parent) { if (parent[p] == p) return p; parent[p] = findpar(parent[p], parent); return parent[p]; } void merge(long long int a, long long int b, vector<long long int> &parent, vector<long long int> &size_) { a = findpar(a, parent); b = findpar(b, parent); if (a != b) { if (size_[a] < size_[b]) swap(a, b); parent[b] = a; size_[a] += size_[b]; } } void solve() { long long int n; cin >> n; string s; cin >> s; long long int zero = 0; long long int one = 0; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1] && s[i - 1] == '1') one++; if (s[i] == s[i - 1] && s[i - 1] == '0') zero++; } cout << max(zero, one) << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int H = 17, M = 1 << H, inf = 0x3fffffff; int n, k; pair<int, int> seg[M << 1]; pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) { return make_pair(min(a.first, b.first), max(a.second, b.second)); } pair<int, int> get(int a, int b) { a = a + M - 1; b = b + M + 1; pair<int, int> res = make_pair(inf, -inf); while (a ^ b ^ 1) { if (~a & 1) res = res + seg[a ^ 1]; if (b & 1) res = res + seg[b ^ 1]; a >>= 1; b >>= 1; } return res; } bool can(int t) { for (int i = 1; i <= (n); ++i) { int j = i + t - 1; if (j > n) break; pair<int, int> tmp = get(i, j); if (tmp.second - tmp.first <= k) return true; } return false; } void work(int t) { int ct = 0; ostringstream out; for (int i = 1; i <= (n); ++i) { int j = i + t - 1; if (j > n) break; pair<int, int> tmp = get(i, j); if (tmp.second - tmp.first <= k) { ++ct; out << i << ' ' << j << endl; } } cout << ct << endl << out.str(); } int main(void) { scanf("%d%d", &n, &k); for (int i = 1; i <= (n); ++i) { int a; scanf("%d", &a); seg[i + M] = make_pair(a, a); } for (int i = (M - 1); i >= (1); --i) seg[i] = seg[i + i] + seg[i + i + 1]; int l = 1, r = n, mid; while (l < r) { mid = (l + r + 1) >> 1; if (can(mid)) l = mid; else r = mid - 1; } cout << l << ' '; work(l); }
5
#include <bits/stdc++.h> using namespace std; int main(){ int H,W,N; cin>>H>>W>>N; if(H<W){ swap(H,W); } int r = (N+H-1)/H; cout << r <<"\n"; }
0
#include <bits/stdc++.h> using namespace std; const double PI = 3.141592653589793; const long long I_MAX = 1LL << 60; long long mod = 1000000007; const long long maxN = 2E5; vector<long long> inv(maxN + 5, 1); vector<long long> fac(maxN + 5, 1); void ADD(long long &x, long long y) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; } void MUL(long long &x, long long y) { x *= y; x = (x % mod + mod) % mod; } long long mypow(long long b, long long e) { long long ans = 1; while (e) { if (e & 1) MUL(ans, b); MUL(b, b); e >>= 1; } return ans; } long long modinv(long long n) { return mypow(n, mod - 2); } void calcInv(long long n) { for (int i = 2; i <= n; ++i) { inv[i] = (mod - mod / i) * inv[mod % i] % mod; } } void calcFac(long long n) { for (int i = 2; i <= n; ++i) { fac[i] = fac[i - 1] * i % mod; } } long long cm(long long a, long long b) { long long ans = 1; if (a < b) return 0; MUL(ans, inv[fac[a - b]]); MUL(ans, inv[fac[b]]); MUL(ans, fac[a]); return ans; } long long Lucas(long long n, long long m) { if (m == 0) return 1; return cm(n % mod, m % mod) * Lucas(n / mod, m / mod) % mod; } long long doLucas(long long n, long long m) { calcInv(mod); calcFac(mod); return Lucas(n, m); } const long long maxn = 1E5; void precal() { return; } void solve() { long long n; scanf("%I64d", &n); int pln[n + 5][n + 5]; memset(pln, -1, sizeof(pln)); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if ((i & 1) and (j & 1)) pln[i][j] = 0; else if ((i & 1)) pln[i][j] = 2; else if ((j & 1)) pln[i][j] = 4; else pln[i][j] = 6; } pln[1][1] = 1; pln[n][n] = 0; int rep; for (int i = 1; i <= n; i += 2) { for (int j = 1; j <= n - 2 - 2 * (i == n); j += 2) { printf("? %d %d %d %d\n", i, j, i, j + 2); fflush(stdout); scanf("%d", &rep); if (rep == 1) { pln[i][j + 2] = pln[i][j]; } else { pln[i][j + 2] = pln[i][j] ^ 1; } } if (i + 2 <= n) { printf("? %d %d %d %d\n", i, 1, i + 2, 1); fflush(stdout); scanf("%d", &rep); if (rep == 1) { pln[i + 2][1] = pln[i][1]; } else { pln[i + 2][1] = pln[i][1] ^ 1; } } } for (int i = 2; i <= n; i += 2) { for (int j = 1; j <= n - 2; j += 2) { printf("? %d %d %d %d\n", i, j, i, j + 2); fflush(stdout); scanf("%d", &rep); if (rep == 1) { pln[i][j + 2] = pln[i][j]; } else { pln[i][j + 2] = pln[i][j] ^ 1; } } if (i + 2 <= n) { printf("? %d %d %d %d\n", i, 1, i + 2, 1); fflush(stdout); scanf("%d", &rep); if (rep == 1) { pln[i + 2][1] = pln[i][1]; } else { pln[i + 2][1] = pln[i][1] ^ 1; } } } for (int i = 1; i <= n; i += 2) { for (int j = 2; j <= n - 2; j += 2) { printf("? %d %d %d %d\n", i, j, i, j + 2); fflush(stdout); scanf("%d", &rep); if (rep == 1) { pln[i][j + 2] = pln[i][j]; } else { pln[i][j + 2] = pln[i][j] ^ 1; } } if (i + 2 <= n) { printf("? %d %d %d %d\n", i, 2, i + 2, 2); fflush(stdout); scanf("%d", &rep); if (rep == 1) { pln[i + 2][2] = pln[i][2]; } else { pln[i + 2][2] = pln[i][2] ^ 1; } } } for (int i = 2; i <= n; i += 2) { for (int j = 2; j <= n - 2; j += 2) { printf("? %d %d %d %d\n", i, j, i, j + 2); fflush(stdout); scanf("%d", &rep); if (rep == 1) { pln[i][j + 2] = pln[i][j]; } else { pln[i][j + 2] = pln[i][j] ^ 1; } } if (i + 2 <= n) { printf("? %d %d %d %d\n", i, 2, i + 2, 2); fflush(stdout); scanf("%d", &rep); if (rep == 1) { pln[i + 2][2] = pln[i][2]; } else { pln[i + 2][2] = pln[i][2] ^ 1; } } } printf("? %d %d %d %d\n", 1, 1, 2, 2); fflush(stdout); scanf("%d", &rep); if (rep == 1) { for (int i = 2; i <= n; i += 2) for (int j = 2; j <= n; j += 2) { pln[i][j] ^= 7; } } else { for (int i = 2; i <= n; i += 2) for (int j = 2; j <= n; j += 2) { pln[i][j] ^= 6; } } printf("? %d %d %d %d\n", 1, 2, 2, 3); fflush(stdout); scanf("%d", &rep); if (rep == 1) { int x = pln[2][3] ^ 2; for (int i = 2; i <= n; i += 2) for (int j = 1; j <= n; j += 2) { pln[i][j] ^= x; } } else { int x = pln[2][3] ^ 3; for (int i = 2; i <= n; i += 2) for (int j = 1; j <= n; j += 2) { pln[i][j] ^= x; } } int px = n, py = n; for (int i = 2; i <= n; ++i) { for (int j = 3; j <= n; ++j) { if (pln[i - 1][j - 2] == pln[i][j - 1] and pln[i][j - 2] == pln[i][j]) { px = i; py = j; } if (pln[i - 1][j - 2] != pln[i][j - 1] and pln[i][j - 2] != pln[i][j]) { px = i; py = j; } if (pln[i - 1][j - 2] == pln[i][j - 1] and pln[i - 1][j - 1] == pln[i][j]) { px = i; py = j; } if (pln[i - 1][j - 2] != pln[i][j - 1] and pln[i - 1][j - 1] != pln[i][j]) { px = i; py = j; } if (pln[i - 1][j - 2] == pln[i - 1][j] and pln[i][j - 2] == pln[i][j]) { px = i; py = j; } if (pln[i - 1][j - 2] != pln[i - 1][j] and pln[i][j - 2] != pln[i][j]) { px = i; py = j; } } } printf("? %d %d %d %d\n", px - 1, py - 2, px, py); fflush(stdout); scanf("%d", &rep); if (px % 2 != py % 2) { if (rep == 1) { int x = pln[px][py] ^ pln[px - 1][py - 2]; for (int i = 2; i <= n; i += 2) for (int j = 1; j <= n; j += 2) { pln[i][j] ^= x; } for (int i = 1; i <= n; i += 2) for (int j = 2; j <= n; j += 2) { pln[i][j] ^= x; } } else { int x = pln[px][py] ^ pln[px - 1][py - 2] ^ 1; for (int i = 2; i <= n; i += 2) for (int j = 1; j <= n; j += 2) { pln[i][j] ^= x; } for (int i = 1; i <= n; i += 2) for (int j = 2; j <= n; j += 2) { pln[i][j] ^= x; } } } else { if (rep == 1) { int x = pln[px][py] ^ pln[px - 1][py - 2]; for (int i = 2; i <= n; i += 2) for (int j = 1; j <= n; j += 2) { pln[i][j] ^= x; } for (int i = 1; i <= n; i += 2) for (int j = 2; j <= n; j += 2) { pln[i][j] ^= x; } } else { int x = pln[px][py] ^ pln[px - 1][py - 2] ^ 1; for (int i = 2; i <= n; i += 2) for (int j = 1; j <= n; j += 2) { pln[i][j] ^= x; } for (int i = 1; i <= n; i += 2) for (int j = 2; j <= n; j += 2) { pln[i][j] ^= x; } } } printf("!\n"); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { printf("%d", pln[i][j]); } printf("\n"); } fflush(stdout); } int main() { precal(); int t = 1; for (int i = 1; i <= t; ++i) { solve(); } return 0; }
5
#include <bits/stdc++.h> const int mod = 1000000007; const int MAXN = 1010; typedef long long LL; void reduce(int & x) { x += x >> 31 & mod; } int mul(int a, int b) { return (LL) a * b % mod; } int pow(int a, int b, int res = 1) { for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a); return res; } int f[MAXN], g[MAXN]; int n, A, B, C, D; int fac[MAXN], inv[MAXN]; int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); fac[0] = fac[1] = inv[0] = inv[1] = 1; for (int i = 2; i != MAXN; ++i) { fac[i] = mul(fac[i - 1], i); inv[i] = mul(inv[mod % i], mod - mod / i); } for (int i = 2; i != MAXN; ++i) inv[i] = mul(inv[i - 1], inv[i]); std::cin >> n >> A >> B >> C >> D; f[0] = 1; for (int i = A; i <= B; ++i) { memset(g, 0, sizeof g); for (int j = C; j <= D; ++j) { int sz = i * j, co = pow(inv[i], j, inv[j]); for (int k = sz; k <= n; ++k) reduce(g[k] += mul(f[k - sz], co) - mod); } for (int j = 0; j <= n; ++j) reduce(f[j] += g[j] - mod); } std::cout << mul(f[n], fac[n]) << std::endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; #define debug(x) cerr<<#x<<": "<<x<<endl typedef long long ll; const ll mod = (ll)1e9+7; int w,d,n; int m[59][59]; int x[59],y[59],z[59]; int myabs(int x){return x<0?-x:x;} int main() { scanf("%d%d%d",&w,&d,&n); for(int i=1;i<=n;i++)scanf("%d%d%d",&x[i],&y[i],&z[i]); int flag=1; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ if(myabs(x[i]-x[j])+myabs(y[i]-y[j])<myabs(z[i]-z[j]))flag=0; } if(flag==0){ printf("No\n");return 0; } for(int i=1;i<=w;i++)for(int j=1;j<=d;j++)m[i][j]=-100000; for(int i=1;i<=n;i++){ for(int j=1;j<=w;j++){ for(int k=1;k<=d;k++){ m[j][k]=max(m[j][k],z[i]-myabs(x[i]-j)-myabs(y[i]-k)); } } } int ans=0; for(int i=1;i<=w;i++)for(int j=1;j<=d;j++)ans+=m[i][j]; printf("%d\n",ans); return 0; } /* */
0
#include <bits/stdc++.h> using namespace std; inline int in() { int x; scanf("%d", &x); return x; } const int MX = 100000; const int INF = 1001001001; int main() { int n = in(); long long r; scanf("%I64d", &r); vector<long long> as; long long ans = 0, w = 0, x = 0; vector<int> ls(n), ts(n); for (int i = 0; i < n; ++i) ls[i] = in(); for (int i = 0; i < n; ++i) ts[i] = in(); for (int i = 0; i < n; ++i) { int l = ls[i]; int t = ts[i]; if (l > t) { puts("-1"); return 0; }; if (l <= w) { w -= l; x += l; } else { x += w; l -= w; t -= w; w = 0; t = l * 2 - t; if (t > 0) { x += (l - t) * 2; ans += t / r; for (int i = 0; i < t / r; ++i) { if (int(as.size()) > MX) break; as.push_back(x + r * i); } x += t / r * r; if (t % r) { as.push_back(x); w = r - t % r; ans++; x += t % r; } } else { x += l * 2; } } } printf("%I64d\n", ans); if (ans <= MX) { for (int i = 0; i < ans; ++i) printf("%I64d%c", as[i], i == ans - 1 ? '\n' : ' '); } return 0; }
4
#include <bits/stdc++.h> int idx, one, zero, a[30], b[30], c[60], q[30]; inline void plus(int src1, int src2, int dst) { printf("+ %d %d %d\n", src1, src2, dst); } inline void less(int src1, int src2, int dst) { printf("< %d %d %d\n", src1, src2, dst); } void prepare() { idx = 5; for (int i = 0; i < 30; i++) { a[i] = ++idx; } for (int i = 0; i < 30; i++) { b[i] = ++idx; } for (int i = 0; i < 59; i++) { c[i] = ++idx; } for (int i = 0; i < 30; i++) { q[i] = ++idx; } zero = q[0]; one = q[1]; plus(0, 1, 3); less(0, 3, 4); less(1, 3, 5); plus(4, 5, one); less(zero, one, one); for (int i = 2; i < 30; i++) { plus(q[i - 1], q[i - 1], q[i]); plus(q[i], one, q[i]); } } inline void getBit(int *bit, int src) { int num = ++idx, pow = ++idx; for (int i = 29; i >= 0; i--) { plus(num, q[i], bit[i]); less(bit[i], src, bit[i]); plus(bit[i], zero, pow); for (int j = 0; j < i; j++) { plus(pow, pow, pow); } plus(num, pow, num); } } inline void calc() { int num = ++idx; for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++) { plus(a[i], b[j], num); less(one, num, num); plus(num, c[i + j], c[i + j]); } } for (int i = 0; i < 59; i++) { for (int j = 0; j < i; j++) { plus(c[i], c[i], c[i]); } plus(2, c[i], 2); } } int main() { printf("5641\n"); prepare(); getBit(a, 0); getBit(b, 1); calc(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int xx[4] = {0, 0, 1, -1}; int yy[4] = {1, -1, 0, 0}; int n, m; int t[int(1e5 + 100)][10], f[int(1e5 + 100)][10]; int main() { scanf("%d%d", &m, &n); for (int i = (1), _b = (m); i <= _b; i++) { int r = 0; for (int j = (1), _b = (n); j <= _b; j++) { scanf("%d", &t[i][j]); r = max(r, f[i - 1][j]) + t[i][j]; f[i][j] = r; } printf("%d ", r); } }
2
#include <bits/stdc++.h> using namespace std; bool f(int a, int b) { if (b > a + 1) return false; while (a > 2) { a -= 2; b--; } if (b >= 0) return true; else return false; } int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (f(c, b) or f(d, a)) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> int main() { int n, a, b; scanf("%d", &n); scanf("%d", &a); scanf("%d", &b); b = b % n; if ((n + a + b) % n != 0) printf("%d\n", (n + b + a) % n); else printf("%d\n", n); }
1
#include <cstdio> #include <algorithm> #include<cmath> typedef long long ll; using namespace std; int main(){ int t,b,c; ll s,i; scanf("%d",&t); while(t--){ scanf("%d%d",&b,&c); s=1ll*b*c; i=sqrt(s)+1; while(i*i>=s) i--; ll ans=i*2-2; if(i*(i+1)<s) ans++; if(b==c) ans++; printf("%lld\n",ans); } return 0; }
0
#include <iostream> #include <algorithm> #include <queue> #include <string> #include <map> using namespace std; const int MAX_H = 30; const int MAX_W = 30; const int dx[] = {1,0,-1,0}; const int dy[] = {0,1,0,-1}; int W, H; int G[MAX_H][MAX_W]; int order[6]; map<char, int> id; struct S { int x, y, step, face[3], cost; }; bool vis[MAX_H][MAX_W][7][7][7][7]; int bfs(int sx, int sy) { S s; queue<S> que; fill(vis[0][0][0][0][0], vis[MAX_H][0][0][0][0], false); s.x = sx; s.y = sy; s.step = 0; s.face[0] = 1; s.face[1] = 2; s.face[2] = 3; s.cost = 0; que.push(s); while(!que.empty()) { s = que.front(); que.pop(); if(G[s.y][s.x] != 0) { if(G[s.y][s.x] == order[s.step]) { ++s.step; if(s.step == 6) return s.cost; } else { continue; } } for(int i = 0; i < 4; ++i) { S t = s; t.x += dx[i]; t.y += dy[i]; ++t.cost; if(t.x < 0 || t.x >= W) continue; if(t.y < 0 || t.y >= H) continue; if(G[t.y][t.x] == 7) continue; switch(i) { case 0: t.face[0] = 7 - s.face[2]; t.face[2] = s.face[0]; break; case 1: t.face[0] = s.face[1]; t.face[1] = 7 - s.face[0]; break; case 2: t.face[0] = s.face[2]; t.face[2] = 7 - s.face[0]; break; case 3: t.face[0] = 7 - s.face[1]; t.face[1] = s.face[0]; break; } if(G[t.y][t.x] != 0 && G[t.y][t.x] != t.face[0]) continue; if(vis[t.y][t.x][t.step][t.face[0]][t.face[1]][t.face[2]]) continue; vis[t.y][t.x][t.step][t.face[0]][t.face[1]][t.face[2]] = true; que.push(t); } } return -1; } int main() { id['w'] = 0; id['r'] = 1; id['g'] = 2; id['b'] = 3; id['y'] = 4; id['m'] = 5; id['c'] = 6; id['k'] = 7; while((cin >> W >> H) && (W || H)) { int sx, sy; char c; for(int i = 0; i < H; ++i) { for(int j = 0; j < W; ++j) { cin >> c; if(c == '#') { sx = j, sy = i; c = 'w'; } G[i][j] = id[c]; } } for(int i = 0; i < 6; ++i) { cin >> c; order[i] = id[c]; } int ans = bfs(sx, sy); if(ans == -1) cout << "unreachable" << endl; else cout << ans << endl; } return 0; }
0
#include <iostream> using namespace std; int main(){ int a,b,x; cin>>a>>b>>x; if(x>=a&&x<=a+b){ printf("YES"); } else{ printf("NO"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> using VV = vector<vector<T>>; template <class T> inline T SMIN(T& a, const T b) { return a = (a > b) ? b : a; } template <class T> inline T SMAX(T& a, const T b) { return a = (a < b) ? b : a; } template <class Iter> void dumpc(Iter begin, Iter end) { if (false) { for (; begin != end; ++begin) { cerr << *begin << ','; } cerr << endl; } } const int MAX_N = 3010 + 1; string W1[MAX_N], W2[MAX_N]; int N; template <class T> struct RollingHash { public: int M; string P; T d; T MOD; T h; T p; RollingHash(string P, T d = 131, T MOD = 1e9 + 7) : M(P.size()), P(P), d(d), MOD(MOD) { this->h = powmod(d, M - 1, MOD); this->p = calcRollingHash(P); } T calcRollingHash(string X) { T res = 0; for (int i = 0; i < min((int)X.size(), M); ++i) { res = (d * res) % MOD + X[i], res %= MOD; } return res; } T updateRollingHash(T base, string S, int i) { T res = d * (base + MOD - ((T)S[i] * h) % MOD) % MOD + S[i + M]; res %= MOD; return res; } int doRabbinKarpMatch(string S) { int N = S.size(); T s = calcRollingHash(S); for (int i = 0; i <= N - M; ++i) { if (false) { cerr << "i" << " = " << (i) << ", " << "s" << " = " << (s) << ", " << "p" << " = " << (p) << endl; }; if (s == p) return i; s = updateRollingHash(s, S, i); } return -1; } private: T powmod(T a, T b, T MOD) { T res = 1; for (T mask = 1; mask <= b; mask <<= 1) { if (b & mask) res *= a, res %= MOD; a *= a; a %= MOD; } return res; } }; struct RollingHash2 { static const long long mo0 = 1000000021, mo1 = 1000000009; static long long mul0, mul1; static const long long add0 = 1000010007, add1 = 1003333331; static vector<long long> pmo[2]; string s; int l; vector<long long> hash_[2]; void init(string s) { this->s = s; l = s.size(); int i, j; hash_[0] = hash_[1] = vector<long long>(1, 0); if (!mul0) mul0 = 10009 + (((long long)&mul0) >> 5) % 259, mul1 = 10007 + (((long long)&mul1) >> 5) % 257; if (pmo[0].empty()) pmo[0].push_back(1), pmo[1].push_back(1); for (int i = 0; i < (l); ++i) hash_[0].push_back((hash_[0].back() * mul0 + add0 + s[i]) % mo0); for (int i = 0; i < (l); ++i) hash_[1].push_back((hash_[1].back() * mul1 + add1 + s[i]) % mo1); } pair<long long, long long> hash(int l, int r) { if (l > r) return make_pair(0, 0); while (pmo[0].size() < r + 2) pmo[0].push_back(pmo[0].back() * mul0 % mo0), pmo[1].push_back(pmo[1].back() * mul1 % mo1); return make_pair( (hash_[0][r + 1] + (mo0 - hash_[0][l] * pmo[0][r + 1 - l] % mo0)) % mo0, (hash_[1][r + 1] + (mo1 - hash_[1][l] * pmo[1][r + 1 - l] % mo1)) % mo1); } pair<long long, long long> hash(string s) { init(s); return hash(0, s.size() - 1); } static pair<long long, long long> concat(pair<long long, long long> L, pair<long long, long long> R, int RL) { while (pmo[0].size() < RL + 2) pmo[0].push_back(pmo[0].back() * mul0 % mo0), pmo[1].push_back(pmo[1].back() * mul1 % mo1); return make_pair((R.first + L.first * pmo[0][RL]) % mo0, (R.second + L.second * pmo[1][RL]) % mo1); } }; vector<long long> RollingHash2::pmo[2]; long long RollingHash2::mul0, RollingHash2::mul1; void end() { fprintf(stdout, "NO"), putchar('\n'); ; exit(0); } pair<int, int> boundary2(string s, string t, int l, int r) { int resl = 0, resr = 0, M = int((s).size()); int j = l - 1; for (; j >= 0; --j) { if (s[j] != t[j]) break; } resl = l - j - 1; assert(resl >= 0); j = r + 1; for (; j < M; ++j) if (s[j] != t[j]) break; resr = j - r - 1; assert(resr >= 0); return {resl, resr}; } int si = -1, sp = -1; pair<int, int> boundary1(string s, string t, int l, int r, string x) { int resl = 0, resr = 0, M = int((s).size()); int j = l - 1; for (; j >= 0; --j) { int d = l - j; if (s[j] != t[j] || sp - d < 0 || s[j] != W1[si][sp - d]) break; } resl = l - j - 1; assert(resl >= 0); j = r + 1; for (; j < M; ++j) { int d = j - r; int jj = sp + int((x).size()) + d - 1; if (s[j] != t[j] || jj >= int((W1[si]).size()) || s[j] != W1[si][jj]) break; } resr = j - r - 1; assert(resr >= 0); return {resl, resr}; } void solve_org() { string s = "", t = ""; int lb, rb; for (int i = 0; i < (N); ++i) { int M = int((W1[i]).size()); int l = 3010, r = -1; for (int j = 0; j < (M); ++j) if (W1[i][j] != W2[i][j]) SMIN(l, j), SMAX(r, j); if (r != -1) { if (s.empty()) { s = W1[i].substr(l, r - l + 1); t = W2[i].substr(l, r - l + 1); lb = l, rb = int((W1[i]).size()) - r - 1; sp = l, si = i; } else { if (r - l + 1 != int((s).size())) end(); int L = int((s).size()); for (int j = 0; j < (L); ++j) { if (W1[i][l + j] != s[j]) end(); if (W2[i][l + j] != t[j]) end(); } auto p = boundary1(W1[i], W2[i], l, r, s); SMIN(lb, p.first); SMIN(rb, p.second); } } else { RollingHash<long long> rh(W1[i]); int j = rh.doRabbinKarpMatch(s); if (j == -1) continue; auto p = boundary2(W1[i], W2[i], j, j + int((s).size()) - 1); if (p.first >= lb && p.second >= rb) end(); } } if (s.empty()) { s += W1[0][0], t += W1[0][0]; } else { if (false) { cerr << "s" << " = " << (s) << ", " << "t" << " = " << (t) << ", " << "lb" << " = " << (lb) << ", " << "rb" << " = " << (rb) << endl; }; if (false) { cerr << "sp" << " = " << (sp) << ", " << "si" << " = " << (si) << ", " << "W1[si]" << " = " << (W1[si]) << ", " << "W2[si]" << " = " << (W2[si]) << endl; }; s = W1[si].substr(sp - lb, lb + rb + int((s).size())); t = W2[si].substr(sp - lb, lb + rb + int((t).size())); } fprintf(stdout, "YES"), putchar('\n'); ; fprintf(stdout, "%s", s.c_str()), putchar('\n'); ; fprintf(stdout, "%s", t.c_str()), putchar('\n'); ; } pair<int, int> boundary(string s, string t, int l, int r) { int resl = 0, resr = 0, M = int((s).size()); string u = W1[si]; int j = l - 1; for (; j >= 0; --j) { int d = l - j; if (sp - d < 0 || s[j] != u[sp - d]) break; } resl = l - j - 1; assert(resl >= 0); j = r + 1; for (; j < M; ++j) { int d = j - r; int jj = sp + r - l + d; if (jj >= int((u).size()) || s[j] != u[jj]) break; } resr = j - r - 1; assert(resr >= 0); return {resl, resr}; } RollingHash2 rh[3030]; void solve_clean() { string s = "", t = "", pre = "", post = ""; int lb, rb; for (int i = 0; i < (N); ++i) { rh[i].init(W1[i]); int M = int((W1[i]).size()); if (false) { cerr << "i" << " = " << (i) << ", " << "M" << " = " << (M) << endl; }; int l = 3010, r = -1; for (int j = 0; j < (M); ++j) if (W1[i][j] != W2[i][j]) SMIN(l, j), SMAX(r, j); if (r != -1) { string ss = W1[i].substr(l, r - l + 1); string tt = W2[i].substr(l, r - l + 1); string prepre = W1[i].substr(0, l); string postpost = W2[i].substr(r + 1); reverse((prepre).begin(), (prepre).end()); if (s.empty()) { s = ss, t = tt, pre = prepre, post = postpost; lb = l, rb = int((W1[i]).size()) - r - 1; sp = l, si = i; } else { if (ss != s || tt != t) { if (false) { cerr << "ss" << " = " << (ss) << ", " << "s" << " = " << (s) << ", " << "tt" << " = " << (tt) << ", " << "t" << " = " << (t) << endl; }; end(); } if (r - l + 1 != int((s).size())) end(); int L = int((s).size()); for (int j = 0; j < (L); ++j) { if (W1[i][l + j] != s[j]) end(); if (W2[i][l + j] != t[j]) end(); } auto p = boundary(W1[i], W2[i], l, r); SMIN(lb, p.first); SMIN(rb, p.second); } } } if (false) { cerr << "s" << " = " << (s) << ", " << "t" << " = " << (t) << ", " << "pre" << " = " << (pre) << ", " << "post" << " = " << (post) << endl; }; if (false) { cerr << "sp" << " = " << (sp) << ", " << "si" << " = " << (si) << ", " << "W1[si]" << " = " << (W1[si]) << ", " << "W2[si]" << " = " << (W2[si]) << endl; }; s = W1[si].substr(sp - lb, lb + rb + int((s).size())); t = W2[si].substr(sp - lb, lb + rb + int((t).size())); reverse((pre).begin(), (pre).end()); auto ha = rh[N].hash(s); for (int i = 0; i < (N); ++i) { string x = W1[i], y = W2[i]; int jj = -1; for (int j = 0; j + s.size() <= x.size(); j++) { if (rh[i].hash(j, j + s.size() - 1) == ha) { x = x.substr(0, j) + t + x.substr(s.size() + j); jj = j; break; } } if (x != y) end(); } fprintf(stdout, "YES"), putchar('\n'); ; fprintf(stdout, "%s", s.c_str()), putchar('\n'); ; fprintf(stdout, "%s", t.c_str()), putchar('\n'); ; } void solve() { string s = "", t = "", pre = "", post = ""; for (int i = 0; i < (N); ++i) { int M = int((W1[i]).size()); int l = 3010, r = -1; for (int j = 0; j < (M); ++j) if (W1[i][j] != W2[i][j]) SMIN(l, j), SMAX(r, j); if (r != -1) { string ss = W1[i].substr(l, r - l + 1); string tt = W2[i].substr(l, r - l + 1); string prepre = W1[i].substr(0, l); string postpost = W2[i].substr(r + 1); reverse((prepre).begin(), (prepre).end()); if (s.empty()) { s = ss, t = tt, pre = prepre, post = postpost; } else { if (ss != s || tt != t) { if (false) { cerr << "ss" << " = " << (ss) << ", " << "s" << " = " << (s) << ", " << "tt" << " = " << (tt) << ", " << "t" << " = " << (t) << endl; }; end(); } for (int j = 0; j < (int((pre).size())); ++j) if (j >= int((prepre).size()) || prepre[j] != pre[j]) { pre.resize(j); break; } for (int j = 0; j < (int((post).size())); ++j) if (j >= int((postpost).size()) || postpost[j] != post[j]) { post.resize(j); break; } } } } if (false) { cerr << "s" << " = " << (s) << ", " << "t" << " = " << (t) << ", " << "pre" << " = " << (pre) << ", " << "post" << " = " << (post) << endl; }; reverse((pre).begin(), (pre).end()); s = pre + s + post; t = pre + t + post; auto ha = rh[N].hash(s); for (int i = 0; i < (N); ++i) { rh[i].init(W1[i]); string x = W1[i], y = W2[i]; for (int j = 0; j + s.size() <= x.size(); j++) { if (rh[i].hash(j, j + s.size() - 1) == ha) { x = x.substr(0, j) + t + x.substr(s.size() + j); break; } } if (x != y) end(); } fprintf(stdout, "YES"), putchar('\n'); ; fprintf(stdout, "%s", s.c_str()), putchar('\n'); ; fprintf(stdout, "%s", t.c_str()), putchar('\n'); ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); scanf("%d", &N); if (false) { cerr << "N" << " = " << (N) << endl; }; for (int i = 0; i < (N); ++i) { char c[3010]; scanf("%s", c); W1[i] = string(c); } for (int i = 0; i < (N); ++i) { char c[3010]; scanf("%s", c); W2[i] = string(c); } solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; struct p { int x, id; bool operator<(const p &a) const { return x < a.x; } } a[MAX]; int n, ans, K; int fa[MAX], siz[MAX], tot[MAX], b[MAX]; set<int> S; int find(int x) { if (fa[x] != x) fa[x] = find(fa[x]); return fa[x]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i].x), b[i] = a[i].x, a[i].id = i, fa[i] = i, siz[i] = 1; sort(a + 1, a + 1 + n); b[0] = b[n + 1] = 2e9; for (int i = 1; i <= n; ++i) { int id = a[i].id, k = a[i].x + 1; if (k == 61) { cout << ""; } S.insert(1), ++tot[1]; if (b[id - 1] < k) { int r1 = find(id - 1); --tot[1], --tot[siz[r1]]; if (!tot[1]) S.erase(1); if (!tot[siz[r1]]) S.erase(siz[r1]); fa[id] = r1, ++tot[++siz[r1]]; if (tot[siz[r1]] == 1) S.insert(siz[r1]); } if (b[id + 1] < k) { int r1 = find(id + 1), r2 = find(id); --tot[siz[r1]], --tot[siz[r2]]; if (!tot[siz[r1]]) S.erase(siz[r1]); if (!tot[siz[r2]]) S.erase(siz[r2]); fa[r1] = r2, ++tot[siz[r2] += siz[r1]]; if (tot[siz[r2]] == 1) S.insert(siz[r2]); } if (S.size() == 1 && tot[*S.begin()] > ans) ans = tot[*S.begin()], K = k; } return printf("%d", K), 0; }
4
#include <bits/stdc++.h> using namespace std; int main(){ int t,n; scanf("%d",&t); while(t--){ scanf("%d",&n); double ans=0; vector<double>miners; vector<double>diamonds; for(int i=0;i<2*n;i++){ double a,b; scanf("%lf%lf",&a,&b); if (a==0){miners.push_back(abs(b));} if (b==0){diamonds.push_back(abs(a));} } sort(miners.begin(),miners.end()); sort(diamonds.begin(),diamonds.end()); for (int i=0;i<n;i++){ ans+=sqrt(miners[i]*miners[i]+diamonds[i]*diamonds[i]); } printf("%.15lf\n",ans); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, x; scanf("%d%d%d", &n, &m, &k); int ans = m * k; m = n - m; for (int i = 0; i < n; i++) { scanf("%d", &x); if (m) { ans += x; m--; } } printf("%d\n", ans); }
1
// CGL_1_A #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; int main(){ double xx, yy, X, Y, x, y; int q, xp[2], yp[2]; cin>>xp[0]>>yp[0]>>xp[1]>>yp[1]; X = xp[1] - xp[0]; Y = yp[1] - yp[0]; cin>>q; for(int i=0;i<q;i++){ cin>>xx>>yy; if(Y == 0){ x = xx; y = yp[0]; }else if(X == 0){ x = xp[0]; y = yy; }else{ x = (xp[0]*Y*Y+xx*X*X+yy*X*Y-yp[0]*X*Y)/(X*X+Y*Y); y = -X/Y*(x-xx)+yy; } printf("%.10lf %.10lf\n", x, y); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int kMod = 998244353; const int kNmax = 2e5 + 5; int n, m; vector<char> a, b; int pw2[kNmax]; vector<char> read_bits() { string s; cin >> s; return vector<char>(s.begin(), s.end()); } int main() { cin >> n >> m; a = read_bits(); b = read_bits(); pw2[0] = 1; for (int i = 1; i <= n; i++) { pw2[i] = (2LL * pw2[i - 1]) % kMod; } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int sum = 0; for (int i = 0; i < a.size(); i++) if (a[i] == '1') sum = (sum + pw2[i]) % kMod; while (b.size() < a.size()) b.push_back('0'); while (a.size() < b.size()) a.push_back('0'); int ans = 0; while (b.size() > 0) { if (b.back() == '1') { ans = (ans + sum) % kMod; } if (a.back() == '1') { sum = (sum + kMod - pw2[int(a.size()) - 1]) % kMod; } a.pop_back(); b.pop_back(); } cout << ans << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); int t; t = 1; while (t--) { long long a, b, ans = 0; cin >> a >> b; for (long long i = 1; i < (a + 1); ++i) { long long ch1 = ((b * (b - 1)) / 2) % 1000000007; long long ch = (ch1 * ((i * b + 1) % 1000000007)) % 1000000007; ans = (ans % 1000000007 + ch % 1000000007) % 1000000007; } cout << ans << endl; } }
1
#include <bits/stdc++.h> using namespace std; void bug() { cout << " " << endl; } template <class T> void bug(T a) { cout << a << " "; } template <class T> void bug(T* a, int n) { for (int i = 0; i < (n); i++) bug(a[i]); } const double pi = acos(-1.0); int flip(int n, int i) { int n2 = n, ans = 0; for (; n2 > 1; n2 >>= 1, i >>= 1) ans = (ans << 1) | (i & 1); return ans; } vector<complex<double> > FFT(vector<complex<double> > buf, int t) { int n = buf.size(); vector<complex<double> > v(n); for (int i = 0; i < n; i++) v[i] = buf[flip(n, i)]; for (int m = 2; m <= n; m *= 2) { complex<double> w = polar(1.0, 2 * pi * t / m); for (int i = 0; i < n; i += m) { complex<double> now(1.0, 0); for (int j = 0; 2 * j < m; j++) { complex<double> x = v[i + j]; complex<double> y = now * v[i + j + m / 2]; v[i + j] = x + y; v[i + j + m / 2] = x - y; now *= w; } } } return v; } int n, m; int ok[1111111]; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < (n); i++) { int x; cin >> x; ok[x] = 1; } vector<complex<double> > coef, prod; int l = m * 2 + 10; while (((l) & (-(l))) != l) l++; for (int i = 0; i < (m + 1); i++) coef.push_back(complex<double>(ok[i], 0)); for (int i = m + 1; i < l; i++) coef.push_back(complex<double>(0, 0)); prod = FFT(coef, 1); for (int i = 0; i < (l); i++) prod[i] *= prod[i]; prod = FFT(prod, -1); for (int i = 0; i < (m + 1); i++) if (prod[i].real() / l > 1e-3 && ok[i] == 0) { cout << "NO" << endl; exit(0); } vector<int> ans; for (int i = 0; i < (m + 1); i++) if (prod[i].real() / l < 1e-3 && ok[i] == 1) ans.push_back(i); cout << "YES" << endl; cout << ans.size() << endl; for (int i = 0; i < (ans.size()); i++) cout << ans[i] << " "; return 0; }
5
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using ld = long double; using ii = pair<int, int>; const char space = ' ', ln = '\n'; template <typename T> T read() { T value; cin >> value; return value; } int ri() { return read<int>(); } ll rll() { return read<ll>(); } string rs() { return read<string>(); } template <typename T> vector<T> rv(size_t size) { vector<T> a(size); for (size_t i = 0; i < size; ++i) { a[i] = read<T>(); } return a; } template <typename T> void remin(T& target, T source) { target = min(target, source); } template <typename T> void remax(T& target, T source) { target = max(target, source); } vector<string> split(string& s, string seps) { vector<string> parts; string part = ""; auto add_part = [&]() { parts.push_back(part); part = ""; }; for (char ch : s) { if (string::npos != seps.find(ch)) { add_part(); } else { part += ch; } } add_part(); return parts; } ld sqrt_ld(ld value) { return sqrt(value); } void init_io() { string file_name = ""; if (!file_name.empty()) { string input_file_name = file_name + ".in"; freopen(input_file_name.c_str(), "rt", stdin); string output_file_name = file_name + ".out"; freopen(output_file_name.c_str(), "wt", stdout); } else { } ios::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(10) << fixed; } void solve() { int n; cin >> n; int a[n], sum[(int)2e5 + 3200]; for (int i = 0; i < n; ++i) { cin >> a[i]; } int max = 0; for (int i = 0; i < n - 1; ++i) { for (int j = i + 1; j < n; ++j) { sum[a[i] + a[j]]++; if (sum[a[i] + a[j]] > max) { max = sum[a[i] + a[j]]; } } } cout << max; } int main() { solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; bool f[10000007]; vector<int> prime; void sieve() { f[0] = f[1] = 1; for (int i = 2; i < 10000007; ++i) { if (f[i]) continue; prime.push_back(i); for (int j = i * i; i <= 10000 && j < 10000007; j += i) f[j] = 1; } } vector<int> factor; void getFactor(int n) { for (int i = 2; i <= n / i; ++i) { while (n % i == 0) { factor.push_back(i); n /= i; } } if (n != 1) factor.push_back(n); } long long MaxSubarray(long long arr[], int n) { long long best = -1e18, sum = 0; for (int i = 0; i < n; ++i) { sum = max(arr[i], arr[i] + sum); best = max(best, sum); } return best; } long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; } long long LCM(long long a, long long b) { return a / GCD(a, b) * b; } long long fast_power(long long b, long long p) { long long res = 1; while (p) { if (p % 2) res = (res * b) % 1000000007; b = (b * b) % 1000000007; p /= 2; } return res; } int factorialTrailingZeros(int n) { int cnt = 0; for (int i = 5; n >= i; i *= 5) cnt += n / i; return cnt; } long long fac(int st, int en) { if (st > en) return -1; long long ret = 1; if (st == 0) ++st; for (int i = st; i <= en; ++i) ret *= i; return ret; } long long P(int n, int k) { if (n < k) return -1; if (n == 0) return 0; return fac(n - k + 1, n); } long long C(int n, int k) { if (n < k) return -1; int k1 = n - k; return fac(max(k1 + 1, k + 1), n) / fac(0, min(k1, k)); } long long C_over(int n, int k) { if (n < k) return -1; long long ret = 1; int k1 = n - k; vector<int> a, b; for (int i = max(k1 + 1, k + 1); i <= n; ++i) a.push_back(i); for (int i = 2; i <= min(k1, k); ++i) b.push_back(i); for (int i = 0; i < a.size(); ++i) { for (int j = 0; j < b.size(); ++j) { int x = GCD(a[i], b[j]); a[i] /= x, b[j] /= x; } } for (int i = 0; i < a.size(); ++i) ret *= a[i]; return ret; } template <typename T> void printPQ(T& q) { while (!q.empty()) { std::cout << q.top() << " "; q.pop(); } std::cout << '\n'; } int n, arr[5005]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", arr + i); for (int i = 1; i <= n; ++i) { if (arr[arr[arr[i]]] == i) exit(puts("YES")); } puts("NO"); return 0; }
1
#include <iostream> int A, B; int main() { std::cin >> A >> B; std::cout << A * B - A - B + 1; return 0; }
0
#include <bits/stdc++.h> using namespace std; bool fl; int a, b; int ask(int x, int y) { printf("? %d %d\n", x, y); fflush(stdout); int res = 0; scanf("%d", &res); return res; } int main() { a = b = 0; fl = 1; if (ask(a, b) < 0) fl = 0; for (int i = 29; i >= 0; i--) { int qa = a ^ (1 << i), qb = b ^ (1 << i); int x = ask(qa, b), y = ask(a, qb); if (x == y) { if (fl) a ^= (1 << i); else b ^= (1 << i); fl = (x == 1); } else if (x == -1 && y == 1) a ^= (1 << i), b ^= (1 << i); } printf("! %d %d\n", a, b); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 10; const int M = 1000500; vector<int> rec[N], prime; int ans, n, who, pre[N]; long long a[N], rest[N]; bool used[N], flag[M], sth, have[N]; map<long long, int> mymap; void init() { for (int i = 2; i < M; i++) { if (flag[i]) continue; else prime.push_back(i); for (int j = 2; j * i < M; j++) flag[i * j] = true; } } int add(long long x) { if (x == 1) return 0; if (mymap.find(x) != mymap.end()) return mymap[x]; else { int cnt = 0; long long pre = x; for (int i = 0; i < (int)prime.size() && prime[i] * prime[i] <= x; i++) { long long y = prime[i]; while (x % y == 0) cnt++, x /= y; } if (x == pre) return 1; if (x != 1) cnt++; mymap[pre] = cnt; return cnt; } } int cal() { int ret = 0; for (int i = 1; i <= n; i++) { int how = add(rest[i]); if (how == 1 && rest[i] == a[i]) ; else ret += how; } return ret; } void dfs(int cur, int got) { if ((!sth && (got == n + 1)) || (sth && got == n)) { int need = cal(); if (!sth) ans = min(ans, need + n + 1); else ans = min(ans, need + n); return; } if (!sth) { pre[cur] = 0; dfs(cur + 1, got + 1); pre[cur] = -1; } for (int i = 1; i < cur; i++) { if (rest[i] % a[cur] == 0) { rest[i] /= a[cur]; pre[cur] = i; dfs(cur + 1, got + 1); rest[i] *= a[cur]; pre[cur] = -1; } } } bool cmp(const long long &a, const long long &b) { return a > b; } int main() { init(); scanf("%d", &n); sth = false; for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); sort(a + 1, a + 1 + n, cmp); for (int i = 1; i <= n; i++) { rest[i] = a[i]; } for (int i = 1; i <= n; i++) { long long tmp = a[i]; for (long long j = 1; j * j <= tmp; j++) if (tmp % j == 0) { rec[i].push_back(j); if (j != tmp / j) rec[i].push_back(tmp / j); } sort(rec[i].begin(), rec[i].end()); } ans = (int)1e9; dfs(0, 0); sth = used[1] = true; dfs(2, 1); printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long INF = 1E18; const long double EPS = 1E-10; const int N = 1e5 + 1; const long long MOD = 1E9 + 7; int n; vector<char> v; int main() { cin >> n; char a; for (int i = 1; i <= n; i++) { cin >> a; if (a == '4') { v.push_back('3'); v.push_back('2'); v.push_back('2'); } else if (a == '6') { v.push_back('5'); v.push_back('3'); } else if (a == '8') { v.push_back('2'); v.push_back('2'); v.push_back('2'); v.push_back('7'); } else if (a == '9') { v.push_back('3'); v.push_back('3'); v.push_back('2'); v.push_back('7'); } else if (a != '0' && a != '1') v.push_back(a); } sort(v.begin(), v.end()); for (int i = v.size() - 1; i >= 0; i--) cout << v[i]; return 0; }
3
#include<stdio.h> int main(){ int a,b=0,c=0,n,j,i; scanf("%d",&n); for(j=1;j<=n;j+=2) {for(i=1;i<=j;i++) { if(j%i==0) {b++;} } if(b==8) {c++;} b=0; } printf("%d\n",c); return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mod=1e9+7; int main(){ string s;cin >> s; int n=s.size(); int d;cin >> d; ll dp[n+1][2][d]; memset(dp,0,sizeof(dp)); dp[0][0][0]=1; for(int i=0;i<n;i++){ int p=s[i]-'0'; for(int j=0;j<2;j++){ for(int k=0;k<d;k++){ for(int l=0;l<=(j?9:p);l++){ dp[i+1][j||(l<p)][(k+l)%d]+=dp[i][j][k]; dp[i+1][j||(l<p)][(k+l)%d]%=mod; } } } } cout << (dp[n][0][0]+dp[n][1][0]-1+mod)%mod << endl; }
0
#include <iostream> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <map> #include <set> #include <iomanip> #include <queue> using namespace std; #define ll long long #define ull unsigned long long #define ft first #define sc second #define add push_back #define all(v) v.begin(),v.end() #define mpr make_pair #define sz(v) v.size() #define vi vector<int> #define pii pair<int,int> #define pll <pair<ll,ll> > #define FOR(i,a,b) for(ll i = (a); i < (b); i++) #define FORD(i,a,b) for(ll i = (a); i >= (b); i--) #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); const ll MOD = 1e9+7; const int INF = 1e9; const int N = 300105; int y[51]; int main() { fastio; int n,q; cin>>n>>q; FOR(i,1,51) { y[i] = INF; } FOR(i,1,n+1) { int a; cin>>a; y[a] = min(y[a],(int)i); } while(q--) { int a; cin>>a; cout<<y[a]<<" "; FOR(i,1,51) { if(y[i] < y[a]) { y[i]++; } } y[a] = 1; } return 0; } /* ░▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒░ ▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒ ▓▓▓▒░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▒░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒░░▒▓▓▓▓▓▓▓ ▓▓▓▓▓▒░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒░░░░▒▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▒░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░▒▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▒░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▒▒░░░░░░▒▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▒░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▒░░░▒▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▒░▒▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▒░░▒▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░░▒▓▓▓▓▓▓▓ ▓▓▓▓▓▓▒░░░░░▒▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓▓▓ ▓▓▓▓▓▒░░░░░░░░▒▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░▒▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░░▒▓▓ ▓▓▓▓▓▒░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░▒▓▓▓ ▓▓▓▓▓▓▒░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░▒▓▓▓▓ ▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒ ░▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒░ ⠀⠀⠀⠀⠀ */
3
#include <bits/stdc++.h> using namespace std; int a[250], b[250]; int main() { int T; scanf("%d", &T); while (T--) { for (int i = 0; i < 250; i++) scanf("%d", &a[i]); double mina = a[0], maxa = a[0], minb = b[0], maxb = b[0]; double mida = 0; for (int i = 0; i < 250; i++) { mida += a[i]; mina = min(mina, (double)a[i]); maxa = max(maxa, (double)a[i]); } mida /= 250.; int sum = 0, sumb = 0; double len = mida / 2; for (int i = 0; i < 250; i++) { if (a[i] > (mida - len) && a[i] < (mida + len)) sum++; if (a[i] <= 1) sumb++; } if (sum < 180 || sumb > 3) { printf("%.0lf\n", (mina + maxa) / 2 + 0.5); } else { printf("%.0lf\n", mida + 0.5); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; double ans, cnt, s; double p[maxn]; int n; void solve() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lf", p + i); p[0] = 0; for (int i = 1; i <= n; i++) { cnt = (cnt + s * 2 + 1 - p[i - 1]) * p[i]; s = (s + 1 - p[i - 1]) * p[i]; ans += cnt; } printf("%.10lf", ans); } int main() { solve(); }
2
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433832795; const long double eps = 1e-9; deque<int> d; int main() { int a, b, x, i, ans = 0, j; for (i = 0; i < 4; i++) { scanf("%d", &j); d.push_back(j); } scanf("%d%d", &a, &b); sort(d.begin(), d.end()); for (i = a; i <= b; i++) { x = ((((i % d[0]) % d[1]) % d[2]) % d[3]); if (x == i) ans++; } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a, b, mini = 1000000000; scanf("%d %d", &n, &m); int i, j, k, l, ara[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf("%d", &ara[i][j]); } } scanf("%d %d", &a, &b); if (a == 1 && b == 1) { for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (ara[i][j] == 0) { mini = 0; break; } } if (mini == 0) break; } if (mini == 0) printf("0\n"); else printf("1\n"); return 0; } int cnt = 0, z = 0; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (i + a - 1 < n && j + b - 1 < m) { for (k = i; k < i + a; k++) { for (l = j; l < j + b; l++) { if (ara[k][l] == 1) cnt++; } } if (cnt < mini) { mini = cnt; } } cnt = 0; if (mini == 0) break; } if (mini == 0) break; } if (mini == 0) { printf("%d\n", mini); return 0; } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (i + b - 1 < n && j + a - 1 < m) { for (k = i; k < i + b; k++) { for (l = j; l < j + a; l++) { if (ara[k][l] == 1) cnt++; } } if (cnt < mini) { mini = cnt; } } cnt = 0; if (mini == 0) break; } if (mini == 0) break; } printf("%d\n", mini); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long n, m, ans, f[N][10], Ans[N]; vector<long long> g[N]; inline long long read() { long long ret = 0, f = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) { ret = ret * 10 + c - 48; c = getchar(); } if (f) return -ret; return ret; } void dfs(long long u, long long fa) { f[u][0]++; for (long long i = 0; i < g[u].size(); i++) { long long v = g[u][i]; if (v == fa) continue; dfs(v, u); Ans[u] = Ans[u] + Ans[v] + f[v][0]; for (long long j = 0; j < m; j++) f[u][(j + 1) % m] += f[v][j]; } } void solve(long long u, long long fa) { ans += Ans[u]; for (long long i = 0; i < g[u].size(); i++) { long long v = g[u][i]; if (v == fa) continue; Ans[u] = Ans[u] - Ans[v] - f[v][0]; for (long long j = 0; j < m; j++) f[u][(j + 1) % m] -= f[v][j]; Ans[v] = Ans[v] + Ans[u] + f[u][0]; for (long long j = 0; j < m; j++) f[v][(j + 1) % m] += f[u][j]; solve(v, u); for (long long j = 0; j < m; j++) f[v][(j + 1) % m] -= f[u][j]; Ans[v] = Ans[v] - Ans[u] - f[u][0]; for (long long j = 0; j < m; j++) f[u][(j + 1) % m] += f[v][j]; Ans[u] = Ans[u] + Ans[v] + f[v][0]; } } signed main() { n = read(); m = read(); for (long long i = 1; i < n; i++) { long long x = read(), y = read(); g[x].push_back(y); g[y].push_back(x); } dfs(1, 0); solve(1, 0); cout << ans / 2; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f, mod = 1000000007; const double pi = 3.1415926535897932, eps = 1e-6; int cnt[262200]; long long sum[262200][5]; void chan(int x) { int i, l = x << 1, r = (x << 1) + 1; cnt[x] = cnt[l] + cnt[r]; for (int(i) = (0); (i) <= (4); (i)++) sum[x][i] = sum[l][i] + sum[r][(i - cnt[l] % 5 + 5) % 5]; } int n, a[100005]; vector<pair<int, int> > v; vector<int> num; int main() { cin >> n; for (int(i) = (1); (i) <= (n); (i)++) { string com; int g; cin >> com; if (com[0] != 's') { cin >> g; } v.push_back(make_pair(com[0] == 's' ? inf : g, com[0])); if (com[0] != 's') num.push_back(g); } sort(num.begin(), num.end()); int s = unique(num.begin(), num.end()) - num.begin(); for (int(i) = (1); (i) <= (n); (i)++) { pair<int, int> now = v[i - 1]; if (now.second == 's') cout << sum[1][2] << endl; else { int typ = now.second == 'a' ? 0 : 1, id = (1 << 17) + (lower_bound(num.begin(), num.begin() + s, now.first) - num.begin()); cnt[id] = 1 - typ; sum[id][0] = (1 - typ) * now.first; for (int j = id >> 1; j; j >>= 1) chan(j); } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int> > v(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { char c; cin >> c; v[i][j] = c - '0'; } } vector<int> f(n, 0); vector<int> c(n, 0); int count = 0; for (int k = 0; k < n; k++) { int cc = 0; for (int j = n - 1; j > k; j--) { if ((v[k][j] + c[j] + cc) % 2 == 1) { c[j] = 1 - c[j]; cc = 1 - cc; count++; v[j][j] = 1 - v[j][j]; } } int ff = 0; for (int i = n - 1; i > k; i--) { if ((v[i][k] + f[i] + ff) % 2 == 1) { f[i] = 1 - f[i]; ff = 1 - ff; count++; v[i][i] = 1 - v[i][i]; } } if ((v[k][k] + ff + cc) % 2 == 1) count++; } cout << count << endl; }
3
#define __USE_MINGW_ANSI_STDIO 0 #include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define PB push_back const ll LLINF = (1LL<<60); const int INF = (1LL<<30); const int MOD = 1000000007; template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; } template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); } template<class S,class T> ostream &operator <<(ostream& out,const pair<S,T>& a){ out<<'('<<a.first<<','<<a.second<<')'; return out; } template<class T> ostream &operator <<(ostream& out,const vector<T>& a){ out<<'['; REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';} out<<']'; return out; } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // 遅延セグメントツリー template <typename T, typename E> struct segtree { using F = function<T(T,T)>; using G = function<T(T,E)>; using H = function<E(E,E)>; using P = function<E(E,int)>; F f; G g; H h; P p; T d1; E d0; int n; vector<int> dat, lazy; segtree(){} segtree(int n_, F f_, G g_, H h_, T d1_, E d0_, P p_=[](E a, int b){return a;}): f(f_), g(g_), h(h_), p(p_), d1(d1_), d0(d0_) { n = 1; while(n < n_) n *= 2; dat.assign(n*2, d1); lazy.assign(n*2, d0); } void build(vector<T> v) { REP(i, v.size()) dat[i+n-1] = v[i]; for(int i=n-2; i>=0; --i) dat[i] = f(dat[i*2+1], dat[i*2+2]); } // 区間の幅がlenの節点kについて遅延評価 inline void eval(int len, int k) { if(lazy[k] == d0) return; if(k*2+1 < n*2-1) { lazy[2*k+1] = h(lazy[k*2+1], lazy[k]); lazy[2*k+2] = h(lazy[k*2+2], lazy[k]); } dat[k] = g(dat[k],p(lazy[k],len)); lazy[k] = d0; } // [a, b) T update(int a, int b, ll x, int k, int l, int r) { eval(r-l, k); if(b <= l || r <= a) return dat[k]; if(a <= l && r <= b) { lazy[k] = h(lazy[k], x); return g(dat[k], p(lazy[k],r-l)); } return dat[k] = f(update(a, b, x, 2*k+1, l, (l+r)/2), update(a, b, x, 2*k+2, (l+r)/2, r)); } void update(int a, int b, ll x) { update(a, b, x, 0, 0, n); } // [a, b) ll query(int a, int b, int k, int l, int r) { eval(r-l, k); if(b <= l || r <= a) return INT_MAX; if(a <= l && r <= b) return dat[k]; T vl = query(a, b, 2*k+1, l, (l+r)/2); T vr = query(a, b, 2*k+2, (l+r)/2, r); return f(vl, vr); } ll query(int a, int b) { return query(a, b, 0, 0, n); } // デバッグ出力 void debug() { cout << "---------------------" << endl; int cnt = 0; for(int i=1; i<=n; i*=2) { REP(j, i) {cout << "(" << dat[cnt] << "," << lazy[cnt] << ") "; cnt++;} cout << endl; } cout << "---------------------" << endl; } }; signed main(void) { cin.tie(0); ios::sync_with_stdio(false); int n, q; cin >> n >> q; auto f = [](int l, int r){return min(l,r);}; auto g = [](int l, int r){return l+r;}; auto h = [](int l, int r){return l+r;}; segtree<int,int> seg(n, f, g, h, 0, 0); REP(i, q) { int type; cin >> type; if(type == 0) { int s, t, x; cin >> s >> t >> x; seg.update(s, t+1, x); } else { int s, t; cin >> s >> t; cout << seg.query(s, t+1) << endl; } // seg.debug(); } return 0; }
0
#include <bits/stdc++.h> const int INF = 1000000007; inline int max(const int &a, const int &b) { return a > b ? a : b; } inline int min(const int &a, const int &b) { return a < b ? a : b; } int t[5015][2], f[5005][102], g[5005][102], sz[5005]; int stk[5005], tp, cur, tot = 10, rt; char str[10010]; bool now; void dfssz(int x) { sz[x] = 1; if (t[x][0] > 10) dfssz(t[x][0]), sz[x] += sz[t[x][0]]; if (t[x][1] > 10) dfssz(t[x][1]), sz[x] += sz[t[x][1]]; } void init() { memset(f, 192, sizeof(f)); memset(g, 127, sizeof(g)); int l = strlen(str), fa; for (int i = 0; i < l; ++i) { if (isdigit(str[i])) cur = str[i] - '0'; if (str[i] == '?') { stk[++tp] = ++tot; t[tot][0] = cur; } if (str[i] == ')') { fa = stk[tp--]; t[fa][1] = cur; cur = rt = fa; } } dfssz(rt); } int calcMax(int x, int p); int calcMin(int x, int p) { if (p < 0 || p > sz[x]) return INF; if (x < 10) return x; if (sz[x] == 1) return now == (bool)p ? t[x][0] + t[x][1] : t[x][0] - t[x][1]; if (g[x][p] < INF) return g[x][p]; int mn = min(sz[t[x][0]], p), ans1, ans2; for (int i = 0; i <= mn; ++i) { ans1 = calcMin(t[x][0], i) + calcMin(t[x][1], p - now - i); ans2 = calcMin(t[x][0], i) - calcMax(t[x][1], p + now - 1 - i); g[x][p] = min(g[x][p], min(ans1, ans2)); } return g[x][p]; } int calcMax(int x, int p) { if (p < 0 || p > sz[x]) return -INF; if (x < 10) return x; if (sz[x] == 1) return now == (bool)p ? t[x][0] + t[x][1] : t[x][0] - t[x][1]; if (f[x][p] > -INF) return f[x][p]; int mn = min(sz[t[x][0]], p), ans1, ans2; for (int i = 0; i <= mn; ++i) { ans1 = calcMax(t[x][0], i) + calcMax(t[x][1], p - now - i); ans2 = calcMax(t[x][0], i) - calcMin(t[x][1], p + now - 1 - i); f[x][p] = max(f[x][p], max(ans1, ans2)); } return f[x][p]; } int main() { scanf("%s", str); if (strlen(str) == 1) { puts(str); return 0; } init(); int a, b; scanf("%d%d", &a, &b); if (a < b) now = 1; else now = 0; printf("%d", calcMax(rt, now ? a : b)); }
5
#include<iostream> #include<algorithm> using namespace std; #define MOD 1000000007 #define MAX_N 210 #define MAX_W 10100 int dp[2][MAX_N][MAX_W]; int N, W, x[MAX_N]; int main() { cin >> N >> W; for (int i = 1; i <= N; i++) { cin >> x[i]; } x[N + 1] = W + 1; sort(x + 1, x + N + 1); dp[1][1][0] = 1; for (int i = 1; i <= N; i++) { for (int j = 0; j <= N; j++) { for (int k = 0; k <= W; k++) { dp[0][j][k] = dp[1][j][k]; dp[1][j][k] = 0; } } int w = x[i]; int y = 0, z = 0; for (int j = 1; j <= i; j++) { y = j; z = j; if (y == i) { y += 1; } for (int k = 0; k <= W; k++) { if (k + w <= W) { dp[1][y][k + w] += dp[0][j][k]; dp[1][y][k + w] %= MOD; } dp[1][z][k] += dp[0][j][k]; dp[1][z][k] %= MOD; } } } int res = 0, v = 0; for (int i = 1; i <= N + 1; i++) { v = max(0, W - x[i] + 1); for (int j = v; j <= W; j++) { res += dp[1][i][j]; res %= MOD; } } cout << res << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int N, dp[1919810][2], i, ans, con; static inline long long dpt(int b){ return ((b - 3 >= 0) ? dp[b - 3][1] + N - b + 1 : N - 1) % 1000000007; } int main(){ scanf("%lld", &N); ans = con = (N - 1) * (N - 1) % 1000000007; dp[0][0] = N; dp[0][1] = N; for(i = 1; i <= N; ++i){ dp[i][0] = (dp[i - 1][0] + con + dpt(i)) % 1000000007; dp[i][1] = (dp[i - 1][1] + dp[i][0]) % 1000000007; } cout << dp[N - 1][0] << endl; return 0; }
0
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <complex> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> using namespace std; typedef pair<int,int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define EQ(a,b) (abs((a)-(b))<EPS) int bo[12560000]; int cal(int s) { return s*(s+1)/2+1; } int main() { int n,m; cin >> n >> m; for(int i=0;i<m;i++) { int a,b,x; cin >> a >> b >> x; bo[cal(a)+b]=x+1; } int ans=0; for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { bo[cal(i)+j]=max(max(bo[cal(i)+j],bo[cal(i-1)+j]-1),bo[cal(i-1)+j-1]-1); if(bo[cal(i)+j])ans++; } } cout << ans << endl; return 0; }
0
#include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int main() { int n, m; cin >> m >> n; vector<vector<double> > win(m, vector<double>(n)); for(int i=0; i<m; ++i){ for(int j=0; j<n; ++j){ cin >> win[i][j]; } } vector<vector<double> > dp(1<<m, vector<double>(m+1, 1.0)); for(int a=n-1; a>=0; --a){ // ツ攻ツ猟ェツつキツづゥツ敵 vector<vector<double> > next(1<<m, vector<double>(m+1, 0.0)); for(int b=1; b<(1<<m); ++b){ // ツ残ツづィツづ個猫 for(int c=0; c<=m; ++c){ // ツ選ツ妥ーツつオツつスツ猫(c==mツづ按づァツづ篠、ツづ慊つセツ選ツ妥ーツつオツづつ「ツづ按つ「) double ret = 0.0; if(c == m){ for(int i=0; i<m; ++i){ bitset<16> bs(b); if(!bs[i]) continue; double p = win[i][a] * dp[b][i]; bs[i] = false; p += (1.0 - win[i][a]) * next[bs.to_ulong()][m]; ret = max(ret, p); } }else{ bitset<16> bs(b); ret = win[c][a] * dp[b][c]; bs[c] = false; ret += (1.0 - win[c][a]) * next[bs.to_ulong()][m]; } next[b][c] = ret; } } dp.swap(next); } printf("%.10f\n", dp[(1<<m)-1][m]); return 0; }
0
#include<iostream> using namespace std; int main(){ int n,a,b; while(cin>>n,n){ int asum=0,bsum=0; for(int i=0;i<n;i++){ cin>>a>>b; if(a>b)asum+=a+b; else if(a<b)bsum+=a+b; else {asum+=a; bsum+=a;} } cout<<asum<<' '<<bsum<<endl; } }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long INF = (long long)1000000007 * 1000000007; const long double eps = 1e-8; const long double pi = acos(-1.0); int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <class Cap, class Cost> struct MinCostFlow { public: MinCostFlow() {} MinCostFlow(int n) : _n(n), g(n) {} int add_edge(int from, int to, Cap cap, Cost cost) { assert(0 <= from && from < _n); assert(0 <= to && to < _n); int m = int(pos.size()); pos.push_back({from, int(g[from].size())}); g[from].push_back(_edge{to, int(g[to].size()), cap, cost}); g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost}); return m; } struct edge { int from, to; Cap cap, flow; Cost cost; }; edge get_edge(int i) { int m = int(pos.size()); assert(0 <= i && i < m); auto _e = g[pos[i].first][pos[i].second]; auto _re = g[_e.to][_e.rev]; return edge{ pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost, }; } std::vector<edge> edges() { int m = int(pos.size()); std::vector<edge> result(m); for (int i = 0; i < m; i++) { result[i] = get_edge(i); } return result; } std::pair<Cap, Cost> flow(int s, int t) { return flow(s, t, std::numeric_limits<Cap>::max()); } std::pair<Cap, Cost> flow(int s, int t, Cap flow_limit) { return slope(s, t, flow_limit).back(); } std::vector<std::pair<Cap, Cost>> slope(int s, int t) { return slope(s, t, std::numeric_limits<Cap>::max()); } std::vector<std::pair<Cap, Cost>> slope(int s, int t, Cap flow_limit) { assert(0 <= s && s < _n); assert(0 <= t && t < _n); assert(s != t); std::vector<Cost> dual(_n, 0), dist(_n); std::vector<int> pv(_n), pe(_n); std::vector<bool> vis(_n); auto dual_ref = [&]() { std::fill(dist.begin(), dist.end(), std::numeric_limits<Cost>::max()); std::fill(pv.begin(), pv.end(), -1); std::fill(pe.begin(), pe.end(), -1); std::fill(vis.begin(), vis.end(), false); struct Q { Cost key; int to; bool operator<(Q r) const { return key > r.key; } }; std::priority_queue<Q> que; dist[s] = 0; que.push(Q{0, s}); while (!que.empty()) { int v = que.top().to; que.pop(); if (vis[v]) continue; vis[v] = true; if (v == t) break; for (int i = 0; i < int(g[v].size()); i++) { auto e = g[v][i]; if (vis[e.to] || !e.cap) continue; Cost cost = e.cost - dual[e.to] + dual[v]; if (dist[e.to] - dist[v] > cost) { dist[e.to] = dist[v] + cost; pv[e.to] = v; pe[e.to] = i; que.push(Q{dist[e.to], e.to}); } } } if (!vis[t]) { return false; } for (int v = 0; v < _n; v++) { if (!vis[v]) continue; dual[v] -= dist[t] - dist[v]; } return true; }; Cap flow = 0; Cost cost = 0, prev_cost = -1; std::vector<std::pair<Cap, Cost>> result; result.push_back({flow, cost}); while (flow < flow_limit) { if (!dual_ref()) break; Cap c = flow_limit - flow; for (int v = t; v != s; v = pv[v]) { c = std::min(c, g[pv[v]][pe[v]].cap); } for (int v = t; v != s; v = pv[v]) { auto &e = g[pv[v]][pe[v]]; e.cap -= c; g[v][e.rev].cap += c; } Cost d = -dual[s]; flow += c; cost += c * d; if (prev_cost == d) { result.pop_back(); } result.push_back({flow, cost}); prev_cost = cost; } return result; } private: int _n; struct _edge { int to, rev; Cap cap; Cost cost; }; std::vector<std::pair<int, int>> pos; std::vector<std::vector<_edge>> g; }; using edge = MinCostFlow<int, int>::edge; const int inf = 10000000; int n, k; vector<int> a(n), b(n); void solve() { MinCostFlow<int, int> mcf(n + k + 5); int ans = inf * n; int S = n + k + 1; for (int i = 0; i < n; i++) { mcf.add_edge(i, n + k, 1, inf - (k - 1) * b[i]); for (int j = 0; j < k; j++) { mcf.add_edge(i, n + j, 1, inf - a[i] - j * b[i]); } } for (int i = 0; i < n; i++) { mcf.add_edge(S, i, 1, 0); } for (int j = 0; j < k; j++) { mcf.add_edge(n + j, S + 1, 1, 0); } mcf.add_edge(n + k, S + 1, n - k, 0); pair<int, int> p = mcf.flow(S, S + 1); auto es = mcf.edges(); vector<int> v(k); vector<bool> used(n); for (auto e : es) { if (e.to - n < 0 || e.to - n >= k) continue; if (e.flow == 1) { v[e.to - n] = e.from; used[e.from] = true; } } printf("%d\n", 2 * n - k); for (int i = 0; i < k - 1; i++) { printf("%d ", v[i] + 1); } for (int i = 0; i < n; i++) { if (!used[i]) { printf("%d %d ", i + 1, -i - 1); } } printf("%d\n", v[k - 1] + 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); int t; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%d%d", &n, &k); a.resize(n); b.resize(n); for (int i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]); solve(); } }
6
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100 + 10; int c2[MAX_N]; int n, k; const int INF = ~0U >> 2; struct Trie { Trie* ch[26]; Trie() { memset(ch, 0, sizeof ch); cnt = 0; } Trie* get(int a) { if (ch[a] == 0) ch[a] = new Trie; return ch[a]; } int am[MAX_N]; int cnt; void solve() { for (int i = 0; i < 26; ++i) { if (ch[i]) ch[i]->solve(); } static int nxt[MAX_N]; for (int i = 0; i <= k; ++i) { if (i <= cnt) am[i] = 0; else am[i] = -INF; } for (int i = 0; i < 26; ++i) { if (ch[i]) { fill(nxt, nxt + k + 1, -INF); int *a = am, *b = ch[i]->am; for (int p = 0; p <= k; ++p) { if (am[p] > -INF) { int eq = k - p; for (int q = 0; q <= eq; ++q) { nxt[p + q] = max(nxt[p + q], a[p] + b[q]); } } } memcpy(am, nxt, sizeof(int) * (k + 1)); } } for (int i = 0; i <= k; ++i) { am[i] += c2[i]; } } }; int main() { cin >> n >> k; for (int i = 0; i < MAX_N; ++i) { c2[i] = i * (i - 1) / 2; } Trie* rt = new Trie; for (int i = 0; i < n; ++i) { string s; cin >> s; Trie* t = rt; for (int j = 0; j < s.size(); ++j) { t = t->get(s[j] - 'a'); } t->cnt++; } rt->solve(); cout << rt->am[k] - c2[k] << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; stack<char> s; int c1 = 0, c2 = 0; cin >> n; for (int i = 0; i < n; i++) { char c; cin >> c; if (c == '(') { c1++; s.push('('); } else { c2++; if (s.empty() == 0) { s.pop(); } } } if ((s.size() == 0 || s.size() == 1) && c1 == c2) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; }
3
#include <iostream> #include <cstdio> #include <cstdlib> #include <queue> #include <stack> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <cmath> #include <complex> #include <map> #include <climits> #include <sstream> using namespace std; #define reep(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) reep((i),0,(n)) #define ALL(v) (v).begin(),(v).end() #define PB push_back #define EPS 1e-8 #define F first #define S second #define mkp make_pair static const double PI=6*asin(0.5); typedef long long ll; typedef complex<double> CP; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vint; static const int INF=1<<24; template <class T> void initvv(vector<vector<T> > &v,int a,int b, const T &t = T()){ v.assign(a,vector<T>(b,t)); } //v.erase(unique(v.begin(),v.end()),v.end()); int main(){ int n; cin>>n; rep(j,n){ // if(k==l) continue; vint ans; int a,b; // cout<<a<<" -> "<<b<<endl; cin>>a>>b; // cout<<q.front()<<endl; queue<int> q; rep(i,10) q.push(i); rep(i,5) q.push(5-i); rep(i,30){ int t=q.front(); q.push(t); q.pop(); if(t==a){ ans.clear(); ans.PB(t); } else if(t==b&&ans.size()&&ans[0]==a){ ans.PB(t); break; } else{ ans.PB(t); } } rep(i,ans.size()){ if(i) cout<<" "; cout<<ans[i]; } cout<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int n, m, x[15], y[15], mini = 1005, temp, cek, mx = 10, my = 10; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> temp; x[temp]++; mx = min(mx, temp); } for (int i = 1; i <= m; i++) { cin >> temp; y[temp]++; if (x[temp] == y[temp]) { if (temp < mini) mini = temp; cek = 1; } my = min(my, temp); } if (cek == 1) ; else { mini = min(mx, my) * 10; mini = mini + max(mx, my); } cout << mini << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long m = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) { ans = (ans * x) % m; } x = (x * x) % m; y /= 2; } return ans % m; } long long modInverse(long long a, long long m = 1000000007) { long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } bool isp(long long b, map<long long, long long> mp) { bool f = true; for (auto &it : mp) { if (it.first * it.first == b) { it.second = 0; } for (auto &jt : mp) { if (it.first * jt.first == b) { long long z = min(it.second, jt.second); it.second -= z; jt.second -= z; } } } for (auto it : mp) { if (it.second > 0) { f = false; } } return f; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; t = 1; while (t--) { long long n, m, p, i, j, ans = 0; string s, t; cin >> s >> t; n = s.length(); m = t.length(); j = 0; vector<long long> ft(m, -1), sd(m, -1); for (i = 0; i < n; i++) { if (s[i] == t[j] && ft[j] == -1) { ft[j] = i; j++; } } reverse(t.begin(), t.end()); reverse(s.begin(), s.end()); j = 0; for (i = 0; i < n; i++) { if (s[i] == t[j] && sd[m - j - 1] == -1) { sd[m - j - 1] = n - i - 1; j++; } } ans = sd[0]; ans = max(ans, n - ft[m - 1] - 1); for (i = 0; i < m; i++) { ans = max(ans, max(0LL, sd[i] - ft[i - 1] - 1)); } cout << ans; } return 0; }
4
#include <bits/stdc++.h> void dbg(const char* fmt, ...) {} double fact[100]; double c(int n, int k) { if (k < 0 || n < k) return 0; return fact[n] / fact[k] / fact[n - k]; } long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } std::pair<int, int> ans[100][100]; double solve(int n, int m) { double count = 0; double total_count = 0; for (int i = 1; i <= n; i++) { double temp = c(m - 1, i - 1) * c(m * (n - 1), n - i); total_count += temp; count += temp * i / n; } ans[n][m] = std::pair<int, int>((int)round(count * n), (int)round(total_count * n)); return count / total_count; } double solve2(int n, int m) { if (n == 1) return 1; double ans = 1 / (double)n; double nn = n - 2; double magic2 = 2 * nn * nn + 7 * nn + 6; double magic1 = nn * nn + nn - 1; double magic0 = -2 - 3 * nn - nn * nn; for (int i = 1; i < m; i++) { double zzz = magic2 * i * i + magic1 * i + magic0; ans *= 1 + (n - 1) * (n - 1) / zzz; } return ans; } int main() { fact[0] = 1; for (int i = 1; i < 100; i++) fact[i] = fact[i - 1] * i; int n, m; scanf("%d%d", &n, &m); printf("%.10lf\n", solve2(n, m)); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int counter = 0; string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == '1' || s[i] == '3' || s[i] == '5' || s[i] == '7' || s[i] == '9') counter++; } cout << counter; return 0; }
1
#include <iostream> #include <map> using namespace std; int main(void){ map<int,int> m; double tmp; int n,i=0,t; cin >> n; for(;i<n;i++){ cin >> tmp; if(tmp < 165){ m[1]++; }else if(tmp < 170){ m[2]++; }else if(tmp < 175){ m[3]++; }else if(tmp < 180){ m[4]++; }else if(tmp < 185){ m[5]++; }else{ m[6]++; } } for(int i=1;i<=6;i++){ cout << i << ":"; for(int j=0;j<m[i];j++) cout << "*"; cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; int chh; const int N = 350005; int n, m; pair<pair<int, int>, int> p[N]; set<pair<int, int> > s; set<pair<int, int> >::iterator iter; int main() { int ans, id, h, src, tmp, cnt; pair<int, int> t, d; while (~scanf("%d %d", &n, &m)) { for (int i = 0; i < (n); i++) { scanf("%d %d", &t.first, &t.second); p[i].first = t, p[i].second = i + 1; } sort(p, p + n); s.clear(); ans = 0, id = 0, src = p[0].first.first; for (int i = 0; i < (n); i++) { d = p[i].first; h = d.first; while (!s.empty()) { if (s.begin()->first < h) s.erase(s.begin()); else break; } if ((s).size() < m) t = make_pair(-1, -1); for (; id <= n - 1; id++) { d = p[id].first; if (d.first <= h) { s.insert(make_pair(d.second, id)); if ((s).size() == m) t = *s.begin(); else if ((s).size() > m) { if (make_pair(d.second, id) > t) { iter = s.find(t); t = *(++iter); } } } else break; } if ((s).size() >= m) { tmp = t.first - h + 1; if (tmp > ans) ans = tmp, src = h; } } printf("%d\n", ans); if (ans > 0) { cnt = 0; for (int i = 0; i < (n); i++) if (p[i].first.first <= src && p[i].first.second >= src + ans - 1) { printf("%d\n", p[i].second); if (++cnt == m) break; } } else { for (int i = (1); i <= (m); i++) printf("%d\n", i); } } return 0; }
4
#include <iostream> #include <algorithm> #include <vector> using namespace std; class Edge { public: int from, to, cost; Edge(){} }; const int INF = 999999999; int cost[1000]; Edge e[2000]; int V, E, r; bool isUpdated; bool bellmanford() { fill(cost, cost + V, INF); cost[r] = 0; for (int i = 0; i < V; i++) { isUpdated = false; for (int j = 0; j < E; j++) { if (cost[e[j].from] == INF) continue; if (cost[e[j].to] <= cost[e[j].from] + e[j].cost) continue; isUpdated = true; cost[e[j].to] = cost[e[j].from] + e[j].cost; } if (!isUpdated) return true; } return false; } int main() { cin >> V >> E >> r; for (int i = 0; i < E; i++) { cin >> e[i].from >> e[i].to >> e[i].cost; } if (bellmanford()) { for (int i = 0; i < V; i++) { if (cost[i] == INF) { cout << "INF" << endl; continue; } cout << cost[i] << endl; } } else { cout << "NEGATIVE CYCLE" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int recur_depth = 0; const long long sz = 1e5 + 10; long long a[sz]; int main() { long long n, k, sum = 0; cin >> n >> k; for (long long i = 1; i <= n; ++i) scanf("%lld", &a[i]), sum += a[i]; if (sum < k) { printf("-1\n"); return 0; } if (k == 0) { for (long long i = 1; i <= n; ++i) printf("%lld ", i); printf("\n"); return 0; } long long lo = 1, hi = 1e9, ret = 0, bad = 0; while (lo <= hi) { long long mid = lo + hi >> 1; long long tot = 0; for (long long i = 1; i <= n; ++i) tot += min(a[i], mid); if (tot <= k) { ret = mid, bad = tot; lo = mid + 1; } else hi = mid - 1; } deque<int> lst; for (long long i = 1; i <= n; ++i) { a[i] -= min(a[i], ret); if (a[i] > 0) lst.push_back(i); } k -= bad; while (k > 0) { k--; long long x = lst.front(); lst.pop_front(); a[x]--; if (a[x] != 0) lst.push_back(x); } for (int& x : lst) printf("%d ", x); printf("\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using pli = pair<ll, int>; using pil = pair<int, ll>; template <typename T> using Array = vector<T>; template <typename T> using Matrix = Array<Array<T>>; using Graph = Matrix<int>; template <typename T> bool relax(T& r, const T& first) { if (first < r) { r = first; return true; } return false; } template <typename T> bool heavy(T& h, const T& first) { if (h < first) { h = first; return true; } return false; } const int DX[8] = {-1, +1, +0, +0, +1, -1, +1, -1}; const int DY[8] = {+0, +0, -1, +1, +1, -1, -1, +1}; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int first; cin >> first; for (int a = 1; a <= first; a++) for (int b = 1; b <= first; b++) if (a % b == 0 && a * b > first && a < b * first) cout << a << ' ' << b, exit(0); cout << -1; return false; }
1
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto& a : x) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; ll m[55][55]; int main() { cin.sync_with_stdio(false); cin.tie(0); cin.exceptions(cin.failbit); for (;;) { int n; cin >> n; if (!n) break; ll fm = 0; rep(i, 1, n + 1) { m[0][i] = 1ll << i; fm |= 1ll << i; } vector<vi> f(33); rep(i, 1, n + 1) { int t; cin >> t; rep(j, 0, t) { int k; cin >> k; f[k].push_back(i); } } rep(i, 1, 31) { rep(j, 1, n + 1) m[i][j] = m[i - 1][j]; ll mm = 0; for (int h : f[i]) mm |= m[i][h]; if (mm == fm) { cout << i << endl; goto done; } for (int h : f[i]) m[i][h] = mm; } cout << -1 << endl; done: void(0); } }
0
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 1e6 + 10; const long long mod = 1e9 + 7; const double PI = acos(-1); int sum[400]; inline int ab(int x) { return x >= 0 ? x : -x; } int main() { int n, x, ans = 400; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &x); sum[i] = sum[i - 1] + x; } for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { ans = min(ans, ab(360 - (sum[j] - sum[i - 1]) * 2)); } } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 5; int a[MAXN][1 << 3]; int f[MAXN]; int id1, id2; int n, m; bool check(int ans) { for (int i = 0; i <= 1 << m; i++) { f[i] = 0; } int mx = (1 << m) - 1, temp; for (int i = 1; i <= n; i++) { temp = 0; for (int j = 0; j < m; j++) { if (a[i][j] >= ans) temp |= 1 << j; } for (int x = temp; x; x = (x - 1) & temp) { if (f[x]) break; f[x] = i; } f[0] = i; temp ^= mx; if (f[temp]) { id1 = i, id2 = f[temp]; return true; } } return false; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &a[i][j]); } } int L = 0, R = 1 << 30, mid; while (L <= R) { mid = L + R >> 1; if (check(mid)) { L = mid + 1; } else { R = mid - 1; } } printf("%d %d", id1, id2); return 0; }
4
#include <bits/stdc++.h> using namespace std; bool is_lucky(string s) { int Sum1 = 0, Sum2 = 0, mid = (s.size()) / 2; for (int i = 0; i < int(s.size()); i++) { if (s[i] != '4' && s[i] != '7') return false; if (i < mid) Sum1 += int(s[i] - '0'); else Sum2 += int(s[i] - '0'); } return (Sum1 == Sum2); } int main() { int n; cin >> n; string s; cin >> s; if (is_lucky(s)) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> const int N = 1001; int x[N], y[N]; std::map<std::pair<int, int>, std::set<long long>> slope_map; int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", &x[i], &y[i]); long long total = 0, res = 0; for (int i = 1; i <= n - 1; ++i) for (int j = i + 1; j <= n; ++j) { int x1 = x[i], y1 = y[i], x2 = x[j], y2 = y[j]; int a = y1 - y2, b = x1 - x2; int d = gcd(a, b); a /= d, b /= d; if (a < 0 || (a == 0 && b < 0)) { a = -a; b = -b; } std::pair<int, int> slope(a, b); long long c = 1LL * a * x1 - 1LL * b * y1; if (!slope_map[slope].count(c)) { ++total; slope_map[slope].insert(c); res += total - slope_map[slope].size(); } } printf("%lld\n", res); }
3
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define irep(i, n) for (int i = (n); i >= 0; i++) using namespace std; using ll = long long; using P = pair<int, int>; const int INF = 1 << 30; int main() { int n; cin >> n; vector<int> o(100010), e(100010); rep(i,n) { int v; cin >> v; if (i%2 == 1) o[v]++; else e[v]++; } int i = max_element(e.begin(),e.end()) - e.begin(); int j = max_element(o.begin(), o.end()) - o.begin(); if (i != j) { cout << n-e[i]-o[j] << endl; } else { sort(e.begin(),e.end(),greater<int>()); sort(o.begin(),o.end(),greater<int>()); int an = min(n-e[0]-o[1],n-e[1]-o[0]); cout << an << endl; } return 0; }
0
#include <stdio.h> #include <string.h> #define R register #define MN 100005 int n,m,q,x,y,xx,yy,aa[MN],bb[MN]; char a[MN],b[MN]; inline int read(){ R int x; R bool f; R char c; for (f=0; (c=getchar())<'0'||c>'9'; f=c=='-'); for (x=c^'0'; (c=getchar())>='0'&&c<='9'; x=(x<<1)+(x<<3)+(c^'0')); return f?-x:x; } int main(){ scanf("%s%s",a+1,b+1); n=strlen(a+1),m=strlen(b+1); for (R int i=1; i<=n; ++i) aa[i]=a[i]-'A'+aa[i-1]+1; for (R int i=1; i<=m; ++i) bb[i]=b[i]-'A'+bb[i-1]+1; q=read();while(q--){ x=read(),y=read(),xx=read(),yy=read(); (aa[y]-aa[x-1])%3==(bb[yy]-bb[xx-1])%3? puts("YES"):puts("NO"); }return 0; }
0
#include <bits/stdc++.h> using namespace std; void func(void) { freopen("input.c", "r", stdin); freopen("output.c", "w", stdout); } int main() { long long n, q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m, fg = 0, mx = 0, mx1 = 0, mn = 8000000000000000000, mn1 = 8000000000000000000; cin >> n >> x >> y; for (i = 0; i <= 10000000; i++) { temp = n - i * x; if (temp >= 0 and temp % y == 0) { cout << "YES" << endl; cout << i << " " << temp / y << endl; return 0; } } cout << "NO\n"; }
2
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 100001; void precalc() {} void clear() {} void test() {} void solve() {} int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); precalc(); int n; cin >> n; string s; cin >> s; int x = 0; int y = 0; int ans = 0; if (s[0] == 'U') { y = y + 1; } else { x = x + 1; } int st = 0; if (y > x) { st = 1; } else { if (x > y) { st = -1; } } for (int i = 1; i < n; i++) { if (s[i] == 'U') { y = y + 1; } else { x = x + 1; } int cur = 0; if (y > x) { cur = 1; } else { if (x > y) { cur = -1; } } if (st != cur && cur != 0) { ans++; } if (cur != 0) { st = cur; } } cout << ans << endl; int t = 0; while (t--) { solve(); } }
2
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int b[100100], s[100100]; deque<pair<int, int> > qb, qs; int main() { int n, ss; while (~scanf("%d%d", &n, &ss)) { char op[3]; memset(b, 0, sizeof(b)); memset(s, 0, sizeof(s)); int maxp = 0; for (int i = 0; i < n; i++) { int p, q; scanf("%s%d%d", op, &p, &q); if (op[0] == 'S') s[p] += q; else b[p] += q; maxp = max(maxp, p); } while (!qb.empty()) qb.pop_front(); while (!qs.empty()) qs.pop_front(); for (int i = maxp; i >= 0 && (int)qb.size() < ss; i--) if (b[i]) qb.push_back(make_pair(i, b[i])); for (int i = 0; i <= maxp && (int)qs.size() < ss; i++) if (s[i]) qs.push_front(make_pair(i, s[i])); while (!qs.empty()) { printf("S %d %d\n", qs.front().first, qs.front().second); qs.pop_front(); } while (!qb.empty()) { printf("B %d %d\n", qb.front().first, qb.front().second); qb.pop_front(); } } return 0; }
2
#include <bits/stdc++.h> int minimum(int num1, int num2) { if (num1 <= num2) return num1; else return num2; } int main() { int a, b, counter = 0; scanf("%d %d", &a, &b); int min = minimum(a, b); for (int i = 0; i <= min; i++) { for (int j = 0; j <= min; j++) { if ((i * i) + j == a && i + (j * j) == b) { counter++; } } } printf("%d\n", counter); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 3010; int r, c, n, K, a[21]; long long ans; vector<int> V[maxn]; int main() { scanf("%d %d %d %d", &r, &c, &n, &K); for (int _ = 0, x, y; _ < n; _++) { scanf("%d %d", &x, &y), V[x].push_back(y); } for (int _ = 1; _ <= r; _++) { multiset<int> S; long long cur = 0; for (int i = _; i <= r; i++) { for (int $ : V[i]) { auto R = S.insert(a[K] = $), L = R++; for (int j = 1; j <= K; j++) { a[K - j] = L == S.begin() ? 0 : *--L; a[K + j] = R == S.end() ? c + 1 : *R++; } for (int j = 1; j <= K; j++) { cur += (a[K + j] - a[K + j - 1]) * (a[j] - a[j - 1]); } } ans += cur; } } printf("%lld\n", ans); return 0; }
6
#include <bits/stdc++.h> #define int long long using namespace std; signed main() { int N,A,B;cin>>N>>A>>B; cout<<min(A,B)<<" "<<max(A+B-N,0LL)<<endl; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const int maxN = 2100; int n, k; string s[maxN]; int d[maxN][maxN]; int ans[maxN]; struct node { int cnt; vector<int> d; node* children[26]; node() { for (int i = 0; i < 26; ++i) children[i] = NULL; cnt = 0; } }; void add(node* root, string s) { node* cur = root; for (int i = 0; i < s.size(); ++i) if (cur->children[s[i] - 'a'] != NULL) { cur = cur->children[s[i] - 'a']; } else { cur->children[s[i] - 'a'] = new node(); cur = cur->children[s[i] - 'a']; } ++cur->cnt; } int dp[30][maxN]; void dfs(node* root, int depth = 0) { vector<pair<int, int> > children; for (int i = 0; i < 26; ++i) if (root->children[i] != NULL) { dfs(root->children[i], depth + 1); children.push_back(make_pair(root->children[i]->d.size(), i)); } if (children.size() == 0) { for (int i = 0; i <= root->cnt; ++i) root->d.push_back((i * (i - 1)) / 2 * depth); return; } if (root->cnt != 0) { children.push_back(make_pair(root->cnt + 1, -1)); } sort(children.begin(), children.end()); int sum = 0; for (int i = 0; i < children.size(); ++i) { if (i == 0) { int index = children[i].second; if (index == -1) { for (int j = 0; j <= root->cnt; ++j) dp[i][j] = (j * (j - 1)) / 2 * depth; sum += root->cnt; } else { for (int j = 0; j < children[i].first; ++j) dp[i][j] = root->children[index]->d[j]; sum += children[i].first - 1; } } else { int index = children[i].second; vector<int> cur; if (index != -1) { cur = root->children[index]->d; } else { for (int j = 0; j <= root->cnt; ++j) cur.push_back((j * (j - 1)) / 2 * depth); } for (int j = 0; j <= sum + cur.size() - 1; ++j) dp[i][j] = 0; for (int j = 0; j <= sum; ++j) for (int k = 0; k < cur.size(); ++k) { dp[i][j + k] = max(dp[i][j + k], dp[i - 1][j] + cur[k] + k * j * depth); } sum += cur.size() - 1; } } if (sum == 0) { dp[children.size() - 1][0] = 0; } for (int i = 0; i <= sum; ++i) root->d.push_back(dp[children.size() - 1][i]); for (int i = 0; i < children.size(); ++i) { int index = children[i].second; if (index != -1) { root->children[index]->d.clear(); } } } void solve() { node* root = new node(); for (int i = 0; i < n; ++i) add(root, s[i]); dfs(root); printf("%d\n", root->d[k]); } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) { cin >> s[i]; } solve(); return 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { for (int l = 0; l < s[i].size() && l < s[j].size(); ++l) { if (s[i][l] == s[j][l]) d[i][j] = l + 1; else break; } } } int res = 0; for (int i = 0; i < (1 << n); ++i) { int bits = 0; for (int j = 0; j < n; ++j) if (i & (1 << j)) ++bits; int score = 0; for (int j = 0; j < n; ++j) if (i & (1 << j)) for (int l = j + 1; l < n; ++l) if (i & (1 << l)) score += d[j][l]; if (bits == k) { res = max(res, score); } } printf("%d\n", res); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int x, y, z, xx, yy, zz; x = max(a, 2 * d + 1); xx = 2 * a; y = max(b, 2 * d + 1); yy = 2 * b; z = max(c, d); zz = min(2 * c, 2 * d); if (xx >= x && yy >= y && zz >= z) { for (int i = x; i <= xx; i++) { for (int j = y; j <= yy; j++) { for (int k = z; k <= zz; k++) { if (i > j && j > k) { cout << i << endl << j << endl << k << endl; return 0; } } } } } cout << -1 << endl; return 0; }
1
#include<iostream> #include<iomanip> #include<algorithm> #include<bitset> #include<cctype> #include<climits> #include<cmath> #include<cstdio> #include<cstring> #include<deque> #include<list> #include<map> #include<set> #include<stack> #include<string> #include<sstream> #include<queue> #include<vector> using namespace std; #define M 14 int main() { int A[M][M] = {}; for (int x, y, size; scanf("%d,%d,%d", &x, &y, &size) != EOF;) { x += 2; y += 2; A[x][y]++; A[x + 1][y]++; A[x][y + 1]++; A[x - 1][y]++; A[x][y - 1]++; if (size == 1)continue; A[x + 1][y + 1]++; A[x + 1][y - 1]++; A[x - 1][y + 1]++; A[x - 1][y - 1]++; if (size == 2)continue; A[x + 2][y]++; A[x][y + 2]++; A[x - 2][y]++; A[x][y - 2]++; } int cnt = 100, MAX = 0; for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { if (A[x + 2][y + 2]) { cnt--; MAX = max(MAX, A[x + 2][y + 2]); } } } cout << cnt << endl; cout << MAX << endl; return 0; }
0
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <map> using namespace std; map<string,bool> adj = { {"AB", true}, {"AD", true}, {"BA", true}, {"BC", true}, {"BE", true}, {"CB", true}, {"CF", true}, {"DA", true}, {"DE", true}, {"DG", true}, {"EB", true}, {"ED", true}, {"EF", true}, {"EH", true}, {"FC", true}, {"FE", true}, {"FI", true}, {"GD", true}, {"GH", true}, {"HE", true}, {"HG", true}, {"HI", true}, {"IF", true}, {"IH", true} }; int main() { string s; for (int i=0; i<1000; ++i) { cin >> s; bool ok = true; for (int i=1; i<(int)s.size(); ++i) { if ( ! adj[s.substr(i-1, 2)]) { ok = false; break; } } if (ok) cout << s << endl; } return 0; }
0
# include <bits/stdc++.h> using namespace std; vector < int > adj[100007]; int used[100007], dp[100007], mx; void dfs( int v ) { if ( used[v] ) return; used[v] = 1; for ( auto u: adj[v] ) { dfs(u); dp[v] = max( dp[v], dp[u] + 1 ); } } int main() { int n, m; cin >> n >> m; for ( int i = 0; i < m; i++ ) { int u, v; cin >> u >> v; adj[u].push_back(v); } for ( int i = 1; i <= n; i++ ) { dfs(i); mx = max( mx, dp[i] ); } cout << mx << endl; }
0
#include <bits/stdc++.h> using namespace std; vector<int> T[300300]; int N, Q, P[300300], S[300300], C[300300]; void dfs(int x) { S[x] = 1; vector<int> c; for (int y : T[x]) { dfs(y); S[x] += S[y]; c.push_back(C[y]); } for (int i = 0; i < T[x].size(); i++) { int y = T[x][i]; if (S[y] * 2 >= S[x]) { y = C[y]; while (S[y] * 2 < S[x]) y = P[y]; C[x] = y; return; } } C[x] = x; } int main() { scanf("%d %d", &N, &Q); for (int i = 2; i <= N; i++) scanf("%d", &P[i]), T[P[i]].push_back(i); dfs(1); for (int i = 0; i < Q; i++) { int x; scanf("%d", &x); printf("%d\n", C[x]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXD = 10; struct intArr { int num[MAXD]; }; const int INFINITE = 0x7FFFFFFF; const int MAXN = 100001; int n, q, l, r, tk; int t[MAXD]; char s[MAXN]; vector<int> a[MAXN]; intArr c; vector<intArr> b; inline void addPosition(int p) { tk++, a[p].push_back(tk); memcpy(c.num, t, sizeof(t)), b.push_back(c); } inline void whileProcess() { int k = 0; while (k < n) { int i = k + 1, b = k + 1, d = 1, p = 0; while (i >= b && i <= n) { k = max(k, i), addPosition(i); if (s[i] >= '0' && s[i] <= '9') { t[s[i] - '0']++; if (s[i] == '0') s[i] = 0; else s[i]--; p = 0; } else if (s[i] == '<' || s[i] == '>') { if (s[i] == '<') d = -1; else d = 1; if (p > 0) s[p] = 0; p = i; } i += d; } addPosition(i); } } int main() { scanf("%d %d\n", &n, &q); for (int i = 1; i <= n; ++i) scanf("%c", &s[i]); whileProcess(); for (int i = 1; i <= q; ++i) { scanf("%d %d\n", &l, &r); int hd = a[l][0], tl = INFINITE; int id1 = lower_bound(a[l - 1].begin(), a[l - 1].end(), hd) - a[l - 1].begin(); if (id1 < (int)a[l - 1].size()) tl = min(tl, a[l - 1][id1]); int id2 = lower_bound(a[r + 1].begin(), a[r + 1].end(), hd) - a[r + 1].begin(); if (id2 < (int)a[r + 1].size()) tl = min(tl, a[r + 1][id2]); hd--, tl--; for (int j = 0; j <= 9; ++j) printf("%d%s", b[tl].num[j] - b[hd].num[j], (j < 9) ? " " : "\n"); } fclose(stdin); fclose(stdout); return 0; }
4
#include <cstdio> #include <iostream> using namespace std; int main(){ char s[17]; int a,b; for (int i = 0;i < 9;i++){ cin >> s >> a >> b; cout << s << " "; cout << a + b << " "; cout << a * 200 + b * 300 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; if (k == n) cout << -1; else if (k == n - 1) for (long long i = 1; i <= n; i++) cout << i << ' '; else { cout << k + 2 << ' '; for (long long i = 2; i <= k + 1; i++) cout << i << ' '; for (long long i = k + 3; i <= n; i++) cout << i << ' '; cout << 1; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAX = 170; double C(int n, int k) { double ans = 1; k = min(k, n - k); for (int i = 0; i < (k); i++) { ans = (ans * (n - i)) / (i + 1); } return ans; } int main() { int n, m, h, s = 0, tot; cin >> n >> m >> h; n--; for (int i = 1; i <= m; i++) { int a; cin >> a; if (i != h) s += a; else tot = a - 1; } tot += s; if (tot < n) { cerr << -1 << endl; cout << -1 << endl; return 0; } double ans = 1; if (n <= s) { cerr << s << " " << s - n << endl; cerr << tot << " " << tot - n << endl; double p = C(s, n); p /= C(tot, n); ans -= p; } cerr << ans << endl; printf("%.10f\n", ans); return 0; }
2