solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int ar[202][202]; int visited[202]; int c; int n; int dfs(int x, int y) { int mx1 = 0, mx2 = 0, mx = 0; for (int i = 1; i <= n; i++) { if (ar[x][i] && i != y) { mx = max(mx, dfs(i, x)); if (c > mx1) mx2 = mx1, mx1 = c; else if (c > mx2) mx2 = c; } } mx = max(mx, mx1 + mx2); c = mx1 + 1; return (mx); } int main() { cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; ar[a][b] = 1; ar[b][a] = 1; } int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { int l = 0, k = 0; if (ar[i][j]) { l = dfs(i, j); k = dfs(j, i); ans = max(ans, l * k); } } cout << ans; }
10
CPP
n = int(input()) s1 = input().strip() s2 = input().strip() p = 1 i = 0 prevm = 0 while i < len(s1): if s1[i] != s2[i]: if prevm == 0: p *= 6 elif prevm == 1: p *= 2 elif prevm == 2: p *= 3 prevm = 2 i += 1 else: if prevm == 0: p *= 3 elif prevm == 1: p *= 2 prevm = 1 i += 1 print(p % 1000000007)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int64_t ans; int n, m, nn[1000001], vis[1000001]; vector<int> e[1000001]; void dfs(int i) { vis[i] = 1; for (auto&& x : e[i]) if (!vis[x]) dfs(x); } int main() { scanf("%d%d", &n, &m); if (m < 2) return puts("0"), 0; for (int _ = 0; _ < m; ++_) { int s, t; scanf("%d%d", &s, &t); if (s == t) ++nn[s]; else e[s].push_back(t), e[t].push_back(s); } for (int i = 1; i <= n; ++i) if (e[i].size()) { dfs(i); break; } for (int i = 1; i <= n; ++i) if ((nn[i] || e[i].size()) && !vis[i]) return puts("0"), 0; int64_t slc = accumulate(nn, nn + n + 1, 0); ans = slc * (slc - 1); for (int i = 1; i <= n; ++i) ans += e[i].size() * (e[i].size() - 1 + slc); cout << ans / 2; }
8
CPP
#include <bits/stdc++.h> using namespace std; int sum[1000000 + 10][27]; char c1[1000000 + 10], c2[1000000 + 10]; long long n, m; long long len1, len2; long long gcd(long long a, long long b) { long long t = a; while (b) { a = b; b = t % b; t = a; } return a; } int main() { cin >> n >> m; scanf("%s%s", c1, c2); len1 = strlen(c1), len2 = strlen(c2); long long t = gcd(len1, len2); long long k = n / (len2 / t); long long res = 0; for (int i = 0; i < len2; ++i) sum[i % t][c2[i] - 'a']++; for (int i = 0; i < len1; ++i) for (int j = 0; j < 26; ++j) if (j != c1[i] - 'a') res += sum[i % t][j]; res *= k; cout << res << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; int maxdegree(int n, int m, int i, int j) { int d = 0; if (i == 0 || i == n - 1) d += 1; else d += 2; if (j == 0 || j == m - 1) d += 1; else d += 2; return d; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; vector<vector<int> > a(n, vector<int>(m)); for (int i = 0; i < n; ++i) for (int i_ = 0; i_ < (int)a[i].size(); ++i_) cin >> a[i][i_]; string ans = "YES"; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (a[i][j] > maxdegree(n, m, i, j)) ans = "NO"; cout << ans << '\n'; if (ans == "YES") for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) cout << maxdegree(n, m, i, j) << " "; cout << '\n'; } } exit(0); }
8
CPP
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const long long INF = (long long)5e18; const int MOD = 998244353; int _abs(int x) { return x < 0 ? -x : x; } int add(int x, int y) { x += y; return x >= MOD ? x - MOD : x; } int sub(int x, int y) { x -= y; return x < 0 ? x + MOD : x; } void Add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } void Sub(int &x, int y) { x -= y; if (x < 0) x += MOD; } void Mul(int &x, int y) { x = (long long)(x) * (y) % MOD; } int qpow(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = (long long)(ret) * (x) % MOD; x = (long long)(x) * (x) % MOD; y >>= 1; } return ret; } void checkmin(int &x, int y) { if (x > y) x = y; } void checkmax(int &x, int y) { if (x < y) x = y; } void checkmin(long long &x, long long y) { if (x > y) x = y; } void checkmax(long long &x, long long y) { if (x < y) x = y; } inline int read() { int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } const int N = 200005; int n; pair<int, int> a[N]; priority_queue<int> q; long long ans = 0; int main() { n = read(); for (int i = 1; i <= n; i++) a[i].first = read(); for (int i = 1; i <= n; i++) a[i].second = read(); sort(a + 1, a + n + 1); int now = a[1].first; q.push(a[1].second); long long sum = a[1].second; for (int i = 2; i <= n; i++) { while (!q.empty() && now < a[i].first) { int u = q.top(); q.pop(); ans += sum - u; now++; sum -= u; } q.push(a[i].second); sum += a[i].second; now = a[i].first; } while (!q.empty()) { int u = q.top(); q.pop(); ans += sum - u; sum -= u; } cout << ans << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; long long t, k; const long long N = 2e5 + 10; vector<pair<long long, long long> > g[N]; long long sz[N]; struct node { long long a, b, t; } p[N]; void dfs(long long x, long long fa) { sz[x] = 1; for (long long i = 0; i < g[x].size(); i++) { if (g[x][i].first == fa) continue; ; dfs(g[x][i].first, x); sz[x] += sz[g[x][i].first]; } } int32_t main() { cin >> t; while (t--) { cin >> k; memset(sz, 0, sizeof sz); for (long long i = 1; i <= 2 * k; i++) g[i].clear(); for (long long i = 1; i <= 2 * k - 1; i++) { long long a, b, t; scanf("%lld%lld%lld", &a, &b, &t); p[i] = {a, b, t}; g[a].push_back({b, t}); g[b].push_back({a, t}); } long long mini = 0, maxi = 0; dfs(1, -1); for (long long i = 1; i <= 2 * k - 1; i++) { long long a, b, t; a = p[i].a, b = p[i].b, t = p[i].t; long long tp = min(sz[a], sz[b]); if (tp % 2 == 1) mini += t; maxi += min(tp, 2 * k - tp) * t; } cout << mini << " " << maxi << endl; } }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long l, r, k; cin >> l >> r >> k; long long n = 1; long long dif; bool sw = false; while (n <= r) { if (n >= l) { cout << n << " "; sw = true; } dif = n; n *= k; if (n / k != dif) break; } if (!sw) cout << -1; return 0; }
7
CPP
a=4 n = input() x= int(n) for i in range(x+1,9013): s=str(i) ss=set(s) if(len(ss)==a): main=i break print(main)
7
PYTHON3
n=int(input()) l=list(map(int,input().split())) l.sort() if(sum(l)%2==0 and l[n-1]<=(sum(l)-l[n-1])): print("YES") else: print("NO")
8
PYTHON3
n=int(input()) tv1=-1 tv2=-1 a=[] d=False for _ in range(n): a.append(tuple(int(x) for x in input().split())) a.sort() for b,c in a: if tv1 < b: tv1=c elif tv2 < b: tv2=c else: d=True if d==False: print('YES') else: print('NO')
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mxn = 2000001; int n; long long a[mxn], b[mxn]; void answer() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i + n] = a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; b[i + n] = b[i]; } for (long long i = 2, j = 0x3f3f3f3f3f3f3f3f; i <= 2 * n; i++) { a[i] += a[i - 1], b[i] += b[i - 1]; j = min(j, a[i - 1] - b[i - 2]); if (a[i] - b[i] > j) { cout << "NO" << '\n'; return; } } cout << (a[n] <= b[n] ? "YES" : "NO") << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; for (int i = 0; i < t; i++) answer(); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; string ss,s; int main() { cin >> s >> ss; cout << ss << s; }
0
CPP
c=[] for i in range(5): a = input().split() c.append(a) for i in range(5): for j in range(5): if c[i][j]=='1': print(abs(i-2)+abs(j-2))
7
PYTHON3
n = int(input()) arr = sorted([int(a) for a in input().split()]) res = 0 size = 0 for i in range(n-1): size += arr[i] if arr[i+1] > size * 2: res = 0 else: res += 1 res += 1 print(res)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; struct Vector { double x, y; Vector() {} Vector(double x, double y) : x(x), y(y) {} Vector(const Vector& p1, const Vector& p2) : x(p2.x - p1.x), y(p2.y - p1.y) {} Vector(const Vector& p1, const Vector& p2, double t) : x(p1.x + t * p2.x), y(p1.y + t * p2.y) {} double norm() const { return hypot(x, y); } }; double dot(Vector p1, Vector p2) { return p1.x * p2.x + p1.y * p2.y; } double dist_pt_to_pt(Vector p1, Vector p2) { return Vector(p1, p2).norm(); } double dist_pt_to_segment(Vector p, Vector a, Vector b) { Vector u(a, p), v(a, b); double s = dot(u, v) / dot(v, v); if (s < 0 || s > 1) return min(dist_pt_to_pt(p, a), dist_pt_to_pt(p, b)); return dist_pt_to_pt(Vector(a, v, s), p); } int main() { int n, px, py; cin >> n >> px >> py; Vector p(px, py); vector<Vector> poly(n); for (int c = 0; c < n; c++) cin >> poly[c].x >> poly[c].y; double mr = 1e40, Mr = 0; for (int c = 0; c < n; c++) { mr = min(mr, dist_pt_to_segment(p, poly[c], poly[(c + 1) % n])); Mr = max(Mr, dist_pt_to_pt(p, poly[c])); } cout << fixed << setprecision(20); double pi = acos(-1); cout << pi * (Mr * Mr - mr * mr) << endl; return 0; }
9
CPP
import math n=int(input()) arr=list(map(int,input().split())) summ=sum(arr) ans=math.ceil(summ/(n-1)) print(max(max(arr),ans))
7
PYTHON3
lr = input().strip() s = input().strip() st = dict() st[0] = 'qwertyuiop' st[1] = 'asdfghjkl;' st[2] = 'zxcvbnm,./' L = dict() R = dict() for i in range(3): for j in range(len(st[i])-1): R[st[i][j]] = st[i][j+1] L[st[i][j+1]] = st[i][j] if lr == 'L': print(''.join([R[a] for a in s])) else: print(''.join([L[a] for a in s]))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, counter, x, y; cin >> n >> m; counter = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> x >> y; if (x != 0 || y != 0) counter++; } cout << counter; }
7
CPP
#include <bits/stdc++.h> using namespace std; int n, a[100], mn = 2e9; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; mn = min(mn, a[i]); } cout << 2 + (a[3] ^ mn); }
10
CPP
def pyramid(cubes): count, i = 0, 0 while count <= cubes: i += 1 count += i * (i + 1) // 2 return i - 1 print(pyramid(int(input())))
7
PYTHON3
#include <iostream> using namespace std; int main(){ int i,j,n,a[50000]; a[0] = 0; a[1] = 0; for(i=2;i<50000;i++) a[i] = 1; for(i=2;i<=25000;i++){ if(a[i] == 1){ for(j=i*2;j<50000;j+=i) a[j] = 0; } } while(1){ int ans; cin >> n; if(n == 0) break; ans = 0; for(i=2;i*2<=n;i++){ if(a[i] == 1 && a[n-i] == 1) ans++; } cout << ans << endl; } return 0; }
0
CPP
n=int(input()) x,y=map(int,input().split()) if 2*n-y-x<x+y-2: print("Black") else: print("White")
7
PYTHON3
#!/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 solve(): n = int(input().strip()) A = [int(s) for s in input().strip().split()] B = [int(s) for s in input().strip().split()] cnt = [0] * 3 for a in A: cnt[a] += 1 for a, b in zip(A[::-1], B[::-1]): cnt[a] -= 1 if (b > a and cnt[1] == 0) or (b < a and cnt[-1] == 0): print("NO") return print("YES") def main(): for _ in range(int(input().strip())): solve() # 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 if __name__ == "__main__": main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } const long long int MOD = 1e9 + 7, INF = 1e18; long long int dx[8] = {0, -1, 1, 0, 1, -1, 1, -1}, dy[8] = {1, 0, 0, -1, -1, -1, 1, 1}; struct edge { long long int cost; long long int u, v; }; vector<int64_t> divisor(int64_t n) { vector<int64_t> ret; for (int64_t i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } long long int gcd(long long int a, long long int b) { if (b == 0) return a; else return gcd(b, a % b); } long long int N; int main() { cin >> N; auto vec = divisor(N); pair<long long int, long long int> p = {N, 1}; for (long long int i = (((long long int)(vec).size())) - 1, i_begin_ = (0); i >= i_begin_; i--) { if (vec[i] < N / vec[i]) break; if (gcd(vec[i], N / vec[i]) == 1) { if (max(p.first, p.second) > vec[i]) { p = {vec[i], N / vec[i]}; } } } cout << p.first << " " << p.second << '\n'; }
9
CPP
def palindrome(s: str) -> bool: length = len(s) pre = length // 2 for i in range(pre): if s[i] != s[-i-1]: return False return True def longestPalindrome(line: str) -> str: b, e = 0, len(line)-1 while b < e and line[b] == line[e]: b += 1 e -= 1 if b >= e: return line i = e while i > b: if palindrome(line[b:i]) is True: break i -= 1 p = line[:i] + line[e+1:] i = b + 1 while i < e: if palindrome(line[i:e+1]) is True: break i += 1 p2 = line[:b] + line[i:] return p2 if len(p2) > len(p) else p if __name__ == "__main__": n = int(input()) for i in range(n): line = input().strip(' \r\n') print(longestPalindrome(line)) # print(palindrome(line))
10
PYTHON3
from math import * a = int(input()) b = int(input()) c = int(input()) d = int(input()) e = int(input()) f = int(input()) t1 = 0 t2 = 0 ma = -1 for i in range(min(a, d) + 1): t2 = max(min(b, c, d - i),0) ma = max(ma, i*e + t2*f) print(ma)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main(){ int n, k;cin>>n>>k; for(int i=0; i<=32; ++i){ if(pow(k,i)> n) {cout<<i; break;} } }
0
CPP
from sys import stdin from collections import deque def readGenerator(): while True: tokens = stdin.readline().strip().split(' ') for t in tokens: yield t reader = readGenerator() def readWord(): return next(reader) def readInt(): return int(next(reader)) def readFloat(): return float(next(reader)) def readLine(): return input() def solve(n, x, w): a = [] s = 0 i = 0 j = n - 1 while (j - i) >= 0: if s + w[i] != x: s += w[i] a.append(str(w[i])) i += 1 elif s + w[j] != x: s += w[j] a.append(str(w[j])) j -= 1 else: break if len(a) != n: print('NO') else: print('YES') print(' '.join(a)) t = readInt() for _ in range(t): n, x = readInt(), readInt() w = [readInt() for _ in range(n)] solve(n, x, w)
7
PYTHON3
#include <iostream> using namespace std; int n, k, a; string s; int main() { cin >> n >> k >> s; for(int i = 0; i < n-1; ++i) if(s[i] == s[i+1]) ++a; cout << min(a + k*2, n - 1); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int A[2000001]; int main() { int i, k, n, off, last, curr, newPos; scanf("%d", &n); for (i = 1; i <= n; i++) A[i] = i; for (off = 1, k = 2; k <= n; k++, off++) { last = A[off]; for (i = off; i < off + n; i += k) { newPos = min(k + i, off + n); curr = A[newPos]; A[newPos] = last; last = curr; } } for (i = off; i < n + off; i++) printf("%d ", A[i]); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, a; cin >> n >> a; if (a & 1) cout << (a + 1) / 2 << endl; else cout << n / 2 - a / 2 + 1; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-8; const int mod = 1000000007; const double pi = acos(-1); 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; } int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } long long powmod(long long a, long long x, long long mod) { long long t = 1ll; while (x) { if (x & 1) t = t * a % mod; a = a * a % mod; x >>= 1; } return t; } const int maxn = 1e5 + 5; int numb[maxn], l[maxn], r[maxn], k[maxn]; int n; int main() { gn(n); for (int i = (1); i <= (n); i++) gn(numb[i]); l[0] = r[n + 1] = 0; for (int i = (1); i <= (n); i++) l[i] = min(numb[i], l[i - 1] + 1); for (int i = (n); i >= (1); i--) r[i] = min(numb[i], r[i + 1] + 1); int ans = 0; for (int i = (1); i <= (n); i++) ans = max(ans, min(l[i], r[i])); cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MX = 1 << 22; vector<pair<long long, int> > A; void solve() { double a, b, m, vx, vy, vz; cin >> a >> b >> m >> vx >> vy >> vz; double t = -m / vy; double x = a / 2.0 + vx * t, z = vz * t; while (x < 0 || x > a) { if (x < 0) x *= -1; else x = a - (x - a); } while (z < 0 || z > b) { if (z < 0) z *= -1; else z = b - (z - b); } printf("%.10lf %.10lf\n", x, z); } int main() { solve(); }
10
CPP
n = int(input()) arr = list(map(int, input().split())) s1, s2 = 0, 0 i, j = 0, n - 1 res = 0 while i < n: s1 += arr[i] while s2 < s1 and j > i: s2 += arr[j] j -= 1 if s1 == s2: res = s1 i += 1 print(res)
9
PYTHON3
import sys, os.path import math if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") n = int(input()) lucky = [4,7,47,74,444,447,474,477,744,747,774,777] flag = 0 for i in lucky: if n % i == 0: flag = 1 break if (flag): print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> int k, a, n; int main() { int ans; ans = 0; scanf("%d", &n); while (n--) { scanf("%d%d", &k, &a); if (k + 1 > ans) ans = k + 1; a--; while (a > 0) { a >>= 2; k++; } if (k > ans) ans = k; } printf("%d\n", ans); return 0; }
7
CPP
#include <bits/stdc++.h> int main(void) { int n, count; int i = 1; scanf("%d", &n); count = n; if (n % 2 == 0) { printf("%d\n", n / 2); for (i = 1; i <= n / 2; i++) printf("2 "); } else { printf("%d\n", n / 2); for (i = 1; i <= n / 2; i++) { if (count - 2 > 1) printf("2 "); else printf("3 "); count -= 2; } } return 0; }
7
CPP
n1=int(input()) li=list(map(int,input().split())) a=(sum(li))/2 b=li[0]/2 lis=[li[0],] ans=[1,] for i in range(1,n1): if sum(lis)<=a and b>=li[i]: ans.append(i+1) lis.append(li[i]) if len(ans)==0 or sum(lis)<=a: ans=[] print(len(ans)) for i in ans: print(i,end=" ")
7
PYTHON3
n, k = map(int, input().split()) cs = map(int, input().split()) # table[c][s] has bit ss set if can make subset s and sub-subset ss # using only the first c coins. # We only ever need to know table[c-1] to compute table[c]. table = [[0 for _ in range(k+1)] for _ in range(2)] # Can always make subset 0 and sub-subset 0 using 0 coins. table[0][0] = 1 for i, c in enumerate(cs,1): for s in range(k+1): # Include the coin in neither subset nor sub-subset. table[i%2][s] |= table[(i-1)%2][s] if c <= s: # Include the coin in subset but not sub-subset. table[i%2][s] |= table[(i-1)%2][s-c] # Include the coin in both the subset and sub-subset. table[i%2][s] |= (table[(i-1)%2][s-c] << c) possible = [str(i) for i in range(k+1) if (table[n%2][k] >> i) & 1] print(len(possible)) print(*possible) # Made By Mostafa_Khaled
9
PYTHON3
n=int(input()) a=list(map(int, input().rstrip().split())) h=0 x=a[0] for _ in range(len(a)-1): if h-(a[_+1]-a[_])<0: x+=(a[_+1]-a[_]-h) h=0 else: h+=a[_]-a[_+1] print(x)
8
PYTHON3
#include <bits/stdc++.h> const int N = 12; using namespace std; int n, x, y, a[N][N]; pair<pair<int, int>, pair<int, int> > b[N] = { {{0, 0}, {0, 0}}, {{1, 1}, {3, 3}}, {{1, 4}, {3, 6}}, {{1, 7}, {3, 9}}, {{4, 1}, {6, 3}}, {{4, 4}, {6, 6}}, {{4, 7}, {6, 9}}, {{7, 1}, {9, 3}}, {{7, 4}, {9, 6}}, {{7, 7}, {9, 9}}}; int main() { ios_base::sync_with_stdio(0); n = 9; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { char c; cin >> c; a[i][j] = (int)c; } cin >> x >> y; x %= 3; y %= 3; if (x == 0) x = 3; if (y == 0) y = 3; int flag = 0; for (int i = b[(x - 1) * 3 + y].first.first; i <= b[(x - 1) * 3 + y].second.first; ++i) for (int j = b[(x - 1) * 3 + y].first.second; j <= b[(x - 1) * 3 + y].second.second; ++j) if (a[i][j] == 46) flag = 1, a[i][j] = 33; if (flag == 0) { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (a[i][j] == 46) a[i][j] = 33; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cout << char(a[i][j]); if (j % 3 == 0 && j != n) cout << " "; } cout << "\n"; if (i % 3 == 0) cout << "\n"; } return 0; }
8
CPP
name = input() name.lower() lst = [] for i in name: if i not in lst: lst.append(i) if len(lst) % 2 == 1: print("IGNORE HIM!") else: print("CHAT WITH HER!")
7
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1000000000") using namespace std; using pii = pair<int, int>; using ll = long long; using pll = pair<long long, long long>; using pil = pair<int, long long>; using vi = vector<int>; using vll = vector<long long>; using vs = vector<string>; using vc = vector<char>; using vb = vector<bool>; using vpii = vector<pair<int, int>>; using vd = vector<double>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pss = pair<string, string>; const ll mod = 1e9 + 9; const ll INF = 1e18; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; string g; cin >> n >> g; map<char, int> m; m['A'] = m['G'] = m['C'] = m['T'] = 0; for (auto& x : g) { if (x == '?') continue; m[x]++; n--; } int mx = -1; for (auto& x : m) mx = max(mx, x.second); int gr = -1; for (int i = mx; i < 255; i++) { int cur = n; for (auto& x : m) cur -= i - x.second; if (!cur) { gr = i; break; } } if (gr == -1) cout << "==="; else { int i = 0; for (auto& x : m) { while (x.second != gr) { while (g[i] != '?') i++; g[i] = x.first; x.second++; } } cout << g; } return 0; }
8
CPP
#!/usr/bin/python stat = 0 num = int(input()) arr = list(map(int, input().split(" "))) m = 0 for i in range(num): if (arr[i] > min(m+1, i)): print(i+1) stat += 1 break else: if (arr[i] > m): m = arr[i] if (stat == 0): print(-1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const double PI = 4.0 * atan(1.0); const int maxn = 100010; int vis[maxn], ft[maxn], rem[maxn]; int n, m; vector<int> ve; int on(int x) { ve.clear(); int tmp = x; for (int i = 2; i * i <= x; i++) { if (x % i == 0) { if (ft[i]) return ft[i]; ve.push_back(i); while (x % i == 0) x /= i; } } if (x != 1) { if (ft[x]) return ft[x]; ve.push_back(x); } for (int i = 0; i < ve.size(); ++i) ft[ve[i]] = tmp; vis[tmp] = 1; return 0; } void off(int x) { int tmp = x; for (int i = 2; i * i <= x; ++i) { if (x % i == 0) { ft[i] = 0; while (x % i == 0) x /= i; } } if (x != 1) { ft[x] = 0; } vis[tmp] = 0; } int main() { while (scanf("%d%d", &n, &m) == 2) { memset(vis, 0, sizeof(vis)); memset(ft, 0, sizeof(ft)); char op; int num; for (int k = 0; k < m; ++k) { scanf(" %c %d", &op, &num); if (op == '+') { if (vis[num]) { printf("Already on\n"); continue; } int ans = on(num); printf(ans ? "Conflict with %d\n" : "Success\n", ans); } else { if (!vis[num]) { printf("Already off\n"); continue; } printf("Success\n"); off(num); } } } return 0; }
8
CPP
z=[int(p) for p in input().split()] m=z[2] lst=[] lst.append(z[0]) lst.append(z[1]) tempi=min(lst) tempa=max(lst) count=0 fibo=[] n=1000000000000000000 def fib(n): a, b = 0, 1 while b < n: fibo.append(b) a, b = b, a+b def add(m): n=10**18 tempi=min(lst) tempa=max(lst) count=0 if(tempi>0 and tempa>0 and m>tempa): fib(n) for p in range(1,len(fibo)+1): if(((fibo[p]*tempa)+ (fibo[p-1]*tempi))>=m): count+=p return count elif(tempi==0 or tempa==0): count=1 fib(n) tempi=tempa+(tempi) for p in range(1,len(fibo)): if(((fibo[p]*tempa)+ (fibo[p-1]*tempi))>=m): count+=p return count elif(tempi<0 and tempa>0): val=(-(tempi)//tempa)+1 count=val tempi=tempi+(val*tempa) fib(n) for p in range(1,len(fibo)): if(((fibo[p]*tempa)+ (fibo[p-1]*tempi))>=m): count+=p return count if(tempi>0 and tempa>0 and m>tempa): cou=add(m) print(cou) elif(tempi>0 and tempa>0 and m<=tempa): print(0) elif(tempi==0 ): if(tempi==0 and tempa==0 and m>0): print(-1) elif(tempi==0 and tempa==0 and m<=0): print(0) elif(tempa==0 and tempi<0 and m>0): print(-1) else: cou=add(m) print(cou) elif(tempi<0 and tempa>0): if(m<=tempa): print(0) else: cou=add(m) print(cou) elif(tempi<0 and tempa==0): if(m>0): print(-1) else: print(0) elif(tempi<0 and tempa<0): if(m>=0): print(-1) elif(m<0 and m<=tempa): print(0) elif(m<0 and m>tempa): print(-1)
7
PYTHON3
# https://codeforces.com/contest/1154/problem/B n = int(input()) b = [int(i) for i in input().split()] b.sort() arr = [] for i in b: if i not in arr: arr.append(i) if len(arr) > 3: print(-1) elif len(arr) == 3: res = -1 for j in range(1, len(arr)): if res == -1: res = arr[j] - arr[j-1] elif arr[j] - arr[j-1] != res: res = -1 break print(res) elif len(arr) == 2: if (arr[1] - arr[0]) % 2 == 0: print((arr[1]- arr[0]) // 2) else: print(arr[1] - arr[0]) elif len(arr) ==1: print(0) else: print(-1)
8
PYTHON3
#include<iostream> using namespace std; int main(){ int n,k; cin>>n>>k; if(n%k==0) cout<<"0"; else cout<<"1"; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int n, depth = 0, max = 0, temp; long long int man[200000]; cin >> n; for (long long int i = 1; i <= n; i++) { cin >> man[i]; } for (long long int i = 1; i <= n; i++) { depth = 1; temp = man[i]; while (temp != -1) { temp = man[temp]; depth++; } if (depth > max) { max = depth; } } cout << max; return 0; }
7
CPP
a=int(input()) m={} for i in range(1000000): if a in m: print(i+1) break m[a] = i if a %2==0: a //=2 else: a = 3*a+1
0
PYTHON3
#include <bits/stdc++.h> using namespace std; long long i, j, n, x, y, k, b; long long a[12345678]; int main() { cin >> n >> x >> y; i = 1; j = 1; while (i <= x || j <= y) { if (i * y < j * x) { ++k; a[k] = 1; ++i; } else if (i * y > j * x) { ++k; a[k] = 2; ++j; } else { ++k; a[k] = 3; ++k; a[k] = 3; ++i; ++j; } } for (i = 0; i < n; ++i) { cin >> b; b = b % (x + y); if (a[b] == 1) cout << "Vanya" << endl; else if (a[b] == 2) cout << "Vova" << endl; else cout << "Both" << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, i = 2, j, k = 1, v, c = 0; cin >> n >> v; c = (v <= n - 1) ? v : n - 1; j = c; while (j < n - 1) { j++; c += i; i++; } cout << c; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int n, m; char a[105]; int main() { cin >> n >> m; cin >> (a + 1); while (m--) { int l, r; char c1, c2; cin >> l >> r >> c1 >> c2; for (int i = l; i <= r; i++) if (a[i] == c1) a[i] = c2; } cout << (a + 1); return 0; }
7
CPP
#include <iostream> using namespace std; int main(){ int M1,D1,M2,D2; cin >> M1>>D1>>M2>>D2; if(M1==M2){ cout<<"0"; }else{ cout<<"1"; } }
0
CPP
#include<bits/stdc++.h> using namespace std; const int MAXN = 5e3 + 5; typedef long long ll; typedef long double ld; typedef unsigned long long ull; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } vector <int> a[MAXN]; int depth[MAXN], father[MAXN]; int n, m, tot, ans, x[MAXN], y[MAXN]; bool used[MAXN], ok[MAXN], res[MAXN]; int nowx[MAXN], nowy[MAXN], f[MAXN], g[MAXN]; void dfs(int pos, int fa) { depth[pos] = depth[fa] + 1; father[pos] = fa; for (auto x : a[pos]) if (x != fa) dfs(x, pos); } void mark(int a, int b, int c, int d) { static int cnt[MAXN]; memset(cnt, 0, sizeof(cnt)); while (a != b) { if (depth[a] < depth[b]) swap(a, b); cnt[a]++, a = father[a]; } while (c != d) { if (depth[c] < depth[d]) swap(c, d); cnt[c]++, c = father[c]; } for (int i = 1; i <= n; i++) ok[i] |= cnt[i] >= 2; } void work(int pos, int fa) { for (auto x : a[pos]) if (x != fa) work(x, pos); if (pos != 1) { int cnt = 0; for (int i = 1; i <= tot; i++) if (!used[i]) cnt += (nowx[i] == pos) ^ (nowy[i] == pos); ans += min(max(cnt, ok[pos] * 2), 2); if (cnt <= 1 || ok[pos]) { for (int i = 1; i <= tot; i++) if (!used[i]) { if (nowx[i] == pos) nowx[i] = fa; if (nowy[i] == pos) nowy[i] = fa; } return; } vector <int> points; for (int i = 1; i <= tot; i++) if (!used[i] && ((nowx[i] == pos) ^ (nowy[i] == pos))) { points.push_back(i); } int a = points[0], b = points[1]; mark(nowx[a], nowy[a], nowx[b], nowy[b]); used[a] = used[b] = true; if (nowx[a] == pos && nowx[b] == pos) { tot++, f[tot] = -a, g[tot] = -b; x[tot] = y[a], nowx[tot] = nowy[a]; y[tot] = y[b], nowy[tot] = nowy[b]; } if (nowy[a] == pos && nowx[b] == pos) { tot++, f[tot] = a, g[tot] = -b; x[tot] = x[a], nowx[tot] = nowx[a]; y[tot] = y[b], nowy[tot] = nowy[b]; } if (nowx[a] == pos && nowy[b] == pos) { tot++, f[tot] = -a, g[tot] = b; x[tot] = y[a], nowx[tot] = nowy[a]; y[tot] = x[b], nowy[tot] = nowx[b]; } if (nowy[a] == pos && nowy[b] == pos) { tot++, f[tot] = a, g[tot] = b; x[tot] = x[a], nowx[tot] = nowx[a]; y[tot] = x[b], nowy[tot] = nowx[b]; } for (int i = 1; i <= tot; i++) if (!used[i]) { if (nowx[i] == pos) nowx[i] = fa; if (nowy[i] == pos) nowy[i] = fa; } } } int main() { read(n), read(m), tot = m; for (int i = 1; i <= n - 1; i++) { int x, y; read(x), read(y); a[x].push_back(y); a[y].push_back(x); } for (int i = 1; i <= m; i++) { read(x[i]), read(y[i]); nowx[i] = x[i]; nowy[i] = y[i]; } dfs(1, 0); work(1, 0); writeln(ans); for (int i = tot; i >= 1; i--) { if (!used[i]) res[i] = true; if (i > m) { int a = abs(f[i]), b = abs(g[i]); res[a] = res[i] ^ (x[i] != x[a]); res[b] = res[i] ^ (y[i] != y[b]); } } for (int i = 1; i <= m; i++) if (res[i]) printf("%d %d\n", x[i], y[i]); else printf("%d %d\n", y[i], x[i]); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; using lint = long long int; constexpr int MOD = 1000000007; constexpr int INF = 2147483647; void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\n"; } vector<string> s; vector<vector<int>> group; int n; int m; void solve(int i, int j, int gr) { group[i][j] = gr; if (i != n - 1 && s[i + 1][j] == '#' && group[i + 1][j] == -1) { solve(i + 1, j, gr); } if (i != 0 && s[i - 1][j] == '#' && group[i - 1][j] == -1) { solve(i - 1, j, gr); } if (j != m - 1 && s[i][j + 1] == '#' && group[i][j + 1] == -1) { solve(i, j + 1, gr); } if (j != 0 && s[i][j - 1] == '#' && group[i][j - 1] == -1) { solve(i, j - 1, gr); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; s = vector<string>(n); for (int i = (0), i_end_ = (n); i < i_end_; i++) cin >> s[i]; bool vacant_row = false; bool vacant_col = false; for (int i = (0), i_end_ = (n); i < i_end_; i++) { bool flg = true; for (int j = (0), j_end_ = (m); j < j_end_; j++) { if (s[i][j] == '#') { flg = false; break; } } if (flg) vacant_row = true; } for (int j = (0), j_end_ = (m); j < j_end_; j++) { bool flg = true; for (int i = (0), i_end_ = (n); i < i_end_; i++) { if (s[i][j] == '#') { flg = false; break; } } if (flg) vacant_col = true; } if ((vacant_row && !vacant_col) || (!vacant_row && vacant_col)) { cout << -1 << endl; return 0; } group = vector<vector<int>>(n, vector<int>(m, -1)); int gr = 0; for (int i = (0), i_end_ = (n); i < i_end_; i++) { for (int j = (0), j_end_ = (m); j < j_end_; j++) { if (s[i][j] == '#' && group[i][j] == -1) { solve(i, j, gr); gr++; } } } if (gr == 0) { cout << 0 << endl; return 0; } for (int i = (0), i_end_ = (n); i < i_end_; i++) { int mode = 0; for (int j = (0), j_end_ = (m); j < j_end_; j++) { if (s[i][j] == '#') { if (mode == 0) { mode++; } else if (mode == 2) { cout << -1 << endl; return 0; } } else { if (j != 0 && mode == 1) { mode++; } } } } for (int j = (0), j_end_ = (m); j < j_end_; j++) { int mode = 0; for (int i = (0), i_end_ = (n); i < i_end_; i++) { if (s[i][j] == '#') { if (mode == 0) { mode++; } else if (mode == 2) { cout << -1 << endl; return 0; } } else { if (i != 0 && mode == 1) { mode++; } } } } cout << gr << endl; }
8
CPP
N=int(input()) leng=1 max_leng=1 last_type_leng=1 max_now=1 A=list(map(int,input().split())) last_one=A[0] for i in range(1,N): if (A[i]==last_one): leng+=1 last_one=A[i] if (last_type_leng>leng): max_now=leng*2 else: max_now=last_type_leng*2 else: last_type_leng=leng leng=1 last_one=A[i] if (last_type_leng>leng): max_now=leng*2 else: max_now=last_type_leng*2 if (max_now>max_leng): max_leng=max_now print(max_leng)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main() { int n,m,q; cin>>n>>m>>q; vector<int>a[505]; for(int i=0;i<m;i++) { int t,t1; cin>>t>>t1; a[t].push_back(t1); } for(int i=0;i<n;i++) sort(a[i].begin(),a[i].end()); for(int i=0;i<q;i++) {int ans=0; int s,e; cin>>s>>e; for(int j=s;j<=e;j++) { ans+=(upper_bound(a[j].begin(),a[j].end(),e)-a[j].begin()); } printf("%d\n",ans); } }
0
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e18; const string nl = "\n"; int32_t main() { ios::sync_with_stdio(0); cin.tie(nullptr); long long n; cin >> n; vector<long long> a(n); for (auto& i : a) { cin >> i; } auto factor = [&](long long x) { deque<long long> hld; for (long long i = 1; i * i <= x; ++i) { if (x % i == 0) { hld.push_back(i); if (i * i != x) { hld.push_back(x / i); } } } return hld; }; vector<long long> hld; for (long long i = 0; i < ((long long)(a).size()); ++i) { if (a[i] == 1) { hld.push_back(i); } } deque<long long> hld1 = factor(accumulate(a.begin(), a.end(), 0LL)); sort(hld1.begin(), hld1.end()); hld1.pop_front(); long long ans = INF; for (long long i = 0; i < ((long long)(hld1).size()); ++i) { long long hld2 = hld1[i] / 2; long long ans1 = 0; for (long long j = 0; j <= ((long long)(hld).size()) - hld1[i]; j += hld1[i]) { for (long long h = j; h < j + hld1[i]; ++h) { assert(h < ((long long)(hld).size()) && "h must be < si(hld)"); ans1 += abs(hld[j + hld2] - hld[h]); } } ans = min(ans, ans1); } cout << (ans == INF ? -1 : ans) << nl; }
11
CPP
#include<iostream> using namespace std; int main() { int n,k,sum=0,x; cin>>n>>k; for(int i=0;i<n;i++) { cin>>x; if(x>=k) sum++; } cout<<sum; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { register long long x = 0; register bool f = 0; register char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = 1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - 48; c = getchar(); } return f ? -x : x; } char cr[200]; long long tt; inline void print(register long long x, register char k = '\n') { if (!x) putchar('0'); if (x < 0) putchar('-'), x = -x; while (x) cr[++tt] = x % 10 + '0', x /= 10; while (tt) putchar(cr[tt--]); putchar(k); } long long pw[50], t, d, p, f[50], g[50]; signed main() { t = read(); while (t--) { pw[0] = 1; d = read() - 1; p = read(); for (long long i = 1; i <= 40; i++) { pw[i] = pw[i - 1] * 2; } f[0] = 3; for (long long i = 1; i <= 40; i++) { f[i] = (f[i - 1] + pw[i]) % p; } g[1] = 2; for (long long i = 2; i <= 40; i++) { g[i] = (g[i - 1] * f[i - 2]) % p; } long long ans = 1 % p; if (d == 0) { print(ans); continue; } for (long long i = 1; i <= 40; i++) { if (d < pw[i]) { ans = (ans + d % p * g[i] % p) % p; break; } else { ans = (ans + pw[i] % p * g[i] % p) % p; d -= pw[i]; } } print(ans % p); } }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; map<int, int> m; int u = 0; long long powers[32]; powers[0] = 1; for (int i = 1; i < 32; i++) powers[i] = 2 * powers[i - 1]; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; } bool flag = false; for (int i = 0; i < n; i++) { flag = false; for (int j = 0; j < 32; j++) { long long x = powers[j] - a[i]; if (m[x] > 1 || (m[x] == 1 && x != a[i])) { flag = true; break; } } if (!flag) u++; } cout << u << endl; return 0; }
9
CPP
#include <stdio.h> #include <stdlib.h> #include <iostream> #define rep(i, n) for(int i=0;i<n;i++) #define FOR(i, j, n) for(int i=j;i<n;i++) #define IS_NEGATIVE(i) 0>i #define BIG 2 #define MEDIUM 1 #define SMALL 0 #define DEBUG 0 using namespace std; int N = 0; int data[10][10]; int result[12][3]; int can_fill(int x, int y, int type); int spoit(int x, int y, int type, int n, int v); void print_data() { rep(i, 10) { rep(j, 10) { if(DEBUG) cout << data[i][j] << ","; } if(DEBUG) cout << endl; } } int dfs(int n) { if (n == N) { rep(i, 10) { rep(j, 10) { if (data[i][j]) { return 0; } } } rep(i, N) cout << result[i][0] << " " << result[i][1] << " " << result[i][2] << endl; return 1; } rep(i, 10) { rep(j, 10) { if(!data[i][j]) continue; rep(type, 3) { if(!can_fill(j, i, type)) { continue; } spoit(j, i, type, n, 1); if (dfs(n + 1)) { return 1; } spoit(j, i, type, n, -1); } return 0; } } return 0; } int spoit(int x, int y, int type, int n, int v) { switch(type) { case BIG: data[y][x] -= v; data[y + 1][x - 1] -= v; data[y + 1][x] -= v; data[y + 1][x + 1] -= v; data[y + 2][x - 2] -= v; data[y + 2][x - 1] -= v; data[y + 2][x] -= v; data[y + 2][x + 1] -= v; data[y + 2][x + 2] -= v; data[y + 3][x - 1]-= v; data[y + 3][x]-= v; data[y + 3][x + 1]-= v; data[y + 4][x] -=v; if (v) { result[n][0] = x; result[n][1] = y + 2; result[n][2] = type + 1; } break; case MEDIUM: data[y][x] -= v; data[y][x + 1] -= v; data[y][x + 2] -= v; data[y + 1][x] -= v; data[y + 1][x + 1] -= v; data[y + 1][x + 2] -= v; data[y + 2][x] -= v; data[y + 2][x + 1] -= v; data[y + 2][x + 2] -= v; if (v) { result[n][0] = x + 1; result[n][1] = y + 1; result[n][2] = type + 1; } break; case SMALL: data[y][x] -= v; data[y + 1][x - 1] -= v; data[y + 1][x] -= v; data[y + 1][x + 1] -= v; data[y + 2][x] -= v; if (v) { result[n][0] = x; result[n][1] = y + 1; result[n][2] = type + 1; } break; default: break; } if (DEBUG) cout << "----------------------------" << endl; print_data(); return 1; } int can_fill(int x, int y, int type) { switch(type) { case BIG: if (y < 0 || y > 5 || x < 2 || x > 7) return 0; if (!data[y][x] || !data[y + 1][x - 1] || !data[y + 1][x] || !data[y + 1][x + 1] || !data[y + 2][x - 2] || !data[y + 2][x - 1] || !data[y + 2][x] || !data[y + 2][x + 1] || !data[y + 2][x + 2] || !data[y + 3][x - 1] || !data[y + 3][x] || !data[y + 3][x + 1] || !data[y + 4][x]) { return 0; } return 1; case MEDIUM: if (y < 0 || y > 7 || x < 0 || x > 7) return 0; if (!data[y][x] || !data[y][x + 1] || !data[y][x + 2] || !data[y + 1][x] || !data[y + 1][x + 1] || !data[y + 1][x + 2] || !data[y + 2][x] || !data[y + 2][x + 1] || !data[y + 2][x + 2]) { return 0; } return 1; case SMALL: if (y < 0 || y > 7 || x < 1 || x > 8) return 0; if (!data[y][x] || !data[y + 1][x - 1] || !data[y + 1][x] || !data[y + 1][x + 1] || !data[y + 2][x]) { return 0; } return 1; default: break; } return 1; } int main() { cin >> N; rep(i, 10) rep(j, 10) cin >> data[i][j]; dfs(0); return 0; }
0
CPP
x,y=map(int,input().split()) lol=[] for __ in range(x): lol.append(list(map(int,input().split()))) yo=[] for i in lol: po=i[:] yo.append(po) for i in range(x): k=1 for j in range(y): k&=lol[i][j] for j in range(y): yo[i][j]&=k #print(yo,k,lol) for i in range(y): k=1 for j in range(x): k&=lol[j][i] for j in range(x): yo[j][i]&=k #print(yo,lol) yo0=[] for i in yo: po=i[:] yo0.append(po) for i in range(x): k=0 for j in range(y): k|=yo[i][j] for j in range(y): yo0[i][j]|=k for i in range(y): k=0 for j in range(x): k|=yo[j][i] for j in range(x): yo0[j][i]|=k if yo0==lol: print("YES") for i in yo: print(*i) else: print("NO") #0 in a row then whole row 0, implement it afterwards this implementation
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 10, M = N * 2; typedef long long ll; typedef pair<ll, ll> pii; #define endl '\n' // 1 到当前点: // 0:没有路 // 1:一条路 // 2:多条路 // -1:无线条路 // 与1号点在同一强连通分量中的点:-1 // 搜一遍,每个点最多两边 int n, m, t; int h[N], ne[M], e[M], idx; void add(int a, int b){ e[idx] = b, ne[idx] = h[a], h[a] = idx++; } int dfn[N], low[N], timestamp; stack<int> s; bool in_s[N], st[N]; int scc_cnt; int a[N]; void tarjan(int u) { dfn[u] = low[u] = ++timestamp; s.push(u); in_s[u] = 1; for (int i = h[u]; ~i; i = ne[i]) { int v = e[i]; if (u == v)a[v] = -1; //自己连自己 自环 if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (in_s[v])low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { if (s.top() != u) { //找强连通块 ++scc_cnt; int v; do { v = s.top(); s.pop(); in_s[v] = 0; a[v] = -1; } while (v != u); } else { in_s[s.top()] = 0; s.pop(); } } } void init(){ while (!s.empty()) s.pop(); for(int i = 1; i <= n + 5; i++){ timestamp = scc_cnt = dfn[i] = low[i] = st[i] = a[i] = 0; h[i] = -1; } } void cal(int u, int k){ if (k == -1) a[u] = -1; if (a[u] == -1) return; a[u] = min(a[u] + k, 2); } void dfs1(int x){ if(st[x]) return; st[x] = 1; for(int i = h[x]; ~i; i = ne[i]){ int j = e[i]; cal(j, a[x]); dfs1(j); } } void dfs2(int x){ for(int i = h[x]; ~i; i = ne[i]){ int j = e[i]; if(a[j] == 1){ a[j] = 2; dfs2(j); } } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>t; while(t--){ cin>>n>>m; init(); for(int i = 1; i <= m; i++){ int a, b; cin>>a>>b; add(a, b); } tarjan(1); // for(int i = 1; i <= n; i++) cout<<a[i]<<" "; cal(1, 1); for (int i = 1; i <= n; i++) if (a[i] == -1) dfs1(i); dfs1(1); for(int i = 1; i <= n; i++) if(a[i] == 2) dfs2(i); for(int i = 1; i <= n; i++) cout<<a[i]<<" "; cout<<endl; } return 0; }
13
CPP
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int main() { double x, h; while (cin >> x >> h) { if (x == 0.0 && h == 0.0) break; printf("%.6f\n", (x*x + 4.0*(x*sqrt(h*h+(x*x)/4.0))/2.0)); } }
0
CPP
want = input() n = int(input()) l = [input() for _ in range(n)] works = False if want in l: works = True for i in range(n): for j in range(n): if want in l[i] + l[j]: works = True print("YES" if works else "NO")
7
PYTHON3
#!/usr/bin/env python3 s = input() last_nonzero_idx = None for idx in range(len(s)-1, -1, -1): if s[idx] != '0': last_nonzero_idx = idx break n = s[:idx+1] length = len(n) def isPalindrome(n): for i in range( length // 2 ): if n[i] != n[length-1-i]: return False return True print('YES' if isPalindrome(n) else 'NO')
7
PYTHON3
#include <iostream> #include <cstdio> #include <string> using namespace std; int main(){ int n; cin>>n; for(int i=0; i<n; i++) { int out=0,bass=0,point=0; string s; while(cin>>s) { if( s=="HIT") bass++; else if( s=="OUT" ) out++; else { point += bass+1; bass=0; } if( bass == 4 ) { point++; bass=bass-1; } if( out == 3 ) break; } cout<<point<<endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int n, m; string s[20]; vector<pair<int, int> > left1, right1; int main() { cin >> n >> m; int mark = 0, limit = 0; for (int i = 0; i < n; i++) { int prev = 0, sum1 = 0, sum2 = 0; cin >> s[i]; for (int j = 0; j < s[i].size(); j++) { if (s[i][j] == '1') { sum1 += abs(prev - j); prev = j; } } int len = m + 2; left1.push_back({sum1, abs(prev)}); prev = len - 1; for (int j = len - 1; j >= 0; j--) { if (s[i][j] == '1') { sum2 += abs(prev - j); prev = j; } } right1.push_back({sum2, abs(prev)}); if (sum1 == 0 && mark == 0) limit++; else mark = 1; } int ans = 1e17, lim = pow(2, abs(n)); for (int i = 0; i < lim; i++) { int temp = 0; for (int j = n - 1; j >= limit; j--) { if (i & (1 << j)) { if (j == n - 1) { temp += m + 1; } temp += right1[j].first; if (j != limit) { if (i & (1 << (j - 1))) temp += abs(m + 1 - right1[j].second); else temp += abs(right1[j].second); } } else { temp += left1[j].first; if (j != limit) { if (i & (1 << (j - 1))) temp += abs(m + 1 - left1[j].second); else temp += abs(left1[j].second); } } if (j != limit) temp++; } ans = min(ans, temp); } cout << ans << "\n"; return 0; }
8
CPP
#include <bits/stdc++.h> const long long oo = 1e9; const long long mod = 95542721; const long long MAX = 1e9; const long long ARR = 410; using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; int a[n], cnt[n]; memset(cnt, 0, sizeof(cnt)); long long s = 0, res = 0; long long sum = 0; for (long long i = (0); i < (long long)(n); ++i) { cin >> a[i]; sum += a[i]; } if (sum % 3 != 0) return cout << 0, 0; sum /= 3; for (int i = n - 1; i >= 0; i--) { s += a[i]; if (sum == s) { cnt[i] = 1; } } for (int i = n - 2; i >= 0; --i) cnt[i] += cnt[i + 1]; s = 0; for (long long i = (0); i < (long long)(n - 2); ++i) { s += a[i]; if (s == sum) res += cnt[i + 2]; } cout << res; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline void RST(T& A) { memset(A, 0, sizeof(A)); } template <class T> inline void FLC(T& A, int x) { memset(A, x, sizeof(A)); } template <class T> inline void CLR(T& A) { A.clear(); } const int dx4[] = {-1, 0, 1, 0}; const int dy4[] = {0, 1, 0, -1}; const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1}; const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2}; const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1}; const int mod = 1000000007; const int inf = 0x3f3f3f3f; const long long inff = 1LL << 60; const double eps = 1e-9; const double oo = 1e25; const double pi = acos(-1.0); template <class T> inline void checkMin(T& a, const T b) { if (b < a) a = b; } template <class T> inline void checkMax(T& a, const T b) { if (a < b) a = b; } template <class T> inline void checkMin(T& a, T& b, const T x) { checkMin(a, x), checkMin(b, x); } template <class T> inline void checkMax(T& a, T& b, const T x) { checkMax(a, x), checkMax(b, x); } template <class T, class C> inline void checkMin(T& a, const T b, C c) { if (c(b, a)) a = b; } template <class T, class C> inline void checkMax(T& a, const T b, C c) { if (c(a, b)) a = b; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline T sqr(T a) { return a * a; } template <class T> inline T cub(T a) { return a * a * a; } template <class T> inline T ceil(T x, T y) { return (x - 1) / y + 1; } inline int sgn(double x) { return x < -eps ? -1 : x > eps; } inline int sgn(double x, double y) { return sgn(x - y); } inline double cot(double x) { return 1. / tan(x); }; inline double sec(double x) { return 1. / cos(x); }; inline double csc(double x) { return 1. / sin(x); }; namespace BO { inline bool _1(int x, int i) { return bool(x & 1 << i); } inline bool _1(long long x, int i) { return bool(x & 1LL << i); } inline long long _1(int i) { return 1LL << i; } inline long long _U(int i) { return _1(i) - 1; }; inline int reverse_bits(int x) { x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc); x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00); x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000); return x; } inline long long reverse_bits(long long x) { x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL); x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL); x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL); x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL); x = ((x >> 16) & 0x0000ffff0000ffffLL) | ((x << 16) & 0xffff0000ffff0000LL); x = ((x >> 32) & 0x00000000ffffffffLL) | ((x << 32) & 0xffffffff00000000LL); return x; } template <class T> inline bool odd(T x) { return x & 1; } template <class T> inline bool even(T x) { return !odd(x); } template <class T> inline T low_bit(T x) { return x & -x; } template <class T> inline T high_bit(T x) { T p = low_bit(x); while (p != x) x -= p, p = low_bit(x); return p; } template <class T> inline T cover_bit(T x) { T p = 1; while (p < x) p <<= 1; return p; } template <class T> inline int cover_idx(T x) { int p = 0; while (_1(p) < x) ++p; return p; } inline int clz(int x) { return __builtin_clz(x); } inline int clz(long long x) { return __builtin_clzll(x); } inline int ctz(int x) { return __builtin_ctz(x); } inline int ctz(long long x) { return __builtin_ctzll(x); } inline int lg2(int x) { return !x ? -1 : 31 - clz(x); } inline int lg2(long long x) { return !x ? -1 : 63 - clz(x); } inline int low_idx(int x) { return !x ? -1 : ctz(x); } inline int low_idx(long long x) { return !x ? -1 : ctz(x); } inline int high_idx(int x) { return lg2(x); } inline int high_idx(long long x) { return lg2(x); } inline int parity(int x) { return __builtin_parity(x); } inline int parity(long long x) { return __builtin_parityll(x); } inline int count_bits(int x) { return __builtin_popcount(x); } inline int count_bits(long long x) { return __builtin_popcountll(x); } } // namespace BO using namespace BO; namespace NT { template <class T> inline T lowbit(T x) { return x & -x; } template <class T> inline T GCD(T A, T B) { T C; while (B != 0) C = B, B = A % B, A = C; return A; } template <class T> inline T LCM(T A, T B) { return A * (B / GCD(A, B)); } template <class T> inline T Mod(T a, T b) { a %= b; return a < 0 ? a + b : a; } template <class T> inline T MulMod(T a, T b, T c) { if (c == 1) return 0; if (c <= 0) return -1; T ret = 0, tmp; tmp = a = Mod(a, c); b = Mod(b, c); while (b) { if (b & 0x1) if ((ret += tmp) >= c) ret -= c; if ((tmp <<= 1) >= c) tmp -= c; b >>= 1; } return ret; } template <class T, class Tb> inline T PowMod(Tb a, T b, T c) { if (c == 1) return 0; if (c <= 0) return -1; a = Mod(a, (Tb)c); Tb ret(1L % c); while (b) { if (b & 0x1) ret = ret * a % c; a = a * a % c; b >>= 1; } return (T)ret; } template <class T> inline T HPowMod(T a, T b, T c) { if (c == 1) return 0; if (c <= 0) return -1; a = Mod(a, c); T ret(1L % c); while (b) { if (b & 0x1) ret = MulMod(ret, a, c); a = MulMod(a, a, c); b >>= 1; } return ret; } template <class T, class Tb> inline T Pow(T a, Tb b) { T c(1); while (b) { if (b & 1) c *= a; a *= a, b >>= 1; } return c; } template <class T> inline T EXT_GCD(T a, T b, T& x, T& y) { T t, ret; if (!b) { x = 1, y = 0; return a; } ret = EXT_GCD(b, a % b, x, y); t = x, x = y, y = t - a / b * y; return ret; } template <class T> inline T Inv(T a, T n) { if (n <= 0) return -1; T d, x, y; d = EXT_GCD(a, n, x, y); if (d != 1) return -1; return Mod(x, n); } template <class T, int MAXN, T MOD = -1> class Matrix { public: T m[MAXN][MAXN]; Matrix() {} void init(T num[MAXN][MAXN]) { for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { m[i][j] = num[i][j]; if (MOD != -1) m[i][j] %= MOD; } } } friend Matrix operator*(const Matrix& m1, const Matrix& m2) { int i, j, k; Matrix ret; memset(ret.m, 0, sizeof(ret.m)); for (i = 0; i < MAXN; i++) { for (j = 0; j < MAXN; j++) if (m1.m[i][j]) { for (k = 0; k < MAXN; k++) { ret.m[i][k] += m1.m[i][j] * m2.m[j][k]; if (MOD != -1) ret.m[i][k] %= MOD; } } } return ret; } friend Matrix operator+(const Matrix& m1, const Matrix& m2) { int i, j; Matrix ret; for (i = 0; i < MAXN; i++) { for (j = 0; j < MAXN; j++) { ret.m[i][j] = 0; ret.m[i][j] = m1.m[i][j] + m2.m[i][j]; if (MOD != -1) ret.m[i][j] %= MOD; } } return ret; } friend Matrix operator^(const Matrix& _M, long long nx) { Matrix ret, M(_M); for (int i = 0; i < MAXN; i++) { for (int j = 0; j < MAXN; j++) { if (i == j) ret.m[i][j] = 1; else ret.m[i][j] = 0; } } while (nx) { if (nx & 1) ret = ret * M; nx = nx >> 1; M = M * M; } return ret; } }; const int PMAX = 1000; vector<int> Prime; bitset<PMAX> isnotPrime; void sieve() { if (!Prime.empty()) return; for (int i = int(2); i < int(PMAX); ++i) { if (!isnotPrime[i]) Prime.push_back(i); for (int j = 0; j < int(Prime.size()) && i * Prime[j] < PMAX; ++j) { isnotPrime[i * Prime[j]] = 1; if (!(i % Prime[j])) break; } } } inline bool isPrime(int n) { return !isnotPrime[n]; } template <class T> T phi(T n) { T ret = 1, i; for (i = 2; i * i <= n; i++) if (n % i == 0) { n /= i, ret *= i - 1; while (n % i == 0) n /= i, ret *= i; } if (n > 1) ret *= n - 1; return ret; } inline void _INC(int& a, int b) { a += b; if (a >= mod) a -= mod; } inline int _sum(int a, int b) { a += b; if (a >= mod) a -= mod; return a; } inline void _DEC(int& a, int b) { a -= b; if (a < 0) a += mod; } inline int _dff(int a, int b) { a -= b; if (a < 0) a += mod; return a; } inline void _MUL(int& a, int b) { a = (long long)a * b % mod; } inline int _pdt(int a, int b) { return (long long)a * b % mod; } inline int _I(int b) { int a = mod, x1 = 0, x2 = 1, q; while (true) { q = a / b, a %= b; if (!a) return (x2 + mod) % mod; _DEC(x1, _pdt(q, x2)); q = b / a, b %= a; if (!b) return (x1 + mod) % mod; _DEC(x2, _pdt(q, x1)); } } inline void _DIV(int& a, int b) { _MUL(a, _I(b)); } inline int _qtt(int a, int b) { return _pdt(a, _I(b)); } struct Int { int val; operator int() const { return val; } Int(int val = 0) : val(val) { if (val >= 0 && val < mod) return; val %= mod; if (val < 0) val += mod; } inline Int& operator+=(const int& rhs) { _INC(val, rhs); return *this; } inline Int operator+(const int& rhs) const { return _sum(val, rhs); } inline Int& operator-=(const int& rhs) { _DEC(val, rhs); return *this; } inline Int operator-(const int& rhs) const { return _dff(val, rhs); } inline Int& operator*=(const int& rhs) { _MUL(val, rhs); return *this; } inline Int operator*(const int& rhs) const { return _pdt(val, rhs); } inline Int& operator/=(const int& rhs) { _DIV(val, rhs); return *this; } inline Int operator/(const int& rhs) const { return _qtt(val, rhs); } }; } // namespace NT using namespace NT; namespace Date { int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct date { int year, month, day; }; inline int leap(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } inline int legal(date a) { if (a.month < 0 || a.month > 12) return 0; if (a.month == 2) return a.day > 0 && a.day <= 28 + leap(a.year); return a.day > 0 && a.day <= days[a.month - 1]; } inline int datecmp(date a, date b) { if (a.year != b.year) return a.year - b.year; if (a.month != b.month) return a.month - b.month; return a.day - b.day; } int weekday(date a) { int tm = a.month >= 3 ? (a.month - 2) : (a.month + 10); int ty = a.month >= 3 ? a.year : (a.year - 1); return (ty + ty / 4 - ty / 100 + ty / 400 + (int)(2.6 * tm - 0.2) + a.day) % 7; } int date2int(date a) { int ret = a.year * 365 + (a.year - 1) / 4 - (a.year - 1) / 100 + (a.year - 1) / 400, i; days[1] += leap(a.year); for (i = 0; i < a.month - 1; ret += days[i++]) ; days[1] = 28; return ret + a.day; } date int2date(int a) { date ret; ret.year = a / 146097 * 400; for (a %= 146097; a >= 365 + leap(ret.year); a -= 365 + leap(ret.year), ret.year++) ; days[1] += leap(ret.year); for (ret.month = 1; a >= days[ret.month - 1]; a -= days[ret.month - 1], ret.month++) ; days[1] = 28; ret.day = a + 1; return ret; } } // namespace Date namespace BIGNUM { const int __base = 1e8; const int P10[] = {1, 10, int(1e2), int(1e3), int(1e4), int(1e5), int(1e6), int(1e7), int(1e8), int(1e9)}; const int MAX_BUF_SIZE = 10009; char __buf[MAX_BUF_SIZE]; inline char* RS(char* s) { scanf("%s", s); return s; } class bignum { friend istream& operator>>(istream&, bignum&); friend ostream& operator<<(ostream&, const bignum&); friend bignum operator+(const bignum&, const bignum&); friend bignum operator-(const bignum&, const bignum&); friend bignum operator*(const bignum&, const bignum&); friend bignum operator/(const bignum&, const bignum&); friend bignum operator%(const bignum&, const bignum&); friend bignum operator+(const bignum&, const int&); friend bignum operator-(const bignum&, const int&); friend bignum operator*(const bignum&, const int&); friend bignum operator/(const bignum&, const int&); friend bignum operator%(const bignum&, const int&); friend bool operator==(const bignum&, const bignum&); friend bool operator!=(const bignum&, const bignum&); friend bool operator<(const bignum&, const bignum&); friend bool operator>(const bignum&, const bignum&); friend bool operator<=(const bignum&, const bignum&); friend bool operator>=(const bignum&, const bignum&); friend bool operator==(const bignum&, const int&); friend bool operator!=(const bignum&, const int&); friend bool operator<(const bignum&, const int&); friend bool operator>(const bignum&, const int&); friend bool operator<=(const bignum&, const int&); friend bool operator>=(const bignum&, const int&); friend int do_comp(const bignum&, const int&); friend int do_comp(const bignum&, const bignum&); friend void divide(const bignum&, const bignum&, bignum&, bignum&); friend bignum pow(bignum, int); friend bignum pow(int, int); public: inline bignum(){}; inline bignum(int s) { while (s) data.push_back(s % __base), s /= __base; if (data.empty()) data.push_back(0); } inline bignum(long long s) { while (s) data.push_back(int(s % __base)), s /= __base; if (data.empty()) data.push_back(0); } inline bignum(string s) { int t, i; CLR(data); for (i = int(s.size()) - 8; i > 0; i -= 8) { istringstream(s.substr(i, 8)) >> t; data.push_back(t); } istringstream(s.substr(0, i + 8)) >> t; data.push_back(t); } void input() { CLR(data); RS(__buf); int t = 0, c = 0; for (int i = int(strlen(__buf) - 1); i >= int(0); --i) { t += P10[c] * (int(__buf[i]) - '0'), ++c; if (c == 8) data.push_back(t), c = t = 0; } if (c) data.push_back(t); } void operator=(const int); void operator=(const string); void operator=(const bignum); bignum& operator+=(const bignum&); bignum& operator-=(const bignum&); bignum& operator*=(const bignum&); bignum& operator/=(const bignum&); bignum& operator%=(const bignum&); bignum& operator+=(const int&); bignum& operator-=(const int&); bignum& operator*=(const int&); bignum& operator/=(const int&); bignum& operator%=(const int&); bool undefined(); int do_try(const int&); int do_try(const bignum&); void do_trim(); deque<int> data; int size() { deque<int>::iterator it; int res = 0; for (it = data.begin(); it != data.end(); it++) res += 8; it--; if (*it >= 10000) { if ((*it) >= 1000000) { if (*it >= 10000000) ; else res--; } else { if ((*it) >= 100000) res -= 2; else res -= 3; } } else if ((*it) >= 100) { if (*it >= 1000) res -= 4; else res -= 5; } else { if ((*it) >= 10) res -= 6; else res -= 7; } return res; } void do_reserve(int a) { if (a <= 0) return; deque<int>::iterator it; for (it = data.begin(); it != data.end() && a > 0; it++) a -= 8; if (it == data.end() && a >= 0) return; a += 8, it--; int f = 1; for (int i = 0; i < a; i++) f *= 10; (*it) %= f; data.erase(++it, data.end()); do_trim(); } }; inline void bignum::operator=(const bignum a) { data.clear(); for (deque<int>::const_iterator i = a.data.begin(); i != a.data.end(); i++) { data.push_back(*i); } } inline void bignum::operator=(const string a) { (*this) = bignum(a); } inline void bignum::operator=(const int a) { (*this) = bignum(a); } inline istream& operator>>(istream& input, bignum& a) { string s; int t, i; input >> s; a.data.clear(); for (i = int(s.size()) - 8; i > 0; i -= 8) { istringstream(s.substr(i, 8)) >> t; a.data.push_back(t); } istringstream(s.substr(0, i + 8)) >> t; a.data.push_back(t); return input; } inline ostream& operator<<(ostream& output, const bignum& a) { deque<int>::const_reverse_iterator i = a.data.rbegin(); output << *i; for (i++; i != a.data.rend(); i++) { if (*i >= 10000) { if (*i >= 1000000) { if (*i >= 10000000) cout << *i; else cout << 0 << *i; } else { if (*i >= 100000) cout << "00" << *i; else cout << "000" << *i; } } else { if (*i >= 100) { if (*i >= 1000) cout << "0000" << *i; else cout << "00000" << *i; } else { if (*i >= 10) cout << "000000" << *i; else cout << "0000000" << *i; } } } return output; } inline bool bignum::undefined() { return data.empty(); } inline int do_comp(const bignum& a, const bignum& b) { if (a.data.size() < b.data.size()) return -1; if (a.data.size() > b.data.size()) return 1; deque<int>::const_reverse_iterator i; deque<int>::const_reverse_iterator j; for (i = a.data.rbegin(), j = b.data.rbegin(); j != b.data.rend(); i++, j++) { if (*i < *j) return -1; if (*i > *j) return 1; } return 0; } inline int do_comp(const bignum& a, const int& b) { return do_comp(a, bignum(b)); } inline bool operator==(const bignum& a, const bignum& b) { return do_comp(a, b) == 0; } inline bool operator!=(const bignum& a, const bignum& b) { return do_comp(a, b) != 0; } inline bool operator<(const bignum& a, const bignum& b) { return do_comp(a, b) == -1; } inline bool operator>(const bignum& a, const bignum& b) { return do_comp(a, b) == 1; } inline bool operator<=(const bignum& a, const bignum& b) { return do_comp(a, b) != 1; } inline bool operator>=(const bignum& a, const bignum& b) { return do_comp(a, b) != -1; } inline bool operator==(const bignum& a, const int& b) { return do_comp(a, b) == 0; } inline bool operator!=(const bignum& a, const int& b) { return do_comp(a, b) != 0; } inline bool operator<(const bignum& a, const int& b) { return do_comp(a, b) == -1; } inline bool operator>(const bignum& a, const int& b) { return do_comp(a, b) == 1; } inline bool operator<=(const bignum& a, const int& b) { return do_comp(a, b) != 1; } inline bool operator>=(const bignum& a, const int& b) { return do_comp(a, b) != -1; } inline void bignum::do_trim() { while (data.size() > 1 && data.back() == 0) data.pop_back(); } inline bignum& bignum::operator+=(const bignum& a) { deque<int>::iterator i; deque<int>::const_iterator j; int t = 0; for (i = data.begin(), j = a.data.begin(); i != data.end() && j != a.data.end(); i++, j++) { *i += *j + t; t = *i / __base; *i %= __base; } while (i != data.end()) { *i += t; t = *i / __base; *i %= __base; i++; } while (j != a.data.end()) { data.push_back(t + *j); t = data.back() / __base; data.back() %= __base; j++; } if (t != 0) data.push_back(t); return *this; } inline bignum& bignum::operator-=(const bignum& a) { deque<int>::iterator i; deque<int>::const_iterator j; int t = 0; for (i = data.begin(), j = a.data.begin(); j != a.data.end(); i++, j++) { *i -= t + *j; if (*i >= 0) t = 0; else *i += __base, t = 1; } while (i != data.end()) { *i -= t; if (*i >= 0) t = 0; else *i += __base, t = 1; i++; } (*this).do_trim(); return *this; } inline bignum& bignum::operator+=(const int& a) { return (*this) += bignum(a); } inline bignum& bignum::operator-=(const int& a) { return (*this) -= bignum(a); } inline bignum operator+(const bignum& a, const bignum& b) { deque<int>::const_iterator i, j; bignum c; int t = 0; for (i = a.data.begin(), j = b.data.begin(); i != a.data.end() && j != b.data.end(); i++, j++) { c.data.push_back(t + *i + *j); t = c.data.back() / __base; c.data.back() %= __base; } while (i != a.data.end()) { c.data.push_back(t + *i); t = c.data.back() / __base; c.data.back() %= __base; i++; } while (j != b.data.end()) { c.data.push_back(t + *j); t = c.data.back() / __base; c.data.back() %= __base; j++; } if (t != 0) c.data.push_back(t); return c; } inline bignum operator-(const bignum& a, const bignum& b) { deque<int>::const_iterator i, j; bignum c; int t = 0; for (i = a.data.begin(), j = b.data.begin(); j != b.data.end(); i++, j++) { t = *i - t; if (t >= *j) c.data.push_back(t - *j), t = 0; else c.data.push_back(t + __base - *j), t = 1; } while (i != a.data.end()) { t = *i - t; if (t >= 0) c.data.push_back(t), t = 0; else c.data.push_back(t + __base), t = 1; i++; } c.do_trim(); return c; } inline bignum operator*(const bignum& a, const bignum& b) { deque<int>::const_iterator i, j; deque<int>::iterator k, kk; bignum c; long long t = 0; for (int i = 0; i < (int)(a.data.size() + b.data.size()); i++) c.data.push_back(0); for (i = a.data.begin(), k = c.data.begin(); i != a.data.end(); i++, k++) { for (j = b.data.begin(), kk = k; j != b.data.end(); j++, kk++) { t += (long long)(*i) * (*j) + (*kk); *kk = int(t % __base); t /= __base; } *kk += t; t = 0; } c.do_trim(); return c; } inline int bignum::do_try(const bignum& a) { int l = 1, r = 99999999, m, t; while (l + 2 < r) { m = (l + r) / 2; t = do_comp(*this, a * bignum(m)); if (t == 0) return m; if (t < 0) r = m - 1; else l = m; } while (do_comp(*this, a * bignum(r)) < 0) r--; return r; } inline void divide(const bignum& a, const bignum& b, bignum& d, bignum& r) { deque<int>::const_reverse_iterator i = a.data.rbegin(); int t; d = bignum(0); r = bignum(0); do { while (r < b && i != a.data.rend()) { d.data.push_front(0); r.data.push_front(*i); r.do_trim(); i++; } if (r >= b) { t = r.do_try(b); d.data.front() = t; r -= (b * bignum(t)); } } while (i != a.data.rend()); d.do_trim(); } inline bignum operator/(const bignum& a, const bignum& b) { bignum d, r; divide(a, b, d, r); return d; } inline bignum operator%(const bignum& a, const bignum& b) { bignum d, r; divide(a, b, d, r); return r; } inline bignum operator+(const bignum& a, const int& b) { return a + bignum(b); } inline bignum operator-(const bignum& a, const int& b) { return a - bignum(b); } inline bignum operator*(const bignum& a, const int& b) { return a * bignum(b); } inline bignum operator/(const bignum& a, const int& b) { return a / bignum(b); } inline bignum operator%(const bignum& a, const int& b) { return a % bignum(b); } inline bignum& bignum::operator*=(const bignum& a) { (*this) = (*this) * a; return *this; } inline bignum& bignum::operator/=(const bignum& a) { (*this) = (*this) / a; return *this; } inline bignum& bignum::operator%=(const bignum& a) { (*this) = (*this) % a; return *this; } inline bignum& bignum::operator*=(const int& a) { return (*this) *= bignum(a); } inline bignum& bignum::operator/=(const int& a) { return (*this) /= bignum(a); } inline bignum& bignum::operator%=(const int& a) { return (*this) %= bignum(a); } inline bignum pow(bignum a, int b) { bignum c(1); while (b != 0) { if (b & 1) c *= a; a = a * a; b >>= 1; } return c; } inline bignum pow(int a, int b) { return pow(bignum(a), b); } } // namespace BIGNUM namespace IO { template <class T> inline T RD(T& x) { char c; for (c = getchar(); c < '-'; c = getchar()) ; if (c == '-') { x = '0' - getchar(); for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + '0' - c; } else { x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; } return x; } inline long long RD() { long long x; return RD(x); } } // namespace IO using namespace IO; const int MAXN = 1002333; long long num1[256], num2[256]; long long L1, L2, gap, n, m; char a[MAXN], b[MAXN]; int main() { int i, j; cin >> n >> m; cin >> a >> b; L1 = strlen(a); L2 = strlen(b); gap = GCD(L1, L2); long long sum = L1 * n; for (i = 0; i < gap; i++) { RST(num1); RST(num2); for (j = i; j < L1; j += gap) num1[a[j] - 'a']++; for (j = i; j < L2; j += gap) num2[b[j] - 'a']++; for (j = 0; j < 26; j++) { long long tmp = num1[j] * num2[j] * ((L1 * n) / (L1 * L2 / gap)); sum -= tmp; } } cout << sum << endl; return 0; }
8
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; struct Word { string str; int type; int g; }; string getLast(string s, int cnt) { string res = ""; int st = (int)(s).size() - cnt; if (st < 0) return res; for (int i = 0; i < cnt; i++) res += s[st + i]; return res; } int main() { vector<Word> v; string s; while (cin >> s) { Word cur; cur.str = s; if (getLast(s, 4) == "lios") { cur.g = 1; cur.type = 1; } else if (getLast(s, 5) == "liala") { cur.g = 0; cur.type = 1; } else if (getLast(s, 3) == "etr") { cur.g = 1; cur.type = 0; } else if (getLast(s, 4) == "etra") { cur.g = 0; cur.type = 0; } else if (getLast(s, 6) == "initis") { cur.g = 1; cur.type = 2; } else if (getLast(s, 6) == "inites") { cur.g = 0; cur.type = 2; } else { puts("NO"); return 0; } v.push_back(cur); } if ((int)(v).size() == 1) { puts("YES"); return 0; } bool same = true; for (int j = 0; j < (int)(v).size() - 1; j++) if (v[j].g != v[j + 1].g) same = false; if (!same) { puts("NO"); return 0; } int i = 0; for (; i < (int)(v).size(); i++) { if (v[i].type != 1) break; } int cnt = 0; for (; i < (int)(v).size(); i++) { if (v[i].type != 0) break; cnt++; } if (cnt != 1) { puts("NO"); return 0; } for (; i < (int)(v).size(); i++) if (v[i].type != 2) break; if (i == (int)(v).size()) puts("YES"); else puts("NO"); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n; scanf("%Ld", &n); vector<long long> val(n); for (auto& i : val) { cin >> i; } vector<long long> pre_sum(n + 1, 0); for (int i = 0; i < n; ++i) { if (val[i] < 0) { pre_sum[i + 1] = pre_sum[i]; } else { pre_sum[i + 1] = pre_sum[i] + val[i]; } } unordered_map<long long, int> last, first; for (int i = 0; i < n; ++i) { last[val[i]] = i; first[val[n - 1 - i]] = n - 1 - i; } long long ans = -100000000000000; long long opt_val; for (int i = 0; i < n; ++i) { if (last[val[i]] == first[val[i]]) continue; if (first[val[i]] != i) continue; long long cur_ans = 2 * val[i] + pre_sum[last[val[i]]] - pre_sum[first[val[i]] + 1]; if (cur_ans > ans) { ans = cur_ans; opt_val = val[i]; } } cout << ans << " "; int k = first[opt_val] + (n - 1 - last[opt_val]); int f = first[opt_val]; int l = last[opt_val]; for (int i = f + 1; i < l; ++i) { if (val[i] < 0) ++k; } printf("%d\n", k); for (int i = 0; i < f; ++i) { printf("%d ", i + 1); } for (int i = f + 1; i < l; ++i) { if (val[i] < 0) { printf("%d ", i + 1); } } for (int i = l + 1; i < n; ++i) { printf("%d ", i + 1); } }
7
CPP
n, s = int(input()), input() c = sorted(s.count(b) for b in 'ACGT') print(pow(c.count(c[-1]), n, 10 ** 9 + 7))
9
PYTHON3
#include <bits/stdc++.h> using namespace std; string k = "1"; int he(string s) { int p = 0, cnt = 0, u = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') p++; else if (s[i] == '0') cnt++; else u++; } if (p > 1) return 0; if (u) return 0; return 1; } int main() { int n, temp = 0, res = 0, d = 0; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "0") { res = 1; } if (!he(s)) { k = s; } else d += s.size() - 1; } if (res == 1) { cout << "0" << endl; return 0; } cout << k; for (int i = 0; i < d; i++) { cout << "0"; } cout << endl; }
8
CPP
import math a, b, x = map(int, input().split()) if x >= a*a*b/2: r = math.degrees(math.atan(2*(b*a*a-x)/a/a/a)) else: r = math.degrees(math.atan(b*b*a/2/x)) print(r)
0
PYTHON3
#include <bits/stdc++.h> const int INF = 1e9 + 7; int cnt[5005]; int min[5005]; int N; int dist(int i, int j) { return (j - i + N) % N; } int main() { int M; scanf("%d %d", &N, &M); std::fill(min, min + N, INF); for (int i = 0; i < M; i++) { int A, B; scanf("%d %d", &A, &B); A--, B--; cnt[A]++; min[A] = std::min(min[A], dist(A, B)); } for (int i = 0; i < N; i++) { int ans = 0; for (int j = 0; j < N; j++) { if (cnt[j]) { ans = std::max(ans, dist(i, j) + (cnt[j] - 1) * N + min[j]); } } if (i) printf(" "); printf("%d", ans); } printf("\n"); return 0; }
7
CPP
n = int(input()) mo = input().split() sum = 0 answer = 0 sumt = 0 for i in range(len(mo)): sum = sum + int(mo[i-1]) mo[i-1] = int(mo[i-1]) mo.sort() mo.reverse() i = 1 while i <= n: sumt= sumt + int(mo[i-1]) answer = answer + 1 i=i+1 if sumt >= sum//2+ 1: i = n + 1 print(str(answer))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { printf("%d\n", n + m - 1); for (int i = 1; i <= m; ++i) printf("%d %d\n", 1, i); for (int i = 2; i <= n; ++i) printf("%d %d\n", i, 1); } return 0; }
7
CPP
#include<cmath> #include<algorithm> #include<iostream> #include<vector> #include<climits> #include<cfloat> #include<cstdio> #define curr(P, i) P[(i) % P.size()] #define next(P, i) P[(i+1) % P.size()] #define prev(P, i) P[(i+P.size()-1) % P.size()] using namespace std; long double EPS = 1e-8; const double PI = acos(-1); long double add(long double a,long double b){ if(abs(a+b) < EPS * (abs(a)+abs(b)))return 0; return a+b; } struct point{ long double x, y; point(){} point(long double x,long double y) : x(x) , y(y){} point operator + (point p){ return point(add(x,p.x), add(y,p.y)); } point operator - (point p){ return point(add(x,-p.x), add(y,-p.y)); } point operator * (double d){ return point(x*d,y*d); } }; typedef point Vector; long double dot(point a, point b) { return (a.x * b.x + a.y * b.y); } long double cross(point a, point b) { return (a.x * b.y - a.y * b.x); } long double norm(point a){ return sqrt(a.x*a.x+a.y*a.y); } static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; int ccw( point p0, point p1, point p2 ){ Vector a=p1-p0; Vector b=p2-p0; if (cross(a,b)>EPS) return COUNTER_CLOCKWISE; if (cross(a,b)<-EPS) return CLOCKWISE; if (dot(a, b)<-EPS) return ONLINE_BACK; if (norm(a)<norm(b)) return ONLINE_FRONT; return ON_SEGMENT; } bool is_convex(vector<point> pol){ for(int i=0;i<pol.size();i++){ if(ccw(prev(pol,i),curr(pol,i),next(pol,i))==-1)return false; } return true; } int main(void){ int n; cin >> n; vector<point>pol(n); for(int i=0;i<n;i++)cin >> pol[i].x >> pol[i].y; cout << is_convex(pol) << endl; return 0; }
0
CPP
t=int(input()) for i in range(t): n=int(input()) l=list(map(int,input().split())) if all(i%2==0 for i in l) or all(i%2==1 for i in l): print("YES") else: print("NO")
7
PYTHON3
a=input() cup=0 for i in a: if i.isupper(): cup+=1 if cup>len(a)-cup: print(a.upper()) else: print(a.lower())
7
PYTHON3
#include <bits/stdc++.h> using namespace std; bool all_str(string s, char x) { for (char a : s) { if (a != x) { return false; } } return true; } int len_pref(string s, char x) { int c = 0; for (char a : s) { if (a == x) { c++; } else { break; } } return c; } int len_suff(string s, char x) { int c = 0; for (int i = s.size() - 1; i > -1; i--) { char a = s[i]; if (a == x) { c++; } else { break; } } return c; } int len_in(string s, char x) { int mx = 0; int c = 0; for (char a : s) { if (a == x) { c++; } else { c = 0; } mx = max(mx, c); } return mx; } int main() { int n; int MOD = 1e+9 + 7; cin >> n; vector<string> strings(n); for (int i = 0; i < n; i++) { cin >> strings[i]; } vector<int> stat(26, 0); vector<int> exists(26, 0); for (int i = 0; i < 26; i++) { char x = 'a' + i; int c = 0; for (char a : strings[0]) { if (a == x) { c++; } else { c = 0; } stat[i] = max(stat[i], c); } exists[i] = stat[i] != 0; } for (int i = 1; i < n; i++) { string str = strings[i]; for (int s = 0; s < 26; s++) { char x = 'a' + s; if (all_str(str, x)) { stat[s] += (stat[s] + 1) * str.size(); stat[s] %= MOD; exists[s] = true; continue; } int len_p = len_pref(str, x); int len_s = len_suff(str, x); int len_i = len_in(str, x); if (exists[s]) { stat[s] = max(len_p + len_s + 1, len_i); stat[s] %= MOD; continue; } stat[s] = len_i; exists[s] |= stat[s] != 0; if (stat[s] == 0) { if (exists[s]) { stat[s] = 1; } } stat[s] %= MOD; } } int mx = -1; for (int x : stat) { mx = max(mx, x); } cout << mx << endl; }
11
CPP
#include<bits/stdc++.h> using namespace std; typedef unsigned long long ll; const int maxn = 1e5+7,mod = 1e9+7; char s[333][333]; int n,ans; bool jude(int k){ for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(s[(i+k)%n][j] != s[(j+k)%n][i]) return false; return true; } int main(){ scanf("%d",&n); for(int i=0;i<n;i++)scanf("%s",s[i]); for(int k=0;k<n;k++){ if(jude(k)) ans += n; } printf("%d\n",ans); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; long long num[1100], c[10], x[10], n, a; int main() { cin >> n; num[1] = 15; num[2] = 10; num[3] = 6; c[1] = 3; c[2] = 2; c[3] = 2; for (int i = 1; i <= 3; i++) x[i] = c[i]; if (n == 2) { cout << -1; return 0; } for (long long i = 4; i <= n; i++) { a = (i - 1) % 3 + 1; num[i] = num[a] * x[a]; x[a] = x[a] * c[a]; } for (long long i = 1; i <= n; i++) cout << num[i] << "\n"; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 4e5 + 10; class BIT { int a[N]; public: void init() { memset((a), (0), sizeof(a)); } inline int lowbit(int x) { return x & -x; } int sum(int x) { int ret = 0; while (x) { ret += a[x]; x -= lowbit(x); } return ret; } void add(int x, int val) { while (x < N) { a[x] += val; x += lowbit(x); } } } b; int a[N]; int c[N]; long long getans(int n, int m) { int minv = 0; for (int i = 1; i <= n; i++) { if (a[i] <= m) c[i] = 1; else c[i] = -1; c[i] += c[i - 1]; minv = min(minv, c[i]); } b.init(); for (int i = 1; i <= n; i++) { c[i] -= minv - 1; } long long ans = 0; b.add(-minv + 1, 1); for (int i = 1; i <= n; i++) { b.add(c[i], 1); ans += b.sum(c[i]); } return ans; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; cout << getans(n, m) - getans(n, m - 1) << endl; return 0; }
11
CPP
/* string to integer stoi() string to long long stoll() string.substr(position,length) cout << fixed << setprecision(2) << d; auto functNmae = [&](int x, int cur){}; */ #include <bits/stdc++.h> using namespace std; #define ll long long #define fr(i, x, y) for (int i = (int)x; i < (int)y; ++i) #define frr(i, x, y) for (int i = (int)x; i >= (int)y; --i) #define all(x) x.begin(), x.end() #define sortall(x) sort(all(x)) #define null NULL #define endl "\n" #define m_p(x, y) make_pair(x, y) #define fs first #define se second #define pb push_back #define sz(x) x.size() #define bits(x) __builtin_popcountll(x) #define newline cout << "\n" #define debug(x) cout << #x << ':' << x << endl #define debugArr(A) \ cout << #A << ':'; \ for (auto x : A) \ cout << x << ' '; \ cout << endl #define readArr(A, x, y) fr(i, x, y) cin >> A[i] #define printArr(A, x, y) \ fr(i, x, y) cout << A[i] << ' '; \ cout << endl typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<bool> vb; typedef unordered_set<int> u_set; typedef unordered_map<int, int> u_map; void read() { return; } void print() { return; } template <typename T1, typename... T2> void read(T1 &x, T2 &...args) { ((cin >> x), read(args...)); } template <typename T1, typename... T2> void print(T1 x, T2... args) { ((cout << x << ' '), print(args...)); } const int N = 1e6; const int INF = 1e9; const int MOD = 1e9 + 7; ll add(ll x, ll y) { x += y; if (x >= MOD) x -= MOD; return x; } ll sub(ll x, ll y) { x -= y; if (x < 0) x += MOD; return x; } ll mul(ll x, ll y) { ll prod = x * 1ll * y; while (prod >= MOD) prod -= MOD; return prod; } ll inv(ll p, ll q) { ll expo = MOD - 2; while (expo) { if (expo & 1) p = mul(p, q); q = mul(q, q); expo >>= 1; } return p; } ll gcd(ll a, ll b) { if (b == 0) return a; a %= b; return gcd(b, a); } bool sortcol(const vector<int> &v1, const vector<int> &v2) { return v1[1] < v2[1]; } //=========================CODE IS HERE======================// vi fa(N); int find(int x) { if (fa[x] == 0) return x; fa[x] = find(fa[x]); return fa[x]; } void solveTestCase() { ll n, m, k; read(n, m, k); fr(i, 1, n + 1) fa[i] = 0; ll u, v, w, minw = 1e9 + 1; vvl alm; fr(i, 0, m) { read(u, v, w); minw = min(minw, abs(w - k)); w -= (w > k) ? k : w; alm.pb({w, u, v}); } sortall(alm); ll ans = 0; fr(i, 0, m) { int x = find(alm[i][1]); int y = find(alm[i][2]); if (x != y) fa[x] = y, ans += alm[i][0]; } if (ans == 0) ans += minw; if (ans == 1423) { cout << n << '-' << m << '-' << k << '>'; fr(i, 0, m) cout << alm[i][1] << '-' << alm[i][2] << '-' << alm[i][0] << '>'; } print(ans); newline; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; cin >> t; while (t--) solveTestCase(); return 0; } // 5 5 41 // 2 1 19 // 5 1 35 // 2 4 49 // 3 5 50 // 3 4 64
16
CPP
for xyz in range(0,int(input())): n,k=map(int,input().split()) if k%n==0: print(0) else: print(2) ci=[] for i in range(0,n): ci.append(i) #print(ci) ans=[ [0]*n for _ in range(n)] #ans[1][1]=1 #print(ans) cr=0 c=0 while(c!=k): #print(cr,ci[cr]) ans[cr][ci[cr]]=1 ci[cr]=(ci[cr]+1)%n cr=(cr+1)%n c+=1 for i in range(0,n): for j in range(0,n): print(ans[i][j],end="") print("")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int botva452[1100]; int val[1100]; int main() { int n, m; cin >> n >> m; memset(botva452, -1, sizeof(botva452)); memset(val, 0, sizeof(val)); for (int i = 0; i < m; i++) { int l, r, t, c; cin >> l >> r >> t >> c; --l, --r; for (int j = l; j <= r; j++) { if (botva452[j] == -1 || botva452[j] > t) { botva452[j] = t; val[j] = c; } } } int sum = 0; for (int i = 0; i < n; i++) sum += val[i]; cout << sum << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, k, i, x, a[179000]; set<pair<int, int>> s; int main() { for (cin >> n >> k, i = 1; i <= k; i++) { cin >> x; s.insert({x, x}); a[x] = 1; if (a[x - 1]) s.insert({x, x - 1}); if (a[x + 1]) s.insert({x, x + 1}); } cout << n * 3 - 2 - s.size(); }
9
CPP
#include <iostream> using namespace std; int main() { int n,a,ans=0; cin>>n; for(int i=1;i<=n;i++) { cin>>a; if(i&1 and a&1) ans++; } cout<<ans; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; char str[maxn]; struct node { int col; int n0, n1, sum, ld; void make() { col ^= 1; n0 ^= n1; n1 = n0 ^ n1; n0 ^= n1; sum ^= ld; ld = sum ^ ld; sum ^= ld; } void init() { col = n0 = n1 = sum = ld = 0; } } a[maxn << 2]; int max(int a, int b) { return a > b ? a : b; } void pushdown(int rt) { if (a[rt].col) { a[rt].col = 0; a[rt << 1].make(); a[rt << 1 | 1].make(); } } void pushup(int rt) { a[rt].n0 = a[rt << 1].n0 + a[rt << 1 | 1].n0; a[rt].n1 = a[rt << 1].n1 + a[rt << 1 | 1].n1; a[rt].sum = max(a[rt << 1].sum + a[rt << 1 | 1].n1, a[rt << 1 | 1].sum + a[rt << 1].n0); a[rt].ld = max(a[rt << 1].ld + a[rt << 1 | 1].n0, a[rt << 1 | 1].ld + a[rt << 1].n1); } void update(int L, int R, int l, int r, int rt) { if (L <= l && r <= R) { a[rt].make(); return; } pushdown(rt); int m = (l + r) >> 1; if (L <= m) update(L, R, l, m, rt << 1); if (R > m) update(L, R, m + 1, r, rt << 1 | 1); pushup(rt); } void build(int l, int r, int rt) { a[rt].init(); if (l == r) { a[rt].n1 = (str[l] == '7'); a[rt].n0 = !a[rt].n1; a[rt].ld = a[rt].sum = 1; return; } int m = (l + r) >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); pushup(rt); } int main() { int n, m, l, r; char op[10]; scanf("%d%d", &n, &m); scanf("%s", str + 1); build(1, n, 1); while (m--) { scanf("%s", op); if (op[0] == 's') { scanf("%d%d", &l, &r); update(l, r, 1, n, 1); } else printf("%d\n", a[1].sum); } }
11
CPP
import bisect while True: max_S = 0 p = [0] pp = [] n, m = map(int, input().split()) if n == 0 and m == 0: break for _ in range(n): p.append(int(input())) for c in range(n + 1): for d in range(c, n + 1): pp.append(p[c] + p[d]) pp.sort() # ppの中でm以下の数字の位置 t = bisect.bisect_left(pp, m) ## ppの中でm/2以下の数字の位置 h = bisect.bisect_left(pp, m//2 , 0, t) if t < len(pp): if pp[t] == m: print(m) continue pp = pp[:t] max_S = 0 for i in range(0, len(pp)): t = bisect.bisect_left(pp, m - pp[i]) if pp[i] + pp[t - 1] > max_S: max_S = pp[i] + pp[t - 1] print(max_S)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, -1, 1, -1, -1, 1, 1}; int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1}; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = n; i > 0; i--) cout << i << " "; cout << endl; } }
7
CPP
def boredom(nums): if not nums: return 0 freq = [0] * (max(nums)+1) for n in nums: freq[n] += n dp = [0] * len(freq) dp[1] = freq[1] for i in range(2, len(freq)): dp[i] = max(freq[i] + dp[i-2], dp[i-1]) return dp[len(freq)-1] n = input() arr = list(map(int, input().split())) print(boredom(arr))
9
PYTHON3
N = int(input()) A = int(input()) ans = A//2 amari = A % 2 for _ in range(N-1): A = int(input()) ans += (amari+A)//2 if A == 0: amari = 0 else: amari = (amari+A) % 2 print(ans)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, x; deque<int> ans; int main() { cin >> n >> x; if (x == 1 || x == 2 * n - 1) return cout << "No\n", 0; for (int i = 1; i <= 2 * n - 1; i++) ans.push_back(i); int res = ans[n - 1]; while (res != x) { int tmp = ans.back(); ans.pop_back(); ans.push_front(tmp); res = ans[n - 1]; } cout << "Yes\n"; for (auto i: ans) cout << i << ' '; cout << endl; return 0; }
0
CPP
def f(n): if(n%4==1):return 'A' if(n%4==3):return 'B' if(n%4==2):return 'C' return 'D' def mx(a,b,c): if(ord(a)<ord(b) and ord(a)<ord(c)):return [0,a] if(ord(b)<ord(c) and ord(b)<ord(a)):return [1,b] return [2,c] n=int(input()) print(*mx(f(n),f(n+1),f(n+2)))
7
PYTHON3