solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
def is_equivalent(s): if len(s) % 2: return s a = is_equivalent(s[:len(s)//2]) b = is_equivalent(s[len(s)//2:]) return a + b if a < b else b + a a = str(input()) b = str(input()) print('YES' if is_equivalent(a) == is_equivalent(b) else 'NO')
8
PYTHON3
a = input() b = input() s = input() m = a+b for i in range(len(m)): if m.count(m[i]) != s.count(m[i]): print("NO") break else: if len(m) == len(s): print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; double ax[2000000]; int main(void) { int n, m; scanf("%d%d", &n, &m); double sum = 0; for (int i = 0; i < pow(2, n); i++) { scanf("%lf", &ax[i]); sum += ax[i]; } n = pow(2, n); printf("%.6lf\n", sum / n); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); sum = sum - ax[a] + b; ax[a] = b; printf("%.6lf\n", sum / n); } return 0; }
10
CPP
def inp(): return map(int, input().split()) def solve(): n = int(input()) s = input() d = {(0, 0): -1} ll = -1 x, y = 0, 0 res = (-1, -1) for i in range(n): if s[i] == 'L': x -= 1 if s[i] == 'R': x += 1 if s[i] == 'U': y += 1 if s[i] == 'D': y -= 1 if (x, y) not in d: d[(x, y)] = i else: dist = i - d[(x, y)] if dist < ll or ll == -1: ll = dist res = d[(x, y)] + 2, i + 1 if dist == 2: break d[(x, y)] = i # print(d) # print(ll) if ll != -1: print(*res) else: print(ll) def main(): t = int(input()) while t: solve() t -= 1 main()
9
PYTHON3
n,m=[int(i) for i in input().split()] x=[int(i) for i in input().split()] y=[int(i) for i in input().split()] ans=[] for i in range(len(x)): if x[i] in y: ans.append(x[i]) if len(ans)==0: print() else: print(" ".join(str(i) for i in ans))
7
PYTHON3
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector") #include <bits/stdc++.h> #include<set> using namespace std; #define ll long long #define endl '\n' #define pb push_back #define ff first #define ss second #define si(x) int(x.size()) #define sum_all(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()) vector<bool> vis; ll n, k; vector<ll> par; ll ask(vector<ll> &arr) { assert(si(arr) == k); cout << "? "; for (ll i = 0; i < k; ++i) { cout << arr[i] << " "; } cout << endl; ll x; cin >> x; return x; } void solve() { cin >> n >> k; /* we do bfs from 0 to n while mantaining a parent to contruct the path also */ vis.clear(); vis.resize(n + 1); par.clear(); par.resize(n + 1); queue<pair<ll, ll>> q; q.push({ 0,-1 }); while (!q.empty()) { auto u = q.front().ff; auto p = q.front().ss; q.pop(); if (vis[u])continue; vis[u] = true; par[u] = p; for (int j = u + k,c=0; j >= u - k;c++, j -= 2) { if (j > n or j < 0) continue; ll uc = k - c; if (n - u < uc or u<c) continue; if (vis[j]) continue; q.push({ j,u }); } } if (vis[n] == false) { cout << -1 << endl; return; } vector<int> ans; int v = n; while (true) { ans.pb(v); v = par[v]; if (v == -1) break; } reverse(ans.begin(), ans.end()); set<ll> c, uc; for (ll i = 1; i <= n; i++) { uc.insert(i); } vector<vector<ll>> res; for (ll i = 0; i < si(ans) - 1; i++) { res.pb({}); ll a = ans[i]; ll b = ans[i + 1]; ll val = b - a; val = k - val; val /= 2; /* send val from c to uc ans k-val from uc to c */ vector<ll> qc, quc; ll cnt = 0; while (cnt < val) { auto x = *(c.begin()); c.erase(x); qc.pb(x); res.back().pb(x); cnt++; } cnt = 0; while (cnt < k - val) { auto x = *(uc.begin()); uc.erase(x); quc.pb(x); res.back().pb(x); cnt++; } while (!qc.empty()) { auto x = qc.back(); uc.insert(x); qc.pop_back(); } while (!quc.empty()) { auto x = quc.back(); c.insert(x); quc.pop_back(); } } ll y = 0; for (auto &a : res) { y ^= ask(a); } cout << "! " << y << endl; } int main() { solve(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; while (n--) { long long int a, b, c; long long int x, y; long long int flag = 0; long long int pro = 0; cin >> a >> b >> c; cin >> x >> y; if (y > x) { if (a >= 2 * c) { pro += c * y; a = a - 2 * c; c = 0; } else { a = a / 2; pro += a * y; cout << pro << endl; flag = 1; } if (flag != 1) { if (a >= 2 * b) { pro += b * x; a = a - 2 * b; b = 0; cout << pro << endl; } else { a = a / 2; pro += a * x; cout << pro << endl; } } } else { if (a >= 2 * b) { pro += b * x; a = a - 2 * b; b = 0; } else { a = a / 2; pro += a * x; cout << pro << endl; flag = 1; } if (flag != 1) { if (a >= 2 * c) { pro += c * y; a = a - 2 * c; c = 0; cout << pro << endl; } else { a = a / 2; pro += a * y; cout << pro << endl; } } } flag = 0; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } long long int lcm(long long int a, long long int b) { return (a * b) / gcd(a, b); } long long int power(long long int a, long long int b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 1) return (power(a, b - 1) * a) % 1000000007; long long int q = power(a, b / 2); return (q * q) % 1000000007; } long long int arr[500]; bool done[500][500]; int main() { std::ios::sync_with_stdio(false); long long int n; cin >> n; for (int i = 0; i < 2 * n; i++) cin >> arr[i]; long long int co = 0; for (int i = 0; i < 2 * n; i++) { for (int j = 0; j < 2 * n; j++) { if (j != i && !done[i][j]) { if (arr[i] == arr[j] && i != j - 1 && j != i + 1) { long long int a = i; long long int b = j; long long int dis; if (b > a) { dis = b - a - 1; } else { dis = a - b - 1; } for (int x = b; x > a; x--) { swap(arr[x], arr[x - 1]); } co += dis; done[i][j] = true; } } } } cout << co << "\n"; return 0; }
10
CPP
import math t = int(input()) for kase in range(t): n = int(input()) assert n % 2 == 0 ans = 0 step_angle = math.pi / n for i in range(n): ang = math.pi / 2 - step_angle * (i + 1) ans += math.cos(ang) print(ans)
9
PYTHON3
n = int(input()) arr1 = list(map(int, input().strip().split(' '))) arr2 = list(map(int, input().strip().split(' '))) arr3 = arr1[1:] + arr2[1:] flag= 0 counts = dict() for i in range(1,n+1): counts[i]=0 for i in arr3: counts[i] = counts.get(i, 0) + 1 for i in counts.values(): if i == 0: flag = 1 if flag ==1: print("Oh, my keyboard! ") else: print("I become the guy.")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, min = 999999999; cin >> n; vector<int> arr(n); int val; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr.begin(), arr.end()); for (int i = 0; i < n - 1; i++) { if (arr[i + 1] - arr[i] < min) { min = arr[i + 1] - arr[i]; } } cout << min << endl; } }
8
CPP
size = input().split(" ") limit = int(size[0]) * int(size[1]) answer = int(limit/2) print(answer)
7
PYTHON3
#include <iostream> using namespace std; #define INF 10000000 int main(){ int e; while(cin>>e,e){ int ans=INF; for(int z=0;z*z*z<=e;z++){ for(int y=0;y*y<=e-z*z*z;y++){ int x=e-y*y-z*z*z; ans=min(ans,x+y+z); } } cout<<ans<<endl; } return 0; }
0
CPP
str = input() tmpstr = "" counter = sum = 0 for num in str: if (int(num) % 3 == 0): counter += 1 tmpstr = "" sum = 0 else: tmpstr += num sum += int(num) if (sum % 3 == 0): counter += 1 tmpstr = "" sum = 0 else: for i in range(0, len(tmpstr) - 2): sum -= int(tmpstr[i]) if (sum % 3 == 0): counter += 1 tmpstr = "" sum = 0 break print(counter)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, t; char grid1[11][11], grid2[11][11]; int c_id[11][11], cap_id[11][11]; int dist[2][11][11]; bool valid(int a, int b, int x, int y, bool reactor) { bool ret = a + x >= 0 && a + x < n && b + y >= 0 && b + y < n && grid1[a + x][b + y] >= '0' && grid1[a + x][b + y] <= '9' && dist[reactor][a + x][b + y] > dist[reactor][a][b] + 1; if (!reactor) ret = ret && dist[1][a + x][b + y] > dist[0][a][b] + 1; return ret; } void bfs(int x, int y) { bool reactor = (grid1[x][y] == 'Z'); for (int i = 0; i < 11; i++) for (int j = 0; j < 11; j++) dist[reactor][i][j] = 1000000; queue<pair<int, int> > fila; fila.push(make_pair(x, y)); dist[reactor][x][y] = 0; while (!fila.empty()) { pair<int, int> u = fila.front(); fila.pop(); if (valid(u.first, u.second, -1, 0, reactor)) { dist[reactor][u.first - 1][u.second] = dist[reactor][u.first][u.second] + 1; fila.push(make_pair(u.first - 1, u.second)); } if (valid(u.first, u.second, 1, 0, reactor)) { dist[reactor][u.first + 1][u.second] = dist[reactor][u.first][u.second] + 1; fila.push(make_pair(u.first + 1, u.second)); } if (valid(u.first, u.second, 0, -1, reactor)) { dist[reactor][u.first][u.second - 1] = dist[reactor][u.first][u.second] + 1; fila.push(make_pair(u.first, u.second - 1)); } if (valid(u.first, u.second, 0, 1, reactor)) { dist[reactor][u.first][u.second + 1] = dist[reactor][u.first][u.second] + 1; fila.push(make_pair(u.first, u.second + 1)); } } } vector<int> graph[1010]; bool visited[1010]; int matchs[1010]; bool match_vertex(int u) { for (int v : graph[u]) if (!visited[v]) { visited[v] = true; if (matchs[v] < 0 || match_vertex(matchs[v])) { matchs[v] = u; return true; } } return false; } int max_match() { memset(matchs, -1, sizeof(matchs)); int result = 0; for (int u = 0; u < 1010; u++) { memset(visited, 0, sizeof(visited)); if (match_vertex(u)) result++; } return result; } bool reachable(int a, int b) { bool ret = dist[0][a][b] < dist[1][a][b] && dist[0][a][b] <= t; int mini = 1000000; if (a > 0) mini = min(mini, dist[0][a - 1][b]); if (a < n - 1) mini = min(mini, dist[0][a + 1][b]); if (b > 0) mini = min(mini, dist[0][a][b - 1]); if (b < n - 1) mini = min(mini, dist[0][a][b + 1]); return ret || (mini < dist[1][a][b] && mini + 1 <= t); } void gen_edges(int a, int b) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if (grid2[i][j] >= '1' && grid2[i][j] <= '9' && reachable(i, j)) { for (int k = c_id[a][b]; k < c_id[a][b] + (grid1[a][b] - '0'); k++) for (int l = cap_id[i][j]; l < cap_id[i][j] + (grid2[i][j] - '0'); l++) graph[k].push_back(l); } } } int main() { scanf("%d %d", &n, &t); for (int i = 0; i < n; i++) scanf("%s", grid1[i]); for (int i = 0; i < n; i++) scanf("%s", grid2[i]); int cont = 1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (grid1[i][j] >= '1' && grid1[i][j] <= '9') { c_id[i][j] = cont; cont += grid1[i][j] - '0'; } cont = 1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (grid2[i][j] >= '1' && grid2[i][j] <= '9') { cap_id[i][j] = cont; cont += grid2[i][j] - '0'; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (grid1[i][j] == 'Z') bfs(i, j); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (grid1[i][j] >= '1' && grid1[i][j] <= '9') { bfs(i, j); gen_edges(i, j); } printf("%d\n", max_match()); return 0; }
11
CPP
#include<iostream> #include<algorithm> using namespace std; #define N_MAX 3000 class Point{ public: int x, y; Point(int x = 0, int y = 0):x(x), y(y){ } }; int N, K; Point P[N_MAX]; Point T[N_MAX]; bool compX(const Point &p1, const Point &p2){ if ( p1.x == p2.x ) return p1.y < p2.y; return p1.x < p2.x; } bool compA(const Point &p1, const Point &p2){ return p2.x*p1.y < p1.x*p2.y; } bool eq(const Point &p1, const Point &p2){ return p2.x*p1.y == p1.x*p2.y; } int check(int p, int M){ sort(T, T + M, compA); int maxl = 1; int l = 1; Point pre = T[0]; for ( int i = 1; i < M; i++ ){ if ( eq(pre, T[i]) ){ l++; maxl = max(maxl, l); } else { l = 1; } pre = T[i]; } return maxl; } bool solve(){ for ( int i = 0; i < N; i++ ){ // origin int k = 0; int v = 0; for ( int j = i+1; j < N; j++ ){ if ( P[i].x == P[j].x ){ v++; } else { T[k] = P[j]; T[k].x -= P[i].x; T[k].y -= P[i].y; k++; } } if ( 1 + v >= K ) return true; if ( 1 + check(i, k) >= K ) return true; } return false; } int main(){ cin >> N >> K; for ( int i = 0; i < N; i++ ) cin >> P[i].x >> P[i].y; sort(P, P + N, compX); cout << solve() << endl; return 0; }
0
CPP
for i in range(int(input())): n = int(input()) A = list(map(int, input().split())) L = list(map(int, input().split())) B = [-1000000000 if L[i]==1 else A[i] for i in range(n)] B.sort() B = B[::-1] k = 0 for i in range(n): if L[i] == 0: A[i] = B[k] k += 1 for i in A: print(i, end=" ") print()
8
PYTHON3
a = int(input()) b = list(map(int,input().strip().split())) c = list(map(int,input().strip().split())) b1 = b[1:] c1 = c[1:] n = set(b1+c1) l = len(n) if l==a: print("I become the guy.") else: print("Oh, my keyboard!")
7
PYTHON3
def my_bisect_right(x, a): for i in range(len(x)): if a < x[i]: return i return len(x) def my_bisect_right_enhanced(x, a): result_left = 0 result_right = len(x) while result_left + 1 < result_right: mid = (result_right + result_left) // 2 if a < x[mid]: result_right = mid else: result_left = mid if a < x[result_left]: return result_left else: return result_right from bisect import bisect_right str1 = "5" str2 = "3 10 8 6 11" str3 = "4" str1 = input() str2 = input() str3 = input() n = int(str1) x = sorted(list(map(int, str2.split()))) q = int(str3) y = list(map(int, "1 10 3 11".split())) y = list(range(q)) for i in range(q): y[i] = int(input()) # print(x) # print(bisect_right(x, 3)) # q = int(input()) q = 4 for m in y: # print(my_bisect_right(x, m)) print(my_bisect_right_enhanced(x, m)) # c = 0 # for j in x: # if m >= j: # c += 1 # print(c)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, i, is_one(0), ok(0), c(0); cin >> n; long a[n], b[n], s(0); for (i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; if (i != 0 && a[i - 1] > a[i]) { ok = 1; } if (a[i] == 1) is_one = 1; } if (is_one == 1 || ok == 0) { cout << "YES" << endl; } else { sort(b, b + n); s = b[0]; for (i = 0; i < n; i++) { if (a[i] == b[i] || a[i] % s == 0) continue; else { c = 1; break; } } if (c == 0) cout << "YES" << endl; else cout << "NO" << endl; } } }
9
CPP
#include <bits/stdc++.h> using namespace std; int H,W,K,dp[110][110],i,j,k,m,M=1e9+7; int main(){ cin>>H>>W>>K; dp[0][1]=1; for(i=1;i<=H;++i) for(k=0;k<1<<(W-1);++k)//kは各縦棒に対して横棒があるかどうかのビット列 if(!(k&k/2))//連続二箇所で横棒がなければ(/2によってkのビット列を右に1ずらすことができる,>>1と同じ) for(m=k<<1,j=1;j<=W;++j) (dp[i][j]+=dp[i-1][m>>j&1?j+1:m>>(j-1)&1?j-1:j])%=M;//m>>j&1は現在位置から右方向に横棒があるかどうか、m>>(j-1)&1は現在位置から左方向に横棒があるかどうか cout<<dp[H][K]<<endl; return 0; } /* dp[i-1][j-1] -(左方向の横棒)- dp[i-1][j] -(右方向の横棒)- dp[i-1][j+1] dp[i][j] 移動元は(i-1,j-1),(i-1,j),(i-1,j+1) 移動先は(i,j) (i-1,j-1)から右->下と移動して(i,j) (i-1,j+1)から左->下と移動して(i,j) (i-1,j)から真下と移動して(i,j) */
0
CPP
n=int(input()) s=[[],[]] for i,x in enumerate(input().split()): s[int(x)%2]+=[i+1] print(s[0][0] if len(s[0])==1 else s[1][0])
7
PYTHON3
t = int(input()) for i in range(t): a, b, n = map(int, input().split()) p = n % 3 if p == 0: print(a) elif p == 1: print(b) else: print(a ^ b)
7
PYTHON3
s=input() l=[] for i in range(len(s)): if s[i] not in l: l.append(s[i]) if len(l)%2==0: print("CHAT WITH HER!") else: print("IGNORE HIM!")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100010; int n, k, a[N]; int main(int argc, char const *argv[]) { scanf("%d %d", &n, &k); scanf("%d", a + 1); int r = a[1] % k, flag = 0, small = a[1]; for (int i = 2; i <= n; ++i) { scanf("%d", a + i); small = min(small, a[i]); if (a[i] % k != r) { flag = 1; } } if (flag) { puts("-1"); return 0; } long long moves = 0; for (int i = 1; i <= n; ++i) { moves += (1ll * a[i] - small) / (1ll * k); } printf("%lld\n", moves); return 0; }
7
CPP
n, k = map(int, input().split()) print(len([1 for i in map(int, input().split()) if 5 - i >= k ]) // 3 )
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int arr[100][100]; int row[100]; vector<pair<pair<int, int>, pair<int, int> > > v; int pos[30000][2]; int main() { int n; int sum = 0; while (scanf(" %d", &n) == 1) { v.clear(); sum = 0; for (int i = 1; i <= n; i++) scanf(" %d", &row[i]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= row[i]; j++) { scanf(" %d", &arr[i][j]); pos[arr[i][j]][0] = i; pos[arr[i][j]][1] = j; } sum += row[i]; } int cnt = 1; int swcnt = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= row[i]; j++) { if (arr[i][j] != cnt) { swcnt++; int r = pos[cnt][0]; int c = pos[cnt][1]; int val = arr[i][j]; arr[i][j] = cnt; arr[r][c] = val; pos[val][0] = r; pos[val][1] = c; pos[cnt][0] = i; pos[cnt][1] = j; v.push_back(make_pair(make_pair(i, j), make_pair(r, c))); } cnt++; } } printf("%d\n", swcnt); for (int i = 0; i < ((int)v.size()); i++) printf("%d %d %d %d\n", v[i].first.first, v[i].first.second, v[i].second.first, v[i].second.second); } }
8
CPP
#include <bits/stdc++.h> using namespace std; char gchar() { static char tbuf1[1000], *tmp_st = tbuf1, *tmp_ed = tbuf1; if (tmp_st == tmp_ed) { int len = fread(tbuf1, 1, 1000, stdin); if (len == 0) return -1; tmp_ed = tbuf1 + len; tmp_st = tbuf1; } return *(tmp_st++); } inline bool gn(int& x) { char c, sg = 0; while (c = gchar(), (c > '9' || c < '0') && c != '-' && ~c) ; if (c == -1) return 0; for ((c == '-' ? sg = 1, c = gchar() : 0), x = 0; c >= '0' && c <= '9'; c = gchar()) x = (x << 1) + (x << 3) + c - '0'; if (sg) x = -x; return 1; } pair<int, int> p[3001]; int sum[3001]; char ans[3001][3001]; inline void replace(int u, int v, int c) { p[u].first -= c, p[v].first -= 2 - c; int a = p[u].second, b = p[v].second; if (c == 2) ans[a][b] = 'W', ans[b][a] = 'L'; else if (c == 1) ans[a][b] = ans[b][a] = 'D'; else ans[a][b] = 'L', ans[b][a] = 'W'; } int main() { int n, m; gn(n), gn(m); for (int i = 1; i <= n; i++) { p[i].second = i; ans[i][i] = 'X'; sum[i] = sum[i - 1] + (n - i << 1); } int first = 0; for (int i = 1; i <= m; i++) { gn(p[i].first); first += p[i].first; if (first > sum[i]) return puts("no"); } if (sum[n] > first + p[m].first * (n - m)) return puts("no"); for (int i = m + 1; i <= n; i++) { p[i].first = (sum[n] - first + n - i) / (n - i + 1); first += p[i].first; } for (int i = 1; i <= n; i++) { sort(p + i, p + n + 1, [](pair<int, int> a, pair<int, int> b) { return a.first > b.first; }); for (int j = i + 1; j <= n; j++) { if (p[i].first == n - j + 1 << 1) replace(i, j, 2); else replace(i, j, 1); } } puts("yes"); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) putchar(ans[i][j]); puts(""); } }
11
CPP
print("YES" if int(input())%7%5%3==0 else "NO")
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(void) { int n; scanf("%d", &n); for (int i = 30; i >= 0; i--) { if (n >> i & 1) printf("%d ", i + 1); } puts(""); return 0; }
7
CPP
## necessary imports import sys input = sys.stdin.readline # from math import ceil, floor, factorial; def ceil(x, y): return (x + y - 1)//y; # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp ## gcd function def gcd(a,b): if b == 0: return a return gcd(b, a % b); ## nCr function efficient using Binomial Cofficient def nCr(n, k): if(k > n - k): k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return int(res) ## upper bound function code -- such that e in a[:i] e < x; def upper_bound(a, x, lo=0): hi = len(a) while lo < hi: mid = (lo+hi)//2 if a[mid] < x: lo = mid+1 else: hi = mid return lo ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0 and n > 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0 and n > 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## MODULAR EXPONENTIATION FUNCTION def power(x, y, p): res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 x = (x * x) % p return res ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b # find function with path compression included (recursive) # def find(x, link): # if link[x] == x: # return x # link[x] = find(link[x], link); # return link[x]; # find function with path compression (ITERATIVE) def find(x, link): p = x; while( p != link[p]): p = link[p]; while( x != p): nex = link[x]; link[x] = p; x = nex; return p; # the union function which makes union(x,y) # of two nodes x and y def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e7 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve() def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())) ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################### ---------------- TEMPLATE ENDS HERE ---------------- ################### for _ in range(int(input())): n = int(input()); ans = 0; i = 1; while(n): half = (n + 1) // 2; ans += half * i; i += 1; n -= half; print(ans);
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int mark[N]; pair <int , int> A[N], B[N]; vector <int> g[N]; bool cmp(int i, int j) {return B[i] < B[j];} bool cmp2(int i, int j) {return A[i] < A[j];} int dfs(int v) { mark[v] = 1; for (int u : g[v]) if (!mark[u]) dfs(u); } int main() { int n; cin >> n; for (int i = 0; i < n; i ++) cin >> A[i].first, A[i].second = i; for (int i = 0; i < n; i ++) cin >> B[i].first, B[i].second = i; sort(B, B + n); sort(A, A + n); for (int i = 0; i < n; i ++) if (A[i].first > B[i].first) return cout << "No\n", 0; for (int i = 0; i < n - 1; i ++) if (A[i + 1].first <= B[i].first) return cout << "Yes\n", 0; for (int i = 0; i < n; i ++) g[A[i].second].push_back(B[i].second); int cnt = 0; for (int i = 0; i < n; i ++) if (!mark[i]) dfs(i), cnt++; if (cnt > 1) return cout << "Yes\n", 0; return cout << "No\n", 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int OFF = (1 << 23); const int DOD = (1 << 22); int tour[2 * OFF], prop[2 * OFF], n, r; vector<pair<pair<int, int>, pair<int, int> > > v; void refresh(int i) { if (!prop[i]) return; tour[i] += prop[i]; if (i < OFF) { prop[2 * i] += prop[i]; prop[2 * i + 1] += prop[i]; } prop[i] = 0; } void update(int i, int a, int b, int lo, int hi, int x) { refresh(i); if (lo <= a && b <= hi) { prop[i] += x; refresh(i); return; } if (a > hi || b < lo) return; update(2 * i, a, (a + b) / 2, lo, hi, x); update(2 * i + 1, (a + b) / 2 + 1, b, lo, hi, x); tour[i] = max(tour[2 * i], tour[2 * i + 1]); } int main() { scanf("%d%d", &n, &r); for (int i = 0; i < n; i++) { int xx, yy; scanf("%d%d", &xx, &yy); int x = xx + yy; int y = xx - yy; v.push_back({{x - r, 0}, {y - r + DOD, y + r + DOD}}); v.push_back({{x + r, 1}, {y - r + DOD, y + r + DOD}}); } sort(v.begin(), v.end()); int ans = 0; for (int i = 0; i < 2 * n; i++) { update(1, 0, OFF - 1, v[i].second.first, v[i].second.second, 1 - 2 * v[i].first.second); ans = max(ans, tour[1]); } printf("%d\n", ans); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int M = 100 * 1000 + 1; map<int, pair<int, int> > vs[M]; int pre[M]; int main() { int n; scanf("%d", &n); pair<int, int> ret = pair<int, int>(0, -1); for (int i = 0; i < n; i++) { int v, c, l, r; scanf("%d%d%d%d", &v, &c, &l, &r); if (l == 0) { vs[l][c + r] = pair<int, int>(0, -1); } if (vs[l].count(c + r) > 0) { auto p = pair<int, int>(vs[l][c + r].first + v, i); pre[i] = vs[l][c + r].second; if (r == 0) { ret = max(ret, p); } else if (c + l < M) { vs[l + c][r] = max(vs[l + c][r], p); } } } vector<int> order; int c = ret.second; while (c >= 0) { order.push_back(c + 1); c = pre[c]; } reverse(order.begin(), order.end()); printf("%d\n", (int)order.size()); auto sp = ""; for (auto v : order) { printf("%s%d", sp, v); sp = " "; } puts(""); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int ans, a, b, c, d; a = b = c = d = -1000000000; int n, x, y; scanf("%d", &n); while (n--) { scanf("%d%d", &x, &y); a = max(a, x + y); b = max(b, x - y); c = max(c, y - x); d = max(d, -x - y); } cout << a + b + c + d + 4; }
9
CPP
#include <bits/stdc++.h> using namespace std; bool prim(long long n) { long long d = 2; while (d * d <= n) { if (n % d == 0) return false; d++; } return true; } long long t, n, nr; int main() { cin >> t; while (t--) { cin >> n; nr = 0; if (n == 1) { cout << "FastestFinger\n"; continue; } if (n == 2) { cout << "Ashishgup\n"; continue; } while (n % 2 == 0) { n /= 2; nr++; } if (n == 1) { cout << "FastestFinger\n"; continue; } if (nr == 1 && prim(n)) { cout << "FastestFinger\n"; continue; } cout << "Ashishgup\n"; } return 0; }
9
CPP
def solve(): n = int(input()) m = list(map(int, input().split())) s = sum(m) print("{:.12f}".format(s/n)); solve()
8
PYTHON3
#include <bits/stdc++.h> int check(char *aa, char *bb, int n) { static int kk[26]; int i, c; memset(kk, 0, sizeof kk); for (i = 0; i < n; i++) kk[aa[i] - 'a']++; for (i = 0; i < n; i++) kk[bb[i] - 'a']--; for (c = 0; c < 26; c++) if (kk[c]) return 0; return 1; } int main() { int q; scanf("%d", &q); while (q--) { static char aa[100 + 1], bb[100 + 1]; int n, h, i, j, max; scanf("%d%s%s", &n, aa, bb); if (!check(aa, bb, n)) { printf("-1\n"); continue; } max = 0; for (i = 0; i < n; i++) { for (j = i, h = 0; j < n; j++, h++) { while (h < n && aa[h] != bb[j]) h++; if (h == n) break; } if (max < j - i) max = j - i; } printf("%d\n", n - max); } return 0; }
11
CPP
while True: a,b=input().split() if b=='X': break a=list(a) if ('_' in a) and (b=='U' or b=='L'): c=0 for i in range(len(a)): if i==0 and b=='U': a[i]=a[i].upper() elif a[i]=='_': c+=1 elif c==1: a[i]=a[i].upper() c=0 a=[i for i in a if i!='_'] elif b=='U': a[0]=a[0].upper() elif b=='L': a[0]=a[0].lower() else: s=0 for i in range(len(a)): if i==0: a[0]=a[0].lower() s+=1 elif a[s].isupper(): a[s]=a[s].lower() a.insert(s,'_') s+=2 else: s+=1 for i in range(len(a)): if i==len(a)-1: print(a[i]) else: print(a[i],end='')
0
PYTHON3
n = int(input()) s = input() w = s.count('.') b = 0 r = w for i, x in enumerate(s): if x == '#': b += 1 else: w -= 1 r = min(r, b + w) print(r)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 10; const long long inf = 1e18; const long long mxb = 30; const long long mod = 1e9 + 7; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; long long n; long long a[N]; long long q; long long curans = 0; map<pair<long long, long long>, long long> mp; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (long long i = 0; i < n; ++i) { cin >> a[i]; curans += a[i]; } cin >> q; while (q--) { long long s, t, u; cin >> s >> t >> u; --s; --u; if (mp.find(make_pair(s, t)) != mp.end()) { long long wasu = mp[make_pair(s, t)]; ++a[wasu]; if (a[wasu] > 0) ++curans; mp.erase(mp.find(make_pair(s, t))); } if (u != -1) { --a[u]; if (a[u] >= 0) --curans; mp[make_pair(s, t)] = u; } cout << curans << endl; } }
11
CPP
n = int(input()) p = list(map(int, input().split())) odd = (n + 1) // 2 memo = {} def best(i, odd_count, last): if i == n: return 0 key = (i, odd_count, last) if key in memo: return memo[key] poss = range(2) if p[i] == 0 else [p[i] % 2] res = 10 ** 9 for parity in poss: new_odd_count = odd_count - parity new_even_count = n - i - odd_count - (parity ^ 1) if new_odd_count < 0 or new_even_count < 0: continue res = min(res, best(i + 1, new_odd_count, parity) + (1 if i > 0 and last != parity else 0)) memo[key] = res return res print(best(0, odd, 0))
7
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) ce = 0 co = 0 for i in range(n): if i%2!=arr[i]%2: if i%2 == 0: ce+=1 else: co+=1 if co == ce: print(co) else: print(-1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod=998244353; const int maxn=300; struct node{ int x,y; node operator-(const node&p)const { return (node){x-p.x,y-p.y}; } int operator*(const node&p)const { return x*p.y-y*p.x; } }d[maxn]; int n,p[maxn]; bool check(node a,node b,node c){ return ((b-a)*(c-a)==0); } int main(){ scanf("%d",&n); if(n<=2) return puts("0"),0; for (int i=1; i<=n; i++) scanf("%d%d",&d[i].x,&d[i].y); p[0]=1; for (int i=1; i<=n; i++) p[i]=(p[i-1]*2)%mod; int cnt; long long ans=0; for (int i=1; i<n; i++) for (int j=i+1; j<n; j++){ cnt=n-j; for (int k=j+1; k<=n; k++) if(check(d[i],d[j],d[k])) cnt--; (ans+=1ll*p[n-j-cnt]*(p[cnt]-1)%mod)%=mod; } printf("%lld\n",ans); return 0; }
0
CPP
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { ll N, W; cin >> N >> W; vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0)); for (int i = 1, v, w, m; i <= N; i++) { cin >> v >> w >> m; for (int j = 0; j < w; j++) { deque<int> dq; for (int k = 0; k * w + j <= W; k++) { if (k > m && dq.front() == k - m - 1) dq.pop_front(); while (!dq.empty() && dp[i - 1][dq.back() * w + j] - v * dq.back() < dp[i - 1][k * w + j] - v * k) { dq.pop_back(); } dq.push_back(k); dp[i][k * w + j] = dp[i - 1][dq.front() * w + j] - v * dq.front() + v * k; } } } cout << dp[N][W] << endl; return 0; }
0
CPP
#include <bits/stdc++.h> const int N = 3003; using namespace std; long long ans, sum; int n, m, d, k, ax[N], ay[N], na[N], nb[N], sa[N], sb[N], pre[N], nxt[N], t; bool cmpa(int x, int y) { return ay[x] < ay[y]; } bool cmpb(int x, int y) { return ax[x] > ax[y]; } void dfs(int x, int d) { if (d > k || x < 0) return; sum -= sa[x] * sb[x]; dfs(pre[x], d + 1); sb[x] = sb[nxt[x]]; if (d == 1) sa[x] += sa[nxt[x]]; if (d) sum += sa[x] * sb[x]; } int main() { scanf("%d%d%d%d", &n, &m, &d, &k); for (int i = 1; i <= d; i++) scanf("%d%d", ax + i, ay + i); ay[d + 1] = m + 1; for (int i = 1; i <= n; i++) { sum = t = 0; for (int j = 1; j <= d; j++) if (ax[j] >= i) t++, na[t] = nb[t] = j; na[++t] = d + 1; sort(na + 1, na + t, cmpa); sort(nb + 1, nb + t, cmpb); for (int j = 0, s; j < t; j++) { if (s = na[j]) pre[s] = na[j - 1]; else pre[s] = -1; nxt[s] = na[j + 1], sa[s] = ay[na[j + 1]] - ay[s], sb[s] = j + k < t ? m + 1 - ay[na[j + k]] : 0; sum += sa[s] * sb[s]; } for (int j = n, l = 1; j >= i; j--) { for (int s; l < t && ax[nb[l]] > j; l++) { s = nb[l], dfs(s, 0); nxt[pre[s]] = nxt[s]; pre[nxt[s]] = pre[s]; } ans += sum; } } printf("%lld", ans); }
12
CPP
#include <bits/stdc++.h> using namespace std; int n, m, a[300010], f[300010], p; vector<int> x[300010], w; int main() { srand(time(0)); int i, j, k, l; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) { a[i] = i + 1; for (j = i + i; j <= n; j += i) x[j].push_back(i); } p = n - 1; for (i = 1; i <= p; i++) { for (j = 2; j * j <= a[i]; j++) if (a[i] % j == 0) break; if (j * j > a[i] && a[i] * 2 > n) { swap(a[i], a[p]); p--; i--; } } for (int t = 1; t <= 12; t++) { for (i = 1; i <= n; i++) f[i] = 0; for (i = 1; i <= p; i++) swap(a[i], a[((rand() << 15) + rand()) % i + 1]); for (i = 1, k = 0; i <= p && k < m; i++) { for (j = a[i], l = 0; j <= n; j += a[i]) if (f[j]) l++; for (auto j : x[a[i]]) if (f[j]) l++; if (k + l <= m) { k += l; f[a[i]] = 1; w.push_back(a[i]); } if (k + w.size() >= m - (n - 1 - p)) { k += w.size(); w.push_back(1); for (j = p + 1; k < m; j++, k++) w.push_back(a[j]); break; } } if (k < m && k + w.size() >= m - (n - 1 - p)) { k += w.size(); w.push_back(1); for (j = p + 1; k < m; j++, k++) w.push_back(a[j]); } if (k == m) { printf("Yes\n%d\n", w.size()); for (auto i : w) printf("%d ", i); return 0; } w.clear(); } printf("No\n"); return 0; }
12
CPP
num = int(input()) scores = list(map(int, input().split())) count = 0 minScore = scores[0] maxScore = scores[0] for i in range(1, len(scores)): if scores[i] > maxScore: count += 1 maxScore= scores[i] continue if scores[i] < minScore: count += 1 minScore = scores[i] print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; struct triple { int a, b, c; triple() : a(0), b(0), c(0) {} triple(int a, int b, int c) : a(a), b(b), c(c) {} }; namespace std { template <> class hash<pair<int, int> > { public: inline size_t operator()(const pair<int, int>& p) const { return hash<int>()(p.first) ^ hash<int>()(p.second); } }; }; // namespace std template <typename Comparer> bool gen(const vector<triple>& v, Comparer comp, int pos, vector<pair<char, char> >& ans, triple& val) { if (pos == v.size()) return comp(ans, val); ans[pos] = {'L', 'M'}; val.a += v[pos].a; val.b += v[pos].b; if (!gen(v, comp, pos + 1, ans, val)) return false; val.a -= v[pos].a; val.b -= v[pos].b; ans[pos] = {'L', 'W'}; val.a += v[pos].a; val.c += v[pos].c; if (!gen(v, comp, pos + 1, ans, val)) return false; val.a -= v[pos].a; val.c -= v[pos].c; ans[pos] = {'M', 'W'}; val.b += v[pos].b; val.c += v[pos].c; if (!gen(v, comp, pos + 1, ans, val)) return false; val.b -= v[pos].b; val.c -= v[pos].c; return true; } template <typename Comparer> void launch_gen(const vector<triple>& v, Comparer comp) { vector<pair<char, char> > ans(v.size()); triple val; gen(v, comp, 0, ans, val); } unordered_map<pair<int, int>, pair<int, vector<pair<char, char> > > > m; int best = -1234567890; vector<pair<char, char> > res; inline bool map_putter(const vector<pair<char, char> >& ans, const triple& val) { pair<int, int> p(val.a - val.b, val.b - val.c); if (!m.count(p) || val.c > m[p].first) m[p] = make_pair(val.c, ans); return true; } inline bool ans_seeker(const vector<pair<char, char> >& ans, const triple& val) { pair<int, int> p(val.b - val.a, val.c - val.b); if (m.count(p)) { auto& ptr = m[p]; if (ptr.first + val.c > best) { best = ptr.first + val.c; res.clear(); res.insert(res.end(), ptr.second.begin(), ptr.second.end()); res.insert(res.end(), ans.begin(), ans.end()); } } return true; } int main() { int n; cin >> n; vector<triple> v1, v2; for (int i = 0; i < n; i++) { triple t; cin >> t.a >> t.b >> t.c; ((i < (n / 2)) ? v1 : v2).push_back(t); } launch_gen(v1, map_putter); launch_gen(v2, ans_seeker); if (res.empty()) cout << "Impossible" << endl; else for (auto it : res) cout << it.first << it.second << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; vector<long long> time(n + 1), storeTime(n + 1), moments(m); for (long long i = 1; i <= n; i++) { long long x, y; cin >> x >> y; time[i] = (x * y); } for (long long i = 0; i < m; i++) cin >> moments[i]; storeTime[1] = time[1]; for (int i = 2; i <= n; i++) { storeTime[i] = (time[i] + storeTime[i - 1]); } for (int i = 0; i < m; i++) { auto l = lower_bound(storeTime.begin(), storeTime.end(), moments[i]); long long lowerDistance = l - storeTime.begin(); cout << lowerDistance << "\n"; } }
8
CPP
#include <bits/stdc++.h> int a, b, r; int main() { scanf("%d%d%d", &a, &b, &r); printf("%s\n", 2 * r > ((a) < (b) ? (a) : (b)) ? "Second" : "First"); }
7
CPP
#include <bits/stdc++.h> using namespace std; template <class T1, class T2, class T3 = hash<T1>> using umap = unordered_map<T1, T2, T3>; template <class T> using uset = unordered_set<T>; template <class T> using vec = vector<T>; const long long infll = numeric_limits<long long>::max() >> 1; const long long inf = numeric_limits<long long>::max() >> 5; const long long N = 2e5 + 5; long long n, k; pair<long long, long long> p[N]; vec<pair<long long, long long>> v[N]; void input() { cin >> n >> k; for (long long i = 0; i < n; ++i) { cin >> p[i].first; } for (long long i = 0; i < n; ++i) { cin >> p[i].second; } } bool test(long long q) { for (long long i = 0; i <= k + 2; ++i) { v[i].clear(); } for (long long i = 0; i < n; ++i) { long long d = min(k, p[i].first / p[i].second); v[d].push_back(p[i]); } long long cnt = 0; for (long long i = 0; i < k - 1; ++i) { cnt++; for (pair<long long, long long> e : v[i]) { long long c = ceil((double)((i + 1) * e.second - e.first) / q); v[min(k, (e.first + c * q) / e.second)].push_back( {e.first + c * q, e.second}); cnt -= c; if (cnt < 0) { return 0; } } v[i].clear(); } return 1; } long long bin_search(long long l, long long r) { if (l == r) { return l; } long long q = l + r >> 1; if (test(q)) { return bin_search(l, q); } else { return bin_search(q + 1, r); } } void solve() { long long q = bin_search(0, inf); if (q == inf) { cout << "-1\n"; } else { cout << q << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); input(); solve(); }
10
CPP
n = int(input()) teams, score = ['', ''], [0, 0] for i in range(n): s = input() if (teams[0] == ''): teams[0] = s score[0] = 1 elif (teams[0] == s): score[0] += 1 elif (teams[1] == ''): teams[1] = s score[1] = 1 else: score[1] += 1 print(teams[0] if score[0] > score[1] else teams[1])
7
PYTHON3
#include <cstdio> const int N=4e3+5,mod=1e9+7; int A,B,n,c[N][N],f[N][N],ans; int main(){ scanf("%d%d",&A,&B); n=4000; for(int i=0;i<=n;++i){ for(int j=0;j<=i;++j){ if(j==0) c[i][j]=1; else c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod; f[i][j]=c[i][j]; } } for(int i=0;i<=n;++i){ for(int j=1;j<=n;++j){ f[i][j]=(f[i][j]+f[i][j-1])%mod; } } for(int i=0;i<=n;++i){ for(int j=1;j<=n;++j){ f[i][j]=(f[i][j]+f[i][j-1])%mod; } } for(int i=1;i<=A+1;++i){ for(int j=0;j<=i-1;++j){ int tmp=(j==0?1:f[j-1][i-j-1]); ans=(ans+1ll*c[B-1][j]*tmp%mod)%mod; } } printf("%d\n",ans); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; long long int a, b; long long int n, exclude; vector<int> ans1, ans2; void init(); void process(); int main() { ios::sync_with_stdio(false); cin.tie(0); init(); process(); cout.flush(); return 0; } void init() { cin >> a >> b; n = (sqrt(1 + 8 * (a + b)) - 1) / 2; if (n * (n + 1) / 2 < a + b) n++; exclude = n * (n + 1) / 2 - a - b; } void stat1(long long int a, long long int b) { vector<int> arr, brr; for (long long int x = min(n, a); a > 0 && x > 0; x = min(a, x - 1)) { if (x == exclude) continue; a -= x, arr.push_back(x); } long long int sum = 0; reverse(arr.begin(), arr.end()); for (long long int i = 1, j = 0; i <= n; i++) { if (j < arr.size() && arr[j] == i) j++; else { if (sum + i > b) break; sum += i, brr.push_back(i); } } if (arr.size() + brr.size() > ans1.size() + ans2.size()) ans1 = arr, ans2 = brr; } void stat2(long long int a, long long int b) { vector<int> arr, brr; for (long long int x = min(n, a) - 1; a > 0 && x > 0; x = min(a, x - 1)) { if (x == exclude) continue; a -= x, arr.push_back(x); } long long int sum = 0; reverse(arr.begin(), arr.end()); for (long long int i = 1, j = 0; i <= n; i++) { if (j < arr.size() && arr[j] == i) j++; else { if (sum + i > b) break; sum += i, brr.push_back(i); } } if (arr.size() + brr.size() > ans1.size() + ans2.size()) ans1 = arr, ans2 = brr; } void process() { stat1(a, b); stat2(a, b); cout << ans1.size() << '\n'; for (auto x : ans1) cout << x << " "; cout << '\n'; cout << ans2.size() << '\n'; for (auto x : ans2) cout << x << " "; cout << '\n'; }
9
CPP
def delit(n): i = 1 arr = [] while i*i <= n: if n % i == 0: arr.append(i) if i != n//i: arr.append(n//i) i += 1 return sorted(arr) def reverse(s): str = "" for i in s: str = i + str return str def jifr(arr, ta, na): arr2 = '' for i in arr: arr2 = reverse(ta[:i]) ta = ta[i:] ta = arr2 + ta return ta n = int(input()) t = input() de = delit(n) del de[0] print(jifr(de, t, n))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 200000; const int MAX_E2 = 1 << 19; const int M = 5; const int INF = 1 << 29; struct Elm { int ms[M][M]; Elm() {} void init() { for (int i = 0; i < M; i++) fill(ms[i], ms[i] + M, INF), ms[i][i] = 0; } int get(int i, int j) const { return ms[i][j]; } void set(int i, int j, int v) { ms[i][j] = v; } void _setmin(int &a, int b) const { if (a > b) a = b; } Elm operator+(const Elm &e) const { Elm r; for (int i = 0; i < M; i++) for (int j = i; j < M; j++) { r.ms[i][j] = INF; for (int k = i; k <= j; k++) _setmin(r.ms[i][j], ms[i][k] + e.ms[k][j]); } return r; } void print() { for (int i = 0; i < M; i++) { for (int j = 0; j < M; j++) printf("%d ", ms[i][j]); putchar('\n'); } } }; template <typename T, const int MAX_E2> struct SegTree { int e2; T nodes[MAX_E2], z; SegTree() {} void init(int n, T _z) { z = _z; for (e2 = 1; e2 < n; e2 <<= 1) ; fill(nodes, nodes + MAX_E2, z); } T &get(int i) { return nodes[e2 - 1 + i]; } void seti(int i, T v) { get(i) = v; } void setall() { for (int j = e2 - 2; j >= 0; j--) nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2]; } T sum_range(int r0, int r1, int k, int i0, int i1) { if (r1 <= i0 || i1 <= r0) return z; if (r0 <= i0 && i1 <= r1) return nodes[k]; int im = (i0 + i1) / 2; T v0 = sum_range(r0, r1, k * 2 + 1, i0, im); T v1 = sum_range(r0, r1, k * 2 + 2, im, i1); return v0 + v1; } T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); } }; char s[MAX_N + 4]; SegTree<Elm, MAX_E2> st; int main() { int n, qn; scanf("%d%d%s", &n, &qn, s); Elm zelm; zelm.init(); st.init(n, zelm); for (int i = 0; i < n; i++) { Elm e; e.init(); switch (s[i]) { case '2': e.set(0, 0, 1); e.set(0, 1, 0); break; case '0': e.set(1, 1, 1); e.set(1, 2, 0); break; case '1': e.set(2, 2, 1); e.set(2, 3, 0); break; case '6': e.set(3, 3, 1); e.set(4, 4, 1); break; case '7': e.set(3, 3, 1); e.set(3, 4, 0); break; } st.seti(i, e); } st.setall(); while (qn--) { int l, r; scanf("%d%d", &l, &r); l--; Elm e = st.sum_range(l, r); int v = e.get(0, 4); printf("%d\n", (v >= INF) ? -1 : v); } return 0; }
11
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/stack:64000000") using namespace std; const double eps = 1e-9; const double PI = 2.0 * acos(0.0); int v[3002][3002]; int area[3002][3002]; int hb[3002][3002][2]; int vb[3002][3002][2]; int nmax = 3002; inline int get_area(int a, int b) { if (a < 0 || b < 0) return 0; return area[a][b]; } int sol[100007]; int main() { int id = 0; int n; cin >> n; for (int i = 0; i < n; ++i) { int x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; for (int px = x1; px < x2; ++px) for (int py = y1; py < y2; ++py) v[px + 1][py + 1] = i + 1; } for (int i = 0; i < nmax; ++i) { for (int j = 0; j < nmax; ++j) { if (v[i][j] == 0) continue; hb[i][j][0] = hb[i][j][1] = 1; if (i > 0 && v[i][j] == v[i - 1][j]) hb[i][j][0] = 0; if (i + 1 < nmax && v[i][j] == v[i + 1][j]) hb[i][j][1] = 0; } } for (int i = 0; i < nmax; ++i) for (int j = nmax - 1; j >= 0; --j) { if (j + 1 < nmax) { if (hb[i][j][0]) hb[i][j][0] += hb[i][j + 1][0]; if (hb[i][j][1]) hb[i][j][1] += hb[i][j + 1][1]; } } for (int j = 0; j < nmax; ++j) { for (int i = 0; i < nmax; ++i) { if (v[i][j] == 0) continue; vb[i][j][0] = vb[i][j][1] = 1; if (j > 0 && v[i][j] == v[i][j - 1]) vb[i][j][0] = 0; if (j + 1 < nmax && v[i][j] == v[i][j + 1]) vb[i][j][1] = 0; } } for (int j = 0; j < nmax; ++j) { for (int i = nmax - 1; i >= 0; --i) { if (i + 1 < nmax) { if (vb[i][j][0]) vb[i][j][0] += vb[i + 1][j][0]; if (vb[i][j][1]) vb[i][j][1] += vb[i + 1][j][1]; } } } for (int i = 0; i < nmax; ++i) for (int j = 0; j < nmax; ++j) { area[i][j] = get_area(i - 1, j) + get_area(i, j - 1) - get_area(i - 1, j - 1); if (v[i][j]) area[i][j] += 1; } int best = 0; int bx, by; for (int i = 0; i < nmax; ++i) for (int j = 0; j < nmax; ++j) if (hb[i][j][0] > best && vb[i][j][0] > best) { int W = hb[i][j][0]; int H = vb[i][j][0]; for (int best2 = best + 1; best2 <= min(W, H); ++best2) { int px = i + best2 - 1; int py = j + best2 - 1; if (px >= nmax || py >= nmax) break; if (get_area(px, py) - get_area(px, j - 1) - get_area(i - 1, py) + get_area(i - 1, j - 1) != best2 * best2) break; if (hb[px][j][1] < best2) continue; if (vb[i][py][1] < best2) continue; best = best2; bx = i; by = j; } } if (best == 0) { cout << "NO\n"; return 0; } cout << "YES " << '\n'; set<int> my_corr; for (int i = 0; i < best; ++i) for (int j = 0; j < best; ++j) { sol[v[bx + i][by + j]] = 1; if (v[bx + i][by + j] == 0) cout << "zero;"; my_corr.insert(v[bx + i][by + j]); } for (int i = 0; i < nmax; ++i) for (int j = 0; j < nmax; ++j) { if (i >= bx && i < bx + best && j >= by && j < by + best) continue; if (my_corr.find(v[i][j]) != my_corr.end()) cout << "outside"; } vector<int> vlad; for (int i = 1; i <= n; ++i) if (sol[i]) vlad.push_back(i); cout << vlad.size() << '\n'; for (int i = 0; i < vlad.size(); ++i) { if (i) cout << " "; cout << vlad[i]; } cout << '\n'; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; void run_case() { int64_t l, r; cin >> l >> r; if ((r / 2 + 1) >= l) cout << r % (r / 2 + 1) << "\n"; else cout << r % l << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int start = clock(); int test_cases; test_cases = 1; cin >> test_cases; while (test_cases--) { run_case(); } cerr << "[Finished in " << clock() - start << " ms]" << "\n"; return 0; }
7
CPP
for i in range(0,int(input())): n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) c=min(a) d=min(b) m=0 e=0 f=0 for j in range(0,n): if(a[j]>c): e=a[j]-c else: e=0 if(b[j]>d): f=b[j]-d else: f=0 if(e!=0 or f!=0): m+=max(e,f) print(m)
8
PYTHON3
from sys import stdin n = int(stdin.readline()) a = stdin.readline() b = stdin.readline() a += "0" b += "0" ans = 0 i = 0 while (i < n): if (a[i] != b[i]): if (a[i] == b[i + 1] and b[i] == a[i + 1]): i += 2 ans += 1 else: i += 1 ans += 1 else: i += 1 print(ans)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int f(int k) { return (k * (k - 1) / 2 - k / 2) / 2; } int main() { int n, m, k(1); scanf("%d %d", &n, &m); while (k <= n && f(k) < m) ++k; if (k > n) { printf("-1\n"); return 0; } if (f(k) > m) --k; int rem = m - f(k); int l = 2 * k - 2 * rem + 1; for (int i = 1; i <= k; ++i) printf("%d ", i); if (k < n) { printf("%d ", l); const int N = 1e6; for (int i = k + 2; i <= n; ++i) printf("%d ", N + 10002 * i); } printf("\n"); }
11
CPP
n, m = map(int, input().split()) p = n * m if min(n, m) % 2 == 0: print("Malvika") else: print("Akshat")
7
PYTHON3
n, l, r = map(int, input().split()) def solve(n, l, r): min_ = (n-l) + 2**l-1 max_ = (n-r) * 2**(r-1) + 2**r-1 return min_, max_ min_, max_ = solve(n, l, r) print(min_, max_)
8
PYTHON3
n=int(input()) z=input() sol=0 k=z.count('8') while n>=11 and k>=1: if n>=11 and k>=1: sol+=1 k-=1 n-=11 else: break print(sol)
7
PYTHON3
n = int(input()) k = list(map(int, input().split())) k = sorted(k) r=[] for i in k: if i not in r: r.append(i) r=sorted(r) if len(r)==1:print(len(k)*k[0]) elif len(r)==2: while r[0]!=r[1]: r.append(r[1]-r[0]) r=sorted(r) r.remove(r[-1]) print(len(k)*r[0]) elif len(k)==3: s=[] for h in range(1,len(k)): if k[h] % k[0] != 0: s.append(len(k)) else: t = [] for w in range(1, len(r)): t.append(r[w] - r[w - 1]) s.append(min(t) * len(k)) print(min(s)) elif len(r)>3: t=[] for w in range(1,len(r)): t.append(r[w]-r[w-1]) print(min(t)*len(k))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; string s[500000]; const int mod = 1000003; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> s[i]; int result = 1; for (int i = 0; i < n; i++) { bool ok1 = false, ok2 = false; for (int j = 0; j < m; j++) { if (j % 2 == 0 && (s[i][j] == '1' || s[i][j] == '2')) ok1 = true; if (j % 2 == 1 && (s[i][j] == '3' || s[i][j] == '4')) ok1 = true; if (j % 2 == 1 && (s[i][j] == '1' || s[i][j] == '2')) ok2 = true; if (j % 2 == 0 && (s[i][j] == '3' || s[i][j] == '4')) ok2 = true; } if (ok1 && ok2) result *= 0; if (!ok1 && !ok2) result = (result * 2) % mod; } for (int j = 0; j < m; j++) { bool ok1 = false, ok2 = false; for (int i = 0; i < n; i++) { if (i % 2 == 0 && (s[i][j] == '4' || s[i][j] == '1')) ok1 = true; if (i % 2 == 1 && (s[i][j] == '2' || s[i][j] == '3')) ok1 = true; if (i % 2 == 1 && (s[i][j] == '4' || s[i][j] == '1')) ok2 = true; if (i % 2 == 0 && (s[i][j] == '2' || s[i][j] == '3')) ok2 = true; } if (ok1 && ok2) result *= 0; if (!ok1 && !ok2) result = (result * 2) % mod; } cout << result; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m, x; scanf("%d%d%d", &n, &m, &x); x--; int res = (n - 2 * x + m - 2 * x) - 2; if (res == 0) res = 1; if (n - 2 * x <= 0 || m - 2 * x <= 0) res = 0; printf("%d\n", (res < 0 ? 0 : res)); return 0; }
8
CPP
s=input() l=len(s) s+="123" for i in range(l): if s[i]==s[i+1]: print(i+1,i+2) exit() elif s[i]==s[i+2]: print(i+1,i+3) exit() print(-1,-1)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; ifstream in; ofstream out; const long long INF = LLONG_MAX; const long double EPS = 1e-9; const long double pi = 3.141592653589793238462643383279502884; long long n; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(20); srand(time(0)); cin >> n; vector<long long> a[n]; for (long long i = 0; i < n; i++) { long long x, y; cin >> x >> y; x--, y--; a[i].push_back(x); a[i].push_back(y); } vector<long long> ans; long long k1 = a[0][0]; long long k2 = a[0][1]; if (a[k1][0] == k2 || a[k1][1] == k2) ans.push_back(k1), ans.push_back(k2); else ans.push_back(k2), ans.push_back(k1); while (ans.size() != n) { if (a[ans[ans.size() - 2]][0] == ans.back()) ans.push_back(a[ans[ans.size() - 2]][1]); else ans.push_back(a[ans[ans.size() - 2]][0]); } for (long long i : ans) cout << i + 1 << ' '; return 0; }
10
CPP
#include <bits/stdc++.h> const double eps = 0.0000001; using namespace std; inline int sgn(double x) { return (x > eps) - (x < -eps); } inline char gc() { static char buf[1 << 16], *S, *T; if (S == T) { T = (S = buf) + fread(buf, 1, 1 << 16, stdin); if (S == T) return EOF; } return *S++; } inline int read() { register int x = 0, f = 1; register char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x * f; } long long f[(long long)1e5]; long long num[(long long)1e5]; long long Idx; bool flag = 1; void sum(long long Sum, long long idx, long long IDX) { if (Sum < 0) return; if (idx <= 0) return; if (Sum == 0) { Idx = IDX; flag = 0; return; } if (flag == 0) return; num[IDX] = f[idx]; sum(Sum - f[idx], idx - 1, IDX + 1); sum(Sum, idx - 1, IDX); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n = 3; f[1] = 1; f[2] = 1; long long s, k; cin >> s >> k; for (; f[n - 1] < s; ++n) { f[n] = 2 * f[n - 1] - (n - k - 1 < 0 ? 0 : f[n - k - 1]); } sum(s, n, 1); cout << Idx - 1 << endl; if (Idx - 1 == 1) { cout << 0 << " "; } for (int i = 1; i < Idx; ++i) cout << num[i] << " "; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long n, m, nn, rt, sm, sm2, vtd[100069], vl[100069], sq[100069]; vector<pair<long long, long long>> al[100069]; bitset<100069> py; bool bad, e; void dfs(long long x) { long long i, sz = al[x].size(), l, w; vtd[x] = 1; nn++; vl[nn] = sq[x] * (py[x] * 2 - 1); for (i = 0; i < sz; i++) { l = al[x][i].first; w = al[x][i].second; if (!vtd[l]) { py[l] = py[x] ^ 1; sq[l] = w - sq[x]; dfs(l); } else if (py[l] == py[x]) { rt = l; bad = 0; sm = 0; sm2 = 0; e = 0; } if (rt) { if (!bad) { sm += w; sm2 += w * e; e ^= 1; } if (x == rt) { sq[x] = sm / 2 - sm2; bad = 1; } return; } } } void trv(long long x) { long long i, sz = al[x].size(), l, w; vtd[x] = 2; for (i = 0; i < sz; i++) { l = al[x][i].first; w = al[x][i].second; if (vtd[l] != 2) { sq[l] = w - sq[x]; trv(l); } else if (sq[l] != w - sq[x]) { printf("NO\n"); exit(0); } } } int main() { long long i, k, l, w; scanf("%lld%lld", &n, &m); for (i = 0; i < m; i++) { scanf("%lld%lld%lld", &k, &l, &w); w *= 2; al[k].push_back({l, w}); al[l].push_back({k, w}); } for (i = 1; i <= n; i++) { if (!vtd[i]) { rt = 0; nn = 0; dfs(i); if (!rt) { rt = i; sort(vl + 1, vl + nn + 1); sq[i] = vl[(nn + 1) / 2]; } trv(rt); } } printf("YES\n"); for (i = 1; i <= n; i++) { if (sq[i] < 0) { printf("-"); } printf("%lld", abs(sq[i]) / 2); if (sq[i] % 2) { printf(".5"); } printf("%c", " \n"[i == n]); } }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { string s; cin >> s; string res = ""; for (int i = 0; i < n; i++) { if (s[i] <= '1') continue; else { if (s[i] == '4') res += '3', res += '2', res += '2'; else { if (s[i] == '6') res += '5', res += '3'; else { if (s[i] == '8') res += '7', res += '2', res += '2', res += '2'; else { if (s[i] == '9') res += '3', res += '3', res += '2', res += '7'; else res += s[i]; } } } } } sort(res.begin(), res.end()); reverse(res.begin(), res.end()); cout << res << endl; } }
7
CPP
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; const int nax=20*1007; const int d=15; const int kax=32; int n, q; int tab[nax]; int cel[nax]; int jump[nax][kax][kax]; int aktu[kax][kax]; int dalszy(int a, int b) { return (tab[a]>tab[b] ? a : b); } int main() { scanf("%d%d", &n, &q); for (int i=1; i<=n; i++) { scanf("%d", &tab[i]); tab[i]=min(n, i+tab[i]); } vector<int> stos; for (int i=n; i; i--) { while(!stos.empty() && tab[i]>tab[stos.back()]) stos.pop_back(); stos.push_back(i); int bsa=0; int bsb=(int)stos.size()-1; while(bsa<bsb) { int bss=(bsa+bsb)>>1; if (stos[bss]<=tab[i]) bsb=bss; else bsa=bss+1; } cel[i]=stos[bsa]; } for (int i=n; i; i--) { jump[i][0][0]=cel[i]; for (int j=1; j<kax; j++) jump[i][0][j]=dalszy(min(n, tab[i]+j), jump[i][0][j-1]); for (int j=1; j<d; j++) for (int l=0; l<kax; l++) for (int p=0; p<=l; p++) jump[i][j][l]=dalszy(jump[i][j][l], jump[jump[i][j-1][p]][j-1][l-p]); } while(q--) { int a, b, k; scanf("%d%d%d", &a, &b, &k); if (a==b) { printf("0\n"); continue; } if (tab[a]+k>=b) { printf("1\n"); continue; } int wyn=0; for (int j=0; j<=k; j++) aktu[d][j]=a; for (int j=d-1; j>=0; j--) { for (int l=0; l<=k; l++) aktu[j][l]=a; int moge=0; for (int l=0; l<=k; l++) { aktu[j][l]=a; for (int p=0; p<=l; p++) aktu[j][l]=dalszy(aktu[j][l], jump[aktu[j+1][p]][j][l-p]); if (tab[aktu[j][l]]+k-l>=b) moge=1; } if (moge) { for (int l=0; l<=k; l++) aktu[j][l]=aktu[j+1][l]; } else { wyn+=(1<<j); } } printf("%d\n", wyn+2); } return 0; }
14
CPP
for _ in range(int(input())): n,m=map(int,input().split()) a=n*m if(a%2==0): print(a//2) else: print((a+1)//2)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long n, k, p, bobwin; long f[250010][30]; long long fac[250010], inversefac[250010]; long power(long x, long y, long p); long F(long n, long k, long p); void lists(long a[], long i, long num, long n, long k, long p); void inverse(long a, long b, long long& x, long long& y) { if (a == 1) { y = 0; x = 1; return; } else if (b == 1) { x = 1; y = a - 1; return; } if (a > b) { long r = a % b, k = a / b; inverse(r, b, x, y); long long x1 = x, y1 = k * x + y; x = x1; y = y1; } else { long r = b % a, k = b / a; inverse(a, r, x, y); long long x1 = k * y + x, y1 = y; x = x1; y = y1; } return; } long long C(long long n, long long m, long long p) { return (((fac[n] * inversefac[m]) % p) * inversefac[n - m]) % p; } long power(long x, long y, long p) { if (y == 0) { return 1; } else { long long t = power(x, y / 2, p); if (y % 2 == 0) { return (t * t) % p; } else { return (((t * t) % p) * x) % p; } } } void lists(bool a[], long i, long num, long n, long k, long p) { if (i == 0) { long long res = (F(num, k - 1, p) * C(n, num, p)) % p; res = (res * k) % p; f[n][k] = (f[n][k] + res) % p; return; } if (a[i]) { lists(a, i - 1, num, n, k, p); lists(a, i - 1, num + (1 << i), n, k, p); } else { lists(a, i - 1, num, n, k, p); } return; } long F(long n, long k, long p) { if ((n == 0) || (k == 1)) { return 1; } if (f[n][k] != 0) { return f[n][k]; } bool a[19]; long i, oneInN = 0; for (i = 1; i < 18; i++) { if ((n >> i) % 2 == 1) { a[i] = true; oneInN++; } else { a[i] = false; } } i--; while (!a[i]) i--; f[n][k] = 0; lists(a, i - 1, 0, n, k, p); return f[n][k]; } int main() { cin >> n >> k >> p; long total = power(k, n, p); if (n % 2 == 1) { cout << total << endl; } else { fac[0] = 1; inversefac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % p; long long y; inverse(fac[i], p, inversefac[i], y); } int log2n = (int)(log(n) / log(2)); bobwin = 0; memset(f, 0, sizeof(f)); bobwin = F(n, k, p) % p; cout << (total - bobwin + p) % p << endl; } return 0; }
9
CPP
x = int(input()) y = input() k = 0 for i in range(x): if int(y[i])%2 == 0: k = k + i + 1 print(k)
7
PYTHON3
a = input() b = input() c = int(a) + int(b) digits_a = [(i) for i in str(a) if i != '0'] digits_b = [(j) for j in str(b) if j != '0'] digits_c = [k for k in str(c) if k != '0'] a1 = ''.join(digits_a) b1 = ''.join(digits_b) c1 = ''.join(digits_c) if int(a1) + int(b1) == int(c1): print("YES") else: print("NO")
7
PYTHON3
if __name__ == '__main__': n = int(input()) record = [] for i in range(n): record.append(tuple(map(int, input().split()))) # mark_record = sorted(record, key=lambda x:(x[0])) # early_record = sorted(record, key=lambda x:(x[1], x[0])) # early_record1 = sorted(early_record, key=lambda x:x[0]) current, max_day = 0, 0 r = sorted(record, key=lambda x:(x[0], x[1])) for i in r: if i[1] > max_day: max_day = i[1] else: if i[1] != max_day: max_day = i[0] print(max_day) # for i in range(n - 1): # if early_record[i][0] > early_record[i + 1][0]: # print(mark_record[n - 1][0]) # exit(0) # print(early_record[n - 1][1]) # print(early_record) # print(mark_record) # print(early_record == mark_record) # for i in range(n): # if mark_record[i] != early_record[i]: # print(mark_record[n - 1][0]) # exit(0) # if early_record[n - 1][0] == mark_record[n - 1][0]: # print(early_record[n - 1][1]) # else: # print(mark_record[n - 1][0])
9
PYTHON3
import sys from collections import deque input = sys.stdin.buffer.readline n = int(input()) e = [[] for _ in range(n+1)] ls = list(map(int, input().split())) for i, u in enumerate(ls): e[u].append(i+2) pp = [0] + list(map(int, input().split())) q, order, par = deque([1]), [], [0]*(n+1) while q: u = q.popleft() order.append(u) for v in e[u]: if v == par[u]: continue par[v] = u q.append(v) leaf, cc = [0]*(n+1), 0 for u in reversed(order): lcc = 0 for v in e[u]: if v == par[u]: continue lcc += leaf[v] pp[u] += pp[v] lcc = max(1, lcc) leaf[u] = lcc cc = max(cc, (pp[u]+lcc-1)//lcc) print(cc)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<long long int> divisor; void getDivisors(long long int n) { for (long long int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) divisor.push_back(i); else { divisor.push_back(i); divisor.push_back(n / i); } } } } long long int power(long long int a, long long int n) { if (n == 0) return 1; else if (n == 1) return a; long long int temp = power(a, n / 2); if (n % 2 == 0) { return ((temp % 1000000007) * (temp % 1000000007)) % 1000000007; } else { return ((((temp % 1000000007) * (temp % 1000000007)) % 1000000007) * a) % 1000000007; } } int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; vector<vector<long long int>> a(2, vector<long long int>(n + 50)); for (int i = 0; i < 2; i = i + 1) for (int j = 0; j < n; j = j + 1) cin >> a[i][j]; long long int left = 0, right = 0; for (int i = 1; i < n; i = i + 1) right += a[0][i]; long long int sum = 0; for (int i = 0; i < n - 1; i = i + 1) sum += a[1][i]; long long int ans = 1e18; for (int i = 0; i < n; i = i + 1) { long long int bob = max(left, right); long long int alice = sum; ans = min(ans, bob); left += a[1][i]; right -= a[0][i + 1]; sum -= a[1][i]; sum += a[0][i + 1]; } if (ans == 1e18) { cout << 0 << endl; } else cout << ans << endl; } }
9
CPP
#include <bits/stdc++.h> const long long mod = 1e9 + 7; using namespace std; int main() { int T; cin >> T; for (int t = 0; t < T; t++) { int n; cin >> n; vector<int> weights(51, 0); int temp; for (int i = 0; i < n; i++) { cin >> temp; weights[temp]++; } int maxPossible = 0; for (int i = 2; i <= 100; i++) { int result = 0; int low, high; low = 1; high = i - 1; while (low <= high) { if (low == high) { result += weights[low] / 2; } else if (high <= 50) { result += min(weights[low], weights[high]); } low++; high--; } maxPossible = max(result, maxPossible); } cout << maxPossible << endl; } return 0; }
9
CPP
import sys def check(t): maxx = max(ud_maxx, r_maxx + t, l_maxx - t) minx = min(ud_minx, r_minx + t, l_minx - t) maxy = max(lr_maxy, u_maxy + t, d_maxy - t) miny = min(lr_miny, u_miny + t, d_miny - t) return (maxx - minx) * (maxy - miny) n = int(input()) lr_maxy, lr_miny, ud_maxx, ud_minx = -1e9, 1e9, -1e9, 1e9 l_maxx, l_minx, r_maxx, r_minx = -1e9, 1e9, -1e9, 1e9 u_maxy, u_miny, d_maxy, d_miny = -1e9, 1e9, -1e9, 1e9 for line in sys.stdin: x, y, d = line.rstrip().split() x, y = int(x), int(y) if d == 'L': lr_maxy = max(lr_maxy, y) lr_miny = min(lr_miny, y) l_maxx = max(l_maxx, x) l_minx = min(l_minx, x) elif d == 'R': lr_maxy = max(lr_maxy, y) lr_miny = min(lr_miny, y) r_maxx = max(r_maxx, x) r_minx = min(r_minx, x) elif d == 'U': ud_maxx = max(ud_maxx, x) ud_minx = min(ud_minx, x) u_maxy = max(u_maxy, y) u_miny = min(u_miny, y) else: ud_maxx = max(ud_maxx, x) ud_minx = min(ud_minx, x) d_maxy = max(d_maxy, y) d_miny = min(d_miny, y) inflections = {0} inflections.add(max(0, l_maxx - ud_maxx)) inflections.add(max(0, (l_maxx - r_maxx) / 2)) inflections.add(max(0, ud_maxx - r_maxx)) inflections.add(max(0, l_minx - ud_minx)) inflections.add(max(0, (l_minx - r_minx) / 2)) inflections.add(max(0, ud_minx - r_minx)) inflections.add(max(0, d_maxy - lr_maxy)) inflections.add(max(0, (d_maxy - u_maxy) / 2)) inflections.add(max(0, lr_maxy - u_maxy)) inflections.add(max(0, d_miny - lr_miny)) inflections.add(max(0, (d_miny - u_miny) / 2)) inflections.add(max(0, lr_miny - u_miny)) print(min(map(check, inflections)))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int n; cin >> n; string s1, s2; cin >> s1 >> s2; map<long long int, long long int> m1, m2; for (long long int i = 0; i < n; i++) { long long int a = s1[i] - 'a'; m1[a] += 1; long long int b = s2[i] - 'a'; m2[b] += 1; } long long int f = 0; for (long long int i = 0; i < 26; i++) { if (m1[i] != m2[i]) { f = -1; break; } if (m1[i] > 1) { f = 1; } } if (f == 1) { cout << "YES" << endl; continue; } else if (f == -1) { cout << "NO" << endl; continue; } for (long long int i = 0; i < n; i++) { long long int a = s1[i] - 'a'; m1[a] = i + 1; long long int b = s2[i] - 'a'; m2[b] = i + 1; } long long int c = 0; for (long long int i = 0; i < n; i++) { long long int u = s1[i] - 'a'; if (m2[u] != i + 1) { long long int j = m2[u] - 1; swap(s2[i], s2[j]); c += 1; m2[s2[i] - 'a'] = i + 1; m2[s2[j] - 'a'] = j + 1; } } if (c % 2) { cout << "NO" << endl; } else { cout << "YES" << endl; } } }
12
CPP
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <climits> #include <iostream> #include <algorithm> #include <set> #include <map> #include <queue> #include <vector> #include <sstream> #include <typeinfo> #include <fstream> #define DIV 1000000007 using namespace std; long long N, Q; long long state[100005]; long long A[100005]; long long B[100005]; int main(){ cin >> N >> Q; for(int i = 0; i < Q; i++){ cin >> A[i] >> B[i]; A[i]--;B[i]--; } long long pos = 0; state[0] = 1; state[1] = 1; for(int i = 0; i < Q; i++){ if(pos == A[i]){ pos = B[i]; }else if(pos == B[i]){ pos = A[i]; } swap(state[A[i]], state[B[i]]); if(pos > 0){ state[pos-1] = 1; } if(pos < N-1){ state[pos+1] = 1; } } long long ans = 0; for(int i = 0; i < N; i++){ //cout << "i=" << i << " state " << state[i] << endl; if(state[i]){ ans++; } } cout << ans << endl; }
0
CPP
#include <bits/stdc++.h> const int MAXN = 100000; const int MAXB = 317; long long addTag[MAXB], sum[MAXB], val[MAXN]; bool isBigSet[MAXN]; int bigSetId[MAXN], bigSetIt, intersectionCnt[MAXN][MAXB]; std::vector<int> set[MAXN], parentOf[MAXN]; char op[10]; int main() { int N, M, Q; scanf("%d %d %d", &N, &M, &Q); for (int i = 0; i < N; ++i) scanf("%I64d", &val[i]); for (int i = 0, s; i < M; ++i) { scanf("%d", &s); if (s >= MAXB) isBigSet[i] = true, bigSetId[i] = bigSetIt++; for (int j = 0, p; j < s; ++j) { scanf("%d", &p); set[i].push_back(--p); if (isBigSet[i]) parentOf[p].push_back(bigSetIt - 1), sum[bigSetIt - 1] += val[p]; } } for (int i = 0; i < M; ++i) for (int j = 0; j < set[i].size(); ++j) for (int k = 0, u = set[i][j]; k < parentOf[u].size(); ++k) ++intersectionCnt[i][parentOf[u][k]]; while (Q--) { int x; scanf("%s %d", op, &x); --x; if (op[0] == '?') { long long ans = 0; if (isBigSet[x]) { ans += sum[bigSetId[x]]; for (int i = 0; i < bigSetIt; ++i) ans += intersectionCnt[x][i] * addTag[i]; } else { for (int i = 0; i < set[x].size(); ++i) ans += val[set[x][i]]; for (int i = 0; i < bigSetIt; ++i) ans += intersectionCnt[x][i] * addTag[i]; } printf("%I64d\n", ans); } else { long long add; scanf("%I64d", &add); if (isBigSet[x]) addTag[bigSetId[x]] += add; else { for (int i = 0; i < set[x].size(); ++i) val[set[x][i]] += add; for (int i = 0; i < bigSetIt; ++i) sum[i] += intersectionCnt[x][i] * add; } } } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const long long M = 1e9, I = 1e18; const int mxn = 1e5; int t, n; string s, pr, ans; bool fl; void solve() { fl = 0; ans = ""; cin >> n; cin >> pr; ans = pr; for (int i = 1; i < n - 2; i++) { cin >> s; if (pr[1] != s[0]) { fl = 1; ans += s[0]; } ans += s[1]; pr = s; } if (!fl) ans = "a" + ans; cout << ans << endl; } int main() { cin >> t; while (t--) solve(); }
8
CPP
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265359; signed main() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; a[i]--; } long long x = 1000000000, y = -1; for (long long i = 0; i < n - 1; i++) if (a[i] == a[i + 1]) { cout << "NO"; return 0; } for (long long i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) == 1) continue; if (y == -1) y = abs(a[i] - a[i + 1]); else if (y != abs(a[i] - a[i + 1])) { cout << "NO"; return 0; } } if (y == -1) y = 1000000000; for (long long i = 0; i < n - 1; i++) { if (abs(a[i] - a[i + 1]) == 1) { if (a[i] / y != a[i + 1] / y) { cout << "NO"; return 0; } } } cout << "YES\n"; cout << x << ' ' << y; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; cin >> t; while (t--) { long long int n; cin >> n; n; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; ; long long int sum = 0, temp = 0, ans = 0; for (long long int i = 0; i < n; i++) if (a[i] == 0) temp++; for (long long int i = 0; i < n; i++) sum += a[i]; ans = temp; sum += temp; if (sum == 0) ans++; cout << ans << endl; } return 0; }
7
CPP
#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); typedef long long ll; const int maxn=1e6+10; #define mod 1000000007 int main(){ fast; ll t,a,b; cin>>t; while(t--){ cin>>a>>b; if(abs(a-b)<=1){ cout<<a+b<<'\n'; continue; }else{ ll mn=min(a,b),mx=max(a,b); ll tt=mn; mn+=2; cout<<tt+mn+1+(mx-mn)*2<<'\n'; } } return 0; } // combnatorics(pigeonhole principle, stars and bars (n+k-1)C(k-1)) // comparator in inbuilt sorting // lcs // period of a string(prefix-function) with KMP // prime func // subset production // factorization // modpow // modinv ( a^(p-2)=a^(-1) (mod p) ) // sieve // ncr // euler totien func (co prime divisiors upto n) // matrix exponentation(fast fibonacci) // dsu // Trees // Graphs(dfs,bfs,shortes path etc.) // DP-Recursion
7
CPP
count = 0 for _ in range(int(input())): p, q = list(map(int, input().split())) if ((q - p) >= 2): count += 1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0) return 1; if (b == 1) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long cnt = 1; set<pair<long long, long long> > s; for (long long i = 0; i < n; i++) { long long x; cin >> x; for (long long j = 1; j <= x; j++) { s.insert({cnt, i + 1}); cnt++; } } long long m; cin >> m; for (long long i = 0; i < m; i++) { long long k; cin >> k; auto x = s.lower_bound({k, 0}); cout << (*x).second << "\n"; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, m, rb, cb, rd, cd; cin >> n >> m >> rb >> cb >> rd >> cd; int dx = 1, dy = 1; int count = 0; while (rb != rd && cb != cd) { count++; if ((rb == n && dy == 1) || (rb == 1 && dy == -1)) dy *= -1; if ((cb == m && dx == 1) || (cb == 1 && dx == -1)) dx *= -1; rb += dy; cb += dx; } cout << count << endl; } return 0; }
7
CPP
s = input() a = list(map(int, input().split())) s1 = sum(a[::3]) s2 = sum(a[1::3]) s3 = sum(a[2::3]) if s3 > s1 and s3 > s2: print('back') elif s2 > s1 and s2 > s3: print('biceps') elif s1 > s3 and s1 > s2: print('chest')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n; cin >> n; for (int i = 1; i <= n; i++) { cout << i << " "; } cout << endl; } }
7
CPP
#include <bits/stdc++.h> namespace Thao_Nguyen_dang_yeu { const long long ThaoNguyen = 3e5 + 5, inf = 1e10, thaonguyenscuteness = inf; } using namespace std; using namespace Thao_Nguyen_dang_yeu; long long a[ThaoNguyen], b[ThaoNguyen], n, k, ua[ThaoNguyen], ub[ThaoNguyen]; bool ea[ThaoNguyen], eb[ThaoNguyen]; signed main() { ea[0] = true, eb[0] = true; ua[0] = 0, ub[0] = 0; ios::sync_with_stdio(false); cin >> n >> k; for (long long i = 1; i <= n; i++) { cin >> a[i]; } for (long long i = 1; i <= n; i++) { cin >> b[i]; } for (long long i = 1; i <= n; i++) { if (b[i] * k >= a[i] + ua[i - 1]) { ; } else { ua[i] = a[i] + ua[i - 1] - b[i] * k; if (ua[i] > k) { cout << "NO"; return 0; } } if (a[i] * k >= b[i] + ub[i - 1]) { ; } else { ub[i] = b[i] + ub[i - 1] - a[i] * k; if (ub[i] > k) { cout << "NO"; return 0; } } } cout << "YES"; }
12
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int tc; cin >> tc; while (tc--) { long long int a, b, c, ans; cin >> a >> b >> c; long long int kop = min(b, c / 2); ans = kop * 3; b -= kop; b /= 2; long long int joss = min(a, b); cout << ans + (joss * 3) << endl; } }
7
CPP
import sys def is_prime(n): for i in range(2, int(n ** 0.5) + 2): if n % i == 0 and i != n: return False return True n, m = map(int, input().split()) for i in range(n + 1, m - 1): if is_prime(i): print("NO") sys.exit() if is_prime(m): print("YES") else: print("NO")
7
PYTHON3
n=int(input()) s=input() ar=[-1]*n b=True for i in range(n-1): if(ar[i]==-1): ar[i]=0 for j in range(i+1,n): if(s[i]>s[j]): if(ar[j]==-1 and ar[i]==0): ar[j]=1 elif(ar[j]==-1 and ar[i]==1): ar[j]=0 elif(ar[j]!=-1): if(ar[j]==ar[i]): b=False break if(b==False): break if(ar[n-1]==-1): ar[n-1]=0 if(b==False): print("NO") else: print("YES") for j in ar: print(j,end="") print()
11
PYTHON3
#include<bits/stdc++.h> using namespace std; int main(void){ int n; int m; string s; int i; int cnt; vector<int>v; int flg; int sum; int x; while(1){ cin>>n; if(n==0) break; cin>>m; cin>>s; v.clear(); i=0; cnt=0; while(s.size()-1>i){ flg=0; if(s[i]=='I'){ if(s[i+1]=='O'){ if(s[i+2]=='I'){ cnt++; flg=1; i+=2; } } } if(flg==0){ if(cnt!=0){ v.push_back(cnt); } cnt=0; i++; } } /* for(i=0;i<v.size();i++){ cout<<v[i]<<endl; } */ sum=0; for(i=0;i<v.size();i++){ if(v[i]+1-n>=0){ sum+=v[i]+1-n; } } cout<<sum<<endl; } return 0; }
0
CPP