solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; vector<int> prefix[30]; int main() { ios_base::sync_with_stdio(false); int test; cin >> test; while (test--) { int n, m; string s; cin >> n >> m >> s; for (char c = 'a'; c <= 'z'; c++) { prefix[c - 'a'] = vector<int>(n + 1); for (int i = 1; i <= n; i++) { prefix[c - 'a'][i] = prefix[c - 'a'][i - 1] + (s[i - 1] == c); } } vector<int> p(m), ans(30); for (int i = 0; i < m; i++) { cin >> p[i]; for (char c = 'a'; c <= 'z'; c++) { ans[c - 'a'] += prefix[c - 'a'][p[i]]; } } for (int i = 0; i < n; i++) { ans[s[i] - 'a']++; } for (int i = 0; i < 26; i++) cout << ans[i] << " "; cout << endl; for (int i = 0; i < 26; i++) prefix[i].clear(); } }
3
#include <iostream> using namespace std;int main(void){int A, B, C;cin>>A>>B>>C;cout<<(A+B<C?"No":"Yes")<<endl;}
0
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; #define ll long long #define ull unsigned long long #define rep(i,n,N) for(ll i=n;i<=N;++i) #define rap(i,n,N) for(int i=n;i>=N;--i) #define mp make_pair #define pb push_back #define pob pop_back #define pf push_front #define pof pop_front #define fi first #define se second //#define ff fi.fi //#define fs fi.se #define sf se.fi #define ss se.se #define lc (id<<1) #define rc ((id<<1)|1) #define db(x) cerr << ">>>>>> " << #x << " -> " << x << endl #define nani(x) cerr << "Line " << __LINE__ << ": " << #x << " is " << x << endl #define all(x) x.begin(),x.end() #define pii pair<int,int> #define pll pair<ll,ll> #define plll pair<ll,pll> #define piii pair<int,pii> #define piiii pair<pii,pii> #define psi pair<string,int> #define endl '\n' const int MAX = 2e5+5; const ll MAX2 = 11; const ll MOD = 1000000007; const ll MOD2 = 1000005329; const ll INF = 2e18; const int dr[]={1,0,-1,0,1,1,-1,-1,0}; const int dc[]={0,1,0,-1,1,-1,1,-1,0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 2000; ll cnt[MAX],id,ans,tmp; stack<int> buk; stack<piii> tup; string sss; int main(){ // cout<<fixed<<setprecision(10); // freopen("input.txt", "r", stdin); // freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>sss; buk.push(0), tup.push({0,{0,0}}); for(auto i:sss){ ++id; if(i=='(')buk.push(id); else if(i==')'){ if(buk.top()==0){ tup.push({id,{-1,cnt[0]}}); cnt[0] = 0; } else { tmp = buk.top(); buk.pop(); tup.push({id,{buk.top(),tmp}}); ++cnt[buk.top()]; ans+=cnt[buk.top()]; } } else { if(buk.top()>tup.top().fi)buk.pop(); else { if(tup.top().sf==-1){ cnt[0] = tup.top().ss; tup.pop(); } else { ans-=cnt[tup.top().sf]; cnt[tup.top().sf]--; buk.push(tup.top().ss); tup.pop(); } } } assert(ans>=0); cout<<ans<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int mxn = 1005; const int mxm = 3005; int n, m; char str[mxn]; bool valid[mxn][mxm]; inline void print(int p) { stack<char> ans; for (int i = n; i; --i) switch (str[i - 1]) { case 'L': ans.push('L'); p = p + 1; break; case 'W': ans.push('W'); p = p - 1; break; case 'D': ans.push('D'); break; case '?': if (valid[i - 1][p - 1]) ans.push('W'), p = p - 1; else if (valid[i - 1][p + 1]) ans.push('L'), p = p + 1; else ans.push('D'); break; } while (!ans.empty()) cout << ans.top(), ans.pop(); } signed main() { cin >> n >> m >> str; valid[0][1500] = true; for (int i = 0; i < n; ++i) { valid[i][1500 + m] = false; valid[i][1500 - m] = false; switch (str[i]) { case 'L': for (int j = 0; j < mxm; ++j) if (valid[i][j]) valid[i + 1][j - 1] = true; break; case 'W': for (int j = 0; j < mxm; ++j) if (valid[i][j]) valid[i + 1][j + 1] = true; break; case 'D': for (int j = 0; j < mxm; ++j) if (valid[i][j]) valid[i + 1][j] = true; break; case '?': for (int j = 0; j < mxm; ++j) if (valid[i][j]) { valid[i + 1][j] = true; valid[i + 1][j - 1] = true; valid[i + 1][j + 1] = true; } break; } } if (valid[n][1500 + m]) return print(1500 + m), 0; if (valid[n][1500 - m]) return print(1500 - m), 0; return puts("NO"), 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long x1, x2, y1, y2; long long a, b, c; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; int ans = 0; while (n--) { scanf("%lld%lld%lld", &a, &b, &c); if (((a * x1 + b * y1 + c) / abs(a * x1 + b * y1 + c)) * ((a * x2 + b * y2 + c) / abs(a * x2 + b * y2 + c)) < 0) { ans++; } } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); string s; char c[6] = {'a', 'e', 'i', 'o', 'u', 'y'}; long long int i, n, x, y, j; cin >> n >> s; for (i = 0; i < s.length() - 1; i++) { x = 0; y = 0; for (j = 0; j < 6; j++) { if (s[i] == c[j]) { x = 1; } if (s[i + 1] == c[j]) { y = 1; } } if (x == 1 && y == 1) { s.erase(s.begin() + i + 1); i--; } } cout << s; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1000005; long long modd = 998244353; vector<bool> isprime(N); vector<int> primes; void seive() { isprime[0] = false; isprime[1] = false; for (int i = 2; i <= 1e6; i++) { isprime[i] = true; } for (int i = 2; i * i <= 1e6; i++) { if (isprime[i]) { for (int j = i * i; j <= 1e6; j += i) { isprime[j] = false; } } } for (int i = 2; i < 1e6; i++) { if (isprime[i]) { primes.push_back(i); } } } void Reverse_Matrix() { long long n, l, r, q, k, i; cin >> n; for (i = 1; i <= n; i++) cout << "1 "; cout << endl; } int main() { ios_base::sync_with_stdio(false); int TEST = 1; cin >> TEST; while (TEST--) { Reverse_Matrix(); } }
1
#include <bits/stdc++.h> using namespace std; long long int m, n, k, p1, p2, p3, p, f, a[120][120], b[120][120], mi[120]; string s, s1, s2; vector<int> r, c, r1, c1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < 120; i++) { mi[i] = 1200; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> a[i][j]; b[i][j] = a[i][j]; mi[i] = min(mi[i], a[i][j]); } for (int i = 0; i < n; i++) { for (int j = 0; j < mi[i]; j++) { r.push_back(i); for (int l = 0; l < m; l++) { a[i][l]--; } } } for (int i = 0; i < 120; i++) { mi[i] = 1200; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { mi[j] = min(mi[j], a[i][j]); } for (int i = 0; i < m; i++) { for (int j = 0; j < mi[i]; j++) { c.push_back(i); for (int l = 0; l < n; l++) { a[l][i]--; } } } int fl = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (a[i][j]) fl = 1; } if (fl) { cout << -1; } else { int ans1 = c.size() + r.size(), ans2 = 0; for (int i = 0; i < 120; i++) { mi[i] = 1200; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { a[i][j] = b[i][j]; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { mi[j] = min(mi[j], a[i][j]); } for (int i = 0; i < m; i++) { for (int j = 0; j < mi[i]; j++) { c1.push_back(i); for (int l = 0; l < n; l++) { a[l][i]--; } } } for (int i = 0; i < 120; i++) { mi[i] = 1200; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { mi[i] = min(mi[i], a[i][j]); } for (int i = 0; i < n; i++) { for (int j = 0; j < mi[i]; j++) { r1.push_back(i); for (int l = 0; l < m; l++) { a[i][l]--; } } } ans2 = r1.size() + c1.size(); cout << min(ans1, ans2) << endl; if (ans1 < ans2) { for (int i : r) cout << "row " << i + 1 << endl; for (int j : c) { cout << "col " << j + 1 << endl; } } else { for (int i : r1) cout << "row " << i + 1 << endl; for (int j : c1) { cout << "col " << j + 1 << endl; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, k; cin >> n >> m >> k; if (m >= n && k >= n) { cout << "YES"; } else { cout << "NO"; } cout << endl; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int num; cin >> num; vector<int> answers; answers.emplace_back(1); for (int power = 1; power <= 9; power++) { int start = (int)(pow(10, power) + 1e-9); if (num < start) { cout << answers.size() << "\n"; return 0; } answers.emplace_back(start); for (int i = 0; i < (int)(pow(2, power) - 1 + 1e-9); i++) { int next = start + answers[i]; if (num < next) { cout << answers.size() << "\n"; return 0; } answers.emplace_back(next); } } }
3
#include <bits/stdc++.h> using namespace std; int n; int a[2007], b[2007], p[2007], h[2007]; vector<int> ch[2007]; // 0 - white // 1 - red // 2 - blue int type[2007]; int cntred = 0; bool active[2007]; vector<int> res; void rotate(int w) { res.push_back(w); memcpy(b, a, sizeof a); b[w] = a[0]; while(w) { b[p[w]] = a[w]; w = p[w]; } memcpy(a, b, sizeof b); } void dfs(int w) { active[w] = 1; for(int u : ch[w]) dfs(u); if(ch[w].size() == 0 || (ch[w].size() == 1 && type[ch[w][0]] == 1)) { type[w] = 1; cntred++; } } void dfs2(int w) { for(int u : ch[w]) { h[u] = h[w] + 1; dfs2(u); } } mt19937 rng(time(0)); int rand(int a, int b) { return uniform_int_distribution<int>(a, b)(rng); } int main() { scanf("%d", &n); //~ n = 2000; for(int i = 1 ; i < n ; i++) { scanf("%d", &p[i]); //~ p[i] = rand(0, i - 1); //~ cout << p[i] << " "; ch[p[i]].push_back(i); } dfs2(0); for(int i = 0 ; i < n ; i++) scanf("%d", &a[i]); //~ a[i] = i; //~ shuffle(a, a + n, rng); int cn = 0; while(ch[0].size()) { memset(type, 0, sizeof type); memset(active, 0, sizeof active); cntred = 0; dfs(0); if(type[0] == 1) cntred--; type[0] = 0; //~ cout << "dfs" << endl; //~ for(int i = 0 ; i < n ; i++) //~ cout << type[i] << " " ; //~ cout << endl; while(cntred) { cn++; //~ cout << "it" << " " << cn << endl; //~ for(int i = 0 ; i < n ; i++) //~ cout << "in vertex " << i << " there is a ball color " << type[a[i]] << " number " << a[i] << endl; //~ cout << endl; if(type[a[0]] == 1) { int w = a[0]; while(type[p[w]] == 1 || type[p[w]] == 2) w = p[w]; while(!ch[w].empty() && (type[a[ch[w][0]]] != 2 || (type[a[ch[w][0]]] == 2 && h[a[ch[w][0]]] < h[a[0]]))) w = ch[w][0]; cntred--; type[a[0]] = 2; rotate(w); } else { int w = 0; for(int i = 1 ; i < n ; i++) if(type[a[i]] < 2 && h[i] > h[w] && active[i]) w = i; type[a[0]] = 3; rotate(w); } //~ if(cn > 100) //~ exit(0); } for(int i = 0 ; i < n ; i++) { if(type[a[i]] == 2) { ch[p[i]].erase(find(ch[p[i]].begin(), ch[p[i]].end(), i)); } } } printf("%d\n", res.size()); for(int x : res) printf("%d\n", x); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; const int si[4] = {-1, 0, +1, 0}; const int sj[4] = {0, +1, 0, -1}; const int MOD = 5000011; int main() { string s; cin >> s; vector<pair<string, int> > a; int i = 0; int j = 0; while (i < s.size()) { string t = ""; while (s[j] != ',') { t += s[j]; j++; } j++; int ch = 0, r = 0; while (s[j] != ',' && j < s.size()) { ch = s[j] - '0' + ch * 10; j++; } j++; i = j; a.push_back({t, ch}); } stack<int> st; vector<vector<string> > ans; ans.resize(900000); int v = 0; for (int i = 0; i < a.size(); i++) { ans[v].push_back(a[i].first); if (v > 0) st.top()--; if (a[i].second > 0) v++, st.push(a[i].second); while (st.size() > 0 && st.top() == 0) { st.pop(); v--; } } int kl = 0; for (int i = 0; i < ans.size(); i++) { if (ans[i].size() == 0) { kl = i; break; } } cout << kl << endl; for (int i = 0; i < kl; i++) { for (int j = 0; j < ans[i].size(); j++) cout << ans[i][j] << ' '; cout << endl; } }
5
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vl; ll h,w; int main(int argc, char const *argv[]) { int T; scanf("%d", &T); for(int unko=0;unko<T;unko++){ string s; cin >> s; stack<int> po; int num = 0; for(int i=0;i<s.size();i++){ if(s[i] == '['){ po.push(-1); } else if(s[i] == ']'){ if(num != 0){ po.pop(); po.push((num + 1)/2); num = 0; } else{ vector<int> v; while(po.top() != -1){ v.push_back(po.top()); po.pop(); } po.pop(); sort(v.begin(), v.end()); int n = 0; for(int j=0;j<(v.size() + 1)/2; j++)n += v[j]; po.push(n); } } else{ num *= 10; num += s[i] - '0'; } } printf("%d\n", po.top()); } return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<int> v; void dfs(deque<long long int> q, int m) { if (m == 1) { v.push_back(max(q.back(), q.front())); return; } deque<long long int> p, r; r = q; p = q; r.pop_front(); p.pop_back(); dfs(r, m - 1); dfs(p, m - 1); return; } int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; ; for (long int z = 0; z < t; z++) { string a, b, c; cin >> a >> b >> c; int d = 0; ; for (long int i = 0; i < a.size(); i++) { if (a[i] == c[i]) { b[i] = c[i]; } else if (b[i] == c[i]) { a[i] = c[i]; } else d = 1; } if (d == 1) cout << "NO" << '\n'; else cout << "YES" << '\n'; } return 0; }
1
#include <bits/stdc++.h> int prime = 100003; long long N, M; int main() { std::cin >> N >> M; std::cout << prime << " " << prime << "\n"; for (int i = 1; i <= N - 2; ++i) std::cout << i << " " << i + 1 << " " << 1 << "\n"; std::cout << N - 1 << " " << N << " " << prime - N + 2 << "\n"; M -= N - 1; for (int i = 1; i <= N - 1 and M > 0; ++i) { for (int j = i + 2; j <= N and M > 0; ++j) { std::cout << i << " " << j << " " << 1000000000 << "\n"; M -= 1; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; int n, t, a, b, x; string s; vector<int> v; int main() { scanf("%d", &t); while (t--) { scanf("%d%d", &a, &b); int ans = 0; while (a != 0 && b != 0) { if (a >= b) { ans += a / b; a %= b; } else { ans += b / a; b %= a; } } printf("%d\n", ans); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a, b; scanf("%d%d", &n, &m); if (n <= 6) printf("%d", m); else { vector<vector<int>> d(8, vector<int>(8, 0)); for (int i = 0; i < m; i++) scanf("%d%d", &a, &b), d[a][b] = 1, d[b][a] = 1; int mn = 21; for (int i = 1; i <= 7; i++) for (int j = 1; j <= 7; j++) { int x = 0; for (int k = 1; k <= 7; k++) x += d[i][k] && d[k][j]; mn = min(mn, x); } printf("%d", m - mn); } }
1
#include <bits/stdc++.h> using namespace std; long long dp[100001] = {0}, mod = (long long int)1000000007; long long extgcd(long long a, long long b, long long &x, long long &y) { long long g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; } long long invMod(long long a, long long m) { long long x, y; if (extgcd(a, m, x, y) == 1) return (x + m) % m; else return 0; } int main() { long long int n; cin >> n; dp[1] = 2; for (long long int i = 2; i <= n; ++i) { dp[i] = (((dp[i - 1] * (2 * i - 1)) % mod) * 2 * invMod(i, mod)) % mod; } cout << (dp[n] - n) % mod << endl; cin >> n; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> p) { os << "{ "; for (auto i : p) { os << i << ","; } os << "\b"; os << "}"; return os; } template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <typename T> void keySort(vector<T> &arr, vector<int> &key) { int n = arr.size(); if (key.size() != n) { throw runtime_error("Size of data array not equals to size of key array"); } vector<T> ans(n); int ar[n]; for (int i = 0; i < n; i++) { ar[key[i]] = i; } for (int i = 0; i < n; i++) { ans[i] = arr[ar[i]]; } arr = ans; } void solve(); int main() { string s; int t; cin >> t; while (t--) { solve(); cin >> s; if (s != "Correct") { return 0; } } return 0; } int ask(vector<int> &vc) { cout << "? " << vc.size() << ' '; for (auto i : vc) { cout << i << ' '; } cout << endl; vc.clear(); int ans; cin >> ans; return ans; } int ask(int l, int r) { cout << "? " << r - l + 1 << ' '; for (int i = l; i <= r; i++) { cout << i << ' '; } cout << endl; int ans; cin >> ans; return ans; } void solve() { int n, k; cin >> n >> k; vector<set<int>> sets; for (int i = 0; i < k; i++) { int a; cin >> a; set<int> vc; int b; while (a--) { cin >> b; vc.insert(b); } sets.push_back(vc); } vector<int> query; for (int i = 1; i <= n; i++) { query.push_back(i); } int ans; ans = ask(query); int mxEl = ans; int lo = 1, hi = n; while (hi != lo) { int m = (lo + hi) / 2; ans = ask(lo, m); if (ans == mxEl) { hi = m; } else lo = m + 1; } int setWithMax = -1; for (int i = 0; i < k; i++) { if (sets[i].find(lo) != sets[i].end()) { setWithMax = i; } } if (setWithMax == -1) { cout << "! "; for (int i = 0; i < k; i++) { cout << mxEl << ' '; } cout << endl; return; } for (int i = 1; i <= n; i++) { if (sets[setWithMax].find(i) != sets[setWithMax].end()) { continue; } query.push_back(i); } vector<int> an; for (int i = 0; i < k; i++) { if (i == setWithMax) { an.push_back(ask(query)); } else { an.push_back(mxEl); } } cout << "! "; for (auto i : an) { cout << i << ' '; } cout << endl; }
6
#include <bits/stdc++.h> using namespace std; double PI = (acos(-1)); long long md = 1000000007; long long pw(long long a, long long b) { long long c = 1, m = a; while (b) { if (b & 1) c = (c * m); m = (m * m); b /= 2; } return c; } long long pwmd(long long a, long long b) { long long c = 1, m = a; while (b) { if (b & 1) c = ((c * m)) % md; m = (m * m) % md; b /= 2; } return c; } long long modinv(long long n) { return pwmd(n, md - 2); } long long nc2(long long n) { return (1ll * n * (n - 1)) / 2; } bool prime(long long a) { for (int i = 2; i * i <= a; i++) { if (a % i == 0) return false; } return true; } long long ceel(long long a, long long b) { if (a % b == 0) return a / b; else return a / b + 1; } long long fun(vector<long long> x, vector<long long> y, vector<long long> z) { long long a = (int)x.size(), b = (int)y.size(), c = (int)z.size(); vector<long long> ans; for (long long i = 0; i < b; i++) { long long s = lower_bound(x.begin(), x.end(), y[i] + 1) - x.begin() - 1; long long second = lower_bound(z.begin(), z.end(), y[i]) - z.begin(); if (s >= 0 && second < c) { ans.push_back(pw(y[i] - x[s], 2) + pw(y[i] - z[second], 2) + pw(x[s] - z[second], 2)); } } sort(ans.begin(), ans.end()); if ((int)ans.size()) return ans[0]; else return 3e18; } void solve() { long long a, b, c; cin >> a >> b >> c; vector<long long> x(a), y(b), z(c); for (long long i = 0; i < a; i++) cin >> x[i]; for (long long i = 0; i < b; i++) cin >> y[i]; for (long long i = 0; i < c; i++) cin >> z[i]; sort(x.begin(), x.end()), sort(y.begin(), y.end()), sort(z.begin(), z.end()); vector<long long> ans; ans.push_back(fun(x, y, z)); ans.push_back(fun(x, z, y)); ans.push_back(fun(y, x, z)); ans.push_back(fun(y, z, x)); ans.push_back(fun(z, y, x)); ans.push_back(fun(z, x, y)); sort(ans.begin(), ans.end()); cout << ans[0] << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long tests = 1; cin >> tests; while (tests--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int i, j = 0, n, k = 0, l = 0; string s; cin >> s; for (i = 0; i < s.length(); i++) { if (isupper(s[i])) { j++; } else { k++; } } if (j > k) { transform(s.begin(), s.end(), s.begin(), ::toupper); } else { transform(s.begin(), s.end(), s.begin(), ::tolower); } cout << s; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int a[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } long long int k; long long int b[n]; for (int i = 1; i < (n - 1); i++) { k = ((a[0][i + 1]) * (a[i][i + 1])); k /= (a[0][i]); k = sqrt(k); b[i + 1] = k; } b[1] = ((a[2][1]) / (b[2])); b[0] = ((a[1][0]) / (b[1])); for (int i = 0; i < n; i++) { cout << b[i] << " "; } cout << endl; return 0; }
2
#include <bits/stdc++.h> int main() { int n; std::cin >> n; long long int sum[100010] = {0}, v[100010], t[100010]; for (int i = 0; i < n; ++i) std::cin >> v[i]; for (int i = 0; i < n; ++i) std::cin >> t[i]; sum[0] = t[0]; for (int i = 1; i < n; ++i) { sum[i] += sum[i - 1] + t[i]; } long long int add[100010] = {0}, many[100010] = {0}; long long int summing = 0; for (int i = 0; i < n; ++i) { int point = std::lower_bound(sum, sum + n, v[i] + summing) - sum; add[point] += t[point] - (sum[point] - (v[i] + summing)); ++many[i]; --many[point]; summing += t[i]; } for (int i = 1; i < n; ++i) { many[i] += many[i - 1]; } long long int suma = 0; for (int i = 0; i < n; ++i) { suma += many[i] * t[i]; suma += add[i]; std::cout << suma << " "; suma = 0; } }
2
#include <bits/stdc++.h> using namespace std; int main(){ string s; for(int i=0;i<3;i++){ cin>>s; cout<<s[i]; } return 0; }
0
//0066 #include<stdio.h> int main(void) { const int N = 9; char str[N+1]; while(scanf("%s", str) == 1) { const char ca[] = "ox"; char findC = 'd'; for(int i = 0; i <= 1; i++) { const char c = ca[i]; if( (c == str[0] && c == str[1] && c == str[2]) || (c == str[3] && c == str[4] && c == str[5]) || (c == str[6] && c == str[7] && c == str[8]) || (c == str[0] && c == str[3] && c == str[6]) || (c == str[1] && c == str[4] && c == str[7]) || (c == str[2] && c == str[5] && c == str[8]) || (c == str[0] && c == str[4] && c == str[8]) || (c == str[2] && c == str[4] && c == str[6]) ) { findC = c; break; } } putchar(findC); putchar('\n'); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long dp[19][2][1 << 8][2520]; bool okey[1 << 8][2520]; int mul[20]; int dig[20]; int n; long long dfs(int pos, int U, int st, int sum) { if (pos == -1) { return okey[st][sum]; } long long &ret = dp[pos][U][st][sum]; if (ret != -1) return ret; int end = 9; if (U) end = dig[pos]; ret = 0; for (int i = 0; i <= end; i++) { int NU = U; if (i != end) NU = 0; int Nst = st; if (i >= 2) Nst |= (1 << (i - 2)); int Nsum = (sum + i * mul[pos]) % 2520; ret += dfs(pos - 1, NU, Nst, Nsum); } return ret; } long long func(long long num) { n = 0; while (num) { dig[n++] = num % 10; num /= 10; } for (int i = 0; i < n; i++) { memset(dp[i][1], -1, sizeof(dp[i][1])); } return dfs(n - 1, 1, 0, 0); } void init() { for (int i = 0; i < 256; i++) { for (int j = 0; j < 2520; j++) { okey[i][j] = true; for (int d = 2; d <= 9; d++) { if (i & (1 << (d - 2))) { if (j % d != 0) { okey[i][j] = false; break; } } } } } } int main() { init(); memset(dp, -1, sizeof(dp)); mul[0] = 1; for (int i = 1; i < 20; i++) { mul[i] = (mul[i - 1] * 10) % 2520; } int T; cin >> T; while (T--) { long long a, b; cin >> a >> b; cout << func(b) - func(a - 1) << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[100]; int main() { long long n, l, r; cin >> n >> l >> r; if (n == 0) return 0 * printf("0\n"); int i = 0; while (n) { a[i++] = n % 2; n >>= 1; } i--; int len = i; int sum = (r - l + 1) / 2; if (((r - l + 1) & 1) && r & 1) sum++; long long s = l & 1 ? l + 1 : l; for (; s <= r; s += 2) { for (i = 0; i < len; i++) { if ((s - (1LL << len - i)) % (1LL << len - i + 1) == 0) sum += a[i]; } } printf("%d\n", sum); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; vector<int> adj[1100000]; int arr[1100000][2], ans[1100000]; bool vis[1100000]; void DFS(int p, int b) { vis[p] = 1; ans[p] = b + 1; for (int i = 0; i < adj[p].size(); i++) { int nx = adj[p][i]; if (vis[nx]) continue; DFS(nx, b ^ 1); } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i][0] >> arr[i][1]; arr[i][0]--, arr[i][1]--; adj[arr[i][0]].push_back(arr[i][1]); adj[arr[i][1]].push_back(arr[i][0]); } for (int i = 0; i < n; i++) { adj[i * 2].push_back(i * 2 + 1); adj[i * 2 + 1].push_back(i * 2); } for (int i = 0; i < 2 * n; i++) if (!vis[i]) { DFS(i, 0); } for (int i = 0; i < n; i++) cout << ans[arr[i][0]] << ' ' << ans[arr[i][1]] << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; inline double get_y(double xx, double yy, double mi) { return yy - xx / tan(mi); } int main() { double y1, y2, yw, xb, yb, r; cin >> y1 >> y2 >> yw >> xb >> yb >> r; double lo = 0, hi = atan2(xb, yw - r - yb), mi; for (int it = 0; it < 10000; it++) { mi = (lo + hi) / 2.; double d = (yw - r - yb) * tan(mi); double xx = xb - d, yy = yw - r; double xd = xx + r * cos(mi), yd = yy - r * sin(mi); if (get_y(xx, yy, mi) < y1 + r) lo = mi; else hi = mi; } double v1 = lo; lo = 0, hi = atan2(xb, yw - r - yb); for (int it = 0; it < 10000; it++) { mi = (lo + hi) / 2.; double d = (yw - r - yb) * tan(mi); double xx = xb - d, yy = yw - r; double xd = xx - r * cos(mi), yd = yy + r * sin(mi); if (get_y(xx, yy, mi) > y2 - r || get_y(xd, yd, mi) > y2 || xd < 0) hi = mi; else lo = mi; } mi = (lo + v1) / 2; double d = (yw - r - yb) * tan(mi); double xx = xb - d, yy = yw - r; double xu = xx - r * cos(mi), yu = yy + r * sin(mi); double xd = xx + r * cos(mi), yd = yy - r * sin(mi); yy = get_y(xx, yy, mi); if (yy < y1 + r + 1e-8 || yy > y2 - r - 1e-8 || get_y(xu, yu, mi) > y2) puts("-1"); else printf("%.12lf\n", xx); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; vector<long long> fib(200); fib[0] = 1; fib[1] = 1; for (int i(2); i < (103); ++i) fib[i] = (fib[i - 1] + fib[i - 2]) % (int(1e9)); cin >> n >> m; vector<int> d(n); for (int i = 0; i < (n); ++i) cin >> d[i]; for (int q = 0; q < (m); ++q) { int type; cin >> type; if (type == 1) { int x, v; cin >> x >> v; d[x - 1] = v; } else { int l, r; cin >> l >> r; --l; --r; long long res = 0; for (int i = 0; i < (r - l + 1); ++i) { res += fib[i] * d[l + i]; res %= (int(1e9)); } cout << res << endl; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << "=" << h << "\n"; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...); } int32_t main() { int n; scanf("%d", &n); vector<int> v; for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); v.push_back(x); } sort(v.begin(), v.end()); int cnt = 0; multiset<int> s; for (int i = n - 1; i >= 0; --i) { if (s.upper_bound(v[i]) == s.end()) ++cnt; else s.erase(s.upper_bound(v[i])); s.insert(v[i]); } printf("%d", cnt); }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; set<int> s; int n; cin >> n; int temp; for (int i = 0; i < n; i++) { cin >> temp; s.insert(temp); } vector<int> v; set<int>::iterator it; if (s.size() == 1) { cout << "NO"; } else { it = s.begin(); it++; cout << *it; } return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; vector <P> e; ll p,q; int main(){ int w,h; cin>>w>>h; for(int i=0;i<w;i++){ cin>>p; e.push_back(P(p,0)); } for(int i=0;i<h;i++){ cin>>q; e.push_back(P(q,1)); } sort(e.begin(),e.end()); int hc=-1,wc=-1; ll ans=0; for(int i=0;i<(int)e.size();i++){ //cout<<e[i].first<<" "<<e[i].second<<endl; if(e[i].second == 0){ wc++; ans+=(h-hc)*e[i].first; } else{ hc++; ans+=(w-wc)*e[i].first; } } cout<<ans<<endl; return 0; }
0
#include "iostream" #include <algorithm> using namespace std; int partition(int A[],int p,int r){ int x = A[r]; int i = p - 1; for(int j = p ; j < r ; j++){ if(A[j] <= x){ i=i +1; swap(A[i],A[j]); } } swap(A[i+1],A[r]); return i + 1; } int main(){ int n,m; int A[1000000]; cin >> n; for(int i = 0; i<n;i++) cin >> A[i]; m = partition(A,0,n-1); for(int i = 0 ; i < n; i++){ if(i) cout << " "; if(i == m) cout << "["; cout << A[i]; if(i == m) cout << "]"; } cout << endl; return 0; }
0
#include <iostream> #include <string> #include <cctype> using namespace std; int dx[] = {-1, 0}; int dy[] = {0, -1}; string getMax(string a, string b) { if(a.size() != b.size()) return (a.size() < b.size() ? b : a); for(int i=0; i<a.size(); i++) if(a[i] != b[i]) return (a[i] < b[i] ? b : a); return a; } string add(string a, char b) { string res; if(a.size() == 1 && a[0] == '0') res += b; else res += a + b; return res; } int main() { int W,H; while(cin >> W >> H, (W||H)) { string field[70]; for(int i=0; i<H; i++) cin >> field[i]; string ans[70][70]; string res; for(int i=0; i<H; i++) for(int j=0; j<W; j++) { if(!isdigit(field[i][j])) continue; string tmp; for(int k=0; k<2; k++) { int tx = j + dx[k]; int ty = i + dy[k]; if(tx<0||ty<0) continue; tmp = getMax(tmp, ans[ty][tx]); } tmp = add(tmp, field[i][j]); ans[i][j] = tmp; res = getMax(res, tmp); } cout << res << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { vector<int> a0, a1, b0, b1; int ct[200010], ct2[200010]; char a[200010], b[200010]; scanf("%s", a); scanf("%s", b); int sa = strlen(a), sb = strlen(b); for (int i = 0; i < sa; ++i) { if (a[i] == '0') a0.push_back(i); else a1.push_back(i); } for (int i = 0; i < sb; ++i) { if (b[i] == '0') { b0.push_back(i); if (i == 0) { ct[i] = 0; ct2[i] = 1; } else { ct[i] = ct[i - 1]; ct2[i] = ct2[i - 1] + 1; } } else { b1.push_back(i); if (i == 0) { ct[i] = 1; ct2[i] = 0; } else { ct[i] = ct[i - 1] + 1; ct2[i] = ct2[i - 1]; } } } int ch; long long ans = 0; for (int i = 0; i < a0.size(); ++i) { ch = (sb - (sa - a0[i])); if (a0[i] == 0) ans += ct[ch]; else ans += (ct[ch] - ct[a0[i] - 1]); } for (int i = 0; i < a1.size(); ++i) { ch = (sb - (sa - a1[i])); if (a1[i] == 0) ans += ct2[ch]; else ans += (ct2[ch] - ct2[a1[i] - 1]); } printf("%I64d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; string s, ans; int n, k, lo, hi; bool sat(int x) { int occ[26], ret = 0; memset(occ, 0, sizeof(occ)); int len = s.length(); for (int i = 0; i < len; i++) occ[s[i] - 'a']++; for (int i = 0; i < 26; i++) ret += ((occ[i] + x - 1) / x); return ret <= n; } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); cin >> s; cin >> n; lo = 0, hi = s.length(); while (lo < hi) { int mid = lo + (hi - lo + 1) / 2; if (sat(mid)) hi = mid - 1; else lo = mid; } if ((lo + 1) > s.length()) { cout << -1 << '\n'; return 0; } int occ[26]; memset(occ, 0, sizeof(occ)); for (int i = 0; i < s.length(); i++) occ[s[i] - 'a']++; for (int i = 0; i < 26; i++) for (int j = 0; j < ((occ[i] + (lo + 1) - 1) / (lo + 1)); j++) ans += (i + 'a'); for (int i = ans.length(); i < n; i++) ans += 'a'; cout << lo + 1 << '\n'; cout << ans << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n; int arr[maxn]; string ans; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); while (cin >> n) { for (int i = 1; i <= n; ++i) { cin >> arr[i]; } ans = ""; int l = 1, r = n; int last = 0; while (r - l >= 0) { if ((arr[l] < arr[r] && arr[l] > last) || (arr[r] < last && arr[l] > last)) { ans += "L"; last = arr[l++]; } else if (arr[r] > last) { ans += "R"; last = arr[r--]; } else { break; } } cout << ans.size() << endl; cout << ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int z[1000010]; void vector_mul(int *x, int *y, int n) { for (int i = 0; i < n; i++) z[i] = x[y[i]]; for (int i = 0; i < n; i++) x[i] = z[i]; } int t[1000010]; void vector_pow(int *x, int n, int k) { for (int i = 0; i < n; i++) t[i] = i; while (k) { if (k & 1) vector_mul(t, x, n); vector_mul(x, x, n); k >>= 1; } for (int i = 0; i < n; i++) x[i] = t[i]; } char q[1000010]; char s[1000010]; int l_move[1000010]; int d_sort[1000010]; int main(int argc, char *argv[]) { int n, m, k, d; scanf("%s", q); n = strlen(q); for (int i = 0; i < n; i++) l_move[i] = (i + 1) % n; cin >> m; while (m--) { scanf("%d%d", &k, &d); if (n == 1) { printf("%s\n", q); continue; } int x = 0; int y = 0; for (int i = 0; i < n; i++) d_sort[i] = i; for (int i = 0; i < k; i++) { d_sort[i] = x + y * d; if (x + (y + 1) * d >= k) { x += 1; y = 0; } else { y += 1; } } vector_mul(d_sort, l_move, n); vector_pow(d_sort, n, n - k + 1); for (int i = 0; i < n; i++) { s[i] = q[d_sort[i]]; } x = 0; for (int i = k - 1; i < n; i++) q[x++] = s[i]; for (int i = 0; i < k - 1; i++) q[x++] = s[i]; printf("%s\n", q); } return 0; }
3
#include<bits/stdc++.h> #define IL inline #define _ 200005 #define ll long long using namespace std; IL int gi(){ int data=0,fu=1; char ch=0; while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar(); if(ch=='-')fu=-1,ch=getchar(); while('0'<=ch&&ch<='9')data=(data<<1)+(data<<3)+(ch^48),ch=getchar(); return data*fu; } #define mod 1000000007 int n,X[_],V[_],f[_],lp[_],rp[_],bit[_],vov,xox; struct Point{int x,v; }p[_]; vector<int>vec[_]; IL int Query(int x){++x; int ret=0; while(x<=vov+1)ret=(ret+bit[x])%mod,x+=(x&-x); return ret; } IL void Insert(int x,int fv){++x; while(x)bit[x]=(bit[x]+fv)%mod,x-=(x&-x); return; } IL bool cmp(Point i,Point j){return i.x<j.x; } int main(){ n=gi(); for(int i=1;i<=n;++i)p[i].x=gi(),p[i].v=gi(),X[++xox]=p[i].x,V[++vov]=p[i].v; sort(X+1,X+xox+1); xox=unique(X+1,X+xox+1)-X-1; sort(V+1,V+vov+1); vov=unique(V+1,V+vov+1)-V-1; sort(p+1,p+n+1,cmp); for(int i=1;i<=n;++i){ p[i].x=lower_bound(X+1,X+xox+1,p[i].x)-X; p[i].v=lower_bound(V+1,V+vov+1,p[i].v)-V; } for(int i=1,v=-mod;i<=n;++i)v=max(v,p[i].v),rp[i]=v; for(int i=n,v= mod;i>=1;--i)v=min(v,p[i].v),lp[i]=v; for(int i=1;i<=n;++i)vec[rp[i]].push_back(lp[i]); Insert(0,1); for(int i=1;i<=vov;++i){ for(auto l:vec[i])f[i]=Query(l-1),Insert(i,f[i]); } cout<<Query(vov)<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; map<pair<long long, long long>, pair<long long, long long> > f; long long n; pair<long long, long long> solve(long long n, long long Mlc) { if (n <= 9) { if (n == 0 && Mlc == 0) return {0LL, 0LL}; if (Mlc == 0) return {1LL, 0LL}; if (Mlc >= n) return {1LL, n - Mlc}; return {2LL, -Mlc}; } if (f.count({n, Mlc})) { return f[{n, Mlc}]; } long long aux = n; long long p = 1LL; while (p <= n / 10LL) p *= 10LL; pair<long long, long long> q1 = solve(n % p, max(Mlc, n / p)); n = n - n % p; n += q1.second; pair<long long, long long> q2 = solve(n, Mlc); pair<long long, long long> q = {q1.first + q2.first, q2.second}; f[{aux, Mlc}] = q; return q; } int main() { scanf("%lld", &n); printf("%lld", solve(n, 0).first); return 0; }
3
#include <bits/stdc++.h> const int maxn = 100100; using namespace std; struct node { int ls, rs, mx; node() { ls = rs = mx = 0; } } tree[maxn * 40]; int cnt = 0, n, m, root[maxn]; void insert(int &x, int l, int r, int pos, int v) { if (!x) x = ++cnt; tree[x].mx = max(tree[x].mx, v); if (l == r) return; int mid = l + r >> 1; if (mid >= pos) insert(tree[x].ls, l, mid, pos, v); else insert(tree[x].rs, mid + 1, r, pos, v); } int query(int x, int l, int r, int L, int R) { if (!x) return 0; if (R < l || L > r) return 0; if (L <= l && r <= R) return tree[x].mx; int mid = l + r >> 1; return max(query(tree[x].ls, l, mid, L, R), query(tree[x].rs, mid + 1, r, L, R)); } int main() { scanf("%d%d", &n, &m); int ans = 1; for (int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); int tmp = query(root[u], 0, 100000, 0, w - 1) + 1; insert(root[v], 0, 100000, w, tmp); ans = max(tmp, ans); } cout << ans; }
6
#include <bits/stdc++.h> using namespace std; int ans[1005]; struct node { int pp, ppp; } a[1005]; bool cmp(struct node aa, struct node b) { return aa.pp < b.pp; } int main() { int n, sum; cin >> n; long long summ = 0; cin >> sum; for (int i = 1; i < n; i++) { cin >> a[i].pp; summ += a[i].pp; a[i].ppp = i + 1; } int flag = 0; sort(a + 1, a + n, cmp); long long tot = 0, summm = summ; ans[0] = 1; int u = 1, anss = 0; for (int i = 1; i < n; i++) { if (a[i].pp <= sum / 2) { summm -= a[i].pp; anss += a[i].pp; ans[u] = a[i].ppp, ++u; } else { break; } } if (summ < sum) flag = 0; if (summm >= anss + sum) flag = 1; if (flag) { cout << 0 << '\n'; return 0; } cout << u << '\n'; for (int i = 0; i < u; i++) cout << ans[i] << ' '; return 0; }
1
#include <iostream> #include <vector> #include <set> #include <algorithm> using namespace std; int main() { string s, ans="NO"; cin >> s; reverse(s.begin(),s.end()); while(true){ if(s==""){ ans = "YES"; break; }else if(s.find("maerd")==0 || s.find("esare")==0){ s.erase(0,5); }else if(s.find("remaerd")==0){ s.erase(0,7); }else if(s.find("resare")==0){ s.erase(0,6); }else{ break; } } cout<< ans <<endl; }
0
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int size[100005]; int main() { int k, b, n, a; int assign, mod = 0, mods = 0; long long ans = 0; size[0] = 1; mp[0] = 0; mods++; cin >> k >> b >> n; long long zero = 0, cost = 0; for (int i = 0; i < n; i++) { cin >> a; zero = (a) ? 0 : zero + 1; if (b == k - 1 || b == 0) cost += zero; mod = (mod + a) % (k - 1); if (mp.find(mod) == mp.end()) mp[mod] = mods++; assign = mp[mod]; int sup = (mod + (k - 1) - b) % (k - 1); if (mp.find(sup) == mp.end()) { size[assign]++; continue; } sup = mp[sup]; ans += size[sup]; size[assign]++; } if (b == 0) cout << cost << endl; else cout << ans - cost << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> words[1000009]; int n, c; class FenwickTree { private: vector<int> ft1, ft2; int rsq(vector<int>& ft, int i) { int sum = 0; while (i > 0) { sum += ft[i]; i -= (i & -i); } return sum; } void update(vector<int>& ft, int i, int v) { while (i < (int)ft.size()) { ft[i] += v; i += (i & -i); } } public: FenwickTree(int n) { ft1.assign(n + 1, 0); ft2.assign(n + 1, 0); } void update(int i, int j, int v) { if (j < i) return; update(ft1, i, v); update(ft1, j + 1, -v); update(ft2, i, v * (i - 1)); update(ft2, j + 1, -v * j); } int rsq(int i) { return rsq(ft1, i) * i - rsq(ft2, i); } int rsq(int i, int j) { return rsq(j) - rsq(i - 1); } }; int main() { scanf("%d %d", &n, &c); for (int i = 0, l, k; i < n; i++) { scanf("%d", &l); for (int j = 0; j < l; j++) { scanf("%d", &k); words[i].push_back(k); } } FenwickTree arr(c); int arr0 = 0; for (int i = 0, j; i < n - 1; i++) { j = i + 1; bool equal = true; for (int k = 0; k < (int)min(words[i].size(), words[j].size()); k++) { if (words[i][k] < words[j][k]) { equal = false; arr.update(c - words[j][k] + 1, c - words[i][k], 1); break; } if (words[i][k] == words[j][k]) { } if (words[i][k] > words[j][k]) { arr0 = 1; equal = false; arr.update(1, c - words[i][k], 1); arr.update(c - words[j][k] + 1, c, 1); break; } } if (words[i].size() > words[j].size() && equal) { arr.update(1, c, 1); arr0 = 1; } } int can = -1; for (int i = 1; i < c; i++) { if (arr.rsq(i, i) == 0) { can = i; } } if (arr0 == 0) { can = 0; } printf("%d\n", can); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4; char buf[maxn]; int pos[maxn], cnt; long long s[maxn], p[maxn]; long long S[maxn], top; long long calc(int L, int R, long long s[]) { top = 0; for (int i = L; i <= R; i++) { if (s[i] >= 0) S[top++] = s[i]; if (s[i] == -2) { S[top - 1] *= s[i + 1]; ++i; } } long long res = 0; for (int i = 0; i < top; i++) { res += S[i]; } return res; } void solve() { cnt = 0; pos[cnt++] = -1; int n = strlen(buf); for (int i = 0; i < n; i++) { if (buf[i] == '*') pos[cnt++] = i; if (buf[i] == '+') s[i] = -1; else if (buf[i] == '*') s[i] = -2; else s[i] = buf[i] - '0'; } pos[cnt++] = n; long long ans = calc(0, n - 1, s); for (int i = 0; i < cnt; i++) { for (int j = i + 1; j < cnt; j++) { int n1 = 0; for (int k = 0; k <= pos[i]; k++) { p[n1++] = s[k]; } p[n1++] = calc(pos[i] + 1, pos[j] - 1, s); for (int k = pos[j]; k < n; k++) { p[n1++] = s[k]; } ans = max(ans, calc(0, n1 - 1, p)); } } printf("%I64d\n", ans); } int main() { while (~scanf("%s", buf)) solve(); return 0; }
5
#include<bits/stdc++.h> using namespace std; int a[100010],n; int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; sort(a,a+n+1); cout<<a[n/2+1]-a[n/2]; return 0; }
0
#include<iostream> #include<vector> #include<algorithm> using namespace std; vector<int> V; void solve(){ sort(V.begin(),V.end()); int pos = 0; while(pos < V.size()){ int start = pos; while(V[pos]+1 == V[pos+1] && pos < V.size()-1) pos++; if(start != 0) cout << " "; if(pos-start == 0) cout << V[pos++]; else cout << V[start] << "-" << V[pos++]; } cout << endl; } int main(){ int n; while(cin >> n && n){ V.clear(); while(n--){ int in; cin >> in; V.push_back(in); } solve(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; long long int min = 100000; int A, B, C; for (int i = 1; i <= 10000; i++) { int temp = a - i; if (temp < 0) temp = temp * (-1); for (int j = i; j <= 20000; j += i) { int temp2 = b - j; if (temp2 < 0) temp2 = temp2 * (-1); for (int k = j; k <= 20000; k += j) { int temp3 = c - k; if (temp3 < 0) temp3 = temp3 * (-1); int sum = temp + temp2 + temp3; if (sum < min) { min = sum; A = i; B = j; C = k; } } } } cout << min << "\n"; cout << A << " " << B << " " << C << "\n"; } }
4
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 200005; const double PI = 4 * atan(1); const double eps = 1e-7; const long long oo = 1e10; const int K = 205; long long dp[N][5]; long long num[N][5]; long long n, k; vector<long long> adj[N]; long long ans = 0; void dfs(long long src, long long par) { num[src][0]++; for (auto t : adj[src]) { if (t == par) continue; dfs(t, src); for (int i = 0; i < k; i++) { long long x = dp[t][(i + k - 1) % k]; if (i == 0) x += num[t][(i + k - 1) % k] * k; long long y = num[t][(i + k - 1) % k]; for (int j = 0; j < k; j++) { ans += ((x / k) * num[src][j] + (dp[src][j] / k) * y); if (i + j > k) ans += 2 * (num[src][j] * y); else if (i + j > 0) ans += (num[src][j] * y); } } for (int i = 0; i < k; i++) { dp[src][(i + 1) % k] += (dp[t][i]); if (i == k - 1) dp[src][(i + 1) % k] += (num[t][i] * k); num[src][(i + 1) % k] += num[t][i]; } } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; x--; y--; adj[x].push_back(y); adj[y].push_back(x); } dfs(0, 0); cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxi = 1e6 + 10; long long a[maxi]; int n, q; string s; vector<int> v[maxi]; long long sum[maxi]; map<long long, long long> mp; int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; cin >> q; while (q--) { long long k; scanf("%lld", &k); long long ans = 0; long long cur = n - 1; long long step = k; long long cnt = 1; if (mp[k]) { printf("%I64d ", mp[k]); continue; } while (cur > 0) { ans += cnt * (sum[cur] - sum[max(0ll, cur - step)]); cnt++; cur -= step; step *= k; } mp[k] = ans; printf("%I64d ", ans); } printf("\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; void file() {} void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); } unordered_map<int, int> mp; int n, p; vector<int> v1; bool valid0(int x) { int cnt = 1, curVal = x - 1; for (int i = 0; i < n && v1[i] < x; i++) cnt++; for (int i = 0; i < n; i++) { cnt--, curVal++, cnt += mp[curVal]; if (cnt < 1) return 0; } return 1; } bool valid(int x) { int cnt = 1, curVal = x - 1; for (int i = 0; i < n && v1[i] < x; i++) cnt++; for (int i = 0; i < n; i++) { cnt--, curVal++, cnt += mp[curVal]; if (cnt >= p) return 0; } return 1; } int main() { file(); fast(); cin >> n >> p; v1 = vector<int>(n); for (int i = 0; i < n; i++) { cin >> v1[i]; mp[v1[i]]++; } sort(v1.begin(), v1.end()); int start = 1, end = 1e9, mid, L = -1, R = -1; while (start <= end) { mid = (start + end) / 2; if (valid0(mid)) { L = mid; end = mid - 1; } else start = mid + 1; } if (L == -1) return cout << "0\n", 0; start = 1, end = 1e9, mid; while (start <= end) { mid = (start + end) / 2; if (valid(mid)) { R = mid; start = mid + 1; } else end = mid - 1; } if (R < L) return cout << "0\n", 0; cout << R - L + 1 << "\n"; for (int i = L; i <= R; i++) cout << i << ' '; cout << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; long long inf = 1e18; queue<int> q1, q2; int dis_1[1001], dis_2[1001]; int vis1[1001], vis2[1001]; vector<int> v[1001]; int main() { int n, m, s, t, k; cin >> n >> m >> s >> t; int x, y; k = m; while (m--) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } vis1[s] = 1; vis2[t] = 1; q1.push(s); q2.push(t); while (!(q1.empty())) { int x = q1.front(); q1.pop(); for (int i = 0; i < v[x].size(); i++) if (vis1[v[x][i]] == 0) { dis_1[v[x][i]] = dis_1[x] + 1; q1.push(v[x][i]); vis1[v[x][i]] = 1; } } while (!(q2.empty())) { int x = q2.front(); q2.pop(); for (int i = 0; i < v[x].size(); i++) if (vis2[v[x][i]] == 0) { dis_2[v[x][i]] = dis_2[x] + 1; q2.push(v[x][i]); vis2[v[x][i]] = 1; } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (!((i == s && j == t) || (i == t && j == s))) { if (1 + min(dis_1[i] + dis_2[j], dis_2[i] + dis_1[j]) >= dis_1[t]) ans++; } } } if (dis_1[t] == 1) cout << ans - k + 1; else cout << ans - k; }
4
#include <bits/stdc++.h> using namespace std; const int N = 20; const int M = 30; char s[N][M]; int a[N][M], t[N][M], c[N][M]; int dp[(1 << N) + 5]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%s", s + i); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) scanf("%d", &a[i][j]); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { int maxc = 0; for (int k = 0; k < n; ++k) if (s[i][j] == s[k][j]) { t[i][j] |= (1 << k); c[i][j] += a[k][j]; maxc = max(maxc, a[k][j]); } c[i][j] -= maxc; } memset(dp, 0x3f, sizeof(dp)); dp[0] = 0; for (int mk = 1; mk < (1 << n); ++mk) { int lowbit; for (int i = 0; i < n; ++i) if (mk & (1 << i)) { lowbit = i; break; } for (int i = 0; i < m; ++i) { dp[mk] = min(dp[mk], dp[mk & (mk ^ t[lowbit][i])] + c[lowbit][i]); dp[mk] = min(dp[mk], dp[mk ^ (1 << lowbit)] + a[lowbit][i]); } } printf("%d\n", dp[(1 << n) - 1]); return 0; }
5
#include <bits/stdc++.h> using namespace std; map<long long, long long> ma; map<long long, long long>::iterator it; long long arr[100005], pre[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; pre[0] = 0; for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + arr[i]; ma[pre[i]]++; } long long ans = 9999999; for (it = ma.begin(); it != ma.end(); it++) { long long val = it->second; ans = min(ans, n - val); } cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; int n, d[maxn]; map<pair<int, int>, set<int> > H; map<pair<int, int>, set<int> > H2; vector<int> E[maxn]; vector<int> ans; void dfs(int x, int y, int id) { int a = min(x, y); int b = max(x, y); ans.push_back(x); for (auto xx : H2[make_pair(a, b)]) { if (xx == id) continue; int mid = xx; int z = E[mid][0] + E[mid][1] + E[mid][2] - a - b; ans.pop_back(); dfs(x, z, mid); ans.pop_back(); dfs(z, y, mid); ans.pop_back(); } ans.push_back(y); } void solve() { H.clear(); H2.clear(); ans.clear(); cin >> n; for (int i = 0; i < n - 2; i++) { E[i].clear(); for (int j = 0; j < 3; j++) { int x; cin >> x; E[i].push_back(x); } d[i] = 0; sort(E[i].begin(), E[i].end()); H[make_pair(E[i][0], E[i][1])].insert(i); H[make_pair(E[i][0], E[i][2])].insert(i); H[make_pair(E[i][1], E[i][2])].insert(i); H2[make_pair(E[i][0], E[i][1])].insert(i); H2[make_pair(E[i][0], E[i][2])].insert(i); H2[make_pair(E[i][1], E[i][2])].insert(i); } for (auto x : H) { set<int> p = x.second; pair<int, int> GG = x.first; for (auto t : p) { d[t] += p.size() - 1; } } set<pair<int, int> > Q; for (int i = 0; i < n - 2; i++) { Q.insert(make_pair(d[i], i)); } vector<int> order; while (!Q.empty()) { pair<int, int> nowP = *Q.begin(); int now = nowP.second; Q.erase(Q.begin()); order.push_back(now); H[make_pair(E[now][0], E[now][1])].erase(now); H[make_pair(E[now][0], E[now][2])].erase(now); H[make_pair(E[now][1], E[now][2])].erase(now); for (auto x : H[make_pair(E[now][0], E[now][1])]) { Q.erase(make_pair(d[x], x)); d[x]--; Q.insert(make_pair(d[x], x)); } for (auto x : H[make_pair(E[now][0], E[now][2])]) { Q.erase(make_pair(d[x], x)); d[x]--; Q.insert(make_pair(d[x], x)); } for (auto x : H[make_pair(E[now][1], E[now][2])]) { Q.erase(make_pair(d[x], x)); d[x]--; Q.insert(make_pair(d[x], x)); } } if (H2[make_pair(E[order[0]][0], E[order[0]][1])].size() > 1) { dfs(E[order[0]][0], E[order[0]][1], order[0]); ans.push_back(E[order[0]][2]); } if (H2[make_pair(E[order[0]][0], E[order[0]][2])].size() > 1) { dfs(E[order[0]][0], E[order[0]][2], order[0]); ans.push_back(E[order[0]][1]); } if (H2[make_pair(E[order[0]][1], E[order[0]][2])].size() > 1) { dfs(E[order[0]][1], E[order[0]][2], order[0]); ans.push_back(E[order[0]][0]); } if (n == 3) { cout << "1 2 3" << endl; } else { for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << endl; } for (int i = 0; i < order.size(); i++) cout << order[i] + 1 << " "; cout << endl; } int main() { int t; cin >> t; while (t--) solve(); }
5
# include<bits/stdc++.h> using namespace std; const int MAX=2e5+5; int n,ans; int a[MAX]; map<int,int> ma; bool use[MAX]; int main() { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&a[i]),++ma[a[i]]; sort(a+1,a+1+n); for(int i=31;i>=1;--i) { for(int j=1;j<=n;++j) if(ma[a[j]]) { int x=(1<<i)-a[j]; if(x<=0) break; if(x!=a[j]) { if(ma[x]) ++ans,--ma[x],--ma[a[j]]; } else { if(ma[x]>=2) ++ans,ma[x]-=2; } } } return printf("%d",ans),0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1048576 + 50; const int mod = 1e9 + 7; int n, f[MAXN], pw[MAXN]; void fwt(int *a, int len, int flg) { for (int i = 2; i <= len; i <<= 1) for (int j = 0; j < len; j += i) for (int k = j; k < j + i / 2; ++k) a[k] = (a[k] + 1ll * (flg == 1 ? 1 : mod - 1) * a[k + i / 2]) % mod; } int main() { scanf("%d", &n); int len = pw[0] = 1; for (int i = 1, x; i <= n; ++i) { scanf("%d", &x), ++f[x]; while (len <= x) len <<= 1; pw[i] = pw[i - 1] * 2ll % mod; } fwt(f, len, 1); for (int i = 0; i < len; ++i) f[i] = pw[f[i]]; fwt(f, len, -1); printf("%d\n", f[0] == pw[n] ? f[0] - 1 : f[0]); }
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long expo_pow(long long x, long long y) { if (y == 0) return 1; y = y % (mod - 1); x %= mod; if (y == 0) y = mod - 1; long long res = 1; while (y) { if (y & 1) res = (res * x) % mod; x = (x * x) % mod; y >>= 1; } return res; } long long add() { return 0; } template <typename T, typename... Types> T add(T var1, Types... var2) { return (((((long long)(var1)) % mod + (long long)(add(var2...))) % mod) + mod) % mod; } long long mul() { return 1; } template <typename T, typename... Types> T mul(T var1, Types... var2) { return (((long long)(var1)) % mod * (long long)(mul(var2...))) % mod; } long long ans[2000][2000]; char grid[2000][2000]; bool visited[2000][2000]; long long n, m; long long dx[] = {-1, 0, 1, 0}; long long dy[] = {0, -1, 0, 1}; bool is_valid(long long x, long long y) { return (x >= 0 and x < n) and (y >= 0 and y < m); } void solve() { cin >> n >> m; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) cin >> grid[i][j]; queue<pair<long long, long long> > qu; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { long long fl = 1; for (long long k = 0; k < 4; k++) { long long x = i + dx[k]; long long y = j + dy[k]; if (!is_valid(x, y) or grid[x][y] != grid[i][j]) { fl = 0; break; } } if (fl) continue; visited[i][j] = 1; qu.push({i, j}); ans[i][j] = 1; } } while (!qu.empty()) { auto cur = qu.front(); qu.pop(); for (long long i = 0; i < 4; i++) { long long x = cur.first + dx[i]; long long y = cur.second + dy[i]; if (is_valid(x, y) and grid[x][y] == grid[cur.first][cur.second] and visited[x][y] == 0) { visited[x][y] = 1; ans[x][y] = ans[cur.first][cur.second] + 1; qu.push({x, y}); } } } long long pat = 0; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) pat += ans[i][j]; cout << pat << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long test = 1; long long i = 1; while (test--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f, N = 1e5 + 10; int n, K, top; int a[N], sta[N], vis[N], las[N], pre[N], f[N]; vector<int> vec[N]; int read() { int ret = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 0; c = getchar(); } while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar(); return f ? ret : -ret; } int getlen() { for (int i = 1; i <= top; ++i) vis[i] = 0; sta[top = 1] = INF; for (int i = 1; i <= n; ++i) { int p = upper_bound(sta + 1, sta + top + 1, a[i]) - sta; sta[p] = a[i]; las[i] = vis[p]; vis[p] = i; pre[i] = vis[p - 1]; if (p == top) sta[++top] = INF; } return top - 1; } void LDS(int len) { vec[++K].clear(); for (int i = vis[len]; i; i = pre[i]) vec[K].push_back(a[i]); } void LIS(int len) { for (int i = 1; i <= len; ++i) { vec[++K].clear(); for (int j = vis[i]; j; j = las[j]) vec[K].push_back(a[j]); } } int main() { int T = read(); while (T--) { n = read(); K = 0; for (int i = 1; i <= n; ++i) a[i] = read(); while (n) { int len = getlen(); if (n <= (long long)len * (len + 1) / 2) LDS(len); else { LIS(len); break; } int tn = n, j = len; n = 0; f[j + 1] = 0; for (int i = vis[len]; i; i = pre[i]) f[j--] = a[i]; ++j; for (int i = 1; i <= tn; ++i) if (a[i] ^ f[j]) a[++n] = a[i]; else ++j; } printf("%d\n", K); for (int i = 1; i <= K; ++i) { printf("%d ", (int)vec[i].size()); reverse(vec[i].begin(), vec[i].end()); for (int j = 0; j < (int)vec[i].size(); ++j) printf("%d ", vec[i][j]); puts(""); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxd = 18; const int maxn = 500005; int lat[maxn][maxd]; int rmq[maxn][maxd]; int rad[maxn], best[maxn]; int n, N; long long ret; inline int getint() { int ret = 0; char ch; while ((ch = getchar()) && (ch < '0' || ch > '9')) ; ret = ch - '0'; while ((ch = getchar()) && ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0'; return ret; } void init() { n = getint(); N = n + n; for (int i = 0; i < n; i++) rad[i] = getint(), rad[i + n] = rad[i]; } void work() { for (int i = 0; i < N; i++) best[i] = i - rad[i], rmq[i][0] = i; for (int i = N - 1; i >= 0; i--) for (int d = 1; d < maxd && (1 << d) <= n; d++) { if (best[rmq[i][d - 1]] < best[rmq[i + (1 << (d - 1))][d - 1]]) rmq[i][d] = rmq[i][d - 1]; else rmq[i][d] = rmq[i + (1 << (d - 1))][d - 1]; } for (int i = 0; i < N; i++) { int v = best[i]; if (v <= 0) { lat[i][0] = 0; continue; } int d = (int)log2(i - v + 1); if (best[rmq[v][d]] < best[rmq[i - (1 << d) + 1][d]]) lat[i][0] = rmq[v][d]; else lat[i][0] = rmq[i - (1 << d) + 1][d]; } for (int i = 0; i < N; i++) for (int j = 1; j < maxd; j++) lat[i][j] = lat[lat[i][j - 1]][j - 1]; ret = 0; for (int i = n; i < N; i++) { ret++; if (best[i] <= i - n + 1) continue; int u = i; for (int j = maxd - 1; j >= 0; j--) if (best[lat[u][j]] > i - n + 1) u = lat[u][j], ret += 1 << j; ret++; } } void print() { printf("%I64d\n", ret); } int main() { init(); work(); print(); return 0; }
2
#include<bits/stdc++.h> using namespace std; const int MAXN = 305; const int MAXLOG = 64; typedef long long ll; typedef long double ld; typedef unsigned long long ull; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } bool vis[MAXN]; int n, c, a[MAXN], f[MAXN], h[MAXN], bit[MAXN]; double ans, cnt, dp[MAXN][MAXN][MAXLOG]; void work(int pos) { if (pos == n + 1) { cnt += 1, memset(dp, 0, sizeof(dp)); dp[0][a[1]][(bit[n] << 1) - 1] = 1; for (int i = 0; i <= c - 1; i++) for (int j = i; j <= c; j++) for (int s = 0; s <= (bit[n] << 1) - 1; s++) { int pos = h[i % n]; if (j != i) dp[i + 1][j][s] += dp[i][j][s]; if (bit[pos] & s) dp[i + 1][min(max(j, i + a[pos]), c)][s ^ bit[pos]] += dp[i][j][s] / (c / n); } ans += dp[c][c][0]; return; } for (int i = 1; i <= n - 1; i++) if (!vis[i]) { f[pos] = i; h[i] = pos; vis[i] = true; work(pos + 1); vis[i] = false; } } int main() { read(n), read(c), c *= n; for (int i = 1; i <= n; i++) read(a[i]), a[i] *= n; for (int i = 2; i <= n; i++) bit[i] = 1 << (i - 2); sort(a + 1, a + n + 1, [&] (int x, int y) {return x > y; }); work(2), printf("%.15lf\n", ans / cnt); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 5050; int n, m, cur, t; double p, dp[2][maxn], ans; int main() { scanf("%d%d", &n, &m); dp[0][0] = 1.0; for (int i = 1; i <= n; i++) { cur ^= 1; memset(dp[cur], 0, sizeof(dp[cur])); scanf("%lf%d", &p, &t); p /= 100; double tmp = 0; for (int j = i; j <= m; j++) { tmp *= (1.0 - p); tmp += dp[cur ^ 1][j - 1]; if (j - t - 1 >= 0) tmp -= dp[cur ^ 1][j - t - 1] * pow(1 - p, t); dp[cur][j] += tmp * p; if (j - t >= 0) dp[cur][j] += dp[cur ^ 1][j - t] * pow(1 - p, t); ans += dp[cur][j]; } } printf("%.6lf", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; set<int> factors(long long n) { set<int> fs; for (int i = sqrt(n) + 1 - 1; i >= 1; i--) if (n % i == 0) { fs.insert(-i); fs.insert(-n / i); } return fs; } int main() { ios::sync_with_stdio(false); long long a, b; cin >> a >> b; set<int> fa = factors(a), fb = factors(b); long long s = a + b; for (int i = sqrt(s) + 1 - 1; i >= 1; i--) { if (s % i == 0) { auto ai = fa.lower_bound(-i); auto bi = fb.lower_bound(-i); if ((ai != fa.end() && a / -*ai <= s / i) || (bi != fb.end() && b / -*bi <= s / i)) { cout << 2 * (i + s / i) << endl; return 0; } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long pow_mod(long long x, int k) { long long ans = 1; while (k) { if (k & 1) ans = ans * x % 998244353; x = x * x % 998244353; k >>= 1; } return ans; } long long facd[100005], facv[100005]; void pre(int n) { facd[0] = 1; for (int i = 1; i <= n; i++) facd[i] = facd[i - 1] * i % 998244353; facv[n] = pow_mod(facd[n], 998244353 - 2); for (int i = n - 1; i >= 0; i--) facv[i] = facv[i + 1] * (i + 1) % 998244353; } struct Edge { int t, next; Edge() {} Edge(int a, int b) : t(a), next(b) {} }; Edge e[200005]; int head[100005]; namespace Poly { void rev(long long *p, int len) { int j = len >> 1; for (int i = 1; i < len - 1; i++) { if (i < j) swap(p[i], p[j]); int k = len >> 1; while (j >= k) { j -= k; k >>= 1; } if (j < k) j += k; } } void ntt(long long *p, int len, int check) { rev(p, len); for (int i = 2; i <= len; i <<= 1) { long long wn = pow_mod(3, (998244353 - 1) / i); for (int j = 0; j < len; j += i) { long long w = 1; for (int k = j; k < j + (i >> 1); k++) { long long u = p[k]; long long v = w * p[k + (i >> 1)] % 998244353; p[k] = (u + v) % 998244353; p[k + (i >> 1)] = (u - v + 998244353) % 998244353; w = w * wn % 998244353; } } } if (check == -1) { reverse(p + 1, p + len); long long nev = pow_mod(len, 998244353 - 2); for (int i = 0; i < len; i++) p[i] = p[i] * nev % 998244353; } } void mul(vector<int> &a, vector<int> &b, vector<int> &c) { static long long t1[1 << 18], t2[1 << 18]; int len = 1; while (len < a.size() + b.size()) len <<= 1; for (int i = 0; i < a.size(); i++) t1[i] = a[i]; for (int i = a.size(); i < len; i++) t1[i] = 0; for (int i = 0; i < b.size(); i++) t2[i] = b[i]; for (int i = b.size(); i < len; i++) t2[i] = 0; ntt(t1, len, 1); ntt(t2, len, 1); for (int i = 0; i < len; i++) t1[i] = t1[i] * t2[i] % 998244353; ntt(t1, len, -1); c.resize(a.size() + b.size() - 1); for (int i = 0; i < c.size(); i++) c[i] = t1[i]; } void add(vector<int> &a, vector<int> &b, vector<int> &c) { c.resize(max(a.size(), b.size())); for (int i = 0; i < c.size(); i++) { c[i] = 0; if (i < a.size()) c[i] = (c[i] + a[i]) % 998244353; if (i < b.size()) c[i] = (c[i] + b[i]) % 998244353; } } void right(vector<int> &a, vector<int> &b) { b.resize(a.size() + 1); for (int i = 1; i < a.size(); i++) b[i + 1] = a[i]; b[0] = 0; b[1] = 1; } } // namespace Poly vector<int> f[100005], s1[400000], s2[400000]; vector<int> ans[100005]; void solve(int l, int r, int o) { static vector<int> t1; if (l == r) s1[o] = s2[o] = f[l]; else { int m = ((l + r) >> 1); solve(l, m, o * 2); solve(m + 1, r, o * 2 + 1); Poly::mul(s1[o * 2], s1[o * 2 + 1], s1[o]); Poly::mul(s1[o * 2], s2[o * 2 + 1], t1); Poly::add(s2[o * 2], t1, s2[o]); } } namespace HLD { int fa[100005], size[100005], son[100005]; void calc(int x) { int tot = 0; for (int i = x; i; i = son[i]) f[++tot] = ans[i]; solve(1, tot, 1); ans[x] = s2[1]; } void dfs1(int x) { size[x] = 1; for (int i = head[x]; i; i = e[i].next) if (e[i].t != fa[x]) { int u = e[i].t; fa[u] = x; dfs1(u); size[x] += size[u]; if (size[u] > size[son[x]]) son[x] = u; } } void dfs2(int x, int anc) { if (son[x]) dfs2(son[x], anc); for (int i = head[x]; i; i = e[i].next) if (e[i].t != fa[x] && e[i].t != son[x]) { dfs2(e[i].t, e[i].t); Poly::right(ans[e[i].t], ans[x]); } if (!ans[x].size()) { ans[x].resize(2); ans[x][0] = 0; ans[x][1] = 1; } if (x == anc) calc(x); } } // namespace HLD long long C(vector<int> &a, long long fir, int n) { fir %= 998244353; long long s = 1, ans = 0; for (int i = 1; i <= n; i++) { ans = (ans + s * facv[i - 1] % 998244353 * a[i]) % 998244353; s = s * (fir + i) % 998244353; } return ans; } int main() { int n; long long fir = 0; scanf("%d%lld", &n, &fir); pre(n); for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); e[2 * i - 1] = Edge(y, head[x]); head[x] = 2 * i - 1; e[2 * i] = Edge(x, head[y]); head[y] = 2 * i; } HLD::dfs1(1); HLD::dfs2(1, 1); long long sum = C(ans[1], fir, n); printf("%lld\n", sum); return 0; }
6
#include <bits/stdc++.h> using namespace std; struct node { long long l, r; mutable long long v; node() {} node(long long _l, long long _r, long long _v) : l(_l), r(_r), v(_v) {} const bool operator<(const node& x) const { return l < x.l; } }; set<node> s; long long n, m, seed, vmax, op, l, r, x, y; long long a[100005]; long long rnd(long long mod) { long long ret = seed; seed = (seed * 7 + 13) % 1000000007; return ret % mod + 1; } long long fpow(long long a, long long b, long long mod) { long long res = 1, ans = a % mod; while (b) { if (b & 1) res = res * ans % mod; ans = ans * ans % mod; b >>= 1; } return res; } set<node>::iterator split(long long pos) { set<node>::iterator it = s.lower_bound(node(pos, 0, 0)); if (it != s.end() && it->l == pos) return it; it--; long long l = it->l, r = it->r, v = it->v; s.erase(it); s.insert(node(l, pos - 1, v)); return s.insert(node(pos, r, v)).first; } void assign(long long l, long long r, long long v) { set<node>::iterator itr = split(r + 1), itl = split(l); s.erase(itl, itr); s.insert(node(l, r, v)); } void add(long long l, long long r, long long x) { set<node>::iterator itr = split(r + 1), itl = split(l); for (; itl != itr; itl++) itl->v += x; } long long kth(long long l, long long r, long long k) { vector<pair<long long, long long> > vec; set<node>::iterator itr = split(r + 1), itl = split(l); for (; itl != itr; itl++) vec.push_back(make_pair(itl->v, itl->r - itl->l + 1)); sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { k -= vec[i].second; if (k <= 0) return vec[i].first; } } long long sum(long long l, long long r, long long x, long long y) { long long res = 0; set<node>::iterator itr = split(r + 1), itl = split(l); for (; itl != itr; itl++) res = (res + (itl->r - itl->l + 1) % y * fpow(itl->v, x, y) % y) % y; return res; } int main() { scanf("%lld%lld%lld%lld", &n, &m, &seed, &vmax); for (int i = 1; i <= n; i++) { a[i] = rnd(vmax); s.insert(node(i, i, a[i])); } for (int i = 1; i <= m; i++) { op = rnd(4), l = rnd(n), r = rnd(n); if (l > r) swap(l, r); if (op == 3) x = rnd(r - l + 1); else x = rnd(vmax); if (op == 4) y = rnd(vmax); switch (op) { case 1: add(l, r, x); break; case 2: assign(l, r, x); break; case 3: printf("%lld\n", kth(l, r, x)); break; case 4: printf("%lld\n", sum(l, r, x, y)); break; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int dx[4] = {-1, 0, 0, +1}; long long int dy[4] = {0, -1, +1, 0}; const long long int LINF = 1e18; const long long int INF = 1e9; const long long int mod = 1e9 + 7; long long int power(long long int a, long long int b, long long int m) { if (b == 0) return 1; if (b == 1) return a % m; long long int t = power(a, b / 2, m); t = (t * t) % m; if (b & 1) t = (t * a) % m; return t; } long long int modInverse(long long int a, long long int m) { return power(a, m - 2, m); } vector<long long int> G[200001]; set<long long int> used; set<pair<long long int, long long int>, greater<pair<long long int, long long int> > > leaves; set<long long int> visited; long long int ans = 0, n; map<long long int, long long int> below; void DFS(long long int cur, long long int dep) { visited.insert(cur); leaves.insert(make_pair(dep - below[cur], cur)); long long int sz = G[cur].size(); for (long long int i = 0; i <= sz - 1; i++) { if (visited.find(G[cur][i]) == visited.end()) { DFS(G[cur][i], dep + 1); } } } void DFS1(long long int cur, long long int dep) { visited.insert(cur); long long int sz = G[cur].size(); ans += dep; for (long long int i = 0; i <= sz - 1; i++) { if (visited.find(G[cur][i]) == visited.end()) { if (used.find(cur) != used.end()) DFS(G[cur][i], dep); else DFS(G[cur][i], dep + 1); } } } long long int DFSS(long long int cur) { visited.insert(cur); if (G[cur].size() == 0 || (G[cur].size() == 1 && G[cur][0] == cur)) { return 0; } long long int sz = G[cur].size(), bel = 0; for (long long int i = 0; i <= sz - 1; i++) { if (visited.find(G[cur][i]) == visited.end()) { long long int val = DFSS(G[cur][i]); bel += (val + 1); } } below[cur] = bel; return bel; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; while (t--) { long long int k, i, j; cin >> n >> k; for (long long int i = 1; i <= n - 1; i++) { long long int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } DFSS(1); visited.clear(); DFS(1, 0); set<pair<long long int, long long int>, greater<pair<long long int, long long int> > >::iterator it; it = leaves.begin(); while (k > 0) { ans += it->first; k--; it++; } cout << ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; struct triple { int a, b, c; triple() : a(0), b(0), c(0) {} triple(int a, int b, int c) : a(a), b(b), c(c) {} }; template <typename Comparer> bool gen(const vector<triple>& v, Comparer comp, int pos, vector<string>& ans, triple& val) { if (pos == v.size()) return comp(ans, val); ans[pos] = "LM"; val.a += v[pos].a; val.b += v[pos].b; if (!gen(v, comp, pos + 1, ans, val)) return false; val.a -= v[pos].a; val.b -= v[pos].b; ans[pos] = "LW"; val.a += v[pos].a; val.c += v[pos].c; if (!gen(v, comp, pos + 1, ans, val)) return false; val.a -= v[pos].a; val.c -= v[pos].c; ans[pos] = "MW"; val.b += v[pos].b; val.c += v[pos].c; if (!gen(v, comp, pos + 1, ans, val)) return false; val.b -= v[pos].b; val.c -= v[pos].c; return true; } template <typename Comparer> void launch_gen(const vector<triple>& v, Comparer comp) { vector<string> ans(v.size()); triple val; gen(v, comp, 0, ans, val); } map<pair<int, int>, pair<int, vector<string> > > m; int best = -1234567890; vector<string> res = {"Impossible"}; bool map_putter(const vector<string>& ans, const triple& val) { pair<int, int> p(val.a - val.b, val.b - val.c); if (!m.count(p) || val.c > m[p].first) m[p] = make_pair(val.c, ans); return true; } bool ans_seeker(const vector<string>& ans, const triple& val) { pair<int, int> p(val.b - val.a, val.c - val.b); if (m.count(p)) if (m[p].first + val.c > best) { best = m[p].first + val.c; res.clear(); res.insert(res.end(), m[p].second.begin(), m[p].second.end()); res.insert(res.end(), ans.rbegin(), ans.rend()); } return true; } int main() { int n; cin >> n; vector<triple> v1, v2; for (int i = 0; i < n; i++) { triple t; cin >> t.a >> t.b >> t.c; ((i < (n / 2)) ? v1 : v2).push_back(t); } reverse(v2.begin(), v2.end()); launch_gen(v1, map_putter); launch_gen(v2, ans_seeker); for (const string& it : res) cout << it << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int MAX = 100; const int64 INF = numeric_limits<int64>::max(); int64 grid[MAX][MAX]; int64 dp[MAX][MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tc; cin >> tc; for (int _ = 0; _ < tc; _++) { int n, m; cin >> n >> m; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) { cin >> grid[x][y]; } int64 ans = INF; for (int bx = 0; bx < n; bx++) for (int by = 0; by < m; by++) { int64 base = grid[bx][by] - bx - by; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) { dp[x][y] = INF; int64 delta = grid[x][y] - (base + x + y); if (delta < 0) { continue; } if (x == 0 and y == 0) { dp[x][y] = delta; } if (x > 0 and dp[x - 1][y] != INF) { dp[x][y] = min(dp[x][y], dp[x - 1][y] + delta); } if (y > 0 and dp[x][y - 1] != INF) { dp[x][y] = min(dp[x][y], dp[x][y - 1] + delta); } } ans = min(ans, dp[n - 1][m - 1]); } cout << ans << '\n'; } }
6
#include <bits/stdc++.h> using namespace std; int kmpS[53], kmpT[53], nextS[53][26], nextT[53][26], dp[1003][53][53], n, m, k; char c[1003], s[53], t[53]; void BuildKMP(char s[], int lengthh, int kmp[], int nextt[][26]) { kmp[1] = 0; int cur; for (int i = 2; i <= lengthh; ++i) { cur = kmp[i - 1]; while (cur > 0 && s[cur + 1] != s[i]) { cur = kmp[cur]; } if (s[cur + 1] == s[i]) { kmp[i] = cur + 1; } else { kmp[i] = 0; } } for (int i = 0; i <= lengthh; ++i) { for (char x = 'a'; x <= 'z'; ++x) { cur = i; while (cur > 0 && s[cur + 1] != x) { cur = kmp[cur]; } if (s[cur + 1] == x) { ++cur; } nextt[i][x - 'a'] = cur; } } } void DP() { for (int i = 0; i <= k; ++i) { for (int j = 0; j <= n; ++j) { for (int z = 0; z <= m; ++z) { dp[i][j][z] = -1e9 - 7; } } } dp[0][0][0] = 0; for (int i = 1; i <= k; ++i) { for (int j = 0; j <= n; ++j) { for (int z = 0; z <= m; ++z) { for (char x = 'a'; x <= 'z'; ++x) { if (c[i] == '*' || c[i] == x) { int curS = nextS[j][x - 'a'], curT = nextT[z][x - 'a']; dp[i][curS][curT] = max(dp[i][curS][curT], (dp[i - 1][j][z] + (curS == n) - (curT == m))); } } } } } int maxx = -1e9 - 7; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= m; ++j) { maxx = max(maxx, dp[k][i][j]); } } printf("%d", maxx); } int main() { scanf("%s%s%s", c + 1, s + 1, t + 1); n = strlen(s + 1), m = strlen(t + 1), k = strlen(c + 1); BuildKMP(s, n, kmpS, nextS); BuildKMP(t, m, kmpT, nextT); DP(); return 0; }
4
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n) for(int i=0;i<n;++i) #define REP1(i,n) for(int i=1;i<=n;++i) #define SZ(i) int(i.size()) #ifdef tmd #define IOS() #define debug(...) fprintf(stderr,"#%d: %s = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__); template<typename T> void _do(T &&x){cerr<<x<<endl;} template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);} template<typename It> ostream& _printRng(ostream &os,It bg,It ed) { os<<"{"; for(It it=bg;it!=ed;it++) { os<<(it==bg?"":",")<<*it; } os<<"}"; return os; } template<typename T> ostream &operator << (ostream &os,vector<T> &v){return _printRng(os,v.begin(), v.end());} template<typename T> void pary(T bg, T ed){_printRng(cerr,bg,ed);cerr<<endl;} #else #define IOS() ios_base::sync_with_stdio(0);cin.tie(0); #define endl '\n' #define debug(...) #define pary(...) #endif const int MAXN = 51; const ll MOD = 1000000007; int w, d, n; int ans[MAXN][MAXN]; typedef pair<int,int> pii; struct BFS { const int shift = 200; vector<pii> data[405]; void add (int hei, pii cell) { data[hei+shift].emplace_back(cell); } vector<pii> get(int hei) { return data[hei+shift]; } } bfs; struct Walker { const int sz = 4; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; bool validPosition (pii cur) { return cur.first >= 1 && cur.first <= w && cur.second >= 1 && cur.second <= d; } vector<pii> validMoves (pii cur) { vector<pii> ret; for (int i=0; i<sz; i++) { pii nw = {cur.first+dx[i], cur.second+dy[i]}; if (validPosition(nw)) { ret.emplace_back(nw); } } return ret; } } walker; const int INF = 0x3f3f3f3f; /*********************GoodLuck***********************/ int main () { IOS(); memset(ans, INF, sizeof(ans)); cin >> w >> d >> n; REP (i, n) { int hei; pii cell; cin >> cell.first >> cell.second >> hei; bfs.add(hei, cell); ans[cell.first][cell.second] = hei; } for (int i=200; i>= -200; i--) { vector<pii> lst = bfs.get(i); for (auto cur : lst) { vector<pii> valid = walker.validMoves(cur); for (auto nw : valid) { if (ans[nw.first][nw.second] == INF) { bfs.add(i-1, nw); ans[nw.first][nw.second] = i-1; } } } } #ifdef tmd for (int i=1; i<=w; i++) { for (int j=1; j<=d; j++) { cout << ans[i][j] << " \n"[j==d]; } } #endif // tmd int sum = 0; bool bad = false; for (int i=1; i<=w; i++) { for (int j=1; j<=d; j++) { if (i>1 && abs(ans[i][j]-ans[i-1][j])>1) { bad = true; } if (j>1 && abs(ans[i][j]-ans[i][j-1])>1) { bad = true; } sum += ans[i][j]; } } if (bad) { cout << "No" << endl; } else { cout << sum << endl; } }
0
/***************************** ??????????????????????????????dp(?????£????????????) ?????£?????????????¬??????\???????£????????????????????????????????????????????????????????????¨??????1??\????????¬?????§???????????????????????? ????????????3?????§?????°???????????¨????????£????????????n,d???10??\????????£????????§?????? ???????????°???????§??????????????????£??? ????????????????????????????????£????????????(x - j)???????????????????¶??????????????????£?????¨?????? *****************************/ #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define vec vector int main(void) { int n, d, x; cin >> n >> d >> x; vec<vec<int>> p(d, vec<int>(n)); rep(i, d) rep(j, n) cin >> p[i][j]; rep(i, d - 1) { //i??\??????k?????£??????i+1??\???????????????????????§???????????¨????????? dp[k] vec<int> dp(x + 1, 0); rep(j, n) { for (int k = p[i][j]; k <= x; k++) if (dp[k] < dp[k - p[i][j]] + p[i + 1][j]) dp[k] = dp[k - p[i][j]] + p[i + 1][j]; } int p = x; rep(j, x + 1) if (p < dp[j] + (x - j)) p = dp[j] + (x - j); x = p; } cout << x << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int x[2014], n, k, ans = 0; vector<int> a[2014]; int main() { cin >> n >> k; if (k == 1) { cout << n; return 0; } for (int i = 1; i <= n; i++) { x[i] = 1; for (int j = i * 2; j <= n; j += i) { a[j].push_back(i); } } for (int i = 2; i <= k; i++) { for (int j = 2; j <= n; j++) { for (vector<int>::iterator it = a[j].begin(); it != a[j].end(); ++it) { x[(*it)] = (x[(*it)] % 1000000007) + (x[j] % 1000000007); } } } for (int i = 1; i <= n; i++) { ans = (ans % 1000000007) + (x[i] % 1000000007); } cout << ans; }
4
#include <bits/stdc++.h> using namespace std; long long min(long long a, long long b) { return a < b ? a : b; } struct ball { long long x; long long c; bool operator<(const ball& other) const { return x < other.x; } }; const long long infinity = 2000000000; int n; vector<ball> balls; vector<long long> penalties; long long getPenalty(int k) { if (penalties[k] == infinity) { long long result = infinity; if (k == 1) { result = balls[0].c; } else { long long distance_penalty = 0; for (int i = k - 1; i >= 0; i--) { distance_penalty += (k - i - 1) * (balls[i + 1].x - balls[i].x); result = min(result, distance_penalty + getPenalty(i)); } } penalties[k] = result + balls[k].c; } return penalties[k]; } int main() { cin >> n; for (int i = 0; i < n; i++) { ball temp; cin >> temp.x >> temp.c; balls.push_back(temp); } sort(balls.begin(), balls.end()); ball last; last.x = infinity; last.c = 0; balls.push_back(last); penalties.resize(n + 1, infinity); penalties[0] = balls[0].c; cout << getPenalty(n) << endl; return 0; }
5
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int maxn = 1e6 + 10, mod = 1e9 + 7, mod2 = 2e9 + 7; long long qsm(long long a, long long b, long long mod) { long long ans = 1; for (; b; b >>= 1) { if (b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } int n, p; long long a[maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); int _t; cin >> _t; while (_t--) { cin >> n >> p; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n, greater<long long>()); long long ans1 = 0, ans2 = 0; for (int i = 1; i <= n; i++) { if (ans1 == 0 and ans2 == 0) { ans1 = (ans1 + qsm(p, a[i], mod)) % mod; ans2 = (ans2 + qsm(p, a[i], mod2)) % mod2; } else { ans1 = (ans1 - qsm(p, a[i], mod) + mod) % mod; ans2 = (ans2 - qsm(p, a[i], mod2) + mod2) % mod2; } } cout << ans1 << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxs = 500000, key = 1000003; char dat[maxs + 1]; int calcleft(int n, int m) { int i, j, p; int now, temp; int ans = 1; for (i = 0; i < n; i++) { now = 2; for (p = i * m, j = 0; j < m; j++, p++) if (dat[p] == '1' || dat[p] == '2') { if (j % 2) temp = 1; else temp = 0; if (now != 2 && now != temp) return 0; else now = temp; } else if (dat[p] == '3' || dat[p] == '4') { if (!(j % 2)) temp = 1; else temp = 0; if (now != 2 && now != temp) return 0; else now = temp; } if (now == 2) ans *= 2; ans %= key; } return ans; } int calcup(int n, int m) { int i, j, p; int now, temp; int ans = 1; for (j = 0; j < m; j++) { now = 2; for (p = j, i = 0; i < n; i++, p += m) if (dat[p] == '1' || dat[p] == '4') { if (i % 2) temp = 1; else temp = 0; if (now != 2 && now != temp) return 0; else now = temp; } else if (dat[p] == '2' || dat[p] == '3') { if (!(i % 2)) temp = 1; else temp = 0; if (now != 2 && now != temp) return 0; else now = temp; } if (now == 2) ans *= 2; ans %= key; } return ans; } int main() { int n, m; int i, j, p; cin >> n >> m; for (p = 0, i = 0; i < n; i++) { for (j = 0; j < m; j++, p++) cin >> dat[p]; cin.get(); } cout << ((long long)calcleft(n, m) * (long long)calcup(n, m)) % key << endl; return 0; }
3
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #include<iostream> #include<sstream> #include<string> #include<vector> #include<queue> #include<set> #include<map> #include<utility> #include<numeric> #include<algorithm> #include<bitset> #include<complex> #include<stack> using namespace std; typedef long long Int; typedef vector<int> vint; typedef pair<int,int> pint; typedef vector<string> vstring; typedef vector<pint> vpint; typedef stringstream SS; template<class T> void chmin(T &t, T f) { if (t > f) t = f; } template<class T> void chmax(T &t, T f) { if (t < f) t = f; } #define rep(i,n) for(int i=0;i<(n);++i) #define repn(i,m,n) for(int i=(m);i<(n);++i) #define repd(i,n) for(int i=(n)-1;i>=0;--i) #define repnd(i,m,n) for(int i=(n)-1;i>=(m);--i) #define rep0(i,n) for(i=0;i<(n);++i) #define repn0(i,m,n) for(i=(m);i<(n);++i) #define repd0(i,n) for(i=(n)-1;i>=0;--i) #define repnd0(i,m,n) for(i=(n)-1;i>=(m);--i) #define repc(i,n) for(int i=0;i<=(n);++i) #define repcn(i,m,n) for(int i=(m);i<=(n);++i) #define repcd(i,n) for(int i=(n);i>=0;--i) #define repcnd(i,m,n) for(int i=(n);i>=(m);--i) #define repc0(i,n) for(i=0;i<=(n);++i) #define repcn0(i,m,n) for(i=(m);i<=(n);++i) #define repcd0(i,n) for(i=(n);i>=0;--i) #define repcnd0(i,m,n) for(i=(n);i>=(m);--i) #define all(n) n.begin(),n.end() #define sz(n) ((int)(n).size()) #define IL for(;;) #define MP make_pair #define PB push_back #define X second #define Y first #define p_queue(n) priority_queue<n,vector<n>,greater<n> > #define PUTLINE cout<<"LINE:"<<__LINE__<<endl; const int INF = 2147483647/3; const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[]={1,-1,0,0,1,1,-1,-1,0}; const int dy[]={0,0,1,-1,1,-1,1,-1,0}; Int gcd(Int a, Int b) { return (b == 0) ? a : gcd(b, a%b); } int main() { Int n, a, b, g, res; IL { cin >> n >> a >> b; if (n == 0 && a == 0 && b == 0) break; g = gcd(a, b); res = 0; if (g > 1) { res += n - n / g; n /= g; a /= g; b /= g; } repn (i, 1, a) { rep (j, a) { if (b * j % a == i) { if (b * j > n) res += n / a + (i <= n % a ? 1 : 0); else res += b * j / a; break; } } } cout << res << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; const int N = 100; char str[N]; int n; char a[4]; int Ch(char c) { if ('a' <= c && c <= 'z') return c - 'a' + 1; if ('A' <= c && c <= 'Z') return c - 'A' + 1; return 0; } int main() { a[0] = '@', a[1] = '[', a[2] = '`', a[3] = '{'; scanf("%s", str); n = strlen(str); int ret = 0; for (int i = 0; i < n; i++) { int x = ((a[0] < str[i]) && (a[1] > str[i])) * (int)Ch(str[i]); int y = ((a[2] < str[i]) && (a[3] > str[i])) * (int)Ch(str[i]); ret += x - y; } printf("%d\n", ret); return 0; }
3
#include <bits/stdc++.h> using namespace std; bool is_upper(long long int x0, long long int y0, long long int x1, long long int y1, long long int X, long long int Y) { return (y1 - y0) * X + (x1 - x0) * y0 - (y1 - y0) * x0 < (x1 - x0) * Y; } bool is_lower(long long int x0, long long int y0, long long int x1, long long int y1, long long int X, long long int Y) { return (x1 - x0) * Y < (y1 - y0) * X + (x1 - x0) * y0 - (y1 - y0) * x0; } int main() { long long int x[3][50005], y[3][50005], n[3], stage; long long int p[3] = {0, 0, 0}, cnt[3] = {0, 0, 0}; vector<long long int> vx, vy; for (long long int i = 0; i < ((long long int)(3)); i++) { cin >> n[i]; for (long long int j = 0; j < ((long long int)(n[i])); j++) cin >> x[i][j] >> y[i][j]; } for (long long int i = 0; i < ((long long int)(3)); i++) for (long long int j = 0; j < ((long long int)(n[i])); j++) { double d0 = atan2(y[i][(j + 1) % n[i]] - y[i][j], x[i][(j + 1) % n[i]] - x[i][j]); double d1 = atan2(y[i][p[i] + 1] - y[i][p[i]], x[i][p[i] + 1] - x[i][p[i]]); if (d0 < d1) p[i] = j; } vx.push_back(x[0][p[0]] + x[1][p[1]] + x[2][p[2]]); vy.push_back(y[0][p[0]] + y[1][p[1]] + y[2][p[2]]); while (n[0] != cnt[0] || n[1] != cnt[1] || n[2] != cnt[2]) { long long int t = -1; for (long long int i = 0; i < ((long long int)(3)); i++) if (n[i] != cnt[i]) { if (t == -1) { t = i; continue; } double d0 = atan2(y[i][(p[i] + 1) % n[i]] - y[i][p[i]], x[i][(p[i] + 1) % n[i]] - x[i][p[i]]); double d1 = atan2(y[t][(p[t] + 1) % n[t]] - y[t][p[t]], x[t][(p[t] + 1) % n[t]] - x[t][p[t]]); if (d0 < d1) t = i; } p[t] = (p[t] + 1) % n[t]; cnt[t]++; vx.push_back(x[0][p[0]] + x[1][p[1]] + x[2][p[2]]); vy.push_back(y[0][p[0]] + y[1][p[1]] + y[2][p[2]]); } long long int m0 = 0, m1 = 0; for (long long int i = 0; i < ((long long int)(((long long int)vx.size()))); i++) { if (vx[i] < vx[m0]) m0 = i; if (vx[m1] < vx[i]) m1 = i; } vector<pair<long long int, long long int> > _up, _down, up, down; for (long long int i = 0; i < ((long long int)(((long long int)vx.size()))); i++) { if (!is_upper(vx[m0], vy[m0], vx[m1], vy[m1], vx[i], vy[i])) _down.push_back(make_pair(vx[i], vy[i])); if (!is_lower(vx[m0], vy[m0], vx[m1], vy[m1], vx[i], vy[i])) _up.push_back(make_pair(vx[i], vy[i])); } sort(_up.begin(), _up.end()); sort(_down.begin(), _down.end()); for (long long int i = 0; i < ((long long int)(((long long int)_up.size()))); i++) { if (((long long int)up.size()) == 0 || up.back().first != _up[i].first) up.push_back(_up[i]); else up.back().second = max(up.back().second, _up[i].second); } for (long long int i = 0; i < ((long long int)(((long long int)_down.size()))); i++) { if (((long long int)down.size()) == 0 || down.back().first != _down[i].first) down.push_back(_down[i]); else down.back().second = min(down.back().second, _down[i].second); } for (cin >> stage; stage--;) { long long int xx, yy; cin >> xx >> yy; xx *= 3; yy *= 3; vector<pair<long long int, long long int> >::iterator u1, u0, d1, d0; u0 = upper_bound(up.begin(), up.end(), make_pair(xx, (1ll << (long long int)(60)))); d0 = upper_bound(down.begin(), down.end(), make_pair(xx, (1ll << (long long int)(60)))); if (u0 == up.begin() || d0 == down.begin()) { cout << "NO" << endl; continue; } u1 = u0--; d1 = d0--; if (u1 == up.end()) { cout << ((xx == u0->first && d0->second <= yy && yy <= u0->second) ? "YES" : "NO") << endl; continue; } bool ok = true; if (is_upper(u0->first, u0->second, u1->first, u1->second, xx, yy)) ok = false; if (is_lower(d0->first, d0->second, d1->first, d1->second, xx, yy)) ok = false; cout << ((ok) ? "YES" : "NO") << endl; } }
5
#include <bits/stdc++.h> using namespace std; int main() { string x; cin >> x; int c1 = 0, c0 = 0; bool f = 0; for (int i = 0; i < x.size(); i++) { if (x[i] == '0') { c0++; } else if (x[i] == '1') { c1++; } if (c0 < 7 && x[i] == '1') { c0 = 0; } if (c1 < 7 && x[i] == '0') { c1 = 0; } } if (c1 >= 7 || c0 >= 7) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
1
#include <bits/stdc++.h> using pii = std::pair<long long, long long>; using namespace std; const long long maxm = 20; long long n, m, cnt[maxm][maxm], dp[(1 << maxm) + 5] = {0}; string s; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> s; for (long long i = 0; i < n - 1; i++) { cnt[s[i] - 'a'][s[i + 1] - 'a']++; cnt[s[i + 1] - 'a'][s[i] - 'a']++; } dp[0] = 0; for (long long i = 1; i < (1 << m); i++) { long long len = __builtin_popcount(i); dp[i] = 2e18; for (long long j = 0; j < m; j++) if (i & (1 << j)) { long long cur = 0; for (long long k = 0; k < m; k++) { if (k == j) continue; if (i & (1 << k)) cur += len * cnt[j][k]; else cur -= len * cnt[j][k]; } dp[i] = min(dp[i], cur + dp[i ^ (1 << j)]); } } cout << dp[(1 << m) - 1] << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; #define REP(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i) #define debug(...) fprintf(stderr, __VA_ARGS__) #define mp make_pair #define x first #define y second #define pb push_back #define SZ(x) (int((x).size())) #define ALL(x) (x).begin(), (x).end() template<typename T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } typedef long long LL; const int oo = 0x3f3f3f3f; const int Mod = 1e9 + 7; const int maxn = 410; int n; int a[maxn + 5], b[maxn + 5]; int dp[2][maxn + 5][maxn + 5][2]; int main() { #ifdef matthew99 freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif scanf("%d", &n); REP(i, 0, n) scanf("%d", a + i), --a[i]; REP(i, 0, n) scanf("%d", b + i), --b[i]; if (a[0] == b[0]) { printf("0\n"); return 0; } dp[0][0][0][0] = 1; static bool visa[maxn + 5]; static bool visb[maxn + 5]; memset(visa, 0, sizeof visa); REP(i, 0, n + 1) { int cur = i & 1, nxt = !cur; memset(dp[nxt], 0, sizeof dp[nxt]); memset(visb, 0, sizeof visb); REP(j, 0, n + 1) { REP(k, 0, n + 1) REP(l, 0, 2) if (dp[cur][j][k][l]) { if (l == 0) { if (i < n && visb[a[i]]) (dp[nxt][j][k][l] += dp[cur][j][k][l]) %= Mod; else { if (i < n && k) (dp[nxt][j][k - 1][l] += (LL)k * dp[cur][j][k][l] % Mod) %= Mod; (dp[cur][j][k][1] += dp[cur][j][k][l]) %= Mod; } } else { if (j < n && visa[b[j]]) (dp[cur][j + 1][k][l] += dp[cur][j][k][l]) %= Mod; else { if (i < n && j < n && a[i] == b[j]) continue; if (j < n && k) { (dp[cur][j + 1][k - 1][l] += (LL)k * dp[cur][j][k][l] % Mod) %= Mod; } if (i < n && j < n && a[i] != b[j]) { (dp[nxt][j + 1][k + 1][0] += dp[cur][j][k][l]) %= Mod; } } } } if (j < n) visb[b[j]] = 1; } if (i < n) visa[a[i]] = 1; } int m = n / 3; int ans = dp[n & 1][n][0][1]; int cnt = 0; REP(i, 0, m) { ans = (LL)ans * (cnt + 1) % Mod; ans = (LL)ans * (cnt + 2) % Mod; cnt += 3; } (ans += Mod) %= Mod; printf("%d\n", ans); return 0; }
0
#include <bits/stdc++.h> #include <cstdlib> using namespace std; typedef long long ll; bool isPrime(long long int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (long long int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } unsigned int countBits(unsigned int n) { unsigned int count = 0; while (n) { count++; n >>= 1; } return count; } bool isPerfectSquare(long double x) { // Find floating point value of // square root of x. long double sr = sqrt(x); // If square root is an integer return ((sr - floor(sr)) == 0); } int subArraySum(int arr[], int n) { /* Initialize curr_sum as value of first element and starting point as 0 */ int curr_sum = arr[0], start = 0, i,count=0,len; len=0; /* Add elements one by one to curr_sum and if the curr_sum exceeds the sum, then remove starting element */ for (i = 1; i <= n; i++) { // If curr_sum exceeds the sum, // then remove the starting elements len++; while (curr_sum > len && start < i - 1) { curr_sum = curr_sum - arr[start]; start++; len++; } // If curr_sum becomes equal to sum, // then return true if (curr_sum == len) { count++; } // Add this element to curr_sum if (i < n && i>0) { curr_sum = curr_sum + arr[i]; } } // If we reach here, then no subarray return count; } void printDigit(int N, vector<long long int> &arr) { // To store the digit // of the number N int i = 0; int j, r; // Till N becomes 0 while (N != 0) { // Extract the last digit of N r = N % 10; // Put the digit in arr[] arr.push_back(r); i++; // Update N to N/10 to extract // next last digit N = N / 10; } // Print the digit of N by traversing // arr[] reverse // for (j = i - 1; j > -1; j--) { // printf("%d ", arr[j]); // } } // void solver1(vector<long long int> arr, long long int start, long long int& count, long long int a1){ // long long int sum=0; // for(long long int i=start+1;i<arr.size();i++){ // sum+=arr. Find the minimum integer [i]; // if(sum==a1 && i==arr.size()-1) // count++; // } // } // void solver(vector<long long int> arr, long long int start, long long int& count, long long int a1){ // long long int sum=0; // for(long long int i=start+1;i<arr.size()-1;i++){ // sum+=arr[i]; // if(sum==a1) // count++; // } // } bool checkDivisibility(long long int n, int digit) { // If the digit divides the number // then return true else return false. if(digit==0) return true; return (digit != 0 && n % digit == 0); } // Function to check if all digits // of n divide it or not bool allDigitsDivide( long long int n) { long long int temp = n; while (temp > 0) { // Taking the digit of the // number into digit var. int digit = temp % 10; if (!(checkDivisibility(n, digit))) return false; temp /= 10; } return true; } bool isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } bool isPowerOfTwo (unsigned long long x) { /* First x in the below expression is for the case when x is 0 */ return x && (!(x&(x-1))); } // Function to return the smallest // prime number greater than N int nextPrime(int N) { // Base case if (N <= 1) return 2; int prime = N; bool found = false; // Loop continuously until isPrime returns // true for a number greater than n while (!found) { prime++; if (isPrime(prime)) found = true; } return prime; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s; cin>>s; while(getline(cin,s)){ if(s.length()==0) break; else cout<<"NO"<<endl; } //} return 0; // 0 1 2 4 5 6 7 // 1 3 1 2 1 1 1 }
1
#include <bits/stdc++.h> using namespace std; long long dp[55][55][55][55]; long long ara[55]; double f[55]; long long cnt(long long i, long long k, long long sum, long long zawad) { if (i == 0) return (k == 0 && sum == 0); if (k > i || sum < 0) return 0; if (dp[i][k][sum][zawad] != -1) return dp[i][k][sum][zawad]; dp[i][k][sum][zawad] = cnt(i - 1, k, sum, zawad); if (i != zawad) dp[i][k][sum][zawad] += cnt(i - 1, k - 1, sum - ara[i], zawad); return dp[i][k][sum][zawad]; } int main() { long long n, i, j, k, p, q, x, y; cin >> n; long long s = 0; for (i = 1; i <= n; i++) { cin >> ara[i]; s += ara[i]; } cin >> p; if (s <= p) { printf("%lld\n", n); return 0; } f[1] = 1.0 / n; for (i = 2; i <= n; i++) f[i] = f[i - 1] * (i - 1) / (n - i + 1.0); memset(dp, -1, sizeof(dp)); double ans = 0; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { for (k = p - ara[i] + 1; k <= p; k++) { q = cnt(n, j - 1, k, i); ans += q * (j - 1) * f[j]; } } } printf("%.10lf\n", ans); return 0; }
4
#include <bits/stdc++.h> int main() { int n, count = 0, i; scanf("%d\n", &n); char str[n]; gets(str); for (i = 0; i < n - 10; i++) { if (str[i] == '8') { count++; } } if (count > ((n - 11) / 2)) { printf("YES"); } else { printf("NO"); } }
2
#include <bits/stdc++.h> using namespace std; int main() { bool f[3]; for (int i = 0; i < 3; ++i) cin >> f[i]; f[2] = !f[2]; cout << (f[0] == f[1] && f[1] == f[2] ? "Open" : "Close") << endl; }
0
#include <iostream> #include <algorithm> using namespace std; bool comp(const int& v1, const int& v2) { return v1 > v2; } int main() { bool pr[1000001]; int s, t, a[1001], b[1001], p, q; fill(pr, pr+1000001, true); pr[0] = pr[1] = false; for(int i = 2; i <= 1000000; i++){ if(pr[i]){ for(int j = 2*i; j <= 1000000; j+=i){ pr[j] = false; } } } while(1){ cin >> s >> t; if(s == 0 && t == 0){ break; } p = q = 0; for(int i = 2; i <= s; i++){ if(pr[i] && s%i == 0){ a[p++] = i; } } for(int i = 2; i <= t; i++){ if(pr[i] && t%i == 0){ b[q++] = i; } } sort(a, a+p, comp); sort(b, b+q, comp); s = a[0]; t = b[0]; for(int i = 1; i < p; i++){ s -= a[i]; } for(int i = 1; i < q; i++){ t -= b[i]; } if(s > t){ cout << "a" << endl; } else { cout << "b" << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; #define x first #define y second #define bg begin() #define ed end() #define pb push_back #define mp make_pair #define sz(a) int((a).size()) #define R(i,n) for(int i(0);i<(n);++i) #define L(i,n) for(int i((n)-1);i>=0;--i) const int iinf=0x3f3f3f3f; const ll linf=0x3f3f3f3f3f3f3f3f;// //Data const int N=2e5,D=5e5;// int n,m,a[N]; ll w[D]; //Main int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n>>m; R(i,n) cin>>a[i]; sort(a,a+n,greater<int>()); ll tot=0; int ns=iinf; w[0]=1,w[1]=-1; for(int d=0,i=0;d<D-3;++d){ w[d+1]+=w[d],tot+=w[d]; if(tot+w[d+1]>=m){ns=d+1; break;} while(i<n&&w[d]){ int l=(a[i]-1)>>1,r=a[i]-1-l; ++i; w[d+2]+=2,--w[d+2+l],--w[d+2+r]; --w[d],--tot; } } if(ns==iinf) cout<<-1<<'\n'; else cout<<ns<<'\n'; return 0; }
6
#include <iostream> using namespace std; int main(){ string s; cin>>s; int K; cin>>K; for(int i=0;i<s.size();i++){ if(s[i] != 'a' && 'z' - s[i] + 1 <= K){ K -= 'z' - s[i] + 1; s[i] = 'a'; } } K = K % 26; while(K > 0){ s[s.size() - 1] = (s[s.size() - 1] - 'a' + 1) % 26 + 'a'; K--; } cout<<s<<endl; }
0
#include <bits/stdc++.h> using namespace std; int m, n; bool vis[401][401]; bool v[401]; struct xx { int num; int cnt; }; queue<xx> q; struct node { int b[401]; int num; } a[401], c[401]; int bfstrain(int n1) { v[1] = 1; xx x1; x1.cnt = 0; x1.num = n1; q.push(x1); xx xxxx, x2; while (!q.empty()) { xxxx = q.front(); q.pop(); if (xxxx.num == n) { return xxxx.cnt; } for (int i = 0; i < a[xxxx.num].num; i++) { if (!v[a[xxxx.num].b[i]]) { x2.cnt = xxxx.cnt + 1; ; x2.num = a[xxxx.num].b[i]; q.push(x2); v[a[xxxx.num].b[i]] = 1; } } } return -1; } int bfsbus(int n1) { v[1] = 1; xx x1; x1.cnt = 0; x1.num = n1; q.push(x1); xx xxxx, x2; while (!q.empty()) { xxxx = q.front(); q.pop(); if (xxxx.num == n) { return xxxx.cnt; } for (int i = 0; i < c[xxxx.num].num; i++) { if (!v[c[xxxx.num].b[i]]) { x2.cnt = xxxx.cnt + 1; x2.num = c[xxxx.num].b[i]; q.push(x2); v[c[xxxx.num].b[i]] = 1; } } } return -1; } int main() { scanf("%d%d", &n, &m); memset(a, 0, sizeof(a)); memset(c, 0, sizeof(c)); memset(v, 0, sizeof(v)); memset(vis, 0, sizeof(vis)); bool is_train = 1; while (m--) { int u, v; scanf("%d%d", &u, &v); a[u].b[a[u].num++] = v; a[v].b[a[v].num++] = u; vis[u][v] = 1; vis[v][u] = 1; if (u == 1 && v == n || u == n && v == 1) is_train = 0; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (vis[i][j] != 1 && vis[j][i] != 1) { c[i].b[c[i].num++] = j; c[j].b[c[j].num++] = i; } } } while (!q.empty()) { q.pop(); } int ans = 0; if (is_train) ans = bfstrain(1); else ans = bfsbus(1); printf("%d\n", ans); return 0; }
1
#include <stdio.h> #include <vector> #include <algorithm> #include <queue> using namespace std; namespace MCF{ const int MAXN=120; const int MAXM=2100; int to[MAXM]; int next[MAXM]; int first[MAXN]; int c[MAXM]; long long w[MAXM]; long long pot[MAXN]; int rev[MAXM]; long long ijk[MAXN]; int v[MAXN]; long long toc; int tof; int n; int m; void init(int _n){ n=_n;for(int i=0;i<n;i++)first[i]=-1; } void ae(int a,int b,int cap,int wei){ next[m]=first[a];to[m]=b;first[a]=m;c[m]=cap;w[m]=wei;m++; next[m]=first[b];to[m]=a;first[b]=m;c[m]=0;w[m]=-wei;m++; } int solve(int s,int t,int flo){ toc=tof=0; for(int i=0;i<n;i++)pot[i]=0; while(tof<flo){ for(int i=0;i<n;i++)ijk[i]=9999999999999LL; for(int i=0;i<n;i++)v[i]=0; priority_queue<pair<long long,int> > Q; ijk[s]=0; Q.push(make_pair(0,s)); while(Q.size()){ long long cost=-Q.top().first; int at=Q.top().second; Q.pop(); if(v[at])continue; v[at]=1; for(int i=first[at];~i;i=next[i]){ int x=to[i]; if(v[x]||ijk[x]<=ijk[at]+w[i]-pot[x]+pot[at])continue; if(c[i]==0)continue; ijk[x]=ijk[at]+w[i]-pot[x]+pot[at]; rev[x]=i; Q.push(make_pair(-ijk[x],x)); } } int flow=flo-tof; if(!v[t])return 0; int at=t; while(at!=s){ flow=min(flow,c[rev[at]]); at=to[rev[at]^1]; } at=t; tof+=flow; toc+=flow*(ijk[t]-pot[s]+pot[t]); at=t; while(at!=s){ c[rev[at]]-=flow; c[rev[at]^1]+=flow; at=to[rev[at]^1]; } for(int i=0;i<n;i++)pot[i]+=ijk[i]; } return 1; } } int main(){ int a,b,c;scanf("%d%d%d",&a,&b,&c); MCF::init(a); for(int i=0;i<b;i++){ int p,q,r,s;scanf("%d%d%d%d",&p,&q,&r,&s); MCF::ae(p,q,r,s); } int res=MCF::solve(0,a-1,c); if(!res)printf("-1\n"); else printf("%lld\n",MCF::toc); }
0
#include <bits/stdc++.h> using namespace std; bool comp(int x, int y) { return x > y; } int n; int temp; map<int, int> m; pair<int, int> arr[3]; priority_queue<pair<int, int> > pq; vector<vector<int> > res; int main() { while (cin >> n) { res.clear(); m.clear(); for (int i = 0; i < n; ++i) { cin >> temp; m[temp]++; } map<int, int>::iterator it; for (it = m.begin(); it != m.end(); it++) { pq.push(make_pair((*it).second, (*it).first)); } while (pq.size() > 2) { vector<int> temp; for (int i = 0; i < 3; ++i) { arr[i] = pq.top(); temp.push_back(arr[i].second); pq.pop(); } sort(temp.begin(), temp.end(), comp); res.push_back(temp); for (int i = 0; i < 3; ++i) { arr[i].first--; if (arr[i].first) pq.push(arr[i]); } } cout << res.size() << '\n'; for (int i = 0; i < res.size(); ++i) { cout << res[i][0]; for (int j = 1; j < 3; ++j) cout << " " << res[i][j]; cout << '\n'; } while (!pq.empty()) pq.pop(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int lcm(long long int a, long long int b) { return (max(a, b) / gcd(a, b)) * min(a, b); } void build() {} long long int modexp(long long int base, long long int exponent, long long int modulus) { if (modulus == 1) return 0; long long int result = 1; base = base % modulus; while (exponent > 0) { if (exponent % 2 == 1) { result = (result * base) % modulus; } exponent = exponent >> 1; base = (base * base) % modulus; } return result; } bool sorte(long long int a, long long int b) { return a > b; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; build(); long long int n, m; cin >> n >> m; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, sorte); long long int low = 1, high = 1000000000; long long int sum = 0, ans = 0; while (low <= high) { long long int mid = low + (high - low) / 2; sum = 0; for (long long int i = 0; i < n; i++) { long long int x = a[i] - i / mid; if (x > 0) sum += x; } if (sum >= m) { ans = mid; high = mid - 1; } else { low = mid + 1; } } if (ans) cout << ans; else cout << -1; }
4
#include <bits/stdc++.h> using namespace std; template <typename T> inline void pv(vector<T> v) { for (T x : v) cout << x << " "; cout << "\n"; } inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * f; } const int N = 1e9 + 7; const int mod = 998244353; vector<int> BIT(200005, 0), ans, b; vector<pair<int, int>> a; vector<pair<pair<int, int>, int>> q; void add(int i, int x) { for (; i < BIT.size(); i += i & (-i)) BIT[i] += x; } int get(int i) { int sum = 0; for (; i > 0; i -= i & (-i)) sum += BIT[i]; return sum; } int bit_search(long long val) { long long sum = 0, pos = 0; for (int i = log2(BIT.size()); i >= 0; i--) if (pos + (1 << i) < BIT.size() && sum + BIT[pos + (1 << i)] < val) sum += BIT[pos + (1 << i)], pos += (1 << i); return pos + 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n = read(), k, pos, m; b.push_back(N); for (int i = 0; i < (n); i++) b.push_back(read()); for (int i = 0; i < (n + 1); i++) a.push_back(make_pair(-b[i], i)); m = read(); for (int i = 0; i < (m); i++) { k = read(), pos = read(); q.push_back(make_pair(make_pair(k, pos), i)); } sort((a).begin(), (a).end()); sort((q).begin(), (q).end()); int ind = 1; ans.resize(m, -1); for (int i = 0; i < (m); i++) { while (q[i].first.first >= ind) add(a[ind].second, 1), ind++; pos = bit_search(q[i].first.second); ans[q[i].second] = pos; } for (int x : ans) cout << b[x] << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 998244353; int n, k, l[10], r[10], a[200001], b[200002] = { 0, 1, -1 }; int main() { cin >> n >> k; for (int i = 0; i < k; ++i) { cin >> l[i] >> r[i]; } for (int i = 1; i < n; ++i) { a[i] = (a[i - 1] + b[i]) % mod; for (int j = 0; j < k; ++j) { int x, y; x = min(n + 1, i + l[j]); y = min(n + 1, i + r[j] + 1); b[x] += a[i]; b[x] %= mod; b[y] -= a[i]; b[y] %= mod; } } a[n] = (a[n - 1] + b[n]) % mod; if (a[n] < 0) a[n] += mod; cout << a[n] << endl; }
0
#include <bits/stdc++.h> using namespace std; bool isPrime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } int main() { long long n, m, r; cin >> n; long long one = 0, two = 0; vector<long long> a(n); for (auto &i : a) { cin >> i; one += (i == 1); two += (i == 2); } vector<long long> ans; long long last = 0; if (two > 0) { two--; ans.push_back(2); } else { for (auto i : a) cout << i << ' '; return 0; } vector<long long> Primes; for (int i = 2; i < 5 * 1e5; i++) if (isPrime(i)) Primes.push_back(i); while (last != n && two + one != 0) { long long need = Primes[last + 1] - Primes[last]; while (two > 0 && need > 1) { two--; ans.push_back(2); need -= 2; } while (need != 0 && one > 0) { one--; ans.push_back(1); need--; } last++; } for (auto i : ans) cout << i << ' '; }
1
#include <bits/stdc++.h> using namespace std; static struct FastInput { static constexpr int BUF_SIZE = 1 << 20; char buf[BUF_SIZE]; size_t chars_read = 0; size_t buf_pos = 0; FILE *in = stdin; char cur = 0; inline char get_char() { if (buf_pos >= chars_read) { chars_read = fread(buf, 1, BUF_SIZE, in); buf_pos = 0; buf[0] = (chars_read == 0 ? -1 : buf[0]); } return cur = buf[buf_pos++]; } inline void tie(int) {} inline explicit operator bool() { return cur != -1; } inline static bool is_blank(char c) { return c <= ' '; } inline bool skip_blanks() { while (is_blank(cur) && cur != -1) { get_char(); } return cur != -1; } inline FastInput& operator>>(char& c) { skip_blanks(); c = cur; return *this; } inline FastInput& operator>>(string& s) { if (skip_blanks()) { s.clear(); do { s += cur; } while (!is_blank(get_char())); } return *this; } template <typename T> inline FastInput& read_integer(T& n) { // unsafe, doesn't check that characters are actually digits n = 0; if (skip_blanks()) { int sign = +1; if (cur == '-') { sign = -1; get_char(); } do { n += n + (n << 3) + cur - '0'; } while (!is_blank(get_char())); n *= sign; } return *this; } template <typename T> inline typename enable_if<is_integral<T>::value, FastInput&>::type operator>>(T& n) { return read_integer(n); } #if !defined(_WIN32) || defined(_WIN64) inline FastInput& operator>>(__int128& n) { return read_integer(n); } #endif template <typename T> inline typename enable_if<is_floating_point<T>::value, FastInput&>::type operator>>(T& n) { // not sure if really fast, for compatibility only n = 0; if (skip_blanks()) { string s; (*this) >> s; sscanf(s.c_str(), "%lf", &n); } return *this; } } fast_input; #define cin fast_input static struct FastOutput { static constexpr int BUF_SIZE = 1 << 20; char buf[BUF_SIZE]; size_t buf_pos = 0; static constexpr int TMP_SIZE = 1 << 20; char tmp[TMP_SIZE]; FILE *out = stdout; inline void put_char(char c) { buf[buf_pos++] = c; if (buf_pos == BUF_SIZE) { fwrite(buf, 1, buf_pos, out); buf_pos = 0; } } ~FastOutput() { fwrite(buf, 1, buf_pos, out); } inline FastOutput& operator<<(char c) { put_char(c); return *this; } inline FastOutput& operator<<(const char* s) { while (*s) { put_char(*s++); } return *this; } inline FastOutput& operator<<(const string& s) { for (int i = 0; i < (int) s.size(); i++) { put_char(s[i]); } return *this; } template <typename T> inline char* integer_to_string(T n) { // beware of TMP_SIZE char* p = tmp + TMP_SIZE - 1; if (n == 0) { *--p = '0'; } else { bool is_negative = false; if (n < 0) { is_negative = true; n = -n; } while (n > 0) { *--p = (char) ('0' + n % 10); n /= 10; } if (is_negative) { *--p = '-'; } } return p; } template <typename T> inline typename enable_if<is_integral<T>::value, char*>::type stringify(T n) { return integer_to_string(n); } #if !defined(_WIN32) || defined(_WIN64) inline char* stringify(__int128 n) { return integer_to_string(n); } #endif template <typename T> inline typename enable_if<is_floating_point<T>::value, char*>::type stringify(T n) { sprintf(tmp, "%.17f", n); return tmp; } template <typename T> inline FastOutput& operator<<(const T& n) { auto p = stringify(n); for (; *p != 0; p++) { put_char(*p); } return *this; } } fast_output; // here puts define typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define rint register int #define rll register ll #define pii pair<int, int> #define pll pair<ll, ll> #define fors(i, a, b) for (ll i = (a); i <= (b); ++i) #define _fors(i, a, b) for (ll i = (a); i >= (b); --i) #define cyc(m) fors(rqtwqtqwt, 1, m) #define mp(a, b) make_pair(a, b) #define mt(a, b, c) make_tuple(a, b, c) #define mem(A, b) memset(A, b, sizeof(A)) #define all(X) (X).begin(), (X).end() #define y0 gawgfawgawg #define y1 sdfyseyegeh #define pb push_back #define eb emplace_back #define cout fast_output #define endl '\n' #define yes cout << "YES" << endl #define no cout << "NO" << endl #define int long long int start_time; const int _ = 105; const int mod = 1e9 + 7; const int inv2 = (mod + 1) / 2; inline int add(int a, int b) { return (((a + b) % mod) + mod) % mod; } inline int mul(int a, int b) { return 1ll * a * b % mod; } int n, q; int b[_], c[_]; int dp[_*_], s[_*_]; int sum[_]; int m; map<int, int> f; void solve() { cin >> n; fors(i, 1, n) cin >> c[i]; fors(i, 1, n - 1) cin >> b[i]; fors(i, 2, n) { sum[i] = sum[i-1]; fors(j, 1, i - 1) sum[i] += b[j]; } // cout << '*' << m << endl; int lef = 0; fors(i, 2, n) lef = min(lef, -sum[i] / i); fors(xx, lef - 1, lef + 100) { dp[0] = 1; fors(i, 0, 10000) s[i] = 1; fors(id, 1, n) { fors(i, 0, 10000) { if (i <= c[id]) dp[i] = s[i]; else dp[i] = add(s[i], -s[i-c[id]-1]); } fors(i, 0, min(10000ll, id * xx + sum[id] - 1)) dp[i] = 0; s[0] = dp[0]; fors(i, 1, 10000) s[i] = add(s[i-1], dp[i]); } f[xx] = s[10000]; } int ans = 1; fors(i, 1, n) ans = mul(ans, c[i] + 1); cin >> q; while (q--) { int xx; cin >> xx; if (xx > lef + 100) cout << 0 << endl; else if (xx < lef - 1) cout << ans << endl; else cout << f[xx] << endl; } return ; } signed main() { #ifdef Sakuyalove freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); start_time = clock(); int T = 1; // cin >> T; while (T--) { solve(); } #ifdef Sakuyalove cout << "time = " << clock() - start_time << endl; #endif return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int a[200][200] = {{1}, {0, 1}}, n; cin >> n; for (int i = 2, j; i <= n; ++i) { a[i][0] = a[i - 2][0]; for (j = 1; j <= i; ++j) a[i][j] = (a[i - 1][j - 1] + a[i - 2][j]) % 2; } cout << n << '\n'; for (int i = 0; i <= n; cout << a[n][i++] << ' ') ; cout << '\n' << --n << '\n'; for (int i = 0; i <= n; cout << a[n][i++] << ' ') ; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXNUM = 500000; const int SM = 300; class Solution { public: Solution() : m(MAXNUM + 1, 0), sm(SM) { for (int i = 0; i < SM; ++i) sm[i].resize(i + 1, 0); } void add(int x, int y) { m[x] += y; for (int i = 1; i <= SM; ++i) sm[i - 1][x % i] += y; } int get(int x, int y) { if (x <= SM) return sm[x - 1][y]; int result = 0; for (int i = y; i <= MAXNUM; i += x) { result += m[i]; } return result; } vector<int> m; vector<vector<int>> sm; }; int main() { int q; cin >> q; Solution s; for (int i = 0; i < q; ++i) { int t, x, y; cin >> t >> x >> y; if (t == 1) { s.add(x, y); } else { int result = s.get(x, y); cout << result << endl; } } return 0; }
6