solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
import sys n, m = map(int, input().split()) M = [[int(el) for el in input().split()] for _ in range(n)] res = 0 for i in range(n): res ^= M[i][0] if res: print('TAK') print(*[1 for _ in range(n)]) else: res = [1] * n for i in range(n): for j in range(m): if M[i][j] != M[i][0]: res[i] = j + 1 print('TAK') print(*res) sys.exit(0) print('NIE')
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } lint extgcd(lint a, lint b, lint &x, lint &y) { lint d = a; if (b != 0) d = extgcd(b, a % b, y, x), y -= (a / b) * x; else x = 1, y = 0; return d; } lint mod_inverse(lint a, lint m) { lint x, y; extgcd(a, m, x, y); return (m + x % m) % m; } struct UndirectedTree { int V; int root; int dfs_cnt; vector<vector<int>> edge; UndirectedTree(int N = 0) : V(N), dfs_cnt(0), edge(N) {} void add_edge(int u, int v) { edge[u].emplace_back(v); edge[v].emplace_back(u); } vector<int> par; vector<int> subtree_size; vector<int> dfs_id; void _tree_dfs(int now, int prv) { par[now] = prv; subtree_size[now] = 1; dfs_id[now] = dfs_cnt++; for (auto nxt : edge[now]) if (nxt != prv) { _tree_dfs(nxt, now); subtree_size[now] += subtree_size[nxt]; } } void tree_dfs(int r) { root = r; par.resize(V, -1); subtree_size.resize(V, -1); dfs_id.resize(V, 0); dfs_cnt = 0; _tree_dfs(root, -1); } vector<int> prev_heavy; int DT; void _tree_dfs_th(int now, int prv, int ph) { prev_heavy[now] = ph; if ((int)edge[now].size() >= DT) ph = now; for (auto nxt : edge[now]) if (nxt != prv) { _tree_dfs_th(nxt, now, ph); } } void tree_dfs_th(int B) { DT = B; prev_heavy.assign(V, -1); _tree_dfs_th(root, -1, -1); } vector<vector<int>> parinfo; void _hp_dfs(int now, int prv, int c, int r) { parinfo[r][now] = c; for (auto nxt : edge[now]) if (nxt != prv) _hp_dfs(nxt, now, c, r); } void heavy_prepare() { parinfo.resize(V); for (int now = (0), now_end_ = (V); now < now_end_; now++) if ((int)edge[now].size() >= DT) { parinfo[now].resize(V); for (auto nxt : edge[now]) if (nxt != par[now]) _hp_dfs(nxt, now, nxt, now); } } }; constexpr lint MOD = 998244353; struct BIT { using T = lint; int len; vector<T> val; BIT(int num) : len(num), val(num + 1) {} BIT() : BIT(0) {} void reset() { fill(val.begin(), val.end(), 0); } void _add(int pos, T v) { while (pos > 0 and pos <= len) { val[pos] += v; if (val[pos] >= MOD) val[pos] -= MOD; pos += pos & -pos; } } T _sum(int pos) const { T res = 0; while (pos > 0) res += val[pos], pos -= pos & -pos; return res % MOD; } void range_add(int l, int r, T v) { _add(l + 1, v); _add(r + 1, MOD - v); } T get(int x) { return _sum(x + 1); } }; vector<lint> cache; constexpr int B = 200; int main() { int N, Q; cin >> N >> Q; UndirectedTree tree(N); for (int _ = (0), __end_ = (N - 1); _ < __end_; _++) { int u, v; cin >> u >> v; tree.add_edge(u - 1, v - 1); } tree.tree_dfs(0); tree.tree_dfs_th(B); tree.heavy_prepare(); lint Ninv = mod_inverse(N, MOD); BIT bit(N); cache.resize(N); for (int _ = (0), __end_ = (Q); _ < __end_; _++) { int q, v; cin >> q >> v; v--; int bid = tree.dfs_id[v]; if (q == 1) { lint d; cin >> d; lint stsz = tree.subtree_size[v]; lint A = N - stsz; bit.range_add(0, N, d * stsz % MOD * Ninv % MOD); bit.range_add(bid, bid + stsz, d * A % MOD * Ninv % MOD); if ((int)tree.edge[v].size() >= B) { (cache[v] += d) %= MOD; } else { for (auto nxt : tree.edge[v]) if (nxt != tree.par[v]) { lint cid = tree.dfs_id[nxt], cstsz = tree.subtree_size[nxt]; bit.range_add(cid, cid + cstsz, MOD - d * cstsz % MOD * Ninv % MOD); } } } else { lint ret = bit.get(bid); while (tree.prev_heavy[v] >= 0) { int p = tree.prev_heavy[v]; int c = tree.parinfo[p][v]; ret = ret + MOD - cache[p] * tree.subtree_size[c] % MOD * Ninv % MOD; v = p; } ret %= MOD; printf("%lld\n", ret); } } }
2,700
CPP
for _ in range(int(input())): n, m = map(int, input().split()) if m>n: print(0) elif m==n: print(str(n)[-1]) else: mm = m arr = [] last = str(mm)[-1] while last not in arr: arr.append(last) mm += m last = str(mm)[-1] ## print(arr) arr = [int(i) for i in arr] number_of_elements = len(arr) tot_numbers = n//m ans = sum(arr)*(tot_numbers//number_of_elements) left = tot_numbers%number_of_elements ans += sum(arr[:left]) print(ans)
1,200
PYTHON3
a = 0 b = 0 for i in range(5): l = [int(x) for x in input().split()] for j in range(5): if l[j]: a = i b = j break print(abs(2-a) +abs(2-b))
800
PYTHON3
a,b=map(int,input().split()) p1=0 d=0 p2=0 for i in range(1,7): a1=abs(a-i) a2=abs(b-i) if a1<a2: p1+=1 elif a1==a2: d+=1 else: p2+=1 print(p1," ",d," ",p2)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m; const long long N = 155; const long long mod = 1e9 + 7; long long dp[N][N][N][2][2], s[N][N][N][2][2]; long long getsum(long long x, long long l1, long long r1, long long l2, long long r2, long long t1, long long t2) { return (s[x][l1][r2][t1][t2] - s[x][l1][l2 - 1][t1][t2] - s[x][r1 + 1][r2][t1][t2] + s[x][r1 + 1][l2 - 1][t1][t2] + mod * 2) % mod; } signed main() { cin >> n >> m; for (long long i = 1; i <= n; i++) { for (long long l = 1; l <= m; l++) for (long long r = l; r <= m; r++) { dp[i][l][r][0][0] = 1 + s[i - 1][l][r][0][0]; dp[i][l][r][0][1] = getsum(i - 1, l, r, r + 1, m, 0, 0) + getsum(i - 1, l, r, r, m, 0, 1); dp[i][l][r][1][0] = getsum(i - 1, 1, l - 1, l, r, 0, 0) + getsum(i - 1, 1, l, l, r, 1, 0); dp[i][l][r][1][1] = getsum(i - 1, 1, l - 1, r + 1, m, 0, 0) + getsum(i - 1, 1, l - 1, r, m, 0, 1) + getsum(i - 1, 1, l, r + 1, m, 1, 0) + getsum(i - 1, 1, l, r, m, 1, 1); } for (long long len = 1; len <= m; len++) for (long long l = 1; l <= m - len + 1; l++) for (long long t1 = 0; t1 <= 1; t1++) for (long long t2 = 0; t2 <= 1; t2++) { long long r = l + len - 1; dp[i][l][r][t1][t2] %= mod; s[i][l][r][t1][t2] = (s[i][l][r - 1][t1][t2] + s[i][l + 1][r][t1][t2] - s[i][l + 1][r - 1][t1][t2] + dp[i][l][r][t1][t2] + mod * 2) % mod; } } long long ans = 0; for (long long i = 1; i <= n; i++) for (long long t1 = 0; t1 <= 1; t1++) for (long long t2 = 0; t2 <= 1; t2++) ans = (ans + s[i][1][m][t1][t2]) % mod; cout << ans << endl; return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int MOD = 1000000007; const double EPS = 1e-9; int n, a[2024]; long long c[2024][2024], fact[2024]; bool vis[2024]; int main() { long long m = 0, tot = 0; fact[0] = 1; c[0][0] = 1; for (int i = 1; i <= 2000; i++) { fact[i] = (fact[i - 1] * i) % MOD; c[i][0] = 1; for (int j = 1; j <= i; j++) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD; } scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); if (a[i] != -1) vis[a[i]] = true; } for (int i = 0; i < n; i++) { if (a[i] == -1 && !vis[i + 1]) m++; if (a[i] == -1) tot++; } long long ans = 0; int sign = 1; for (int i = 1; i <= m; i++) { ans = (ans + sign * c[m][i] * fact[tot - i]) % MOD; sign = -sign; } ans = (fact[tot] - ans + MOD) % MOD; printf("%I64d\n", ans); return 0; }
2,000
CPP
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): n = I() a = LI() return '{:0.5f}'.format(sum(a) / n) print(main())
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1003; int n; int a[N]; map<int, int> mp; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; int ans = 0; for (int i = 1; i <= n; ++i) { if (a[i] == 0) continue; mp[a[i]]++; if (mp[a[i]] > 2) { cout << "-1" << endl; return 0; } else if (mp[a[i]] == 2) ++ans; } cout << ans << endl; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int s[200005]; int p[200005]; int main() { int n, d, final; cin >> n >> d; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) cin >> p[i]; s[d] += p[1]; multiset<int, greater<int> > st; for (int i = n - d + 2, j = 1; i <= n and j < d; i++, j++) st.insert(p[i]); final = d; for (int i = d - 1; i >= 1; i--) { auto it = st.lower_bound(s[d] - s[i]); if (it == st.end()) break; else st.erase(it), final--; } cout << final << endl; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long v; cin >> v; vector<long long> cost(9); for (long long i = 0; i < 9; ++i) cin >> cost[i]; long long minCost = *min_element(cost.begin(), cost.end()); if (minCost > v) { cout << -1 << endl; return 0; } long long maxPossLength = v / minCost, currentLength = 0; string str = ""; for (long long i = 9; i >= 1; --i) { long long key = i, Cost = cost[i - 1]; if (maxPossLength == currentLength) continue; while (currentLength < maxPossLength && v - Cost >= (maxPossLength - currentLength - 1) * minCost) { v -= Cost; str.push_back(char('0' + key)); currentLength++; } } cout << str << endl; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; template <class Type> using decay_t = typename decay<Type>::type; namespace cheapwine { template <class C> auto csize(C const &c) -> typename make_signed<decltype(c.size())>::type { return (typename make_signed<decltype(c.size())>::type)c.size(); } template <class T> struct BinArray { vector<T> d; BinArray(int n = 0) : d(n) {} struct ref { BinArray *obj; int p; operator T() const { T r = obj->d[p]; for (int i = p, ed = p & (p + 1); i != ed; i &= i - 1) r -= obj->d[i - 1]; return r; } void add(T const &rv) const { for (int i = p; i < (int)obj->d.size(); i |= i + 1) obj->d[i] += rv; } const ref &operator+=(T const &rv) const { if (rv) add(rv); return *this; } const ref &operator=(T const &rv) const { return *this += rv - T(); } }; const ref operator[](int n) { return {this, n}; } T operator()(int n) const { if (n < 0) return {0}; T r = d[n]; for (n &= n + 1; n; n &= n - 1) r += d[n - 1]; return r; } }; } // namespace cheapwine using namespace cheapwine; string s; int n; void contest_main() { cin >> s; n = csize(s); BinArray<int> ae(n + 1), ao(n + 1), be(n + 1), bo(n + 1); for (decay_t<decltype(n)> i = 0; i < (n); ++i) { if (i & 1) { if (s[i] == 'a') { ao[i] += 1; } else { bo[i] += 1; } } else { if (s[i] == 'a') ae[i] += 1; else be[i] += 1; }; }; ; long long co, ce; co = ce = 0; for (decay_t<decltype(n)> i = 0; i < (n); ++i) { if (i & 1) { if (s[i] == 'a') { int oc = ao(n) - ao(i - 1), ec = ae(n) - ae(i - 1); ; ; co += oc; ce += ec; } else { int oc = bo(n) - bo(i - 1), ec = be(n) - be(i - 1); co += oc; ce += ec; } } else { if (s[i] == 'a') { int oc = ao(n) - ao(i - 1), ec = ae(n) - ae(i - 1); co += ec; ce += oc; } else { int oc = bo(n) - bo(i - 1), ec = be(n) - be(i - 1); co += ec; ce += oc; } }; ; } cout << ce << ' ' << co << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); contest_main(); return 0; }
2,000
CPP
s=input().split() b=0 k1=0 n=int(s[0]) k=int(s[1]) l=list(map(int,input().split())) i=0 p=l[k-1] if l[k-1]==0 and l[0]==0 : b=1 while i<=n-1 : if l[i]>= p and b!=1 and l[i]!=0 : k1=k1+1 i=i+1 print(k1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int arr[1000000]; long long ltor[1000000]; long long rtol[1000000]; bool bin_search(long long x, int l, int r) { int mid = (l + r) / 2; if (x == rtol[mid]) return true; if (l == r) return false; if (x < rtol[mid]) return bin_search(x, mid + 1, r); else return bin_search(x, l, mid); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; ltor[i] = (i != 0) ? ltor[i - 1] + arr[i] : arr[i]; } for (int i = n - 1; i > -1; i--) rtol[i] = (i != n - 1) ? rtol[i + 1] + arr[i] : arr[i]; long long ans = 0; for (int i = 0; i < n - 1; i++) { if (bin_search(ltor[i], i + 1, n - 1)) ans = max(ans, ltor[i]); } cout << ans; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { int k, r; cin >> k >> r; for (int i = 1;; ++i) { k *= 3; r *= 2; if (k > r) { cout << i << endl; break; } } }
800
CPP
#include <bits/stdc++.h> using namespace std; int n, x[300010], p; int main() { int i, j, k; scanf("%d", &n); printf("1"); for (i = 1, k = n; i <= n; i++) { scanf("%d", &j); x[j] = 1; p++; while (x[k]) k--, p--; printf(" %d", p + 1); } printf("\n"); return 0; }
1,500
CPP
n = int(input()) sList = list(map(int,input().split(' '))) sList = sorted(sList) smallSum = 0 wantedSum = 0 count = 0 for i in range(n): smallSum += sList[i] while wantedSum <= smallSum: wantedSum += sList[n-1] smallSum -= sList[n-1] count += 1 n -= 1 print(count)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int arr[15]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; ++i) cin >> arr[i]; for (int bit = 0; bit < (1 << n); ++bit) { int deg = 0; for (int i = 0; i < n; ++i) if (bit & (1 << i)) deg += arr[i]; else deg -= arr[i]; if (deg % 360 == 0) { cout << "YES\n"; return 0; } } cout << "NO\n"; }
1,200
CPP
n,m,k = map(int,input().split()) a = list(map(len,input().replace(" ","").split("0"))) b = list(map(len,input().replace(" ","").split("0"))) p = [(i,k//i) for i in range(1,int(k**0.5)+1) if (k%i==0)] p += [(j,i) for i,j in p if (i!=j)] ans = 0 for i,j in p: A = 0 B = 0 for l in a: if (i<=l): A += l-i+1 for l in b: if (j<=l): B += l-j+1 ans += A*B print(ans)
1,500
PYTHON3
n=int(input()) a=list(map(int,input().split()))[:n] c=0 if a.count(max(a))>1: x=a.index(max(a)) for i in range(x,0,-1): c=c+1 a[i],a[i-1]=a[i-1],a[i] else: x=a.index(max(a)) for i in range(x,0,-1): c=c+1 a[i],a[i-1]=a[i-1],a[i] if a.count(min(a))>1: a=a[::-1] x=a.index(min(a)) a=a[::-1] for i in range(n-x-1,n-1): c=c+1 else: x=a.index(min(a)) for i in range(x,n-1): c=c+1 print(c)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int N, K; int main(int argc, char** argv) { scanf("%d%d", &N, &K); if ((!K && N != 1) || N * 9 < K) puts("-1 -1"); else { int k = K; string s, second; for (int i = (1); i <= (N); i++) { if (K > 9) s.push_back('9'), K -= 9; else if (i != N && K > 1) s.push_back((K - 1) + '0'), K = 1; else if (i != N) s.push_back('0'); else s.push_back(K + '0'); } K = k; for (int i = (1); i <= (N); i++) { if (K >= 9) second.push_back('9'), K -= 9; else if (K) second.push_back(K + '0'), K = 0; else second.push_back('0'); } reverse((s).begin(), (s).end()); printf("%s %s\n", s.c_str(), second.c_str()); } return 0; }
1,400
CPP
# A. Raising Bacteria print(bin(int(input())).count('1'))
1,000
PYTHON3
/** Nguyen Anh Tu **/ #include <bits/stdc++.h> #define maxn 200005 #define maxc 1000000007 #define MOD 1000000007 #define task "" #define mp make_pair #define pb push_back #define ep emplace_back #define F first #define S second #define pii pair<int,int> #define ll long long #define bit(p,x) ((x>>p) & 1) #define remain(a,b) (a+b >= MOD) ? (a+b - MOD) : (a+b) #define mid ((l + r)/2) #define Time cerr << "Time collapse : " << fixed << setprecision(3) << 1.000*clock()/CLOCKS_PER_SEC using namespace std; int n, p[maxn], w[maxn], c[maxn], cnt[maxn]; vector<int> a[maxn]; pii edge[maxn]; void Solve(){ cin >> n; ll ans = 0; for(int i = 1; i <= n; ++ i){ cin >> w[i]; a[i].clear(); cnt[i] = 0; ans += w[i]; } multiset<pii> s; for(int i = 1; i < n; ++ i){ int u, v; cin >> u >> v; cnt[u] ++; cnt[v] ++; a[u].pb(v); a[v].pb(u); } for(int i = 1; i <= n; ++ i){ for(int j = 2; j <= cnt[i]; ++ j){ s.insert({w[i], i}); } } cout << ans << ' '; for(int i = 2; i <= n - 1; ++ i){ auto it = s.end(); -- it; pii x = *it; s.erase(it); ans += x.F; cout << ans << ' '; } cout << '\n'; } int main(){ ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); if(fopen(task".inp","r")){ freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } int t; cin >> t; while(t --){ Solve(); } Time; return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; long long int p, q; int n; long long int ara[100]; int main() { cin >> p >> q >> n; for (int i = 0; i < n; i++) cin >> ara[i]; for (int i = 0; i < n - 1; i++) { if (q > p / ara[i]) { cout << "NO" << endl; return 0; } p -= q * ara[i]; swap(p, q); } if (q != 0 && p % q == 0 && p / q == ara[n - 1]) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
1,700
CPP
from math import * from bisect import bisect_left, bisect_right from queue import Queue from sys import stdin, stdout from collections import Counter input = lambda: stdin.readline().strip() fast_print = stdout.write t = int(input()) for _ in range(t): n = int(input()) ls = list(map(int, input().split())) d = {} visited = {} for i in range(n): d[ls[i]] = i visited[i] = False cur = 1 flag = 0 while True: start = cur if cur==n+1: break while True: visited[d[cur]] = True if cur!=start and d[cur]!=d[cur-1]+1: fast_print("No\n") flag = 1 break if d[cur]+1==n or visited[d[cur]+1]: cur+=1 break cur+=1 if flag==1: break if flag!=1: fast_print("Yes\n")
1,500
PYTHON3
t = int(input()) from collections import Counter def check(c): # print(c) left = 0 for k in c: if not left: if c[k + 1] == 0: left += 1 else: if c[k + 1] == 0: # print('here') return False # print(k, c[k + 1], left) return True for _ in range(t): n = int(input()) arr = map(int, input().split()) c = Counter(arr) if check(c): print('YES') else: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100005; const int inf = 1000 * 1000 * 1000; const int mod = 1000 * 1000 * 1000 + 7; string s, S; int main() { cin >> s; int ind = -1; for (int i = (int)s.size() - 1; i >= 0; i--) { if (s[i] != '0') { ind = i; break; } } int a = 0, b = ind; while (a <= b) { if (s[a] != s[b]) { cout << "NO" << endl; return 0; } a++; b--; } cout << "YES" << endl; return 0; }
900
CPP
import os from io import BytesIO input = BytesIO(os.read(0, os.fstat(0).st_size)).readline MOD = 998244353 MODF = 1.0*MOD MODF_inv = 1.0/MODF from math import trunc def quickmod(a): return a-MODF*trunc(a*MODF_inv) def main(): n = int(input()) a = [int(i) for i in input().split()] f0, f1 = [1.0] * 201, [0.0] * 201 nf0, nf1 = [0.0] * 201, [0.0] * 201 for i in range(n): if a[i] == -1: for j in range(200): nf0[j + 1] = quickmod(nf0[j] + f0[j] + f1[j]) nf1[j + 1] = quickmod(nf1[j] - f0[j] + f0[j + 1] - f1[j] + f1[200]) else: for j in range(200): nf0[j + 1], nf1[j + 1] = nf0[j], nf1[j] if j + 1 == a[i]: nf0[j + 1] = quickmod(nf0[j] + f0[j] + f1[j]) nf1[j + 1] = quickmod(nf1[j] - f0[j] + f0[j + 1] - f1[j] + f1[200]) f0, f1, nf0, nf1 = nf0, nf1, f0, f1 nf0[0]=0.0 os.write(1, str(int(quickmod(f1[200]))%MOD).encode()) if __name__ == '__main__': main()
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> inline T sqr(T x) { return x * x; } const double PI = acos(-1.0); using namespace std; struct Node { int l, r; int mx; double p; int dep; vector<Node *> son; vector<double> dp; void init(int l, int r, double p) { this->l = l; this->r = r; this->p = p; son.clear(); dp.clear(); } Node() {} } nd[30005]; int cnt; Node *newNode(int l, int r, double p) { nd[cnt].init(l, r, p); return &nd[cnt++]; } struct Item { int l, r; double p; bool operator<(const Item &it) const { if (l == it.l) return r > it.r; return l < it.l; } } it[30005]; int n, m; Node *buildTree(int l, int r, double p, int &i) { Node *rt = newNode(l, r, p); if (i == m) return rt; for (; i < m;) { if (l <= it[i].l && it[i].r <= r) { i++; Node *son = buildTree(it[i - 1].l, it[i - 1].r, it[i - 1].p, i); rt->son.push_back(son); } else break; } int sz = rt->son.size(); if (sz) { int last = l - 1; for (int i = 0; i < sz; i++) { Node *ch = rt->son[i]; if (ch->l > last + 1) { Node *son = buildTree(last + 1, ch->l - 1, 0.0, i); rt->son.push_back(son); } last = ch->r; } if (last != r) { Node *son = buildTree(last + 1, r, 0.0, i); rt->son.push_back(son); } } return rt; } int a[10 * 30005]; double buffer[30005]; void dfs(Node *rt) { int sz = rt->son.size(); rt->dep = 1; rt->mx = 0; for (int i = 0; i < sz; i++) { dfs(rt->son[i]); rt->dep = max(rt->dep, rt->son[i]->dep + 1); rt->mx = max(rt->mx, rt->son[i]->mx); } rt->dp.resize(rt->dep + 1); fill(rt->dp.begin(), rt->dp.end(), 0); if (sz == 0) { for (int i = rt->l; i <= rt->r; i++) rt->mx = max(rt->mx, a[i]); rt->dp[0] = 1 - rt->p; rt->dp[1] = 1; } else { for (int i = 0; i <= rt->dep; i++) { int val = rt->mx + i; buffer[i] = 1; for (int j = 0; j < sz; j++) { Node *ch = rt->son[j]; int offset = val - ch->mx; offset = min(offset, ch->dep); buffer[i] *= ch->dp[offset]; } } for (int i = 0; i <= rt->dep; i++) { if (i + 1 <= rt->dep) rt->dp[i + 1] += buffer[i] * rt->p; rt->dp[i] += buffer[i] * (1 - rt->p); } } } int main(void) { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 0; i < m; i++) scanf("%d %d %lf", &it[i].l, &it[i].r, &it[i].p); sort(it, it + m); int i = 0; Node *root = buildTree(1, n, 0.0, i); dfs(root); double ans = 0; int sz = root->dp.size(); for (int j = sz - 1; j >= 0; j--) { if (j) root->dp[j] -= root->dp[j - 1]; ans += (root->mx + j + 0.0) * root->dp[j]; } printf("%.10lf\n", ans); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) { cout << "Washington" << endl; } else if (n == 2) { cout << "Adams" << endl; } else if (n == 3) { cout << "Jefferson" << endl; } else if (n == 4) { cout << "Madison" << endl; } else if (n == 5) { cout << "Monroe" << endl; } else if (n == 6) { cout << "Adams" << endl; } else if (n == 7) { cout << "Jackson" << endl; } else if (n == 8) { cout << "Van Buren" << endl; } else if (n == 9) { cout << "Harrison" << endl; } else if (n == 10) { cout << "Tyler" << endl; } else if (n == 11) { cout << "Polk" << endl; } else if (n == 12) { cout << "Taylor" << endl; } else if (n == 13) { cout << "Fillmore" << endl; } else if (n == 14) { cout << "Pierce" << endl; } else if (n == 15) { cout << "Buchanan" << endl; } else if (n == 16) { cout << "Lincoln" << endl; } else if (n == 17) { cout << "Johnson" << endl; } else if (n == 18) { cout << "Grant" << endl; } else if (n == 19) { cout << "Hayes" << endl; } else if (n == 20) { cout << "Garfield" << endl; } else if (n == 21) { cout << "Arthur" << endl; } else if (n == 22) { cout << "Cleveland" << endl; } else if (n == 23) { cout << "Harrison" << endl; } else if (n == 24) { cout << "Cleveland" << endl; } else if (n == 25) { cout << "McKinley" << endl; } else if (n == 26) { cout << "Roosevelt" << endl; } else if (n == 27) { cout << "Taft" << endl; } else if (n == 28) { cout << "Wilson" << endl; } else if (n == 29) { cout << "Harding" << endl; } else if (n == 30) { cout << "Coolidge" << endl; } else if (n == 31) { cout << "Hoover" << endl; } else if (n == 32) { cout << "Roosevelt" << endl; } else if (n == 33) { cout << "Truman" << endl; } else if (n == 34) { cout << "Eisenhower" << endl; } else if (n == 35) { cout << "Kennedy" << endl; } else if (n == 36) { cout << "Johnson" << endl; } else if (n == 37) { cout << "Nixon" << endl; } else if (n == 38) { cout << "Ford" << endl; } else if (n == 39) { cout << "Carter" << endl; } else if (n == 40) { cout << "Reagan" << endl; } }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t, b, c, j, k, n, i; cin >> t; while (t--) { string s; cin >> s; n = s.size(); k = 0; j = 0; c = 0; for (i = 0; i < n; i++) { if (s[i] == '+') c++; else { c--; if ((c + j) < 0) { k += (i + 1); j++; } } } cout << k + n << endl; } return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; long long a[200005]; queue<long long> q; void solve() { int n; long long t; scanf("%d%lld", &n, &t); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); q.push(a[i]); } long long ans = 0; while (!q.empty()) { long long sum = 0; int num = q.size(), o = 0, l = 0; while (o < num) { o++; long long p = q.front(); q.pop(); if (t >= p) { t -= p; l++; sum += p; ans++; q.push(p); } } if (sum == 0) break; long long tmp = t / sum; ans += l * tmp; t -= tmp * sum; } printf("%lld\n", ans); } int main() { solve(); return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; long long n, sum, n1, pick, ans, t, a[N], b[N], f[N], f1[N], m, t1; int main() { scanf("%lld", &n); for (int i = 1; i <= 8; i++) { scanf("%lld", &a[i]); sum = sum + a[i] * i; } if (sum <= n) return printf("%lld\n", sum), 0; n1 = std::max((long long)0, n - 840), n = n - n1; for (int i = 1; i <= 8; i++) { if (a[i] < n1 / i) pick = a[i]; else pick = n1 / i; if (a[i] - pick < 10) pick = std::max((long long)0, a[i] - 10); ans = ans + pick * i, n1 = n1 - pick * i, a[i] = a[i] - pick; } n += n1; for (int i = 1; i <= 8; i++) { t = 1; while (a[i] > 0) { t1 = std::min(t, a[i]); a[i] = a[i] - t1; f[++m] = t1 * i; t <<= 1; } } for (long long i = 1; i <= m; i++) for (long long j = n; j >= f[i]; j--) f1[j] = std::max(f1[j], f1[j - f[i]] + f[i]); printf("%lld\n", ans + f1[n]); return 0; }
2,300
CPP
string, flag = input().strip(), True for i in range(len(string)): if string[i] == "0": print(string[:i] + string[i + 1:]) flag = False break if flag: print(string[:-1])
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int isPrime(long long int n) { long long int flag = 1; for (int i = 2; i <= sqrt(n); i++) { if ((n % i) == 0) { flag = 0; break; } } if (n == 1) return false; else if (flag == 0) return false; else return true; } bool isPower(long long int n) { if (n == 0 || n == 1) return false; else return (ceil(log2(n)) == floor(log2(n))); } int main() { long long int n; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } double sum = 0.0; sort(a, a + n); if (n % 2 == 0) { for (int i = 0; i < n; i++) { sum = sum + ((a[i + 1] * a[i + 1]) - (a[i] * a[i])); i++; } sum = sum * double(3.14159256); cout << fixed << setprecision(4) << sum << endl; } if (n % 2 != 0) { for (int i = 1; i < n; i++) { sum = sum + ((a[i + 1] * a[i + 1]) - (a[i] * a[i])); i++; } sum = sum + (a[0] * a[0]); sum = sum * double(3.14159256); cout << fixed << setprecision(4) << sum << endl; } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; const int N = 6e5; const long long mo = 1e9 + 7; const long long inf = 1e9; const long long Inf = 1e18; int lz[N], a[N], ls[N], rs[N], mi[N], mx[N]; bool ok[N]; char s[N]; void f(int x, int l, int r, int y, int z, int L, int R) { if (l == L && r == R && (!z || (L == R))) { if (z) ok[x] ^= 1; lz[x] += y; mi[x] += y; mx[x] += y; ls[x] -= y; rs[x] -= y; return; } int m = (L + R) / 2; if (lz[x]) { lz[x * 2] += lz[x]; mi[x * 2] += lz[x]; mx[x * 2] += lz[x]; ls[x * 2] -= lz[x]; rs[x * 2] -= lz[x]; lz[x * 2 + 1] += lz[x]; mi[x * 2 + 1] += lz[x]; mx[x * 2 + 1] += lz[x]; ls[x * 2 + 1] -= lz[x]; rs[x * 2 + 1] -= lz[x]; lz[x] = 0; } if (r <= m) f(x * 2, l, r, y, z, L, m); else if (l > m) f(x * 2 + 1, l, r, y, z, m + 1, R); else { f(x * 2, l, m, y, z, L, m); f(x * 2 + 1, m + 1, r, y, 0, m + 1, R); } ok[x] = ok[x * 2] | ok[x * 2 + 1]; mi[x] = min(mi[x * 2], mi[x * 2 + 1]); if (!ok[x * 2]) { mx[x] = mx[x * 2 + 1]; ls[x] = ls[x * 2 + 1]; a[x] = a[x * 2 + 1]; rs[x] = max(mx[x * 2 + 1] - mi[x * 2] * 2, rs[x * 2 + 1]); } else if (!ok[x * 2 + 1]) { mx[x] = mx[x * 2]; rs[x] = rs[x * 2]; a[x] = a[x * 2]; ls[x] = max(mx[x * 2] - mi[x * 2 + 1] * 2, ls[x * 2]); } else { mx[x] = max(mx[x * 2], mx[x * 2 + 1]); ls[x] = max(mx[x * 2] - mi[x * 2 + 1] * 2, max(ls[x * 2], ls[x * 2 + 1])); rs[x] = max(mx[x * 2 + 1] - mi[x * 2] * 2, max(rs[x * 2], rs[x * 2 + 1])); a[x] = max(max(ls[x * 2] + mx[x * 2 + 1], mx[x * 2] + rs[x * 2 + 1]), max(a[x * 2], a[x * 2 + 1])); } } int main() { int n, m; scanf("%d%d", &n, &m); scanf("%s", s + 1); f(1, 0, 0, 0, 1, 0, n * 2 - 2); for (int i = (1); i <= (n * 2 - 2); ++i) { if (s[i] == '(') f(1, i, n * 2 - 2, 1, 1, 0, n * 2 - 2); else f(1, i, n * 2 - 2, -1, 0, 0, n * 2 - 2); } printf("%d\n", a[1]); for (int i = (1); i <= (m); ++i) { int l, r; scanf("%d%d", &l, &r); if (l > r) swap(l, r); if (s[l] != s[r]) { if (s[l] == '(') f(1, l, r - 1, -2, 1, 0, n * 2 - 2); else f(1, l, r - 1, 2, 1, 0, n * 2 - 2); f(1, r, r, 0, 1, 0, n * 2 - 2); swap(s[l], s[r]); } printf("%d\n", a[1]); } }
2,700
CPP
n = int(input()) a = list(map(int, input().split())) a.sort() print(a[(n-1)//2])
800
PYTHON3
for i in range(int(input())): n=int(input()) if n%4==0: c=n//4 else: c=(n//4)+1 l=[9]*(n-c) l2=[8]*c l3=l+l2 print(*l3,sep='')
1,000
PYTHON3
def solve(): w = input() a = [0]*len(w) an=0 for i in range(0,len(w)-2): if(a[i]==0 and w[i]==w[i+1]): an+=1 a[i+1]+=1 if(a[i]==0 and w[i]==w[i+2]): an+=1 a[i+2]+=1 if(len(w)>1): i=len(w)-2 if(a[i]==0 and w[i]==w[i+1]): an+=1 a[i+1]+=1 print(an) t = int(input()) for s in range(0,t): solve()
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int const N = 3000 + 20; int n, dp[N], mx1, mx2, ans; vector<pair<int, bool>> adj[N]; void dfs1(int v, int p = -1) { for (auto x : adj[v]) { int u = x.first, f = x.second; if (u != p) { dfs1(u, v); dp[v] += dp[u] + (!f); } } return; } void dfs2(int v, int p = -1) { for (auto x : adj[v]) { int u = x.first, f = x.second; if (u != p) { dp[u] = dp[v]; f ? dp[u]++ : dp[u]--; dfs2(u, v); } } return; } pair<int, int> dfs3(int v, int p = -1) { int m1 = 0, m2 = 0; for (auto x : adj[v]) { int u = x.first, f = x.second; if (u != p) { m2 = max(m2, dfs3(u, v).first + (f ? -1 : 1)); if (m2 > m1) swap(m1, m2); } } return {m1, m2}; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; ans = n - 1; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; x--, y--; adj[x].push_back({y, 1}); adj[y].push_back({x, 0}); } dfs1(0); dfs2(0); for (int i = 0; i < n; i++) { pair<int, int> cur = dfs3(i); mx1 = cur.first, mx2 = cur.second; ans = min(ans, dp[i] - mx1 - mx2); } return cout << ans, 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; const int mod1 = 998244353; const int mod2 = 1e9 + 7; const int b1 = 337; const int b2 = 421; const int inf = 2147483647; struct Pair { int x[2] = {}; bool operator<(const Pair a) const { return x[0] < a.x[0] || x[0] == a.x[0] && x[1] < a.x[1]; } }; struct BitSet { unsigned long long b[16] = {}; inline void reset() { memset(b, 0, sizeof(b)); } inline void set(int x) { b[x / 64] |= (1ull << (x % 64)); } inline void operator^=(const BitSet &a) { for (int i = 0; i < 16; ++i) b[i] ^= a.b[i]; } inline Pair getHashVal() { Pair ans; for (int i = 0; i < 16; ++i) { ans.x[0] = (1ll * ans.x[0] * b1 % mod1 + b[i] % mod1) % mod1; ans.x[1] = (1ll * ans.x[1] * b2 % mod2 + b[i] % mod2) % mod2; } return ans; } }; int n, s, d; BitSet bs[31], sta[1 << 20], cur; int cnt[1 << 20], m; map<Pair, int> mp; void init() { m = min(s, 20); for (int i = 0; i < m; ++i) { cnt[1 << i] = 1; sta[1 << i] = bs[i]; } for (int i = 0; i < (1 << m); ++i) { if (i != (i & -i)) { cnt[i] = cnt[i - (i & -i)] + 1; sta[i] = sta[i - (i & -i)]; sta[i] ^= sta[i & -i]; } Pair x = sta[i].getHashVal(); if (!mp.count(x) || mp[x] > cnt[i]) { mp[x] = cnt[i]; } } for (int i = 0; i < (s - m); ++i) { sta[1 << i] = bs[i + m]; } for (int i = 1; i < (1 << (s - m)); ++i) { if (i == (i & -i)) continue; sta[i] = sta[i - (i & -i)]; sta[i] ^= sta[i & -i]; } } int getans() { BitSet now; int ans = inf; for (int i = 0; i < (1 << (s - m)); ++i) { if (cnt[i] >= ans) continue; now = cur, now ^= sta[i]; Pair x = now.getHashVal(); if (mp.count(x) && mp[x] + cnt[i] < ans) { ans = mp[x] + cnt[i]; } } return ans == inf ? -1 : ans; } int main() { scanf("%d%d%d", &n, &s, &d); for (int i = 0; i < s; ++i) { int c; scanf("%d", &c); for (int j = 0; j < c; ++j) { int u; scanf("%d", &u); u--; bs[i].set(u); } } init(); vector<int> ans; for (int i = 0; i < d; ++i) { int t; scanf("%d", &t); cur.reset(); while (t--) { int u; scanf("%d", &u); u--; cur.set(u); } ans.push_back(getans()); } for (int i = 0; i < ans.size(); ++i) printf("%d\n", ans[i]); puts(""); }
2,600
CPP
#include <bits/stdc++.h> using namespace std; const int oo = 2000000001; vector<int> Values; int N, K, Solution; inline int Abs(const int value) { if (value < 0) return -value; return value; } bool CanAssign(const int maxDifference) { vector<int> DP[2]; DP[0] = DP[1] = vector<int>(N, N); DP[0][0] = 0; DP[1][0] = 1; for (int n = 1; n < N; ++n) { DP[0][n] = n; for (int replace = 0; replace + 1 <= n && replace < DP[0][n]; ++replace) { int difference = Abs(Values[n] - Values[n - replace - 1]); int newDifference = difference / (replace + 1); if (difference % (replace + 1) != 0) ++newDifference; if (newDifference <= maxDifference) DP[0][n] = min(DP[0][n], DP[0][n - replace - 1] + replace); } DP[1][n] = min(DP[0][n - 1], DP[1][n - 1]) + 1; } return (DP[0][N - 1] <= K || DP[1][N - 1] <= K); } void Solve() { Solution = oo; int left = 0, right = oo; while (left <= right) { int middle = (1LL * left + 1LL * right) / 2LL; if (CanAssign(middle)) { right = middle - 1; Solution = middle; } else { left = middle + 1; } } } void Read() { cin >> N >> K; Values = vector<int>(N); for (int i = 0; i < N; ++i) cin >> Values[i]; } void Print() { cout << Solution << "\n"; } int main() { Read(); Solve(); Print(); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; set<string> S; char buf[1000]; int main() { int ret = 0; while (true) { if (!(cin.getline(buf, 999))) break; if (buf[0] == '+') { string tec(buf + 1); S.insert(tec); } else if (buf[0] == '-') { string tec(buf + 1); S.erase(tec); } else { int tec = 0; while (buf[tec] != ':') tec++; ret += strlen(buf + tec + 1) * S.size(); } } cout << ret << endl; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t; cin >> t; while (t--) { int n, k; cin >> n >> k; string s; cin >> s; int w = 0; for (auto i : s) { if (i == 'W') { w++; } } if (w == 0) { cout << max(2 * k - 1, 0) << "\n"; continue; } w += k; if (w >= n) { cout << n * 2 - 1 << "\n"; continue; } vector<long long int> v; long long int val = 0; for (long long int i = 0; i < n; i++) { if (s[i] == 'L') { val++; } else { v.push_back(val); val = 0; } } if (val > 0) { v.push_back(val); } if (s[0] == 'L') { v.erase(v.begin()); } if (s[n - 1] == 'L') { v.erase(v.begin() + v.size() - 1); } long long int p = 0, i; sort(v.begin(), v.end()); for (i = 0; i < v.size(); i++) { if (p + v[i] > k) { break; } p += v[i]; } long long int wl = v.size() - i; cout << 2 * w - wl - 1 << "\n"; } return 0; }
1,400
CPP
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) b = [0] * (n + 2) c = 1 d = a[0] ans = [a[0]] b[a[0]] = 1 for i in range(1, len(a)): if a[i] == a[i - 1]: while b[c] != 0: c += 1 if c > a[i]: ans = -1 break else: b[c] = 1 ans.append(c) else: ans.append(a[i]) b[a[i]] = 1 if ans == -1: print(ans) else: print(*ans)
1,200
PYTHON3
from sys import stdin ############################################################### def iinput(): return int(stdin.readline()) def minput(): return map(int, stdin.readline().split()) def linput(): return list(map(int, stdin.readline().split())) ############################################################### n, m = minput() a = linput() b = linput() a.sort(); b.sort() i = j = 0 while i<n and j<m: if b[j] >= a[i]: i+=1 j+=1 else: j+=1 print(n-i)
1,200
PYTHON3
a = input() if a.find('0000000')!=-1 or a.find('1111111')!=-1: print('YES') else: print("NO")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { long long n, k, j = 1, x, second = 0; cin >> n >> k; vector<long long> a(n); vector<long long> b(n); for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); for (long long i = 0; i < k; i++) { x = *(b.end() - j); if (a[i] < x) { j++; swap(a[i], x); } else break; } for (long long i = 0; i < n; i++) second += a[i]; cout << second << "\n"; } return 0; }
800
CPP
n = int(input()) i = 0 for x in range(n): a,b,c = input().split() if int(a) and int(b) and int(c) == 1: i = i + 1 elif int(a) and int(b) == 1: i = i + 1 elif int(a) and int(c) == 1: i = i + 1 elif int(b) and int(c) == 1: i = i + 1 else: i = i + 0 print(i)
800
PYTHON3
#include <bits/stdc++.h> long long int mod = 1000000007; using namespace std; int a[] = {1, 2, 3, 4, 6, 12}; int b[] = {12, 6, 4, 3, 2, 1}; int main() { int t; cin >> t; string s; for (int i = 0; i < t; i++) { cin >> s; int ans[6] = {0}; int ct = 0; for (int i = 0; i < 12; i++) { if (s[i] == 'X') { ct++; } } if (ct == 12) { cout << 6 << " "; for (int i = 0; i < 6; i++) { cout << a[i] << "x" << b[i] << " "; } cout << endl; continue; } if (ct == 0) { cout << 0 << endl; continue; } if (ct >= 1) { ans[0] = 1; ct = 1; } for (int i = 0; i < 6; i++) { if (s[i] == 'X' && s[i + 6] == 'X') { ans[1] = 1; ct++; break; } } for (int i = 0; i <= 3; i++) { if (s[i] == 'X' && s[i + 4] == 'X' && s[i + 8] == 'X') { ans[2] = 1; ct++; break; } } for (int i = 0; i <= 2; i++) { if (s[i] == 'X' && s[i + 3] == 'X' && s[i + 6] == 'X' && s[i + 9] == 'X') { ans[3] = 1; ct++; break; } } for (int i = 0; i <= 1; i++) { if (s[i] == 'X' && s[i + 2] == 'X' && s[i + 4] == 'X' && s[i + 6] == 'X' && s[i + 8] == 'X' && s[i + 10] == 'X') { ans[4] = 1; ct++; break; } } cout << ct << " "; for (int i = 0; i <= 4; i++) { if (ans[i] == 1) { cout << a[i] << "x" << b[i] << " "; } } cout << endl; } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 110; int n, k; int q[maxn], s[maxn], p[maxn]; int tmp[maxn]; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) p[i] = i; for (int i = 1; i <= n; i++) scanf("%d", &q[i]); bool flag = true; for (int i = 1; i <= n; i++) { scanf("%d", &s[i]); if (s[i] != i) flag = false; } if (flag) { cout << "NO" << endl; return 0; } for (int i = 1; i <= n; i++) p[i] = i; int k1 = 0, k2 = 0; bool flag1 = false; while (1) { for (int i = 1; i <= n; i++) { tmp[i] = p[q[i]]; } k1++; bool f = true; for (int i = 1; i <= n; i++) { p[i] = tmp[i]; if (p[i] != s[i]) f = false; } if (f) { flag1 = true; break; } if (k1 == k) break; } bool flag2 = false; for (int i = 1; i <= n; i++) p[i] = i; while (1) { for (int i = 1; i <= n; i++) { tmp[q[i]] = p[i]; } k2++; bool f = true; for (int i = 1; i <= n; i++) { p[i] = tmp[i]; if (p[i] != s[i]) f = false; } if (f) { flag2 = true; break; } if (k2 == k) break; } if (!flag1 && !flag2) { cout << "NO" << endl; return 0; } if (k1 == 1 && k2 == 1) { if (k == 1) cout << "YES" << endl; else cout << "NO" << endl; return 0; } if (k < min(k1, k2)) { cout << "NO" << endl; return 0; } if ((flag1 && (k - k1) % 2 == 0) || (flag2 && (k - k2) % 2 == 0)) { cout << "YES" << endl; return 0; } cout << "NO" << endl; return 0; }
1,800
CPP
n = int(input()) nt=n nl = [] count = 0 while nt!=0: nl = list(map(int, input().split(" "))) a=0 if nl[0]==1: a+=1 if nl[1]==1: a+=1 if nl[2]==1: a+=1 if a>=2: count+=1 nt-=1 print(f'{count}')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using ull = uint64_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; using f64 = double; using pii = pair<int, int>; using pll = pair<ll, ll>; using ld = double; namespace io { const int BUFSIZE = 1 << 20; int isize, osize; char ibuf[BUFSIZE + 10], obuf[BUFSIZE + 10]; char *is, *it, *os = obuf, *ot = obuf + BUFSIZE; char getchar() { if (is == it) { is = ibuf; it = ibuf + fread(ibuf, 1, BUFSIZE, stdin); if (is == it) return EOF; } return *is++; } char getalpha() { char c = getchar(); while (!isalpha(c)) c = getchar(); return c; } void putchar(char c) { *os++ = c; if (os == ot) { fwrite(obuf, 1, BUFSIZE, stdout); os = obuf; } } int inp() { int x = 0, f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) { if (ch == EOF) return -1; if (ch == '-') f = 1; } for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return f ? -x : x; } ll inp_ll() { ll x = 0; int f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = 1; for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return f ? -x : x; } template <class T> bool read(T& x) { x = 0; int f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) { if (ch == EOF) return 0; if (ch == '-') f = 1; } for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; if (f) x = -x; return 1; } bool read(char* s) { char* t = s; char ch = getchar(); for (; ch == ' ' || ch == '\n'; ch = getchar()) ; for (; ch != ' ' && ch != '\n' && ch != EOF; ch = getchar()) *t++ = ch; *t = 0; return s != t; } template <class T, class... Args> bool read(T& x, Args&... args) { return read(x) && read(args...); } template <class T> bool readln(T& x) { x = 0; int f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) { if (ch == EOF) return 0; if (ch == '-') f = 1; } for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; if (f) x = -x; for (; ch != '\n' && ch != EOF; ch = getchar()) ; return 1; } bool readln(char* s) { char* t = s; while (1) { char ch = getchar(); if (ch == '\n' || ch == EOF) break; *t++ = ch; } *t = 0; return s != t; } template <class T, class... Args> bool readln(T& x, Args&... args) { return read(x) && readln(args...); } template <class T> void write(T x) { static char s[22]; static char* it = s + 20; static char* end = s + 20; if (x < 0) { putchar('-'); x = -x; } do { *--it = x % 10 + '0'; x /= 10; } while (x); for (; it < end; ++it) putchar(*it); } void write(const char* s) { for (; *s; ++s) putchar(*s); } template <> void write(char* s) { write((const char*)s); } template <> void write(char c) { putchar(c); } template <class T, class... Args> void write(T x, Args... args) { write(x); write(args...); } void writeln() { putchar('\n'); } template <class T, class... Args> void writeln(T x, Args... args) { write(x); writeln(args...); } template <class Iterator> bool input(Iterator st, Iterator ed) { for (; st != ed; ++st) { if (!read(*st)) return false; } return true; } template <class T> bool input(T& a) { return input(a.begin(), a.end()); } template <class Iterator> void print(Iterator st, Iterator ed, const char* c = " ") { int flag = 0; for (; st != ed; ++st) { if (flag) write(c); flag = 1; write(*st); } writeln(); } template <class T> void print(const T& a, const char* c = " ") { print(a.begin(), a.end(), c); } struct ender { ~ender() { if (os != obuf) fwrite(obuf, 1, os - obuf, stdout); } } __ender; } // namespace io int64_t power(int64_t a, int64_t b, int64_t p) { if (!b) return 1; int64_t t = power(a, b >> 1, p); t = t * t % p; if (b & 1) t = t * a % p; return t; } pll exgcd(ll a, ll b) { if (b == 0) return {1, 0}; auto e = exgcd(b, a % b); ll x = e.first; ll y = e.second; return {y, x - a / b * y}; } mt19937 rd(1); using namespace io; template <class T> inline void freshmin(T& a, const T& b) { if (a > b) a = b; } template <class T> inline void freshmax(T& a, const T& b) { if (a < b) a = b; } int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; int dy[] = {0, 0, -1, 1, -1, 1, -1, 1}; template <class T> T det(T x1, T y1, T x2, T y2, T x3, T y3) { return x1 * y2 - x2 * y1 + x2 * y3 - x3 * y2 + x3 * y1 - x1 * y3; } template <class T> T dis(T x1, T y1, T x2, T y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } const int INF = 1000000000; void solve() { int n; read(n); vector<vector<int>> v(n); for (int i = 1; i < n; ++i) { int x, y; read(x, y); x -= 1; y -= 1; v[x].push_back(y); v[y].push_back(x); } vector<int> sz(n); int root = -1, best = n; function<void(int, int)> dfs = [&](int x, int pre) { sz[x] = 1; int largest = 0; for (auto y : v[x]) { if (y == pre) continue; dfs(y, x); sz[x] += sz[y]; freshmax(largest, sz[y]); } freshmax(largest, n - sz[x]); if (largest < best) { best = largest; root = x; } }; dfs(0, -1); vector<int> dis(n); vector<int> w; function<void(int, int)> dfs2 = [&](int x, int pre) { w.push_back(x); for (auto y : v[x]) { if (y == pre) continue; dis[y] = dis[x] + 1; dfs2(y, x); } }; dfs2(root, -1); int step = n / 2; ll ans = 0; vector<int> res(n); for (int i = 0; i < n; ++i) { res[w[i]] = w[(i + step) % n]; ans += dis[w[i]] + dis[res[w[i]]]; } writeln(ans); for (auto& x : res) x += 1; print(res); } int main() { int T = 1; for (int test = 1; test <= T; ++test) { solve(); } return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 102; long long dis[maxn][maxn], n, d, ar[maxn], x[maxn], y[maxn]; int main() { cin >> n >> d; for (int i = 2; i < n; i++) cin >> ar[i]; for (int i = 1; i <= n; i++) cin >> x[i] >> y[i]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) dis[i][j] = (abs(x[i] - x[j]) + abs(y[i] - y[j])) * d; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (k != i && k != j) dis[i][j] = min(dis[i][j], dis[i][k] - ar[k] + dis[k][j]); cout << dis[1][n] << endl; }
2,100
CPP
import math t = int(input()) for _ in range(t): k, d = (int(i) for i in input().split()) a = k//d print(min(a*d+ math.floor(d/2),k))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n = 0, a = 0; cin >> n >> a; if (n < 1 || n % 2 != 0 || n > 100000) return -1; if (a < 1 || a > 100000 || a > n) return -1; if (a % 2 == 0) { cout << (n - a) / 2 + 1; } else { cout << (a / 2) + 1; } return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; int a[500005], b[300][300]; int main() { ios::sync_with_stdio(0); cin.tie(0); for (int i = 0; i < 500005; i++) a[i] = 0; for (int i = 0; i < 300; i++) for (int j = 0; j < 300; j++) b[i][j] = 0; int q; cin >> q; while (q--) { int type, x, y; cin >> type >> x >> y; if (type == 1) { a[x] += y; for (int i = 1; i < 300; i++) b[i][x % i] += y; } else { if (x >= 300) { int64_t tong = 0; for (int i = y; i <= 500000; i += x) tong += a[i]; cout << tong << '\n'; } else { cout << b[x][y] << '\n'; } } } }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int x, n, BASE, t, same; pair<int, int> p[200010]; long long ans = 1; int main() { cin >> n; for (int i = 0; i < 2 * n; i++) { cin >> x; p[i] = pair<int, int>(x, i % n); } cin >> BASE; sort(p, p + 2 * n); int i = 0, j = 0; while (i < 2 * n) { while (j <= 2 * n && p[j + 1].first == p[j].first) if (p[++j].second == p[j - 1].second) same++; for (int k = 2; k <= j - i + 1; k++) { t = k; while (!(t & 1) && same) t /= 2, same--; ans = (ans * t) % BASE; } i = ++j; } cout << ans << endl; return 0; }
1,600
CPP
sum=0 n=int(input()) a=list(map(int,input().split())) for i in range(0,n): sum=sum+a[i] if sum==0: print("EASY") else: print("HARD")
800
PYTHON3
def rule(x, y): if x == y: return '0' else: return '1' a = list(input()) b = list(input()) t = map(rule, a, b) print(''.join(t))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } int n, q, c; int dp[(100 + 10)][(100 + 10)][20]; class star { public: int x, y, s; void init(int i) { x = read(), y = read(), s = read(); dp[x][y][s]++; } } a[(100000 + 100)]; int calc(int s, int t, int n) { return (s + t) % (c + 1) * n; } void first() { for (int i = 1; i < (100 + 10); i++) { for (int j = 1; j < (100 + 10); j++) { for (int k = 0; k <= c; k++) { dp[i][j][k] += dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k]; } } } } long long findAns(int x1, int y1, int x2, int y2, int t) { long long ans = 0; for (int i = 0; i <= c; i++) { int num = dp[x2][y2][i] - dp[x2][y1 - 1][i] - dp[x1 - 1][y2][i] + dp[x1 - 1][y1 - 1][i]; ans += calc(i, t, num); } return ans; } int main() { n = read(), q = read(), c = read(); for (int i = 1; i <= n; i++) a[i].init(i); first(); for (int i = 1; i <= q; i++) { int t = read(), x1 = read(), y1 = read(), x2 = read(), y2 = read(); if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y2, y2); cout << findAns(x1, y1, x2, y2, t) << endl; } return 0; }
1,600
CPP
import math l, r = list(map(int, input().split())) print('YES') for i in range(l, r, 2): print(i, i+1)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; long long Increment[500005]; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = s.size(); for (int i = 0; i < n; i++) { if (s[i] == 'I' || s[i] == 'E' || s[i] == 'A' || s[i] == 'O' || s[i] == 'U' || s[i] == 'Y') { int mxV = min(n - i, i + 1); int st = mxV, en = n - mxV + 1; Increment[1]++, Increment[st + 1]--; Increment[en + 1]--; } } long double ans = 0; long long cur = 0, inc = 0; for (int i = 1; i <= n; i++) { inc += Increment[i]; cur += inc; ans += cur / (long double)(i); } cout << fixed << setprecision(9) << ans << "\n"; return 0; }
2,000
CPP
def maximum_number(n, m): if n % 2 == 0 or m % 2 == 0: return int(n*m/2) return int((n-1)*m/2 + m//2) def main(): n, m = tuple(map(int, input().split(' '))) result = maximum_number(n, m) print(result) if __name__ == "__main__": main()
800
PYTHON3
n = int(input()) graf = dict() ucka = [] vcka = [] pocty = n * [0] for i in range(n-1): [u, v] = [int(x) for x in input().split()] ucka.append(u) vcka.append(v) pocty[u-1] += 1 pocty[v-1] += 1 hrana = None if max(pocty)>2: for i in range(n): if pocty[i]>2: hrana = i+1 break spodne = 0 akt = 3 if hrana == None: for i in range(n-1): print(i) else: for i in range(n-1): if (ucka[i] == hrana or vcka[i] == hrana) and spodne <3: print(spodne) spodne = spodne + 1 else: print(akt) akt = akt+1
1,500
PYTHON3
n = int(input()) count = 0 D = [100,20,10,5,1] for i in D: if(n/i != 0): count += int(n/i) n -= int(n/i)*i print(count)
800
PYTHON3
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): n = I() a = LI() s = set() r = 0 for c in a: if c in s: s.remove(c) else: s.add(c) if r < len(s): r = len(s) return r print(main())
800
PYTHON3
n, m = map(int, input().split()) x = n // 2 + n % 2 while x <= n and x % m != 0: x += 1 if x > n: print(-1) else: print(x)
1,000
PYTHON3
l = [] def f(p, k): if p == 0: return r = p % k q = (p-r)//(-k) l.append(r) f(q, k) p, k = map(int, input().split()) f(p, k) print(len(l)) print(*l)
2,000
PYTHON3
t = int(input()) for i in range(t): count = 0 a, b = map(int, input().split()) diff = abs(a-b) while(diff>0): if diff >= 10: count = count + int(diff/10) diff = diff%10 if diff==0: break elif diff >= 9: count = count + int(diff/9) diff = diff%9 if diff==0: break elif diff >= 8: count = count + int(diff/8) diff = diff%8 if diff==0: break elif diff >= 7: count = count + int(diff/7) diff = diff%7 if diff==0: break elif diff >= 6: count = count + int(diff/6) diff = diff%6 if diff==0: break elif diff >= 5: count = count + int(diff/5) diff = diff%5 if diff==0: break elif diff >= 4: count = count + int(diff/4) diff = diff%4 if diff==0: break elif diff >= 3: count = count + int(diff/3) diff = diff%3 if diff==0: break elif diff >= 2: count = count + int(diff/2) diff = diff%2 if diff==0: break else: count = count + 1 break print(count)
800
PYTHON3
n=int(input()) l=list(map(int,input().split()[:n])) l.sort() for i in l: print(i,end=' ')
900
PYTHON3
x, y, x0, y0 = map(int, input().split()) s = input() s = s[:len(s) - 1] visited = [[False for i in range(y + 1)] for j in range(x + 1)] visited[x0][y0] = True ans = '1 ' cnt = 1 for i in s: if i == 'U': if x0 != 1: x0 -= 1 if not visited[x0][y0]: ans += '1 ' cnt += 1 visited[x0][y0] = True else: ans += '0 ' else: ans += '0 ' elif i == 'D': if x0 != x: x0 += 1 if not visited[x0][y0]: ans += '1 ' cnt += 1 visited[x0][y0] = True else: ans += '0 ' else: ans += '0 ' elif i == 'L': if y0 != 1: y0 -= 1 if not visited[x0][y0]: ans += '1 ' cnt += 1 visited[x0][y0] = True else: ans += '0 ' else: ans += '0 ' else: if y0 != y: y0 += 1 if not visited[x0][y0]: ans += '1 ' cnt += 1 visited[x0][y0] = True else: ans += '0 ' else: ans += '0 ' z = str(x * y - cnt) print(ans + z)
1,600
PYTHON3
n = int(input()) for _ in range(n): s=input() if(len(s)>10): print("{}{}{}".format(s[0],len(s)-2,s[-1])) else: print(s) # 1523566616638
800
PYTHON3
#include <bits/stdc++.h> using namespace std; string fixStr(string s) { if (s[1] == '>') { return string(1, s[2]) + "<" + string(1, s[0]); } return s; } string getAns(string s1, string s2, string s3) { int arrLetterOnLeftCount[300]; arrLetterOnLeftCount['A'] = 0; arrLetterOnLeftCount['B'] = 0; arrLetterOnLeftCount['C'] = 0; arrLetterOnLeftCount[s1[0]]++; arrLetterOnLeftCount[s2[0]]++; arrLetterOnLeftCount[s3[0]]++; int countA, countB, countC; countA = arrLetterOnLeftCount['A']; countB = arrLetterOnLeftCount['B']; countC = arrLetterOnLeftCount['C']; if (countA != 2 && countB != 2 && countC != 2) return "Impossible"; if (countA == 2) { if (countB == 1) { return "ABC"; } else { return "ACB"; } } else if (countB == 2) { if (countA == 1) { return "BAC"; } else { return "BCA"; } } if (countA == 1) { return "CAB"; } return "CBA"; } int main() { string s1, s2, s3; cin >> s1 >> s2 >> s3; cout << getAns(fixStr(s1), fixStr(s2), fixStr(s3)) << endl; }
1,200
CPP
N = int(input()) for _ in range(N): K = int(input()) A = map(int, input().split()) B = map(int, input().split()) have_1 = False have_m1 = False res = [] for u, v in zip(A, B): if u == v: res.append(True) elif u > v and have_m1: res.append(True) elif u < v and have_1: res.append(True) else: res.append(False) # print(u, v, res[-1]) # print() if u == 1: have_1 = True if u == -1: have_m1 = True if all(res): print('YES') else: print('NO')
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MXN = 5e4 + 5; const int INF = 1e9; const long long P = 29; const long long MOD = 1e9 + 7; int t; int main() { srand(time(0)); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); cin >> t; while (t--) { int n, x1, x2, x3; cin >> n; cin >> x1 >> x2; for (int i = 2; i < n; i++) { cin >> x3; } if (x1 + x2 <= x3) cout << 1 << ' ' << 2 << ' ' << n << '\n'; else cout << -1 << '\n'; } return 0; }
800
CPP
a,b,ab=map(int,input().split()) if (a>b):print(ab*2+b*2+1) elif (b>a):print(ab*2+a*2+1) else:print(ab*2+a*2)
800
PYTHON3
from sys import stdin,stderr def rl(): return [int(w) for w in stdin.readline().split()] M = 1000000007 def pow(p, x): r = 1 p2k = p while x > 0: if x & 1: r = r * p2k % M x >>= 1 p2k = p2k * p2k % M return r t, = rl() for _ in range(t): n, p = rl() k = rl() if p == 1: print(n % 2) continue else: k.sort(reverse=True) multiplier = 1 i = 0 while i < n - 1: if multiplier == 0: multiplier = 1 elif multiplier > n: break else: dk = k[i] - k[i+1] if multiplier > 0 and dk >= 20: # p**20 > len(k) break multiplier = abs(multiplier * p ** dk - 1) i += 1 r = multiplier * pow(p, k[i]) % M for x in k[i+1:]: r = (r - pow(p, x)) % M print(r)
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; int main(int argc, const char* argv[]) { int cases; scanf("%d", &cases); while (cases--) { int a, b; scanf("%d%d", &a, &b); int side = max(max(a, b), 2 * min(a, b)); printf("%d\n", side * side); } }
800
CPP
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; const unsigned long long mod = (1LL << 32); unsigned long long n, p, q, ans; unsigned long long c[135], a[135], b[135]; unsigned long long gcd(unsigned long long a, unsigned long long b) { if (!b) return a; return gcd(b, a % b); } unsigned long long ksm(unsigned long long x, unsigned long long y) { unsigned long long sum = 1; while (y) { if (y & 1) sum = sum * x % mod; y >>= 1; x = x * x % mod; } return sum; } signed main() { cin >> n >> p >> q; p = min(p, n - 1); c[0] = 1; for (unsigned long long k = 1; k <= p; k++) { c[k] = 1; for (unsigned long long i = 1; i <= k; i++) a[i] = n - i + 1, b[i] = i; for (unsigned long long i = 1; i <= k; i++) { for (unsigned long long j = 2; j <= k; j++) { unsigned long long tp = gcd(a[i], b[j]); if (tp != 1) a[i] /= tp, b[j] /= tp; } } for (unsigned long long i = 1; i <= k; i++) c[k] = c[k] * a[i] % mod; } for (unsigned long long i = 1; i <= q; i++) { unsigned long long tmp = 0; for (unsigned long long j = 0; j <= p; j++) tmp = (tmp + c[j] * ksm(i, j)) % mod; ans = ans ^ (i * tmp % mod); } cout << ans; }
2,900
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; int a[maxn][maxn], n, m, q, up[maxn][maxn], down[maxn][maxn], l[maxn][maxn], r[maxn][maxn]; int x, y, op; void update() { a[x][y] = 1 - a[x][y]; for (int j = 1; j <= m; j++) if (a[x][j]) l[x][j] = l[x][j - 1] + 1; else l[x][j] = 0; for (int j = m; j >= 1; j--) if (a[x][j]) r[x][j] = r[x][j + 1] + 1; else r[x][j] = 0; for (int i = 1; i <= n; i++) if (a[i][y]) up[i][y] = up[i - 1][y] + 1; else up[i][y] = 0; for (int i = n; i >= 1; i--) if (a[i][y]) down[i][y] = down[i + 1][y] + 1; else down[i][y] = 0; } void query() { if (a[x][y] == 0) { printf("0\n"); return; } int ans = 0; int U = 1e9, D = 1e9, L = 1e9, R = 1e9; for (int i = y; i >= 1; i--) { U = min(U, up[x][i]); D = min(D, down[x][i]); ans = max(ans, (U + D - 1) * (y - i + 1)); } U = 1e9, D = 1e9, L = 1e9, R = 1e9; for (int i = y; i <= m; i++) { U = min(U, up[x][i]); D = min(D, down[x][i]); ans = max(ans, (U + D - 1) * (i - y + 1)); } U = 1e9, D = 1e9, L = 1e9, R = 1e9; for (int i = x; i >= 1; i--) { L = min(L, l[i][y]); R = min(R, r[i][y]); ans = max(ans, (L + R - 1) * (x - i + 1)); } U = 1e9, D = 1e9, L = 1e9, R = 1e9; for (int i = x; i <= n; i++) { L = min(L, l[i][y]); R = min(R, r[i][y]); ans = max(ans, (L + R - 1) * (i - x + 1)); } printf("%d\n", ans); } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) if (a[i][j]) l[i][j] = l[i][j - 1] + 1; for (int j = m; j >= 1; j--) if (a[i][j]) r[i][j] = r[i][j + 1] + 1; } for (int j = 1; j <= m; j++) { for (int i = 1; i <= n; i++) if (a[i][j]) up[i][j] = up[i - 1][j] + 1; for (int i = n; i >= 1; i--) if (a[i][j]) down[i][j] = down[i + 1][j] + 1; } for (int i = 1; i <= q; i++) { scanf("%d%d%d", &op, &x, &y); if (op == 1) update(); else query(); } }
2,000
CPP
s = list(input()) h = "hello" r = 0 for i in range(len(h)): if h[i] in s: j = s.index(h[i]) s = s[j+1:] else: r = 1 print('NO') break if r != 1:print('YES')
1,000
PYTHON3
n = int(input()) a = list(map(int, input().split())) print(len(set(a)) - (0 in a))
800
PYTHON3
n = int(input()) m = 0 passenger = 0 for i in range(n): a, b = map(int, input().split()) passenger = passenger - a + b m = max(m, passenger) print(m)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const unsigned long long MOD = 1e9 + 7; struct Matrix { unsigned long long ma[110][110]; int n; Matrix() {} Matrix(int _n) { n = _n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ma[i][j] = 0; } Matrix operator*(const Matrix &b) const { Matrix ret = Matrix(n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) { ret.ma[i][j] += ma[i][k] * b.ma[k][j]; ret.ma[i][j] %= MOD; } return ret; } void set_one() { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ma[i][j] = (i == j); } }; void Mapow(Matrix &a, Matrix &ans, long long n) { ans.set_one(); while (n) { if (n & 1) ans = ans * a; a = a * a; n >>= 1; } } int main() { long long n; int m; scanf("%lld%d", &n, &m); if (n < m) return 0 * printf("1\n"); Matrix a(m); Matrix ans(m); for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) a.ma[i][j] = 0; for (int i = 1; i < m; i++) a.ma[i][i - 1] = 1; a.ma[0][m - 1] = a.ma[m - 1][m - 1] = 1; Mapow(a, ans, n); unsigned long long res = 0; for (int i = 0; i < m; i++) { res += ans.ma[i][0]; if (res >= MOD) res -= MOD; } printf("%llu\n", res); }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const long long c = 2000, inf = 2000000000ll; long long a[c], first[c], n, m; long long t, x, y, second; long long mod = 1000000000ll; int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; first[0] = first[1] = 1ll; for (int i = 2; i <= n; i++) first[i] = (first[i - 1] + first[i - 2]) % mod; for (int i = 0; i < m; i++) { cin >> t >> x >> y; if (t == 1) a[x] = y; else { second = 0; for (int i = x; i <= y; i++) second = (second + (a[i] * first[i - x]) % mod) % mod; cout << second << endl; } } return 0; }
1,500
CPP
params = input().split(" ") misha = max((3*int(params[0]))/10, (int(params[0])-((int(params[0])/250)*int(params[2])))) vasya = max((3*int(params[1]))/10, (int(params[1])-((int(params[1])/250)*int(params[3])))) if misha==vasya: print("Tie") elif misha>vasya: print("Misha") elif vasya>misha: print("Vasya")
900
PYTHON3
/* author: Maksim1744 created: 06.05.2021 16:01:27 */ #include "bits/stdc++.h" using namespace std; using ll = long long; using ld = long double; #define mp make_pair #define pb push_back #define eb emplace_back #define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll)) #define mine(a) (*min_element((a).begin(), (a).end())) #define maxe(a) (*max_element((a).begin(), (a).end())) #define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin()) #define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin()) #define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin()) #define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin()) template<typename T> vector<T>& operator-- (vector<T> &v){for (auto& i : v) --i; return v;} template<typename T> vector<T>& operator++ (vector<T> &v){for (auto& i : v) ++i; return v;} template<typename T> istream& operator>>(istream& is, vector<T> &v){for (auto& i : v) is >> i; return is;} template<typename T> ostream& operator<<(ostream& os, vector<T> v){for (auto& i : v) os << i << ' '; return os;} template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;} template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;} template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second; return is;} template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U> p){os << p.first << ' ' << p.second; return os;} template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);} template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);} template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;} template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;} #ifdef HOME #define SHOW_COLORS #else #define show(...) void(0) #define mclock void(0) #define shows void(0) #define debug if (false) #endif const int B = 20; struct Node { Node* to[2]; int sz = 0; int mod = 0; int def0 = (1 << B) - 1; int def1 = (1 << B) - 1; Node() { to[0] = nullptr; to[1] = nullptr; } }; ostream &operator << (ostream &o, const Node &n) { return o << "[sz=" << n.sz << ", mod=" << n.mod << ", def0=" << n.def0 << ", def1=" << n.def1 << "]"; } void add_num(Node *node, int x) { for (int i = 0; i < B; ++i) { node->sz++; node->def1 &= x; node->def0 &= ((1 << B) - 1) ^ x; int b = ((x >> (B - i - 1)) & 1); if (!node->to[b]) { node->to[b] = new Node(); } node = node->to[b]; } node->sz++; node->def1 &= x; node->def0 &= ((1 << B) - 1) ^ x; } void update(Node *node, int level) { if (!node) return; node->sz = 0; if (node->to[0]) node->sz += node->to[0]->sz; if (node->to[1]) node->sz += node->to[1]->sz; node->def0 = (1 << B) - 1; node->def1 = (1 << B) - 1; if (node->to[0]) { node->def0 &= node->to[0]->def0; node->def1 &= node->to[0]->def1; } if (node->to[1]) { node->def0 &= node->to[1]->def0; node->def1 &= node->to[1]->def1; } if (!node->to[0]) node->def1 |= (1 << (B - level - 1)); if (!node->to[1]) node->def0 |= (1 << (B - level - 1)); } void modify(Node *node, int x) { if (!node) return; node->mod ^= x; int mask = (x & (node->def0 | node->def1)); node->def0 ^= mask; node->def1 ^= mask; } void push(Node *node, int level) { if (!node) { return; } if ((node->mod >> (B - level - 1)) & 1) { swap(node->to[0], node->to[1]); } modify(node->to[0], node->mod); modify(node->to[1], node->mod); node->mod = 0; } pair<Node*, Node*> split(Node *node, int m, int l = 0, int r = (1 << B) - 1, int level = 0) { push(node, level); if (!node) return {nullptr, nullptr}; if (r <= m) return {node, nullptr}; if (l > m) return {nullptr, node}; int mid = (l + r) / 2; auto L = new Node(); auto R = new Node(); if (mid < m) { auto [a, b] = split(node->to[1], m, mid + 1, r, level + 1); if (b) { R->to[1] = b; } else { R = nullptr; } if (node->to[0] || a) { L->to[0] = node->to[0]; L->to[1] = a; } else { L = nullptr; } update(L, level); update(R, level); return {L, R}; } else { auto [a, b] = split(node->to[0], m, l, mid, level + 1); if (node->to[1] || b) { R->to[0] = b; R->to[1] = node->to[1]; } else { R = nullptr; } if (a) { L->to[0] = a; } else { L = nullptr; } update(L, level); update(R, level); return {L, R}; } } Node* mrg(Node *a, Node *b, int level) { if (!a) return b; if (!b) return a; if (level == B) { assert(a->sz == 1 && b->sz == 1); return a; } Node *root = a; push(a, level); push(b, level); a->to[0] = mrg(a->to[0], b->to[0], level + 1); a->to[1] = mrg(a->to[1], b->to[1], level + 1); update(root, level); return root; } tuple<Node*, Node*, Node*> cut(Node *root, int l, int r) { auto [a, bc] = split(root, l - 1); auto [b, c] = split(bc, r); return {a, b, c}; } Node* merge3(Node *a, Node *b, Node *c) { return mrg(mrg(a, b, 0), c, 0); } void push_or_bit(Node *node, int b, int level) { if (!node) return; push(node, level); if (level == B - b - 1) { modify(node->to[0], 1 << b); node->to[1] = mrg(node->to[0], node->to[1], level + 1); node->to[0] = nullptr; update(node, level); return; } if ((node->def1 >> b) & 1) { return; } if ((node->def0 >> b) & 1) { modify(node, 1 << b); return; } push_or_bit(node->to[0], b, level + 1); push_or_bit(node->to[1], b, level + 1); update(node, level); } void push_or(Node *root, int x) { for (int b = 0; b < B; ++b) { if ((x >> b) & 1) { push_or_bit(root, b, 0); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); Node *root = new Node(); auto call_xor = [&](int l, int r, int x) { auto [a, b, c] = cut(root, l, r); modify(b, x); root = merge3(a, b, c); }; auto call_or = [&](int l, int r, int x) { auto [a, b, c] = cut(root, l, r); push_or(b, x); root = merge3(a, b, c); }; auto call_ask = [&](int l, int r) { auto [a, b, c] = cut(root, l, r); int res = (b ? b->sz : 0); return res; }; auto call_and = [&](int l, int r, int x) { call_xor(0, (1 << B) - 1, (1 << B) - 1); call_or(r ^ ((1 << B) - 1), l ^ ((1 << B) - 1), ((1 << B) - 1) ^ x); call_xor(0, (1 << B) - 1, (1 << B) - 1); }; int n, q; cin >> n >> q; vector<int> a(n); cin >> a; sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); n = a.size(); for (int k : a) { add_num(root, k); } while (q--) { int tp, l, r, x; cin >> tp >> l >> r; if (tp != 4) cin >> x; if (tp == 1) { call_and(l, r, x); } else if (tp == 2) { call_or(l, r, x); } else if (tp == 3) { call_xor(l, r, x); } else if (tp == 4) { cout << call_ask(l, r) << '\n'; } else { assert(false); } } return 0; }
3,500
CPP
#include <bits/stdc++.h> using namespace std; const int inf = int(5e5) + 5; const int imp = int(1e9) + 9; int n, t; int a[555555]; char second[555555]; int calc(int k) { int cur = k; int prevNeg = -1, prevZero = -1, lastZero = -1, lastHouse = -1; int res = 0; for (int i = 1; i <= n; ++i) { if (second[i - 1] == 'H') { --cur; lastHouse = i; } else if (second[i - 1] == 'S') ++cur; if (cur < 0) { if (prevNeg != -1) { if (prevZero != -1) { res += min(i - prevNeg, 2 * (prevZero - prevNeg)); prevZero = -1; prevNeg = i; } } else { prevNeg = i; prevZero = -1; } } else if (cur == 0) { if (prevNeg != -1 && (prevZero == -1 || prevZero < prevNeg)) { prevZero = i; lastZero = i; } } } assert(lastHouse != -1); if (prevNeg != -1) { if (prevZero == -1) return imp; else { int cur = 2 * prevZero - prevNeg; if (lastHouse > prevZero) { cur += lastHouse - prevNeg; cur = min(cur, 2 * lastHouse - prevNeg); } return res + cur; } } else { return lastHouse; } } int main() { scanf("%d%d\n", &n, &t); gets(second); int l = -1, r = inf; while (r - l > 1) { int mid = (l + r) / 2; if (calc(mid) <= t) r = mid; else l = mid; } if (r < inf) printf("%d\n", r); else puts("-1"); return 0; }
2,300
CPP
n = int(input()) arr = list(map(int, input().split())) s1, s2 = 0, 0 i, j = 0, n - 1 res = 0 while i < n: s1 += arr[i] while s2 < s1 and j > i: s2 += arr[j] j -= 1 if s1 == s2: res = s1 i += 1 print(res)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int MOD = 1e9 + 7; long long dp[36][36][36]; long long dp2[36][36]; long long solve(int s, int e) { if (e < s) return 1; long long ans = 0; if (dp2[s][e] != -1) return dp2[s][e]; for (int i = s; i <= e; i++) { ans += solve(s, i - 1) * solve(i + 1, e); } return dp2[s][e] = ans; } long long solve(int s, int e, int h) { if (e < s) { if (h == 0) return 1; else return 0; } if (dp[s][e][h] != -1) return dp[s][e][h]; long long ans = 0; for (int i = s; i <= e; i++) { ans += solve(s, i - 1, max(0, h - 1)) * solve(i + 1, e) - solve(s, i - 1, max(0, h - 1)) * solve(i + 1, e, max(0, h - 1)) + solve(s, i - 1) * solve(i + 1, e, max(0, h - 1)); } return dp[s][e][h] = ans; } int main() { int n, h; cin >> n >> h; memset(dp, -1, sizeof(dp)); memset(dp2, -1, sizeof(dp2)); cout << solve(1, n, h); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long x, y, z, s, p, r, k, a, b, c, m, q, max = 0; cin >> x >> y >> z >> k; if (x > y) swap(x, y); if (y > z) swap(y, z); if (x > y) swap(x, y); x--; y--; z--; if (x + y + z <= k) cout << (x + 1) * (y + 1) * (z + 1) << endl; else { m = min(k, x); for (a = 0; a <= m; a++) { q = k - a; p = q / 2; r = q % 2; if (y >= p) { b = p; if (p + r <= z) c = p + r; else c = z; } else { b = y; if (z >= q - y) c = q - y; else c = z; } p = (a + 1) * (b + 1) * (c + 1); if (p > max) max = p; } cout << max << endl; } }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long a, v, b, n, m, i, j, k, l; while (cin >> n) { m = 9999999; l = sqrt(n); for (i = 1; i <= l; i++) { if (n % i == 0) { v = n / i - i; if (v < m) { m = v; a = i; b = n / i; } } } cout << a << " " << b << endl; } }
800
CPP
for i in range(int(input())): l,r=map(int,input().split()) while(l|(l+1)<=r): l|=l+1 print(l) # Made By Mostafa_Khaled
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long i, j, k, l, n, m; cin >> n >> m >> k; long long mul = 1, flag = 0; for (i = 1;; i++) { if (mul >= n && mul <= m) { cout << mul << " "; flag = 1; } if ((mul * 1.0) * k > m * 1.0) break; mul *= k; } if (flag == 0) cout << "-1"; cout << endl; }
1,500
CPP
for i in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] if a[0] > 0: ch = 1 ma = a[0] else: ch = -1 mi = a[0] sy = 0 for j in range(1,n): if a[j] > 0: if ch == 1: ma = max(a[j],ma) else: sy += mi ch = 1 ma = a[j] else: if ch == -1: mi = max(a[j],mi) else: sy += ma ch = -1 mi = a[j] if a[-1] > 0: print(sy + ma) else: print(sy + mi)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; for (long long i = 0; i < a.size(); i++) { if (a[i] == '5') { a[i] = '4'; } else if (a[i] == '6') { a[i] = '3'; } else if (a[i] == '7') { a[i] = '2'; } else if (a[i] == '8') { a[i] = '1'; } else if (i != 0 && a[i] == '9') { a[i] = '0'; } } cout << a << endl; }
1,200
CPP
n = int(input()) arr = [bin(i)[2:][::-1] for i in map(int, input().split())] bits = [0] * 20 for i in arr: for j in range(len(i)): if i[j] == '1': bits[j] += 1 newarr = [] while bits.count(0) != len(bits): num = 0 for i in range(len(bits)): if bits[i] != 0: bits[i] -= 1 num += (2 ** i) newarr.append(num) summ = 0 for i in newarr: summ += i * i print(summ)
1,700
PYTHON3
def main(): q = int(input()) for t in range(q): n, k = map(int, input().split()) k -= 1 if k == 0: print(1) elif n % 2 == 1: h = n // 2 step = h + 1 all = k // h * step + (k % h) print((all % n) + 1) else: print(k % n + 1) main()
1,200
PYTHON3
k = int(input()) l = int(input()) m = int(input()) n = int(input()) d = int(input()) if k == 1 or l == 1 or m == 1 or n == 1: print(d) else: for i in range(1, d+1): if (i%k != 0) and (i%l != 0) and (i%m != 0) and (i%n != 0): d -= 1 print(d)
800
PYTHON3