solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
s=input() p=input() s=s+s a=s.find(p) if a==-1: print('No') else: print('Yes')
0
PYTHON3
n=int(input()) m=int(input()) if n>m//2: print(m) else: r=1 a=2 while n>0: if n%2: r=r*a n=n-1 else: a=a*a n=n//2 print(m%r)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; using ld = long double; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; const int MX = 3 * 1e5; char cad[MX + 2]; int puntos; int num_componentes; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; for (int i = 1; i < n + 1; ++i) cin >> cad[i]; for (int i = 1; i < n + 1; ++i) { if (cad[i] == '.') { puntos++; if (cad[i + 1] != '.') num_componentes++; } else { cad[i] = '#'; } } cad[0] = '#'; cad[n + 1] = '#'; for (int kk = 0; kk < m; ++kk) { int idx; cin >> idx; char c; cin >> c; if (c != '.') c = '#'; if (cad[idx] != c) { if (cad[idx] == '.') { puntos--; if (cad[idx - 1] == cad[idx + 1]) { if (cad[idx + 1] == '.') { num_componentes++; } else { num_componentes--; } } } else { puntos++; if (cad[idx - 1] == cad[idx + 1]) { if (cad[idx + 1] == '.') { num_componentes--; } else { num_componentes++; } } } cad[idx] = c; } cout << puntos - num_componentes << '\n'; } return 0; }
9
CPP
n = int(input()) line = [int(k) for k in input().split()] if line[0]%2-line[1]%2 == 0: for i in range(2,n): if line[i]%2 != line[0]%2: print(i+1) break else: if line[0]%2 == line[2]%2: print(2) else: print(1)
7
PYTHON3
currPos = input() targetPos = input() step = [] currCol = currPos[0] currRow = currPos[1] while True: if (ord(currCol) < ord(targetPos[0]) and ord(currRow) < ord(targetPos[1])): step.append("RU") currCol = chr(ord(currCol)+1) currRow = chr(ord(currRow)+1) elif (ord(currCol) < ord(targetPos[0]) and ord(currRow) == ord(targetPos[1])): step.append("R") currCol = chr(ord(currCol)+1) elif (ord(currCol) < ord(targetPos[0]) and ord(currRow) > ord(targetPos[1])): step.append("RD") currCol = chr(ord(currCol)+1) currRow = chr(ord(currRow)-1) elif (ord(currCol) == ord(targetPos[0]) and ord(currRow) > ord(targetPos[1])): step.append("D") currRow = chr(ord(currRow)-1) elif (ord(currCol) > ord(targetPos[0]) and ord(currRow) > ord(targetPos[1])): step.append("LD") currCol = chr(ord(currCol)-1) currRow = chr(ord(currRow)-1) elif (ord(currCol) > ord(targetPos[0]) and ord(currRow) == ord(targetPos[1])): step.append("L") currCol = chr(ord(currCol)-1) elif (ord(currCol) > ord(targetPos[0]) and ord(currRow) < ord(targetPos[1])): step.append("LU") currCol = chr(ord(currCol)-1) currRow = chr(ord(currRow)+1) elif (ord(currCol) == ord(targetPos[0]) and ord(currRow) < ord(targetPos[1])): step.append("U") currRow = chr(ord(currRow)+1) if (ord(currCol) == ord(targetPos[0]) and ord(currRow) == ord(targetPos[1])): break print(len(step)) for element in step: print(element)
7
PYTHON3
#include <algorithm> #include <iostream> #include <queue> #include <vector> using namespace std; static const int NMAX = 100000; static const int WMAX = 1000; int N; struct Edge { int target, weight; }; vector<Edge> G[NMAX]; bool visited[NMAX]; int weights[NMAX]; void clear() { for (int i = 0; i < N; i++) { visited[i] = false; weights[i] = 0; } } void visit(int n, int w) { visited[n] = true; weights[n] = w; } int bfs(int n, int *maxn_store) { clear(); queue<int> q; q.push(n); visit(n, 0); int maxw = 0; int max_node = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : G[u]) { if (!visited[v.target]) { int w = weights[u] + v.weight; if (w > maxw) { maxw = w; max_node = v.target; } visit(v.target, w); q.push(v.target); } } } *maxn_store = max_node; return maxw; } int main() { cin >> N; for (int i = 0; i < N - 1; i++) { int s, t, w; cin >> s >> t >> w; G[s].push_back(Edge{t, w}); G[t].push_back(Edge{s, w}); } int maxn; bfs(0, &maxn); cout << bfs(maxn, &maxn) << endl; }
0
CPP
#D - Hit the Lottery n = int(input()) #1, 5, 10, 20, 100 notas = 0 if n >= 100: notas += n // 100 n = n - notas * 100 if n >= 20: notas += n // 20 n = n - (n // 20) * 20 if n >= 10: notas += n // 10 n = n - (n // 10) * 10 if n >= 5: notas += n // 5 n = n - (n // 5) * 5 if n >= 1: notas += n // 1 print(notas)
7
PYTHON3
N = int(input()) dp = [[0] * 3 for _ in range(N + 1)] for i in range(N): a, b, c = list(map(int, input().split())) dp[i+1][0] = a + max(dp[i][1:]) dp[i+1][1] = b + max(dp[i][::2]) dp[i+1][2] = c + max(dp[i][:2]) print(max(dp[N]))
0
PYTHON3
import sys,math input=sys.stdin.readline L=lambda : list(map(int,input().split())) M=lambda : map(int,input().split()) n=int(input()) l=L() l=[0]+l d=0 for i in range(1,n+1): if(l[l[l[l[i]]]]==l[i]): print("YES") d=1 break if(d==0): print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 101010; int N, digits[MAXN]; int main() { scanf("%d", &N); int dsum = 0; for (int i = 0; i < N; i++) { scanf("%d", &digits[i]); dsum += digits[i]; } sort(digits, digits + N); if (digits[0] != 0) { printf("-1\n"); return 0; } if (dsum % 3 != 0) { bool good = false; for (int i = 0; i < N; i++) if (dsum % 3 == digits[i] % 3) { digits[i] = -1; good = true; break; } if (!good) { int targ = 3 - (dsum % 3); int k = 0; for (int i = 0; i < N && k < 2; i++) if (targ == digits[i] % 3) { digits[i] = -1; k++; } } } int high_pos = N - 1; while (high_pos >= 0 && digits[high_pos] == -1) high_pos--; if (high_pos < 0) { printf("-1\n"); return 0; } else if (digits[high_pos] == 0) { printf("0\n"); return 0; } for (int i = N - 1; i >= 0; i--) if (digits[i] != -1) printf("%d", digits[i]); printf("\n"); return 0; }
8
CPP
s = input().split() l = int(s[0]) r = int(s[1]) k = int(s[2]) ans = '' fl = False for i in range(0, 1000): if k ** i >= l and k ** i <= r: ans += str(k ** i) + ' ' if len(ans) == 0: print(-1) else: print(ans)
7
PYTHON3
import sys readline = sys.stdin.readline sys.setrecursionlimit(10**8) mod = 10**9+7 #mod = 998244353 INF = 10**18 eps = 10**-7 m,n,k = map(int,readline().split()) def comb(n, r, mod): r = min(r, n-r) mol = 1 deno = 1 for i in range(1, r+1): mol = mol * (n-r+i) % mod deno = deno * i % mod ret = mol * pow(deno, mod-2, mod) % mod return ret ans = m*n*(m+n)*(m*n-1)//3 ans = (ans * comb(m*n-2,k-2,mod))%mod ans = ans*pow(2,mod-2,mod)%mod print(ans)
0
PYTHON3
n = int(input()) s = ["I hate","I love"] kmp = "that" i = 0 while i<n: if i==n-1: kmp = "it" print(s[i%2],kmp,end=' ') i+=1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { string s; cin >> s; long long int l = 0, r = 0, u = 0, d = 0; for (int a = 0; a < s.size(); a++) { if (s[a] == 'L') l++; else if (s[a] == 'R') r++; else if (s[a] == 'U') u++; else d++; } l = min(l, r); d = min(d, u); r = l; u = d; if (l == 0 && d == 0) { cout << 0 << "\n"; cout << "\n"; continue; } else if (l == 0 || d == 0) { cout << 2 << "\n"; if (l > 0) cout << 'R'; if (d > 0) cout << 'D'; if (l > 0) cout << 'L'; if (d > 0) cout << 'U'; cout << "\n"; } else { cout << 2 * l + 2 * d << "\n"; for (int a = 0; a < l; a++) cout << 'R'; for (int a = 0; a < d; a++) cout << 'D'; for (int a = 0; a < l; a++) cout << 'L'; for (int a = 0; a < u; a++) cout << 'U'; cout << "\n"; } } return 0; }
8
CPP
#pyrival orz import os import sys import math from io import BytesIO, IOBase input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) ############ ---- Dijkstra with path ---- ############ def dijkstra(start, distance, path, n): # requires n == number of vertices in graph, # adj == adjacency list with weight of graph visited = [False for _ in range(n)] # To keep track of vertices that are visited distance[start] = 0 # distance of start node from itself is 0 for i in range(n): v = -1 # Initialize v == vertex from which its neighboring vertices' distance will be calculated for j in range(n): # If it has not been visited and has the lowest distance from start if not visited[v] and (v == -1 or distance[j] < distance[v]): v = j if distance[v] == math.inf: break visited[v] = True # Mark as visited for edge in adj[v]: destination = edge[0] # Neighbor of the vertex weight = edge[1] # Its corresponding weight if distance[v] + weight < distance[destination]: # If its distance is less than the stored distance distance[destination] = distance[v] + weight # Update the distance path[destination] = v # Update the path def gcd(a, b): if b == 0: return a else: return gcd(b, a%b) def lcm(a, b): return (a*b)//gcd(a, b) def ncr(n, r): return math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) def npr(n, r): return math.factorial(n)//math.factorial(n-r) def seive(n): primes = [True]*(n+1) ans = [] for i in range(2, n): if not primes[i]: continue j = 2*i while j <= n: primes[j] = False j += i for p in range(2, n+1): if primes[p]: ans += [p] return ans def factors(n): factors = [] x = 1 while x*x <= n: if n % x == 0: if n // x == x: factors.append(x) else: factors.append(x) factors.append(n//x) x += 1 return factors # Functions: list of factors, seive of primes, gcd of two numbers, # lcm of two numbers, npr, ncr def main(): try: for _ in range(inp()): n = inp() a = inlt() print(*sorted(a, reverse = True), sep = " ") except Exception as e: print(e) # region fastio BUFSIZE = 8192 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") # endregion if __name__ == "__main__": main()
8
PYTHON3
n = int(input()) current_capacity = 0 min_capacity = 0 for i in range(n): go_out, go_in = map(int, input().split()) current_capacity += go_in - go_out if current_capacity > min_capacity: min_capacity = current_capacity print(min_capacity)
7
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int mod = 998244353; int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } int pw(int a, int b) { if (b == 0) return 1; if (b & 1) return mult(a, pw(a, b - 1)); int res = pw(a, b / 2); return mult(res, res); } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } const int maxN = 2005; int dp[maxN][maxN]; string s; int tp[maxN]; int dp0[maxN][maxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> s; dp[0][0] = 1; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') tp[i + 1] = 1; else if (s[i] == ')') tp[i + 1] = 0; else tp[i + 1] = 2; } int n = s.size(); for (int i = 1; i <= n; i++) { if (tp[i] == 1) { for (int cnt = 1; cnt <= i; cnt++) { dp[i][cnt] = dp[i - 1][cnt - 1]; } } else if (tp[i] == 0) { for (int cnt = 0; cnt <= i; cnt++) { dp[i][cnt] = dp[i - 1][cnt]; } } else { for (int cnt = 0; cnt <= i; cnt++) { dp[i][cnt] = dp[i - 1][cnt]; if (cnt) dp[i][cnt] = sum(dp[i][cnt], dp[i - 1][cnt - 1]); } } } dp0[n + 1][0] = 1; for (int i = n; i >= 1; i--) { if (tp[i] == 0 || tp[i] == 2) { for (int cnt = 1; cnt <= n - i + 2; cnt++) { dp0[i][cnt] = sum(dp0[i + 1][cnt - 1], dp0[i][cnt]); } } if (tp[i] == 1 || tp[i] == 2) { for (int cnt = 0; cnt <= n - i + 2; cnt++) { dp0[i][cnt] = sum(dp0[i + 1][cnt], dp0[i][cnt]); } } } int val = 0; for (int pref = 1; pref <= n; pref++) { int pref_sum = 0; for (int cnt = n; cnt >= 1; cnt--) { pref_sum = sum(pref_sum, dp0[pref + 1][cnt]); if (pref_sum == 0) continue; if (tp[pref] == 0) continue; val = sum(val, mult(pref_sum, dp[pref - 1][cnt - 1])); } } cout << val << '\n'; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; class JuRuo { bool sb; int neg_IQ; } LHM; long long rev(string s) { for (int i = 0, j = int((s).size()) - 1; i < j; i++, j--) swap(s[i], s[j]); stringstream cc(s); long long c; cc >> c; return c; } int main() { long long a, b; string c; cin >> a >> c; cout << a + rev(c) << endl; return 0; }
7
CPP
n=int(input()) if n % 2 == 0: print('NO') else: print('YES') k=0 N=2*n output=[' ']*N while k<N: output[k]=str(N-k) output[(k+n) % n]=str(N-k-1) output[(k+1+n) % n]=str(k+2) output[k+1]=str(k+1) k=k+2 print(' '.join(output))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, guests; cin >> n >> guests; int tools[101] = {0}; int max = 0; for (int i = 0; i < n; i++) { int cur; cin >> cur; tools[cur]++; if (tools[cur] > max) { max = tools[cur]; } } int tmp = max % guests; max -= tmp; if (tmp > 0) max += guests; int missing = 0; for (int i = 1; i < 101; i++) { if (tools[i]) missing += max - tools[i]; assert(tools[i] >= 0); } cout << missing; return 0; }
7
CPP
for _ in range(0, int(input())): n = int(input()) cnt = 0 y = str(bin(n)[2:]) for i in range (len(y)): if y[i] == '1': #print("") cnt += 1 print( n*2 - cnt )
9
PYTHON3
if __name__ == "__main__": s = input() print(s[:4] + ' ' + s[4:])
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int k, t; long long C[20][20]; inline long long calc(int w, vector<int>& v) { static long long f[20]; memset(f, 0, sizeof f); *f = 1; for (int x : v) { static long long g[20]; memset(g, 0, sizeof g); for (int d = 0; d <= x; ++d) for (int i = w; i >= d; --i) g[i] += f[i - d] * C[w - i + d][d]; memcpy(f, g, sizeof f); } return f[w]; } string solve(int w, int k, vector<int>& v, bool flag = 0) { if (!w) return ""; for (int s = flag; s < 16; ++s) if (v[s]) { --v[s]; long long tot = calc(w - 1, v); if (tot >= k) { char vs = s < 10 ? (s ^ '0') : (s - 10 + 'a'); return vs + solve(w - 1, k, v); } k -= tot; ++v[s]; } exit(233); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> k >> t; **C = 1; for (int i = 1; i < 20; ++i) { C[i][0] = 1; for (int j = 1; j <= i; ++j) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; } vector<int> vc; for (int i = 0; i < 16; ++i) vc.push_back(t); for (int w = 1;; ++w) { --vc[1]; long long tot = calc(w - 1, vc) * 15; ++vc[1]; if (tot >= k) { cout << solve(w, k, vc, 1) << '\n'; return 0; } k -= tot; } return 114514; }
12
CPP
import math a,b,C=map(int,input().split()) s=(a*b*math.sin(math.radians(C)))/2 c=(a**2+b**2-2*a*b*math.cos(math.radians(C)))**(1/2) h=s*2/a print(f'{s:4f}') print(f'{a+b+c:4f}') print(f'{h:4f}')
0
PYTHON3
s=list(map(int,input().split("+"))) s.sort() print("+".join(map(str,s)))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; bool is_prime(long long n) { for (long long i = 2; i <= sqrt(n); ++i) if (n % i == 0) return false; return true; } int main() { long long n; cin >> n; if (is_prime(n)) cout << 1; else { long long prim; for (long long i = 2; i < sqrt(n) + 1; ++i) { if ((n % i) == 0) { prim = i; break; } } cout << 1 + (n - prim) / 2; } return 0; }
8
CPP
a = int(input()) genome = input() n = 0 z = 0 for nucl in genome: if nucl == 'n': n += 1 for nucl in genome: if nucl == 'z': z += 1 print('1 ' * n +'0 ' * z)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; string A, B, C; cin >> A >> B >> C; int ans = 0; for(int i=0; i<N; i++){ if(A[i]==B[i] && B[i] == C[i]) continue; else if(A[i]==B[i] || B[i]==C[i] || C[i]==A[i]) ans+=1; else ans+=2; } cout << ans << endl; }
0
CPP
#include <bits/stdc++.h> int main() { int i, j, sayac = 0; char dizi[3][3], c; for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) { scanf("%c", &c); if (c != '\n') dizi[i][j] = c; else { j--; continue; } } for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) if (dizi[i][j] != dizi[2 - i][2 - j]) sayac++; if (sayac > 0) printf("NO"); else printf("YES"); return 0; }
7
CPP
#include <bits/stdc++.h> bool r[1001]; bool c[1001]; int main() { int n, m; scanf("%d %d", &n, &m); memset(r, true, sizeof(r)); memset(c, true, sizeof(c)); for (int i = 0; i < m; ++i) { int x, y; scanf("%d %d", &x, &y); r[y] = false; c[x] = false; } int res = 0; for (int i = 2; i < n; ++i) { if (r[i]) res++; if (c[i]) res++; if (n % 2 == 1 && i == n / 2 + 1 && r[i] && c[i]) res--; } printf("%d\n", res); return 0; }
8
CPP
n=[int(i) for i in input().split()] def minnum(x,y,z): if x>y: if y>z: return z else: return y else: if x>z: return z else: return x a=n[1]*n[2] b=n[5] c=n[3]*n[4] a=a//n[6] b=b//n[7] print(minnum(a,b,c)//n[0])
7
PYTHON3
#include <bits/stdc++.h> const long long int R = 1000 * 1000 * 1000 + 7; long long int f[1005][1005][2]; long long int max2(long long int a, long long int b) { return a > b ? a : b; } int main(void) { f[0][0][false] = 0, f[0][0][true] = 0; int n; scanf("%d", &n); for (int right = 1; right <= n; right++) { f[0][right][true] = f[0][right - 1][false] + 1; f[0][right][false] = max2(f[0][right - 1][true], f[0][right - 1][false]); for (int left = 1; left < right; left++) { f[left][right][true] = max2(f[left - 1][right][false] + max2(f[left][right - 1][true], f[left][right - 1][false]), f[left][right - 1][false] + max2(f[left - 1][right][true], f[left - 1][right][false])) + 1; f[left][right][false] = max2(f[left - 1][right][false], f[left - 1][right][true]) + max2(f[left][right - 1][false], f[left][right - 1][true]); } f[right][right][true] = f[right - 1][right][false] + 1; f[right][right][false] = max2(f[right - 1][right][true], f[right - 1][right][false]); for (int left = 0; left <= right; left++) for (int b = 0; b < 2; b++) f[left][right][b] %= R; } printf("%lld\n", max2(f[n][n][0], f[n][n][1])); return 0; }
10
CPP
a,b,c,x,y=map(int,input().split()) if a+b<=2*c:print(a*x+b*y) else: if x<y:print(x*2*c+(y-x)*min(b,2*c)) else:print(y*2*c+(x-y)*min(a,2*c))
0
PYTHON3
a=input() b=input() c='aeuoi' if len(a)!=len(b): print('No') else: for i in range(len(a)): if (a[i] in c and b[i] not in c) or (a[i] not in c and b[i] in c): print("No") exit() print("Yes")
7
PYTHON3
# -*- coding: utf-8 -*- """A. Young Physicist(69A)_Final-Codeforces.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1htn8g9_tNtk4CQNG8dOd5FblVXrqezRn """ n=int(input()) l1=[] l2=[] l3=[] for i in range(n): mylist = list(map(int , input().strip().split())) l1.append(int(mylist[0])) l2.append(int(mylist[1])) l3.append(int(mylist[2])) r1=sum(l1) r2=sum(l2) r3=sum(l3) if r1==0 and r2==0 and r3==0: print("YES") else: print("NO")
7
PYTHON3
n=int(input()) o=-1 for x in range(1+n//4): y=(n-4*x)/7 if y==int(y): o=1 print('4'*x+'7'*int(y)) break if o==-1:print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int main() { string s; cin >> s; int n = s.size(); int l = (n + 1) / 2; int q; scanf("%d", &q); vector<int> cnt(l); for (int qq = 0; qq < q; qq++) { int a; scanf("%d", &a); ++cnt[a - 1]; } int now = 0; for (int i = 0; i < l; i++) { now += cnt[i]; if (now & 1) { swap(s[i], s[n - i - 1]); } } puts(s.data()); return 0; }
8
CPP
N=int(input()) t,x,y=0,0,0 s=0 for i in range(N): tn,xn,yn=map(int,input().split()) if ((tn-t)-(xn-x+yn-y))%2==1 or ((tn-t)-(xn-x+yn-y))<0: s=1 if s==0: print('Yes') else: print('No')
0
PYTHON3
h=int(input()) d=1 ans=0 while h: ans+=d h//=2 d*=2 print(ans)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int h, g; int n, m; int a[(1 << 21) + 5]; int ret[(1 << 20) + 5]; bool ok(int v) { while (a[2 * v] != 0 || a[2 * v + 1] != 0) { if (a[2 * v] > a[2 * v + 1]) v = 2 * v; else v = 2 * v + 1; } return v > m; } void f(int v) { while (a[2 * v] != 0 || a[2 * v + 1] != 0) { if (a[2 * v] > a[2 * v + 1]) { a[v] = a[2 * v]; v = 2 * v; } else { a[v] = a[2 * v + 1]; v = 2 * v + 1; } } a[v] = 0; } int main() { int T; scanf("%d", &T); for (; T > 0; T--) { scanf("%d%d", &h, &g); n = (1 << h) - 1; m = (1 << g) - 1; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = n + 1; i <= 2 * n + 1; i++) { a[i] = 0; } int r = 1; for (int i = 1; i <= n - m; i++) { while (!ok(r)) r++; ret[i] = r; f(r); } long long sum = 0; for (int i = 1; i <= m; i++) { sum += a[i]; } printf("%lld\n", sum); for (int i = 1; i <= n - m; i++) { printf("%d%c", ret[i], (i == n - m) ? '\n' : ' '); } } }
11
CPP
#include<bits/stdc++.h> using namespace std; int a[100005][2]; int main(){ int n,m,cnt1=0,cnt2=0; scanf("%d%d",&n,&m); for(int i=0;i<m;i++){ int b; string str; cin>>b>>str; if(a[b][0]==0 && str=="AC" ){ cnt2+=a[b][1]; cnt1++; a[b][0]=1; } else if(a[b][0]==0 && str=="WA"){ a[b][1]++; } } printf("%d %d",cnt1,cnt2); return 0; }
0
CPP
n, m, a = list(map(int, input().split(" "))) x, xr = n // a, n % a y, yr = m // a, m % a w = x if xr == 0 else x + 1 h = y if yr == 0 else y + 1 print(w * h)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y; cin >> n >> x >> y; string s; cin >> s; long long co = 0; long long ans = 0; co = 0; char prev = '1'; for (int i = 0; i < n; i++) { if (s[i] == '0' && prev != s[i]) { co++; } prev = s[i]; } if (co == 0) { cout << 0 << "\n"; return 0; } if (co == 1) { cout << min(y, co * y + x) << "\n"; return 0; } ans = min(co * y, (co - 1) * x + y); cout << ans << "\n"; }
9
CPP
#include <bits/stdc++.h> int main() { int n, k; std::map<char, int> freq, actual; std::string input; std::cin >> n >> k >> input; for (char i : input) { freq[i]++; actual[i]++; } std::map<char, int>::iterator it = actual.begin(); while (k--) { if (it->second == 0) { it++; k++; continue; } it->second--; } for (char i : input) { if (actual[i] < freq[i]) { actual[i]++; continue; } std::cout << i; } return 0; }
9
CPP
N = int(input()) for i in range(N): print('ACL',end='')
0
PYTHON3
#include <bits/stdc++.h> using namespace std; map<int, int> a; int t, n, x, flag; void fenz(int n) { if (n == 0) { flag = 0; return; } if (a[n] > 0) a[n]--; else { fenz(n / 2); fenz(n / 2); } } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); flag = 1; a.clear(); for (int i = 0; i < n; i++) { scanf("%d", &x); a[x]++; } fenz(2048); if (flag) printf("YES\n"); else printf("NO\n"); } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; long long n, k; void solve() { cin >> n >> k; if (n < k) { cout << k - n << "\n"; return; } if ((n % 2 == 0 and k % 2 == 0) or (n % 2 == 1 and k % 2 == 1)) cout << 0 << "\n"; else cout << 1 << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; for (long long i = 1; i <= t; i++) solve(); }
7
CPP
#include <bits/stdc++.h> using namespace std; int a[30]; int b[30] = {0, 0, 1, 1, 5, 1, 21, 1, 85, 73, 341, 89, 1365, 1, 5461, 4681, 21845, 1, 87381, 1, 349525, 299593, 1398101, 178481, 5592405, 1082401, 22369621, 19173961}; int q; int gcd(int x, int y) { if (x < y) swap(x, y); int z = x % y; while (z != 0) { x = y; y = z; z = x % y; } return y; } int main() { int x = 2, ans, pos; a[1] = 1; for (int i = 2; i < 30; i++) { a[i] = a[i - 1] + x; x = x * 2; } scanf("%d", &q); while (q--) { scanf("%d", &x); pos = lower_bound(a + 1, a + 30, x) - a; ans = a[pos]; if (ans == x) { ans = b[pos]; } printf("%d\n", ans); } return 0; }
9
CPP
n = int(input()) print(' '.join(map(str, range(10000000 - n, 10000000))))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> vec(201, 0); int a[n]; int ans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; vec[a[i]]++; ans = max(ans, vec[a[i]]); } for (int i = 1; i <= 200; i++) { vector<int> ves(201); for (int j = 0; j <= 200; j++) ves[j] = vec[j]; int l = -1; int r = n; while (ves[i] >= 2) { for (int j = l + 1; j < n; j++) { ves[a[j]]--; if (a[j] == i) { l = j; break; } } for (int j = r - 1; j >= 0; j--) { ves[a[j]]--; if (a[j] == i) { r = j; break; } } for (int j = 1; j <= 200; j++) { ans = max(ans, vec[i] - ves[i] + ves[j]); } } } cout << ans << endl; } return 0; }
11
CPP
#include <bits/stdc++.h> using LL = long long; const int N = 5000 + 5; int dfn[N], low[N], belong[N], stack[N], instack[N], tim, top, tot, n, m; std::vector<int> edges[N], redges[N]; void tarjan(int u) { dfn[u] = low[u] = ++tim; stack[top++] = u; instack[u] = 1; for (int v : edges[u]) { if (!dfn[v]) { tarjan(v); low[u] = std::min(low[u], low[v]); } else if (instack[v]) { low[u] = std::min(low[u], dfn[v]); } } if (low[u] == dfn[u]) { while (true) { int v = stack[--top]; instack[v] = 0; belong[v] = tot; if (v == u) break; } tot++; } } void scc() { top = tim = tot = 0; memset(dfn, 0, sizeof(dfn)); memset(instack, 0, sizeof(instack)); for (int i = 0; i < n; i++) if (!dfn[i]) tarjan(i); } int solve(int source) { if (edges[source].empty()) return 0; std::queue<int> que; std::vector<int> dis(n, -1); dis[source] = 0; que.push(source); while (!que.empty()) { int u = que.front(); que.pop(); for (int v : redges[u]) { if (dis[v] == -1) { dis[v] = dis[u] + 1; que.push(v); } } } int ret = n + 1; for (int v : edges[source]) { ret = std::min(ret, dis[v] + 1); } return ret; } int work() { scc(); int cycle = 0, cnt = 0; for (int i = 0; i < n; ++i) { bool flag = true; for (int j = 0; j < i; ++j) { if (belong[i] == belong[j]) { flag = false; break; } } if (flag) { int best = n + 1; for (int j = i; j < n; ++j) { if (belong[j] == belong[i]) { best = std::min(best, solve(j)); } } cycle += best; if (best) { cnt++; } } } return 999 * cycle + cnt + n - cycle; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { int a, b; scanf("%d%d", &a, &b); a--; b--; edges[a].push_back(b); redges[b].push_back(a); } printf("%d\n", work()); }
11
CPP
n,k=map(int,input().split()) l=[int(i) for i in input().split()] if n==1: print(k-l[0]) exit() l.sort() cnt=0 while min(l)<k: cnt+=1 for i in range(0,n-1): if l[i]!=l[i+1]: l[i]+=1 l[n-1]+=1 #print(min(l)) print(cnt)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; unsigned long long int dp[20000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); unsigned long long int n; cin >> n; dp[0] = 1; dp[1] = 2; int i = 1; while (dp[i] <= n) { i++; dp[i] = dp[i - 1] + dp[i - 2]; } cout << (i - 1); }
7
CPP
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int shapes[n]; for (int i = 0; i < n; i++) cin >> shapes[i]; string ans = "Finite"; int p = 0; for (int i = 0; i < n - 1; i++) { if (shapes[i] == 2 && shapes[i + 1] == 3) { ans = "Infinite"; break; } else if (shapes[i] == 3 && shapes[i + 1] == 2) { ans = "Infinite"; break; } else if (shapes[i] == 1 && shapes[i + 1] == 2) { if (n >= 3) { if (shapes[i - 1] == 3) p += 2; else p += 3; } else p += 3; } else if (shapes[i] == 2 && shapes[i + 1] == 1) { p += 3; } else if (shapes[i] == 1 && shapes[i + 1] == 3) { p += 4; } else if (shapes[i] == 3 && shapes[i + 1] == 1) { p += 4; } } if (ans == "Infinite") cout << ans; else cout << ans << endl << p; return 0; }
7
CPP
n,m = map(int,input().split()) d = dict() for _ in range (m): a,b = input().split() x = [a,b][len(a) > len(b)] d[a] = d[b] = x s = input().split() for i in s: print(d[i],end=' ')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; priority_queue<long long> Q; int main() { int n, k1, k2, k; long long a[1000], b[1000]; cin >> n >> k1 >> k2; k = k1 + k2; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) { Q.push(abs(a[i] - b[i])); } while (k > 0) { k--; int now = Q.top(); Q.pop(); if (now > 0) now--; else now++; Q.push(now); } long long ans = 0; while (!Q.empty()) { ans += Q.top() * Q.top(); Q.pop(); } cout << ans << endl; return 0; }
8
CPP
chat = list(input()) try: h = chat.index('h') e = chat.index('e', h, len(chat)) l = chat.index('l', e, len(chat)) chat.pop(l) L = chat.index('l', l, len(chat)) + 1 o = chat.index('o', L, len(chat)) + 1 if h<e<l<L<o: print("YES") except ValueError or NameError: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> LP(const string& S, const string& T) { const int M = int((T).size()); vector<int> last_pos_in_T(26, 0); const int N = int((S).size()); vector<int> res(N, -1); for (int i = 0, j = 0; i < N; ++i) { int ch_id = S[i] - 'a'; if (j < M && S[i] == T[j]) { res[i] = ++j; last_pos_in_T[ch_id] = j; } else { res[i] = last_pos_in_T[ch_id]; } } return res; } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); string S, T; cin >> S; cin >> T; vector<int> L = LP(S, T); reverse(S.begin(), S.end()); reverse(T.begin(), T.end()); vector<int> R = LP(S, T); reverse(R.begin(), R.end()); for (int& x : R) x = int((T).size()) - x + 1; bool ok = true; for (int i = 0; i < int((S).size()); ++i) { if (L[i] < 0 || R[i] < 0 || L[i] < R[i]) { ok = false; break; } } cout << (ok ? "Yes" : "No") << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { stack<int> st; int n, x; cin >> n; for (int i = 1; i <= n; i++) { if (i == 1) { cin >> x; st.push(x); } else { cin >> x; if (st.size() != 0 && (x - st.top()) % 2 == 0) st.pop(); else { st.push(x); } } } if (st.size() <= 1) { printf("YES"); } else printf("NO"); return 0; }
10
CPP
#include <bits/stdc++.h> const int MAX_N = (int)1e5 + 123; const double eps = 1e-6; const int inf = (int)2e9 + 123; using namespace std; int n, k; pair<int, int> a[MAX_N]; long long int ans[MAX_N]; vector<pair<int, pair<int, int> > > events; multiset<pair<int, int> > now; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].first, &a[i].second); events.push_back(make_pair(a[i].second - k + 1, make_pair(a[i].first, 1))); events.push_back(make_pair(a[i].second + 1, make_pair(a[i].first, -1))); } sort(events.begin(), events.end()); for (int i = 0; i < (int)((events).size()); i++) { int x = events[i].second.first; if (events[i].second.second == 1) now.insert(make_pair(x - k + 1, 1)), now.insert(make_pair(x + 1, -1)); else now.erase(now.find(make_pair(x - k + 1, 1))), now.erase(now.find(make_pair(x + 1, -1))); if (i == (int)((events).size()) - 1) { assert(now.empty()); continue; } if (events[i + 1].first == events[i].first) continue; int last = (int)2e9, cnt = 0; for (auto j : now) { if (last != int(2e9) && j.first != last && cnt) { ans[cnt] += (j.first - last) * 1ll * (events[i + 1].first - events[i].first); } cnt += j.second; last = j.first; } } for (int i = 1; i <= n; i++) printf( "%lld" " ", ans[i]); return 0; }
10
CPP
t = int(input()) for _ in range(t): n = int(input()) str = input() o = c = a = 0 for i in range(n): if str[i] == '(':o += 1 else: if o > 0: o -= 1 n -= 2 print(n//2)
9
PYTHON3
n,k = map(int, input().split()) a = [-1,-1,-1] for i in range(n+1): for j in range(n-i+1): if i*10000+j*5000+(n-i-j)*1000 == k: a = [i,j,n-i-j] print(*a)
0
PYTHON3
import sys f = sys.stdin # f = open("input.txt", "r") y, k, n = map(int, f.readline().strip().split()) if y >= n: first = -1 else: t = k while t <= y: t += k first = t-y if first == -1: print(-1) else: if first+y > n: print(-1) else: res = [] for i in range(first, n+1-y, k): res.append(i) print(*res)
7
PYTHON3
a = input() b = list(a) if "+" in b: b.remove("+") for i in b: if i == "+": b.remove(i) b.sort() print("+".join(b))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n; cin >> n; int ans = 0; for (int a = 1; a <= n; ++a) { for (int b = 1; b <= a; ++b) { int c = a ^ b; if (c <= b && a < b + c && b < a + c && c < a + b) ++ans; } } cout << ans << endl; return 0; }
8
CPP
l = list(map(int, input().split())) if(sum(l)%len(l) == 0 and sum(l) > 0): print(sum(l)//len(l)) else: print(-1)
7
PYTHON3
n = int(input()) count = 0 for i in range(n): p, v, t = input().split() p = int(p) v = int(v) t = int(t) if(p+v+t >= 2): count = count + 1 print(count)
7
PYTHON3
n = int(input()) list0 = [] for i in range(n): s = input() count = 0 for i in list0 : if i == s: count += 1 break list0.append(s) if count>=1: print('YES') else: print('NO')
7
PYTHON3
numero, pontos = input().split() numero = int(numero) pontos = int(pontos) if(int(numero/2) > pontos or (int(numero/2) == 0 and pontos != 0)): print("-1\n", end='') elif(int(numero/2) == pontos): i = 1 while(i<= numero): if(i > 1): print(" ", end='') print(i, end='') i += 1 print("\n", end='') else: temporario = pontos - int(numero/2) + 1 print(str(temporario)+" "+ str(temporario*2), end='') i = temporario * 2 + 1 j = 1 while(j < int(numero/2)): print(" "+str(i)+" " + str(i+1), end='') i += 2 j += 1 if((numero % 2) == 1): print(" " + str(i), end='') print("\n", end='')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, f = 1; char ch; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long n, m, W, L; long long ans; long long head[2000010], pos; struct edge { long long to, next, c; } e[2000010 << 1]; void add(long long a, long long b, long long c) { pos++; e[pos].to = b, e[pos].c = c, e[pos].next = head[a], head[a] = pos; } void insert(long long a, long long b, long long c) { add(a, b, c); add(b, a, c); } long long f[2000010], size[2000010], sum, rt; bool vis[2000010]; void find_root(long long u, long long fa) { size[u] = 1, f[u] = 0; for (long long i = head[u]; i; i = e[i].next) { long long v = e[i].to; if (v == fa || vis[v]) continue; find_root(v, u); size[u] += size[v]; f[u] = max(f[u], size[v]); } f[u] = max(f[u], sum - size[u]); if (f[u] < f[rt]) rt = u; } long long C[2000010], tot, st; void add(long long x, long long val) { if (x <= 0) return; while (x <= n) { C[x] += val; x += x & (-x); } } long long ask(long long x) { long long ret = 0; if (x < 0) return 0; while (x) { ret += C[x]; x -= x & (-x); } return ret; } struct node { long long w, l; } d[2000010], p[2000010]; bool operator<(node a, node b) { return a.w < b.w; } long long sz; void get_dep(long long u, long long fa) { sz++; for (long long i = head[u]; i; i = e[i].next) { long long v = e[i].to; if (v == fa || vis[v]) continue; d[v].l = d[u].l + 1, d[v].w = d[u].w + e[i].c; p[++tot] = d[v]; get_dep(v, u); } } long long cal(long long u, long long now1, long long now2) { tot = 0; d[u].w = now1, d[u].l = now2; sz = 0; get_dep(u, 0); long long ret = 0; sort(p + 1, p + tot + 1); long long l = 1, r = tot; for (long long i = l; i <= r; i++) { add(p[i].l, 1); if (p[i].l + now2 <= L && p[i].w + now1 <= W) ret++; } while (l < r) { if (p[l].w + p[r].w > W) add(p[r--].l, -1); else { add(p[l].l, -1); ret += ask(L - p[l].l); l++; } } while (l <= r) { add(p[l].l, -1); l++; } return ret; } void work(long long u) { vis[u] = 1; ans += cal(u, 0, 0); for (long long i = head[u]; i; i = e[i].next) { long long v = e[i].to; if (vis[v]) continue; ans -= cal(v, e[i].c, 1); sum = size[v], rt = 0; find_root(v, 0); work(rt); } } int main() { n = read(); L = read(); W = read(); for (long long i = 1; i < n; i++) { long long a = i + 1, b = read(), c = read(); insert(a, b, c); } f[0] = 1 << 26, sum = n; find_root(1, 0); work(rt); cout << ans << "\n"; }
11
CPP
#include<bits/stdc++.h> #define db double using namespace std; const int N=5e5+10; int n,l=1,r=0; db maxl,v,sumv,t,ans; struct node{ db t,v; }q[N]; int main(){ scanf("%d%lf",&n,&maxl); while(n--){ scanf("%lf%lf",&t,&v); while(v+sumv>maxl){ db out=min(v+sumv-maxl,q[l].v); ans-=out*q[l].t,sumv-=out,q[l].v-=out; if(!q[l].v) l++; } q[++r]=(node){t,v}; ans+=t*v,sumv+=v; while(l<r && q[r-1].t>q[r].t){ q[r-1].t=(q[r-1].t*q[r-1].v+q[r].t*q[r].v)/(q[r-1].v+q[r].v); q[r-1].v+=q[r].v; r--; } printf("%.7lf\n",ans/sumv); } }
0
CPP
t=int(input()) for i in range(t): s=list(input().split()) l1= list(filter(lambda x: s[0][x] == "R", range(len(s[0])))) if(len(l1)>=1): x=abs(l1[0]+1) for j in range(len(l1)-1): x=max(x,abs(l1[j]-l1[j+1])) x=max(x,abs(l1[len(l1)-1]-len(s[0]))) print(x) else: print(len(s[0])+1)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<set<long long int>> ed, leaf; struct comp { bool operator()(long long int a, long long int b) const { if (leaf[a].size() == leaf[b].size()) return a < b; return leaf[a].size() > leaf[b].size(); } }; signed main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int n, k; cin >> n >> k; ed = vector<set<long long int>>(n); leaf = vector<set<long long int>>(n); for (long long int(i) = (0); (i) < (n - 1); (i)++) { long long int u, v; cin >> u >> v; u--; v--; ed[u].insert(v); ed[v].insert(u); } for (long long int(i) = (0); (i) < (n); (i)++) { if (ed[i].size() == 1) { leaf[*ed[i].begin()].insert(i); } } set<long long int, comp> maxLeaf; for (long long int(i) = (0); (i) < (n); (i)++) { maxLeaf.insert(i); } long long int res = 0; while (1) { long long int v = *maxLeaf.begin(); if (leaf[v].size() < k) break; maxLeaf.erase(v); for (long long int(i) = (0); (i) < (k); (i)++) { long long int x = *leaf[v].begin(); leaf[v].erase(x); if (leaf[x].find(v) != leaf[x].end()) { leaf[x].erase(v); maxLeaf.erase(x); maxLeaf.insert(x); } ed[x].erase(v); ed[v].erase(x); } if (ed[v].size() == 1) { long long int to = *ed[v].begin(); maxLeaf.erase(to); leaf[to].insert(v); maxLeaf.insert(to); } maxLeaf.insert(v); res++; } cout << res << "\n"; } return 0; }
12
CPP
#include <iostream> using namespace std; signed main() { int N,K; cin >> N >> K; cout << N-K+1 << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int amount; vector<int> position[100005]; int main() { scanf("%d", &amount); for (int i = 1; i <= 2 * amount; i++) { int cake; scanf("%d", &cake); position[cake].push_back(i); } int alpha = 1, belta = 1; long long total = 0; for (int i = 1; i <= amount; i++) { total += abs(position[i][0] - alpha); total += abs(position[i][1] - belta); alpha = position[i][0]; belta = position[i][1]; } printf("%lld", total); }
8
CPP
#include <bits/stdc++.h> using namespace std; const int mxN = 1001; bool mp[mxN][mxN]; int comp[mxN][mxN]; bool vis[mxN][mxN]; int sz[mxN * mxN]; int m, n; int when[mxN * mxN]; bool vis2[mxN * mxN]; pair<int, int> start[mxN * mxN]; bool e(int y, int x, bool col) { return y >= 0 && y < m && x >= 0 && x < n && mp[y][x] == col; } void dfs(int y, int x, int r) { if (vis[y][x]) return; vis[y][x] = 1; comp[y][x] = r; sz[r]++; if (e(y + 1, x, mp[y][x])) dfs(y + 1, x, r); if (e(y - 1, x, mp[y][x])) dfs(y - 1, x, r); if (e(y, x + 1, mp[y][x])) dfs(y, x + 1, r); if (e(y, x - 1, mp[y][x])) dfs(y, x - 1, r); } set<int> adj; void dfs2(int y, int x) { if (vis[y][x]) return; vis[y][x] = 1; if (e(y + 1, x, !mp[y][x])) adj.insert(comp[y + 1][x]); if (e(y + 1, x, mp[y][x])) dfs2(y + 1, x); if (e(y - 1, x, !mp[y][x])) adj.insert(comp[y - 1][x]); if (e(y - 1, x, mp[y][x])) dfs2(y - 1, x); if (e(y, x + 1, !mp[y][x])) adj.insert(comp[y][x + 1]); if (e(y, x + 1, mp[y][x])) dfs2(y, x + 1); if (e(y, x - 1, !mp[y][x])) adj.insert(comp[y][x - 1]); if (e(y, x - 1, mp[y][x])) dfs2(y, x - 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> m >> n >> t; for (int j = 0; j < m; j++) { string s; cin >> s; for (int i = 0; i < n; i++) { mp[j][i] = s[i] == '1'; } } int comps = 0; for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { if (!vis[j][i]) { start[comps] = {j, i}; dfs(j, i, comps++); } } } for (int j = 0; j < m; j++) for (int i = 0; i < n; i++) vis[j][i] = 0; queue<int> q; for (int j = 0; j < m; j++) for (int i = 0; i < n; i++) { when[j * n + i] = -1; if (sz[j * n + i] > 1) { q.push(j * n + i); vis2[j * n + i] = 1; when[j * n + i] = 0; } } while (q.size()) { int i = q.front(); q.pop(); adj.clear(); dfs2(start[i].first, start[i].second); for (int j : adj) { if (!vis2[j]) { vis2[j] = 1; when[j] = when[i] + 1; q.push(j); } } } while (t--) { int y, x; cin >> y >> x; y--, x--; long long p; cin >> p; int c = comp[y][x]; if (when[c] == -1 || p <= when[c]) cout << mp[y][x] << "\n"; else { bool flip = (p - when[c]) % 2; cout << (mp[y][x] ^ flip) << "\n"; } } }
9
CPP
#include <bits/stdc++.h> using namespace std; typedef long long ll; constexpr int MOD = 998244353; vector<vector<int>> dp(512, vector<int>(512, 0)); vector<vector<int>> memo0(512, vector<int>(512, 0)); vector<vector<int>> memo1(512, vector<int>(512, 0)); int main() { int N, X; cin >> N >> X; for (int i = 0; i <= X; i++) { dp[i][i] = 1; } for (int k = 0; k < 512; k++) { for (int j = 0; j <= X; j++) { if (j == 0) memo0[j][k] = dp[j][k]; else memo0[j][k] = memo0[j-1][k] + dp[j][k]; memo0[j][k] %= MOD; } } for (int i = 1; i < N; i++) { for (int j = 0; j <= X; j++) { for (int k = 0; k < 512; k++) { dp[j][k] = memo0[j][k^j]; } } for (int k = 0; k < 512; k++) { for (int j = 0; j <= X; j++) { if (j == 0) memo1[j][k] = dp[j][k]; else memo1[j][k] = memo1[j-1][k] + dp[j][k]; memo1[j][k] %= MOD; } } memo0 = memo1; } ll ans = 0; for (int i = 0; i <= X; i++) { ans += dp[i][X]; ans %= MOD; } cout << ans % MOD << endl; }
0
CPP
# t = int(input()) # import math # output = [] # for _ in range(t): # a = input() # # for item in output: # print(item) n, m = list(map(int, input().split())) # n is the number of days you will write names in the notebook # m is the number of names which can be written on each page of the notebook. names_days = list(map(int, input().split())) output = [] names_before = 0 names_after = 0 for names_day in names_days: no_times_turned = 0 names_after += names_day output.append(str(names_after // m - names_before // m)) names_before += names_day print(' '.join(output))
7
PYTHON3
t = int(input()) for i in range(t): n = int(input()) p = [0]+[*map(int,input().split(" "))] s = input() q = [0]*(n+1) for j in range(1,n+1) : q[p[j]] = j disliked = [] liked = [] for j in range(1,n+1) : if (s[(q[j]-1):q[j]] == "1"): liked += [q[j]] else : disliked += [q[j]] songs = [0]*(n+1) x = 1 for j in disliked: songs[j] = x x += 1 for j in liked: songs[j] = x x += 1 for j in range(1,n+1) : print(songs[j],end=" ") print()
8
PYTHON3
for i in range(int(input())): n = input() n = input() while '()' in n: n = n.replace('()', '') print(int(len(n) / 2))
9
PYTHON3
# Description of the problem can be found at http://codeforces.com/problemset/problem/189/A n, a, b, c = map(int, input().split()) dp = [0] + [-1e9]*5000 for i in range(1, n + 1): dp[i] = max(dp[i - a], dp[i - b], dp[i - c]) + 1 print(dp[n])
7
PYTHON3
inp=int(input()) out=0 for i in range(inp): string=input() if string=='Tetrahedron': out+=4 elif string=='Cube': out+=6 elif string=='Octahedron': out+=8 elif string=='Dodecahedron': out+=12 elif string=='Icosahedron': out+=20 print(out)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; bool test_cases; void init(bool k) { test_cases = k; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } struct test_cases { void prepare_left(long long *great_left, long long n, long long *a) { stack<long long> st; great_left[0] = -1; st.push(a[0]); for (long long i = 1; i < n; i++) { while (!st.empty() and st.top() < a[i]) { st.pop(); } if (st.empty()) { great_left[i] = -1; } else { great_left[i] = st.top(); } st.push(a[i]); } } void prepare_right(long long *great_right, long long n, long long *a) { stack<long long> st; great_right[n - 1] = -1; st.push(a[n - 1]); for (long long i = n - 2; i >= 0; i--) { while (!st.empty() and st.top() < a[i]) { st.pop(); } if (st.empty()) { great_right[i] = -1; } else { great_right[i] = st.top(); } st.push(a[i]); } } void test_case(long long test) { long long n; cin >> n; long long a[n]; long long great_left[n]; long long great_right[n]; for (long long i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (long long i = 0; i < n - 1; i++) { ans = max(ans, a[i] ^ a[i + 1]); } prepare_left(great_left, n, a); prepare_right(great_right, n, a); for (long long i = 0; i < n; i++) { long long left = great_left[i]; long long right = great_right[i]; if (left != -1) { ans = max(ans, left ^ a[i]); } if (right != -1) { ans = max(ans, right ^ a[i]); } if (left != -1 and right != -1) { ans = max(ans, left ^ right); } } cout << ans << "\n"; } }; int32_t main() { init(false); long long tc; if (test_cases) { cin >> tc; } else { tc = 1; } for (long long i = 0; i < tc; i++) { struct test_cases o; o.test_case(i + 1); } return 0; };
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7, M = 2e9, OO = 0x3f3f3f3f; const long long int MOD = 1000000007; const double pi = 3.1415926536; int main() { int n, ans = 0, cnt = 0; string s; cin >> n >> s; while (s.size() > 0) { int itr = s.find("xxx"); if (itr != string::npos) { s.erase(s.begin() + itr); ans++; } else break; } cout << ans; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (n == 1) cout << 1; else { if (m - 2 >= n - m - 1) cout << m - 1; else cout << m + 1; } }
8
CPP
#include<stdio.h> int main(){ int q,w; scanf("%d %d",&q,&w); if(q>w){ printf("a > b\n"); }else if(q<w){ printf("a < b\n"); }else{ printf("a == b\n"); } return 0; }
0
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; using ld = long double; const int N = 1e5 + 100; int n, m; int U[N], V[N]; vi in[N]; ll ans; int deg[N]; int32_t main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; --u; --v; deg[u]++; deg[v]++; if (u > v) { in[v].push_back(i); U[i] = u; V[i] = v; } else { in[u].push_back(i); U[i] = v; V[i] = u; } } for (int i = 0; i < n; i++) { ans += 1LL * (int)((in[i]).size()) * (deg[i] - (int)((in[i]).size())); } cout << ans << '\n'; int q; cin >> q; while (q--) { int v; cin >> v; --v; int rem = (int)((in[v]).size()); for (int i : in[v]) { int u = U[i]; ans -= (deg[v] - rem) + (int)((in[u]).size()); in[u].push_back(i); rem--; ans += rem + (deg[u] - (int)((in[u]).size())); swap(U[i], V[i]); } in[v].clear(); cout << ans << '\n'; } }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 200010; long long a[N]; long long b[N]; long long l[N]; long long r[N]; long long x[N]; vector<pair<long long, long long> > rg; const long long INF = 3000000000000000000ll; int main() { int n; long long t; scanf("%d%lld", &n, &t); const long long M = INF - t; for (long long i = 1; i <= n; i++) { l[i] = 0; r[i] = M; } for (long long i = 1; i <= n; i++) { scanf("%lld", a + i); } a[0] = 0; a[n + 1] = M; for (long long i = 1; i <= n; i++) { scanf("%lld", x + i); } for (long long i = 1; i <= n; i++) { if (x[i] < i) { puts("No"); exit(0); } l[x[i]] = a[i]; r[x[i]] = a[x[i] + 1] - 1; if (i + 1 <= x[i]) { if (rg.empty() || i + 1 > rg.back().second + 1) { rg.push_back({i + 1, x[i]}); } else { rg.back().second = max(rg.back().second, x[i]); } } } for (int i = 1; i <= n; i++) { l[i] = max(l[i], a[i]); } for (auto x : rg) { for (int i = x.first; i <= x.second; i++) { l[i - 1] = max(l[i - 1], a[i]); } } for (int i = n; i >= 1; i--) { if (l[i] <= r[i]) { b[i] = r[i]; r[i - 1] = min(r[i - 1], r[i] - 1); } else { puts("No"); exit(0); } } puts("Yes"); for (int i = 1; i <= n; i++) { printf("%lld ", b[i] + t); } puts(""); return 0; }
7
CPP
k,n,s,p = map(int, input().split()) import math print(math.ceil(k * math.ceil(n / s) / p))
7
PYTHON3
a, b, c, d = list(map(int, input().split())) s1 = min(a, c, d) * 256 a -= min(a, c, d) s1 += min(a, b) * 32 print(s1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int64_t n; cin >> n; char mat[n + 1][n + 1]; for (int64_t i = 1; i <= n; i++) { for (int64_t j = 1; j <= n; j++) cin >> mat[i][j]; } int64_t m; cin >> m; int64_t p[m]; for (int64_t i = 0; i < m; i++) cin >> p[i]; int64_t dp[n + 1][n + 1]; for (int64_t i = 1; i <= n; i++) { for (int64_t j = 1; j <= n; j++) { dp[i][j] = (int)(mat[i][j] - '0'); if (dp[i][j] == 0 && i != j) dp[i][j] = INT_MAX; } } for (int64_t k = 1; k <= n; k++) { for (int64_t i = 1; i <= n; i++) { for (int64_t j = 1; j <= n; j++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } } } vector<pair<int64_t, int64_t>> ans; ans.push_back({p[0], 0}); for (int64_t i = 1; i < m; i++) { if (dp[ans.back().first][p[i]] < (i - ans.back().second)) { ans.push_back({p[i - 1], i - 1}); } } ans.push_back({p[m - 1], m - 1}); cout << ans.size() << '\n'; for (int64_t i = 0; i < ans.size(); i++) cout << ans[i].first << ' '; return 0; }
9
CPP
a = input() b = input() c = str(int(a) + int(b)) if int(a.replace("0", "")) + int(b.replace("0", "")) == int(c.replace("0", "")): print("YES") else: print("NO")
7
PYTHON3
q = int(input()) for _ in range(q): x1,y1,x2,y2 = map(int, input().split()) if x1 == x2: print(abs(y2-y1)) elif y1 == y2: print(abs(x2-x1)) else: print(abs(x2-x1)+abs(y2-y1)+2)
7
PYTHON3
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; template<typename Monoid, typename OperatorMonoid=Monoid> struct LazySegmentTree{ using F=function<Monoid(Monoid, Monoid)>; using G=function<Monoid(Monoid, OperatorMonoid, int)>; using H=function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; int sz; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const Monoid e1; const OperatorMonoid e0; LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &e1, const OperatorMonoid &e0): f(f), g(g), h(h), e1(e1), e0(e0){ sz=1; while(sz<n) sz<<=1; data.resize(2*sz-1, e1); lazy.resize(2*sz-1, e0); } void build(vector<Monoid> v){ for(int i=0; i<v.size(); i++) data[i+sz-1]=v[i]; for(int i=sz-2; i>=0; i--) data[i]=f(data[2*i+1], data[2*i+2]); } void eval(int k, int l, int r){ if(lazy[k]!=e0){ data[k]=g(data[k], lazy[k], r-l); if(k<sz-1){ lazy[2*k+1]=h(lazy[2*k+1], lazy[k]); lazy[2*k+2]=h(lazy[2*k+2], lazy[k]); } } lazy[k]=e0; } void update(int a, int b, const OperatorMonoid &x, int k, int l, int r){ eval(k, l, r); if(r<=a || b<=l) return; if(a<=l && r<=b){ lazy[k]=h(lazy[k], x); eval(k, l, r); }else{ update(a, b, x, 2*k+1, l, (l+r)/2); update(a, b, x, 2*k+2, (l+r)/2, r); data[k]=f(data[2*k+1], data[2*k+2]); } } void update(int a, int b, const OperatorMonoid &x){ return update(a, b, x, 0, 0, sz); } Monoid find(int a, int b, int k, int l, int r){ eval(k, l, r); if(b<=l || r<=a) return e1; if(a<=l && r<=b) return data[k]; else return f(find(a, b, 2*k+1, l, (l+r)/2), find(a, b, 2*k+2, (l+r)/2, r)); } Monoid find(int a, int b){ return find(a, b, 0, 0, sz); } Monoid operator[](const int &k){ return find(k, k+1); } }; template< typename G=vector<vector<int>> > struct HeavyLightDecomposition { G &g; vector< int > sz, in, out, head, rev, par; HeavyLightDecomposition(G &g) : g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {} void dfs_sz(int idx, int p) { par[idx] = p; sz[idx] = 1; if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back()); for(auto &to : g[idx]) { if(to == p) continue; dfs_sz(to, idx); sz[idx] += sz[to]; if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to); } } void dfs_hld(int idx, int par, int &times) { in[idx] = times++; rev[in[idx]] = idx; for(auto &to : g[idx]) { if(to == par) continue; head[to] = (g[idx][0] == to ? head[idx] : to); dfs_hld(to, idx, times); } out[idx] = times; } void build() { dfs_sz(0, -1); int t = 0; dfs_hld(0, -1, t); } int la(int v, int k) { while(1) { int u = head[v]; if(in[v] - k >= in[u]) return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u, int v) { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) return u; } } template< typename T, typename Q, typename F > T query(int u, int v, const T &ti, const Q &q, const F &f, bool edge = false) { T l = ti, r = ti; for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v), swap(l, r); if(head[u] == head[v]) break; l = f(q(in[head[v]], in[v] + 1), l); } return f(f(q(in[u] + edge, in[v] + 1), l), r); } template< typename Q > void add(int u, int v, const Q &q, bool edge = false) { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) break; q(in[head[v]], in[v] + 1); } q(in[u] + edge, in[v] + 1); } }; int main() { int n, Q; cin>>n>>Q; vector<vector<int>> G(n); for(int i=0; i<n-1; i++){ int a, b; cin>>a>>b; G[a].push_back(b); G[b].push_back(a); } auto f=[](ll a, ll b){ return a+b;}; auto g=[](ll a, ll x, int len){ return a+x*len;}; auto h=[](ll x, ll y){ return x+y;}; LazySegmentTree<ll> seg(n, f, g, h, 0, 0); HeavyLightDecomposition<> hld(G); hld.build(); for(int z=0; z<Q; z++){ int t; cin>>t; if(t==0){ int u, v; cin>>u>>v; auto q=[&](int i, int j){ return seg.find(i, j);}; cout<<hld.query(u, v, 0ll, q, f, true)<<endl; }else{ int v; ll w; cin>>v>>w; seg.update(hld.in[v]+1, hld.out[v], w); } } return 0; }
0
CPP
import sys input=sys.stdin.readline def main(): n = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) d = list(map(int, input().split())) prices = [a,b,c,d] inc = [{} for _ in range(3)] for i in range(3): ni = int(input()) for _ in range(ni): k,l = map(int, input().split()) if k-1 in inc[i]: inc[i][k-1].add(l-1) else: inc[i][k-1] = set([l-1]) step = 3 mem2 = prices[3][::] for step in range(3,0,-1): mem = mem2[::] index_sorted = sorted(range(n[step]), key= lambda x:mem[x]) mem2 = [float('inf')]*n[step-1] for i in range(n[step-1]): index = 0 while i in inc[step-1] and index<n[step] and index_sorted[index] in inc[step-1][i]: index+=1 if index<n[step]: mem2[i] = prices[step-1][i] + mem[index_sorted[index]] rep = min(mem2) if rep<float('inf'): print(rep) else: print(-1) main()
11
PYTHON3
#include <bits/stdc++.h> using namespace std; int32_t main() { long long int n; cin >> n; vector<vector<long long int>> v(n, vector<long long int>(n)); for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < n; j++) cin >> v[i][j]; } long long int s = 0; long long int j = 0; if (n == 1) { cout << v[0][0]; return 0; } for (long long int i = 0; i < n; i++) { s += v[i][j]; s += v[i][n - 1 - j]; j++; } for (long long int i = 0; i < n; i++) { s += v[i][n / 2]; } for (long long int i = 0; i < n; i++) { s += v[n / 2][i]; } s -= (3 * v[n / 2][n / 2]); cout << s; return 0; }
7
CPP
n = int(input()) s = input() g = 'ACTG' res = float('inf') for i in range(n - 3): val = 0 for j in range(4): kek = abs(ord(g[j]) - ord(s[i + j])) val += min(kek, 26 - kek) res = min(res, val) print(res)
7
PYTHON3
x=int (input()) count=0 for i in range(0,x): a,y,z=map(int, input().split()) if a+y+z > 1: count +=1 print(count)
7
PYTHON3
from sys import stdin,stdout n = int(stdin.readline()) a = [0 for i in range(n)] for i in range(n): inp = stdin.readline().split() x = int(inp[0]) y = int(inp[1]) a[i] = 2 * (x % 2) + (y % 2) + 1 stdout.write("YES") stdout.write('\n') stdout.write("\n".join(str(i) for i in a))
10
PYTHON3
/* Aa^~ kokoro ga pyonpyon suru n jaa^~ // ZZZXXkXkkkZ!``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ``` ```?Wfpppbpbbpbbpbbbkbkk // ppbbbpbbpVr`` `` ` ` ` ` ```` `` ` ` `` ` ` ` ` ` ` ` ` ` dppbbkkkkkkkkkkqkqkk // HkqqqqqkkWr`` ` ` ``` ``` `?G, ` ` ``.JC!```` ` ` `` `` ````(Wpbkkkkkkkkqkkkkkqk // mmmmmqqqqpr` `` `` ```````.+zT=`` `` 7TO-.```````` `` `` ```(yppbkkkkkkkkkkkkkkk // ggmgmmqqqH$ ``````````....````` ` ````````.`````` `` ``````.yfpppbbbbkkkkqqqqqH // gmmmmmqqqkW<```` `````...````` .,.` ````....````` ``````` (Wbqqmgmmgmggggggggg // qmmmqqqqkkWk.``````````````````` ;:<`` `````.`````````````-_<-?WHHqmmmmmmgmmgggg // @@@@@@@gggHH6- ``````````````` `` _ `` ```````````````` ._~~_.`-?Wkqmmmmmmmggg@g // @@@@g@gggHY~.-<_- `````````````````````````````````` ._~~(<-``.`.(WHqqqmmggggmmm // @@g@gggHH=.`..._<-___..```````````````````````. .-_~~~_(!``-.``.`` OHHWUWHmqHWXW // gggggmqK1.``..~.. _<<+-(____.. ```````` ..__~~_((<<!.`.``` .``.`` j0C1XUHmHIdW // ggmmqH0!,_``.>`````` _<<;<v<<<++((((((((((<<<<<<~_. (-.``~``.>..``` jOuWHHqHIdH // gmmqkW!`(_ J>` `` ` _~<`_~~~~<<<<<<<~~__````````` ?1. ._`(__``` zXWHg@HkXH // gHHWS{``(lJ<!``.``.```(:+>`._`````.` <..`` - ``. ` _ ?&._.I`_`````` .XyVfppppW // HHHSv``.(X:_..... _..(;+<!.(<..-.....-.-_..+_`..<.`.`..`_IJd} .`..````jqg@@@@@@ // XHWZ{..<Jk~!.`.. (<.-(+>(_.(1.(_..`.`.`.<_.+<_..<<-..._..-zy>.`_`...```.WH@HHHHH // bkWt~.-jCz(_..`.(+<.(;< ._-<=_(<_..-....(_.<1<..(<<.`._..-JUS-._.`...```dHmH9VUH // WUUO..(f.(c...__+z<-(+~` _-+<_(><..__.`.(<._.z_.(1;_..__.(C(zT-(..`...``(WHR<+Xk // kkkk._(_.->..._(z;:_><.._>_+_<(1>_._<...(v<<.(<.(+z<..-_(Z~_<_j+_..`...`(WHKz1ZW // @@gR._+_..~..-<+z<<?<>```_.<_.(+1><_;_..(1_:`.<<??1z--(+Z!..<_.j<....`..(bgHAAQX // @@mR.(j:..~.._<z!`.(>~``` ~(_.(+<1><><_.(((_`.<__`.<_.(X>...<_.(<.....`.JUWWWyWW // @gmH_(zl..(.._+>```<+_````.~>``(+.<?>>_._(<```(<<``(__<>....<.._<.......dXkkkHHH // mmqHl(dk_.(_.-=~`.`.1-..._~-1.``_:`(??<_~(`.--.&_.`.<(;<...._.._<..`..._Xg@@@@@@ // qHkpk(dX<.(;..j_```.(((JJ&a&-~``````.1<_```-(((e+.-(/`(>...._..(<......(Wmggg@@g // HVHbWcz><__+_.(_.(dWWHHH@HHc~````````.+~`` (jHMMMHHHm&.?..._<..(<_..._.(WqqHHmHg // 0>vWWkzZwl~<o.__`__~X@@HM@Hb ```.`.``. ```` d@@HHH@@K?76...(<..(<_...(_(ppWWWWHq // X0XWHKXXw$<(z<.( `` WHHMHHHH_``````````````.WHHMNMHHH_`(...(<_.(z_..._<(fWVC174W // XuXWHHWWz>__+z+.!`..??CZYCOX_`````````````.`~.OvTUZUS_`~.._+?_.(_~_.._zjO=1+~+jy // kkkkkkkkX:._<z=1(_`` << ``->``.``.``.``.```` ?<`` (v!`._..(??_.(1._.._=dUOOzzzwX // @@@@@@@@H<...1O=v<_...__ -_````````````````.`` `` ~.`` :.~+=?~.(;_(...jdQQQQQkkk // H@@@@@@@H~...(==>.~~~~~....`.`````````.`````.`........->.(===~~<<.(...(dg@@@@@@@ // @@@H@@HHH_.__(=l>~.~~~~~....``.``.``.```..`......~~~~~(<_+=l=~_<.->..~_dqggggg@g // @H@@@@MHH_._<(=l>...........```````````````.`...~~~~~~+<(=lz=~((j=z_..~jWqmmgggm // @@H@@HHWH_._<(lll-.......```.````.``.`..`````........_z<+llZz~(lOO=<...(VYUUUW9Y // @@HMMHWZf>~_=:=llw+.`````````.`.```__~~_``.`````.....(z+llOOz_zllOlz~..~<<1+dW>_ // MMM#MHHWXl~_=>1ltwOl&.`.``.`````.``````````````.````.(llttwtz(OltwOz<..__zwOwwOz // HM#HMHUUI<._1z+ttOZttlt&....``.``.`.````.``...``...(zZtttOktzjttttwlz_._<(Xkkkkk // HHHmHSZu:(_~+OztttXtttOZZttO+-..............-(+ztOttwttttd0tOZttttwOl<~.(_dMMHHH // rvuuXuuI~~<~(uttttwvOwwwkQQHMMHHHHHHHHHMMMNmgey?OwwwrtttwXOtwttttttXtO-~.((wZyyy // HHHHHHK>(~(-(dOrtrrl(QgMHMMMHHHHHHHHHHHHHHHH##HMNkX0rrrrXXrd%`` (Ctwwtz_~.<(Wg@H // NNNNNHD(~(zo~zXrrrQdHHMMNMHHHHHHHHHHHHHHHHHHHHHH##HNmyrdKkwZ ` _``-zwrt1~~_<(MNM // MMMMM#<<_jwr:(Z4QHHMMHMHHHHHHHHHHHHHHHHHHHHHHHHHHHH###NHSXZ>` ~````.OXtt>~._<?MM */ #include <bits/stdc++.h> using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using LL = long long; using VL = vector<LL>; using VVL = vector<VL>; using PLL = pair<LL, LL>; using VS = vector<string>; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FF first #define SS second template<class S, class T> istream& operator>>(istream& is, pair<S,T>& p){ return is >> p.FF >> p.SS; } template<class T> istream& operator>>(istream& is, vector<T>& xs){ for(auto& x: xs) is >> x; return is; } template<class S, class T> ostream& operator<<(ostream& os, const pair<S,T>& p){ return os << p.FF << " " << p.SS; } template<class T> ostream& operator<<(ostream& os, const vector<T>& xs){ for(unsigned int i=0;i<xs.size();++i) os << (i?" ":"") << xs[i]; return os; } template<class T> void maxi(T& x, T y){ if(x < y) x = y; } template<class T> void mini(T& x, T y){ if(x > y) x = y; } void debug(istringstream&){} template <char sep=',', class Head, class... Tail> void debug(istringstream& iss, Head&& head, Tail&&... tail) { string name; getline(iss, name, ','); cout << sep << name << "=" << head; debug(iss, forward<Tail>(tail)...); } #ifdef PYONPOI #define DEBUG(...) \ do{ \ istringstream ss(#__VA_ARGS__); \ debug<' '>(ss, __VA_ARGS__); \ cout<<endl; \ }while(0) #else #define DEBUG #endif const double EPS = 1e-10; const double PI = acos(-1.0); const LL MOD = 1e9+7; template<class T> T ext_gcd(T a, T b, T& x, T& y){ T d = a; if(b == 0){ x = 1; y = 0; } else{ d = ext_gcd<T>(b, a%b, y, x); y -= (a / b) * x; } return d; } template<class T> bool ext_gcd_n(T a, T b, LL c, T& x, T& y, bool zero=true) { auto g = ext_gcd(a, b, x, y); if(c % g != 0) return false; LL d = c / g; x *= d; y *= d; auto dx = b / g; auto dy = a / g; auto t = T{0}; if(x < 0) t = -x / dx + 1; else t = -x / dx; x += t * dx; if(!zero && x == 0){ ++t; x += dx; } y -= t * dy; return (y < 0 || (y == 0 && zero)); } int main(){ cin.tie(0); ios_base::sync_with_stdio(false); LL A, B; cin >> A >> B; if(B % A == 0){ cout << -1 << endl; } else{ LL ans = 1e18; for(LL a=1;a<A;++a){ LL x, y; if(ext_gcd_n(A, B, a, x, y, false)){ DEBUG(a, A, x, B, y); LL g = A * x + B * y; if(x < a - y){ DEBUG(a, A, x, B, y); mini(ans, x * A); } } } cout << (ans < 1e18 ? ans : -1) << endl; } return 0; }
0
CPP