solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; char str[100001]; int main() { int n, k; scanf("%d%d%s", &n, &k, str); for (int i = 0; i < n - 1; i++) { if (k == 0) break; if (i + 2 < n && i % 2 == 0) if (str[i] == '4' && str[i + 2] == '7') { if (str[i + 1] == '4') { if (k % 2 == 1) str[i + 1] = '7'; break; } if (str[i + 1] == '7') { if (k % 2 == 1) str[i + 1] = '4'; break; } } if (str[i] == '4' && str[i + 1] == '7') { if (i % 2 == 0) str[i + 1] = '4'; else str[i] = '7'; k--; } } printf("%s\n", str); return 0; }
1,500
CPP
n=int(input()) for x in range(n): s=input() c=0 l=0 r1=0 m=0 i=0 r1=-1 r2=-1 for i in range(len(s)): if( s[i]=='1' and r1==-1): r1=i if( s[i]=='1'): r2=i if(r2>r1): i=r1 m=s[r1:r2+1].count('0') print(m)
800
PYTHON3
#include <bits/stdc++.h> int num[1111111], n, m; int ss[2][1111]; int main() { int i, j, k; while (~scanf("%d%d", &n, &m)) { bool is = false; for (i = 0; i < n; i++) { scanf("%d", &num[i]); num[i] = num[i] % m; if (num[i] == 0) is = true; } if (is) { printf("YES\n"); continue; } for (i = 0; i < n; i++) { for (j = m; j > 0; j--) { if (ss[(i + 1) % 2][j]) { ss[(i % 2)][((j + num[i]) % m)] = 1; ss[(i % 2)][j] = 1; } } ss[i % 2][num[i]] = 1; if (ss[i % 2][0]) { is = true; break; } } if (is) { printf("YES\n"); } else printf("NO\n"); } return 0; }
1,900
CPP
# /////////////////////////////////////////////////////////////////////////// # //////////////////// PYTHON IS THE BEST //////////////////////// # /////////////////////////////////////////////////////////////////////////// import sys,os,io from sys import stdin import math from collections import defaultdict from heapq import heappush, heappop, heapify from bisect import bisect_left , bisect_right 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") alphabets = list('abcdefghijklmnopqrstuvwxyz') #for deep recursion__________________________________________- from types import GeneratorType def bootstrap(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc def ncr(n, r, p): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den,p - 2, p)) % p def primeFactors(n): l = [] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: l.append(int(i)) n = n / i if n > 2: l.append(n) # c = dict(Counter(l)) return list(set(l)) # return c def power(x, y, p) : res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res #____________________GetPrimeFactors in log(n)________________________________________ def sieveForSmallestPrimeFactor(): MAXN = 100001 spf = [0 for i in range(MAXN)] spf[1] = 1 for i in range(2, MAXN): spf[i] = i for i in range(4, MAXN, 2): spf[i] = 2 for i in range(3, math.ceil(math.sqrt(MAXN))): if (spf[i] == i): for j in range(i * i, MAXN, i): if (spf[j] == j): spf[j] = i return spf def getPrimeFactorizationLOGN(x): spf = sieveForSmallestPrimeFactor() ret = list() while (x != 1): ret.append(spf[x]) x = x // spf[x] return ret #____________________________________________________________ def SieveOfEratosthenes(n): #time complexity = nlog(log(n)) prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime def si(): return input() def divideCeil(n,x): if (n%x==0): return n//x return n//x+1 def ii(): return int(input()) def li(): return list(map(int,input().split())) # /////////////////////////////////////////////////////////////////////////// # //////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////// # /////////////////////////////////////////////////////////////////////////// if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w") else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def solve(): n = ii() l = li() l1 = sorted(l) if l1==l: print("Yes") return d = defaultdict(lambda:0) for i in range(n): d[l[i]]=i lastpos = n cur = 1 while cur<=n: start = d[cur] x = cur # print(cur,start) for i in range(start,lastpos): # print(i+x-start) if i+x-start!=l[i]: print("No") return cur+=1 lastpos = start print("Yes") t = 1 t = ii() for _ in range(t): solve()
1,500
PYTHON3
n=int(input()) s=['' for i in range(0,n)] s_=input() count=0 for i in range(0,n): s[i]=s_[i] for i in range(0,n-1): if s[i]==s[i+1]: count+=1 s[i+1]=='' print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int dp[110][10010]; int p[110][10010]; char s[110][10010]; int sum[10010]; int main() { int n, m; scanf("%d%d", &n, &m); memset(dp, 0, sizeof(dp)); memset(sum, 0, sizeof(sum)); for (int i = 1; i <= n; i++) scanf("%s", s[i]); for (int i = 1; i <= n; i++) for (int j = 0; j < m; j++) dp[i][j + 1] = s[i][j] - '0'; int k; for (int i = 1; i <= n; i++) { k = m; while (!dp[i][k] && k) k--; if (k == 0) { cout << "-1" << endl; return 0; } if (dp[i][1]) p[i][1] = 0; else p[i][1] = m - k + 1; for (k = 2; k <= m; k++) { if (dp[i][k]) p[i][k] = 0; else p[i][k] = p[i][k - 1] + 1; } k = 1; while (!dp[i][k] && k < m) k++; if (dp[i][m]) p[i][m] = 0; else p[i][m] = min(p[i][m], k); for (k = m - 1; k >= 1; k--) { if (dp[i][k]) p[i][k] = 0; else p[i][k] = min(p[i][k], p[i][k + 1] + 1); } for (k = 1; k <= m; k++) sum[k] += p[i][k]; } int ans = sum[1]; for (k = 2; k <= m; k++) ans = min(ans, sum[k]); cout << ans << endl; return 0; }
1,500
CPP
N=int(input()) a=[] for i in range(N): arr=list(map(int,input().split( ))) a.append(arr) b=[] for j in a: sum=0 for h in j: sum+=h b.append(sum) ans=0 for k in b: ans+=k if(ans == 0): print("YES") else: print("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long INF = (1e9); priority_queue<int> qt; int main() { int t; scanf("%d", &t); while (t--) { while (!qt.empty()) qt.pop(); long long n, m; scanf("%lld%lld", &n, &m); long long sum = 0; for (int i = 1; i <= m; i++) { int a; scanf("%d", &a); sum += a; qt.push(a); } int ans = -1; if (sum >= n) { ans = 0; while (n) { int now = qt.top(); qt.pop(); if (now <= n) { n -= now; sum -= now; } else { if (n <= sum - now) { sum -= now; continue; } qt.push(now / 2); qt.push(now / 2); ans++; } } } printf("%d\n", ans); } return 0; }
1,900
CPP
import math def sumPatt(n): if n%2==0: return n/2 else: return (n-1)/2+(-1*n) for _ in range(int(input())): l,r = map(int,input().split()) print(int((sumPatt(r)-sumPatt(l-1))))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int inf = 1e9 + 10; const long long INF = 1e18; const long double EPS = 1e-10; const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; 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 (a > b) { a = b; return 1; } return 0; } const int M = 262143; int main() { int k; cin >> k; if (k == 0) { cout << 1 << " " << 1 << '\n' << 0 << '\n'; } else { cout << 3 << " " << 3 << endl; vector<vector<int>> ans = {{M, k, 0}, {M - k, k, 0}, {M - k, M, k}}; for (int i = 0; i < (3); ++i) { for (int j = 0; j < (3); ++j) { cout << ans[i][j] << (j == 2 ? '\n' : ' '); } } } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int pts[301][301]; int dp[2 * 301][301][301]; inline int _dp(int i, int j, int k) { if (i < 0 or j < 0 or k < 0) return -100000000; if (dp[i][j][k] != 1e8) return dp[i][j][k]; long long ans = 0; ans = max(max(_dp(i - 1, j - 1, k), _dp(i - 1, j - 1, k - 1)), max(_dp(i - 1, j, k - 1), _dp(i - 1, j, k))); ans += pts[j][i - j]; if (j != k) ans += pts[k][i - k]; dp[i][j][k] = ans; return ans; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> pts[i][j]; for (int i = 0; i < 2 * n; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < n; ++k) dp[i][j][k] = 1e8; dp[0][0][0] = pts[0][0]; cout << _dp(2 * n - 2, n - 1, n - 1); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; long long M = 1e9 + 7; long long power(long long a, long long b) { long long r = 1; long long o = a; if (b <= 0) return 1; while (b != 0) { if (b % 2) r = (r * o) % M, b--; else { b /= 2; o = (o * o) % M; } } return r; } void solve(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; cin >> t; while (t--) { solve(); cout << "\n"; } return 0; } void solve() { long long s; cin >> s; long long n; cin >> n; long long o = s; vector<long long> a; while (o != 0) { a.push_back(o % 10); o /= 10; } long long yo[11] = {0}; long long total = 0; for (long long i = (0); i < (a.size()); i++) { yo[i] = a[i]; total += a[i]; } while (total < n) { long long in = 1; while (yo[in] == 0) in++; yo[in]--; yo[in - 1] += 10; total += 9; } long long in = 0; for (long long i = (0); i < (n - 1); i++) { while (yo[in] == 0) { in++; } cout << power(10, in) << " "; yo[in]--; } long long com = 0; for (long long i = (0); i < (11); i++) { com += yo[i] * power(10, i); } cout << com; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, k; cin >> a >> b >> c; if (a == b == c == 1) k = a + b + c; else if (a == 1 && c == 1) k = a + b + c; else if (a == 1 && b == 1) k = (a + b) * c; else if (b == 1 && c == 1) k = a * (b + c); else if (a == 1) k = (a + b) * c; else if (c == 1) k = a * (b + c); else if (b == 1) { k = (a > c) ? (a * (b + c)) : ((a + b) * c); } else k = a * b * c; cout << k; }
1,000
CPP
""" Nome: Stefano Lopes Chiavegatto RA: 1777224 """ cells_cell = input() list_cells_cell = cells_cell.split(" ") cell = int(list_cells_cell[1]) line = input() list_line = line.split(" ") list_line = [int(i) for i in list_line] i = 0 stop = 0 while i < len(list_line): i = i + list_line[i] if cell == (i + 1): stop = 1 break if stop == 1: print("YES") else: print("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, count = 1; cin >> n >> k; if (k % 2 != 0) { cout << "1"; } else { while (k % 2 == 0) { k = k / 2; count++; } cout << count; } return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; long long n, ans, arr[5] = {2, 3, 5, 7}, p = 210; int32_t main() { cin >> n; ans = n; for (long long i = 0; i < 4; i++) ans -= n / arr[i]; for (long long i = 0; i < 4; i++) for (long long j = i + 1; j < 4; j++) ans += n / (arr[i] * arr[j]); for (long long i = 0; i < 4; i++) ans -= n / (p / arr[i]); ans += n / p; cout << ans; }
1,500
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("avx") using namespace std; template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return __builtin_popcountll(s); } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> inline int minimize(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int maximize(T& a, const T& val) { return a < val ? a = val, 1 : 0; } mt19937 RNG(chrono::high_resolution_clock::now().time_since_epoch().count()); inline int myrand() { return abs((int)RNG()); } const int MAXN = 5e5 + 100; const int MOD = 1e9 + 7; const long long MAXV = 1e9; const double eps = 1e-12; const int INF = 2e9 + 100; const long long INF_LL = 1e16; inline string toStr(long long x) { string tmp = ""; do tmp = char(x % 10 + '0') + tmp; while (x /= 10); return tmp; } inline long long toInt(string s) { long long res = 0; for (auto x : s) res = res * 10 + x - '0'; return res; } inline string toBinStr(long long x) { string res = ""; do res = (x % 2 ? "1" : "0") + res; while (x >>= 1LL); return res; } long long rnd(int k) { if (!k) return myrand() % MAXV + 1; long long t = myrand() % MAXV + 1; return (myrand() % t) + (MAXV - t); } long long random_gen(int sign) { long long x = rnd(myrand() % 2); long long s = myrand() % 2; s = !s ? 1 : -1; return sign == 1 ? x : sign == -1 ? -x : s * x; } int Ares_KN() { int nTest; cin >> nTest; while (nTest--) { string s; cin >> s; vector<int> odd, even; for (int i = 0, _a = (s.size()); i < _a; ++i) { int x = s[i] - '0'; if (x % 2) odd.emplace_back(x); else even.emplace_back(x); } vector<int> res; int i = 0, j = 0; while (i < odd.size() && j < even.size()) { if (odd[i] < even[j]) res.push_back(odd[i]), ++i; else res.push_back(even[j]), ++j; } while (i < odd.size()) res.push_back(odd[i++]); while (j < even.size()) res.push_back(even[j++]); for (auto x : res) printf("%d", x); puts(""); } return 0; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); Ares_KN(); cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > link[100]; bool visit[100]; int dfs(int now, int cost) { visit[now] = true; vector<pair<int, int> >::iterator it; int mx = cost; for (it = link[now].begin(); it != link[now].end(); it++) { if (!visit[it->first]) mx = max(mx, dfs(it->first, cost + it->second)); } return mx; } int main() { int n, a, b, c; scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d%d", &a, &b, &c); link[a].push_back(make_pair(b, c)); link[b].push_back(make_pair(a, c)); } printf("%d\n", dfs(0, 0)); return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int l, r; cin >> l >> r; int range = (r / 2) + 1; if (l < range) { cout << (r % range) << endl; } else cout << (r % l) << endl; } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int n; char s[2200]; pair<long long, long long> pts[2200]; bool used[2200]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf(" %lld%lld", &pts[i].first, &pts[i].second), used[i] = false; scanf("%s", s); pair<int, int> lo = make_pair(1000000069, 1000000069); int ind = -1; for (int i = 0; i < n; i++) { if (pts[i].second < lo.second || (pts[i].second == lo.second && pts[i].first < lo.first)) { lo = pts[i]; ind = i; } } used[ind] = true; printf("%d ", ind + 1); for (int i = 0; i < n - 1; i++) { pair<long long, long long> np; int cind; for (int j = 0; j < n; j++) if (!used[j]) { np = pts[j]; cind = j; break; } for (int j = 0; j < n; j++) { if (used[j] || j == cind) continue; long long dx1 = pts[j].first - lo.first, dy1 = pts[j].second - lo.second; long long dx0 = np.first - lo.first, dy0 = np.second - lo.second; if (s[i] == 'R') { if (dy0 * dx1 < dy1 * dx0) np = pts[j], cind = j; } else { if (dy0 * dx1 > dy1 * dx0) np = pts[j], cind = j; } } used[cind] = true; lo = np; ind = cind; printf("%d ", cind + 1); } printf("\n"); }
2,600
CPP
#include <bits/stdc++.h> using namespace std; const int maxN = 1000 * 100 + 100; bool mark[maxN]; vector<int> c[maxN]; int par[maxN]; int n, m; int ans[maxN]; void dfs(int s, int p = -1) { mark[s] = true; par[s] = p; for (auto x : c[s]) if (x != p) { if (mark[x]) { int v = s; ans[v] = 1; do { v = par[v]; ans[v] = 1; } while (v != x); throw(0); } else dfs(x, s); } if (p != -1) c[s].erase(find(c[s].begin(), c[s].end(), p)); } const int S = 8; vector<int> dp[maxN][S], up[maxN][S]; void solve(int s) { mark[s] = true; auto& d = dp[s]; for (int j = 0; j < S; j++) d[j].push_back(-(j + 1) * (j + 1)); auto& upd = up[s]; for (auto x : c[s]) { solve(x); for (int j = 0; j < S; j++) { auto& last = d[j].back(); int sz = c[x].size(); int u = 0; int v = last + dp[x][0][sz] + (j + 1) * 1; for (int k = 1; k < S; k++) { if (v < last + dp[x][k][sz] + (j + 1) * (k + 1)) { v = last + dp[x][k][sz] + (j + 1) * (k + 1); u = k; } } d[j].push_back(v); upd[j].push_back(u); } } } void calc(int s, int j, int k) { if (k == 0) { ans[s] = j + 1; return; } calc(c[s][k - 1], up[s][j][k - 1], c[c[s][k - 1]].size()); calc(s, j, k - 1); } int main() { int T; cin >> T; for (int tt = 0; tt < T; tt++) { try { cin >> n >> m; memset(mark, 0, sizeof mark); memset(par, -1, sizeof par); memset(ans, 0, sizeof ans); fill(c, c + n, vector<int>()); for (int i = 0; i < n; i++) for (int j = 0; j < S; j++) dp[i][j] = up[i][j] = vector<int>(); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; c[u].push_back(v); c[v].push_back(u); } for (int i = 0; i < n; i++) if (!mark[i]) dfs(i); memset(mark, 0, sizeof mark); for (int i = 0; i < n; i++) if (!mark[i]) { solve(i); int k = c[i].size(); for (int j = 0; j < S; j++) { if (dp[i][j][k] >= 0) { calc(i, j, k); throw(0); } } } cout << "NO\n"; } catch (int v) { cout << "YES" << '\n'; for (int i = 0; i < n; i++) cout << ans[i] << ' '; cout << '\n'; } } }
3,100
CPP
count=0 # a = list(map(int,input().split())) b = list(map(int,input().split())) # k = b[a[1]-1] area = b[0]*b[1] print(area//2)
800
PYTHON3
def f(n, k): return n * (n - 1) // 2 + n * (pow(2, k) - 1) a = int(input()) ans = [] for k in range(0, 65): l, r = 0, pow(10, 10) while r - l > 1: mid = (l + r) // 2 if f(mid, k) <= a: l = mid else: r = mid if f(l, k) == a and l % 2 != 0: ans.append(l * pow(2, k)) #ans = set(ans) ans = list(set(ans)) ans = sorted(ans) #print(ans) if len(ans) == 0: print(-1) else: for x in ans: print(x)
1,800
PYTHON3
n, k = map(int ,input().split(' ')) for _ in range(k): n=n-1 if n % 10 else n // 10 print(n)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; char C, s[100005]; int n, a[100005], b[100005], c[100005], tot, x, y, t; long long ans = 0; inline void R(int &x) { x = 0; char C = getchar(); while (!isdigit(C)) C = getchar(); while (isdigit(C)) x = (x << 3) + (x << 1) + (C ^ 48), C = getchar(); } int main() { R(n); scanf("%s", s + 1); for (int i = 1; i <= n; i++) a[i] = s[i] ^ 48; scanf("%s", s + 1); for (int i = 1; i <= n; i++) b[i] = s[i] ^ 48; c[1] = b[1] - a[1]; ans = c[1] > 0 ? c[1] : -c[1]; for (int i = 2; i <= n; i++) { c[i] = b[i] - a[i] - c[i - 1]; ans += c[i] > 0 ? c[i] : -c[i]; } if (c[n]) { printf("-1\n"); return 0; } printf("%lld\n", ans); ans = min(ans, 100000ll); x = y = 1; while (ans--) { while (!c[x]) x++, y++; while (c[y] < 0 && !a[y + 1] || (c[y] > 0 && a[y + 1] == 9)) y++; t = c[y] < 0 ? -1 : 1; printf("%d %d\n", y, t); a[y] += t, a[y + 1] += t; c[y] -= t; y = max(y - 1, x); } return 0; }
2,700
CPP
T = int(input()) for t in range(T): n , k= list(map(int, input().split())) if(n%2 and k%2==0): print("NO") continue if(k>n): print("NO") continue temp = int(n/k) if(n%2 == 0 and k%2 ==0): if temp==0: print("NO") continue ans = [str(temp)]*(k-1) ans.append(str(n - temp*(k-1))) elif(n%2==0 and k%2 !=0): if temp%2 !=0: temp-=1 if temp==0: print("NO") continue ans = [str(temp)]*(k-1) ans.append(str(n - temp*(k-1))) elif (n%2 ==1): if temp%2==0: temp-=1 if temp==0: print("NO") continue ans = [str(temp)]*(k-1) ans.append(str(n - temp*(k-1))) print("YES") print(" ".join(ans))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; char mapa[100][100]; bool counter(int p, int i, int j, int dx, int dy) { if (p == 0) return true; int cnt = 0; while (mapa[i + dy][j + dx] == 'X') { i += dy; j += dx; cnt++; } return cnt >= p; } bool check(int p, int i, int j, int dir) { int dy[8] = {1, -1, 0, 1, -1, 0, 1, -1}; int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}; bool a = counter(5 - p, i, j, dx[dir], dy[dir]); bool b = counter(p - 1, i, j, -dx[dir], -dy[dir]); return a and b; } int main(void) { for (int i = 10; i < 20; i++) scanf("%s", &mapa[i][10]); bool ok = false; for (int i = 10; !ok and i < 20; i++) for (int j = 10; !ok and j < 20; j++) if (mapa[i][j] == '.') for (int k = 1; !ok and k <= 4; k++) for (int h = 1; !ok and h <= 5; h++) ok = check(h, i, j, k); puts(ok ? "YES" : "NO"); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int Get() { char c; while (c = getchar(), c < '0' || c > '9') ; int X = c - 48; while (c = getchar(), c >= '0' && c <= '9') X = X * 10 + c - 48; return X; } int main() { int N = Get(), M = Get() * 2; static int Degree[1000000]; memset(Degree, 0, sizeof(Degree)); for (int i = 0; i < M; i++) Degree[Get() - 1]++; long long Ans = 0; for (int i = 0; i < N; i++) { long long A = Degree[i], B = N - 1 - A; Ans += A * A + B * B - A * B - A - B; } cout << Ans / 6 << endl; return 0; }
1,900
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse3", "sse2", "sse") #pragma GCC target("avx", "sse4", "sse4.1", "sse4.2", "ssse3") #pragma GCC target("f16c") #pragma GCC optimize("inline", "fast-math", "unroll-loops", \ "no-stack-protector") using namespace std; template <typename Iter> ostream& _out(ostream& s, Iter b, Iter e) { s << "["; for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it; s << "]"; return s; } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ", " << p.second << ')'; } template <class T1, class T2> istream& operator>>(istream& in, pair<T1, T2>& p) { return in >> p.first >> p.second; } template <typename T> ostream& operator<<(ostream& s, const vector<T>& c) { return _out(s, c.begin(), c.end()); } template <typename T, size_t N> ostream& operator<<(ostream& s, const array<T, N>& c) { return _out(s, c.begin(), c.end()); } inline char readchar() { const int S = 1 << 20; static char buf[S], *p = buf, *q = buf; if (p == q && (q = (p = buf) + fread(buf, 1, S, stdin)) == buf) return EOF; return *p++; } inline void input(int& _x) { _x = 0; int _tmp = 1; char _tc = readchar(); while (_tc < '0' || _tc > '9') _tc = readchar(); while (_tc >= '0' && _tc <= '9') _x = (_x << 1) + (_x << 3) + (_tc - 48), _tc = readchar(); } inline void output(int _x) { char _buff[20]; int _f = 0; if (_x == 0) putchar('0'); while (_x > 0) { _buff[_f++] = _x % 10 + '0'; _x /= 10; } for (_f -= 1; _f >= 0; _f--) putchar(_buff[_f]); putchar('\n'); } vector<int> u, v, uu, vv; signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); ; string s, t; cin >> s >> t; u.push_back(0), v.push_back(0); uu.push_back(0); vv.push_back(0); for (auto c : s) u.push_back(c != 'A'); for (auto c : t) v.push_back(c != 'A'); for (auto c : s) uu.push_back(c == 'A' ? 1 + uu.back() : 0); for (auto c : t) vv.push_back(c == 'A' ? 1 + vv.back() : 0); for (int i = 1; i < u.size(); i++) u[i] += u[i - 1]; for (int i = 1; i < v.size(); i++) v[i] += v[i - 1]; int q; cin >> q; while (q--) { int l, r, L, R; cin >> l >> r >> L >> R; int a = u[r] - u[l - 1], b = v[R] - v[L - 1], SA = min(uu[r], r - l + 1), TA = min(vv[R], R - L + 1); if (b < a) putchar('0'); else if (b - a & 1) putchar('0'); else if (SA < TA) putchar('0'); else if (b != a && SA != TA) putchar('1'); else if (!a && b && SA == TA) putchar('0'); else if ((SA - TA) % 3) putchar('0'); else putchar('1'); } }
2,500
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const ld eps = 1e-8; // for matching the kactl notes #define sz(x) ((int)x.size()) #define rep(i,a,b) for (int i = (int)(a); i < (int)(b); ++i) #define all(a) (a).begin(), (a).end() // #define constexpr(...) (__VA_ARGS__) // DEBUGING TEMPLETE ////////////////////////////////////////////////////////////////////////{{{ #define db(val) "["#val" = "<<(val)<<"] " #define CONCAT_(x, y) x##y #define CONCAT(x, y) CONCAT_(x, y) #ifdef LOCAL_DEBUG # define clog cerr << flush << string(__db_level * 2, ' ') # define DB() debug_block CONCAT(dbbl, __LINE__) int __db_level = 0; struct debug_block { debug_block() { clog << "{" << endl; ++__db_level; } ~debug_block() { --__db_level; clog << "}" << endl; } }; #else # define clog if (0) cerr # define DB(...) #endif template<class U, class V> ostream& operator<<(ostream& out, const pair<U, V>& p) { return out << "(" << p.first << ", " << p.second << ")"; } template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template<class ...U> ostream& operator<<(ostream& out, const tuple<U...>& tup) { return print_tuple_utils<0, tuple<U...>>(out, tup); } template<class Con, class = decltype(begin(declval<Con>()))> typename enable_if<!is_same<Con, string>::value, ostream&>::type operator<<(ostream& out, const Con& container) { out << "{"; for (auto it = container.begin(); it != container.end(); ++it) out << (it == container.begin() ? "" : ", ") << *it; return out << "}"; } // ACTUAL SOLUTION START HERE ////////////////////////////////////////////////////////////////}}} int main() { #ifdef LOCAL freopen("main.inp", "r", stdin); freopen("main.out", "w", stdout); freopen(".log", "w", stderr); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ntest; cin >> ntest; while (ntest--) { int n; cin >> n; int k = n; while (n > 0) { k = (n & -n) - 1; n -= n & -n; } cout << k << '\n'; } return 0; } // vim: foldmethod=marker
800
CPP
#include <bits/stdc++.h> using namespace std; const int maxN = 101; struct TPerson { int val, last, s, ans; } _p[maxN]; typedef TPerson* PPerson; vector<PPerson> v; int s[maxN], n, k, m, a, g; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> m >> a; for (int i = 1; i <= a; ++i) cin >> g, ++_p[g].val, _p[g].last = i; v.resize(n); auto p = _p; for (auto& t : v) t = ++p; sort(v.begin(), v.end(), [](const PPerson a, const PPerson b) { return a->val > b->val || a->val == b->val && a->last < b->last; }); auto it = v.begin(); (*it)->s = (*it)->val; while (++it != v.end()) (*it)->s = (*(it - 1))->s + (*it)->val; PPerson q = new TPerson; for (auto it = v.begin(); it != v.end(); ++it) { q->val = (*it)->val + m - a; if (q->val == 0) { (*it)->ans = 3; continue; } auto pos = upper_bound( v.begin(), it, q, [](const PPerson a, const PPerson b) { return a->val > b->val; }); if (pos - v.begin() >= k) { (*it)->ans = 3; continue; } int low = it - v.begin() + 1, high = v.size() - 1, mid; while (low <= high) { mid = low + high >> 1; if (((*it)->val + 1) * int(v.begin() + mid - it) - v[mid]->s + (*it)->s <= m - a) low = mid + 1; else high = mid - 1; } if (low <= k && (*it)->val) (*it)->ans = 1; else (*it)->ans = 2; } for (int i = 1; i <= n; ++i) cout << _p[i].ans << ' '; return 0; }
2,100
CPP
n, k = map(int, input().split()) red = 2*n; green = 5*n; blue = 8*n; need = 0 need += int(red/k) if (red % k == 0) else int(red/k + 1) need += int(green/k) if (green % k == 0) else int(green/k + 1) need += int(blue/k) if (blue % k == 0) else int(blue/k + 1) print(need)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, L; int a[55]; long long dp[2][55][55]; double res; double sk[55]; void preCalc() { sk[0] = 1.0 / n; sk[n - 1] = sk[0]; for (int i = 1; i < n - 1; i++) { sk[i] = sk[i - 1] * i / (n - i); } } int main() { scanf("%d", &n); int r = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); r += a[i]; } scanf("%d", &L); if (r <= L) { printf("%f\n", (double)n); return 0; } preCalc(); res = 0; for (int ms = 1; ms <= n; ms++) { int v = 0; memset(dp[v], 0, sizeof(dp[v])); dp[v][0][0] = 1; v ^= 1; for (int nn = 1; nn <= n; nn++) { memset(dp[v], 0, sizeof(dp[v])); for (int kk = 0; kk <= nn; kk++) { for (int LL = 0; LL <= L; LL++) { if (dp[1 ^ v][kk][LL]) { if (LL + a[nn] <= L && nn != ms) { dp[v][kk + 1][LL + a[nn]] += dp[1 ^ v][kk][LL]; } dp[v][kk][LL] += dp[1 ^ v][kk][LL]; } if (nn == n && LL >= L - a[ms] + 1) { res += sk[kk] * dp[v][kk][LL] * kk; } } } v ^= 1; } } printf("%f\n", res); return 0; }
1,900
CPP
import math n, m = map(int, input().strip().split()) a = list(map(int, input().strip().split())) a.sort() lastval = -1 b = list() for x in a: if x != lastval: b.append(1) lastval = x else: b[-1] += 1 k = len(b) while k > (1 << ((8*m)//n)): k -= 1 ans = 0 for x in range(k): ans += b[x] res = ans for x in range(k, len(b)): res = res + b[x] - b[x-k] ans = max(res,ans) print(n - ans)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 23; const int MOD = 1e9 + 7; const int N = 1e5 + 10; void mul(int &a, int b) { a = a * 1LL * b % MOD; } int main() { ios_base::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; if (n < k || k == 1) { int ans = 1; for (int i = (0); i < (n); ++i) mul(ans, m); cout << ans << "\n"; return 0; } if (n == k) { int ans = 1; for (int i = (0); i < ((n + 1) / 2); ++i) mul(ans, m); cout << ans << "\n"; return 0; } if (k & 1) { cout << m * m << "\n"; return 0; } cout << m << "\n"; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> void vout(T x) { cout << x << endl; exit(0); } const int inf = 1e9 + 1; const long long INF = 1e18 + 1e2; const int mod = 1e9 + 7; bool ok(int n) { return n % 10 != 3 && n % 3 != 0; } void solve() { int k; cin >> k; int res = -1; for (int candidate = 1;; candidate++) { if (ok(candidate)) k--; if (k == 0) { cout << candidate << endl; return; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int q = 1; cin >> q; while (q--) { solve(); } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { long long int n, x, i, p, a, b, flag = 0, ans = 0; cin >> n >> x; string s; cin >> s; long long int dp[n + 1]; dp[0] = 0; for (i = 0; i < n; i++) { if (s[i] == '0') { dp[i + 1] = dp[i] + 1; } else { dp[i + 1] = dp[i] - 1; } } p = dp[n]; if (p == 0) { for (i = 0; i < n; i++) { if (dp[i] == x) { flag = 1; i = n; } } if (flag == 1) { cout << -1 << endl; } else { cout << 0 << endl; } } else { for (i = 0; i < n; i++) { a = x - dp[i]; if (abs(a) % p == 0 && a * p >= 0) { ans++; } } cout << ans << endl; } } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0); int t, n, d; cin >> t; while (t--) { cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); long long md = -1e18; md = max(md, 1LL * v[0] * v[1] * v[2] * v[3] * v[4]); md = max(md, 1LL * v[0] * v[1] * v[2] * v[3] * v[n - 1]); md = max(md, 1LL * v[0] * v[1] * v[n - 1] * v[n - 2] * v[n - 3]); md = max(md, 1LL * v[0] * v[n - 1] * v[n - 2] * v[n - 3] * v[n - 4]); md = max(md, 1LL * v[n - 1] * v[n - 2] * v[n - 3] * v[n - 4] * v[n - 5]); cout << md << endl; } }
1,200
CPP
t=int(input()) for i in range(t): n,m=map(int,input().split()) ma=[] c=[0]*m r=[0]*n for j in range(n): te=input().split() ma.append(te) for j in range(n): for k in range(m): if ma[j][k]=='1': r[j]=-1 c[k]=-1 co=0 p=r.count(0) q=c.count(0) co=min(p,q) if co%2!=0: print('Ashish') else: print('Vivek')
1,100
PYTHON3
#Made by John Jerome Roa# n = int(input()) list = input().split(" ") list = [int(a) for a in list] list.sort() x = ""; for a in list: x += str(a) + " " print(x)
900
PYTHON3
import sys import math data = [ int(x) for x in sys.stdin.readline().strip().split(" ")] # print(data) n = data[0] m = data[1] a = data[2] result = math.ceil( n * 1.0 / a ) * math.ceil( m * 1.0 / a ) print(result)
1,000
PYTHON3
t = int(input()) for _ in range(t): d,v,l,r = map(int,input().split()) have = d // v exp = r//v - (l-1)//v print(have-exp)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; if (k == 0) { cout << 0 << " " << 0 << "\n"; return 0; } cout << min(n - k, 1) << " "; if (k <= (n - 1) / 2) cout << min(k * 2, n - k); else cout << n - k; }
1,200
CPP
n = (int)(input()) c=0 i=0 if (n-2)%3==0: b=0 while c==0: a = (n-2-6*b)/3 q = (3*b)*(3*b + 1)/2 if a>=q: b=b+1 else: break if (n-4)%3==0: b=0 while c==0: a = (n-4-6*b)/3 q = (3*b + 1)*(3*b + 2)/2 if a>=q: b=b+1 else: break if (n-6)%3==0: b=0 while c==0: a = (n-6-6*b)/3 q = (3*b + 2)*(3*b + 3)/2 if a>=q: b=b+1 else: break print(b)
1,700
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int,input().split())) arr.insert(0,0) to_be_used = 1 while to_be_used<n: minipos = to_be_used for i in range(to_be_used+1,n+1): if arr[i] < arr[minipos]: minipos = i if minipos == to_be_used: to_be_used+=1 else: start = minipos while start>to_be_used: temp = arr[start-1] arr[start-1] =arr[start] arr[start] = temp start-=1 to_be_used = minipos for i in range(1,n): print(arr[i],end=" ") print(arr[n])
1,400
PYTHON3
# your code goes here n=int(input()) l=sorted(map(int,input().split()),reverse=True) a=0 i=0 while a<=sum(l)/2: a+=l[i] i+=1 print(i)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; char s[1000], name[5][10] = {"Danil", "Olya", "Slava", "Ann", "Nikita"}; int main() { int i, j, k, cnt = 0; scanf("%s", s); for (i = 0; s[i]; ++i) { for (j = 0; j < 5; ++j) { for (k = 0; name[j][k] && s[i + k]; ++k) if (name[j][k] != s[i + k]) break; if (!name[j][k]) ++cnt; } } printf("%s\n", cnt == 1 ? "YES" : "NO"); return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; long long power(int x, int y) { if (y == 0) return 1; else if (y & 1) return x * power(x * x, y / 2); else return power(x * x, y / 2); } int main() { int n; cin >> n; vector<int> v(n); for (auto i = 0; i < n; i += 1) cin >> v[i]; sort(v.begin(), v.end()); int f = 0, l = 0; for (auto i = 1; i < n; i += 1) { if (v[i] - v[i - 1] == 1) l++; else if (v[i] != v[i - 1]) l = 0; if (l == 2) { f = 1; break; } } if (f) cout << "YES"; else cout << "NO"; return 0; }
900
CPP
n=int(input()) lst=list(map(float,input().strip().split(' '))) s=0 for j in lst: s+=j x=(s/(n*100)) print( "{:.12f}".format(x*100))
800
PYTHON3
import math n = int(input()) for _ in range(n): a, b, c, d, k = tuple(map(int, input().split())) pen_cnt = math.ceil(a/c) pencil_cnt = math.ceil(b/d) print('{} {}'.format(pen_cnt, pencil_cnt)) if k >= pen_cnt + pencil_cnt else print(-1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T, size_t N> int SIZE(const T (&t)[N]) { return N; } template <typename T> int SIZE(const T &t) { return t.size(); } string to_string(const string s, int x1 = 0, int x2 = 1e9) { return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c) { return string({c}); } template <size_t N> string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) { string t = ""; for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A(&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if (l_v_l_v_l == 0) res += '\n'; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += '\n'; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if (l_v_l_v_l == 0) res += '\n'; return res; } void dbgm() { ; } template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cout << to_string(H) << " | "; dbgm(T...); } const long long INFFLOW = 1e18; const long long INFCOST = 1e18; struct MCF { int n; vector<long long> prio, pot; vector<long long> curflow; vector<int> prevedge, prevnode; priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q; struct edge { int to, rev; long long f, cap; long long cost; }; vector<vector<edge>> g; MCF(int n) : n(n), prio(n), curflow(n), prevedge(n), prevnode(n), pot(n), g(n) {} void add_edge(int s, int t, long long cap, long long cost) { g[s].push_back((edge){t, ((int)g[t].size()), 0, cap, cost}); g[t].push_back((edge){s, ((int)g[s].size()) - 1, 0, 0, -cost}); } pair<long long, long long> get_flow(int s, int t) { long long flow = 0; long long flowcost = 0; while (1) { q.push({0, s}); fill(prio.begin(), prio.end(), INFCOST); prio[s] = 0; curflow[s] = INFFLOW; while (!q.empty()) { auto cur = q.top(); long long d = cur.first; int u = cur.second; q.pop(); if (d != prio[u]) continue; for (int i = 0; i < ((int)g[u].size()); ++i) { edge &e = g[u][i]; int v = e.to; if (e.cap <= e.f) continue; long long nprio = prio[u] + e.cost + pot[u] - pot[v]; if (prio[v] > nprio) { prio[v] = nprio; q.push({nprio, v}); prevnode[v] = u; prevedge[v] = i; curflow[v] = min(curflow[u], e.cap - e.f); } } } if (prio[t] == INFCOST) break; for (int i = 0; i < n; i++) pot[i] += prio[i]; long long df = min(curflow[t], INFFLOW - flow); flow += df; for (int v = t; v != s; v = prevnode[v]) { edge &e = g[prevnode[v]][prevedge[v]]; e.f += df; g[v][e.rev].f -= df; flowcost += df * e.cost; } } return {flow, flowcost}; } }; int main() { int n, m; cin >> n >> m; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int N = 2 * n; MCF mcf(N + 3); mcf.add_edge(N, N + 1, m, 0); mcf.add_edge(N + 1, N + 2, m, 0); for (int i = 0; i < n; i++) { int me = 2 * i; int other = me + 1; mcf.add_edge(N + 1, me, 1, __builtin_popcount(a[i])); mcf.add_edge(me, other, 1, -1e9); mcf.add_edge(other, N + 2, 1, 0); for (int j = i + 1; j < n; j++) { mcf.add_edge(other, 2 * j, 1, a[i] == a[j] ? 0 : __builtin_popcount(a[j])); } } mcf.get_flow(N, N + 2); long long res = 0; int x = 0; bool done[n]; memset(done, 0, sizeof(done)); int printedBy[n]; memset(printedBy, -1, sizeof(printedBy)); for (auto &e : mcf.g[N + 1]) if (e.cap && e.f) { done[e.to / 2] = 1; printedBy[e.to / 2] = x++; res += e.cost; } for (int i = 0; i < n; i++) if (!done[i]) { for (auto &e : mcf.g[2 * i]) if (!e.cap && e.f) { int parent = e.to / 2; assert(printedBy[parent] != -1); printedBy[i] = printedBy[parent]; res -= e.cost; } } int currentVal[m]; memset(currentVal, -1, sizeof(currentVal)); vector<string> program; for (int i = 0; i < n; i++) { string x = string(1, printedBy[i] + 'a'); if (currentVal[printedBy[i]] != a[i]) { program.push_back(x + "=" + to_string(a[i])); } program.push_back("print(" + x + ")"); currentVal[printedBy[i]] = a[i]; } cout << ((int)program.size()) << " " << res << '\n'; for (auto x : program) cout << x << '\n'; return 0; }
2,700
CPP
def gcd(a,b): if b==0: return a else: return gcd(b,a%b) n, m = map(int, input().split(' ')) x = list(map(int, input().split(' '))) p = list(map(int, input().split(' '))) dx = [x[i+1]-x[i] for i in range(n-1)] d = dx[0] for i in dx: d = gcd(i, d) for i in range(m): if d % p[i] == 0: print('YES') print(x[0], i+1) exit() print('NO')
1,300
PYTHON3
from datetime import datetime import math class Read: @staticmethod def string(): return input() @staticmethod def int(): return int(input()) @staticmethod def list(sep=' '): return input().split(sep) @staticmethod def list_int(sep=' '): return list(map(int, input().split(sep))) def solve(): n, x, a, b = Read.list_int() print(int(min(n - 1, math.fabs(a - b) + x))) query_count = 1 query_count = Read.int() while query_count: query_count -= 1 solve()
800
PYTHON3
n = int(input()) s = input() a = 0 for i in range(1,n): if s[i] == s[i-1]: a += 1 print(a)
800
PYTHON3
n = int(input()) for a in range(n): values = list(map(int,input().split())) maximum = values[0] total = 0 for a in range(3): if values[a] > maximum: maximum = values[a] total += values[a] total += values[3] if total%3 == 0 and total/3 >= maximum: print("YES") else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> g[3048]; int n; int visit[3048] = {0}, back[3048]; int ans[3028]; int yes = 0; void dfs1(int node, int prev) { int i, tmp; visit[node] = 1; for ((i) = 0; (i) < (g[node].size()); (i)++) if (g[node][i] != prev) { tmp = g[node][i]; back[tmp] = node; if (!visit[tmp]) { dfs1(tmp, node); if (yes == 2) return; } else { visit[tmp] = 2; yes = 2; return; } } } void dfs(int node, int k) { visit[node] = true; if (ans[node] != 2) ans[node] = k + 1; int i, tmp; for ((i) = 0; (i) < (g[node].size()); (i)++) { tmp = g[node][i]; if (!visit[tmp]) { if (ans[node] != 2) dfs(tmp, k + 1); else dfs(tmp, k); } } } int main() { int i, x, y; cin >> n; for ((i) = 0; (i) < (n); (i)++) { cin >> x >> y; x--; y--; g[x].push_back(y); g[y].push_back(x); } memset(back, -1, sizeof back); dfs1(0, -1); int k; for ((i) = 0; (i) < (n); (i)++) if (visit[i] == 2) { k = i; break; } do { visit[i] = 2; i = back[i]; } while (i != k); for ((i) = 0; (i) < (n); (i)++) if (visit[i] == 2) ans[i] = 2; memset(visit, 0, sizeof visit); for ((i) = 0; (i) < (n); (i)++) if (ans[i] == 2) { dfs(i, 2); break; } for ((i) = 0; (i) < (n); (i)++) if (!i) cout << ans[i] - 2; else cout << " " << ans[i] - 2; cout << endl; return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int w, h; cin >> w >> h; char t[105][105]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { scanf(" %c", &t[i][j]); } } for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { cout << t[j][i]; cout << t[j][i]; } cout << endl; for (int j = 0; j < h; j++) { cout << t[j][i]; cout << t[j][i]; } cout << endl; } return 0; }
1,200
CPP
# https://codeforces.com/contest/1352/problem/0 def main(): t = int(input()) arr = [] for i in range(t): temp = input() arr.append(temp) for j in range(t): roundnumbers(arr[j]) def roundnumbers(number): revnumber = number[::-1] arr = [] for i in range(len(number)): if revnumber[i] != "0": temp = revnumber[i] + "0" * i arr.append(temp) print(len(arr)) for i in arr: print(i, end=" ") print() if __name__ == "__main__": main()
800
PYTHON3
t = int(input()) for i in range(t): a, b, c = [int(x) for x in input().split()] y = min(c // 2, b) x = min((b - y) // 2, a) print((x + y)*3)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const double eps = 1e-15; const int maxn = 2000 + 10; struct Line { int a, b, c; } e[maxn]; inline int dcmp(double x) { if (fabs(x) < eps) return 0; return x < 1 ? -1 : 1; } struct Point { double x, y; Point() {} Point(double x, double y) : x(x), y(y) {} } inter; int n, top; double st[maxn]; long long ans; 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 * 10 + ch - '0'; ch = getchar(); } return x * f; } inline double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; } inline double Length(Point A) { return sqrt(Dot(A, A)); } inline double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; } inline double Angle(Point A, Point B) { double cos = Dot(A, B) / Length(A) / Length(B); double sin = Cross(A, B) / Length(A) / Length(B); return atan2(sin, cos); } inline Point Get_Inter(int l, int r) { return Point(((double)e[l].b * e[r].c - e[l].c * e[r].b) / ((double)e[l].a * e[r].b - e[l].b * e[r].a), ((double)e[l].a * e[r].c - e[l].c * e[r].a) / ((double)e[l].b * e[r].a - e[l].a * e[r].b)); } inline bool On_left(Point A, Point B) { if (dcmp(A.x * B.y - A.y * B.x) <= 0) return 0; return 1; } int main() { if (fopen("D.in", "r") != NULL) { freopen("D.in", "r", stdin); freopen("D.out", "w", stdout); } n = read(); long long res = 0; for (int i = 1; i <= n; i++) { e[i].a = read(), e[i].b = read(); e[i].c = read(), e[i].c *= -1; if (e[i].c == 0) res++, i--, n--; } if (res) res = (res * (res - 1) >> 1) * n; for (int i = 1; i <= n; i++) { top = 0; Point v = Point(-e[i].b, e[i].a); for (int j = 1; j <= n; j++) { if (i == j) continue; inter = Get_Inter(i, j); if (On_left(v, Point(-e[j].b, e[j].a)) ^ On_left(v, inter)) st[++top] = Angle(Point(e[j].b, -e[j].a), inter); else st[++top] = Angle(Point(-e[j].b, e[j].a), inter); } sort(st + 1, st + top + 1); st[top + 1] = 100; for (int j = 2, cnt = 1; j <= top + 1; j++) if (dcmp(st[j] - st[j - 1]) != 0) { ans += (long long)cnt * (cnt - 1) >> 1; cnt = 1; } else cnt++; } printf("%I64d\n", (ans >> 1) + res); return 0; }
2,900
CPP
a, b, c = map(int, input().split()) h = 0 if c % a == 0: k = c // a else: k = c // a + 1 if c % b == 0: m = c // b else: m = c // b + 1 if c - a*k < 0 and c - b*m < 0 and ((c < 2 * a) and c < 2*b): print('No') else: for i in range(k+1): if (c - a*i) % b == 0 and (h == 0): print('Yes') h = 1 if h == 0: print('No')
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = double; using pll = pair<ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; using vvll = vector<vll>; int main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n; cin >> n; vector<string> a; for (ll i = 0; i < (ll)n; ++i) { string b; cin >> b; a.push_back(b); } sort(a.begin(), a.end(), [](const string& l, const string& r) -> bool { return l.size() < r.size(); }); for (int i = 1; i < n; ++i) { if (a[i].find(a[i - 1]) == string::npos) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; for (ll i = 0; i < (ll)n; ++i) { cout << a[i] << endl; } return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(); int n, a[101], dem = 0, i = 1; cin >> n; a[1] = 1; while (n >= a[i]) { n -= a[i]; dem++; i++; a[i] = i; } a[i - 1] = a[i - 1] + n; cout << dem << endl; for (int i = (1); i <= (dem); i++) cout << a[i] << " "; return 0; }
1,000
CPP
n=int(input()) a = list(map(int,input().split())) a_1=a[0:n] a_2=a[n:2*n] if(sum(a_1)!=sum(a_2)): for item in a: print (item , end=" ") else: if(max(a_1)==min(a_1) and max(a_2)==min(a_2)): print("-1") else: flag=0 for i in range(0,len(a_1)): for j in range(0,len(a_2)): if(a_1[i]!=a_2[j]): temp=a_1[i] a_1[i]=a_2[j] a_2[j]=temp flag=1 break if(flag==1): break for item in a_1: print (item,end=" ") for item in a_2: print (item,end=" ")
1,000
PYTHON3
entrada = [int(x) for x in input().split()] res = [] for i in range(entrada[0]): linha = input() linhaR = '' for j in range(entrada[1]): if linha[j] == '.': if((i+j)%2 == 0): linhaR+='B' else: linhaR+='W' else: linhaR+='-' res.append(linhaR) for i in res: print(i)
1,200
PYTHON3
#include <bits/stdc++.h> #define TESTING false using namespace std; typedef long long ll; typedef pair <ll, ll> pll; vector <char> adding(string a, string b, char val) { vector <char> res; ll i = 0, j = 0; while(i < a.size() && j < b.size()) { if(a[i] == b[j]) { res.push_back(a[i]); i++; j++; continue; } if(a[i] != val) { res.push_back(b[j++]); } else { res.push_back(a[i++]); } } while(i < a.size()) { res.push_back(a[i++]); } while(j < b.size()) { res.push_back(b[j++]); } return res; } string uni(string a, string b, ll temp) { vector <char> res = adding(a, b, temp > 0 ? '1' : '0'); string res2 = string(res.size(), ' '); for(ll i = 0; i < res.size(); i++) { res2[i] = res[i]; } return res2; } ll common(string a, string b, ll n) { ll cnt = 0, cnt2 = 0; for(auto i: a) { if(i == '1') cnt++; } for(auto i: b) { if(i == '1') cnt2++; } bool res0 = (2 * n - min(cnt, cnt2) <= n); bool res1 = (2 * n - min(2 * n - cnt, 2 * n - cnt2) <= n); if(res0) return -1; if(res1) return 1; return 0; } int main() { //ifstream cin("input.txt"); ios::sync_with_stdio(0); ll t; cin >> t; while(t--) { ll n; cin >> n; string s[3]; for(ll i = 0; i < 3; i++) { cin >> s[i]; } string res; for(ll i = 0; i < 3; i++) { for(ll j = 0; j < 3; j++) { if(i == j) continue; ll temp = common(s[i], s[j], n); if(temp != 0) { res = uni(s[i], s[j], temp); assert(res.size() <= 3 * n); } } } cout << res << endl; } return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } vector<long long> v[200001]; long long vis[200001], color[200001]; vector<pair<long long, long long>> edge; int main() { long long n, m; cin >> n >> m; while (m--) { long long a, b; cin >> a >> b; edge.push_back(make_pair(a, b)); v[a].push_back(b); v[b].push_back(a); } queue<long long> q; q.push(1); color[1] = 1; vis[1] = 1; while (!q.empty()) { long long a = q.front(); q.pop(); for (auto i = v[a].begin(); i != v[a].end(); i++) { if (!vis[*i]) { if (color[a] == 1) color[*i] = 2; else color[*i] = 1; q.push(*i); vis[*i] = 1; } else if (color[a] == color[*i]) { cout << "NO"; return 0; } } } cout << "YES\n"; for (auto i = edge.begin(); i != edge.end(); i++) { if (color[(i->first)] == 1) cout << "0"; else cout << "1"; } }
1,700
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") using namespace std; long long MOD = 998244353; double eps = 1e-12; int main() { int n, t; cin >> n >> t; vector<int> times(n + 1); for (int i = 0; i < n; i++) { cin >> times[i]; } int i = 0, j = 0; long long sum = 0; int maxi = 0; while (i < n) { while (sum + times[j] <= t && j < n) { sum += times[j]; j++; } maxi = max(maxi, j - i); sum -= times[i]; i++; } cout << maxi << "\n"; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; long long a[3000][3000], fl[3000][3000], fr[3000][3000], maxx = 0, maxi, maxj, minn = 0, mini, minj; int n; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%I64d", &a[i][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) fl[i][j] = fl[i - 1][j - 1] + a[i][j]; for (int i = n - 1; i >= 1; i--) for (int j = n - 1; j >= 1; j--) fl[i][j] = fl[i + 1][j + 1]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) fr[i][j] = fr[i - 1][j + 1] + a[i][j]; for (int i = n - 1; i >= 1; i--) for (int j = n; j >= 2; j--) fr[i][j] = fr[i + 1][j - 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if ((i + j) % 2 == 0) { if (maxx <= fl[i][j] + fr[i][j] - a[i][j]) { maxx = fl[i][j] + fr[i][j] - a[i][j]; maxi = i; maxj = j; } } else { if (minn <= fl[i][j] + fr[i][j] - a[i][j]) { minn = fl[i][j] + fr[i][j] - a[i][j]; mini = i; minj = j; } } } } cout << maxx + minn << endl; printf("%I64d %I64d %I64d %I64d\n", maxi, maxj, mini, minj); return 0; }
1,900
CPP
s=input() count=s.count("a") if count>(len(s)/2): print(len(s)) else: print(2*count-1)
800
PYTHON3
def main(): l = [input() for _ in range(int(input()))] for i, s in enumerate(l): if s.startswith("OO"): l[i] = "++" + s[2:] elif s.endswith("OO"): l[i] = s[:3] + "++" else: continue print("YES") print("\n".join(l)) return print("NO") if __name__ == '__main__': main()
800
PYTHON3
n = int(input()) for i in range(n): x = int(input()) a, b, c = 0, 0, 0 while x % 2 == 0: x //= 2 a += 1 while x % 3 == 0: x //= 3 b += 1 while x % 5 == 0: x //= 5 c += 1 if x != 1: print(-1) else: print(a + 2 * b + 3 * c)
800
PYTHON3
y,k,n=map(int,input().split()) if k>n:print(-1) else: m=y%k m=(k-m) ct=0 for i in range(m,n+1,k): if (i+y)>n:break ct+=1 print(i,end=" ") if ct==0: print(-1)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int hoge, foo, bar, t, m, n; int mapp[505][505]; int fla[505][505]; int main() { memset(fla, 0, sizeof(fla)); int x, y, x1, y1; scanf("%d%d%d%d", &x, &y, &x1, &y1); char s[100006]; scanf("%s", s); int num = 0; n = strlen(s); for (int i = 0; i < n; i++) { if (fla[x1][y1] == 0) { printf("1 "); fla[x1][y1] = 1; num++; } else { printf("0 "); } if (s[i] == 'U') { if (x1 - 1 > 0) { x1 -= 1; } } else if (s[i] == 'D') { if (x1 + 1 <= x) { x1 += 1; } } else if (s[i] == 'R') { if (y1 + 1 <= y) { y1 += 1; } } else if (s[i] == 'L') { if (y1 - 1 > 0) { y1 -= 1; } } } printf("%d\n", x * y - num); return 0; }
1,600
CPP
t = int(input()) for _ in range(t): s = input() k = 0 t = 0 t1 = 0 for i in range(len(s)): if s[i] == '1': t = i break for i in range(len(s)-1,-1,-1): if s[i] == '1': t1 = i break while t < t1 and t < len(s): if s[t] == '0': k += 1 t += 1 print(k)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; struct point { int x, y; point(int X = 0, int Y = 0) { x = X, y = Y; } }; struct cmpx { bool operator()(const point &a, const point &b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } }; struct cmpy { bool operator()(const point &a, const point &b) { return a.y == b.y ? a.x < b.x : a.y < b.y; } }; inline bool cmp1(const point &a, const point &b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } inline bool cmp2(const point &a, const point &b) { return a.y == b.y ? a.x < b.x : a.y < b.y; } set<point, cmpx> X; set<point, cmpy> Y; int n, x, y; inline int solve(set<point, cmpx> &sx, set<point, cmpy> &sy) { if (sx.size() <= 1) return 1; set<point, cmpx> tx; set<point, cmpy> ty; int pos; set<point, cmpx>::iterator itx1(sx.begin()); set<point, cmpx>::reverse_iterator itx2(sx.rbegin()); set<point, cmpy>::iterator ity1(sy.begin()); set<point, cmpy>::reverse_iterator ity2(sy.rbegin()); while (cmp1(*itx1, *itx2) && cmp2(*ity1, *ity2)) { pos = itx1->x, itx1++; if ((itx1->x) - pos > 1) { while ((sx.begin()->x) <= pos) { point planet = *sx.begin(); tx.insert(planet), ty.insert(planet); sx.erase(planet), sy.erase(planet); } return solve(sx, sy) + solve(tx, ty); } pos = itx2->x, itx2++; if (pos - (itx2->x) > 1) { while ((sx.rbegin()->x) >= pos) { point planet = *sx.rbegin(); tx.insert(planet), ty.insert(planet); sx.erase(planet), sy.erase(planet); } return solve(sx, sy) + solve(tx, ty); } pos = ity1->y, ity1++; if ((ity1->y) - pos > 1) { while ((sy.begin()->y) <= pos) { point planet = *sy.begin(); tx.insert(planet), ty.insert(planet); sx.erase(planet), sy.erase(planet); } return solve(sx, sy) + solve(tx, ty); } pos = ity2->y, ity2++; if (pos - (ity2->y) > 1) { while ((sy.rbegin()->y) >= pos) { point planet = *sy.rbegin(); tx.insert(planet), ty.insert(planet); sx.erase(planet), sy.erase(planet); } return solve(sx, sy) + solve(tx, ty); } } return 1; } inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return x * f; } int main() { n = read(); for (int i = 1; i <= n; ++i) { x = read(), y = read(); X.insert(point(x, y)); Y.insert(point(x, y)); } printf("%d\n", solve(X, Y)); }
2,900
CPP
n=int(input()) l=[] l1=[0 for i in range(1000001) ] L=[] L1=[0 for i in range(1000001) ] k=0 for i in range(n) : a,b=map(int,input().split()) if a>0 : l.append(a) l1[a]=b else : L.append(abs(a)) L1[abs(a)]=b l=sorted(l) L=sorted(L) p=0 for i in range(min(len(l),len(L))) : k=k+l1[l[i]] k=k+L1[abs(L[i])] p=p+1 if len(l)>len(L) : k=k+l1[l[p]] if len(l)<len(L) : k=k+L1[abs(L[p])] print(k)
1,100
PYTHON3
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 # 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, 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)] for _ in range(int(data())): n, m = sp() row, col = set(), set() mat = [] for i in range(n): mat.append(l()) for j in range(m): if mat[i][j]: row.add(i) col.add(j) par = 1 while True: temp1, temp2 = -1, -1 for i in range(n): if i not in row: temp1 = i break for i in range(m): if i not in col: temp2 = i break if temp1 == -1 or temp2 == -1: if par & 1: out("Vivek") else: out("Ashish") break row.add(temp1) col.add(temp2) par = 1 - par
1,100
PYTHON3
k=int(input()) l=int(input()) m=int(input()) n=int(input()) d=int(input()) li=[] i=1 while(i*k<=d): li.append(i*k) i+=1 j=1 while(j*l<=d): li.append(j*l) j+=1 a=1 while(a*m<=d): li.append(a*m) a+=1 b=1 while(b*n<=d): li.append(b*n) b+=1 print(len(set(li)))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long T; cin >> T; while (T--) { long long s, a, b, c; cin >> s >> a >> b >> c; long long sum = s / c; long long sum1 = sum + (sum / a) * b; cout << sum1 << endl; } }
800
CPP
from math import log def play_2048(s): if 2048 in s: return "YES" x = 2048 b = True while s and b: b = False for i, v in enumerate(s): if v in s[i+1:]: b = True x -= v*2 s.pop(s.index(v)) s[s.index(v)] = v*2 if 2048 in s: return "YES" return "NO" q = int(input()) for _ in range(q): n = int(input()) s = sorted(filter(lambda i: i <= 2048, map(int, input().strip().split())), reverse=True) print(play_2048(s))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; char s[710]; const long long mod = 1e9 + 7; long long dp[710][710][3][3]; stack<int> st; int mp[750]; void dfs(int i, int j) { if (i + 1 == j) { for (int k = 1; k <= 2; ++k) dp[i][j][0][k] = dp[i][j][k][0] = 1; return; } if (mp[i] == j) { dfs(i + 1, j - 1); for (int l = 0; l < 3; ++l) for (int r = 0; r < 3; ++r) { if (l != 1) dp[i][j][1][0] = (dp[i][j][1][0] + dp[i + 1][j - 1][l][r]) % mod; if (r != 1) dp[i][j][0][1] = (dp[i][j][0][1] + dp[i + 1][j - 1][l][r]) % mod; if (l != 2) dp[i][j][2][0] = (dp[i][j][2][0] + dp[i + 1][j - 1][l][r]) % mod; if (r != 2) dp[i][j][0][2] = (dp[i][j][0][2] + dp[i + 1][j - 1][l][r]) % mod; } } else { int m = mp[i]; dfs(i, m); dfs(m + 1, j); for (int l = 0; l < 3; ++l) for (int r = 0; r < 3; ++r) for (int a = 0; a < 3; ++a) for (int b = 0; b < 3; ++b) { if (a == b && a != 0) continue; dp[i][j][l][r] = (dp[i][j][l][r] + dp[i][m][l][a] * dp[m + 1][j][b][r]); dp[i][j][l][r] %= mod; } } } int main() { scanf("%s", s + 1); int k = strlen(s + 1); for (int i = 1; i <= k; ++i) { if (s[i] == '(') st.push(i); else { mp[st.top()] = i; mp[i] = mp[st.top()]; st.pop(); } } dfs(1, k); long long ans = 0; for (int l = 0; l < 3; ++l) for (int r = 0; r < 3; ++r) ans = (ans + dp[1][k][l][r]) % mod; printf("%lld\n", ans); return 0; }
1,900
CPP
o,d = map(int,input().split()) sol = [] flag = 0 count = 0 for i in range(d): s = input() if "0" in s: #print("yes") count +=1 else: #print("no") flag = 1 sol.append(count) count = 0 if flag == 0: print(count) else: sol.append(count) print(max(sol))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int fx[] = {1, -1, 0, 0}; const int fy[] = {0, 0, 1, -1}; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } long long pwr(long long n, long long p) { long long r = 1; for (; p >= 1; p--) r = r * n; return r; } bool isprime(long long n) { long long i; if (n == 1) return false; else if (n == 2) return true; for (i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } void SieveOE(int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p <= n; p++) if (prime[p]) cout << p << " "; } void dtb(int n) { if (n <= 1) { printf("%d", n); } else { dtb(n / 2); printf("%d", n % 2); } } int ibtd(int n) { int num = n, dec_value = 0, base = 1; int temp = num; while (temp) { int last_digit = temp % 10; temp = temp / 10; dec_value += last_digit * base; base = base * 2; } return dec_value; } void printDivisors(int n) { for (int i = 1; i <= n; i++) { if (n % i == 0) { cout << i << " "; } } cout << endl; } int smallestDivisor(long long n) { if (n % 2 == 0) return 2; for (long long i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } bool cmpf(pair<int, int> a, pair<int, int> b) { return (a.first < b.first) || ((a.first == b.first) && (a.second > b.second)); } bool cmpss(string i, string j) { return i < j; } bool cmps(pair<char, int> a, pair<char, int> b) { if (a.second < b.second) return 1; if (a.second > b.second) return 0; return (a.first > b.first); } void smulti(string a, string b) { long long i, j; vector<int> v(a.size() + b.size()); for (i = a.size() - 1; i >= 0; i--) { for (j = b.size() - 1; j >= 0; j--) { v[i + j + 1] += (b[j] - '0') * (a[i] - '0'); } } for (i = v.size() - 1; i >= 0; i--) { if (v[i] >= 10) { v[i - 1] += v[i] / 10; v[i] %= 10; } } i = 0; if (v[0] == 0) i = 1; for (; i < v.size(); i++) { cout << v[i]; } cout << endl; } void bitmask(string s) { long long mask, i, n; n = s.size(); for (mask = 1; mask < (i << n); mask++) { for (i = 0; i < n; i++) { if (mask and (1 << i)) cout << s[i]; } cout << endl; } } long long nof(long long n) { long long cn = 0; while (n) { cn += n / 5; n /= 5; } return cn; } bool chksub(string s1, string s2) { long long a = s1.size(), b = s2.size(), f; for (auto i = 0; i <= a - b; i++) { for (auto j = i; j < i + b; j++) { f = 1; if (s1[j] != s2[j - i]) { f = 0; break; } } if (f == 1) break; } return f; } int main() { ios::sync_with_stdio(false); cin.tie(0); ; string s; long long i, cn = 0, n; cin >> n >> s; for (i = 0; i + 2 < n; i++) { if (s[i] == 'x' and s[i + 1] == 'x' and s[i + 2] == 'x') cn++; } cout << cn << endl; }
800
CPP
n = int(input()) acc = 0 for _ in range(n): s = input() if s == "Tetrahedron": acc += 4 elif s == "Cube": acc += 6 elif s == "Octahedron": acc += 8 elif s == "Dodecahedron": acc += 12 elif s == "Icosahedron": acc += 20 print(acc)
800
PYTHON3
q = int(input()) for i in range(q): a,b,c = [int(x) for x in input().split()] print(a+b+c>>1)
800
PYTHON3
def mymain(): na = int(n / a) ma = int(m / a) if n % a != 0: na += 1 if m % a != 0: ma += 1 return (na * ma) n, m, a = map(int, input().split()) res = mymain() print (res)
1,000
PYTHON3
n, k = map(int, input().split()) l = list(map(int, input().split())) if n < k: print("NO") ll = [] out = [] for i in range(k): ll.append([l[i]]) out.append(i + 1) for i in range(k, n): fl = 1 for j in range(k): if l[i] not in ll[j]: ll[j].append(l[i]) out.append(j + 1) fl = 0 break if fl: print("NO") exit(0) print("YES") for i in out: print(i, end = ' ')
1,400
PYTHON3
import math import operator as op import functools as fun def ncr(n, r): r = min(r, n-r) if r == 0: return 1 numer = fun.reduce(op.mul, range(n, n-r, -1)) denom = fun.reduce(op.mul, range(1, r+1)) return numer//denom n=int(input()) m=0 m=ncr(n,5)+ncr(n,6)+ncr(n,7) print(m)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; struct Query { int p, s, id; bool operator<(const Query &b) const { return (s < b.s); } }; int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); int m; scanf("%d", &m); vector<Query> q(m); for (int i = 0; i < m; ++i) { scanf("%d%d", &q[i].p, &q[i].s); --q[i].p; q[i].id = i; } sort(q.begin(), q.end()); vector<long long> ans(m, 0); int lim = sqrt(n); for (int i = 0; i < q.size(); ++i) { if (q[i].s > lim) { for (int j = q[i].p; j < n; j += q[i].s) ans[q[i].id] += a[j]; } else { int cs = q[i].s; vector<long long> sums(n, 0); for (int j = n - 1; j >= 0; --j) sums[j] = a[j] + ((j + cs < n) ? sums[j + cs] : 0); int j; for (j = i; j < q.size() && q[j].s == cs; ++j) ans[q[j].id] = sums[q[j].p]; i = j - 1; } } for (int i = 0; i < ans.size(); ++i) printf("%I64d\n", ans[i]); return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000009; long long q[55]; vector<int> v; void dfs(int n, long long k, int g) { if (n == 0) return; if (k > q[n - 1]) { v.push_back(g + 1); v.push_back(g); dfs(n - 2, k - q[n - 1], g + 2); } else { v.push_back(g); dfs(n - 1, k, g + 1); } } int main() { int n; long long k; q[0] = 1; q[1] = 1; for (int j = 2; j < 55; j++) q[j] = q[j - 1] + q[j - 2]; cin >> n >> k; dfs(n, k, 1); for (int j = 0; j < v.size(); j++) { if (j) cout << " "; cout << v[j]; } cout << endl; return 0; }
1,900
CPP
n = input() tmp = input().split() tmp = list(map(int,tmp)) tmp.sort() tmp = list(map(str,tmp)) answer = ' '.join(tmp) print(answer)
900
PYTHON3
import math,sys,bisect,heapq from collections import defaultdict,Counter,deque from itertools import groupby,accumulate #sys.setrecursionlimit(200000000) input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ ilele = lambda: map(int,input().split()) alele = lambda: list(map(int, input().split())) def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] #MOD = 1000000000 + 7 def Y(c): print(["NO","YES"][c]) def y(c): print(["no","yes"][c]) def Yy(c): print(["No","Yes"][c]) n,m = ilele() A = alele() if n == 1: print(1) else: d = deque() for i in range(n): d.append((A[i],i+1)) while d: if len(d) == 1: print(d[0][1]) break x,y = d.popleft() x -= m if x <= 0: continue else: d.append((x,y))
1,000
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) d = {} res = 0 for i in range(n): # ind = 0 # while arr[i] != 0: # arr[i] = arr[i] >> 1 # ind += 1 ind = len(bin(arr[i])) if ind in d: res += d[ind] d[ind] += 1 else: d[ind] = 1 print(res)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (a[0] == 1) cout << -1; else cout << 1; }
1,000
CPP
for i in range(int(input())): print((int(input()) +1) // 10)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n; vector<long long> a, b; struct Solution { void solve() { vector<bool> safe(n); for (int i = 0; i != n; ++i) { for (int j = i + 1; j != n; ++j) { if (a[i] == a[j]) { safe[i] = safe[j] = true; break; } } } for (int i = 0; i != n; ++i) { for (int j = 0; j != n; ++j) { if (!safe[j] || i == j) continue; if ((a[i] | a[j]) == a[j]) { safe[i] = true; } } } long long res = 0; for (int i = 0; i != n; ++i) { if (safe[i]) res += b[i]; } cout << res << endl; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int T = 1; while (T--) { cin >> n; a.resize(n); b.resize(n); for (int i = 0; i != n; ++i) cin >> a[i]; for (int i = 0; i != n; ++i) cin >> b[i]; Solution test; test.solve(); } }
1,700
CPP
from math import gcd n,m=map(int,input().split()) a=[] if m<(n-1): exit(print('Impossible')) for i in range(1,n+1): for j in range(i+1,n+1): if gcd(i,j)==1: a.append([i,j]) m-=1 if m==0: break if m==0: break if m>0: exit(print('Impossible')) print('Possible') for i in a: print(*i)
1,700
PYTHON3
#include <bits/stdc++.h> using std::make_pair; using std::pair; using std::sort; using std::swap; const int N = 1e3 + 10; int n, top, ans[N][2], cnt; bool incv[N]; struct point { int x, y, org; bool color; inline point(int a = 0, int b = 0) { x = a; y = b; } inline const bool operator==(const point &p) const { return color == p.color; } inline const friend double distance(const point &p1, const point &p2) { return sqrt(pow(1.0 * (p1.x - p2.x), 2.0) + pow(1.0 * (p1.y - p2.y), 2.0)); } } p[N], s[N], ss[N]; struct vector { int x, y; inline vector(const point &a, const point &b) { x = b.x - a.x; y = b.y - a.y; } inline const double length() { return distance(point(0, 0), point(x, y)); } inline const friend int cross_product(const vector &v1, const vector &v2) { return v1.x * v2.y - v1.y * v2.x; } inline const friend int dot_product(const vector &v1, const vector &v2) { return v1.x * v2.x + v1.y * v2.y; } inline const friend double getcos(vector v1, vector v2) { return dot_product(v1, v2) / v1.length() / v2.length(); } }; struct triangle { point v[3]; inline triangle(const point &a, const point &b, const point &c) { v[0] = a; v[1] = b; v[2] = c; } inline const bool difference() { int cnt[2]; cnt[0] = cnt[1] = 0; for (int i = 0; i < 3; i++) cnt[v[i].color]++; return cnt[0] > cnt[1]; } inline const pair<point, point> same() { bool c = difference(); int s[2], cnt = -1; for (int i = 0; i < 3; i++) if (v[i].color ^ c) s[++cnt] = i; return make_pair(v[s[0]], v[s[1]]); } inline const bool in(const point &p) { int cp1 = cross_product(vector(v[0], v[1]), vector(v[0], p)), cp2 = cross_product(vector(v[1], v[2]), vector(v[1], p)), cp3 = cross_product(vector(v[2], v[0]), vector(v[2], p)); return cp1 && cp2 && cp3 && (cp1 > 0) == (cp2 > 0) && (cp1 > 0) == (cp3 > 0); } }; int fa[N]; inline const void init() { for (int i = 1; i <= n; i++) fa[i] = i; } inline const int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } inline const void Union(int x, int y) { fa[find(x)] = find(y); } inline const bool operator<(const point &p1, const point &p2) { int cp = cross_product(vector(p[1], p1), vector(p[1], p2)); if (cp > 0) return true; if (!cp && distance(p[0], p1) < distance(p[0], p2)) return true; return false; } inline const void convex_hull() { for (int i = 1; i <= n; i++) if (p[i].y < p[1].y) swap(p[1], p[i]); sort(p + 2, p + n + 1); s[++top] = p[1]; incv[p[1].org] = 1; for (int i = 2; i <= n; i++) { while (top > 1 && cross_product(vector(s[top - 1], s[top]), vector(s[top], p[i])) <= 0) incv[s[top].org] = 0, top--; s[++top] = p[i]; incv[p[i].org] = 1; } } inline const void divide(triangle t) { bool c = t.difference(); static std::vector<point> same, diff; same.clear(); diff.clear(); for (int i = 1; i <= n; i++) if (t.in(p[i])) if (p[i].color ^ c) same.push_back(p[i]); else diff.push_back(p[i]); pair<point, point> P = t.same(); if (diff.empty()) { same.push_back(P.first); same.push_back(P.second); for (int i = 0; i < same.size() - 1; i++) for (int u, v, j = i + 1; j < same.size(); j++) if (find(u = same[i].org) ^ find(v = same[j].org)) ans[++cnt][0] = u - 1, ans[cnt][1] = v - 1, Union(u, v); return; } point nxt = diff[0]; for (int i = 0; i < 2; i++) divide(triangle(t.v[i], t.v[i + 1], nxt)); divide(triangle(t.v[2], t.v[0], nxt)); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].color), p[i].org = i; convex_hull(); init(); for (int i = 1; i <= top; i++) ss[i] = s[i]; int tot = std::unique(ss + 1, ss + top + 1) - ss - 1; if (tot > 3 || tot == 3 && ss[1].color ^ ss[tot].color) return puts("Impossible"), 0; int fst = 0, lst = 0; bool c = ss[1].color; if (tot > 2) { for (int i = 1; i <= top; i++) { if (s[i].color ^ c && !fst) fst = i; if (s[i].color ^ c) lst = i; } for (int i = fst; i < lst; i++) divide(triangle(s[i], s[i + 1], s[lst + 1])); for (int i = lst + 1; i < top; i++) divide(triangle(s[i], s[i + 1], s[fst])); for (int i = 1; i < fst - 1; i++) divide(triangle(s[i], s[i + 1], s[fst])); divide(triangle(s[top], s[1], s[fst])); } if (tot == 2) { int pos; for (int i = 1; i <= top; i++) if (s[i].color ^ c) { pos = i; break; } for (int i = pos; i < top; i++) divide(triangle(s[pos - 1], s[i], s[i + 1])); for (int i = 1; i < pos - 1; i++) divide(triangle(s[i], s[i + 1], s[top])); } if (tot == 1) { point one; bool found = 0; for (int i = 1; i <= n; i++) if (!incv[p[i].org] && p[i].color ^ c) { one = p[i]; found = 1; break; } if (found) { for (int i = 1; i < top; i++) divide(triangle(s[i], s[i + 1], one)); divide(triangle(s[top], s[1], one)); } else { for (int i = 1; i < n; i++) for (int j = i + 1; j <= n; j++) if (find(i) ^ find(j)) ans[++cnt][0] = i - 1, ans[cnt][1] = j - 1, Union(i, j); } } printf("%d\n", cnt); for (int i = 1; i <= cnt; i++) printf("%d %d\n", ans[i][0], ans[i][1]); return 0; }
3,200
CPP
#include <bits/stdc++.h> using namespace std; long long fac[200005]; int position[200005]; long long pen[200005]; void update(int ind, int add) { while (ind < 200005) { pen[ind] += add; ind += ind & (-ind); } } long long query(int ind) { long long ret = 0; while (ind > 0) { ret = ret + pen[ind]; ind = ind & (ind - 1); } return ret; } int cc[200005]; void updateC(int ind, int add) { while (ind < 200005) { cc[ind] += add; ind += ind & (-ind); } } void construct(int n, long long cur) { int l = min(100, n); for (int i = n - l + 1; i <= n; ++i) { update(position[i], -i); updateC(i, 1); } for (int i = n - l + 1; i <= n; ++i) { int co = n - i + 1; long long u = cur / fac[co - 1]; assert(u < co); cur -= fac[co - 1] * u; ++u; long long sum = 0; int ind = 0; for (int j = 20; j >= 0; --j) { int nind = ind + (1 << j); if (nind <= n && sum + cc[nind] < u) { ind = nind; sum += cc[nind]; } } ++ind; updateC(ind, -1); position[ind] = i; update(position[ind], ind); } assert(cur == 0); } void solve() { int n, q; scanf("%d %d ", &n, &q); fac[0] = 1; const long long inf = 1e13; for (int i = 1; i <= n; ++i) { fac[i] = min(inf, fac[i - 1] * i); position[i] = i; update(i, i); } long long cur = 0; while (q--) { int type; scanf("%d ", &type); if (type == 1) { int l, r; scanf("%d %d ", &l, &r); printf("%lld\n", query(r) - query(l - 1)); } else { int x; scanf("%d ", &x); cur += x; construct(n, cur); } } } int main() { solve(); return 0; }
2,400
CPP