solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
t = int(input()) for i in range(0, t): n = int(input()) if(n < 3): print(0) continue if( n % 2 == 1): print(int(n / 2)) continue else: print(int(n/2 - 1)) continue
7
PYTHON3
n, m = map(int, input().strip().split()) a = list(map(int, input().strip().split())) dp = [None] * (n+1) dp[0] = 0 x = (0,2,5,5,4,5,6,3,7,6) for i in range(2, n+1): for v in a: if i-x[v] >= 0 and dp[i-x[v]] != None: if not dp[i]: dp[i] = dp[i-x[v]]*10+v else: dp[i] = max(dp[i], dp[i-x[v]]*10+v) print(dp[n])
0
PYTHON3
a,b = input().split(' ') a1=list(map(int,input().split(' '))) b1=list(map(int,input().split(' '))) c =[] for i in a1: for j in b1: c.append([i*j,i]) c.sort() a1.remove(c[-1][1]) c=[] for i in a1: for j in b1: c.append(i*j) c.sort() print(c[-1])
7
PYTHON3
s = input() if len(s) >= 4 and s[:4] == "YAKI": print("Yes") else: print("No");
0
PYTHON3
n=list(input()) i=0 while(i<len(n)): if(n[i].lower()=='a' or n[i].lower()=='e' or n[i].lower()=='i' or n[i].lower()=='o' or n[i].lower()=='u' or n[i].lower()=='y'): n.remove(n[i]) i=i-1 elif(n[i].isalpha()): n.insert(i,'.') i=i+1 i=i+1 print((''.join(n)).lower())
7
PYTHON3
#include <bits/stdc++.h> using namespace std; struct Point { double X, Y, Z; void Get() { scanf("%lf%lf%lf", &X, &Y, &Z); } }; Point operator-(Point A, Point B) { return (Point){A.X - B.X, A.Y - B.Y, A.Z - B.Z}; } double operator*(Point A, Point B) { return A.X * B.X + A.Y * B.Y + A.Z * B.Z; } const double inf = 1e9; const double eps = 1e-6; double Solve(Point A, Point V, double R) { double a = V * V, b = A * V * 2, c = A * A - R * R; double Delta = b * b - 4 * a * c; if (Delta < -eps) return inf; Delta = sqrt(fabs(Delta)); double T0 = (-b + Delta) / (2 * a), T1 = (-b - Delta) / (2 * a); return (T0 < 0) ? inf : ((T1 < 0) ? T0 : T1); } int main() { Point O, V; O.Get(); V.Get(); double R, Ans = inf; scanf("%lf", &R); int N; scanf("%d", &N); while (N--) { Point _O; _O.Get(); double r; scanf("%lf", &r); Ans = min(Ans, Solve(O - _O, V, R + r)); int M; scanf("%d", &M); while (M--) { Point Delta; Delta.Get(); Ans = min(Ans, Solve(O - _O - Delta, V, R)); } } if (Ans == inf) printf("-1\n"); else printf("%0.10lf\n", Ans); return 0; }
10
CPP
#include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int main() { for(;;){ string s; cin >> s; if(s == "#") return 0; int n = s.size(); int i = 0; bool ret = false; while(i < n){ vector<bool> tilde(3, false); vector<string> t(3); bool b = true; for(int j=0; j<3; ++j){ ++ i; if(s[i] == '~'){ tilde[j] = true; ++ i; } while(s[i] != '&' && s[i] != ')'){ t[j] += s[i]; ++ i; } for(int k=0; k<j; ++k){ if(t[j] == t[k] && tilde[j] != tilde[k]) b = false; } } if(b){ ret = true; break; } i += 2; } if(ret) cout << "yes" << endl; else cout << "no" << endl; } }
0
CPP
from collections import Counter,defaultdict,deque #alph = 'abcdefghijklmnopqrstuvwxyz' #from math import factorial as fact #import math #tt = 1#int(input()) #total=0 #n = int(input()) #n,m,k = [int(x) for x in input().split()] #n = int(input()) n,m = [int(x) for x in input().split()] for i in range(m): a = input() print('01'*(n//2)+'0'*(n%2==1))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int read() { char x = getchar(); int u = 0, fg = 0; while (!isdigit(x)) { if (x == '-') fg = 1; x = getchar(); } while (isdigit(x)) { u = (u << 3) + (u << 1) + (x ^ 48); x = getchar(); } return fg ? -u : u; } const int maxx = 2e5 + 10, inf = 1e9; int n, b, q, s, t, be[maxx], ne[maxx], to[maxx], w[maxx], num[maxx][5], now[maxx], dep[maxx], e = 1, cnt = 5; pair<int, int> a[maxx]; void add(int x, int y, int z) { to[++e] = y; ne[e] = be[x]; be[x] = e; w[e] = z; } bool bfs() { queue<int> q; q.push(s); for (int i = 1; i <= cnt; ++i) dep[i] = 0; dep[s] = 1; dep[t] = 0; now[s] = be[s]; while (!q.empty()) { int id = q.front(); q.pop(); for (int i = be[id]; i; i = ne[i]) { int go = to[i]; if (!dep[go] && w[i] > 0) { dep[go] = dep[id] + 1; now[go] = be[go]; q.push(go); } } } return dep[t] > 0; } int dfs(int id, int mini) { if (id == t || !mini) return mini; int flow, ret = 0; for (int &i = now[id]; i; i = ne[i]) { int go = to[i]; if (w[i] > 0 && dep[go] == dep[id] + 1 && (flow = dfs(go, min(w[i], mini)))) { if (!flow) dep[go] = inf; w[i] -= flow; w[i ^ 1] += flow; ret += flow; mini -= flow; } if (!mini) return ret; } return ret; } int dinic() { int ans = 0, tmp; while (bfs()) { tmp = dfs(s, inf); if (!tmp) break; ans += tmp; } return ans; } int main() { n = read(); b = read(); q = read(); for (int i = 1; i <= b; ++i) { for (int j = 0; j <= 4; ++j) num[i][j] = num[i - 1][j]; ++num[i][i % 5]; } for (int i = 1; i <= q; ++i) { a[i].first = read(); a[i].second = read(); } a[q + 1].first = b; a[q + 1].second = n; sort(a + 1, a + q + 2); s = 99999; t = 100000; for (int i = 1; i <= 5; ++i) { add(s, i, n / 5); add(i, s, 0); } for (int i = 1; i <= q + 1; ++i) { int C = a[i].second - a[i - 1].second, l = a[i - 1].first + 1, r = a[i].first; if (C > r - l + 1 || C < 0) { puts("unfair"); return 0; } add(++cnt, t, C); add(t, cnt, 0); for (int k = 0; k <= 4; ++k) { add(k + 1, cnt, num[r][k] - num[l - 1][k]); add(cnt, k + 1, 0); } } if (dinic() == n) puts("fair"); else puts("unfair"); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; template <class T> void Min(T &x, T y) { if (y < x) x = y; } template <class T> void Max(T &x, T y) { if (y > x) x = y; } const int maxn = 100010; const int mod = 1000000007; int c[1010][2010]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i <= 1000; i++) { c[i][0] = 1; c[i][i] = 1; for (int j = 1; j < i; j++) { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; } } printf("%I64d\n", (long long)c[n - 1][2 * k] * c[m - 1][2 * k] % mod); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; #ifdef LOCAL #define Gene template< class #define Rics printer& operator, Gene c> struct rge{c b, e;}; Gene c> rge<c> range(c i, c j){ return {i, j};} struct printer{ ~printer(){cerr<<endl;} Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;} Rics(string x){cerr<<x;return *this;} Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";} Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));} Gene c >Rics(rge<c> x){ *this,"["; for(auto it = x.b; it != x.e; ++it) *this,(it==x.b?"":", "),*it; return *this,"]";} }; #define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer() #define dbg(x) "[",#x,": ",(x),"] " #define tham getchar() #endif #define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define eq(x, y) (fabs((x)-(y))<error) #define bt(i) (1LL<<(i)) mt19937_64 rng((unsigned)chrono::system_clock::now().time_since_epoch().count()); //const ll mod = ; //const ld error = ; const ld PI = acosl(-1); //const int/ll inf = ; const int nmax = 1e6+5; ///=========================================== template ======================================================= map<ll, ll> allpow; bool composite[nmax]; void precal(){ for(int i = 2; i<nmax; i++){ if(composite[i]) continue; ll tmp = i; tmp *= i; do{ tmp *= i; allpow[tmp] = i; }while(tmp < ll(1e18)/i); for(int j = i+i; j<nmax; j+=i){ composite[j] = true; } } } //using floating points ull modmult(ull a, ull b, ull m){ if(a >= m) a %= m; if(b >= m) b %= m; ull q = ull(ld(a)*b/m); ll r = ll(a*b-q*m) % ll(m); return r < 0? r+m: r; } ll modexpo(ll x, ll n, ll m){ x %= m; ll ret = 1; while(n){ if(n & 1LL) ret = modmult(ret, x, m); x = modmult(x, x, m); n >>= 1; } return ret; } ll SPRP[7] = {2LL, 325LL, 9375LL, 28178LL, 450775LL, 9780504LL, 1795265022LL}; bool RabinMiller(ll p, int t = 7) //t = 7 for SPRP base { if(p < 4) return (p > 1); if(!(p & 1LL)) return false; ll x = p - 1; int e = __builtin_ctzll(x); x >>= e; while(t--) { // ll witness = (rng() % (p-3)) + 2; //Using random witness ll witness = SPRP[t]; //Using SPRP witness = modexpo(witness, x, p); if(witness <= 1) continue; for(int i = 0; i<e && witness != p-1; i++) witness = modmult(witness, witness, p); if(witness != p-1) return false; } return true; } //999999999999999989 [checker, it's a prime, ll(1e18)-11] ll getPow(ll x){ if(RabinMiller(x)) return x; ll y = (ll)sqrtl(x); if(y*y == x && RabinMiller(y)) return y; if(allpow.count(x)) return allpow[x]; return -1; } int main(){ FASTIO; precal(); int n, k; cin>>n>>k; map<ll, vector<ll>> pows; vector<ll> candidates; ll best = -1; int bestcnt = 1e9; for(int i = 0; i<n; i++){ ll x; cin>>x; ll p = getPow(x); if(p == -1 || pows[p].size() > 1){ candidates.push_back(x); } else{ pows[p].push_back(x); } } vector<ll> powdoubles; vector<ll> primes; for(auto &p : pows){ if(p.second.size() <= 1) continue; primes.push_back(p.first); powdoubles.push_back(p.second[0]); powdoubles.push_back(p.second[1]); } for(int i = 0; i<candidates.size(); i++){ ll tmp = candidates[i]; int tmpcnt = 0; for(ll p : primes){ if(tmp % p == 0) tmpcnt++; while(tmp % p == 0) tmp /= p; if(tmp == 1) break; } if(tmp > 1){ candidates.erase(candidates.begin() + i); i--; continue; } if(tmpcnt < bestcnt){ bestcnt = tmpcnt; best = candidates[i]; } } // debug(), dbg(candidates); // debug(), dbg(powdoubles); // debug(), dbg(best), dbg(bestcnt); if(k == 1 || k > candidates.size() + powdoubles.size()){ cout<<0<<endl; return 0; } if(k > powdoubles.size()){ for(ll x : powdoubles){ cout<<x<<" "; k--; } for(int i = 0; k>0; k--, i++){ cout<<candidates[i]<<" "; } cout<<endl; } else if(k % 2 == 0){ for(int i = 0; k>0; k--, i++) cout<<powdoubles[i]<<" "; cout<<endl; } else if(best == -1){ cout<<0<<endl; } else if(1+2*bestcnt > k){ cout<<0<<endl; } else { cout<<best<<" "; candidates.erase(find(candidates.begin(), candidates.end(), best)); for(int i = 0; i<powdoubles.size(); i++){ ll p = getPow(powdoubles[i]); if(best % p == 0){ cout<<powdoubles[i]<<" "; powdoubles.erase(powdoubles.begin()+i); i--; } } k -= 1+2*bestcnt; for(int i = 0; k > 0; i++, k--){ cout<<powdoubles[i]<<" "; } cout<<endl; } return 0; }
19
CPP
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") 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)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 18 MOD = 10**9+7 from math import gcd Ri = lambda : [int(x) for x in sys.stdin.readline().split()] ri = lambda : sys.stdin.readline().strip() n,m = Ri() a = Ri() b = Ri() ans = 0 for l in range(2**9 +1): flag = True for i in range(n): tflag = False for j in range(m): if (a[i] & b[j])|l == l: tflag = True break if not tflag : flag = False; break if flag : ans= l;break print(ans)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); unsigned short int t; cin >> t; while (t--) { unsigned short int n; cin >> n; short int a[n], b[n]; for (unsigned short int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i += 2) { b[i] = -1 * a[i + 1]; b[i + 1] = a[i]; } for (unsigned short int i = 0; i < n; i++) cout << b[i] << " "; cout << "\n"; } return 0; }
7
CPP
k = int(input()) s = [] z = [] for i in range(k): s.append([]) z.append([]) for i in range(k): n = int(input()) num = input() num = num.split() add = 0 for j in range(n): tup = (int(num[j]), i + 1, j + 1) z[i].append(tup) add += tup[0] for j in range(n): s[i].append((add - z[i][j][0], z[i][j][1], z[i][j][2])) m = [] for i in range(k): for j in range(len(s[i])): m.append(s[i][j]) m.sort(key=lambda tup: tup[0]) flag = True for i in range(len(m) - 2): if m[i][0] == m[i + 1][0] and not(m[i][1] == m[i + 1][1]): flag = False print("YES") print(m[i][1], m[i][2]) print(m[i + 1][1], m[i + 1][2]) break if flag: print("NO")
9
PYTHON3
#Bhargey Mehta (Junior) #DA-IICT, Gandhinagar import sys, math, queue, collections #sys.stdin = open('input.txt', 'r') MOD = 10**9+7 sys.setrecursionlimit(1000000) n, k = map(int, input().split()) a = list(input()) if k == 0: print(''.join(a)) exit() if n == 1: print(0) exit() if a[0] != '1': a[0] = '1' k -= 1 for i in range(1, n): if k > 0 and a[i] != '0': a[i] = '0' k -= 1 if k == 0: break print(''.join(a))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int nmax = 1e5 + 42; int n, m; struct tree { int x, h, l, r; }; tree inp[nmax]; struct mushroom { int x, p; }; mushroom mush[nmax]; double tree[4 * nmax]; int values[nmax]; void build(int node, int l, int r) { if (l == r) { tree[node] = mush[l].p; return; } tree[node] = 1; int av = (l + r) / 2; build(node * 2, l, av); build(node * 2 + 1, av + 1, r); } void update(int node, int l, int r, int lq, int rq, double prob) { if (l == lq && r == rq) { tree[node] = tree[node] * prob; return; } int av = (l + r) / 2; if (lq <= av) update(node * 2, l, av, lq, min(av, rq), prob); if (av < rq) update(node * 2 + 1, av + 1, r, max(av + 1, lq), rq, prob); } double outp = 0; void go(int node, int l, int r) { if (l == r) { outp = outp + tree[node]; return; } int av = (l + r) / 2; tree[node * 2] = tree[node * 2] * tree[node]; tree[node * 2 + 1] = tree[node * 2 + 1] * tree[node]; tree[node] = 1; go(node * 2, l, av); go(node * 2 + 1, av + 1, r); } bool cmp(mushroom a, mushroom b) { return a.x < b.x; } int main() { ios_base::sync_with_stdio(false); cin.tie(); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> inp[i].x >> inp[i].h >> inp[i].l >> inp[i].r; } for (int i = 1; i <= m; i++) { cin >> mush[i].x >> mush[i].p; } sort(mush + 1, mush + m + 1, cmp); for (int i = 1; i <= m; i++) values[i] = mush[i].x; build(1, 1, m); for (int i = 1; i <= n; i++) { int p = upper_bound(values + 1, values + m + 1, inp[i].x) - values; int q = upper_bound(values + 1, values + m + 1, inp[i].x + inp[i].h) - values; q--; if (p <= q) update(1, 1, m, p, q, 1 - inp[i].r / 100.0); int s = lower_bound(values + 1, values + m + 1, inp[i].x - inp[i].h) - values; int t = lower_bound(values + 1, values + m + 1, inp[i].x) - values; t--; if (s <= t) update(1, 1, m, s, t, 1 - inp[i].l / 100.0); } go(1, 1, m); printf("%.18f\n", outp); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 2; int cnt[maxn]; int main() { int a; cin >> a; string s; cin >> s; for (int i = 0; i < s.size(); i++) { int now = 0; for (int j = i; j < s.size(); j++) { now += s[j] - '0'; cnt[now]++; } } long long ans = 0; if (a == 0) { ans = cnt[0] * 1LL * cnt[0]; for (int i = 1; i <= 36000; i++) { ans += cnt[0] * 2LL * cnt[i]; } cout << ans; return 0; } int to = (int)sqrt(1.0 * a); for (int i = 1; i <= to; i++) { if (a % i != 0 || a > i * 36000LL) continue; if (a == i * i) { ans += cnt[i] * 1LL * cnt[i]; } else ans += cnt[i] * 2LL * cnt[a / i]; } cout << ans; return 0; }
9
CPP
def bn(n): s = '' while n > 0: s += str(n % 2) n //= 2 return s t = int(input()) f = [] for i in range(t): a, b = map(int, input().split()) s1, s2 = bn(a), bn(b) q, e = len(s1), len(s2) if q > e: while q > e: s2 += '0' e += 1 else: while q < e: s1 += '0' q += 1 step = 1 ans = 0 for i in range(len(s1)): if s1[i] != s2[i]: ans += step step *= 2 f.append(ans) for i in f: print(i)
7
PYTHON3
#include <iostream> #include <algorithm> #include <string> using namespace std; string ANS="0000000000"; void cal(string A,string B,int C,int D){ if(A.size()==D+1){ string st=""; for(int i=0;i<A.size();i++)st+=(A[i]-B[i]+'0'); reverse(st.begin(),st.end()); if(ANS<st)ANS=st; } else{ if(A[D]>=B[D]){ cal(A,B,C,D+1); } else{ A[D]+=10; string AA=A; A[D+1]--; cal(A,B,C,D+1); if(C>0){ cal(AA,B,C-1,D+1); } } } } int main(){ string a,b; int c; cin>>a>>b>>c; reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); while(a.size()<10)a+="0"; while(a.size()>b.size())b+="0"; cal(a,b,c,0); for(int i=0,j=0;i<ANS.size();i++){ if(j==0&&ANS[i]!='0')j=1; if(j)cout<<ANS[i]; } cout<<endl; return 0; }
0
CPP
#include<iostream> #include<vector> #include<algorithm> using namespace std; int H, W, N; vector<vector<int>> field; int main() { cin >> H >> W >> N; field.resize(H); for (int i = 0; i < N; i++) { int x, y; cin >> x >> y; x--, y--; if (x >= y) { field[x].push_back(y); } } int ans = H - 1; int wlimit = 0; for (int i = 0; i < H; i++)sort(field[i].begin(), field[i].end()); for (int i = 1; i < H; i++) { if (field[i].size() == 0)wlimit++; else { if (field[i][0] <= wlimit) { cout << i << endl; return 0; } else if (field[i][0] == wlimit + 1) { //cannot do anything } else { wlimit++; } } } cout << ans + 1 << endl; return 0; }
0
CPP
b, k = map(int, input().split()) b = b%2 a = [int(i)%2 for i in input().split()] sum = a[k-1] for i in range(k-1): a[i] = a[i]*b sum += a[i] print('even' if sum%2 == 0 else 'odd')
7
PYTHON3
#include<bits/stdc++.h> /* Miku */ using namespace std; typedef long long ll ; const ll mk = 100005; #define endl "\n" const int mod = 1e9 + 7; int n,k,a[2000],t,f[2000],i,j,l,ans; inline int Mikuplaying(int x,int y){ int ans=1; for(;y;y>>=1,x=1ll*x*x%mod) if(y&1)ans=1ll*ans*x%mod;return ans; } int main(){ cin >> n >> k; for(i=1;i*i<=n;++i)if(n%i==0){a[++t]=i;if(i*i !=n)a[++t]=n/i;} std::sort(a+1,a+t+1); for(j=1;j<=t; ans=(ans+1ll*f[j]*(a[j]&1?a[j]:a[j]/2))%mod,++j) for(f[j]=Mikuplaying(k,a[j]+1>>1),l=1;l<j;++l)if(a[j]%a[l]==0)f[j]=(f[j]+mod-f[l])%mod; cout << ans; return 0; }
0
CPP
for _ in range(int(input())): nb_student, max_mark = [int(x) for x in input().split()] marks = [int(x) for x in input().split()] print(min(sum(marks), max_mark))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int NK = 2e6 + 10, MOD = 1e9 + 7; vector<int> lp; void sieve(int lim) { lp.resize(lim + 1); vector<int> primes; for (int i = 2; i <= lim; i++) { if (!lp[i]) { lp[i] = i; primes.push_back(i); } for (int p : primes) { if (p > lp[i] || p * 1LL * i > lim) break; lp[p * i] = p; } } } int mobius(int n) { int ret = 1; while (n > 1) { if (lp[n / lp[n]] == lp[n]) return 0; n /= lp[n], ret = -ret; } return ret; } int powmod(int a, int first) { if (first == 0) return 1; if (first % 2 == 0) return powmod(a * 1LL * a % MOD, first / 2); return a * 1LL * powmod(a, first - 1) % MOD; } int m[NK], p[NK]; vector<int> dv[NK]; int main() { int n, k; scanf("%d %d", &n, &k); sieve(k); for (int i = 1; i <= k; i++) { m[i] = mobius(i), p[i] = powmod(i, n); for (int j = i; j <= k; j += i) { dv[j].push_back(i); } } int sum = 0, ans = 0; for (int i = 1; i <= k; i++) { for (int d : dv[i]) { sum = (sum + m[d] * (p[i / d] - p[i / d - 1])) % MOD; if (sum < 0) sum += MOD; } ans = (ans + (sum ^ i)) % MOD; } printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int n, m, w[3010], b[3010], sz[3010]; vector<int> adj[3010]; vector<pair<int, long long> > dp[3010]; void update_dp(int u, int v) { vector<pair<int, long long> > ndp(dp[u].size() + dp[v].size() - 1, pair<int, long long>(-1, 0)); for (int i = 1; i < dp[u].size(); i++) { for (int j = 1; j < dp[v].size(); j++) { pair<int, long long> tmp = pair<int, long long>( dp[u][i].first + dp[v][j].first, dp[u][i].second + dp[v][j].second); ndp[i + j - 1] = max(ndp[i + j - 1], tmp); tmp = pair<int, long long>( dp[u][i].first + dp[v][j].first + (dp[v][j].second > 0), dp[u][i].second); ndp[i + j] = max(ndp[i + j], tmp); } } dp[u] = ndp; } void dfs(int u, int par = 0) { sz[u] = 1; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == par) continue; dfs(v, u); update_dp(u, v); } } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); } for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); } for (int i = 0; i < n - 1; i++) { int x, y; scanf("%d%d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1; i <= n; i++) { dp[i].resize(2); dp[i][1] = pair<int, long long>(0, w[i] - b[i]); } dfs(1); int ans = dp[1][m].first + (dp[1][m].second > 0); printf("%d\n", ans); for (int i = 1; i <= n; i++) { adj[i].clear(); dp[i].clear(); } } }
12
CPP
#include <bits/stdc++.h> using namespace std; const int N = 401000; vector<int> A, Ans, B; namespace PAM { struct Node { unordered_map<int, int> ch; int len, fail; } T[N]; int tot, lst; void Init(void) { T[tot = lst = 1].len = -1; T[0].len = 0; T[0].fail = 1; } int Insert(int pos, int val) { while (B[pos - T[lst].len - 1] != B[pos]) { lst = T[lst].fail; } if (T[lst].ch.find(val) == T[lst].ch.end()) { T[++tot].len = T[lst].len + 2; int cur = T[lst].fail; while (B[pos - T[cur].len - 1] != B[pos]) { cur = T[cur].fail; } T[tot].fail = T[cur].ch[val]; T[lst].ch[val] = tot; } lst = T[lst].ch[val]; return T[lst].len; } } // namespace PAM int n, m, vis[N], len[N], reachable[N]; int main(void) { scanf("%d%d", &n, &m); for (int i = 1, x; i <= n; i++) { scanf("%d", &x); A.push_back(x); } sort(A.begin(), A.end()); for (int i = 0; i < A.size(); i++) { int v = A[i]; } PAM::Init(); B.resize(n); for (int i = 1; i < A.size(); i++) { B[i] = A[i] - A[i - 1]; len[i] = PAM::Insert(i, B[i]); } int now = PAM::lst; if (now == 1) { now = PAM::T[now].fail; } while (~PAM::T[now].len) { int l = PAM::T[now].len; if (l == n - 1) { Ans.push_back((A[0] + A.back()) % m); } else { int x = n - l - 2; if (len[x] == x && A[0] + A[x] + m == A[x + 1] + A[n - 1]) { Ans.push_back(A[0] + A[x]); } } now = PAM::T[now].fail; } sort(Ans.begin(), Ans.end()); printf("%d\n", Ans.size()); for (int v : Ans) { printf("%d ", v); } puts(""); return 0; }
13
CPP
n = int(input()) tasks = [] for i in range(n): task = list(map(int, input().split())) tasks.append(task) count = 0 for task in tasks: sum = 0 for i in range(len(task)): sum += task[i] if sum >= 2: count += 1 print(count)
7
PYTHON3
#a = list(map(int,input().split())) from math import factorial t = int(input()) for _ in range(t): #n = int(input()) a,b = list(map(int,input().split())) s = input() n = a+b l = [c for c in s] done = False for i,x in enumerate(l): if x!="?" and l[n-i-1]!="?": if x != l[n-i-1]: done = True break if done: print (-1) continue for i,x in enumerate(l): if x!="?": l[n-i-1] = x a-=l.count("0") b-=l.count("1") half = n//2 if a%2 and b%2: print (-1) continue if a<0 or b<0: print (-1) continue if a%2: l[half] = "0" a-=1 elif b%2: l[half] = "1" b-=1 for i,x in enumerate(l): if x=="?": if a: l[n-i-1] = "0" l[i] = "0" a-=2 else: l[n-i-1] = "1" l[i] = "1" b-=2 print ("".join(l))
9
PYTHON3
s=[int(input()) for i in range(10)] s.sort() print(s[9]) print(s[8]) print(s[7])
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int A[30]; int main() { ios_base::sync_with_stdio(false); int SIGMA; cin >> SIGMA; for (int i = 1; i <= SIGMA; ++i) cin >> A[i]; sort(A + 1, A + SIGMA + 1); map<int, int> Map; A[SIGMA + 1] = -1; long long dimension = 0; for (int i = SIGMA; i >= 1; i--) { while (A[i] >= 1 && Map.count(A[i])) A[i]--; assert(A[i] >= 0); Map[A[i]] = 1; dimension += A[i]; } cout << dimension << "\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; int count = 1; int num = -1; cin >> n >> k; int x[50]; for (int i = 0; i < n; i++) { cin >> x[i]; } sort(x, x + n); for (int i = 0; i < n; i++) { count = 1; for (int j = i + 1; j < n; j++) { if (x[j] >= x[i]) count++; } if (count == k) { num = x[i]; break; } } if (num == -1) cout << num; else cout << num << " " << num; }
8
CPP
#include <iostream> using namespace std; int main() { int time1, time2; int rate1, rate2; cin >> time1 >> time2 >> rate1 >> rate2; if(rate1 == -1 || rate2 == -1) { if(time1 < time2) { cout << "Alice" << endl; } else if(time1 > time2) { cout << "Bob" << endl; } else { cout << "Draw" << endl; } } else { if(rate1 > rate2) { cout << "Alice" << endl; } else if(rate1 < rate2) { cout << "Bob" << endl; } else { cout << "Draw" <<endl; } } return 0; }
0
CPP
#include<bits/stdc++.h> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) using namespace std; int main(){ int n; cin >> n; if( n == 0 ) puts("1"); else if( n == 1 ) puts("2"); else if( n == 2 ) puts("1"); else puts("0"); return 0; }
0
CPP
#include<bits/stdc++.h> int main(){ using namespace std; unsigned long Q; cin >> Q; constexpr unsigned long M{205891132094649UL}; const auto& calc = [](unsigned long a, unsigned long b, unsigned long c, unsigned long d) -> unsigned long { auto ub{b}, lb{b}, p{M}; while(p /= 3)if(a / p % 3 == 1 || a / p * p + p * (1 + (a / p % 3 == 2)) <= c){ if(ub / p % 3 == 1)ub = ub / (3 * p) * 3 * p + 2 * p; if(lb / p % 3 == 1)lb = lb / (3 * p) * 3 * p + p - 1; } return c - a + min(ub - b + max(ub, d) - min(ub, d), b + d - 2 * lb); }; for(unsigned long _{0}, a, b, c, d; _ < Q; ++_){ cin >> a >> b >> c >> d; if(--a > --c){ swap(a, c); swap(b, d); } if(--b > --d){ b = M - b - 1; d = M - d - 1; } cout << max(calc(a, b, c, d), calc(b, a, d, c)) << endl; } return 0; }
0
CPP
n = int(input()) l = [int(input()) for _ in range(n)] count = 0 for i in range(n): j, c = i, 0 while j >= 0: j = l[j]-1 c += 1 count = max(count, c) print(count)
7
PYTHON3
s = input() k = int(input()) if len(s) < k: print('impossible') else: ss = set(s) l = k - len(ss) if l < 0: l = 0 print(l)
7
PYTHON3
def get_input(): while True: try: yield ''.join(input()) except EOFError: break N = list(get_input()) for ll in range(len(N)): a = [int(i) for i in N[ll].split(",")] l = [] totalLen = 0 for i in range(10): l.append(a[i]) totalLen += a[i] v1 = a[10] v2 = a[11] midPoint = totalLen*v1/(v1+v2) ans = 10 for i in range(10): midPoint -= l[i] if midPoint <= 0: ans = i+1 break print(ans)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, last, i, j, k, jem; vector<int> a(100007); stringstream sss; int main() { cin >> n >> m; for (i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.begin() + n); for (j = 1; j < a[0]; j++) { jem += j; if (jem > m) break; k++; sss << j << " "; } for (i = 1; i < n; i++) { if (jem < m) for (j = a[i - 1] + 1; j < a[i]; j++) { jem += j; if (jem > m) break; k++; sss << j << " "; } } if (jem < m) for (j = a[n - 1] + 1;; j++) { jem += j; if (jem > m) break; k++; sss << j << " "; } cout << k << endl << sss.str(); }
9
CPP
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; #define rep(i,n) for (int i = 0; i < (n); ++i) const ll MOD = 1000000007; int main() { int h, w, k; cin >> h >> w >> k; k; vector<vector<int>> dp(h+1, vector<int>(w, 0)); dp[0][0] = 1; rep(i, h) rep(j, w) rep(l, 1<<(w-1)) { bool ok = true; rep(m, w-2) if ( ((l>>m) & 1) && ((l>>(m+1)) & 1) ) ok = false; if ( !ok ) continue; if ( j - 1 >= 0 && ((l>>(j-1)) & 1) ) dp[i+1][j-1] = (dp[i+1][j-1] + dp[i][j]) % MOD; else if ( j < w - 1 && ((l>>j) & 1) ) dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j]) % MOD; else dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % MOD; } cout << dp[h][k-1] << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline bool smin(T &a, const T &b) { return a > b ? a = b : a; } template <typename T> inline bool smax(T &a, const T &b) { return a < b ? a = b : a; } const int N = (int)12, mod = (int)0; long long dp[N][N][N], odp[N][N][N]; int nxt_st_sv[N][N][N][N], nxt_pos_sv[N][N][N][N], nxt_num_sv[N][N][N][N]; int main() { string s; cin >> s; int n = (int)s.size(); long long res = 0; for (int cur = 0; cur <= 10; ++cur) { for (int st = 0; st <= 10; ++st) for (int num = 0; num <= 10; ++num) { for (int pos = 0; pos <= 10; ++pos) { int nxt_start = (st + num) % 11; int nxt_pos = nxt_start; for (int c = st; c != pos; c = (c + 1) % 11) { nxt_pos += c; nxt_pos %= 11; } nxt_pos = (nxt_pos + cur) % 11; int nxt_num = 0; for (int c = st, i = 0; i < num; ++i, c = (c + 1) % 11) { nxt_num = (nxt_num + c) % 11; } nxt_st_sv[cur][st][num][pos] = nxt_start; nxt_pos_sv[cur][st][num][pos] = nxt_pos; nxt_num_sv[cur][st][num][pos] = nxt_num; } } } for (int j = 0; j < n; ++j) { memcpy(odp, dp, sizeof dp); memset(dp, 0, sizeof dp); int cur = s[j] - '0'; if (cur != 0) dp[0][10][cur]++; for (int st = 0; st <= 10; ++st) for (int num = 0; num <= 10; ++num) { for (int pos = cur + 1; pos <= 10; ++pos) if (odp[st][num][pos]) { int nxt_start = nxt_st_sv[cur][st][num][pos]; int nxt_pos = nxt_pos_sv[cur][st][num][pos]; int nxt_num = nxt_num_sv[cur][st][num][pos]; dp[nxt_start][nxt_num][nxt_pos] += odp[st][num][pos]; } } for (int a = 0; a <= 10; ++a) for (int b = 0; b <= 10; ++b) for (int c = 0; c <= 10; ++c) if (dp[a][b][c]) { res += dp[a][b][c]; } } cout << res << endl; }
10
CPP
s = input() n = len(s) count = 0 a = 'Danil' b = 'Olya' c = 'Slava' d = 'Ann' e = 'Nikita' for i in range(n-4): if s[i:i+5] == a or s[i:i+5] == c: count += 1 for j in range(n-3): if s[j:j+4] == b: count += 1 for x in range(n-2): if s[x:x+3] == d: count += 1 for k in range(n-5): if s[k:k+6] == e: count += 1 if count == 1: print('YES') else: print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; string s; cin >> n >> k >> s; s = "{" + s + "{"; long long a[30] = {0}, i, j = 0, b; long long ans = 0; char c; while (k--) { cin >> c; b = c - 'a'; a[b]++; } for (i = 0; i <= n + 1; i++) { b = s[i] - 'a'; if (!a[b]) { ans += (i - j) * (i - j - 1) / 2; j = i; } } cout << ans << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T bpow(T p, T e) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } int toLlint(string s) { long long int sm; stringstream ss(s); ss >> sm; return sm; } int ts, kk = 1; int n, cl[200005]; vector<int> al[200005]; int on[200005]; int dfs(int u, int pr) { on[u] = (cl[u] == -1); for (int i = 0; i < al[u].size(); i++) { int v = al[u][i]; if (v != pr) { on[u] += dfs(v, u); } } return on[u]; } int cnt = 0; bool prnt(int u, int pr) { if (on[u] == 0) return 0; if (pr == -1) printf("1"); else { printf(" %d", u); cl[u] = -cl[u]; if (cl[u] == -1) cnt++; else cnt--; if (cnt == 0) exit(0); } for (int i = 0; i < al[u].size(); i++) { int v = al[u][i]; if (pr != v && prnt(v, u)) { printf(" %d", u); cl[u] = -cl[u]; if (cl[u] == -1) cnt++; else cnt--; if (cnt == 0) exit(0); } } if (cl[u] == -1 && pr != -1) { printf(" %d %d", pr, u); cl[u] = -cl[u]; cl[pr] = -cl[pr]; if (cl[u] == -1) cnt++; else cnt--; if (cl[pr] == -1) cnt++; else cnt--; if (cnt == 0) exit(0); } return 1; } int main() { int t, i, j, k; scanf("%d", &n); bool g = 0; for (i = 1; i < n + 1; i++) { scanf("%d", &cl[i]); g |= (cl[i] == -1); } for (i = 1; i < n; i++) { scanf("%d%d", &j, &k); al[j].push_back(k); al[k].push_back(j); } if (!g) { printf("1\n"); return 0; } cnt = dfs(1, -1); prnt(1, -1); if (cl[1] == -1) { t = al[1][0]; printf(" %d %d %d\n", t, 1, t); cnt--; } assert(cnt == 0); return 0; }
11
CPP
n, t = list(map(int, input().split())) start = '1'+'0'*(n-1) end = '1'+'0'*n for i in range(int(start), int(end)): if i%t == 0: print(i) quit() print("-1")
7
PYTHON3
n = input() count = int(0) for x in range(int(n)): temp = [int(y) for y in input().split()] if(temp[1] - temp[0] > int(1)): count += 1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; struct Cube { long x, y; long countDependOnHim; }; int main() { long m; cin >> m; vector<Cube> cubes; map<pair<long, long>, long> cubeToId; for (long i = 0; i < m; i++) { long x, y; cin >> x >> y; cubeToId[make_pair(x, y)] = i; Cube newCube; newCube.x = x; newCube.y = y; newCube.countDependOnHim = 0; cubes.push_back(newCube); } set<long> idsNoDependencies; for (long i = 0; i < cubes.size(); i++) { long dx[3] = {-1, 0, 1}; for (long j = 0; j < 3; j++) { long newX = cubes[i].x + dx[j]; long newY = cubes[i].y + 1; if (cubeToId.count(make_pair(newX, newY))) { long bases = 0; for (long k = 0; k < 3; k++) { long downX = newX + dx[k]; long downY = newY - 1; if (cubeToId.count(make_pair(downX, downY))) { bases++; } } if (bases == 1) { cubes[i].countDependOnHim++; } } } if (cubes[i].countDependOnHim == 0) { idsNoDependencies.insert(i); } } long long solution = 0; long round = 1; while (idsNoDependencies.size() != 0) { long nextValue = 0; if (round % 2) { set<long>::iterator it = idsNoDependencies.end(); it--; nextValue = *it; } else { nextValue = *idsNoDependencies.begin(); } idsNoDependencies.erase(nextValue); round++; solution = ((solution * m) + nextValue) % 1000000009; long xRem = cubes[nextValue].x; long yRem = cubes[nextValue].y; cubeToId.erase(make_pair(xRem, yRem)); long dx[5] = {-2, -1, 0, 1, 2}; long dy[5] = {-2, -1, 0, 1, 2}; for (long i = 0; i < 5; i++) { for (long j = 0; j < 5; j++) { if (cubeToId.count(make_pair(xRem + dx[i], yRem + dy[j]))) { long currentId = cubeToId[make_pair(xRem + dx[i], yRem + dy[j])]; cubes[currentId].countDependOnHim = 0; long dx[3] = {-1, 0, 1}; for (long k = 0; k < 3; k++) { long newX = cubes[currentId].x + dx[k]; long newY = cubes[currentId].y + 1; if (cubeToId.count(make_pair(newX, newY))) { long bases = 0; for (long l = 0; l < 3; l++) { long downX = newX + dx[l]; long downY = newY - 1; if (cubeToId.count(make_pair(downX, downY))) { bases++; } } if (bases == 1) { cubes[currentId].countDependOnHim++; } } } if (cubes[currentId].countDependOnHim == 0) { idsNoDependencies.insert(currentId); } else { idsNoDependencies.erase(currentId); } } } } } cout << solution << endl; return 0; }
8
CPP
n = int(input()) a = [int(x) for x in input().split(' ')] up, down = [[None for _ in range(n)] for _ in range(2)] k = lambda list, i: list[i] if i >= 0 and i < n else 0 for i, ai in enumerate(a): up[i] = ai + k(up, i - 2) for i, ai in reversed(list(enumerate(a))): down[i] = ai + k(down, i + 2) result = 0 for i in range(n): states = [k(up, i - x) + k(down, i + j + 1) for j, x in enumerate([2, 1])] if states[0] == states[1]: result += 1 print(result)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { long long n, i, t, j; cin >> n; vector<long long> v(n, 0), ans(n, 0); for (i = 0; i < n; i++) { cin >> v[i]; } for (i = 0; i < n; i++) { t = v[i]; j = i + 1; long long c = 1; while (t != j) { t = v[t - 1]; c++; } ans[i] = c; } for (i = 0; i < n; i++) { cout << ans[i] << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long i, t; cin >> t; for (i = 0; i < t; i++) { solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int n = 2e5 + 6; vector<int> v[n]; int vis[n], check; void dfs(int f) { vis[f] = 1; int x, i, n = v[f].size(); if (n != 2) check = 0; for (i = 0; i < n; i++) { x = v[f][i]; if (vis[x] == -1) dfs(x); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int n, m, ans = 0; cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } memset(vis, -1, sizeof(vis)); for (int i = 1; i <= n; i++) { if (vis[i] == -1) { check = 1; dfs(i); if (check == 1) { ans++; } } } cout << ans << "\n"; return 0; }
11
CPP
import math T= int(input()) for _ in range(T): a,b=map(int, input().split()) temp = math.ceil(a/b) print(b*temp-a)
10
PYTHON3
l, r, x, y, k = [float(i) for i in input().split()] for i in range(int(x), int(y + 1)): if i * k >= l and i * k <= r: print('YES') exit(0) print('NO')
7
PYTHON3
for _ in range(int(input())): n=int(input()) a = [int(x) for x in input().split()] i,ans = 0,0 while i < n: j = i+1 mx = a[i] while j < n: if (a[i]>0 and a[j]>0) or (a[i]<0 and a[j]<0): mx = max(mx,a[j]) j += 1 else: break i = j ans += mx print(ans)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int c[1000005], n, k, mi; int main() { scanf("%d%d", &n, &k); int mi = 0x7fffffff; for (int i = 0; i < n; ++i) { int t; scanf("%d", &t); mi = min(mi, t); ++c[max(t - k, 1)]; --c[t + 1]; } if (mi <= k) { printf("%d", mi); return 0; } for (int i = 1; i <= 1000000; ++i) c[i] += c[i - 1]; int ans = k; for (int i = k + 1; i <= 1000000; ++i) { int tot = 0; for (int j = i; j <= 1000000; j += i) tot += c[j]; if (tot == n) ans = i; } printf("%d", ans); }
9
CPP
import sys n = int(sys.stdin.readline()) countbcks = n * 2 for i in range(0, n + 1): outstr = " " * countbcks arr = [] for j in range(i + 1): arr.append(str(j)) for k in range(i - 1, -1, -1): arr.append(str(k)) outstr += " ".join(arr) print(outstr) countbcks -= 2 countbcks = 2 for i in range(n - 1, -1, -1): outstr = " " * countbcks countbcks += 2 arr = [] for j in range(i + 1): arr.append(str(j)) for k in range(i - 1, -1, -1): arr.append(str(k)) outstr += " ".join(arr) print(outstr)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, i, j, x, h; char s[1001]; char c = '<'; int main() { scanf("%s", &s); h = 0; i = 0; while (i < strlen(s)) { if (s[i + 1] != '/') { printf("%*c", 1 + 2 * h, c); printf("%c>\n", s[i + 1]); h++; i += 3; } else { h--; printf("%*c", 1 + 2 * h, c); printf("/%c>\n", s[i + 2]); i += 4; } } return 0; }
8
CPP
n=int(input()) l1=list(map(int,input().split())) l2=[0]*n for i in range(n): l2[l1[i]-1]=i+1 print(*l2,sep=' ')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 105; int main() { int n; scanf("%d", &n); int a, flag = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a); if (a == 1) { flag = 1; } } if (flag) { printf("HARD\n"); } else { printf("EASY\n"); } return 0; }
7
CPP
s = list(input()) s2 = list(reversed(s)) #print(s , s2) for i in s : print(i , end = '') for i in s2 : print(i , end = '')
8
PYTHON3
#include <bits/stdc++.h> char Gc() { static char now[1 << 20], *S, *T; if (T == S) { T = (S = now) + std::fread(now, 1, 1 << 20, stdin); if (T == S) return EOF; } return *S++; } template <typename T> void Read(T &x) { x = 0; int f = 1; char c; while ((c = Gc()) < '0' || c > '9') if (c == '-') f = -1; x = c - '0'; while ((c = Gc()) >= '0' && c <= '9') x = x * 10 + c - '0'; x *= f; } template <typename T, typename... Args> void Read(T &x, Args &...args) { Read(x); Read(args...); } template <typename T> void checkmax(T &x, T y) { if (x < y) x = y; } template <typename T> void checkmin(T &x, T y) { if (x > y) x = y; } struct Edge { int to, nxt, flow; double cost; } e[160000]; int n, x[401], y[401], E, head[1001], s, t, ed[1001], flow[1001], le[1001], pre[1001]; double dis[1001]; bool vis[1001]; double Dis(int a, int b) { return std::hypot(x[a] - x[b], y[a] - y[b]); } void Add(int f, int t, double c, int w) { e[E].to = t; e[E].cost = c; e[E].flow = w; e[E].nxt = head[f]; head[f] = E++; } bool Spfa() { std::fill(dis + 1, dis + t + 1, 1e18); std::memset(vis, false, sizeof(vis)); std::memset(flow, 0, sizeof(flow)); flow[s] = 1e9; pre[t] = -1; std::queue<int> q; q.emplace(s); vis[s] = true; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int i = head[u]; i != -1; i = e[i].nxt) { int v = e[i].to; if (e[i].flow && dis[u] + e[i].cost < dis[v]) { dis[v] = dis[u] + e[i].cost; pre[v] = u; flow[v] = std::min(flow[u], e[i].flow); le[v] = i; if (!vis[v]) { q.emplace(v); vis[v] = true; } } } } return ~pre[t]; } int main(int argc, char const *argv[]) { std::memset(head, -1, sizeof(head)); Read(n); t = 2 * n + 1; for (int i = 1; i <= n; i++) { Read(x[i], y[i]); Add(s, i, 0.0, 2); Add(i, s, 0.0, 0); ed[i] = E; Add(i + n, t, 0.0, 1); Add(t, i + n, 0.0, 0); } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (y[i] > y[j]) { Add(i, n + j, Dis(i, j), 1); Add(n + j, i, -Dis(i, j), 0); } double ans = 0.0; while (Spfa()) { ans += dis[t] * flow[t]; int p = t; while (p != s) { e[le[p]].flow -= flow[t]; e[le[p] ^ 1].flow += flow[t]; p = pre[p]; } } int sum = 0; for (int i = 1; i <= n; i++) sum += e[ed[i]].flow; if (sum > 1) std::printf("-1"); else std::printf("%.8lf", ans); return 0; }
11
CPP
for _ in range(int(input())): n,x,y=map(int,input().split()) maxi=min(x+y-1, n) m=min(x+y-n+1, n) mini=max(m,1) print(mini, maxi)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-11; const long long MOD = 1000000007; const int N = 109; void fast() { std::ios_base::sync_with_stdio(0); } int main() { int n, x, y, m; scanf("%d%d", &n, &m); vector<vector<int>> adjm(n, vector<int>(n)); vector<int> edges(n); while (m--) { scanf("%d%d", &x, &y); x--, y--; adjm[x][y] = 1; adjm[y][x] = 1; edges[x]++; edges[y]++; } int ans = 1e9; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (adjm[i][j]) { for (int k = j + 1; k < n; k++) { if (adjm[i][k] && adjm[j][k]) { ans = min(ans, edges[i] + edges[j] + edges[k]); } } } } } if (ans == 1e9) puts("-1"); else printf("%d\n", ans - 6); return 0; }
8
CPP
t = int(input()) i = 0 l = list() while i < t: n, m = map(int, input().split()) l.append(n) l.append(m) i += 1 j = 0 i = 0 while i < t: m = l[j] j += 1 n = l[j] j += 1 if m == 1 or n == 1 or (m == 2 and n == 2): print("YES") else: print("NO") i += 1
7
PYTHON3
a=input() b=input() a=a.lower() b=b.lower() c=0 i=0 while(i<len(a)): if (a[i])>(b[i]): c=1 break elif (a[i])<(b[i]): c=-1 break i=i+1 print(c)
7
PYTHON3
# Qa# a = input() words = a.replace(',', ';').split(';') numbers = [] strings = [] for word in words: if not word: strings.append(word) #empty strings elif (word[0] == "0") & (len(word) >= 2): strings.append(word) elif word.isnumeric(): numbers.append(word) else: strings.append(word) if not numbers: print("-") print("\"",','.join(strings),"\"", sep="") elif not strings: print("\"",','.join(numbers),"\"", sep="") print("-") else: print("\"",','.join(numbers),"\"", sep="") print("\"",','.join(strings),"\"", sep="")
7
PYTHON3
a = input() sum = 0 prev = "a" for i in a: count = abs(ord(i)-ord(prev)) if (count > 12): count = abs(26 - count) sum = sum + count prev = i print(sum)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; long long a[maxn], res[maxn], to_add[maxn]; pair<long long, long long> p[maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; long long sum = 0; for (int i = 0; i < m; i++) { cin >> a[i]; sum += a[i]; } for (int i = 0; i < m; i++) { if (a[i] + i > n || sum < n) { cout << "-1" << endl; exit(0); } } for (int i = 0; i < m; i++) { p[i].first = a[i]; p[i].second = i; } multiset<long long> s; for (int i = 0; i < m; i++) { s.insert(p[i].first + i); } int last = 0; for (int i = 0; i < m - 1; i++) { s.erase(s.lower_bound(p[i].first + i)); int delta = min(n - *(prev(s.end())) - last, p[i].first - 1); to_add[i] = delta; last += delta; if (last + *(prev(s.end())) >= n) break; } for (int i = 1; i < m; i++) { to_add[i] += to_add[i - 1]; } res[p[0].second + 1] = 1; for (int i = 1; i < m; i++) res[p[i].second + 1] = to_add[i - 1] + i + 1; for (int i = 1; i <= m; i++) cout << res[i] << " "; cout << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline istream& operator>>(istream& fin, pair<T1, T2>& pr) { fin >> pr.first >> pr.second; return fin; } template <class T0, class T1, class T2> inline istream& operator>>(istream& fin, tuple<T0, T1, T2>& t) { fin >> get<0>(t) >> get<1>(t) >> get<2>(t); return fin; } template <class T> inline istream& operator>>(istream& fin, vector<T>& a) { if (!a.size()) { size_t n; fin >> n; a.resize(n); } for (auto& u : a) fin >> u; return fin; } string probB() { vector<string> a(2); cin >> a; const auto n = a[0].length(); size_t diff = 0; for (size_t i = 0; i < n; ++i) diff += a[0][i] != a[1][i]; if (diff & 1) return "impossible"; string str; size_t k = 0; for (size_t i = 0; i < n; ++i) if (a[0][i] == a[1][i]) str.push_back(a[0][i]); else if (2 * k < diff) str.push_back(a[0][i]), ++k; else str.push_back(a[1][i]); return str; } int main(const int argc, char* argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cout << probB(); return EXIT_SUCCESS; }
8
CPP
#include <cstdio> #include <algorithm> #include <bitset> #define MAXN 50005 #define MAXBS 70005 using namespace std; bitset<MAXBS> bs; int a[MAXN]; int main(void){ int n,k; int ll,rr; scanf("%d%d",&n,&k); for (int i=1;i<=n;i++){ scanf("%d",a+i); } sort(a+1,a+n+1); //Min Necessary:[ll,rr] ll=0,rr=n+1; while (rr>ll){ int mid=(ll+rr)>>1; int necessary=0; if (a[mid]>=k){ rr=mid; continue; } bs.reset(); // bs=0 bs[0]=1; // bs[0]=1; for (int i=1;i<=n;i++) if (i!=mid) bs|=(bs<<a[i]); for (int i=k-1;i>=k-a[mid];i--){ if (bs[i]){ necessary=1; break; } } if (necessary) rr=mid; else ll=mid+1; } printf("%d",ll-1); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, v; cin >> n >> v; vector<int> p1(3002, 0), p2(3002, 0); int maxday = 0; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; p2[a] += b; if (maxday < a) maxday = a; } long long ans = 0LL; for (int i = 1; i <= maxday; ++i) { int start = 0; start = min(p1[i], v); p1[i] = 0; ans += start; int x = 0; if (start < v) { x = min(v - start, p2[i]); ans = ans + x; } p1[i + 1] += p2[i] - x; p2[i] = 0; } ans = ans + min(p1[maxday + 1], v); cout << ans << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MX = 140140; struct Node { long long x[10]; char nxt[10]; bool need; } a[MX * 10]; int b[MX], mx, n, m, i, t, l, r, xfr, y; long long j, res, cur[10], scur[10]; void init(int i, int l, int r) { for (int j = 0; j < 10; j++) a[i].nxt[j] = j; if (l == r) { b[l] = i; return; } init(i * 2, l, (l + r) / 2); init(i * 2 + 1, (l + r) / 2 + 1, r); } void recount(int i, int l, int r) { if (l == r) return; recount(i * 2, l, (l + r) / 2); recount(i * 2 + 1, (l + r) / 2 + 1, r); for (int j = 0; j < 10; j++) a[i].x[j] = a[i * 2].x[j] + a[i * 2 + 1].x[j]; } void psh(int i, int L, int R) { if (L != R) for (int k = i * 2; k <= i * 2 + 1; k++) { for (int j = 0; j < 10; j++) cur[j] = 0; for (int j = 0; j < 10; j++) cur[a[i].nxt[j]] += a[k].x[j]; for (int j = 0; j < 10; j++) { a[k].x[j] = cur[j]; a[k].nxt[j] = a[i].nxt[a[k].nxt[j]]; a[k].need |= (a[k].nxt[j] != j); } } a[i].need = false; for (int j = 0; j < 10; j++) a[i].nxt[j] = j; } void modify(int i, int L, int R, int l, int r) { if (a[i].need) psh(i, L, R); if (L == l && R == r) { if (a[i].x[xfr] > 0 && xfr != y) { a[i].x[y] += a[i].x[xfr]; a[i].x[xfr] = 0; for (int j = 0; j < 10; j++) if (a[i].nxt[j] == xfr) { a[i].nxt[j] = y; a[i].need |= (L < R && j != y); } } return; } int h = (L + R) / 2; if (l <= h) modify(i * 2, L, h, l, min(h, r)); if (r > h) modify(i * 2 + 1, h + 1, R, max(h + 1, l), r); for (int j = 0; j < 10; j++) a[i].x[j] = a[i * 2].x[j] + a[i * 2 + 1].x[j]; } void fsum(int i, int L, int R, int l, int r) { if (a[i].need > 0) psh(i, L, R); if (L == l && R == r) { for (int j = 0; j < 10; j++) scur[j] += a[i].x[j]; return; } int h = (L + R) / 2; if (l <= h) fsum(i * 2, L, h, l, min(h, r)); if (r > h) fsum(i * 2 + 1, h + 1, R, max(h + 1, l), r); for (int j = 0; j < 10; j++) a[i].x[j] = a[i * 2].x[j] + a[i * 2 + 1].x[j]; } int main() { scanf("%d%d", &n, &m); init(1, 1, n); for (i = 1; i <= n; i++) { scanf("%d", &y); for (j = 1; y > 0; y /= 10, j *= 10) a[b[i]].x[y % 10] += j; } recount(1, 1, n); for (i = 0; i < m; i++) { scanf("%d%d%d", &t, &l, &r); if (t == 1) { scanf("%d%d", &xfr, &y); modify(1, 1, n, l, r); } else { for (int j = 0; j < 10; j++) scur[j] = 0; fsum(1, 1, n, l, r); for (int j = res = 0; j < 10; j++) res += j * scur[j]; printf("%lld\n", res); } } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, n, arr[100005], vis[5005]; deque<int> Q; memset(vis, 0, sizeof vis); scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &arr[i]); sort(arr, arr + n); int maxi = arr[n - 1]; for (i = n - 1; i >= 0; i--) { if (vis[arr[i]] == 0) { vis[arr[i]]++; Q.push_back(arr[i]); } else if (vis[arr[i]] == 1 && arr[i] != maxi) { vis[arr[i]]++; Q.push_front(arr[i]); } } printf("%d\n", Q.size()); for (i = 0; i < Q.size(); i++) { if (i > 0) printf(" "); printf("%d", Q[i]); } printf("\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); int size = 0; int count = 1; for (int i = 0; i < n - 1; i++) { if ((s[i] != s[i + 1])) { count++; size = 0; } else { size++; } if (size == 5) { count++; size = 0; } } cout << count << endl; }
7
CPP
#include <bits/stdc++.h> using namespace std; mt19937 rng(static_cast<unsigned int>( chrono::steady_clock::now().time_since_epoch().count())); template <typename num_t> inline void add_mod(num_t& a, const long long& b, const int& m) { a = (a + b) % m; if (a < 0) a += m; } template <typename num_t> inline bool update_max(num_t& a, const num_t& b) { return a < b ? a = b, true : false; } template <typename num_t> inline bool update_min(num_t& a, const num_t& b) { return a > b ? a = b, true : false; } template <typename num_t> num_t gcd(const num_t& lhs, const num_t& rhs) { return !lhs ? rhs : gcd(rhs % lhs, lhs); } template <typename num_t> num_t lcm(const num_t& lhs, const num_t& rhs) { return lhs / ::gcd(lhs, rhs) * rhs; } template <typename num_t> num_t pw(num_t n, num_t k, const num_t& mod) { if (k == -1) k = mod - 2; num_t res = 1; for (; k > 0; k >>= 1) { if (k & 1) res = 1ll * res * n % mod; n = 1ll * n * n % mod; } return res % mod; } const int inf = 1e9 + 7; const int mod = 998244353; const long long ll_inf = (long long)inf * inf * 9 + 7; const int maxn = 200000 + 7; int n, m; long long a[maxn]; long long sum[maxn]; long long cost[maxn]; struct DisjointSet { vector<int> link; vector<int> max_value; int n, group_count; void init(int n_) { group_count = n = n_; link.assign(n + 1, -1); max_value.resize(n + 1); iota(max_value.begin(), max_value.end(), 0); } int get_root(int u) { return link[u] < 0 ? u : link[u] = get_root(link[u]); } bool is_root(int u) { return u == get_root(u); } bool join(int u, int v) { u = get_root(u); v = get_root(v); if (u == v) return false; if (link[u] > link[v]) swap(u, v); update_max(max_value[u], max_value[v]); link[u] += link[v]; link[v] = u; --group_count; return true; } int size(int u) { return -link[get_root(u)]; } int get_max(int u) { return max_value[get_root(u)]; } bool connected(int u, int v) { return get_root(u) == get_root(v); } } dsu; void solve() { int n, m; cin >> n >> m; dsu.init(n + 2); for (int i = (0), _b = (m); i < _b; ++i) { int u, v; cin >> u >> v; --u; --v; dsu.join(u, v); } int total = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < dsu.get_max(i); ++j) { if (dsu.join(i, j)) { ++total; } } i = dsu.get_max(i); } cout << total << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); const bool multiple_test = false; int test = 1; if (multiple_test) cin >> test; for (int i = 0; i < test; ++i) { solve(); } }
10
CPP
#include <iostream> using namespace std; int main(void){ int a,b,c; cin>>a>>b>>c; int coin=0; int day=0; while(7){ if(coin>=c){ break; } coin=coin+a; day=day+1; if(day%7==0){ coin=coin+b; } } cout<<day<<endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; namespace IO { const int BUFFER_SIZE = 1 << 15; char input_buffer[BUFFER_SIZE]; int input_pos = 0, input_len = 0; char output_buffer[BUFFER_SIZE]; int output_pos = 0; char number_buffer[100]; uint8_t lookup[100]; void _update_input_buffer() { input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin); input_pos = 0; if (input_len == 0) input_buffer[0] = EOF; } inline char next_char(bool advance = true) { if (input_pos >= input_len) _update_input_buffer(); return input_buffer[advance ? input_pos++ : input_pos]; } template <typename T> inline void read_int(T &number) { bool negative = false; number = 0; while (!isdigit(next_char(false))) if (next_char() == '-') negative = true; do { number = 10 * number + (next_char() - '0'); } while (isdigit(next_char(false))); if (negative) number = -number; } template <typename T, typename... Args> inline void read_int(T &number, Args &...args) { read_int(number); read_int(args...); } void _flush_output() { fwrite(output_buffer, sizeof(char), output_pos, stdout); output_pos = 0; } inline void write_char(char c) { if (output_pos == BUFFER_SIZE) _flush_output(); output_buffer[output_pos++] = c; } template <typename T> inline void write_int(T number, char after = '\0') { if (number < 0) { write_char('-'); number = -number; } int length = 0; while (number >= 10) { uint8_t lookup_value = lookup[number % 100]; number /= 100; number_buffer[length++] = (lookup_value & 15) + '0'; number_buffer[length++] = (lookup_value >> 4) + '0'; } if (number != 0 || length == 0) write_char(number + '0'); for (int i = length - 1; i >= 0; i--) write_char(number_buffer[i]); if (after) write_char(after); } void init() { bool exit_success = atexit(_flush_output) == 0; assert(exit_success); for (int i = 0; i < 100; i++) lookup[i] = (i / 10 << 4) + i % 10; } } // namespace IO mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count() * ((uint64_t) new char | 1)); const int BITS = 18; template <typename T> struct xor_basis { T basis[BITS]; int n = 0; T min_value(T start) const { for (int i = 0; i < n; i++) start = min(start, start ^ basis[i]); return start; } T max_value(T start = 0) const { for (int i = 0; i < n; i++) start = max(start, start ^ basis[i]); return start; } bool add(T x) { if (n == BITS) return false; x = min_value(x); if (x == 0) return false; basis[n++] = x; for (int k = n - 1; k > 0 && basis[k] > basis[k - 1]; k--) swap(basis[k], basis[k - 1]); return true; } void merge(const xor_basis<T> &other) { for (int i = 0; i < other.n && n < BITS; i++) add(other.basis[i]); } void merge(const xor_basis<T> &a, const xor_basis<T> &b) { *this = a; merge(b); } }; int main() { IO::init(); int N; IO::read_int(N); vector<int> S(N); for (int &s : S) IO::read_int(s); shuffle(S.begin(), S.end(), rng); vector<int> original; for (int bits = BITS; bits >= 0; bits--) { xor_basis<int> basis; original.clear(); for (int s : S) if (s < 1 << bits && basis.add(s)) original.push_back(s); if ((int)original.size() == bits) break; } int bits = original.size(); vector<int> permutation = {0}; int number = 0; for (int i = 1; i < 1 << bits; i++) { number ^= original[__builtin_ctz(i)]; permutation.push_back(number); } IO::write_int(bits, '\n'); for (int i = 0; i < 1 << bits; i++) IO::write_int(permutation[i], i < (1 << bits) - 1 ? ' ' : '\n'); }
11
CPP
from math import floor,ceil for _ in range(int(input())): L,v,l,r = map(int, input().split()) lanterns_before_end = int(floor(r/v)) # print(lanterns_before_end) lanterns_before_start = int(ceil(l/v))-1 # print(lanterns_before_start) total = int(floor(L/v)) # print(total) print(total - lanterns_before_end + lanterns_before_start)
7
PYTHON3
a = input() if a[0].isupper(): print(a) else: print(a[0].upper()+a[1::])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int arr[10]; int main() { int t; scanf("%d", &t); for (int i = 1; i <= t; i++) { for (int j = 1; j <= 3; j++) { scanf("%d", &arr[j]); } int maxi = max({arr[1], arr[2], arr[3]}); if ((maxi == arr[1]) + (maxi == arr[2]) + (maxi == arr[3]) >= 2) { printf("YES\n"); printf("%d %d %d\n", max({arr[1], arr[2], arr[3]}), min({arr[1], arr[2], arr[3]}), min({arr[1], arr[2], arr[3]})); } else { printf("NO\n"); } } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; using Real = double; using Point = complex< Real >; const Real EPS = 1e-8, PI = acos(-1); inline bool eq(Real a, Real b) { return fabs(b - a) < EPS; } Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); } istream &operator>>(istream &is, Point &p) { Real a, b; is >> a >> b; p = Point(a, b); return is; } ostream &operator<<(ostream &os, Point &p) { os << fixed << setprecision(10) << p.real() << " " << p.imag(); } Point rotate(Real theta, const Point &p) { return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag()); } Real radian_to_degree(Real r) { return (r * 180.0 / PI); } Real degree_to_radian(Real d) { return (d * PI / 180.0); } Real get_angle(const Point &a, const Point &b, const Point &c) { const Point v(b - a), w(c - b); Real alpha = atan2(v.imag(), v.real()), beta = atan2(w.imag(), w.real()); if(alpha > beta) swap(alpha, beta); Real theta = (beta - alpha); return min(theta, 2 * acos(-1) - theta); } namespace std { bool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); } } struct Line { Point a, b; Line() = default; Line(Point a, Point b) : a(a), b(b) {} Line(Real A, Real B, Real C) // Ax + By = C { if(eq(A, 0)) a = Point(0, C / B), b = Point(1, C / B); else if(eq(B, 0)) b = Point(C / A, 0), b = Point(C / A, 1); else a = Point(0, C / B), b = Point(C / A, 0); } friend ostream &operator<<(ostream &os, Line &p) { return os << p.a << " to " << p.b; } friend istream &operator>>(istream &is, Line &a) { return is >> a.a >> a.b; } }; struct Segment : Line { Segment() = default; Segment(Point a, Point b) : Line(a, b) {} }; struct Circle { Point p; Real r; Circle() = default; Circle(Point p, Real r) : p(p), r(r) {} }; using Points = vector< Point >; using Polygon = vector< Point >; using Segments = vector< Segment >; using Lines = vector< Line >; using Circles = vector< Circle >; Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); } Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C int ccw(const Point &a, Point b, Point c) { b = b - a, c = c - a; if(cross(b, c) > EPS) return +1; // "COUNTER_CLOCKWISE" if(cross(b, c) < -EPS) return -1; // "CLOCKWISE" if(dot(b, c) < 0) return +2; // "ONLINE_BACK" if(norm(b) < norm(c)) return -2; // "ONLINE_FRONT" return 0; // "ON_SEGMENT" } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A bool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A bool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A Point projection(const Line &l, const Point &p) { double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + (l.a - l.b) * t; } Point projection(const Segment &l, const Point &p) { double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + (l.a - l.b) * t; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B Point reflection(const Line &l, const Point &p) { return p + (projection(l, p) - p) * 2.0; } bool intersect(const Line &l, const Point &p) { return abs(ccw(l.a, l.b, p)) != 1; } bool intersect(const Line &l, const Line &m) { return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS; } bool intersect(const Segment &s, const Point &p) { return ccw(s.a, s.b, p) == 0; } bool intersect(const Line &l, const Segment &s) { return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS; } Real distance(const Line &l, const Point &p); bool intersect(const Circle &c, const Line &l) { return distance(l, c.p) <= c.r + EPS; } bool intersect(const Circle &c, const Point &p) { return abs(abs(p - c.p) - c.r) < EPS; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B bool intersect(const Segment &s, const Segment &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } int intersect(const Circle &c, const Segment &l) { if(norm(projection(l, c.p) - c.p) - c.r * c.r > EPS) return 0; auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b); if(d1 < c.r + EPS && d2 < c.r + EPS) return 0; if(d1 < c.r - EPS && d2 > c.r + EPS || d1 > c.r + EPS && d2 < c.r - EPS) return 1; const Point h = projection(l, c.p); if(dot(l.a - h, l.b - h) < 0) return 2; return 0; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp int intersect(Circle c1, Circle c2) { if(c1.r < c2.r) swap(c1, c2); Real d = abs(c1.p - c2.p); if(c1.r + c2.r < d) return 4; if(eq(c1.r + c2.r, d)) return 3; if(c1.r - c2.r < d) return 2; if(eq(c1.r - c2.r, d)) return 1; return 0; } Real distance(const Point &a, const Point &b) { return abs(a - b); } Real distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); } Real distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); } Real distance(const Segment &s, const Point &p) { Point r = projection(s, p); if(intersect(s, r)) return abs(r - p); return min(abs(s.a - p), abs(s.b - p)); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D Real distance(const Segment &a, const Segment &b) { if(intersect(a, b)) return 0; return min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)}); } Real distance(const Line &l, const Segment &s) { if(intersect(l, s)) return 0; return min(distance(l, s.a), distance(l, s.b)); } Point crosspoint(const Line &l, const Line &m) { Real A = cross(l.b - l.a, m.b - m.a); Real B = cross(l.b - l.a, l.b - m.a); if(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a; return m.a + (m.b - m.a) * B / A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C Point crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D pair< Point, Point > crosspoint(const Circle &c, const Line l) { Point pr = projection(l, c.p); Point e = (l.b - l.a) / abs(l.b - l.a); if(eq(distance(l, c.p), c.r)) return {pr, pr}; double base = sqrt(c.r * c.r - norm(pr - c.p)); return {pr - e * base, pr + e * base}; } pair< Point, Point > crosspoint(const Circle &c, const Segment &l) { Line aa = Line(l.a, l.b); if(intersect(c, l) == 2) return crosspoint(c, aa); auto ret = crosspoint(c, aa); if(dot(l.a - ret.first, l.b - ret.first) < 0) ret.second = ret.first; else ret.first = ret.second; return ret; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E pair< Point, Point > crosspoint(const Circle &c1, const Circle &c2) { Real d = abs(c1.p - c2.p); Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d)); Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real()); Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r); Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r); return {p1, p2}; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F pair< Point, Point > tangent(const Circle &c1, const Point &p2) { return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r))); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G Lines tangent(Circle c1, Circle c2) { Lines ret; if(c1.r < c2.r) swap(c1, c2); Real g = norm(c1.p - c2.p); if(eq(g, 0)) return ret; Point u = (c2.p - c1.p) / sqrt(g); Point v = rotate(PI * 0.5, u); for(int s : {-1, 1}) { Real h = (c1.r + s * c2.r) / sqrt(g); if(eq(1 - h * h, 0)) { ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r); } else if(1 - h * h > 0) { Point uu = u * h, vv = v * sqrt(1 - h * h); ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s); ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s); } } return ret; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_B bool is_convex(const Polygon &p) { int n = (int) p.size(); for(int i = 0; i < n; i++) { if(ccw(p[(i + n - 1) % n], p[i], p[(i + 1) % n]) == -1) return false; } return true; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_A Polygon convex_hull(Polygon &p) { int n = (int) p.size(), k = 0; if(n <= 2) return p; sort(p.begin(), p.end()); vector< Point > ch(2 * n); for(int i = 0; i < n; ch[k++] = p[i++]) { while(k >= 2 && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k; } for(int i = n - 2, t = k + 1; i >= 0; ch[k++] = p[i--]) { while(k >= t && cross(ch[k - 1] - ch[k - 2], p[i] - ch[k - 1]) < 0) --k; } ch.resize(k - 1); return ch; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_C enum { OUT, ON, IN }; int contains(const Polygon &Q, const Point &p) { bool in = false; for(int i = 0; i < Q.size(); i++) { Point a = Q[i] - p, b = Q[(i + 1) % Q.size()] - p; if(a.imag() > b.imag()) swap(a, b); if(a.imag() <= 0 && 0 < b.imag() && cross(a, b) < 0) in = !in; if(cross(a, b) == 0 && dot(a, b) <= 0) return ON; } return in ? IN : OUT; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033 void merge_segments(vector< Segment > &segs) { auto merge_if_able = [](Segment &s1, const Segment &s2) { if(abs(cross(s1.b - s1.a, s2.b - s2.a)) > EPS) return false; if(ccw(s1.a, s2.a, s1.b) == 1 || ccw(s1.a, s2.a, s1.b) == -1) return false; if(ccw(s1.a, s1.b, s2.a) == -2 || ccw(s2.a, s2.b, s1.a) == -2) return false; s1 = Segment(min(s1.a, s2.a), max(s1.b, s2.b)); return true; }; for(int i = 0; i < segs.size(); i++) { if(segs[i].b < segs[i].a) swap(segs[i].a, segs[i].b); } for(int i = 0; i < segs.size(); i++) { for(int j = i + 1; j < segs.size(); j++) { if(merge_if_able(segs[i], segs[j])) { segs[j--] = segs.back(), segs.pop_back(); } } } } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1033 vector< vector< int > > segment_arrangement(vector< Segment > &segs, vector< Point > &ps) { vector< vector< int > > g; int N = (int) segs.size(); for(int i = 0; i < N; i++) { ps.emplace_back(segs[i].a); ps.emplace_back(segs[i].b); for(int j = i + 1; j < N; j++) { const Point p1 = segs[i].b - segs[i].a; const Point p2 = segs[j].b - segs[j].a; if(cross(p1, p2) == 0) continue; if(intersect(segs[i], segs[j])) { ps.emplace_back(crosspoint(segs[i], segs[j])); } } } sort(begin(ps), end(ps)); ps.erase(unique(begin(ps), end(ps)), end(ps)); int M = (int) ps.size(); g.resize(M); for(int i = 0; i < N; i++) { vector< int > vec; for(int j = 0; j < M; j++) { if(intersect(segs[i], ps[j])) { vec.emplace_back(j); } } for(int j = 1; j < vec.size(); j++) { g[vec[j - 1]].push_back(vec[j]); g[vec[j]].push_back(vec[j - 1]); } } return (g); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_C Polygon convex_cut(const Polygon &U, Line l) { Polygon ret; for(int i = 0; i < U.size(); i++) { Point now = U[i], nxt = U[(i + 1) % U.size()]; if(ccw(l.a, l.b, now) != -1) ret.push_back(now); if(ccw(l.a, l.b, now) * ccw(l.a, l.b, nxt) < 0) { ret.push_back(crosspoint(Line(now, nxt), l)); } } return (ret); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_3_A Real area2(const Polygon &p) { Real A = 0; for(int i = 0; i < p.size(); ++i) { A += cross(p[i], p[(i + 1) % p.size()]); } return A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_H Real area2(const Polygon &p, const Circle &c) { if(p.size() < 3) return 0.0; function< Real(Circle, Point, Point) > cross_area = [&](const Circle &c, const Point &a, const Point &b) { Point va = c.p - a, vb = c.p - b; Real f = cross(va, vb), ret = 0.0; if(eq(f, 0.0)) return ret; if(max(abs(va), abs(vb)) < c.r + EPS) return f; if(distance(Segment(a, b), c.p) > c.r - EPS) return c.r * c.r * arg(vb * conj(va)); auto u = crosspoint(c, Segment(a, b)); vector< Point > tot{a, u.first, u.second, b}; for(int i = 0; i + 1 < tot.size(); i++) { ret += cross_area(c, tot[i], tot[i + 1]); } return ret; }; Real A = 0; for(int i = 0; i < p.size(); i++) { A += cross_area(c, p[i], p[(i + 1) % p.size()]); } return A; } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_4_B Real convex_diameter(const Polygon &p) { int N = (int) p.size(); int is = 0, js = 0; for(int i = 1; i < N; i++) { if(p[i].imag() > p[is].imag()) is = i; if(p[i].imag() < p[js].imag()) js = i; } Real maxdis = norm(p[is] - p[js]); int maxi, maxj, i, j; i = maxi = is; j = maxj = js; do { if(cross(p[(i + 1) % N] - p[i], p[(j + 1) % N] - p[j]) >= 0) { j = (j + 1) % N; } else { i = (i + 1) % N; } if(norm(p[i] - p[j]) > maxdis) { maxdis = norm(p[i] - p[j]); maxi = i; maxj = j; } } while(i != is || j != js); return sqrt(maxdis); } // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_5_A Real closest_pair(Points ps) { if(ps.size() <= 1) throw (0); sort(begin(ps), end(ps)); auto compare_y = [&](const Point &a, const Point &b) { return imag(a) < imag(b); }; vector< Point > beet(ps.size()); const Real INF = 1e18; function< Real(int, int) > rec = [&](int left, int right) { if(right - left <= 1) return INF; int mid = (left + right) >> 1; auto x = real(ps[mid]); auto ret = min(rec(left, mid), rec(mid, right)); inplace_merge(begin(ps) + left, begin(ps) + mid, begin(ps) + right, compare_y); int ptr = 0; for(int i = left; i < right; i++) { if(abs(real(ps[i]) - x) >= ret) continue; for(int j = 0; j < ptr; j++) { auto luz = ps[i] - beet[ptr - j - 1]; if(imag(luz) >= ret) break; ret = min(ret, abs(luz)); } beet[ptr++] = ps[i]; } return ret; }; return rec(0, (int) ps.size()); } int main() { int N, R; cin >> N >> R; Polygon ps(N); double ret = 0.0; auto check = [&](double x) { double vv = 0.0; double low = 1e9, high = -1e9; Line l = Line(Point(x, 0), Point(x, 1)); for(int i = 0; i < N; i++) { if(intersect(l, Segment(ps[i], ps[(i + 1) % N]))) { auto point = crosspoint(l, Segment(ps[i], ps[(i + 1) % N])); low = min(low, imag(point)); high = max(high, imag(point)); } } for(int i = 0; i < 100; i++) { double left = (low * 2 + high) / 3; double right = (low + high * 2) / 3; auto A = area2(ps, Circle(Point(x, left), R)); auto B = area2(ps, Circle(Point(x, right), R)); vv = max(vv, max(A, B)); if(A < B) low = left; else high = right; } ret = max(ret, vv); return (vv); }; double low = 1e9, high = -1e9; for(int i = 0; i < N; i++) { cin >> ps[i]; low = min(low, real(ps[i])); high = max(high, real(ps[i])); } for(int i = 0; i < 100; i++) { double left = (low * 2 + high) / 3; double right = (low + high * 2) / 3; if(check(left) < check(right)) low = left; else high = right; } cout << fixed << setprecision(10) << ret * 0.5 << endl; }
0
CPP
#include<stdio.h> #include<string.h> #include<math.h> int main(void) { int a,s[101],d,f,g[101],h,i,j,z; while(1){ scanf("%d",&a); if(a==0) break; for(i=0;i<=a;i++) scanf("%d",&s[i]); for(i=0;i<=a;i++){ d=0; for(j=0;j<=a;j++){ if(i!=j){ g[d]=s[j]; d++; } if(i==j) z=s[i]; } h=0; f=g[a-2]-g[a-3]; for(j=a-1;j>0;j--){ if(g[j]-g[j-1]!=f){ h=1; break; } } if(h==0){ printf("%d\n",z); break; } } } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; using i64 = long long; const i64 MOD = 1e9 + 7; const i64 INF = i64(1e18) + 7; template <typename T> bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template <typename T> bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } struct LowLink{ vector<vector<int>>& edges; // 関節点 vector<int> art; vector<pair<int,int>> bridge; vector<int> ord, low; int k; void dfs(int idx, int par){ ord[idx] = k++; low[idx] = ord[idx]; bool is_art = false; int cnt = 0; for(auto& to : edges[idx]){ if(ord[to] == -1){ ++cnt; dfs(to, idx); low[idx] = min(low[idx], low[to]); is_art |= par != -1 && low[to] >= ord[idx]; if(ord[idx] < low[to]) bridge.emplace_back(idx, to); }else if(to != par) low[idx] = min(low[idx], ord[to]); } is_art |= (par == -1 && cnt > 1); if(is_art) art.emplace_back(idx); } LowLink(vector<vector<int>>& edges) : edges(edges), ord(edges.size(), -1), low(edges.size(), 0), k(0) { for(int i = 0; i < edges.size(); ++i) if(ord[i] == -1) dfs(i, -1); for(auto& b : bridge) b = minmax(b.first, b.second); sort(art.begin(), art.end()); sort(bridge.begin(), bridge.end()); } }; struct Result{ int group_cnt; vector<int> group; vector<vector<int>> group_elm_list; // 同じ二重辺連結成分の辺をグループごとに列挙する, 片方向のみ(辺数倍化しない) vector<vector<pair<int,int>>> same_group_edges; // 橋, 片方向のみ vector<pair<int,int>> bridges; // 関節点 vector<int> arts; vector<vector<int>> tree_graph; vector<int> par; vector<vector<int>> childs; }; Result two_edge_connected_components_tree(vector<vector<int>>& edges){ int n = edges.size(); LowLink ll(edges); vector<vector<int>> not_bridge_graph(n); for(int i = 0; i < n; ++i) for(auto j : edges[i]){ pair<int,int> min_max = minmax(i, j); auto iter = lower_bound(ll.bridge.begin(), ll.bridge.end(), min_max); if(iter == ll.bridge.end() || *iter != min_max) not_bridge_graph[i].push_back(j); } vector<int> group(n, -1); function<void(int)> group_dfs = [&](int x){ for(auto y : not_bridge_graph[x]) if(group[y] == -1){ group[y] = group[x]; group_dfs(y); } }; int group_cnt = 0; for(int i = 0; i < n; ++i) if(group[i] == -1){ group[i] = group_cnt++; group_dfs(i); } vector<vector<int>> group_elm_list(group_cnt); for(int i = 0; i < n; ++i) group_elm_list[group[i]].push_back(i); vector<vector<pair<int,int>>> same_group_edges(group_cnt); vector<vector<int>> tree_edges(group_cnt); vector<int> par(group_cnt, -1); vector<vector<int>> childs(group_cnt); for(int i = 0; i < n; ++i) for(auto j : edges[i]) if(group[i] == group[j] && i < j) same_group_edges[group[i]].emplace_back(i, j); for(auto& p : ll.bridge){ tree_edges[group[p.first]].push_back(group[p.second]); tree_edges[group[p.second]].push_back(group[p.first]); } vector<bool> flag(n, false); function<void(int)> tree_dfs = [&](int x){ for(auto y : tree_edges[x]) if(!flag[y]){ flag[y] = true; par[y] = x; childs[x].push_back(y); tree_dfs(y); } }; flag[0] = true; tree_dfs(0); Result res; res.group_cnt = group_cnt; res.group_elm_list = move(group_elm_list); res.same_group_edges = move(same_group_edges); res.bridges = move(ll.bridge); res.arts = move(ll.art); res.group = move(group); res.tree_graph = move(tree_edges); res.par = move(par); res.childs = move(childs); return res; } struct BinaryLifting{ int n; vector<vector<int>> next; vector<int> depth; BinaryLifting(vector<vector<int>>& edges, int root = 0) : n(edges.size()), depth(n, -1){ vector<int> par(n, -1); function<void(int)> dfs = [&](int x){ for(auto y : edges[x]) if(depth[y] == -1){ depth[y] = depth[x] + 1; par[y] = x; dfs(y); } }; depth[root] = 0; dfs(0); next.push_back(move(par)); for(int k = 0;; ++k){ bool fl = false; next.emplace_back(n, -1); for(int i = 0; i < n; ++i){ next[k + 1][i] = (next[k][i] == -1 ? -1 : next[k][next[k][i]]); if(next[k + 1][i] != -1) fl = true; } if(!fl) break; } } // kth_next(x, 0) => x int kth_next(int x, int k){ for(int i = 0; i < next.size() && k; ++i){ if(k & (1 << i)){ x = next[i][x]; if(x == -1) break; } } return x; } int lca(int x, int y){ int min_depth = min(depth[x], depth[y]); x = kth_next(x, depth[x] - min_depth); y = kth_next(y, depth[y] - min_depth); if(x == y) return x; for(int i = next.size() - 1; i >= 0; --i) if(next[i][x] != next[i][y]){ x = next[i][x]; y = next[i][y]; } return next[0][x]; } }; signed main(){ int n, m; cin >> n >> m; vector<int> a(m), b(m); for(int i = 0; i < m; ++i){ cin >> a[i] >> b[i]; --a[i], --b[i]; } int k; cin >> k; vector<int> u(k), v(k); for(int i = 0; i < k; ++i){ cin >> u[i] >> v[i]; --u[i], --v[i]; } vector<vector<int>> edges(n); for(int i = 0; i < m; ++i){ edges[a[i]].push_back(b[i]); edges[b[i]].push_back(a[i]); } Result res = two_edge_connected_components_tree(edges); BinaryLifting tree(res.tree_graph); vector<int> fl1(res.group_cnt, 0); vector<int> fl2(res.group_cnt, 0); for(int i = 0; i < k; ++i){ int uu = res.group[u[i]]; int vv = res.group[v[i]]; int lca = tree.lca(uu, vv); ++fl1[uu]; ++fl2[vv]; --fl1[lca]; --fl2[lca]; } function<void(int)> df = [&](int x){ for(auto& y : res.childs[x]){ df(y); fl1[x] += fl1[y]; fl2[x] += fl2[y]; } }; df(0); for(int i = 1; i < n; ++i) if(fl1[i] && fl2[i]){ cout << "No" << endl; return 0; } map<pair<int,int>, int> edge_data; for(int i = 1; i < res.group_cnt; ++i){ int j = tree.kth_next(i, 1); if(fl1[i]) edge_data[minmax(i, j)] = 1; else edge_data[minmax(i, j)] = 2; } map<pair<int,int>, int> edge_raw_data; for(int i = 0; i < res.group_cnt; ++i){ vector<int>& elm_list = res.group_elm_list[i]; vector<pair<int,int>>& edge_list = res.same_group_edges[i]; int group_size = elm_list.size(); vector<vector<int>> group_edges(group_size); for(auto& p : edge_list){ int p1 = distance(elm_list.begin(), lower_bound(elm_list.begin(), elm_list.end(), p.first)); int p2 = distance(elm_list.begin(), lower_bound(elm_list.begin(), elm_list.end(), p.second)); group_edges[p1].emplace_back(p2); group_edges[p2].emplace_back(p1); } vector<bool> visited(group_size, false); function<void(int,int)> f = [&](int x, int prev){ for(auto y : group_edges[x]){ if(y == prev) continue; if(!visited[y]){ auto p = minmax(elm_list[x], elm_list[y]); edge_raw_data[minmax(elm_list[x], elm_list[y])] = 1; visited[y] = true; f(y, x); } else{ auto p = minmax(elm_list[x], elm_list[y]); edge_raw_data[minmax(elm_list[x], elm_list[y])] = 2; } } }; visited[0] = true; f(0, -1); } vector<int> ans(m, -1); for(int i = 0; i < m; ++i){ if(res.group[a[i]] != res.group[b[i]]) ans[i] = edge_data[minmax(res.group[a[i]], res.group[b[i]])]; else ans[i] = edge_raw_data[minmax(a[i], b[i])]; } cout << "Yes" << endl; for(int i = 0; i < m; ++i){ if(ans[i] == 2) cout << a[i] + 1 << " " << b[i] + 1 << endl; else cout << b[i] + 1 << " " << a[i] + 1 << endl; } return 0; }
0
CPP
n=int(input()) s=input() s=list(s) count=0 for i in range(1,n): if(s[i-1]==s[i]): count+=1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; map<string, bool> was; string s; int ans; int main() { cin >> s; for (int i = 0; i < s.size(); i++) { char last = s[s.size() - 1]; if (!was[s]) ans++, was[s] = 1; s.erase(s.size() - 1, 1); s = last + s; } cout << ans; }
7
CPP
from math import sin,radians def C2(n): interiorangle = 360/(2*n) side = sin(radians(45)) angle = 45 number = (n-1)/2 for i in range(int(number)): angle += interiorangle if angle > 90: angle = 180 - angle side += sin(radians(angle)) angle = 180 - angle else: side += sin(radians(angle)) angle = 45 for i in range(int(number)): angle -= interiorangle if angle < 0: angle = -1 * angle side += sin(radians(angle)) angle = -1 * angle else: side += sin(radians(angle)) print(float(side)) a = int(input()) for _ in range(a): x = int(input()) C2(x)
9
PYTHON3
a = int(input()) b = 0 if a % 5 == 0: print((a // 5)) else: print(int(a // 5 + 1))
7
PYTHON3
def inp(): return list(map(int, input().split())) def solve(): (black, white) = ('*', '.') [n, m, priceOf1, priceOf2] = inp() theaterPaves = [] for _ in range(n): theaterPaves.append(list(input())) cost = 0 for i in range(n): for j in range(m): if theaterPaves[i][j] == black: continue if priceOf2 >= 2 * priceOf1: cost += priceOf1 else: if j == (m - 1): cost += priceOf1 elif theaterPaves[i][j + 1] == white: cost += priceOf2 theaterPaves[i][j + 1] = black else: cost += priceOf1 return cost if __name__=='__main__': [tests] = inp() for _ in range(tests): print(solve())
8
PYTHON3
def solve(n, a): chest = sum(a[0::3]) biceps = sum(a[1::3]) back = sum(a[2::3]) if chest > biceps and chest > back: return "chest" if biceps > chest and biceps > back: return "biceps" if back > chest and back > biceps: return "back" def main(): n = int(input()) a = list(map(int, input().split())) print(solve(n, a)) main()
7
PYTHON3
#include <bits/stdc++.h> int main() { long long n, m, k; long long a, b; scanf("%lld%lld%lld", &n, &m, &k); if (k <= n - 1) printf("%lld 1", k + 1); else { k = k - n; a = k / (m - 1); b = k % (m - 1); if (a % 2 == 0) { printf("%lld %lld", n - a, b + 2); } else { printf("%lld %lld", n - a, m - b); } } return 0; }
8
CPP
#include <bits/stdc++.h> long long modinv(long long x) { long long i = 1, j = 998244353 - 2; while (j > 0) { if (j % 2 == 1) i = i * x % 998244353; x = x * x % 998244353; j /= 2; } return i; } int main() { long long n = 0, k; scanf("%lld", &n); long long p = 0, q = 1, ex = 0; while (n--) { scanf("%lld", &k); ex = (((ex + 1) * 100) % 998244353 * modinv(k)) % 998244353; } printf("%lld\n", ex); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long Power(long long $a, long long $b) { long long $result = 1; $a %= 1000000007; while ($b) { if ($b % 2 == 1) $result *= $a; $a *= $a; $a %= 1000000007; $result %= 1000000007; $b /= 2; } return $result; } long long Mod_Inv(long long $a) { $a %= 1000000007; $a = Power($a, 1000000007 - 2); return $a; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; long long x = Power(3, n); if (n % 2 == 0) x += 3; else x -= 3; x %= 1000000007; long long i = Mod_Inv(4); i %= 1000000007; long long y = (x * i) % 1000000007; cout << y; }
11
CPP
import sys input = sys.stdin.readline n = int(input()) h = tuple(map(lambda x:int(x)-1,input().split())) a = tuple(map(int,input().split())) #update(i,x):Aiをxに更新する def update(i,x): k = i + num - 1 while k >= 0: seg[k] = max(x,seg[k]) k = (k-1)//2 #query(a,b,0,0,num):[a,b)の最大値 def query(a,b,k,l,r): if r <= a or b <= l: return -float("inf") elif a <= l and r <= b: return seg[k] else: return max(query(a,b,2*k+1,l,(l+r)//2),query(a,b,2*k+2,(l+r)//2,r)) #初期化 #num : nより大きい最小の2のべき乗 num = 1 while num <= n: num *= 2 seg = [0]*(2*num-1) #dp[i]:i番目が最後となる時の美しさの最大値 dp = [a[0]] + [0]*(n-1) update(h[0],a[0]) for i in range(1,n): dp[i] = max(0,query(0,h[i],0,0,num))+a[i] update(h[i],dp[i]) print(max(dp))
0
PYTHON3
n,m,a = map(int,input().split()) re = (-n//a*(-m//a)) print(re)
7
PYTHON3
#include<iostream> #include<map> #include<string> using namespace std; int main() { map<string , int> cpp; string str; int n, com, q; cin >> q; while (q--) { cin >> com >> str; switch (com) { case 0: cin >> n; cpp[str] = n; break; case 1: cout << cpp[str] << endl; break; case 2: cpp.erase(str); break; } } return 0; }
0
CPP
#include<bits/stdc++.h> using namespace std; #define rep(i,a) for(int i = 0; i < (a); i++) int main() { int n; while(cin >> n,n) { vector<pair<double,int>> data; rep(i,n){ int index, h, w; scanf("%d %d %d", &index, &h, &w); data.push_back(make_pair(abs(22.0 - 10000.0 * w / (h * h)),index)); } sort(data.begin(), data.end()); cout << data[0].second << endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; struct team { long long t; long long w; }; bool operator<(const team &a, const team &b) { return a.t > b.t; } int main() { int n; cin >> n; vector<team> t(n - 1); team d; cin >> d.t >> d.w; for (int i = 0; i < n - 1; ++i) cin >> t[i].t >> t[i].w; sort((t).begin(), (t).end()); t.push_back({0, 0}); int ans = n; long long cur = 0; long long last = d.t; set<pair<long long, int>> s1, s2; for (int i = 0; i < n; ++i) { int l = i + 1; if (t[i].t <= d.t) { cur += last - t[i].t; last = t[i].t; while (!s1.empty() && !s2.empty() && (--s1.end())->first > s2.begin()->first) { pair<long long, int> x = *(--s1.end()); pair<long long, int> y = *s2.begin(); cur += x.first - y.first; s1.erase(x); s2.erase(y); s1.insert(y); s2.insert(x); } while (!s2.empty() && s2.begin()->first <= cur) { pair<long long, int> x = *s2.begin(); cur -= x.first; s1.insert(x); s2.erase(x); } ans = min(ans, l - (int)(s1).size()); } s2.insert({t[i].w - t[i].t + 1, i}); while (i < n && t[i].t == t[i + 1].t) { ++i; s2.insert({t[i].w - t[i].t + 1, i}); } } cout << ans << endl; return 0; }
10
CPP
import sys """ sys.stdin = open("/Users/isym444/Desktop/PythonCP/CP1/Codewars/Practice/input.txt", "r") sys.stdout = open("/Users/isym444/Desktop/PythonCP/CP1/Codewars/Practice/output.txt", "w") """ a = int(input()) if a % 2 == 0: b = int(a / 2) print(b) c = [2 for _ in range(b)] c = list(str(i) for i in c) c = " ".join(c) print(c) else: b = int(a // 2) print(b) c = [2 for _ in range(b - 1)] c.append(3) c = list(str(i) for i in c) c = " ".join(c) print(c)
7
PYTHON3
#include <bits/stdc++.h> #pragma optimize("Ofast") using namespace std; using ll = long long; const int N = 2e5 + 5; vector<int> g[N]; bool vis[N]; int cnt; void dfs(int s) { ++cnt; vis[s] = true; for (auto it : g[s]) { if (!vis[it]) { dfs(it); } } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; g[i + 1].push_back(x); g[x].push_back(i + 1); } deque<ll> d; for (int i = 1; i <= n; ++i) { if (!vis[i]) { cnt = 0; dfs(i); d.push_back(cnt); } } sort(d.rbegin(), d.rend()); if (d.size() == 1) d.push_back(0); ll ans = (d[0] + d[1]) * (d[0] + d[1]); for (int i = 2; i < d.size(); ++i) { ans += d[i] * d[i]; } cout << ans << "\n"; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 50000 + 10; struct query { int type, x, y; } qu[maxn]; int n, fa[maxn]; long long val[maxn], sz[maxn]; vector<int> v[maxn]; bool mark[maxn], have[maxn]; long long d[maxn]; int dfs(int x) { sz[x] = 1; int ret = 0; for (auto i : v[x]) ret += dfs(i), sz[x] += sz[i]; for (auto i : v[x]) d[i] = (sz[x] - sz[i]) * val[x]; have[x] = ((ret || mark[x]) ? 1 : 0); if (ret >= 2 || mark[x]) { mark[x] = 1; return 1; } else return ret; } struct P { int to, ssum; long long dsum; }; vector<P> v2[maxn]; int fa2[maxn]; long long sz2[maxn], sz2sum[maxn]; long long pnum[maxn]; void dfs2(int x, bool isfir = 0) { if (v2[x].empty()) { if (!isfir) return; pnum[x] = sz[x] * sz[x]; for (auto i : v[x]) pnum[x] -= sz[i] * sz[i]; return; } sz[x] = 1 + sz2[x]; pnum[x] = 0; for (auto i : v2[x]) dfs2(i.to, isfir), sz[x] += sz[i.to] + i.ssum, pnum[x] -= (sz[i.to] + i.ssum) * (sz[i.to] + i.ssum); pnum[x] += sz[x] * sz[x] - sz2sum[x]; } int getid(int x, int y) { for (int i = 0;; i++) if (v2[x][i].to == y) return i; } void build_graph(int num) { fill(mark, mark + n + 1, 0); fill(have, have + n + 1, 0); for (int i = 1; i <= n; i++) v[i].clear(), v2[i].clear(); for (int i = 2; i <= n; i++) v[fa[i]].push_back(i); for (int i = 0; i < num; i++) { mark[qu[i].x] = 1; if (qu[i].type == 1) mark[qu[i].y] = 1; } mark[1] = 1; dfs(1); for (int i = 1; i <= n; i++) if (mark[i]) { sz2[i] = sz2sum[i] = 0; for (auto j : v[i]) if (!have[j]) sz2[i] += sz[j], sz2sum[i] += sz[j] * sz[j]; } for (int i = 2; i <= n; i++) if (mark[i]) { int j, ssum = 0; long long dsum = 0LL; for (j = i;; j = fa[j]) { dsum += d[j]; if (mark[fa[j]]) break; } ssum = sz[j] - sz[i]; j = fa[j]; v2[j].push_back((P){i, ssum, dsum}); fa2[i] = j; } } bool first = 1; void process(int num) { build_graph(num); long long nowans = 0LL, tot = (long long)n * n; for (int i = 1; i <= n; i++) { long long add = sz[i] * sz[i]; for (auto j : v[i]) add -= sz[j] * sz[j]; nowans += add * val[i]; } if (first) printf("%.15f\n", ((double)nowans) / tot), first = 0; dfs2(1, 1); for (int i = 0; i < num; i++) { if (qu[i].type == 2) { int x = qu[i].x, nval = qu[i].y; nowans += pnum[x] * (nval - val[x]); for (auto &j : v2[x]) j.dsum += (sz[x] - j.ssum - sz[j.to]) * (nval - val[x]); val[x] = nval; printf("%.15f\n", ((double)nowans) / tot); continue; } int x = qu[i].y, y = qu[i].x, id; for (int j = x; j != 1; j = fa2[j]) if (j == y || y == 1) { swap(x, y); break; } fa[y] = x; long long dif = 0LL; for (int j = y; j != 1; j = fa2[j]) { id = getid(fa2[j], j); dif -= v2[fa2[j]][id].dsum; } for (int j = y; j != 1; j = fa2[j]) { sz[fa2[j]] -= sz[y]; for (auto &k : v2[fa2[j]]) if (k.to != j) k.dsum -= val[fa2[j]] * sz[y]; } int f = fa2[y]; id = getid(f, y); sz2[f] += v2[f][id].ssum; sz2sum[f] += (long long)v2[f][id].ssum * v2[f][id].ssum; swap(v2[f][id], v2[f][v2[f].size() - 1]); v2[f].pop_back(); v2[x].push_back((P){y, 0, val[x] * sz[x]}); fa2[y] = x; if (v2[f].empty()) pnum[f] = sz[f] * sz[f] - sz2sum[f]; for (int j = y; j != 1; j = fa2[j]) { sz[fa2[j]] += sz[y]; for (auto &k : v2[fa2[j]]) if (k.to != j) k.dsum += val[fa2[j]] * sz[y]; id = getid(fa2[j], j); dif += v2[fa2[j]][id].dsum; } nowans += 2 * dif * sz[y]; printf("%.15f\n", ((double)nowans) / tot); dfs2(1); } } int main() { scanf("%d", &n); fa[1] = 1; for (int i = 2; i <= n; i++) scanf("%d", &fa[i]); for (int i = 1; i <= n; i++) scanf("%I64d", &val[i]); int Q; scanf("%d", &Q); int sq = (int)sqrt(Q + 0.5); for (int i = 0; i < Q; i++) { char c[5]; scanf("%s%d%d", c, &qu[i % sq].x, &qu[i % sq].y); qu[i % sq].type = (c[0] == 'P' ? 1 : 2); if ((i % sq) == sq - 1 || i == Q - 1) process(i % sq + 1); } }
11
CPP
a=int(input()) b=int(input()) c=int(input()) s1=a+b+c s2=a*b+c s3=a*(b+c) s4=a+b*c s5=(a+b)*c s6=a*b*c print(max(s1,s2,s3,s4,s5,s6))
7
PYTHON3
n, m = map(int, input().split()) ans = 0 A = [input() for i in range(n)] for i in range(n - 1): for j in range(m - 1): if sorted(A[i][j] + A[i][j + 1] + A[i + 1][j] + A[i + 1][j + 1]) == sorted("face"): ans += 1 print(ans)
7
PYTHON3