solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
s = input() string1 = "" string2 = "" flag = 0 n = len(s) for i in range(n): string1+=s[i] if(s[i]!="a"): string2+=s[i] if(string1+string2)==s: flag = 1 break if(flag==0): print(":(") else: print(string1)
8
PYTHON3
inp = input().split(" ") nam = [] nam.append(inp[0]) nam.append(inp[1]) final = [] final.append(nam) n = int(input()) for i in range(n): name = final[-1].copy() k = input().split(" ") if name[0] == k[0]: name[0] = k[1] else: name[1] = k[1] final.append(name) for i in final: print(*i)
7
PYTHON3
k, n, w = map(int, input().split()) dollars = int(k * w * (w + 1) / 2 - n) print(dollars if dollars > 0 else 0)
7
PYTHON3
a = int(input()) b = int(input()) c = int(input()) def operation1(a,b,c): return a*b+c def operation2(a,b,c): return a*(b+c) def operation3(a,b,c): return a+(b*c) def operation4(a,b,c): return (a+b)*c def operation5(a,b,c): return a*b*c def operation6(a,b,c): return a+b+c possibilities = [operation1(a,b,c), operation2(a,b,c), operation3(a,b,c), operation4(a,b,c), operation5(a,b,c), operation6(a,b,c)] print(max(possibilities))
7
PYTHON3
n=int(input()) x=input() xx=x.split( ) for i in range(0,n): xx[i]=int(xx[i]) if n==16: a=[xx] b=[] for i in range(0,8): b.append(xx[i]) a.append(b) b=[] for i in range(8,16): b.append(xx[i]) a.append(b) b=[] for i in range(0,4): b.append(xx[i]) a.append(b) b=[] for i in range(4,8): b.append(xx[i]) a.append(b) b=[] for i in range(8,12): b.append(xx[i]) a.append(b) b=[] for i in range(12,16): b.append(xx[i]) a.append(b) p=0 for i in a: b=i[:] i.sort() if b==i: p=len(i) break if p==0: for i in range(0,n-1,2): if xx[i+1]>=xx[i]: p=2 break if p==0: p=1 print(p) elif n==8: a=[xx] b=[] for i in range(0,4): b.append(xx[i]) a.append(b) b=[] for i in range(4,8): b.append(xx[i]) a.append(b) p=0 for i in a: b=i[:] i.sort() if b==i: p=len(i) break if p==0: for i in range(0,n-1,2): if xx[i+1]>=xx[i]: p=2 break if p==0: p=1 print(p) elif n==4: a=[xx] b=[] for i in range(0,2): b.append(xx[i]) a.append(b) b=[] for i in range(2,4): b.append(xx[i]) a.append(b) p=0 for i in a: b=i[:] i.sort() if b==i: p=len(i) break if p==0: print(1) else: print(p) elif n==2: if xx[1]>=xx[0]: print(2) else: print(1) else: print(1)
7
PYTHON3
#include<iostream> #include<stdio.h> #include<string.h> #include<ctype.h> #include<cmath> #include<iomanip> using namespace std; int main(void){ int d,b,t,a[200][200],c,q[200][200],hokan,ue,mae,m; cin>>m; for(c=0;c<m;c++){ cin>>a[1][c]>>a[2][c]>>a[3][c]>>a[4][c]>>a[5][c]>>a[6][c];} for(c=0;c<m;c++){ for(d=0;d<c;d++){ for(int o=0;o<4;o++){ for(t=0;t<4;t++){ hokan=a[2][c]; a[2][c]=a[3][c]; a[3][c]=a[5][c]; a[5][c]=a[4][c]; a[4][c]=hokan; if(a[1][c]==a[1][d]&&a[2][c]==a[2][d]&&a[3][c]==a[3][d]&&a[4][c]==a[4][d]&&a[5][c]==a[5][d]&&a[6][c]==a[6][d]){ cout<<"No"<<endl; return 0; } } hokan=a[1][c]; a[1][c]=a[2][c]; a[2][c]=a[6][c]; a[6][c]=a[5][c]; a[5][c]=hokan; } hokan=a[1][c]; a[1][c]=a[3][c]; a[3][c]=a[6][c]; a[6][c]=a[4][c]; a[4][c]=hokan; for(b=0;b<4;b++){ hokan=a[2][c]; a[2][c]=a[3][c]; a[3][c]=a[5][c]; a[5][c]=a[4][c]; a[4][c]=hokan;if(a[1][c]==a[1][d]&&a[2][c]==a[2][d]&&a[3][c]==a[3][d]&&a[4][c]==a[4][d]&&a[5][c]==a[5][d]&&a[6][c]==a[6][d]){ cout<<"No"<<endl; return 0; } } hokan=a[1][c]; a[1][c]=a[3][c]; a[3][c]=a[6][c]; a[6][c]=a[4][c]; a[4][c]=hokan; hokan=a[1][c]; a[1][c]=a[3][c]; a[3][c]=a[6][c]; a[6][c]=a[4][c]; a[4][c]=hokan; for(b=0;b<4;b++){ hokan=a[2][c]; a[2][c]=a[3][c]; a[3][c]=a[5][c]; a[5][c]=a[4][c]; a[4][c]=hokan; if(a[1][c]==a[1][d]&&a[2][c]==a[2][d]&&a[3][c]==a[3][d]&&a[4][c]==a[4][d]&&a[5][c]==a[5][d]&&a[6][c]==a[6][d]){ cout<<"No"<<endl; return 0; } }}} cout<<"Yes"<<endl; return 0; }
0
CPP
import sys input=sys.stdin.buffer.readline t=int(input()) for _ in range(t): n,k=[int(x) for x in input().split()] arr=[int(x) for x in input().split()] #requiredTopUps=[0 for _ in range(n)] requiredTopUps=dict() #requiredTopUp:count for i in range(n): remainder=arr[i]%k if remainder!=0: if k-remainder not in requiredTopUps.keys(): requiredTopUps[k-remainder]=0 requiredTopUps[k-remainder]+=1 #If requiredTopUp is t, x needs to be t + a*k where a is a non-negative integer. #For each t, we need requiredTopUps[t] distinct values of a to top up #all of them. # A particular x used to top up for a particular t can never be used #alternatively to top up another t. If it is possible, then t1+ak==t2+bk #meaning t1-t2=(b-a)k which is not possible as t1,t2<k so t1-t2<k. #So we find largest t+(requireTopUps[t]-1)*k which gives largest x has be #to top up a number. Then +1 because of the last top up move. finalx=0 for t in requiredTopUps.keys(): finalx=max(finalx,t+(requiredTopUps[t]-1)*k) if finalx==0: #no move required. no need extra last move print(finalx) else: print(finalx+1)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, t, tp, tu, td; int v[305][305], s[305][305][4]; int add(int x, int y) { if (x == y) return tp; if (x < y) return td; return tu; } int main() { cin >> n >> m >> t >> tp >> tu >> td; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> v[i][j]; } } for (int j = 1; j < m; ++j) { for (int i = 0; i < n; ++i) { s[i][j][0] = s[i][j - 1][0] + add(v[i][j], v[i][j - 1]); s[i][j][1] = s[i][j - 1][1] + add(v[i][j - 1], v[i][j]); } } for (int j = 0; j < m; ++j) { for (int i = 1; i < n; ++i) { s[i][j][2] = s[i - 1][j][2] + add(v[i][j], v[i - 1][j]); s[i][j][3] = s[i - 1][j][3] + add(v[i - 1][j], v[i][j]); } } int anst = INT_MAX, sum; int a0, a1, a2, a3; for (int x1 = 0; x1 < n - 2; ++x1) { for (int y1 = 0; y1 < m - 2; ++y1) { for (int x2 = x1 + 2; x2 < n; ++x2) { for (int y2 = y1 + 2; y2 < m; ++y2) { sum = s[x1][y2][0] - s[x1][y1][0] + s[x2][y2][1] - s[x2][y1][1] + s[x2][y2][2] - s[x1][y2][2] + s[x2][y1][3] - s[x1][y1][3]; if (sum == t) { printf("%d %d %d %d", x1 + 1, y1 + 1, x2 + 1, y2 + 1); return 0; } int temp = abs(sum - t); if (temp < anst) { anst = temp; a0 = x1 + 1; a1 = y1 + 1; a2 = x2 + 1; a3 = y2 + 1; } } } } } printf("%d %d %d %d", a0, a1, a2, a3); return 0; }
10
CPP
Str = input() b = Str.split(" ") n = int(b[0]) m = int(b[1]) a = int(b[2]) if n % a == 0: finalN = int(n / a) else: finalN = int(n / a) + 1 if m % a == 0: finalM = int(m / a) else: finalM = int(m / a) + 1 final = finalN * finalM print(final)
7
PYTHON3
t=int(input()) while t>0: n,k=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) a.sort() b.sort(reverse=True) ans=sum(a) i=0 while i<k and a[i]<b[i]: ans=ans-a[i]+b[i] i+=1 print(ans) t-=1
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int matriz[5][5]; int first[9]; vector<int> v; unordered_map<int, int> qt; inline bool valid(int val) { return qt.count(val) && qt[val] > 0; } void usa(int val) { qt[val]--; } void rm(int val) { qt[val]++; } void fim() { for (int(i) = (1); (i) < (5); (i)++) for (int(j) = (1); (j) < (5); (j)++) printf("%d%c", matriz[i][j], j == 4 ? '\n' : ' '); exit(0); } void solve(int id) { for (int(i) = (0); (i) < (16); (i)++) if ((!i || v[i] != v[i - 1]) && valid(v[i])) { first[id] = v[i]; usa(v[i]); if (id == 1) { matriz[2][4] = first[1]; solve(id + 1); } if (id == 2) { matriz[3][2] = first[2]; solve(id + 1); } if (id == 3) { matriz[3][3] = first[3]; matriz[2][1] = -first[1] + first[2] + first[3]; if (valid(matriz[2][1])) { usa(matriz[2][1]); solve(id + 1); rm(matriz[2][1]); } } if (id == 4) { matriz[3][4] = first[4]; matriz[1][1] = first[1] + first[4] - first[5]; matriz[1][2] = first[1] - first[2] + first[3] + first[4] - first[5] - first[6] + first[8]; matriz[1][3] = -first[1] + first[2] - first[3] - first[4] + first[5] + first[5] + first[6]; matriz[1][4] = -first[1] - first[4] + first[5] + first[6] + first[7]; matriz[2][2] = -first[1] - first[3] - first[4] + first[5] + first[5] + first[6] + first[7]; matriz[2][3] = first[1] - first[2] + first[4] - first[5] + first[8]; matriz[3][1] = -first[2] - first[3] - first[4] + first[5] + first[6] + first[7] + first[8]; if (valid(matriz[1][1]) && valid(matriz[1][2]) && valid(matriz[1][3]) && valid(matriz[1][4]) && valid(matriz[2][2]) && valid(matriz[2][3]) && valid(matriz[3][1])) { vector<int> aux; for (int j = 1; j <= 4; j++) for (int k = 1; k <= 4; k++) aux.push_back(matriz[j][k]); sort((aux).begin(), (aux).end()); if (aux == v) fim(); } } rm(v[i]); } } int ent[9]; int main() { int n, s = 0; scanf("%d", &n); v.resize(n * n); for (int(i) = (0); (i) < (n * n); (i)++) scanf("%d", &v[i]), s += v[i], qt[v[i]]++; sort((v).begin(), (v).end()); if (n == 4) { s /= 4; printf("%d\n", s); for (int(i) = (0); (i) < (16); (i)++) for (int(j) = (i + 1); (j) < (16); (j)++) for (int(k) = (j + 1); (k) < (16); (k)++) for (int(l) = (k + 1); (l) < (16); (l)++) { if (v[i] + v[j] + v[k] + v[l] != s) continue; first[5] = v[i], first[6] = v[j], first[7] = v[k], first[8] = v[l]; usa(v[i]), usa(v[j]), usa(v[k]), usa(v[l]); do { matriz[4][1] = first[5]; matriz[4][2] = first[6]; matriz[4][3] = first[7]; matriz[4][4] = first[8]; solve(1); swap(first[5], first[6]); matriz[4][1] = first[5]; matriz[4][2] = first[6]; matriz[4][3] = first[7]; matriz[4][4] = first[8]; solve(1); swap(first[5], first[6]); } while (next_permutation(first + 6, first + 9)); rm(v[i]), rm(v[j]), rm(v[k]), rm(v[l]); } } else { for (int(i) = (0); (i) < (n * n); (i)++) ent[i] = v[i]; sort(ent, ent + n * n); do { int mat[n][n]; for (int(i) = (0); (i) < (n * n); (i)++) { mat[i / n][i % n] = ent[i]; } long long sum = 0; for (int(i) = (0); (i) < (n); (i)++) sum += mat[i][0]; bool pode = true; for (int(i) = (0); (i) < (n); (i)++) { long long soma = 0; for (int(j) = (0); (j) < (n); (j)++) { soma += mat[i][j]; } if (sum != soma) pode = false; } for (int(j) = (0); (j) < (n); (j)++) { long long soma = 0; for (int(i) = (0); (i) < (n); (i)++) { soma += mat[i][j]; } if (sum != soma) pode = false; } long long soma = 0; for (int(i) = (0); (i) < (n); (i)++) soma += mat[i][i]; if (soma != sum) pode = false; soma = 0; for (int(i) = (0); (i) < (n); (i)++) soma += mat[n - i - 1][i]; if (soma != sum) pode = false; if (pode) { printf("%lld\n", soma); for (int(i) = (0); (i) < (n); (i)++) { for (int(j) = (0); (j) < (n); (j)++) printf("%d ", mat[i][j]); puts(""); } return 0; } } while (next_permutation(ent, ent + n * n)); } }
10
CPP
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) using namespace std; using ll = long long; using P = pair<int,int>; int main() { string s, t; cin >> s >> t; int n = sz(s), m = sz(t); vector<vector<int>> is(26); rep(i, n) is[s[i]-'a'].push_back(i); rep(i, n) is[s[i]-'a'].push_back(i+n); ll ans = 0; int p = 0; rep(i, m){ int c = t[i] - 'a'; if (sz(is[c]) == 0){ puts("-1"); return 0; } p = *lower_bound(is[c].begin(),is[c].end(), p)+1; if(p >= n){ p -= n; ans += n; } } ans += p; cout << ans << endl; return 0; }
0
CPP
N, K = map(int, input().split()) T = input() def findAns(N, T): for i in range(N-1): if T[i+1:] == T[:N-1-i]: return T[N-1-i:] return T ans = findAns(N, T) print(T + ans*(K-1))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m; struct Point { long long x, y; Point(const long long &x = 0, const long long &y = 0) : x(x), y(y) {} Point operator+(const Point &B) const { return Point(x + B.x, y + B.y); } Point operator-(const Point &B) const { return Point(x - B.x, y - B.y); } long long operator*(const Point &B) const { return x * B.y - y * B.x; } } P[100005]; int F[100005]; int Find(int u) { return !F[u] ? u : F[u] = Find(F[u]); } vector<pair<double, int> > G[100005]; int sz[100005], sx[100005], ty[100005]; void dfs(int u, int ff) { sz[u] = 1; for (int i = 0, v; i < G[u].size() - 1; i++) { if ((v = G[u][i].second) == ff) swap(G[u][i], G[u].back()); v = G[u][i].second; dfs(v, u); sz[u] += sz[v]; } } int main() { scanf("%d%d", &n, &m); for (int i = (1), LIM = (m); i <= LIM; i++) scanf("%d%d", &sx[i], &ty[i]); int rt = 0; for (int i = (1), LIM = (n); i <= LIM; i++) { scanf("%lld%lld", &P[i].x, &P[i].y); if (!rt || (P[i].x < P[rt].x)) rt = i; } for (int i = (1), LIM = (m); i <= LIM; i++) { int u = sx[i], v = ty[i]; if (Find(u) ^ Find(v)) { G[u].push_back(make_pair(atan2(P[v].y - P[u].y, P[v].x - P[u].x), v)), G[v].push_back(make_pair(atan2(P[u].y - P[v].y, P[u].x - P[v].x), u)); F[Find(u)] = Find(v); } } G[rt].push_back(make_pair(3.1415926535897932384626433832795, 0)); dfs(rt, 0); for (int i = (1), LIM = (n); i <= LIM; i++) { sort(G[i].begin(), G[i].end() - 1); for (int j = (0), LIM = (G[i].size() - 2); j <= LIM; j++) G[i][j].second = sz[G[i][j].second] + (j ? G[i][j - 1].second : 0); } int q; scanf("%d", &q); for (int S; q--;) { scanf("%d", &S); static int ar[100005]; long long area = 0; for (int i = (1), LIM = (S); i <= LIM; i++) scanf("%d", &ar[i]); for (int i = (1), LIM = (S); i <= LIM; i++) { int v = i % S + 1; area += P[ar[i]] * P[ar[v]]; } if (area < 0) reverse(ar + 1, ar + 1 + S); int ans = 0; for (int i = (1), LIM = (S); i <= LIM; i++) { int pr = (i + S - 2) % S + 1, sc = i % S + 1, u = ar[i]; double pa = atan2(P[ar[pr]].y - P[ar[i]].y, P[ar[pr]].x - P[ar[i]].x), sa = atan2(P[ar[sc]].y - P[ar[i]].y, P[ar[sc]].x - P[ar[i]].x); if (pa < sa) { if (pa + 1e-8 < G[u].back().first && G[u].back().first < sa - 1e-8) ans += sz[u]; int tl = lower_bound(G[u].begin(), G[u].end() - 1, make_pair(pa + 1e-8, 0x3f3f3f3f)) - G[u].begin() - 1, tr = lower_bound(G[u].begin(), G[u].end() - 1, make_pair(sa - 1e-8, 0)) - G[u].begin() - 1; if (tr >= 0) ans -= G[u][tr].second; if (tl >= 0) ans += G[u][tl].second; } else { if (pa + 1e-8 < G[u].back().first || G[u].back().first < sa - 1e-8) ans += sz[u]; swap(pa, sa); int tl = lower_bound(G[u].begin(), G[u].end() - 1, make_pair(pa - 1e-8, 0)) - G[u].begin() - 1, tr = lower_bound(G[u].begin(), G[u].end() - 1, make_pair(sa + 1e-8, 0x3f3f3f3f)) - G[u].begin() - 1; if (G[u].size() > 1) ans -= G[u][G[u].size() - 2].second; if (tr >= 0) ans += G[u][tr].second; if (tl >= 0) ans -= G[u][tl].second; } } printf("%d\n", ans); } }
11
CPP
#include <bits/stdc++.h> using namespace std; int solve() { long long n; cin >> n; vector<long long int> a, b; long long x; for (long long int i = 0; i < n; i++) { cin >> x; if (x > 0) a.push_back(x); else b.push_back(x); } long long sum1 = accumulate(a.begin(), a.end(), (long long)0); long long sum2 = abs(accumulate(b.begin(), b.end(), (long long)0)); if (sum1 > sum2) cout << "first"; else if (sum2 > sum1) cout << "second"; else { long long s = min((long long int)a.size(), (long long int)b.size()); for (long long int i = 0; i < s; i++) { if (a[i] > abs(b[i])) { cout << "first"; return 0; } else if (a[i] < abs(b[i])) { cout << "second"; return 0; } } if (x > 0) cout << "first"; else cout << "second"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) solve(); }
8
CPP
import bisect pre=[] dup=0 i=1 while(dup<1000000001): temp=(i*((3*i)+1))//2 pre.append(temp) dup=temp i+=1 t=int(input()) for _ in range(t): n=int(input()) cnt=0 while(True): if(n<2): break x = bisect.bisect_left(pre,n,0,len(pre)) if(pre[x]==n): cnt+=1 break else: x=x-1 n=n-pre[x] cnt+=1 print(cnt)
8
PYTHON3
s=input() s1=s[1:] s2=s[0].upper() string=s2+s1 print(string)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; int d=b/a; cout << min(c,d); }
0
CPP
#include <bits/stdc++.h> using namespace std; int n, A[109][109], vis[109], res; int main() { cin >> n; for (int i = 0; i < n; i++) for (int h = 0; h < n; h++) cin >> A[i][h], vis[i] = (A[i][h] == 1 or A[i][h] == 3 ? 1 : vis[i]); for (int i = 0; i < n; i++) if (!vis[i]) res++; cout << res << '\n'; for (int i = 0; i < n; i++) if (!vis[i]) cout << i + 1 << ' '; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; float arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; arr[i] = ceil(arr[i] / m); } int ans = 0; for (int i = 0; i < n; i++) { if (arr[i] >= arr[ans]) { ans = i; } } cout << ans + 1; return 0; }
7
CPP
def simple_cycle(n,a,b,c): maxi=0 if(a[1] == b[1]): s = 1 + c[1] maxi=s else: s = abs(b[1] - a[1]) + 1 + c[1] maxi=s for i in range(2,n): if(b[i] == a[i]): s=1+c[i] else: s = max(s - abs(b[i] - a[i]) + 1 + c[i] ,abs(b[i] - a[i]) + 1 + c[i]) if(maxi<s): maxi = s return maxi if(__name__=='__main__'): t = int(input()) for i in range(0,t): n=int(input()) c=list(map(int,input().split())) a=list(map(int,input().split())) b=list(map(int,input().split())) print(simple_cycle(n,a,b,c))
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 200 * 1000 + 5; int a[N]; vector<char> vec; deque<int> q; bool mark[N]; char c[55][55]; string s; int main() { int x, h, m, ans = 0; cin >> x >> h >> m; while (h % 10 != 7 && h / 10 != 7 && m % 10 != 7 && m / 10 != 7) { m -= x; if (m < 0) { h--; } m = (m + 60) % 60, h = (h + 24) % 24; ans++; } cout << ans; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int pnt[] = {25, 18, 15, 12, 10, 8, 6, 4, 2, 1}; map<string, vector<int> > g; vector<pair<vector<int>, string> > mas; int main() { int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { string s; cin >> s; if (g.count(s) == 0) g[s] = vector<int>(51, 0); g[s][i + 1]--; if (i < 10) g[s][0] -= pnt[i]; } } for (map<string, vector<int> >::iterator it = g.begin(); it != g.end(); it++) { mas.push_back(make_pair(it->second, it->first)); } sort(mas.begin(), mas.end()); cout << mas[0].second << endl; for (int i = 0; i < mas.size(); i++) { swap(mas[i].first[0], mas[i].first[1]); } sort(mas.begin(), mas.end()); cout << mas[0].second << endl; return 0; }
8
CPP
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int N, M, t, s, d, ans; vector< vector<int> > packets; while (cin >> N >> M, N) { vector<bool> infected(N, false); infected[0] = true; for (int i = 0; i < M; ++i) { cin >> t >> s >> d; packets.push_back({t, s-1, d-1}); } sort(packets.begin(), packets.end()); for (int i = 0; i < M; ++i) { s = packets[i][1]; d = packets[i][2]; if (infected[s]) infected[d] = true; } ans = 0; for (int i = 0; i < N; ++i) if (infected[i]) ++ans; cout << ans << endl; packets.erase(packets.begin(), packets.end()); } return 0; }
0
CPP
T = '#.' while True: H, W = [int(n) for n in input().split()] n, r = divmod(W, 2) if H==W==0: break for i in range(H): if i%2==0: print(T*n + T[0]*r) else: print(T[::-1]*n + T[-1]*r) print()
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, ans[1000005]; bool mark[1000005]; bool has(int a, int b) { return (a & (1 << b)); } bool submask(int a, int b) { for (int i = 0; i < 20; i++) { if (has(b, i) && !has(a, i)) return false; } return true; } int main() { while (scanf("%d", &n) != EOF) { for (int i = 0; i <= n; i++) ans[i] = i; int mask = 1; while (mask <= n) mask <<= 1; mask -= 1; memset(mark, false, sizeof(mark)); long long sum = 0; for (int i = n; i >= 0; i--) { if (mark[i]) continue; while ((mask ^ i) > n || mark[mask ^ i]) mask >>= 1; ans[i] = mask ^ i; ans[mask ^ i] = i; sum += mask * 2; mark[i] = true, mark[mask ^ i] = true; ; } printf("%I64d\n", sum); for (int i = 0; i <= n; i++) { if (i) printf(" "); printf("%d", ans[i]); } printf("\n"); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; vector <pair<int,pair<int,string> > > v; //replyTo, (depth, M) void put_dots(int n){ for(int i = 0; i < n; i++){ cout << "."; } } void output(int n){ put_dots(v[n].second.first); cout << v[n].second.second << endl; for(int i = n; i < v.size(); i++){ if(v[i].first == n) output(i); } } int main(){ int n, k; string M; pair<int, pair<int, string> > p; cin >> n; cin >> k >> M; p = make_pair(-1, make_pair(0,M)); v.push_back(p); for(int i = 1; i < n; i++){ int depth; cin >> k >> M; k--; depth = v[k].second.first + 1; p = make_pair(k, make_pair(depth, M)); v.push_back(p); } output(0); }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { char a, b; int flag = 0; scanf("%c %c", &a, &b); char res[100], ans[100]; for (int i = 0; i < 5; ++i) { cin >> res[i] >> ans[i]; if (res[i] == a) flag = 1; else if (ans[i] == b) flag = 1; } if (flag) printf("YES\n"); else printf("NO\n"); return 0; }
7
CPP
#include <bits/stdc++.h> #define int long long #define inf 200000000000000000 #define N 200050 using namespace std; int n, q, S, T, A[N], bit[N], ans; void upd(int l, int r, int x) { if(1 <= l && l <= n + 1) for(int i = l; i < N; i += (i&-i)) bit[i] += x; if(1 <= r + 1&& r + 1 <= n + 1) for(int i = r + 1; i < N; i += (i&-i)) bit[i] -= x; } int query(int x) { int aux = 0; if(x <= 0 || x > n + 1) return inf; for(int i = x; i > 0; i -= (i&-i)) aux += bit[i]; return aux; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>q>>S>>T; for(int i = 1; i <= n + 1; i++) cin>>A[i], upd(i, i, A[i]); for(int i = 1; i <= n; i++) ans += (A[i] <= A[i + 1] ? (- A[i + 1] + A[i])*S : (A[i] - A[i + 1])*T); for(int i = 1; i <= q; i++) { int li, ri, xi; cin>>li>>ri>>xi; li ++, ri ++; int Hl = query(li); int Hr = query(ri); int H_depois = query(ri + 1); int H_antes = query(li - 1); upd(li, ri, xi); int dl = 0, dr = 0; if(H_antes != inf && H_antes <= Hl) dl -= abs(Hl - H_antes)*S ; else if(H_antes != inf) dl += abs(Hl - H_antes)*T; if(Hr <= H_depois && H_depois != inf) dr -= abs(H_depois - Hr)*S ; else if(H_depois != inf) dr += abs(H_depois - Hr)*T; ans = ans - dl - dr; Hl += xi, Hr += xi; dl = 0, dr = 0; if(H_antes <= Hl&& H_antes != inf) dl -= abs(Hl - H_antes)*S ; else if(H_antes != inf) dl += abs(Hl - H_antes)*T; if(Hr <= H_depois && H_depois != inf) dr -= abs(H_depois - Hr)*S ; else if(H_depois != inf) dr += abs(H_depois - Hr)*T; ans += dl + dr; cout<<ans<<"\n"; } }
0
CPP
import sys def main(): names = [] for i in range(26): s = "A" + chr(ord('a')+i) s1 = "X" + chr(ord('a')+i) names.append(s) names.append(s1) n,k = map(int,sys.stdin.readline().split()) r = list(sys.stdin.readline().split()) res = ["Ho"]*n cur = 0 firstYes = -1 for i in range(n-k+1): if r[i] == "YES": firstYes = i break if firstYes == -1: print(" ".join(res)) return for i in range(firstYes+1, firstYes+k): res[i] = names[cur] cur+=1 for i in range(firstYes+1, n-k+1): if r[i] == "YES": res[i+k-1] = names[cur] cur+=1 else: res[i+k-1] = res[i] print(" ".join(res)) main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<vector<int> > AdjList; vector<int> dfs_num, ts; const int UNVISITED = -1; const int VISITED = 1; int main() { int q; cin >> q; while (q--) { int l, r, d; cin >> l >> r >> d; if (d < l) { cout << d << endl; } else cout << d * (r / d + 1) << endl; } return 0; }
7
CPP
#include<bits/stdc++.h> #define kickStart ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define newLine '\n' #define ll long long int #define INF 1e18 using namespace std; int cnt[100005][26]; void solve() { int n,m; cin >> n >> m; string s,ans; memset(cnt,0,sizeof(cnt)); for(int i=1; i<=n; i++) { cin >> s; for(int j=0; j<m; j++) cnt[j][s[j]-'a']++; } for(int i=1; i<=n-1; i++) { cin >> s; for(int j=0; j<m; j++) cnt[j][s[j]-'a']--; } for(int i=0; i<m; i++) for(int j=0; j<26; j++) if(cnt[i][j]) ans+='a'+j; cout << ans << newLine; } int main() { kickStart; int t; cin >> t; while(t--) solve(); return 0; }
8
CPP
def reverse(s1): for i in range(0,len(s1)//2,1): temp = s1[i] s1[i] = s1[len(s1)-i-1] s1[len(s1)-i-1] = temp return s1 s1 = list(input()) s2 = list(input()) if(reverse(s1)==s2): print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int a[200005], pre[200005], suc[200005], last[200005], vis[200005]; long long ans, sum[200005 << 2], flag[200005 << 2], mn[200005 << 2]; void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; mn[rt] = mn[rt << 1 | 1]; } void build(int l, int r, int rt) { if (l == r) { sum[rt] = mn[rt] = suc[l + 1]; return; } int mid = (l + r) >> 1; build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1); pushup(rt); } void work(int rt, long long v, int l, int r) { sum[rt] = v * (r - l + 1); flag[rt] = mn[rt] = v; } void pushdown(int rt, int l, int r) { if (flag[rt]) { int mid = (l + r) >> 1; work(rt << 1, flag[rt], l, mid); work(rt << 1 | 1, flag[rt], mid + 1, r); flag[rt] = 0; } } void add(int l, int r, int rt, int L, int R, long long v) { if (R < L || L < l || R > r) return; if (L == l && R == r) { work(rt, v, l, r); return; } pushdown(rt, l, r); int mid = (l + r) >> 1; if (R <= mid) add(l, mid, rt << 1, L, R, v); else if (L > mid) add(mid + 1, r, rt << 1 | 1, L, R, v); else add(l, mid, rt << 1, L, mid, v), add(mid + 1, r, rt << 1 | 1, mid + 1, R, v); pushup(rt); } long long query(int l, int r, int rt, int L, int R) { if (R < L) return 0; if (L == l && R == r) return sum[rt]; pushdown(rt, l, r); int mid = (l + r) >> 1; if (R <= mid) return query(l, mid, rt << 1, L, R); else if (L > mid) return query(mid + 1, r, rt << 1 | 1, L, R); else return query(l, mid, rt << 1, L, mid) + query(mid + 1, r, rt << 1 | 1, mid + 1, R); } int get_pos(int l, int r, int rt, int v) { if (l == r) return 1 + (sum[rt] >= v); int mid = (l + r) >> 1; pushdown(rt, l, r); if (mn[rt << 1] >= v) return mid - l + 1 + get_pos(mid + 1, r, rt << 1 | 1, v); else return get_pos(l, mid, rt << 1, v); } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) { int tmp = 0; for (int j = 1; j * j <= a[i]; j++) if (a[i] % j == 0) { if (vis[j]) tmp = max(tmp, j); if (vis[a[i] / j]) tmp = max(tmp, a[i] / j); vis[j] = vis[a[i] / j] = 1; last[j] = last[a[i] / j] = i; } pre[i] = max(pre[i - 1], tmp); if (i < n) ans += pre[i]; } memset(vis, 0, sizeof(vis)); for (int i = n; i; i--) { int tmp = 0; for (int j = 1; j * j <= a[i]; j++) if (a[i] % j == 0) { if (vis[j]) tmp = max(tmp, j); if (vis[a[i] / j]) tmp = max(tmp, a[i] / j); vis[j] = vis[a[i] / j] = 1; } suc[i] = max(suc[i + 1], tmp); if (i > 1) ans += suc[i]; } build(1, n, 1); for (int i = 2; i < n; i++) { add(1, n, 1, max(i, get_pos(1, n, 1, pre[i - 1])), n, pre[i - 1]); for (int j = 1; j * j <= a[i - 1]; j++) if (a[i - 1] % j == 0) { add(1, n, 1, max(i, get_pos(1, n, 1, j)), last[j] - 1, j); add(1, n, 1, max(i, get_pos(1, n, 1, a[i - 1] / j)), last[a[i - 1] / j] - 1, a[i - 1] / j); } long long tmp = query(1, n, 1, i, n - 1); ans += tmp; } printf("%I64d\n", ans); }
9
CPP
from math import sqrt a, b = map(int, input().split()) if a > b: a, b = b, a u = [] sqa = int(sqrt(a)) sqb = int(sqrt(b)) for i in range(1, sqb + 1): if i <= sqa and a % i == 0: u.append((i, a // i)) elif b % i == 0: u.append((i, b // i)) ok = False sqab = int(sqrt(a + b)) for i in range(sqab, 0, -1): if (a + b) % i == 0: k = (a + b) // i for j in u: if i >= j[0] and k >= j[1]: ok = True break if ok: break print(2 * (i + k))
12
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> void read(T& x) { T res = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { res = (res << 3) + (res << 1) + c - '0'; c = getchar(); } x = res * f; } const long long N = 200000 + 10; long long t, x, n, a[N]; int main() { read(t); while (t--) { read(n), read(x); int ji = 0, ou = 0; for (int i = 1; i <= n; i++) { read(a[i]); if (a[i] & 1) ji++; else ou++; } if (x & 1) { if (ji && ((ji - 1) / 2 * 2 + ou / 2 * 2 >= x - 1)) printf("Yes\n"); else printf("No\n"); } else { ou--; if (ou < 0) { printf("No\n"); continue; } x--; if (ji && ((ji - 1) / 2 * 2 + ou / 2 * 2 >= x - 1)) printf("Yes\n"); else printf("No\n"); } } return 0; }
7
CPP
import sys n = int(sys.stdin.readline()) arr = list(sys.stdin.readline().rstrip()) for i in range(n - 1): if arr[i] != arr[i + 1]: print('YES') print(''.join(arr[i : i + 2])) sys.exit(0) print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(V) V.begin(),V.end() const ll MOD = 1000000007; map<ll, vector<pair<ll,ll>>> M; int main() { ll N, X, D, A, B, S = 0, T = 0, AM; cin >> N >> X >> D; if (D < 0) { X += (N - 1) * D; D = -D; } if (D == 0) { if (X == 0)cout << 1 << endl; else cout << N + 1 << endl; return 0; } D *= 2; for (ll i = 0;i <= N;i++) { A = X * 2 * i; AM = (A % D + D) % D; S += i;T = S - i; if (M.count(AM) == 0) { M.insert({ AM, {} }); } M[AM].push_back({ -(A - AM) / D - T,-(A - AM) / D - (i * N - S) }); } ll sp; ll ans = 0, nr, fr; for (auto x : M) { sp = x.second.size(); sort(all(x.second)); reverse(all(x.second)); tie(nr, fr) = x.second[0]; for (ll i = 1;i < sp;i++) { auto p = x.second[i]; if (fr < p.second)continue; if (p.first < fr) { ans += nr - fr + 1; nr = p.first; fr = p.second; } else { fr = p.second; } } ans += nr - fr + 1; } cout << ans << endl; }
0
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; const long long MAX = 2e3 + 5; const long long MAX2 = 11; const int MOD = 1000000000 + 7; const long long INF = 20000; const int nr[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int nc[] = {0, 1, 0, -1, 1, -1, 1, -1}; long long n, a, b, c, x[MAX][MAX], y[2 * MAX], z[2 * MAX], id1[MAX][MAX], id2[MAX][MAX], tmp, cnt, sz, p, q; string s, t; pair<int, int> a1, a2; int main() { cout << fixed << setprecision(10); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> x[i][j]; c = 2 * n - 1; a = n, b = 1; for (int i = 1; i <= c; i++) { p = 0; while (a + p <= n && b + p <= n) { id1[a + p][b + p] = i; y[i] += x[a + p][b + p]; p++; } if (a == 1) b++; else a--; } a = 1, b = 1; for (int i = 1; i <= c; i++) { p = 0; while (a - p > 0 && b + p <= n) { id2[a - p][b + p] = i; z[i] += x[a - p][b + p]; p++; } if (a == n) b++; else a++; } a = b = 0; a2 = {1, 1}, a1 = {1, 2}; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { tmp = y[id1[i][j]] + z[id2[i][j]] - x[i][j]; if ((i + j) & 1) { if (a < tmp) a = tmp, a1 = {i, j}; } else { if (b < tmp) b = tmp, a2 = {i, j}; } } cout << a + b << "\n"; cout << a2.first << " " << a2.second << " " << a1.first << " " << a1.second << "\n"; return 0; }
9
CPP
#include <bits/stdc++.h> char str[105]; using namespace std; int main() { while (~scanf("%s", str)) { int len, i = 1, h = 1, flag = 0; len = strlen(str); printf("%c", str[0]); while (h < len) { if (str[h] == 'd' && str[h + 1] == 'o' && str[h + 2] == 't' && ((h + 2) < (len - 1))) { printf("."); h += 3; } else if (str[h] == 'a' && str[h + 1] == 't' && ((h + 1) < (len - 1))) { if (!flag) { flag = 1; printf("@"); h += 2; } else { printf("%c", str[h]); ++h; } } else { printf("%c", str[h]); ++h; } } puts(""); } return 0; }
9
CPP
#include <bits/stdc++.h> int main() { int i, counta = 0, countu = 0, countl = 0, countb = 0, countB = 0, countr = 0, length = 0, counts = 0, count = 0; char str[100000]; gets(str); for (i = 0; str[i] != NULL; i++) { length++; } for (int j = 0; j < length; j++) { if (str[j] == 'B') { countB++; } else if (str[j] == 'a') { counta++; } else if (str[j] == 'u') { countu++; } else if (str[j] == 'l') { countl++; } else if (str[j] == 'r') { countr++; } else if (str[j] == 's') { counts++; } else if (str[j] == 'b') { countb++; } } for (int j = 1;; j++) { if (countB >= 1 && counta >= 2 && countu >= 2 && countl >= 1 && counts >= 1 && countr >= 1 && countb >= 1) { count++; countB--; countu = countu - 2; countl--; counta = counta - 2; counts--; countr--; countb--; } else { break; } } printf("%d", count); }
7
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long double PI = 4 * atan((long double)1); long long power(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res *= a; a *= a; n /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; cin >> n; long long int a[n]; for (int i = (0); i < (n); ++i) cin >> a[i]; long long int dp[n][2]; for (int i = (0); i < (n); ++i) dp[i][1] = INT_MIN; dp[n - 1][0] = 1; for (int i = n - 2; i >= 0; i--) { if (a[i] < a[i + 1]) { dp[i][0] = dp[i + 1][0] + 1; if (dp[i + 1][1] != INT_MIN) dp[i][1] = dp[i + 1][1] + 1; } else { if (i != n - 2 && i != 0 && a[i + 2] - a[i] > 1) { dp[i][0] = 1; dp[i][1] = dp[i + 1][0] + 1; } else if (i == 0) { dp[i][0] = 1; dp[i][1] = dp[i + 1][0] + 1; } else if (i == n - 2) { dp[i][0] = 1; dp[i][1] = dp[i + 1][0] + 1; } else { if (a[i + 1] - a[i - 1] > 1) { dp[i][0] = 1; dp[i - 1][0] = 2; dp[i][1] = dp[i + 1][0] + 1; dp[i - 1][1] = dp[i][1] + 1; i--; continue; } dp[i][0] = 1; dp[i][1] = dp[i][0] + 1; } } } long long int max = INT_MIN, k; for (int i = (0); i < (n); ++i) { for (int j = (0); j < (2); ++j) { if (dp[i][j] > max) { max = dp[i][j]; k = j; } if (k == 1 && dp[i][j] == max) { k = j; } } } if (k == 1 || max == n) cout << max; else cout << max + 1; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const long long int INF = (1LL << 60) - 1; long long int lf[200000], rt[200000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; string s; cin >> s; lf[0] = 1, rt[n - 1] = 1; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1]) lf[i] = lf[i - 1] + 1; else lf[i] = 1; } for (int i = n - 2; i >= 0; i--) { if (s[i] == s[i + 1]) rt[i] = rt[i + 1] + 1; else rt[i] = 1; } for (int i = 0; i < n; i++) { if (s[i] == 'a') { if (lf[i] == 1) cout << "a"; } else if (s[i] == 'i') { if (lf[i] == 1) cout << "i"; } else if (s[i] == 'u') { if (lf[i] == 1) cout << "u"; } else if (s[i] == 'y') { if (lf[i] == 1) cout << "y"; } else if (s[i] == 'o') { if (lf[i] == 1 || (lf[i] == 2 && rt[i] == 1)) cout << "o"; } else if (s[i] == 'e') { if (lf[i] == 1 || (lf[i] == 2 && rt[i] == 1)) cout << "e"; } else { cout << s[i]; } } }
17
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 20; long long a[N], b[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } long long l = -1, r = 2e10 + 10; while (r - l > 1) { long long mid = (l + r) / 2; int j = 0; for (int i = 0; i < n; i++) { if (j >= m) break; while (a[i] == b[j]) { j++; if (j >= m) break; } if (j >= m) break; if (b[j] > a[i]) { while (b[j] <= a[i] + mid) { j++; if (j >= m) break; } if (j >= m) break; continue; } if (mid < a[i] - b[j]) break; long long x = max(mid - 2 * (a[i] - b[j]), (mid - (a[i] - b[j])) / 2); if (x <= 0) x = 0; while (b[j] <= a[i] + x) { j++; if (j >= m) break; } if (j >= m) break; } if (j >= m) { r = mid; continue; } l = mid; } cout << r; }
11
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const long long int maxn = 1e3 + 1; void solve() { long long int n, m; cin >> n >> m; vector<pair<long long int, long long int>> jiro; for (long long int i = 0; i < n; i++) { string s; cin >> s; long long int x, y; if (s == "ATK") x = 1; else x = 0; cin >> y; jiro.push_back(make_pair(y, x)); } sort(jiro.begin(), jiro.end()); long long int ciel[m]; for (long long int i = 0; i < m; i++) { cin >> ciel[i]; } sort(ciel, ciel + m); long long int ans = 0; long long int k = m - 1; for (long long int i = 0; i < n; i++) { if (jiro[i].second == 1) { if (ciel[k] > jiro[i].first) { ans += (ciel[k] - jiro[i].first); k--; } else break; } if (k < 0) break; } if (m >= n) { long long int flag = 1; k = m - 1; for (long long int i = n - 1; i >= 0; i--) { if (jiro[i].second and jiro[i].first > ciel[k]) flag = 0; if (jiro[i].second == 0 and ciel[k] <= jiro[i].first) flag = 0; k--; } if (flag) { for (long long int i = 0; i < n; i++) { if (jiro[i].second == 0) { for (long long int j = 0; j < m; j++) { if (ciel[j] > jiro[i].first) { ciel[j] = 0; break; } } } } long long int cc = 0, bb = 0; for (long long int i = 0; i < n; i++) { if (jiro[i].second == 1) bb += jiro[i].first; } for (long long int i = 0; i < m; i++) { cc += ciel[i]; } ans = max(ans, cc - bb); } } cout << ans << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; t = 1; for (long long int i = 0; i < t; i++) { solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> #define int long long using namespace std; signed main(){ int N; cin>>N; map<char,int> dic; for(int i=0;i<N;i++){ string S; cin>>S; dic[S[0]]++; } int ans=0; ans+=dic['M']*dic['A']*(dic['R']+dic['C']+dic['H']); ans+=dic['M']*dic['R']*(dic['C']+dic['H']); ans+=dic['M']*dic['C']*dic['H']; ans+=dic['A']*dic['R']*(dic['C']+dic['H']); ans+=dic['C']*dic['H']*(dic['A']+dic['R']); cout<<ans<<endl; }
0
CPP
#include <iostream> #include <ctime> using namespace std; int main() { string weeks[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; struct tm t; t.tm_year = 2004 - 1900; t.tm_hour = 0; t.tm_min = 0; t.tm_sec = 0; t.tm_isdst = 0; for (int month, day; cin >> month >> day && (month || day);) { t.tm_mon = month - 1; t.tm_mday = day; mktime(&t); cout << weeks[t.tm_wday] << endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; long long sum = 0; vector<long long> len(m); for (int i = 0; i < m; i++) { cin >> len[i]; sum += len[i]; if (i + len[i] > n) return cout << -1 << endl, 0; } if (sum < n) return cout << -1 << endl, 0; for (int i = 0; i < m; i++) { cout << max((long long)(i + 1), n - sum + 1); if (i < m - 1) cout << ' '; sum -= len[i]; } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 20001; const int MAX_LOG = 20; using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pll = pair<ll, ll>; const double delta = 1e-6; const long long mod = 1e9 + 7; const int magic_const = 1e9; const ll INF = 2e18 + 2; const ll BIG_DT = 1e12; mt19937 gen(54323244234); inline ll modoper(ll x) { return ((x % mod) + mod) % mod; } ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); } inline ll get_rand(ll x) { return (gen() % x + x) % x; } template <typename T> inline void sort(vector<T>& x) { sort(x.begin(), x.end()); } template <typename T> inline void reverse(vector<T>& x) { reverse(x.begin(), x.end()); } template <typename T> inline void set_max(T& x, T y) { x = max(x, y); } template <typename T> inline void set_min(T& x, T y) { x = min(x, y); } ll bin_pow(ll x, ll pow) { if (pow == 0) return 1; if ((pow & 2) == 1) { return (bin_pow(x, pow - 1) * x) % mod; } else { auto tmp = bin_pow(x, pow / 2); return (tmp * tmp) % mod; } } struct fenwick_tree { vector<int> a; fenwick_tree(int n) : a(n + 2) {} void add(int pos, int delta) { for (pos++; pos < a.size(); pos += pos & -pos) a[pos] += delta; } int get_sum(int pos) { int sum = 0; for (pos++; pos > 0; pos -= pos & -pos) sum += a[pos]; return sum; } }; void solve() { int n, d; cin >> n >> d; vector<int> a(n + 1, 1); a[0] = 0; int sum_now = (n * (n - 1)) / 2 - d; for (int l = 2, r = a.size() - 1; l < r; r--) { while (l < r && (sum_now - r + l < 0 || a[l] == a[l - 1] * 2)) { l++; } if (l >= r) break; sum_now -= r; sum_now += l; a[l]++; a[r]--; } if (sum_now != 0) { cout << "NO\n"; return; } cout << "YES\n"; vector<int> pref_sums = a; for (int i = 1; i < pref_sums.size(); i++) { pref_sums[i] += pref_sums[i - 1]; } for (int i = 2; i < pref_sums.size(); i++) { for (int k = 0; k < a[i]; k++) { cout << pref_sums[i - 2] + 1 + k / 2 << " "; } } cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); srand(23321); int t = 1; cin >> t; for (int i = 0; i < t; i++) solve(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int vis[100000]; vector<pair<int, char>> G[100000]; vector<int> part[3]; bool dfs(int i, int p, char c) { if (vis[i] != 0) { return vis[i] == p; } vis[i] = p; part[p].push_back(i); for (auto x : G[i]) { if (dfs(x.first, x.second == c ? p : p ^ 3, c) == false) { return false; } } return true; } vector<int> solve(int n, char c) { vector<int> ans; memset(vis, 0, sizeof vis); for (int i = 0; i < n; i++) { if (vis[i] == 0) { part[1].clear(); part[2].clear(); if (dfs(i, 1, c) == false) { for (int j = 0; j < n + 1; j++) { ans.push_back(-1); } return ans; } if (part[1].size() > part[2].size()) part[1] = part[2]; ans.insert(ans.end(), part[1].begin(), part[1].end()); } } return ans; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int x, y; char c; scanf("%d %d %c", &x, &y, &c); x--; y--; G[x].emplace_back(y, c); G[y].emplace_back(x, c); } auto f = solve(n, 'R'); auto g = solve(n, 'B'); if (g.size() < f.size()) { f = g; } if (f.size() > n) { printf("-1\n"); return 0; } printf("%ld\n", f.size()); for (auto x : f) { printf("%d ", x + 1); } printf("\n"); return 0; }
8
CPP
#include<bits/stdc++.h> /* #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including */ using namespace std; //using namespace __gnu_pbds; //typedefs typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int,int> pii; typedef pair<double, double> pdd; typedef pair<ll, ll> pll; typedef vector<pii> vii; typedef vector<pll> vll; typedef vector<int>::iterator vit; typedef set<int>::iterator sit; /* template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<typename F, typename S> using ordered_map = tree<F, S, less<F>, rb_tree_tag, tree_order_statistics_node_update>; */ //#Defines #define rep(i,a,b) for(i=a;i<=b;i++) #define repR(i,a,b) for(i=a;i>=b;i--) //#define pb push_back #define pb emplace_back #define F first #define S second #define mp make_pair #define all(c) c.begin(),c.end() #define endl '\n' #define pf printf #define sf scanf //#define left __left //#define right __right //#define tree __tree #define MOD 1000000007 //#define harmonic(n) 0.57721566490153286l+log(n) #define RESET(a,b) memset(a,b,sizeof(a)) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define sqr(a) ((a) * (a)) #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield); const double PI = acos(-1); const double eps = 1e-9; const int inf = 2000000000; const ll infLL = 9000000000000000000; //Bit Operations inline bool checkBit(ll n, int i) { return n&(1LL<<i); } inline ll setBit(ll n, int i) { return n|(1LL<<i);; } inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); } int fx[] = {0, 0, +1, -1}; int fy[] = {+1, -1, 0, 0}; //int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1}; //int dy[] = {0, +1, 0, -1, +1, -1, +1, -1}; //Inline functions inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; } inline bool isLeapYear(ll year) { return (year%400==0) || (year%4==0 && year%100!=0); } inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); } inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; } inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; } inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } inline ll modInverse(ll a) { return modPow(a, MOD-2); } inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); } inline bool isInside(pii p,ll n,ll m) { return (p.first>=0&&p.first<n&&p.second>=0&&p.second<m); } inline bool isInside(pii p,ll n) { return (p.first>=0&&p.first<n&&p.second>=0&&p.second<n); } inline bool isSquare(ll x) { ll s = sqrt(x); return (s*s==x); } inline bool isFib(ll x) { return isSquare(5*x*x+4)|| isSquare(5*x*x-4); } inline bool isPowerOfTwo(ll x) { return ((1LL<<(ll)log2(x))==x); } struct func { //this is a sample overloading function for sorting stl bool operator()(pii const &a, pii const &b) { if(a.F==b.F) return (a.S<b.S); return (a.F<b.F); } }; //Prime Number Generator /* #define M 100000000 int marked[M/64 + 2]; #define on(x) (marked[x/64] & (1<<((x%64)/2))) #define mark(x) marked[x/64] |= (1<<((x%64)/2)) vl prime; bool isPrime(int num) { return num > 1 && (num == 2 || ((num & 1) && !on(num))); } void sieve(ll n) { for (ll i = 3; i * i < n; i += 2) { if (!on(i)) { for (ll j = i * i; j <= n; j += i + i) { mark(j); } } } prime.pb(2); for(ll i = 3; i <= n; i += 2) { if(!on(i)) prime.pb(i); } } */ // //debug #ifdef tajir template < typename F, typename S > ostream& operator << ( ostream& os, const pair< F, S > & p ) { return os << "(" << p.first << ", " << p.second << ")"; } template < typename T > ostream &operator << ( ostream & os, const vector< T > &v ) { os << "{"; for(auto it = v.begin(); it != v.end(); ++it) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; } template < typename T > ostream &operator << ( ostream & os, const set< T > &v ) { os << "["; for(auto it = v.begin(); it != v.end(); ++it) { if( it != v.begin() ) os << ", "; os << *it; } return os << "]"; } template < typename T > ostream &operator << ( ostream & os, const multiset< T > &v ) { os << "["; for(auto it = v.begin(); it != v.end(); ++it) { if( it != v.begin() ) os << ", "; os << *it; } return os << "]"; } template < typename F, typename S > ostream &operator << ( ostream & os, const map< F, S > &v ) { os << "["; for(auto it = v.begin(); it != v.end(); ++it) { if( it != v.begin() ) os << ", "; os << it -> first << " = " << it -> second ; } return os << "]"; } #define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0) clock_t tStart = clock(); #define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC) void faltu () { cerr << endl; } template <typename T> void faltu( T a[], int n ) { for(int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl; } template <typename T, typename ... hello> void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); } #else #define dbg(args...) #endif // tajir int main() { #ifdef tajir freopen("input.txt", "r", stdin); #else // online submission #endif optimize(); int n,m; cin >> n; string s; cin >> s; cin >> m; string ss[10000]; for(int i = 0; i < m; ++i) cin >> ss[i]; int cntS[30] = {0}; for(int i = 0; i < n; ++i) cntS[s[i]-'a']++; vector < string > store; vi cntStore(30,0); for(int i = 0; i < m; ++i) { int rejected = false; vi cnt(30,0); for(int j = 0; j < ss[i].size(); ++j) { if(s[j] != '*' && s[j] != ss[i][j]) { rejected = true; break; } cnt[ss[i][j]-'a']++; if(cnt[ss[i][j]-'a'] > cntS[ss[i][j]-'a'] && cntS[ss[i][j]-'a'] > 0) { rejected = true; break; } } if(rejected) continue; dbg(ss[i]); store.pb(ss[i]); for(int i = 0; i < 26; ++i) { if(cnt[i]) cntStore[i]++; } } for(int i = 0; i < 26; ++i) { if(cntS[i]) cntStore[i] = 0; } dbg(cntStore); int res = 0; for(int i = 0; i < 26; ++i) { if(cntStore[i] == store.size()) res++; } cout << res << endl; return 0; } //?
11
CPP
n=input() cnt=n.count("4")+n.count("7") if len(str(cnt))==str(cnt).count("4")+str(cnt).count("7"): print("YES") else: print("NO")
7
PYTHON3
import sys from collections import deque N = int(input()) A = [deque([int(i) for i in input().split()]) for i in range(N)] ans=0 empty = [deque() for i in range(N)] pre_fight = set([i for i in range(N)]) record = set() while True: for g in pre_fight: if A[g]!=deque(): if g+1 == A[A[g][0]-1][0]: record.add(g) record.add(A[g][0]-1) if record==set(): break else: for i in record: A[i].popleft() ans+=1 pre_fight = record record = set() if A==empty: print(ans) sys.exit() print(-1)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; long long i, j, k, l, n, m; string s; long long a[200009]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int nt; cin >> nt; while (nt--) { cin >> n; long long summ = 0, cnt = 0; for (i = 1; i <= n; i++) cin >> a[i], summ += a[i]; long long sum = -((long long)1e18 + 9ll); l = 1; for (i = 1; i <= n; i++) { sum = max(sum, a[i]); cnt += a[i]; if (cnt < 0) cnt = 0, l = i + 1; if (cnt == 0) l = i + 1; if (!(l == 1 && i == n)) sum = max(sum, cnt); } if (summ <= sum) cout << "NO" << '\n'; else cout << "YES" << '\n'; } exit(0); }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, mo = 998244353, t[200005]; long long ans = 1; int main() { cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; t[u]++; t[v]++; ans = ans * t[u] % mo * t[v] % mo; } cout << ans * n % mo; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main(void) { int n; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) scanf("%d", a + i); int marked[100005] = {0}; int visited[100005] = {0}; for (int i = 0; i < n; i++) { if ((a[i] >= 1 && a[i] <= n) && !visited[a[i]]) { marked[i] = 1; visited[a[i]] = 1; } } int counter = 1; for (int i = 0; i < n; i++) { if (!marked[i]) { while (visited[counter]) counter++; a[i] = counter; visited[counter] = 1; } } for (int i = 0; i < n; i++) printf("%d ", a[i]); return 0; }
8
CPP
n = int(input()) a = list(map(int, input().split())) m1 = a[0]; m2 = a[0]; ans = 0 for i in range(1, n): if a[i] > m1: m1 = a[i] ans += 1 elif a[i] < m2: m2 = a[i] ans += 1 print(ans)
7
PYTHON3
import sys input = sys.stdin.readline S = list(input())[: -1] N = len(S) print(5) print("R", 2) N += N - 2 print("R", 2) N += N - 2 print("L", 2) N += 1 print("R", 2) N += N - 2 print("R", N - 1)
9
PYTHON3
n,k = map(int,input().split()) l=[int(x) for x in input().split()] m=[] ans = 0 for i in range (n) : if l[i] > n-k : index = i ans+=l[i] break value = 1 for i in range (i+1,n) : if l[i] > n-k : value = (value*(i-index))%998244353 index = i ans+=l[i] print(ans,value)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8; string s[100]; string read[100]; int len[100]; string words[100][100]; int n; int subs(int pos) { if (n > len[pos]) return 0; int j = 0; for (int i = (0); i < (int)(n); i++) { while (j < len[pos] && words[pos][j] != s[i]) j++; if (j == len[pos]) { return 0; } else { j++; } } return 1; } int perm[100]; int main() { cin >> n; for (int i = (0); i < (int)(n); i++) { cin >> read[i]; } int m; cin >> m; for (int i = (0); i < (int)(m); i++) { cin >> len[i]; for (int j = (0); j < (int)(len[i]); j++) { cin >> words[i][j]; } } int found = false; for (int i = (0); i < (int)(n); i++) { perm[i] = i + 1; } string aux; int ans = -1; int val = -1; for (int i = (0); i < (int)(m); i++) { do { for (int j = (0); j < (int)(n); j++) { s[j] = read[perm[j] - 1]; } if (subs(i)) { int p = 0; int x = 0; for (int j = (0); j < (int)(n); j++) { for (int k = j + 1; k < n; k++) { if (perm[k] < perm[j]) x++; } } p = n * (n - 1) / 2 - x + 1; if (p > val) { ans = i; val = p; } } } while (next_permutation(perm, perm + n)); } if (ans == -1) { cout << "Brand new problem!\n"; } else { cout << ans + 1 << endl; cout << "[:"; for (int i = (0); i < (int)(val); i++) { cout << "|"; } cout << ":]" << endl; } return 0; }
8
CPP
#include<bits/stdc++.h> using namespace std; using ll = long long; int main() { ll n; cin >> n; int ans = 0, past = 0; for(int i = 1; i <= n; i++) { if(n - i <= past) { ans = i; break; } past += i; } for(int i = ans; i >= 1; i--) { if(n - i >= 0) { cout << i << endl; n -= i; } } }
0
CPP
#include <bits/stdc++.h> using namespace std; int n, st = 1, dr = 1, l_cur, pas; char a[2005][2005]; int nod[2005], len[2005], kkt; inline void Unite(int x1, int y1, int x2, int y2) { int i, j; for (i = x1; i <= y1; ++i) for (j = x2; j <= y2; ++j) a[i][j] = a[j][i] = 'Y'; } inline void Solve(int x) { if (x == 1) { Unite(st, dr, 2, 2); ++l_cur; return; } int k = 0, i; while (x % 2 == 0) { Unite(st, dr, n + 1, n + 2); st = n + 1; dr = n + 2; ++l_cur; n += 2; x >>= 1; } if (x == 1) { Unite(st, dr, 2, 2); ++l_cur; return; } nod[++kkt] = ++n; len[kkt] = l_cur + 1; Unite(st, dr, n, n); Solve(x - 1); } int main() { int i, j, k; cin.sync_with_stdio(0); cin >> k; for (i = 1; i <= 1000; ++i) for (j = 1; j <= 1000; ++j) a[i][j] = 'N'; n = 2; Solve(k); for (i = 1; i <= kkt; ++i) { int aux = nod[i]; for (j = 1; j < l_cur - len[i]; ++j) { ++n; Unite(aux, aux, n, n); aux = n; } if (l_cur - len[i] > 1) Unite(n, n, 2, 2); else Unite(nod[i], nod[i], 2, 2); } cout << n << "\n"; for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) cout << a[i][j]; cout << "\n"; } return 0; }
8
CPP
s=str(input()) flag=0 for i in range(len(s)): if s[i]=="H" or s[i]=="Q" or s[i]=="9" : flag=1 if flag==1: print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int a1, a2, a3, a4, a5, a6; cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6; cout << ((a1 + a2 + a3) * (a1 + a2 + a3)) - (a1 * a1) - (a3 * a3) - (a5 * a5); }
9
CPP
import sys input = sys.stdin.readline from operator import itemgetter sys.setrecursionlimit(10000000) def dijkstra(n, start, nbl): """n: 頂点数, start: 始点, nbl: 隣接リスト""" import heapq WHITE = 1<<5 GRAY = 1<<10 BLACK = 1<<15 INF = 1<<20 color = [WHITE] * n distance = [INF] * n parent = [-1] * n q = [] distance[start] = 0 heapq.heappush(q, (0, start)) while len(q) != 0: du, u = heapq.heappop(q) # print("u: {}, du: {}".format(u, du)) color[u] = BLACK if distance[u] < du: continue for next, cost in nbl[u]: if color[next] != BLACK and distance[u] + cost < distance[next]: distance[next] = distance[u] + cost color[next] = GRAY heapq.heappush(q, (distance[next], next)) return distance def main(): n = int(input().strip()) nb = [[] for _ in range(n)] for i in range(n): l = list(map(int, input().strip().split())) for j in range(l[1]): nb[i].append((l[2 + 2 * j], l[3 + 2 * j])) d = dijkstra(n, 0, nb) for i, v in enumerate(d): print(i, v) if __name__ == '__main__': main()
0
PYTHON3
#include<iostream> #include<string> using namespace std; int main() { string w,t; int n=0; cin >> w; while(1){ cin >> t; if( t == "END_OF_TEXT" ) break; for( int i=0; i<t.size(); i++ ){ // size()??§????´???°??????????????? t[i] = tolower(t[i]); //?°?????????§?????? } if( w==t ) n++; } cout << n << "\n"; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; string s; char znak1; char znak2; long long int brojac1; long long int brojac2; int main() { long long int rijesenje = 0; cin >> s; int n = s.length(); for (int i = 0; i < s.length(); ++i) { if (s[i] != '@') continue; int j = i; brojac1 = 0; while (--j + 1) { if (j < 0) break; if (j == n) break; if (s[j] == '.' || s[j] == '@') break; if (s[j] >= 'a' && s[j] <= 'z') ++brojac1; } j = i; brojac2 = 0; while (++j) { if (j == n) { --j; break; } if (s[j] == '.') break; if (s[j] == '@' || s[j] == '_') break; } if (s[j] != '.') continue; int k = j; if (j - i < 2) continue; while (++k) { if (k == n) break; if (s[k] < 'a' || s[k] > 'z') break; ++brojac2; } rijesenje += brojac1 * brojac2; } cout << rijesenje; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, j, k, a[200005], l = 0, r = 0, left, right; vector<long long> v; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 0) v.push_back(i); } v.push_back(999999999); left = 9999999999; right = v[0]; for (i = 0; i < n; i++) { if (a[i] != 0) a[i] = min(abs(i - left), abs(i - right)); while (v[l] < i) { left = v[l]; l++; } while (v[r] < i) { r++; right = v[r]; } } for (i = 0; i < n; i++) cout << a[i] << " "; }
8
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll t; t = 1; while (t--) { ll n; cin >> n; vector<ll> a(n), b(n); for (ll i = 0; i < n; i++) cin >> a[i]; b = a; sort(b.begin(), b.end()); ll ind1 = 0, ind2 = 0; for (ll i = 0; i < n; i++) { if (a[i] != b[i]) { ind1 = i; break; } } ll flag = 1; for (ll i = n - 1; i >= 0; i--) { if (a[i] != b[i]) { ind2 = i; break; } } for (ll i = ind1; i < ind2; i++) { if (a[i] < a[i + 1]) flag = 0; } if (flag) { cout << "yes\n" << ind1 + 1 << " " << ind2 + 1; } else cout << "no\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, empty, q, temp; string s, part, res; bool f; cin >> n; for (int i = 0; i < n; i++) { cin >> s; if (s == "::") { cout << "0000:0000:0000:0000:0000:0000:0000:0000" << endl; continue; } part = ""; res = ""; empty = -1; q = 0; for (int j = 0; j < s.size(); j++) { part += s[j]; if (s[j] == ':' || j == s.size() - 1) { temp = j == s.size() - 1 ? 1 : 0; q++; if (part == ":") { empty = q - 1; if (!temp) { res += part; } part = ""; continue; } for (int k = part.size(); k <= 4 - temp; k++) { part = "0" + part; } res += part; part = ""; continue; } } if (empty != -1) { if (s[0] == ':') { empty = 0; q--; res.erase(0, 1); } part = ""; for (; q <= 8; q++) { part += "0000:"; } part.pop_back(); res = res.insert(empty * 5, part); } cout << res << endl; } }
8
CPP
#include <bits/stdc++.h> const int MAXN = 1000000007; const double EPS = 1e-8; const double pi = acos(-1); const int INF = 1e9 + 7; using namespace std; int a, b, t = 0; int main() { scanf("%d%d", &a, &b); while (a > 0 && b > 0) { if (a == 1 && b == 1) break; t++; if (a < b) { a++; b -= 2; } else { b++; a -= 2; } } printf("%d\n", t); return 0; }
7
CPP
import collections _n = input() n = int(_n) int_list = list() first = (0,0) int_list.append(first) # print(n) # print(int_list) graph = {} for x in range(n): q = input().split() # print("Query:{}", q) a = int(q[1]) b = int(q[2]) interval = (a, b) if q[0] == '1': # adding an interval to list and graph int_list.append(interval) val = list() for k in graph.keys(): it = graph[k] c = k[0] d = k[1] # 'interval' can reach 'k' if a > c and a < d: val.append( (c, d) ) elif b > c and b < d: val.append( (c, d) ) # 'k' can reach 'interval' if c > a and c < b: it.append(interval) elif d > a and d < b: it.append(interval) graph[k] = it graph[interval] = val # print("Graph: ", graph) #add (q[1], q[2]) to set elif q[0] == '2': # process query beg = a end = b # print("beg: {} end: {}".format(beg, end)) beg_int = int_list[beg] end_int = int_list[end] visited = set() que = collections.deque() que.append(beg_int) found = False while que: # print("Que: ", que) # print("Visited: ", visited) i = que.pop() # print("intervals adjacent to {}: {}".format(i,graph[i])) adj_list = graph[i] if i[0] == end_int[0] and i[1] == end_int[1]: found = True break visited.add(i) for a_int in adj_list: if a_int not in visited: que.append(a_int) if found: print('YES') else: print('NO')
11
PYTHON3
k=input() z=input() j=[] for i in range(len(k)): if k[i]==z[i]: j.append(0) else: j.append(1) print(*j, sep='')
7
PYTHON3
x=input() s='' i=0 while(i<len(x)): if i<len(x)-2: if x[i]=='W' and x[i+1]=='U' and x[i+2]=='B': if len(s)==0 or s[-1]==' ': s=s else: s+=' ' i+=3 else: s+=x[i] i+=1 else: s+=x[i] i+=1 z=0 for i in range(len(s)-1,-1,-1): if s[i]==' ': z+=1 else: break print(s[:len(s)-z])
7
PYTHON3
#include "bits/stdc++.h" using namespace std; using ll = long long; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n,m; cin >> n >> m; map<int, vector<int> >mp; for(int i = 0; i < m; i++){ int a,b; cin >> a >> b; a--; b--; mp[a].push_back(b); mp[b].push_back(a); } for(int i: mp[0]){ for(int j: mp[i]){ if(j == n-1){ cout << "POSSIBLE" << endl; return 0; } } } cout << "IMPOSSIBLE" << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; struct v { unsigned long long cnt; int t; } it[(1 << 23)]; unsigned long long k = 20; void fr(unsigned long long i, unsigned long long l, unsigned long long r) { if (l + 1 == r) { return; } if (it[i].t == 0) { return; } if (it[i].t == 1) { it[i * 2].cnt = (r - l) / 2; it[i * 2].t = 1; it[i * 2 + 1].cnt = (r - l) / 2; it[i * 2 + 1].t = 1; } else if (it[i].t == 2) { it[i * 2].cnt = 0; it[i * 2].t = 2; it[i * 2 + 1].cnt = 0; it[i * 2 + 1].t = 2; } else { if (it[i * 2].t == 3) { it[i * 2].t = 0; it[i * 2].cnt = (r - l) / 2 - it[i * 2].cnt; } else if (it[i * 2].t == 2) { it[i * 2].t = 1; it[i * 2].cnt = (r - l) / 2; } else if (it[i * 2].t == 1) { it[i * 2].t = 2; it[i * 2].cnt = 0; } else { it[i * 2].t = 3; it[i * 2].cnt = (r - l) / 2 - it[i * 2].cnt; } if (it[i * 2 + 1].t == 3) { it[i * 2 + 1].t = 0; it[i * 2 + 1].cnt = (r - l) / 2 - it[i * 2 + 1].cnt; } else if (it[i * 2 + 1].t == 2) { it[i * 2 + 1].t = 1; it[i * 2 + 1].cnt = (r - l) / 2; } else if (it[i * 2 + 1].t == 1) { it[i * 2 + 1].t = 2; it[i * 2 + 1].cnt = 0; } else { it[i * 2 + 1].t = 3; it[i * 2 + 1].cnt = (r - l) / 2 - it[i * 2 + 1].cnt; } } it[i].t = 0; } unsigned long long first0(unsigned long long ll = 0, unsigned long long rr = (1 << k), unsigned long long ind = 1) { fr(ind, ll, rr); if (it[ind].cnt == rr - ll) { return rr; } if (ll + 1 == rr) { return ll; } unsigned long long f1 = first0(ll, (ll + rr) / 2, ind * 2); if (f1 == (ll + rr) / 2) { return first0((ll + rr) / 2, rr, ind * 2 + 1); } return f1; } void ins(unsigned long long l, unsigned long long r, unsigned long long t, unsigned long long ll = 0, unsigned long long rr = (1 << k), unsigned long long ind = 1) { fr(ind, ll, rr); if (l <= ll && rr <= r) { it[ind].t = t; if (t == 1) { it[ind].cnt = rr - ll; } if (t == 2) { it[ind].cnt = 0; } if (t == 3) { it[ind].cnt = (rr - ll) - it[ind].cnt; } return; } if (rr <= l || r <= ll) { return; } ins(l, r, t, ll, (ll + rr) / 2, ind * 2); ins(l, r, t, (ll + rr) / 2, rr, ind * 2 + 1); it[ind].cnt = it[ind * 2].cnt + it[ind * 2 + 1].cnt; } unsigned long long tq[(1 << 20)], lq[(1 << 20)], rq[(1 << 20)]; vector<unsigned long long> vals; map<unsigned long long, unsigned long long> comp, revComp; int main() { ios::sync_with_stdio(false); cin.tie(NULL); unsigned long long n; cin >> n; for (unsigned long long i = 0; i < n; i++) { cin >> tq[i] >> lq[i] >> rq[i]; lq[i]--; vals.push_back(lq[i]); vals.push_back(rq[i]); vals.push_back(rq[i] + 1); } vals.push_back(0); sort(vals.begin(), vals.end()); unsigned long long cv = 0; for (unsigned long long i = 0; i < vals.size(); i++) { if (i && vals[i] != vals[i - 1]) { cv++; } comp[vals[i]] = cv; revComp[cv] = vals[i]; } for (unsigned long long i = 0; i < n; i++) { ins(comp[lq[i]], comp[rq[i]], tq[i]); cout << revComp[first0()] + 1 << "\n"; } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 14; int q; long long a, b, m; int main() { ios::sync_with_stdio(0), cin.tie(0); q = 1; cin >> q; while (q--) { cin >> a >> b >> m; vector<long long> v({a}); bool done = 0; do { auto d = v; for (int i = 1; i < d.size(); i++) { long long lo = accumulate(d.begin(), d.begin() + i, 0ll), hi = lo + m; while (hi - lo > 1) { long long val = lo + hi >> 1; d[i] = val; bool ok = d[i] >= b; for (int j = i + 1; !ok && j < d.size(); j++) ok = (d[j] = accumulate(d.begin(), d.begin() + j, 0ll) + m) >= b; (ok ? hi : lo) = val; } d[i] = hi; } if (v.size() == 5) { for (auto x : d) cerr << x << ' '; cerr << '\n'; } if (d.back() == b) { done = 1; cout << d.size() << ' '; for (auto x : d) cout << x << ' '; cout << '\n'; } v.push_back(accumulate(v.begin(), v.end(), 0ll) + 1); } while (!done && v.back() <= b); if (!done) cout << "-1\n"; } }
10
CPP
#include <bits/stdc++.h> using namespace std; int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int t; cin >> t; while(t--){ int n; cin >> n; int a[n]; for(int i=0;i<n;i++){ cin >> a[i]; } sort(a,a+n); int ls=a[n-1]; int temp,count=0,maxcnt=0; if(a[0]>=1){ cout << 1 << endl; } else{ int kt=n-1; int f1=1; while(a[kt]>0){ kt--; f1*=0; } if(f1==0){ kt++; } for(int k=kt;k>=0 && k>=maxcnt;k--){ ls=a[k]; if(ls>0){ count=0; for(int i=k;i>=0;i--){ // cout << "<" << ls << ">"; // cout << "==" << a[i]; temp=a[i]; count++; int j=i-1; while(abs(temp-a[j])<ls && j>=0){ j--; } j++; if(j==-1 && abs(temp-a[j+1])>=ls){ count++; } i=j; } // cout << endl; } else{ count=k+1; } // cout << a[k] << " " << count << endl; maxcnt=max(count,maxcnt); if(ls<=0){ break; } int flag=1; while(a[k-1]==ls && k>=0){ k--; flag*=0; } if(flag==0){ k++; } } cout << maxcnt << endl; } } return 0; }
8
CPP
n = int(input()) degs = [] for i in range(n): degs.append(int(input())) res = False def cnt(now,i): if i==n and now%360==0: res = True return True elif i<n: return cnt(now+degs[i],i+1) or cnt(now-degs[i],i+1) if cnt(0,0)==True: print("YES") else: print("NO")
8
PYTHON3
#!/usr/bin/env python3 OUT = (1, 0, 2, 3) try: while True: n, m = map(int, input().split()) bus = [[None] * 4 for i in range(n)] cur = 1 for row in range(n << 1): if row >= n: i = row - n k = b = 1 else: i = row k = 3 b = 0 for j in range(2): if cur <= m: bus[i][k * j + b] = cur cur += 1 for i in range(n): for j in range(4): if bus[i][OUT[j]] is not None: print(bus[i][OUT[j]], end=' ') print() except EOFError: pass
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void read(int &x) { x = 0; char c = getchar(); int p = 1; for (; c < 48; c = getchar()) if (c == '-') p = -1; for (; c > 47; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= p; } void read(long long &x) { x = 0; char c = getchar(); int p = 1; for (; c < 48; c = getchar()) if (c == '-') p = -1; for (; c > 47; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x *= p; } void Min(int &x, int y) { if (x > y) x = y; } void Max(int &x, int y) { if (x < y) x = y; } void Min(long long &x, long long y) { if (x > y) x = y; } void Max(long long &x, long long y) { if (x < y) x = y; } struct ed { int x, nx; } e[500005 << 2]; int nx[500005], ecnt = 2, tp, st[500005], son[500005], fa[500005], dfn[500005], low[500005], tot, L[500005], DFN, n, m, colcnt, nct, a[500005], id[500005], de[500005], cnt[500005], top[500005]; bool et[500005 << 2], vis[500005]; void add(int x, int y) { e[ecnt] = (ed){y, nx[x]}; nx[x] = ecnt++; } void dfs(int x) { vis[x] = 1; dfn[x] = low[x] = ++DFN; st[++tp] = x; for (int i = nx[x]; i; i = e[i].nx) { if (!vis[e[i].x]) { et[i] = et[i ^ 1] = 1; dfs(e[i].x); Min(low[x], low[e[i].x]); if (dfn[x] <= low[e[i].x]) { ++colcnt; add(colcnt, x); for (; tp && st[tp] != e[i].x;) { add(colcnt, st[tp]); tp--; } add(colcnt, st[tp]); tp--; } } else { if (!et[i]) Min(low[x], dfn[e[i].x]); } } } void dfs1(int f, int x) { de[x] = de[f] + 1; cnt[x] = 1; fa[x] = f; for (int i = nx[x]; i; i = e[i].nx) if (e[i].x != f) { dfs1(x, e[i].x); cnt[x] += cnt[e[i].x]; if (cnt[e[i].x] > cnt[son[x]]) { son[x] = e[i].x; } } } void dfs2(int f, int x) { top[x] = f; if (son[x]) dfs2(f, son[x]); for (int i = nx[x]; i; i = e[i].nx) if (e[i].x != fa[x] && e[i].x != son[x]) { dfs2(e[i].x, e[i].x); } } int lca(int x, int y) { for (; top[x] != top[y];) { if (de[top[x]] > de[top[y]]) x = fa[top[x]]; else y = fa[top[y]]; } if (de[x] < de[y]) return x; return y; } int main() { int q, i, x, y; read(n); read(m); read(q); nct = n; for (i = 1; i <= m; i++) { read(x); read(y); add(x, y); add(y, x); } colcnt = n; dfs(1); for (i = 1; i <= n; i++) nx[i] = 0; for (x = n + 1; x <= colcnt; x++) { for (i = nx[x]; i; i = e[i].nx) { add(e[i].x, x); } } n = colcnt; dfs1(0, 1); dfs2(1, 1); for (; q--;) { read(x); read(y); printf("%d\n", (de[x] + de[y] - 2 * de[lca(x, y)]) / 2); } return 0; }
8
CPP
#include <bits/stdc++.h> typedef struct { long long w; int u, v; } Edge; int cmp(const void *a, const void *b) { const Edge *ea = (const Edge *)a, *eb = (const Edge *)b; if (ea->w == eb->w) return 0; return ea->w > eb->w ? -1 : 1; } void store_edge(Edge *e, int u, int v, long long w) { e->w = w; e->u = u; e->v = v; } Edge edges[200000]; int dsu[2 * 100000], a[100000], b[100000]; int find(int n) { if (dsu[n] == n) return n; return dsu[n] = find(dsu[n]); } int min(int a, int b) { int d = b - a; return a + (d & (d >> 31)); } void uni(int a, int b) { int sa = find(a), sb = find(b); dsu[sa] = dsu[sb] = min(sa, sb); } int main(void) { int m, n, s, x; scanf("%d%d", &m, &n); for (int i = n + m - 1; ~i; i--) dsu[i] = i; for (int i = 0; i < m; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); int len = 0; for (int i = 0; i < m; i++) { scanf("%d", &s); while (s--) { scanf("%d", &x); --x; store_edge(edges + len++, i, m + x, (long long)a[i] + b[x]); } } qsort(edges, len, sizeof(Edge), cmp); long long ans = 0; for (int i = 0; i < len; i++) { if (find(edges[i].u) == find(edges[i].v)) ans += edges[i].w; else uni(edges[i].u, edges[i].v); } printf("%lld\n", ans); return 0; }
11
CPP
n = input() s = input() k = 0 for i in range(1, len(s)): if s[i] == s[i - 1]: k += 1 print(k)
7
PYTHON3
a, t_a = map(int, input().split()) b, t_b = map(int, input().split()) h, m = map(int, input().split(':')) time = h * 60 + m start = time end = time + t_a - 1 start_time = 300 end_time = start_time + t_b - 1 intersections = 0 while start_time <= 1439: if end >= start_time and end_time >= start: intersections += 1 start_time += b end_time = start_time + t_b - 1 print(intersections)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, k; while (cin >> n >> k) { for (int i = 1; i < n - k; i++) cout << i << ' '; for (int i = n; i >= n - k; i--) cout << i << ' '; cout << endl; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 100005, INF = 2000000000000000000; const long double EPS = 0.000000000001; long long power(long long a, long long b, long long p) { if (a == 0) return 0; long long res = 1; a %= p; while (b > 0) { if (b & 1) res = (res * a) % p; b >>= 1; a = (a * a) % p; } return res; } bool prime[N] = {true}; long long sum[N] = {0}; void pre() { memset(prime, true, sizeof(prime)); for (long long i = 2; i <= N; i++) { if (prime[i]) { for (long long j = i * i; j <= N; j += i) { prime[j] = false; } } } for (long long i = 2; i < N; i++) { if (prime[i]) sum[i] += i; sum[i + 1] = sum[i]; } return; } class yup { public: long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } }; long long minCoins(long long coins[], long long m, long long V) { if (V == 0) return 0; long long res = INT_MAX; for (long long i = 0; i < m; i++) { if (coins[i] <= V) { long long sub_res = minCoins(coins, m, V - coins[i]); if (sub_res != INT_MAX && sub_res + 1 < res) res = sub_res + 1; } } return res; } bool com(long long a, long long b) { return a > b; } long long ans = 0; class gfg { public: unsigned long long getFirstSetBitPos(long long n) { return log2(n & -n) + 1; } }; int32_t main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n; cin >> n; long long a[n]; long long on = 0, tw = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 1) { on++; } else { tw++; } } if (tw >= 1) { cout << "2" << " "; tw--; if (on >= 1) { cout << "1" << " "; on--; for (long long i = 0; i < tw; i++) { cout << "2" << " "; } for (long long i = 0; i < on; i++) { cout << "1" << " "; } } else { for (long long i = 0; i < tw; i++) { cout << "2" << " "; } } } else { for (long long i = 0; i < on; i++) { cout << "1" << " "; } } }
7
CPP
#include <bits/stdc++.h> using namespace std; const int SIZE = 3010; vector<long long> adj[SIZE]; long long d[SIZE][SIZE]; void bfs(long long s) { long long vis[SIZE] = {0}; queue<long long> q; q.push(s); vis[s] = 1; d[s][s] = 0; while (!q.empty()) { long long u = q.front(); q.pop(); for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (vis[v] == 1) continue; d[s][v] = d[s][u] + 1; vis[v] = 1; q.push(v); } } } int main() { long long n, m; scanf("%lld", &n); ; scanf("%lld", &m); ; for (int i = 1; i <= 3000; i++) for (int j = 1; j <= 3000; j++) d[i][j] = INT_MAX; for (int i = 1; i <= m; i++) { long long u, v; scanf("%lld", &u); ; scanf("%lld", &v); ; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) bfs(i); long long s1, t1, l1, s2, t2, l2; scanf("%lld", &s1); ; scanf("%lld", &t1); ; scanf("%lld", &l1); ; scanf("%lld", &s2); ; scanf("%lld", &t2); ; scanf("%lld", &l2); ; long long mx = 0; if (d[s1][t1] > l1 || d[s2][t2] > l2) { printf("-1"); return 0; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (d[s1][i] + d[i][j] + d[j][t1] <= l1 && d[s2][i] + d[i][j] + d[j][t2] <= l2) mx = max(mx, m - d[s1][i] - d[i][j] - d[j][t1] - d[s2][i] - d[j][t2]); if (d[t1][i] + d[i][j] + d[j][s1] <= l1 && d[s2][i] + d[i][j] + d[j][t2] <= l2) mx = max(mx, m - d[t1][i] - d[i][j] - d[j][s1] - d[s2][i] - d[j][t2]); } mx = max(mx, m - d[s1][t1] - d[s2][t2]); printf("%lld", mx); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); double l, d, v, g, r, cur = 0, ans = 0; cin >> l >> d >> v >> g >> r; int st = 0, light = 0; while (true) { ans++; if (st) { if (light == r) { light = 0; st = 0; } } else { if (light == g) { light = 0; st = 1; } } light++; cur += v; if (st && cur - v <= d && cur > d) cur = d; if (cur >= l) { cur -= v; ans--; ans += (l - cur) / v; break; } } cout << fixed << setprecision(10); cout << ans << "\n"; return 0; }
8
CPP
n = input() x = '' for i in n: x = i+x # print(x) num = 0 arr = [] arr.append(1) for i in range(1,101): arr.append(arr[i-1]*2) data = [] data.append(1) for i in range(1,51): data.append(data[i-1]*4) for i in range(len(x)): if(x[i]=='1'): num = num + arr[i]; # print(num) for i in range(51): if(num<=data[i]): print(i) break
7
PYTHON3
#include <bits/stdc++.h> using namespace std; signed long long int pow(signed long long int base, signed long long int exponent, signed long long int modulus) { signed long long int result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } int main() { int N; cin >> N; if (N == 1) { cout << "YES\n" << 1; return 0; } if (N == 4) { cout << "YES\n1\n3\n2\n4"; return 0; } for (int i = 2; i < N; ++i) { if (N % i == 0) { cout << "NO"; return 0; } } cout << "YES\n1\n"; for (int i = 2; i < N; ++i) { cout << (i * pow(i - 1, N - 2, N)) % N << endl; } cout << N; }
9
CPP
n=int(input()) ar=list(map(int,list(input()))) li=list(map(int,input().split())) flag=False for i in range(n): if(ar[i]<li[ar[i]-1]): if(not(flag)): flag=True ar[i]=li[ar[i]-1] elif(ar[i]>li[ar[i]-1]): if(flag): break for i in ar: print(str(i),end='')
8
PYTHON3
n = input() if int(n) < 0: if len(n) <= 2: print(0) elif int(n[:len(n)-1]) > int(n[:len(n)-2] + n[len(n)-1:]): print(int(n[:len(n)-1])) else: print(int(n[:len(n)-2] + n[len(n)-1:])) if int(n) >= 0: print(int(n))
7
PYTHON3
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_back #define all(v) (v).begin(),(v).end() #define fi first #define se second typedef vector<int>vint; typedef pair<int,int>pint; typedef vector<pint>vpint; template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;} template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;} const int INF=1001001001001001001ll; int N; int R[2222],A[2222],W[2222],T[2222]; int dp[4444]; int nex[4444]; signed main(){ cin>>N; rep(i,N)cin>>R[i]>>A[i]>>W[i]>>T[i]; vpint lis; rep(i,N)lis.pb({R[i]+T[i],i}); sort(all(lis));reverse(all(lis)); for(auto ukuku:lis){ int i=ukuku.se; fill(nex,nex+4444,-INF); rep(j,4444){ if(R[i]<=j)chmax(nex[min(j-R[i],T[i])],dp[j]+A[i]); } for(int j=4444-1;j>=0;j--)if(j+R[i]<4444)chmax(nex[j],nex[j+R[i]]+W[i]); rep(j,4444)chmax(dp[j],nex[j]); } cout<<*max_element(dp+1,dp+4444)<<endl; return 0; }
0
CPP
import statistics import math n = int(input()) while(n): i = int(input()) a = [] s = input() a = s.split(" ") for z in range(len(a)): a[z] = int(a[z]) print(math.ceil(statistics.mean((a)))) n = n-1
7
PYTHON3
l = list(map(int,input().split())) if(sum(l) % 5 == 0 and sum(l) != 0): print(sum(l) // 5 ) else: print(-1)
7
PYTHON3
N = int(input()) print((N+1) // 2)
7
PYTHON3
def cc(a,b,c,n): l = [] l.append(a) l.append(b) l.append(c) l = sorted(l) k = (l[2] - l[0]) + (l[2] - l[1]) p = n - k if p>=0 and p%3==0: print("YES") else: print("NO") t = int(input()) for i in range(t): a,b,c,n = [int(i) for i in input().split()] ans = cc(a,b,c,n) ans
7
PYTHON3
from collections import deque inf = 10**10 dxdy = ((0,1),(0,-1),(1,0),(-1,0)) h, w = map(int, input().split()) ch, cw = map(int, input().split()) dh, dw = map(int, input().split()) s = ['#'*(w+2)] + ['#' + input() + '#' for _ in range(h)] + ['#'*(w+2)] d = [[inf]*(w+1) for _ in range(h+1)] d[ch][cw] = 0 q = deque([(ch,cw)]) while q: y,x = q.pop() for dx,dy in dxdy: if s[y+dy][x+dx]=='.' and d[y+dy][x+dx] > d[y][x]: d[y+dy][x+dx] = d[y][x] q.append((y+dy,x+dx)) for i in range(-2,3): for j in range(-2,3): if i==j==0: continue elif 1<=y+i<=h and 1<=x+j<=w and s[y+i][x+j]=='.' and d[y+i][x+j]>d[y][x]+1: d[y+i][x+j] = d[y][x]+1 q.appendleft((y+i,x+j)) ans = d[dh][dw] if ans == inf: print(-1) else: print(ans)
0
PYTHON3
## necessary imports import sys input = sys.stdin.readline from bisect import bisect_left; from bisect import bisect_right; from math import ceil, factorial; def ceil(x): if x != int(x): x = int(x) + 1; return x; # 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); ## 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(1e5 + 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())); def float_array(): return list(map(float, 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 ---------------- ################### def mine(n, a): f = 1; x = min(a); b = sorted(a); this = []; for i in range(n): if a[i] != b[i]: this.append(b[i]); yo = x; for i in this: yo = gcd(yo, i); if yo == x: return "YES"; else: return "NO"; for _ in range(int(input())): n = int(input()); a = int_array(); print(mine(n, a));
9
PYTHON3
while 1: a=input() if '?' in a: break else: print(int(eval(a)))
0
PYTHON3