solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; char s[105]; int a, b, x, y, p1, p2; bool check(int k1, int k2) { if (k1 == 0 && k2 == 0) return true; if (x == 0 && y == 0) { if (k1 == 0 && k2 == 0) return true; return false; } if (x == 0) { if (k1 == 0 && (long long)y * k2 > 0 && k2 % y == 0) return true; return false; } if (y == 0) { if (k2 == 0 && (long long)x * k1 > 0 && k1 % x == 0) return true; return false; } if ((long long)x * k2 == (long long)y * k1 && (long long)x * k1 > 0 && (long long)y * k2 > 0 && k1 % x == 0 && k2 % y == 0) return true; return false; } int main() { scanf("%d%d%s", &a, &b, s); x = 0; y = 0; for (int i = 0; s[i]; i++) { if (s[i] == 'L') x--; else if (s[i] == 'R') x++; else if (s[i] == 'D') y--; else y++; } if (check(a, b)) { puts("Yes"); return 0; } int len = strlen(s); for (int i = len - 1; i >= 0; i--) { p1 = a, p2 = b; for (int j = i; j >= 0; j--) { if (s[j] == 'L') p1++; else if (s[j] == 'R') p1--; else if (s[j] == 'D') p2++; else p2--; } if (check(p1, p2)) { puts("Yes"); return 0; } } puts("No"); return 0; }
7
CPP
n, d = map(int, input().split()) a = '#' * d b = '#' + '.'*(d- 1) j = 1 i = 0 while i < n : print(a) i += 1 if i == n : break print([b,b[::-1]][j]) j = (j + 1) % 2 i += 1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> void chmax(T &a, T b) { if (a < b) a = b; } template <class T> void chmin(T &a, T b) { if (a > b) a = b; } const int N = 1e5 + 10; const long long INF = 1LL << 60; void solve() { long long n, q; cin >> n >> q; long long arr[n]; long long d[n - 1]; for (long long i = 0; i < n; i++) { cin >> arr[i]; if (i != 0) d[i - 1] = abs(arr[i] - arr[i - 1]); } while (q--) { long long in[n], out[n]; long long l, r; cin >> l >> r; l--, r--; stack<pair<long long, long long>> st; long long ans = 0; for (long long i = l; i < r; i++) { while (!st.empty() && st.top().first <= d[i]) { out[st.top().second] = i - 1; st.pop(); } if (st.empty()) in[i] = l; else in[i] = st.top().second + 1; st.push(make_pair(d[i], i)); } while (!st.empty()) { out[st.top().second] = r - 1; st.pop(); } for (long long i = l; i < r; i++) { ans += (i - in[i] + 1) * (out[i] - i + 1) * d[i]; } cout << ans << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long Tests = 1; while (Tests--) { solve(); } return 0; }
8
CPP
n,x=map(int,input().split()) l=list(map(int,input().split())) ans=[] d={} w=0 e=0 f=0 for i in l: d[i]=d.get(i,0)+1 for j in list(d.values()): if j>=2: ans.append(0) f=1 p=[t&x for t in l] d2={} for k in p: d2[k]=d2.get(k,0)+1 for i in range(n): if l[i] != p[i]: if d.get(p[i],0): ans.append(1) e=1 for z in list(d2.values()): if z>=2: ans.append(2) w=1 if w+e+f==0: ans.append(-1) print(min(ans))
8
PYTHON3
def revmatch(s,t,i,j): # print("R",i,j) if j==len(t)-1: return True if i>0 and j<len(t)-1: if s[i-1]==t[j+1]: if(revmatch(s,t,i-1,j+1)): return True return False def match(s, t, i , j): # print("C",i,j,len(t)-1) if j==len(t)-1: return True if i<len(s)-1 and j<len(t)-1: if s[i+1]==t[j+1]: if(match(s,t,i+1,j+1)): return True if i>0 and j<len(t)-1: if s[i-1]==t[j+1]: if(revmatch(s,t,i-1,j+1)): return True return False for _ in range(int(input())): s=input() t=input() n=len(s) m=len(t) flag="NO" for i in range(n): if s[i]!=t[0]: continue if(match(s,t,i,0)): flag="YES" break # print(i,flag) print(flag)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mx = 1e5 + 5; int32_t main() { int n; cin >> n; string s; cin >> s; int x = 0, y = 0; for (int i = 1; i < n; i++) { if (s[i - 1] == 'F' && s[i] == 'S') { y++; } if (s[i - 1] == 'S' && s[i] == 'F') { x++; } else { continue; } } if (x > y) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
7
CPP
t=int(input()) for i in range(t): n=int(input()) arr=list(map(int,input().split())) def check(arr,n): brr=[] brr.append(arr[0]) for i in range(1,n-1): if (arr[i]>arr[i-1] and arr[i]>arr[i+1]) or (arr[i]<arr[i-1] and arr[i]<arr[i+1]): brr.append(arr[i]) brr.append(arr[-1]) print(len(brr)) for i in brr: print(i,end=" ") print() return check(arr,n)
8
PYTHON3
# by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def main(): for _ in range(int(input())): n = int(input()) score = [0,0] y = 0 while n: if n == 4: n //= 2 score[y] += n if not n%4 or n%2: n -= 1 score[y] += 1 else: n //= 2 score[y] += n y ^= 1 print(score[0]) #Fast IO Region 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") if __name__ == '__main__': main()
7
PYTHON3
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string t; cin >> t; replace(t.begin(), t.end(), '?', 'D'); cout << t << endl; }
0
CPP
n=int(input()) s=[] for i in range(n): s.append(int(input())) x=list(set(s)) x.sort() if len(x)==1: print('Exemplary pages.') elif len(s)==2 and sum(x)%2==0: print('{} ml. from cup #{} to cup #{}.'.format((x[1]-x[0])//2,s.index(x[0])+1,s.index(x[1])+1)) elif len(x)==3 and s.count(x[0])==1 and s.count(x[2])==1 and x[1]-x[0]==x[2]-x[1]: v=x[1]-x[0] q=s.index(x[0])+1 k=s.index(x[2])+1 print('{} ml. from cup #{} to cup #{}.'.format(v,q,k)) else: print('Unrecoverable configuration.')
8
PYTHON3
#include <iostream> using namespace std; int main(){ int t; cin>>t; long long int ans[t]; int n,c; long long int mini[2],sumi[2]; for(int i=0;i<t;i++){ cin>>n; cin>>mini[0]; cin>>mini[1]; ans[i]=n*(mini[0]+mini[1]); sumi[0]=mini[0]; sumi[1]=mini[1]; for(int j=2;j<n;j++){ cin>>c; mini[j%2]=(c<mini[j%2])?c:mini[j%2]; sumi[j%2]+=c; if(sumi[j%2]-mini[j%2]+sumi[(j+1)%2]-mini[(j+1)%2]+(n-j/2)*mini[j%2]+(n-(j-1)/2)*mini[(j+1)%2]<ans[i]) ans[i]=sumi[j%2]-mini[j%2]+sumi[(j+1)%2]-mini[(j+1)%2]+(n-j/2)*mini[j%2]+(n-(j-1)/2)*mini[(j+1)%2]; } } for(int i=0;i<t;i++){ cout<<ans[i]<<endl; } return 0; }
9
CPP
#include <bits/stdc++.h> #define int long long using namespace std; int n,q,temp,p=0,c=0; int a[100005],b[100005]; signed main() { cin>>n>>q; for (int i=1;i<=q;i++) { string s; cin>>temp>>s; if (s=="AC") a[temp]=1; else { if (a[temp]==0) b[temp]++; } } for (int i=1;i<=n;i++) { if (a[i]) { c++; p+=b[i]; } } cout<<c<<' '<<p<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int N = 110; char str[110]; int v[N]; char *pch; int adj[N][N]; int pos[N], m, n; int dp[N][N]; int solve(int at, int last) { if (at == m) return dp[at][last] = 1; int &ret = dp[at][last]; if (ret != -1) return ret; if (v[at]) { if (last == pos[at]) return ret = 0; else return ret = solve(at + 1, pos[at]); } for (int i = 0; i < n; i++) { if (last == i) continue; if (adj[at][i]) continue; if (solve(at + 1, i)) return ret = 1; } return ret = 0; } vector<string> names; vector<string> words; void build(int at, int last) { if (at == m) return; if (v[at]) { cout << words[at] << endl; build(at + 1, pos[at]); } else { for (int i = 0; i < n; i++) { if (last == i) continue; if (adj[at][i]) continue; if (dp[at + 1][i]) { cout << names[i]; cout << words[at].substr(1, words[at].size() - 1) << endl; build(at + 1, i); return; } } } } int main(void) { int t; cin >> t; while (t--) { names.clear(); words.clear(); memset(dp, -1, sizeof dp); memset(adj, 0, sizeof adj); cin >> n; map<string, int> mapa; for (int i = 0; i < n; i++) { string s; cin >> s; mapa[s] = i; names.push_back(s); } cin >> m; getchar(); for (int i = 0; i < m; i++) { string s; getline(cin, s); for (int j = 0; j < s.size(); j++) str[j] = s[j]; str[s.size()] = '\0'; words.push_back(s); if (s[0] == '?') v[i] = 0; else v[i] = 1; pch = strtok(str, " .,!?:"); while (pch != NULL) { string ss = pch; pch = strtok(NULL, " .,!?:"); if (v[i] == 1) { pos[i] = mapa[ss]; break; } if (mapa.count(ss)) adj[i][mapa[ss]] = 1; } } int r = -1; if (v[0]) { r = solve(1, pos[0]); if (!r) printf("Impossible\n"); else { cout << words[0] << endl; build(1, pos[0]); } } else { for (int i = 0; i < n; i++) { if (!adj[0][i] && solve(1, i)) { r = i; break; } } if (r == -1) printf("Impossible\n"); else { cout << names[r]; cout << words[0].substr(1, words[0].size() - 1) << endl; build(1, r); } } } return 0; }
9
CPP
n=int(input()) a=[] for j in range(n): flag=0 l,r=map(int,input().split()) a.append([l,2*l]) for i in range(len(a)): print(a[i][0],a[i][1])
7
PYTHON3
# import sys # sys.stdin=open("input.in","r") n=int(input()) m=list(map(int,input().split())) c=0 for i in m: if i==1: c+=1 print(c) for i in range(n-1): if(m[i]>=m[i+1]): print(m[i],end=" ") print(m[-1])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long m = 1000000007; string s = "YORG"; int main() { long long x, i = 0, ans = 0; while (cin >> x) { ans += x * i++; } cout << ans; }
9
CPP
n = input().split() print(n[1]+n[0])
0
PYTHON3
t = input() if (int(t) == 2): print("NO") elif(int(t) % 2 == 0): print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5+10; int n, m, a[maxn], b[maxn], p[maxn]; vector<int> to[maxn]; bool cmd(int i, int j) { return a[i]-b[i] < a[j]-b[j]; } int f[maxn]; long long B[maxn], w[maxn]; int find(int x) { return f[x]>0?f[x]=find(f[x]):x; } bool vis[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d%d", a+i, b+i); B[i] = b[i]; p[i] = i; } int u, v; for (int j = 1; j <= m; ++j) { scanf("%d%d", &u, &v); to[u].push_back(v); to[v].push_back(u); } sort(p+1, p+1+n, cmd); for (int _ = 1; _ <= n; ++_) { int i = p[_]; // printf("%d: %d\n", i, a[i]-b[i]); w[i] = max(0, a[i]-b[i]); for (auto __: to[i]) if (vis[__]) { int j = find(__); if (j == i) continue; if (B[j]+w[j] >= a[i]-b[i]) w[i] = min(w[i], w[j]); else w[i] = min(w[i], (a[i]-b[i])-B[j]); B[i] += B[j]; f[j] = i; } vis[i] = 1; // printf("%d: B=%d w=%d\n", i, B[i], w[i]); } cout << w[p[n]]+B[p[n]] << endl; }
0
CPP
#include "bits/stdc++.h" #define ll long long using namespace std; int main(){ vector<pair<int,int>> tas; ll n,tmp1,tmp2; cin>>n; for(int i=0;i<n;i++){ cin>>tmp1>>tmp2; tas.emplace_back(make_pair(tmp2,tmp1)); } sort(tas.begin(),tas.end()); int t=0; for(int i=0;i<n;i++){ t+=tas[i].second; if(tas[i].first<t){ printf("No\n"); return 0; } } printf("Yes\n"); }
0
CPP
n,m = map(int,input().split()) x = sorted(list(map(int,input().split()))) gap = sorted([x[i+1]-x[i] for i in range(m-1)]) if n < m: print(sum(gap[:m-n])) else: print(0)
0
PYTHON3
n=int(input()) a=list(map(int, input().split())) a=[abs(i) for i in a] print(sum(a))
7
PYTHON3
import sys import math MAXNUM = math.inf MINNUM = -1 * math.inf ASCIILOWER = 97 ASCIIUPPER = 65 def getInt(): return int(sys.stdin.readline().rstrip()) def getInts(): return map(int, sys.stdin.readline().rstrip().split(" ")) def getString(): return sys.stdin.readline().rstrip() def printOutput(a, b): if not a: sys.stdout.write(str(-1) + "\n") else: for x, y in a: sys.stdout.write(str(x) + " " + str(y) + "\n") for x, y in b: sys.stdout.write(str(x) + " " + str(y) + "\n") def incorrectAlg(ln): ans = [] for i in range(1, ln - 3): ans.append((1, i + 1)) ans.append((1, ln - 2)) ans.append((ln - 2, ln - 1)) ans.append((ln - 2, ln)) return ans def correctAlgorithm(ln): ans = [] for i in range(1, ln): ans.append((i, i + 1)) return ans def solve(ln): if ln < 6: a = None else: a = incorrectAlg(ln) b = correctAlgorithm(ln) return a, b def readinput(): ln = getInt() printOutput(*solve(ln)) readinput()
9
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int read() { int f = 1, ans = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ans = ans * 10 + c - '0'; c = getchar(); } return f * ans; } const int MAXN = (1 << 12) + 1; int N, p[MAXN], res, q[MAXN], v[MAXN], Ma[MAXN], A[MAXN]; void fix(int x, int y) { int d = q[x] ^ v[x], k = Ma[d]; if (k == x) return; if (k == y) { swap(p[x], p[y]), swap(Ma[p[x]], Ma[p[y]]); return; } swap(p[k], p[x]), swap(q[k], q[y]), swap(Ma[p[x]], Ma[p[k]]); fix(k, y); } int main() { N = (1 << read()); for (int i = 0; i < N; i++) A[i] = read(), res ^= A[i]; if (res) { printf("Fou\n"); return 0; } for (int i = 0; i < N; i++) p[i] = q[i] = Ma[i] = i; printf("Shi\n"); for (int i = 0; i < N - 1; i++) if (v[i] != A[i]) { int d = v[i] ^ A[i]; v[i] ^= d, v[i + 1] ^= d; fix(i, i + 1); } for (int i = 0; i < N; i++) printf("%d ", p[i]); printf("\n"); for (int i = 0; i < N; i++) printf("%d ", q[i]); printf("\n"); }
11
CPP
#include <bits/stdc++.h> using namespace std; int q, n; int main() { cin >> q; for (int i = 0; i < q; i++) { long long a; long long c = 0; cin >> n; for (int j = 0; j < n; j++) { cin >> a; c += a; } if (c % n == 0) { cout << c / n << endl; } else { while (c % n != 0) { c++; } cout << c / n << endl; } } return 0; }
7
CPP
#include <bits/stdc++.h> const int MAXN(1e5 + 5); inline int read() { char ch(getchar()); int x(0); int flag(1); while (!isdigit(ch)) { if (ch == '-') { flag = -1; } ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return flag * x; } int sum[MAXN]; int a[MAXN]; int dp[MAXN]; int main() { int n(read()); for (int i = 1; i <= n; i++) { a[i] = read(); } for (int i = n; i >= 1; i--) { sum[i] = sum[i + 1] + a[i]; dp[i] = std::max(dp[i + 1], sum[i + 1] - dp[i + 1] + a[i]); } std::printf("%d %d\n", sum[1] - dp[1], dp[1]); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int n; int a[100514]; int c[100514]; long long int c2(long long int k) { return k * (k + 1) / 2; } long long int f(long long int a, long long int b, long long int c, long long int d) { return (a + 1) * (n - b) + (c - a) * (n - d); } long long int sol() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i); int fff = 1; for (int i = 0; i < n; i++) { if (a[i] != a[n - 1 - i]) fff = 0; } if (fff) { return c2(n); } for (int i = 1; i <= n; i++) c[i] = 0; for (int i = 0; i < n; i++) { c[a[i]] ^= 1; } fff = 0; int odd = 0; for (int i = 1; i <= n; i++) { fff += c[i]; if (c[i]) odd = i; } if (n % 2 == 0 && fff != 0) return 0; if (n % 2 == 1 && fff != 1) return 0; if (n % 2) c[odd] ^= 1; int x = 0; for (; x < n; x++) { if (a[x] != a[n - 1 - x]) break; } int l = 0; if (n % 2 == 0) { for (; l < n / 2 + 0 && a[n / 2 - 1 - l] == a[n / 2 + l]; l++) ; } else { for (; l < n / 2 + 1 && a[n / 2 - l] == a[n / 2 + l]; l++) ; } int y = x; fff = 0; for (; y < n / 2; y++) { c[a[y]]++; if (c[a[y]] == 0) fff--; else if (c[a[y]] == +1) fff++; c[a[n - 1 - y]]--; if (c[a[n - 1 - y]] == 0) fff--; else if (c[a[n - 1 - y]] == -1) fff++; if (fff == 0 && y + l >= (n - 1) / 2) { return f(x, y, (n - 1) - y, n - 1 - x); } } for (int k = 0, la, lb; k < 2; k++) { for (int i = 1; i <= n; i++) c[i] = 0; for (int i = x; i < n / 2; i++) c[a[i]]++; fff = 0; if (odd) { c[odd]--; if (c[odd] == -1) fff--; } for (int i = n / 2; i <= n - 1 - x; i++) { c[a[i]]--; if (c[a[i]] == -1) fff--; } for (y = n / 2; y <= n - 1 - x; y++) { c[a[y]] += 2; if (c[a[y]] == 0) fff++; if (fff == 0) { if (k == 0) { la = x; lb = y; } else { return f(la, lb, (n - 1) - y, n - 1 - x); } break; } } for (int i = 0; i + i < n; i++) swap(a[i], a[n - 1 - i]); } return 7122; } int main() { while (1) { long long int ans = sol(); printf("%I64d\n", ans); break; } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1e6 + 5; const long long LINF = 1e18; const int MAXN = 1e6 + 5; const int MOD = 1e9 + 7; const double eps = 1e-14; int a[MAXN], n; void solve() { cin >> n; bool odd = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] % 2 == 1) odd = 1; } if (odd) cout << "First"; else cout << "Second"; } int main() { if (!1) { freopen( "474" ".in", "r", stdin); freopen( "474" ".out", "w", stdout); } ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; const int mod = 998244353; int add(int x, int y) { x += y; return x >= mod ? x - mod : x; } int sumdp[maxn], dp[maxn][105]; int len, k, n, nm[maxn], cnt[105][maxn]; int main() { scanf("%d%d%d", &n, &k, &len); if (len == 1) { puts("0"); return 0; } for (int i = 1; i <= n; i++) { scanf("%d", &nm[i]); for (int j = 1; j <= k; j++) { cnt[j][i] = cnt[j][i - 1] + (nm[i] == -1 || nm[i] == j); } } if (nm[1] == -1) { for (int j = 1; j <= k; j++) dp[1][j] = 1; sumdp[1] = k; } else dp[1][nm[1]] = 1, sumdp[1] = 1; for (int i = 2; i <= n; i++) { if (nm[i] != -1) { int j = nm[i]; dp[i][j] = sumdp[i - 1]; if (i < len || cnt[j][i] - cnt[j][i - len] != len) goto zz; if (i == len) dp[i][j] = add(dp[i][j], mod - 1); else dp[i][j] = add(dp[i][j], add(mod - sumdp[i - len], dp[i - len][j])); } else { for (int j = 1; j <= k; j++) { dp[i][j] = sumdp[i - 1]; if (i < len || cnt[j][i] - cnt[j][i - len] != len) continue; if (i == len) dp[i][j] = add(dp[i][j], mod - 1); else dp[i][j] = add(dp[i][j], add(mod - sumdp[i - len], dp[i - len][j])); } } zz: for (int j = 1; j <= k; j++) sumdp[i] = add(sumdp[i], dp[i][j]); } printf("%d", sumdp[n]); }
12
CPP
#include<stdio.h> #pragma GCC target("avx") #pragma GCC optimize("O3") #define mod 1000000007 typedef long long lint; int k,n=-1,cnt=13; char c[15]; lint fact[2000010],inv[2000010]; int main(){ while(true){ *c=getchar_unlocked(); if(*c=='\n')break; k*=10; k+=*c-'0'; } while(getchar_unlocked()!=-1)n++; fact[0]=1; for(int i=1;i<=n+k;i++)fact[i]=fact[i-1]*i%mod; inv[n+k]=1; lint now=fact[n+k]; int b=mod-2; while(b){ if(b&1){ inv[n+k]*=now; inv[n+k]%=mod; } b>>=1; now*=now; now%=mod; } for(int i=n+k-1;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod; lint ans=1;now=1; for(int i=1;i<=k;i++){ now*=25; now%=mod; ans+=fact[n+k]*inv[n+k-i]%mod*inv[i]%mod*now; ans%=mod; } *(c+14)='\n'; while(ans){ *(c+cnt)=ans%10+'0'; ans/=10; cnt--; } fwrite(c+cnt+1,1,14-cnt,stdout); return 0; }
0
CPP
n=int(input()) ls=list(map(int,input().split())) neg=0 pos=0 for i in ls: if(i<0): neg+=i else: pos+=i print(pos-neg)
7
PYTHON3
from math import ceil n,m,a,b=map(int,input().split()) w1=((ceil(n/m)*m)-n)*a w2=(n-((n//m)*m))*b print(min(w1,w2))
7
PYTHON3
l2=input().split() n=int(l2[0]) k=int(l2[1]) min=0 l=[int(x) for x in input().split()] ans=1 for i in range(k): min=min+l[i] sum=min if min==k: print(1) else: for i in range(n-k): sum=sum+l[i+k]-l[i] if sum<min: min=sum ans=i+2 print(ans)
8
PYTHON3
from functools import reduce import heapq, sys input = sys.stdin.readline N = int(input()) XD = sorted([tuple(map(int, input().split())) for _ in range(N)], key=lambda x: x[0], reverse=True) P = 998244353 class Node: def __init__(self, ch): self.ch = ch if len(self.ch) == 0: self.score = 2 else: self.score = reduce(lambda a, b: (a * b) % P, [ch.score for ch in self.ch]) + 1 hq = [] # (x, Node) heapq.heapify(hq) for x, d in XD: ch = [] while len(hq) != 0 and hq[0][0] < x+d: ch.append(heapq.heappop(hq)[1]) heapq.heappush(hq, (x, Node(ch))) ans = reduce(lambda a, b: (a * b) % P, [node.score for x, node in hq]) print(ans)
0
PYTHON3
from itertools import combinations_with_replacement t = input() t = int(t) while t > 0: n = input() n = int(n) s = input() combs = combinations_with_replacement(["0", "1"], n) for comb in combs: count = 0 x = "".join(comb) for i in range(n): ss = s[i: i + n] for j in range(len(ss)): if ss[j] == x[j]: count += 1 break if count == n: print(x) break t -= 1
7
PYTHON3
from math import factorial from collections import Counter from heapq import heapify, heappop, heappush import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------------------ def RL(): return map(int, sys.stdin.readline().rstrip().split()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 1000000007 INF = float('inf') from math import ceil # ------------------------------ def main(): N = int(input()) for _ in range(N): n, x = RL() arr = list(RL()) # arr.sort(reverse=True) ma = max(arr) res = ceil(x/ma) # print(x, ma, res) if ma>x: if x in arr: print(1) else: print(2) else: print(res) if __name__ == "__main__": main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m; cin >> n >> m; long long int i, j, a[m][n], x = -1, z = 1; map<int, int> d; for (i = 0; i < m; i++) { x = -1; for (j = 0; j < n; j++) { cin >> a[i][j]; if (a[i][j] > x) { x = a[i][j]; z = j + 1; } } d[z]++; } x = -1; z = 1; for (auto i : d) { if (i.second > x) { x = i.second; z = i.first; } } cout << z << endl; }
7
CPP
import math n,k = map(int,input().split()) if n<=(2*k+1): print(1) print(math.ceil(n/2)) exit() x=(math.ceil(n/(2*k+1))) k1=n%(2*k+1);ans=[] if k1>0 and k1<k+1 : print(x);l=2*k+1; for i in range(1,n+1,l): print(i,end=" ") else: print(x);l=2*k+1 for i in range(k+1,n+1,l):print(i,end=" ")
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int fx[] = {1, -1, 0, 0}; int fy[] = {0, 0, 1, -1}; class data { public: int cc; int freq; data() {} data(int a, int b) { cc = a; freq = b; } bool operator<(const data& b) const { return freq < b.freq; } }; vector<data> vv; string ss; int k; int yo[250]; int main() { std::ios_base::sync_with_stdio(false); cin >> ss; cin >> k; for (int i = 0; i <= (int)ss.size() - 1; i++) { yo[ss[i]]++; } for (int i = 'a'; i <= 'z'; i++) { vv.push_back(data(i, yo[i])); } sort(vv.begin(), vv.end()); for (int i = 0; i <= vv.size() - 1; i++) { yo[vv[i].cc] -= min(vv[i].freq, k); k -= min(vv[i].freq, k); } int ans = 0; for (int i = 'a'; i <= 'z'; i++) { if (yo[i]) { ans++; } } cout << ans << "\n"; for (int i = 0; i <= (int)ss.size() - 1; i++) { if (yo[ss[i]]) { cout << ss[i]; yo[ss[i]]--; } } return 0; }
9
CPP
n=int(input()) x=input() ca=0 cd=0 for i in x: if i=='A': ca+=1 else: cd+=1 if ca>cd: print("Anton") elif cd>ca: print("Danik") else: print("Friendship")
7
PYTHON3
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<iostream> #include<map> #include<string> #define maxn 400005 #define MOD 924844033 using namespace std; inline int getint() { int num=0,flag=1;char c; while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1; while(c>='0'&&c<='9')num=num*10+c-48,c=getchar(); return num*flag; } int n; int fir[maxn],nxt[maxn],to[maxn],cnt; int rev[maxn<<2]; int sz[maxn],a[maxn<<2],b[maxn<<2]; int fac[maxn],inv[maxn]; inline void newnode(int u,int v) {to[++cnt]=v,nxt[cnt]=fir[u],fir[u]=cnt;} inline void dfs(int u,int fa) { sz[u]=1; for(int i=fir[u];i;i=nxt[i])if(to[i]!=fa) { dfs(to[i],u),sz[u]+=sz[to[i]]; a[sz[to[i]]]++; } a[n-sz[u]]++; } inline int ksm(int num,int k) { int ret=1; for(;k;k>>=1,num=1ll*num*num%MOD)if(k&1)ret=1ll*ret*num%MOD; return ret; } inline void getrev(int N) {for(int i=0;i<N;i++)rev[i]=(rev[i>>1]>>1)|(i&1?N>>1:0);} inline void NTT(int *a,int opt,int N) { for(int i=0;i<N;i++)if(rev[i]<i)swap(a[i],a[rev[i]]); for(int i=1;i<N;i<<=1) { int wn=ksm(5,(MOD-1)/(i<<1)); if(!~opt)wn=ksm(wn,MOD-2); for(int j=0;j<N;j+=(i<<1))for(int k=0,w=1;k<i;k++,w=1ll*w*wn%MOD) { int x=a[j+k],y=1ll*a[i+j+k]*w%MOD; a[j+k]=(x+y)%MOD,a[i+j+k]=(x-y+MOD)%MOD; } } if(!~opt)for(int i=0,Inv=ksm(N,MOD-2);i<N;i++)a[i]=1ll*a[i]*Inv%MOD; } int main() { n=getint(); for(int i=1;i<n;i++) { int u=getint(),v=getint(); newnode(u,v),newnode(v,u); } fac[0]=fac[1]=inv[0]=inv[1]=1; for(int i=2;i<=n;i++)fac[i]=1ll*fac[i-1]*i%MOD; for(int i=2;i<=n;i++)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD; for(int i=2;i<=n;i++)inv[i]=1ll*inv[i]*inv[i-1]%MOD; dfs(1,0);a[0]=0; for(int i=0;i<=n;i++)a[i]=1ll*a[i]*fac[i]%MOD; for(int i=0;i<=n;i++)b[i]=inv[n-i]; int len=1; while(len<=2*n)len<<=1; getrev(len); NTT(a,1,len),NTT(b,1,len); for(int i=0;i<len;i++)a[i]=1ll*a[i]*b[i]%MOD; NTT(a,-1,len); for(int i=1;i<=n;i++) { int ans=1ll*fac[n]*inv[i]%MOD*inv[n-i]%MOD*n%MOD; printf("%lld\n",(ans-1ll*a[i+n]*inv[i]%MOD+MOD)%MOD); } }
0
CPP
T = int(input()) for _ in range(T): l, r = map(int, input().split()) if r >= 2*l: print("NO") else: print("YES")
7
PYTHON3
n=int(input()) a=list(map(int,input().split())) max=min=a[0] cnt=0 for i in range(1,n): if a[i]>max: cnt+=1 max=a[i] if a[i]<min: cnt+=1 min=a[i] print(cnt)
7
PYTHON3
#include <bits/stdc++.h> #define PI 3.141592653589793 using namespace std; int main() { // your code goes here int a,b,C; cin >> a >> b >> C; double c = C/180.0*PI; double S = a*b*sin(c)/2.0; double L = a+b+sqrt(a*a+b*b-2*a*b*cos(c)); double h = b*sin(c); printf("%.6lf\n%.6lf\n%.6lf\n",S,L,h); return 0; }
0
CPP
t = int(input()) def solve(mid): if n + (mid + 1) > sum_: return True else: return False for _ in range(t): n, x, y = list(map(int, input().split())) sum_ = x + y max_ = min(sum_ - 1, n) ok = 10**18 ng = 0 if solve(0): print(1, max_) continue while abs(ok - ng) > 1: mid = (ok + ng) // 2 if solve(mid): ok = mid else: ng = mid print(min(ok + 1, n), max_)
8
PYTHON3
n = int(input()) problems = 0 for num in range(n): choice = input() petya_num, vasya_num, tonya_num = int(choice[0]), int(choice[2]), int(choice[4]) s = petya_num + vasya_num + tonya_num if s > 1: problems += 1 print(problems)
7
PYTHON3
#include <iostream> #include <cstdio> using namespace std; int n, cnt; string s; int main() { cin >> n >> s; for(int i=1; i<=n; i++) if(s[i] != s[i-1]) cnt++; cout << cnt; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int L; cin >> L; vector<long long> A(L); for (auto& x : A) cin >> x; vector<vector<long long>> a(L+1, vector<long long>(5)); for (int i = 0; i < L; ++ i) { auto m = 1LL<<60; for (int j = 0; j < 5; ++ j) { m = min(m, a[i][j]); if (j == 0 || j == 4) { a[i+1][j] = m + A[i]; } else { auto k = (A[i] + j + 1) % 2; if (A[i] == 0 && j%2 == 1) k = 2; a[i+1][j] = m + k; } } } /*for (int i = 0; i < L+1; ++ i) { for (int j = 0; j < 5; ++ j) { cerr << a[i][j] << " "; } cerr << endl; }*/ auto r = 1LL<<60; for (int j = 0; j < 5; ++ j) r = min(r, a[L][j]); cout << r << endl; }
0
CPP
a, b = map(int, input().split()) if b - a == 1 and a != 0: print(str(a) + "99", str(b) + "00") elif a - b == 8 and a != 0: print(str(a) + "9", str(b) + "00") elif a == b and a != 0: print(str(a) + "12", str(b) + "13") else: print(-1)
7
PYTHON3
n=list(map(int,input().split())) a=list(map(int,input().split())) k=a[n[1]-1] count=0 for i in a: if i>=k and i>0: count=count+1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int ar[1010][1010], ar00[1010], ar01[1010], ar10[1010], ar11[1010], diag[1010]; vector<int> vec; int main() { int n, i, j, ans, prev, first, q, b; scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &ar[i][j]); if (i == j) { diag[i] = ar[i][j]; } } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) { if (ar[i][j] == 0 && ar[j][i] == 0) { ar00[i]++; ar00[i]++; } else if (ar[i][j] == 0 && ar[j][i] == 1) { ar01[i]++; ar01[i]++; } else if (ar[i][j] == 1 && ar[j][i] == 0) { ar10[i]++; ar10[i]++; } else if (ar[i][j] == 1 && ar[j][i] == 1) { ar11[i]++; ar11[i]++; } } } } ans = 0; for (i = 0; i < n; i++) { ans = (ans + ar11[i] % 2) % 2; } for (i = 0; i < n; i++) { ans = (ans + diag[i] % 2) % 2; } scanf("%d", &q); while (q--) { int a; scanf("%d", &a); if (a == 1) { scanf("%d", &i); i--; prev = (ar11[i] + diag[i] % 2) % 2; int p, q, r, s; p = ar00[i]; q = ar01[i]; r = ar10[i]; s = ar11[i]; ar00[i] = r; ar10[i] = p; ar11[i] = q; ar01[i] = s; diag[i]++; first = (ar11[i] + diag[i] % 2) % 2; ans = ((ans + first - prev) % 2 + 2) % 2; } else if (a == 2) { scanf("%d", &i); i--; prev = (ar11[i] + diag[i] % 2) % 2; int p, q, r, s; p = ar00[i]; q = ar01[i]; r = ar10[i]; s = ar11[i]; ar11[i] = r; ar01[i] = p; ar00[i] = q; ar10[i] = s; diag[i]++; first = (ar11[i] + diag[i] % 2) % 2; ans = ((ans + first - prev) % 2 + 2) % 2; } else if (a == 3) { vec.push_back((ans + 2) % 2); } } for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { printf("%d", (*it)); } }
9
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; long long a[N], dp[N], up, dn; int n; int main() { cin >> n; for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); up = a[0]; dn = -a[0]; for (int i = 0; i < n; i++) { dp[i + 1] = max(up - a[i], dn + a[i]); up = max(up, dp[i + 1] + a[i + 1]); dn = max(dn, dp[i + 1] - a[i + 1]); } cout << dp[n] << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, x, y; int ans[2001]; int main() { cin >> n >> x >> y; --x; --y; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { int dist = min(j - i, abs(i - x) + abs(j - y) + 1); ++ans[dist]; } } for (int k = 1; k < n; ++k) { cout << ans[k] << endl; } }
0
CPP
t = int(input()) while t>0: c = 0 t-=1 e1 = 0 o1 = 0 e2 = 0 o2 = 0 n = int(input()) a = list(map(int, input().split())) m = int(input()) b = list(map(int, input().split())) i = 0 # print('n:', n) # print('a:', a) # print('m:', m) # print('b:', b) while i<n: if a[i]%2 == 0: e1+=1 else: o1+=1 i+=1 i = 0 while i<m: if b[i]%2 == 0: e2+=1 else: o2+=1 i+=1 c = e1*e2 + o1*o2 print(c)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N], b[N], c[N], d[N], e[N], p, x, y, tot, n, t = 1, g; long long ans = 0; int inline read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void dfs(int v, int g, int f) { d[v] = g; e[f] = v; if (!d[a[v]]) { dfs(a[v], g, f + 1); return; } if (d[a[v]] == g) { y = f; p = 1; for (int i = 1; i < f; i++) if (a[v] == e[i]) { x = i; break; } if (!x) x = f; } } inline void init() { n = read(); for (int i = 1; i <= n; i++) b[i] = read(); for (int i = 1; i <= n; i++) a[i] = read(); } void inline slv(int i) { g = 1e9; x = y = p = 0; t++; dfs(i, t, 1); if (p) { for (int j = x; j <= y; j++) g = min(g, b[e[j]]); ans += g; } } int main() { init(); for (int i = 1; i <= n; i++) { tot = 0; if (d[i] || d[a[i]]) { d[i] = 1; continue; } if (a[i] == i) { d[i] = 1; ans += b[i]; continue; } slv(i); } printf("%I64d", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, q; int sg = 0; int a; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (long long i = 0; i < (n); i++) { for (long long j = 0; j < (n); j++) { cin >> a; if (i == j && a == 1) sg ^= 1; } } cin >> q; for (long long i = 0; i < (q); i++) { cin >> n; if (n == 3) { cout << sg; continue; } sg ^= 1; cin >> n; } cout << "\n"; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; long long f[20][20][20], n[20], ans, z, l; char s[20]; void get() { long long i, j, k, k2; f[0][0][0] = 1; for (i = 1; i <= 10; i++) { for (j = 0; j <= 10; j++) { for (k = 0; k <= 9; k++) { if (k == 4 || k == 7) { for (k2 = 0; k2 <= 9; k2++) { f[k][i][j] += f[k2][i - 1][j - 1]; } } else { for (k2 = 0; k2 <= 9; k2++) { f[k][i][j] += f[k2][i - 1][j]; } } } } } } int main() { long long i, j, j1, j2, j3, j4, j5, j6, k, k1, k2, k3, k4, k5, k6; get(); scanf("%s", s + 1); l = strlen(s + 1); for (i = l; i >= 1; i--) { if (s[i] - 48 == 4 || s[i] - 48 == 7) z++; } n[z]++; long long x = 0; for (i = 0; i <= 10; i++) { x = 0; for (j = 1; j <= l; j++) { for (k = 0; k <= s[j] - 49; k++) { n[i + x] += f[k][l - j + 1][i]; } if (s[j] - 48 == 4 || s[j] - 48 == 7) x++; } if (n[i] == 0) break; } n[0]--; for (i = 1; i <= 10; i++) { long long an = 1; if (n[i]) { an *= n[i]; an %= 1000000007; z = i - 1; for (j1 = 0; j1 <= z; j1++) { if (n[j1] == 0) continue; k1 = an; z -= j1; an *= n[j1]; an %= 1000000007; n[j1]--; for (j2 = 0; j2 <= z; j2++) { if (n[j2] == 0) continue; k2 = an; z -= j2; an *= n[j2]; an %= 1000000007; n[j2]--; for (j3 = 0; j3 <= z; j3++) { if (n[j3] == 0) continue; k3 = an; z -= j3; an *= n[j3]; an %= 1000000007; n[j3]--; for (j4 = 0; j4 <= z; j4++) { if (n[j4] == 0) continue; k4 = an; z -= j4; an *= n[j4]; an %= 1000000007; n[j4]--; for (j5 = 0; j5 <= z; j5++) { if (n[j5] == 0) continue; k5 = an; z -= j5; an *= n[j5]; an %= 1000000007; n[j5]--; for (j6 = 0; j6 <= z; j6++) { if (n[j6] == 0) continue; k6 = an; an *= n[j6]; an %= 1000000007; ans += an; ans %= 1000000007; an = k6; } an = k5; n[j5]++; z += j5; } an = k4; n[j4]++; z += j4; } an = k3; n[j3]++; z += j3; } an = k2; n[j2]++; z += j2; } an = k1; n[j1]++; z += j1; } } else break; } cout << ans; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n, m; bool a[111][111]; int X[] = {-1, 0, 1, 0}; int Y[] = {0, -1, 0, 1}; void DFS(int x, int y) { a[x][y] = 0; for (int i = 0; i < 4; i++) { if (a[x + X[i]][y + Y[i]]) DFS(x + X[i], y + Y[i]); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; char x; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> x; if (x == 'B') a[i][j] = 1; } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j]) { DFS(i, j); ans++; } } } cout << ans; return 0; }
10
CPP
#include <iostream> using namespace std; int main(){ int t; cin >> t; while(t--){ int n,top,right,bottom,left; cin >> n >> top >> right >> bottom >> left; int top1 = top,right1 = right,bottom1 = bottom,left1 = left; // std::vector<int> v = {top,right,bottom,left}; if(n - top1 == 0) { left--; right--; } if(n - top1 == 1) { if(left>right) left--; else right--; } if(n - right1 == 0) { top--; bottom--; } if(n - right1 == 1) { if(top>bottom) top--; else bottom--; } if(n - bottom1 == 0) { left--; right--; } if(n - bottom1 == 1) { if(left>right) left--; else right--; } if(n - left1 == 0) { top--; bottom--; } if(n - left1 == 1) { if(top>bottom) top--; else bottom--; } //cout << top << " " << right << " " << bottom << " " << left << endl; if(top >=0 && right >=0 && bottom >=0 && left >=0) cout << "YES\n"; else cout << "NO\n"; } } // 2 0 1 2 2
8
CPP
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n, k; cin >> n >> k; vector<long long> a(n); vector<long long> b(n); vector<long long> c(n); long long sum1 = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; } for (long long i = 0; i < n; i++) { cin >> b[i]; sum1 += b[i]; } for (long long i = 0; i < n; i++) { c[i] = b[i] - a[i]; } sort(c.begin(), c.end()); reverse(c.begin(), c.end()); long long sum2 = 0; for (long long i = 0; i < k; i++) { sum2 += c[i]; } for (long long i = k; i < n; i++) { if (c[i] > 0) sum2 += c[i]; } cout << sum1 - sum2 << endl; }
9
CPP
""" ~~ Author : Bhaskar ~~ Dated : 23~05~2020 """ import sys from math import sqrt INT_MAX = sys.maxsize INT_MIN = -(sys.maxsize)-1 mod = 1000000007 def div(n): res = [] for i in range(1,int(sqrt(n))+1): if n%i == 0: if n//i == i: res.append(i) else: res.append(n//i) res.append(i) return res def solve(): t = int(sys.stdin.readline()) for _ in range(t): n,k = map(int,sys.stdin.readline().split()) ans = sorted(div(n)) nn = None for i in ans: if i <= k: nn = i print(n//nn) if __name__ == "__main__": solve()
10
PYTHON3
// =================================== // author: M_sea // website: http://m-sea-blog.com/ // =================================== #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> #define re register using namespace std; inline int read() { int X=0,w=1; char c=getchar(); while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); } while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar(); return X*w; } const int N=900000+10; const int mod=1e9+7; inline int qpow(int a,int b) { int c=1; for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) c=1ll*c*a%mod; return c; } int fac[N],ifac[N],pw[N]; inline void init(int n) { fac[0]=1; for (re int i=1;i<=n;++i) fac[i]=1ll*fac[i-1]*i%mod; ifac[n]=qpow(fac[n],mod-2); for (re int i=n;i;--i) ifac[i-1]=1ll*ifac[i]*i%mod; for (re int i=pw[0]=1;i<=n;++i) pw[i]=3ll*pw[i-1]%mod; } inline int C(int n,int m) { return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod; } int main() { init(900000); int n=read(),m=read(),k=read(); if (m<k) swap(m,k); int ans=0,lst=1; for (re int i=0;i<=m+k;++i) { ans=(ans+1ll*C(n+i-1,n-1)*pw[m+k-i]%mod*lst)%mod; if (i<k) lst=2ll*lst%mod; else if (i<m) lst=(2ll*lst+mod-C(i,k))%mod; else lst=(2ll*lst+mod-C(i,k)+mod-C(i,i-m))%mod; } printf("%d\n",ans); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; string s[205]; string fi[205], la[205]; bool dp[201][13][1 << 12]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; fi[i] = la[i] = s[i]; for (int j = 1; j <= 12; j++) { if (j <= s[i].size()) { int val = 0; for (int k = 0; k < j; k++) { if (s[i][k] == '1') val += (1 << k); } dp[i][j][val] = 1; for (int k = j; k < s[i].size(); k++) { if (s[i][k - j] == '1') val--; val /= 2; if (s[i][k] == '1') val += (1 << (j - 1)); dp[i][j][val] = 1; } } } if (fi[i].size() > 20) fi[i] = fi[i].substr(0, 20); if (la[i].size() > 20) la[i] = la[i].substr(la[i].size() - 20, 20); } int m; cin >> m; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; n++; if (fi[a].size() < 20) { fi[n] = fi[a] + fi[b]; } else { fi[n] = fi[a]; } if (la[b].size() < 20) { la[n] = la[a] + la[b]; } else { la[n] = la[b]; } string mid = la[a] + fi[b]; int ans = 0; for (int j = 1; j <= 12; j++) { if (j <= fi[n].size()) { int val = 0; for (int k = 0; k < j; k++) { if (fi[n][k] == '1') val += (1 << k); } dp[n][j][val] = 1; for (int k = j; k < fi[n].size(); k++) { if (fi[n][k - j] == '1') val--; val /= 2; if (fi[n][k] == '1') val += (1 << (j - 1)); dp[n][j][val] = 1; } } if (j <= la[n].size()) { int val = 0; for (int k = 0; k < j; k++) { if (la[n][k] == '1') val += (1 << k); } dp[n][j][val] = 1; for (int k = j; k < la[n].size(); k++) { if (la[n][k - j] == '1') val--; val /= 2; if (la[n][k] == '1') val += (1 << (j - 1)); dp[n][j][val] = 1; } } if (j <= mid.size()) { int val = 0; for (int k = 0; k < j; k++) { if (mid[k] == '1') val += (1 << k); } dp[n][j][val] = 1; for (int k = j; k < mid.size(); k++) { if (mid[k - j] == '1') val--; val /= 2; if (mid[k] == '1') val += (1 << (j - 1)); dp[n][j][val] = 1; } } bool ok = 1; for (int k = 0; k < (1 << j); k++) { dp[n][j][k] |= dp[a][j][k]; dp[n][j][k] |= dp[b][j][k]; if (!dp[n][j][k]) ok = 0; } if (ok) ans = max(ans, j); } printf("%d\n", ans); if (fi[n].size() > 20) fi[n] = fi[n].substr(0, 20); if (la[n].size() > 20) la[n] = la[n].substr(la[n].size() - 20, 20); } }
10
CPP
#include <bits/stdc++.h> using namespace std; double f[110][100010], g[100010]; int s[110]; int main() { int n, m; scanf("%d%d", &n, &m); int s0 = 0; for (int i = 1; i <= n; i++) scanf("%d", &s[i]), s0 += s[i]; double d = 1.0 / (m - 1); f[0][0] = g[0] = 1; for (int i = 1; i <= n; i++) { for (int j = i; j <= i * m; j++) { int lf = max(i - 1, j - m), rf = min((i - 1) * m, j - 1); f[i][j] = d * g[rf] - (lf ? d * g[lf - 1] : 0) - (lf <= j - s[i] && j - s[i] <= rf ? d * f[i - 1][j - s[i]] : 0); } for (int j = 1; j <= i * m; j++) g[j] = g[j - 1] + f[i][j]; } double ans = 0; for (int j = n; j < s0; j++) ans += f[n][j]; printf("%.10lf", ans * (m - 1) + 1); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) const int W = 12, N = 6e5; int countOfA[1 << W], numOfP[N]; char s[W + 1]; map<char, int> st = {{'A', 0}, {'O', 0}, {'X', 0}, {'a', 2}, {'o', 1}, {'x', 1}}, add = {{'A', 1}, {'O', -1}, {'X', 2}, {'a', -1}, {'o', 1}, {'x', -1}}; int dp[1 << W]; int main() { int w, n, m; scanf("%d%d%d", &w, &n, &m); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); countOfA[x]++; } for (int i = 0; i < (1 << w); i++) { for (int j = 0; j < (1 << w); j++) { int x = 0; for (int k = 0; k < w; k++) { x = x * 3 + ((i >> k) & 1) + ((j >> k) & 1); } numOfP[x] += countOfA[i] * countOfA[j]; } } for (int i = 0; i < m; i++) { scanf(" %s", s); int y = 0, d = 0; for (int j = 0, p = 1; j < w; j++, p *= 3) { if (add[s[j]] == -1) y |= 1 << j; dp[1 << j] = add[s[j]] * p; d += st[s[j]] * p; } long long int ans = numOfP[d]; for (int j = 1; j < (1 << w); j++) { dp[j] = dp[j & -j] + dp[j ^ (j & -j)]; if ((j & y) == 0) { ans += numOfP[dp[j] + d]; } } printf("%lld\n", ans); } return 0; }
12
CPP
from collections import defaultdict t = int(input()) for case in range(0, t): n = int(input()) A = input().split() A = [int(x) for x in A] counts = defaultdict(int) max_count = 0 for x in A: counts[x]+=1 max_count = max(max_count, counts[x]) k=0 for x in counts: if counts[x] == max_count: k+=1 print(((n-k)//(max_count-1))-1)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; using INT = long long; using pii = pair<INT, INT>; using pi3 = pair<pii, INT>; INT a[5010]; bool dp[5010][5010]; INT val[5010]; vector<pi3> ans; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); INT n, k, v; cin >> n >> k >> v; for (INT i = 1; i <= n; i++) cin >> a[i]; INT sum = 0; for (INT i = 1; i <= n; i++) sum += a[i]; if (sum < v) { cout << "NO" << endl; return 0; } for (INT i = 0; i < k; i++) val[i] = n + 1; dp[0][0] = 1; for (INT i = 1; i <= n; i++) { for (INT j = 0; j < k; j++) if (dp[i - 1][j]) { INT temp = (j + a[i]) % k; dp[i][temp] = 1; dp[i][j] = 1; val[temp] = min(val[temp], i); } dp[i][a[i] % k] = 1; val[a[i] % k] = min(val[a[i] % k], i); } INT p = v % k; int fff = 0; INT ID, sz; if (p == 0) { cout << "YES" << endl; if (a[1]) cout << (a[1] + k - 1) / k << " 1 2" << endl; a[2] += a[1]; fff = 1; ID = 1; sz = 0; } if (!fff) { INT id = val[p]; if (id == n + 1) { cout << "NO" << endl; return 0; } cout << "YES" << endl; ID = id; p -= (a[id] % k); if (p < 0) p += k; sz = a[id]; id = val[p]; while (p != 0) { p -= (a[id] % k); ans.push_back(pi3(pii(a[id] / k + 1, id), ID)); sz += a[id]; a[id] = 0; if (p < 0) p += k; id = val[p]; } } INT sr = 1; if (ID == sr) sr = n; for (INT i = 1; i <= n; i++) { if (a[i] == 0 || i == ID || i == sr) continue; ans.push_back(pi3(pii((a[i] + k - 1) / k, i), sr)); } INT val = (v - sz) / k; if (val > 0) { ans.push_back(pi3(pii(val, sr), ID)); } if (val < 0) { ans.push_back(pi3(pii(-val, ID), sr)); } for (auto p : ans) { INT u = p.first.first; INT sr = p.first.second; INT ta = p.second; cout << u << " " << sr << " " << ta << endl; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; void fastscan(int &number) { bool negative = false; register int c; number = 0; c = getchar(); if (c == '-') { negative = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48; if (negative) number *= -1; } void Findsol() { string s[9]; for (int i = 0; i < 9; i++) { getline(cin, s[i]); } char c1 = s[0].at(8); char c2 = s[0].at(7); for (int i = 0; i < 9; i++) { int ind = (i / 3 + 3 * (i % 3)); if (s[i].at(ind) != c1) { s[i].at(ind) = c1; } else { s[i].at(ind) = c2; } } for (int i = 0; i < 9; i++) { cout << s[i] << "\n"; } } int main() { int t; fastscan(t); while (t-- > 0) { Findsol(); } return 0; }
10
CPP
testtest = int(input()) for case in range(testtest): n,m=(int(i) for i in input().split()) fristst=[sorted([int(i) for i in input().split()]) for j in range(n)] secondddd=[[] for i in fristst] for iii in range(m): mn=min(range(n), key=lambda x:fristst[x][0]) for i in range(len(fristst)): if i==mn: secondddd[i].append(fristst[i].pop(0)) else: secondddd[i].append(fristst[i].pop()) for testcases in secondddd: for i in testcases: print(i,end=' ') print()
8
PYTHON3
#include<iostream> #include<cstdio> using namespace std; int main(){ int a,b; cin>>a>>b; if(a%b==0) cout<<-1; else cout<<a; }
0
CPP
while 1: H,W = map(int,input().split()) if H==0: break print("#"*W+"\n"+("#"+"."*(W-2)+"#"+"\n")*(H-2)+"#"*W+"\n")
0
PYTHON3
nn=int(input()) xx=list(input()) xx.sort() a=''.join(xx) print(a)
7
PYTHON3
from math import * from bisect import bisect_left, bisect_right from queue import Queue from sys import stdin, stdout from collections import Counter input = lambda: stdin.readline().strip() fast_print = stdout.write t = int(input()) for _ in range(t): n, k = map(int, input().split()) ls = list(map(int, input().split())) peaks = {} for i in range(n): peaks[i] = False cnt = 0 for i in range(1, k-1): if ls[i-1]<ls[i]>ls[i+1]: peaks[i] = True cnt+=1 M = cnt+1 ans = 1 for i in range(k-1, n-1): if ls[i-1]<ls[i]>ls[i+1]: peaks[i] = True cnt+=1 if peaks[i-k+2]: cnt-=1 if cnt+1>M: M = cnt+1 ans = i-k+3 fast_print(str(M)+' '+str(ans)+'\n')
8
PYTHON3
t = int(input()) for tc in range(t): n = int(input()) arr = list(map(int, input().split())) diff = -1 for i in range(n): if arr[i] != arr[0]: diff = i break if(diff == -1): print("NO") else: print("YES") for i in range(n): if(arr[i] != arr[0]): print("1", i+1) for i in range(n): if(i != 0 and arr[i] == arr[0]): print(diff+1, i+1)
10
PYTHON3
a = sorted([input() for i in range(int(input()))], key=lambda x: len(x)) print('YES\n' + '\n'.join(a) if all([a[i] in a[i + 1] for i in range(len(a) - 1)]) else 'NO')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve(std::istream &in, std::ostream &out); int main() { #ifdef LOCAL freopen("../IO/e.in", "r", stdin); // freopen("../IO/e.out", "w", stdout); #else std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); #endif solve(std::cin, std::cout); return 0; } template <typename T> T inverse(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U& x) { value = normalize(x); } template <typename U> static Type normalize(const U& x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type& operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); } template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); } Modular& operator++() { return *this += 1; } Modular& operator--() { return *this -= 1; } Modular operator++(int) { Modular result(*this); *this += 1; return result; } Modular operator--(int) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm( "divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (mod()) ); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) { int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) { value = normalize(value * rhs.value); return *this; } Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } template <typename U> friend const Modular<U>& abs(const Modular<U>& v) { return v; } template <typename U> friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); template <typename U> friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); template <typename U> friend std::istream& operator>>(std::istream& stream, Modular<U>& number); private: Type value; }; template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; } template<typename T, typename U> Modular<T> power(const Modular<T>& a, const U& b) { assert(b >= 0); Modular<T> x = a, res = 1; U p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } template <typename T> bool IsZero(const Modular<T>& number) { return number() == 0; } template <typename T> string to_string(const Modular<T>& number) { return to_string(number()); } template <typename T> std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) { return stream << number(); } template <typename T> std::istream& operator>>(std::istream& stream, Modular<T>& number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } constexpr int md = (int) 1e9 + 7; using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; void solve(std::istream &in, std::ostream &out) { int N, X; in >> N >> X; vector<int> L(N); for (int &l : L) in >> l; sort(L.rbegin(), L.rend()); vector<vector<Mint>> dp(N + 1, vector<Mint>(2 * X, 0)); dp[0][0] = 1; vector<Mint> factorial(N + 1); factorial[0] = 1; for (int i = 1; i <= N; ++i) { factorial[i] = factorial[i - 1] * i; } for (int i = 0; i < N; ++i) { vector<vector<Mint>> ndp(N + 1, vector<Mint>(X + 1, 0)); int l = L[i]; int nums = min(X / l, i); for (int num = 0; num <= nums; ++num) { for (int sum = 0; sum <= X - l; ++sum) { ndp[num + 1][sum + l] = dp[num][sum]; } } for (int num = 1; num <= nums; ++num) { for (int sum = 0; sum <= X; ++sum) { ndp[num][sum] += dp[num][sum] * (-num * (l - 1) + sum); } } for (int num = 1; num <= nums; ++num) { for (int x = 1; x <= l; ++x) { for (int sum = 0; sum <= X - x; ++sum) { ndp[num][sum + x] += dp[num][sum] * 2 * num; } } } for (int num = 2; num <= nums; ++num) { for (int x = 1; x <= l; ++x) { for (int sum = 0; sum <= X - x; ++sum) { ndp[num - 1][sum + x] += dp[num][sum] * num * (num - 1) * (l + 1 - x); } } } dp = ndp; } Mint sum = 0; cout << dp[1][X] << '\n'; // for (int i = 1; i <= N; ++i) { // sum += dp[i][X] * factorial[i]; // } // out << sum << '\n'; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; vector<long double> a(n); for (long long int i = 0; i < n; i++) { cin >> a[i]; } long long int tf = 0; for (long long int i = 0; i < n; i++) { tf += floor(a[i]); } tf = -tf; long long int count = 0; vector<long long int> b(n); for (long long int i = 0; i < n; i++) { if ((long long int)(a[i]) == a[i]) { b[i] = a[i]; } else if (count >= tf) { b[i] = floor(a[i]); } else { b[i] = ceil(a[i]); count++; } } for (long long int i = 0; i < n; i++) { cout << b[i] << "\n"; } }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, one[100009], whe[100009], hh[100009]; int main() { int i, j, k, x; while (scanf("%d", &n) + 1) { for (i = 1; i <= n; i++) scanf("%d", &one[i]); for (i = 1; i <= n; i++) { scanf("%d", &x); whe[x] = i; } for (i = 1; i <= n; i++) hh[whe[one[i]]] = i; for (i = 1; i < n; i++) printf("%d ", hh[i]); printf("%d\n", hh[i]); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100100; const int inf = 1e9; int n; int nums[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> nums[i]; } set<pair<int, pair<int, int> > > magic; magic.insert( pair<int, pair<int, int> >(inf + 1, pair<int, int>(nums[0] + 1, 1))); magic.insert( pair<int, pair<int, int> >(nums[0] - 1, pair<int, int>(-inf - 1, 0))); for (int i = 1; i < n; i++) { auto it = magic.lower_bound(pair<int, pair<int, int> >( nums[i], pair<int, int>(-inf - 1, -inf - 1))); int onde = (*it).second.second; pair<int, pair<int, int> > save = *it; magic.erase(it); int par = -1; if (onde) { par = save.second.first - 1; pair<int, pair<int, int> > esq = pair<int, pair<int, int> >(nums[i] - 1, pair<int, int>(par + 1, 0)); magic.insert(esq); pair<int, pair<int, int> > dir = pair<int, pair<int, int> >( save.first, pair<int, int>(nums[i] + 1, 1)); magic.insert(dir); } else { par = save.first + 1; pair<int, pair<int, int> > esq = pair<int, pair<int, int> >( nums[i] - 1, pair<int, int>(save.second.first, 0)); magic.insert(esq); pair<int, pair<int, int> > dir = pair<int, pair<int, int> >(par - 1, pair<int, int>(nums[i] + 1, 1)); magic.insert(dir); } cout << par << ' '; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = (1e9) + 7, maxN = 2000 + 40; long long cmp[maxN][maxN], fac[maxN]; int n, tmp, cur; int dp[maxN] = {0}; int bp[maxN] = {0}; long long ans; int main() { scanf("%d", &n); for (int(i) = (0); (i) < int(n); ++(i)) { scanf("%d", &tmp); if (tmp == -1) cur++; else { dp[i + 1] = 1; bp[tmp] = 1; } } fac[0] = 1; for (int(i) = (1); (i) < int(cur + 1); ++(i)) fac[i] = (i * fac[i - 1]) % MOD; for (int(i) = (1); (i) < int(cur + 1); ++(i)) for (int(j) = (1); (j) < int(i + 1); ++(j)) if (j == 1) cmp[i][j] = i; else if (j == i) cmp[i][j] = 1; else cmp[i][j] = (cmp[i - 1][j] + cmp[i - 1][j - 1]) % MOD; ans += fac[cur]; int ted = 0; for (int(i) = (1); (i) < int(n + 1); ++(i)) { if (dp[i] != 1 and bp[i] != 1) { ted++; ans = (ans - fac[cur - 1] + MOD) % MOD; for (int(j) = (1); (j) < int(ted); ++(j)) { if (j % 2) { ans = (ans + ((fac[cur - j - 1] * cmp[ted - 1][j]) % MOD)) % MOD; } else ans = (((ans + MOD) - ((fac[cur - j - 1] * cmp[ted - 1][j]) % MOD))) % MOD; } } } printf("%I64d\n", ans); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int n, t, s1[200005], s2[200005], pn, a[200005]; pair<int, int> p[5000005]; bool ok(int l, int r) { if (s2[r] - s2[l - 1] >= t) return 1; if (s1[r] - s1[l - 1] >= t) return 1; return 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); s1[i] = s1[i - 1]; s2[i] = s2[i - 1]; if (a[i] == 1) s1[i]++; else s2[i]++; } for (t = 1; t <= n; ++t) { int v = 1, pos, last, p1 = 0, p2 = 0; while (v <= n) { int l = v, r = n; while (r - l > 1) { int mid = (r + l) >> 1; if (ok(v, mid)) r = mid; else l = mid; } if (ok(v, l)) pos = l; else if (ok(v, r)) pos = r; else break; if (s2[pos] - s2[v - 1] == t) { p2++; last = 2; } else { last = 1; p1++; } v = pos + 1; } if (v == n + 1) { if (a[n] == 1) { if (last == 1 && p1 > p2) p[pn++] = make_pair(p1, t); } else { if (last == 2 && p2 > p1) p[pn++] = make_pair(p2, t); } } } sort(p, p + pn); printf("%d\n", pn); for (int i = 0; i < pn; ++i) printf("%d %d\n", p[i].first, p[i].second); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lca(long long a, long long b) { return (a * b / gcd(a, b)); } long long digitsum(long long n) { long long ans = 0; while (n) { ans += n % 10; n /= 10; } return ans; } vector<long long> combine(const vector<long long>& a, const vector<long long>& b) { vector<long long> res(a.size()); for (long long i = 0; i < a.size(); i++) { res[i] = i; } for (long long i = 0; i < a.size(); i++) { res[i] = b[a[i]]; } return res; } long long cnt(const string& s) { set<char> ans; for (long long i = 0; i < s.length(); i++) ans.insert(s[i]); return ans.size(); } long long get_new_cur(long long id, long long a, long long b, long long f, long long cur, bool flag) { long long d1, d2; if (id & 1) { d1 = f; d2 = a - f; } else { d1 = a - f; d2 = f; } if (flag) { long long z = b - d2; if (cur < d1) return -1; if (z < 0) return -1; else { return z; } } else { long long nz = cur - d2; if (nz < 0) return -1; else return nz; } } int main() { long long n; cin >> n; set<long long> cand; for (long long i = 1; i <= n; i++) { cand.insert(i); } vector<long long> a(n); map<long long, queue<long long> > pos; for (long long i = 0; i < n; i++) { cin >> a[i]; if (pos.find(a[i]) == pos.end()) { pos[a[i]] = queue<long long>(); if (cand.find(a[i]) != cand.end()) cand.erase(a[i]); } pos[a[i]].push(i); } long long cnt = 0; vector<long long> w; map<long long, long long> count; for (auto x : pos) { count[x.first] = x.second.size(); while (x.second.size() > 0) { long long elem = x.second.front(); x.second.pop(); w.push_back(elem); } } sort(w.begin(), w.end()); deque<long long> d; for (auto elem : w) { d.push_back(elem); } cnt = cand.size(); vector<bool> flag(a.size() + 1, false); for (auto x : cand) { while (true) { long long pos = d.front(); d.pop_front(); long long cur = a[pos]; if (cur < x && count[cur] > 1 && !flag[cur]) { flag[cur] = true; continue; } if (count[a[pos]] > 1) { count[a[pos]]--; a[pos] = x; break; } } } cout << cnt << endl; for (long long i = 0; i < a.size(); i++) { cout << a[i] << " "; } return 0; }
10
CPP
#include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<char, int> m; bool f = true; while (n--) { string s; cin >> s; map<char, int> t; for (char c : s) { if (f) m[c]++; t[c]++; } f = false; for (char c = 'a'; c <= 'z'; c++) { m[c] = min(m[c], t[c]); } } for (char c = 'a'; c <= 'z'; c++) { while (m[c]--) cout << c; } cout << endl; }
0
CPP
x=0 n=int(input()) for i in range(n): X=input() if X.find('+') >= 0: x+=1 else: x=x-1 print(x)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 500005; struct Edge { int from, to, nxt; Edge() {} Edge(int from, int to, int nxt) : from(from), to(to), nxt(nxt) {} } edges[maxn << 1]; int n, E_cnt, lst[maxn], siz[maxn], fa[maxn]; struct AS { long long a[2][2]; void clear() { memset(a, 0, sizeof(a)); } long long *operator[](int x) { return a[x]; } } f[maxn], g[maxn], c[maxn], s[maxn]; inline int read() { int res = 0, f_f = 1; int ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f_f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') res = (res << 3) + (res << 1) + (ch - '0'), ch = getchar(); return res * f_f; } inline void add_edge(int from, int to) { edges[++E_cnt] = Edge(from, to, lst[from]); lst[from] = E_cnt; } inline void mul(AS &a, AS &b, AS &cc) { for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) a[j][k] = b[j][k] * cc[0][0] + b[0][0] * cc[j][k]; a[0][0] -= b[0][0] * cc[0][0]; a[1][1] += b[1][0] * (cc[1][0] + cc[0][1]) + cc[1][0] * (b[1][0] + b[0][1]); } inline void dfs1(int u, int cur) { AS b, bb; b.clear(); b[0][0] = 1; siz[u] = 1, fa[u] = cur; for (int i = lst[u]; ~i; i = edges[i].nxt) { Edge &e = edges[i]; if (e.to == cur) continue; dfs1(e.to, u); bb = b, mul(b, bb, f[e.to]); for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { } } siz[u] += siz[e.to]; } f[u][0][0] = b[1][0]; f[u][0][1] = b[1][1]; f[u][1][0] = b[0][0]; f[u][1][1] = b[0][1] + b[1][0]; } long long tot, w[maxn]; inline void dfs2(int u, int cur) { int tot = 0; if (!cur) g[u][1][0] = 1; for (int i = lst[u]; ~i; i = edges[i].nxt) { Edge &e = edges[i]; if (e.to == cur) continue; w[++tot] = e.to; c[tot][0][0] = c[tot][1][0] = c[tot][0][1] = c[tot][1][1] = 0; s[tot][0][0] = s[tot][1][0] = s[tot][0][1] = s[tot][1][1] = 0; } c[0][0][0] = 1; for (int i = 1; i <= tot; i++) { int o = w[i]; mul(c[i], c[i - 1], f[o]); } s[tot + 1].clear(); s[tot + 1][0][0] = 1; for (int i = tot; i; i--) { int o = w[i]; AS b, bb; bb = c[i - 1], mul(b, bb, s[i + 1]); g[o][0][0] = g[u][1][0] * b[0][0]; g[o][0][1] = g[u][1][1] * b[0][0] + g[u][1][0] * (b[0][1] + b[1][0]); g[o][1][0] = g[u][0][0] * b[0][0] + g[u][1][0] * b[1][0]; g[o][1][1] = g[u][0][1] * b[0][0] + g[u][0][0] * (b[1][0] + b[0][1]) + g[u][1][1] * b[1][0] + g[u][1][0] * (b[1][1] + b[0][0]); mul(s[i], s[i + 1], f[o]); } for (int i = lst[u]; ~i; i = edges[i].nxt) { Edge &e = edges[i]; if (e.to == cur) continue; dfs2(e.to, u); } } int main() { memset(lst, -1, sizeof(lst)); n = read(), E_cnt = -1; for (int i = 2; i <= n; i++) { int from = read(), to = read(); add_edge(from, to); add_edge(to, from); } dfs1(1, 0), dfs2(1, 0); long long ans = 0; for (int i = 2; i <= n; i++) { ans += (f[i][0][1] + f[i][1][0]) * g[i][1][1]; ans += f[i][0][0] * g[i][1][0] * siz[i] * (n - siz[i]); } printf("%lld\n", ans); return 0; }
10
CPP
import cmath import itertools import math import os import random import sys INF = float("inf") PI = cmath.pi TAU = cmath.pi * 2 EPS = 1e-10 if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(10 ** 9) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 # MOD = 998244353 class Point: """ 2次元空間上の点 """ # 反時計回り側にある CCW_COUNTER_CLOCKWISE = 1 # 時計回り側にある CCW_CLOCKWISE = -1 # 線分の後ろにある CCW_ONLINE_BACK = 2 # 線分の前にある CCW_ONLINE_FRONT = -2 # 線分上にある CCW_ON_SEGMENT = 0 def __init__(self, x: float, y: float): self.c = complex(x, y) @property def x(self): return self.c.real @property def y(self): return self.c.imag @staticmethod def from_complex(c: complex): return Point(c.real, c.imag) @staticmethod def from_polar(r: float, phi: float): c = cmath.rect(r, phi) return Point(c.real, c.imag) def __add__(self, p): """ :param Point p: """ c = self.c + p.c return Point(c.real, c.imag) def __iadd__(self, p): """ :param Point p: """ self.c += p.c return self def __sub__(self, p): """ :param Point p: """ c = self.c - p.c return Point(c.real, c.imag) def __isub__(self, p): """ :param Point p: """ self.c -= p.c return self def __mul__(self, f: float): c = self.c * f return Point(c.real, c.imag) def __imul__(self, f: float): self.c *= f return self def __truediv__(self, f: float): c = self.c / f return Point(c.real, c.imag) def __itruediv__(self, f: float): self.c /= f return self def __repr__(self): return "({}, {})".format(round(self.x, 10), round(self.y, 10)) def __neg__(self): c = -self.c return Point(c.real, c.imag) def __eq__(self, p): return abs(self.c - p.c) < EPS def __abs__(self): return abs(self.c) @staticmethod def ccw(a, b, c): """ 線分 ab に対する c の位置 線分上にあるか判定するだけなら on_segment とかのが速い Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C&lang=ja :param Point a: :param Point b: :param Point c: """ b = b - a c = c - a det = b.det(c) if det > EPS: return Point.CCW_COUNTER_CLOCKWISE if det < -EPS: return Point.CCW_CLOCKWISE if b.dot(c) < -EPS: return Point.CCW_ONLINE_BACK if b.dot(b - c) < -EPS: return Point.CCW_ONLINE_FRONT return Point.CCW_ON_SEGMENT def dot(self, p): """ 内積 :param Point p: :rtype: float """ return self.x * p.x + self.y * p.y def det(self, p): """ 外積 :param Point p: :rtype: float """ return self.x * p.y - self.y * p.x def dist(self, p): """ 距離 :param Point p: :rtype: float """ return abs(self.c - p.c) def norm(self): """ 原点からの距離 :rtype: float """ return abs(self.c) def phase(self): """ 原点からの角度 :rtype: float """ return cmath.phase(self.c) def angle(self, p, q): """ p に向いてる状態から q まで反時計回りに回転するときの角度 -pi <= ret <= pi :param Point p: :param Point q: :rtype: float """ return (cmath.phase(q.c - self.c) - cmath.phase(p.c - self.c) + PI) % TAU - PI def area(self, p, q): """ p, q となす三角形の面積 :param Point p: :param Point q: :rtype: float """ return abs((p - self).det(q - self) / 2) def projection_point(self, p, q, allow_outer=False): """ 線分 pq を通る直線上に垂線をおろしたときの足の座標 Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A&lang=ja :param Point p: :param Point q: :param allow_outer: 答えが線分の間になくても OK :rtype: Point|None """ diff_q = q - p # 答えの p からの距離 r = (self - p).dot(diff_q) / abs(diff_q) # 線分の角度 phase = diff_q.phase() ret = Point.from_polar(r, phase) + p if allow_outer or (p - ret).dot(q - ret) < EPS: return ret return None def reflection_point(self, p, q): """ 直線 pq を挟んで反対にある点 Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B&lang=ja :param Point p: :param Point q: :rtype: Point """ # 距離 r = abs(self - p) # pq と p-self の角度 angle = p.angle(q, self) # 直線を挟んで角度を反対にする angle = (q - p).phase() - angle return Point.from_polar(r, angle) + p def on_segment(self, p, q, allow_side=True): """ 点が線分 pq の上に乗っているか Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C&lang=ja :param Point p: :param Point q: :param allow_side: 端っこでギリギリ触れているのを許容するか :rtype: bool """ if not allow_side and (self == p or self == q): return False # 外積がゼロ: 面積がゼロ == 一直線 # 内積がマイナス: p - self - q の順に並んでる return abs((p - self).det(q - self)) < EPS and (p - self).dot(q - self) < EPS @staticmethod def circumcenter_of(p1, p2, p3): """ 外心 :param Point p1: :param Point p2: :param Point p3: :rtype: Point|None """ if abs((p2 - p1).det(p3 - p1)) < EPS: # 外積がゼロ == 一直線 return None # https://ja.wikipedia.org/wiki/外接円 a = (p2.x - p3.x) ** 2 + (p2.y - p3.y) ** 2 b = (p3.x - p1.x) ** 2 + (p3.y - p1.y) ** 2 c = (p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2 num = p1 * a * (b + c - a) + p2 * b * (c + a - b) + p3 * c * (a + b - c) den = a * (b + c - a) + b * (c + a - b) + c * (a + b - c) return num / den @staticmethod def incenter_of(p1, p2, p3): """ 内心 :param Point p1: :param Point p2: :param Point p3: """ # https://ja.wikipedia.org/wiki/三角形の内接円と傍接円 d1 = p2.dist(p3) d2 = p3.dist(p1) d3 = p1.dist(p2) return (p1 * d1 + p2 * d2 + p3 * d3) / (d1 + d2 + d3) class Circle: def __init__(self, o, r): """ :param Point o: :param float r: """ self.o = o self.r = r def __eq__(self, other): return self.o == other.o and abs(self.r - other.r) < EPS def ctc(self, c): """ 共通接線 common tangent の数 4: 離れてる 3: 外接 2: 交わってる 1: 内接 0: 内包 inf: 同一 Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=ja :param Circle c: :rtype: int """ if self.o == c.o: return INF if abs(self.r - c.r) < EPS else 0 # 円同士の距離 d = self.o.dist(c.o) - self.r - c.r if d > EPS: return 4 elif d > -EPS: return 3 # elif d > -min(self.r, c.r) * 2: elif d + min(self.r, c.r) * 2 > EPS: return 2 elif d + min(self.r, c.r) * 2 > -EPS: return 1 return 0 def has_point_on_edge(self, p): """ 指定した点が円周上にあるか :param Point p: :rtype: bool """ return abs(self.o.dist(p) - self.r) < EPS def contains(self, p, allow_on_edge=True): """ 指定した点を含むか :param Point p: :param bool allow_on_edge: 辺上の点を許容するか """ if allow_on_edge: # return self.o.dist(p) <= self.r return self.o.dist(p) - self.r < EPS else: # return self.o.dist(p) < self.r return self.o.dist(p) - self.r < -EPS def area(self): """ 面積 """ return self.r ** 2 * PI def circular_segment_area(self, angle): """ 弓形⌓の面積 :param float angle: 角度ラジアン """ # 扇形の面積 sector_area = self.area() * angle / TAU # 三角形部分を引く return sector_area - self.r ** 2 * math.sin(angle) / 2 def intersection_points(self, other, allow_outer=False): """ :param Segment|Circle other: :param bool allow_outer: """ if isinstance(other, Segment): return self.intersection_points_with_segment(other, allow_outer=allow_outer) if isinstance(other, Circle): return self.intersection_points_with_circle(other) raise NotImplementedError() def intersection_points_with_segment(self, s, allow_outer=False): """ 線分と交差する点のリスト Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D&lang=ja :param Segment s: :param bool allow_outer: 線分の間にない点を含む :rtype: list of Point """ # 垂線の足 projection_point = self.o.projection_point(s.p1, s.p2, allow_outer=True) # 線分との距離 dist = self.o.dist(projection_point) # if dist > self.r: if dist - self.r > EPS: return [] if dist - self.r > -EPS: if allow_outer or s.has_point(projection_point): return [projection_point] else: return [] # 足から左右に diff だけ動かした座標が答え diff = Point.from_polar(math.sqrt(self.r ** 2 - dist ** 2), s.phase()) ret1 = projection_point + diff ret2 = projection_point - diff ret = [] if allow_outer or s.has_point(ret1): ret.append(ret1) if allow_outer or s.has_point(ret2): ret.append(ret2) return ret def intersection_points_with_circle(self, other): """ 円と交差する点のリスト Verify: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E&langja :param circle other: :rtype: list of Point """ ctc = self.ctc(other) if not 1 <= ctc <= 3: return [] if ctc == 3: # 外接 return [Point.from_polar(self.r, (other.o - self.o).phase()) + self.o] if ctc == 1: # 内接 if self.r > other.r: return [Point.from_polar(self.r, (other.o - self.o).phase()) + self.o] else: return [Point.from_polar(self.r, (self.o - other.o).phase()) + self.o] # 2つ交点がある assert ctc == 2 a = other.r b = self.r c = self.o.dist(other.o) # 余弦定理で cos(a) を求めます cos_a = (b ** 2 + c ** 2 - a ** 2) / (2 * b * c) angle = math.acos(cos_a) phi = (other.o - self.o).phase() return [ self.o + Point.from_polar(self.r, phi + angle), self.o + Point.from_polar(self.r, phi - angle), ] def tangent_points_with_point(self, p): """ p を通る接線との接点 :param Point p: :rtype: list of Point """ dist = self.o.dist(p) # if dist < self.r: if dist - self.r < -EPS: # p が円の内部にある return [] if dist - self.r < EPS: # p が円周上にある return [Point(p.x, p.y)] a = math.sqrt(dist ** 2 - self.r ** 2) b = self.r c = dist # 余弦定理で cos(a) を求めます cos_a = (b ** 2 + c ** 2 - a ** 2) / (2 * b * c) angle = math.acos(cos_a) phi = (p - self.o).phase() return [ self.o + Point.from_polar(self.r, phi + angle), self.o + Point.from_polar(self.r, phi - angle), ] def tangent_points_with_circle(self, other): """ other との共通接線との接点 :param Circle other: :rtype: list of Point """ ctc = self.ctc(other) if ctc > 4: raise ValueError('2つの円が同一です') if ctc == 0: return [] if ctc == 1: return self.intersection_points_with_circle(other) assert ctc in (2, 3, 4) ret = [] # 共通外接線を求める # if self.r == other.r: if abs(self.r - other.r) < EPS: # 半径が同じ == 2つの共通外接線が並行 phi = (other.o - self.o).phase() ret.append(self.o + Point.from_polar(self.r, phi + PI / 2)) ret.append(self.o + Point.from_polar(self.r, phi - PI / 2)) else: # 2つの共通外接線の交点から接線を引く intersection = self.o + (other.o - self.o) / (self.r - other.r) * self.r ret += self.tangent_points_with_point(intersection) # 共通内接線を求める # 2つの共通内接線の交点から接線を引く intersection = self.o + (other.o - self.o) / (self.r + other.r) * self.r ret += self.tangent_points_with_point(intersection) return ret @staticmethod def circumscribed_of(p1, p2, p3): """ p1・p2・p3 のなす三角形の外接円 Verify: :param Point p1: :param Point p2: :param Point p3: """ if p1.on_segment(p2, p3): return Circle((p2 + p3) / 2, p2.dist(p3) / 2) if p2.on_segment(p1, p3): return Circle((p1 + p3) / 2, p1.dist(p3) / 2) if p3.on_segment(p1, p2): return Circle((p1 + p2) / 2, p1.dist(p2) / 2) o = Point.circumcenter_of(p1, p2, p3) return Circle(o, o.dist(p1)) @staticmethod def min_enclosing(points): """ points をすべて含む最小の円 計算量の期待値は O(N) https://www.jaist.ac.jp/~uehara/course/2014/i481f/pdf/ppt-7.pdf Verify: https://www.spoj.com/problems/QCJ4/ :param list of Point points: :rtype: Circle """ points = points[:] random.shuffle(points) # 前から順番に決めて大きくしていく ret = Circle(points[0], 0) for i, p in enumerate(points): if ret.contains(p): continue ret = Circle(p, 0) for j, q in enumerate(points[:i]): if ret.contains(q): continue # 2点を直径とする円 ret = Circle((p + q) / 2, abs(p - q) / 2) for k, r in enumerate(points[:j]): if ret.contains(r): continue # 3点に接する円 ret = Circle.circumscribed_of(p, q, r) return ret def argsort(li, key=None, reverse=False): return [i for _, i in sorted( [(a, i) for i, a in enumerate(li)], key=(lambda t: key(t[0])) if key else None, reverse=reverse )] N, K = list(map(int, sys.stdin.buffer.readline().split())) XYC = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(N)] def test(t: float): # http://tottoripaper.hatenablog.jp/entry/2015/03/10/231142 # K こ以上の肉を焼けるか circles = [] for x, y, c in XYC: circles.append(Circle(Point(x, y), t /c)) # 交点 points = [] for c1, c2 in itertools.combinations(circles, r=2): points += c1.intersection_points_with_circle(c2) for c in circles: points.append(c.o) for p in points: cnt = 0 for c in circles: cnt += c.contains(p) if cnt >= K: return True return False # # # T: iとjが重なる時間 # T = [] # for i, j in itertools.combinations(range(N), r=2): # xi, yi, ci = XYC[i] # xj, yj, cj = XYC[j] # d = (xi - xj) ** 2 + (yi - yj) ** 2 # t = d / (ci + cj) # T.append((t, i, j)) # T.sort() # print(T) # ans = INF # for t, i, j in T: # xi, yi, ci = XYC[i] # xj, yj, cj = XYC[j] # p = Point(xi, yi) # q = Point(xj, yj) # test_p = Point.from_polar(ci * t, (q - p).phase()) # if test(test_p, t): # ans = t # break # print(ans) ok = 1 << 100 ng = 0 while abs(ok - ng) > EPS: mid = (ok + ng) / 2 if test(mid): ok = mid else: ng = mid print(ok)
0
PYTHON3
#pragma comment(linker, "/STACK:512000000") #define _CRT_SECURE_NO_WARNINGS /*#if !defined(YCM) && 0 #define _FORTIFY_SOURCE 0 #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("avx,tune=native") #include <stdio.h> #endif*/ //#include "testlib.h" #include <bits/stdc++.h> using namespace std; #define all(a) a.begin(), a.end() typedef long long li; typedef long double ld; void solve(bool); void precalc(); clock_t start; int main() { #ifdef AIM freopen("/home/alexandero/ClionProjects/ACM/input.txt", "r", stdin); //freopen("/home/alexandero/ClionProjects/ACM/output.txt", "w", stdout); //freopen("out.txt", "w", stdout); #else //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif start = clock(); int t = 1; cout.sync_with_stdio(0); cin.tie(0); precalc(); cout.precision(10); cout << fixed; //cin >> t; int testNum = 1; while (t--) { //cout << "Case #" << testNum++ << ": "; //cerr << testNum << endl; solve(true); //cerr << testNum - 1 << endl; } cout.flush(); #ifdef AIM1 while (true) { solve(false); } #endif #ifdef AIM cerr << "\n\n time: " << (clock() - start) / 1.0 / CLOCKS_PER_SEC << "\n\n"; #endif return 0; } //BE CAREFUL: IS INT REALLY INT? template<typename T> T binpow(T q, T w, T mod) { if (!w) return 1 % mod; if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod; return binpow(q * 1LL * q % mod, w / 2, mod); } template<typename T> T gcd(T q, T w) { while (w) { q %= w; swap(q, w); } return q; } template<typename T> T lcm(T q, T w) { return q / gcd(q, w) * w; } template <typename T> void make_unique(vector<T>& a) { sort(all(a)); a.erase(unique(all(a)), a.end()); } template<typename T> void relax_min(T& cur, T val) { cur = min(cur, val); } template<typename T> void relax_max(T& cur, T val) { cur = max(cur, val); } void precalc() {} #define int li const int mod = 1000000007; const int C = 2010; int c[C][C]; vector<vector<vector<vector<int>>>> dp; vector<vector<int>> g; vector<int> cnt; void add(int& cur, int val) { cur += val; cur %= mod; } int rev2; using DP = vector<vector<vector<int>>>; void dfs(int v) { cnt[v] = 1; dp[v].assign(1, vector<vector<int>>(2, vector<int>(3, 0))); dp[v][0][0][0] = 1; DP new_dp; int children = 0; for (int to : g[v]) { dfs(to); new_dp.assign(cnt[v] + cnt[to], vector<vector<int>>(2, vector<int>(3, 0))); for (int cur_diff = 0; cur_diff < dp[v].size(); ++cur_diff) { for (int cur_k = 0; cur_k < 2; ++cur_k) { for (int cur_deg = 0; cur_deg <= 2; ++cur_deg) { int cur_dp = dp[v][cur_diff][cur_k][cur_deg]; if (!cur_dp) continue; for (int new_diff = 0; new_diff < dp[to].size(); ++new_diff) { for (int new_k = 0; new_k < 2; ++new_k) { for (int new_deg = 0; new_deg <= 2; ++new_deg) { int to_dp = dp[to][new_diff][new_k][new_deg]; if (!to_dp) continue; add(new_dp[cur_diff + new_diff][cur_k ^ new_k][cur_deg], cur_dp * to_dp); if (cur_deg == 2 || new_deg == 2) { continue; } int dp_deg = cur_deg + 1; int dp_k = (cur_k ^ new_k ^ 1); int dp_diff = cur_diff + new_diff + 1; int val = cur_dp * to_dp % mod; if (new_deg && cur_deg) { val = val * rev2 % mod; } else if (cur_deg == 0 && new_deg == 0) { val = val * 2 % mod; } add(new_dp[dp_diff][dp_k][dp_deg], val); } } } } } } cnt[v] += cnt[to]; dp[v].swap(new_dp); ++children; } } void solve(bool read) { for (int i = 0; i < C; ++i) { c[i][i] = c[i][0] = 1; for (int j = 1; j < i; ++j) { c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; } } rev2 = binpow(2LL, mod - 2, mod); int n; cin >> n; g.resize(n); cnt.assign(n, 0); dp.resize(n); for (int i = 1; i < n; ++i) { int p; cin >> p; --p; g[p].push_back(i); } dfs(0); vector<int> fact(2 * n + 5, 1); for (int i = 1; i < fact.size(); ++i) { fact[i] = fact[i - 1] * i % mod; } int ans = 0; for (int diff = 0; diff < n; ++diff) { int cur_fact = fact[n - diff]; for (int par_k = 0; par_k < 2; ++par_k) { /*for (int j = 0; j < 3; ++j) { if (dp[0][diff][par_k][j] > 0) { cout << diff << " " << par_k << " " << j << " " << dp[0][diff][par_k][j] << endl; } }*/ //cout << diff << " " << par_k << " " << cur_fact * (dp[0][diff][par_k][0] + dp[0][diff][par_k][1] + dp[0][diff][par_k][2]) << endl; add(ans, cur_fact * (dp[0][diff][par_k][0] + dp[0][diff][par_k][1] + dp[0][diff][par_k][2])); cur_fact = -cur_fact; } } if (ans < 0) { ans += mod; } cout << ans << endl; }
0
CPP
number_of_testcases=int(input()) for i in range(number_of_testcases): n,m=list(map(int,input().split())) a=sum(map(int,input().split(' '))) print(min(a,m))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int k, a, b, v; int res = 0, val = 0; cin >> k >> a >> b >> v; for (int i = 1; i < 1000; i++) { int res = min(k * i, b + i); if (res * v >= a) { cout << i << endl; break; } } }
7
CPP
import sys def main(): def input(): return sys.stdin.readline()[:-1] N, k = map(int,input().split()) # union-find parent = [k for k in range(N)] def find(x): if parent[x] == x: return x else: parent[x] = find(parent[x]) return find(parent[x]) def unite(x,y): parent[find(x)] = find(y) ans = 0 for q in range(k): x, y = map(int,input().split()) if find(x-1) == find(y-1): ans += 1 else: unite(x-1,y-1) print(ans) if __name__ == '__main__': main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int m[4][4]; vector<int> a; void swap(int& x, int& y) { int t = x; x = y; y = t; } int abs(int x) { return x < 0 ? -x : x; } int func(int n, int s) { int sum, ret = 0; for (int i = 0; i < n; i++) { sum = 0; for (int j = 0; j < n; j++) sum += m[i][j]; ret += abs(sum - s); } for (int j = 0; j < n; j++) { sum = 0; for (int i = 0; i < n; i++) sum += m[i][j]; ret += abs(sum - s); } sum = 0; for (int i = 0; i < n; i++) sum += m[i][i]; ret += abs(sum - s); sum = 0; for (int i = 0; i < n; i++) sum += m[i][n - i - 1]; ret += abs(sum - s); return ret; } int main() { bool updated; int cur_value, new_value, n, s, sum = 0; cin >> n; a.resize(n * n); for (int i = 0; i < n * n; i++) { cin >> a[i]; sum += a[i]; } s = sum / n; for (;;) { random_shuffle(a.begin(), a.end()); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) m[i][j] = a[i * n + j]; for (;;) { updated = false; cur_value = func(n, s); for (int i = 0; i < n * n; i++) for (int j = i; j < n * n; j++) { int r1 = i / n; int c1 = i % n; int r2 = j / n; int c2 = j % n; if (m[r1][c1] == m[r2][c2]) continue; swap(m[r1][c1], m[r2][c2]); new_value = func(n, s); if (new_value < cur_value) { i = j = n * n; updated = true; break; } swap(m[r1][c1], m[r2][c2]); } if (!updated) break; } cur_value = func(n, s); if (cur_value == 0) break; } cout << s << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << m[i][j] << " "; cout << endl; } return 0; }
10
CPP
int(input()) citizens = [int(x) for x in input().split()] mx = max(citizens) n = 0 for x in citizens: n += (mx - x) print(n)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n, m, x1, y1, x2, y2; scanf("%d %d %d %d %d %d", &n, &m, &x1, &y1, &x2, &y2); long long x = ((x1 - x2) < 0 ? (-(x1 - x2)) : (x1 - x2)); long long y = ((y1 - y2) < 0 ? (-(y1 - y2)) : (y1 - y2)); long long a = n - 1 - x, b = m - 1 - y; long long result = n * (long long)m - (a + 1) * (b + 1) * 2 + ((a - x + 1) > (0) ? (a - x + 1) : (0)) * ((b - y + 1) > (0) ? (b - y + 1) : (0)); cout << result << endl; } return 0; }
8
CPP
#w= int(input()) #if 101>w>2: # if w%2 == 0: # print("YES") #else: # print("NO") n=int(input()) if n==2: print('NO') elif n%2==0: print('YES') else: print('NO')
7
PYTHON3
import sys rl = lambda: sys.stdin.readline() victims = rl().strip().split(' ') def print_list(): print('{} {}'.format(victims[0], victims[1])) print_list() num_line = int(rl()) for _ in range(num_line): victim, subst = rl().strip().split(' ') if victims[0] == victim: victims[0] = subst else: victims[1] = subst print_list()
7
PYTHON3
n=int(input()) a=[] for i in range(n): a.append(input()) for i in range(n): if(len(a[i])>10): print(a[i][0]+str(len(a[i])-2)+a[i][len(a[i])-1]) else: print(a[i])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, d; vector<int> arr; int bs(int x) { int lo = 0; int hi = n - 1; int ans = -1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= x) { ans = max(ans, mid); lo = mid + 1; } else { hi = mid - 1; } } return ans; } int main() { cin >> n >> d; arr.resize(n); for (auto &i : arr) cin >> i; long long ans = 0; for (int x = 0; x < n; x++) { int idx = bs(arr[x] + d); if (idx - x < 2) continue; long long els = idx - x - 1; ans += ((els * (els + 1)) / 2); } cout << ans << endl; }
7
CPP
n, m = map(int, input().split()) a = [None]*m c = [0]*m for i in range(m): a[i], b = map(int, input().split()) ls = list(map(int,input().split())) for j in ls: c[i] |= 1<<(j-1) n2 = 1<<n dp = [1e9]*(n2) dp[0] = 0 for i in range(m): for S in range(n2): if dp[S|c[i]] > dp[S]+a[i]: dp[S|c[i]] = dp[S]+a[i] if dp[(n2)-1] < 1e9: print(dp[(n2)-1]) else: print(-1)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int x, y, z, t, n; const int MAXN = 1000005; int b[600000]; int show[2]; char c[200005]; int main() { cin >> t; while (t--) { scanf("%d", &n); scanf("%s", c + 1); show[1] = show[2] = 0; queue<int> a0; queue<int> a1; int id = 0; for (int i = 1; i <= n; i++) { x = c[i] - '0'; if (x == 1) { if (!a1.empty()) { int top = a1.front(); a1.pop(); b[i] = top; a0.push(top); } else { b[i] = ++id; a0.push(id); } } if (x == 0) { if (!a0.empty()) { int top = a0.front(); a0.pop(); b[i] = top; a1.push(top); } else { b[i] = ++id; a1.push(id); } } } printf("%d\n", id); for (int i = 1; i <= n; i++) { printf("%d ", b[i]); } cout << endl; } return 0; }
10
CPP
#include<bits/stdc++.h> using namespace std; long long f[45]={2};int x,i; int main(){ scanf("%d",&x); for(i=1;i<=x;i++) f[i]=f[i-1]*2+2; printf("%lld",f[x]); return 0; }
0
CPP