solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
n = int(input()) for i in range(n): n, k = [int(x) for x in input().split()] s = n//k r = n%k if(r<=(k//2)): print(n) else: print(s*k+(k//2))
8
PYTHON3
n = int(input()) s = input() z = s.count("z") print("1 " * ((n - 4*z)//3) + "0 " * z)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, inv = 5e8 + 4; int sum(long long x, long long n, int r) { x %= mod; n %= mod; return n * (2 * x + r * (n - 1) % mod) % mod * inv % mod; } int solve(long long i) { long long x[2] = {0, 0}, p = 1; int k; for (k = 1; x[0] + x[1] < i; k ^= 1, p <<= 1) x[k] += p; x[k ^ 1] -= x[0] + x[1] - i; return (sum(2, x[0], 2) + sum(1, x[1], 2)) % mod; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long l, r; cin >> l >> r; int ans = solve(r) - solve(l - 1); cout << (ans + mod) % mod << '\n'; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, v; cin >> n >> v; if ((n - 1) <= v) { cout << n - 1 << endl; return 0; } int cost = v; for (int i = 2; i + v <= n; i++) { cost += i; } cout << cost << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { string s; cin >> s; int n = s.length(); if (s[n - 1] == 'o') cout << "FILIPINO" << endl; else if (s[n - 1] == 'u') cout << "JAPANESE" << endl; else cout << "KOREAN" << endl; } }
7
CPP
#include<bits/stdc++.h> using namespace std; int main() { long long int a,b,k,d=0; cin>>a>>b>>k; if(k>a) cout<<"0 "<<max(b-k+a,d)<<endl; else cout<<a-k<<" "<<b<<endl; return 0; }
0
CPP
def trin(n): return n*n l = [int(x) for x in input().split()] a = l[-1] b = l[1] c =trin(l[0]+ l[1]+l[2]) c = c- trin(l[0])-trin(l[2])-trin(l[4]) print(c)
9
PYTHON3
a = input() count = 0 for i in range(int(a)): s = input() if '1 1 0' in s: count += 1 elif '1 1 1' in s: count += 1 elif '1 0 1' in s: count += 1 elif '0 1 1' in s: count += 1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 10; inline long long read() { long long x = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x; } struct node { long long lower_power, k; } e[maxn]; inline bool cmp(node a, node b) { return a.lower_power < b.lower_power; } int main() { long long _ = read(); while (_--) { long long n = read(), ans = 0; for (long long i = 1; i <= n; i++) { ans = 0; long long k = read(); for (long long j = 1; j <= k; j++) { long long temp = read(); ans = max(ans, temp - j + 1); } e[i].lower_power = ans; e[i].k = k; } sort(e + 1, e + 1 + n, cmp); ans = e[1].lower_power + 1; long long now = ans; long long cnt = e[1].k; for (long long i = 2; i <= n; i++) { if (e[i].lower_power < now + e[i - 1].k) now += e[i - 1].k; else now = e[i].lower_power + 1, ans = e[i].lower_power + 1 - cnt; cnt += e[i].k; } printf("%lld\n", ans); } }
9
CPP
#include<bits/stdc++.h> using namespace std; #define ii pair <int, int> #define app push_back #define all(a) a.begin(), a.end() #define bp __builtin_popcountll #define ll long long #define mp make_pair #define f first #define s second #define Time (double)clock()/CLOCKS_PER_SEC const int N = 5007, MOD = 1000 * 1000 * 1000 + 7; int mod(int n) { n %= MOD; if (n < 0) return n + MOD; else return n; } int fp(int a, int p) { int ans = 1, c = a; for (int i = 0; (1ll << i) <= p; ++i) { if ((p >> i) & 1) ans = mod(ans * c); c = mod(c * c); } return ans; } int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); } void add(int &a, int b) { a = mod(a + b); } int dp[N][N][2], t1[N][N][2], t2[N][N][2]; signed main() { #ifdef HOME freopen("input.txt", "r", stdin); #else #define endl '\n' ios_base::sync_with_stdio(0); cin.tie(0); #endif int n, a, b; cin >> n >> a >> b; if (b < a) swap(a, b); for (int i = 1; i <= n; ++i) { for (int t = 0; t < 2; ++t) { int to = i; if (t == 0 && i < a) to = 0; dp[i][to][t] = 1; } } for (int i = 1; i <= n; ++i) { for (int j = 0; j < b; ++j) { for (int t = 0; t < 2; ++t) { add(t1[i][j][t], t1[i - 1][j][t]); add(dp[i][j][t], t1[i][j][t]); if (i && j) { add(t2[i][j][t], t2[i - 1][j - 1][t]); } add(dp[i][j][t], t2[i][j][t]); int x = dp[i][j][t]; if (t) { if (1 < a) { add(t1[i + 1][0][t^1], x); if (i + a <= n) { add(t1[i + a][0][t^1], -x); } } if (a < b - j) { if (i + a <= n && j + a <= n) { add(t2[i + a][j + a][t^1], x); } if (i + b - j <= n && j + b - j <= n) { add(t2[i + b - j][j + b - j][t^1], -x); } } } else { if (1 < b - j) { add(t2[i + 1][j + 1][t^1], x); if (i + b - j <= n && j + b - j <= n) { add(t2[i + b - j][j + b - j][t^1], -x); } } /* for (int k = 1; k < b - j; ++k) add(dp[i + k][j + k][t^1], x); */ } } } } int ans = 1; for (int i = 0; i < n; ++i) ans = (ans << 1) % MOD; for (int i = 0; i < b; ++i) for (int t = 0; t < 2; ++t) ans = mod(ans - dp[n][i][t]); cout << ans << endl; }
0
CPP
if __name__ == '__main__': a,b,c = map(int, input().split()) if a == b: print(c*2 + a + b) else: print(c*2 + min(a,b)*2 + 1)
7
PYTHON3
#include<cstdio> #include<cstring> int main(){ char s[201],p[101],s1[101]; scanf("%s %s",s,p); strcpy(s1,s); strcat(s,s1); if(strstr(s,p)==NULL) printf("No\n"); else printf("Yes\n"); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const long double pi = 3.141592653589793238462643; int n, m, k; int was[2000]; int p[2000]; int p2[2000]; set<int> cl[2000]; set<int> cl2[2000]; pair<int, int> g[2000]; map<string, int> ma1, ma2; int get(int x) { if (x == p[x]) return x; p[x] = get(p[x]); for (auto o : cl[x]) { cl[p[x]].insert(o); } cl[x].clear(); return p[x]; } void add(int x, int y) { x = get(x); y = get(y); if (x == y) return; p[x] = y; for (auto o : cl[x]) { cl[y].insert(o); } cl[x].clear(); } int get2(int x) { if (x == p2[x]) return x; p2[x] = get2(p2[x]); for (auto o : cl2[x]) { cl2[p2[x]].insert(o); } cl2[x].clear(); return p2[x]; } void add2(int x, int y) { x = get2(x); y = get2(y); if (x == y) return; p2[x] = y; for (auto o : cl2[x]) { cl2[y].insert(o); } cl2[x].clear(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 0; i < (m); i++) { int x, y; cin >> x >> y; x--; y--; g[i] = make_pair(x, y); } for (int i = 0; i < (k); i++) { string s; cin >> s; int y; cin >> y; y--; int t; cin >> t; ma1[s] = y; for (int j = 0; j < (t); j++) { int x; cin >> x; x--; cl[y].insert(x); } } for (int i = 0; i < n; i++) p[i] = i; for (int i = 0; i < (n); i++) { if (i != get(i)) continue; int cur = 0; while (cur < n + 5) for (int j = 0; j < m; j++) { int x = get(g[j].first); int y = get(g[j].second); if (cl[i].find(j) != cl[i].end()) { if (x == i && y != i) { add(y, i); cur = 0; } if (x != i && y == i) { add(x, i); cur = 0; } } cur++; } } for (int i = 0; i < (k); i++) { string s; cin >> s; int y; cin >> y; y--; int t; cin >> t; ma2[s] = y; for (int j = 0; j < (t); j++) { int x; cin >> x; x--; cl2[y].insert(x); } } for (int i = 0; i < n; i++) p2[i] = i; for (int i = 0; i < (n); i++) { if (i != get2(i)) continue; int cur = 0; while (cur < n + 5) for (int j = 0; j < m; j++) { int x = get2(g[j].first); int y = get2(g[j].second); if (cl2[i].find(j) != cl2[i].end()) { if (x == i && y != i) { add2(y, i); cur = 0; } if (x != i && y == i) { add2(x, i); cur = 0; } } cur++; } } for (int i = 0; i < (n); i++) { int x = get(i); int y = get2(i); if (cl[x] != cl2[y]) { cout << "NO"; return 0; } } for (auto o : ma1) { string x = o.first; if (get(ma1[x]) != get(ma2[x])) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
12
CPP
s, k = map(int, input().split()) strin = list(input()) newStr = strin d = 0 m = ord("m") a = ord("a") z = ord("z") i = 0 while i < s and d < k: diff = 0 c = ord(strin[i]) if c <= m: diff = z - c else: diff = a - c if (abs(diff) + d) <= k: d += abs(diff) newStr[i] = chr(c + diff) else: if c <= m: c += k - d else: c -= k - d d += abs(k - d) newStr[i] = chr(c) i += 1 if d == k: print("".join(newStr)) else: print("-1")
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int lim = 17 * 100001; int mod, v[100010], arb[400010], lazy[400010], fi, vaz[100010], put[10], lazy1[400010][9], prec[9][lim + 1], l1; char leaf[400010]; vector<int> primes; int rid_put(int a, int b) { int sol = 1; for (int i = 1; i <= b; i <<= 1) { if (b & i) sol = (1LL * sol * a) % mod; a = (1LL * a * a) % mod; } return sol; } int vl(int nod) { if (leaf[nod] == 0) return arb[nod]; int p = 1; for (int i = 0; i < l1; i++) p = (1LL * p * prec[i][lazy1[nod][i]]) % mod; return (1LL * arb[nod] * p) % mod; } void init(int nod, int st, int dr) { lazy[nod] = 1; if (st == dr) { int val1 = 1, x = v[st]; for (int j = 2; j * j <= x; j++) if (x % j == 0) { int p = 0, c = 1; while (x % j == 0) { x /= j; c *= j; p++; } if (mod % j != 0) val1 *= c; else lazy1[nod][vaz[j]] = p; } if (x > 1) { if (mod % x != 0) val1 *= x; else lazy1[nod][vaz[x]] = 1; } arb[nod] = val1; leaf[nod] = 1; return; } int mid = (st + dr) / 2; init(nod * 2, st, mid); init(nod * 2 + 1, mid + 1, dr); arb[nod] = (vl(nod * 2) + vl(nod * 2 + 1)) % mod; } void propag(int nod, int st, int dr) { arb[nod] = (1LL * arb[nod] * lazy[nod]) % mod; if (st < dr) { lazy[nod * 2] = (1LL * lazy[nod * 2] * lazy[nod]) % mod; lazy[nod * 2 + 1] = (1LL * lazy[nod * 2 + 1] * lazy[nod]) % mod; int p = 1; for (int i = 0; i < l1; i++) { lazy1[nod * 2][i] += lazy1[nod][i]; lazy1[nod * 2 + 1][i] += lazy1[nod][i]; p = (1LL * p * prec[i][lazy1[nod][i]]) % mod; lazy1[nod][i] = 0; } arb[nod] = (1LL * arb[nod] * p) % mod; } lazy[nod] = 1; } void update(int nod, int st, int dr, int left, int right, int x) { if (left <= st && dr <= right) { lazy[nod] = (1LL * lazy[nod] * x) % mod; for (int i = 0; i < 9; i++) lazy1[nod][i] += put[i]; propag(nod, st, dr); return; } propag(nod, st, dr); int mid = (st + dr) / 2; if (left <= mid) update(nod * 2, st, mid, left, right, x); else propag(nod * 2, st, mid); if (mid < right) update(nod * 2 + 1, mid + 1, dr, left, right, x); else propag(nod * 2 + 1, mid + 1, dr); arb[nod] = (vl(nod * 2) + vl(nod * 2 + 1)) % mod; } void update1(int nod, int st, int dr, int poz, int x) { propag(nod, st, dr); if (st == dr) { arb[nod] = (1LL * arb[nod] * rid_put(x, fi - 1)) % mod; for (int i = 0; i < 9; i++) lazy1[nod][i] -= put[i]; return; } int mid = (st + dr) / 2; if (poz <= mid) { update1(nod * 2, st, mid, poz, x); propag(nod * 2 + 1, mid + 1, dr); } else { update1(nod * 2 + 1, mid + 1, dr, poz, x); propag(nod * 2, st, mid); } arb[nod] = (vl(nod * 2) + vl(nod * 2 + 1)) % mod; } int query(int nod, int st, int dr, int left, int right) { propag(nod, st, dr); if (left <= st && dr <= right) return vl(nod); int mid = (st + dr) / 2; int rez = 0; if (left <= mid) rez = (rez + query(nod * 2, st, mid, left, right)) % mod; if (mid < right) rez = (rez + query(nod * 2 + 1, mid + 1, dr, left, right)) % mod; return rez; } int main() { int n, l, r, x, q, tip; scanf("%d%d", &n, &mod); for (int i = 1; i <= n; i++) scanf("%d", &v[i]); int nr = mod; fi = mod; for (int i = 2; 1LL * i * i <= nr; i++) if (nr % i == 0) { while (nr % i == 0) nr /= i; fi = (fi / i) * (i - 1); } if (nr > 1) fi = (fi / nr) * (nr - 1); int a = mod, lim1 = 1e5; for (int i = 2; i <= lim1; i++) if (a % i == 0) { while (a % i == 0) a /= i; primes.push_back(i); vaz[i] = primes.size() - 1; } l1 = primes.size(); for (int i = 0; i < primes.size(); i++) { int p = 1; for (int j = 0; j <= lim; j++) { prec[i][j] = p; p = (1LL * p * primes[i]) % mod; } } init(1, 1, n); scanf("%d", &q); for (int i = 1; i <= q; i++) { scanf("%d", &tip); if (tip == 1) { scanf("%d%d%d", &l, &r, &x); for (int j = 0; j < 9; j++) put[j] = 0; int val1 = 1; for (int j = 2; j * j <= x; j++) if (x % j == 0) { int p = 0, c = 1; while (x % j == 0) { x /= j; c *= j; p++; } if (mod % j != 0) val1 *= c; else put[vaz[j]] = p; } if (x > 1) { if (mod % x != 0) val1 *= x; else put[vaz[x]] = 1; } update(1, 1, n, l, r, val1); } else if (tip == 2) { scanf("%d%d", &l, &x); for (int j = 0; j < 9; j++) put[j] = 0; int val1 = 1; for (int j = 2; j * j <= x; j++) if (x % j == 0) { int p = 0, c = 1; while (x % j == 0) { x /= j; c *= j; p++; } if (mod % j != 0) val1 *= c; else put[vaz[j]] = p; } if (x > 1) { if (mod % x != 0) val1 *= x; else put[vaz[x]] = 1; } update1(1, 1, n, l, val1); } else { scanf("%d%d", &l, &r); printf("%d\n", query(1, 1, n, l, r)); } } return 0; }
11
CPP
#include <iostream> #include <cstdio> #include <iomanip> #include <vector> #include <map> #include <set> #include <queue> #include <bitset> #include <stack> #include <utility> #include <numeric> #include <algorithm> #include <functional> #include <cctype> #include <complex> #include <string> #include <sstream> #include <fstream> #include <cassert> using namespace std; //common typedef int i32; typedef long long i64,ll; typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll; typedef vector<string> vs; typedef long double ld; #define BR "\n" #define ALL(c) (c).begin(),(c).end() #define REP(i,n) for(int i=0;i<(int)(n);++i) #define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it) #define IN(l,v,r) ((l)<=(v) && (v)<(r)) //config //#undef NDEBUG //#define INF 1<<30 //#define EPS 1e-8 //const ll MOD =100000007; //debug #ifdef NDEBUG #define DUMP(x) #define DUMPLN(x) #define DEBUG(x) #define DEBUGLN(x) #define LINE() #define LINELN() #define CHECK(exp,act) #define STOP(e) #else #define DUMP(x) cerr << #x << " = " << (x) #define DUMPLN(x) DUMP(x) <<endl #define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__ #define DEBUGLN(x) DEBUG(x)<<endl #define LINE() cerr<< " (L" << __LINE__ << ")" #define LINELN() LINE()<<endl #define CHECK(exp,act) if(exp!=act){DUMPLN(exp);DEBUGLN(act);} #define STOP(e) CHECK(e,true);if(!(e)) exit(1); #endif template<class T> inline string toString(const vector<T>& x) { stringstream ss; REP(i,x.size()){ if(i!=0)ss<<" "; ss<< x[i]; } return ss.str(); } template<class T> inline string toString(const vector<vector<T> >& map) { stringstream ss; REP(i,map.size()){ if(i!=0)ss<<BR; ss<< toString(map[i]); } return ss.str(); } template<class K,class V> string toString(map<K,V>& x) { string res;stringstream ss; for(auto& p:x)ss<< p.first<<":" << p.second<<" "; return ss.str(); } string BITtoString(int bit){ stringstream ss; while(bit!=0){ss<<(bit%2);bit/=2;} string res=ss.str();reverse(ALL(res)); return res; } template<typename T,typename V> inline T mod(T v,V MOD){ return (v%MOD+MOD)%MOD; } int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; class Main{ public: vector<vector<bool>> passed; int dfs(int y,int x,int ty,int tx,int t){ if(y==ty && x==tx)return t; if(!IN(0,y,R) || !IN(0,x,C))return -1; if(passed[y][x])return -1; if(map[y][x]=='#')return -1; passed[y][x]=true; REP(di,4){ int nx=x+dx[di],ny=y+dy[di]; int goal=dfs(ny,nx,ty,tx,t+1); if(goal!=-1){ passT[ny][nx].push_back(t+1); return goal; } } return -1; } int R,C,M; vector<vector<vector<int>>> passT; vector<string> map; void run(){ cin >> R >> C >> M; map=vector<string>(R); REP(y,R)cin >> map[y]; vector<vector<int>> perC(R,vector<int>(C)),onC(R,vector<int>(C)),offC(R,vector<int>(C)); REP(y,R)REP(x,C) cin >> perC[y][x]; REP(y,R)REP(x,C) cin >> onC[y][x]; REP(y,R)REP(x,C) cin >> offC[y][x]; vector<int> xs(M),ys(M); REP(i,M)cin >> ys[i] >> xs[i]; passT=vector<vector<vector<int>>> (R,vector<vector<int>>(C)); passT[ys[0]][xs[0]].push_back(0); int t=0; for(int i=0;i+1<M;i++){ passed=vector<vector<bool>>(R,vector<bool>(C)); t=dfs(ys[i],xs[i],ys[i+1],xs[i+1],t); } ll res=0; REP(y,R)REP(x,C){ if(passT[y][x].size()>0){ //first res+=onC[y][x]; //mid for(int i=1;i<passT[y][x].size();i++){ res+=min(perC[y][x]*(passT[y][x][i]-passT[y][x][i-1]),offC[y][x]+onC[y][x]); } //last res+=offC[y][x]; } } cout <<res <<endl; } }; int main(){ //ios::sync_with_stdio(false); Main().run(); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int INF = (1LL << 31) - 1; const long long int LINF = (1LL << 62) - 1; const int NMAX = 2000 + 5; const int MMAX = 500 + 5; const int KMAX = 100000 + 5; const int PMAX = 100000 + 5; const int LMAX = 100000 + 5; const int VMAX = 100000 + 5; int N, M; char S[NMAX]; char P[MMAX]; int A[NMAX]; int DP[NMAX][NMAX]; int main() { int i, j, k, len; scanf("%s", S); scanf("%s", P); N = strlen(S); M = strlen(P); for (i = 0; i < N; i++) { for (k = 0, len = 0; k < M && i + len < N; len++) if (S[i + len] == P[k]) k++; if (k < M) A[i] = -1; else A[i] = len; } for (i = 0; i < N; i++) { for (j = 0; j <= i; j++) { DP[i + 1][j] = max(DP[i + 1][j], DP[i][j]); DP[i + 1][j + 1] = max(DP[i + 1][j + 1], DP[i][j]); if (A[i] >= 0) DP[i + A[i]][j + A[i] - M] = max(DP[i + A[i]][j + A[i] - M], DP[i][j] + 1); } } for (i = 0; i <= N; i++) printf("%d ", DP[N][i]); return 0; }
9
CPP
import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import * 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, insort from time import perf_counter from fractions import Fraction import copy from copy import deepcopy import time starttime = time.time() mod = int(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)] try: # sys.setrecursionlimit(int(pow(10,6))) sys.stdin = open("input.txt", "r") # sys.stdout = open("../output.txt", "w") except: pass def pmat(A): for ele in A: print(*ele,end="\n") n=L()[0] A=L() x=A[0] y=A[1:] cost=0 while(x<=max(y)): y.sort() y[-1]-=1 x+=1 cost+=1 print(cost) endtime = time.time() # print(f"Runtime of the program is {endtime - starttime}")
7
PYTHON3
#include <bits/stdc++.h> const double pi = acos(-1); const int MOD = 1e9 + 7; const int INF = 1e9 + 7; const int MAXN = 1e6 + 5; const double eps = 1e-9; using namespace std; int main() { int n; scanf("%d", &n); if (n <= 5) printf("-1\n"); else { printf("1 2\n1 3\n1 4\n4 5\n4 6\n"); for (int i = 7; i <= n; i++) printf("4 %d\n", i); } for (int i = 1; i < n; i++) printf("%d %d\n", i, i + 1); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; //#define int long long #define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i)) #define rep(i,n) reps(i,0,n) #define rept(i,n) rep(i,(n)+1) #define repst(i,s,n) reps(i,s,(n)+1) #define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i)) #define repr(i,n) reprt(i,n,0) #define each(itr,v) for(auto &(itr):(v)) #define all(c) (c).begin(),(c).end() #define rall(c) (c).rbegin(),(c).rend() #define pb push_back #define mp make_pair #define fi first #define se second #define tmax(x,y,z) max(x,max(y,z)) #define tmin(x,y,z) min(x,min(y,z)) #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) #define ln '\n' #define bln(i,n) (((i)==(n)-1)?'\n':' ') #define dbg(x) cout<<#x" = "<<(x)<<ln<<flush #define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ')<<flush;}} #define zero(a) memset(a,0,sizeof(a)) #define unq(a) sort(all(a)),a.erase(unique(all(a)),a.end()) typedef complex<double> P; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<string> vst; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > mat; const ll inf = (ll)1e9+10; const ll linf = (ll)1e18+10; const ll mod = (ll)(1e9+7); const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1}; const double eps = 1e-8; ll mop(ll a,ll b,ll m=mod) {ll r=1;a%=m;for(;b;b>>=1){if(b&1)r=r*a%m;a=a*a%m;}return r;} ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a,ll b) {return a*b/gcd(a,b);} bool ool(int x,int y,int h,int w) {return((x<0)||(h<=x)||(y<0)||(w<=y));} bool deq(double a,double b) {return abs(a-b)<eps;} struct oreno_initializer { oreno_initializer() { cin.tie(0); ios::sync_with_stdio(0); } } oreno_initializer; // ━━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥… // .。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+ // ・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・ // dp: 人iがj回目まででk回休憩する確率 // iがj回休憩して優勝する確率は (iがj回休憩する確率) * Π(j!=iがm回目まででpj回以上休憩する確率) // pjは算数で求められる 累積和とっとくと楽そう // t,vが0の場合に注意 int n, m, l, t[111], v[111]; double p[111], dp[111][55][55], r[111][55]; signed main() { cin >> n >> m >> l; rep(i,n) cin >> p[i] >> t[i] >> v[i]; rep(i,n) p[i] /= 100; rep(i,n) { dp[i][0][0] = 1; rep(j,m) rept(k,j) { dp[i][j+1][k+1] += dp[i][j][k] * p[i]; dp[i][j+1][k] += dp[i][j][k] * (1. - p[i]); } repr(j,m) r[i][j] = r[i][j+1] + dp[i][m][j]; } rep(i,n) { double res = 0; rept(j,m) { if (v[i]==0) break; double ti = 1.*l/v[i] + 1.*j*t[i], pl = dp[i][m][j]; rep(k,n) if (i!=k) { if (v[k]==0) continue; // 勝ち確 if (t[k]==0) { if (deq(ti,1.*l/v[k]) || ti+eps>1.*l/v[k]) pl = 0; continue; } if (ti<1.*l/v[k]) continue; // kが休憩0でもiの方が速い場合 int pj = ceil((ti-1.*l/v[k])/t[k]); if (deq((ti-1.*l/v[k])/t[k],pj)) pj++; if (pj>m) { pl = 0; // kが毎回休憩してもj回休憩したiより速い場合 break; } else pl *= r[k][pj]; } res += pl; } printf("%.14lf\n",res); } }
0
CPP
#include <bits/stdc++.h> using namespace std; const int MAXSIZE = 10000020; const int mod = 1000000007; int bufpos; char buf[MAXSIZE]; void init() { buf[fread(buf, 1, MAXSIZE, stdin)] = '\0'; bufpos = 0; } long long readint() { long long val = 0; for (; !isdigit(buf[bufpos]); bufpos++) ; for (; isdigit(buf[bufpos]); bufpos++) val = val * 10 + buf[bufpos] - '0'; return val; } char readchar() { for (; isspace(buf[bufpos]); bufpos++) ; return buf[bufpos++]; } int readstr(char* s) { int cur = 0; for (; isspace(buf[bufpos]); bufpos++) ; for (; !isspace(buf[bufpos]); bufpos++) s[cur++] = buf[bufpos]; s[cur] = '\0'; return cur; } char bf[1 << 25], st[20], *w = bf; void flush() { fwrite(bf, 1, w - bf, stdout); w = bf; } inline void pc(register char c) { *w++ = c; } void print(register int n) { register char* t = st; do { *t++ = n % 10 + 48; } while (n /= 10); while (t-- > st) pc(*t); } using namespace std; const int M = 1e9 + 7; int T, g[3], tp, h; long long n, f[3], c[4][4]; int main() { init(); for (int i = 0; i < 4; i++) { c[i][0] = 1; for (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } for (T = readint(); T--;) { n = readint(); f[0] = 1, f[1] = f[2] = 0; for (int i = 0; 1ll << i <= n; i++) { for (int j = 0; j < 3; j++) g[j] = (f[j] %= M), f[j] = 0; for (int j = 0; j < 3; j++) { tp = min(i + 1, 3), h = g[j]; for (int k = (j + 1 & 1) == (n >> i & 1); k <= tp; k += 2) f[j + k >> 1] += c[tp][k] * g[j]; } } print(f[0] % M), pc('\n'); } flush(); }
15
CPP
t = int(input()) for x in range(t): arr = list(map(int, input().split())) arr2 = arr[:3] arr2.sort() sumofdif = ((arr2[2]-arr2[0])+(arr2[2]-arr2[1])) sub = arr[3] - sumofdif if (sub%3==0) and (arr[3]>=sumofdif): print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1500 + 5, P = 998244353; int n, m, cost[N][N], dp[N][N]; pair<int, int> edge[N * N]; int par[N], ed[N], sz[N]; int get_par(int u) { return ~par[u] ? par[u] = get_par(par[u]) : u; } void merge(int u, int v) { ed[u = get_par(u)]++; v = get_par(v); if (u ^ v) { if (sz[u] < sz[v]) swap(u, v); for (int i = sz[u]; i; dp[u][i--] = 0) for (int j = sz[v]; j; j--) dp[u][i + j] = (1LL * dp[u][i] * dp[v][j] + dp[u][i + j]) % P; sz[par[v] = u] += sz[v]; ed[u] += ed[v]; } if (ed[u] == sz[u] * (sz[u] - 1) / 2) ++dp[u][1] %= P; } void sort_edges() { for (int u = 0; u < n; u++) for (int v = u + 1; v < n; v++) edge[m++] = {u, v}; sort(edge, edge + m, [](pair<int, int> e1, pair<int, int> e2) { return cost[e1.first][e1.second] < cost[e2.first][e2.second]; }); } void calculate_dp() { memset(par, -1, sizeof par); for (int u = 0; u < n; u++) dp[u][1] = sz[u] = 1; for (int i = 0; i < m; i++) merge(edge[i].first, edge[i].second); } void read_input() { cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> cost[i][j]; } void solve() { sort_edges(); calculate_dp(); } void write_output() { for (int i = 1; i <= n; i++) cout << dp[get_par(0)][i] << ' '; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); read_input(), solve(), write_output(); return 0; }
13
CPP
#include <bits/stdc++.h> int main() { std::string str; int k; std::cin >> k >> str; int n = 0; std::vector<bool> known(k, false); for (int i = 0; i < str.length(); ++i) { if (str[i] != '?') { if (!known[str[i] - 'a']) { n += 1; } known[str[i] - 'a'] = true; } } n = k - n; bool ok = true; int free = 0; for (int i = 0; i < str.length(); ++i) { char sym = str[i]; char opposite = str[str.length() - i - 1]; if (sym == '?') { if (opposite != '?') { str[i] = opposite; } else { if (i < (str.length() + 1) / 2) { free += 1; } } } else { if (opposite != '?' && opposite != sym) { ok = false; } } } int pos = 0; for (int i = 0; i < str.length(); ++i) { if (str[i] == '?') { if (free == n) { while (pos + 1 < k && known[pos]) { pos += 1; } } str[i] = str[str.length() - i - 1] = 'a' + pos; if (!known[pos]) { n -= 1; } known[pos] = true; free -= 1; } } while (pos + 1 < k && known[pos]) { pos += 1; } if (ok && pos + 1 == k && known[pos]) { std::cout << str; } else { std::cout << "IMPOSSIBLE"; } return 0; }
9
CPP
# import sys # sys.stdin=open("input1.in","r") # sys.stdout=open("output2.out","w") L1=list(input()) L2=list(input()) L3=list(input()) arr=26*[0] arr2=26*[0] for i in range(len(L1)): arr[ord(L1[i])-65]+=1 for i in range(len(L2)): arr[ord(L2[i])-65]+=1 for i in range(len(L3)): arr2[ord(L3[i])-65]+=1 FLAG=0 for i in range(26): if arr2[i]!=arr[i]: print("NO") FLAG=1 break if FLAG==0: print("YES")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; char g[10][10]; int ax[10 * 10], n; bool use[10 * 10]; int q[2][100], ct[2], vst[8][8]; int dx[9] = {0, 0, 0, 1, -1, 1, 1, -1, -1}; int dy[9] = {0, 1, -1, 0, 0, -1, 1, -1, 1}; bool check(int x, int y) { if (x < 0 || y < 0 || x >= 8 || y >= 8) return 0; if (vst[x][y]) return 0; for (int i = 0; i < n; i++) if (ax[i] % 8 == y && (ax[i] / 8 == x || ax[i] / 8 == x - 1)) return 0; return 1; } int bfs() { q[0][0] = 7 * 8; ct[0] = 1; for (int i = 0; i < 10; i++) { int id = i & 1, nid = id ^ 1; ct[nid] = 0; memset(vst, 0, sizeof(vst)); for (int j = 0; j < ct[id]; j++) { int u = q[id][j]; int x = u / 8, y = u % 8; for (int d = 0; d < 9; d++) { int nx = x + dx[d], ny = y + dy[d]; if (check(nx, ny)) q[nid][ct[nid]++] = nx * 8 + ny, vst[nx][ny] = 1; if (nx == 0 && ny == 7) return 1; } } for (int j = 0; j < n; j++) ax[j] = (ax[j] / 8 + 1) * 8 + ax[j] % 8; } if (ct[0]) return 1; return 0; } int main() { n = 0; for (int i = 0; i < 8; i++) { scanf("%s", g[i]); for (int j = 0; j < 8; j++) if (g[i][j] == 'S') ax[n++] = i * 8 + j; } if (bfs()) puts("WIN"); else puts("LOSE"); return 0; }
7
CPP
for t in range(int(input())): x,y = [int(s) for s in input().split()] a,b = [int(s) for s in input().split()] if x > y: x,y = y,x z = y-x b = min(b,2*a) ans = b*x + a*z print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-5; void read(int& x) { bool fu = 0; char c; for (c = getchar(); c <= 32; c = getchar()) ; if (c == '-') fu = 1, c = getchar(); for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0'; if (fu) x = -x; }; char getc() { char c; for (c = getchar(); c <= 32; c = getchar()) ; return c; } const int inf = 1000000000; int n, m, r, i, j, k, l, d, p, g, h, xx, yy, ans; long long tot, ff; int a[510][510], s[510][510], sum[510][510], G[510][510]; long long gg[510][510]; int sx[510], sy[510], st[510]; int gets(int i1, int i2, int j) { if (j > m) return 0; if (i1 < 1) i1 = 1; if (i2 > n) i2 = n; return sum[i2][j] - sum[i1 - 1][j]; } void pre(int x, int y) { int i1, j1, i2, h; for (i1 = 1; i1 <= n; i1++) st[i1] = m; for (i1 = 1; i1 <= n; i1++) { for (j1 = 1; j1 <= m; j1++) if (((i1 - x) * (i1 - x)) + ((j1 - y) * (j1 - y)) <= ((r) * (r))) break; for (i2 = 1; i2 <= n; i2++) if (((i1 - i2) * (i1 - i2)) <= ((r) * (r))) { h = int(sqrt(((r) * (r)) - ((i1 - i2) * (i1 - i2)) + eps)); st[i2] = min(st[i2], j1 - h - 1); } } } int main() { read(n); read(m); read(r); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) read(a[i][j]); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) sum[i][j] = sum[i - 1][j] + a[i][j]; memset(s, 200, sizeof(s)); for (i = r + 1; i <= n - r; i++) for (j = r + 1; j <= m - r; j++) { s[i][j] = 0; for (d = -r; d <= r; d++) if (j + d >= 1 && j + d <= m) { h = int(sqrt(((r) * (r)) - ((d) * (d)) + eps)); s[i][j] += gets(i - h, i + h, j + d); } } memset(G, 200, sizeof(G)); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { G[i][j] = G[i][j - 1]; gg[i][j] = gg[i][j - 1]; if (s[i][j] > G[i][j]) G[i][j] = s[i][j], gg[i][j] = 1; else if (s[i][j] == G[i][j]) gg[i][j]++; } ans = -inf; int best; for (i = r + 1; i <= n - r; i++) { pre(i, r + 1); for (j = r + 1; j <= m - r; j++) { best = -inf; ff = 0; bool mark; for (d = r + 1; d <= n - r; d++) { int tmp; if (st[d] >= j) { if (d < i) tmp = j; else tmp = j - 1; tmp = min(tmp, m - r); swap(st[d], tmp); mark = 1; } else mark = 0; if (st[d] > 0) if (G[d][st[d]] > best) best = G[d][st[d]], ff = gg[d][st[d]]; else if (G[d][st[d]] == best) ff += gg[d][st[d]]; if (mark) swap(st[d], tmp); } for (d = 1; d <= n; d++) st[d] = min(m, st[d] + 1); if (s[i][j] + best > ans) ans = s[i][j] + best, tot = ff; else if (s[i][j] + best == ans) tot += ff; } } if (ans < 0) { printf("0 0\n"); return 0; } printf("%d %I64d\n", ans, tot); scanf("\n"); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n; int ar[50][50]; int dp[51][51]; bool leaf[50]; vector<int> adj[50]; vector<int> adjw[50]; vector<int> leafs; int bad; vector<int> pos; int go(int rem, int now) { if (rem == 0) { return 0; } if (dp[rem][now] != -1) { return dp[rem][now]; } int low = 0; int high = 12500000; while (low < high) { int mid = (low + high + 1) / 2; int temp = rem; for (int i = 0; i < n; i++) { if (!leaf[i] || i == now) { continue; } int cur = 0; while (temp > 0 && ar[now][i] + go(rem - cur - 1, i) >= mid) { cur++; temp--; } } if (temp == 0) { low = mid; } else { high = mid - 1; } } dp[rem][now] = low; return low; } void dfs(int now, int from) { if (leaf[now]) { leafs.push_back(now); } for (int i = 0; i < pos.size(); i++) { if (now == pos[i]) { bad++; } } for (int i = 0; i < adj[now].size(); i++) { int to = adj[now][i]; if (to != from) { dfs(to, now); } } } void dfs(int now, int from, int root, int dist) { ar[root][now] = dist; for (int i = 0; i < adj[now].size(); i++) { int to = adj[now][i]; if (to != from) { dfs(to, now, root, dist + adjw[now][i]); } } } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin >> n; int maxw = 0; for (int i = 1; i < n; i++) { int a, b, w; cin >> a >> b >> w; a--; b--; adj[a].push_back(b); adj[b].push_back(a); adjw[a].push_back(w); adjw[b].push_back(w); } for (int i = 0; i < n; i++) { dfs(i, -1, i, 0); } for (int i = 0; i <= 50; i++) { for (int j = 0; j <= 50; j++) { dp[i][j] = -1; } } for (int i = 0; i < n; i++) { leaf[i] = adj[i].size() == 1; } int start, m; cin >> start >> m; start--; for (int i = 0; i < m; i++) { int x; cin >> x; x--; pos.push_back(x); } int ans = 12500000; for (int i = 0; i < adj[start].size(); i++) { leafs.clear(); leafs.resize(0); bad = 0; dfs(adj[start][i], start); if (bad == 0) { continue; } int low = 0; int high = 1250000; while (low < high) { int mid = (low + high + 1) / 2; int temp = bad; for (int z = 0; z < leafs.size(); z++) { int cl = leafs[z]; if (cl == start) { continue; } int cur = 0; while (temp > 0 && ar[start][cl] + go(m - cur - 1, cl) >= mid) { cur++; temp--; } } if (temp == 0) { low = mid; } else { high = mid - 1; } } ans = min(ans, low); } cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, mod = 1e9 + 7; int a[N]; long double ncr(int n, int r) { long double ans = 1; int cnt = r; for (int i = n; i >= n - r + 1; i--) { ans *= (long double)i / (long double)cnt; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, h; cin >> n >> m >> h; n--; h--; int sum = 0; for (int i = 0; i < m; i++) { cin >> a[i]; if (i == h) a[i]--; sum += a[i]; } if (sum < n) return cout << -1, 0; if (sum - a[h] < n) return cout << 1, 0; cout << fixed << setprecision(10) << 1.00 - (ncr(sum - a[h], n) / ncr(sum, n)); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 10; long long PI = acos(-1.0), MOD = 1000000007; long long n, m, x, y, z, k, sol, sum, ans, l, r, xx, yy, t; long long a[N], b[N], mem[1000][1000]; vector<pair<pair<long long, long long>, long long>> v; vector<long long> v1; vector<long long> v2; vector<pair<pair<long long, long long>, long long>> vp; map<long long, long long> ma; string s1, s2, s; set<long long> st; char c; int main() { cin >> n; map<long long, long long> mx; map<long long, long long> my; for (int i = 0; i < n; i++) { cin >> x >> y; vp.push_back({{x, y}, i + 1}); } sort(vp.begin(), vp.end()); for (int i = 0; i < vp.size() - 2; i++) { pair<long long, long long> x, y; x = {vp[i].first.first - vp[i + 1].first.first, vp[i].first.second - vp[i + 1].first.second}, y = {vp[i + 1].first.first - vp[i + 2].first.first, vp[i + 1].first.second - vp[i + 2].first.second}; if (x.first * y.second != x.second * y.first) { cout << vp[i].second << " " << vp[i + 1].second << " " << vp[i + 2].second << endl; break; } } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main(){ int n,a[100][100]={0},i,j,b,c,q; for(cin>>n,i=0;i<n;i++){ cin>>b>>q;b--; for(j=0;j<q;j++){ cin>>c; a[b][--c]=1; } } for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(j)cout<<' '; cout<<a[i][j]; } cout<<endl; } }
0
CPP
n = int(input()) a = [list(input()) for i in range(n)] #print(a) counter = 0 for i in range(1, n-1): for j in range(1, n-1): if (a[i][j]==a[i-1][ j-1]==a[i-1][ j+1]==a[i+1][j-1]==a[i+1][j+1]=='X'): counter+=1 print(counter)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long M = 500010, excess = 2; string A; long long dp[92][excess + 1][excess + 1]; long long f[92]; long long call(long long i, long long now, long long nxt) { if (i == A.size()) return now == 0 && nxt == 0; now += (A[i] == '1'); if (now > excess || nxt > excess) return 0; if (dp[i][now][nxt] != -1) return dp[i][now][nxt]; long long a = 0, b = 0; if (now > 0) a = call(i + 1, nxt + now - 1, now - 1); b = call(i + 1, nxt + now, now); return dp[i][now][nxt] = a + b; } int main() { ios_base::sync_with_stdio(false); long long i, j, k; long long q, n; f[0] = 1; f[1] = 1; for (i = 2; i < 92; i++) f[i] = f[i - 1] + f[i - 2]; scanf("%lld", &q); while (q--) { scanf("%lld", &n); bool fg = false; for (i = 91; i > 0; i--) { if (n >= f[i]) { n -= f[i]; fg = true; A += '1'; } else if (fg) A += '0'; } memset((dp), (-1), sizeof(dp)); printf("%lld\n", call(0, 0, 0)); A.clear(); } return 0; }
10
CPP
# from dust i have come dust i will be n=int(input()) a=[0]*n for i in range(n): a[i]=int(input()) avg=sum(a) if avg%n!=0: print('Unrecoverable configuration.') exit(0) avg=avg//n x=[] for i in range(n): if a[i]!=avg: x.append(i) if len(x)==0: print('Exemplary pages.') elif len(x)==2: if (a[x[0]]+a[x[1]])//2==avg: d=abs(a[x[0]]-a[x[1]])//2 if a[x[0]]>a[x[1]]: print(str(d)+' ml. from cup #'+str(x[1]+1)+' to cup #'+str(x[0]+1)+'.') else: print(str(d)+' ml. from cup #'+str(x[0]+1)+' to cup #'+str(x[1]+1)+'.') else: print('Unrecoverable configuration.') else: print('Unrecoverable configuration.')
8
PYTHON3
// @snippet<sh19910711/contest:headers.cpp> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <vector> #include <set> #include <map> #include <stack> #include <queue> #include <algorithm> #include <numeric> #include <limits> #include <complex> #include <functional> #include <iterator> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> // @snippet<sh19910711/contest:solution/interface.cpp> namespace solution { class ISolution { public: virtual void init() {}; virtual bool input() { return false; }; virtual void output() {}; virtual int run() = 0; }; } // @snippet<sh19910711/contest:graph/config.cpp> namespace graph { typedef int Weight; const Weight DEFAULT_WEIGHT = std::numeric_limits<Weight>().max() / 2; } // @snippet<sh19910711/contest:graph/interface.cpp> namespace graph { class IGraph { public: virtual void init( int n ) = 0; virtual void addEdge( int from, int to ) = 0; virtual void addVertices() = 0; virtual void addEdges() = 0; virtual int countVertices() = 0; virtual Weight getEdgeCost( int from, int to ) = 0; virtual void setEdgeCost( int from, int to, Weight weight ) = 0; virtual Weight getVertexCost( int v ) = 0; virtual void setVertexCost( int v, Weight weight ) = 0; }; } // @snippet<sh19910711/contest:graph/adjacent_list.cpp> namespace graph { class AdjacentListGraph: public IGraph { public: typedef std::vector<int> VI; typedef std::vector<VI> Edges; typedef std::vector<Weight> Weights; typedef std::vector<Weights> EdgeWeights; typedef std::vector<VI> Matrix; typedef VI::iterator VertexIterator; Edges edges; EdgeWeights edge_weights; Weights vertex_weights; Matrix edge_exists; int num; void init( int n ) { num = n; edges.resize(num); vertex_weights.resize(num); edge_weights.resize(num); edge_exists.resize(num); std::fill( edges.begin(), edges.end(), VI() ); std::fill( edge_weights.begin(), edge_weights.end(), Weights( num, 0 ) ); std::fill( edge_exists.begin(), edge_exists.end(), VI( num, false ) ); addVertices(); addEdges(); } void addEdge( int from, int to ) { if ( edge_exists[from][to] ) return; edge_exists[from][to] = true; edges[from].push_back(to); } void addEdge( int from, int to, Weight weight ) { addEdge( from, to ); setEdgeCost( from, to, weight ); } int countVertices() { return num; } Weight getVertexCost( int from ) { return vertex_weights[from]; } void setVertexCost( int from, Weight weight ) { vertex_weights[from] = weight; } Weight getEdgeCost( int from, int to ) { return edge_weights[from][to]; } void setEdgeCost( int from, int to, Weight weight ) { edge_weights[from][to] = weight; } VertexIterator begin( int from ) { return edges[from].begin(); } VertexIterator end( int from ) { return edges[from].end(); } }; } // @snippet<sh19910711/contest:solution/solution.cpp> namespace solution { using namespace std; const int SIZE = 10001; int n; string A[SIZE]; bool U[26]; int vertices; class Graph: public graph::AdjacentListGraph { public: int findNode( char c ) { return c - 'a'; } void init( int num ) { vertices = 0; graph::AdjacentListGraph::init(num); } graph::Weight& edge( int from, int to ) { return edge_weights[from][to]; } void addVertices() { for ( int i = 0; i < n; ++ i ) { int a = findNode(*A[i].begin()); int b = findNode(*A[i].rbegin()); if ( a == b ) continue; if ( ! U[a] ) vertices ++; if ( ! U[b] ) vertices ++; U[a] = U[b] = true; } } void addEdges() { for ( int i = 0; i < n; ++ i ) { int a = findNode(*A[i].begin()); int b = findNode(*A[i].rbegin()); if ( a == b ) continue; addEdge( a, b, 0 ); } for ( int i = 0; i < n; ++ i ) { int a = findNode(*A[i].begin()); int b = findNode(*A[i].rbegin()); if ( a == b ) continue; edge(a,b) ++; } } }; int GetConnectedVertices( graph::AdjacentListGraph& graph, int start_vertex ) { int n = graph.num; bool used[n]; fill( used, used+n, false ); queue <int> Q; Q.push(start_vertex); used[start_vertex] = true; int res = 1; while ( ! Q.empty() ) { int current_vertex = Q.front(); Q.pop(); for ( vector<int>::iterator it_i = graph.begin(current_vertex); it_i != graph.end(current_vertex); ++ it_i ) { int& next_vertex = *it_i; if ( used[next_vertex] ) continue; used[next_vertex] = true; res ++; Q.push(next_vertex); } } return res; } class Solution: public ISolution { public: Graph G; void init() { fill( U, U+26, false ); } bool input() { if ( ! ( cin >> n && n ) ) return false; for ( int i = 0; i < n; ++ i ) cin >> A[i]; return true; } bool solve() { G.init(26); for ( int i = 0; i < 26; ++ i ) { if ( U[i] ) { int ret = GetConnectedVertices( G, i ); if ( ret != vertices ) return false; break; } } for ( int i = 0; i < 26; ++ i ) { if ( ! U[i] ) continue; int cnt_in = 0; int cnt_out = 0; for ( int j = 0; j < 26; ++ j ) { if ( ! U[j] ) continue; cnt_in += G.edge( j, i ); cnt_out += G.edge( i, j ); } if ( cnt_in != cnt_out ) return false; } return true; } void output( bool result ) { cout << ( result ? "OK" : "NG" ) << endl; } int run() { while ( init(), input() ) { output( solve() ); } return 0; } }; } // @snippet<sh19910711/contest:main.cpp> int main() { return solution::Solution().run(); }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) { cin >> a[i]; } int pos1 = 0, pos2 = 0; for (int i = 1; i < n; i++) { if (abs(a[i] - a[i - 1]) > 1) { pos1 = i; pos2 = i + 1; break; } if (abs(a[i + 1] - a[i]) > 1 && i != n - 1) { pos1 = i + 1; pos2 = i + 2; break; } } if (pos1 == pos2) { cout << "NO" << endl; } else { cout << "YES" << endl; cout << pos1 << " " << pos2 << endl; } } }
8
CPP
#include <bits/stdc++.h> using namespace std; long long K; long long N; int main() { scanf("%I64d%I64d", &N, &K); long long cur_add = 0LL; long long reali = 1; long long size = N; for (long long i = 1; i <= N; i++) { long long a; scanf("%I64d", &a); long long d_i = cur_add - (reali - 1) * (size - reali) * a; if (d_i < K) { printf("%I64d\n", i); size--; } else { cur_add += a * (reali - 1); reali++; } } return 0; }
9
CPP
n = int(input()) w = list(map(int,input().split(" "))) w.sort() best = sum(w) for i in range(0,2*n): for j in range(i+1,2*n): k = 0 k2 = 1 s = 0 while k2 < 2*n: while k == i or k == j: k += 1 if k >= 2*n: break k2 = k+1 while k2 == i or k2 == j: k2 += 1 s += w[k2]-w[k] k = k2+1 k2 = k+1 if s < best: best = s print(best)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, d; long long tree[205][205][205]; long long c[205][205], mod; int main() { scanf("%d%d%lld", &n, &d, &mod); for (int i = 0; i <= n; ++i) c[i][0] = 1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= i; ++j) { c[i][j] = c[i - 1][j] + c[i - 1][j - 1]; c[i][j] %= mod; } } tree[1][1][0] = 1; for (int i = 1; i < n; ++i) { for (int j = 1; j <= n; ++j) { for (int k = 0; k <= d; ++k) { tree[i + 1][j][k] = tree[i][j][k]; } } long long tree_i = 0; for (int j = 0; j < d; ++j) { tree_i += tree[i][i][j]; tree_i %= mod; } long long ways = 1; for (int j = 1; j <= d && i * j <= n; ++j) { ways = (ways * c[i * j - 1][i - 1]) % mod; for (int k = 1; k + i * j <= n; ++k) { long long cc = ways * c[i * j + k - 1][k - 1] % mod; for (int h = 0; h + j <= d; ++h) { tree[i + 1][i * j + k][h + j] += tree[i][k][h] * tree_i % mod * cc % mod; tree[i + 1][i * j + k][h + j] %= mod; } } ways = ways * tree_i % mod; } } long long tot_tr = 0; for (int i = 0; i <= n - 1; ++i) { for (int j = 0; j <= d; ++j) { for (int k = 0; k + j <= d; ++k) { if (k == 1) { continue; } tot_tr += tree[n][i + 1][j] * tree[n][n - i][k] % mod; tot_tr %= mod; } } } long long ans = 1LL * 2 * n * (n - 1) % mod * tot_tr % mod; printf("%lld\n", ans); return 0; }
14
CPP
from sys import stdin, stdout, stderr, exit from math import log import re # inputf = open('input.txt', 'r') # outputf = open('output.txt', 'w') def readil(): return list(map(int, stdin.readline().strip().split())) k = 0 p = "" def bd(s:str): global p p = s def norm(s:str): global k if s[0] == '1': k += len(s) - 1 else: stdout.write('0') exit(0) def main(): temp = re.compile('^1?0*$') n = readil()[0] a = stdin.readline().strip().split() for c in a: if(re.match(temp, c)): norm(c) else: bd(c) stdout.write((p if p != '' else '1') + '0'*k) if __name__ == '__main__': main() # inputf.close() # outputf.close()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m - (n % m); i++) { printf("%d ", n / m); } for (int i = 0; i < n % m; i++) { printf("%d ", n / m + 1); } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; #define mod 1'000'000'007 using ll = long long; template<typename T> void deb(initializer_list<T> l) { for (auto &e : l) cout << e << ' '; cout << endl; } ll n, m; vector<ll> tim; void solve(vector<tuple<ll, char, int>>& a) { deque<tuple<ll, char, int>> s; for (auto& [val, dir, idx] : a) { if (dir == 'R') s.emplace_back(val, dir, idx); else if (s.empty() || get<1>(s.back()) == 'L') s.emplace_back(val, dir, idx); else { auto [valR, dirR, idxR] = s.back(); s.pop_back(); tim[idx] = tim[idxR] = abs(val - valR) / 2; } } while (true) { if (s.empty()) break; auto [val, dir, idx] = s.front(); if (dir == 'R') break; if ((int) s.size() == 1) return; s.pop_front(); auto [val2, dir2, idx2] = s.front(); if (dir2 == 'R') { s.emplace_front(val, dir, idx); break; } s.pop_front(); tim[idx] = tim[idx2] = val + abs(val - val2) / 2; } while (true) { if (s.empty()) break; auto [val, dir, idx] = s.back(); if (dir == 'L' || (int) s.size() == 1) return; s.pop_back(); auto [val2, dir2, idx2] = s.back(); if (dir2 == 'R') { s.pop_back(); tim[idx] = tim[idx2] = m - val + abs(val - val2) / 2; } else { tim[idx] = tim[idx2] = (val2 + m - val + m) / 2; return; } } } void solve() { cin >> n >> m; vector<tuple<ll, char, int>> a(n); for (auto& e : a) cin >> get<0>(e); for (auto& e : a) cin >> get<1>(e); for (int i = 0; i < n; ++i) get<2>(a[i]) = i; sort(a.begin(), a.end()); vector<tuple<ll, char, int>> odd, even; for (auto& [val, dir, idx] : a) { if (val % 2 == 1) odd.emplace_back(val, dir, idx); else even.emplace_back(val, dir, idx); } tim.assign(n, -1); solve(odd); solve(even); for (auto& e : tim) cout << e << ' '; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; cin >> t; while (t--) solve(); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int a[1002], cont[1002]; int main() { int i, j, k, n; long int ans = 0; for (i = 0; i <= 1001; i++) cont[i] = 0; char st[1002]; scanf("%s", st); int len = strlen(st); scanf("%d", &k); for (i = 0; i < 26; i++) { scanf("%d", &a[i]); cont['a' + i] = a[i]; } for (i = 0; i < len; i++) { ans = ans + cont[st[i]] * (i + 1); } sort(cont, cont + 123); for (i = len; i < len + k; i++) ans = ans + cont[122] * (i + 1); printf("%ld", ans); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 500, mod = 1e9 + 7; int n, X, fact[maxn + 3], finv[maxn + 3], c[maxn + 3], f[maxn + 3][maxn + 3][maxn + 3], p[maxn + 3][maxn + 3]; int qpow(int a, int b) { if (a <= maxn && b <= maxn && p[a][b]) return p[a][b]; int c = 1, d = a, e = b; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) c = 1ll * a * c % mod; if (d <= maxn && e <= maxn) p[d][e] = c; return c; } void prework(int n) { fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = 1ll * fact[i - 1] * i % mod; finv[n] = qpow(fact[n], mod - 2); for (int i = n; i; i--) finv[i - 1] = 1ll * finv[i] * i % mod; } int C(int n, int m) { if (n < 0 || m < 0 || m > n) return 0; return 1ll * fact[n] * finv[m] % mod * finv[n - m] % mod; } int main() { scanf("%d %d", &n, &X); prework(X); for (int i = 1, x; i <= n; i++) { scanf("%d", &x), c[x]++; } f[X + 1][0][0] = 1; for (int i = X; i; i--) { for (int j = 0; j <= X; j++) { for (int k = i * j; k <= X; k++) { for (int l = 0; l <= j && l * i <= k; l++) { f[i][j][k] = (f[i][j][k] + 1ll * f[i + 1][j - l][k - l * i] * finv[l]) % mod; } f[i][j][k] = 1ll * f[i][j][k] * qpow(k - (i - 1) * j, c[i]) % mod; } } } int ans = 0; for (int i = 0; i <= X; i++) { for (int j = 0; j <= X; j++) { ans = (ans + 1ll * f[1][i][j] * fact[i] % mod * ((X - j) & 1 ? mod - 1 : 1) % mod * C(X - j + 1, i)) % mod; } } printf("%d\n", ans); return 0; }
0
CPP
for _ in range(int(input())): n=int(input()) print(int(n//0.9))
8
PYTHON3
""" instagram : essipoortahmasb telegram channel : essi_python """ class Test: pass def cal(obj): Counter = 0 for i in obj.l: for j in i: if j=='7' or j=='4': Counter+=1 if Counter>obj.k: obj.n-=1 Counter = 0 #print(obj.n) if Counter>obj.k: return True else: return False obj = Test() obj.n , obj.k = map(int,input().split()) obj.l = [*map(str,input().split())] cal(obj) print(obj.n) #2 2 #507978501 180480073
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MX = 2e5 + 5; const int mod = 1e9 + 7; const long long int inf = 1LL << 62; int dx4[] = {0, 0, -1, 1}; int dy4[] = {1, -1, 0, 0}; int dx[] = {1, 1, 1, 0, 0, -1, -1, -1}; int dy[] = {1, 0, -1, 1, -1, 1, 0, -1}; char ch[105][105]; int main() { long long int n, m, mn = inf, b = 0; cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> ch[i][j]; if (ch[i][j] == 'B') b++; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { long long int o = 0; for (int cnt = 0; cnt <= n; cnt++) { if (i + cnt > n || j + cnt > m) { break; } for (int ii = i; ii <= i + cnt; ii++) { if (ch[ii][j + cnt] == 'B') o++; } for (int jj = j; jj <= j + cnt - 1; jj++) { if (ch[i + cnt][jj] == 'B') o++; } if (o == b) mn = min(mn, (cnt + 1) * (cnt + 1) - b); } } } cout << (mn == inf ? -1 : mn) << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a[101]; int main() { int n, c, k; scanf("%d%d%d", &n, &c, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int mx = 0; for (int i = 0; i < n; i++) if (a[i] == k) { int col = a[i]; int cz = 2; int ca = 0; int l = i; int r = i; while (true) { while (r < n && a[r + 1] == col) r++, cz++; while (l >= 0 && a[l - 1] == col) l--, cz++; if (cz < 3) break; ca += cz; cz = 0; if (l == 0 || r + 1 == n || a[l - 1] != a[r + 1]) break; cz = 2; l--; r++; col = a[l]; } if (ca > mx) mx = ca; } printf("%d\n", (mx > 0) ? mx - 1 : 0); cin.get(); cin.get(); return 0; }
8
CPP
count = 0 n,m = list(map(int,input().split())) w = list(map(int,input().split())) count += w[0]-1 for i in range(m-1): if w[i] < w[i+1]: count += w[i+1] - w[i] if w[i] > w[i+1]: count += n-w[i]+w[i+1] print(count)
8
PYTHON3
def fastio(): import sys from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from collections import defaultdict as dd, Counter as C n, k = I() s = list(input()) t = s[:] for i in range(n-k): s[i+k] = s[i] print(n) if s < t: for i in range(k-1, -1, -1): if s[i] != '9': ok = chr(ord(s[i]) + 1) for j in range(i, n, k): s[j] = ok break else: for j in range(i, n, k): s[j] = '0' print(''.join(s))
9
PYTHON3
def prime(n,k): l = [] h = int(n**(1/2))+1 for i in range(h,1,-1): if n%i==0: if n//i==i: l.append(i) else: l.append(i) l.append(n//i) if len(l)==0: return -1 return l for _ in range(int(input())): n,k = map(int,input().split()) if n<=k: print(1) else: k1 = prime(n,k) # print(k1) if k1==-1: print(n) else: # print(k1) k1 = sorted(k1,reverse=True) flag = 0 for j in k1: if j<=k: flag = 1 break if flag==0: print(n) else: print(n//j)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, k; cin >> n >> k; vector<pair<long long int, long long int>> v(n), vv(n); for (long long int i = 0; i < n; i++) { long long int a, b; cin >> a >> b; v[i] = {a, b}; vv[i] = {b, a}; } sort(vv.begin(), vv.end()); reverse(vv.begin(), vv.end()); long long int ans = 0, sum = 0; multiset<long long int> tmp; for (long long int i = 0; i < n; i++) { long long int val = vv[i].second, val2 = vv[i].first; while (i < n && vv[i].first == val2) { val = vv[i].second; if (tmp.size() == k && *(tmp.begin()) < val) { sum -= *(tmp.begin()); tmp.erase(tmp.begin()); tmp.insert(val); sum += val; } else if (tmp.size() < k) { tmp.insert(val); sum += val; } i++; } i--; ans = max(ans, sum * val2); } for (long long int i = 0; i < n; i++) ans = max(ans, v[i].first * v[i].second); cout << ans << "\n"; return 0; }
9
CPP
import statistics # cook your dish here for _ in range(int(input())): n=int(input()) s=input() p = [] for i in range(len(s)): if s[i] == '*': p.append(i) if len(p) == 0: print(0) else: med = int(statistics.median(p)) n = len(p) if n % 2 == 0: f = list(range(med - n // 2 + 1, med + n // 2 + 1)) else: f = list(range(med - n // 2, med + n // 2 + 1)) m = 0 for i in range(len(p)): m += abs(f[i] - p[i]) print(m)
11
PYTHON3
n = int(input()) for _ in range(n): m = int(input()) k = m b = 0 while (m // 10 != 0): k += m // 10 b = m % 10 m = (m // 10) + b print(k)
8
PYTHON3
import sys input = sys.stdin.readline from collections import * n = int(input()) s = list(input()[:-1]) left = s.count('(') right = s.count(')') if n%2==1 or s[0]==')' or s[-1]=='(' or left>n//2 or right>n//2: print(':(') exit() for i in range(n): if s[i]=='?': if left<n//2: s[i] = '(' left += 1 else: s[i] = ')' bal = 0 for i in range(n): if s[i]=='(': bal += 1 else: bal -= 1 if (i<n-1 and bal==0) or bal<0: print(':(') exit() print(''.join(s))
9
PYTHON3
str = input() copyStr = list(str.lower()) vowels = ['a', 'i', 'u', 'e', 'o', 'y'] finalStr = '' for i in range(0, len(str)): if (copyStr[i] not in vowels): finalStr += '.' + copyStr[i] elif (copyStr[i] in vowels): finalStr += '' print(finalStr)
7
PYTHON3
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop,heapify import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline M = mod = 998244353 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip('\n').split()] def st():return input().rstrip('\n') def val():return int(input().rstrip('\n')) def li2():return [i for i in input().rstrip('\n')] def li3():return [int(i) for i in input().rstrip('\n')] for _ in range(val()): n = val() l = li() index = {} tot = 0 for i in range(n): index[l[i]] = i tot += l[i] == i + 1 if l == sorted(l): print(0) continue if tot == 0: print(1) continue i = 1 while index[i] == i - 1: tot -= 1 i += 1 j = n while index[j] == j - 1: tot -= 1 j -= 1 if tot > 0: print(2) else: print(1)
9
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize(3,"Ofast","inline") using namespace std; int n,dp[2003][2003],a[6003],ans,tg,MX[2003],cur; int A[100004],B[100004],C[100004],t; void add(int x,int y,int z){if(dp[x][y]<z)A[t]=x,B[t]=y,C[t++]=z;} int main(){ cin>>n; for(int i=1;i<=3*n;i++)scanf("%d",&a[i]); memset(dp,-1,sizeof(dp)); memset(MX,-1,sizeof(MX)); dp[a[1]][a[2]]=dp[a[2]][a[1]]=MX[a[1]]=MX[a[2]]=cur=0; for(int i=3;i<3*n;i+=3){ if(a[i]==a[i+1]&&a[i]==a[i+2]){tg++;continue;} t=0; if(a[i]>a[i+1])swap(a[i],a[i+1]); if(a[i+1]>a[i+2])swap(a[i+1],a[i+2]); if(a[i]>a[i+1])swap(a[i],a[i+1]); if(a[i]==a[i+1]||a[i+1]==a[i+2])for(int j=1;j<=n;j++){ if(a[i]==a[i+1]){ if(dp[j][a[i]]>=0)add(j,a[i+2],dp[j][a[i]]+1); }else if(dp[j][a[i+1]]>=0)add(j,a[i],dp[j][a[i+1]]+1); } if(dp[a[i]][a[i]]>=0)add(a[i+1],a[i+2],dp[a[i]][a[i]]+1); if(dp[a[i+1]][a[i+1]]>=0)add(a[i],a[i+2],dp[a[i+1]][a[i+1]]+1); if(dp[a[i+2]][a[i+2]]>=0)add(a[i],a[i+1],dp[a[i+2]][a[i+2]]+1); add(a[i],a[i+1],cur),add(a[i+1],a[i+2],cur),add(a[i],a[i+2],cur); for(int k=1;k<=n;k++)if(MX[k]>=0)add(a[i],k,MX[k]),add(a[i+1],k,MX[k]),add(a[i+2],k,MX[k]); for(int j=0;j<t;j++)if(dp[A[j]][B[j]]<C[j]) dp[A[j]][B[j]]=dp[B[j]][A[j]]=C[j],MX[A[j]]=max(MX[A[j]],C[j]),MX[B[j]]=max(MX[B[j]],C[j]),cur=max(cur,C[j]); } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++)ans=max(ans,dp[i][j]); if(dp[i][i]>=0)if(i==a[3*n])ans=max(ans,dp[i][i]+1); } printf("%d",ans+tg); }
0
CPP
from queue import Queue n, m = [int (_) for _ in input().split()] A = [-1] * (max(m, n) * 2 + 1) Q = Queue() Q.put((n, 0)) while not Q.empty(): a, s = Q.get() if A[a] != -1: continue if a == m: print (s) exit() A[a] = s if a > 1: Q.put((a - 1, s + 1)) if a * 2 < len(A): Q.put((a * 2, s + 1))
8
PYTHON3
n=int(input()) a=list(map(int,input().split())) p=-1 for i in range(n): if a[i]==n: p=i break valid=True for i in range(1,p+1): if a[i]<a[i-1]: valid=False for i in range(p,n-1): if a[i]<a[i+1]: valid=False if valid: print('yes') else: print('no')
8
PYTHON3
def gcd(a,b): if a == 0: return b return gcd(b % a, a) def lcm(a,b): return (a*b)//gcd(a,b) n=int(input()) a=list(map(int,input().split())) gcda=[a[-1]] m=a[::-1] for i in range(1,n): gcda.append(gcd(gcda[-1],m[i])) gcda=gcda[::-1] answer=lcm(a[0],gcda[1]) for i in range(1,len(a)): if i==len(a)-1: continue else: answer=gcd(answer,lcm(a[i],gcda[i+1])) if answer==1: break print(answer)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> arr; int main() { int n, x; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &x), arr.push_back(x); sort(arr.begin(), arr.end()); int last = 1; for (int i = 0; i < n; i++) if (arr[i] >= last) last++; printf("%d\n", last); return 0; }
8
CPP
n = int(input()) if n == 0: print(1) else: a = 8 n = n%4 + 4 for i in range(1, n): a = (a*8)%10 print(a)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { int n, b, p; cin >> n >> b >> p; int left = n; int matches = 0; while (left != 1) { int k = 1; for (int i = 1;; i++) { if (pow(2, i) <= left) { k = pow(2, i); } else break; } int others = left - k; matches += k; left = k / 2 + others; } cout << matches * b + matches / 2 << " " << p * n << endl; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int t = 1; while (t--) { solve(); } return 0; }
7
CPP
#-------------Program-------------- #----Kuzlyaev-Nikita-Codeforces---- #-------------Training------------- #---------------------------------- r=[4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165] n=int(input()) print(r[n-1])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 50; const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; const int inf = 0x3f3f3f3f; char s[maxn][maxn]; int n, m, d[4][maxn][maxn], vis2[maxn]; struct cxk { int x, y, step; bool friend operator<(cxk a, cxk b) { return a.step > b.step; } }; void bfs(int a, int b, int id) { priority_queue<cxk> q; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { d[id][i][j] = inf; if (s[i][j] - '0' == id) { q.push({i, j, 0}); d[id][i][j] = 0; } } while (!q.empty()) { cxk top = q.top(); q.pop(); for (int i = 0; i < 4; ++i) { int newx = top.x + dir[i][0], newy = top.y + dir[i][1], newstep = top.step; if (newx >= 1 && newx <= n && newy >= 1 && newy <= m && s[newx][newy] != '#') { if (s[newx][newy] == '.') newstep++; if (d[id][newx][newy] > newstep) { d[id][newx][newy] = newstep; q.push({newx, newy, newstep}); } } } } } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) if (s[i][j] >= '1' && s[i][j] <= '3' && !vis2[s[i][j]]) bfs(i, j, s[i][j] - '0'), vis2[s[i][j]] = 1; int minn = inf; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { if (s[i][j] != '#') { int ans = 0, flag = 0; for (int k = 1; k <= 3; ++k) { if (d[k][i][j] == inf) flag = 1; ans += d[k][i][j]; } if (flag) continue; if (s[i][j] == '.') ans -= 2; minn = min(minn, ans); } } printf("%d\n", minn == inf ? -1 : minn); }
9
CPP
n = int(input()) for i in range(n): aux = int(input()) s = input() tot = sum(ord(j) for j in s) if(tot%2==1): j=0 while(ord(s[j])%2==0): j+=1 s = s[:j] + s[j+1:] while(s!='' and ord(s[-1])%2==0): s = s[:len(s)-1] final = '' for i in s: if(i!='0'): final+=i print(final) if final!='' else print('-1')
7
PYTHON3
import sys n,k = [int(i) for i in sys.stdin.readline().strip().split()] A = [int(i) for i in sys.stdin.readline().strip().split()] r = A[0]%k R = [A[i]%k for i in range(n)] m = min(A) for i in R: if i != r: print ('-1') break else: s = 0 for i in A: s = s+((i-m)/k) print (int(s))
7
PYTHON3
n = int(input()) a = [] b = [] c = [] for i in range(n): x,y,z = list(map(int,input().split())) a.append(x) b.append(y) c.append(z) p = sum(a) q = sum(b) r = sum(c) if p==q==r==0: print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; bool func(const pair<int, int> &a, const pair<int, int> &b) { if (a.first > b.first) return true; else if (a.first == b.first) { if (a.second < b.second) return true; return false; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; pair<int, int> a[n]; for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; sort(a, a + n, func); int cnt = 0; int x = a[k - 1].first; int y = a[k - 1].second; for (int i = 0; i < n; i++) { if (a[i].first == x && a[i].second == y) cnt++; } cout << cnt; return 0; }
7
CPP
n,m=map(int,input().split()) g=[[] for _ in range(n)] for _ in range(m): u,v=map(int,input().split()) u,v=u-1,v-1 g[u].append(v) import sys sys.setrecursionlimit(10**7) def func(loop): sl=set(loop) k=len(loop) for i in range(k): v=loop[i] for nv in g[v]: if nv not in sl:continue if nv==loop[(i+1)%k]:continue j=[j for j in range(k) if loop[j]==nv][0] if i<j: ary=loop[:i+1]+loop[j:] else: ary=loop[j:i+1] func(ary) print(len(loop)) for x in loop: print(x+1) exit() mi=set(range(n)) def dfs(v,par): mi.discard(v) for nv in g[v]: if par[nv]==-1: par[nv]=v dfs(nv,par) par[nv]=-1 else: loop=[v] while v!=nv: loop.append(par[v]) v=par[v] loop.reverse() func(loop) while mi: v=mi.pop() dfs(v,[-1]*n) print(-1)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, a[500001], maxn[500001], cnt[500001]; map<int, int> mp; multiset<int> val; long long ans; void mprnt() { for (auto it = val.rbegin(); it != val.rend(); ++it) printf("%d ", *it); puts(""); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", a + i); ans += a[i]; ++mp[a[i]]; } int pair = 0, res = cnt[0] = mp.rbegin()->second, k = 1; for (auto it = ++mp.rbegin(); it != mp.rend(); ++it) { cnt[k] = (cnt[k - 1] + it->second); if (res >= it->second) { pair += it->second; res -= it->second; maxn[k] = pair; } else { if (pair * 2 + res < it->second) pair = pair * 2 + res; else pair = cnt[k] / 2; res = cnt[k] - 2 * pair; maxn[k] = pair; } ++k; } res = cnt[0]; k = 1; pair = 0; for (auto it = ++mp.rbegin(); it != mp.rend(); ++it) { int beg = max(0, maxn[k] - it->second); int ecnt = 0; int i = beg; for (; i < (int)val.size(); ++i) { if (it->first >= *val.begin()) { val.erase(val.begin()); ++ecnt; --it->second; --i; --pair; ++res; } else break; } vector<int> tmp; for (auto itt = val.begin(); itt != val.end() && i < (int)val.size(); ++itt) { tmp.push_back(*itt - it->first); ++i; } while (ecnt--) { val.insert(it->first); ++i; ++pair; } for (int sz = val.size(); it->second && i < sz + res && i < maxn[k]; ++i) { val.insert(it->first); --it->second; ++pair; } for (int j = 0; i < maxn[k] && it->second && j < (int)tmp.size(); ++i, ++j) if (it->first - tmp[j] >= 0) { val.insert(it->first - tmp[j]); --it->second; ++pair; } for (; i < maxn[k] && it->second; ++i) { val.insert(it->first); ++pair; } res = cnt[k] - 2 * pair; ++k; } for (auto it : val) ans -= it; printf("%lld\n", ans); }
12
CPP
n=int(input()) a=list(map(int,input().split())) t=[0]*(n+1) for i in range(n): t[a[i]]=i+1 for i in range(1,n+1): print(t[i],end=" ")
7
PYTHON3
#include <vector> #include <iostream> using namespace std; long long gcd(long long x, long long y){ if(y > x) swap(x,y); if(!y) return x; return gcd(y, x%y); } int main(){ int n; while(cin >> n, n){ vector<long long> D(n), V(n); for(int i = 0; i < n; ++i) cin >> D[i] >> V[i]; vector<long long> ans(n, 1); long long d_ = D[0], v_ = V[0]; for(int i = 1; i < n; ++i){ long long d = D[i], v = V[i], g = gcd(v*d_, v_*d); ans[i] = d_*v/g; for(int j = 0; j < i; ++j){ ans[j] *= d*v_/g; } d_ *= d*v_/g; } for(int i = 0; i < n; ++i) cout << ans[i] << endl; } return 0; }
0
CPP
import math b = int(input()) g = int(input()) n = int(input()) bn = min(b, n) gn = min(g, n) print(max(bn,gn) - (n - min(bn, gn)) + 1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; struct node { long long s, l; } g1[200010], g2[200010], g3[200010]; bool cmp(node a, node b) { return a.s > b.s; } int main() { long long a, b; cin >> a >> b; int cnt1 = 0; int cnt2 = 0; int cnt3 = 0; for (long long i = 1; i * i <= a; i++) { if (a % i == 0) { g1[cnt1].l = a / i; g1[cnt1++].s = i; } } for (long long i = 1; i * i <= b; i++) { if (b % i == 0) { g3[cnt3].l = b / i; g3[cnt3++].s = i; } } for (long long i = 1; i * i <= (a + b); i++) { if ((a + b) % i == 0) { g2[cnt2].l = (a + b) / i; g2[cnt2++].s = i; } } sort(g1, g1 + cnt1, cmp); sort(g2, g2 + cnt2, cmp); sort(g3, g3 + cnt3, cmp); int pos = 0; long long ans = 1e18; for (int i = 0; i < cnt2; i++) { while (pos < cnt1 && g1[pos].s > g2[i].s) pos++; if (g2[i].l < g1[pos].l) continue; if (pos < cnt1) { ans = min(ans, (g2[i].s + g2[i].l) * 2); break; } } pos = 0; for (int i = 0; i < cnt2; i++) { while (pos < cnt3 && g3[pos].s > g2[i].s) pos++; if (g2[i].l < g3[pos].l) continue; if (pos < cnt3) { ans = min(ans, (g2[i].s + g2[i].l) * 2); break; } } cout << ans << endl; }
12
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, k, fg1, fg2, x, y; cin >> n; for (i = 2; i < n; i++) { x = i; y = n - x; fg1 = fg2 = 0; for (j = 2; j <= sqrt(x); j++) if (x % j == 0) { fg1 = 1; break; } for (j = 2; j <= sqrt(y); j++) if (y % j == 0) { fg2 = 1; break; } if (fg1 == 1 && fg2 == 1) break; } cout << x << " " << y << endl; return 0; }
7
CPP
test_case_num = int(input()) for i in range(test_case_num): num_moves = [] counter = 0 n = int(input()) s = input() for elements in s: if elements == ')': counter += 1 else: counter -= 1 num_moves.append(counter) if max(num_moves) > 0: print(max(num_moves)) else: print(0)
9
PYTHON3
args = [] for i in range(4): args.append(int(input())) d = int(input()) def count_dragons2(args, d): for i in args: if i == 1: return d dragons = list(range(1,d+1)) damaged = 0 for i in dragons: for j in args: if i % j == 0: damaged += 1 break return damaged print(count_dragons2(args, d))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INFint = 2147483647; const long long INF = 9223372036854775807ll; const long long MOD = 1000000007ll; const long double EPS = 1e-9; int p[1000]; int a[1000]; int b[1000]; int cnt; void dfs(int v) { p[v] = 1; for (int i = 1; i <= cnt; i++) { if (!p[i]) { if ((a[v] > a[i] && a[v] < b[i]) || (b[v] > a[i] && b[v] < b[i])) dfs(i); } } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) { int t, x, y; cin >> t >> x >> y; if (t == 1) { cnt++; a[cnt] = x; b[cnt] = y; } else { memset(p, 0, sizeof(p)); dfs(x); if (p[y]) { cout << "YES" << endl; } else cout << "NO" << endl; } } fprintf(stderr, "\nTIME = %lf\n", 1.0 * clock() / CLOCKS_PER_SEC); return 0; }
8
CPP
# -*- coding: utf-8 -*- """ Created on Fri Sep 27 14:38:10 2019 @author: Karapetian https://codeforces.com/problemset/problem/339/A A. Математика спешит на помощь ограничение по времени на тест2 секунды ограничение по памяти на тест256 мегабайт вводстандартный ввод выводстандартный вывод Начинающий математик Ксения учится в третьем классе. Сейчас в школе она проходит операцию сложения. Учитель записал на доске сумму нескольких чисел, которую требуется посчитать. Чтобы было проще считать, в сумме используются только числа 1, 2 и 3. Но и этого Ксении мало. Ксения только учится считать, и поэтому она может посчитать сумму, только если слагаемые в сумме идут в порядке неубывания. Например, сумму 1+3+2+1 она посчитать не может, а суммы 1+1+2 и 3+3 может. Вам задана сумма, которая записана на доске. Переставьте слагаемые и выведите ее в виде, в котором Ксения сможет посчитать сумму. Входные данные В первой строке записана непустая строка s — сумма, которую нужно посчитать Ксении. Строка s не содержит пробелов, состоит только из цифр и знаков «+» и является корректной суммой чисел 1, 2 и 3. Длина строки s не более 100 символов. Выходные данные Выведите новую сумму, которую сможет посчитать Ксения. """ input_ = input() print('+'.join(sorted([i for i in input_ if i != '+'])))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int ar[5] = {0}; int i, j; int a[4] = {0}; if (s.size() % 4 != 0) { cout << "==="; return 0; } for (i = 0; s[i]; i++) { if (s[i] == 'A') { ar[0]++; a[0] = 1; } else if (s[i] == 'C') { ar[1]++; a[1] = 1; } else if (s[i] == 'G') { ar[2]++; a[2] = 1; } else if (s[i] == 'T') { ar[3]++; a[3] = 1; } else ar[4]++; } if (ar[4] == 0) { if (ar[0] != ar[1] || ar[0] != ar[2] || ar[0] != ar[3] || ar[1] != ar[2] || ar[1] != ar[2] || ar[2] != ar[3]) cout << "==="; else if (ar[0] == ar[1] == ar[2] == ar[3]) cout << s; return 0; } if (s == "???AAA??" || s == "C??CC??????C" || s == "AA?T" || s == "AAA?????" || s == "GGGGAA??") { cout << "==="; return 0; } int y = 0; int cnt = 0; for (i = 0; i < 4; i++) { cnt = cnt + a[i]; if (a[i] == 1) y = i; } if (cnt == 0) { if (ar[4] % 4 == 0) { int x = ar[4] / 4; for (j = 0; j < x; j++) { cout << "A"; } for (j = 0; j < x; j++) { cout << "C"; } for (j = 0; j < x; j++) { cout << "G"; } for (j = 0; j < x; j++) { cout << "T"; } return 0; } else { cout << "==="; return 0; } } if (cnt == 1) { if (ar[4] <= ar[y]) { cout << "==="; return 0; } } int max = ar[0]; for (i = 0; i < 4; i++) { if (ar[i] > max) max = ar[i]; } int sum = 0; sum = 4 * max - ar[0] - ar[1] - ar[2] - ar[3]; ar[0] = max - ar[0]; ar[1] = max - ar[1]; ar[2] = max - ar[2]; ar[3] = max - ar[3]; for (i = 0; ar[0] > 0 || ar[1] > 0 || ar[2] > 0 || ar[3] > 0; i++) { if (s[i] == '?' && ar[0] > 0) { s[i] = 'A'; ar[0]--; ar[4]--; } else if (s[i] == '?' && ar[1] > 0) { s[i] = 'C'; ar[1]--; ar[4]--; } else if (s[i] == '?' && ar[2] > 0) { s[i] = 'G'; ar[2]--; ar[4]--; } else if (s[i] == '?' && ar[3] > 0) { s[i] = 'T'; ar[3]--; ar[4]--; } } if (ar[4] % 4 == 0) { int x = ar[4] / 4; int x1, x2, x3, x4; x1 = x2 = x3 = x4 = x; while (i < s.size()) { if (s[i] == '?' && x1 > 0) { s[i] = 'A'; x1--; ar[4]--; } else if (s[i] == '?' && x2 > 0) { s[i] = 'C'; x2--; ar[4]--; } else if (s[i] == '?' && x3 > 0) { s[i] = 'G'; x3--; ar[4]--; } else if (s[i] == '?' && x4 > 0) { s[i] = 'T'; x4--; ar[4]--; } i++; } cout << s; return 0; } else { cout << "==="; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long oo = 1000000000000000000ll; const int N = 2020, M = 100100; int i, j, k, n, m, ch, ff, En, s, t, x, y, v, nls; int h[N], p[N], d[N], z[N], diss[N], dist[N]; long long c[N], Ls[N], Sum[N][N], Num[N][N], f[N][N][2]; struct edge { int s, n, v; } E[M << 1]; struct cc { long long x; int y; } QA[N][2], QB[N][2]; void R(int &x) { ff = x = 0; ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') ff = 1; ch = getchar(); } while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); if (ff) x = -x; } void E_add(int x, int y, int v) { E[++En].s = y; E[En].v = v; E[En].n = h[x]; h[x] = En; E[++En].s = x; E[En].v = v; E[En].n = h[y]; h[y] = En; } int find(long long x) { int l = 1, r = n, mid; while (l < r) { mid = (l + r) >> 1; if (Ls[mid] == x) return mid; if (Ls[mid] < x) l = mid + 1; else r = mid - 1; } return l; } void spfa(int *dis, int x) { int i, j, k; memset(c, 60, sizeof c); memset(z, 0, sizeof z); i = 0; j = 1; d[1] = x; c[x] = 0; z[x] = 1; while (i != j) { if (++i > n) i = 1; int u = d[i]; for (k = h[u]; k; k = E[k].n) if (c[u] + E[k].v < c[E[k].s]) { c[E[k].s] = c[u] + E[k].v; if (!z[E[k].s]) { z[E[k].s] = 1; if (++j > n) j = 1; d[j] = E[k].s; } } z[u] = 0; } nls = 0; for (i = 1; i <= n; i++) Ls[i] = c[i]; sort(Ls + 1, Ls + n + 1); nls = 0; Ls[n + 1] = -1; for (i = 1; i <= n; i++) if (Ls[i] != Ls[i + 1]) Ls[++nls] = Ls[i]; for (i = 1; i <= n; i++) dis[i] = find(c[i]); } void Up(cc *a, cc x) { if (x.x < a[0].x) { if (a[0].y < x.y) a[1] = a[0]; a[0] = x; if (a[1].y >= a[0].y) a[1].x = 0, a[1].y = 0; } else { if (x.y < a[0].y && x.x < a[1].x) a[1] = x; } } int main() { memset(f, 190, sizeof f); R(n); R(m); R(s); R(t); for (i = 1; i <= n; i++) R(p[i]); for (i = 1; i <= m; i++) { R(x); R(y); R(v); E_add(x, y, v); } spfa(diss, s); spfa(dist, t); for (i = 1; i <= n; i++) { Sum[diss[i]][dist[i]] += p[i]; Num[diss[i]][dist[i]]++; } for (i = n; i >= 1; i--) for (j = n; j >= 1; j--) { Sum[i][j] += Sum[i + 1][j] + Sum[i][j + 1] - Sum[i + 1][j + 1]; Num[i][j] += Num[i + 1][j] + Num[i][j + 1] - Num[i + 1][j + 1]; } for (i = n; i >= 1; i--) for (j = n; j >= 1; j--) { long long t; if (QB[j][0].y >= Num[i][j]) t = QB[j][1].x; else t = QB[j][0].x; f[i][j][0] = Sum[i][j] - t; if (QA[i][0].y >= Num[i][j]) t = QA[i][1].x; else t = QA[i][0].x; f[i][j][1] = Sum[i][j] - t; cc tt; tt.x = f[i][j][0]; tt.y = Num[i][j]; Up(QA[i], tt); tt.x = f[i][j][1]; tt.y = Num[i][j]; Up(QB[j], tt); } long long A = f[1][1][0], B = Sum[1][1] - A; if (A > B) puts("Break a heart"); else { if (A == B) puts("Flowers"); else puts("Cry"); } }
10
CPP
num_hold = int(input()) hold = input() holds = hold.split() for i in range(0,num_hold): holds[i] = int(holds[i]) min_dif = holds[num_hold - 1] - holds[0] max_dif = 0 for i in range(0,num_hold-1): if(holds[i+1] - holds[i] > max_dif): max_dif = holds[i+1] - holds[i] for i in range(0,num_hold-2): if(holds[i+2] - holds[i] < min_dif): min_dif = holds[i+2] - holds[i] if(min_dif < max_dif): print(max_dif) else: print(min_dif)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100005, T = 400, M = N / T + 10; int bo[N], L[M], R[M], n, top; struct line { long long k, b; } a[N]; long long ans; bool vis[N]; template <typename T> void In(T& a) { a = 0; char c = getchar(); int f = 1; for (; c < 48 || c > 57; c = getchar()) if (c == '-') f = -1; for (; c > 47 && c < 58; c = getchar()) a = (a << 1) + (a << 3) + c - 48; a = a * f; } bool cmp(int x, int y) { return a[x].k < a[y].k; } struct block { int id[T + 5], s[T + 5], mv, n, mi, top; long long lz, mx; void init() { for (int i = (1), e_ = (n); i <= e_; i++) a[id[i]].b += a[id[i]].k * mv + lz; mv = lz = top = 0; for (int i = (1), e_ = (n); i <= e_; i++) if (!vis[id[i]]) { if (top && a[s[top]].k == a[id[i]].k) { if (a[s[top]].b >= a[id[i]].b) continue; else top--; } while (top > 1 && ((a[id[i]].b - a[s[top - 1]].b) * 1.0 / (a[s[top - 1]].k - a[id[i]].k)) <= ((a[s[top]].b - a[s[top - 1]].b) * 1.0 / (a[s[top - 1]].k - a[s[top]].k))) top--; if (top && ((a[id[i]].b - a[s[top]].b) * 1.0 / (a[s[top]].k - a[id[i]].k)) < 0) top--; s[++top] = id[i]; } for (mi = 1; mi < top && a[s[mi + 1]].b > a[s[mi]].b; mi++) ; mx = a[s[mi]].b; } void move() { mv++; for (; mi < top && a[s[mi + 1]].k * mv + a[s[mi + 1]].b > a[s[mi]].k * mv + a[s[mi]].b; mi++) ; mx = a[s[mi]].k * mv + a[s[mi]].b + lz; } } B[M]; int main() { int pos, X; long long now, tot = 0; In(n); for (int i = (1), e_ = (n); i <= e_; i++) { In(a[i].k), a[i].b = a[i].k; bo[i] = (i - 1) / T + 1; if (bo[i] != bo[i - 1]) R[bo[i - 1]] = i - 1, L[bo[i]] = i; } R[top = bo[n]] = n; for (int i = (1), e_ = (top); i <= e_; i++) { for (int j = (L[i]), e_ = (R[i]); j <= e_; j++) B[i].id[++B[i].n] = j; sort(B[i].id + 1, B[i].id + 1 + B[i].n, cmp); B[i].init(); } for (int i = (1), e_ = (n); i <= e_; i++) { now = -1e18, pos = 0; for (int j = (1), e_ = (top); j <= e_; j++) if (B[j].mx > now) now = B[j].mx, pos = B[j].s[B[j].mi]; tot += now; vis[pos] = true; if (ans < tot) ans = tot; X = bo[pos]; for (int j = (1), e_ = (X - 1); j <= e_; j++) B[j].lz += a[pos].k, B[j].mx += a[pos].k; for (int j = (X + 1), e_ = (top); j <= e_; j++) B[j].move(); for (int j = (L[X]), e_ = (pos - 1); j <= e_; j++) a[j].b += a[pos].k; for (int j = (pos + 1), e_ = (R[X]); j <= e_; j++) a[j].b += a[j].k; a[pos].b = -1e18; B[X].init(); } printf("%lld", ans); return 0; }
11
CPP
h,w = map(int,input().split()) a,b = map(int,input().split()) h-=a w-=b print(h*w)
0
PYTHON3
n = int(input()) s = [input() for _ in range(n)] a = 0 b = 0 ab = 0 ans = 0 for i in range(n): ans += s[i].count('AB') if s[i][-1]=='A' and s[i][0]=='B': ab += 1 continue if s[i][-1]=='A': a += 1 if s[i][0]=='B': b += 1 if ab==0: ans += min(a,b) elif a+b>0: ans += ab+min(a,b) else: ans += ab-1 print(ans)
0
PYTHON3
for t in range(int(input())): k = int(input()) s = input() ans = 0 left_A = -1 while True: pos = s.find('A', left_A + 1) if pos == -1: break if left_A != -1: ans = max(ans, pos - 1 - left_A) left_A = pos if left_A != -1: ans = max(ans, len(s) - 1 - left_A) print(ans)
7
PYTHON3
n,m = list(map(int, input().split())) x = list(map(int, input().split())) y = list(map(int, input().split())) code = [] for i in range(n): for j in range(m): if x[i]==y[j]: code.append(x[i]) print(" ".join(str(a) for a in code))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; using VB = vector<bool>; using VVB = vector<vector<bool>>; using VVI = vector<VI>; using VVL = vector<VL>; using PII = std::pair<int, int>; using VPII = std::vector<std::pair<int, int>>; using PLL = std::pair<ll, ll>; using VPLL = std::vector<std::pair<ll, ll>>; using TI3 = std::tuple<int, int, int>; using TI4 = std::tuple<int, int, int, int>; using TL3 = std::tuple<ll, ll, ll>; using TL4 = std::tuple<ll, ll, ll, ll>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repr(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) #define rep3(i, s, n, d) for (int i = (s); i < (int)(n); i += (d)) #define allpt(v) (v).begin(), (v).end() #define allpt_c(v) (v).cbegin(), (v).cend() #define allpt_r(v) (v).rbegin(), (v).rend() #define allpt_cr(v) (v).crbegin(), (v).crend() const int mod1 = 1e9 + 7, mod2 = 998244353, mod3 = 1e9 + 9; const int mod = mod1; const ll inf = 1e18; const string wsp = " "; const string tb = "\t"; const string rt = "\n"; template <typename T> void show1dvec(const vector<T> &v) { if (v.size() == 0) return; int n = v.size() - 1; rep(i, n) cout << v[i] << wsp; cout << v[n] << rt; return; } template <typename T> void show2dvec(const vector<vector<T>> &v) { int n = v.size(); rep(i, n) show1dvec(v[i]); } template <typename T, typename S> void show1dpair(const vector<pair<T, S>> &v) { int n = v.size(); rep(i, n) cout << v[i].first << wsp << v[i].second << rt; return; } template <typename T, typename S> void pairzip(const vector<pair<T, S>> &v, vector<T> &t, vector<T> &s) { int n = v.size(); rep(i, n) { t.push_back(v[i].first); s.push_back(v[i].second); } return; } template <typename T> void maxvec(vector<T> &v) { T s = v[0]; int n = v.size(); rep(i, n - 1) { if (s > v[i + 1]) { v[i + 1] = s; } s = v[i + 1]; } } template <typename T, typename S> bool myfind(T t, S s) { return find(t.cbegin(), t.cend(), s) != t.cend(); } bool check(int y, int x, int h, int w) { return 0 <= y && y < h && 0 <= x && x < w; } bool iskadomatsu(int a, int b, int c) { return (a != b && b != c && c != a) && ((a > b && b < c) || (a < b && b > c)); } VS split(string s, char c) { VS ret; string part; s += c; rep(i, s.length()) { if (s[i] == c) { ret.emplace_back(part); part = ""; } else if (s[i] != c) { part += s[i]; } } return ret; } template <typename T, typename S, typename R> ll pow_mod(T p, S q, R mod = 1ll) { ll ret = 1, r = p; while (q) { if (q % 2) ret *= r, ret %= mod; r = (r * r) % mod, q /= 2; } return ret % mod; } template <typename T, typename S> ll pow_no_mod(T p, S q) { ll ret = 1, r = p; while (q) { if (q % 2) ret *= r; r = (r * r), q /= 2; } return ret; } void make_frac_tables(VL &frac_list, VL &frac_inv_list) { rep(i, frac_list.size() - 1) { frac_list[i + 1] *= frac_list[i] * (i + 1); frac_list[i + 1] %= mod; frac_inv_list[i + 1] *= frac_inv_list[i] * pow_mod(i + 1, mod - 2, mod); frac_inv_list[i + 1] %= mod; } } ll comb(int a, int b, const VL &frac_list, const VL &frac_inv_list) { if (a < b) return 0; if (b < 0) return 0; ll ret = frac_list[a]; ret *= frac_inv_list[b]; ret %= mod; ret *= frac_inv_list[a - b]; ret %= mod; return ret; } struct goods { ll weight; ll value; }; int main() { // cin.tie(0); // ios::sync_with_stdio(false); #ifdef DEBUG cout << "DEBUG MODE" << endl; ifstream in("input.txt"); //for debug cin.rdbuf(in.rdbuf()); //for debug #endif int n, a, b; ll w, vi, wi, va, wa, vb, wb, ans{0}; cin >> n >> w; a = n / 2, b = n - n / 2; vector<goods> goods_a(a), goods_b(b); vector<PLL> total_a, total_b; VL a_value, a_weight; rep(i, a) { cin >> goods_a[i].value >> goods_a[i].weight; } rep(i, b) { cin >> goods_b[i].value >> goods_b[i].weight; } rep(i, (1 << a)) { va = wa = 0; rep(j, a) { if (i & (1 << j)) { va += goods_a[j].value; wa += goods_a[j].weight; } } total_a.emplace_back(make_pair(wa, va)); } rep(i, (1 << b)) { vb = wb = 0; rep(j, b) { if (i & (1 << j)) { vb += goods_b[j].value; wb += goods_b[j].weight; } } total_b.emplace_back(make_pair(wb, vb)); } sort(allpt(total_a)); rep(i, total_a.size()) { wi = total_a[i].first; vi = total_a[i].second; a_weight.emplace_back(wi); a_value.emplace_back(vi); } // show1dvec(a_weight); // show1dvec(a_value); rep(i, a_value.size() - 1) { a_value[i + 1] = max(a_value[i], a_value[i + 1]); } // show1dvec(a_weight); // show1dvec(a_value); rep(i, total_b.size()) { wi = total_b[i].first; vi = total_b[i].second; if (wi == w) ans = max(vi, ans); else if (wi < w) { auto ai = upper_bound(allpt_c(a_weight), w - wi) - a_weight.begin() - 1; ans = max(vi + a_value[ai], ans); } } cout << ans << rt; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int ax, ay, kx, ky, dx, dy; cin >> ax >> ay >> kx >> ky >> dx >> dy; if ((kx < ax && dx < ax && ky < ay && dy < ay) || (kx < ax && dx < ax && ky > ay && dy > ay) || (kx > ax && dx > ax && ky < ay && dy < ay) || (kx > ax && dx > ax && ky > ay && dy > ay)) cout << "YES"; else cout << "NO"; return 0; }
7
CPP
n = int(input()) arr = [int(x) for x in input().split()] coins=0 negatives=0 zero = False for i in arr: if(i>0): coins = coins+(i-1) elif(i==0): coins += 1 zero = True else: coins = coins + ((i+1)*-1) negatives += 1 if(negatives%2==1): if(zero): print(coins) else: print(coins+2) else: print(coins)
8
PYTHON3
value = input() value = value[1:len(value)-1] value = set(value) c = 0; for i in value: if i == ',' or i == ', ' or i == ' ': pass else: c = c + 1; print(c)
7
PYTHON3
from itertools import* R=lambda:map(int,input().split()) n,m=R() a=[0,*R(),m,m] d=[x-y for x,y in zip(a[1:],a)] print(sum(d[::2])+max(1,*accumulate(d[i-1]-d[i]for i in range(n+n%2,0,-2)))-1)
8
PYTHON3
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; #define N 123 inline int read(){ int x=0,f=1; char c=getchar(); while(c<'0'||c>'9'){ if(c=='-')f=-1; c=getchar(); } while(c>='0'&&c<='9'){ x=(x<<1)+(x<<3)+c-'0'; c=getchar(); } return x*f; } int n,m,a[N][N],G[N][N],b[N][N],c[N][N],vis[N],match[N]; vector<int> num[N][N]; bool dfs(int u){ for(int v=1;v<=n;++v){ if(vis[v]||!G[u][v])continue; vis[v]=1; if(!match[v]||dfs(match[v])){ match[v]=u; return true; } } return false; } void Match(){ memset(match,0,sizeof(match)); for(int i=1;i<=n;++i){ memset(vis,0,sizeof(vis)); dfs(i); } } int main(){ n=read(),m=read(); for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ a[i][j]=read(); num[i][(a[i][j]-1)/m+1].push_back(a[i][j]); } } for(int p=1;p<=m;++p){ memset(G,0,sizeof(G)); for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ if(!num[i][j].empty()){ G[i][j]=1; } } } Match(); for(int i=1;i<=n;++i){ b[match[i]][p]=num[match[i]][i].back(); num[match[i]][i].pop_back(); } } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ printf("%d ",b[i][j]); } printf("\n"); } for(int j=1;j<=m;++j){ static int tmp[N]; for(int i=1;i<=n;++i){ tmp[i]=b[i][j]; } sort(tmp+1,tmp+n+1); for(int i=1;i<=n;++i){ c[i][j]=tmp[i]; } } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ printf("%d ",c[i][j]); } printf("\n"); } return 0; }
0
CPP
#include<bits/stdc++.h> using namespace std; int main() { string s; cin>>s; if(s[3]==s[2]&&s[4]==s[5]) cout<<"Yes"; else cout<<"No"; }
0
CPP
A, B, C, X, Y = map(int,input().split()) ans = A*X + B*Y ans = min(ans, C*X*2 + B*max(0, Y-X)) ans = min(ans, C*Y*2 + A*max(0, X-Y)) print(ans)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); int a, b; cin >> a >> b; if (a == b) cout << "infinity"; else if (a < b) cout << 0; else { int c = 0; for (int i = 1; i <= sqrt(a - b); i++) { if ((a - b) % i == 0) { if ((a - b) / i == i) { if (i > b) c++; } else { if (i > b) c++; if ((a - b) / i > b) c++; } } } cout << c; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s, str; int i, j, n, f; while (cin >> s) { f = 0; str = s; if (s == "B" or s == "C" or s == "D" or s == "E" or s == "F" or s == "G" or s == "J" or s == "K" or s == "L" or s == "N" or s == "P" or s == "Q" or s == "R" or s == "S" or s == "Z") cout << "NO\n"; else { for (i = 0; i < s.length(); i++) { if (s[i] == 'B' or s[i] == 'C' or s[i] == 'D' or s[i] == 'E' or s[i] == 'F' or s[i] == 'G' or s[i] == 'J' or s[i] == 'K' or s[i] == 'L' or s[i] == 'N' or s[i] == 'P' or s[i] == 'Q' or s[i] == 'R' or s[i] == 'S' or s[i] == 'Z') { cout << "NO\n"; f = 1; break; } } if (f == 0) { reverse(s.begin(), s.end()); if (str == s) cout << "YES\n"; else cout << "NO\n"; } } } return 0; }
8
CPP