solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
t=int(input()) for count in range(t): n=int(input()) h_list=[int(i) for i in input().split(" ")] delta_list=[] for i in range(n-1): delta_list.append(h_list[i+1]-h_list[i]) if max(delta_list)>0: max_up=max(delta_list) else: max_up=0 if min(delta_list)<0: max_down=min(delta_list)*(-1) else: max_down=0 print("%d %d" %(max_up,max_down))
0
PYTHON3
from collections import deque def main(): h, w = map(int, input().split()) cs = list(map(int, input().split())) ds = list(map(int, input().split())) S = [[s for s in input()] for _ in range(h)] curq = deque() nexq = deque([(cs[0] - 1, cs[1] - 1)]) l = 0 while len(nexq) > 0: curq, nexq = nexq, deque() while len(curq) > 0: y, x = curq.popleft() if S[y][x] != ".": continue S[y][x] = l for dy in range(-2, 3): for dx in range(-2, 3): if 0 <= y + dy < h and 0 <= x + dx < w and S[y + dy][x + dx] == ".": if abs(dx) + abs(dy) < 2: curq.append((y + dy, x + dx)) else: nexq.append((y + dy, x + dx)) l += 1 ans = S[ds[0] - 1][ds[1] - 1] print(ans if ans != "." else -1) if __name__ == '__main__': main()
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, a[1100]; char mp[10000][10000]; int main() { scanf("%d", &n); int X = 0, Y = 0, high = 0, low = 0; for (int i = 0; i < n; i++) { scanf("%d", a + i); X += a[i]; Y += a[i] * (i % 2 == 0 ? 1 : -1); high = max(Y, high); low = min(Y, low); } int x = high, y = 0; memset(mp, 32, sizeof(mp)); for (int i = 0; i < n; i++) { for (int j = 0; j < a[i]; j++) { if (i % 2 == 0) { mp[x][y] = '/'; y++; x--; } else { mp[x][y] = '\\'; y++; x++; } } if (i % 2 == 0) x++; else x--; } for (int i = 1; i <= high - low; i++) { for (int j = 0; j < X; j++) { printf("%c", mp[i][j]); } putchar(10); } return 0; }
9
CPP
def mult(l): p = 1 for i in l: p*=i return p k = int(input()) s = 'codeforces' p = [1]*10 i = 0 while mult(p)<k: if i==10: i=0 p[i] += 1 i+=1 for i in range(10): print(s[i]*p[i],end='') print()
8
PYTHON3
from sys import stdin, setrecursionlimit setrecursionlimit(100000) input = stdin.readline def solve(s, c): if len(s) == 1: if s[0] == chr(c + ord("a")): return 0 else: return 1 current = 0 for i in range(len(s) // 2): if s[i] != chr(c + ord("a")): current += 1 a = solve(s[len(s) // 2:], c + 1) + current current = 0 for i in range(len(s) // 2, len(s)): if s[i] != chr(c + ord("a")): current += 1 b = solve(s[:len(s) // 2], c + 1) + current return min(a, b) t = int(input()) for _ in range(t): n = int(input()) s = input().rstrip() ans = solve(s, 0) print(ans)
10
PYTHON3
n = int(input()) if n%2 == 0: print(int(n*n/2)) else: print(int((1+n//2)*n-n//2)) ans = "" if n%2 == 0: for i in range(n): ans = "" for j in range(int(n/2)): if i%2 == 0: ans = ans + "C." else: ans = ans + ".C" print(ans) else: for i in range(n): ans = "" for j in range(n): if i%2 == 0: if j%2 == 0: ans = ans + "C" else: ans = ans + "." else: if j%2 == 1: ans = ans + "C" else: ans = ans + "." print(ans)
7
PYTHON3
#!/usr/bin/python3 def readint(): return int(input()) def readline(): return [int(c) for c in input().split()] def main(): N, M = readline() grid = [] for i in range(N): grid.append([e for e in input()]) empty = [['.']*M for _ in range(N)] for crow in range(1, N-1): for ccol in range(1, M-1): apply = True for i in range(crow - 1, crow + 2): for j in range(ccol - 1, ccol + 2): if not(i == crow and j == ccol): if grid[i][j] == '.': apply = False break if not apply: break if apply: empty[crow-1][ccol-1] = '#' empty[crow-1][ccol] = '#' empty[crow-1][ccol+1] = '#' empty[crow][ccol-1] = '#' empty[crow][ccol+1] = '#' empty[crow+1][ccol-1] = '#' empty[crow+1][ccol] = '#' empty[crow+1][ccol+1] = '#' for i in range(N): for j in range(M): if grid[i][j] != empty[i][j]: print('NO') return print('YES') if __name__ == '__main__': main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long a1, b1, a2, b2, k, ans; cin >> a1 >> b1 >> a2 >> b2 >> k; if (a2 > b1 || a1 > b2) { cout << 0 << endl; return 0; } else if (a1 <= a2 && b1 >= b2) { ans = b2 - a2 + 1; if (a2 <= k && b2 >= k) ans--; } else if (a2 <= a1 && b2 >= b1) { ans = b1 - a1 + 1; if (a1 <= k && b1 >= k) ans--; } else if (a1 <= a2 && b1 <= b2) { ans = b1 - a2 + 1; if (k >= a2 && k <= b1) ans--; } else if (a2 <= a1 && b2 <= b1) { ans = b2 - a1 + 1; if (k >= a1 && k <= b2) ans--; } cout << ans << endl; return 0; }
7
CPP
n,k = input().split() n,k = int(n) ,int(k) f = [] t = [] for i in range(n): tmp1,tmp2 = input().split() tmp1,tmp2 = int(tmp1) ,int(tmp2) f.append(tmp1) t.append(tmp2) res = -9999999999999999 for i in range(n): if t[i] >= k: tmp = f[i] - (t[i] - k) else: tmp = f[i] res = max(res, tmp) print(res)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int tINF = (int)INT_MAX; const long long lINF = (long long)LLONG_MAX; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; cin >> n >> m; if (n > 1000) { cout << 0 << '\n'; return 0; } vector<long long> ar(n); for (auto &i : ar) cin >> i; long long ans = 1; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { ans *= abs(ar[i] - ar[j]) % m; ans %= m; } } cout << ans << '\n'; }
9
CPP
#include<iostream> using namespace std; int main(){ int n, m, Taro[100], Hanako[100]; while(1){ cin>>n>>m; if(!(n + m)) return 0; int sum_T = 0, sum_H = 0; for(int i=0;i<n;i++){ cin>>Taro[i]; sum_T += Taro[i]; } for(int i=0;i<m;i++){ cin>>Hanako[i]; sum_H += Hanako[i]; } int diff; diff = sum_T - sum_H; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if((Taro[i] - Hanako[j]) * 2 == diff){ cout<<Taro[i]<<" "<<Hanako[j]<<endl; i = n; j = m; } if(i == n-1 && j == m-1){ cout<<-1<<endl; } } } } }
0
CPP
a, b = map(int, input().split()) t = 0 while a <= b: t += 1 a *= 3 b *= 2 print(t)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); long long int a, b; cin >> a >> b; while (a > 0 && b > 0) { if (a >= 2 * b) a -= ((a / b) / 2) * 2 * b; else if (b >= 2 * a) b -= ((b / a) / 2) * 2 * a; else break; } cout << a << " " << b; }
8
CPP
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define LL long long int n, num[100005], opt[100005]; LL sum[100005], now[100005], ans; int main() { char ch[2]; scanf("%d%d", &n, &num[1]); opt[1] = 1; now[1] = sum[1] = num[1]; for(int i = 2; i <= n; i++) { scanf("%s%d", ch, &num[i]); opt[i] = (ch[0] == '+') ? 1 : -1; now[i] = now[i-1] + opt[i] * num[i]; sum[i] = sum[i-1] + num[i]; } ans = now[n]; int last = -1; for(int i = 1; i <= n; i++) if(opt[i] == -1) { if(last != -1) ans = max(ans, now[last-1] - (sum[i-1] - sum[last-1]) + (sum[n] - sum[i-1])); last = i; } printf("%lld\n", ans); }
0
CPP
for tt in range(int(input())): n = int(input()) arr = list(map(int,input().split())) st = set(arr) res = [] for i in range(len(arr)): if arr[i] not in res : res.append(arr[i]) else: continue print(*res)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, r, ans, i, j, d, q; int main() { cin >> q >> n; char c; for (i = 0; i < q; i++) { cin >> c >> m; if (c == '+') n = n + m; if (c == '-' && n - m < 0) d++; if (c == '-' && n - m >= 0) n = n - m; } cout << n << ' ' << d << endl; }
7
CPP
base=998244353; def power(x, y): if(y==0): return 1 t=power(x, y//2) t=(t*t)%base if(y%2): t=(t*x)%base return t; def inverse(x): return power(x, base-2) ft=[0] for i in range(0, 200000): ft.append(0) def get(i): res=0 while(i<=200000): res+=ft[i] i+=i&-i return res def update(i, x): while(i): ft[i]+=x i-=i&-i n=int(input()) a=[0] a+=list(map(int, input().split())) neg=[0] non=[0] for i in range(1, n+1): non.append(0) for i in range(1, n+1): if(a[i]!=-1): non[a[i]]+=1 for i in range(1, n+1): non[i]+=non[i-1] for i in range(1, n+1): if(a[i]==-1): neg.append(neg[i-1]+1) else: neg.append(neg[i-1]) m=neg[n] ans=0 for i in range(1, n+1): if(a[i]!=-1): ans+=get(a[i]) update(a[i], 1) fm=1 fs=fm for i in range(1, m+1): fs=fm fm=(fm*i)%base fs=(fs*inverse(fm))%base for i in range(1, n+1): if(a[i]!=-1): less=a[i]-non[a[i]] more=m-less ans=(ans+neg[i]*more*fs)%base ans=(ans+(m-neg[i])*less*fs)%base ans=(ans+m*(m-1)*inverse(4))%base print(ans)
12
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class X, class Y> bool minimize(X &x, const Y &y) { X eps = 1e-9; if (x > y + eps) { x = y; return true; } else return false; } template <class X, class Y> bool maximize(X &x, const Y &y) { X eps = 1e-9; if (x + eps < y) { x = y; return true; } else return false; } template <class T> T Abs(const T &x) { return (x < 0 ? -x : x); } int a[1000100], n; int score[(1LL << (21)) + 7]; void init(void) { scanf("%d", &n); for (int i = (1), _b = (n); i <= _b; i++) scanf("%d", &a[i]); } queue<int> q; bool inQueue[(1LL << (21)) + 7]; void addNumber(int x) { queue<int> q; q.push(x); inQueue[x] = true; while (!q.empty()) { int u = q.front(); q.pop(); inQueue[u] = false; score[u]++; int tmp = u; while (tmp > 0) { int maskOfBit = tmp & -tmp; int v = u ^ maskOfBit; if (score[v] < 2 && !inQueue[v]) { q.push(v); inQueue[v] = true; } tmp ^= maskOfBit; } } } int chooseGreedy(int x) { int needMask = 0; int res = 0; for (int i = (21 - 1), _a = (0); i >= _a; i--) { bool canBitOne = (((x) >> (i)) & 1) || score[needMask | (1LL << (i))] >= 2; if (canBitOne) { res |= (1LL << (i)); if (!(((x) >> (i)) & 1)) needMask |= (1LL << (i)); } } return res; } void process(void) { int res = 0; for (int i = (n), _a = (1); i >= _a; i--) { maximize(res, chooseGreedy(a[i])); addNumber(a[i]); } cout << res << endl; } int main(void) { init(); process(); return 0; }
12
CPP
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main(void) { int n,m,i,j,k,a,b,c,p[100000],pl[100001],su[100001],g; int x,y; long long sum,xx,yy,mn; scanf("%d %d",&n,&m); for(i=0;i<m;i++) scanf("%d",&p[i]); for(i=1;i<=n;i++) { pl[i]=0; su[i]=0; } for(i=1;i<m;i++) { x=p[i-1]; y=p[i]; if(x>y) swap(x,y); pl[x]++; pl[y]--; } su[0]=0; for(i=1;i<=n;i++) { su[i]=su[i-1]+pl[i]; } sum=0; for(i=1;i<n;i++) { scanf("%d %d %d",&a,&b,&c); xx=(long long)a*su[i]; yy=(long long)b*su[i]+c; mn=min(xx,yy); sum+=mn; } printf("%lld\n",sum); return 0; }
0
CPP
def shorter(word): if len(word)>10: first_letter= word[0] last_letter= word[-1] num=len(word)-2 ans= first_letter+str(num)+last_letter else: ans=word return ans n = int(input()) while n!=0: n=n-1 print(shorter(input()))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long dp[25][100000 + 100]; long long a[100000 + 100]; long long buffer[100000 + 100]; int l, r; long long val = 0; long long work(int p, int q) { while (r < q) val += buffer[a[++r]]++; while (r > q) val -= --buffer[a[r--]]; while (l > p) val += buffer[a[--l]]++; while (l < p) val -= --buffer[a[l++]]; return val; } long long calc(long long layer, int p, int q) { return dp[layer - 1][p] + work(p + 1, q); } void solve(int k, int L, int R, int al, int ar) { int mid = (L + R) >> 1; int arr = min(mid, ar); dp[k][mid] = calc(k, al, mid); int it = al; for (int i = al + 1; i <= arr; i++) { long long tmp = calc(k, i, mid); if (tmp <= dp[k][mid]) { dp[k][mid] = tmp; it = i; } } if (L == R) return; solve(k, L, mid, al, it); solve(k, mid + 1, R, it, ar); } int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); dp[1][i] = dp[1][i - 1] + buffer[a[i]]++; } for (int i = 2; i <= k; i++) { memset(buffer, 0, sizeof(buffer)); l = 1; r = 0; val = 0; solve(i, 1, n, 1, n); } cout << dp[k][n] << endl; }
12
CPP
import os,sys from io import BytesIO, IOBase def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().split()) def li(): return list(mi()) import math for i in range(ii()): a,b=mi() c=0 l=[9,99,999,9999,99999,999999,9999999,99999999,999999999] c=0 for i in range(len(l)): if b>=l[i]: c+=1 print(a*c)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long m = n + (n - 1); long long dd = 0; int all_nine = 1; while (m > 0) { if (m % 10 != 9) { all_nine = 0; } dd++; m /= 10; } m = n + n - 1; if (!all_nine) { dd--; } long long p10 = 1; long long nn = 0; for (int i = 0; i < dd; i++) { p10 *= 10; nn = nn * 10 + 9; } long long ans = 0; for (int i = 0; i < 10; i++) { long long cc = p10 * i + nn; if (cc <= n) { ans += (cc - 1) / 2; } else { long long l = 1, r = (cc - 1) / 2 + 1; while (l < r) { long long mm = (l + r) / 2; if (cc - mm > n) l = mm + 1; else r = mm; } ans += (cc - 1) / 2 - l + 1; } } cout << ans << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9, PI = acos(-1); class vec { public: double x, y; vec() {} vec(double tx, double ty) { x = tx, y = ty; } } pt[100010]; double pa[100010], len[100010]; int n, m, st[200010][18]; pair<double, double> sec[200010]; bool check(double mid) { memset(st, 0, sizeof(st)); for (int i = 1; i <= m; i++) { double md = pa[i], delta = acos(mid / len[i]); if (md + delta > PI) md -= 2 * PI; sec[i * 2 - 1] = make_pair(md + delta, md - delta); sec[i * 2] = make_pair(md + delta + 2 * PI, md - delta + 2 * PI); } sort(sec + 1, sec + 2 * m + 1); for (int i = 1, j = 1; i <= 2 * m; i++) { while (j < 2 * m && sec[j].second <= sec[i].first) j++; st[i][0] = j; } for (int j = 1; j < 18; j++) for (int i = 1; i <= 2 * m; i++) st[i][j] = st[st[i][j - 1]][j - 1]; for (int i = 1; i <= m; i++) { int pos = i, ans = 1; for (int j = 17; j >= 0; j--) { if (st[pos][j] < i + m) { ans += (1 << j); pos = st[pos][j]; } } if (ans <= n) return 1; } return 0; } int main() { scanf("%d%d", &m, &n); double l = eps, r = 1e14; for (int i = 1; i <= m; i++) { scanf("%lf%lf", &pt[i].x, &pt[i].y); r = min(r, sqrt(pt[i].x * pt[i].x + pt[i].y * pt[i].y)); } if (r < eps) { puts("0"); return 0; } r -= eps; for (int i = 1; i <= m; i++) pa[i] = atan2(pt[i].y, pt[i].x); for (int i = 1; i <= m; i++) len[i] = sqrt(pt[i].x * pt[i].x + pt[i].y * pt[i].y); while (r - l > 1e-7) { double mid = (l + r) / 2; if (check(mid)) l = mid; else r = mid; } printf("%.10lf\n", l); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long int a[100010], b[100010], c[100010]; int main() { long long int k1, k2, k3, t1, t2, t3; cin >> k1 >> k2 >> k3 >> t1 >> t2 >> t3; int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) c[i] = a[i]; queue<pair<long long int, long long int> > q; for (long long int i = 1; i <= n && i <= k1; i++) { q.push(pair<long long int, long long int>(i, a[i])); b[i] = a[i] + t1; } for (long long int i = k1 + 1; i <= n; i++) { long long int x = q.front().second; x += t1; long long int w = a[i]; w = max(w, x); q.pop(); q.push(pair<long long int, long long int>(i, w)); b[i] = w + t1; } for (long long int i = 1; i <= n; i++) a[i] = b[i]; queue<pair<long long int, long long int> > newq; q = newq; for (long long int i = 1; i <= n && i <= k2; i++) { q.push(pair<long long int, long long int>(i, a[i])); b[i] = a[i] + t2; } for (long long int i = k2 + 1; i <= n; i++) { long long int x = q.front().second; x += t2; long long int w = a[i]; w = max(w, x); q.pop(); q.push(pair<long long int, long long int>(i, w)); b[i] = w + t2; } for (long long int i = 1; i <= n; i++) a[i] = b[i]; q = newq; for (long long int i = 1; i <= n && i <= k3; i++) { q.push(pair<long long int, long long int>(i, a[i])); b[i] = a[i] + t3; } for (long long int i = k3 + 1; i <= n; i++) { long long int x = q.front().second; x += t3; long long int w = a[i]; w = max(w, x); q.pop(); q.push(pair<long long int, long long int>(i, w)); b[i] = w + t3; } long long int res = 0; for (long long int i = 1; i <= n; i++) res = max(res, b[i] - c[i]); cout << res << endl; }
8
CPP
n,h = [int(i) for i in input().split()] a = [int(i) for i in input().split()] tot = 0 for i in a: if i>h: tot+=2 else: tot+=1 print(tot)
7
PYTHON3
#include <cstdio> #include <cstdlib> using namespace std; int main() { int s; scanf("%d", &s); for(int data = 0; data < s; ++data) { unsigned int n[9]; for(int i = 0; i < 9; ++i) scanf("%x", n + i); int carry = 0; unsigned int k = 0; for(int i = 0; i < 32; ++i) { int cnt = 0; for(int j = 0; j < 8; ++j) if(n[j] & (1 << i)) ++cnt; if(((cnt + carry) & 1) != ((n[8] >> i) & 1)) { k |= (1 << i); cnt = 8 - cnt; } carry = ((cnt + carry) >> 1); } printf("%x\n", k); } return EXIT_SUCCESS; }
0
CPP
#include <bits/stdc++.h> using namespace std; void find(string A, int n) { int k = 0; int B = 0; int l = -1; for (int i = 0; i < n; i++) { if (A[i] == '?') { continue; } else { if (A[i] == 'B') { B = 1; } else { B = 0; } if (i > 0 && A[i - 1] == '?') { int j = i - 1; while (j >= 0 && A[j] == '?') { if (B == 1) { A[j] = 'R'; B = 0; } else { A[j] = 'B'; B = 1; } j--; } } l = i; } } if (l < (n - 1)) { if (l == -1) { B = 1; int i = 0; while (i < n) { if (B == 0) { A[i] = 'B'; B = 1; } else { A[i] = 'R'; B = 0; } i++; } } else { if (A[l] == 'B') { B = 1; } else { B = 0; } int i = l + 1; while (i < n) { if (B == 0) { A[i] = 'B'; B = 1; } else { A[i] = 'R'; B = 0; } i++; } } } cout << A; cout << "\n"; return; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; string A; cin >> A; find(A, n); } return 0; }
8
CPP
import math for _ in range(int(input())): # Q = int(input()) N = int(input()) A = list(map(int,input().split())) print(math.ceil(sum(A)/N))
7
PYTHON3
#include <bits/stdc++.h> inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } long long int gcd(long long int a, long long int b) { return (a ? gcd(b % a, a) : b); } long long int modPow(long long int a, long long int b, long long int MOD) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % MOD; } b /= 2; y = (y * y) % MOD; } return x; } long long int modInverse(long long int a, long long int p) { return modPow(a, p - 2, p); } using namespace std; const int N = 65; int bit1[N]; int bit2[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); long long int n, a, b, i, j, k, ans; cin >> n; for (int i = 0; i < (n); i++) { cin >> a >> b; long long int ta, tb; ta = a; tb = b; memset(bit1, 0, sizeof(bit1)); memset(bit2, 0, sizeof(bit2)); for (int i = 0; i < (N); i++) { bit1[i] = ta % 2; ta /= 2; } ans = a; for (int i = 0; i < (N); i++) { if (bit1[i]) continue; else { if (ans + (long long int)(1LL << i) <= b) { ans += (long long int)(1LL << i); } else break; } } cout << ans << endl; } }
7
CPP
#include <bits/stdc++.h> using namespace std; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; void file() {} void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); } int mod = 1e9 + 7; long long fpow(long long x, long long p) { if (!p) return 1; long long sq = fpow(x, p / 2); sq *= sq; sq %= mod; if (p & 1) sq *= x; return sq % mod; } const int N = 2e3 + 9; int mem[N]; vector<int> adjL[N]; int n, m, k; int solve(int idx) { if (idx >= n) return 1; if (mem[idx] == 2) return 0; if (mem[idx]) return 1; mem[idx] = 1; int ch = 1; for (auto& it : adjL[idx]) ch = min(ch, solve(it)); return ch; } int main() { file(); fast(); cin >> n >> m >> k; for (int i = 0; i < n; i++) { if (i + k - 1 >= n) break; for (int j = 0; j < k / 2; j++) adjL[j + i].push_back(k - j - 1 + i), adjL[k - j - 1 + i].push_back(j + i); } long long ans = 1; for (int i = 0; i < n; i++) { ans *= max(1, solve(i) * m); ans %= mod; for (int j = 0; j < n; j++) mem[j] = (mem[j] ? 2 : 0); } cout << ans << "\n"; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 60; int n, p; long long dp[N][2]; int main() { scanf("%d %d", &n, &p); dp[0][0] = 1, dp[0][1] = 0; for (int i = 1; i <= n; i++) { int u; scanf("%d", &u); dp[i][0] = dp[i - 1][0] + dp[i - 1][u % 2]; dp[i][1] = dp[i - 1][1] + dp[i - 1][(u % 2) ^ 1]; } cout << dp[n][p]; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int V[] = {0, 1, 3, 16, 23, 28, 42, 76, 82, 86, 119, 137, 154, 175, 183, 232, 277, 322, 402, 426}; int main() { int N; cin >> N; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { if (j) cout << ' '; if (i == j) cout << 0; else cout << V[i] + V[j]; } cout << endl; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, j; long long n, k, a, b; while (cin >> n >> k >> a >> b) { if (k == 1) { cout << a * (n - 1) << endl; } else { long long num = 0, x = n; while (x > 1) { if (x % k) { long long tmp = x % k; if (x < k) tmp--; num += tmp * a; x -= tmp; } else { long long tmp = x / k; num += min(b, (k - 1) * tmp * a); x = tmp; } } cout << num << endl; } } return 0; }
8
CPP
q = int(input()) mapping = dict() for _ in range(q): old, new = input().split() if old in mapping: mapping[new] = mapping[old] del mapping[old] else: mapping[new] = old print(len(mapping)) for key, value in mapping.items(): print(value, key)
8
PYTHON3
n=int(input()) a=list(map(int,input().split())) ans=sorted(a)+[min(a)] print(' '.join([str(ans[ans.index(a[i])+1]) for i in range(n)]))
10
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int rd() { int x = 0; char IO = getchar(); bool f = 0; while ((IO < '0' || IO > '9') && IO != '-') IO = getchar(); if (IO == '-') f = 1, IO = getchar(); while (IO >= '0' && IO <= '9') x = (x << 3) + (x << 1) + (IO ^ 48), IO = getchar(); return f ? -x : x; } const int Mod = 1e9 + 7; const int SIZE = 200005; int n; int a[SIZE]; int sum[1000005]; int cnt[1000005]; int Pow(int a, int b) { int res = 1; while (b) { if (b & 1) res = 1LL * res * a % Mod; b >>= 1; a = 1LL * a * a % Mod; } return res; } void _main() { n = rd(); for (register int i = 1, _n = n; i <= _n; ++i) ++sum[rd()]; for (register int i = 2, _n = 1000000; i <= _n; ++i) { for (int j = i; j <= 1000000; j += i) cnt[i] += sum[j]; if (cnt[i]) cnt[i] = 1LL * cnt[i] * Pow(2, cnt[i] - 1) % Mod; } int ans = 0; for (register int i = 1000000, _n = 2; i >= _n; --i) { ans += 1LL * i * cnt[i] % Mod; if (ans >= Mod) ans -= Mod; for (int j = i + i; j <= 1000000; j += i) if (cnt[j]) { ans += Mod - 1LL * i * cnt[j] % Mod; if (ans >= Mod) ans -= Mod; cnt[i] += Mod - cnt[j]; if (cnt[i] >= Mod) cnt[i] -= Mod; } } printf("%d\n", ans); } int main() { _main(); return 0; }
10
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]; bool leaf[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); fill(leaf, leaf + n + 1, 1); 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]; if (i == 1) continue; 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; leaf[j] = 0; } } 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]) { sz[fa2[j]] -= sz[y]; for (auto &k : v2[fa2[j]]) if (k.to != j) k.dsum -= val[fa2[j]] * sz[y]; else dif -= k.dsum; } 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]; else dif += k.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
n = int(input()) a, b = int(n ** 0.5), int(n ** 0.5) fl = 0 j = a * b while j < n: if fl == 0: a += 1 j += b else: b += 1 j += a fl = (fl + 1) % 2 print(a + b)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<pair<long long, int> > have[505000]; int n, m, Siz, mx, mn, idx; long long num[505000], tag[505000]; int find1(int p, long long x) { int l = 0, r = have[p].size(); while (l < r) { int mid = (l + r) >> 1; if (have[p][mid].first > x) r = mid; else l = mid + 1; } if (l == 0) return 0; return have[p][l - 1].first == x ? have[p][l - 1].second : 0; } int find2(int p, long long x) { int l = 0, r = have[p].size(); while (l < r) { int mid = (l + r) >> 1; if (have[p][mid].first >= x) r = mid; else l = mid + 1; } if (l == (int)have[p].size()) return n + 1; return have[p][l].first == x ? have[p][l].second : n + 1; } int main() { scanf("%d%d", &n, &m), Siz = sqrt(n); for (int i = 1; i <= n; i++) scanf("%d", &num[i]); for (int i = 1; i <= n; i++) have[(i / Siz + (i % Siz != 0))].push_back(make_pair(num[i], i)); for (int i = 1; (i / Siz + (i % Siz != 0)) <= (n / Siz + (n % Siz != 0)); i += Siz) sort(have[(i / Siz + (i % Siz != 0))].begin(), have[(i / Siz + (i % Siz != 0))].end()); for (int i = 1, kind, x, y, z; i <= m; i++) { scanf("%d", &kind); if (kind == 1) { scanf("%d%d%d", &x, &y, &z); if ((x / Siz + (x % Siz != 0)) == (y / Siz + (y % Siz != 0))) { for (int j = x; j <= y; j++) num[j] += z; have[(x / Siz + (x % Siz != 0))].clear(); for (int j = x - 1; (x / Siz + (x % Siz != 0)) == (j / Siz + (j % Siz != 0)); j--) have[(j / Siz + (j % Siz != 0))].push_back(make_pair(num[j], j)); for (int j = x; (x / Siz + (x % Siz != 0)) == (j / Siz + (j % Siz != 0)) && j <= n; j++) have[(j / Siz + (j % Siz != 0))].push_back(make_pair(num[j], j)); sort(have[(x / Siz + (x % Siz != 0))].begin(), have[(x / Siz + (x % Siz != 0))].end()); continue; } have[(x / Siz + (x % Siz != 0))].clear(); for (int j = x; (x / Siz + (x % Siz != 0)) == (j / Siz + (j % Siz != 0)); j++) num[j] += z; for (int j = x - 1; (x / Siz + (x % Siz != 0)) == (j / Siz + (j % Siz != 0)); j--) have[(x / Siz + (x % Siz != 0))].push_back(make_pair(num[j], j)); for (int j = x; (x / Siz + (x % Siz != 0)) == (j / Siz + (j % Siz != 0)) && j <= n; j++) have[(x / Siz + (x % Siz != 0))].push_back(make_pair(num[j], j)); sort(have[(x / Siz + (x % Siz != 0))].begin(), have[(x / Siz + (x % Siz != 0))].end()); have[(y / Siz + (y % Siz != 0))].clear(); for (int j = y; (y / Siz + (y % Siz != 0)) == (j / Siz + (j % Siz != 0)); j--) num[j] += z; for (int j = y - 1; (y / Siz + (y % Siz != 0)) == (j / Siz + (j % Siz != 0)); j--) have[(y / Siz + (y % Siz != 0))].push_back(make_pair(num[j], j)); for (int j = y; (y / Siz + (y % Siz != 0)) == (j / Siz + (j % Siz != 0)) && j <= n; j++) have[(y / Siz + (y % Siz != 0))].push_back(make_pair(num[j], j)); sort(have[(y / Siz + (y % Siz != 0))].begin(), have[(y / Siz + (y % Siz != 0))].end()); for (int j = (x / Siz + (x % Siz != 0)) + 1; j < (y / Siz + (y % Siz != 0)); j++) tag[j] += z; continue; } scanf("%d", &x), mx = 0, mn = n + 1, ++idx; for (int j = 1; (j / Siz + (j % Siz != 0)) <= (n / Siz + (n % Siz != 0)); j += Siz) mx = max(find1((j / Siz + (j % Siz != 0)), x - tag[(j / Siz + (j % Siz != 0))]), mx), mn = min(find2((j / Siz + (j % Siz != 0)), x - tag[(j / Siz + (j % Siz != 0))]), mn); if (mx && mn <= n && num[mx] + tag[(mx / Siz + (mx % Siz != 0))] == x && num[mn] + tag[(mn / Siz + (mn % Siz != 0))] == x) printf("%d\n", mx - mn); else printf("-1\n"); } }
11
CPP
n = int(input()) count,mainCount = 0,0 for i in range(n): x = [int(x) for x in input().split()] for i in x: if i==1: count+=1 if count>=2: mainCount+=1 count=0 print(mainCount)
7
PYTHON3
t = int(input()) for i in range(t): n = int(input()) s = input() if n < 11: print('NO') elif n == 11: if s[0] != '8': print('NO') else: print('YES') else: if s[0] != '8': if any(s[j] == '8' for j in range(n-10)): print('YES') else: print('NO') else: print('YES')
7
PYTHON3
queries = int(input()) for i in range(queries): counter = 0 n = int(input()) if n == 1: print(0) continue while int(list(str(n))[-1]) in range(0, 9, 2): n //= 2 counter += 1 while int(list(str(n))[-1]) == (0 or 5): n //= 5 counter += 3 while sum(list(map(int, str(n)))) % 3 == 0: n //= 3 counter += 2 if n == 1: print(counter) else: print(-1)
7
PYTHON3
t=int(input()) for i in range(0,t): n,k=map(int,input().split()) s=input() m=0 dd=0 flag=0 for j in range(0,n): if(s[j]=="0"): m+=1 else: if(flag==0): dd=dd+m//(k+1) else: dd=dd+max((m-k)//(k+1),0) m=0 flag=1 if(flag==0): dd=dd+(m+k)//(k+1) else: dd=dd+max(0,m//(k+1)) print(dd)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; const long long inf = 1e14; long long a[maxn], sum[maxn]; map<long long, long long> map1; set<long long> s; int main(void) { cin.tie(0); std::ios::sync_with_stdio(false); long long n, k; cin >> n >> k; for (long long i = 1; i <= n; i++) cin >> a[i]; for (long long i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; s.insert(1); long long tmp = k; for (long long i = 1; i <= 60; i++) { if (tmp > inf) break; s.insert(tmp); tmp *= k; } long long ans = 0; map1[0] = 1; for (long long i = 1; i <= n; i++) { for (set<long long>::iterator it = s.begin(); it != s.end(); it++) { ans += map1[sum[i] - *it]; } map1[sum[i]]++; } cout << ans << endl; return 0; }
9
CPP
#n je duljina stringa, a t broj sekundi #Imamo string koji nam daje redoslijed u redu u kantini i nakon svake sekunde se B(muski) micu jedno mjesto unazad #Input n, t = map(int, input().split()) string = input() while t!=0: t -= 1 lista = list(string) string = '' for i in range(0, n - 1): if lista[i] == 'B' and lista[i + 1] == 'G': #Pretvaramo ga u bilo koji drugi element osim B tako da se ne mice vise u toj istoj sekundi lista[i] = '-' lista[i], lista[i + 1] = lista[i + 1], lista[i] for i in lista: string += i string = string.replace('-', 'B') #Output print(string)
8
PYTHON3
#include<bits/stdc++.h> using namespace std; int main() {long long a,b,c,d; cin>>a>>b>>c; cout<<a-b-(a-b)/c*c<<endl; }
0
CPP
n, k = map(int, input().split()) h = [int(i) for i in input().split()] h.sort(reverse=True) print(sum(h[k:]))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int main() { int c, d, n, m, k; int dp[105]; cin >> c >> d >> n >> m >> k; int res = 0; int all = n * m - k; if (all <= 0) { puts("0"); return 0; } int p = d * n; int t = all / n; int g = all % n; res = t * min(c, p) + min(g * d, c); cout << res << endl; return 0; }
7
CPP
n=int(input()) for i in range(n): a=[] for j in range(n): if (i+j)%2==0: a.append('W') elif (i+j)%2!=0: a.append('B') print(''.join(a))
8
PYTHON3
import bisect n=int(input()) arr=list(map(int,input().split())) m=int(input()) arr.sort() for i in range(m): rs=int(input()) ans=bisect.bisect(arr,rs) print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 3000; long long int a[maxn], b[maxn], va[maxn], vb[maxn], verse[maxn]; int n, k; int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); for (int i = 0; i < n; i++) scanf("%I64d", &b[i]); long long int ans = 0; b[n] = 1e+18; while (k--) { int ra = -1, rb; for (int i = n - 1, j = n, minb = n; i >= 0; i--) { if (j > i) j = i; if (b[j] < b[minb] && !vb[j]) minb = j; while ((j - 1) >= 0 && verse[j - 1]) if (b[--j] < b[minb] && !vb[j]) minb = j; if (!va[i] && (ra == -1 || a[i] + b[minb] < a[ra] + b[rb])) ra = i, rb = minb; } ans += a[ra] + b[rb]; va[ra]++; vb[rb]++; for (int i = ra; i < rb; i++) verse[i]++; for (int i = rb; i < ra; i++) verse[i]--; } printf("%I64d\n", ans); return 0; }
21
CPP
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC optimize("-O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int gcd(int a, int b) { while (b) { a %= b; swap(a, b); } return a; } int lcm(int a, int b) { return a * b / gcd(a, b); } inline long long get(long long ind, long long c, long long b) { long long full = (ind + 1) / c; long long res = full * b; if ((ind + b) / c != full) { res += ind % c - (c - b) + 1; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int t; cin >> t; while (t--) { int a, b, q; cin >> a >> b >> q; int c = lcm(a, b); if (a > b) { swap(a, b); } for (int i = (0); i < (q); i++) { long long l, r; cin >> l >> r; cout << get(r, c, c - b) - get(l - 1, c, c - b) << " "; } cout << '\n'; } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long K = 1; int main() { int W = 0, H1 = 0, H2 = 0, n, i; vector<pair<int, int> > v; scanf("%d", &n); v.resize(n); for (i = 0; i < n; i++) { scanf("%d%d", &v[i].first, &v[i].second); W += v[i].first; if (v[i].second >= H1) { H2 = H1; H1 = v[i].second; } else if (v[i].second > H2) { H2 = v[i].second; } } for (i = 0; i < n; i++) { if (v[i].second == H1) printf("%d ", (W - v[i].first) * H2); else printf("%d ", (W - v[i].first) * H1); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int A, B, C; int main() { cin >> A >> B >> C; for (int i = 1; i <= 400; ++i) { A *= 10; if (A / B == C) { cout << i << endl; return 0; } A %= B; } cout << "-1" << endl; return 0; }
8
CPP
#include<cstdio> #include<iostream> #include<algorithm> #include<cmath> using namespace std; const int maxn = 5e5+5; int n,m; int far[maxn]; int Rank[maxn]; int find(int x){ if(far[x] == x)return x; else return find(far[x]); } void unite(int x,int y){ x = find(x); y = find(y); if(x == y) return; if(Rank[x] < Rank[y]) far[x] = y; else { far[y] = x; if(Rank[x] == Rank[y]) Rank[x]++; } } bool check(int x,int y){ return find(x) == find(y); } void init(int n){ for(int i = 0;i <= n;i++) { far[i] = i; //Rank[i] = 0; } } int main() { while(~scanf("%d %d",&n,&m) &&(m||n)) { init(n); int a; long long ans = 0; for(int i = 2;i <= n;i++) { scanf("%d",&far[i]); } char c[3]; for(int i = 0;i <m;i++) { scanf("%s %d",c,&a); if(c[0] == 'M') far[a] = a; else { ans += find(a); } } printf("%lld\n",ans); } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int n, m; char mapp[3000][3000]; long long a[3000]; long long b[3000][3000]; long long GetHash(char *str, int m) { long long res = 0; for (int i = 0; i < m; i++) { res = (res * 233 % 1000000009 + str[i] - 'a' + 1) % 1000000009; } return res; } void deBug() { for (int i = 0; i < n; i++) { cout << a[i] << endl; } cout << endl; for (int i = 0; i < m; i++) { for (int j = 0; j <= n - m; j++) { cout << b[i][j] << " "; } cout << endl; } cout << endl; } void input() { cin >> n >> m; char str[3000]; for (int i = 0; i < n; i++) { scanf("%s", str); a[i] = GetHash(str, m); } for (int i = 0; i < m; i++) { scanf("%s", mapp[i]); } for (int i = 0; i < m; i++) { for (int j = 0; j <= n - m; j++) { b[i][j] = GetHash(mapp[i] + j, m); } } map<long long, int> H_map; for (int i = 0; i <= n - m; i++) { int temp = 0; for (int j = 0; j < m; j++) { temp = (temp * 233 % 1000000009 + a[i + j]) % 1000000009; } if (H_map.find(temp) == H_map.end()) { H_map[temp] = i; } } for (int i = 0; i <= n - m; i++) { int temp = 0; for (int j = 0; j < m; j++) { temp = (temp * 233 % 1000000009 + b[j][i]) % 1000000009; } if (H_map.find(temp) != H_map.end()) { cout << H_map[temp] + 1 << " " << i + 1 << endl; return; } } return; } int main() { input(); }
7
CPP
#include <bits/stdc++.h> using namespace std; long long int x[1000007], y[1000007], z[1000007]; string s; int main() { { long long int a = 0, b = 0, d, e, i, j, k, l, m, n, p = 1, q, r, t, u, val, w, tc, mn = 2134567891, mx = 0, sum = 0, ans = 0; cin >> n; for (i = 0; i < n; i++) cin >> x[i]; for (i = 0; i < n; i++) { if (x[i] == 25) a++; else if (x[i] == 50) { if (a) a--, b++; else { p = 0; break; } } else { if (a && b) a--, b--; else if (a >= 3) a -= 3; else { p = 0; break; } } } if (p) cout << "YES\n"; else cout << "NO\n"; memset(x, 0, sizeof(x)); } return 0; }
7
CPP
#include<bits/stdc++.h> #define MOD 1000000007 #define MAXN 210000 #define Inv2 500000004 #define lowbit(x) (x)&(-x) using namespace std; template <typename T> inline void read(T &s) { s = 0;char ch = getchar(); while(!isdigit(ch)) ch = getchar(); while(isdigit(ch)) s = ((s+(s<<2))<<1)+ch-'0',ch = getchar(); } int n,A[MAXN],C[MAXN],S=1,tl,Ans,bel[MAXN]; vector <int> v[MAXN]; pair<int,int> T[MAXN]; inline int Mod(int u){return u < MOD?u:u-MOD;} struct Bit { int tmp[MAXN]; Bit(){memset(tmp,0,sizeof(tmp));} inline void Insert(int x,int val){for(;x<=n;x+=lowbit(x)) tmp[x] = Mod(Mod(val+tmp[x])+MOD);} inline int Query(int x){int Sum = 0;for(;x;x-=lowbit(x)) Sum = Mod(tmp[x]+Sum);return Sum;} }Sum1,cnt,Sum2; inline int Fast_Power(int u,int k=MOD-2) { int Sum = 1; while(k) { if(k&1) Sum = 1ll * Sum * u % MOD; u = 1ll * u * u % MOD,k >>= 1; } return Sum; } int main() { read(n),T[0] = make_pair(1,0); for(int i = 1;i<=n;++i) read(A[i]),C[A[i]]++; for(int i = n;i;--i) C[i] += C[i+1]; for(int i = 1;i<=n;++i) C[i] = C[i] - n + i,S = 1ll * S * C[i] % MOD; if(S <= 0) return puts("0"),0; for(int i = 1;i<=n;++i) { T[i] = T[i-1]; if(C[i] == 1) T[i].second++,++tl,bel[i] = tl; else T[i].first = 1ll * T[i].first * (C[i]-1) % MOD * Fast_Power(C[i]) % MOD,bel[i] = tl; } for(int i = 1;i<=n;++i) Ans = Mod(Ans + 1ll * Mod(Mod(i - cnt.Query(A[i])+MOD) - 1 + MOD) * S % MOD),cnt.Insert(A[i],1),v[bel[A[i]]].push_back(i); for(int i = 0;i<=tl;++i) { for(auto x : v[i]) { Ans = Mod(Ans + 1ll * (Sum2.Query(A[x])) * T[A[x]].first % MOD * S % MOD * Inv2 % MOD); Ans = Mod(Ans - 1ll * Mod(Sum1.Query(n) - Sum1.Query(A[x]) + MOD) * Fast_Power(T[A[x]].first) % MOD * S % MOD * Inv2 % MOD + MOD); Sum1.Insert(A[x],T[A[x]].first),Sum2.Insert(A[x],Fast_Power(T[A[x]].first)); } for(auto x : v[i]) Sum1.Insert(A[x],-T[A[x]].first),Sum2.Insert(A[x],-Fast_Power(T[A[x]].first)); } printf("%d\n",Ans); return 0; }
0
CPP
""" Brandt Smith, Peter Haddad, Lemuel Gorion Codeforces.com Problem 579A """ print(bin(int(input()))[2:].count('1'))
7
PYTHON3
def main(): for _ in inputt(): x, n, m = inputi() while x > 20 and n: x = x // 2 + 10 n -= 1 x -= 10 * m print("YES" if x <= 0 else "NO") # region M # region import # 所有import部分 from math import * from heapq import * from itertools import * from functools import reduce, lru_cache, partial from collections import Counter, defaultdict import re, copy, operator, cmath import sys, io, os, builtins sys.setrecursionlimit(1000) # endregion # region fastio # 快速io,能提升大量时间 BUFSIZE = 8192 class FastIO(io.IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = io.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(io.IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): sys.stdout.write(kwargs.pop("split", " ").join(map(str, args))) sys.stdout.write(kwargs.pop("end", "\n")) sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip() inputt = lambda t = 0: range(t) if t else range(int(input())) inputs = lambda: input().split() inputi = lambda: map(int, inputs()) inputl = lambda t = 0, k = inputi: map(list, (k() for _ in range(t))) if t else list(k()) # endregion # region bisect # 二分搜索,标准库没有函数参数 def len(a): if isinstance(a, range): return -((a.start - a.stop) // a.step) return builtins.len(a) def bisect_left(a, x, key = None, lo = 0, hi = None): if lo < 0: lo = 0 if hi == None: hi = len(a) if key == None: key = do_nothing while lo < hi: mid = (lo + hi) // 2 if key(a[mid]) < x: lo = mid + 1 else: hi = mid return lo def bisect_right(a, x, key = None, lo = 0, hi = None): if lo < 0: lo = 0 if hi == None: hi = len(a) if key == None: key = do_nothing while lo < hi: mid = (lo + hi) // 2 if x < key(a[mid]): hi = mid else: lo = mid + 1 return lo def insort_left(a, x, key = None, lo = 0, hi = None): lo = bisect_left(a, x, key, lo, hi) a.insert(lo, x) def insort_right(a, x, key = None, lo = 0, hi = None): lo = bisect_right(a, x, key, lo, hi) a.insert(lo, x) do_nothing = lambda x: x bisect = bisect_right insort = insort_right def index(a, x, default = None, key = None, lo = 0, hi = None): if lo < 0: lo = 0 if hi == None: hi = len(a) if key == None: key = do_nothing i = bisect_left(a, x, key, lo, hi) if lo <= i < hi and key(a[i]) == x: return a[i] if default != None: return default raise ValueError def find_lt(a, x, default = None, key = None, lo = 0, hi = None): if lo < 0: lo = 0 if hi == None: hi = len(a) i = bisect_left(a, x, key, lo, hi) if lo < i <= hi: return a[i - 1] if default != None: return default raise ValueError def find_le(a, x, default = None, key = None, lo = 0, hi = None): if lo < 0: lo = 0 if hi == None: hi = len(a) i = bisect_right(a, x, key, lo, hi) if lo < i <= hi: return a[i - 1] if default != None: return default raise ValueError def find_gt(a, x, default = None, key = None, lo = 0, hi = None): if lo < 0: lo = 0 if hi == None: hi = len(a) i = bisect_right(a, x, key, lo, hi) if lo <= i < hi: return a[i] if default != None: return default raise ValueError def find_ge(a, x, default = None, key = None, lo = 0, hi = None): if lo < 0: lo = 0 if hi == None: hi = len(a) i = bisect_left(a, x, key, lo, hi) if lo <= i < hi: return a[i] if default != None: return default raise ValueError # endregion # region main # main 函数部分 if __name__ == "__main__": main() # endregion # endregion
8
PYTHON3
n=input() lst=[] lst1=[] n1=n[:10] lst.append(n1) n2=n[10:20] lst.append(n2) n3=n[20:30] lst.append(n3) n4=n[30:40] lst.append(n4) n5=n[40:50] lst.append(n5) n6=n[50:60] lst.append(n6) n7=n[60:70] lst.append(n7) n8=n[70:] lst.append(n8) for i in range(10): s=input() lst1.append(str(i)+' '+s) for j in lst: for k in lst1: if(j in k): print(k[0],end='')
7
PYTHON3
n, a, b = map(int, input().split()) x = list(map(int, input().split())) cost = 0 for i in range(n-1): dist = x[i+1] - x[i] cost += min(dist*a, b) print(cost)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int p=0; int main(void){ // Your code here! while (true){ int a,b,n=0; cin >> a >> b; if (a==0 && b==0) break; if (p!=0) cout << endl; for (int i=a;i<b+1;i++){ if (i%400==0) {n=1;cout << i << endl;} else if (i%100==0) continue; else if (i%4==0) {n=1;cout << i << endl;} } if (n==0) cout << "NA" << endl; p++; } }
0
CPP
n=int(input()) a=list(map(int,input().split())) m=10**9+7 s=sum(a)%m ans=0 for i in a: ans+=i*(s-i) ans%=m s=(s-i)%m print(ans)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<bool> p(1000000, true); p[0] = false; p[1] = false; for (int i = 2; i < p.size(); i++) { if (!p[i]) continue; for (int j = 2; i * j < p.size(); j++) p[i * j] = false; } int i = 1; while (n) { if (p[i]) { int t = i; int r = 0; while (t > 0) { r = r * 10 + t % 10; t /= 10; } if (p[r] && r != i) { n--; } } i++; } cout << i - 1 << endl; }
12
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "-funroll-loops", "-fdelete-null-pointer-checks") #pragma GCC target("ssse3", "sse3", "sse2", "sse", "avx2", "avx") #pragma GCC optimize(3, "Ofast", "inline") using namespace std; template <class T> bool ckmax(T& x, T y) { return x < y ? x = y, 1 : 0; } template <class T> bool ckmin(T& x, T y) { return x > y ? x = y, 1 : 0; } inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = 0; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f ? x : -x; } const int K = 103; const int N = 52 * K; const int D = K - 2; const int M = 2 * (K + 52 + 52 * K + 2 * 52 * 52 * K); const int inf = 0x3f3f3f3f; int n, m, k, c, d, a[52], S, T; int dis[N], mf, mc, inc[N], pre[N]; bool vis[N], inq[N]; struct edge { int nx, to, fl, c; } e[M]; int et = 1, hed[N]; void addedge(int u, int v, int fl, int c) { e[++et].nx = hed[u], e[et].to = v, e[et].fl = fl, e[et].c = c, hed[u] = et; } void adde(int u, int v, int fl, int c) { addedge(u, v, fl, c), addedge(v, u, 0, -c); } int id(int dep, int x) { return (dep - 1) * n + x; } bool spfa() { queue<int> q; for (int i = 1, iend = T; i <= iend; ++i) dis[i] = inf, inq[i] = 0; inc[S] = inf, dis[S] = 0, q.push(S); while (!q.empty()) { int u = q.front(); q.pop(), inq[u] = 0; for (int i = hed[u]; i; i = e[i].nx) { int v = e[i].to; if (e[i].fl && ckmin(dis[v], dis[u] + e[i].c)) { pre[v] = i, inc[v] = min(e[i].fl, inc[u]); if (!inq[v]) inq[v] = 1, q.push(v); } } } return dis[T] < inf; } void EK() { int rl = inc[T]; mf += rl; for (int u = T; u != S; u = e[pre[u] ^ 1].to) e[pre[u]].fl -= rl, e[pre[u] ^ 1].fl += rl, mc += e[pre[u]].c * rl; } signed main() { n = read(), m = read(), k = read(), c = read(), d = read(), S = id(D, n) + 1, T = S + 1; for (int i = 1, iend = k; i <= iend; ++i) a[i] = read(), adde(S, id(1, a[i]), 1, 0); for (int i = 1, iend = m; i <= iend; ++i) { int x = read(), y = read(); for (int j = 1, jend = D - 1; j <= jend; ++j) for (int l = 0, lend = k - 1; l <= lend; ++l) adde(id(j, x), id(j + 1, y), 1, (2 * l + 1) * d + c), adde(id(j, y), id(j + 1, x), 1, (2 * l + 1) * d + c); } for (int i = 1, iend = D - 1; i <= iend; ++i) for (int j = 1, jend = n; j <= jend; ++j) adde(id(i, j), id(i + 1, j), k, c); for (int i = 1, iend = D; i <= iend; ++i) adde(id(i, 1), T, k, 0); while (spfa()) EK(); printf("%d\n", mc); return 0; }
13
CPP
#include <bits/stdc++.h> int main() { char str[150]; int word = 0, seq = 0; bool ok = true; scanf("%[^\n]", str); int leng = strlen(str); for (int i = 0; i < leng; i++) { if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '_') { word = 1; continue; } if (str[i] == '@') { if (word == 0) { ok = false; } if (seq > 0) { ok = false; } else { seq = 1; } word = 0; continue; } if (str[i] == '.') { if (word == 0) { ok = false; } if (seq > 2 || seq == 0) { ok = false; } else { seq = 2; } word = 0; continue; } if (str[i] == '/') { if (word == 0) { ok = false; } if (seq > 2 || seq == 0) { ok = false; } else { seq = 3; } word = 0; continue; } else { ok = false; } } (ok && word && seq >= 1) ? puts("YES") : puts("NO"); return 0; }
7
CPP
def modFact(n, p): if n >= p: return 0 result = 1 for i in range(1, n + 1): result = (result * i) % p return result def maxWays(n,p): # return (2^(n - 1)) -2 return ((pow(2, n - 1) - 2)%p); # Driver Code n = int(input()) p = 10**9+7 a=modFact(n, p) print((a-maxWays(n,p)-2)%p)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; inline namespace Infinity { inline namespace IO { const char CR = '\n'; const char SP = ' '; inline void write(const int n) { printf("%d", n); } inline void write(const unsigned n) { printf("%u", n); } inline void write(const long long n) { cout << n; } inline void write(const unsigned long long n) { cout << n; } inline void writef(const double a, const int n = 15) { printf("%.*f", n, a); } inline void writef(const long double a, const int n = 18) { cout << setprecision(n) << fixed << a; } inline void write(const char c) { printf("%c", c); } inline void write(const char s[]) { printf("%s", s); } inline void write(const string &s) { cout << s; } inline void write(const pair<int, int> &p) { printf("%d %d", p.first, p.second); } template <class T> inline void write(const T a) { for (auto i : a) write(i), write(SP); } inline void writeln() { write(CR); } template <typename T> inline void writeln(const T &a) { write(a); write(CR); } inline void writefln(const double a, int n) { printf("%.*f", n, a); write(CR); } inline void writefln(const long double a, int n = 18) { cout << setprecision(n) << fixed << a << endl; } inline void writesln(const int *a, const int l, const int r) { for (int i = l; i <= r; i++) printf("%d ", a[i]); writeln(CR); } template <class T> inline void writelns(const T a) { for (__typeof a.begin() i = a.begin(); i != a.end(); i++) writeln(*i); } template <typename T, typename... types> inline void write(const T &a, const types &...args) { write(a); write(args...); } template <typename... types> inline void writeln(const types &...args) { write(args...); write(CR); } template <typename... types> inline void writeSP(const types &...args) { write(args...); write(SP); } inline void writelnYN(bool b) { writeln(b ? "YES" : "NO"); } inline void writelnyn(bool b) { writeln(b ? "Yes" : "No"); } string caseSharpSpace(int n) { return "Case #" + to_string(n) + ": "; } string caseNoSharpSpace(int n) { return "Case " + to_string(n) + ": "; } string caseSharpNoSpace(int n) { return "Case #" + to_string(n) + ":"; } string caseNoSharpNoSpace(int n) { return "Case " + to_string(n) + ":"; } inline int read(int &n) { return scanf("%d", &n); } inline int read(long long &n) { return cin >> n ? 1 : -1; } template <typename T, typename... types> inline int read(T &n, types &...args) { read(n); return read(args...); } inline char getcc() { char c; do c = getchar(); while (c == ' ' || c == '\n'); return c; } inline int getint() { int n; read(n); return n; } inline long long getll() { long long n; read(n); return n; } inline double getdouble() { double n; scanf("%lf", &n); return n; } inline vector<int> getints(int n) { vector<int> v(n); for (int i = 0; i < n; i++) v[i] = getint(); return v; } inline vector<pair<int, int>> getpairs(int n) { vector<pair<int, int>> v(n); for (int i = 0; i < n; i++) { int a = getint(), b = getint(); v[i] = pair<int, int>(a, b); } return v; } inline void read(string &str, const unsigned size) { char s[size]; scanf("%s", s); str = string(s); } inline string getstr(const unsigned size = 1048576) { string s; read(s, size + 1); return s; } inline string getln(const unsigned size = 1048576) { char s[size + 1]; scanf("%[^\n]", s); getchar(); return string(s); } } // namespace IO inline namespace Functions { inline constexpr int ctoi(const char c) { return c - '0'; } inline constexpr char itoc(const int n) { return n + '0'; } inline int dtoi(const double d) { return round(d); } template <typename T> inline bool in(T x, T l, T r) { return l <= x && x <= r; } template <class T> inline int size(const T &a) { return a.size(); } template <typename T1, typename T2> inline pair<T1, T2> mkp(const T1 &a, const T2 &b) { return make_pair(a, b); } template <class T> inline void sort(T &a) { std::sort(a.begin(), a.end()); } template <class T1, class T2> inline void sort(T1 &a, T2 comp) { std::sort(a.begin(), a.end(), comp); } template <class T1, typename T2> inline int lbound(const T1 &a, const T2 k) { return lower_bound(a.begin(), a.end(), k) - a.begin(); } template <class T1, typename T2> inline int ubound(const T1 &a, const T2 k) { return upper_bound(a.begin(), a.end(), k) - a.begin(); } template <class T1, class T2> int count(T1 &a, T2 k) { return ubound(a, k) - lbound(a, k); } template <class T1, class T2> int find(T1 &a, T2 k) { return count(a, k) ? lbound(a, k) : -1; } template <typename T> inline void clear(T &a) { memset(a, 0, sizeof a); } template <typename T> T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } long long qpow(long long a, long long b, long long c) { return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1; } template <typename T> T exGcd(T a, T b, T &x, T &y) { T d = a; if (b) { d = exGcd(b, a % b, y, x); y -= a / b * x; } else { x = 1; y = 0; } return d; } template <typename T> T mps(T l, T r, T k) { return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1; } template <typename T> T sgn(T a) { return a == 0 ? 0 : a > 0 ? 1 : -1; } template <typename T> constexpr T sq(T a) { return a * a; } } // namespace Functions inline namespace TypeDefine {} inline namespace Constant { const int maxint = INT_MAX; const long long maxll = LLONG_MAX; } // namespace Constant } // namespace Infinity class DisjointSetUnion { public: DisjointSetUnion(int n) : a(n + 1), r(n + 1) { iota(a.begin(), a.end(), 0); } int find(int x) { return a[x] != x ? a[x] = find(a[x]) : x; } void join(int x, int y) { if ((x = find(x)) == (y = find(y))) return; if (r[x] < r[y]) a[x] = y; else a[y] = x; if (r[x] == r[y]) r[x]++; } bool same(int x, int y) { return find(x) == find(y); } protected: vector<int> a; vector<int> r; }; class DimensionConvert { public: DimensionConvert(int, int m) : m(m) {} int ptoi(int x, int y) { return x * m + y; } int ptoi(const pair<int, int> &p) { return ptoi(p.first, p.second); } pair<int, int> itop(int n) { return pair<int, int>(n / m, n % m); } protected: int m; }; int main(int, char *[]) { int n = getint(); int m = getint(); DimensionConvert dc(n, m); vector<pair<int, pair<int, int>>> v; v.reserve(n * m); DisjointSetUnion dsu(n * m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { v.push_back(mkp(getint(), mkp(i, j))); if (i && v[dc.ptoi(i - 1, j)].first == v.back().first) dsu.join(dc.ptoi(i - 1, j), dc.ptoi(i, j)); if (j && v[dc.ptoi(i, j - 1)].first == v.back().first) dsu.join(dc.ptoi(i, j - 1), dc.ptoi(i, j)); } for (int i = 0; i < n; i++) { auto begin = v.begin() + dc.ptoi(i, 0); auto end = v.begin() + dc.ptoi(i + 1, 0); vector<pair<int, pair<int, int>>> t(end - begin); copy(begin, end, t.begin()); sort(t); for (int j = 1; j < size(t); j++) if (t[j].first == t[j - 1].first) dsu.join(dc.ptoi(t[j].second), dc.ptoi(t[j - 1].second)); } for (int j = 0; j < m; j++) { vector<pair<int, pair<int, int>>> t; for (int i = 0; i < n; i++) t.push_back(v[dc.ptoi(i, j)]); sort(t); for (int i = 1; i < n; i++) if (t[i].first == t[i - 1].first) dsu.join(dc.ptoi(t[i].second), dc.ptoi(t[i - 1].second)); } sort(v, [&dsu, &dc](const pair<int, pair<int, int>> &p1, const pair<int, pair<int, int>> &p2) { return p1.first != p2.first ? p1.first < p2.first : dsu.find(dc.ptoi(p1.second)) < dsu.find(dc.ptoi(p2.second)); }); vector<int> xm(n), ym(m), mm(n * m); vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < size(v); i++) { int j = i; while (j < size(v) && v[j].first == v[i].first && dsu.find(dc.ptoi(v[j].second)) == dsu.find(dc.ptoi(v[i].second))) j++; int m = 0; for (int k = i; k < j; k++) m = max(m, max(xm[v[k].second.first], ym[v[k].second.second]) + 1); for (int k = i; k < j; k++) { a[v[k].second.first][v[k].second.second] = m; xm[v[k].second.first] = ym[v[k].second.second] = m; } i = j - 1; } for (int i = 0; i < n; i++, writeln()) for (int j = 0; j < m; j++) write(a[i][j], SP); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int g[22] = {}; int n, m; bool dp[1 << 22] = {}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; g[u] |= (1 << v); g[v] |= (1 << u); } dp[0] = 1; for (int i = 0; i < n; i++) { dp[1 << i] = 1; } int ans = n, ansi = 0; for (int i = 0; i < (1 << n); i++) { int tot = 0; bool flag = 1; for (int j = 0; j < n; j++) { if (((g[j] | (1 << j)) != (1 << n) - 1) && ((g[j] & i) == 0)) { flag = 0; } if (i & (1 << j)) dp[i] |= (dp[i ^ (1 << j)] && (g[j] & i)), tot++; } flag &= dp[i]; if (flag) { if (tot <= ans) { ans = tot; ansi = i; } } } cout << ans << endl; for (int i = 0; i < n; i++) { if (ansi & (1 << i)) { cout << i + 1 << " "; } } cout << endl; return 0; }
9
CPP
import collections for t in range(int(input())): n=int(input()) a=list(map(int,input().split())) al=a[0] bo=0 AA,BB=a[0],0 ll,rr,ans=0,n,1 while rr-ll>1: if ans%2==0: while BB>=AA: ll+=1 if ll==rr: break else: AA+=a[ll] BB=0 al+=AA ans+=1 else: while AA>=BB: rr-=1 if ll==rr: break else: BB+=a[rr] AA=0 bo+=BB ans+=1 print(ans,al,bo)
10
PYTHON3
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) x = int(input()) chg = [0] for i in range(n//2): chg.append(x-a[i]) for i in range(1, n//2+1): chg[i] += chg[i-1] for i in range(1, n//2+1): chg[i] = min(chg[i], chg[i-1]) pref = sum(a) for k in range((n+1)//2, n+1): if pref+chg[n-k]>0: print(k) exit() pref += x print(-1)
11
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 50; const int inf = 0x3f3f3f3f; int a[150]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; bool flag = 1; if (n == 1) cout << a[0] << endl; else { int cnt = a[1] - a[0]; for (int i = 2; i < n; ++i) { if (a[i] - a[i - 1] != cnt) { flag = 0; break; } } if (flag) cout << a[n - 1] + cnt << endl; else cout << a[n - 1] << endl; } return 0; }
20
CPP
#include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <string> #include <climits> using namespace std; #define REP(i,a,n) for(i=(a); i<(int)(n); ++i) #define rep(i,n) REP(i,0,n) #define DEB 0 int add(int x, int y){ return max(x,y); } int imul(int x, int y){ return min(x,y); } int inv(int x){ return 2-x; } int p,q,r; int parse(string in){ //printf("init:%s\n",in.c_str()); switch(in[0]){ case '0': case '1': case '2': return in[0]-'0'; case 'P': return p; case 'Q': return q; case 'R': return r; case '-': return inv(parse(in.substr(1))); case '(': int x=0; int idx=1; for(;idx<in.size();idx++){ if( in[idx]=='(' )x++; if( in[idx]==')' )x--; if( x==0 && (in[idx]=='*' || in[idx]=='+') )break; } //printf("idx:%d\n",idx); if( in[idx]=='*' ){ //printf("%d\n",idx); return imul(parse(in.substr(1,idx-1)), parse(in.substr(idx+1))); }else{ return add(parse(in.substr(1,idx-1)), parse(in.substr(idx+1))); } } } int main(){ string in; while(cin>>in, in!="."){ int ans = 0; for(p=0; p<=2; p++){ for(q=0; q<=2; q++){ for(r=0; r<=2; r++){ if( parse(in)==2 )ans++; } } } printf("%d\n",ans); } return 0; }
0
CPP
# -*- coding: utf-8 -*- """ Created on Mon Dec 23 18:19:49 2019 @author: Tuan """ n = int(input()) s = input() print(abs(s.count('0') - s.count('1')))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; typedef long long ll; void showpq(vector<ll> gq) { vector<ll> g = gq; reverse(g.begin(), g.end()); while (!g.empty()) { cout << g.back() << " "; g.pop_back(); } cout << '\n'; } bool mai(string a, string b) { return a.size() > b.size(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, k, chk = 1, a, f = 0; vector<ll> v; cin >> n >> k; if (n < k) cout << "NO"; else { a = n; while (a != 0) { if (chk > k) { f = 1; break; } v.push_back(pow(2, ((ll)(log2(double(a)))))); a = a - pow(2, ((ll)(log2(double(a))))); chk++; } chk--; if (f == 1) { cout << "NO"; } else { for (ll i = 0, j = 0; i < (k - chk); ++i) { if (v[j] != 1) { v[j] = (ll)(v[j] / 2); v.push_back(v[j]); } else { --i; j++; } } cout << "YES" << endl; sort(v.begin(), v.end()); showpq(v); } } return 0; }
9
CPP
import io,os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n,k,q = map(int,input().split()) mod = 10**9+7 dp = [[0 for j in range((n+1)//2)] for i in range(k//2+1)] dp[0] = [1 for j in range((n+1)//2)] for i in range(1,k//2+1): for j in range((n+1)//2): if j: dp[i][j] += dp[i-1][j-1] dp[i][j] %= mod if j!=(n+1)//2-1: dp[i][j] += dp[i-1][j+1] dp[i][j] %= mod if n%2==1: dp[i][(n+1)//2-1] += dp[i-1][(n+1)//2-2] dp[i][(n+1)//2-1] %= mod else: dp[i][(n+1)//2-1] += dp[i-1][(n+1)//2-1] dp[i][(n+1)//2-1] %= mod cnt = [0 for i in range((n+1)//2)] if k%2==0: for i in range((n+1)//2): cnt[i] += dp[k//2][i] * dp[k//2][i] cnt[i] %= mod sub = [dp[-1][j] for j in range((n+1)//2)] for i in range(k//2+1,k+1): next = [0 for j in range((n+1)//2)] for j in range((n+1)//2): if j: next[j] += sub[j-1] next[j] %= mod if j!=(n+1)//2-1: next[j] += sub[j+1] next[j] %= mod if n%2==1: next[(n+1)//2-1] += sub[(n+1)//2-2] next[(n+1)//2-1] %= mod else: next[(n+1)//2-1] += sub[(n+1)//2-1] next[(n+1)//2-1] %= mod for j in range((n+1)//2): cnt[j] += 2 * next[j] * dp[k-i][j] cnt[j] %= mod sub = next if n%2==1: cnt = cnt + [cnt[-2-j] for j in range(n//2)] else: cnt = cnt + [cnt[-1-j] for j in range(n//2)] a = list(map(int,input().split())) res = 0 for i in range(n): res += a[i] * cnt[i] res %= mod ans = [] for i in range(q): idx,x = map(int,input().split()) idx -= 1 res = res + cnt[idx] * (x - a[idx]) res %= mod a[idx] = x ans.append(res) print(*ans,sep="\n")
10
PYTHON3
#include<iostream> #include<algorithm> #include<vector> using namespace std; typedef long long ll; ll bit[300100]; ll n,p[300100],l[300100]; void init(int n){ for(int i=1;i<=n;i++)bit[i] = 0; } ll sum(int i){ ll res = 0; while(i>0){ res += bit[i]; i -= i&-i; } return res; } void add(int i,ll val){ while(i<=n){ bit[i] += val; i += i&-i; } } int main(){ cin >> n; vector<ll> s(n+1), ss(n+1); ss[0] = s[0] = 0; for(int i=0;i<n;i++){ cin >> p[i]; ss[i+1] = s[i+1] = s[i] + p[i]; } for(int i=0;i<n;i++)cin >> l[i]; sort(ss.begin(),ss.end()); for(int i=0;i<n;i++){ if(ss[i+1]-ss[i]<l[i]){ cout << -1 << endl; return 0; } } n++; ss.erase(unique(ss.begin(),ss.end()), ss.end()); for(int i=0;i<n;i++){ s[i] = lower_bound(ss.begin(),ss.end(),s[i]) - ss.begin(); } init(n); ll res = 0; for(int i=n-1;i>=0;i--){ res += sum(s[i]+1); add(s[i]+1,1); } cout << res << endl; }
0
CPP
import sys LI=lambda:list(map(int, sys.stdin.readline().split())) MI=lambda:map(int, sys.stdin.readline().split()) SI=lambda:sys.stdin.readline().strip('\n') II=lambda:int(sys.stdin.readline()) def fnd1(s, e): global arr, k l, r=0, k-1 ret=-1 while l<=r: m=(l+r)//2 if arr[m]>=s: r=m-1 ret=m else: l=m+1 return ret if arr[ret]<=e else -1 def fnd2(s, e): global arr, k l, r=0, k-1 ret=-1 while l<=r: m=(l+r)//2 if arr[m]<=e: l=m+1 ret=m else: r=m-1 return ret if arr[ret]>=s else -1 def val(s, e): #print(s, e, end=' ') global arr, k, a, b if s==0 or e==0: return 0 if s==e: i=fnd1(s, e) if i==-1: #print(a) return a else: j=fnd2(s, e) #print(b*(j-i+1)) return b*(j-i+1) else: m=(s+e)//2 i=fnd1(s, e) if i==-1: #print(a) return a else: #print('TBC') j=fnd2(s, e) return min(b*(j-i+1)*(e-s+1), val(s, m)+val(m+1, e)) n, k, a, b=MI() arr=sorted(LI()) print(val(1, 2**n))
9
PYTHON3
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) kek = list(map(int, input().split())) if sorted(arr) == arr: print('YES') else: if sum(kek) in (0, n): print('NO') else: print('YES')
8
PYTHON3
__author__ = 'Utena' s=int(input()) n=list(map(int,input().split())) n1=max(n) n2=min(n) for i in range(s): if n[i]==n1: s1=i break for j in range(s): if n[s-1-j]==n2: s2=j break if s1+s2<=s-2: print(s2+s1) else: print(s2+s1-1)
7
PYTHON3
#include<iostream> using namespace std; int main(){ int a,b;cin>>a>>b; cout<<(a-1)*(b-1)<<endl; }
0
CPP
def a(t,i): if i == l - 1: if eval(t)==7: print(t+"=7") exit() return a(t+"+"+N[i+1],i+1) a(t+"-"+N[i+1],i+1) N = input() l = len(N) a(N[0],0)
0
PYTHON3
n,k=[int(x) for x in input().split()] arr=[int(x) for x in input().split()] a=arr[k-1] ans=0 for i in arr: if i>=a and i>0: ans+=1 print(ans)
7
PYTHON3
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<std::vector<int>> e(n); for (int i = 0; i < n - 1; ++i) { int u, v; std::cin >> u >> v; --u; --v; e[u].push_back(v); e[v].push_back(u); } std::vector<int> isroot(n), parent(n); std::function<void(int)> dfs1 = [&](int u) { for (auto v : e[u]) { if (v == parent[u]) continue; parent[v] = u; dfs1(v); if (!isroot[v]) isroot[u] = 1; } if (u == 0 && !isroot[u]) { isroot[u] = 1; isroot[e[u][0]] = 0; } }; parent[0] = -1; dfs1(0); std::vector<int> ans(n), a; std::function<void(int)> dfs2 = [&](int u) { a.push_back(u); for (auto v : e[u]) { if (v == parent[u] || isroot[v]) continue; dfs2(v); } }; int length = 2 * n; for (int i = 0; i < n; ++i) { if (isroot[i] == 1) { length -= 2; a.clear(); dfs2(i); for (int j = 0; j < int(a.size()); ++j) ans[a[j]] = a[(j + 1) % a.size()]; } } std::cout << length << "\n"; for (int i = 0; i < n; ++i) std::cout << ans[i] + 1 << " \n"[i == n - 1]; return 0; }
8
CPP
import sys input = sys.stdin.readline T = int(input()) for i in range (0,T): n, r = map(int,input().split()) a=list(set((map(int,input().split())))) a.sort(reverse=1) leng = len(a) ans = 0 for j in range (0,leng): if (a[j] - ans * r) > 0: ans += 1 else: break print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> res; class segtree { public: struct node { int r = 0, s = 0, p = 0; void apply(char u) { r = 0, s = 0, p = 0; if (u == 'R') r = 1; if (u == 'P') p = 1; if (u == 'S') s = 1; } }; node unite(const node &a, const node &b) { node res; res.r = a.r + b.r; res.s = a.s + b.s; res.p = a.p + b.p; return res; } inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } int n; vector<node> tree; template <typename M> void build(int x, int l, int r, const vector<M> &v) { if (l == r) { tree[x].apply(v[l]); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); build(x + 1, l, y, v); build(z, y + 1, r, v); pull(x, z); } node get(int x, int l, int r, int ll, int rr) { if (l >= ll && rr >= r) { return tree[x]; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); node res{}; if (rr <= y) { res = get(x + 1, l, y, ll, rr); } else { if (ll > y) { res = get(z, y + 1, r, ll, rr); } else { res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); } } return res; } node get(int l, int r) { assert(0 <= l && l <= r && r <= n - 1); return get(0, 0, n - 1, l, r); } void modify(int x, int l, int r, int p, char u) { if (l == r) { tree[x].apply(u); return; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); if (p <= y) { modify(x + 1, l, y, p, u); } else { modify(z, y + 1, r, p, u); } pull(x, z); } template <typename M> segtree(const vector<M> &v) { n = v.size(); assert(n > 0); tree.resize(2 * n - 1); build(0, 0, n - 1, v); } }; set<int> rock, paper, scissor; int n, q; string tour; vector<char> har; int main() { cin >> n >> q; cin >> tour; for (int i = 0; i < n; i++) har.push_back(tour[i]); for (int i = 0; i < n; i++) { if (tour[i] == 'R') rock.insert(i); if (tour[i] == 'P') paper.insert(i); if (tour[i] == 'S') scissor.insert(i); } segtree rev(har); auto calc = [&]() { int res = 0; { if (!scissor.size() && !paper.size()) res += n; if (scissor.size() > 0) { if (paper.size() > 0) { int u = *scissor.begin(), v = *scissor.rbegin(), s = *paper.begin(), t = *paper.rbegin(); res += rev.get(0, min(u, s)).r + rev.get(max(v, t), n - 1).r + rev.get(u, v).r; } else res += n - scissor.size(); } } { if (!paper.size() && !rock.size()) res += n; if (paper.size() > 0) { if (rock.size() > 0) { int u = *paper.begin(), v = *paper.rbegin(), s = *rock.begin(), t = *rock.rbegin(); res += rev.get(0, min(u, s)).s + rev.get(max(v, t), n - 1).s + rev.get(u, v).s; } else res += n - paper.size(); } } { if (!rock.size() && !scissor.size()) res += n; if (rock.size() > 0) { if (scissor.size() > 0) { int u = *rock.begin(), v = *rock.rbegin(), s = *scissor.begin(), t = *scissor.rbegin(); res += rev.get(0, min(u, s)).p + rev.get(max(v, t), n - 1).p + rev.get(u, v).p; } else res += n - rock.size(); } } return res; }; res.push_back(calc()); while (q--) { int i; char u; cin >> i >> u; i--; if (tour[i] == 'R') rock.erase(i); if (tour[i] == 'P') paper.erase(i); if (tour[i] == 'S') scissor.erase(i); tour[i] = u; rev.modify(0, 0, n - 1, i, u); if (u == 'R') rock.insert(i); if (u == 'P') paper.insert(i); if (u == 'S') scissor.insert(i); res.push_back(calc()); } for (auto i : res) cout << i << endl; return 0; }
10
CPP
n = int(input()) string = [] for x in range(n): word = input() string.append(word) for x in range(n): if len(string[x]) > 10: print(string[x][0]+str(len(string[x])-2)+string[x][-1]) else: print(string[x])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long maxN = 1e5 + 5; const long long inf = 1e10; const long long mod = 1e9 + 7; long long n, m; long long a[maxN]; long long maxx; long long sum; long long res; int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (long long i = 1; i <= n; i++) cin >> a[i]; for (long long i = 1; i <= n; i++) { maxx = max(maxx, a[i]); sum += a[i]; } sort(a + 1, a + 1 + n); for (long long i = n; i >= 1; i--) { if (maxx == 0) { res += i; break; } if (a[i] < maxx) { res += maxx - a[i]; maxx = a[i]; } res++; maxx--; } if (maxx > 0) { res += maxx; } cout << (sum - res); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf(" %d", &n); if (n == 1 || n == 2) printf("-1\n"); else { printf("%d", n); for (int i = n - 1; i > 0; i--) printf(" %d", i); printf("\n"); } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, sum = 0; cin >> n; cin.ignore(); string cell; getline(cin, cell); for (int i = 0; i < n; i++) if (cell[i] == '1' && i == n - 1) cout << n; else for (int i = 0; i < n; i++) if (cell[i] == '1') sum++; else { cout << sum + 1; return 0; } return 0; }
7
CPP
n, m = map(int, input().split()) grid = [] sum = 0 for i in range(n): grid.append([0] * m) row = map(int, input().split()) zeroes = 0 ones = 0 for ir, e in enumerate(row): grid[i][ir] = e if e: ones += 1 else: zeroes += 1 sum += 2 ** ones - 1 sum += 2 ** zeroes - 1 for i in range(m): zeroes = 0 ones = 0 for k in range(n): if grid[k][i]: ones += 1 else: zeroes += 1 sum += 2 ** ones - 1 sum += 2 ** zeroes - 1 print(sum - (m * n))
8
PYTHON3
# import sys # sys.stdin = open("test.in","r") # sys.stdout = open("test.out","w") n=int(input()) a=list(map(int,input().split())) if n==1: if a[0]<=15: print(a[0]+15) exit() else: print('15') exit() for i in range(n-1): if a[0]>15: print('15') exit() elif a[i+1]-a[i]>15: print(a[i]+15) exit() print(min(90,a[-1]+15))
7
PYTHON3
num = int(input()) words = [] for _i in range(num): word = input() if len(word) > 10: words.append(word[0] + str(len(word) - 2) + word[-1]) else: words.append(word) for i in words: print(i)
7
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; int value[1 << 4][13][1 << 4][13]; const int mod = 1e9 + 7; struct matrix { int n; long long a[210][210]; matrix() { memset(a, 0, sizeof(a)); } matrix operator*(const matrix &aa) const { matrix ret; ret.n = aa.n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { ret.a[i][j] += a[i][k] * aa.a[k][j] % mod; } ret.a[i][j] %= mod; } } return ret; } }; matrix f_pow(matrix a, int b) { matrix res, temp = a; res.n = a.n; for (int i = 0; i < res.n; i++) res.a[i][i] = 1; while (b) { if (b & 1) res = res * temp; temp = temp * temp; b >>= 1; } return res; } int main() { int n, k, m; scanf("%d %d %d", &n, &k, &m); for (int i = 0; i < (1 << m); i++) { for (int j = 0; j <= k; j++) { value[i][j][i >> 1][j] = 1; if (j != k) value[i][j][(i >> 1) + (1 << (m - 1))][j + 1] = __builtin_popcount(i) + 1; } } matrix a; a.n = ((1 << m) * (k + 1)); for (int i = 0; i < (1 << m); i++) { for (int j = 0; j <= k; j++) { for (int ii = 0; ii < (1 << m); ii++) { for (int jj = 0; jj <= k; jj++) { a.a[i * (k + 1) + j][ii * (k + 1) + jj] = value[i][j][ii][jj]; } } } } a = f_pow(a, n); long long ans = 0; for (int i = 0; i < (1 << m); i++) { ans += a.a[0][i * (k + 1) + k]; } printf("%lld\n", ans % mod); }
12
CPP
n = str(input()).split(' ') find = int(n[1]) ai = str(input()).split(' ') for i in range(len(ai)): ai[i] = int(ai[i]) i = 0 ans = "NO" if len(ai) < find: ans = "YES" else: ans = "NO" while i < len(ai): if i + 1 == find: ans = "YES" break i += ai[i] print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; const int MOD = 1e9 + 7; bool a[maxn]; vector<long long> b; int main() { int n; cin >> n; fill(a, a + n + 1, false); for (int i = 1; i <= n; i++) { int t; scanf("%d", &t); if (t >= 1 && t <= n && !a[t]) a[t] = 1; else b.push_back(t); } sort(b.begin(), b.end()); int ib = 0; long long ans = 0; for (int i = 1; i <= n; i++) { if (!a[i]) { ans += abs(1LL * i - b[ib++]); } } cout << ans; return 0; }
9
CPP
n=int(input()) p=input().rstrip() x=list(p) l=[0]*10; for i in range(0,len(x)): if x[i]=='L': T=l.index(0) l[T]=1; elif x[i]=='R': C=list(l) C.reverse(); B=10-1-C.index(0) l[B]=1; else: l[int(x[i])]=0; for i in range(0,len(l)): print(l[i],end='')
7
PYTHON3
#include "bits/stdc++.h" using namespace std; #ifdef _DEBUG #include "dump.hpp" #else #define dump(...) #endif //#define int long long #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define all(c) begin(c),end(c) const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9) + 7; template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } //#define double long double const double EPS = 1e-8; const double PI = acos(-1); #define equals(a,b) (fabs((a)-(b)) < EPS) #define next(P,i) P[(i+1)%P.size()] #define prev(P,i) P[(i+P.size()-1)%P.size()] struct Point { double x, y; Point() {} Point(double x, double y) :x(x), y(y) {} Point &operator+=(const Point &p) { x += p.x; y += p.y; return *this; } Point &operator-=(const Point &p) { x -= p.x; y -= p.y; return *this; } Point &operator*=(double a) { x *= a; y *= a; return *this; } Point &operator/=(double a) { x /= a; y /= a; return *this; } double abs() { return sqrt(norm()); } double norm() { return x*x + y*y; } }; Point operator+(const Point &p1, const Point &p2) { return Point(p1) += p2; } Point operator-(const Point &p1, const Point &p2) { return Point(p1) -= p2; } Point operator*(const Point &p, double a) { return Point(p) *= a; } Point operator/(const Point &p, double a) { return Point(p) /= a; } bool operator==(const Point &p1, const Point &p2) { return equals(p1.x, p2.x) && equals(p1.y, p2.y); } bool operator<(const Point &p1, const Point &p2) { //return p1.y != p2.y ? p1.y < p2.y : p1.x < p2.x; //y?????? -> x?????? return p1.x != p2.x ? p1.x < p2.x : p1.y < p2.y; //x?????? -> y?????? } bool operator>(const Point &p1, const Point &p2) { return p2 < p1; } inline istream &operator >> (istream &is, Point &p) { double x, y; is >> x >> y; p = Point(x, y); return is; } inline ostream &operator << (ostream &os, const Point &p) { os << p.x << ' ' << p.y; return os; } struct Vector :public Point { using Point::Point; Vector() {} Vector(const Point &P) { x = P.x; y = P.y; } Vector rotate(double rad) { return Vector(x*cos(rad) - y*sin(rad), x*sin(rad) + y*cos(rad)); } Vector unit() { return *this / abs(); } }; //?????? dot product double dot(Vector a, Vector b) { return a.x*b.x + a.y*b.y; } //?????? cross product ?????§???????????£???????????? double cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x; } struct Line { Point p1, p2; Line() {} Line(Point p1, Point p2) :p1(p1), p2(p2) {} }; struct Segment :public Line { using Line::Line; Segment() {} Segment(const Line &L) { p1 = L.p1; p2 = L.p2; } Vector vec() { return p2 - p1; } }; struct Circle { Point c; //center double r; //radius Circle() {} Circle(Point c, double r) :c(c), r(r) {} }; using Polygon = vector<Point>; //degree to radian double rad(double deg) { return PI*deg / 180; } //radian to degree double deg(double rad) { return rad / PI * 180; } //????§? argument double arg(Vector p) { return atan2(p.y, p.x); } //?\???¢??? polar form Vector polar(double r, double a) { return Point(cos(a)*r, sin(a)*r); } //2??????????????????????§???? double angle(Vector a, Vector b) { double lena = a.abs(), lenb = b.abs(); if (lena == 0 || lenb == 0)return 0; //?§£?????? double costheta = dot(a, b) / (lena*lenb); if (equals(costheta, 1))costheta = 1; //???????????? return acos(costheta); } bool inrange(Point p, double x1, double y1, double x2, double y2) { return x1 <= p.x&&p.x <= x2&&y1 <= p.y&&p.y <= y2; } //??´????????? bool is_orthogonal(Vector a, Vector b) { return equals(dot(a, b), 0.0); } bool is_orthogonal(Segment s1, Segment s2) { return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } //???????????? bool is_parallel(Vector a, Vector b) { return equals(cross(a, b), 0.0); } bool is_parallel(Segment s1, Segment s2) { return equals(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0); } //?°???± Point project(Segment s, Point p) { Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / base.norm(); return s.p1 + base*r; } //????°? Point reflect(Segment s, Point p) { return p + (project(s, p) - p)*2.0; } //??????(p0,p1)????????????p2???????????¢??? enum { ONLINE_FRONT = -2, CLOCKWISE, ON_SEGMENT, COUNTER_CLOCKWISE, ONLINE_BACK }; int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0, b = p2 - p0; if (cross(a, b) > EPS)return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS)return CLOCKWISE; if (dot(a, b) < -EPS)return ONLINE_BACK; //p2 p0 p1 if (a.norm() < b.norm())return ONLINE_FRONT; //p0 p1 p2 return ON_SEGMENT; } int ccw(Vector a, Vector b) { if (cross(a, b) > EPS)return COUNTER_CLOCKWISE; if (cross(a, b) < -EPS)return CLOCKWISE; if (dot(a, b) < -EPS)return ONLINE_BACK; //p2 p0 p1 if (a.norm() < b.norm())return ONLINE_FRONT; //p0 p1 p2 return ON_SEGMENT; } //??´?????¨??´?????????????????? bool intersect(Segment a, Segment b) { Point p1 = a.p1, p2 = a.p2, p3 = b.p1, p4 = b.p2; return (ccw(p1, p2, p3)*ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1)*ccw(p3, p4, p2) <= 0); } //(?????????????¶????) //2??????????????¢ double get_distance(Point a, Point b) { return (a - b).abs(); } //??´?????¨???????????¢ double get_distance(Line l, Point p) { return abs(cross(l.p2 - l.p1, p - l.p1) / (l.p2 - l.p1).abs()); } //????????¨???????????¢ double get_distance(Segment s, Point p) { if (dot(s.p2 - s.p1, p - s.p1) < 0.0)return (p - s.p1).abs(); if (dot(s.p1 - s.p2, p - s.p2) < 0.0)return (p - s.p2).abs(); return get_distance(Line(s), p); } //????????¨??????????????¢ double get_distance(Segment s1, Segment s2) { if (intersect(s1, s2))return 0.0; return min( min(get_distance(s1, s2.p1), get_distance(s1, s2.p2)), min(get_distance(s2, s1.p1), get_distance(s2, s1.p2)) ); } //?????¨??´?????????????????? bool intersect(Circle c, Line l) { return get_distance(l, c.c) <= c.r + EPS; } //?????¨?????????????????? ??±?????\????????° int intersect(Circle c1, Circle c2) { double d = get_distance(c1.c, c2.c); if (d > c1.r + c2.r)return 4; if (d == c1.r + c2.r)return 3; if (d + c1.r == c2.r || d + c2.r == c1.r)return 1; if (d + c1.r < c2.r || d + c2.r < c1.r)return 0; return 2; } //????????¨??????????????? Point get_cross_point(Segment a, Segment b) { assert(intersect(a, b)); Vector base = b.p2 - b.p1; double area1 = abs(cross(base, a.p1 - b.p1)); double area2 = abs(cross(base, a.p2 - b.p1)); double t = area1 / (area1 + area2); return a.p1 + (a.p2 - a.p1)*t; } //?????¨??´???????????? pair<Point, Point> get_cross_points(Circle c, Line l) { assert(intersect(c, l)); Vector pr = project(l, c.c); Vector e = (l.p2 - l.p1) / (l.p2 - l.p1).abs(); double base = sqrt(c.r*c.r - (pr - c.c).norm()); return make_pair(pr + e*base, pr - e*base); } //?????¨???????????? pair<Point, Point> get_cross_points(Circle c1, Circle c2) { int m = intersect(c1, c2); assert(m != 4 && m != 0); double d = (c1.c - c2.c).abs(); double a = acos((c1.r*c1.r - c2.r*c2.r + d*d) / (2 * c1.r*d)); double t = arg(c2.c - c1.c); return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a)); } //???????????? enum { OUT = 0, ON, IN }; int contains(const Polygon &pl, Point p) { int n = pl.size(); bool x = false; for (int i = 0; i < n; i++) { Point a = pl[i] - p, b = pl[(i + 1) % n] - p; if (abs(cross(a, b)) < EPS&&dot(a, b) < EPS)return ON; if (a.y > b.y)swap(a, b); if (a.y < EPS&&EPS<b.y&&cross(a, b)>EPS)x = !x; } return (x ? IN : OUT); } int contains(Circle c, Point p) { double d = get_distance(c.c, p); if (equals(d, c.r))return ON; if (d < c.r)return IN; return OUT; } //????§???¢?????¢??? double area(const Polygon &p) { double a = 0; for (size_t i = 0; i < p.size(); i++) a += cross(p[i], p[(i + 1) % p.size()]); return fabs(a / 2.0); } //?????§????????????????¨?????????? bool is_convex(Polygon g) { for (size_t i = 0; i < g.size(); i++) if (ccw(g[i], g[(i + 1) % g.size()], g[(i + 2) % g.size()]) == CLOCKWISE) return false; return true; } //?????? //Graham scan https://en.wikipedia.org/wiki/Graham_scan //??????????????????????????? Polygon convex_hull(Polygon P) { sort(P.begin(), P.end()); Polygon up; for (Point &p : P) { while (up.size() > 1 && ccw(up[up.size() - 2], up[up.size() - 1], p) != CLOCKWISE)up.pop_back(); up.emplace_back(p); } Polygon down; for (Point &p : P) { while (down.size() > 1 && ccw(down[down.size() - 2], down[down.size() - 1], p) != COUNTER_CLOCKWISE)down.pop_back(); down.emplace_back(p); } reverse(up.begin(), up.end()); //???????¨?????????? down.insert(down.end(), up.begin() + 1, up.end() - 1); return down; } //?????? //Graham scan https://en.wikipedia.org/wiki/Graham_scan //????????????????????? Polygon convex_hull_with_points_online(Polygon P) { sort(P.begin(), P.end()); Polygon up; for (Point &p : P) { int _ccw; while (up.size() > 1 && (_ccw = ccw(up[up.size() - 2], up[up.size() - 1], p)) != CLOCKWISE &&_ccw != ONLINE_FRONT) up.pop_back(); up.emplace_back(p); } Polygon down; for (Point &p : P) { int _ccw; while (down.size() > 1 && (_ccw = ccw(down[down.size() - 2], down[down.size() - 1], p)) != COUNTER_CLOCKWISE && _ccw != ONLINE_FRONT) down.pop_back(); down.emplace_back(p); } reverse(up.begin(), up.end()); //???????¨?????????? down.insert(down.end(), up.begin() + 1, up.end() - 1); return down; } //???????§???¢??????????????????????????¢ //calipers https://en.wikipedia.org/wiki/Rotating_calipers double diameter(Polygon P) { P = convex_hull(P); auto mima = minmax_element(P.begin(), P.end()); int I = mima.first - P.begin(); int J = mima.second - P.begin(); double maxd = get_distance(P[I], P[J]); int maxi, maxj, i, j; i = maxi = I; j = maxj = J; do { if (ccw(next(P, i) - P[i], next(P, j) - P[j]) == COUNTER_CLOCKWISE) j = (j + 1) % P.size(); else i = (i + 1) % P.size(); if (maxd < get_distance(P[i], P[j])) { maxd = get_distance(P[i], P[j]); maxi = i, maxj = j; } } while (!(i == I&&j == J)); return maxd; } //????§???¢???(0,0)???????????¨???????????¢ Polygon rotate(const Polygon &P, double rad) { Polygon ret; for (auto &p : P) ret.emplace_back(p.x*cos(rad) - p.y*sin(rad), p.x*sin(rad) + p.y*cos(rad)); return ret; } //Heron's formula double area(double a, double b, double c) { double s = (a + b + c) / 2; return sqrt(s*(s - a)*(s - b)*(s - c)); } //????§???¢????????? Point center(const Polygon &P) { Point ret(0, 0); for (auto &p : P)ret = ret + p; ret = ret / P.size(); return ret; } //?????´???????????? Line get_bisection(const Point &p1, const Point &p2) { Circle c1(p1, INF), c2(p2, INF); //INF ?????¨??????????????? auto ps = get_cross_points(c1, c2); return Line(ps.first, ps.second); } //3??????????????¢??????????????? (3???????????¨????????????????????????) Point get_center(const Point &p1, const Point &p2, const Point &p3) { Line l1 = get_bisection(p1, p2), l2 = get_bisection(p2, p3); return get_cross_point(l1, l2); } //???p????????????c?????\?????????????????\??? pair<Point, Point> get_tangent(const Circle &c, const Point &p) { double d = get_distance(c.c, p); Circle c2((c.c + p) / 2, d / 2); return get_cross_points(c, c2); } signed main() { cin.tie(0); ios::sync_with_stdio(false); Point p; Circle c; cin >> p >> c.c >> c.r; auto res = get_tangent(c, p); if (res.first > res.second)swap(res.first, res.second); cout << fixed << setprecision(10); cout << res.first << endl; cout << res.second << endl; return 0; }
0
CPP