solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; set<int> s[n + 1]; for (int i = 1; i <= n; i++) { int t; cin >> t; s[t].insert(i); } int res = n - 1; for (int i = 1; i <= m; i++) { for (int a : s[i]) if (s[i].find(a + 1) != s[i].end()) res--; } cout << res << "\n"; for (int i = 0; i < m - 1; i++) { int a, b; cin >> a >> b; if (s[a].size() < s[b].size()) swap(s[a], s[b]); for (int x : s[b]) { if (s[a].find(x - 1) != s[a].end()) res--; if (s[a].find(x + 1) != s[a].end()) res--; } for (int x : s[b]) s[a].insert(x); cout << res << "\n"; } return 0; }
11
CPP
import sys def make_gen(str): arr = str.split(" ") for item in arr: yield int(item) s = input() s = make_gen(s) n = next(s) k = next(s) disc_price = [0] * n norm_price = [0] * n s = input() s = make_gen(s) for i in range(0, n): disc_price[i] = next(s) s = input() s = make_gen(s) for i in range(0, n): norm_price[i] = next(s) total = sum(norm_price) new_price = [] for i in range(0, n): new_price.append(norm_price[i] - disc_price[i]) new_price.sort(reverse=True) win_sum = 0 for i in range(0, k): win_sum += new_price[i] while k < n and new_price[k] > 0: win_sum += new_price[k] k += 1 print(total - win_sum)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; string ans = string(); ans += string(gy - sy, 'U') + string(gx - sx, 'R'); ans += string(gy - sy, 'D') + string(gx - sx, 'L'); ans += 'L' + string(gy - sy + 1, 'U') + string(gx - sx + 1, 'R') + 'D'; ans += 'R' + string(gy - sy + 1, 'D') + string(gx - sx + 1, 'L') + 'U'; cout << ans << endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; // infty #define ULLINF (ULLONG_MAX) #define LLINF (LLONG_MAX) #define IINF (INT_MAX) #define INF (1<<29) // math #define Sq(x) ((x)*(x)) // container utility #define ALL(x) (x).begin(), (x).end() #define MP make_pair #define PB push_back #define EACH(it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();it++) // rep #define REP(i,a,b) for(int i=a;i<b;i++) #define rep(i,n) REP(i,0,n) // typedef typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<PII> VII; typedef vector<VI> VVI; typedef long long ll; // pair util #define FST first #define SND second // range validator #define CK(n,a,b) (a<=n && n<b) // conversion template<class T> inline string toStr(T a) { ostringstream oss_; oss_ << a; return oss_.str(); } inline int toInt(string s) { return atoi(s.c_str()); } // prime bool isPrime(int a) { for(int i=2; i*i <=a; i++) if(a%i == 0) return false; return true; } int const dx[] = {-1,0,1,0,-1,1,1,-1}; int const dy[] = {0,-1,0,1,-1,-1,1,1}; ////////////////////////////////////////////////////////////// #define EPS (1e-5) typedef complex<double> P; typedef pair<double, P> C; typedef pair<P, P> S; double dot(P a, P b) { return a.real()*b.real()+a.imag()*b.imag(); } double cross(P a, P b) { return a.real()*b.imag()-a.imag()*b.real(); } typedef S Line; double distanceLP(Line l, P a) { P base = l.SND-l.FST; return abs(cross(base, a-l.FST)) / abs(base); } double distanceSP(S s, P a) { { P X = s.SND - s.FST, Y = a - s.FST; if(dot(X, Y) < EPS) return abs(Y); } { P X = s.FST - s.SND, Y = a - s.SND; if(dot(X, Y) < EPS) return abs(Y); } return distanceLP(s, a); } int main() { while(1) { int N; cin >> N; if(N == 0) break; vector<C> circles(N); for(int i=0; i<N; i++) { double x, y, r; cin >> x >> y >> r; circles[i] = C(r, P(x, y)); } int Q; cin >> Q; while(Q--) { double x, y; cin >> x >> y; P taro(x, y); cin >> x >> y; P devil(x, y); bool ok = 0; for(int idx=0; idx<N; idx++) { if( distanceSP( S(taro, devil), circles[idx].SND ) < circles[idx].FST + EPS ) { if( abs(taro-circles[idx].SND) > circles[idx].FST || abs(devil-circles[idx].SND) > circles[idx].FST ) ok = 1; } } if(ok) cout << "Safe" << endl; else cout << "Danger" << endl; } } return 0; }
0
CPP
N, A, B = map(int, input().split()) v = [int(i) for i in input().split()] v.sort() v.reverse() print("{:.7f}".format(sum(v[0: A]) / A)) cnt = 0 target = v[A - 1] first = -1 for i, vv in enumerate(v): if vv == target: if first == -1: first = i cnt += 1 def comb(n, r): import math return math.factorial(n) // math.factorial(r) // math.factorial(n - r) if first == 0: # cnt C A + ... + cnt C min(cnt, B) ans = 0 for i in range(A, min(cnt, B) + 1): ans += comb(cnt, i) print(ans) else: print(comb(cnt, A - first))
0
PYTHON3
N, M = map(int, input().split()) if M & 1: print(M, M+1) m=M//2 for i in range(m): print(i+1, (-2-i)%N+1) print(i+m+1, 2*M-m-i)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; inline long long getint() { long long ssum = 0, ff = 1; char ch; for (ch = getchar(); !isdigit(ch) && ch != '-'; ch = getchar()) ; if (ch == '-') ff = -1, ch = getchar(); for (; isdigit(ch); ch = getchar()) ssum = ssum * 10 + ch - '0'; return ssum * ff; } const long long M = 1e6 + 5, mod = 1e9 + 7; long long ans, f[M], g[M]; long long n, t[3]; signed main() { cin >> n; for (long long i = 1; i <= n; i++) t[getint()]++; f[0] = 1; f[1] = 1; for (long long i = 2; i <= t[1]; i++) (f[i] = f[i - 1] + f[i - 2] * (i - 1)) %= mod; g[0] = f[t[1]]; for (long long i = 1; i <= t[2]; i++) (g[i] = g[i - 1] * (t[1] + i)) %= mod; cout << g[t[2]] << endl; return 0; }
10
CPP
x,y,l,r=map(int,input().split()) fx=[1] fy=[1] while (fx[-1]<=r): fx.append(fx[-1]*x) while (fy[-1]<=r): fy.append(fy[-1]*y) f=[l-1,r+1] for i in fx: for j in fy: f.append(i+j) f.sort() ans=0 for i in range(len(f)-1): if (f[i+1]-f[i]>1 and f[i+1]>l and f[i]<r): ans=max(ans,min(r,f[i+1]-1)-max(l,f[i]+1)+1) print(ans)
8
PYTHON3
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase 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") # ------------------- fast io -------------------- testcases=int(input()) for j in range(testcases): n=int(input()) vala=[int(s) for s in input()] valb=[int(s) for s in input()] outy=[] for s in range(n-1,-1,-1): if vala[s]==valb[s]: continue else: #2 checks if vala[0]!=valb[s]: for b in range(s+1): vala[b]+=1 vala[b]=vala[b]%2 for b in range((s)//2 +1): vala[b],vala[s-b]=vala[s-b],vala[b] outy.append(s+1) else: vala[0]+=1 vala[0]=vala[0]%2 outy.append(1) for b in range(s+1): vala[b]+=1 vala[b]=vala[b]%2 for b in range((s)//2 +1): vala[b],vala[s-b]=vala[s-b],vala[b] outy.append(s+1) if len(outy)>0: outy=[len(outy)]+outy print(*outy) else: print(0) #0110011011 #1110011011 #0001100100 #0010011000 #1000110100
7
PYTHON3
Rooms=int(input()) count=0 for i in range(Rooms): people_living,total_capacity=map(lambda x:int(x),input().split()) if (total_capacity-people_living)>=2: count+=1 print(count)
7
PYTHON3
n,p,w,d=[int(x) for x in input().split()] for i in range(0,1000000): num=p-(d*i) if num<0: continue if num%w: continue cnt=i+(num//w) if cnt>n: continue print(num//w,i,n-cnt) exit() print(-1)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * f; } const int N = 1e5 + 5; int n, suma = 0, sumb = 0, a[17], b[17], cnta[1 << 16], cntb[1 << 16], f[1 << 16][200]; int main() { memset(f, -1, sizeof(f)); f[0][0] = 0; cin >> n; int maxn = (1 << n) - 1, res = 214111111; char ch; for (int i = 0; i < n; i++) { scanf(" %c", &ch); a[i] = read(); b[i] = read(); suma += a[i]; sumb += b[i]; for (int j = 0; j <= maxn; j++) { if (j >> i & 1) { if (ch == 'R') cnta[j]++; else cntb[j]++; } } } for (int S = 0; S <= maxn; ++S) { for (int j = 0; j <= 150; j++) { if (f[S][j] == -1) continue; for (int i = 0; i < n; i++) { if (S >> i & 1) continue; int T = S | (1 << i); int ma = min(a[i], cnta[S]), mb = min(b[i], cntb[S]); f[T][j + ma] = max(f[T][j + ma], f[S][j] + mb); } } } for (int i = 0; i <= 150; i++) if (~f[maxn][i]) res = min(res, max(suma - i, sumb - f[maxn][i])); cout << res + n; return 0; }
9
CPP
# -*- coding: utf-8 -*- """ Created on Thu Oct 22 09:59:43 2020 @author: SAIGANESH """ t=int(input()) for i in range(t): n=int(input()) s=list(map(int,input().split())) ct=0 for i in s: if(i%2!=0): ct+=1 if(ct%2!=0 or (ct>0 and n-ct>0)): print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 250, p = 1e9 + 7; void inc(int &x, int y) { x += y; if (x >= p) x -= p; } int n, m; char s[N][N]; int dp[2][1 << 15][2][2]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", s[i]); if (n <= m) { for (int i = 0; i < m; i++) for (int j = 0; j < i; j++) swap(s[j][i], s[i][j]); swap(n, m); } int x = 0, y = 1, U = 1 << m; dp[x][0][0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++, swap(x, y)) { memset(dp[y], 0, U * sizeof(dp[y][0])); for (int S = 0; S < U; S++) for (int a = 0; a < 2; a++) for (int b = 0; b < 2; b++) if (s[i][j] == 'x') { inc(dp[y][S & ~(1 << j)][a][0], dp[x][S][a][b]); } else { if ((S >> j & 1) || b) inc(dp[y][S][a][b], dp[x][S][a][b]); else if (!a) inc(dp[y][S][1][b], dp[x][S][a][b]); inc(dp[y][S | (1 << j)][a][1], dp[x][S][a][b]); } } for (int S = 0; S < U; S++) for (int a = 0; a < 2; a++) { dp[y][S][a][0] = dp[x][S][a][0]; inc(dp[y][S][a][0], dp[x][S][a][1]); dp[y][S][a][1] = 0; } swap(x, y); } int ans = 0; for (int S = 0; S < U; S++) for (int a = 0; a < 2; a++) inc(ans, dp[x][S][a][0]); printf("%d\n", ans); return 0; }
12
CPP
#include<cstdio> char s[105]; int main(){ int n,i,sum=0; scanf("%d%s",&n,s); for(i=0;i<n;++i)if(s[i]=='R')++sum; puts(sum>(n>>1)?"Yes":"No"); return 0; }
0
CPP
def read(): n = int(input()) a = [] for i in range(n): start, end = map(int, input().split()) a.append((start, end)) return n, a def solve(n, a): # if n == 1: # return 0 a.sort(key=lambda p: p[0], reverse=True) last_start = a[0][0] a.sort(key=lambda p: p[1]) first_end = a[0][1] # start, end = a[0] # for i in range(1, n): # start_i, end_i = a[i] # if start < start_i: # start = start_i if start_i <= end else end # if start_i > end: # end = start_i if last_start > first_end: return last_start - first_end else: return 0 for i in range(int(input())): result = solve(*read()) print(result)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<string> v[1000001]; char str[1000001]; int max_nivel = 1; void read(int nivel) { max_nivel = max(max_nivel, nivel); int filhos; scanf(",%[^,],%d", str, &filhos); v[nivel].push_back(str); while (filhos--) read(nivel + 1); } int main() { int filhos; scanf("%[^,],%d", str, &filhos); v[1].push_back(str); while (filhos--) read(2); int c; while ((c = getchar()) != '\n' && c != EOF) { scanf("%[^,],%d", str, &filhos); v[1].push_back(str); while (filhos--) read(2); } printf("%d\n", max_nivel); for (int i = 1; i < 1000001 && !v[i].empty(); i++) { for (auto s : v[i]) printf("%s ", s.c_str()); printf("\n"); } return 0; }
11
CPP
#include<bits/stdc++.h> using namespace std; #define int long long const int MOD = 1000000007; signed main(){ int n; cin >> n; list<int> L; for(int i = 0; i < n; i++){ int a; cin >> a; if(i % 2 == 0) L.push_back(a); else L.push_front(a); } if(n % 2 == 1) reverse(L.begin(), L.end()); for(auto x : L) cout << x << " "; cout << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int c = 0, n, m, z; cin >> n >> m >> z; for (int i = n; i <= z; i += n) { for (int j = m; j <= z; j += m) { if (i == j) c++; } } cout << c << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const double dinf = 1e200; void mytimer(string task) {} void ext(int c) {} int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < (n); ++i) { int a, b, t; cin >> a >> b >> t; if (a == b) { cout << t << '\n'; continue; } int res = abs(a - b); int t0 = (a < b) ? (a - 1) : (m - 1 + m - a); res += t + (2 * m - 2 - (t - t0 + 2 * m - 2) % (2 * m - 2)) % (2 * m - 2); cout << res << '\n'; } return 0; }
7
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long int myRand(long long int B) { return (unsigned long long)rng() % B; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int q; cin >> q; while (q--) { string s, t; cin >> s >> t; reverse(s.begin(), s.end()); reverse(t.begin(), t.end()); int del = 0; int j = 0; for (int i = 0; i < s.size() and j < t.size(); i++) { if (del > 0) { del--; } else { if (t[j] == s[i]) { j++; } else { del++; } } } if (j == t.size()) { printf("YES\n"); } else { printf("NO\n"); } } }
10
CPP
#include <bits/stdc++.h> using namespace std; vector<long long> v; int main() { v.push_back(1); long long x, i, xx = 10, c = 2, cc = 0; while (1) { i = 1; cc = c - 1; xx = 0; while (i <= c) { xx = xx + pow(2, cc); cc++; i++; } c++; if (xx <= 100000) { v.push_back(xx); } else { break; } } int sz = v.size(); long long nn; cin >> nn; for (int ii = sz - 1; ii >= 0; ii--) { if (v[ii] <= nn && nn % v[ii] == 0) { cout << v[ii] << endl; return 0; } } return 0; }
8
CPP
import sys A,B=map(int,input().split()) for i in range(1010): if int(i*0.1)==B and int(i*0.08)==A: print(i) sys.exit() print(-1)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 5000 + 10; const int P = 1e9 + 7; int n, x[MAX_N], y[MAX_N]; int mark[MAX_N]; int dis(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]); } bool dfs(int u, int w) { bool ok = true; for (int i = 0; i < n; i++) { if (mark[i] == 0 && dis(i, u) > w) mark[i] = 3 - mark[u], ok &= dfs(i, w); else if (mark[i] == mark[u] && dis(i, u) > w) ok = false; } return ok; } void dfs1(int u, int w) { mark[u] = 1; for (int i = 0; i < n; i++) if (mark[i] == 0 && dis(i, u) > w) dfs1(i, w); } bool check(int w) { bool ok = true; memset(mark, 0, sizeof mark); for (int i = 0; i < n; i++) if (mark[i] == 0) mark[i] = 1, ok &= dfs(i, w); return ok; } int main() { ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; int L = -1, R = 2 * MAX_N; while (L + 1 < R) { int m = (L + R) / 2; check(m) ? R = m : L = m; } int ans = 1; cout << R << endl; memset(mark, 0, sizeof mark); for (int i = 0; i < n; i++) if (mark[i] == 0) dfs1(i, R), ans *= 2, ans %= P; cout << ans << endl; return 0; }
11
CPP
N, K = map(int, input().split()) A = list(map(int, input().split())) pos = [] neg = [] for v in A: if v >= 0: pos.append(v) else: neg.append(-v) pos.sort(reverse=True) neg.sort(reverse=True) MOD = 10 ** 9 + 7 ppos = [1] * (len(pos)+1) pneg = [1] * (len(neg)+1) for i, v in enumerate(pos): ppos[i+1] = ppos[i] * v % MOD for i, v in enumerate(neg): pneg[i+1] = pneg[i] * v % MOD i = j = 0 while i + j < K: if i == len(pos): j += 1 elif j == len(neg) or pos[i] >= neg[j]: i += 1 else: j += 1 if j % 2 == 1: v = (-1, -1, -1) if i and j < len(neg): v = (neg[j-1] * neg[j], i-1, j+1) if i < len(pos) and j: v = max(v, (pos[i-1] * pos[i], i+1, j-1)) if v[0] == -1: X = pos + neg X.sort() r = 1 for v in X[:K]: r = r * v % MOD print(-r % MOD) raise SystemExit i, j = v[1:] r = 1 for x in pos[:i]: r = r * x % MOD for x in neg[:j]: r = r * x % MOD print(r)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const long long maxn = (long long)1e6 + 6; const long double EPS = 1e-9; const long long INF = (long long)1e18 + 18; const long long mod = (long long)1e9 + 7; long long n, q; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); a.resize(distance(a.begin(), unique(a.begin(), a.end()))); n = (long long)a.size(); vector<long long> d(n - 1); for (long long i = 1; i < n; i++) { d[i - 1] = a[i] - a[i - 1]; } sort(d.begin(), d.end()); vector<long long> prefix(n - 1); for (long long i = 0; i < n - 1; i++) { if (i == 0) { prefix[i] = d[i]; } else { prefix[i] = prefix[i - 1] + d[i]; } } cin >> q; while (q--) { long long l, r; cin >> l >> r; long long segmentLength = r - l + 1; long long cut = upper_bound(d.begin(), d.end(), segmentLength) - d.begin(); long long ans = (cut > 0 ? prefix[cut - 1] : 0LL) + (n - cut) * segmentLength; cout << ans << " "; } cout << '\n'; return 0; }
10
CPP
from collections import Counter for _ in range(int(input())): for i in range(9): s=list(input());s[s.index('1')]='2' print(''.join(s))
10
PYTHON3
q = int(input()) for i in range(q): s = input() t = input() if len(s) == len(t): m = set(s) & set(t) if len(m) != 0: print('YES') else: print('NO') else: print('NO')
8
PYTHON3
import sys input = sys.stdin.readline for _ in range(int(input())): n,m = map(int, input().split()) D = {} for i in range(n): line = list(map(int, input().split())) D[line[0]] = line res = [] for i in range(m): colum = list(map(int, input().split())) if colum[0] in D: res = colum for i in res: print(*D[i])
8
PYTHON3
from fractions import gcd n = int(input()) field = [] for i in range(n): field.append([int(item) for item in input().split()]) val = field[0][0] for item in field[0][:]: val = gcd(val, item) def divisors(n): divisors = [] for i in range(1, int(n**0.5)+1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n//i) divisors.sort() return divisors div = divisors(val) for item in div: a = field[0][1] // item b = field[0][2] // item c = field[1][2] if a * b == c: ans = [item] for ret in field[0][1:]: ans.append(ret // item) break print(" ".join([str(item) for item in ans]))
8
PYTHON3
for _ in range (int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) b.sort(reverse = True) a.sort() c = 0 i = 0 s = 0 #print(a, b) while (i < n): if a[i] < b[i] and c < k: s += b[i] c += 1 else: s += a[i] i += 1 print(s)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7, INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n) { long long r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } long long inv(long long first) { return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; } inline int rd() { int first = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar(); return first; } const int N = 3e6 + 10; int n; char s[N]; int f[10][10][10]; int main() { scanf("%s", s + 1); n = strlen(s + 1); memset(f, 0x3f, sizeof f); for (int i = 0; i <= 9; ++i) for (int j = 0; j <= 9; ++j) { for (int ii = 0; ii <= 9; ++ii) for (int jj = 0; jj <= 9; ++jj) if (!i || !j || ii || jj) { f[i][j][(i * ii + j * jj) % 10] = min(f[i][j][(i * ii + j * jj) % 10], ii + jj); } } for (int i = 0; i <= 9; ++i) { for (int j = 0; j <= 9; ++j) { int now = 0, ans = 0; for (int k = 1; k <= n; ++k) { if (k == 1 && s[k] == '0') continue; else if (k == 1) ++ans; int delta = (s[k] - '0' - now) % 10; if (delta < 0) delta += 10; if (s[k] == s[k - 1]) ans += max(0, f[i][j][0] - 1); else ans += max(0, f[i][j][delta] - 1); now = s[k] - '0'; if (ans >= INF - 10) break; } if (ans >= INF - 10) ans = -1; printf("%d ", ans); } putchar(10); } }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << p.first << " " << p.second; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { for (T i : v) os << i << ", "; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> second) { for (T i : second) os << i << ", "; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, map<T1, T2> m) { for (pair<T1, T2> i : m) os << i << endl; return os; } int dist[1000101]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N, M; cin >> N >> M; int a[N], b[N], c[M], d[M]; for (int i = 0; i < N; i++) { cin >> a[i] >> b[i]; } for (int i = 0; i < M; i++) { cin >> c[i] >> d[i]; } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (c[j] - a[i] >= 0) { dist[c[j] - a[i]] = max(dist[c[j] - a[i]], d[j] - b[i] + 1); } } } int mx = 0; int ans = INT_MAX; for (int i = 1000100; i >= 0; i--) { mx = max(dist[i], mx); ans = min(ans, i + mx); } cout << ans << endl; return 0; }
10
CPP
t=int(input()) while t>0: n=int(input()) if n%2==0: n=n-1 print(int(n/2)) t=t-1
7
PYTHON3
from collections import Counter for _ in range(int(input())): n, m = map(int, input().split()) cm = [Counter() for _ in range(m)] for _ in range(n): s = input() for i in range(m): cm[i][s[i]] += 1 for _ in range(n-1): s = input() for i in range(m): cm[i][s[i]] -= 1 print(''.join(list(i.elements())[0] for i in cm), flush=True)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; struct info { int val, lev, sum, cnt, lft, pval, chk; } lar[100001]; struct info2 { int lev, val; } arr[1000001]; vector<int> mat[100001], cost[100001]; int st, stk[100000], top, len, blen, vld; void bfs(int x, int lvl) { int v = 0; lar[x].sum = 0; lar[x].lev = lvl; lar[x].cnt = 1; lar[x].chk = 1; while (top != -1) { for (int i = 1; i <= mat[x][0]; i++) { if (lar[mat[x][i]].cnt == 0) { stk[top] = mat[x][i]; top++; lar[mat[x][i]].sum = max(0, v + cost[x][i]); lar[mat[x][i]].lev = lvl + 1; lar[mat[x][i]].cnt++; if (lar[x].chk == 0 || (lar[mat[x][i]].sum > lar[mat[x][i]].val)) { lar[mat[x][i]].chk = 0; len++; } else lar[mat[x][i]].chk = 1; } } top--; if (top == -1) break; x = stk[top]; lvl = lar[x].lev; v = lar[x].sum; } } int main() { ios::sync_with_stdio(false); int i, j, x, y, n, m, z, a, b, c; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &lar[i].val); } for (i = 1; i <= n; i++) { mat[i].clear(); cost[i].clear(); mat[i].push_back(0); cost[i].push_back(0); } for (i = 1; i < n; i++) { a = i + 1; scanf("%d", &b); scanf("%d", &c); mat[a][0] = mat[a][0] + 1; mat[b][0] = mat[b][0] + 1; mat[a].push_back(b); mat[b].push_back(a); cost[a].push_back(c); cost[b].push_back(c); } top = 0; stk[top] = 1; bfs(1, 1); cout << len; return 0; }
9
CPP
l = [[],[],[],[],[]] for i in range(5): l[i].extend(list(map(int,input().split()))) cnt=1 c=-1 for x in l: if 1 in x: c=x.index(1)+1 break cnt=cnt+1 a=3-cnt b=3-c print(abs(a)+abs(b))
7
PYTHON3
n,k=map(int,input().split()) lst=list(map(int,input().split())) lst.sort() if k==0: if lst[0]>1: print(1) else: print(-1) else: j=lst[k-1] if(k!=n and lst[k]==j): print(-1) else: print(j)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; string convertTostr(unsigned long long x) { stringstream ss; ss << x; string st; st = ss.str(); return st; } unsigned long long convertToint(string y) { unsigned long long num; stringstream ss(y); ss >> num; return num; } unsigned long long fpow(unsigned long long b, unsigned long long p, unsigned long long m) { if (p == 0) return 1; if (p == 1) return b % m; unsigned long long res = fpow(b, p / 2, m) % m; res = (res * res) % m; if (p % 2 != 0) res = (res * (b % m)) % m; return res; } long long gcd(long long a, long long b) { while (1) { if (b == 0) return a; if (a == 0) return b; if (a < 0) a = a * -1; else if (b < 0) b *= -1; else if (a > b) swap(a, b); int tmp = a; a = b % a; b = tmp; } } long long gcdnum(vector<long long> vec) { long long ret = vec[0]; for (int i = 1; i < vec.size(); i++) { ret = gcd(ret, vec[i]); } return ret; } const int MAX = 500; void toarr(string str, int arr[MAX]) { int len = str.size() - 1; for (int i = len; i >= 0; i--) { arr[len - i] = str[i] - '0'; } } int getlen(int arr[MAX]) { for (int i = MAX - 1; i >= 0; i--) { if (arr[i] != 0) return i; } return 0; } void product(int n1[MAX], int n2[MAX], int result[MAX]) { int len1 = getlen(n1); int len2 = getlen(n2); if (len1 < len2) return product(n2, n1, result); int ind = 0; for (int i = 0; i <= len2; i++) { int pos = 0; for (int j = 0; j <= len1; j++) { result[pos + ind] += n1[j] * n2[i]; if (result[pos + ind] > 9) { result[pos + ind + 1] += result[pos + ind] / 10; result[pos + ind] = result[pos + ind] % 10; } pos++; } ind++; } } void add(int n1[MAX], int n2[MAX], int result[MAX]) { int len1 = getlen(n1); int len2 = getlen(n2); if (len1 < len2) return add(n2, n1, result); int carry = 0; for (int i = 0; i < MAX; i++) { result[i] = n1[i] + n2[i] + carry; carry = result[i] / 10; result[i] %= 10; } } void display(int arr[MAX]) { int len = getlen(arr); for (int i = len; i >= 0; i--) cout << arr[i]; cout << endl; } void display2(int arr[MAX]) { int len = getlen(arr); int ind = 0; while (arr[ind] == 0) ind++; for (int i = ind; i <= len; i++) cout << arr[i]; cout << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int n, p; int arr[400]; memset(arr, 0, sizeof arr); cin >> p >> n; int x; int ans = -1; for (int i = 0; i < n; i++) { cin >> x; x = x % p; if (arr[x] != 0 && ans == -1) ans = i + 1; arr[x] = 1; } cout << ans; return 0; }
7
CPP
weight = int(input()) if(weight>=0and weight !=2): if(weight%2==0): print('YES') else: print('NO') else: print('NO')
7
PYTHON3
N, K = (int(i) for i in input().split()) A = [int(i) for i in input().split()] V = [-1]*N W = [-1]*N x = 0 V[0] = 0 W[0] = 0 for i in range(1, K+1): x = A[x] - 1 W[i] = x if V[x] == -1: V[x] = i else: x = W[V[x] + (K-i) % (i -V[x])] break print(x+1)
0
PYTHON3
N, M = map(int, input().split()) parent = [i for i in range(N + M)] rank = [1] * (N + M) def find(x): if x == parent[x]: return x parent[x] = find(parent[x]) return parent[x] def unite(x, y): x, y = find(x), find(y) if x == y: return if rank[x] == rank[y]: rank[x] += 1 elif rank[x] < rank[y]: x, y = y, x parent[y] = x for i in range(N): for l in list(map(int, input().split()))[1:]: # 言語はparentの後半 unite(i, N + l - 1) if all(find(0) == find(i) for i in range(N)): print('YES') else: print('NO')
0
PYTHON3
mydict = {} n = int(input()) for i in range(n): val = input() if val not in mydict: mydict[val] = [] mydict[val].append(i) res = [0 for _ in range(n)] for key, value in mydict.items(): k = 0 for idx in value: if k == 0: res[idx] = "OK" k += 1 else: res[idx] = key + str(k) k += 1 for _ in res: print(_)
9
PYTHON3
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <queue> #include <map> #include <set> #include <stack> #include <cstring> #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define rep(i, n) for(int i = 0; i < (n); i++) #define MP make_pair #define X first #define Y second #define all(v) v.begin(), v.end() using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> P; const int INF = 1<<29; int dp[11][2001]; int main(){ int N, K; cin >> N >> K; vector<int> book[11]; rep(i, N){ int c, g; cin >> c >> g; book[g].push_back(c); } rep(i, 11) sort(book[i].rbegin(), book[i].rend()); rep(i, 11){ FOR(j, 1, book[i].size()){ book[i][j] += book[i][j-1] + j*2; } } FOR(i, 1, 11){ rep(j, K+1) dp[i][j] = dp[i-1][j]; FOR(j, 1, book[i].size()+1){ for(int k = 0; k+j <= K; k++){ dp[i][k+j] = max(dp[i][k+j], dp[i-1][k]+book[i][j-1]); } } } cout << dp[10][K] << endl; return 0; }
0
CPP
n = int(input()) C = 10 ** 9 + 7 memo = {0: (0, 0)} for i in range(1, n + 1): if i - 4 not in memo: memo[i - 4] = (C, C) if i - 7 not in memo: memo[i - 7] = (C, C) x1, y1 = memo[i - 4] x1, y1 = memo[i - 4] if x1 == y1 == C else (x1 + 1, y1) x2, y2 = memo[i - 7] x2, y2 = memo[i - 7] if x2 == y2 == C else (x2, y2 + 1) memo[i] = min((x1, y1), (x2, y2)) j, k = memo[n] print(-1) if j == k == C else print("4" * j + "7" * k)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int T, a, b, c, d; int x[5]; int main() { int i; scanf("%d", &T); while (T--) { scanf("%d%d%d%d", &a, &b, &c, &d); x[1] = a + b, x[2] = a + c, x[3] = d + b, x[4] = c + d; sort(x + 1, x + 5); printf("%d\n", x[3]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long int mi(long long int n, long long int m) { long long int pw = n; long long int ex = m - 2; long long int ans = 1; while (ex) { if (ex & 1) ans = ans * pw % m; pw = pw * pw % m; ex >>= 1; } return ans; } long long int pw(long long int a, long long int n) { long long int pw = a; long long int ex = n; long long int ans = 1; while (ex) { if (ex & 1) ans = ans * pw; pw = pw * pw; ex >>= 1; } return ans; } void show(long long int x) { cout << x << " "; } long long int gcd(long long int a, long long int b) { return b == 0 ? a : gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return a / gcd(a, b) * b; } long long int max(long long int a, long long int b) { return a > b ? a : b; } long long int min(long long int a, long long int b) { return a < b ? a : b; } long long int Abs(long long int x) { return x > 0 ? x : (-x); } bool comp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n, m; cin >> n >> m; vector<long long int> vec(n); for (int i = 0; i < n; i++) cin >> vec[i]; sort(vec.begin(), vec.end()); vector<pair<long long int, long long int> > dp(n); for (int i = 0; i < n; i++) { if (i == 0 || i == n - 1) { if (i == 0) { dp[i].first = vec[i] - 1; if (vec[i + 1] != vec[i] + 1) dp[i].second = vec[i] + 1; else dp[i].second = 1e18; } else { dp[i].second = vec[i] + 1; if (vec[i] - 1 != vec[i - 1]) dp[i].first = vec[i] - 1; else dp[i].first = -(1e18); } } else { if (vec[i] + 1 != vec[i + 1]) dp[i].second = vec[i] + 1; else dp[i].second = 1e18; if (vec[i] - 1 != vec[i - 1]) dp[i].first = vec[i] - 1; else dp[i].first = -(1e18); } } map<long long int, bool> mp; for (long long int i : vec) mp[i] = true; set<int> st; for (int i = 0; i < n; i++) st.insert(i); vector<long long int> rk; long long int cnt = 0; long long int ans = 0; while (cnt < m) { vector<long long int> tmp; for (auto it = st.begin(); it != st.end(); it++) { int i = *it; if (dp[i].first != -(1e18)) { if (mp.count(dp[i].first)) dp[i].first = -(1e18); else { mp[dp[i].first] = true; rk.push_back(dp[i].first); ans += Abs(vec[i] - dp[i].first); cnt++; if (mp.count(dp[i].first - 1)) dp[i].first = -(1e18); else dp[i].first--; if (cnt == m) break; } } if (dp[i].second != 1e18) { if (mp.count(dp[i].second)) dp[i].second = 1e18; else { mp[dp[i].second] = true; rk.push_back(dp[i].second); ans += Abs(dp[i].second - vec[i]); cnt++; if (mp.count(dp[i].second + 1)) dp[i].second = 1e18; else dp[i].second++; if (cnt == m) break; } } if (dp[i].first == -(1e18) && dp[i].second == 1e18) tmp.push_back(i); } for (long long int i : tmp) st.erase(i); } cout << ans << "\n"; for (long long int i : rk) show(i); }
10
CPP
#include <bits/stdc++.h> using namespace std; void _print(long long t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } void init_code() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void solve() { long long n; cin >> n; cout << n / 3 + (n % 3 == 1) << " " << n / 3 + (n % 3 == 2) << "\n"; } signed main() { init_code(); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
7
CPP
t = int(input()) for i in range(t): input() lst = list(map(int, input().split())) if len(lst) == 1: if lst[0] % 2 == 0: print(f"1\n1") else: print(-1) else: firstEven, secondEven = False, False if lst[0] % 2 == 0: firstEven = True if lst[1] % 2 == 0: secondEven = True if firstEven: print(f"1\n1") elif secondEven: print(f"1\n2") else: print(f"2\n1 2")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int N, K; struct XY { double x, y; XY() {} XY(double _x, double _y) { x = _x; y = _y; } bool operator<(XY P) const { return x < P.x || x == P.x && y < P.y; } } xy[300000]; map<XY, bool> M; double mx, my; bool check() { int _cnt = 0; int Ni = 0, Nj = N - 1; for (; Ni < N; Ni++) { while (Nj >= 0 && XY(2 * mx - xy[Ni].x, 2 * my - xy[Ni].y) < xy[Nj]) Nj--; if (Nj < 0 || xy[Nj] < XY(2 * mx - xy[Ni].x, 2 * my - xy[Ni].y)) _cnt++; } if (_cnt <= K) return true; else return false; } queue<double> print; int main() { scanf("%d %d", &N, &K); for (int Ni = 0; Ni < N; Ni++) scanf("%lf %lf", &xy[Ni].x, &xy[Ni].y); sort(xy, xy + N); if (K >= N) { puts("-1"); return 0; } int cnt = 0; for (int Ni = 0; Ni <= K; Ni++) for (int Nj = N - 1; Ni + N - 1 - Nj <= K; Nj--) { mx = (xy[Ni].x + xy[Nj].x) / 2; my = (xy[Ni].y + xy[Nj].y) / 2; if (M[XY(mx, my)]) continue; M[XY(mx, my)] = true; if (check()) { print.push(mx); print.push(my); cnt++; } } printf("%d\n", cnt); while (!print.empty()) { printf("%.9lf ", print.front()); print.pop(); printf("%.9lf\n", print.front()); print.pop(); } }
12
CPP
# bsdk idhar kya dekhne ko aaya hai, khud kr!!! # import math # from itertools import * # import random # import calendar # import datetime # import webbrowser # f = open("input.txt", 'r') # g = open("output.txt", 'w') # n, m = map(int, f.readline().split()) #!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def main(): pass # 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion string = input() sorted_string = sorted(set(string), key=string.index) for i in range(0, len(sorted_string)): if sorted_string[i] == chr(97 + i): continue else: exit(print("NO")) print("YES")
8
PYTHON3
n = int(input()) i = 1 word = [] while i <= n: word.append(input()) i += 1 for item in word: indx = word.index(item) if len(item) > 10: e = 1 length = str(len(item) - 2) while e < len(item) - 1: word[indx] = item[0] + length + item[len(item) - 1] e += 1 else : pass for item in word: print(item)
7
PYTHON3
N = int(input()) A = list(map(int, input().split())) A.sort() print(sum(A[2*i] for i in range(N)))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; void getre() { int x = 0; printf("%d\n", 1 / x); } void gettle() { int res = 1; while (1) res <<= 1; printf("%d\n", res); } template <typename T, typename S> inline bool upmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> inline bool upmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } template <typename N, typename PN> inline N flo(N a, PN b) { return a >= 0 ? a / b : -((-a - 1) / b) - 1; } template <typename N, typename PN> inline N cei(N a, PN b) { return a > 0 ? (a - 1) / b + 1 : -(-a / b); } template <typename N> N gcd(N a, N b) { return b ? gcd(b, a % b) : a; } template <typename N> inline int sgn(N a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); } inline void gn(long long &x) { int sg = 1; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; c == '-' ? (sg = -1, x = 0) : (x = c - '0'); while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0'; x *= sg; } inline void gn(int &x) { long long t; gn(t); x = t; } inline void gn(unsigned long long &x) { long long t; gn(t); x = t; } inline void gn(double &x) { double t; scanf("%lf", &t); x = t; } inline void gn(long double &x) { double t; scanf("%lf", &t); x = t; } inline void gs(char *s) { scanf("%s", s); } inline void gc(char &c) { while ((c = getchar()) > 126 || c < 33) ; } inline void pc(char c) { putchar(c); } inline long long sqr(long long a) { return a * a; } inline double sqrf(double a) { return a * a; } const long long inf = 0x3f3f3f3f3f3f3f3fll; const double pi = 3.14159265358979323846264338327950288L; const double eps = 1e-6; namespace tar { int n; bool recha[622222]; struct edge { int v, next; } e[3660005]; int etot; int g[622222], gt[622222], dom[622222]; inline void ae(int *g, int u, int v) { e[etot].v = v; e[etot].next = g[u]; g[u] = etot++; } int dfn[622222], ind, id[622222]; int pre[622222], idom[622222], semi[622222]; int f[622222], best[622222]; int get(int x) { if (x == f[x]) return x; int y = get(f[x]); if (semi[best[x]] > semi[best[f[x]]]) best[x] = best[f[x]]; return f[x] = y; } void dfs(int u) { id[dfn[u] = ++ind] = u; for (int i = g[u]; ~i; i = e[i].next) if (!dfn[e[i].v]) dfs(e[i].v), pre[dfn[e[i].v]] = dfn[u]; } void tarjan() { for (int j = ind, u; u = id[j], j >= 2; j--) { for (int i = gt[u], v; v = dfn[e[i].v], ~i; i = e[i].next) { if (!v) continue; get(v); if (semi[best[v]] < semi[j]) semi[j] = semi[best[v]]; } ae(dom, semi[j], j); int x = f[j] = pre[j]; for (int i = dom[x], z; z = e[i].v, ~i; i = e[i].next) { get(z); if (semi[best[z]] < x) idom[z] = best[z]; else idom[z] = x; } dom[x] = -1; } for (int i = 2; i <= ind; i++) { if (semi[i] != idom[i]) idom[i] = idom[idom[i]]; ae(dom, idom[i], i); } } int ans[222222]; void calc(int u) { ans[u] = 1; for (int i = dom[dfn[u]]; ~i; i = e[i].next) { calc(id[e[i].v]); ans[u] += ans[id[e[i].v]]; } } int work(int s) { for (int i = 1; i <= n; i++) { f[i] = best[i] = semi[i] = i; } dfs(s); tarjan(); calc(s); int ma = 0; for (int i = (1), _ed = (n + 1); i < _ed; i++) if (i != s && recha[i]) upmax(ma, ans[i]); return ma; } }; // namespace tar namespace dij { struct edge { int v, next, w; } e[666666]; int etot = 0; int g[222222]; void ae(int u, int v, int w) { e[etot].v = v; e[etot].w = w; e[etot].next = g[u]; g[u] = etot++; } long long dis[222222]; int n, s; struct node { long long d; int v; }; int operator<(const node &a, const node &b) { return a.d > b.d; } priority_queue<node> qu; void work() { memset((g), (-1), sizeof(g)); gn(n); int m; gn(m); gn(s); while (m--) { int u, v, w; gn(u); gn(v); gn(w); ae(u, v, w); ae(v, u, w); } memset((dis), (63), sizeof(dis)); dis[s] = 0; qu.push((node){0, s}); while (!qu.empty()) { node x = qu.top(); qu.pop(); int u = x.v; if (dis[u] != x.d) continue; for (int i = g[u]; ~i; i = e[i].next) if (upmin(dis[e[i].v], dis[u] + e[i].w)) { qu.push((node){dis[e[i].v], e[i].v}); } } memset((tar::g), (-1), sizeof(tar::g)); memset((tar::gt), (-1), sizeof(tar::gt)); memset((tar::dom), (-1), sizeof(tar::dom)); for (int u = (1), _ed = (n + 1); u < _ed; u++) if (dis[u] < inf) { tar::recha[u] = 1; for (int i = g[u]; ~i; i = e[i].next) if (dis[u] + e[i].w == dis[e[i].v]) { tar::ae(tar::g, u, e[i].v); tar::ae(tar::gt, e[i].v, u); } } tar::n = n; printf("%d\n", tar::work(s)); } }; // namespace dij int main() { dij::work(); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; vector<int> v[500005], in(500005, 0), out(500005, 0), depth(500005, 0), dep[500005]; map<pair<long long int, long long int>, long long int> mp; void dfs() { queue<pair<int, int>> q; depth[1] = 1; dep[1].push_back(1); q.push(make_pair(1, 1)); while (!q.empty()) { pair<int, int> now = q.front(); q.pop(); for (int i = 0; i < (int)v[now.first].size(); i++) { depth[v[now.first][i]] = now.second + 1; dep[now.second + 1].push_back(v[now.first][i]); q.push(make_pair(v[now.first][i], now.second + 1)); } } } int t = 0; string str; void get_in_out(int start) { in[start] = t++; for (int i = 0; i < v[start].size(); i++) get_in_out(v[start][i]); out[start] = t++; } int isok(int a, int b) { return (in[b] <= in[a] && out[b] >= out[a]); } int isleft(int a, int b) { return out[a] < in[b]; } int run(int ver, int h) { if (depth[ver] >= h) { mp[{ver, h}] = 1; printf("Yes\n"); return 0; } int ans1 = -1, ans2 = -1; int l = 0, r = dep[h].size() - 1; while (l <= r) { int m = (l + r) >> 1; if (isok(dep[h][m], ver)) { ans1 = m, r = m - 1; } else if (isleft(dep[h][m], ver)) { l = m + 1; } else { r = m - 1; } } if (ans1 == -1) { mp[{ver, h}] = 1; printf("Yes\n"); return 0; } l = 0, r = dep[h].size() - 1; while (l <= r) { int m = (l + r + 1) >> 1; if (isok(dep[h][m], ver)) { ans2 = m, l = m + 1; } else if (isleft(dep[h][m], ver)) { l = m + 1; } else { r = m - 1; } } if (ans2 == -1) { puts("Yes"); return 0; } vector<int> alpha(26, 0); for (int i = ans1; i <= ans2; i++) { alpha[str[dep[h][i] - 1] - 'a']++; } int x = 0; for (int i = 0; i < 26; i++) { if (alpha[i] & 1) { x++; } } if (x <= 1) { mp[{ver, h}] = 1; printf("Yes\n"); return 0; } else { printf("No\n"); mp[{ver, h}] = -1; return 0; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (long long int i = 2; i <= n; i++) { int a; cin >> a; v[a].push_back(i); } cin >> str; dfs(); get_in_out(1); while (m--) { int ver, h; cin >> ver >> h; int x = mp[{ver, h}]; if (x == 1) { printf("Yes\n"); } else if (x == -1) { printf("No\n"); } else run(ver, h); } return 0; }
10
CPP
#include "bits/stdc++.h" using namespace std; string s1,s2,s3,ans; int n,t; int main(){ cin>>t; while(t--){ cin>>n; ans.clear(); int i=0,j=0,k=0; cin>>s1>>s2>>s3; while(i<2*n&&j<2*n&&k<2*n){ if(s1[i]=='0'&&s2[j]=='0'&&s3[k]=='0') i++,j++,k++,ans.push_back('0'); if(s1[i]=='0'&&s2[j]=='0'&&s3[k]=='1') i++,j++,ans.push_back('0'); if(s1[i]=='0'&&s2[j]=='1'&&s3[k]=='0') i++,k++,ans.push_back('0'); if(s1[i]=='1'&&s2[j]=='0'&&s3[k]=='0') j++,k++,ans.push_back('0'); if(s1[i]=='1'&&s2[j]=='1'&&s3[k]=='1') i++,j++,k++,ans.push_back('1'); if(s1[i]=='1'&&s2[j]=='1'&&s3[k]=='0') i++,j++,ans.push_back('1'); if(s1[i]=='1'&&s2[j]=='0'&&s3[k]=='1') i++,k++,ans.push_back('1'); if(s1[i]=='0'&&s2[j]=='1'&&s3[k]=='1') j++,k++,ans.push_back('1'); } if(i>j) swap(i,j),swap(s1,s2); if(j>k) swap(j,k),swap(s2,s3); if(i>j) swap(i,j),swap(s1,s2); while(j<2*n) ans.push_back(s2[j++]); while(ans.size()<3*n) ans.push_back('1'); cout<<ans<<"\n"; } return 0; }
7
CPP
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=3010; int a[maxn][maxn]; bool vis[maxn]; ll dp[3005][3005][4]; int main() { int n,m,k; cin>>n>>m>>k; for( int i=1;i<=k;i++ ) { int x,y,w; cin>>x>>y>>w; a[x][y]=w; } dp[1][1][1]=a[1][1]; for( int i=1;i<=n;i++ ) { for( int j=1;j<=m;j++ ) { if( i==1 && j==1 ) continue; ll maxup=max(dp[i-1][j][3],max(dp[i-1][j][2],max(dp[i-1][j][1],dp[i-1][j][0]))); dp[i][j][0]=max(maxup,dp[i-1][j][0]); for( int k=1;k<=3;k++ ) { dp[i][j][k]=max(dp[i][j-1][k-1]+a[i][j],dp[i][j-1][k]); } dp[i][j][1]=max(maxup+a[i][j],dp[i][j][1]); } } ll ans=max(dp[n][m][0],max(dp[n][m][1],max(dp[n][m][2],dp[n][m][3]))); cout<<ans<<endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; string multiply(string num1, string num2) { int len1 = num1.size(); int len2 = num2.size(); if (len1 == 0 || len2 == 0) return "0"; vector<int> result(len1 + len2, 0); int i_n1 = 0; int i_n2 = 0; for (int i = len1 - 1; i >= 0; i--) { int carry = 0; int n1 = num1[i] - '0'; i_n2 = 0; for (int j = len2 - 1; j >= 0; j--) { int n2 = num2[j] - '0'; int sum = n1 * n2 + result[i_n1 + i_n2] + carry; carry = sum / 10; result[i_n1 + i_n2] = sum % 10; i_n2++; } if (carry > 0) result[i_n1 + i_n2] += carry; i_n1++; } int i = result.size() - 1; while (i >= 0 && result[i] == 0) i--; if (i == -1) return "0"; string s = ""; while (i >= 0) s += std::to_string(result[i--]); return s; } int isPrime(long long int n) { if (n <= 1) return 0; for (long long int i = 2; i < n; i++) if (n % i == 0) return 0; return 1; } long long int modFact(long long int n, long long int p) { if (n >= p) return 0; long long int result = 1; for (long long int i = 1; i <= n; i++) result = (result * i) % p; return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { int n, k; string s; cin >> n >> k >> s; int zer = 0, one = 0; bool chk = true; for (int i = 0; i < k; i++) { int tmp = -1; for (int j = i; j < n; j += k) { if (s[j] != '?') { if (tmp != -1 && s[j] - '0' != tmp) { chk = false; break; } tmp = s[j] - '0'; } } if (tmp != -1) { (tmp == 0 ? zer : one)++; } } if (max(zer, one) > k / 2) { chk = false; } cout << (chk ? "YES\n" : "NO\n"); } return 0; }
7
CPP
# -*- coding: utf-8 -*- """ Created on Sun Aug 30 01:52:17 2020 @author: Dark Soul """ [n,k]=list(map(int,input().split())) s=input('') ob=k//2 cb=ob sol='' for i in range(n): if s[i]=='(': if ob: sol+=s[i] ob-=1 else: if cb: sol+=s[i] cb-=1 print(sol)
9
PYTHON3
#include<iostream> #include<string> #include<algorithm> #include<cmath> #include<vector> #include<stack> #include<climits> #include<cstring> #include<queue> using namespace std; typedef unsigned long long ull; const ull INF = ULLONG_MAX/3; void add(int s, int t, ull x, vector<ull> &xList, vector<ull> &sxList){ int nn = xList.size(); int sn = sxList.size(); int ss = s/sn; int tt = t/sn; if(ss == tt){ for (int i=0;i<t-s+1;i++){ xList[s+i] += x; } }else{ for(int i=s;i<(ss+1)*sn;i++){ xList[i] += x; } for(int i=tt*sn;i<t+1;i++){ xList[i] += x; } for(int i=ss+1;i<tt;i++){ sxList[i] += x; } } } ull get(int ind, vector<ull> &xList, vector<ull> &sxList){ int sn = sxList.size(); return xList[ind] + sxList[ind/sn]; } int main(){ int N, T, l ,r; cin >> N >> T; int sn = sqrt(T-1) + 1; vector<ull> xList(sn*sn, 0ull); vector<ull> sxList(sn, 0ull); for (int i=0;i<N;i++){ cin >> l >> r; add(l, r-1, 1, xList, sxList); } ull M = 0ull; for(int i=0;i<T;i++){ M = max(M, get(i, xList, sxList)); } cout << M << endl; return 0; }
0
CPP
import sys sys.setrecursionlimit(10**8) def ii(): return int(sys.stdin.readline()) def mi(): return map(int, sys.stdin.readline().split()) def ti(): return tuple(map(int, sys.stdin.readline().split())) def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)] def dp2(ini, i, j): return [[ini]*i for _ in range(j)] def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)] #import bisect #bisect.bisect_left(B, a) #from collections import defaultdict #d = defaultdict(int) d[key] += value #from itertools import accumulate #list(accumulate(A)) ## DP def solve(N): A = ti() dp = dp2(0, N, N) for i in range(N-1): if abs(A[i]-A[i+1]) < 2: dp[i][i+1] = 2 for h in range(2, N): #hは(区間の長さ)-1 for i in range(N): if i+h >= N: break j = i+h # 区間の長さが偶数の場合 if h % 2: if abs(A[i]-A[j]) < 2 and dp[i+1][j-1] == h-1: dp[i][j] = h + 1 continue else: dp[i][j] = max(tuple(dp[i][k]+dp[k+1][j] for k in range(i, j))) # 区間の長さが奇数の場合 else: dp[i][j] = max(dp[i+1][j], dp[i][j-1]) else: continue print(dp[0][N-1]) while True: N = ii() if N == 0: exit() solve(N)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n; cin >> n; vector<int> years(n); for (int i = 0; i < n; i++) { cin >> years[i]; } sort((years).begin(), (years).end()); cout << years[years.size() / 2]; return 0; }
7
CPP
n, l = map(int, input().strip().split(' ')) beam = [a for a in map(int, input().strip().split(' '))] beam.sort() maxspace = 2 * max((beam[0]), (l - beam[len(beam) - 1])) for i in range(n - 1): t = beam[i + 1] - beam[i] if t > maxspace: maxspace = t print(maxspace / 2)
8
PYTHON3
l1 = input().split() l2 = input().split() ''' l_in = open("CodeForcesTestInput.in", "r") l_out = open("CodeForcesTestOutput.out", "w") l1 = l_in.readline().split(" ") l2 = l_in.readline().split(" ") ''' n = int(l1[0]) h = int(l1[1]) a = [int(x) for x in l2] for x in a: if x > h: n += 1 print(n)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int i = 1; int b = 1; bool ok = false; while (b <= n) { int a = n - b; double dsqrt = sqrt(1 + 8.0 * a); int idsqrt = dsqrt; if (dsqrt == idsqrt && idsqrt != 1 && (idsqrt - 1) % 2 == 0) { printf("YES\n"); ok = true; break; } i++; b = (i * (i + 1)) / 2; } if (!ok) printf("NO\n"); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k1, k2, dp[110][110][2]; int solve(int a, int b, int c) { if (dp[a][b][c] != -1) { return dp[a][b][c]; } if (a + b == 0) { return dp[a][b][c] = 1 % 100000000; } dp[a][b][c] = 0; if (c) { for (int i = (int)(1); i <= (int)(min(b, k2)); i++) { dp[a][b][c] += solve(a, b - i, !c); dp[a][b][c] %= 100000000; } } else { for (int i = (int)(1); i <= (int)(min(a, k1)); i++) { dp[a][b][c] += solve(a - i, b, !c); dp[a][b][c] %= 100000000; } } return dp[a][b][c]; } int main() { while (scanf("%d%d%d%d", &n, &m, &k1, &k2) != EOF) { memset(dp, -1, sizeof(dp)); printf("%d\n", (solve(n, m, 0) + solve(n, m, 1)) % 100000000); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0) return 1; if (b == 1) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } long double media(long double sum, long double n) { return sum / n; } int32_t main() { long long n, k, m; cin >> n >> k >> m; long long v[n]; long double sum = 0.0; for (long long i = 0; i < n; i++) { cin >> v[i]; sum += v[i]; } sort(v, v + n); long double melhor = media(sum + min(m, n * k), (long double)n); for (long long i = 0; i < min(m, n - 1); i++) { sum -= v[i]; melhor = max(melhor, media(sum + min(m - i - 1, (n - i - 1) * k), (long double)(n - i - 1))); } cout << fixed << setprecision(20) << melhor << "\n"; }
8
CPP
#include <cstring> #include <iostream> using namespace std; long long modexp(int x, long long e, int m) { long long ans = 1, p = x % m; while (e > 0) { if (e % 2 != 0) ans = (ans * p) % m; p = (p * p) % m; e >>= 1; } return ans; } int main() { int p; cin >> p; int a[p]; for (int i = 0; i < p; i++) cin >> a[i]; int ans[p]; memset(ans, 0, sizeof(ans)); for (int i = 0; i < p; i++) ans[0] += a[i]; ans[p-1] -= a[0]; for (int i = 1; i < p; i++) { for (int j = 0; j < p; j++) { ans[j] -= a[i] * modexp(i, p-1-j, p); } } for (int i = 0; i < p; i++) cout << (ans[i] % p + p) % p << ' '; cout << endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e6; const long long mod = 1e9 + 7; const long long inf = 1e9; long long a[610][610], f[2][610][610]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n; cin >> n; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) { f[0][i][j] = -inf; f[1][i][j] = -inf; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; f[0][1][1] = a[1][1]; for (int k = 3; k <= n + n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int x = k % 2, y = 1 - x; if (k - i > n || k - j > n || k - i < 1 || k - j < 1) { f[x][i][j] = -inf; continue; } long long w = -inf; for (int dx = -1; dx <= 0; dx++) for (int dy = -1; dy <= 0; dy++) w = max(w, f[y][i + dx][j + dy]); f[x][i][j] = w; if (i != j) f[x][i][j] += a[i][k - i] + a[j][k - j]; else f[x][i][j] += a[i][k - i]; } } } cout << f[0][n][n]; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e+9 + 7; const long long INF = 0x7f7f7f7f7f7f7f7f; const int INFi = 0x7f7f7f7f; const long long MAXN = 2e+3 + 500; const long long MAXN1 = 2e+6 + 8; set<long long> sa, sb; void Divisors(long long n, set<long long>& s) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { s.insert(i); } } } int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n + 1]; int mark[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == i) mark[i] = 0; else mark[i] = 1; } int first = 1, last = n; while (mark[first] == 0 and first <= n) first++; while (mark[last] == 0 and last >= 1) last--; if (first == n + 1) { cout << 0 << endl; continue; } int flag = 0; for (int i = first; i <= last; i++) if (!mark[i]) { flag = 1; break; } if (flag) cout << 2 << endl; else cout << 1 << endl; } }
9
CPP
import heapq V, E = map(int, input().split()) edges = [[] for _ in range(V)] for _ in range(E): s, t, w = map(int, input().split()) edges[s-1].append([w, t-1]) edges[t-1].append([w, s-1]) # プリム法 def prim(n, w, edges): used = [True] * n #True:不使用 edgelist = [] for e in edges[0]: heapq.heappush(edgelist,e) used[0] = False res = 0 while len(edgelist) != 0: minedge = heapq.heappop(edgelist) if not used[minedge[1]]: continue v = minedge[1] used[v] = False for e in edges[v]: if used[e[1]]: heapq.heappush(edgelist,e) res += minedge[0] return res print(prim(V, E, edges))
0
PYTHON3
#tattii def lis(arr): n = len(arr) dp= [1]*n for i in range (1 , n): for j in range(0 , i): if arr[i] >= arr[j] : dp[i] = max(dp[i],dp[j]+1) return max(len(lst)-max(dp),0) a,b=map(int,input().strip().split()) lst=[] for i in range(a): x,y=map(float,input().strip().split()) lst.append(x) print(lis(lst))
8
PYTHON3
#include <vector> #include <iostream> #include <algorithm> #pragma warning(disable : 4996) using namespace std; int n, x[3009], y[3009]; bool ok[5009][5009]; int main() { while (scanf("%d", &n), n) { for (int i = 0; i < n; i++) { scanf("%d%d", &x[i], &y[i]); ok[x[i]][y[i]] = true; } int ret = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int va = x[j] - x[i], vb = y[j] - y[i]; int ex = x[i] + vb, ey = y[i] - va; int fx = x[j] + vb, fy = y[j] - va; if (0 <= ex && ex <= 5000 && 0 <= ey && ey <= 5000 && 0 <= fx && fx <= 5000 && 0 <= fy && fy <= 5000 && ok[ex][ey] && ok[fx][fy]) { ret = max(ret, va * va + vb * vb); } } } printf("%d\n", ret); for (int i = 0; i < n; i++) ok[x[i]][y[i]] = false; } return 0; }
0
CPP
import math EPSILON = 0.0001 def calRadians(x1, y1, x2, y2): product = x1*x2 + y1*y2 norm1 = (x1*x1+y1*y1) ** 0.5 norm2 = (x2*x2+y2*y2) ** 0.5 cosVal = max(-1, min(1, product/norm1/norm2)) return math.acos(cosVal) def calCentre(x1, y1, x2, y2, x3, y3): a, b, c, d = x1-x2, y1-y2, x1-x3, y1-y3 e = ((x1*x1-x2*x2) - (y2*y2-y1*y1)) / 2 f = ((x1*x1-x3*x3) - (y3*y3-y1*y1)) / 2 ox = -(d*e-b*f)/(b*c-a*d) oy = -(a*f-c*e)/(b*c-a*d) return ox, oy x1, y1 = map(float, input().split(" ")) x2, y2 = map(float, input().split(" ")) x3, y3 = map(float, input().split(" ")) ox, oy = calCentre(x1, y1, x2, y2, x3, y3) alpha1 = calRadians(x1-ox, y1-oy, x2-ox, y2-oy) alpha2 = calRadians(x1-ox, y1-oy, x3-ox, y3-oy) for i in range(3, 101): unit = 2*math.pi/i if abs(alpha1/unit - round(alpha1/unit)) > EPSILON: continue if abs(alpha2/unit - round(alpha2/unit)) > EPSILON: continue radius = ((x1-ox)**2 + (y1-oy)**2) ** 0.5 area = 0.5 * radius * radius * math.sin(unit) * i; print(area) exit()
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); if (n & 1) printf("black\n"); else printf("white\n1 2\n"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } template <int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) {} ModInt(signed sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } ModInt(signed long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } }; int main() { int a, b; scanf("%d%d", &a, &b); ModInt<1000000007> ans = 0; ModInt<1000000007> t = ModInt<1000000007>(b) * (b - 1) / 2; for (int(k) = (int)(1); (k) <= (int)(a); ++(k)) { ans += (ModInt<1000000007>(k) * b + 1) * t; } printf("%d\n", ans.get()); return 0; }
7
CPP
n = int(input()) s = input() a = 0 for i in range(1,n+1): a = max(a,len(set(s[:i]) & set(s[i:]))) print(a)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; long long hash1[1000100], hash2[1000100]; long long ans = 0; int n, m; vector<int> v[1000100]; long long h1 = 15357, h2 = (long long)26372 * 10000 * 17 + 137, h3 = (long long)92351267 * 125334 * 167 + 11; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++) { sort(v[i].begin(), v[i].end()); for (int j = 0; j < v[i].size(); j++) { hash1[i] = (hash1[i] * h2 + v[i][j] * h1) % h3; } } sort(hash1 + 1, hash1 + n + 1); for (int i = 1; i <= n; i++) { long long kol = 1; while (hash1[i] == hash1[i + 1] && i < n) { kol++; i++; } ans = (long long)ans + (kol) * (kol - 1) / 2; } for (int i = 1; i <= n; i++) { v[i].push_back(i); sort(v[i].begin(), v[i].end()); } for (int i = 1; i <= n; i++) { for (int j = 0; j < v[i].size(); j++) hash2[i] = (long long)(hash2[i] * h2 + v[i][j] * h1) % h3; } sort(hash2 + 1, hash2 + n + 1); for (int i = 1; i <= n; i++) { long long kol = 1; while (hash2[i] == hash2[i + 1] && i < n) { kol++; i++; } ans = (long long)ans + kol * (kol - 1) / 2; } cout << ans << endl; }
9
CPP
a,b,c=map(int,input().split()) print(3*min(a+2,b+1,c)-3)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; void sieve(long long n) { bool prime[1000001]; memset(prime, true, sizeof(prime)); prime[1] = false; for (long long p = 2; p * p <= n; p++) { if (prime[p] == true) { for (long long i = p * 2; i <= n; i += p) prime[i] = false; } } } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) return false; } return true; } int main() { long long a, b, f, k, x; cin >> a >> b >> f >> k; if (b < max(f, a - f)) { cout << "-1"; return 0; } long long c = 0; x = b - f; for (long long i = 1; i < k; i++) { if (i % 2 == 1) { if (x >= 2 * (a - f)) x -= 2 * (a - f); else { if (b >= 2 * (a - f)) { c++; x = b - 2 * (a - f); } else { cout << "-1"; return 0; } } } else { if (x >= 2 * f) { x -= 2 * f; } else { if (b >= 2 * f) { c++; x = b - 2 * f; } else { cout << "-1"; return 0; } } } } if (k % 2 == 0) { if (x < f) c++; } else { if (x < a - f) c++; } cout << c; }
9
CPP
import math import heapq import sys input = sys.stdin.readline n, k, x = map(int, input().split()) a = list(map(int, input().split())) a.sort() s = 0 h = [] for i in range(n - 1): z = a[i + 1] - a[i] y = int(math.ceil(z / x)) - 1 if y > 0: heapq.heappush(h, -y) s += y ans = 1 while h: if s <= k: break s += heapq.heappop(h) ans += 1 print(ans)
9
PYTHON3
l = [] x = int(input("")) for i in range(x): l.append(input("")) c=0 for i in range(x): if i<x-1 and l[i]==l[i+1]: i+=1 else: c+=1 print(c)
7
PYTHON3
import math for rpt in range(int(input())): lect_c, prac_c, pen_dur, char_dur, case_size = [int(x) for x in input().split(" ")] if math.ceil(lect_c / pen_dur) + math.ceil(prac_c / char_dur) <= case_size: print(str(math.ceil(lect_c / pen_dur)) + ' ' + str(math.ceil(prac_c / char_dur))) else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, a[510], b[510], to[110]; string sa[110], sb[110]; queue<int> qa, qb; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> sa[i]; a[sa[i][0]]++; } for (int i = 0; i < n; i++) { cin >> sb[i]; b[sb[i][0]]++; } sort(sa, sa + n); sort(sb, sb + n); int cnta = 0, cntb = 0; int nowa = 0, nowb = 0; for (int k = 'A'; k <= 'z'; k++) { if (a[k] == 0 && b[k] == 0) { continue; } if (a[k] <= b[k]) { if (cnta == 0) { for (int i = 0; i < a[k]; i++) { to[nowa + i] = nowb + i; } for (int i = a[k]; i < b[k]; i++) { qb.push(nowb + i); cntb++; } } else { int s = min(nowb + cnta, nowb + b[k] - a[k]); for (int i = 0; i < a[k]; i++) { to[nowa + i] = s + i; } for (int i = nowb; i < s; i++) { int x = qa.front(); qa.pop(); to[x] = i; cnta--; } for (int i = s + a[k]; i < nowb + b[k]; i++) { qb.push(i); cntb++; } } } else { if (cnta == 0) { int s = min(nowa + cntb, nowa + a[k] - b[k]); for (int i = 0; i < b[k]; i++) { to[s + i] = nowb + i; } for (int i = nowa; i < s; i++) { int x = qb.front(); qb.pop(); to[i] = x; cntb--; } for (int i = s + b[k]; i < nowa + a[k]; i++) { qa.push(i); cnta++; } } else { for (int i = 0; i < b[k]; i++) { to[nowa + i] = nowb + i; } for (int i = b[k]; i < a[k]; i++) { qa.push(nowa + i); cnta++; } } } nowa += a[k]; nowb += b[k]; } for (int i = 0; i < n; i++) { if (i) { cout << ", "; } cout << sa[i] << " " << sb[to[i]]; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; const bool debug = true; const long long INFL = LLONG_MAX; const int output_precision = 15; stringstream ss; int N, M, table[1010][1010]; long long memo[1010][1010][3][3]; bool was[1010][1010][3][3]; long long dp(int x, int y, int dx, int dy) { if (x <= 0 || y <= 0 || x > N || y > M) return -INF; if ((x == 1 || x == N) && (y == 1 || y == M)) return table[x][y]; if (was[x][y][dx][dy]) return memo[x][y][dx][dy]; was[x][y][dx][dy] = 1; long long& d = memo[x][y][dx][dy]; d = -INF; d = max(d, dp(x + dx, y, dx, dy)); d = max(d, dp(x, y + dy, dx, dy)); d += table[x][y]; return d; } int main() { ios_base::sync_with_stdio(0); cout.precision(output_precision); cout << fixed; ss.precision(output_precision); ss << fixed; cin >> N >> M; for (int(i) = 1; (i) <= (N); (i)++) for (int(j) = 1; (j) <= (M); (j)++) cin >> table[i][j]; long long ans = -INF; for (int(i) = 1; (i) <= (N); (i)++) for (int(j) = 1; (j) <= (M); (j)++) { long long acc = 0; acc += dp(i - 1, j, -1, -1); acc += dp(i + 1, j, 1, 1); acc += dp(i, j - 1, 1, -1); acc += dp(i, j + 1, -1, 1); ans = max(ans, acc); } for (int(i) = 1; (i) <= (N); (i)++) for (int(j) = 1; (j) <= (M); (j)++) { long long acc = 0; acc += dp(i - 1, j, -1, 1); acc += dp(i + 1, j, 1, -1); acc += dp(i, j - 1, -1, -1); acc += dp(i, j + 1, 1, 1); ans = max(ans, acc); } cout << ans << '\n'; }
10
CPP
n, a = map(int, input().split()) print(((n - a) // 2 + 1) if a % 2 == 0 else ((a + 1) // 2))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int A,B; cin >> A >> B; if(A%3 && B%3 && (A+B)%3) cout << "Impossible"; else cout << "Possible"; cout << endl; }
0
CPP
n = int(input()) arr = list(map(int, input().split())) for i in range(n): if arr[arr[arr[i]-1]-1]-1 == i: print("YES") break else: print("NO")
7
PYTHON3
#include <iostream> using namespace std; int N, K; bool hate[10]; bool check(int x) { for(; x; x/=10){ if(hate[x%10]) return false; } return true; } int main(void) { cin >> N >> K; int d; for(int i = 0; i < K; i++){ cin >> d; hate[d] = true; } for(int i = N; ; i++){ if(check(i)){ cout << i << endl; return 0; } } return 0; }
0
CPP
#include<bits/stdc++.h> using namespace std; int n; int num[101][101]; int p[101]; void matrixChainMultiplication(){ for(int l=2;l<=n;l++){ for(int i=1;i<=n-l+1;i++){ int j=i+l-1; num[i][j]=INT_MAX; for(int k=i;k<=j-1;k++) num[i][j]=min(num[i][j],num[i][k]+num[k+1][j]+p[i-1]*p[k]*p[j]); } } cout<<num[1][n]<<endl; } int main(){ cin>>n; for(int i=1;i<=n;i++) cin>>p[i-1]>>p[i]; for(int i=1;i<=n;i++)num[i][i]==0; matrixChainMultiplication(); return 0; }
0
CPP
N=int(input()) A,B=map(int,input().split()) P=list(map(int,input().split())) e=0 n=0 h=0 for i in range(N): if P[i]<=A: e+=1 elif P[i]<=B: n+=1 else: h+=1 print(min(e,n,h))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; long long arr[MAXN]; struct { int l, r, mx; long long sum = 0; } tr[MAXN * 4]; void pushup(int pos) { tr[pos].mx = max(tr[pos << 1].mx, tr[pos << 1 | 1].mx); tr[pos].sum = tr[pos << 1].sum + tr[pos << 1 | 1].sum; } void build(int l, int r, int pos) { tr[pos].l = l; tr[pos].r = r; if (l == r) { tr[pos].mx = arr[l]; tr[pos].sum = arr[l]; } else { int mid = (l + r) / 2; build(l, mid, pos << 1); build(mid + 1, r, pos << 1 | 1); pushup(pos); } } int foundat; pair<long long, long long> find(int l, long long val, long long pos) { if (tr[pos].r < l) { return pair<long long, long long>(0, -1); } if (l <= tr[pos].l && tr[pos].mx < val) { return pair<long long, long long>(tr[pos].sum, tr[pos].mx); } if (tr[pos].l == tr[pos].r) { foundat = tr[pos].l; return pair<long long, long long>(tr[pos].sum, tr[pos].mx); } int mid = tr[pos << 1].r; if (mid < l) { pair<long long, long long> res = find(l, val, pos << 1 | 1); return res; } else { pair<long long, long long> res = find(l, val, pos << 1); if (res.second >= val) { return res; } else { pair<long long, long long> res2 = find(l, val, pos << 1 | 1); res2.first += res.first; res2.second = max(res.second, res2.second); return res2; } } } void update(int loc, int val, int pos) { if (tr[pos].l == tr[pos].r) { tr[pos].mx = tr[pos].sum = val; return; } int mid = tr[pos << 1].r; if (loc <= mid) { update(loc, val, pos << 1); } else { update(loc, val, pos << 1 | 1); } pushup(pos); } int main() { cin.tie(0); cin.sync_with_stdio(0); long long N, Q; cin >> N >> Q; for (int a = 1; a <= N; a++) { cin >> arr[a]; } build(1, N, 1); for (int a = 1; a <= Q; a++) { int pi, xi; cin >> pi >> xi; arr[pi] = xi; update(pi, xi, 1); if (arr[1] == 0) { cout << "1\n"; continue; } bool solved = false; int pos = 1; long long sum = 0; while (pos <= N) { if (tr[1].mx < sum) { break; } foundat = -1; pair<long long, long long> res = find(pos, sum, 1); if (foundat == -1) { break; } if (sum + res.first - res.second == res.second) { solved = true; cout << foundat << "\n"; break; } else { sum += res.first; pos = foundat + 1; } } if (!solved) { cout << "-1\n"; } } }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 1e5 + 5; long long C[N]; long long sum[N]; long long po[N]; char s[N]; int a[N]; void gcd(long long a, long long b, long long& d, long long& x, long long& y) { if (!b) { d = a; x = 1; y = 0; } else { gcd(b, a % b, d, y, x); y -= x * (a / b); } } long long inv(long long a, long long n) { long long d, x, y; gcd(a, n, d, x, y); return d == 1 ? (x + n) % n : -1; } int main() { int n, k; while (~scanf("%d%d", &n, &k)) { scanf("%s", s); for (int i = 0; i < n; i++) a[i] = s[i] - '0'; long long ans = 0; po[0] = 1; for (int i = 1; i <= 100000; i++) po[i] = (po[i - 1] * 10) % mod; memset(C, 0, sizeof(C)); C[k] = 1; for (int i = k + 1; i <= n; i++) { long long tp = inv((long long)i - k, mod); C[i] = (C[i - 1] * tp) % mod; C[i] = C[i] * i % mod; } for (int i = 0; i < n; i++) { long long tp = a[i] * po[n - 1 - i] % mod; tp = (tp * C[i]) % mod; ans = (ans + tp) % mod; } if (k == 0) { printf("%I64d\n", ans); continue; } k--; memset(C, 0, sizeof(C)); C[k] = 1; for (int i = k + 1; i <= n; i++) { long long tp = inv((long long)i - k, mod); C[i] = (C[i - 1] * tp) % mod; C[i] = C[i] * i % mod; } for (int i = 0; i <= n - 2; i++) { long long tp = po[i] * C[n - i - 2] % mod; if (i == 0) sum[i] = tp; else sum[i] = (sum[i - 1] + tp) % mod; } for (int i = 0; i < n; i++) { if (n - 2 - i < 0) continue; ans = (ans + sum[n - 2 - i] * a[i] % mod) % mod; } printf("%I64d\n", ans); } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int rem = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (rem < k) { if (i == n - 1) { arr[i] = (k - rem) % 2 == 0 ? arr[i] : arr[i] * -1; rem = k; } else if (i < n - 1 && arr[i] < 0 && arr[i + 1] > 0) { if ((k - rem) % 2 == 0) { if (abs(arr[i]) > arr[i + 1]) { arr[i] *= -1; arr[i + 1] *= -1; } } else { arr[i] *= -1; } rem = k; } else if (arr[i] > 0) { arr[i] = ((k - rem) % 2 == 0) ? arr[i] : arr[i] * -1; rem = k; } else { arr[i] *= -1; rem++; } } sum += arr[i]; } cout << sum; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void solve() { string str; cin >> str; int len = str.size(); int x = 0, y = 0; for (int i = 0; i < len; i++) { if (str[i] == 'x') x++; else y++; ; } if (x > y) { for (int i = 0; i < x - y; i++) cout << "x"; cout << "\n"; } else { for (int i = 0; i < y - x; i++) cout << "y"; cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tt = 1; while (tt--) solve(); }
8
CPP
n, k = [int(x) for x in input().split()] ins = input() if n>1: ans = "1" + "0" * (n - 1) else: ans = "0" changed = 0 for i in range(n): if ans[i] != ins[i] and changed < k: print(ans[i], end='') changed += 1 else:print(ins[i], end='')
8
PYTHON3
""" pppppppppppppppppppp ppppp ppppppppppppppppppp ppppppp ppppppppppppppppppppp pppppppp pppppppppppppppppppppp pppppppppppppppppppppppppppppppp pppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppppp ppppppppppppppppppppp ppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppp pppppppppppppppppppppppppppppppp pppppppppppppppppppppp pppppppp ppppppppppppppppppppp ppppppp ppppppppppppppppppp ppppp pppppppppppppppppppp """ import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush, nsmallest from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction from decimal import Decimal # sys.setrecursionlimit(pow(10, 6)) # sys.stdin = open("input.txt", "r") # sys.stdout = open("output.txt", "w") mod = pow(10, 9) + 7 mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(var): sys.stdout.write(str(var)) def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end) def l(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] def dfs(source): vis[source] = 1 for child in graph[source]: if vis[child] == 1: out("Impossible") exit() if not vis[child]: dfs(child) answer.append(source) vis[source] = 2 n = int(data()) names = [list(data()) for i in range(n)] graph = dd(set) r = True order = [] for i in range(n - 1): j = i + 1 a = 0 while a < min(len(names[i]), len(names[j])) and names[i][a] == names[j][a]: a += 1 if a < min(len(names[i]), len(names[j])) and names[i][a] != names[j][a]: graph[names[i][a]].add(names[j][a]) continue if len(names[i]) > len(names[j]): out("Impossible") exit() vis = dd(int) answer = [] for i in range(26): c = chr(i + 97) if not vis[c]: dfs(c) out(''.join(answer[::-1]))
9
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { long long n, m; cin >> n >> m; long long xc, yc; cin >> xc >> yc; int l; cin >> l; long long count = 0; for (int i = 0; i < l; i++) { long long a, b; cin >> a >> b; long long steps = 0; long long mid; long long l = 0, r = 1e9 + 9; while (l <= r) { mid = l + (r - l) / 2; if (xc + a * mid > 0 && xc + a * mid <= n && yc + b * mid > 0 && yc + b * mid <= m) { steps = mid; l = mid + 1; } else { r = mid - 1; } } xc += steps * a; yc += steps * b; count += steps; } cout << count; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, n, count = 0; cin >> n; char ch[n][n]; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i % 2 == 0) { if (j % 2 == 0) { count++; ch[i][j] = 'C'; } else { ch[i][j] = '.'; } } else { if (j % 2 == 0) { ch[i][j] = '.'; } else { count++; ch[i][j] = 'C'; } } } } cout << count << endl; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cout << ch[i][j]; } cout << endl; } }
7
CPP
n,m = map(int, input().split()) if n % 2 == 0: mid = n//2 else: mid = n//2 + 1 if m <= mid: print(2*m - 1) else: d = m - mid print(2 * d)
7
PYTHON3