solution
stringlengths
52
181k
difficulty
int64
0
6
#include <iostream> #include <vector> #include <deque> using namespace std; struct Node { struct Node * plus; struct Node * mul; int num; }; int main() { int K; cin >> K; vector<struct Node> N(K); for(int i = 0; i < K; i++) { N[i].num = i; N[i].plus = &N[(i + 1) % K]; N[i].mul = &N[(i * 10) % K]; } vector<int> C(K, INT_MAX); deque<pair<int , int>> d; d.push_back(make_pair(1, 0)); while(true) { auto x = d.front(); if(x.first == 0) { cout << x.second + 1 << endl; return 0; } d.pop_front(); if(C[x.first] <= x.second) continue; C[x.first] = x.second; d.push_back(make_pair(N[x.first].plus->num, x.second + 1)); d.push_front(make_pair(N[x.first].mul->num, x.second)); } }
0
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { vector<int> Length(3); for (int i = 0; i < 3; ++i) { scanf("%d", &Length[i]); } sort(Length.begin(), Length.end()); printf("%d\n", max(0, Length[2] - (Length[0] + Length[1]) + 1)); return 0; }
1
#include <bits/stdc++.h> using namespace std; struct pt { long long x, y, idx; pt operator+(pt p) { return {x + p.x, y + p.y}; } pt operator-(pt p) { return {x - p.x, y - p.y}; } bool operator==(pt p) { return (idx == p.idx); } }; pt p[12333]; long double dis(pt a, pt b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } int main() { int n, m; cin >> n >> m; int k = 0; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if ((i > 3 and i < n - 3) or (j > 3 and j < m - 3)) continue; p[k] = {i, j, k}; k++; } } long double mx = 0; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { for (int ii = 0; ii < k; ii++) { for (int jj = 0; jj < k; jj++) { if (i == j or i == ii or i == jj or j == ii or j == jj or ii == jj) continue; mx = max(mx, dis(p[i], p[j]) + dis(p[j], p[ii]) + dis(p[ii], p[jj])); } } } } for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { for (int ii = 0; ii < k; ii++) { for (int jj = 0; jj < k; jj++) { if (i == j or i == ii or i == jj or j == ii or j == jj or ii == jj) continue; if ((fabs(mx - (dis(p[i], p[j]) + dis(p[j], p[ii]) + dis(p[ii], p[jj]))) < 1e-6)) { cout << p[i].x << " " << p[i].y << endl; cout << p[j].x << " " << p[j].y << endl; cout << p[ii].x << " " << p[ii].y << endl; cout << p[jj].x << " " << p[jj].y << endl; return 0; } } } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n, 0); for (int i = 0; i < n; i++) { cin >> a[i]; } set<int> s; for (int i = 0; i < n; i++) { s.insert(a[i]); } if (s.size() > 3) { cout << "-1\n"; return 0; } if (s.size() == 1) { cout << "0\n"; return 0; } if (s.size() == 2) { int a, b; a = *s.begin(); b = *s.rbegin(); if ((b - a) % 2 == 0) { cout << (b - a) / 2 << "\n"; } else { cout << b - a; } return 0; } vector<int> ans(3, 0); int ind = 0; for (auto i : s) { ans[ind] = i; ind++; } if (ans[1] - ans[0] != ans[2] - ans[1]) { cout << "-1\n"; return 0; } else { cout << ans[1] - ans[0] << "\n"; return 0; } }
2
#include <bits/stdc++.h> using namespace std; int n, x, s[400], ans; int Cal(int x) { int y = 360 - x; return x > y ? x - y : y - x; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); s[i] = s[i - 1] + x; } ans = 360; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) ans = min(ans, Cal(s[j] - s[i - 1])); printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m; int main() { ios_base::sync_with_stdio(0); int mn = 1e9; cin >> n >> m; for (int i = 0; i < m; i++) { int l, r; cin >> l >> r; mn = min(mn, r - l + 1); } int cur = 0; cout << mn << "\n"; for (int i = 0; i < n; i++) { if (mn == cur) { cur = 0; } cout << cur << " "; cur++; } return 0; }
3
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <cstring> #include <cstdlib> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #define rep(i, a, b) for(int i = (a); i < (b); ++i) #define per(i, a, b) for(int i = (b) - 1; i >= (a); --i) #define reT() int T; scan_d(T); rep(t, 0, T) #define de(x) cout << #x << " = " << x << endl #define dd(x) cout << #x << " = " << x << " " #define da(x, a, b) for(int i = (a); i < (b); ++i) cout << "\t(" << i << ")" << x[i] << " \n"[i == b - 1]; #define pw(x) (1ll<<(x)) #define modadd(a,b) {a+=b;a%=MOD;} #define modmul(a,b) {a*=b;a%=MOD;} #define LL long long #define IN freopen("in", "r", stdin) #define OUT freopen("out", "w", stdout) using namespace std; const int inf = 0x3f3f3f3f; const int N = 510; const int MOD = 998244353; template <typename T> inline bool scan_d(T& ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) { if ((c = getchar()) == EOF) return 0; }sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } template<typename T> void print(T x) { static char s[33], * s1; s1 = s; if (!x) *s1++ = '0'; if (x < 0) putchar('-'), x = -x; while (x) *s1++ = (x % 10 + '0'), x /= 10; while (s1-- != s) putchar(*s1); } template<typename T> void println(T x) { print(x); putchar('\n'); } struct TPoint { int x, y; bool operator<(const TPoint& comp) const { if (x != comp.x) return x < comp.x; return y < comp.y; } }; int n; TPoint p[N]; int dn[N], dp[N]; int main() { #ifdef _LOCAL_JUDGE_ IN; //OUT; #endif while (scan_d(n)) { rep(i, 0, n) { char op; scanf(" %c", &op); if (op == '+') { int v; scan_d(v); p[i] = { 1, v }; } else p[i] = { 0, 0 }; } int res = 0; rep(id, 0, n) { if (p[id].x == 0) continue; fill(dn, dn + n + 1, 0); fill(dp, dp + n + 1, 0); dn[0] = 1; rep(i, 0, n) { modadd(dn[0], dp[0]); dp[0] = 0; if (p[i].x) { if (i == id) { per(j, 1, n + 1) modadd(dp[j], dp[j - 1]); per(j, 1, n + 1) modadd(dp[j], dn[j - 1]); } else if (TPoint({ p[i].y, i }) < TPoint({ p[id].y, id })) { per(j, 1, n + 1) modadd(dp[j], dp[j - 1]); per(j, 1, n + 1) modadd(dn[j], dn[j - 1]); } else { per(j, 0, n + 1) modmul(dp[j], 2); per(j, 0, n + 1) modmul(dn[j], 2); } } else { modmul(dp[0], 2); modmul(dn[0], 2); rep(j, 1, n + 1) modadd(dp[j - 1], dp[j]); rep(j, 1, n + 1) modadd(dn[j - 1], dn[j]); } //dd(val), de(i); //rep(j, 0, n + 1) dd(j), de(dn[j]); //rep(j, 0, n + 1) dd(j), de(dp[j]); } LL sum = 0; rep(j, 1, n + 1) modadd(sum, (LL)dp[j]); modmul(sum, (LL)p[id].y); modadd(res, (int)sum); } println(res); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 5; long long power(long long a, long long b, long long c) { long long res = 1; while (b) { if (b & 1LL) res = (res * a) % c; a = (a * a) % c; b /= 2; } return res; } void solve() { vector<long long> a(2); for (long long &x : a) cin >> x; sort(a.begin(), a.end()); if (a[1] >= 2 * a[0]) { cout << a[0] << endl; return; } long long ans = a[1] / 2; a[1] = a[1] % 2; a[0] -= ans; ans += (a[0] + a[1]) / 3; cout << ans << endl; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t = 1; cin >> t; while (t--) solve(); return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; long long play(vector<long long> a, long long i) { long long t = a[i]; a[i] = 0; long long d = t / 14, r = t % 14; for (long long i = 0; i < 14; i++) a[i] += d; while (r--) a[(++i) % 14]++; long long res = 0; for (long long i = 0; i < 14; i++) if (a[i] % 2 == 0) res += a[i]; return res; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); vector<long long> a(14); for (long long i = 0; i < 14; i++) cin >> a[i]; long long ans = 0; for (long long i = 0; i < 14; i++) ans = max(ans, play(a, i)); cout << ans; }
2
#include <bits/stdc++.h> using namespace std; int t; const int maxn = 1e5 + 8; char s[maxn]; int n; void find(char s[]) { int mid = n >> 1; for (int i = 1; i <= n; i++) { if (s[i] == '0') { if (i <= mid) { cout << i << " " << n << " " << i + 1 << " " << n << endl; } else { cout << 1 << " " << i << " " << 1 << " " << i - 1 << endl; } return; } } cout << 1 << " " << n - 1 << " " << 2 << " " << n << endl; } int main() { cin >> t; while (t--) { cin >> n; cin >> s + 1; find(s); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, k; long long cur = 1, ans = 0; string s, t; int main() { cin >> n >> k >> s >> t; s = '#' + s; t = '#' + t; for (int i = (int)1; i <= (int)n; ++i) { cur *= 2; if (s[i] == 'b') --cur; if (t[i] == 'a') --cur; cur = min(cur, (long long)1e10); ans += min(cur, 1LL * k); } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; struct point { int x, y; point() {} point(int a, int b) { x = a, y = b; } }; template <class T> T sqr(T a) { return a * a; } template <class T> T power(T n, T p) { T res = 1; for (int i = 0; i < p; i++) res *= n; return res; } template <class T> double getdist(T a, T b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } template <class T> T extract(string s, T ret) { stringstream ss(s); ss >> ret; return ret; } template <class T> string tostring(T n) { stringstream ss; ss << n; return ss.str(); } struct fast { fast() { ios_base::sync_with_stdio(0); cin.tie(0); } } cincout; pair<long long, long long> arr[1010]; bool comp(const pair<long long, long long> &a, const pair<long long, long long> &b) { return abs(a.first - a.second) > abs(b.first - b.second); } int main() { long long n, i, res, mid; cin >> n; for (i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second; sort(arr, arr + n, comp); res = (1LL << 60); for (mid = 1; mid <= 1000; mid++) { long long ulta = n / 2; long long len = 0; for (i = 0; i < n; i++) { if (min(arr[i].first, arr[i].second) > mid) break; if (arr[i].second > mid) { if (ulta == 0) break; ulta--; len += arr[i].second; continue; } if (ulta && arr[i].first > arr[i].second && arr[i].first <= mid) { ulta--; len += arr[i].second; continue; } len += arr[i].first; } if (i == n) res = min(res, len * mid); } cout << res << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; char let[100100]; int cou[100]; int main() { int i, n; scanf("%d", &n); getchar(); memset(cou, 0, sizeof(cou)); int ans = 0; int s = 1; for (i = 1; i <= n; i++) { scanf("%c", &let[i]); if (cou[let[i] - 'A'] == 0) ans = i - s + 1; cou[let[i] - 'A']++; while (s < i) { if (cou[let[s] - 'A'] > 1) { cou[let[s] - 'A']--; s++; } else break; } if (ans > i - s + 1) ans = i - s + 1; } cout << ans << endl; return 0; }
3
#include<iostream> #include<string> #include<algorithm> using namespace std; int dp[5001][5001]; int main() { int N; string S; cin >> N >> S; dp[0][0] = 0; for (int i = 1; i <= N; i++) { for (int j = i + 1; j <= N; j++) { if (S[i - 1] != S[j - 1]) continue; dp[i][j] = min(dp[i - 1][j - 1] + 1, j - i); } } int ans = 0; for (int i = 0; i <= N; i++)for (int j = 0; j <= N; j++)ans = max(ans, dp[i][j]); cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long val[100050]; int degree[100050], par[100050]; struct cmp { bool operator()(int a, int b) { if (val[a] != val[b]) return val[a] < val[b]; return a < b; } }; set<int, cmp> son[100050]; multiset<long long> overall; long long t[100050]; int n, q; long long val_oth(int pos) { return t[pos] / (degree[pos] + 2); } long long val_sel(int pos) { return t[pos] - (degree[pos] + 1) * (t[pos] / (degree[pos] + 2)); } void ins(int pos) { if (!son[pos].empty()) { long long fuck = val[*son[pos].begin()] + val_oth(pos); overall.insert(fuck); } if (son[pos].size() >= 2) { long long fuck = val[*(--son[pos].end())] + val_oth(pos); overall.insert(fuck); } } void era(int pos) { if (!son[pos].empty()) { long long fuck = val[*son[pos].begin()] + val_oth(pos); overall.erase(overall.find(fuck)); } if (son[pos].size() >= 2) { long long fuck = val[*(--son[pos].end())] + val_oth(pos); overall.erase(overall.find(fuck)); } } int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) scanf("%lld", &t[i]); for (int i = 1; i <= n; ++i) scanf("%d", &par[i]), son[par[i]].insert(i), degree[par[i]]++; for (int i = 1; i <= n; ++i) { son[par[i]].erase(i); for (auto u : son[i]) val[i] += val_oth(u); val[i] += val_sel(i); son[par[i]].insert(i); } for (int i = 1; i <= n; ++i) ins(i); for (int i = 1; i <= q; ++i) { int typ; scanf("%d", &typ); if (typ == 1) { int a, b; scanf("%d%d", &a, &b); set<int> st, more; st.insert(a), st.insert(b), st.insert(par[a]), st.insert(par[b]), st.insert(par[par[a]]); more = st; more.insert(par[par[par[a]]]), more.insert(par[par[b]]); for (auto i : more) era(i); for (auto i : st) son[par[i]].erase(i); for (int cur = -1; cur <= 1; cur += 2) { for (auto i : st) { val[i] += cur * val_sel(i); for (auto j : st) if (par[j] == i) val[i] += cur * val_oth(j); } if (cur == -1) { degree[par[a]]--; par[a] = b; degree[par[a]]++; } } for (auto i : st) son[par[i]].insert(i); for (auto i : more) ins(i); } else if (typ == 2) { int pos; scanf("%d", &pos); printf("%lld\n", val[pos] + val_oth(par[pos])); } else { printf("%lld %lld\n", *overall.begin(), *(--overall.end())); } } }
4
#include <bits/stdc++.h> using namespace std; void re(vector<long long> &v, int n) { for (auto i = 0; i < n; i++) cin >> v[i]; } void wr(vector<long long> v) { for (auto i : v) cout << i << " "; } void consoleIO() {} long long fact(long long n) { if (n == 1 || n == 0) return 1; return (n * fact(n - 1)); } long long sol() { int n; cin >> n; return fact(n - 1) / ((n >> 1)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); consoleIO(); cout << sol() << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MaxN = 1 << 18; const int MaxLength = 1 << 4; int N; char S[MaxN][MaxLength]; int Convert(char* S) { int Result = 0; while (*S) { Result = Result * 10 + (((*S) == '?') ? 9 : (*S) - '0'); S++; } return Result; } int main(void) { scanf("%d", &N); for (int i = 0; i < N; i++) scanf(" %s", S[i]); int Last = 0; bool Possible = true; for (int i = 0; i < N; i++) { int Length = strlen(S[i]); for (int j = 0; j < Length; j++) { if (S[i][j] != '?') continue; for (int k = 0; k < 10; k++) { if (j + k == 0) continue; S[i][j] = '0' + k; if (Convert(S[i]) > Last) break; } } if (Convert(S[i]) <= Last) { Possible = false; break; } Last = Convert(S[i]); } if (Possible) { puts("YES"); for (int i = 0; i < N; i++) puts(S[i]); } else puts("NO"); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; n %= 4; if (n == 0 || n == 3) { cout << 0 << endl; } else { cout << 1 << endl; } return 0; }
1
#include <cstdio> using namespace std; int h,a; int main(){ scanf("%d%d",&h,&a); printf("%d",(h-1)/a+1); return 0; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") using namespace std; int n, m, a, b, c, d, i, j, sz, mx; int ar[(int)(1e6 + 7)]; int brainfuck(); string s, s2; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; cin >> s >> s2; cout << brainfuck(); return 0; } int brainfuck() { for (i = 0; i != n; i++) if (s[i] == s2[i]) s[i] = s2[i] = '.'; for (i = 0; i != n; i++) { if (s[i] == '.') continue; if (i && s[i] == '0' && s[i - 1] == '1') a++, s[i] = s[i - 1] = '.'; else if (i && s[i] == '1' && s[i - 1] == '0') a++, s[i] = s[i - 1] = '.'; else if (i != n - 1 && s[i] == '0' && s[i + 1] == '1') a++, s[i] = s[i + 1] = '.'; else if (i != n - 1 && s[i] == '1' && s[i + 1] == '0') a++, s[i] = s[i + 1] = '.'; else a++, s[i] = '.'; } return a; }
3
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; signed main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { long long int s, in, ex; cin >> s >> in >> ex; long long int d = s - in; long long int ans = 0; if (s + ex <= in) cout << 0 << '\n'; else { long long int x1 = ((ex + in - s) / 2) + 1; if (in + ex < s) x1 = 0; cout << max((long long int)0, ex - x1 + 1) << '\n'; } } return 0; } long long int fast_power(long long int base, long long int power, long long int MOD) { long long int result = 1; while (power > 0) { if (power & 1) result = (result * base) % MOD; base = (base * base) % MOD; power >>= 1; } return result; }
1
#include <bits/stdc++.h> using namespace std; using lli = long long int; using ld = long double; using ulli = unsigned long long int; void solve() { lli n; cin >> n; int count = 0; for (lli i = 0; i < n; i++) { if (count == 0 or count == 1) { cout << "a"; count += 1; } else { cout << "b"; count += 1; } if (count == 4) { count = 0; } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); }
2
#include <bits/stdc++.h> using namespace std; int n, a[100005], sum, ans, t1, t2; char ch[100005]; int gets() { int v = 0; bool f = 0; char ch; while (!isdigit(ch)) { f |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { v = (v << 3) + (v << 1) + ch - 48; ch = getchar(); } return f ? -v : v; } int main() { n = gets(); scanf("%s", ch + 1); for (int i = 1; i <= n; i++) a[i] = (ch[i] == 'G' ? 1 : 0), sum += a[i]; for (int i = 1; i <= n + 1; i++) { if (!a[i]) { ans = max(ans, i - t2 - 1); t2 = t1; t1 = i; } } ans = min(ans, sum); printf("%d\n", ans); return 0; }
2
#include <string> #include <vector> #include <sstream> #include <iostream> #include <algorithm> #include <map> #include <list> #include <set> #include <numeric> #include <queue> #include <stack> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <cstring> #include <climits> #include <cfloat> #include <ctime> #include <complex> #include <cassert> #include <iomanip> using namespace std; typedef long long LL; typedef pair<int,int> P; typedef pair<int, P> T; int s[1000]; int n,m,h,k; int order[1000]; int num[1000]; int main() { while(1){ vector<int> path[1000]; vector<int> p[1000]; scanf("%d%d%d%d",&n,&m,&h,&k); if(n==0)return 0; for(int i=0;i<n;i++){ scanf("%d",s+i); order[i]=i; } for(int i=0;i<m;i++){ int a,b; scanf("%d%d",&a,&b); p[b].push_back(a); } for(int i=1;i<h;i++){ for(auto j:p[i]){ path[order[j]].push_back(order[j-1]); path[order[j-1]].push_back(order[j]); swap(order[j],order[j-1]); } } for(int i=0;i<n;i++){ num[order[i]]=s[i]; } /* for(int i=0;i<n;i++){ cout << num[i] << ", " ; } cout << endl; */ priority_queue<T> q; int sum=0; int ret=INT_MAX; for(int i=0;i<k;i++){ sum+=num[i]; for(auto j:path[i]){ if(num[i]>num[j]){ q.push(T(num[i]-num[j],P(i,j))); } } if(i==k-1){ while(q.size()){ auto t=q.top(); if(t.second.first<i-k+1){ q.pop(); continue; } if(i-k-1<=t.second.second&&t.second.second<=i){ q.pop(); continue; } /* if(sum-t.first==70){ cout << sum << ", " << i << ", " << t.second.first << ", " << t.second.second << ", " << t.first << endl; }*/ ret=min(ret,sum-t.first); break; } ret=min(ret,sum); sum-=num[i-k+1]; for(auto j:path[i-k+1]) { if(num[j]>num[i-k+1]&&i-k-1<j&&j<=i){ q.push(T(num[j]-num[i-k-1],P(j,i-k+1))); } } } } printf("%d\n",ret); } }
0
#include <bits/stdc++.h> using namespace std; const int N = 4000009; int main() { string a, b; char upperbound = 'a'; cin >> a >> b; int lt1[124] = {0}, lt2[124] = {0}; for (int i = 0; i < a.size(); i++) { lt1[a[i]]++; } for (int i = 0; i < b.size(); i++) { lt2[b[i]]++; if (b[i] > upperbound) { upperbound = b[i]; } } int count = 0; for (char i = 'a'; i <= upperbound; i++) { if (lt2[i] > 0 && lt1[i] == 0) { cout << -1; return 0; } if (lt1[i] >= lt2[i]) { count += lt2[i]; } else { count += lt1[i]; } } cout << count; return 0; }
2
#include <bits/stdc++.h> using namespace std; inline int gotcha() { register int a = 0, b = 1, c = getchar(); while (!isdigit(c)) b ^= c == '-', c = getchar(); while (isdigit(c)) a = a * 10 + c - 48, c = getchar(); return b ? a : -a; } const int _ = 1002, mo = 1000000007; long long powa(long long a, long long t) { long long b = 1; while (t) { if (t & 1) b *= a, b %= mo; a *= a, a %= mo, t >>= 1; } return b; } long long inv(long long a) { return powa(a, mo - 2) % mo; } long long f[_][_] = {0}, pa, pb, ab; int main() { register int i, j, a, b, tar; tar = gotcha(), a = gotcha(), b = gotcha(); pa = 1ll * a * inv(a + b) % mo, pb = 1ll * b * inv(a + b) % mo, ab = 1ll * a * inv(b) % mo; for (i = tar; i >= 1; i--) for (j = tar; j >= 0; j--) if (i + j >= tar) f[i][j] = (i + j + ab) % mo; else f[i][j] = (pa * f[i + 1][j] + pb * f[i][i + j]) % mo; printf("%lld", f[1][0]); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long a, b, k; long long solve(long long n) { if (n % a == 0) return (n / a - 1); else return (n / a); } int32_t main() { vector<long long> v; long long n; cin >> n >> a >> b >> k; long long x; for (long long i = 0; i < n; i++) { cin >> x; v.push_back(x % (a + b)); } long long res = 0; vector<long long> final; sort(v.begin(), v.end()); for (long long i = 0; i < n; i++) { if (v[i] <= a and v[i] >= 1) res++; else { if (v[i] == 0) { final.push_back(solve(b) + 1); } else final.push_back(solve(v[i])); } } sort(final.begin(), final.end()); for (long long i = 0; i < final.size(); i++) { if (k >= final[i]) { res++; k -= final[i]; } else break; } cout << res << endl; }
4
#include<bits/stdc++.h> using namespace std; char russia[52][52]; int main(){ int n,m; cin>>n>>m; int c=n*m; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>russia[i][j]; } } int d=0; for(int i=1;i<n-1;i++){ for(int j=1;j<n-i;j++){ for(int p=0;p<i;p++){ for(int l=0;l<m;l++){ if(russia[p][l]!='W'){d++;} } } for(int p=i;p<i+j;p++){ for(int l=0;l<m;l++){ if(russia[p][l]!='B'){d++;} } } for(int p=i+j;p<n;p++){ for(int l=0;l<m;l++){ if(russia[p][l]!='R'){d++;} } } if(c>d){c=d;} d=0; } } cout<<c<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, mxmc; map<int, int> adj[100007]; int color[100007]; int sz[100007]; int ans = 100007, s = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> color[i]; } for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; if (color[u] != color[v]) { if (adj[color[u]].find(color[v]) == adj[color[u]].end()) { adj[color[u]][color[v]] = 1; sz[color[u]]++; } if (adj[color[v]].find(color[u]) == adj[color[v]].end()) { adj[color[v]][color[u]] = 1; sz[color[v]]++; } } } for (int i = 1; i <= n; i++) { if (sz[color[i]] > s || (sz[color[i]] == s && color[i] < ans)) { s = sz[color[i]]; ans = color[i]; } } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100100; const int mod = 998244353; inline int read(int x = 0, bool f = 0, char ch = getchar()) { for (; !isdigit(ch); ch = getchar()) f = ch == '-'; for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); return f ? -x : x; } int n, m, fa[N], ch[N][2], p[N]; long long f[N], siz[N], res[N], ans[N], ssiz[N], sum[N], a[N]; vector<int> g[N]; inline bool get(int x) { return ch[fa[x]][1] == x; } inline bool gr(int x) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; } inline void pushup(int x) { ssiz[x] = siz[x] + ssiz[ch[x][0]] + ssiz[ch[x][1]]; sum[x] = sum[ch[x][0]] + sum[ch[x][1]] + siz[x] * a[x]; ans[x] = ans[ch[x][0]] + ans[ch[x][1]] + (siz[x] * siz[x] + f[x]) * a[x] + 2 * siz[x] * ssiz[ch[x][1]] * a[x] + 2 * sum[ch[x][0]] * (ssiz[x] - ssiz[ch[x][0]]) + res[x]; } inline void rotate(int x) { int y = fa[x], z = fa[y], k = get(x), w = ch[x][k ^ 1]; if (gr(y)) ch[z][get(y)] = x; fa[x] = z; ch[x][k ^ 1] = y; fa[y] = x; ch[y][k] = w; fa[w] = y; pushup(y); pushup(x); } inline void splay(int x) { for (; gr(x); rotate(x)) if (gr(fa[x])) rotate(get(fa[x]) == get(x) ? fa[x] : x); } inline void add(int x, int y) { siz[x] += ssiz[y]; f[x] -= ssiz[y] * ssiz[y]; res[x] += ans[y]; pushup(x); } inline void del(int x, int y) { siz[x] -= ssiz[y]; f[x] += ssiz[y] * ssiz[y]; res[x] -= ans[y]; pushup(x); } inline void access(int x) { for (int y = 0; x; y = x, x = fa[x]) { splay(x); del(x, y); add(x, ch[x][1]); ch[x][1] = y; pushup(x); } } inline void link(int x, int y) { access(x); splay(x); splay(y); fa[y] = x; add(x, y); access(y); splay(y); } inline void cut(int x, int y) { access(y); splay(y); del(y, x); pushup(y); } void dfs(int x) { for (int i : g[x]) link(x, i), dfs(i); } int main() { n = read(); for (int i = 2; i <= n; ++i) p[i] = read(), g[p[i]].push_back(i); for (int i = 1; i <= n; ++i) a[i] = read(), siz[i] = ssiz[i] = 1, sum[i] = a[i], ans[i] = a[i]; dfs(1); splay(1); printf("%.10lf\n", 1.0 * ans[1] / n / n); m = read(); while (m--) { char c; scanf(" %c", &c); int x = read(), y = read(); if (c == 'P') { access(y); splay(y); splay(x); if (!gr(y)) swap(x, y); cut(y, p[y]); p[y] = x; link(x, y); splay(1); } if (c == 'V') access(x), splay(x), a[x] = y, pushup(x), splay(1); printf("%.10lf\n", 1.0 * ans[1] / n / n); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; int fx[] = {0, 0, -1, 1, +1, -1, -1, +1}; int fy[] = {1, -1, 0, 0, +1, +1, -1, -1}; bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { return a.first > b.first; } template <typename T> inline T Bigmod(T base, T power, T MOD) { T ret = 1; while (power) { if (power & 1) ret = (ret * base) % MOD; base = (base * base) % MOD; power >>= 1; } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); ; int n; while (cin >> n) { int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int cnt = 0; sort(arr, arr + n); for (int i = 0; i < n; i++) { if (arr[i] == 0) continue; for (int j = i + 1; j < n; j++) { if (arr[j] > 0 && arr[j] % arr[i] == 0) arr[j] = 0; } } for (int i = 0; i < n; i++) { if (arr[i] > 0) cnt++; } cout << cnt << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; constexpr int MAXN = 1e5 + 5; constexpr int MAXW = 1e4 + 5; constexpr int MOD = 998244353; constexpr long long INF = 1e18; constexpr int LOG = 11; int n, m, k, a[MAXN], mx[MAXN], mx2[MAXN], val; vector<int> G[MAXN]; bool mark[MAXN], failed[MAXN]; set<pair<long double, int> > s, ans; bool Check(long double x) { val = x; s.clear(); memset(failed, 0, sizeof failed); for (int i = 1; i <= n; i++) { mx2[i] = mx[i]; } for (int i = 1; i <= n; i++) { if (!mark[i]) { long double a = mx2[i], b = G[i].size(); s.insert(make_pair(a / b, (long double)i)); } else { failed[i] = true; } } while (!s.empty()) { pair<long double, int> p = *s.begin(); int u = p.second; if (x <= p.first) { break; } else { s.erase(s.begin()); failed[u] = true; for (auto v : G[u]) { if (!failed[v]) { long double a = mx2[v], b = G[v].size(); s.erase(make_pair(a / b, v)); mx2[v]--; a--; s.insert(make_pair(a / b, v)); } } } } if (!s.empty()) { ans = s; } return !s.empty(); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= k; i++) { cin >> a[i]; mark[a[i]] = true; } for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } for (int u = 1; u <= n; u++) { if (mark[u]) { continue; } for (auto v : G[u]) { if (!mark[v]) { mx[u]++; } } } long double l = 0, r = 1; int T = 25; while (T--) { long double mid = (l + r) / (long double)2; if (Check(mid)) { l = mid; } else { r = mid; } } if (ans.empty()) { for (int i = 1; i <= n; i++) { if (!mark[i]) { ans.insert(make_pair(0, i)); break; } } } cout << ans.size() << "\n"; for (auto it : ans) { cout << it.second << " "; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 5005; int dp[N][30]; int main() { int n; scanf("%d\n", &n); for (int i = 1; i <= n; ++i) { char c; scanf("%c", &c); int t = c - 'a' + 1; int cur = 1; for (int j = 1; j <= n; ++j) { dp[j][0] = (dp[j][0] - dp[j][t] + mod) % mod; dp[j][t] = cur; cur = (cur + dp[j][0]) % mod; dp[j][0] = (dp[j][0] + dp[j][t]) % mod; } } printf("%d", dp[n][0]); return 0; }
4
#include<iostream> using namespace std; int main() { int n,m; cin>>n>>m; if(n-m>0) cout<<n-m; else cout<<0; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) { cout << -1; } else cout << n << ' ' << n + 1 << ' ' << n * (n + 1); }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; int n, a[N], b[N], c[N], mx[N], gc[N]; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) gc[i] = gcd(i, n); long long ans = 0; for (int g = 1; g < n; g++) if (n % g == 0) { for (int i = 1; i <= n; i++) c[i] = c[i - 1] + (gc[i] == g); for (int i = 0; i < g; i++) mx[i] = a[i]; for (int i = g; i < n; i++) mx[i % g] = max(mx[i % g], a[i]); for (int i = 0; i < n; i++) b[i] = b[i + n] = (mx[i % g] == a[i]); for (int i = n + n - 2; i >= 0; i--) if (b[i]) b[i] += b[i + 1]; for (int i = 0; i < n; i++) ans += c[min(n - 1, b[i])]; } printf("%lld", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; float arr[maxn]; map<float, int> cnt; int main() { int n, a, b, c; char ch; scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> ch; scanf("%d+%d)/%d", &a, &b, &c); arr[i] = (a + b + 0.0) / (c + 0.0); cnt[arr[i]]++; } for (int i = 0; i < n; i++) printf("%d ", cnt[arr[i]]); return 0; }
4
#include <bits/stdc++.h> int main() { int w, b; char c; w = 0; b = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { scanf(" %c", &c); if (c == 'Q') { w += 9; } if (c == 'R') { w += 5; } if (c == 'B' || c == 'N') { w += 3; } if (c == 'P') { w++; } if (c == 'q') { b += 9; } if (c == 'r') { b += 5; } if (c == 'b' || c == 'n') { b += 3; } if (c == 'p') { b++; } } } if (b > w) { printf("Black\n"); } else if (b < w) { printf("White\n"); } else { printf("Draw\n"); } return 0; }
1
#include <bits/stdc++.h> using ll = long long; int n = 0; void readcircuit(std::set<int> &sol) { char ch; std::cin >> ch; std::set<int> aux; if (ch == '*') { n++; sol.insert(n); } else if (ch == '(') { readcircuit(sol); char op; std::cin >> op; if (op == 'S') { while (op != ')') { readcircuit(aux); if (aux.size() < sol.size()) { std::swap(sol, aux); } aux.clear(); std::cin >> op; } } else { while (op != ')') { readcircuit(aux); if (sol.size() < aux.size()) std::swap(aux, sol); for (std::set<int>::iterator it = aux.begin(); it != aux.end(); it++) sol.insert(*it); aux.clear(); std::cin >> op; } } } } void solve() { int target; std::cin >> target; n = 0; std::set<int> sol; readcircuit(sol); std::cout << "REVOLTING "; for (int i = 1; i <= n; i++) if (sol.find(i) == sol.end()) std::cout << 0 << " "; else std::cout << 1LL * sol.size() * target << " "; std::cout << '\n'; } int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int t; std::cin >> t; for (int testcase = 1; testcase <= t; testcase++) solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main(){ int N;double x,e=0;string s;cin>>N; for(int i=0;i<N;i++){ cin>>x>>s; if(s=="JPY")e+=x; else e+=380000*x; } cout<<e<<endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int k = 0; char c; while ((c = getchar()) != EOF) { if (c < 32) continue; int p = 0, t = c; for (int i = 0; i < 8; i++) { p = p * 2 + t % 2; t /= 2; } cout << (k - p + 256) % 256 << endl; k = p; } k = k; }
3
#include <iostream> using namespace std; #include <string> int main(){ int hcount = 0 , bcount = 0; string r , a; while(1){ cin >> r >> a; if( r == "0" && a == "0" ) break; for( int i = 0; i < 4; i++ ){ for( int j = 0; j < 4; j++ ){ if( i == j && r[i] == a[j] ) hcount++; else if( r[i] == a[j] ) bcount++; } } cout << hcount << " " << bcount << endl; hcount = 0; bcount = 0; } return 0; }
0
// CGL_3_B.cpp // #include <bits/stdc++.h> using namespace std; const double EPS = 1e-10; bool EQ(double a, double b){return fabs(a-b) < EPS;} typedef complex<double> Point; #define X real() #define Y imag() namespace std { bool operator == (const Point &a, const Point &b) { return EQ(a.X, b.X) && EQ(a.Y, b.Y); } bool operator < (const Point &a, const Point &b) { return a.X != b.X ? a.X < b.X : a.Y < b.Y; } } struct Segment { Point p1, p2; Segment(){}; Segment(Point p1, Point p2) : p1(p1), p2(p2) {}; }; typedef Segment Line; typedef vector<Point> Polygon; double dot(Point a, Point b){return real(conj(a) * b);} double cross(Point a, Point b){return imag(conj(a) * b);} const int COUNTER_CLOCKWISE = +1; const int CLOCKWISE = -1; const int ONLINE_BACK = +2; const int ONLINE_FRONT = -2; const int ON_SEGMENT = 0; int ccw(Point a, Point b, Point c) { Point x = b - a; Point y = c - a; if(cross(x, y) > EPS) return COUNTER_CLOCKWISE; if(cross(x, y) < -EPS) return CLOCKWISE; if(dot(x, y) < -EPS) return ONLINE_BACK; if(norm(x) < norm(y)) return ONLINE_FRONT; return ON_SEGMENT; } #define curr(P,i) P[i] #define next(P,i) P[(i+1)%P.size()] #define prev(P,i) P[(i+P.size()-1) % P.size()] bool isConvex(Polygon P) { for(int i = 0; i < P.size(); ++i) if(ccw(prev(P,i), curr(P,i), next(P,i)) == CLOCKWISE) return false; return true; } int main() { int n,x,y; Polygon P; cin>>n; for(int i=0;i<n;++i){ cin>>x>>y; P.push_back(Point(x,y)); } cout<<isConvex(P)<<endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ll n; cin>>n; ll sum=0; map<ll,ll>m; ll a[n]; for(int i=0;i<n;i++) cin>>a[i],m[a[i]]++,sum+=a[i]; int q; cin>>q; while(q--) { ll x,b; cin>>x>>b; int fr=m[x]; sum-=(fr*x); m[x]=0; sum+=(fr*b); m[b]+=fr; cout<<sum<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int test(int a, int b, int n) { int step = 0; int total = 0; int ne; int cur; if (a < b) swap(a, b); while (a != 1 && b != 1) { ne = a % b; if (!ne) return 0x7fffffff; cur = (a - ne) / b; step += cur; ++total; a = ne; swap(a, b); } step += a - 1; if (step != n) return 0x7fffffff; return n - total - 1; } vector<int> res; char at[] = {'T', 'B'}; void get(int a, int b) { const int ca = a, cb = b; int total = 0; int ne; int cnt; res.clear(); int i; if (a < b) swap(a, b); res.push_back(1); while (a != 1 && b != 1) { ne = a % b; cnt = (a - ne) / b; for (i = 0; i < cnt; ++i) res.push_back(i == cnt - 1); a = ne; swap(a, b); } for (i = 1; i < a; ++i) res.push_back(i == a - 1); return; } int main() { int n, f; scanf("%d%d", &n, &f); res.reserve(n); if (n == f && n == 1) { printf("0\nT\n"); return 0; } int resmin = n; int i; int ret; int ra; if (n <= f) { for (i = 1; (i << 1) < f; ++i) { ret = test(i, f - i, n - 2); if (ret < resmin) { resmin = ret; ra = i; } } } int cur = 0; if (resmin < n) { get(ra, f - ra); printf("%d\n", resmin); putchar('T'); for (i = res.size() - 1; i >= 0; --i) putchar(at[cur ^= res[i]]); putchar('\n'); } else puts("IMPOSSIBLE"); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int INF = (int)2E9; const long long MOD = (long long)1E9 + 7; const double PI = acos(-1.0); const double EPS = 1E-9; int A[4]; vector<char> ans; vector<int> ans2; inline bool sama() { for (int i = 0; i < 4; i++) if (A[i] != 1) return false; return true; } int main() { for (int i = 0; i < 4; i++) scanf("%d", A + i); while (!sama()) { int ans = -INF, idx; for (int i = 0; i < 4; i++) if (ans < A[i]) ans = A[i], idx = i; if (A[idx] % 2 == 0) { int prev = (idx + 3) % 4, next = (idx + 1) % 4; if (A[prev] % 2 == 0) { A[idx] /= 2; A[prev] /= 2; printf("/%d\n", prev + 1); continue; } else if (A[next] % 2 == 0) { A[idx] /= 2; A[next] /= 2; printf("/%d\n", idx + 1); continue; } else { A[idx]++; A[next]++; printf("+%d\n", idx + 1); continue; } } else { int prev = (idx + 3) % 4, next = (idx + 1) % 4; if (A[prev] % 2 == 1) { A[idx]++; A[prev]++; printf("+%d\n", prev + 1); continue; } else if (A[prev] % 2 == 1) { A[idx]++; A[next]++; printf("+%d\n", idx + 1); continue; } else { A[idx]++; A[next]++; printf("+%d\n", idx + 1); continue; } } } return 0; }
3
#include<iostream> #include<string> using namespace std; int main(){ int length=0, num=0, big=0, small=0; string str; cin >> str; if(str.size() >= 6) length++; for(int i = 0; i < str.size(); i++){ if(str[i] >= '0' && str[i] <= '9') num++; else if(str[i] >= 'a' && str[i] <= 'z') small++; else if(str[i] >= 'A' && str[i] <= 'Z') big++; } if(length != 0 && num != 0 && small != 0 && big != 0) cout << "VALID" << endl; else cout << "INVALID" << endl; }
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; const long long MOD2 = (long long)998244353 * (long long)998244353; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 998244353) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } char s[(2005)][(2005)]; int a[4][(2005)][(2005)]; void fmain(int tid) { scanf("%d%d", &n, &m); for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%s", s[i] + 1); for (int(i) = 1; (i) <= (int)(n); (i)++) for (int(j) = 1; (j) <= (int)(m); (j)++) { if (s[i - 1][j] == s[i][j] && s[i][j - 1] == s[i][j]) { a[0][i][j] = min(a[0][i - 1][j], a[0][i][j - 1]) + 1; } else a[0][i][j] = 1; } for (int(i) = 1; (i) <= (int)(n); (i)++) for (int j = m; j; j--) { if (s[i - 1][j] == s[i][j] && s[i][j + 1] == s[i][j]) { a[1][i][j] = min(a[1][i - 1][j], a[1][i][j + 1]) + 1; } else a[1][i][j] = 1; } for (int i = n; i; i--) for (int(j) = 1; (j) <= (int)(m); (j)++) { if (s[i + 1][j] == s[i][j] && s[i][j - 1] == s[i][j]) { a[2][i][j] = min(a[2][i + 1][j], a[2][i][j - 1]) + 1; } else a[2][i][j] = 1; } for (int i = n; i; i--) for (int j = m; j; j--) { if (s[i + 1][j] == s[i][j] && s[i][j + 1] == s[i][j]) { a[3][i][j] = min(a[3][i + 1][j], a[3][i][j + 1]) + 1; } else a[3][i][j] = 1; } long long ans = 0; for (int(i) = 1; (i) <= (int)(n); (i)++) for (int(j) = 1; (j) <= (int)(m); (j)++) { int len = (1 << 30); for (int(z) = 0; (z) < (int)(4); (z)++) mintt(len, a[z][i][j]); ans += len; } printf("%lld\n", ans); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n, t; map<long long, map<int, long long> > mp; long long gao(long long x, int ns) { if (mp[x].find(ns) != mp[x].end()) return mp[x][ns]; if (ns == 0) return 1; if (x == 0) return 0; long long lx = x, tx = x; while (tx) { lx = tx & (-tx); tx -= lx; } return mp[x][ns] = gao(lx - 1, ns) + gao(x - lx, ns - 1); } int main() { while (cin >> n >> t) { if (t == (t & -t)) { int s = 0; long long tt = t; while (tt) { s++; tt >>= 1; } long long ans = gao(n + 1, s); if (t == 1) ans--; cout << ans << endl; } else cout << 0 << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int f[20010][2], n, a, ans; int main() { ans = 0; memset(f, 0, sizeof(f)); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a); for (int j = 0; j <= 20000; j++) { f[j][i & 1] = 0; if (j >= a) f[j][i & 1] += f[j - a][(i ^ 1) & 1]; if (j + a <= 20000) f[j][i & 1] += f[j + a][(i ^ 1) & 1]; if (f[j][i & 1] >= MOD) f[j][i & 1] -= MOD; if (j == 10000) ans += f[j][i & 1]; if (ans >= MOD) ans -= MOD; } f[10000 + a][i & 1]++; if (f[10000 + a][i & 1] >= MOD) f[10000 + a][i & 1] -= MOD; f[10000 - a][i & 1]++; if (f[10000 - a][i & 1] >= MOD) f[10000 - a][i & 1] -= MOD; } printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 3, mod = 1e9 + 7; int n, m, f[maxn], ans[maxn], ps[maxn], pps[maxn], ted; bool check[maxn]; string s, t, q; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s >> t; n = s.size(); m = t.size(); q = ""; for (int i = 0; i < m; i++) { q += t[i]; } q += '#'; for (int i = 0; i < n; i++) { q += s[i]; } int curr; for (int i = 2; i <= n + m + 1; i++) { curr = f[i - 1]; while (curr > 0 && q[curr] != q[i - 1]) { curr = f[curr]; } if (q[curr] == q[i - 1]) { f[i] = curr + 1; } } for (int i = m + 2; i <= n + m + 1; i++) { if (f[i] == m) { check[i - m - 1] = 1; } } int last = -1; for (int i = 1; i <= n; i++) { int res = 0; if (check[i] == 1) { last = i - m; } if (last != -1) { res = (pps[last] + last + 1) % mod; } ans[i] = res; ted = (ted + res) % mod; ps[i] = (ps[i - 1] + ans[i]) % mod; pps[i] = ((pps[i - 1] + ps[i - 1]) % mod + ans[i]) % mod; } cout << ted << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> g(n + 1); vector<int> a(n); for (int i = 0; i < m; i++) { int maxj = 0; for (int j = 0; j < n; j++) { cin >> a[j]; if (a[j] > a[maxj]) maxj = j; else if (a[j] == a[maxj]) { if (j < maxj) maxj = j; } } g[maxj + 1]++; a.resize(n); } int ma = 0; for (int i = 1; i <= n; i++) { if (g[i] > g[ma]) ma = i; else if (g[i] == g[ma]) { if (i < ma) ma = i; } } cout << ma; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); string a, b; int count = 0; vector<long long> v; cin >> a >> b; if (a.length() != b.length()) { cout << "NO" << endl; } else { for (long long i = 0; i < a.length(); i++) { if (a[i] != b[i]) { count++; v.push_back(i); } if (count > 2) break; } if (count == 2) { if (a[v[0]] == b[v[1]] && a[v[1]] == b[v[0]]) { cout << "YES" << endl; } else { cout << "NO" << endl; } } else { cout << "NO" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 55; long long f[maxn][maxn][maxn][maxn]; long long pw[maxn]; int c[maxn]; void init() { f[0][0][0][0] = 1; pw[0] = 1; for (int i = 1; i < maxn; i++) pw[i] = (pw[i - 1] * 2ll) % mod; } long long cal(int x, bool xx) { return x ? pw[x - 1] : xx; } void cal_ans(int ew, int ow, int eb, int ob, int now, long long& ret) { if (now != 0 && ew) (ret += f[ew - 1][ow][eb][ob] * pw[ew + ow - 1 + eb] % mod * cal(ob, 0) % mod) %= mod; if (now != 0 && ow) (ret += f[ew][ow - 1][eb][ob] * pw[ew + ow - 1 + eb] % mod * cal(ob, 1) % mod) %= mod; if (now != 1 && eb) (ret += f[ew][ow][eb - 1][ob] * pw[eb + ob - 1 + ew] % mod * cal(ow, 0) % mod) %= mod; if (now != 1 && ob) (ret += f[ew][ow][eb][ob - 1] * pw[eb + ob - 1 + ew] % mod * cal(ow, 1) % mod) %= mod; } int main(int argc, const char** argv) { init(); int n, p; cin >> n >> p; for (int i = 1; i <= n; i++) scanf("%d", c + i); long long ans = 0; for (int i = 1; i <= n; i++) for (int ew = 0; ew <= i; ew++) for (int ow = 0; ow + ew <= i; ow++) for (int eb = 0; ow + ew + eb <= i; eb++) { int ob = i - ow - ew - eb; cal_ans(ew, ow, eb, ob, c[i], f[ew][ow][eb][ob]); if (i == n && ((ow + ob) & 1) == p) (ans += f[ew][ow][eb][ob]) %= mod; } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m, pom; cin >> n >> m; vector<vector<int> > plus(m); vector<vector<int> > minus(m); for (int i = 0; i < m; i++) { cin >> n; for (int j = 0; j < n; j++) { cin >> pom; if (pom > 0) plus[i].push_back(pom); else minus[i].push_back(pom); } } int i; for (i = 0; i < m; i++) { bool final = true; for (auto el : minus[i]) { auto res = find(plus[i].begin(), plus[i].end(), -1 * el); if (res != plus[i].end()) { final = false; break; } } if (final) break; } if (i == m) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, k, t, a, b, boths, alices, bobs, cnt[2], ans, res, tmp; vector<int> both, alice, bob; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> t >> a >> b; if (a && b) both.push_back(t); else if (a) alice.push_back(t); else if (b) bob.push_back(t); } boths = both.size(); alices = alice.size(); bobs = bob.size(); sort(both.begin(), both.end()); sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); while (k) { int s1 = 1 << 30, s2 = 1 << 30; if (boths <= cnt[0]) break; if (boths > cnt[0]) s1 = both[cnt[0]]; if (cnt[1] < min(alices, bobs)) s2 = alice[cnt[1]] + bob[cnt[1]]; if (s1 < s2) ans += s1, cnt[0]++, k--; else ans += s2, cnt[1]++, k--; } while (k--) { if (cnt[1] < min(alices, bobs)) ans += alice[cnt[1]] + bob[cnt[1]], cnt[1]++; else if (cnt[0] < boths) ans += both[cnt[0]], cnt[0]++; else return cout << "-1\n", 0; } cout << ans << '\n'; return 0; }
5
#include <bits/stdc++.h> int main() { int t, i, a, j, done; scanf("%d", &t); for (i = 0; i < t; i++) { scanf("%d", &a); done = 0; if (a < 60) done = 0; else { for (j = 1; j <= 358; j++) { if (180 * j == a * (j + 2)) { done = 1; break; } } } if (done == 1) printf("YES\n"); else printf("NO\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long Ans, Res, Tmp, S[4][1005], ST[4], MaxS[4]; int N[4], D[4][1005], P[4][1005][12]; vector<int> E[1005]; void dfs(int n, int p, int t, int d) { D[t][n] = d; P[t][n][0] = p; for (int v : E[n]) if (v != p) dfs(v, n, t, d + 1); } int lca(int t, int u, int v) { if (D[t][u] < D[t][v]) swap(u, v); for (int i = 0; i < 12; i++) if ((D[t][u] - D[t][v]) & (1 << i)) u = P[t][u][i]; if (u == v) return u; for (int i = 11; i >= 0; i--) if (P[t][u][i] != P[t][v][i]) u = P[t][u][i], v = P[t][v][i]; return P[t][u][0]; } int dist(int t, int u, int v) { int l = lca(t, u, v); return D[t][u] + D[t][v] - 2 * D[t][l]; } int main() { cin >> N[1] >> N[2] >> N[3]; Ans = N[1] * N[2] + N[2] * N[3]; for (int t = 1; t <= 3; t++) { for (int i = 1; i <= N[t]; i++) E[i].clear(); for (int i = 1; i < N[t]; i++) { int a, b; cin >> a >> b; E[a].push_back(b), E[b].push_back(a); } dfs(1, 0, t, 0); for (int l = 1; l < 12; l++) for (int n = 1; n <= N[t]; n++) P[t][n][l] = P[t][P[t][n][l - 1]][l - 1]; for (int u = 1; u <= N[t]; u++) { for (int v = 1; v <= N[t]; v++) S[t][u] += dist(t, u, v); ST[t] += S[t][u]; MaxS[t] = max(MaxS[t], S[t][u]); } ST[t] /= 2; } for (int d = 1; d <= 3; d++) { Res = ST[1] + ST[2] + ST[3]; for (int t = 1; t <= 3; t++) if (t != d) Res += MaxS[t] * (N[1] + N[2] + N[3] - N[t]) + N[t] * N[d]; Tmp = 0; int t1 = d == 1 ? 2 : 1; int t2 = d == 3 ? 2 : 3; for (int u = 1; u <= N[d]; u++) for (int v = 1; v <= N[d]; v++) Tmp = max(Tmp, (long long)(N[t1] * S[d][u] + N[t2] * S[d][v] + N[t1] * N[t2] * (dist(d, u, v) + 2))); Ans = max(Ans, Res + Tmp); } cout << Ans; }
5
#include "bits/stdc++.h" using namespace std; int x[55], y[55]; int main(int argc, char const *argv[]) { int n; cin >> n; for(int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } map <pair <int, int>, int> mp; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(i != j) { mp[make_pair(x[i] - x[j], y[i] - y[j])] += 1; } } } int ans = 0; for(auto i : mp) { ans = max(ans, i.second); } cout << (n - ans) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int Mod = 1000000007; struct Int { int a; Int(int _a = 0) : a(_a) {} Int operator+(const Int& o) { Int ret(a + o.a); if (ret.a >= Mod) ret.a -= Mod; return ret; } Int operator*(const Int& o) { Int ret; ret.a = (long long)a * o.a % Mod; return ret; } }; int main() { int n, a = 0, b = 0, x, i; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%d", &x); if (x == 1) ++a; else ++b; } Int _f(1), f(1), t; for (i = 2; i <= a; ++i) { t = Int(i - 1) * _f + f; _f = f; f = t; } for (i = a + 1; i <= a + b; ++i) f = f * i; cout << f.a << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n,m,r; cin >> n >> m; vector<int> vec(n); for(int i = 0;i<2*m;i++){ cin >> r; vec[r-1] += 1; } for(int i :vec){ cout << i << endl; } }
0
#include <bits/stdc++.h> template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; int main(void) { cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; a.push_back(0); long long ans = 0; long long maxi = a[0]; for (int i = 0; i < (int)(n); i++) { if (a[i] * a[i + 1] <= 0) { ans += maxi; maxi = a[i + 1]; } else { chmax(maxi, a[i + 1]); } } cout << ans << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll h[200005],a[200005],dp[200005]; int n; void go(ll i, ll v) { while(i<200005) { dp[i]=max(dp[i],v); i+=i&-i; } } ll query(int i) { ll ans=0; while(i) { ans=max(ans,dp[i]); i-=i&-i; } return ans; } int main() { cin>>n; for(int i=0;i<n;i++)cin>>h[i]; for(int i=0;i<n;i++)cin>>a[i]; for(int i=0;i<n;i++) go(h[i],query(h[i])+a[i]); cout<<query(n); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { char a[55], b[55]; int ct = 0, sum = 1; gets(a); gets(b); for (int i = 0; i < strlen(b); i++) { if (a[ct] == b[i]) { ct++; sum++; } } printf("%d", sum); return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; char a[101010], b[101010]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; int T = 0, H = 0; for (int i = 1; i <= n; i++) if (a[i] == 'H') H++; else T++; int ans = 1000000000; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) b[j] = ' '; for (int j = i, tot = 1; tot <= H; tot++, j = j % n + 1) b[j] = 'H'; for (int j = 1; j <= n; j++) if (b[j] == ' ') b[j] = 'T'; int cur = 0; for (int j = 1; j <= n; j++) if (a[j] != b[j]) cur++; cur /= 2; ans = min(ans, cur); } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e6; const long long mod = 1e9 + 7; const long double PI = 4 * atan((long double)1); long long pw(long long a, long long b) { return (!b ? 1 : (b & 1 ? a * pw(a * a % mod, b / 2) % mod : pw(a * a % mod, b / 2) % mod)); } int n, a[maxn]; vector<int> adj[maxn]; queue<int> q; long long dist[maxn]; bool mark[maxn]; int ans[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; memset(dist, 63, sizeof dist); ; memset(ans, 63, sizeof ans); ; for (int i = 1; i <= n; i++) { cin >> a[i]; if (i - a[i] > 0) { adj[i - a[i]].push_back(i); ; } if (i + a[i] <= n) { adj[i + a[i]].push_back(i); ; } if (a[i] % 2 == 1) { q.push(i); mark[i] = 1; dist[i] = 0; } } while (!q.empty()) { int v = q.front(); q.pop(); for (auto u : adj[v]) { if (!mark[u]) { mark[u] = 1; dist[u] = dist[v] + 1; ans[u] = dist[u]; q.push(u); } } } memset(dist, 63, sizeof dist); ; memset(mark, 0, sizeof mark); ; for (int i = 1; i <= n; i++) { if (a[i] % 2 == 0) { q.push(i); mark[i] = 1; dist[i] = 0; } } while (!q.empty()) { int v = q.front(); q.pop(); for (auto u : adj[v]) { if (!mark[u]) { mark[u] = 1; dist[u] = dist[v] + 1; ans[u] = dist[u]; q.push(u); } } } for (int i = 1; i <= n; i++) { if (ans[i] == ans[0]) { cout << -1 << ' '; } else { cout << ans[i] << ' '; } } return (0); }
5
#include <bits/stdc++.h> using namespace std; const int inf = 2147483647; const long long linf = 9223372036854775807; void fileIO(string name) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } pair<int, int> mx[4][20]; int vi[100005]; void update(int i, int k, pair<int, int> num[], int flag) { if (flag) { if (k > num[10].first) num[10].first = k, num[10].second = i; for (int i = 9; i >= 0; i--) { if (num[i].first < num[i + 1].first) swap(num[i], num[i + 1]); } } else { if (k < num[10].first) num[10].first = k, num[10].second = i; for (int i = 9; i >= 0; i--) { if (num[i].first > num[i + 1].first) swap(num[i], num[i + 1]); } } } void add(int i, int x1, int y1, int x2, int y2) { int x = x1 + x2, y = y1 + y2; update(i, x, mx[0], 1); update(i, y, mx[1], 1); update(i, x, mx[2], 0); update(i, y, mx[3], 0); } int n, K; int main() { for (int i = 0; i <= 10; i++) mx[2][i].first = mx[3][i].first = inf; cin >> n >> K; for (int i = 0; i < n; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; add(i, x1, y1, x2, y2); } long long ans = linf; int now = 0, kill, maxx, minx, maxy, miny; for (int i = 0; i <= K; i++) { for (int j = 0; j <= K; j++) { for (int k = 0; k <= K; k++) { for (int l = 0; l <= K; l++) { kill = 0, now++; maxx = maxy = 0; minx = miny = inf; for (int p = 0; p < i; p++) { if (mx[0][p].first && vi[mx[0][p].second] != now) vi[mx[0][p].second] = now, kill++; } for (int p = 0; p < j; p++) { if (mx[1][p].first && vi[mx[1][p].second] != now) vi[mx[1][p].second] = now, kill++; } for (int p = 0; p < k; p++) { if (mx[2][p].first != inf && vi[mx[2][p].second] != now) vi[mx[2][p].second] = now, kill++; } for (int p = 0; p < l; p++) { if (mx[3][p].first != inf && vi[mx[3][p].second] != now) vi[mx[3][p].second] = now, kill++; } if (kill != K) continue; for (int p = 0; p <= K; p++) { if (mx[0][p].first && vi[mx[0][p].second] != now) maxx = max(maxx, mx[0][p].first); } for (int p = 0; p <= K; p++) { if (mx[1][p].first && vi[mx[1][p].second] != now) maxy = max(maxy, mx[1][p].first); } for (int p = 0; p <= K; p++) { if (mx[2][p].first != inf && vi[mx[2][p].second] != now) minx = min(minx, mx[2][p].first); } for (int p = 0; p <= K; p++) { if (mx[3][p].first != inf && vi[mx[3][p].second] != now) miny = min(miny, mx[3][p].first); } int nowx = maxx - minx, nowy = maxy - miny; nowx = max(nowx, 2); nowy = max(nowy, 2); if (nowx % 2) nowx++; if (nowy % 2) nowy++; ans = min(ans, nowx * 1LL * nowy); } } } } cout << ans / 4 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int arr[11]; int main() { int a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; arr[a]++; arr[b]++; arr[c]++; arr[d]++; arr[e]++; arr[f]++; int fc = 0, m = 0, n = 0, k = 0; for (int i = 1; i <= 9; i++) { if (arr[i] == 4) fc++; else if (arr[i] == 2) m++; else if (arr[i] == 6) n++; else if (arr[i] == 5) k++; } if (fc == 0 && k == 0 && n == 0) cout << "Alien"; else if (fc != 0 && m != 0 || n != 0) cout << "Elephant"; else if (fc != 0 && m == 0 || k != 0) cout << "Bear"; return 0; }
1
#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <cassert> #include <string> #include <memory.h> #include <queue> #include <cstdio> #include <cstdlib> #include <set> #include <map> #include <cctype> #include <iomanip> #include <sstream> #include <cctype> #include <fstream> #include <cmath> using namespace std; #define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++) #define REP(i, n) REP2(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define ITER(c) __typeof((c).begin()) #define PB(e) push_back(e) #define FOREACH(i, c) for(ITER(c) i = (c).begin(); i != (c).end(); ++i) #define MP(a, b) make_pair(a, b) #define PARITY(n) ((n) & 1) typedef long long ll; typedef pair<ll, ll> P; const int INF = 1000 * 1000 * 1000 + 7; const double EPS = 1e-10; int win_turn(int n, const vector<int> &rows, const vector<int> &cols, const vector<int> &ps, int w){ if(n == 1 && w > 1) return ps.size(); vector<int> rc(n, 0); vector<int> cc(n, 0); int d1 = 0, d2 = 0; REP(i, ps.size()){ int r = rows[ps[i]]; int c = cols[ps[i]]; if(r >= 0 && c >= 0){ if(++rc[r] == n) w--; if(++cc[c] == n) w--; if(r == c && ++d1 == n) w--; if(r + c + 1 == n && ++d2 == n) w--; } if(w <= 0){ // cout << i << endl; return i; } } return ps.size(); } int main(){ int n, m, u, v, p; cin >> n >> u >> v >> m; const int PMAX = 1000 * 1000; vector<int> ur(PMAX, -1), uc(PMAX, -1); vector<int> nr(PMAX, -1), nc(PMAX, -1); vector<int> ps(m); REP(i, n)REP(j, n){ cin >> p; --p; ur[p] = i; uc[p] = j; } REP(i, n)REP(j, n){ cin >> p; --p; nr[p] = i; nc[p] = j; } REP(i, m) { cin >> ps[i]; ps[i]--; } int uw = win_turn(n, ur, uc, ps, u); int nw = win_turn(n, nr, nc, ps, v); if(uw < nw){ cout << "USAGI" << endl; }else if(uw > nw){ cout << "NEKO" << endl; }else{ cout << "DRAW" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> inline bool getmin(T *a, const T &b) { if (b < *a) { *a = b; return true; } return false; } template <class T> inline bool getmax(T *a, const T &b) { if (b > *a) { *a = b; return true; } return false; } template <class T> inline void read(T *a) { char c; while (isspace(c = getchar())) { } bool flag = 0; if (c == '-') flag = 1, *a = 0; else *a = c - 48; while (isdigit(c = getchar())) *a = *a * 10 + c - 48; if (flag) *a = -*a; } const int mo = 1000000007; template <class T> T pow(T a, T b, int c = mo) { T res = 1; for (T i = 1; i <= b; i <<= 1, a = 1LL * a * a % c) if (b & i) res = 1LL * res * a % c; return res; } const int N = 210000; int a[N]; int n, l[N], r[N]; struct Qnode { int l, r, x; } q[N * 3]; int m; struct node { int mx, se, num; int tmx, tag, size; long long sum; } tree[N * 4]; void update(node &A, const node &L, const node &R) { A.sum = L.sum + R.sum; A.num = 0; A.mx = min(L.mx, R.mx); A.se = min(L.se, R.se); if (L.mx != A.mx) getmin(&A.se, L.mx); if (R.mx != A.mx) getmin(&A.se, R.mx); if (A.mx == L.mx) A.num += L.num; if (A.mx == R.mx) A.num += R.num; } void down(node &A, node &L, node &R) { if (A.tag) { int x = A.tag; A.tag = 0; L.mx += x, R.mx += x; L.se += x, R.se += x; L.tag += x, R.tag += x; L.sum += 1LL * L.size * x; R.sum += 1LL * R.size * x; } if (A.tmx) { int x = A.tmx; A.tmx = 0; bool f1 = 0, f2 = 0; if (L.mx <= R.mx) f1 = 1; if (R.mx <= L.mx) f2 = 1; if (f1) { L.mx += x; L.tmx += x; L.sum += 1LL * L.num * x; } if (f2) { R.mx += x; R.tmx += x; R.sum += 1LL * R.num * x; } } } void checkmax(int u, int L, int R, int l, int r, const int &d) { if (d <= tree[u].mx) return; else if (l <= L && r >= R && d <= tree[u].se) { tree[u].tmx += d - tree[u].mx; tree[u].sum += 1LL * tree[u].num * (d - tree[u].mx); tree[u].mx = d; return; } down(tree[u], tree[u * 2], tree[u * 2 + 1]); int mid = (L + R) >> 1; if (r <= mid) checkmax(u * 2, L, mid, l, r, d); else if (l > mid) checkmax(u * 2 + 1, mid + 1, R, l, r, d); else checkmax(u * 2, L, mid, l, mid, d), checkmax(u * 2 + 1, mid + 1, R, mid + 1, r, d); update(tree[u], tree[u * 2], tree[u * 2 + 1]); } long long query(int u, int L, int R, int l, int r) { if (l <= L && r >= R) return tree[u].sum; down(tree[u], tree[u * 2], tree[u * 2 + 1]); int mid = (L + R) >> 1; if (r <= mid) return query(u * 2, L, mid, l, r); else if (l > mid) return query(u * 2 + 1, mid + 1, R, l, r); else return query(u * 2, L, mid, l, mid) + query(u * 2 + 1, mid + 1, R, mid + 1, r); } void build(int u, int L, int R) { tree[u].size = R - L + 1; if (L == R) { tree[u].mx = tree[u].sum = 0; tree[u].num = 1; tree[u].se = 100000000; return; } int mid = (L + R) >> 1; build(u * 2, L, mid); build(u * 2 + 1, mid + 1, R); update(tree[u], tree[u * 2], tree[u * 2 + 1]); } bool cmp(const Qnode &A, const Qnode &B) { return A.r > B.r; } vector<int> v[N]; int ll[N], rr[N]; int main() { cin >> n; int mx = 0; for (int i = (1); i <= (n); ++i) { read(a + i); v[a[i]].push_back(i); getmax(&mx, a[i]); if (!l[a[i]]) l[a[i]] = i; getmax(&r[a[i]], i); } for (int i = (1); i <= (mx); ++i) if (v[i].size() > 1) ll[i] = v[i][1], rr[i], v[i][v[i].size() - 2]; for (int i = (1); i <= (mx); ++i) { int lx = 0, rx = 0, ly = n + 1, ry = 0; for (int j = (1); j <= (mx / i); ++j) { if (l[i * j]) if (!lx || l[i * j] < lx) lx = l[i * j]; if (r[i * j]) if (!rx || r[i * j] > rx) rx = r[i * j]; } if (!lx || !rx) continue; for (int j = (1); j <= (mx / i); ++j) { if (l[i * j]) { if (l[i * j] > lx) getmin(&ly, l[i * j]); if (ll[i * j]) getmin(&ly, ll[i * j]); } if (r[i * j]) { if (r[i * j] < rx) getmax(&ry, r[i * j]); if (rr[i * j]) getmax(&ry, rr[i * j]); } } if (lx && rx && rx - lx > 1) { q[++m].x = i; q[m].l = lx + 1; q[m].r = rx - 1; } if (ly < n) { q[++m].x = i; q[m].l = ly + 1; q[m].r = n; } if (ry > 1) { q[++m].x = i; q[m].l = 1; q[m].r = ry - 1; } } build(1, 1, n); long long ans = 0; sort(q + 1, q + m + 1, cmp); int j = 1; for (int i = (n); i >= (1); --i) { while (j <= m && q[j].r >= i) { checkmax(1, 1, n, q[j].l, i, q[j].x); ++j; } ans += query(1, 1, n, 1, i); } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 10; pair<int, int> p[MAXN]; long long sum[MAXN]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d%d", &p[i].second, &p[i].first); } sort(p + 1, p + 1 + n); multiset<int> st; long long now = 0; long long ans = 0; for (int i = n; i >= 1; i--) { now += p[i].second; st.insert(p[i].second); if (st.size() >= k) { now -= *st.begin(); st.erase(st.begin()); } sum[i] = now; } for (int i = 1; i <= n; i++) { ans = max(ans, 1LL * p[i].first * (1LL * p[i].second + sum[i + 1])); } printf("%lld", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } bool customCompare(const pair<int, int> &a, const pair<int, int> &b) { return a.first < b.first; } void solve() { long long n; cin >> n; for (long long i = 1; i <= n - 1; i++) cout << i + 1 << " "; cout << "1\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t = 1; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 5143; const long long Linf = 1e18 + 5413; template <class T> inline void umax(T &a, T b) { if (a < b) a = b; } template <class T> inline void umin(T &a, T b) { if (a > b) a = b; } template <class T> inline T abs(T a) { return a > 0 ? a : -a; } template <class T> inline T lcm(T a, T b) { return a / __gcd(a, b) * b; } inline int read() { int res = 0; int neg; while (true) { char ch = getchar(); if (ch >= '0' && ch <= '9' || ch == '-') { if (ch == '-') neg = -1; else neg = 1, res = ch - '0'; break; } } while (true) { char ch = getchar(); if (ch >= '0' && ch <= '9') res *= 10, res += ch - '0'; else break; } return res * neg; } const int N = 1e5 + 5; bool P[N]; vector<int> G[N]; int L[N], R[N]; inline void update(int &a, int b, int c) { if (b != -inf) umax(a, b + c); } int sub(int node, int pre) { R[node] = P[node] ? 0 : -inf; for (__typeof(G[node].begin()) it = G[node].begin(); it != G[node].end(); it++) { int u = *it; if (u == pre) continue; update(R[node], sub(u, node), 1); } return R[node]; } void dfs(int node, int pre) { vector<pair<int, int> > vals; for (__typeof(G[node].begin()) it = G[node].begin(); it != G[node].end(); it++) { int u = *it; if (u == pre) continue; vals.push_back(pair<int, int>(R[u], u)); } sort(vals.begin(), vals.end()); reverse(vals.begin(), vals.end()); for (__typeof(G[node].begin()) it = G[node].begin(); it != G[node].end(); it++) { int u = *it; if (u == pre) continue; int &rev = L[u]; rev = P[u] ? 0 : -inf; if (u != vals[0].second) update(rev, vals[0].first, 2); else if (vals.size() > 1) update(rev, vals[1].first, 2); update(rev, L[node], 1); dfs(u, node); } } int main() { int n = read(); int m = read(); int d = read(); for (int(i) = (1); (i) <= (m); (i)++) { int x = read(); P[x] = 1; } for (int(i) = (1); (i) <= (n - 1); (i)++) { int u = read(); int v = read(); G[u].push_back(v); G[v].push_back(u); } sub(1, -1); L[1] = P[1] ? 0 : -inf; dfs(1, -1); int answer = 0; for (int(i) = (1); (i) <= (n); (i)++) if (max(L[i], R[i]) <= d) answer++; cout << answer << endl; cerr << "L[3]" << ":" << (L[3]) << endl; cerr << "R[3]" << ":" << (R[3]) << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using ul = unsigned long long; using ld = long double; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, d, m, s = 0, x; cin >> n >> d >> m; vector<ll> a, b; for (int i = 0; i < (n); i++) { cin >> x; if (x > m) a.push_back(x); else b.push_back(x); } sort((a).rbegin(), (a).rend()); sort((b).rbegin(), (b).rend()); vector<ll> pa(a.size() + 1), pb(b.size() + 1); for (int i = 0; i < (a.size()); i++) pa[i + 1] = pa[i] + a[i]; for (int i = 0; i < (b.size()); i++) pb[i + 1] = pb[i] + b[i]; for (int i = 0; i < (a.size() + 1); i++) { if (i * (d + 1) - d > n) continue; s = max(s, pa[i] + pb[min((ll)b.size(), n - (i * (d + 1) - d))]); } cout << s; }
1
#include <bits/stdc++.h> using namespace std; const int N = 111; int a[N], n, c[10], g[10][10], was[10]; vector<int> b[10][10]; void dfs(int v) { for (int i = 0; i <= 6; i++) if (g[v][i]) g[v][i]--, g[i][v]--, dfs(i); a[++a[0]] = v; } void dfs2(int v) { was[v] = 1; for (int i = 0; i <= 6; i++) if (!was[i] && g[v][i]) dfs2(i); } int main() { scanf("%d", &n); if (n == 1) { puts("1 +"); return 0; } for (int i = 1; i <= n; i++) { int x, y; scanf("%d%d", &x, &y); g[x][y]++, g[y][x]++; c[x]++, c[y]++; b[x][y].push_back(i); } int id = -1, cnt = 0; for (int i = 0; i <= 6; i++) { cnt += c[i] % 2; if (c[i] % 2) id = i; } if (cnt > 2) { puts("No solution"); return 0; } cnt = 0; for (int i = 0; i <= 6; i++) if (c[i] && !was[i]) ++cnt, dfs2(i); if (cnt > 1) { puts("No solution"); return 0; } if (id == -1) { for (int i = 0; i <= 6; i++) for (int j = 0; j <= 6; j++) if (g[i][j]) dfs(i); } else { dfs(id); } for (int i = 1; i < a[0]; i++) { int v1 = a[i], v2 = a[i + 1]; if (b[v1][v2].size()) { printf("%d +\n", b[v1][v2][b[v1][v2].size() - 1]); b[v1][v2].pop_back(); } else { printf("%d -\n", b[v2][v1][b[v2][v1].size() - 1]); b[v2][v1].pop_back(); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, sum, a[1000], b[1000]; bool B[1005]; int main() { string x; cin >> x; set<int> wow; int sum = 0, sum_ = 0; bool A = 1; while (A) { A = 0; int l = 0, r = x.size() - 1; while (l < r) { while (x[l] != '(' || B[l] == 1) { l++; if (l >= r) break; } while (x[r] != ')' || B[r] == 1) { r--; if (l >= r) break; } if (l < r) { wow.insert(l + 1); wow.insert(r + 1); B[l] = B[r] = 1; A = 1; sum_ += 2; } } if (A) sum++; } cout << sum << "\n"; if (sum != 0) cout << sum_ << "\n"; set<int>::iterator it; it = wow.begin(); if (sum != 0) for (; it != wow.end(); it++) cout << *it << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 500005, INF = 0x3f3f3f3f; int ta, tb, n, T; long long t[maxn], t1[maxn]; int main(void) { cin >> n >> ta >> tb >> T; string s; char a; cin >> a; if (a == 'w') T -= tb; T--; if (T < 0) { cout << 0 << endl; return 0; } cin >> s; for (int i = 0; i < n - 1; i++) { t[i] = t[i - 1] + 1 + ta; if (s[i] == 'w') t[i] += tb; } reverse(s.begin(), s.end()); for (int i = 0; i < n - 1; i++) { t1[i] = t1[i - 1] + 1 + ta; if (s[i] == 'w') t1[i] += tb; } int res = 0; for (int i = 0; i < n; i++) { if (t1[i - 1] > T) break; int l = 0, r = n - i; while (l < r - 1) { int mid = (l + r) / 2; if (mid > 0 && t1[i - 1] + t[mid - 1] + i * ta <= T) l = mid; else r = mid; } res = max(res, l + i); } for (int i = 0; i < n; i++) { if (t[i - 1] > T) break; int l = 0, r = n - i; while (l < r - 1) { int mid = (l + r) / 2; if (mid > 0 && t[i - 1] + t1[mid - 1] + i * ta <= T) l = mid; else r = mid; } res = max(res, l + i); } cout << res + 1 << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; void reverseQueue(queue<long long int>& q) { if (q.empty()) return; long long int data = q.front(); q.pop(); reverseQueue(q); q.push(data); } int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<long long int> friends; queue<long long int> q; map<long long int, int> check; for (int i = 0, temp; i < n; i++) { cin >> temp; friends.emplace_back(temp); if (i < k) { if (check.find(friends[i]) == check.end() || check[friends[i]] == 0) { q.push(temp); check[temp] = 1; } } } for (int i = k; i < n; i++) { if (check.find(friends[i]) == check.end() || check[friends[i]] == 0) { if (q.size() == k) { check[q.front()] = 0; q.pop(); } q.push(friends[i]); check[friends[i]] = 1; } } reverseQueue(q); cout << q.size() << endl; while (!q.empty()) { cout << q.front() << " "; q.pop(); } cout << endl; return 0; }
2
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; int exist[100000]; int main(){ while(true){ int m; cin >> m; if(m == 0){ break; } for(int i = 0; i < 10000; i++){ exist[i] = 0; } exist[0] = 1; int a, b; for(int loop = 0; loop < m; loop++){ cin >> a >> b; for(int i = 9999; i >= 0; i--){ if(exist[i] > 0){ for(int j = 1; j <= b; j++){ exist[i + a * j] += exist[i]; } } } } int g; cin >> g; for(int loop = 0; loop < g; loop++){ int n; cin >> n; cout << exist[n] << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; #define N 100010 #define pb push_back inline int read() { int x=0; char ch=getchar(); while (!isdigit(ch)) ch=getchar(); while (isdigit(ch)) x=x*10+ch-'0',ch=getchar(); return x; } char s[N]; vector<int> V[N],v[N]; int d[N],vis[N],val[N],dp[N],D; inline void dfs(int x,int fa) { dp[x]=val[x]; for (auto y:v[x]) if (y!=fa) { dfs(y,x),D=max(dp[x]+dp[y],D),dp[x]=max(dp[x],dp[y]+val[x]); } } int main() { int n=read(),cnt=0; for (int i=1;i<=n-1;i++) { int x=read(),y=read(); V[x].pb(y),V[y].pb(x); d[x]++,d[y]++; } scanf("%s",s+1); queue<int> q; for (int i=1;i<=n;i++) if (d[i]==1 && s[i]=='B') q.push(i); while (!q.empty()) { int x=q.front(); q.pop(); vis[x]=true; for (auto y:V[x]) if (!vis[y]) if ((--d[y])==1 && s[y]=='B') q.push(y); } // for (int i=1;i<=n;i++) printf("%d ",d[i]); puts(""); int sum=0; for (int x=1;x<=n;x++) for (auto y:V[x]) if (!vis[x] && !vis[y]) v[x].pb(y),++sum; for (int i=1;i<=n;i++) if (!vis[i]) { if ((s[i]=='B' && (d[i]&1)) || (s[i]=='W' && !(d[i]&1))) val[i]=2,++cnt; } int rot=0; for (int i=1;i<=n;i++) if (!vis[i]) {rot=i;break;} dfs(rot,0); // printf("%d %d %d\n",sum,cnt,D); cout<<sum+cnt-D<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; void solve() { long long n; cin >> n; long long r1, p1, s1; cin >> r1 >> s1 >> p1; long long r2, p2, s2; cin >> r2 >> s2 >> p2; long long mn = max({r1 - r2 - p2, p1 - p2 - s2, s1 - s2 - r2, 0LL}); long long mx = min(r1, s2) + min(p1, r2) + min(s1, p2); cout << mn << ' ' << mx << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long T = 1, t = 0; while (t++ < T) { solve(); } }
5
#include <bits/stdc++.h> #define eb emplace_back #define all(v) (v).begin(), (v).end() #define fi first #define se second using namespace std; typedef pair <int, int> pii; int arr[301010], brr[301010]; vector <int> ta, tb; map <int, vector <int> > g; set <int> chk; int dfs(int k) { if(chk.find(k) != chk.end()) return 0; chk.insert(k); int ret = 0; for(auto i : g[k]) { if(i == k) continue; ret++; ret += dfs(i); } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i = 1; i <= n; i++) { cin >> arr[i]; arr[0] ^= arr[i]; ta.eb(arr[i]); } ta.eb(arr[0]); for(int i = 1; i <= n; i++) { cin >> brr[i]; brr[0] ^= brr[i]; tb.eb(brr[i]); } tb.eb(brr[0]); sort(all(ta)); sort(all(tb)); for(int i = 0; i <= n; i++) { if(ta[i] != tb[i]) { cout << -1; return 0; } } for(int i = 0; i <= n; i++) { g[arr[i]].eb(brr[i]); } int ans = max(1, dfs(arr[0])) - 1; if(arr[0] == brr[0] && ans != 0) ans++; for(int i = 1; i <= n; i++) { if(arr[i] != brr[i] && chk.find(arr[i]) == chk.end()) ans += dfs(arr[i]) + 1; } cout << ans; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& s, vector<T>& v) { s << '{'; for (int i = 0; i < v.size(); ++i) s << (i ? "," : "") << v[i]; return s << '}'; } template <typename S, typename T> ostream& operator<<(ostream& s, pair<S, T>& p) { return s << "(" << p.first << "," << p.second << ")"; } vector<int> bells; int main() { int N, B; cin >> N >> B; if (B > N) B = N; for (int i = 0; i < (N); ++i) { int t; cin >> t; bells.push_back(t); } int size2 = N - B; int size1 = B - size2; assert(size1 + size2 == B); assert(size1 + 2 * size2 == N); int m = 0; for (int i = 0; i < (size2); ++i) { int s = 0; s += bells[i]; s += bells[size2 * 2 - i - 1]; m = max(m, s); } int ix = size2 * 2; for (int i = 0; i < (size1); ++i) { m = max(m, bells[ix]); ix++; } cout << m << endl; }
2
#include <iostream> #include <algorithm> using namespace std; const int inf = 1<<24; const int pmax = 666001; int rem[pmax]; int p, n[6], c[6] = { 1, 5, 10, 50, 100, 500 }; void rem_init() { rem[0] = 0; for ( int i = 1; i < pmax; i++ ) { rem[i] = inf; } for ( int i = 0; i <= pmax; i++ ) { for ( int j = 0; j < 6; j++ ) { int next_i = i + c[j]; if ( next_i > pmax ) continue; rem[next_i] = min( rem[next_i], rem[i] + 1 ); } } } void solve() { int pm = 0; for ( int i = 0; i < 6; i++ ) pm += n[i] * c[i]; int answer = 1<<24; for ( int i = p; i <= p + 1000; i++ ) { int k = 0, s = 0; for ( int j = 5; j >= 0; j-- ) { if ( n[j] == 0 ) continue; int remain = i - s; if ( c[j] > remain ) continue; int use = min( remain / c[j], n[j] ); s += use * c[j]; k += use; } if ( s != i ) continue; // cout << i << "(" << k << ")" << " no oturi ha " << i-p << "(" << rem[i-p] << ")" << endl; answer = min( answer, k + rem[i-p] ); } cout << answer << endl; } int main() { rem_init(); while ( cin >> p && p ) { for ( int i = 0; i < 6; i++ ) { cin >> n[i]; } solve(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int Mod = 1000000007; const int Nmax = 110; long long Now, N, Pwr; char X[Nmax]; int main() { cin >> X; N = strlen(X); Pwr = 1LL; Now = X[N - 1] - '0'; for (int i = N - 2; i >= 0; --i) { Pwr = (1LL * Pwr * 4LL) % Mod; Now = (1LL * Now * 2LL) % Mod; if (X[i] == '1') Now = (1LL * Now + 1LL * Pwr) % Mod; } cout << Now << '\n'; }
3
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846264338327950288419716939937510; const double E = 2.7182818284590452353602874713526624977572470936999596; const double eps = 0.000000001; const int MOD = 998244353; inline int max(int u, int v) { return u > v ? u : v; } inline int min(int u, int v) { return u < v ? u : v; } inline int gcd(int u, int v) { return v ? gcd(v, u % v) : u; } template <typename T> inline void read(T& u) { char id; u = 0; bool flag = 0; id = getchar(); while (id > '9' || id < '0') { if (id == '-') flag = 1; id = getchar(); } while ((id <= '9' && id >= '0')) { u = u * 10 + id - '0'; id = getchar(); } if (flag) u *= -1; } template <typename T> inline void read(T& u, T& v) { read(u); read(v); } template <typename T> inline void read(T& u, T& v, T& tmp) { read(u); read(v); read(tmp); } long long n, m, a[1500][1500], r, c, len, f[2250000]; struct node { long long x, y, v; } str[2250000]; bool cmp(node x, node y) { return x.v < y.v; } long long power(long long a, long long n) { a %= MOD; long long res = 1; while (n) { if (n & 1) res = 1LL * res * a % MOD; a = 1LL * a * a % MOD; n >>= 1; } return res; } long long inv(long long x) { return power(x, MOD - 2) % MOD; } long long cnt, a1, a2, b1, b2, sum; void solve(int p) { f[p] = (sum + (1LL * str[p].x * str[p].x % MOD * cnt % MOD + a2 - 2LL * str[p].x % MOD * a1 % MOD) + (1LL * str[p].y * str[p].y % MOD * cnt % MOD + b2 - 2LL * str[p].y % MOD * b1 % MOD)) % MOD; f[p] = (1LL * f[p] + MOD) % MOD; f[p] = 1LL * f[p] * inv(cnt) % MOD; } inline void add(int p) { cnt++; sum += f[p]; a1 += str[p].x; a2 += 1LL * str[p].x * str[p].x % MOD; b1 += str[p].y; b2 += 1LL * str[p].y * str[p].y % MOD; a1 %= MOD; b1 %= MOD; a2 %= MOD; b2 %= MOD; sum %= MOD; } int main() { read(n, m); for (int i = (1); (i) <= (n); (i)++) for (int j = (1); (j) <= (m); (j)++) read(a[i][j]); for (int i = (1); (i) <= (n); (i)++) for (int j = (1); (j) <= (m); (j)++) str[++len] = (node){i, j, a[i][j]}; sort(str + 1, str + len + 1, cmp); for (int i = (1); (i) <= (len); (i)++) { int j; for (j = i; j <= len; j++) if (str[j].v != str[i].v) break; for (int k = (i); (k) <= (j - 1); (k)++) solve(k); for (int k = (i); (k) <= (j - 1); (k)++) add(k); i = j - 1; } long long r, c; read(r, c); long long res = 0; for (int i = (1); (i) <= (len); (i)++) if (str[i].x == r && str[i].y == c) res = f[i]; res %= MOD; res = (res + MOD) % MOD; cout << res << endl; return 0; }
5
#include<bits/stdc++.h> int main() { long long N; std::cin>>N; std::cout<<N*(N-1)/2; }
0
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; int f[N]; int getp(string s) { f[0] = 0; for (int i = 1; i < (int)s.size(); ++i) { int p = f[i - 1]; while (p && s[p] != s[i]) { p = f[p - 1]; } if (s[p] == s[i]) { f[i] = p + 1; } else { f[i] = 0; } } return f[(int)s.size() - 1]; } bool check(int *a, int *b) { return a[0] <= b[0] && a[1] <= b[1]; } void del(int *a, int *b) { b[0] -= a[0]; b[1] -= a[1]; } int cs[2], ct[2]; int add[2]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); string s, t; cin >> s >> t; int p = getp(t); for (char c : s) ++cs[c - '0']; for (char c : t) ++ct[c - '0']; for (char c : t.substr(p, t.size() - p)) ++add[c - '0']; if (!check(ct, cs)) { cout << s << '\n'; exit(0); } else { cout << t; del(ct, cs); while (check(add, cs)) { cout << t.substr(p, t.size() - p); del(add, cs); } while (cs[0]--) cout << '0'; while (cs[1]--) cout << '1'; cout << '\n'; } }
2
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 0, 1}; int dy[] = {0, -1, 1, 0}; void solve() { long long n; cin >> n; for (long long i = 1; i <= n; i++) { if (i == 1) cout << 2 << endl; else cout << i * (i + 1) * (i + 1) - (i - 1) << endl; } } int main() { ios::sync_with_stdio(false); int T = 1; while (T--) { solve(); cout << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 100000; const int maxm = 1000000; int a[15]; bool cmp(int t1, int t2) { return t1 > t2; } int main() { int tmp; memset(a, 0, sizeof(a)); for (int i = 0; i < 6; i++) { scanf("%d", &tmp); a[tmp]++; } sort(a, a + 10, cmp); if (a[0] >= 4) { if (a[0] == 6) printf("Elephant\n"); else if (a[0] == 5) printf("Bear\n"); else if (a[1] == 1 && a[2] == 1) printf("Bear\n"); else printf("Elephant\n"); } else printf("Alien\n"); return 0; }
1
#include <iostream> #include <string> using namespace std; int main() { int n; string s; cin>>n>>s; if (n%2 == 0 && s.substr(0, n/2) == s.substr(n/2)) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[1010]; int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } bool cmp(int a, int b) { return a < b; } int main() { int n, i, j; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n, cmp); for (i = 1, j = 0; i < n; i++) { if (a[j] != a[i]) a[++j] = a[i]; } n = j + 1; int tmp = gcd(a[0], a[1]); for (i = 2; i < n; i++) tmp = min(tmp, gcd(tmp, a[i])); tmp = a[n - 1] / tmp - n; if (tmp % 2 == 1) cout << "Alice" << endl; else cout << "Bob" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long p = 998244353; long long inv2 = (p + 1) / 2; const long long maxn = 5e5 + 5; vector<long long> a[maxn]; bool used[maxn]; long long dp[maxn][2]; long long po(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 0) { long long u = po(a, b / 2); return (u * u) % p; } else { long long u = po(a, b - 1); return (a * u) % p; } } void dfs(long long x) { used[x] = true; long long r = 1; long long sz = 0; for (auto v : a[x]) { if (!used[v]) { dfs(v); dp[x][0] += dp[v][1]; dp[x][0] %= p; ++sz; r *= (1 + dp[v][1] - dp[v][0]); r %= p; } } r *= po(inv2, sz); r %= p; dp[x][1] = dp[x][0] + (1 - r); dp[x][1] %= p; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; for (long long i = 0; i < (n - 1); ++i) { long long x, y; cin >> x >> y; x--; y--; a[x].push_back(y); a[y].push_back(x); } dfs(0); cout << ((dp[0][1] * po(2, n)) % p + p) % p; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; int i; int sum = 0; int e = 0; int z = 0; cin >> s; for (i = 0; i < s.length(); i++) { if ((s[i] - '0') % 2 == 0) e++; if ((s[i] - '0') == 0) z++; sum = sum + (s[i] - '0'); } if (sum % 3 == 0 && z > 0 && e >= 2) cout << "red"; else cout << "cyan"; cout << endl; } }
1
#include<cstdio> int main(){ char c; scanf("%c", &c); if(c=='a' || c=='i' || c=='u' || c=='e' || c=='o') printf("vowel"); else printf("consonant"); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; vector<pair<int, int>> a; vector<pair<int, int>> b; int main() { cin >> n; int m, dem = 0; for (int i = 1; i <= n; i++) { cin >> m; a.push_back(make_pair(i, m)); } for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (a[i].second > a[j].second || a[i].first > a[j].first) swap(a[i], a[j]); for (int i = 0; i < n; i++) if (a[i].second == a[i + 1].second) dem++; if (dem < 2) cout << "NO"; else { cout << "YES" << endl; for (auto x : a) cout << x.first << " "; cout << endl; b = a; for (int i = 0; i < n; i++) if (a[i].second == a[i + 1].second) { swap(a[i], a[i + 1]); break; } for (auto x : a) cout << x.first << " "; cout << endl; for (int i = 0; i < n - 1; i++) if (b[i].second == b[i + 1].second) swap(b[i], b[i + 1]); for (auto x : b) cout << x.first << " "; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 5; struct node { int prefmin, sufmax; bool ok; node() : prefmin(1e9), sufmax(0), ok(true) {} node operator+(const node& oth) const { node res; res.prefmin = min(prefmin, oth.prefmin); res.sufmax = max(sufmax, oth.sufmax); res.ok = (ok && oth.ok && prefmin > oth.sufmax); return res; } } ST[4 * maxn]; int N, M, Q; void update(int pos, int val, int id = 1, int l = 1, int r = 2 * N) { if (pos < l || pos > r) return; if (l == r) { if (pos & 1) { ST[id].prefmin = val; } else { ST[id].sufmax = val; } return; } int mid = (l + r) / 2; update(pos, val, id << 1, l, mid); update(pos, val, id << 1 | 1, mid + 1, r); ST[id] = ST[id << 1] + ST[id << 1 | 1]; } node get(int L, int R, int id = 1, int l = 1, int r = 2 * N) { if (R < l || r < L) return node(); if (L <= l && r <= R) return ST[id]; int mid = (l + r) / 2; return get(L, R, id << 1, l, mid) + get(L, R, id << 1 | 1, mid + 1, r); } set<int> rows[maxn]; signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M >> Q; for (int tc = 1; tc <= Q; ++tc) { int r, c; cin >> r >> c; if (rows[r].count(c)) { rows[r].erase(c); } else { rows[r].insert(c); } if (r & 1) { update(r, rows[r].empty() ? maxn : *rows[r].begin()); } else { update(r, rows[r].empty() ? 0 : *rows[r].rbegin()); } cout << (ST[1].ok ? "YES" : "NO") << '\n'; } }
6
#include <bits/stdc++.h> using namespace std; struct node { int pos; int id; } k[1000005]; bool cmp(node x, node y) { return x.pos < y.pos; } int main() { string s; cin >> s; int len = s.size(); int minn = 0; int maxx = len; for (int i = 1; i <= len; i++) { k[i].id = i; if (s[i - 1] == 'l') { k[i].pos = maxx--; } else { k[i].pos = minn++; } } sort(k + 1, k + len + 1, cmp); for (int i = 1; i <= len; i++) { printf("%d\n", k[i].id); } }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, MOD = 1e9 + 7, SQ = 450; int m; long long fac[N], inv[N]; int mark[N], dp[SQ][N], mx[SQ + 10]; string s; long long pw(long long a, long long b) { if (b == 0) return 1LL; return pw(a * a % MOD, b / 2) * (b % 2 ? a : 1LL) % MOD; } inline long long c(int k, int n) { if (k < 0 || k > n) return 0; long long ans = (fac[n] * inv[k]) % MOD; ans = (ans * inv[n - k]) % MOD; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); fac[0] = 1, inv[0] = 1; for (int i = 1; i < N; i++) fac[i] = (1LL * fac[i - 1] * i) % MOD, inv[i] = pw(fac[i], MOD - 2LL) % MOD, mark[i] = 0; int cur = 1; cin >> m >> s; while (m) { m--; int t; cin >> t; if (t == 1) cin >> s; else { int n; cin >> n; if (!mark[s.size()]) mark[s.size()] = cur, dp[cur][s.size()] = 1, mx[cur] = s.size(), cur++; long long p = 0; for (int i = mx[mark[s.size()]] + 1; i <= n; i++) { if (i == mx[mark[s.size()]] + 1) p = pw(25LL, i - s.size()); else p = p * 25LL % MOD; dp[mark[s.size()]][i] = dp[mark[s.size()]][i - 1] * 26LL % MOD; dp[mark[s.size()]][i] = 1LL * (1LL * dp[mark[s.size()]][i] + c(s.size() - 1, i - 1) * p % MOD) % MOD; } mx[mark[s.size()]] = max(mx[mark[s.size()]], n); cout << dp[mark[s.size()]][n] << '\n'; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int dp[(1 << 21)][21]; int n; int arr[1000001]; int power(int x, int y) { if (y == 0) return 1; int a = power(x, y / 2); a = (1LL * a * a) % mod; if (y % 2) a = (1LL * a * x) % mod; return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= n; i++) dp[arr[i]][0]++; int tot = 0; for (int k = 1; k <= 20; k++) { for (int i = 0; i < (1 << 20); i++) { dp[i][k] = dp[i][k - 1]; if ((i & (1 << (k - 1))) == 0) dp[i][k] += dp[i + (1 << (k - 1))][k - 1]; } } for (int i = 0; i <= 1000000; i++) { if (dp[i][20] == 0) continue; int val = power(2, dp[i][20]); val--; if (val < 0) val += mod; if (__builtin_popcount(i) % 2) { tot -= val; if (tot < 0) tot += mod; } else { tot += val; tot %= mod; } } cout << tot; }
4