solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; const int n = 2e5 + 10; int N, Q; vector<int> A; int maxP[n]; int cnt[n]; int main() { cin >> N >> Q; A.resize(N); for (int i = 0; i < N; i++) { cin >> A[i]; maxP[A[i]] = i + 1; cnt[A[i]]++; } int sum = N; int to = 1, maxCnt = 0; for (int i = 0; i < N; i++) { if (i < to) { to = max(to, maxP[A[i]]); maxCnt = max(maxCnt, cnt[A[i]]); } else { sum -= maxCnt; maxCnt = cnt[A[i]]; to = maxP[A[i]]; } } sum -= maxCnt; cout << sum << endl; return 0; }
2,000
CPP
n=int(input()) s = 0 for i in range(1,n): if(n%i==0): s+=1 print(s)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int N, sum; bool minu[100005]; priority_queue<pair<int, int> > pq; int main() { cin >> N; for (int i = 0; i < N; ++i) { int tmp; cin >> tmp; pq.push(make_pair(tmp, i)); } while (!pq.empty()) { pair<int, int> p = pq.top(); int tmp = p.first; int i = p.second; pq.pop(); if (sum < 0) { sum += tmp; } else { sum -= tmp; minu[i] = true; } } if (sum < 0) { for (int i = 0; i < N; ++i) { minu[i] = !minu[i]; } } for (int i = 0; i < N; ++i) { cout << (minu[i] ? '-' : '+'); } }
1,900
CPP
t=int(input()) while(t): t-=1 co=[0]*26 n,m=map(int,input().split()) reco=[0]*n s=input() a=list(map(int,input().split())) for i in range(m): reco[0]+=1 reco[a[i]]+=-1 reco[0]+=1 for i in range(1,n): reco[i]=reco[i]+reco[i-1] for i in range(n): z=ord(s[i])-97 co[z]+=reco[i] print(*co)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int x = 0, f = 1; 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; } int n, m, q; vector<int> to[3005], from[3005]; struct problem { int k, s, num; }; vector<problem> pro[3005]; bool vis[3005]; int f[3005][13]; int ans[400005]; void dfs(int x) { vis[x] = true; for (unsigned int i = 0; i < from[x].size(); i++) if (!vis[from[x][i]]) dfs(from[x][i]); } int main() { n = read(); m = read(); q = read(); for (int i = 1; i <= m; i++) { int a = read(), b = read(); from[b].push_back(a); to[a].push_back(b); } for (int i = 1; i <= n; i++) sort(to[i].begin(), to[i].end()); for (int i = 1; i <= q; i++) { int s = read(), t = read(), k = read() - 1; problem node; node.s = s; node.k = k; node.num = i; pro[t].push_back(node); } for (int i = 1; i <= n; i++) { if (pro[i].size() == 0) continue; memset(vis, false, sizeof vis); memset(f, 0, sizeof f); dfs(i); f[n + 1][0] = f[i][0] = n + 1; for (int j = 1; j <= n; j++) { if (j == i) continue; f[j][0] = 0; if (vis[j]) for (unsigned int k = 0; k < to[j].size(); k++) if (vis[to[j][k]]) { f[j][0] = to[j][k]; break; } } for (int j = 1; j <= 12; j++) for (int k = 1; k <= n + 1; k++) f[k][j] = f[f[k][j - 1]][j - 1]; for (unsigned int j = 0; j < pro[i].size(); j++) { problem node = pro[i][j]; ans[node.num] = -1; if (f[node.s][12] == n + 1 || f[node.s][12] == i) { for (int k = 0; k <= 12; k++) if ((node.k >> k) & 1) node.s = f[node.s][k]; if (node.s != n + 1) ans[node.num] = node.s; } } } for (int i = 1; i <= q; i++) printf("%d\n", ans[i]); return 0; }
2,700
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; const double pi = 3.1415926535897932384626433832795; double eps = 1e-9; char ch[1 << 20]; inline string gs() { scanf("%s", ch); return string(ch); } inline string gl() { gets(ch); return string(ch); } inline int gi() { int x; scanf("%d", &x); return x; } void error(bool x) { if (x) { fprintf(stderr, "ERROR\n"); int ttt = 0; cout << 7 / ttt; } } template <class T> T gcd(T a, T b) { return (!a) ? b : gcd(b % a, a); } int mod = 1000000007; inline int ADD(int a, int b) { return (a + b >= mod) ? a + b - mod : a + b; } inline int SUB(int a, int b) { return (a - b < 0) ? a - b + mod : a - b; } inline int MUL(int a, int b) { return 1ll * a * b % mod; } inline int POW(int a, int n) { int r = 1; while (n) { if (n & 1) r = MUL(r, a); n >>= 1; a = MUL(a, a); } return r; } const int MATRIX_SIZE = 11; typedef int matrix[MATRIX_SIZE][MATRIX_SIZE]; void MMUL(matrix &a, matrix &b) { matrix c; for (int i = (0), _b(MATRIX_SIZE); i < _b; ++i) for (int j = (0), _b(MATRIX_SIZE); j < _b; ++j) c[i][j] = 0; for (int i = (0), _b(MATRIX_SIZE); i < _b; ++i) for (int j = (0), _b(MATRIX_SIZE); j < _b; ++j) { for (int k = (0), _b(MATRIX_SIZE); k < _b; ++k) c[i][j] = ADD(c[i][j], MUL(a[i][k], b[k][j])); } for (int i = (0), _b(MATRIX_SIZE); i < _b; ++i) for (int j = (0), _b(MATRIX_SIZE); j < _b; ++j) a[i][j] = c[i][j]; } int solve(long long a, long long b) { if (a > b) swap(a, b); if (a == 0) { return 0; } else { int win = solve(b % a, a); if (win == 0) return 1; long long n = b / a; if (a % 2LL == 1) { if (n % 2LL == 1) return 0; else return 1; } else { if ((n % (a + 1LL)) % 2LL == 0) return 1; else return 0; } } } int main(int argn, char **argv) { int tn = gi(); while (tn--) { long long a; long long b; cin >> a >> b; if (solve(a, b)) printf("First\n"); else printf("Second\n"); } return 0; }
2,300
CPP
#!/usr/bin/env python3 from sys import stdin import collections def solve(tc): n, k = map(int, stdin.readline().split()) if n < k: print("NO") return if n % 2: if k % 2 == 0: print("NO") return else: ans = [1 for i in range(k-1)] ans.append(n-k+1) else: if k%2: if n < k*2: print("NO") return ans = [2 for i in range(k-1)] ans.append(n-2*k+2) else: ans = [1 for i in range(k-1)] ans.append(n-k+1) print("YES") for i in ans: print(i, end=' ') print() LOCAL_TEST = not __debug__ if LOCAL_TEST: infile = __file__.split('.')[0] + "-test.in" stdin = open(infile, 'r') tcs = int(stdin.readline().strip()) tc = 1 while tc <= tcs: solve(tc) tc += 1
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, k, t; cin >> n >> k >> t; if (k <= t && t <= n) { cout << k; return 0; } if (t <= k) { cout << t; return 0; } if (n <= t && t < n + k) cout << n + k - t; return 0; }
800
CPP
t = int(input()) for i in range(t): x = int(input()) y = input() count = 0 dp = [] for i in y: try: if i == ")" and dp[-1] == "(": del dp[-1] # if i == "(" and dp[-1] == ")": # del dp[-1] else: dp.append(i) except IndexError: dp.append(i) print(int(len(dp)/2))
1,000
PYTHON3
class Matrix: def __init__(self): self.matrix = [] for i in range(5): self.matrix.append(input().split()) def answer(self): for i in range(5): if '1' in self.matrix[i]: for j in range(5): if self.matrix[i][j] == "1": index = [i, j] return abs(2 - index[0]) + abs(2 - index[1]) matrix = Matrix() print(matrix.answer())
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n; char s[N]; long long ans; vector<int> v; bool letter(char c) { return c <= 'Z' && c >= 'A' || c <= 'z' && c >= 'a'; } bool num(char c) { return c <= '9' && c >= '0'; } int getcnt1(int bg, int ed) { for (int i = ed - 1; i >= bg; --i) { if (!(letter(s[i]) || num(s[i]) || s[i] == '_')) { bg = i; break; } } int cnt = 0; for (int i = bg; i < ed; ++i) { if (letter(s[i])) ++cnt; } return cnt; } int getcnt2(int bg, int ed) { int ps = -1; for (int i = bg; i < ed; ++i) { if (s[i] == '.') { ps = i; break; } } if (ps == -1) return 0; if (ps == bg) return 0; for (int i = bg; i < ps; ++i) { if (!(letter(s[i]) || num(s[i]))) return 0; } int t = 0; for (int i = ps + 1; i < ed; ++i) { if (letter(s[i])) ++t; else break; } return t; } int main() { scanf("%s", s); n = strlen(s); v.push_back(-1); for (int i = 0; i < n; ++i) { if (s[i] == '@') { v.push_back(i); } } v.push_back(n); for (int i = 1; i < v.size() - 1; ++i) { int cnt1 = getcnt1(v[i - 1] + 1, v[i]); int cnt2 = getcnt2(v[i] + 1, v[i + 1]); ans += 1ll * cnt1 * cnt2; } printf("%lld\n", ans); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long n, m, block, tmp, k; long long a[2000005], ans[2000005]; long long cnt[2000005]; struct Query { long long l, r, id, pos; } q[2000005]; inline bool cmp(Query A, Query B) { return A.pos == B.pos ? (A.pos & 1 ? A.r < B.r : A.r > B.r) : A.pos < B.pos; } inline void del(long long p) { cnt[a[p]]--; tmp -= cnt[a[p] ^ k]; } inline void ins(long long p) { tmp += cnt[a[p] ^ k]; cnt[a[p]]++; } signed main() { n = read(), m = read(), k = read(); block = sqrt(n); for (long long i = 1; i <= n; i++) { a[i] = a[i - 1] ^ read(); } for (long long i = 1; i <= m; i++) { q[i].l = read() - 1, q[i].r = read(); q[i].pos = q[i].l / block, q[i].id = i; } sort(q + 1, q + 1 + m, cmp); cnt[0] = 1; long long l = 0, r = 0; for (long long i = 1; i <= m; i++) { while (l < q[i].l) del(l++); while (l > q[i].l) ins(--l); while (r < q[i].r) ins(++r); while (r > q[i].r) del(r--); ans[q[i].id] = tmp; } for (long long i = 1; i <= m; i++) { printf("%lld\n", ans[i]); } return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const int maxs = 61; long long a[maxn]; int rn, r[maxs][maxn], x[maxn], ind[maxn], is[maxn]; int main() { int n; scanf("%d", &n); long long all = 0; for (int i = 0; i < n; ++i) { scanf("%I64d", &a[i]); all ^= a[i]; } for (int t = 0; t < 2; ++t) { for (int b = maxs - 1; b >= 0; --b) { x[n] = 1; for (int j = 0; j < n; ++j) { x[j] = (a[j] >> b) & 1; } if (((all >> b) & 1) != t) continue; for (int i = 0; i < rn; ++i) { if (!x[ind[i]]) continue; for (int j = 0; j < n + 1; ++j) { x[j] ^= r[i][j]; } } int p = 0; while (p < n && !x[p]) ++p; if (p < n) { memcpy(r[rn], x, sizeof(x)); ind[rn++] = p; } } } memset(is, 0, sizeof(is)); for (int i = rn - 1; i >= 0; --i) { int p = ind[i]; is[p] = r[i][n]; for (int j = i + 1; j < rn; ++j) { is[p] ^= is[ind[j]] * r[i][ind[j]]; } } for (int i = 0; i < n; ++i) { printf("%d ", ++is[i]); } puts(""); return 0; }
2,700
CPP
for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) ans=[0]+l for i in range(n-1,0,-1): ans[i]=max(ans[i+1]-1,ans[i]) for i in range(1,n+1): if ans[i]>0: print('1',end=' ') else: print('0',end=' ') print('')
900
PYTHON3
#include <bits/stdc++.h> using namespace std; inline long long rd() { long long _x = 0; int _ch = getchar(), _f = 1; for (; !isdigit(_ch) && (_ch != '-') && (_ch != EOF); _ch = getchar()) ; if (_ch == '-') { _f = 0; _ch = getchar(); } for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch - '0'; return _f ? _x : -_x; } void write(long long _x) { if (_x >= 10) write(_x / 10), putchar(_x % 10 + '0'); else putchar(_x + '0'); } inline void wrt(long long _x, char _p) { if (_x < 0) putchar('-'), _x = -_x; write(_x); if (_p) putchar(_p); } int n; int s1, s2; int a[100005]; pair<int, int> H[100005]; set<int> st; int dp[100005]; bool check(int len) { for (int i = int(1); i <= (int)(n); i++) dp[i] = n + 1; int pos = 0; st.clear(), st.insert(n + 1); for (int i = int(1); i <= (int)(n + 2); i++) { while (H[pos + 1].first < H[i].first - len) st.insert(H[pos + 1].second), pos++; dp[H[i].second] = *st.lower_bound(H[i].second > n ? 0 : H[i].second); } st.clear(), st.insert(n + 1); pos = n + 3; for (int i = int(n + 2); i >= (int)(1); i--) { while (H[pos - 1].first > H[i].first + len) st.insert(H[pos - 1].second), pos--; dp[H[i].second] = min(dp[H[i].second], *st.lower_bound(H[i].second > n ? 0 : H[i].second)); } int R = max(dp[n + 1], dp[n + 2]) - 1; for (int i = int(1); i <= (int)(n); i++) { if (R < i) return 0; R = max(R, dp[i] - 1); } return 1; } int main() { n = rd(), s1 = rd(), s2 = rd(); for (int i = int(1); i <= (int)(n); i++) a[i] = rd(), H[i] = make_pair(a[i], i); H[n + 1] = make_pair(s1, n + 1); H[n + 2] = make_pair(s2, n + 2); sort(H + 1, H + 3 + n); int l = abs(s1 - s2), r = 1e9, ans = 0; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) r = mid - 1, ans = mid; else l = mid + 1; } wrt(ans, '\n'); }
2,600
CPP
li = list(map(int, input().split())) li.sort() a = li[-1] - li[0] b = li[1] - a c = li[2] - a print(a, b, c)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } bool cmpManh(const std::pair<long long, long long>& l, const std::pair<long long, long long>& r) { return ((llabs(l.first) + llabs(l.second)) < (llabs(r.first) + llabs(r.second))); } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int main(void) { long long int ax, ay, bx, by, cx, cy; cin >> ax >> ay; cin >> bx >> by; cin >> cx >> cy; long long int da = (ax - bx) * (ax - bx) + (ay - by) * (ay - by); long long int dc = (cx - bx) * (cx - bx) + (cy - by) * (cy - by); long long int db = (cx - ax) * (cx - ax) + (cy - ay) * (cy - ay); long long int x1 = ax; long long int y1 = ay; long long int x2 = bx; long long int y2 = by; long long int x3 = cx; long long int y3 = cy; if (da == dc && ((x3 - x2) * (y2 - y1)) != ((y3 - y2) * (x2 - x1))) { cout << "Yes" << '\n'; } else { cout << "No" << '\n'; } return (0); }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; long long int temp = 0; long long int count = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] >= 0) { temp += arr[i]; } else { if (abs(arr[i]) <= temp) { temp -= abs(arr[i]); } else if (abs(arr[i]) > temp) { count += (abs(arr[i]) - temp); temp = 0; } } } cout << count << "\n"; } }
1,000
CPP
import sys input = sys.stdin.readline for _ in range(int(input())): t = list(map(int,input().split())) t.sort() a,b,c = t d = c - b if d < a: c -= (d + (a-d)//2) b -= ((a-d) - (a-d)//2) else: c -= a print(a + min(b,c))
1,100
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:200000000") using namespace std; template <typename T> inline T Abs(T x) { return (x >= 0) ? x : -x; } template <typename T> inline T sqr(T x) { return x * x; } template <typename T> inline string toStr(T x) { stringstream st; st << x; string s; st >> s; return s; } template <typename T> inline int bit(T mask, int b) { return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0; } inline int nextInt() { int x; if (scanf("%d", &x) != 1) throw; return x; } inline long long nextInt64() { long long x; if (scanf("%lld", &x) != 1) throw; return x; } inline double nextDouble() { double x; if (scanf("%lf", &x) != 1) throw; return x; } const int INF = (int)1E9; const long long INF64 = (long long)1E18; const long double EPS = 1E-9; const long double PI = 3.1415926535897932384626433832795; const int MAXN = 200100; long long gcd(long long a, long long b) { if (min(a, b) == 0) return a + b; return gcd(b % a, a); } long long a[MAXN]; int n; int main() { n = nextInt(); for (int i = 0; i < (int)(n); i++) a[i] = nextInt(); sort(a, a + n); long long ans = 0; for (int i = 0; i < (int)(n); i++) ans += a[i]; for (int i = 0; i < (int)(n - 1); i++) ans += (a[i + 1] - a[i]) * (i + 1) * (n - i - 1) * 2; long long z = n, g = gcd(ans, z); cout << ans / g << " " << z / g << endl; return 0; }
1,600
CPP
n,k = map(int,input().split()) s = input() alph = "qwertyuiopasdfghjklzxcvbnm" ti = [0] * 26 num = 1 for i in range(1,n): if s[i] == s[i-1]: num += 1 else: if num >= k: ti[alph.find(s[i-1])] += num // k num = 1 if num >= k: ti[alph.find(s[-1])] += num // k print(max(ti))
1,100
PYTHON3
t = int(input()) res = [] for i in range(t): n = int(input()) a = [int(i) for i in input().split(' ')] b = [int(i) for i in input().split(' ')] k = -1 needToCont = True i = 0 stoped = False while needToCont and i < n: if a[i] < b[i]: if k == -1: k = b[i] - a[i] i += 1 elif k != -1 and b[i] - a[i] != k: needToCont = False elif k != -1 and b[i] - a[i] == k and stoped: needToCont = False else: i += 1 elif a[i] == b[i]: if k != -1: stoped = True i += 1 else: needToCont = False res.append(needToCont) for i in res: print("YES" if i else "NO")
1,000
PYTHON3
def read(): return [int(i) for i in input().split(' ')] ai = input() pin = input() res = 0 while ai.find(pin) != -1: q = ai.find(pin) res += 1 ai = ai[q + len(pin):] print(res)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[1005]; int res[1000005]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i); int cnt = 0; for (int i = 1; i < n - 1; i++) { int t, temp = -1; for (int k = 1; k < n; k++) { if (k == i) t = a[k + 1] - a[k - 1]; else t = a[k] - a[k - 1]; if (t > temp) temp = t; } res[cnt++] = temp; } sort(res, res + cnt); printf("%d\n", res[0]); return 0; }
900
CPP
#include <bits/stdc++.h> const int N = 10002; using namespace std; bool test(int s, int r) { long i = (s / 50) % 475; long n = 25; while (n--) { i = (i * 96 + 42) % 475; if ((26 + i) == r) { return true; } } return false; } int p, x, y; int main() { cin >> p >> x >> y; int s = x; while (s >= y) { if (test(s, p)) { cout << 0; return 0; } s -= 50; } s = x; while (!test(s, p)) { s += 50; } int steps = (s - x) / 50; if (steps % 2 == 1) { steps++; } cout << steps / 2; return 0; }
1,300
CPP
x=input() elements = ['0','1','2','3','5','6','8','9'] cnt=0 count=0 for element in elements: if element in x: cnt=cnt+1 for i in range(len(x)): if x[i]=='7' or x[i]=='4': count=count+1 if count==7 or count==4: print('YES') elif '4' in x and '7' in x: if cnt==0 and count==7 or count==4: print('YES') else: print('NO') else: print("NO")
800
PYTHON3
from sys import stdin,stdout def INPUT():return list(int(i) for i in stdin.readline().split()) def inp():return stdin.readline() def out(x):return stdout.write(x) import math INT_MAX=10**13 from bisect import bisect_left import sys MOD=10**9+7 inf=MOD**2 from decimal import* #=================================================================== # import kivy # from kivy.app import App # from kivy.uix.label import Label # # class A(App): # def build(self): # return Label(text="Hello ") # if __name__=='__main__': # A().run() for _ in range(int(input())): a,b,c=map(int,input().split()) d=abs(a-b) n=2*d if c>n or a>n or b>n: print(-1) print() continue if c-d>0: print(c-d) else: print(c+d) print()
800
PYTHON3
#include <bits/stdc++.h> char* concat(char* str1, char* str2) { char* str3 = (char*)malloc(1 + strlen(str1) + strlen(str2)); strcpy(str3, str1); strcat(str3, str2); return str3; } char* intToString(int i) { char* ans = (char*)malloc(65); sprintf(ans, "%d", i); return ans; } int stringToInt(char* str) { return atoi(str); } int isChanged(char c, char last) { if (c >= 48 && c <= 57 && last >= 65 && last <= 90) return 1; if (c >= 65 && c <= 90 && last >= 48 && last <= 57) return 1; return 0; } char* codeProb(int num) { char* cad = ""; char temp[2]; temp[1] = '\0'; while (num >= 27) { if (num % 26 == 0) { num = num / 26 - 1; cad = concat("Z", cad); } else { temp[0] = num % 26 + 64; cad = concat(temp, cad); num = num / 26; } } if (num > 0 && num < 27) { temp[0] = num + 64; cad = concat(temp, cad); } return cad; } void printCodeChange(char* str) { int i, j = 0, len = strlen(str); char last = 65, arrStr[4][10]; for (i = 0; i < 4; i++) { arrStr[i][0] = '\0'; } char temp[2]; temp[1] = '\0'; for (i = 0; i < len; i++) { if (isChanged(str[i], last)) { j++; } temp[0] = str[i]; strcat(arrStr[j], temp); last = str[i]; } if (arrStr[2][0] == '\0') { int num = 0, pot = 1; len = strlen(arrStr[0]); for (i = len - 1; i >= 0; i--) { num += (arrStr[0][i] - 64) * pot; pot *= 26; } printf("R%sC%d\n", arrStr[1], num); } else { char* code; int num = stringToInt(arrStr[3]); code = codeProb(num); printf("%s%s\n", code, arrStr[1]); } } int main() { int i, n; scanf("%d", &n); char arrStr[n][30]; for (i = 0; i < n; i++) { scanf("%s", arrStr[i]); } for (i = 0; i < n; i++) { printCodeChange(arrStr[i]); } return 0; }
1,600
CPP
t=int(input()) for jkl in range(t): n=int(input()) a=list(map(int, input().split())) ch=0 nc=0 for i in range(n): if a[i]%2==0: ch+=1 else: nc+=1 if n%2==0 and ch!=0 and nc!=0: print('YES') elif nc==0: print('NO') elif ch==0 and n%2!=0: print('YES') elif ch==0 and n%2==0: print('NO') else: print('YES')
800
PYTHON3
n,k = map(int,input().split()) c=0 lista=list(map(int,input().split())) for i in lista: if (5-i >=k): c+=1 print(c//3)
800
PYTHON3
import sys import os from math import* input=sys.stdin.buffer.readline n=int(input()) arr=list(map(int,input().split())) arr.sort() if arr[0]!=1: arr=[1]+arr[0:n-1] elif arr[0]==1 and arr[-1]==1: arr[-1]=2 else: for i in range(n): if arr[i]!=1: arr=arr[0:i]+[1]+arr[i:n-1] break for x in arr: print(x,end=' ')
1,300
PYTHON3
def main(): try: weight = int(input()) except Exception as e: print(str(e)) if weight >= 1 and weight <= 100: if weight % 2 == 0: for i in range(1, weight + 1): if i % 2 == 0 and not i == 2: print("YES") break if weight == 2: print("NO") break else: print("NO") else: print("> 1 and < 100") if __name__ == "__main__": main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; map<char, vector<int> > A, B; int main(int argc, char** argv) { int N; cin >> N; string a, b; cin >> a >> b; int idx = 1; for (const auto& c : a) { if (!A.count(c)) A[c] = vector<int>(); A[c].push_back(idx); idx++; } idx = 1; for (const auto& c : b) { if (!B.count(c)) B[c] = vector<int>(); B[c].push_back(idx); idx++; } vector<pair<int, int> > res; for (char i = 'a'; i <= 'z'; ++i) { if (A.count(i) && B.count(i)) { auto& aa = A[i]; auto& bb = B[i]; while (aa.size() > 0 && bb.size() > 0) { res.push_back(make_pair(aa.back(), bb.back())); aa.pop_back(); bb.pop_back(); } } } if (A.count('?')) { auto& aa = A['?']; for (char i = 'a'; i <= 'z'; ++i) { if (B.count(i)) { auto& bb = B[i]; while (bb.size() > 0 && aa.size() > 0) { res.push_back(make_pair(aa.back(), bb.back())); aa.pop_back(); bb.pop_back(); } } } } if (B.count('?')) { auto& bb = B['?']; for (char i = 'a'; i <= 'z'; ++i) { if (A.count(i)) { auto& aa = A[i]; while (aa.size() > 0 && bb.size() > 0) { res.push_back(make_pair(aa.back(), bb.back())); bb.pop_back(); aa.pop_back(); } } } } if (A.count('?') && B.count('?')) { int z = min(A['?'].size(), B['?'].size()); for (int i = (0); i < (z); ++i) { res.push_back(make_pair(A['?'].back(), B['?'].back())); A['?'].pop_back(); B['?'].pop_back(); } } printf("%d\n", (int)res.size()); for (const auto& pr : res) { printf("%d %d\n", pr.first, pr.second); } return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") int ah = 911382323; int bh = 972663749; template <typename F, typename S> ostream &operator<<(ostream &os, const pair<F, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; typename vector<T>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } return os << "}"; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; typename set<T>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename F, typename S> ostream &operator<<(ostream &os, const map<F, S> &v) { os << "["; typename map<F, S>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } long long dx[8] = {1, -1, 0, 0, 1, 1, -1, -1}; long long dy[8] = {0, 0, 1, -1, 1, -1, 1, -1}; int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long(t); cin >> (t); while ((t)--) { long long n; cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; long long ind = 0; while (ind < n && v[ind] == 1) ind++; if (ind == n) { if (n % 2) cout << ("First") << "\n"; else cout << ("Second") << "\n"; } else { if (ind % 2 == 0) cout << ("First") << "\n"; else cout << ("Second") << "\n"; } } }
1,100
CPP
#include <bits/stdc++.h> using namespace std; long long c[1005], d[1005][1005], s[1005], dp[1005]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> c[i]; s[0] = 1; for (int i = 1; i <= 1000; ++i) { d[i][1] = 1; s[i] = s[i - 1] + 1; } for (int j = 2; j <= 1000; ++j) { d[0][j] = 1; for (int i = 1; i <= 1000; ++i) { d[i][j] = s[i]; s[i] = (s[i - 1] + d[i][j]) % 1000000007; } } long long sum = c[0]; dp[0] = 1; for (int i = 1; i < n; ++i) { dp[i] = (dp[i - 1] * d[c[i] - 1][sum + 1]) % 1000000007; sum += c[i]; } cout << dp[n - 1] % 1000000007; }
1,500
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 lcm(long long a, long long b) { return (a * (b / gcd(a, b))); } bool getbit(int num, int idx) { return (num >> idx & 1) == 1; } int setbit(int num, int idx, bool val) { if (val == true) return (num | (1 << idx)); return num & ~(1 << idx); } int flipbit(int num, int idx) { return num ^ (1 << idx); } int cntbits(int mask) { int res = 0; while (mask) mask &= (mask - 1), res++; return res; } int n, m, offer[(int)1e5 + 10], buy[int(1e5) + 10]; int main() { long long sum = 0; cin >> m; for (int i = 0; i < int(m); ++i) scanf("%d", &offer[i]); cin >> n; for (int i = 0; i < int(n); ++i) scanf("%d", &buy[i]); int idx = n - 1; sort(buy, buy + n); sort(offer, offer + m); while (idx >= 0) { if (idx - offer[0] + 1 >= 0) { for (int i = offer[0]; i > 0; i--) sum += buy[idx--]; idx -= 2; } else for (; idx >= 0; idx--) sum += buy[idx]; } cout << sum << endl; return 0; }
1,400
CPP
p = 31 q = 1000000007 p0 = 27 q0 = 2147483647 class hstr: def __init__(self, s): self.h = [0] self.pq = [1] self.h0 = [0] self.pq0 = [1] n = len(s) for i in range(n): self.h += [(self.h[i] * p + ord(s[i])) % q] self.pq += [(self.pq[i] * p) % q] self.h0 += [(self.h0[i] * p0 + ord(s[i])) % q0] self.pq0 += [(self.pq0[i] * p0) % q0] def get(self, l, r): return ((self.h[r] - self.h[l] * self.pq[r - l]) % q + q) % q def get0(self, l, r): return ((self.h0[r] - self.h0[l] * self.pq0[r - l]) % q0 + q0) % q0 from sys import stdout from sys import stdin def get(): return stdin.readline().strip() def getf(): return [int(i) for i in get().split()] def put(a, end = "\n"): stdout.write(str(a) + end) def putf(a, sep = " ", end = "\n"): stdout.write(sep.join([str(i) for i in a]) + end) def main(): s = get() a = get() h = hstr(s) k = int(get()) r = [] for i in a: if(i == "1"): r += [0] else: r += [1] ns = [0] for i in s: ns += [r[ord(i) - 97] + ns[-1]] st = set() ans = 0 n = len(s) for i in range(n): for j in range(i + 1, n + 1): res = h.get(i, j) * h.get0(i, j) if(res not in st): st.add(res) if(ns[j] - ns[i] <= k): ans += 1 put(ans) main()
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long modd = (1000LL * 1000LL * 1000LL + 7LL); template <class T> T fastpower(T x, long long pw, T id = 1) { T w = x, res = id; while (pw > 0) { if (pw & 1) { res = (res * w); } pw >>= 1; w = (w * w); } return res; } class Mint { friend ostream& operator<<(ostream& o, const Mint& b); public: Mint() : val(0) {} Mint(long long x) { if (x < 0) { x += (1 + abs(x) / modd) * modd; } val = x % modd; } Mint operator+(const Mint& b) const { return Mint((val + b.val) % modd); } Mint operator-(const Mint& b) const { return Mint((val + modd - b.val) % modd); } Mint operator*(const Mint& b) const { return Mint((val * b.val) % modd); } Mint operator/(const Mint& b) const { return Mint(*this * fastpower<Mint>(b, modd - 2)); } Mint operator-() const { return Mint((val * (modd - 1)) % modd); } Mint& operator+=(const Mint& b) { val += b.val; val %= modd; return *this; } Mint& operator-=(const Mint& b) { val += modd - b.val; val %= modd; return *this; } Mint& operator*=(const Mint& b) { val *= b.val; val %= modd; return *this; } Mint& operator/=(const Mint& b) { val = (Mint(val) / b).val; return *this; } Mint& operator++() { ++val; return *this; } Mint operator++(int) { Mint old = *this; operator++(); return old; } Mint& operator--() { --val; return *this; } Mint operator--(int) { Mint old = *this; operator--(); return old; } bool operator==(const Mint& b) const { return val == b.val; } private: long long val; }; ostream& operator<<(ostream& o, const Mint& b) { return (o << b.val); } class Factorial { public: vector<Mint> bin; Factorial(int n) : bin(n + 1, 0) { bin[0] = 1; for (int i = (1); i < (1) + (n); ++i) { bin[i] = Mint(i) * bin[i - 1]; } } Mint val(int i) { return bin[i]; } }; class BinomialWithFactorial { public: Factorial fact; vector<Mint> inverse_fact; BinomialWithFactorial(int n) : fact(n) { FillInverse(); } BinomialWithFactorial(Factorial& f) : fact(f) { FillInverse(); } void FillInverse() { for (int i = (0); i < (0) + (fact.bin.size()); ++i) { inverse_fact.push_back(Mint(1) / fact.val(i)); } } Mint val(int i, int j) { if (i < 0) { return 0; } if ((j < 0) || (j > i)) { return 0; } Mint temp = (fact.val(i) * inverse_fact[j]); return (temp * inverse_fact[i - j]); } }; int main(int argc, char* argv[]) { mt19937 rang(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen(0, modd); ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(12); int test_cases = 1; for (int t = (1); t < (1) + (test_cases); ++t) { int n; cin >> n; int q; cin >> q; BinomialWithFactorial bin(3 * n + 5); vector<Mint> a(3 * n + 4); for (int i = (0); i < (0) + (a.size()); ++i) { a[i] += bin.val(3 * n + 3, i + 1); a[i] -= bin.val(3, i + 1); if (i - 1 >= 0) a[i] -= Mint(3) * a[i - 1]; if (i - 2 >= 0) a[i] -= a[i - 2]; a[i] /= Mint(3); } for (int i = (0); i < (0) + (q); ++i) { int x; cin >> x; cout << a[x] << "\n"; } } return 0; }
2,500
CPP
t = int(input()) for test in range(t): n = int(input()) line = input().split() permutation = [0] * n for i in range(n): permutation[i] = int(line[i]) memo = [-1] * n for i in range(n): if memo[i] != -1: print(memo[memo[i]], end = " ") else: counter = 1 current = i while permutation[current] - 1 != i: current = permutation[current] - 1 memo[current] = i counter += 1 memo[i] = counter print(counter, end = " ") print()
1,000
PYTHON3
cases = int(input()) tests = [] results = [] for i in range(cases): tests.append([int(item) for item in input().split()]) def bs(a, b): left, right = 0, a while left <= right: mid = (left + right)//2 v1 = a - mid v2 = b - 2 * mid if v1 == 2 * v2: return True elif v1 > 2 * v2: right = mid - 1 else: left = mid + 1 return False for test in tests: # #[0] + 2 * [1] = test[0] # #2 * [0] + [1] = test[1] # #[0] + [1] = test[0] + test[1] # #[0] = test[0] - total # if sum(test) % 3 != 0: # results.append("NO") # else: # total = sum(test) // 3 # op1 = test[0] - total # op2 = test[1] - total # if op1 >= 0: # if op2 >= 0: # results.append("YES") # else: # results.append("NO") # else: # results.append("NO") if bs(test[0], test[1]): results.append("YES") else: results.append("NO") for result in results: print(result)
1,300
PYTHON3
# -*- coding: utf-8 -*- """ Created on Wed Oct 23 14:20:41 2019 @author: sihan """ str=input() print(str[0].capitalize()+str[1:])
800
PYTHON3
def df(n): if n&1: return 0 return 1<<(n>>1) print(df(int(input())))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> d; int main() { int n, pos[100069] = {}, ans[100069] = {}, maxi = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); ans[x] = ans[x - 1] + 1; maxi = max(maxi, ans[x]); } cout << n - maxi << endl; return 0; }
1,600
CPP
a,b=map(int,input().split()) l=list(map(int,input().split())) k=0 c=0 s=0 for i in range(len(l)): s=s + l[i] if s<=b: c=c+1 else: s=s-l[k] k=k+1 print(c)
1,400
PYTHON3
from sys import stdin s = stdin.readline().strip().lstrip('0') def toExp(s): if len(s) <= 0: print(0) return l = s.split('.') if len(l) == 1: # No '.' occurs num = l[0] e = len(num[1 : ]) if len(num) <= 0: # 0 print(0) if len(num) == 1: # 1 ~ 9 print(num) else: if len(num[1 : ].strip('0')) == 0: # 100...0 print(num[0] + 'E' + str(e)) else: # xxxx print(num[0] + '.' + num[1 : ].rstrip('0') + 'E' + str(e)) else: # len(l) == 2 num1, num2 = l[0], l[1].rstrip('0') num = num2.lstrip('0') if len(num1) == 0: # .xxx e = len(num2) - len(num) + 1 if len(num) == 0: print(0) elif len(num) == 1: print(num + 'E-' + str(e)) else: print(num[0] + '.' + num[1 : ].rstrip('0') + 'E-' + str(e)) elif len(num) == 0: # xxx. num = l[0] e = len(num[1 : ]) if len(num) <= 0: # 0 print(0) if len(num) == 1: # 1 ~ 9 print(num) else: if len(num[1 : ].strip('0')) == 0: # 100...0 print(num[0] + 'E' + str(e)) else: # xxxx print(num[0] + '.' + num[1 : ].rstrip('0') + 'E' + str(e)) else: # xxx.xxx e = len(num1) - 1 if e > 0: print(num1[0] + '.' + num1[1 : ] + num2 + 'E' + str(e)) else: print(num1 + '.' + num2) toExp(s)
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; const double eps = 1e-6; const int mod = 998244353; const int maxn = 2e6 + 100; const int maxm = 2e6 + 100; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); int t; int n; struct node { int x, y; } a[maxn]; map<int, int> ma; multiset<int> v[maxn]; multiset<int> s; bool cmp(node a, node b) { return a.x < b.x; } long long S[maxn]; int X[maxn]; int main() { long long ans = 0; scanf("%d", &n); int tot = 0; int bg = 0x3f3f3f3f; 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 + 1 + n, cmp); for (int i = 1; i <= n; i++) { bg = min(bg, a[i].x); if (!ma[a[i].x]) { ma[a[i].x] = ++tot; X[tot] = a[i].x; } v[ma[a[i].x]].insert(a[i].y); S[ma[a[i].x]] += a[i].y; } int now = bg; long long sum = 0; for (int i = 1; i <= tot; i++) { int x = X[i]; while (now < x) { if (s.empty()) { now = x; break; } auto it = s.end(); it--; s.erase(it); sum -= (*it); ans += sum; now++; } if (x == now) { if (v[i].size() > s.size()) { v[i].swap(s); sum = S[i]; } for (auto it = v[i].begin(); it != v[i].end(); it++) { s.insert(*it); sum += (*it); } v[i].clear(); auto it = s.end(); it--; sum -= (*it); s.erase(it); ans += sum; now++; continue; } } while (!s.empty()) { auto it = s.end(); it--; sum -= (*it); s.erase(it); ans += sum; now++; } printf("%lld", ans); return 0; }
1,700
CPP
# https://codeforces.com/problemset/problem/158/B n = int(input()) S = [int(x) for x in input().split(" ")] A = [0,0,0,0] for x in S: A[x-1] += 1 taxi = A[3] + A[1] // 2 A[3] = 0 A[1] = A[1] % 2 if A[2] >= A[0]: taxi += A[2] A[0] = 0 A[2] = 0 else: taxi += A[2] A[0] -= A[2] A[2] = 0 if A[0]: taxi += A[0] // 4 A[0] = A[0] % 4 if A[1]: if A[0] == 3: taxi += 2 else: taxi += 1 A[1] = 0 A[0] = 0 elif A[0]: taxi += 1 print(taxi)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long double EPS = 1e-9; long long cel(long long a, long long b) { return ((a - 1) / b + 1); } long long gcd(long long a, long long b) { if (a < b) swap(a, b); return (b == 0) ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long po(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) { ans = (ans * x) % 1000000007; } y >>= 1; x = (x * x) % 1000000007; } return ans; } const int N = 100001; vector<int> vis(N, 0), pus(N, 0), ans; bool ok = true; vector<int> adj[N]; void dfs(int u) { if (vis[u] == 1 && (pus[u] != 1)) { ok = false; return; } if (pus[u] == 1) { return; } vis[u] = 1; for (int t : adj[u]) { dfs(t); } ans.push_back(u); pus[u] = 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, t, x, y, m, k; cin >> n >> k; vector<int> chk(k); for (int i = 0; i < k; i++) { cin >> chk[i]; } for (int i = 1; i < n + 1; i++) { cin >> x; for (int j = 0; j < x; j++) { cin >> y; adj[i].push_back(y); } } for (int i = 0; i < k; i++) { dfs(chk[i]); if (!ok) { break; } } if (!ok) cout << -1; else { cout << ans.size() << "\n"; for (int t : ans) cout << t << " "; } return 0; }
1,500
CPP
X=list(map(int, input().split())) l,b,a = X[0],X[1],X[2] l1 = (l//a) if(l%a!=0): l1+=1 b1 = (b//a) if(b%a!=0): b1+=1 ans = l1*b1 print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; const long double eps = 1e-7; int t, n, q, k, s, op, x[N], y[N], xa[N], ya[N]; struct node { long double x, y; } e[N], f[N]; struct vec { long double a, b, c, d; } w[N], g[N], h[N]; struct vat { int x, y; } gt[N]; inline int read() { int num = 0; char t = '+', g = getchar(); while (g < 48 || 57 < g) t = g, g = getchar(); while (47 < g && g < 58) num = num * 10 + g - 48, g = getchar(); if (t == '-') return -num; return num; } inline bool cmp(node a, node b) { return atan2(a.y, a.x) < atan2(b.y, b.x); } inline bool dmp(node a, node b) { return atan2(a.y, a.x) > atan2(b.y, b.x); } inline bool tmp(vec a, vec b) { return atan2(a.d - a.b, a.c - a.a) < atan2(b.d - b.b, b.c - b.a); } inline bool qmp(vat a, vat b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } inline long double getvac(node a, node b) { return a.x * b.y - a.y * b.x; } inline void adds(node a, node b) { swap(a, b); q++, w[q].a = a.x + x[1], w[q].b = a.y + y[1], w[q].c = b.x + x[1], w[q].d = b.y + y[1]; } inline bool eps_check(long double u) { if (-eps <= u && u <= eps) return 1; return 0; } inline bool node_check(vec u, long double x, long double y) { node a, b; a.x = u.c - u.a, a.y = u.d - u.b, b.x = x - u.a, b.y = y - u.b; return (getvac(a, b) >= -eps); } inline node getnode(vec a, vec b) { node z; long double f1 = (a.d - a.b) * (b.c - b.a), f2 = (b.d - b.b) * (a.c - a.a), f3 = (a.b - b.b) * (b.c - b.a) * (a.c - a.a); f3 = f3 - f1 * a.a + f2 * b.a; z.x = f3 / (f2 - f1); if (eps_check(a.a - a.c)) swap(a, b); z.y = (z.x - a.a) * (a.d - a.b) / (a.c - a.a) + a.b; return z; } inline bool check(vec a, vec b, vec c) { node z = getnode(b, c); return node_check(a, z.x, z.y); } inline bool getvat(node a, node b) { a.x -= b.x, a.y -= b.y, b.x = -b.x, b.y = -b.y; return (getvac(a, b) <= eps); } inline bool getvas(node a, node b) { a.x -= b.x, a.y -= b.y, b.x = -b.x, b.y = -b.y; return (getvac(a, b) <= -eps); } inline int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int main() { t = read(); bool og = 0; while (t--) { n = read(), q = 0, k = 0, op = 0; bool fl = 0; for (int i = 1; i <= n; i++) xa[i] = x[i] = read(), ya[i] = y[i] = read(); for (int i = 2; i <= n; i++) x[i] = x[i] - x[1], y[i] = y[i] - y[1], e[i - 1].x = x[i], e[i - 1].y = y[i]; n--; for (int i = 2; i <= n + 1; i++) { int z = gcd(abs(x[i]), abs(y[i])); x[i] /= z, y[i] /= z; if (x[i] < 0) x[i] = -x[i], y[i] = -y[i]; if (x[i] == 0) y[i] = 1; if (y[i] == 0) x[i] = 1; op++, gt[op].x = x[i], gt[op].y = y[i]; } sort(gt + 1, gt + op + 1, qmp); for (int i = 2; i <= op; i++) if (gt[i - 1].x == gt[i].x && gt[i - 1].y == gt[i].y) fl = 1; sort(e + 1, e + n + 1, cmp); for (int i = 1; i <= n; i++) e[i + n] = e[i]; int j = 1; for (int i = 1; i <= n; i++) { j = max(j, i + 1); while (j <= i + n - 2 && getvat(e[i], e[j + 1])) j++; if (getvat(e[i], e[j])) adds(e[i], e[j]); if (getvat(e[i], e[i + 1])) adds(e[i], e[i + 1]); } q++, w[q].a = -1000000, w[q].b = 1000000, w[q].c = 1000000, w[q].d = 1000000; q++, w[q].a = 1000000, w[q].b = 1000000, w[q].c = 1000000, w[q].d = -1000000; q++, w[q].a = 1000000, w[q].b = -1000000, w[q].c = -1000000, w[q].d = -1000000; q++, w[q].a = -1000000, w[q].b = -1000000, w[q].c = -1000000, w[q].d = 1000000; sort(w + 1, w + q + 1, tmp), g[1] = w[1], k = 1; for (int i = 2; i <= q; i++) { if (eps_check(atan2(w[i].d - w[i].b, w[i].c - w[i].a) - atan2(g[k].d - g[k].b, g[k].c - g[k].a))) { if (node_check(w[i], g[k].a, g[k].b)) g[k] = w[i]; } else g[++k] = w[i]; } int l = 1, r = 2; for (int i = 1; i <= 2; i++) h[i] = g[i]; for (int i = 3; i <= k; i++) { if (i == 3) { node a, b; a = getnode(h[1], h[2]); b = getnode(h[2], h[1]); } while (r - l >= 1 && check(g[i], h[r], h[r - 1])) r--; while (r - l >= 1 && check(g[i], h[l], h[l + 1])) l++; h[++r] = g[i]; } while (r - l >= 2 && check(h[l], h[r], h[r - 1])) r--; if (r - l <= 1 || fl) { if (!og) printf("0\n"); } else { s = 0; long double ans = 0; for (int i = l; i < r; i++) f[++s] = getnode(h[i], h[i + 1]); f[++s] = getnode(h[l], h[r]); for (int i = 2; i <= s; i++) f[i].y -= f[1].y, f[i].x -= f[1].x; for (int i = 2; i < s; i++) ans = ans + abs(getvac(f[i], f[i + 1])) / 2; printf("%.7Lf\n", ans); } } }
3,300
CPP
#include<bits/stdc++.h> #define int long long using namespace std; int n,q,k,a[100005],f[100005],s[100005]; void solve() { scanf("%lld%lld%lld",&n,&q,&k); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<n;i++) f[i]=a[i+1]-a[i-1]-2; f[n]=k-a[n-1]-2; for(int i=1;i<=n;i++) s[i]=s[i-1]+f[i]; //for(int i=1;i<=n;i++) cout<<s[i]<<" "; //puts(""); while(q--) { int l,r; scanf("%lld%lld",&l,&r); if(l==r) printf("%lld\n",k-1); else { if(r==l+1) { printf("%lld\n",max(0LL,a[l+1]-2+k-a[r-1]-1)); } else printf("%lld\n",max(0LL,a[l+1]-2+k-a[r-1]-1+s[r-1]-s[l])); } } } signed main() { int t=1; //cin>>t; while(t--) solve(); return 0; }
1,200
CPP
MOD = 1000000007 from collections import defaultdict as dd, Counter, deque def si(): return input() def ii(): return int(input()) def li(): return list(map(int, input().split())) def mi(): return map(int, input().split()) def out(v): print(v) def spout(): print(v, end=" ") def d2b(n): return bin(n).replace("0b", "") def twod(n, m, num): return [[num for x in range(m)] for y in range(n)] def vow(): return ['a', 'e', 'i', 'o', 'u'] def let(): return [chr(i) for i in range(97, 123)] def gcd(x, y): while y: x, y = y, x % y return x def ispow2(x): return (x and (not (x & (x - 1)))) def prime_factors(n): i = 2 factors = [] while i * i <= n: if n % i: i += 1 else: n //= i factors.append(i) if n > 1: factors.append(n) return (list(factors)) t=ii() while t: t-=1 a,b,c=mi() print(a+b+c-1)
800
PYTHON3
n, k = map(int, input().split()) if k == 1: print("1", end = "") for i in range(1, n): print("0", end = "") print() exit() a = (n + k - 2) // 2 ans = "" if (a - k + 2) % 2: for i in range (a - k + 2): ans += chr(i % 2 + 48) else: ans += chr(48) for i in range(1, a - k + 2): ans += chr(49) for i in range(a - k + 2, n): ans += ans[i - a + k - 2] print(ans)
2,200
PYTHON3
n = int(input()) s = 0 for i in range(n): p, q = map(int, input().split()) if q-p>=2: s += 1 print(s)
800
PYTHON3
k = int(input()) a = [int(x) for x in input().split()] for i in range(k): if(a[i] % 2 == 0): a[i] -= 1 for i in range(k): print(a[i], end = ' ')
800
PYTHON3
import math,sys;input=sys.stdin.readline;S=lambda:input().rstrip();I=lambda:int(S());M=lambda:map(int,S().split());L=lambda:list(M());mod1=1000000007;mod2=998244353 q=I();l=[];ans=[] for i in range(q):l.append(L()) l.reverse();d=[i for i in range(500001)] for j in l: if j[0]==1:ans.append(d[j[1]]) else: d[j[1]]=d[j[2]] print(*ans[::-1])
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; int N, M, sum[501][501], row[507][507], clm[501][501]; char c[507][507]; int main() { cin >> N >> M; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { row[i][j] = row[i][j - 1]; cin >> c[i][j]; if (c[i][j] == '.' && c[i][j - 1] == '.' && j) { row[i][j]++; } } for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { clm[j][i] = clm[j - 1][i]; if (j && c[j][i] == '.' && c[j - 1][i] == '.') { clm[j][i]++; } } } long long T, ans = 0; cin >> T; while (T--) { ans = 0; int x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; x1--; y1--; x2--; y2--; for (int i = y1; i <= y2; i++) { ans += clm[x2][i] - clm[x1][i]; } for (int i = x1; i <= x2; i++) { ans += row[i][y2] - row[i][y1]; } cout << ans << endl; } return 0; }
1,500
CPP
class A: def solve(self): map_dict = {} letters = "abcdefgh" index = 1 for l in letters: map_dict[l] = index index += 1 [sx, sy] = list(input()) [tx, ty] = list(input()) sx = map_dict[sx] tx = map_dict[tx] sy, ty = int(sy), int(ty) diffx, diffy = abs(sx - tx), abs(sy - ty) diagonal_steps = min(diffx, diffy) perp_steps = max(diffx, diffy) - diagonal_steps print(diagonal_steps + perp_steps) vert_step = "" hor_step = "" if sx < tx: hor_step = "R" else: hor_step = "L" if sy < ty: vert_step = "U" else: vert_step = "D" diagonal_path = [hor_step + vert_step for x in range(diagonal_steps)] if diagonal_steps > 0: print("\n".join(diagonal_path)) if diagonal_steps == diffx: if sy < ty: vert_step = "U" else: vert_step = "D" if perp_steps > 0: print("\n".join([vert_step for x in range(perp_steps)])) else: if sx < tx: hor_step = "R" else: hor_step = "L" if perp_steps > 0: print("\n".join([hor_step for x in range(perp_steps)])) A().solve()
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; #define dbg(x...) \ do { \ cout << #x << " -> "; \ err(x); \ } while (0) void err() { cout << endl; } template<class T, class... Ts> void err(T arg, Ts &... args) { cout << arg << ' '; err(args...); } void read() {} template <class T, class... Ts> void read(T &arg, Ts &... args) { T f = 1; char ch; arg = 0; for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) { if (ch == '-') f = -1; } for (; ch >= '0' && ch <= '9'; ch = getchar()) arg = arg * 10 + ch - '0'; arg *= f; read(args...); } template <class T> void myHash(T a[], int n, T w[] = nullptr) { set<T> st; for (int i = 0; i < n; i++) { st.insert(a[i]); } int tot = 0; map<T, int> mp; for (T x : st) { mp[x] = ++tot; } for (int i = 0; i < n; i++) { a[i] = mp[a[i]]; } if (w != nullptr) { for (pair<T, int> p : mp) { w[p.second] = p.first; } } } typedef long long ll; typedef pair<int, int> pii; const int maxn = 1e5 + 7; const int inf = 0x3f3f3f3f; const int mod = 998244353; int a[maxn]; bool check(int n, int m, int mx) { int x1 = n / 2; int x2 = (n + 1) / 2; return m <= n * n - x1 * x1 && mx <= n * x2; } int f[1005][1005]; struct Cell { int x, y, type; bool operator < (const Cell &tmp) const { return type < tmp.type; } }; void run() { int m, k; read(m, k); int mx = 0; for (int i = 1; i <= k; i++) { read(a[i]); mx = max(mx, a[i]); } int l = 1, r = 1000; int n = 0; while (l <= r) { int mid = l + r >> 1; if (check(mid, m, mx)) { n = mid; r = mid - 1; } else { l = mid + 1; } } printf("%d\n", n); vector<pii> vec; for (int i = 1; i <= k; i++) { vec.emplace_back(a[i], i); } sort(vec.rbegin(), vec.rend()); vector<Cell> cell; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { f[i][j] = 0; if (i % 2 == 0 && j % 2 == 0) continue; if (j % 2 == 0) { cell.push_back({i, j, 1}); } else if (i & 1) { cell.push_back({i, j, 2}); } else { cell.push_back({i, j, 3}); } } } sort(cell.begin(), cell.end()); int cur = 0; for (pii p : vec) { int x = p.second; int cnt = p.first; while (cnt--) { Cell pos = cell[cur]; cur++; f[pos.x][pos.y] = x; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { printf("%d%c", f[i][j], j == n ? '\n' : ' '); } } } /* 5 1 2 3 4 */ int main() { int T = 1; read(T); while (T--) { run(); } return 0; }
2,700
CPP
a, b = map(int, input().split()) result=0 for i in range(1, a+1): if b%i==0 and b<=i*a: result+=1 print(result)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100005; struct node { long long best, l, r, sum; node() { best = 0; l = 0; r = 0; sum = 0; } node(long long a, long long b) { best = 0; l = a + 2 * b; r = 2 * b; sum = a; } }; vector<long long> d(N, 0); vector<long long> h(N, 0); vector<node> tree(4 * N, node()); node operator+(const node &a, const node &b) { if (a.l == 0) return b; if (b.l == 0) return a; node c; c.best = max(max(a.best, b.best), a.l + b.r); c.l = max(b.l, a.l + b.sum); c.r = max(a.r, a.sum + b.r); c.sum = a.sum + b.sum; return c; } void build(int idx, int l, int r) { if (l == r) tree[idx] = node(d[l], h[l]); else { int m = (l + r) / 2; build(2 * idx, l, m); build(2 * idx + 1, m + 1, r); tree[idx] = tree[2 * idx] + tree[2 * idx + 1]; } } node query(int idx, int l, int r, int ql, int qr) { if (ql > qr) return node(); if (l == ql && r == qr) return tree[idx]; int m = (l + r) / 2; return query(2 * idx, l, m, ql, min(qr, m)) + query(2 * idx + 1, m + 1, r, max(ql, m + 1), qr); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> d[i]; for (int i = 1; i <= n; i++) cin >> h[i]; build(1, 1, n); while (m--) { int a, b; cin >> a >> b; if (a > b) cout << query(1, 1, n, b + 1, a - 1).best << "\n"; else cout << (query(1, 1, n, b + 1, n) + query(1, 1, n, 1, a - 1)).best << "\n"; } return 0; }
2,300
CPP
#!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def main(): n = int(input()) a = [int(i) for i in input().split()] p = 0 n = 0 z = 0 c = 0 for i in a: if i > 0: p += 1 elif i < 0: n += 1 else: z += 1 c += abs(abs(i) - 1) if n % 2 and z == 0: c += 2 print(c) # 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
900
PYTHON3
arr=[] for i in range(int(input())): s=input() if len(s)<=10: arr.append(s) else: s1=s[0]+str(len(s)-2)+s[-1] arr.append(s1) for i in range(len(arr)): print(arr[i])
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, s, t, h = 0, g = 0; int a[100]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cin >> s >> t; if (s == t) cout << 0; else { if (s > t) { for (int i = t - 1; i < s - 1; i++) g += a[i]; if (t == 1) { for (int i = n - 1; i >= s - 1; i--) h += a[i]; if (g < h) cout << g; else cout << h; } else { for (int i = t - 2; i >= 0; i--) h += a[i]; for (int i = n - 1; i >= s - 1; i--) h += a[i]; if (g < h) cout << g; else cout << h; } } else if (t > s) { for (int i = s - 1; i < t - 1; i++) g += a[i]; if (s == 1) { for (int i = n - 1; i >= t - 1; i--) h += a[i]; if (g < h) cout << g; else cout << h; } else { for (int i = s - 2; i >= 0; i--) h += a[i]; for (int i = n - 1; i >= t - 1; i--) h += a[i]; if (g < h) cout << g; else cout << h; } } } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int dx[9] = {0, 1, -1, 0, 0, -1, -1, 1, 1}; const int dy[9] = {0, 0, 0, -1, 1, -1, 1, -1, 1}; const double pi = acos(-1.0); const int N = 3e5; int n, k; pair<long long, int> c[N + 10]; set<int> myset; int ans[N + 10]; long long temp = 0; int main() { scanf("%d", &n), scanf("%d", &k); for (int i = 1; i <= n; i++) { long long x; scanf("%lld", &x); c[i].first = x, c[i].second = i; } for (int i = 1; i <= n; i++) temp -= (1LL * i * c[i].first); for (int i = 1; i <= n; i++) { myset.insert(k + i); } sort(c + 1, c + 1 + n); for (int i = n; i >= 1; i--) { int idx = c[i].second; auto it = myset.lower_bound(idx); int ju = (*it); temp += 1LL * (*it) * c[i].first; ans[c[i].second] = (*it); myset.erase(it); } printf("%lld", temp), puts(""); for (int i = 1; i <= n; i++) printf("%d", ans[i]), putchar(' '); return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const long long INF = 1e18; const int mod = 1e9 + 7; const int maxn = 1e5 + 2; const int mov[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1}; const int Mov[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1}; inline int 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 read(int &x) { x = 0; int 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(); } x *= f; return; } int mh = 0; int mw = 0; void add() { int h, w; cin >> h >> w; mh = max(mh, max(h, w)); mw = max(mw, min(h, w)); } void query() { int h, w; cin >> h >> w; if (mh <= max(h, w) && mw <= min(h, w)) { cout << "YES" << endl; } else cout << "NO" << endl; } int main(void) { std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int q; cin >> q; char op; while (q--) { cin >> op; switch (op) { case '+': add(); break; default: query(); } } }
1,500
CPP
from sys import stdin def solve(n, a, b): abc = "abcdefghijklmnopqrstuvwxyz" ans = abc[:b]*(n//b) ans+=abc[:n%b] return ans def main(): r = stdin.readline cases = int(r()) for case in range(cases): n, a, b = map(int, r().strip().split()) print(solve(n, a, b)) main()
900
PYTHON3
#------------------------------what is this I don't know....just makes my mess faster-------------------------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #----------------------------------Real game starts here-------------------------------------- ''' ___________________THIS IS AESTROIX CODE________________________ KARMANYA GUPTA ''' for i in range(int(input())): n,s,k = list(map(int, input().split())) closed = list(map(int, input().split())) for i in range(0, k+1): if s-i >= 1 and s-i not in closed: print(i) break elif s+i <= n and s+i not in closed: print(i) break
1,100
PYTHON3
import sys input = sys.stdin.readline def getInt(): return int(input()) def getVars(): return map(int, input().split()) def getList(): return list(map(int, input().split())) def getStr(): return input().strip() ## ------------------------------- t = getInt() for _ in range(t): n = getInt() s = getStr() d = {} d['0/0'] = 0 x = 0 y = 0 res = [0, n+1] for i in range(n): if s[i] == 'L': x -= 1 if s[i] == 'R': x += 1 if s[i] == 'U': y += 1 if s[i] == 'D': y -= 1 k = str(x) + '/' + str(y) if k not in d: d[k] = i+1 else: if i+1 - d[k] < res[1] - res[0]: res = [d[k]+1, i+1] d[k] = i+1 if res[1] == n+1: print(-1) else: print(*res)
1,500
PYTHON3
testCase = int(input()) result = 0 for i in range(testCase): number = int(input()) result = ((number%10)-1)*10 for i in range (1,len(str(number))+1): result = result+i print(result)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, k, x, y, a[300039], f[300039], ans[300039], tot; struct yyy { int x, y, num; } s[300039]; inline bool cmp(yyy x, yyy y) { return x.y < y.y; } int main() { register int i, j; scanf("%lld", &n); k = sqrt(n); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); scanf("%lld", &m); for (i = 1; i <= m; i++) scanf("%lld%lld", &s[i].x, &s[i].y), s[i].num = i; sort(s + 1, s + m + 1, cmp); for (i = 1; i <= m; i++) { if (s[i].y > k) { tot = 0; for (j = s[i].x; j <= n; j += s[i].y) tot += a[j]; ans[s[i].num] = tot; } else { for (j = n; j > n - s[i].y; j--) f[j] = a[j]; for (j = n - s[i].y; j >= 1; j--) f[j] = f[j + s[i].y] + a[j]; for (j = i; s[j].y == s[j + 1].y; j++) ans[s[j].num] = f[s[j].x]; ans[s[j].num] = f[s[j].x]; i = j; } } for (i = 1; i <= m; i++) printf("%lld\n", ans[i]); }
2,100
CPP
def get_transactions(target,x,y,k): stick = 1 transactions = 0 while stick != target: if stick*x <= target: transactions += stick stick *= x else: break # print(stick,transactions,target) while stick < target: rem = target-stick # print('rem',rem) temp = (rem//x) stick += ((temp*x)-temp) transactions += temp # print(stick,transactions,target) if rem < x: transactions += 1 break # print(stick,transactions,target) transactions += k return transactions for _ in range(int(input())): x,y,k = list(map(int,input().split())) target = k+(y*k) print(get_transactions(target,x,y,k))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 50; const long long mod = 1E9 + 7; int n, k, cnt = 0; long long f[maxn + 1][maxn + 1][maxn + 1][2]; int d[maxn + 1][maxn + 1][maxn + 1][2]; struct node { int c[2][2]; bool p; }; queue<node> Q; long long C[maxn + 1][maxn + 1]; int main() { scanf("%d%d", &n, &k); for (int i = 1, a; i <= n; ++i) { scanf("%d", &a); if (a == 50) ++cnt; } for (int i = 0; i <= n; ++i) C[i][0] = 1; for (int i = 1; i <= n; ++i) for (int j = 1; j <= i; ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; memset(d, -1, sizeof(d)); d[0][0][cnt][0] = 0; f[0][0][cnt][0] = 1; node s = {}; s.c[0][0] = cnt; s.c[0][1] = n - cnt; Q.push(s); while (!Q.empty()) { node u = Q.front(); Q.pop(); if (u.p && u.c[1][0] + u.c[1][1] == n) continue; int cnt1 = u.c[u.p][0], cnt2 = u.c[u.p][1]; for (int i = 0, sum1 = 0; i <= cnt1 && sum1 <= k; ++i, sum1 += 50) for (int j = 1 - bool(i), sum2 = j * 100; j <= cnt2 && sum1 + sum2 <= k; ++j, sum2 += 100) { node v = u; v.c[v.p][0] -= i; v.c[v.p][1] -= j; v.p ^= 1; v.c[v.p][0] += i; v.c[v.p][1] += j; if (!~d[v.c[1][0]][v.c[1][1]][v.c[0][0]][v.p]) { d[v.c[1][0]][v.c[1][1]][v.c[0][0]][v.p] = d[u.c[1][0]][u.c[1][1]][u.c[0][0]][u.p] + 1; f[v.c[1][0]][v.c[1][1]][v.c[0][0]][v.p] = f[u.c[1][0]][u.c[1][1]][u.c[0][0]][u.p] * C[cnt1][i] % mod * C[cnt2][j] % mod; Q.push(v); } else { if (d[v.c[1][0]][v.c[1][1]][v.c[0][0]][v.p] == d[u.c[1][0]][u.c[1][1]][u.c[0][0]][u.p] + 1) f[v.c[1][0]][v.c[1][1]][v.c[0][0]][v.p] = (f[v.c[1][0]][v.c[1][1]][v.c[0][0]][v.p] + f[u.c[1][0]][u.c[1][1]][u.c[0][0]][u.p] * C[cnt1][i] % mod * C[cnt2][j] % mod) % mod; } } } printf("%d\n%I64d\n", d[cnt][n - cnt][0][1], f[cnt][n - cnt][0][1]); return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int main() { int n, a, d[12], sum = 0; d[10] = 1; memset(d, 0, sizeof d); cin >> n; for (int i = 0, _n = (n); i < _n; ++i) { cin >> a; d[a]++; sum += a; } if (d[0] == 0) { printf("-1\n"); return 0; } int ost = sum % 3; int i = ost; if (ost) { while (!d[i]) i += 3; if (i < 10) d[i]--; else { if (ost == 1) ost = 2; else ost = 1; int c = 0, i = ost; int p[12]; memset(p, 0, sizeof p); while (c < 2 && i < 10) { if (d[i]) { if (d[i] == 1) { p[i] = 1; c++; } else { if (c == 0) { p[i] = 2; c = 2; } else { p[i] = 1; c++; } } } i += 3; } if (c < 2) { printf("-1\n"); return 0; } for (int i = 0; i <= 9; ++i) d[i] -= p[i]; } } int fl = 0; for (int i = 1; i <= 9; ++i) if (d[i]) fl = 1; if (!fl) { printf("0\n"); return 0; } for (int i = 9; i >= 0; --i) { for (int j = 0; j < d[i]; ++j) printf("%d", i); } printf("\n"); }
1,600
CPP
def cal(num): return (num*(num-1))//2 for _ in range(1, int(input())+1): N, k = map(int, input().split()) string = ['a']*N target = N while cal(target) >= k: target -= 1 string[-target-1] = 'b' string[cal(target)-k] = 'b' print(''.join(string))
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 500; const long long int md = 1e9 + 7; const int MAX = 2750131; const double ep = 1.19209e-07; const int lg = 20; int n; vector<string> s; void check(string &a, string &b) { string newA = "", newB = ""; newA += a[0]; newB += b[0]; vector<int> frA(a.size()), frB(b.size()); frA[0]++, frB[0]++; for (int i = 1; i < a.size(); i++) { if (a[i] != newA[newA.size() - 1]) newA += a[i]; frA[newA.size() - 1]++; } for (int i = 1; i < b.size(); i++) { if (b[i] != newB[newB.size() - 1]) newB += b[i]; frB[newB.size() - 1]++; } if (newB != newA) { cout << "NO\n"; return; } for (int i = 0; i < newA.size(); i++) if (frA[i] > frB[i]) { cout << "NO\n"; return; } cout << "YES\n"; newA.clear(), newB.clear(); } int main() { cin >> n; s.resize(2 * n); for (int i = 0; i < 2 * n; i++) cin >> s[i]; for (int i = 0; i < 2 * n; i += 2) check(s[i], s[i + 1]); return 0; }
1,200
CPP
n=int(input()) l=[int(i) for i in input().split()] print('First' if any(i&1 for i in l) else 'Second')
1,100
PYTHON3
def count(L): maxIndex = 0 minIndex = 0 n = len(L) for i in range(n) : if L[i] == 1 : minIndex = i if L[i] == n : maxIndex = i if minIndex > maxIndex : minIndex,maxIndex = maxIndex,minIndex minIndex,maxIndex = minIndex + 1,maxIndex + 1 return max(minIndex - 1,n - maxIndex) + (maxIndex - minIndex) def for_input() : l = [] n = int(input()) for i in range(n) : l += dear_number_input() if (len(l) >= n) : break; return l def dear_number_input() : a = input() b = a.split(' ') l = [] for i in range(len(b)) : l.append(int(b[i])) return l l = for_input() print(count(l))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, p; scanf("%d %d %d", &n, &m, &p); vector<int> a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); map<int, int> b; for (int i = 0; i < m; i++) { int x; scanf("%d", &x); b[x]++; } set<int> ans; map<int, int> mp; for (int i = 0; i < p; i++) { int l = i, r = i, cnt = 0; while (l < n && r < n) { cnt++; mp[a[r]]++; while (mp[a[r]] > b[a[r]]) { mp[a[l]]--; cnt--; l += p; } if (cnt == m) ans.insert(r - (m - 1) * p + 1); r += p; } mp.clear(); } int sz = ans.size(); printf("%d\n", sz); for (int x : ans) printf("%d ", x); return 0; }
1,900
CPP
def search(cost, shop): if cost >= shop[-1]: return len(shop) elif cost < shop[0]: return 0 l, r = 0, len(shop) - 1 while l < r: mid = l + (r - l)//2 if shop[mid] <= cost: l = mid + 1 else: r = mid return l n = int(input()) shop = list(map(int, input().strip().split())) days = int(input()) shop.sort() output = [] for i in range(days): output.append(search(int(input()), shop)) for o in output: print(o)
1,100
PYTHON3
n=input() s='hello' k=0 for i in range(0,len(n)): if(s[k]==n[i]): k=k+1 if(k==5): break if(k==5): print("YES") else: print("NO")
1,000
PYTHON3
# find largest def solution(nums): largest = 0 for num in nums: if num > largest: largest = num result = '' for num in nums: if largest != num: result += str(largest - num) + ' ' print(result) solution([int(x) for x in input().split()])
800
PYTHON3
t=int(input()) while t: t-=1 n=int(input()) ar=list(map(int,input().split())) front=0 back=n-1 prev=0 turn=1 backMoves=0 frontMoves=0 flag=0 moves=0 frontSum=0 backSum=0 while(flag==0): if turn==2: s=0 while(s<=prev and flag==0): s+=ar[back] backSum+=ar[back] # print(back) back-=1 backMoves+=1 turn=1 if back<front: flag=1 break prev=s moves+=1 else: s=0 while(s<=prev and flag==0): s+=ar[front] frontSum+=ar[front] front+=1 frontMoves+=1 turn=2 if back<front: flag=1 break prev=s moves+=1 # print(moves,front,back,flag) print(moves,frontSum,backSum)
1,300
PYTHON3
import sys from math import * def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) a = [0]*4 for i in range(4): a[i] = mint() if a[0]-a[3] != 0 or a[0] == 0 and a[2] > 0: print(0) else: print(1)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; cin >> n >> k; long long int max = INT_MIN; long long int f, t, i; for (i = 0; i < n; i++) { cin >> f >> t; if (t > k && max < f + k - t) { max = f + k - t; } else if (t <= k && max < f) { max = f; } } cout << max; }
900
CPP
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool umax(T &a, T b) { return a < b ? (a = b, true) : false; } long long POW(long long a, long long p, long long M) { if (!p) return 1LL; long long T = POW(a, p / 2, M); T = T * T % M; if (p & 1) T = T * (a % M) % M; return T; } long long SQRT(long long a) { long long b = (long long)sqrtl(((double)a) + 0.5); while (b * b < a) ++b; while (b * b > a) --b; return (b * b == a) ? b : -1; } const long long MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; int q; cin >> q; while (q--) { int n; cin >> n; long long s; cin >> s; vector<pair<long long, long long>> v(n); for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; sort((v).begin(), (v).end()); long long L = v[n / 2].first; long long R = 1e15; long long ff = L; while (L <= R) { long long mid = L + R; mid /= 2; long long f = 0; int ct = 0; bool ps = false; vector<long long> tt; for (auto i : v) { if (i.first > mid) { f += i.first; ct++; } else if (i.second < mid) f += i.first; else tt.push_back(i.first); } if (tt.size()) { ct++; ps = true; f += mid; tt.pop_back(); } while (ct < (n + 1) / 2 && tt.size()) { ct++; f += mid; tt.pop_back(); } for (auto i : tt) f += i; if (f <= s && ps && ct == (n + 1) / 2) { L = mid + 1; ff = mid; } else R = mid - 1; } cout << ff << endl; } cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n"; return 0; }
1,900
CPP
s = input() ab, ba = [], [] for i in range(len(s) - 1): if s[i:i+2] == 'AB': ab.append(i) elif s[i:i+2] == 'BA': ba.append(i) for u in ab: for v in ba: if abs(u-v) > 1: print('YES') exit() print('NO')
1,500
PYTHON3
n=int(input()) s=input() l=[] for i in s: l.append(i) p="" i=0 while l!=[]: if len(l)%2==0: p+=l.pop(i) else: p=l.pop(i)+p print(p[::-1])
900
PYTHON3
n,m = map(int,input().split()) lang = [set(list(map(int,input().split()))[1:]) for _ in range(n)] g = {} count = 0 for i,e in enumerate(lang): if len(e)!=0: g[i] = [] for j,o in enumerate(lang): if i!=j and len(o)!=0: if len(e&o)!=0: g[i].append(j) else: count+=1 if len(g)!=0: ma = [] for i in range(n): if i in g: stack = [i] visited = [i] while len(stack)!=0: r = stack.pop() for j in g[r]: if j not in visited: visited.append(j) stack.append(j) visited = sorted(visited) if visited not in ma: ma.append(visited) count += len(ma)-1 print(count)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; inline long long bip(long long x, long long n, long long mod) { long long res = 1; while (n) { if (n & 1) { res = (res * x) % mod; } x = (x * x) % mod; n >>= 1; } return res; } const int ppr = 257; const long long INF = 2e18; const int inf = 2e9; const int mod = 1e9 + 7; const int N = 1e6 + 123; const long double pi = 3.1741592653589793238462643; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int n, a[N], l[N], r[N], cnt[N], t[N], add[N]; stack<int> st; void aza(int v, int tl, int tr) { if (tl == tr) { t[v] = cnt[tl]; return; } int tm = (tl + tr) >> 1; aza(v << 1, tl, tm); aza(v << 1 | 1, tm + 1, tr); t[v] = max(t[v << 1], t[v << 1 | 1]); } inline void push(int v) { if (add[v]) { t[v << 1] += add[v]; t[v << 1 | 1] += add[v]; add[v << 1] += add[v]; add[v << 1 | 1] += add[v]; add[v] = 0; } } int get(int v, int tl, int tr, int l, int r) { if (l > r) return -inf; if (tl == l && tr == r) return t[v]; push(v); int tm = (tl + tr) >> 1; return max(get(v << 1, tl, tm, l, min(tm, r)), get(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r)); } void upd(int v, int tl, int tr, int l, int r, int val) { if (l > r) return; if (tl == l && tr == r) { t[v] += val; add[v] += val; return; } push(v); int tm = (tl + tr) >> 1; upd(v << 1, tl, tm, l, min(tm, r), val); upd(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r, val); t[v] = max(t[v << 1], t[v << 1 | 1]); } void upda(int v, int tl, int tr, int pos, int val) { if (tl == tr) { t[v] = val; return; } push(v); int tm = (tl + tr) >> 1; if (pos <= tm) upda(v << 1, tl, tm, pos, val); else upda(v << 1 | 1, tm + 1, tr, pos, val); t[v] = max(t[v << 1], t[v << 1 | 1]); } int get_pos(int v, int tl, int tr, int pos) { if (tl == tr) return t[v]; push(v); int tm = (tl + tr) >> 1; if (pos <= tm) return get_pos(v << 1, tl, tm, pos); else return get_pos(v << 1 | 1, tm + 1, tr, pos); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], a[i + n] = a[i]; for (int i = 1; i <= n + n; i++) { r[i] = n + n + 1; l[i] = i; } for (int i = n; i >= 1; i--) { while (!st.empty() && a[i] < a[st.top()]) st.pop(); st.push(i); cnt[i] += st.size(); } while (!st.empty()) st.pop(); for (int i = n + n; i >= 1; i--) { while (!st.empty() && a[i] < a[st.top()]) st.pop(); if (!st.empty()) r[i] = st.top(); st.push(i); } while (!st.empty()) st.pop(); for (int i = 1; i <= n; i++) { while (!st.empty() && a[i] < a[st.top()]) st.pop(); if (!st.empty()) l[i] = st.top(); st.push(i); cnt[i] += st.size(); } while (!st.empty()) st.pop(); for (int i = 1; i <= n + n; i++) { while (!st.empty() && a[st.top()] > a[i]) st.pop(); if (!st.empty()) l[i] = st.top(); st.push(i); } for (int i = 1; i <= n; i++) cnt[i]--; aza(1, 1, n + n); int mn = +inf, mx; for (int i = n + 1; i <= n + n; i++) { int L = i - n; int R = i - 1; int x = get(1, 1, n + n, L, R); if (mn > x) { mn = x; mx = i - n - 1; } if (a[i] == 1) { upda(1, 1, n + n, i, 1); continue; } upd(1, 1, n + n, i - n, r[i - n] - 1, -1); upd(1, 1, n + n, l[i] + 1, i - 1, +1); int y = get_pos(1, 1, n + n, l[i]); upda(1, 1, n + n, i, y + 1); } cout << mn << ' ' << mx; return 0; }
2,700
CPP
t = int(input()) while (t > 0): t -= 1 s, i, e = [int(x) for x in input().split()] ms = s + e if (ms <= i): print(0) else: c = (ms - i + 1) // 2 print(min(c, e + 1))
1,300
PYTHON3
k, r = map(int, input().split()) #цена лопаты if k == 10 or k == r: print(1) else: br = False for n in range(1,11): i = 0 #print(n * k, 10 * i + r) while(n*k > 10*i): if n*k % 10 == 0 or n*k == 10*i + r: #print(n*k, 10*i + r) print(n) br = True break i+=1 if br: break #print()
800
PYTHON3
def wayTooLongWords(): numOfWords = int(input()) wordList = [] for i in range(numOfWords): wordList.append(input()) for word in wordList: length = len(word) if length > 10: print(word[0] + str(length-2) + word[length-1]) else: print(word) wayTooLongWords()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; set<string> st; st.insert(s); int sz = s.size(); for (int i = 0; i < 100; i++) { string q = ""; int cur = i; cur %= sz; for (int j = 0; j < sz; j++) { q += s[cur]; cur++; cur %= sz; } st.insert(q); } cout << (int)st.size() << endl; return 0; }
900
CPP
import sys import math #to read string get_string = lambda: sys.stdin.readline().strip() #to read list of integers get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) ) #to read integers get_int = lambda: int(sys.stdin.readline()) #--------------------------------WhiteHat010--------------------------------------# l = list(get_string()) s = {'A','B','C'} flag = False for i in range(len(l)-2): if set(l[i:i+3]) == s: flag = True break if flag: print("Yes") else: print("No")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int m, n; scanf("%d %d", &m, &n); int i; double result = 0; for (i = 1; i <= m; i++) { result += i * (pow((double)i / (double)m, (double)n) - pow((double)(i - 1) / (double)m, (double)n)); } printf("%.12f\n", result); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; inline long long bigmod(long long p, long long e, long long M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } const int N = 1001; int dp[N][3]; string str[55]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> str[i]; for (int i = 0; i < n; i++) for (int j = 0; j < 3; j++) dp[i][j] = 1000; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (isalpha(str[i][j])) dp[i][0] = min(dp[i][0], min(j, m - j)); if (str[i][j] >= '0' && str[i][j] <= '9') dp[i][1] = min(dp[i][1], min(j, m - j)); if (str[i][j] == '#' || str[i][j] == '&' || str[i][j] == '*') dp[i][2] = min(dp[i][2], min(j, m - j)); } } int ans = 1e9; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; for (int k = 0; k < n; k++) { if (i == k || j == k) continue; ans = min(ans, dp[i][0] + dp[j][1] + dp[k][2]); } } } printf("%d\n", ans); return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const long long Inf = 1e18; const int inf = 0x3f3f3f3f; const int maxn = 1e5 + 5; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } int an[maxn]; int pos[maxn]; int state[maxn]; int main(void) { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> an[i]; pos[an[i]] = i; } for (int i = n; i >= 1; i--) { int u = pos[i]; state[u] = 0; int v = u; while (v - an[u] > 0) v -= an[u]; for (; v <= n; v += an[u]) { if (an[v] > an[u] && state[v] == 0) { state[u] = 1; break; } } } for (int i = 1; i <= n; i++) { if (state[i]) cout << "A"; else cout << "B"; } return 0; }
1,600
CPP
n, k=map(int, input().split()) x=len(bin(n))-2 s=bin(n)[2:]; z=n; a=0 if k==1: print (n) else: print (2**x-1)
1,300
PYTHON3
'''Author- Akshit Monga''' from sys import stdin,stdout input=stdin.readline t=1 for _ in range(t): n,w=map(int,input().split()) a=[int(x) for x in input().split()] b=[] s=0 for i in a: s+=i b.append(s) x=max(b) if x>w: print(0) continue if x<0: y=min(b) val=abs(y) if val>w: print(0) continue else: print(w-val+1) continue x=w-x y=min(b) if y>=0: print(x+1) continue y=abs(y) ans=x-y+1 if ans<0: ans=0 print(ans)
1,400
PYTHON3