solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
def sumdig(x): s = 0 if x > 0: for i in str(x): s += int(i) return s else: for i in str(x)[1:]: s += int(i) return s*(-1) n = int(input()) r = 0 i = n - 9 * (len(str(n))-1) - 10 #print(i) k = 0 ka = [] while i <= n: r = i + sumdig(i) if r == n: k += 1 ka.append(i) i += 1 if k == 0: if len(str(n)) == 1: if n % 2 == 0: print(1) print(n // 2) else: print(k) else: print(k) else: print(k) for i in ka: print(i)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f; const int maxn = 350; vector<vector<int>> delta(maxn, vector<int>(maxn, inf)); vector<vector<int>> pred(maxn, vector<int>(maxn, -1)); vector<int> ans; vector<vector<int>> tracks(maxn, vector<int>(maxn, 0)); void PRINT(vector<vector<int>> d, int n) { cout << "PRINTING:\n\n"; for (int i = 1; i < n; i++) { for (int j = 1; j < n; j++) { printf("%3d ", d[i][j]); } cout << endl; } } bool pos = true; void PATH(int u, int v) { if (pred[u][v] == -1) { tracks[u][v]++; ans.push_back(u); if (tracks[u][v] > 1) { pos = false; } } else { PATH(u, pred[u][v]); PATH(pred[u][v], v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; delta[u][v] = 1; delta[v][u] = 1; } int current = 1; int destination; for (int k = 1; k <= n; k++) { for (int u = 1; u <= n; u++) { for (int v = 1; v <= n; v++) { if (delta[u][k] + delta[k][v] < delta[u][v]) { pred[u][v] = k; } delta[u][v] = min(delta[u][v], delta[u][k] + delta[k][v]); } } } while (cin >> destination) { PATH(current, destination); current = destination; if (!pos) break; } PATH(current, 1); if (pos) { for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << "1"; } else { cout << -1; } }
10
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> inline T LCM(T a, T b) { if (a < 0) return LCM(-a, b); if (b < 0) return LCM(a, -b); return a * (b / gcd(a, b)); } int main() { vector<int> ans; int n; cin >> n; int from = 5; int c = 0; for (int j = 1; j <= 100000; j++) { int z = 5 * j; while (z % 5 == 0) { c++; z /= 5; } if (c == n) { for (int k = 5 * j; k < 5 * (j + 1); k++) { ans.push_back(k); } } } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { printf("%d ", ans[i]); } return 0; }
8
CPP
#include<iostream> #include<algorithm> #include<vector> #include<queue> #define lol(i,n) for(int i=0;i<n;i++) #define mod 1000000007 typedef long long ll; using namespace std; int main(){ ll sa=0,sb=0,a,b,n,m; cin>>n>>m; lol(i,n){ cin>>a; sa+=a; } lol(i,m){ cin>>b; sb+=b; } cout<<sa*sb<<endl; return 0; }
0
CPP
#include<bits/stdc++.h> using namespace std; int a; bool y,m; int main() { cin>>a; if(a%100<=12&&a%100>=1) y=1; if(a/100<=12&&a/100>=1) m=1; if(y==1&&m==1) { cout<<"AMBIGUOUS"; return 0; } if(y==1) cout<<"YYMM"; else if(m==1) cout<<"MMYY"; else cout<<"NA"; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000009; long long md(long long n) { if (n >= mod) return n % mod; return n; } long long power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, a, b, k, inv; cin >> n >> a >> b >> k; string s; cin >> s; inv = power(a, mod - 2); long long term = power(a, n), sum = 0; for (long long i = 0; s[i]; i++) { if (s[i] == '+') sum = md(sum + term); else sum = md(sum - term + mod); term = md(md(term * inv) * b); } long long ans; long long A = sum, N = (n + 1) / k, R = power(md(b * inv), k); if (R == 1) { ans = sum * (n + 1) / k; ans %= mod; } else { long long numerator = md(A * md((power(R, N) - 1 + mod))); long long den = power(R - 1, mod - 2); numerator = md(numerator * den); ans = numerator; } cout << ans; }
7
CPP
n = int(input()) s = str(input()) lst = [] for i in s: if( len(lst) != 0 and i == lst[-1] or len(lst) == 0): lst.append(i) elif(i != lst[-1]): lst.pop() print(len(lst))
7
PYTHON3
def zadacha(a, b): i = 0 while a<=b: a *= 3 b *= 2 i += 1 return i a,b = map(int, input().split()) print(zadacha(a, b))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; char s[200022]; int main() { int n; scanf("%d%s", &n, s + 1); int ans = -1; for (int i = 1; i <= n - 1; i++) { if (s[i] > s[i + 1]) { ans = i; break; } } if (ans == -1) ans = n; for (int i = 1; i <= n; i++) { if (i != ans) printf("%c", s[i]); } return 0; }
7
CPP
n = int(input()) a = list(map(int,input().split())) day = 0 while n > 0: if a[day] >= n: print(day + 1) exit(0) n -= a[day] day = (day + 1) % 7
7
PYTHON3
for i in range(int(input())): c, su = map(int, input().split()) print((su // c) ** 2 * (c - su % c) + (su // c + 1) ** 2 * (su % c))
7
PYTHON3
n = int(input()) loc = list(input()) if loc[0] == 'S' and loc[n-1] == 'F': print("YES") else: print("NO")
7
PYTHON3
t=int(input()) l=[0,1] for i in range(2,1000): l.append(i*i) while t: t-=1 n=int(input()) l=list(map(int,input().split())) pos=0 if n==1: print(1) continue maxi=-19900000 for i in range(0,n): if l[i]>maxi: if i==0: if l[i]!=l[i+1]: pos=i maxi=l[i] elif i==n-1: if l[i]!=l[i-1]: pos=i maxi=l[i] else: if l[i]!=l[i-1] or l[i]!=l[i+1]: pos=i maxi=l[i] if len(set(l))==1: print(-1) continue else: print(pos+1)
9
PYTHON3
#include<bits/stdc++.h> #define ll long long int using namespace std; int main(void) { int n; cin >> n; string res=""; if(n==0){ cout << "0" << endl; } while(n!=0){ res = to_string(abs(n)%2) + res; n = (n - abs(n)%2) / -2; } cout << res << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000000 + 7; inline int add(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } inline int sub(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } inline int mul(int a, int b) { return (int)((long long)a * b % MOD); } inline int binpow(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = mul(res, a); a = mul(a, a); b /= 2; } return res; } inline int inv(int a) { return binpow(a, MOD - 2); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, k; cin >> n >> k; vector<int> id(n, 0); queue<int> q; set<int> st; for (int i = 0; i < n; i++) { cin >> id[i]; } for (int i = 0; i < n; i++) { if (st.find(id[i]) != st.end()) continue; else if ((int)(q.size()) < min(n, k)) q.push(id[i]); else { st.erase(q.front()); q.pop(); q.push(id[i]); } st.insert(id[i]); } vector<int> res; while (!q.empty()) { res.push_back(q.front()); q.pop(); } reverse(res.begin(), res.end()); int size1 = (int)(res.size()); cout << size1 << "\n"; for (int i = 0; i < size1; i++) cout << res[i] << " "; cout << "\n"; return 0; }
8
CPP
q=list(map(int,input().split())) w=[] if q[1]*q[2]>q[3]: w.append((q[0]%q[1])*q[2]+(q[0]//q[1])*q[3]) w.append((q[0]//q[1]+1)*q[3]) print(min(w)) else: print(q[0]*q[2])
7
PYTHON3
X = input() print(X.upper())
0
PYTHON3
for _ in range(int(input())): a, b, n, m = map(int, input().strip().split()) # n = int(input()) if n+m <= a+b and m <= min(a,b): print("Yes") else: print('No')
9
PYTHON3
def main(): n = int(input()) ans = list() for _ in range(n): k = int(input()) sim_k = (1+ len(str(k)))*len(str(k))//2 sim_before = 10 * (k % 10 - 1) ans.append(sim_k + sim_before) for item in ans: print(item) if __name__ == '__main__': main()
7
PYTHON3
from collections import defaultdict as dd from collections import deque import bisect import heapq def ri(): return int(input()) def rl(): return list(map(int, input().split())) def solve(): n = ri() fail = False p, c = rl() if c > p: fail = True for i in range(n - 1): np, nc = rl() if np < p or nc < c: fail = True if nc > np: fail = True if nc - c > np - p: fail = True p, c = np, nc if fail: print ("NO") else: print ("YES") mode = 'T' if mode == 'T': t = ri() for i in range(t): solve() else: solve()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n1, n2, n3; cin >> n1 >> n2 >> n3; vector<long long> v1(n1); vector<long long> v2(n2); vector<long long> v3(n3); for (int i = 0; i < n1; i++) { cin >> v1[i]; } for (int i = 0; i < n2; i++) { cin >> v2[i]; } for (int i = 0; i < n3; i++) { cin >> v3[i]; } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); sort(v3.begin(), v3.end()); long long ans = 5e18 + 7; for (int i = 0; i < n1; i++) { int l = -1, r = n2; while (r - l > 1) { int mid = (r + l) / 2; if (v2[mid] <= v1[i]) l = mid; else r = mid; } long long min1 = (l == -1 ? -1 : v2[l]); l = -1; r = n2; while (r - l > 1) { int mid = (r + l) / 2; if (v2[mid] >= v1[i]) r = mid; else l = mid; } long long max1 = (r == n2 ? -1 : v2[r]); l = -1, r = n3; while (r - l > 1) { int mid = (r + l) / 2; if (v3[mid] <= v1[i]) l = mid; else r = mid; } long long min2 = (l == -1 ? -1 : v3[l]); l = -1; r = n3; while (r - l > 1) { int mid = (r + l) / 2; if (v3[mid] >= v1[i]) r = mid; else l = mid; } long long max2 = (r == n3 ? -1 : v3[r]); if (min1 > -1 && min2 > -1) { ans = min(ans, (v1[i] - min1) * (v1[i] - min1) + (v1[i] - min2) * (v1[i] - min2) + (min1 - min2) * (min1 - min2)); } if (max1 > -1 && max2 > -1) { ans = min(ans, (v1[i] - max1) * (v1[i] - max1) + (v1[i] - max2) * (v1[i] - max2) + (max1 - max2) * (max1 - max2)); } if (min1 > -1 && max2 > -1) { ans = min(ans, (v1[i] - min1) * (v1[i] - min1) + (v1[i] - max2) * (v1[i] - max2) + (min1 - max2) * (min1 - max2)); } if (max1 > -1 && min2 > -1) { ans = min(ans, (v1[i] - max1) * (v1[i] - max1) + (v1[i] - min2) * (v1[i] - min2) + (max1 - min2) * (max1 - min2)); } } for (int i = 0; i < n2; i++) { int l = -1, r = n1; while (r - l > 1) { int mid = (r + l) / 2; if (v1[mid] <= v2[i]) l = mid; else r = mid; } long long min1 = (l == -1 ? -1 : v1[l]); l = -1; r = n1; while (r - l > 1) { int mid = (r + l) / 2; if (v1[mid] >= v2[i]) r = mid; else l = mid; } long long max1 = (r == n1 ? -1 : v1[r]); l = -1, r = n3; while (r - l > 1) { int mid = (r + l) / 2; if (v3[mid] <= v2[i]) l = mid; else r = mid; } long long min2 = (l == -1 ? -1 : v3[l]); l = -1; r = n3; while (r - l > 1) { int mid = (r + l) / 2; if (v3[mid] >= v2[i]) r = mid; else l = mid; } long long max2 = (r == n3 ? -1 : v3[r]); if (min1 > -1 && min2 > -1) { ans = min(ans, (v2[i] - min1) * (v2[i] - min1) + (v2[i] - min2) * (v2[i] - min2) + (min1 - min2) * (min1 - min2)); } if (max1 > -1 && max2 > -1) { ans = min(ans, (v2[i] - max1) * (v2[i] - max1) + (v2[i] - max2) * (v2[i] - max2) + (max1 - max2) * (max1 - max2)); } if (min1 > -1 && max2 > -1) { ans = min(ans, (v2[i] - min1) * (v2[i] - min1) + (v2[i] - max2) * (v2[i] - max2) + (min1 - max2) * (min1 - max2)); } if (max1 > -1 && min2 > -1) { ans = min(ans, (v2[i] - max1) * (v2[i] - max1) + (v2[i] - min2) * (v2[i] - min2) + (max1 - min2) * (max1 - min2)); } } for (int i = 0; i < n3; i++) { int l = -1, r = n2; while (r - l > 1) { int mid = (r + l) / 2; if (v2[mid] <= v3[i]) l = mid; else r = mid; } long long min1 = (l == -1 ? -1 : v2[l]); l = -1; r = n2; while (r - l > 1) { int mid = (r + l) / 2; if (v2[mid] >= v3[i]) r = mid; else l = mid; } long long max1 = (r == n2 ? -1 : v2[r]); l = -1, r = n1; while (r - l > 1) { int mid = (r + l) / 2; if (v1[mid] <= v3[i]) l = mid; else r = mid; } long long min2 = (l == -1 ? -1 : v1[l]); l = -1; r = n1; while (r - l > 1) { int mid = (r + l) / 2; if (v1[mid] >= v3[i]) r = mid; else l = mid; } long long max2 = (r == n1 ? -1 : v1[r]); if (min1 > -1 && min2 > -1) { ans = min(ans, (v3[i] - min1) * (v3[i] - min1) + (v3[i] - min2) * (v3[i] - min2) + (min1 - min2) * (min1 - min2)); } if (max1 > -1 && max2 > -1) { ans = min(ans, (v3[i] - max1) * (v3[i] - max1) + (v3[i] - max2) * (v3[i] - max2) + (max1 - max2) * (max1 - max2)); } if (min1 > -1 && max2 > -1) { ans = min(ans, (v3[i] - min1) * (v3[i] - min1) + (v3[i] - max2) * (v3[i] - max2) + (min1 - max2) * (min1 - max2)); } if (max1 > -1 && min2 > -1) { ans = min(ans, (v3[i] - max1) * (v3[i] - max1) + (v3[i] - min2) * (v3[i] - min2) + (max1 - min2) * (max1 - min2)); } } cout << ans << "\n"; } }
10
CPP
#include <bits/stdc++.h> using namespace std; //Warsaw debug template #define LOCAL #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1,debug&>::type operator<<(c i){ sim > struct rge {c b,e;}; sim > rge<c> range(c i, c j) {return rge<c>{i,j};} sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug{ #ifdef LOCAL ~debug() {cerr<<endl; } eni(!=) cerr<< boolalpha << i; ris;} eni(==) ris <<range(begin(i),end(i)); } sim, class b dor(pair<b,c> d){ ris<<"("<<d.first<<","<<d.second<<")"; } sim dor(rge<c> d){ *this << "["; for(auto it=d.b;it!=d.e;++it) *this <<"," +(it==d.b)<< *it; ris<<"]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) "["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]" //End of debug template #define FOR(i,n) for (int i = 0; i < (n); ++i) using ll = long long; //codeforces //^ //odfrces //c o d e f o r c e s //^ find first occur //^ pointer from 0 to any s //^ find occur //^ pointer from 0 to any r //^ from here to sz -1, all chars set to be true //^ find occur //^ pointer from 0 to first o //check from this index til end insert to be true void solve(){ string s; cin >> s; int n = s.size(); vector<bool> exists(26); FOR(i,n) exists[s[i] - 'a'] = true; int idx = 0; while(true){ bool isOK = false; for(int i = 25; i>=0;i--){ if(exists[i]){ int where = idx; while(s[where]-'a' != i){ where++; } vector<bool> suffix(26); for(int j = where;j < n;j++){ if(exists[s[j]-'a']){ suffix[s[j]-'a'] = true; } } if(suffix == exists){ cout << s[where]; idx = where + 1; exists[s[where] - 'a'] = false; isOK = true; break; } } } if(!isOK) break; } cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--){ solve(); } return 0; } //void solve(){ //string s; //cin>>s; //vector<int> h(130),vis(130); //vector<char> a; //for(auto i:s)h[i]++; //for(auto i:s){ //h[i]--; //if(!vis[i]){ //while(!a.empty()){ //if(h[a.back()]==0 or a.back()>i)break; //vis[a.back()]=0; //a.pop_back(); //} //a.push_back(i); //vis[i]=1; //} //} //cout<<string(a.begin(),a.end())<<"\n"; //} //int main() //{ //int t;cin>>t; //while(t--)solve(); //}
13
CPP
#include <bits/stdc++.h> using namespace std; int main() { char word; int i, l, count[27], flag, n; while (cin >> n) { flag = 0; memset(count, 0, sizeof(count)); for (i = 0; i < n; i++) { cin >> word; if (word <= 122 && word >= 97) count[word - 96]++; else count[word - 64]++; } for (i = 1; i <= 26; i++) { if (count[i] == 0) { flag = 1; break; } } if (flag) cout << "NO\n"; else cout << "YES\n"; } return 0; }
7
CPP
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} void add(ll &a,ll b){ a=(a+b)%MOD; } void mul(ll &a,ll b){ a%=MOD;b%=MOD; a=a*b%MOD; } ll dp[5005][5005][2][2]; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N,A,B; cin>>N>>A>>B; if(A>B) swap(A,B); dp[0][0][1][0]=1; for(int i=0;i<N;i++) for(int j=0;j<B;j++){ rep(k,2) rep(m,2){ if(dp[i][j][k][m]==0) continue; ll pre=dp[i][j][k][m]; if(k==1&&i+A<=N){ if(j+A<B) add(dp[i+A][j+A][0][1],pre); add(dp[i+A][0][0][0],MOD-pre); } for(int ne=0;ne<2;ne++){ int nj=j,nk=ne,nm=m; if(ne==1||(m==1&&ne==0)) nj=j+1; if(ne==0&&m==0) nj=0; if(ne==1&&m==1) nm=0; add(dp[i+1][nj][nk][nm],pre); } } } ll ans=1; rep(k,N) mul(ans,2); rep(j,B) rep(k,2) rep(m,2) add(ans,MOD-dp[N][j][k][m]); cout<<ans<<endl; return 0; }
0
CPP
#include <iostream> #include <vector> #include <set> #include <map> #include <string> #include <stack> #include <functional> #include <queue> #include <tuple> #include <cstring> using namespace std; #define rep(i,n) for(int i = 0;i < n;i++) #define each(i,n) for(auto i : n) #define clr(n) memset(n,0,sizeof(n)) #define mclr(n) memset(n,-1,sizeof(n)) //2^8 * 100 * 500 * 24 int INF = 1e9; int n, m, H, K; int c[105][105]; int h[105][105]; int r[105][105]; int l[300]; int d[300]; int k[300]; int s, t, p; int me[(1 << 8) + 1][(1 << 8) + 1]; int ct[105][30]; int solve(int i, int bit, int tg) { if (me[i][bit] != -1)return me[i][bit]; if (i == p) { if ((bit & tg) == tg) { return 0; } else { return INF; } } return me[i][bit] = min(solve(i + 1, bit, tg), solve(i + 1, bit | k[i], tg) + d[i]); } vector<vector<int>> ce; int main() { while (cin >> n >> m >> H >> K && n) { mclr(c); mclr(h); mclr(r); mclr(l); mclr(d); clr(k); ce.clear(); ce.resize(n); rep(i, m) { int a, b, e, f, g; cin >> a >> b >> e >> f >> g; a--; b--; g--; ce[a].push_back(b); ce[b].push_back(a); c[a][b] = c[b][a] = e; h[a][b] = h[b][a] = f; r[a][b] = r[b][a] = g; } cin >> s >> t >> p; s--; t--; rep(i, p) { cin >> l[i] >> d[i]; rep(j, l[i]) { int tmp; cin >> tmp; tmp--; k[i] |= (1 << tmp); } } int ans = INF; rep(bit, 1 << K) { mclr(me); int cost = solve(0, 0, bit); if (cost == INF)continue; rep(i, 105)rep(j, 30) { ct[i][j] = INF; } ct[s][0] = cost; typedef tuple<int, int, int> T; priority_queue<T, vector<T>, greater<T>> qu; qu.push(T(cost, s, 0)); while (qu.size()) { int mc = get<0>(qu.top()); int nw = get<1>(qu.top()); int tm = get<2>(qu.top()); qu.pop(); if (ct[nw][tm] < mc)continue; each(i, ce[nw]) { if (c[nw][i] != -1) { int nt = tm + h[nw][i]; if (nt <= H) { int nc = ct[nw][tm]; if (!(bit & (1 << r[nw][i]))) { nc += c[nw][i]; } if (nc < ct[i][nt]) { ct[i][nt] = nc; qu.push(T(nc, i, nt)); } } } } } rep(i, H + 1) { ans = min(ans, ct[t][i]); } } if (ans == INF)ans = -1; cout << ans << endl; } return 0; }
0
CPP
#ifndef _MY #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native") #endif //#define _GLIBCXX_DEBUG #include "bits/stdc++.h" #define F first #define S second #define mp make_pair #define all(x) (x).begin(), (x).end() using namespace std; typedef long long ll; typedef long double ld; const int64_t INF = (int64_t)(2e18); const int inf = (int)(1e9 + 7); const int maxn = 500 * 1000 + 100; clock_t cl; double current_time() { return double(clock() - cl) / CLOCKS_PER_SEC; } //------------------------------------------// int mat[1000][1000]; bool check(int n, vector<int>& lst){ int idx = 0; for (int i = 0; i < n; ++i){ for (int j = 0; j < n; ++j){ mat[i][j] = 0; } } for (int i = 1; i < n && idx < (int)lst.size(); i += 2){ for (int j = 0; j < n && idx < (int)lst.size(); j += 2){ mat[i][j] = lst[idx++]; } } for (int i = 0; i < n && idx < (int)lst.size(); i += 2){ for (int j = 0; j < n && idx < (int)lst.size(); j+=2){ mat[i][j] = lst[idx++]; } } for (int i = 0; i < n && idx < (int)lst.size(); i += 2){ for (int j = 1; j < n && idx < (int)lst.size(); j+=2){ mat[i][j] = lst[idx++]; } } //cout << n << endl; //for (int i = 0; i < n; ++i){ // for (int j = 0; j< n; ++j){ // cout << mat[i][j] << " "; // } // cout << endl; //} if (idx < (int)lst.size()) return false; for (int i = 0; i < n-1; ++i){ for (int j = 0; j < n-1; ++j){ if (mat[i][j] == 0 && mat[i+1][j+1] == 0) continue; if (mat[i+1][j] == 0 && mat[i][j+1] == 0) continue; if (mat[i][j] == mat[i+1][j+1]) return false; if (mat[i+1][j] == mat[i][j+1]) return false; } } return true; } void solve(){ int m, k; cin >> m >> k; map<int,int> ma; for (int i = 1; i <= k; ++i) cin >> ma[i]; vector<pair<int,int>> vp; for (auto& it : ma) vp.push_back(mp(it.S, it.F)); sort(all(vp)); reverse(all(vp)); vector<int> lst; for (auto& it : vp) { for (int i =0 ; i < it.F; ++i) lst.push_back(it.S); } int l = 1; int r = sqrt(3.5*m)+10; while(l < r){ int mid = (l+r)/2; if (check(mid, lst) == false) l = mid+1; else r = mid; } check(r,lst); cout << r << endl; for (int i = 0; i < r; ++i){ for (int j = 0; j < r; ++j){ cout << mat[i][j] << " "; } cout << endl; } } int32_t main(){ cl = clock(); ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); #ifdef _MY freopen("VimProject/input.txt", "r", stdin); freopen("VimProject/output.txt", "w", stdout); #endif int t; cin >> t; while(t--) solve(); return 0; }
11
CPP
n = int(input()) fx = 0 fy = 0 fz = 0 for i in range(n) : x,y,z = input().split() fx += int(x) fy += int(y) fz += int(z) if(abs(fx) + abs(fy) + abs(fz) > 0) : print("NO") else : print("YES")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int ans; int cnt; char a[1000005]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } if (n % 2) { cout << -1; return 0; } for (int i = 1; i <= n; i++) { if (a[i] == '(') { cnt++; } else { cnt--; } if (cnt < 0) { ans++; } if (cnt == 0 && a[i] == '(') { ans++; } } if (cnt != 0) { cout << -1; } else { cout << ans; } return 0; }
9
CPP
(h1, m1), (h2, m2) = map(int, input().split(":")), map(int, input().split(":")) m_1, m_2 = h1 * 60 + m1, h2 * 60 + m2 m = (m_1 + m_2) // 2 h, m = str(m // 60), str(m % 60) print("0" * (2 - len(h)) + h, "0" * (2 - len(m)) + m, sep=':')
7
PYTHON3
lis=input() lik=lis[0].upper()+lis[1:] if lis.isupper():print(lis.lower()) elif lik.isupper():print (lis.capitalize()) else: print (lis)
7
PYTHON3
#include <iostream> #include <algorithm> #include <climits> using namespace std; using ll = long long; int main(){ ll n, x; cin >> n >> x; ll a[n+1], b[n+1]; for(int i=1; i<=n; i++){ cin >> a[i]; b[i] = a[i]; }; ll ans = LLONG_MAX; for(int k=0; k<n; k++){ ll tmp = 0; for(int i=1; i<=n; i++){ int be = (i-k <= 0)? i-k+n: i-k; b[i] = min(b[i], a[be]); tmp += b[i]; } ans = min(ans, tmp + k*x); } cout << ans << endl; return 0; }
0
CPP
import math __author__ = 'Obriel Muga' n,d = list(map(int,input().strip().split())) secuencia = list(map(int,input().strip().split())) resto = 0 contador_op = 0 for i in range(n-1): valor = ((secuencia[i+1] - secuencia[i])) if (valor - resto > 0): resto = 0 continue elif (valor - resto == 0): contador_op += 1 resto = d elif (valor - resto < 0): if (abs(valor - resto))%d == 0: agregar = math.ceil(abs(valor-resto)/d) + 1 else: agregar = math.ceil(abs(valor - resto)/d) contador_op += agregar resto = agregar*d print(contador_op)
7
PYTHON3
def main(): m, n = [int(v) for v in input().split()] vals = [int(v) for v in input().split()] sum = 0 sums = [0] for i, v in enumerate(vals): sum += v sums.append(sum) min_v = sums[n] res = 0 for i in range(m+1): if i+n-1>=m: continue if sums[i + n] - sums[i] < min_v: min_v = sums[i + n] - sums[i] res = i print(res+1) if __name__ == "__main__": main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1004; int d[MAXN]; int n, m; vector<pair<int, int>> v; void Gouzao(int x, int beg_point) { if (x > n) return; int nowd = d[x]; if (x < n) { for (int i = beg_point; i < beg_point + nowd; i++) { for (int j = i + 1; j < beg_point + nowd; j++) { v.push_back(make_pair(i, j)); } } for (int i = beg_point; i < beg_point + nowd; i++) { for (int j = beg_point + nowd; j <= beg_point + d[n]; j++) { v.push_back(make_pair(i, j)); } } n--; for (int i = x; i <= n; i++) d[i] -= nowd; for (int i = x; i <= n; i++) if (d[i] > 0) { Gouzao(i, beg_point + nowd + 1); return; } } else { for (int i = beg_point; i <= beg_point + nowd; i++) { for (int j = i + 1; j <= beg_point + nowd; j++) { v.push_back(make_pair(i, j)); } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { cin >> d[i]; } Gouzao(1, 1); cout << v.size() << '\n'; for (auto i : v) { cout << i.first << ' ' << i.second << '\n'; } return 0; }
10
CPP
#include <bits/stdc++.h> double s1, s2; int T, n, a[255]; int main() { n = 250; scanf("%d", &T); while (T--) { s1 = s2 = 0; double x; for (int i = 0; i < n; ++i) scanf("%lf", &x), s1 += x, s2 += x * x; s1 /= n; s2 /= n; s2 -= s1 * s1; if (s2 / s1 > 3) puts("uniform"); else puts("poisson"); } return 0; }
10
CPP
data = input().split() N, M = int(data[0]), int(data[1]) crossword_t = [] for _ in range(N): crossword_t.append(list(input())) answer = '' for i in range(N): for j in range(M): row, col = crossword_t[i], [r[j] for r in crossword_t] letter_cr = row.count(crossword_t[i][j]) letter_ccl = col.count(crossword_t[i][j]) if ( letter_cr == 1 and letter_ccl == 1 ): answer = answer + crossword_t[i][j] print(answer)
8
PYTHON3
a=input().split() c=int(a[0]) d=int(a[1]) for u in range(0,d): if c%10 == 0: c=c/10 else: c=c-1 print(round(c))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); if (m > n + k) { printf("0.000000"); return 0; } long double res = 1.0; for (int i = m - k; i <= m; ++i) res *= i; for (int i = n + 1; i <= n + k + 1; ++i) res /= i; res = 1 - res; printf("%.4llf", res); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; set<pair<int, int> > all; map<int, int> allx; map<int, int> ally; bool check() { if (all.size() != 8) { return false; } if (allx.size() != 3 || ally.size() != 3) { return false; } vector<pair<int, int> > vx(allx.begin(), allx.end()); vector<pair<int, int> > vy(ally.begin(), ally.end()); if (vx[0].second != 3 || vx[2].second != 3 || vx[1].second != 2 || vy[0].second != 3 || vy[1].second != 2 || vy[2].second != 3) { return false; } if (all.count(pair<int, int>(vx[1].first, vy[1].first))) { return false; } if (!all.count(pair<int, int>(vx[0].first, vy[0].first))) return false; if (!all.count(pair<int, int>(vx[1].first, vy[0].first))) return false; if (!all.count(pair<int, int>(vx[2].first, vy[0].first))) return false; if (!all.count(pair<int, int>(vx[0].first, vy[2].first))) return false; if (!all.count(pair<int, int>(vx[1].first, vy[2].first))) return false; if (!all.count(pair<int, int>(vx[2].first, vy[2].first))) return false; if (!all.count(pair<int, int>(vx[0].first, vy[1].first))) return false; if (!all.count(pair<int, int>(vx[2].first, vy[1].first))) return false; return true; } int main() { for (int i = 0; i < 8; i++) { int x, y; scanf("%d%d", &x, &y); all.insert(pair<int, int>(x, y)); allx[x]++; ally[y]++; } puts(check() ? "respectable" : "ugly"); return 0; }
8
CPP
n,d=map(int,input().split()) a,b=map(int,input().split()) req=[] for i in range(n): x,y=map(int,input().split()) req.append([x,y,i]) ans=[] req.sort(key=lambda x:[x[0]*a+x[1]*b]) for i in range(n): curr= req[i][0]*a+req[i][1]*b if d>=curr: ans.append(req[i][2]+1) d-=curr else: break print(len(ans)) print(*ans)
9
PYTHON3
keyboard = [ ['q','w','e','r','t','y','u','i','o','p'], ['a','s','d','f','g','h','j','k','l',';'], ['z','x','c','v','b','n','m',',','.','/'] ] def get_location(char): for row in range(10): for col in range(10): if keyboard[row][col] == char: return (row,col) shift = input() word = input() new_word = '' if shift == "R" : for char in word: (row,col) = get_location(char) new_word += keyboard[row][col-1] else: for char in word: (row,col) = get_location(char) new_word += keyboard[row][col+1] print (new_word)
7
PYTHON3
n=int(input()) k=n t=n while t!=1: l=[] for i in range(1,t): if t%i==0: l.append(i) k+=max(l) t=max(l) print(k)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n; struct node { int x, y; } a[N]; int cmp(node a, node b) { return a.x < b.x; } double f[55][55][55], ans; int pd(double ans) { memset(f, 127, sizeof f); f[0][0][0] = 0; for (int i = 1; i <= n; i++) { if (a[i].x == a[i - 1].x) { for (int j = 0; j <= i - 1; j++) { int nj = j ? j - 1 : j; for (int k = 0; k <= i - 1; k++) { if (f[i - 1][j][k] + a[i].x - ans * a[i].y < f[i][nj][k]) f[i][nj][k] = f[i - 1][j][k] + a[i].x - ans * a[i].y; if (f[i - 1][j][k] < f[i][j][k + 1]) f[i][j][k + 1] = f[i - 1][j][k]; } } } else { for (int j = 0; j <= i - 1; j++) for (int k = 0; k <= i - 1; k++) f[i - 1][j + k][0] = ((f[i - 1][j + k][0]) < (f[i - 1][j][k]) ? (f[i - 1][j + k][0]) : (f[i - 1][j][k])); for (int j = 0; j <= i - 1; j++) { int nj = j ? j - 1 : j; if (f[i - 1][j][0] + a[i].x - ans * a[i].y < f[i][nj][0]) f[i][nj][0] = f[i - 1][j][0] + a[i].x - ans * a[i].y; if (f[i - 1][j][0] < f[i][j][1]) f[i][j][1] = f[i - 1][j][0]; } } } return f[n][0][0] < 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i].x); for (int i = 1; i <= n; i++) scanf("%d", &a[i].y); sort(a + 1, a + n + 1, cmp); for (double l = 0, r = 1e8 + 1; r - l > 1e-5;) { double m = (l + r) / 2; if (pd(m)) ans = m, r = m; else l = m; } long long as = ceil((ans - 1e-5) * 1000); printf("%I64d\n", as); }
12
CPP
#include <bits/stdc++.h> const int N = 100010; const int inf = 0x3f3f3f3f; using namespace std; int n, m, a, b, c; char s[110][110]; void dfs(int x, int y, int a, int b, int c) { if (x == n + 1 && y == m + 1) return; if (y == m + 1) return dfs(x + 2, 1, a, b, c); if (a >= 2) { s[x][y] = s[x][y + 1] = 'a' + (x + y) % 26; s[x + 1][y] = s[x + 1][y + 1] = 'a' + (x + y + 1) % 26; dfs(x, y + 2, a - 2, b, c); return; } if (b >= 2) { s[x][y] = s[x + 1][y] = 'a' + (x + y) % 26; s[x][y + 1] = s[x + 1][y + 1] = 'a' + (x + y + 1) % 26; dfs(x, y + 2, a, b - 2, c); return; } if (c >= 1) { s[x][y] = s[x + 1][y] = s[x][y + 1] = s[x + 1][y + 1] = 'a' + (x + y) % 26; dfs(x, y + 2, a, b, c - 1); } return; } int main() { scanf("%d%d", &n, &m); scanf("%d%d%d", &a, &b, &c); int tn = n, tm = m; if (m * n % 2 || n * m > 2 * a + 2 * b + 4 * c) return puts("IMPOSSIBLE"), 0; if (m % 2) { if (b * 2 < n || (a / 2 * 4 + 4 * c + (b - n / 2) / 2 * 4 < (m - 1) * n)) return puts("IMPOSSIBLE"), 0; for (int i = 1; i <= n; i += 2) s[i][m] = s[i + 1][m] = 'a' + (i + m) % 26; m--; dfs(1, 1, a, b - n / 2, c); } else if (n % 2) { if (a * 2 < m || ((a - m / 2) / 2 * 4 + b / 2 * 4 + c * 4 < m * (n - 1))) return puts("IMPOSSIBLE"), 0; for (int i = 1; i <= m; i += 2) s[n][i] = s[n][i + 1] = 'a' + (n + i) % 26; n--; dfs(1, 1, a - m / 2, b, c); } else { if (a / 2 * 4 + b / 2 * 4 + c * 4 < m * n) return puts("IMPOSSIBLE"), 0; dfs(1, 1, a, b, c); } for (int i = 1; i <= tn; i++, cout << endl) for (int j = 1; j <= tm; j++) cout << s[i][j]; return 0; }
9
CPP
n, k = map(int, input().strip().split()) s = input().strip() max_len = 0 def fun(letter): global max_len curr_len = swaps = l = 0 r = -1 while r < n-1: r += 1 curr_len += 1 if s[r] == letter: swaps += 1 while swaps > k: if s[l] == letter: swaps -= 1 curr_len -= 1 l += 1 max_len = max(max_len, curr_len) fun('a') fun('b') print(max_len)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> mas(n, 0); for (int i = 0; i < n; ++i) { cin >> mas[i]; } bool f = false; for (int i = 1; i < n; ++i) { if (mas[i] - mas[i - 1] > 1) { f = true; break; } } if (f) cout << "No"; else cout << "Yes"; cout << '\n'; } }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long t; cin >> t; string a, b, c = "", per = "abc"; cin >> a; cin >> b; vector<string> res; do { string cur; for (long long j = 0; j < t; j++) cur += per; res.push_back(cur); res.push_back(string(t, per[0]) + string(t, per[1]) + string(t, per[2])); } while (next_permutation(per.begin(), per.end())); for (auto str : res) { if (str.find(a) == string::npos && str.find(b) == string::npos) { cout << "YES"; cout << endl; cout << str; cout << endl; return 0; } } cout << "NO"; cout << endl; return 0; }
11
CPP
''' Created on Aug 14, 2018 @author: dschn ''' import os import sys x = int(input()) m = 0 c = 0 for i in range(0,x): results = input().split(" ") if results[0]>results[1]: m=m+1 elif results[0]<results[1]: c=c+1 if m>c: print("Mishka") elif c>m: print("Chris") else: print("Friendship is magic!^^")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n; char s[N], t[N]; long long ans, a[N]; void dfs(int x, int w) { if (s[x + 1] + w < '0' || s[x + 1] + w > '9') dfs(x + 1, -w); printf("%d %d\n", x, w); s[x] += w; s[x + 1] += w; if (!--ans) exit(0); } int main() { scanf("%d%s%s", &n, s + 1, t + 1); for (int i = 1; i <= n; ++i) { a[i] = -a[i - 1] + t[i] - s[i]; ans += abs(a[i]); } if (a[n]) return puts("-1"), 0; printf("%lld\n", ans); if (!ans) return 0; ans = min(ans, (long long)1e5); for (int i = 1; i <= n - 1; ++i) { while (s[i] != t[i]) dfs(i, t[i] > s[i] ? 1 : -1); } }
8
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 1, sum = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { sum = sum * 10 + c - '0'; c = getchar(); } return sum * f; } const int MAXN = 2010; pair<int, int> a[MAXN]; int f[MAXN][3][9][750], n; bool vis[MAXN][3][9][750]; vector<int> s[750]; map<vector<int>, int> mp; int cmp(const int a, const int b) { return a > b; } int dfs(int i, int opt, int flo, int S) { if (i == n + 1 && S == 714) return 0; if (flo == 9 || flo == -1) return 0x3f3f3f3f; if (vis[i][opt][flo][S]) return f[i][opt][flo][S]; vis[i][opt][flo][S] = 1; int &now = f[i][opt][flo][S]; vector<int> tmp = s[S]; int num = 0; for (int pos = 0; pos < tmp.size(); pos++) if (tmp[pos] == flo + 1) tmp[pos] = 0, num++; if (num) { sort(tmp.begin(), tmp.end(), cmp); return now = dfs(i, 0, flo, mp[tmp]) + num; } if (i <= n && a[i].first == flo + 1 && !tmp[3]) { tmp[3] = a[i].second; sort(tmp.begin(), tmp.end(), cmp); return now = dfs(i + 1, 0, flo, mp[tmp]) + 1; } if (!opt) return now = min(dfs(i, 1, flo + 1, S), dfs(i, 2, flo - 1, S)) + 1; else if (opt == 1) return now = dfs(i, 1, flo + 1, S) + 1; else if (opt == 2) return now = dfs(i, 2, flo - 1, S) + 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second); int cnt = -1; for (int i = 9; i >= 0; i--) for (int j = i; j >= 0; j--) for (int k = j; k >= 0; k--) for (int p = k; p >= 0; p--) { cnt++; s[cnt].push_back(i); s[cnt].push_back(j); s[cnt].push_back(k); s[cnt].push_back(p); mp[s[cnt]] = cnt; } int ans = dfs(1, 1, 0, 714); cout << ans; return 0; }
9
CPP
k = int(input()) t = input() d = {} for i in t: if i in d: d[i] = d[i] + 1 else: d[i] = 1 str_ = "" for i in d: if d[i]%k: print(-1) exit() d[i] = d[i]//k str_ = str_ + i*((d[i])) print(str_*k)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int num[505][505]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int x; scanf("%d", &x); num[i][j] ^= x; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int x; scanf("%d", &x); num[i][j] ^= x; } for (int i = 1; i < n; i++) for (int j = 1; j < m; j++) if (num[i][j]) { num[i][j] ^= 1; num[i][j + 1] ^= 1; num[i + 1][j] ^= 1; num[i + 1][j + 1] ^= 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (num[i][j]) { puts("No"); return 0; } puts("Yes"); return 0; }
9
CPP
n = int(input()) d = [] z = 0 for i in range(n): d1, d2 = map(int, input().split()) z = z+1 if d1==d2 else 0 d.append(z) ans = 'Yes' if max(d)>=3 else 'No' print(ans)
0
PYTHON3
from copy import copy def arr_float_inp(): return [float(s) for s in input().split()] def arr_int_inp(): return [int(s) for s in input().split()] def int_inp(): return int(input()) def float_inp(): return float(input()) def comp(a): return a[0] def gcd(a, b): if b != 0: return gcd(b, a % b) else: return a if __name__ == '__main__': r, c = arr_int_inp() if r == 1 and c == 1: print(0) else: arr = [[-1 for _ in range(c)] for _ in range(r)] if r == 1 or c == 1: for i in range(r): for j in range(c): arr[i][j] = i + j + 2 else: for i in range(r): for j in range(c): arr[i][j] = (j + 1) * (r + c - i) for row in arr: for el in row: print(el, end=' ') print()
9
PYTHON3
#include<stdio.h> int main(void) { int a, b; scanf("%d %d", &a, &b); if((a+1)/2 >= b) printf("YES\n"); else printf("NO\n"); return 0; }
0
CPP
n, m = map(int, input().split()) b = {} s = {} for _ in range(n): u, v, w = input().split() t = None if u == "B": t = b else: t = s t[int(v)] = t.get(int(v), 0) + int(w) s_k = list(sorted(s.keys())) for i in range(min(m, len(s_k))-1, -1, -1): print("S " + str(s_k[i]) + " " + str(s[s_k[i]])) b_k = list(reversed(sorted(b.keys()))) for i in range(min(m, len(b_k))): print("B " + str(b_k[i]) + " " + str(b[b_k[i]]))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; short steps(short x, short y) { short i = 0; while (x != y) { i++; x++; if (x > 12) x = 1; else if (i > 4) break; } return i; } int main() { short tones[3]; for (short q = 0; q < 3; q++) { string x; cin >> x; if (x == "C") tones[q] = 1; else if (x == "C#") tones[q] = 2; else if (x == "D") tones[q] = 3; else if (x == "D#") tones[q] = 4; else if (x == "E") tones[q] = 5; else if (x == "F") tones[q] = 6; else if (x == "F#") tones[q] = 7; else if (x == "G") tones[q] = 8; else if (x == "G#") tones[q] = 9; else if (x == "A") tones[q] = 10; else if (x == "B") tones[q] = 11; else if (x == "H") tones[q] = 12; } bool found = false; sort(tones, tones + 3); do { if (steps(tones[0], tones[1]) == 3 && steps(tones[1], tones[2]) == 4) { cout << "minor\n"; return 0; } else if (steps(tones[0], tones[1]) == 4 && steps(tones[1], tones[2]) == 3) { cout << "major\n"; return 0; } } while (next_permutation(tones, tones + 3)); cout << "strange" << endl; return 0; }
7
CPP
from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop,heapify import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline from itertools import accumulate M = mod = 998244353 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip('\n').split()] def st():return input().rstrip('\n') def val():return int(input().rstrip('\n')) def li2():return [i for i in input().rstrip('\n')] def li3():return [int(i) for i in input().rstrip('\n')] n = val() s = st() pref0 = [0] pref1 = [0] for i in s: if i == '1': pref1.append(pref1[-1] + 1) pref0.append(pref0[-1]) elif i == '0': pref0.append(pref0[-1] + 1) pref1.append(pref1[-1]) else: pref1.append(pref1[-1] + 1) pref0.append(pref0[-1] + 1) next0 = [-1] next1 = [-1] m1 = m2 = -1 for i in range(n): if s[i] == '1':m2 = i if s[i] == '0':m1 = i next0.append(m1) next1.append(m2) flag = 0 last = float('inf') for x in range(1, n + 1): if flag: ans = 0 else: ans = start = 0 while start + x <= n: # print(start) if pref0[start + x] - pref0[start] == x: start += x ans += 1 elif pref1[start + x] - pref1[start] == x: start += x ans += 1 else: start = next0[start + x] if next1[start + x] > next0[start + x] else next1[start + x] start += 1 if not ans:flag = 1 print(ans,end = ' ')
12
PYTHON3
for _ in range(int(input())): n = int(input()) a = list(input().split('A')) a.pop(0) if n != 1 and len(a)>0: print(len(max(a))) else: print("0")
7
PYTHON3
#include<algorithm> #include<cstdio> using namespace std; const int mod=1e9+7; int n,m,N,sl,fh,ans,a[20],fac[1<<16],ifc[1<<16],num[1<<16],dp[1<<16]; int rd() { sl=0;fh=1; char ch=getchar(); while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();} while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar(); return sl*fh; } int _pow(int k,int i) { int t=1; for(;i;i>>=1,k=1ll*k*k%mod) if(i&1) t=1ll*t*k%mod; return t; } int C(int x,int y) {return 1ll*fac[x]*ifc[y]%mod*ifc[x-y]%mod;} void upd(int &x,int y) {x+=y; if(x>=mod) x-=mod;} int main() { n=rd();m=rd();N=1<<n;fac[0]=1; for(int i=m;i;--i) a[i]=rd(); for(int i=1;i<N;++i) fac[i]=1ll*i*fac[i-1]%mod,num[i]=num[i>>1]+(i&1); ifc[N-1]=_pow(fac[N-1],mod-2); for(int i=N-1;i;--i) ifc[i-1]=1ll*i*ifc[i]%mod; if(!m) {printf("%lld\n",1ll*N*fac[N-1]%mod); return 0;} dp[0]=1; for(int i=1;i<=m;++i) for(int j=N-1;~j;--j) if(dp[j]) for(int k=1;k<=n;++k) if(!((j>>k-1)&1)&&N-a[i]-j>=(1<<k-1)-1) dp[j|(1<<k-1)]=(dp[j|(1<<k-1)]+1ll*dp[j]*C(N-a[i]-j,(1<<k-1)-1)%mod*fac[1<<k-1])%mod; for(int i=0;i<N;++i) dp[i]=1ll*dp[i]*fac[N-i-1]%mod; for(int i=0;i<N;++i) if(num[i]&1) upd(ans,mod-dp[i]); else upd(ans,dp[i]); printf("%lld\n",1ll*N*ans%mod); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a, b, t, n, cur, mn, ans; int tmp[N], dp[N]; string s; void read_input() { cur = 0; cin >> a >> b; cin >> s; n = s.size(); tmp[0] = 1; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1]) { tmp[cur]++; } else { cur++; tmp[cur]++; } } } void solve() { int j; if (s[0] == '1') { j = 2; dp[0] = a; } else { j = 3; dp[1] = a; } for (int i = j; i <= cur; i += 2) { dp[i] = dp[i - 2] + a; dp[i] = min(dp[i], dp[i - 2] + (tmp[i - 1] * b)); } ans = dp[cur]; if (cur > 0) { ans = max(ans, dp[cur - 1]); } cout << ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { memset(dp, 0, sizeof dp); memset(tmp, 0, sizeof tmp); read_input(); solve(); } return 0; }
8
CPP
#!/bin/python3 n = int(input()) home = [] away = [] for i in range(n): a = input().split() home.append(int(a[0])) away.append(int(a[1])) c = 0 for a in away: c += home.count(a) print(c)
7
PYTHON3
x=input();print(["NO","YES"][7*"1" in x or 7*"0" in x])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; long long X1 = 277, X2 = 227; long long M1 = 1777777777, M2 = 1444444447; long long h1[N], h2[N]; long long p1[N], p2[N]; long long gh(int i, int j) { long long g1 = h1[j] - (i ? h1[i - 1] * p1[j - i + 1] : 0); g1 = (g1 % M1 + M1) % M1; long long g2 = h2[j] - (i ? h2[i - 1] * p2[j - i + 1] : 0); g2 = (g2 % M2 + M2) % M2; return g1 << 32 | g2; } void zf(char *s, int n, int *z) { for (int i = 1, j = z[0] = 0; i < n; ++i) { z[i] = max(0, min(z[i - j], j + z[j] - i)); while (i + z[i] < n && s[z[i]] == s[z[i] + i]) ++z[i]; if (j + z[j] < i + z[i]) j = i; } z[n] = 0; } int z1[N], z2[N]; char s1[N], s2[N]; int inf = 1e9; int dup(char *s, int n) { int ans = inf; if (n == 2 && s[0] == s[1]) ans = 1; if (n < 3) return ans; int n1 = n >> 1; int n2 = n - n1; char *a = s, *b = s + n1; ans = min(ans, dup(a, n1)); ans = min(ans, dup(b, n2)); strncpy(s1, b, n2); s1[n2] = '$'; strncpy(s1 + n2 + 1, a, n1); s1[n + 1] = 0; zf(s1, n + 1, z1); reverse(s1, s1 + n + 1); zf(s1, n + 1, z2); for (int i = 0; i <= n + 1; ++i) if (i < n1) { int l = z2[n1 - i]; int r = z1[i + 1 + n2]; if (l + r >= n1 - i) ans = min(ans, n1 - i); } else if (i > n1 + 1) { int l = z1[i - n1]; int r = z2[n2 - (i - n1 - 1) - 1 + n1 + 1]; if (l + r >= i - n1) ans = min(ans, i - n1); } return ans; } char s[N], t[N]; int main() { for (int i = p1[0] = 1; i < N; ++i) p1[i] = p1[i - 1] * X1 % M1; for (int i = p2[0] = 1; i < N; ++i) p2[i] = p2[i - 1] * X2 % M2; gets(s); int pre = 0; for (;;) { int n = strlen(s); for (int i = 0; i < n; ++i) h1[i] = ((i ? h1[i - 1] * X1 : 0) + s[i]) % M1; for (int i = 0; i < n; ++i) h2[i] = ((i ? h2[i - 1] * X2 : 0) + s[i]) % M2; int len = dup(s, n); if (len >= n) break; int m = 0; for (int i = 0; i < n; ++i) { t[m] = s[i]; h1[m] = ((m ? h1[m - 1] : 0) * X1 + t[m]) % M1; h2[m] = ((m ? h2[m - 1] : 0) * X2 + t[m]) % M2; ++m; if (i >= len * 2 - 1) { long long v1 = gh(m - len, m - 1); long long v2 = gh(m - len * 2, m - len - 1); if (v1 == v2) m -= len; } } t[m] = 0; strcpy(s, t); } cout << s << endl; return 0; }
10
CPP
x,y = map(int,input().split()) ans = x/y print(ans)
0
PYTHON3
a = int(input()) b = int(input()) c = int(input()) d = a+b+c e = a*b*c f = a*b+c g = a+b*c h = (a+b)*c i = a+(b*c) j = a*(b+c) print(max(d,e,f,g,h,i,j))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int f[520020]; int main() { int n, d, x, cnt, w; long long sum = 0; scanf("%d%d", &n, &d); f[0] = 1; for (int i = 1; i <= n; i++) { scanf("%d", &x); sum += x; for (int j = sum; j >= x; j--) if (f[j - x]) f[j] = 1; } int j; w = cnt = 0; while (1) { j = w + d; while (!f[j] && j > w) j--; if (j == w) break; w = j; cnt++; } printf("%d %d\n", w, cnt); return 0; }
8
CPP
def main(): word = input() moves = 0 pointerAt = 'a' for letter in word: c = abs(ord(pointerAt) - ord(letter)) ac = 26 - abs(ord(letter) - ord(pointerAt)) moves += min(c, ac) pointerAt = letter print(moves) main()
7
PYTHON3
s = str(input()) n = list(s.split(" ")) Limak = int(n[0]) Bob = int(n[1]) i = 1 while True: if (Limak*3**i) <= (Bob*2**i): i += 1 else: break print (i)
7
PYTHON3
y, k, n = map(int, input().split()) l = [] rem = y % k if rem > 0 and k - rem + y <= n: l.append(k - rem) elif rem == 0 and y + k <= n: l.append(k) if len(l) == 0: print(-1) exit() t = l[0] maxm = n - y while t + k <= maxm: t += k l.append(t) print(*l)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; double maabs(double a) { if (a < 0) return -a; return a; } signed main() { const double e = 0.00000001; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; vector<string> ans; vector<pair<string, string>> v(n); vector<string> fir(n); vector<string> sec(n); vector<long long> var(n, 0); for (long long i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; fir[i] = ""; fir[i] += v[i].first[0]; fir[i] += v[i].first[1]; fir[i] += v[i].first[2]; sec[i] += v[i].first[0]; sec[i] += v[i].first[1]; sec[i] += v[i].second[0]; } for (long long i = 0; i < n; i++) for (long long j = 0; j < n; j++) { if (i == j) continue; string fir1 = fir[i]; string sec1 = sec[i]; string fir2 = fir[j]; string sec2 = sec[j]; if (fir1 == fir2 && sec1 == sec2) { cout << "NO"; exit(0); } } for (long long i = 0; i < n; i++) for (long long j = 0; j < n; j++) { if (i == j) continue; string fir1 = fir[i]; string sec1 = sec[i]; string fir2 = fir[j]; string sec2 = sec[j]; if (fir1 == fir2) { var[i] = 2; var[j] = 2; } } set<string> st, firs; for (long long i = 0; i < n; i++) { string fir0 = fir[i]; string sec0 = sec[i]; if (var[i] == 1) { if (st.find(fir0) != st.end()) { cout << "NO"; exit(0); } else { ans.push_back(fir0); st.insert(fir0); firs.insert(fir0); } } else if (var[i] == 2) { if (st.find(sec0) != st.end() || firs.find(fir0) != firs.end()) { cout << "NO"; exit(0); } else { ans.push_back(sec0); st.insert(sec0); } } else { if (st.find(sec0) == st.end() && firs.find(fir0) == firs.end()) { ans.push_back(sec0); st.insert(sec0); } else if (st.find(fir0) == st.end()) { ans.push_back(fir0); st.insert(fir0); firs.insert(fir0); } else { cout << "NO"; exit(0); } } } cout << "YES\n"; for (long long i = 0; i < ans.size(); i++) cout << ans[i] << "\n"; }
10
CPP
class Solution1117B: def __init__(self): self.m = 0 self.k = 0 self.happiness_values = [] self.result = 0 def parse_input(self): _, self.m, self.k = [int(i) for i in input().split(" ")] self.happiness_values = [int(i) for i in input().split(" ")] def solve(self): largest, second_largest = sorted(self.happiness_values, reverse=True)[:2] second_largest_uses = self.m // (self.k + 1) largest_uses = self.m - second_largest_uses self.result = largest_uses * largest + second_largest_uses * second_largest def get_result(self): return str(self.result) if __name__ == "__main__": solution = Solution1117B() solution.parse_input() solution.solve() print(solution.get_result())
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int const N = 1e6 + 100; long long int const LINF = numeric_limits<long long int>::max(); int const INF = numeric_limits<int>::max(); int const BN = 31; long long int gcd(long long int a, long long int b) { return b ? gcd(b, a % b) : a; } long long int pow_mod(long long int a, long long int e, long long int m) { long long int res = 1; while (e) { if (e & 1) res = res * a % m; a = a * a % m; e >>= 1; } return res; } long long int inv_mod(long long int b, long long int m) { b %= m; long long int x = 0, y = 1, r, q, a = m; while (b) { q = a / b; r = a % b; a = b; b = r; r = x; x = y; y = r - q * y; } x += m; return x % m; } vector<int> fact(10, 1); vector<vector<long long int> > dp(10, vector<long long int>(10, -1)); long long int solve(int n, int m) { if (dp[n][m] != -1) return dp[n][m]; if (n == 1) dp[n][m] = (1ll << m) - 1; else if (m == 1) dp[n][m] = 1; else { long long int ret = (1ll << (n * m)); for (int i = 1; i <= n; i++) { ret -= (fact[n] / (fact[n - i] * fact[i])) * solve(n - i, m); } dp[n][m] = ret; } return dp[n][m]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long int n, m, mod = 1e9 + 7; cin >> n >> m; long long int p = pow_mod(2, m, mod) + mod - 1; p %= mod; p = pow_mod(p, n, mod); cout << p << endl; }
8
CPP
s = set() n = input() for i in n: if i != "{" and i != "}" and i != "," and i != " ": s.add(i) print(len(s))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main() { ios_base ::sync_with_stdio(false); cin.tie(nullptr); int n, m, p, cc = 0; cin >> n >> m >> p; unordered_map<int, int> ma; int ar[n], br[m]; for (int i = 0; i < n; i++) { cin >> ar[i]; } for (int i = 0; i < m; i++) { cin >> br[i]; ma[br[i]]++; } cc = ma.size(); vector<int> ans; for (int i = 0; i < p; i++) { unordered_map<int, int> mm; int la = i, cc = 0, req = m; for (int ind = i; ind < n; ind += p) { if (ma.count(ar[ind])) { if (ma[ar[ind]] > mm[ar[ind]]) { req--; } mm[ar[ind]]++; } assert(req >= 0); if (req == 0) { ans.push_back(la); } if (++cc >= m && ma.count(ar[la])) { if (ma[ar[la]] >= mm[ar[la]]) { req++; } assert(mm[ar[la]]); mm[ar[la]]--; } if (cc >= m) { la += p; } } } sort(ans.begin(), ans.end()); cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) { cout << ans[i] + 1 << " "; } cout << "\n"; }
8
CPP
#include <bits/stdc++.h> using namespace std; long solve(long x, long y) { long ans = LONG_MAX; for (long i=1; i<x; i++) { long d1 = floor(y / 2), d2 = floor((x-i)/2); long sa = y * i, sb1 = d1 * (x - i), sc1 = (y - d1) * (x - i), sb2 = d2 * y, sc2 = (x - i - d2) * y; long ans1 = abs(max({sa, sb1, sc1}) - min({sa, sb1, sc1})), ans2 = abs(max({sa, sb2, sc2}) - min({sa, sb2, sc2})); ans = min({ans, ans1, ans2}); } return ans; } int main(){ long h, w; cin >> h >> w; cout << min(solve(h, w), solve(w, h)) << endl; }
0
CPP
for _ in range(int(input())): n=int(input()) a=[int(i) for i in input().split()] d={} for i in range(n): p=(i+a[i])%n if p not in d: d[p]=i k=[*d.values()] k.sort() f=[] for i in range(n): f.append(i) if(f!=k): print("NO") else: print("YES")
7
PYTHON3
## necessary imports import sys # import random # from math import log2, log, ceil input = sys.stdin.readline # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp ## gcd function def gcd(a,b): if a == 0: return b return gcd(b%a, a) ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b # find function def find(x, link): while(x != link[x]): x = link[x] return x # the union function which makes union(x,y) # of two nodes x and y def union(x, y, size, link): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e6 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve() def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().split())) #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################# ---------------- TEMPLATE ENDS HERE ---------------- ################# ## recursive knapsack function using memoization n, d = int_array(); a = sorted(int_array()); m = int(input()); ans = sum(a[:m]); if m > n: ans -= (m-n)*d; print(ans)
7
PYTHON3
#include<iostream> int main(){for(int a,b,t;std::cin>>a>>b;printf("%d\n",a))for(;t=b;a=t)b=a%b;}
0
CPP
#解説見た n = int(input()) p = list(map(int, input().split())) p.append(0) ans = 0 for i in range(n): if p[i] == (i+1): p[i],p[i+1] = p[i+1], p[i] ans += 1 print(ans)
0
PYTHON3
from math import gcd from bisect import bisect_left, bisect_right from itertools import combinations from itertools import permutations from bisect import bisect_left from math import ceil from math import radians from heapq import heapify, heappush, heappop, nsmallest import bisect from math import pi from collections import deque from math import factorial from math import log, ceil from collections import defaultdict from math import * from sys import stdin, stdout import itertools import os import sys import threading from collections import deque, Counter, OrderedDict, defaultdict from heapq import * # from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd # from bisect import bisect_left,bisect_right # from decimal import *,threading from fractions import Fraction mod = int(pow(10, 9)+7) # mod = 998244353 def ii(): return int(input()) def si(): return str(input()) def mi(): return map(int, input().split()) def li1(): return list(mi()) def fii(): return int(stdin.readline()) def fsi(): return str(stdin.readline()) def fmi(): return map(int, stdin.readline().split()) def fli(): return list(fmi()) abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} def getKey(item): return item[0] def sort2(l): return sorted(l, key=getKey) def d2(n, m, num): return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo(x): return (x and (not (x & (x - 1)))) def decimalToBinary(n): return bin(n).replace("0b", "") def ntl(n): return [int(i) for i in str(n)] def powerMod(x, y, p): res = 1 x %= p while y > 0: if y & 1: res = (res * x) % p y = y >> 1 x = (x * x) % p return res graph = defaultdict(list) visited = [0] * 1000000 col = [-1] * 1000000 def bfs(d, v): q = [] q.append(v) visited[v] = 1 while len(q) != 0: x = q[0] q.pop(0) for i in d[x]: if visited[i] != 1: visited[i] = 1 q.append(i) print(x) def make_graph(e): d = {} for i in range(e): x, y = mi() if x not in d: d[x] = [y] else: d[x].append(y) if y not in d: d[y] = [x] else: d[y].append(x) return d def gr2(n): d = defaultdict(list) for i in range(n): x, y = mi() d[x].append(y) return d def connected_components(graph): seen = set() def dfs(v): vs = set([v]) component = [] while vs: v = vs.pop() seen.add(v) vs |= set(graph[v]) - seen component.append(v) return component ans = [] for v in graph: if v not in seen: d = dfs(v) ans.append(d) return ans def primeFactors(n): s = set() while n % 2 == 0: s.add(2) n = n // 2 for i in range(3, int(sqrt(n)) + 1, 2): while n % i == 0 and i % 2 == 1: s.add(i) n = n // i if n > 2 and n % 2 == 1: s.add(n) return s def find_all(a_str, sub): start = 0 while True: start = a_str.find(sub, start) if start == -1: return yield start start += len(sub) def SieveOfEratosthenes(n, isPrime): isPrime[0] = isPrime[1] = False for i in range(2, n): isPrime[i] = True p = 2 while (p * p <= n): if (isPrime[p] == True): i = p * p while (i <= n): isPrime[i] = False i += p p += 1 return isPrime def dijkstra(edges, f, t): g = defaultdict(list) for l, r, c in edges: g[l].append((c, r)) q, seen, mins = [(0, f, ())], set(), {f: 0} while q: (cost, v1, path) = heappop(q) if v1 not in seen: seen.add(v1) path = (v1, path) if v1 == t: return (cost, path) for c, v2 in g.get(v1, ()): if v2 in seen: continue prev = mins.get(v2, None) next = cost + c if prev is None or next < prev: mins[v2] = next heappush(q, (next, v2, path)) return float("inf") def binsearch(a, l, r, x): while l <= r: mid = l + (r-1)//2 if a[mid]: return mid elif a[mid] > x: l = mid-1 else: r = mid+1 return -1 # def input(): # return stdin.buffer.readline() def readTree(n): adj = [set() for _ in range(n)] for _ in range(n-1): u, v = map(int, input().split()) adj[u-1].add(v-1) adj[v-1].add(u-1) return adj def treeOrderByDepth(n, adj, root=0): parent = [-2] + [-1]*(n-1) ordered = [] q = deque() q.append(root) depth = [0] * n while q: c = q.popleft() ordered.append(c) for a in adj[c]: if parent[a] == -1: parent[a] = c depth[a] = depth[c] + 1 q.append(a) return (ordered, parent, depth) def isSubSequence(str1, str2): m = len(str1) n = len(str2) j = 0 # Index of str1 i = 0 # Index of str2 # Traverse both str1 and str2 # Compare current character of str2 with # first unmatched character of str1 # If matched, then move ahead in str1 while j < m and i < n: if str1[j] == str2[i]: j = j+1 i = i + 1 # If all characters of str1 matched, then j is equal to m return j == m def nextPowerOf2(n): count = 0 # First n in the below # condition is for the # case where n is 0 if (n and not(n & (n - 1))): return n while(n != 0): n >>= 1 count += 1 return 1 << count def cou(n): c = 0 while n > 1: c += 1 n //= 2 return c def sortsec(l): return sorted(l, key=lambda x: x[1]) def BinarySearch(a, x): i = bisect_left(a, x) if i: return (i-1) else: return - 1 def subarray(A): r = set() p = {0} for x in A: p = {x | y for y in p} | {x} r |= p return len(r) def setBitNumber(n): n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 n = n + 1 return (n >> 1) def countSetBits(n): count = 0 while (n): count += n & 1 n >>= 1 return count def allsub(l): n = len(l) a=[] for i in range(n - 1): for j in range(i + 1, n): a.append(l[i:j]) return a for _ in range(ii()): n = ii() a = n % 10 b=0 while n != 0: n = n // 10 b += 1 c = (a - 1) * 10 print(c+(b*(b+1)//2))
7
PYTHON3
#include<cstdio> #include<algorithm> #include<vector> #include<map> using namespace std; typedef pair<int,int>P; int main() { int n,m,p,a,b,x,y,z,i,j,k,l; double dp[30][1<<8],t[8]; vector<P>e[30]; while(scanf("%d%d%d%d%d",&n,&m,&p,&a,&b),n) { --a,--b; for(i=0;i<30;++i)e[i].clear(); for(i=0;i<n;++i)scanf("%lf",t+i); while(p--) { scanf("%d%d%d",&x,&y,&z);--x,--y; e[x].push_back(P(y,z)); e[y].push_back(P(x,z)); } fill(dp[0],dp[30],1e9); dp[a][(1<<n)-1]=0; for(i=(1<<n)-1;i>0;--i) { for(j=0;j<m;++j) { if(dp[j][i]==1e9)continue; for(k=0;k<e[j].size();++k) { for(l=0;l<n;++l) { if(!(i&(1<<l)))continue; double&r=dp[e[j][k].first][i^(1<<l)]; r=min(r,dp[j][i]+e[j][k].second/t[l]); } } } } double r=*min_element(dp[b],dp[b+1]); if(r==1e9)puts("Impossible"); else printf("%f\n",r); } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int n, m, a[200050], b[200050], pos[200050], l[200050], r[200050]; pair<int, int> c[400050]; long long ans = 1; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); for (int i = 1; i <= n; i++) c[i].first = a[i], c[i].second = i; for (int i = 1; i <= m; i++) c[i + n].first = b[i], c[i + n].second = i + n; sort(c + 1, c + n + m + 1); for (int i = 1; i <= n + m; i++) if (c[i].second > n && c[i].first == c[i - 1].first) pos[c[i].second - n] = c[i - 1].second; pos[m + 1] = l[m + 1] = r[m + 1] = n + 1; bool pd = 1; for (int i = 1; i <= m; i++) if (pos[i] == 0 || pos[i] > pos[i + 1]) pd = 0; if (!pd) { printf("0\n"); return 0; } for (int i = 1, j; i <= m; i++) { for (j = pos[i]; j > pos[i - 1]; j--) if (a[j] < b[i]) break; l[i] = j + 1; for (j = pos[i]; j < pos[i + 1]; j++) if (a[j] < b[i]) break; r[i] = j - 1; } for (int i = 1; i <= m + 1; i++) if (l[i] > r[i - 1] + 1) pd = 0; if (!pd) { printf("0\n"); return 0; } for (int i = 1; i <= m + 1; i++) ans = ans * (r[i - 1] - l[i] + 2) % 998244353; printf("%lld\n", ans); return 0; }
11
CPP
import sys #input = sys.stdin.buffer.readline def main(): N = int(input()) S = input() Q = input() x = tuple(map(int,input().split())) for k in x: d,m,dm,dmc = 0,0,0,0 for i in range(N): st = S[i] if st == "D": d += 1 elif st == "M": m += 1 dm += d elif st == "C": dmc += dm if i >= k-1: if S[i-k+1] == "D": dm -= m d -= 1 elif S[i-k+1] == "M": m -= 1 print(dmc) if __name__ == "__main__": main()
0
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int MAX_N = 2e4 + 5; const int INF = 0x7fffffff; const double eps = 1e-6; int n; int a[105]; map<int, int> sg; set<int> primes; void solve(int first) { if (sg.count(first)) return; vector<int> mex(30, 0); for (int i = 0; first >> i; i++) { int nx = ((first >> (i + 1)) | (first & ((1 << i) - 1))); solve(nx); mex[sg[nx]] = 1; } int ret = 0; for (; mex[ret]; ret++) ; sg[first] = ret; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = (0), _end_ = (n); i < _end_; ++i) { cin >> a[i]; int tmp = a[i]; for (int j = 2; j * j <= tmp; j++) { if (tmp % j == 0) { primes.insert(j); while (tmp % j == 0) tmp /= j; } } if (tmp > 1) primes.insert(tmp); } sg[0] = 0; int ans = 0; for (auto p : primes) { int state = 0; for (int i = (0), _end_ = (n); i < _end_; ++i) { int cnt = 0; while (a[i] % p == 0) { a[i] /= p; cnt++; } if (cnt) state |= (1 << (cnt - 1)); } solve(state); ans ^= sg[state]; } if (ans) puts("Mojtaba"); else puts("Arpa"); return 0; }
9
CPP
#include <iostream> using namespace std; int main(){ int a, b, k, i = 101; cin >> a >> b >> k; while(k){ i --; if(a%i == 0 && b%i == 0){ k --; } } cout << i << endl; }
0
CPP
#include <bits/stdc++.h> int main() { long long int n; scanf("%lld", &n); n++; if (n == 1) printf("0"); else if (n & 1) printf("%lld", n); else printf("%lld", n / 2); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; long long n, m, i, j, k, w, a, b, start, x; long long arr[1000005], brr[1000005]; map<long long, long long> mp, vis; vector<long long> vec, v[1000005]; vector<vector<long long> > res; long long cnt, edges, totver, totedges, mx, ans; int main() { srand(53411); ios::sync_with_stdio(false); cin >> n >> start >> x; for (i = 1; i <= n; i++) arr[i] = i; random_shuffle(arr + 1, arr + n + 1); for (i = 1; i <= n; i++) { brr[arr[i]] = i; } pair<long long, long long> lo, hi; lo = make_pair(INT_MIN, start); hi = make_pair(INT_MAX, -1); long long val, nxt; cout << "? " << start << "\n"; fflush(stdout); cin >> val >> nxt; if (val == x) { cout << "! " << x << "\n"; fflush(stdout); return 0; } if (val < x) { lo = make_pair(val, nxt); } for (i = 1; i <= min(n, 1500ll); i++) { cout << "? " << brr[i] << "\n"; fflush(stdout); cin >> val >> nxt; if (val == x) { cout << "! " << x << "\n"; fflush(stdout); return 0; } else if (val < x) { if (lo.first < val) lo = make_pair(val, nxt); } } long long curr = lo.second; while (lo.first < x && lo.second != -1) { cout << "? " << lo.second << "\n"; fflush(stdout); cin >> val >> nxt; if (val == x) { cout << "! " << x << "\n"; fflush(stdout); return 0; } lo = make_pair(val, nxt); } if (lo.first >= x) { cout << "! " << lo.first << "\n"; fflush(stdout); return 0; } cout << "! -1" << "\n"; fflush(stdout); return 0; }
10
CPP
from collections import * from operator import * myname, n, mem = input(), int(input()), defaultdict(int) for i in range(n): s, val = input().split(), 0 x, y = s[0], s[3][:-2] if s[1] == 'posted': val = 15 elif s[1] == 'commented': val = 10 else: val = 5 y = s[2][:-2] if x == myname: mem[y] += val elif y == myname: mem[x] += val else: if not mem[x]: mem[x] = 0 if not mem[y]: mem[y] = 0 ans = sorted(list(mem.items()), reverse=True, key=itemgetter(1, 0)) mem = defaultdict(list) for i, j in ans: mem[j].append(i) for i, j in mem.items(): for k in j[::-1]: print(k)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a[600][600]; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j]; for (int i = n - 2; i >= 0; i--) if (!a[i][m - 1]) a[i][m - 1] = a[i + 1][m - 1] - 1; for (int i = m - 2; i >= 0; i--) if (!a[n - 1][i]) a[n - 1][i] = a[n - 1][i + 1] - 1; for (int i = n - 2; i >= 0; i--) for (int j = m - 2; j >= 0; j--) if (!a[i][j]) a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1; for (int i = 1; i < n; i++) if (a[i][0] <= a[i - 1][0]) return puts("-1"), 0; for (int i = 1; i < m; i++) if (a[0][i] <= a[0][i - 1]) return puts("-1"), 0; for (int i = 1; i < n; i++) for (int j = 1; j < m; j++) if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1]) return puts("-1"), 0; long long sum = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) sum += a[i][j]; cout << sum << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n,m; cin>>n>>m; int a[n]; for(int i=0;i<n;i++)cin>>a[i]; vector<bool> v(n,true); while(m--) { int x,y; cin>>x>>y; if(a[x-1]>a[y-1]) v[y-1]=0; else if(a[x-1]<a[y-1]) v[x-1]=0; else { v[x-1]=0; v[y-1]=0; } } int cnt=0; for(int i=0;i<n;i++) { if(v[i]==1) cnt++; } cout<<cnt; return 0; }
0
CPP
from functools import reduce def gcd(a, b): while b: a, b = b, a % b return abs(a) def lcm(a, b): return a*b//gcd(a, b) N, M = map(int, input().split()) A = [int(a)//2 for a in input().split()] B = [a & -a for a in A] if min(B) != max(B): print(0) else: l = reduce(lcm, A) print(M // l - M // l // 2)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m; int c[555], p[555]; int fst[555][555]; int f[555][555]; int getf(int l, int r) { if (f[l][r] != -1) return f[l][r]; if (l + 1 >= r) return 1; int cur = p[fst[l][r]]; long long tmp = 0ll, res = 0ll; for (int i = cur; i <= r; i++) (tmp += (long long)getf(cur, i) * (long long)getf(i, r) % 998244353) %= 998244353; for (int i = l; i < cur; i++) (res += ((long long)getf(l, i) * (long long)getf(i, cur - 1)) % 998244353 * tmp) %= 998244353; return f[l][r] = res; } int main() { memset(f, -1, sizeof(f)); scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d", &c[i]), p[c[i]] = i; for (int i = 0; i < m; i++) { fst[i][i + 1] = c[i + 1]; for (int j = i + 2; j <= m; j++) fst[i][j] = min(fst[i][j - 1], c[j]); } printf("%d\n", getf(0, m)); return 0; }
12
CPP
#include<bits/stdc++.h> using namespace std; const int Q = 55; int n, m, q; int a[Q], b[Q], c[Q], d[Q]; int A[11], ans = 0; void dfs(int i, int t) { if (i == n + 1) { int s = 0; for (int j = 0; j < q; ++j) if (A[b[j]] - A[a[j]] == c[j]) s += d[j]; ans = max(ans, s); return; } for (A[i] = t; A[i] <= m; ++A[i]) dfs(i + 1, A[i]); } int main() { cin >> n >> m >> q; for (int i = 0; i < q; ++i) cin >> a[i] >> b[i] >> c[i] >> d[i]; dfs(1, 1); cout << ans << endl; }
0
CPP
# .∧_∧ # ( ・ω・。)つ━・*。 # ⊂  ノ    ・゜+. # しーJ   °。+ *´¨) #          .· ´¸.·*´¨) ¸.·*¨) #           (¸.·´ (¸.·'* # # # *** # ***** # *****~~ # **~~~ # *~~~*** # ~~~****** # ~~********* # ************* # *********** # ************* # *************** # *@*************** # ***@************@** # *@****************@** # @**************** # ******************* # *****@*************** # *********************** # ********@**************** # *****************@********* # ***************************** # ||||| # ||||| _8_8_ # ||||| | | |_8_ # ||||| |__|__|___| n = int(input()) a = sorted([[int(i) for i in input().split()] for j in range(n)]) b = sorted([[int(i) for i in input().split()] for j in range(n)], reverse=True) print(a[0][0] + b[0][0], a[0][1] + b[0][1])
8
PYTHON3
#include <bits/stdc++.h> using namespace std; mt19937 rnd(time(0)); const long long maxn = 1e5 + 50; void solve() { long long n; cin >> n; map<long long, long long> mp[n], nb; for (long long i = 0; i < n; ++i) { mp[i] = map<long long, long long>(); } map<vector<long long>, long long> lol; for (long long i = 0; i < n - 2; ++i) { long long a[3]; cin >> a[0] >> a[1] >> a[2]; a[0]--; a[1]--; a[2]--; sort(a, a + 3); lol[{a[0], a[1], a[2]}] = i; for (long long j = 0; j < 3; ++j) for (long long k = j + 1; k < 3; ++k) { nb[a[j]]++; nb[a[k]]++; mp[a[j]][a[k]]++; mp[a[k]][a[j]]++; } } long long last = 0; long long root = 0; long long now = 0; long long p[n]; long long i = 0; long long j = 0; long long q[n - 2]; while (now != root || i == 0) { for (auto &u : mp[now]) { if (u.second == 1 && u.first != last) { p[i++] = now; last = now; now = u.first; break; } } } set<long long> can; for (long long i = 0; i < n; ++i) if (nb[i] == 2) can.insert(i); for (long long i = 0; i < n - 2; ++i) { long long start = *can.begin(); can.erase(start); long long x = mp[start].begin()->first; long long y = (++mp[start].begin())->first; nb[x]--; nb[y]--; nb[y]--; nb[x]--; if (nb[x] == 2) can.insert(x); if (nb[y] == 2) can.insert(y); nb[start]--; nb[start]--; vector<long long> v = {x, y, start}; sort(v.begin(), v.end()); q[i] = lol[v]; if ((--mp[x][y]) == 0) mp[x].erase(y); if ((--mp[y][x]) == 0) mp[y].erase(x); if ((--mp[x][start]) == 0) mp[x].erase(start); if ((--mp[start][x]) == 0) mp[start].erase(x); if ((--mp[start][y]) == 0) mp[start].erase(y); if ((--mp[y][start]) == 0) mp[y].erase(start); } for (long long i = 0; i < n; ++i) cout << p[i] + 1 << ' '; cout << endl; for (long long i = 0; i < n - 2; ++i) cout << q[i] + 1 << ' '; cout << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long int psum = 0, nsum = 0, ans = 0; int i = 0; while (i < n) { while (a[i] >= 0 && i < n) { psum += a[i]; i++; } while (a[i] < 0 && i < n) { nsum = nsum - a[i]; i++; } if (nsum > psum) { ans += nsum - psum; psum = 0; } else if (psum >= nsum) { psum = psum - nsum; } nsum = 0; } cout << ans << endl; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; set<long long> s; long long ans = 0, m; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } void getfactors(long long x) { s.clear(); long long rx = sqrt(x); for (int i = 2; i <= rx; i++) { if (x % i == 0) { x /= i; s.insert(i); while (x % i == 0) x /= i; } } if (x > 1) s.insert(x); } int main() { long long x, y; scanf("%I64d%I64d", &x, &y); getfactors(x); while (y) { long long g = gcd(x, y); x /= g; y /= g; m = y; for (auto p : s) if (x % p == 0) m = min(m, y % p); ans += m; y -= m; } printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long int mod = 998244353; long long int pow(long long int a, long long int b, long long int m) { long long int ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; a = (a * a) % m; b >>= 1; } return ans; } int main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); long long int n, i, j, k, x, y, z, a, b, c; cin >> n; long long int ans = 1; for (i = 1; i <= n; ++i) ans = (ans * i) % mod; long long int tmp = 0; x = 1; y = ans * pow(n, mod - 2, mod) % mod; for (i = n - 1; i >= 2; --i) { tmp = (tmp + x * (y - 1 + mod)) % mod; x = x * i % mod; y = y * pow(i, mod - 2, mod) % mod; } ans = (ans + tmp * n) % mod; cout << ans << endl; return 0; }
10
CPP
#include <iostream> #include <cmath> #include <cstring> using namespace std; void mkPrime(int n,bool p[]){ int i,j; p[0]=p[1]=0; for(i=2;i<=n;i++) p[i]=1; for(i=2;i<=sqrt(n);i++) if(p[i]) for(j=i*2;j<=n;j+=i) p[j]=0; } int main(void){ int a,d,n; bool p[1000050]; mkPrime(1000040,p); while(cin>>a>>d>>n && (a||d||n)){ for(;n!=0;a+=d) if(p[a]) n--; cout<<a-d<<endl; } return 0; }
0
CPP