solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
s = input() fq = 4*[0] # the first is no use. for i in range(0, len(s), 2): fq[int(s[i])] += 1 ns = '' for i in range(1,4) : while fq[i]>0 : fq[i] -= 1 ns += str(i) print('+'.join(ns))
7
PYTHON3
#41 even odds n,k=map(int,input().split()) if n%2==0: if k>n//2: s=(k-n//2)*2 else: s=2*k-1 else: if k>n//2+1: s=(k-n//2-1)*2 else: s=2*k-1 print(s)
7
PYTHON3
while True: n = int(input()) if n==0: break l = list(map(int,input().split())) r = [] for i in range(n): for j in range(i+1,n): r += [abs(l[i]-l[j])] print(min(r))
0
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) if d >= a - 1 and a >= (d - 1) // 2: print('YES') elif c >= b - 1 and b >= (c - 1) // 2: print('YES') else: print('NO')
7
PYTHON3
n = int(input()) body = [0, 0, 0] # forces = [] for i in range(n): force = list(map(int, input().split(" "))) # forces.append(force) body[0] += force[0] body[1] += force[1] body[2] += force[2] if body == [0, 0, 0]: print("YES") else: print("NO")
7
PYTHON3
t=int(input()) for q in range(t): n=int(input()) l=list(map(int,input().split())) N=[0]*101 for i in l: N[i]+=1 s=0 for i in range(101): if N[i]>0: N[i]-=1 else: s=i break for i in range(101): if N[i]>0: N[i]-=1 else: s=s+i break print(s)
7
PYTHON3
from sys import stdin, stdout # kr, kb, ky, kg garland = list(stdin.readline()[:-1]) disabled = garland.count('!') kr, kb, ky, kg = 0, 0, 0, 0 order = ['R', 'B', 'Y', 'G'] def map_colors_to_pos(garland): mapping = {} for num, lamp in enumerate(garland): if lamp != '!': mapping[num % 4] = lamp if len(mapping) > 3: break return mapping def count_broken_lamp(garland, mapping): replace_count = {} for num, lamp in enumerate(garland): if lamp == '!': replace_count[mapping[num % 4]] = replace_count.get(mapping[num % 4], 0) + 1 return replace_count def form_answer(replace_count, order): result = ' '.join([str(replace_count.get(lamp, 0)) for lamp in order]) return result mapping = map_colors_to_pos(garland) replace = count_broken_lamp(garland, mapping) stdout.write(form_answer(replace, order))
8
PYTHON3
w = int(input()) while w<1 or w>100 : w = int(input()) if w == 2 : print("NO") elif w%2 == 0 : print("YES") else : print("NO")
7
PYTHON3
"""609C""" # import math # import sys def main(): n = int(input()) a = list(map(int,input().split())) flag = True for i in range(n-1): if a[i]==n: flag = False continue if flag==False and a[i]<a[i+1]: print("NO") return elif flag==True and a[i]>a[i+1]: print("NO") return print("YES") return return main() # def test(): # t= int(input()) # while t: # main() # t-=1 # test()
8
PYTHON3
#include <iostream> using namespace std; int main(void){ int n,col[9]={}; cin>>n; for(int i=0;i<n;i++){ int a; cin>>a; if(a>3200) a=3200; col[a/400] ++; } int ans=0; for(int i=0;i<8;i++){ ans += (col[i]>0); } cout<<(ans>0?ans:1)<<" "<<(ans+col[8])<<endl; }
0
CPP
N, x = map(int, input().split()) A = list(map(int, input().split())) B = A[:] ans = sum(A) for k in range(1, N): B = [min(B[i], A[i - k]) for i in range(N)] ans = min(ans, sum(B) + k * x) print(ans)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int matriz[52][52], m; int pow_result[52][52]; int mult_result[52][52]; void multiply(int m1[52][52], int m2[52][52]) { for (int i = 0; i < m; ++i) for (int j = 0; j < m; ++j) { mult_result[i][j] = 0; for (int k = 0; k < m; ++k) { mult_result[i][j] += m1[i][k] * 1LL * m2[k][j] % 1000000007; mult_result[i][j] %= 1000000007; } } } void pow(int b[52][52], long long e) { if (e == 0) { memset(pow_result, 0, sizeof pow_result); for (int i = 0; i < 52; ++i) pow_result[i][i] = 1; } else { pow(b, e / 2); multiply(pow_result, pow_result); memcpy(pow_result, mult_result, sizeof pow_result); if (e % 2) { multiply(pow_result, matriz); memcpy(pow_result, mult_result, sizeof pow_result); } } } int main() { int i, j, k, indexes['z' + 1], answer; long long n; char c, restriction[3]; for (c = 'a'; c <= 'z'; ++c) indexes[c] = c - 'a'; for (c = 'A'; c <= 'Z'; ++c) indexes[c] = c - 'A' + 26; scanf("%I64d%d%d", &n, &m, &k); for (i = 0; i < m; ++i) for (j = 0; j < m; ++j) matriz[i][j] = 1; while (k--) { scanf("%s", restriction); matriz[indexes[restriction[0]]][indexes[restriction[1]]] = 0; } pow(matriz, n - 1); answer = 0; for (i = 0; i < m; ++i) for (j = 0; j < m; ++j) answer = (answer + pow_result[i][j]) % 1000000007; printf("%d", answer); return 0; }
11
CPP
n = int(input()) arr = list(map(int, input().split())) test = sorted(arr) start = None end = None for i in range(n): if start == None and arr[i] != test[i]: start = i continue if arr[i] != test[i]: end = i if start == None and end == None: print('yes') print(1, 1) exit(0) arr = arr[:start] + arr[start:end+1][::-1] + arr[end+1:] for i in range(n): if arr[i] != test[i]: print('no') exit(0) print('yes') print(start+1, end + 1)
8
PYTHON3
s=input() s1=input() if len(s)!=len(s1): print("NO") else: d=False for i in range(len(s)): if s[i]!=s1[len(s)-i-1]: d=True print("NO") break if not d: print("YES")
7
PYTHON3
t=int(input()) for _ in range(t): n=int(input()) a=list(map(int,input().split())) a.sort() tf=False for i in range(n-1): if a[i+1]-a[i]>1: tf=True break if tf: print('NO') else: print('YES')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int k, i, r, y; long long n, x[10000001], sum; void Input() { cin >> n; for (i = 1; i <= n; i++) cin >> x[i]; } void Sort(long long a1[], long long n) { for (i = 1; i < n; i++) for (r = 1; r <= n - i; r++) if (a1[r] > a1[r + 1]) { long long temp = a1[r]; a1[r] = a1[r + 1]; a1[r + 1] = temp; } } void Game1() { sort(x, x + n + 1); for (i = 1; i <= n; i++) sum = sum + (i + 1) * x[i]; sum = sum - x[n]; cout << sum; } int main() { Input(); Game1(); }
7
CPP
#include <bits/stdc++.h> using namespace std; typedef struct point { double x; double y; point(const double &x1 = 0, const double &y1 = 0) { x = x1; y = y1; } } point; typedef struct line { point p1; point p2; line(const point &s = point(), const point &t = point()) { p1 = s; p2 = t; } } line; double eps = 1e-8; bool operator<(const point &a, const point &b) { if (abs(a.x - b.x) > eps) return a.x < b.x; return a.y < b.y; } bool operator==(const point &a, const point &b) { return abs(a.x - b.x) < eps && abs(a.y - b.y) < eps; } point operator+(const point &a, const point &b) { return point(a.x + b.x, a.y + b.y); } point operator-(const point &a, const point &b) { return point(a.x - b.x, a.y - b.y); } point operator*(const point &a, const double &r) { return point(a.x * r, a.y * r); } point operator/(const point &a, const double &r) { return point(a.x / r, a.y / r); } inline double area(const point &p1, const point &p2, const point &p3) { point p = p1 - p3; point q = p2 - p3; return abs(p.x * q.y - p.y * q.x) / 2; } enum { LEFT = 1, RIGHT = -1, FRONT = 2, BACK = -2, ON = 0 }; inline int ccw(const point &p1, const point &p2, const point &p3) { point p = p2 - p1; point q = p3 - p1; double r = p.x * q.y - p.y * q.x; if (r > eps) { return LEFT; } else if (r < -eps) { return RIGHT; } else { double t = (p.x * q.x + p.y * q.y) / (p.x * p.x + p.y * p.y); if (t > 1) { return FRONT; } else if (t < 0) { return BACK; } else { return ON; } } } inline bool cross(const line &l1, const line &l2) { return ccw(l1.p1, l1.p2, l2.p1) * ccw(l1.p1, l1.p2, l2.p2) <= 0 && ccw(l2.p1, l2.p2, l1.p1) * ccw(l2.p1, l2.p2, l1.p2) <= 0; } int n, m = 1000000007; int f[201][201]; long long dp[201][201][201]; vector<point> v; int dfs(int x, int y, int z) { int i, j; long long ans = 0; if (dp[x][y][z] != -1) return dp[x][y][z]; if (y + 1 >= z) return dp[x][y][z] = 1; for (i = y; i <= z; i++) { if (f[x][i] == 0 || f[y][i] == 0) continue; if (ccw(v[x], v[y], v[i]) != LEFT) continue; ans += (long long)dfs(i, y, i) * dfs(x, i, z) % m; if (ans >= m) ans -= m; } return dp[x][y][z] = ans; } int main() { int i, j, k; double sum = 0; scanf("%d", &n); for (i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); v.push_back(point(x, y)); } for (i = 2; i < n; i++) { double s = area(v[0], v[i - 1], v[i]); if (ccw(v[0], v[i - 1], v[i]) == RIGHT) s = -s; sum += s; } if (sum < 0) reverse(v.begin(), v.end()); v.push_back(v[0]); for (i = 0; i < v.size(); i++) { for (j = 0; j < v.size(); j++) { line l = line(v[i], v[j]); if (v[i] == v[j]) continue; for (k = 0; k < v.size() - 1; k++) { if (v[i] == v[k] || v[j] == v[k]) continue; if (v[i] == v[k + 1] || v[j] == v[k + 1]) continue; if (cross(l, line(v[k], v[k + 1]))) break; } if (k == v.size() - 1) f[i][j] = 1; } } for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { for (k = 0; k <= n; k++) { dp[i][j][k] = -1; } } } printf("%d", dfs(0, 1, n)); printf("\n"); return 0; }
11
CPP
#include <bits/stdc++.h> #define ll long long int using namespace std; int main() { ll t; cin >> t; while(t--) { ll n; cin >> n; ll a[n]; ll c=0; for(int i=0;i<n;i++) { cin >> a[i]; if(i+1-a[i]==0) c++; } if(c==n) cout << 0 << endl; else if(a[0]==1 || a[n-1]==n) cout << 1 << endl; else if(a[0]==n && a[n-1]==1) cout << 3 << endl; else cout << 2 << endl; } return 0; }
8
CPP
def vh(s1, s2): b=[] b+=s2 for i in range(len(s1)): if s1[i] in b: b.remove(s1[i]) else: return False return True kl=int(input()) for l in range(kl): s=str(input()) h=str(input()) pr=1 for i in range(len(h)-len(s)+1): if vh(h[i:i+len(s)], s): print('YES') pr=0 break if pr: print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 50700; long long f(int n, int k) { long long ans = 1; for (int i = 1, j = n + k - 1; i <= k; i++, j--) { ans = ans * j / i; } return ans; } int main() { int n; cin >> n; cout << f(n, 3) * f(n, 5) << endl; }
13
CPP
N = int(input()) z = [] w = [] for i in range(N): x, y = map(int, input().split()) z.append(x+y) w.append(x-y) z.sort() w.sort() print(max(z[-1]-z[0], w[-1]-w[0]))
0
PYTHON3
#include<bits/stdc++.h> #define int long long using namespace std; signed main() { int n; cin>>n; string s=""; while(n!=0){ int k=n%26; if(k==0){ k=26; n--; } s=char(k+'a'-1)+s; n/=26; } cout<<s; return 0; }
0
CPP
def cut(cake,s): ret_cake = [ (-1,-1),(-1,-1) ] w = cake[2] h = cake[1] s %= 2*(w+h) if w > s: ret_cake = [(h,s),(h,w-s)] elif w+h > s: ret_cake = [(s-w,w),(w+h-s,w)] elif 2*w+h > s: ret_cake = [(h,s-w-h),(h,2*w+h-s)] elif 2*w+2*h > s: ret_cake = [(s-2*w-h,w),(2*w+2*h-s,w)] ret_cake = [ (c[0]*c[1],c[0],c[1]) for c in ret_cake] ret_cake.sort() return ret_cake while True: n,w,d = map(int,input().split()) if n == 0 and w == 0 and d == 0: break p = [0] * n s = [0] * n for i in range(n): p[i],s[i] = map(int,input().split()) cake = [ (w*d,d,w) ] for i in range(n): cake = cake[:p[i]-1] + cake[p[i]:] + cut(cake[p[i]-1],s[i]) cake.sort() print( " ".join(map(str,[c[0] for c in cake])))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; const double PI = acos(-1); const double EPS = 1e-8; int mp[1 << 15]; int num[6500][6500]; int n, n2; int ans; const int mod = 1e9 + 7; void dfs(bool f, int i, int S, int T) { if (f) { if (i == n) { int SS = ((1 << n) - 1) ^ S; int TT = ((1 << n) - 1) ^ T; ans = (ans + num[mp[SS]][mp[TT]]) % mod; return; } } else { if (i == n2) { num[mp[S]][mp[T]]++; return; } } for (int j = 0; j < (int)(n); ++j) { if (S >> j & 1) continue; int t = (i + j) % n; if (T >> t & 1) continue; dfs(f, i + 1, S | 1 << j, T | 1 << t); } } int main() { while (cin >> n) { if (n % 2 == 0) { cout << 0 << endl; continue; } n2 = n / 2; int m = 0; for (int S = 0; S < (int)(1 << n); ++S) if (__builtin_popcount(S) == n2) { mp[S] = m++; } memset(num, 0, sizeof(num)); dfs(0, 0, 0, 0); ans = 0; dfs(1, n2, 0, 0); long long a = ans; for (int i = 0; i < (int)(n); ++i) a = a * (i + 1) % mod; cout << a << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; struct less_key { bool operator()(pair<int64_t, int64_t> p1, pair<int64_t, int64_t> p2) { return (p1.first < p2.first) || (p1.second < p2.second); } }; struct pair_hash { std::size_t operator()(const pair<int64_t, int64_t>& k) const { return static_cast<size_t>(k.first ^ k.second); } }; const int64_t mod = 1000000007; int64_t n; int64_t a[3]; int main() { cin >> n; int64_t i = 1; a[0] = 1; a[1] = 2; for (;;) { if (a[1] > n) { cout << i - 1; return 0; } int64_t t = a[0] + a[1]; swap(a[0], a[1]); a[1] = t; i++; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5e6; const long long mod = 1e9 + 7; mt19937 gen(time(0)); int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; long long x1 = 0, x2 = 0, y1 = 0, y2 = 0; for (long long i = 0; i < n; ++i) { long long k; cin >> k; if (k % 2 == 0) x1++; else y1++; } for (long long i = 0; i < m; ++i) { long long k; cin >> k; if (k % 2 == 0) x2++; else y2++; } cout << min(x1, y2) + min(x2, y1); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main(void) { char str[10]; int mat[2][2], des[2][2]; memset(mat, 0, sizeof(mat)); memset(des, 0, sizeof(des)); for (int i = 0; i < 2; i++) { gets(str); if (str[0] != 'X') mat[i][0] = str[0] - 'A' + 1; if (str[1] != 'X') mat[i][1] = str[1] - 'A' + 1; } for (int i = 0; i < 2; i++) { gets(str); if (str[0] != 'X') des[i][0] = str[0] - 'A' + 1; if (str[1] != 'X') des[i][1] = str[1] - 'A' + 1; } int xdir[][2] = {{0, 1}, {0, 1}}; int ydir[][2] = {{1, 1}, {0, 0}}; for (int i = 0; i < 30; i++) { bool flag = false; int j, k; for (j = 0; j < 2; j++) { for (k = 0; k < 2; k++) { if (mat[j][k] == 0) { flag = true; break; } } if (flag) break; } int x = xdir[j][k], y = ydir[j][k]; mat[j][k] = mat[x][y]; mat[x][y] = 0; flag = false; for (j = 0; j < 2; j++) { for (k = 0; k < 2; k++) { if (mat[j][k] != des[j][k]) { flag = true; break; } } if (flag) break; } if (!flag) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
7
CPP
#include<bits/stdc++.h> using namespace std; #define int long long void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; } void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');} #define lf double #define ll long long #define pii pair<int,int > #define vec vector<int > #define pb push_back #define mp make_pair #define fr first #define sc second #define data asd09123jdf02i3h #define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) const int maxn = 1e6+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7; struct node { int a[26],s; node () {memset(a,0,sizeof a);} node operator + (const node &x) const { node c; for(int i=0;i<26;i++) c.a[i]=a[i]+x.a[i]; c.s=s+x.s; return c; } node operator - (const node &x) const { node c; for(int i=0;i<26;i++) c.a[i]=a[i]-x.a[i]; c.s=s-x.s; return c; } }f[100]; int tot,n,p,nxt[maxn]; char s[maxn]; void gao() { for(int i=2;i<=n;i++) { int j=nxt[i-1]; while(j&&s[j+1]!=s[i]) j=nxt[j]; if(s[j+1]==s[i]) nxt[i]=j+1; } } node solve(int r) { node ans; for(int i=tot;~i;i--) { if(r<f[i].s) continue; r-=f[i].s,ans=ans+f[i]; } for(int i=1;i<=r;i++) ans.a[s[i]-'a']++; return ans; } signed main() { scanf("%s",s+1),n=strlen(s+1)/2; gao();int p=n-nxt[n]; for(int i=1;i<=n;i++) f[0].a[s[i]-'a']++;f[1]=f[0]; for(int i=1;i<=p;i++) f[1].a[s[i]-'a']++; f[0].s=n,f[1].s=n+p;tot=1; for(int i=2;f[i-1].s+f[i-2].s<=1e18;i++) f[++tot]=f[i-1]+f[i-2]; int l,r;read(l),read(r); node ans=solve(r)-solve(l-1); for(int i=0;i<26;i++) printf("%lld ",ans.a[i]);puts(""); return 0; }
0
CPP
from collections import deque totCases = int(input()) for _ in range(totCases): n,m = [int(i) for i in input().split(' ')] stpos = [int(i) for i in input().split(' ')] direc = [i.upper() for i in input().split(' ')] rob = sorted([(stpos[i],direc[i],i) for i in range(n)]) ans = [-1 for i in range(n)] evs,ods = deque(), deque() for i in range(n): targ = rob[i] que = ods if (targ[0]&1)==1 else evs if(targ[1] == 'R'): que.append(targ) else: if not que: que.append((-targ[0], 'R', targ[-1])) else: time = (targ[0]-que[-1][0])>>1 ans[targ[-1]] = time; ans[que.pop()[-1]] = time; for que in [evs,ods]: while(len(que)>=2): l = que.pop(); r = que.pop() r = (m*2-r[0], r[1],r[2]) time = (r[0]-l[0])>>1 ans[l[-1]] = time; ans[r[-1]] = time #output for it in ans: print(it,end=' ') print()
9
PYTHON3
from collections import Counter,defaultdict,deque #import heapq as hq #from itertools import count, islice #from functools import reduce #alph = 'abcdefghijklmnopqrstuvwxyz' #from math import factorial as fact #a,b = [int(x) for x in input().split()] import math import sys input=sys.stdin.readline n = int(input()) mod = 10**9+7 ans = (3**(3*n)-7**n)%mod print(ans)
8
PYTHON3
# cook your dish here import math n=int(input()) x=n//2 m=math.factorial(n)//(math.factorial(x)*math.factorial(x)) s=m*math.factorial(x-1)*math.factorial(x-1) s=s//2 print(s)
11
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int p[2010][2010], k; int input[1010]; struct H { int value; H(int x, int y) { value = p[x + y - 1][y]; } }; inline void createPascal() { for (int i = 0; i <= 2000; i++) p[i][0] = 1, p[i][i] = 1; for (int i = 2; i <= 2000; i++) { for (int j = 1; j < i; j++) { p[i][j] = (p[i - 1][j - 1] % mod + p[i - 1][j] % mod) % mod; } } } int main() { createPascal(); scanf("%d", &k); for (int i = 0; i < k; i++) scanf("%d", &input[i]); int present = input[0]; long long ans = 1; for (int i = 1; i < k; i++) { if (input[i] == 1) { present += 1; continue; } else { ans = (H(present + 1, input[i] - 1).value * ans) % mod; } present += input[i]; } printf("%lld\n", ans); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<string> matr(2005); int dp[2005][2005]; int used[2005][2005]; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> matr[i]; } dp[0][0] = matr[0][0] == 'a'; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i + 1 < n) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + (matr[i + 1][j] == 'a')); } if (j + 1 < n) { dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + (matr[i][j + 1] == 'a')); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i + j - dp[i][j] < k) { matr[i][j] = 'a'; } } } vector<pair<int, int> > cur = {{0, 0}}, nw; string ans; ans.push_back(matr[0][0]); for (int i = 0; i < n * 2 - 1; i++) { nw.clear(); for (char j = 'a'; j <= 'z'; j++) { for (int h = 0; h < cur.size(); h++) { int y = cur[h].first, x = cur[h].second; if (y + 1 < n && j == matr[y + 1][x] && !used[y + 1][x]) { used[y + 1][x] = 1; nw.push_back({y + 1, x}); } if (x + 1 < n && j == matr[y][x + 1] && !used[y][x + 1]) { used[y][x + 1] = 1; nw.push_back({y, x + 1}); } } if (nw.size()) { cur.swap(nw); ans.push_back(j); break; } } } cout << ans; return 0; }
10
CPP
for i in range(int(input())): n=int(input()) arr=list(map(int,input().split())) ans=0 for j in range(n-2,-1,-1): if(arr[j]>arr[j+1]): ans+=(arr[j]-arr[j+1]) print(ans)
9
PYTHON3
a,b,c,d=map(int,input().split()) k=[a-b,a-c,a-d,b-c,b-d,c-d] if k.count(0)==0: print(0) elif k.count(0)==1: print(1) elif k.count(0)==3 or k.count(0)==2: print(2) else: print(3)
7
PYTHON3
from heapq import heapify,heappop,heappush n, a = int(input()), list(map(int,input().split())) res, cnt, total = [], 0, 0 heapify(res) for i in range(n): total += a[i] cnt += 1 if a[i] < 0: heappush(res,a[i]) while total < 0: tmp = heappop(res) total -= tmp cnt -= 1 print(cnt)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const long long mod = 1e9 + 7; const long long inf = 1e18 + 10; long long a[N], cnt[N], sum[N]; void add_cnt(long long x) { for (; x < N; x += x & -x) cnt[x]++; } void add_sum(long long x, long long y) { for (; x < N; x += x & -x) sum[x] += y; } long long get_cnt(long long x) { long long res = 0; for (; x; x -= x & -x) res += cnt[x]; return res; } long long get_sum(long long x) { long long res = 0; for (; x; x -= x & -x) res += sum[x]; return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; double E = 0.0; for (long long i = 1; i <= n; i++) E += (i * ((n - i) * (n + 1) - (n * (n + 1) / 2 - i * (i + 1) / 2))) / ((n + 0.0) * (n + 1.0)); for (long long i = n; i >= 1; i--) { long long c = get_cnt(a[i]), s = get_sum(a[i]); E += c - (i * (c * (n + 1) - s)) / (n * (n + 1) / 2.0); add_cnt(a[i]); add_sum(a[i], i); } cout << setprecision(30) << E; return 0; }
11
CPP
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n; while(cin>>n&&n){ int a1,a2,b1,b2,c1,c2,d1,d2; for(int i=0;i<n;i++){ cin>>a1>>b1>>c1>>d1; cin>>a2>>b2>>c2>>d2; int as=a1*a2-b1*b2-c1*c2-d1*d2; int bs=a1*b2+b1*a2+c1*d2-c2*d1; int cs=a1*c2-b1*d2+a2*c1+b2*d1; int ds=a1*d2+b1*c2-b2*c1+a2*d1; cout<<as<<" "<<bs<<" "<<cs<<" "<<ds<<endl; } } return 0; }
0
CPP
n,d=input().split() n,d=int(n),int(d) a=list(map(int,input().strip().split())) c=0 for i in range(0,len(a)-1): for j in range(i+1,len(a)): if(abs(a[i]-a[j])<=d): c+=2 print(c)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 3e3 + 3; long long n, m, ans; bitset<N> row[N], dia[N], a[N]; string s; int main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < m; j++) if (s[m - 1 - j] == 'z') { row[i][j] = a[i][j] = dia[i][j] = 1; ans++; } } for (int x = 1; x < min(n, m); x++) { for (int i = 0; i < n; i++) row[i] &= a[i] >> x; for (int i = x; i < n; i++) dia[i - x] &= a[i] >> x; for (int i = x; i < n; i++) ans += (row[i - x] & dia[i - x] & row[i]).count(); } cout << ans << endl; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) { cout << "white" << endl; cout << "1 2" << endl; return 0; } cout << "black" << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 22; int n, m; long long cap[N]; int res[N]; int sz[N]; pair<int, int> edge[N]; bool mark[N], eg[N]; vector<int> G[N]; void dfs(int v) { mark[v] = 1; for (auto& e : (G[v])) { int u = edge[e].first + edge[e].second - v; if (mark[u]) continue; eg[e] = 1; dfs(u); } } int main() { cin >> n; long long sum = 0; for (int i = 0; i < (n); i++) cin >> cap[i], sum += cap[i]; if (sum) return cout << "Impossible", 0; cin >> m; for (int i = 0; i < (m); i++) { int x, y; cin >> x >> y; x--, y--; edge[i] = {x, y}; G[x].push_back(i); G[y].push_back(i); } cout << "Possible" << endl; dfs(0); vector<int> vv; for (int i = 0; i < (n); i++) { for (auto& e : (G[i])) { if (!eg[e]) continue; sz[i]++; } if (sz[i] == 1) vv.push_back(i); } while (vv.size()) { int u = vv.back(); vv.pop_back(); for (auto& e : (G[u])) { if (!eg[e]) continue; int r = edge[e].first + edge[e].second - u; eg[e] = 0; sz[r]--; if (sz[r] == 1) vv.push_back(r); res[e] = cap[u]; if (edge[e].first == u) res[e] *= -1; cap[r] += cap[u]; } } for (int i = 0; i < (m); i++) cout << res[i] << endl; return 0; }
12
CPP
numcases = int(input()) for i in range(numcases): n = int(input()) ans = 0 for i in range(2,30): if n%(2**i - 1) == 0: ans = n/(2**i - 1) break print(int(ans))
7
PYTHON3
#include <bits/stdc++.h> char a[200][200]; int d[30][30]; char s[30]; int main() { char t; int i, j, k, n; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%s", a[i]); for (i = 1; i < n; i++) { for (j = 0; a[i][j] && a[i][j] == a[i - 1][j]; j++) ; if (a[i][j] && a[i - 1][j]) d[a[i][j] - 'a'][a[i - 1][j] - 'a'] = 1; if (!a[i][j] && a[i - 1][j]) { puts("Impossible"); return 0; } } for (i = 0; i < 26; i++) s[i] = i + 'a'; for (i = 0; i < 26; i++) for (j = 0; j < 26; j++) for (k = j + 1; k < 26; k++) if (d[s[j] - 'a'][s[k] - 'a']) { t = s[j]; s[j] = s[k]; s[k] = t; } for (j = 0; j < 26; j++) for (k = j + 1; k < 26; k++) if (d[s[j] - 'a'][s[k] - 'a']) { puts("Impossible"); return 0; } puts(s); return 0; }
7
CPP
from math import * import sys input=sys.stdin.readline t=int(input()) while t>0: t-=1 n=int(input()) a=[int(x) for x in input().split()] flag=0 for i in range(n): if a[i]==0 and 0<i<n-1: flag=1 break if flag==1: print("No") continue dp=[[0,0] for i in range(n+1)] flag=[0,0] dp[0][0]=min(n-1,a[0]) dp[0][1]=0 for i in range(1,n): dp[i][0]=min(dp[i-1][0]-1,a[i]) if dp[i][0]<0: flag[0]=2 if dp[i-1][1]+1>a[i] and flag[1]==0: flag[1]=1 dp[i-1][1]=max(dp[i-1][1],a[i-1]) dp[i][1]=min(dp[i-1][1]-1,a[i]) if dp[i][1]<0: flag[1]=2 elif dp[i-1][1]+1<=a[i] and flag[1]==0: dp[i][1]=dp[i-1][1]+1 elif flag[1]==1: dp[i][1]=min(dp[i-1][1]-1,a[i]) if dp[i][1]<0: flag[1]=2 if flag==[2,2]: break if flag==[2,2]: print("No") else: print("Yes")
8
PYTHON3
n=int(input()) A=list(map(int,input().split())) flag=1 count =0 while flag ==1: flag=0 for j in range(n-1,0,-1): if A[j]<A[j-1]: A[j],A[j-1]= A[j-1],A[j] flag=1 count+=1 print(" ".join(map(str,A))) print(count)
0
PYTHON3
#include <bits/stdc++.h> int main(void) { int n = int(); std ::cin >> n; int minX, minY, maxX, maxY; std ::cin >> minX >> minY >> maxX >> maxY; int sum = (maxX - minX) * (maxY - minY); for (int i = 1; i < n; ++i) { int x1, y1, x2, y2; std ::cin >> x1 >> y1 >> x2 >> y2; sum += (x2 - x1) * (y2 - y1); if (x1 < minX) minX = x1; if (x2 > maxX) maxX = x2; if (y1 < minY) minY = y1; if (y2 > maxY) maxY = y2; } if (maxY - minY == maxX - minX && (maxX - minX) * (maxY - minY) == sum) std ::cout << "YES" << std ::endl; else std ::cout << "NO" << std ::endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[100001]; for (int i = 1; i <= n; i++) { cin >> a[i]; } int flag; if (n > 2) { for (int i = 1; i < n; i++) { if (a[i] != a[i + 1]) { swap(a[i], a[i + 1]); flag = 0; for (int j = 2; j <= n; j++) { if (a[j] > a[j - 1]) { flag++; break; } } for (int j = 2; j <= n; j++) { if (a[j] < a[j - 1]) { flag++; break; } } swap(a[i], a[i + 1]); if (flag < 2) continue; cout << i << " " << i + 1 << endl; return 0; } } cout << -1 << endl; } else cout << -1 << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long A[100008]; map<long long, long long> mymap; long long pow(long long a, long long b) { long long res = 1; while (b) { if (b % 2) { res = (res * a) % 1000000007; } a = (a * a) % 1000000007; b /= 2; } return res; } int main() { long long n, x; long long ans, total = 0; scanf("%lld", &n); scanf("%lld", &x); for (int i = 0; i < n; i++) { scanf("%lld", &A[i]); total += A[i]; } for (int i = 0; i < n; i++) { A[i] = total - A[i]; mymap[A[i]]++; } for (typeof((mymap).begin()) it = (mymap).begin(); it != (mymap).end(); it++) { if (it->second / x != 0) { mymap[it->first + 1] += it->second / x; } mymap[it->first] = it->second % x; if (mymap[it->first] != 0) { ans = it->first; break; } } ans = pow(x, min(ans, total)); printf("%lld\n", ans); ; return 0; }
9
CPP
from sys import stdin input = stdin.readline def main(): t = int(input()) for __ in range(t): n,m = map(int,input().split()) row_set = set() col_set = set() for i in range(n): row = list(map(int,input().split())) for j in range(m): if row[j]==1: row_set.add(i) col_set.add(j) rows = n - len(row_set) cols = m - len(col_set) if min(rows,cols)%2 == 1: print("Ashish") else: print("Vivek") if __name__ == '__main__': main()
7
PYTHON3
n,m = map(int, input().split()) if n >=m: (n,m) = (m,n) ver = m//2 tot = n*ver rem = m % 2 ext=0 if rem >=1 : if n >= 2: p = n//2 ext = p*rem tot = tot + ext print(tot)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; int n; int a[23]; int dp[1 << 23]; int solve(int x) { if (~dp[x]) return dp[x]; if (x == 1) return 1; int ret = INF; int cnt = __builtin_popcount(x); for (int i = n - 1; i >= 0; i--) { if (x & 1 << i) { int y = (x ^ (1 << i)) | (1 << (i - 1)); for (int j = 0; j < i; j++) for (int k = 0; k <= j; k++) if (a[j] + a[k] == a[i]) { int tmp = solve(y | (1 << j) | (1 << k)); ret = min(ret, max(cnt, tmp)); } break; } } return dp[x] = ret; } int main() { cin >> n; for (int i = 0; i < (int)(n); i++) cin >> a[i]; memset(dp, -1, sizeof(dp)); int ret = solve(1 << (n - 1)); if (ret == INF) ret = -1; cout << ret << endl; return 0; }
10
CPP
n, k, l, c, d, p, nl, np = map(int,input().split()) tmili = k*l//nl tsalt = p//np tlime = c*d print(min(tmili,tsalt,tlime)//n)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { float a, b, c, d; cin >> a >> b >> c >> d; double ps = a / b, pz = c / d, pw = 0; double nps = 1 - ps, npz = 1 - pz; for (int i = 0;; i++) { double k = pow(nps, i) * pow(npz, i) * ps; pw += k; if (k < 0.0000000000001) break; } printf("%.12f", pw); }
8
CPP
a=list(map(int,input().split())) x=sum(a) flag=0 if x%2!=0: print("NO") else: for i in range(len(a)): s=a[i] for j in range(len(a)): if s==x//2 and i!=j: flag+=1 break elif i!=j: s+=a[j] if flag==1: break if flag==1: print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { char s[100005], g[3]; int f[256]; int n, ans = 0; scanf("%s %d", s, &n); while (n--) { scanf(" %s", g); f[g[0]] = g[1]; f[g[1]] = g[0]; } for (int i = 0; i < strlen(s);) { int j = i, c1 = 0, c2 = 0; while (j < strlen(s) && (s[j] == s[i] || s[j] == f[s[i]])) { if (s[j] == s[i]) c1++; else c2++; j++; } ans += min(c1, c2); i = j; } printf("%d\n", ans); return 0; }
7
CPP
I=lambda:map(int,input().split()) n,s=I() print(max(max(sum(I()),s)for _ in '0'*n))
7
PYTHON3
from bisect import bisect_left def bins(holes, x, bot, top=None): top = top if top is not None else len(holes) pos = bisect_left(holes, x, bot, top) return pos != top and holes[pos] == x b, q, l, m = list(map(int, input().split(' '))) s = list(map(int, input().split(' '))) s = sorted(s) counter = 0 if abs(b) > l: print(0) else: if q == 0 or q == -1 or q == 1: if q == 1: if bins(s, b, 0): print("0") else: print("inf") elif q == 0: res = 0 if not bins(s, b, 0): res = 1 if bins(s, 0, 0): print(res) else: print("inf") elif q == -1: if bins(s, b, 0) and bins(s, -b, 0): print("0") else: print("inf") elif b == 0: if bins(s, b, 0): print("0") else: print("inf") else: while abs(b) <= l: if not bins(s, b, 0): counter += 1 b *= q print(counter)
8
PYTHON3
#include <iostream> using namespace std; int main(){ int N; cin >> N; int ma=0; long long ans=0; for(int i=0; i<N; i++){ int a; cin >> a; ma=max(ma,a); ans +=(ma-a); } cout << ans << endl; }
0
CPP
#include <bits/stdc++.h> #define PB push_back #define MP make_pair #define REP(i,n) for (int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define ALL(a) (a).begin(),(a).end() using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> P; double EPS=1e-8; int cnt[1000][1000][3]; int main(){ int m,n,t;char c; cin>>n>>m>>t; REP(i,n)REP(j,m){ cin>>c; if(c=='J')cnt[j][i][0]++; else if(c=='O')cnt[j][i][1]++; else cnt[j][i][2]++; } REP(i,n)REP(j,m)REP(k,3){ if(i!=0)cnt[j][i][k]+=cnt[j][i-1][k]; if(j!=0)cnt[j][i][k]+=cnt[j-1][i][k]; if(j!=0&&j!=0)cnt[j][i][k]-=cnt[j-1][i-1][k]; } while(t--){ int u,x,y,z; cin>>u>>x>>y>>z;u--;x--;y--;z--; int ans[3]; REP(i,3){ ans[i]=cnt[z][y][i]; if(u!=0)ans[i]-=cnt[z][u-1][i]; if(x!=0)ans[i]-=cnt[x-1][y][i]; if(u!=0&&x!=0)ans[i]+=cnt[x-1][u-1][i]; } cout<<ans[0]<<" "<<ans[1]<<" "<<ans[2]<<endl; } }
0
CPP
#include <bits/stdc++.h> using namespace std; map<long long int, long long int> mp; int a[112345]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } long long int sum = 0; long long int d = 0; for (int i = 0; i < n; i++) { sum += a[i]; mp[sum]++; d = max(d, mp[sum]); } cout << n - d << endl; }
9
CPP
from collections import Counter import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 import math class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def divs(n, start=2): divisors = [] for i in range(start, int(math.sqrt(n) + 1)): if n % i == 0: if n / i == i: divisors.append(i) else: divisors.extend([i, n // i]) return divisors ########################################################## #it=iter(t) #if all(c in it for c in s): #ls.sort(key=lambda x: (x[0], x[1])) # ls.sort(key=lambda x: (x[0], x[1])) import sys inf = float("inf") mod = 1000000007 def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) import sys from bisect import bisect_left #n, k = map(int, sys.stdin.readline().split()) #arr = list(map(int, sys.stdin.readline().split())) #arr=[(int(x),i) for i,x in enumerate(input().split())] # ls=list(map(int,input().split())) # for i in range(m): #print(s[i],end="") #n=int(sys.stdin.readline()) #arr=list(map(int,input().split())) import sys def can(x): cur=n su=0 while cur: val=min(x,cur) su+=val cur-=val cur-=cur//10 return 2*su>=n n=int(sys.stdin.readline()) l=1 r=n while l<=r: mid=(l+r)//2 if can(mid): ans=mid r=mid-1 else: l=mid+1 print(ans)
9
PYTHON3
n,m=map(int,input().split()) s=[int(i) for i in input().split()] t=[int(i) for i in input().split()] mod=10**9+7 dp=[[0 for i in range(m+1)]for j in range(n+1)] for i in range(n): for j in range(m): x=0 if s[i]==t[j]: x=dp[i][j]+1 dp[i+1][j+1]=(dp[i+1][j]+dp[i][j+1]-dp[i][j]+x)%mod print((dp[n][m]+1)%mod)
0
PYTHON3
#include<iostream> #include<string> using namespace std; int main(){ int length=0, num=0, big=0, small=0; string str; cin >> str; if(str.size() >= 6) length++; for(int i = 0; i < str.size(); i++){ if(str[i] >= '0' && str[i] <= '9') num++; else if(str[i] >= 'a' && str[i] <= 'z') small++; else if(str[i] >= 'A' && str[i] <= 'Z') big++; } if(length != 0 && num != 0 && small != 0 && big != 0) cout << "VALID" << endl; else cout << "INVALID" << endl; }
0
CPP
testCases = int(input()) while testCases > 0: a = int(input()) candieList = [int(i) for i in input().split()] orangeList = [int(i) for i in input().split()] cmin = min(candieList) omin = min(orangeList) tot_count = 0 for i in range(a): diff1 = candieList[i] - cmin diff2 = orangeList[i] - omin tot_count += max(diff1, diff2) print(tot_count) testCases -= 1
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 10005; int N, M, used[MAX_N], deg_in[MAX_N], cmp[MAX_N]; vector<vector<int>> G(MAX_N); int bfs(int s, int cnt){ queue<int> q; q.push(s); used[s] = 1; cmp[cnt++] = s; while(!q.empty()){ int u = q.front(); q.pop(); for(int v : G[u]){ deg_in[v]--; if(deg_in[v]==0){ used[v] = 1; cmp[cnt++] = v; q.push(v); } } } return cnt; } int main(){ ios::sync_with_stdio(false), cin.tie(nullptr); cin >> N >> M; for(int i=0;i<M;i++){ int a, b; cin >> a >> b; G[a].push_back(b); deg_in[b]++; } int cnt = 0; for(int i=0;i<N;i++){ if(used[i]==0 && deg_in[i]==0) cnt = bfs(i, cnt); } for(int i=0;i<N;i++) cout << cmp[i] << endl; return 0; }
0
CPP
t = int(input()) while(t): s = input().split('0') s.sort(reverse=True) aliceScr = 0 turn = 0 for ele in s: if turn % 2 == 0: aliceScr += len(ele) turn += 1 print(aliceScr) t -= 1
8
PYTHON3
n, k = map(int, input().split()) print('YES') if (k % 2 == 0): for i in range(n): print('.', end='') print() print('.', end='') for i in range(k//2): print('#', end='') for i in range(n-k//2-1): print('.', end='') print() print('.', end='') for i in range(k // 2): print('#', end='') for i in range(n - k // 2 - 1): print('.', end='') print() for i in range(n): print('.', end='') else: if k>=5: k = k+1 for i in range(n): print('.', end='') print() print('.', end='') print('#', end='') print('.', end='') print('#', end='') for i in range(k // 2 - 3): print('#', end='') for i in range(n - k // 2 - 1): print('.', end='') print() print('.', end='') for i in range(k // 2): print('#', end='') for i in range(n - k // 2 - 1): print('.', end='') print() for i in range(n): print('.', end='') if k == 1: for i in range(n): print('.', end='') print() for i in range(n//2): print('.', end='') print('#', end='') for i in range(n//2): print('.', end='') print() for i in range(n): print('.', end='') print() for i in range(n): print('.', end='') print() if k==3: for i in range(n): print('.', end='') print() for i in range(n//2-1): print('.', end='') print('#', end='') print('#', end='') print('#', end='') for i in range(n//2-1): print('.', end='') print() for i in range(n): print('.', end='') print() for i in range(n): print('.', end='') print()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } cout << v[1] - v[0] << " " << v[n - 1] - v[0] << endl; for (int i = 1; i < n - 1; i++) { cout << min(v[i + 1] - v[i], v[i] - v[i - 1]) << " " << max(v[n - 1] - v[i], v[i] - v[0]) << endl; } cout << v[n - 1] - v[n - 2] << " " << v[n - 1] - v[0] << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const long long int maxn = (3e5) + 10, mod = 1e9 + 7; long long int n, x, y, dp[2][maxn], ans, mark[maxn]; vector<long long int> v[maxn]; void DFS(long long int x, long long int p) { for (auto u : v[x]) { if (u == p) continue; DFS(u, x); } long long int res = 0, pnt = 0; for (auto u : v[x]) if (u != p && !mark[u]) res++; if (res == 1) ans++; else if (res > 1) { ans += 2; mark[x] = 1; } } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> x >> y; bool can = 1; for (long long int i = 0; i < n - 1; i++) { long long int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } for (long long int i = 1; i <= n; i++) if (v[i].size() == n - 1) can = 0; if (x > y && can) return cout << (n - 1) * y << endl, 0; if (!can && x > y) return cout << (n - 2) * y + x << endl, 0; DFS(1, -1); cout << (ans)*x + ((n - 1) - (ans)) * y << endl; return 0; }
10
CPP
T = int(input()) for i in range(T): arr = list(map(int, input().split())) if sum(arr)%9 == 0: if min(arr) >= sum(arr)//9: print('YES') else: print('NO') else: print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; namespace Dinic { const int INF = 1e9 + 1e8; const int N = 11111; const int M = 111111; int fst[N], nxt[M], to[M]; int cap[M]; int dis[N], q[N], ptr[N]; int V, E; void init() { memset(fst, -1, sizeof fst); V = E = 0; } inline int add_node() { V++; return V - 1; } inline void add_edge(int u, int v, int c) { to[E] = v, cap[E] = c, nxt[E] = fst[u], fst[u] = E++; to[E] = u, cap[E] = 0, nxt[E] = fst[v], fst[v] = E++; } inline bool bfs(int S, int T, int n) { memset(dis, -1, sizeof(int) * n); int h = 0, t = 0; dis[S] = 0, q[t++] = S; while (h < t) { int u = q[h++]; for (int e = fst[u]; ~e; e = nxt[e]) if (cap[e] > 0 && dis[to[e]] == -1) { dis[to[e]] = dis[u] + 1, q[t++] = to[e]; if (to[e] == T) return 1; } } return (dis[T] != -1); } int dfs(int u, int T, int f) { if (u == T) return f; for (int &e = ptr[u]; ~e; e = nxt[e]) if (cap[e] > 0 && dis[to[e]] > dis[u]) { int ret = dfs(to[e], T, min(f, cap[e])); if (ret > 0) { cap[e] -= ret, cap[e ^ 1] += ret; return ret; } } return 0; } int max_flow(int S, int T, int n = V) { int ret = 0; while (bfs(S, T, n)) { memcpy(ptr, fst, sizeof(int) * n); for (int cur; (cur = dfs(S, T, INF)) > 0; ret += cur) ; } return ret; } } // namespace Dinic const int N = 5500; struct Edge { int u, v; Edge(int u = 0, int v = 0) : u(u), v(v) {} void in() { cin >> u >> v; } } E[N]; bool can(int mid, int n, int m) { Dinic::init(); int S = n + m + 1, T = S + 1; for (int i = 1; i <= m; i++) { Dinic::add_edge(S, i, 1); Dinic::add_edge(i, m + E[i].u, 1); Dinic::add_edge(i, m + E[i].v, 1); } for (int i = 1; i <= n; i++) Dinic::add_edge(m + i, T, mid); return Dinic::max_flow(S, T, T + 3) == m; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) E[i].in(); int st = 0, en = n, mid, ans; while (st <= en) { mid = st + en >> 1; if (can(mid, n, m)) ans = mid, en = mid - 1; else st = mid + 1; } cout << ans << "\n"; can(ans, n, m); for (int i = 1; i <= m; i++) { if (!Dinic::cap[6 * (i - 1) + 2]) cout << E[i].u << " " << E[i].v << "\n"; else cout << E[i].v << " " << E[i].u << "\n"; } return 0; }
16
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; int pol = 0, pr = 0; for (int i = 0; i < t; i++) { int x; cin >> x; if (pol == 0 and x < 0) pr++; else if (x > 0) pol += x; else if (pol > 0) pol--; } cout << pr; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(a, b)); } int count = 0; for (int i = 0; i < n; i++) { int flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0; for (int j = 0; j < n; j++) { if (v[i].first > v[j].first && v[i].second == v[j].second) { flag2 = 1; } else if (v[i].first < v[j].first && v[i].second == v[j].second) { flag1 = 1; } else if (v[i].first == v[j].first && v[i].second > v[j].second) { flag4 = 1; } else if (v[i].first == v[j].first && v[i].second < v[j].second) { flag3 = 1; } } if (flag1 && flag2 && flag3 && flag4) { count++; } } cout << count << "\n"; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf("%d%d", &n, &k); printf("%d\n", (6 * n - 1) * k); int i, j; for (i = 0; i < n; i++) { for (j = 1; j <= 3; j++) { int tmp = i * 6 + j; printf("%d ", tmp * k); } printf("%d\n", (i * 6 + 5) * k); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int read() { int n = 0; bool b = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') b = 1; c = getchar(); } while (isdigit(c)) { n = (n << 1) + (n << 3) + (c ^ 48); c = getchar(); } return b ? -n : n; } const int N = 307, mod = 1e9 + 7; int a[N], f[2][N][N], cnt, n; void div(int &x) { for (int i = 2; i <= sqrt(x); ++i) { while (!(x % (i * i))) x /= (i * i); } } int main() { n = read(); for (int i = 1; i <= n; ++i) a[i] = read(), div(a[i]); sort(a + 1, a + n + 1); f[0][0][0] = 1; int x = 0; for (int i = 1; i <= n; ++i) { if (a[i] != a[i - 1]) { for (int j = 0; j <= i; ++j) for (int k = 1; k <= cnt; ++k) { f[x ^ 1][j][0] = (f[x ^ 1][j][0] + f[x ^ 1][j][k] % mod) % mod; f[x ^ 1][j][k] = 0; } cnt = 0; } for (int j = 0; j <= i; ++j) for (int k = 0; k <= min(cnt, j); ++k) { if (j && k) f[x][j][k] = (0ll + f[x][j][k] + (1ll * f[x ^ 1][j - 1][k - 1] * (2 * cnt - k + 1) % mod) % mod) % mod; f[x][j][k] = (0ll + f[x][j][k] + (1ll * f[x ^ 1][j + 1][k] * (j + 1 - k) % mod)) % mod; f[x][j][k] = (0ll + f[x][j][k] + (1ll * f[x ^ 1][j][k] * (0ll + i - (2 * cnt - k) - (j - k)) % mod)) % mod; } x ^= 1; ++cnt; memset(f[x], 0, sizeof(f[x])); } printf("%d\n", f[x ^ 1][0][0]); }
9
CPP
n,m,k,l=map(int,input().split()) if n>m: print('First') else: print('Second')
7
PYTHON3
#https://codeforces.com/contest/1203/problem/F2 n, r = map(int, input().split()) arr = [list(map(int, input().split())) for _ in range(n)] def solve1(cur, arr): cnt=0 while len(arr) > 0: max_inc = -9999 choose = None for a, b in arr: if cur >= a and max_inc < b: max_inc = b choose = a if choose is None: flg=False break cnt+=1 cur+=max_inc arr.remove([choose, max_inc]) return cur, cnt arr1 = [[x, y] for x, y in arr if y >= 0] arr2 = [[x, y] for x, y in arr if y < 0] r, cnt = solve1(r, arr1) n = len(arr2) arr2 = [[]] + sorted(arr2, key=lambda x:x[0]+x[1], reverse=True) dp = [[-1] * (n+1) for _ in range(n+1)] for i in range(n+1): dp[i][0] = r for i in range(1, n+1): for j in range(1, i+1): dp[i][j] = dp[i-1][j] if dp[i-1][j-1] >= arr2[i][0] and dp[i-1][j-1] + arr2[i][1] >= 0: dp[i][j] = max(dp[i][j], dp[i-1][j-1]+arr2[i][1]) ans = 0 for j in range(n+1): if dp[n][j] >= 0: ans = j print(ans+cnt) #3 4 #4 6 #10 -2 #8 -1
12
PYTHON3
#include<iostream> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; cout<<(a+b)*c/2<<'\n'; }
0
CPP
#include <bits/stdc++.h> using namespace std; int n, m, dp[200010], s[200010], x[200010]; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) dp[i] = i; for (int i = 1; i <= n; i++) cin >> x[i] >> s[i]; for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) { if (i >= x[j]) dp[i] = min(dp[i], dp[max(2 * x[j] - i - 1, 0)] + max(i - x[j] - s[j], 0)); else dp[i] = min(dp[i], dp[max(x[j] - s[j] - 1, 0)]); } cout << dp[m] << endl; return 0; }
11
CPP
for _ in range(int(input())): n=int(input()) s=input();t=0;m=0;n=0 for i in s: if i=="A": t=max(m,t) n=1 m=0 else: if n>0: m+=1 t=max(t,m) print(t)
7
PYTHON3
from math import ceil n1,m1,a1 = map(int,input().split()) k1 = ceil(n1/a1) l = ceil(m1/a1) print(int(k1*l))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, a[101010], ans = 0; signed 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]; ans = 2 * n - 1 + a[0]; for (long long i = 1; i < n; ++i) { ans += abs(a[i] - a[i - 1]); } cout << ans << endl; return 0; }
8
CPP
#include <bits/stdc++.h> const long long MAX = 2e5; using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; vector<long long> vet(n); for (auto &i : vet) cin >> i; long long r = n - 1; long long disp = 0; long long ans = 0; while (r >= 0) { if (disp <= 0) ans++; if (vet[r] + 1 > disp) disp = vet[r] + 1; disp--; r--; } cout << ans << '\n'; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; const int maxm = 1e9 + 7; int n, m, a[maxn], cnt[32]; void cal(int x) { for (int i = (0); i <= (31); ++i) { if ((x >> i) & 1) cnt[i]++; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { memset(cnt, 0, sizeof(cnt)); cin >> n; for (int i = (1); i <= (n); ++i) { cin >> a[i]; cal(a[i]); } int flag = 0; for (int i = 31; i >= 0; i--) { if (cnt[i] % 2) { if (cnt[i] % 4 == 1) flag = 1; else if (cnt[i] % 4 == 3 && n % 2 == 0) flag = 1; else flag = -1; break; } } if (flag == 1) cout << "WIN" << '\n'; else if (flag == -1) cout << "LOSE" << '\n'; else cout << "DRAW" << '\n'; } }
8
CPP
li = [int(i) for i in input().split()] c3 = li[1] li.remove(c3) k = min(li) tot = 256 * k c2 = li[0] - k m = min(c2,c3) tot = tot + 32*m print(tot)
8
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") #pragma GCC target("sse4.2") using namespace std; mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count()); template <class T, class U> inline bool chmax(T &x, U y) { return x < y ? x = y, 1 : 0; } template <class T, class U> inline bool chmin(T &x, U y) { return x > y ? x = y, 1 : 0; } template <class T> inline void sort(T &a) { sort((a).begin(), (a).end()); } template <class T> inline void rsort(T &a) { sort((a).rbegin(), (a).rend()); } template <class T> inline void reverse(T &a) { reverse((a).begin(), (a).end()); } template <class T, class U> inline istream &operator>>(istream &str, pair<T, U> &p) { return str >> p.first >> p.second; } template <class T> inline istream &operator>>(istream &str, vector<T> &a) { for (auto &i : a) str >> i; return str; } template <class T> inline T sorted(T a) { sort(a); return a; } void read() {} void print() {} void println() { cout << '\n'; } template <class T, class... U> void read(T &x, U &...u) { cin >> x; read(u...); } template <class T, class... U> void print(const T &x, const U &...u) { cout << x; print(u...); } template <class T, class... U> void println(const T &x, const U &...u) { cout << x; println(u...); } template <class F> struct Dinic { struct Edge { int v, rev; F flow; Edge() {} Edge(int _v, F _cap, F _flow, int _rev) : v(_v), flow(_flow - _cap), rev(_rev) {} inline bool sat() const { return flow >= 0; } inline bool unsat() const { return flow < 0; } inline F left() const { return -flow; } inline void addFlow(F x) { flow += x; } }; vector<vector<Edge>> G; vector<int> layer, ptr; int n, second, t; Dinic(int _n) : n(_n) { G.resize(n); layer.resize(n); } void addEdge(int u, int v, F c) { int pu = signed((G[u]).size()), pv = signed((G[v]).size()); G[u].push_back({v, c, 0, pv}); G[v].push_back({u, 0, 0, pu}); } bool buildBfsTree() { for (int i = 0; i < n; ++i) layer[i] = -1; queue<int> que({second}); layer[second] = 0; while (signed((que).size())) { auto first = que.front(); que.pop(); if (first == t) break; for (auto i : G[first]) if (layer[i.v] == -1 && i.unsat()) { layer[i.v] = layer[first] + 1; que.push(i.v); } } return layer[t] >= 0; } F sendFlow(int v, F first) { if (v == t) return first; for (; ptr[v] < signed((G[v]).size()); ++ptr[v]) { Edge &e = G[v][ptr[v]]; if (layer[e.v] != layer[v] + 1 || e.sat()) continue; auto d = sendFlow(e.v, min(first, e.left())); if (d > 0) { e.addFlow(d); G[e.v][e.rev].addFlow(-d); return d; } } return 0; } F findFlow(int _s, int _t) { second = _s, t = _t; F ans = 0; while (buildBfsTree()) { ptr.assign(n, 0); while (F df = sendFlow(second, numeric_limits<F>::max())) ans += df; } return ans; } }; int n, m; int get(int i, int j, int ni, int nj) { return (min(i, ni) * m + min(j, nj)) * 2 + (j != nj); } bool ok(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(12); read(n, m); vector<string> in(n); read(in); int sz = 2 * n * m + 2; int second = sz - 2, t = sz - 1; Dinic<int> fl(sz); vector<pair<int, int>> d = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}; int cnt = 0, vert = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (in[i][j] == '#') { ++cnt; for (auto d : d) { int ni = i + d.first, nj = j + d.second; if ((ni > i || nj > j) && ok(ni, nj) && in[ni][nj] == '#') { int ind = get(i, j, ni, nj); if (ind & 1) fl.addEdge(ind, t, 1); else fl.addEdge(second, ind, 1); ++vert; } } for (int x = 0; x < 4; ++x) { int y = x + 1 & 3; int i1 = i + d[x].first, j1 = j + d[x].second, i2 = i + d[y].first, j2 = j + d[y].second; if (ok(i1, j1) && ok(i2, j2) && in[i1][j1] == '#' && in[i2][j2] == '#') { int ind1 = get(i, j, i1, j1), ind2 = get(i, j, i2, j2); assert(ind1 % 2 + ind2 % 2 == 1); if (ind1 & 1) swap(ind1, ind2); fl.addEdge(ind1, ind2, 1); } } } println(cnt - (vert - fl.findFlow(second, t))); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, p; string s; int main() { ios_base::sync_with_stdio(false); cin >> n >> p; cin >> s; s[n - 1]++; int i = n - 1; while (i < n && i >= 0) { if (s[i] - 'a' == p) { s[i] = 'a'; --i; if (i >= 0) s[i]++; } else { if (!((i > 0 && s[i] == s[i - 1]) || (i > 1 && s[i] == s[i - 2]))) i++; else s[i]++; } } if (i < 0) cout << "NO\n"; else cout << s << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { int N, Q; string S; cin >> N >> S >> Q; vector<set<int>> V(26); for (int i = 0; i < N; i++) V.at(S.at(i) - 'a').insert(i + 1); while (Q--) { int type; cin >> type; if (type == 1) { int i; char c; cin >> i >> c; V.at(S.at(i - 1) - 'a').erase(i); S.at(i - 1) = c; V.at(S.at(i - 1) - 'a').insert(i); } else { int l, r; cin >> l >> r; int ans = 0; for (int i = 0; i < 26; i++) { auto iter = V.at(i).lower_bound(l); if (iter != V.at(i).end() && *iter <= r) ans++; } cout << ans << "\n"; } } }
0
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2") #pragma GCC optimize("unroll-loops") using namespace std; const unsigned _mod = 998244353; const unsigned mod = 1e9 + 7; const long long infi = 0x3f3f3f3f3f3f3fll; const int inf = 0x3f3f3f3f; void rd(int &x) { x = 0; int f = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == 45) f = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar(); x *= f; } long long ksm(long long x, long long y = mod - 2, long long m = mod) { long long ret = 1; while (y) { if (y & 1) ret = ret * x % m; y >>= 1ll; x = x * x % m; } return ret; } long long qpow(long long x, long long y = 2) { long long ret = 1; while (y > 0) { if (y & 1ll) ret = ret * x; y >>= 1ll; x = x * x; } return ret; } int n, r[50010][6], mx[6]; int main() { int T; cin >> T; while (T--) { int pos = -1; cin >> n; for (int i = (1); i <= (n); ++i) for (int j = (1); j <= (5); ++j) rd(r[i][j]); for (int i = (1); i <= (5); ++i) mx[i] = inf; for (int i = (1); i <= (n); ++i) { int cnt = 0; for (int j = (1); j <= (5); ++j) if (r[i][j] < mx[j]) cnt++; if (cnt >= 3) { for (int j = (1); j <= (5); ++j) mx[j] = r[i][j]; pos = i; } } for (int i = (1); i <= (n); ++i) if (i != pos) { int cnt = 0; for (int j = (1); j <= (5); ++j) if (r[i][j] > mx[j]) cnt++; if (cnt < 3) { pos = -1; break; } } cout << pos << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acosl(-1); const double eps = 1e-18; struct point { double first, second; point(double first = 0, double second = 0) : first(first), second(second) {} point operator-(point a) { return point(first - a.first, second - a.second); } point operator+(point a) { return point(first + a.first, second + a.second); } point operator*(double a) { return point(first * a, second * a); } point operator/(double a) { return point(first / a, second / a); } point operator&(point a) { return point(first * a.first - second * a.second, first * a.second + second * a.first); } point rotate(double al) { return *this & point(cosl(al), sinl(al)); } double operator^(point a) { return first * a.second - second * a.first; } double operator*(point a) { return first * a.first + second * a.second; } point norm() { double s = nor(); if (s < eps) return *this; return point(first / s, second / s); } point conj() { return point(-second, first); } double nor() { return sqrt(*this * *this); } double al() { return atan2(second, first); } bool operator<(const point &a) const { if (fabs(first - a.first) > eps) return first < a.first; return second < a.second - eps; } bool operator>(const point &a) const { if (fabs(first - a.first) > eps) return first > a.first; return second > a.second + eps; } bool operator==(const point &a) const { if (fabs(first - a.first) > eps) return 0; return fabs(second - a.second) < eps; } void input() { scanf("%lf %lf", &first, &second); } void print() { printf("%.10lf %.10lf\n", first, second); } int sgn(double a) { return (a > eps) - (a < -eps); } bool intersect(point a, point b, point c, point d) { if (min(a.first, b.first) > max(c.first, d.first) + eps) return 0; if (min(a.second, b.second) > max(c.second, d.second) + eps) return 0; if (max(a.first, b.first) < min(c.first, d.first) - eps) return 0; if (max(a.second, b.second) < min(c.second, d.second) - eps) return 0; if (sgn(a - b ^ c - b) * sgn(a - b ^ d - b) > 0) return 0; if (sgn(c - d ^ a - d) * sgn(c - d ^ b - d) > 0) return 0; return 1; } }; void solve() { int n; cin >> n; cout << n * 4 + 1 << endl; double alpha = pi / 5; point z; double len = cosl(alpha) * 10 * 2; point a = point(10, 0); point b = a.rotate(alpha); point c = point(len, 0); point d = c.rotate(-alpha - eps); point e = a.rotate(-2 * alpha); for (int i = 0; i < n; i++) { a = point(len * i, 0); a.print(); a = d; a.first += len * i; a.print(); a = b; a.first += len * i; a.print(); a = e; a.first += len * i; a.print(); } a = point(len * n, 0); a.print(); for (int i = 0; i < n; i++) { printf("%d %d %d %d %d\n", 1 + 4 * i, 3 + 4 * i, 5 + 4 * i, 2 + 4 * i, 4 + 4 * i); } for (int i = 1; i <= 4 * n; i++) printf("%d ", i); for (int i = 4 * n + 1; i > 0; i -= 4) printf("%d ", i); cout << endl; } int main() { solve(); return 0; }
10
CPP
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int cb[2][100007]; char ca[100007]; bool s[100007]; int main(){ int n,m,q; int i,j,k; int a,b,c,d; for(i=0;i<2;i++){ scanf("%s",ca); a=strlen(ca); for(j=0;j<a;j++){ cb[i][j+1]=cb[i][j]; if(ca[j]=='A')cb[i][j+1]++; else cb[i][j+1]+=2; } } cin>>q; for(i=0;i<q;i++){ scanf("%d%d%d%d",&a,&b,&c,&d); a--,c--; a=(cb[0][b]-cb[0][a])%3; b=(cb[1][d]-cb[1][c])%3; if(a==b)s[i]=true; else s[i]=false; } for(i=0;i<q;i++){ if(s[i])printf("YES\n"); else printf("NO\n"); } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; const int MOD = 998244353; void Add(int& a, int b) { a += b; a -= a >= MOD ? MOD : 0; } int Mul(int a, int b) { return 1ll * a * b % MOD; } int f[N]; int dp[N][2][2]; int Calc(int l, int first, int k) { if (l == 1) return first == k; if (l == 2) return first != k; if (dp[l][first][k] != -1) return dp[l][first][k]; int small = (l - 1) / 2, lim1 = f[small], lim2 = lim1 * 2 + 1; int& ret = dp[l][first][k] = 0; while (true) { int mid1 = small, mid2 = l - 1 - small; if ((mid1 + first) % 2 == k) { if (Calc(mid1, first, 1 - k) && Calc(l - 1 - mid1, 1 - k, k)) { ret = 1; break; } } if (mid2 != mid1 && (mid2 + first) % 2 == k) if (Calc(mid2, first, 1 - k) && Calc(l - 1 - mid2, 1 - k, k)) { ret = 1; break; } break; } return ret; } int main() { int n; cin >> n; int it = 0; for (int i = 1; i <= n; ++i) { while (((1 << (it + 1)) - 1) <= i) ++it; f[i] = (1 << it) - 1; } memset(dp, -1, sizeof dp); int x = (Calc(n, 1, 0) + Calc(n, 1, 1)) % MOD; cout << x << '\n'; }
11
CPP
#include <bits/stdc++.h> using namespace std; void _io() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } const int MAXN = 200005; const long long mod = 1e9 + 7; long long inp1, inp2, inp3; long long T, x, y; long long p[MAXN], num[MAXN], top; namespace Comb { const int maxc = 2000000 + 5; int f[maxc], inv[maxc], finv[maxc]; void init() { inv[1] = 1; for (int i = 2; i < maxc; i++) inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod; f[0] = finv[0] = 1; for (int i = 1; i < maxc; i++) { f[i] = f[i - 1] * 1ll * i % mod; finv[i] = finv[i - 1] * 1ll * inv[i] % mod; } } int C(int n, int m) { if (m < 0 || m > n) return 0; return f[n] * 1ll * finv[n - m] % mod * finv[m] % mod; } int S(int n, int m) { if (n == 0 && m == 0) return 1; return C(m + n - 1, n - 1); } } // namespace Comb using Comb::C; long long qpow(long long a, long long n) { long long ret = 1; while (n) { if (n & 1) ret = (ret * a) % mod; n >>= 1; a = (a * a) % mod; } return ret; } int main() { Comb::init(); long long ans = 0; cin >> T; while (T--) { long long ans = 1; top = 0; cin >> x >> y; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) { p[++top] = i; num[top] = 0; while (x % i == 0) { num[top]++; x /= i; } } } if (x > 1) { p[++top] = x; num[top] = 1; } for (long long i = 1; i <= top; i++) { ans = (ans * C(y + num[i] - 1, y - 1)) % mod; } ans = (ans * qpow(2, y - 1)) % mod; cout << ans << endl; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; string l1, l2; map<char, map<char, int> > dp; bool a(int i, int j) { return l1[i] == '0' && l2[i] == '0' && l2[j] == '0'; } bool b(int i, int j) { return l1[i] == '0' && l1[j] == '0' && l2[j] == '0'; } bool c(int i, int j) { return l1[i] == '0' && l2[i] == '0' && l1[j] == '0'; } bool d(int i, int j) { return l1[j] == '0' && l2[i] == '0' && l2[j] == '0'; } int number(int i, int ans) { char q1, q2; q1 = l1[i]; q2 = l2[i]; if (dp[q1][q2]) return dp[q1][q2]; if (i == l1.size() - 1) return ans; int aa = 0, bb = 0, cc = 0, dd = 0, rr = 0; int j = i + 1; if (a(i, i + 1)) { l2[j] = 'X'; aa = number(i + 1, ans + 1); l2[j] = '0'; } if (b(i, i + 1)) { l1[j] = 'X'; l2[j] = 'X'; bb = number(i + 1, ans + 1); l1[j] = '0'; l2[j] = '0'; } if (c(i, i + 1)) { l1[j] = 'X'; cc = number(i + 1, ans + 1); l1[j] = '0'; } if (d(i, i + 1)) { l1[j] = 'X'; l2[j] = 'X'; dd = number(i + 1, ans + 1); l1[j] = '0'; l2[j] = '0'; } rr = number(i + 1, ans); aa = max(rr, aa); aa = max(max(aa, bb), max(cc, dd)); dp[q1][q2] = aa; return aa; } int main() { cin >> l1 >> l2; int n = l1.size(); cout << number(0, 0); return 0; }
10
CPP
n=int(input()) l=input() j=l.count("A") k=l.count("D") if j>k: print("Anton") elif k>j: print("Danik") elif k==j: print("Friendship")
7
PYTHON3
#include <bits/stdc++.h> int dx4[] = {0, 0, -1, 1}; int dy4[] = {-1, 1, 0, 0}; bool valid(int r, int c, int x, int y) { if (x >= 1 && x <= r && y >= 1 && y <= c) return 1; return 0; } using namespace std; int ar[200005]; int main() { cout.flush(); int t; cin >> t; for (int cs = 1; cs <= t; cs++) { int n, k; cin >> n >> k; vector<int> v[k + 2]; int p, q; for (int i = 0; i < k; i++) { cin >> p; for (int j = 0; j < p; j++) { cin >> q; v[i].push_back(q); } } int val, val1; cout << "? " << n << " "; for (int i = 1; i <= n; i++) cout << i << " "; cout << endl; cin >> val; int low = 0; int high = k - 1; int mid; int pos = 0, m; while (low < high) { mid = (low + high) / 2; vector<int> x; for (int i = 0; i <= mid; i++) { for (int j = 0; j < v[i].size(); j++) { x.push_back(v[i][j]); } } if (x.size()) { cout << "? " << x.size() << " "; for (int i = 0; i < x.size(); i++) cout << x[i] << " "; cout << endl; cin >> m; } if (m < val) { pos = mid + 1; low = mid + 1; } else high = mid; } vector<int> vec; for (int i = 0; i < v[pos].size(); i++) { ar[v[pos][i]]++; } for (int i = 1; i <= n; i++) { if (ar[i] == 0) vec.push_back(i); } if (vec.size()) { cout << "? " << vec.size() << " "; for (int i = 0; i < vec.size(); i++) cout << vec[i] << " "; cout << endl; cin >> val1; } cout << "! "; for (int i = 0; i < k; i++) { if (i == pos) cout << val1 << " "; else cout << val << " "; } cout << endl; string s; cin >> s; memset(ar, 0, sizeof(ar)); } return 0; }
10
CPP
#include <bits/stdc++.h> const int MOD = 1000000007; const int N = 2005; const int M = 8050; int n, k, num[N], dp[N][M][2], vis[N][M][2]; int solve(int len, int s, int flag) { if (s >= (1 << k)) flag = 1; if (vis[len][s][flag]) return dp[len][s][flag]; vis[len][s][flag] = 1; dp[len][s][flag] = 0; if (len == n) { return dp[len][s][flag] = flag; } if (num[len] == 0) { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, s + 2, flag)) % MOD; if ((s & (1 << 1)) == 0) { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, s + 4, flag)) % MOD; } else { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, 4, flag)) % MOD; } } else { if (num[len] == 2) dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, s + num[len], flag)) % MOD; else { if ((s & (1 << 1)) == 0) { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, s + num[len], flag)) % MOD; } else { dp[len][s][flag] = (dp[len][s][flag] + solve(len + 1, num[len], flag)) % MOD; } } } return dp[len][s][flag]; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &num[i]); printf("%d\n", solve(0, 0, 0)); return 0; }
10
CPP
X = int(input()) Temp = bin(X)[2:] Temp = "0" * (6 - len(Temp)) + Temp print(int(Temp[0] + Temp[-1] + Temp[-3] + Temp[2] + Temp[-2] + Temp[1], 2)) # Hope the best for Ravens member
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e5 + 7; const int MOD = (int)1e9 + 7; int n; long long moves; int a[MAXN]; map<int, int> cnt; int main() { cin >> n >> moves; long long sum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; cnt[a[i]]++; } if (sum < moves) { cout << -1; return 0; } long long dead = 0; long long last = 0; for (auto it : cnt) { long long alive = n - dead; long long need = it.first - last; if (alive * need <= moves) { moves -= need * alive; } else { vector<int> all; for (int i = 1; i <= n; i++) { if (a[i] >= it.first) { all.push_back(i); } } bool can = (moves >= alive * (need - 1)); moves %= alive; for (int i = moves; i < all.size(); i++) cout << all[i] << ' '; for (int i = 0; i < moves; i++) { if (a[all[i]] == it.first && can) continue; cout << all[i] << ' '; } return 0; } dead += it.second; last = it.first; } return 0; }
8
CPP