solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; using ld = long double; using pll = pair<ll, ll>; using ull = unsigned long long; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll a, b, c, d; cin >> a >> b >> c >> d; int n, m; cin >> n >> m; ll ans1 = 0, ans2 = 0; for (int i = 0; i < n; i++) { int p; cin >> p; ans1 += min(p * a, b); } ans1 = min(ans1, c); for (int i = 0; i < m; i++) { int p; cin >> p; ans2 += min(p * a, b); } ans2 = min(ans2, c); cout << min(ans1 + ans2, d) << endl; return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int i, j, k, l, n, m; long long int a, b, c, d; cin >> a >> b >> c >> d; l = abs(c - a) + 1; k = abs(d - b) + 1; if (a == c) l++; if (b == d) k++; cout << 2 * (l + k); }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int N = 505; unordered_set<int> dp[N]; int main() { int n, k; scanf("%d%d", &n, &k); dp[0].insert(0); for (int i = 0; i < n; ++i) { int c; scanf("%d", &c); for (int j = k; j >= c; --j) { for (int l : dp[j - c]) dp[j].insert(l); for (int l : dp[j - c]) dp[j].insert(l + c); if (dp[j].size() > 0) dp[j].insert(j); } } printf("%d\n", dp[k].size()); vector<int> res; for (int l : dp[k]) res.push_back(l); sort(res.begin(), res.end()); for (int l : res) printf("%d ", l); }
1,900
CPP
#include <bits/stdc++.h> int a[401], n, i, j, k, p, c, t, S = -1; int main() { scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", a + i); for (i = 0; i < n; i++) for (j = i; j < n; j++) { p = a[i] - 1, c = 1, t = a[j]; for (k = 0; k <= j; k++) c += p / a[k], t += p / a[k] * a[k], p %= a[k]; for (p = t, k = 0; k < n; k++) c -= p / a[k], p %= a[k]; if (c < 0 && (S < 0 || S > t)) S = t; } printf("%d\n", S); }
2,600
CPP
# from dust i have come, dust i will be l,r=map(int,input().split()) print('YES') for i in range(l,r+1,2): print(i,i+1)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int memo[(int)1e6 + 1000], a[(int)1e6 + 1000]; int main() { int n, to = 0; cin >> n; memset(a, -1, sizeof a); for (int i = 0; i < n; i++) { int tmp; cin >> tmp; tmp++; cin >> a[tmp]; to = max(to, tmp); } for (int i = 1; i < to + 1; i++) if (a[i] == -1) memo[i] = memo[i - 1]; else memo[i] = memo[(i - a[i] - 1 >= 0 ? i - a[i] - 1 : 0)] + 1; int mx = memo[to]; for (int i = 1; i < to + 1; i++) { mx = max(mx, memo[i - 1] + 1); } cout << (mx == memo[to] ? n - mx : n + 1 - mx) << endl; }
1,600
CPP
s = list(map(int, input().split())) p = list(map(int, input().split())) p.sort() check = [] least = [] for i in range(0, (s[1]-s[0]+1)): for j in range(i, i+s[0]): ele = p[j] check.append(ele) check.sort() # minimum = check[i+3] - check[i] # least.append(check[i+3] - check[i]) maximum = max(check) minimum = min(check) ele = maximum - minimum least.append(ele) check = [] least_maximum = min(least) print(least_maximum)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int pow(long long int x, long long int y, long long int m) { long long int res = 1; x = x % m; while (y > 0) { if (y & 1) res = ((res % m) * (x % m)) % m; y = y >> 1; x = ((x % m) * (x % m)) % m; } return res % m; } template <typename Arg1> void ZZ(const char* name, Arg1&& arg1) { std::cerr << name << " = " << arg1 << endl; } template <typename Arg1, typename... Args> void ZZ(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " = " << arg1; ZZ(comma, args...); } const long long int INF = 0xFFFFFFFFFFFFFFFL; clock_t time_p = clock(); void abhigyan10() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } long long int seed; mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count()); inline long long int rnd(long long int l = 0, long long int r = INF) { return uniform_int_distribution<long long int>(l, r)(rng); } const long long int mod = 1000000007; map<pair<long long int, long long int>, long long> val; long long int counta[40005]; long long int countb[40005]; long long int cal(long long int x, long long int y, long long int k) { long long int ans = 0; long long int i, d; for (i = 1; i <= x; i++) { d = k / i; if (k % i != 0) continue; ans += (long long)(x - i + 1) * (long long)(max(y - d + 1, 0LL)); } val[{x, y}] = ans; val[{y, x}] = ans; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m, k, i, x, c = 0, j; long long int ans = 0; cin >> n >> m >> k; vector<long long int> a, b; for (i = 1; i <= n; i++) { cin >> x; if (x == 1) c++; else if (c > 0) { if (counta[c] == 0) { a.push_back(c); counta[c]++; } else counta[c]++; c = 0; } } if (c > 0) { if (counta[c] == 0) { a.push_back(c); counta[c]++; } else counta[c]++; c = 0; } for (i = 1; i <= m; i++) { cin >> x; if (x == 1) c++; else if (c > 0) { if (countb[c] == 0) { b.push_back(c); countb[c]++; } else countb[c]++; c = 0; } } if (c > 0) { if (countb[c] == 0) { b.push_back(c); countb[c]++; } else countb[c]++; c = 0; } for (i = 0; i < a.size(); i++) { for (j = 0; j < b.size(); j++) { pair<long long int, long long int> p = {a[i], b[j]}; if (val[p] == 0) ans += counta[a[i]] * countb[b[j]] * cal(a[i], b[j], k); else ans += counta[a[i]] * countb[b[j]] * val[p]; } } cout << ans; return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(long long &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const long long &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.first); putchar(' '); _W(x.second); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } int MOD = 1e9 + 7; void ADD(long long &x, long long v) { x = (x + v) % MOD; if (x < 0) x += MOD; } const int SIZE = 514; int dp0[4][SIZE][SIZE]; short dp[SIZE][SIZE][SIZE]; char s[SIZE][SIZE]; int n, m, q; int get(char c) { if (c == 'R') return 0; if (c == 'G') return 1; if (c == 'Y') return 2; return 3; } int get(int c, int x1, int y1, int x2, int y2) { return dp0[c][x2][y2] + dp0[c][x1][y1] - dp0[c][x1][y2] - dp0[c][x2][y1]; } void solve() { R(n, m, q); for (int i = (1); i <= (n); ++i) scanf("%s", (s[i] + 1)); for (int i = (1); i <= (n); ++i) for (int j = (1); j <= (m); ++j) { s[i][j] = get(s[i][j]); } for (int c = 0; c < (4); ++c) { for (int i = (1); i <= (n); ++i) for (int j = (1); j <= (m); ++j) { dp0[c][i][j] = dp0[c][i - 1][j] + dp0[c][i][j - 1] - dp0[c][i - 1][j - 1] + (s[i][j] == c); } } for (int i = (1); i <= (n); ++i) for (int j = (1); j <= (m); ++j) { int mi = min(i, j); for (int k = (1); k <= (mi); ++k) { if (!(k & 1)) { int half_k = k / 2; int need = half_k * half_k; if (get(3, i - half_k, j - half_k, i, j) == need && get(2, i - half_k, j - k, i, j - half_k) == need && get(1, i - k, j - half_k, i - half_k, j) == need && get(0, i - k, j - k, i - half_k, j - half_k) == need) dp[i][j][k] = k; } dp[i][j][k] = max({dp[i][j][k], dp[i][j][k - 1], dp[i - 1][j][k - 1], dp[i][j - 1][k - 1], dp[i - 1][j - 1][k - 1]}); } } while (q--) { int x1, y1, x2, y2; R(x1, y1, x2, y2); int side = min(x2 - x1, y2 - y1) + 1; short an = 0; if (x2 - x1 < y2 - y1) { int len = (y2 - y1) - (x2 - x1) + 1; for (int i = 0; i < (len); ++i) { an = max(an, dp[x2][y2 - i][side]); } } else { int len = (x2 - x1) - (y2 - y1) + 1; for (int i = 0; i < (len); ++i) { an = max(an, dp[x2 - i][y2][side]); } } W((int)an * (int)an); } } int main() { solve(); return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; int n, m; long long ans, f[1 << 20][20]; bool Map[30][30]; int read() { int s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * f; } int main() { n = read(), m = read(); for (int u, v, i = 1; i <= m; i++) { u = read() - 1, v = read() - 1; Map[u][v] = Map[v][u] = 1; } for (int i = 0; i < n; i++) f[1 << i][i] = 1; int maxn = (1 << n) - 1; for (int S = 0, sta; S <= maxn; S++) { for (int i = 0; i < n; i++) if (S & (1 << i)) { sta = i; break; } for (int i = sta; i < n; i++) for (int j = 0; j < n; j++) { if (!Map[i][j]) continue; if (!(S & (1 << i))) f[S | (1 << i)][i] += f[S][j]; if (i == sta) ans += f[S][j]; } } cout << (ans - m) / 2; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; vector<int> inp; long long lim; long long dp[30][4][4][4][4][30]; vector<string> vs; long long call(int pos, int isSmall, int isStart, int f, int l, int koyta, string num) { if (pos >= lim) { if (f == l && f != 0) { return 1; } return 0; } if (dp[pos][isSmall][isStart][f][l][koyta] != -1) { return dp[pos][isSmall][isStart][f][l][koyta]; } long long ses = isSmall ? 9 : inp[pos]; long long res = 0; if (!isStart) { for (int i = 0; i <= ses; i++) { ostringstream ss; ss << i; res += call(pos + 1, isSmall | i < inp[pos], 0, f, i, koyta + 1, num + ss.str()); } } else { for (int i = 1; i <= ses; i++) { ostringstream ss; ss << i; res += call(pos + 1, isSmall | i < inp[pos], 0, i, i, koyta + 1, num + ss.str()); } res += call(pos + 1, 1, 1, 0, 0, koyta, num); } return dp[pos][isSmall][isStart][f][l][koyta] = res; } long long vanga(long long x) { if (x == 1) return 1; if (x == 0) return 0; inp.clear(); while (x != 0) { inp.push_back(x % 10); x /= 10; } reverse(inp.begin(), inp.end()); lim = inp.size(); memset(dp, -1, sizeof(dp)); return call(0, 0, 1, 0, 0, 0, ""); } int main() { long long n, m; while (cin >> n >> m) { long long ans = vanga(m) - vanga(n - 1); cout << ans << endl; } return 0; }
1,500
CPP
n = int(input()) s = input() a1 = [] b0 = [] for i in s : if i == '1': a1.append(i) else: b0.append(i) print(abs(len(a1) - len(b0)))
900
PYTHON3
(n, q) = map(int, input().split()) z = n // q if z % 2 == 0: print("NO") else: print('YES')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int tinf = (int)1e9 + 7; const long long inf = (long long)1e18 + 7; const int N = 1e5 + 5; int main() { { ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); } long long x, y, l, r; cin >> x >> y >> l >> r; map<long long, long long> g; vector<long long> d; long double a1 = 1, b1 = 1; while (a1 <= 1e18) { b1 = 1; while (b1 + a1 <= 1e18) { long long t = a1 + b1; if (!g[t]) d.push_back(t), g[t] = 1; b1 *= y; } a1 *= x; } if (!g[l]) g[l] = 2, d.push_back(l); if (!g[r]) g[r] = 2, d.push_back(r); if (l == r) { if (g[l] != 1) cout << 1; else cout << 0; return 0; } sort(d.begin(), d.end()); long long ans = 0, e = 0, l1 = 0; for (int i = 0; i < d.size(); i++) if (d[i] == l) { e = 1; l1 = d[i]; } else if (e) { if (d[i] == r) { e = 0; if (g[r] == 2) { if (g[l1] == 1) ans = max(d[i] - l1, ans); else ans = max(d[i] - l1 + 1, ans); } } if (g[d[i]] == 1) { if (g[l1] == 1) ans = max(d[i] - l1 - 1, ans); else ans = max(d[i] - l1, ans); } l1 = d[i]; } cout << ans; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; long long h[100001] = {0}; long long hops[100001] = {0}; long long previous[100001] = {0}; long long ans[100001] = {0}; int main() { int n, d; cin >> n >> d; int end_point = 1; for (int i = 1; i <= n; ++i) { cin >> h[i]; hops[i] = 1; for (int j = max(1, i - 500); j < i; j++) { if (abs(h[i] - h[j]) >= d && hops[j] + 1 > hops[i]) { hops[i] = hops[j] + 1; previous[i] = j; } if (hops[i] > hops[end_point]) { end_point = i; } } } cout << hops[end_point] << '\n'; int T = previous[end_point]; int cnt = 0; ans[cnt] = end_point; while (T) { ans[++cnt] = T; T = previous[T]; } for (int i = cnt; i >= 0; i--) { cout << ans[i] << " "; } return 0; }
2,000
CPP
import os import sys from io import BytesIO, IOBase from collections import defaultdict, deque, Counter, OrderedDict import threading from math import gcd def main(): n,m = map(int,input().split()) adj = [[] for _ in range(n)] for i in range(m): u,v = map(int,input().split());u-=1;v-=1 adj[u].append(v) adj[v].append(u) max_degree = [-1,-1] for i in range(n): if len(adj[i]) > max_degree[0]: max_degree = [len(adj[i]),i] visit = [0]*n q=deque([max_degree[1]]) visit[max_degree[1]] = 1 while q: x = q.popleft() for nx in adj[x]: if not visit[nx]: q.append(nx) visit[nx] = 1 print(x+1,nx+1) 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") # endregion if __name__ == "__main__": """threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start()""" main()
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; const long long mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void solve() { int n, m, d; cin >> d >> n >> m; vector<int> x1(d), x2(d), y1(d), y2(d); vector<pair<int, int> > x, y; for (int i = 0; i < d; i++) { cin >> x1[i] >> y1[i] >> x2[i] >> y2[i]; x.push_back({x1[i], i}); x.push_back({x2[i], i}); y.push_back({y1[i], i}); y.push_back({y2[i], i}); } vector<int> cntl(d + 1, 0), cntr(d + 1, 0), cntt(d + 1, 0), cntb(d + 1, 0); cin >> cntl[d] >> cntr[d] >> cntt[d] >> cntb[d]; sort(x.begin(), x.end()); sort(y.begin(), y.end()); vector<int> mark(n + m + 1, 0); int tot = 0; for (int i = 0; i < 2 * d; i++) { int j = i; int cnt = i - tot; while (j < 2 * d && x[i].first == x[j].first) { cntl[x[j].second] = cnt - mark[x[j].second]; j++; } for (int h = i; h < j; h++) { tot += mark[x[h].second]; mark[x[h].second] = 1; } i = j - 1; } for (int i = 0; i < 2 * d; i++) { mark[x[i].second] = 0; } tot = 0; for (int i = 2 * d - 1; i >= 0; i--) { int j = i; int cnt = 2 * d - i - 1 - tot; while (j >= 0 && x[j].first == x[i].first) { cntr[x[j].second] = cnt - mark[x[j].second]; j--; } for (int h = j + 1; h <= i; h++) { tot += mark[x[h].second]; mark[x[h].second] = 1; } i = j + 1; } tot = 0; for (int i = 0; i < 2 * d; i++) { mark[x[i].second] = 0; } for (int i = 0; i < 2 * d; i++) { int j = i; int cnt = i - tot; while (j < 2 * d && y[i].first == y[j].first) { cntt[y[j].second] = cnt - mark[y[j].second]; j++; } for (int h = i; h < j; h++) { tot += mark[y[h].second]; mark[y[h].second] = 1; } i = j - 1; } for (int i = 0; i < 2 * d; i++) { mark[y[i].second] = 0; } tot = 0; for (int i = 2 * d - 1; i >= 0; i--) { int j = i; int cnt = 2 * d - i - 1 - tot; while (j >= 0 && y[j].first == y[i].first) { cntb[y[j].second] = cnt - mark[y[j].second]; j--; } for (int h = j + 1; h <= i; h++) { tot += mark[y[h].second]; mark[y[h].second] = 1; } i = j + 1; } tot = 0; for (int i = 0; i < 2 * d; i++) { mark[y[i].second] = 0; } for (int i = 0; i < d; i++) { if (cntl[i] == cntl[d] && cntr[i] == cntr[d] && cntt[i] == cntt[d] && cntb[i] == cntb[d]) { cout << i + 1; return; } } cout << -1; } int main() { ios_base::sync_with_stdio(0); int T = 1; while (T--) { solve(); } }
2,000
CPP
#include <bits/stdc++.h> using namespace std; long long vis[100][100]; vector<long long> adj[20005]; vector<pair<long long, long long> > sh[20005]; vector<long long> viva[20005]; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); long long m, n, k, l, i, cnt = 0, x, y, j; cin >> n >> m; for (i = 0; i < m; i++) { cin >> x >> y; x--; y--; adj[x].push_back(y); } for (i = 0; i < n; i++) { sh[i].push_back({i, i}); for (j = 0; j < n; j++) { viva[i].push_back(j * n + i); } } for (i = 0; i < n; i++) { for (auto &cv : adj[i]) { sh[i].push_back({i, viva[cv].back()}); sh[cv].push_back({cv, viva[cv].back()}); viva[cv].pop_back(); } } for (i = 0; i < n; i++) { cout << sh[i].size() << endl; for (j = 0; j < sh[i].size(); j++) { assert(sh[i][j].first == i); cout << sh[i][j].first + 1 << ' ' << sh[i][j].second + 1 << endl; } } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; string num; cin >> num; if (n == 1) { if ((num[0] - '0') % 2 == 0) { cout << 2 << endl; return; } cout << 1 << endl; return; } int e, o = 0; if (n % 2 == 0) { for (int i = 1; i < num.length(); i += 2) { if ((num[i] - '0') % 2 == 0) { cout << 2 << endl; return; } } cout << 1 << endl; return; } for (int i = 0; i < num.length(); i += 2) { if ((num[i] - '0') % 2 != 0) { cout << 1 << endl; return; } } cout << 2 << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { solve(); } }
900
CPP
strs= input() dicts={} for each in strs: if each not in dicts: dicts[each]=1 if(len(dicts)%2 ==0): print("CHAT WITH HER!") else: print("IGNORE HIM!")
800
PYTHON3
l = tuple(int(i) for i in input().split()) if (l[0]*l[1])+2*l[3]==(l[0]*l[2])+2*l[4]: print ("Friendship") elif (l[0]*l[1])+2*l[3]<(l[0]*l[2])+2*l[4]: print ("First") elif (l[0]*l[1])+2*l[3]>(l[0]*l[2])+2*l[4]: print ("Second")
800
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000000") using namespace std; const int INF = 1e8; struct rib { int b, u, c, f; size_t back; }; vector<vector<rib> > g; void add_edge(int a, int b, int u, int c) { rib r1 = {b, u, c, 0, g[b].size()}; rib r2 = {a, 0, -c, 0, g[a].size()}; g[a].push_back(r1); g[b].push_back(r2); } pair<int, int> doit(int s, int t) { int n = g.size(); int flow = 0, cost = 0, k = INF; while (flow < k) { vector<int> id(n, 0); vector<int> d(n, INF); vector<int> q(n); vector<int> p(n); vector<size_t> p_rib(n); int qh = 0, qt = 0; q[qt++] = s; d[s] = 0; while (qh != qt) { int v = q[qh++]; id[v] = 2; if (qh == n) qh = 0; for (size_t i = 0; i < g[v].size(); ++i) { rib& r = g[v][i]; if (r.f < r.u && d[v] + r.c < d[r.b]) { d[r.b] = d[v] + r.c; if (id[r.b] == 0) { q[qt++] = r.b; if (qt == n) qt = 0; } else if (id[r.b] == 2) { if (--qh == -1) qh = n - 1; q[qh] = r.b; } id[r.b] = 1; p[r.b] = v; p_rib[r.b] = i; } } } if (d[t] == INF) break; int addflow = k - flow; for (int v = t; v != s; v = p[v]) { int pv = p[v]; size_t pr = p_rib[v]; addflow = min(addflow, g[pv][pr].u - g[pv][pr].f); } for (int v = t; v != s; v = p[v]) { int pv = p[v]; size_t pr = p_rib[v], r = g[pv][pr].back; g[pv][pr].f += addflow; g[v][r].f -= addflow; cost += g[pv][pr].c * addflow; } flow += addflow; } return make_pair(flow, cost); } int matrix[2000][2000], ind[2000][2000]; int main() { int n, m; cin >> n >> m; g.resize((n * m) + 100); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { scanf("%d", &matrix[i][j]); } int cur = 1, cur2 = 1 + ((n * m) / 2); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((i + j) % 2 == 1) { ind[i][j] = cur++; add_edge(0, ind[i][j], 1, 0); } else { ind[i][j] = cur2++; add_edge(ind[i][j], g.size() - 1, 1, 0); } } } int x, y; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((i + j) % 2 == 1) { for (int f = -1; f < 2; f++) { for (int s = -1; s < 2; s++) { x = i + f; y = j + s; if ((abs(f) + abs(s) != 1) || (!(x >= 0 && x < n && y >= 0 && y < m))) continue; add_edge(ind[i][j], ind[x][y], 1, (matrix[i][j] == matrix[x][y] ? 0 : 1)); } } } } } cout << doit(0, g.size() - 1).second; }
2,300
CPP
t=int(input()) for i in range(t): n,k=map(int,input().split()) if k>=n: print(1) else: l=min(int(n**(0.5))+1,k) j=2 ans=1 while j<=l: if n%j==0: if n//j<=k: ans=n//j j=k+1 else: ans=j j=j+1 print(n//ans)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int lcs[5005][5005], n, a, b, f[5005]; char s[5005]; inline int read() { int ret = 0, ff = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') ff = -ff; ch = getchar(); } while (isdigit(ch)) { ret = (ret << 3) + (ret << 1) + (ch ^ 48); ch = getchar(); } return ret * ff; } signed main() { n = read(), a = read(), b = read(); scanf("%s", s + 1); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) lcs[i][j] = (s[i] == s[j]) ? lcs[i - 1][j - 1] + 1 : 0; memset(f, 0x3f, sizeof(f)); f[0] = 0; for (int i = 1; i <= n; i++) { f[i] = f[i - 1] + a; for (int j = i - 1; j >= 1; j--) f[i] = min(f[i], f[i - min(lcs[i][j], i - j)] + b); } printf("%d", f[n]); return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; char _; inline long long powmod(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; } int n; long long v[4004]; long long d[4004]; long long p[4004]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> v[i] >> d[i] >> p[i]; } vector<int> ans; int cur = 0; while (cur < n) { if (p[cur] < 0) { cur++; continue; } ans.push_back(cur + 1); int i = cur + 1; long long loud = 0; while (i < n) { if (p[i] < 0) { i++; continue; } p[i] -= v[cur]; p[i] -= loud; v[cur]--; v[cur] = max(v[cur], 0LL); if (p[i] < 0) loud += d[i]; i++; } cur++; } cout << int((ans).size()) << '\n'; for (int i = 0; i < int((ans).size()); ++i) cout << ans[i] << ' '; cout << '\n'; return 0; }
1,800
CPP
x = int(input()) y = x // 10 k = x % 10 u = str(x) first = {1: "ten", 2: "twenty" , 3: "thirty", 4: "forty", 5: "fifty", 6: "sixty", 7: "seventy", 8: "eighty", 9: "ninety"} second = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 0: "zero"} bigger_ten = {11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen", 17: "seventeen", 18: "eighteen", 19: "nineteen"} if x < 10: print(second[x]) elif x > 10 and x < 20: print(bigger_ten[x]) elif k == 0: print(first[y]) else: print(first[y]+"-"+second[k])
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; istream& in = cin; long long n, k; long long mod = (long long)1e9 + 7; long long qm(long long a, long long p) { long long ans = 1; while (p) { if (p & 1) { ans = ans * a % mod; } a = a * a % mod; p /= 2; } return ans; } long long pw[2000010], sum[2000010], res[2000010]; bool mark[2000010]; void add(long long& a, long long b) { a %= mod; b %= mod; a += b; a %= mod; if (a < 0) a += mod; } void input() { in >> n >> k; } int main() { int TEST_CASE = 1; while (TEST_CASE-- > 0) { input(); for (int i = 1; i <= k; ++i) { pw[i] = qm(i, n); } long long ans = 0; long long all = 0; res[1] = 1; for (int i = 2; i <= k; ++i) { add(sum[i], 1); add(all, sum[i]); res[i] = pw[i]; add(res[i], -all); add(ans, res[i] ^ i); for (int j = 2; i * j <= k; j++) { add(sum[i * j], res[i] - res[i - 1]); } } cout << ans << endl; } return 0; }
2,300
CPP
import sys t=int(sys.stdin.readline()) for _ in range(t): n,k=map(int,sys.stdin.readline().split()) l=list(map(int,sys.stdin.readline().split())) for i in range(n): if l[i]==k: l[i]=1 elif l[i]<k: l[i]=0 else: l[i]=2 if l.count(1)==0: print("NO") elif n==1: print("YES") else: flag=False for i in range(n): for j in range(i+1,min(n,i+3)): if l[i]>0and l[j]>0: flag=True if flag: print("YES") else: print("NO")
2,000
PYTHON3
#include <bits/stdc++.h> using namespace std; mt19937_64 mt(time(0)); const int MAXN = 12; const int MAXM = 2000 + 10; int nums[MAXN][MAXM], dp[MAXN + 10][1 << MAXN], cache[1 << MAXN]; bool cmp(const pair<int, int>& a, const pair<int, int>& b) { return nums[a.first][a.second] > nums[b.first][b.second]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(10); cout << fixed; int t; cin >> t; while (t--) { memset(dp, 0, sizeof(dp)); int n, m; cin >> n >> m; vector<pair<int, int> > pos; pos.reserve(n * m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> nums[i][j]; pos.emplace_back(i, j); } } sort(pos.begin(), pos.end(), cmp); set<int> table; for (const auto& p : pos) { table.insert(p.second); if (table.size() == n) break; } m = 1 << n; int idx = 0; for (int j : table) { for (int mask = 0; mask < m; mask++) { cache[mask] = 0; for (int k = 0; k < n; k++) { int sum = 0; for (int i = 0; i < n; i++) { if (mask & (1 << i)) { sum += nums[(i + k) % n][j]; } } cache[mask] = max(sum, cache[mask]); } } for (int mask = 0; mask < m; mask++) { for (int cur_mask = mask;; cur_mask = (cur_mask - 1) & mask) { dp[idx + 1][mask] = max(dp[idx + 1][mask], dp[idx][mask ^ cur_mask] + cache[cur_mask]); if (cur_mask == 0) break; } } idx++; } cout << dp[idx][(1 << n) - 1] << '\n'; } return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t; cin >> t; while (t--) { int n, x; bool valid = 1; cin >> n >> x; int ara[n + 1], ara2[n + 1]; int sum1 = 0, sum2 = 0; for (int i = 0; i < n; ++i) { cin >> ara[i]; } for (int i = 0; i < n; ++i) { cin >> ara2[i]; } sort(ara2, ara2 + n, greater<int>()); for (int i = 0; i < n; ++i) { if ((ara[i] + ara2[i]) > x) { valid = 0; break; } } if (!valid) cout << "No\n"; else cout << "Yes\n"; } cout << "\n"; }
800
CPP
#include <bits/stdc++.h> using namespace std; int N, M; int cost[(1 << 9)][2]; int id[(1 << 9)][2]; int cnt[(1 << 9)]; int x, y; int num; int ans = 2e9 + 1; int NN; void work(int s, int s1) { int t = (s | s1); int sum = 0; for (int i = 1; i <= NN; i++) if ((i & t) == i) sum += cnt[i]; if (sum > num) { num = sum; if (s != s1) { ans = cost[s][0] + cost[s1][0]; x = id[s][0]; y = id[s1][0]; } else { ans = cost[s][0] + cost[s][1]; x = id[s][0]; y = id[s][1]; } } if (sum == num) { if (s != s1 && cost[s][0] + cost[s1][0] < ans) { ans = cost[s][0] + cost[s1][0]; x = id[s][0]; y = id[s1][0]; } if (s == s1 && cost[s][0] + cost[s][1] < ans) { ans = cost[s][0] + cost[s][1]; x = id[s][0]; y = id[s][1]; } } } int main() { ios::sync_with_stdio(0); memset(cost, 0x3f, sizeof(cost)); scanf("%d%d", &N, &M); int f, b; for (int i = 1; i <= N; i++) { scanf("%d", &f); int p = 0; while (f--) { scanf("%d", &b); b--; p += (1 << b); } cnt[p]++; } int c; for (int i = 1; i <= M; i++) { scanf("%d%d", &c, &f); int p = 0; while (f--) { scanf("%d", &b); b--; p += (1 << b); } if (c <= cost[p][0]) { cost[p][1] = cost[p][0]; cost[p][0] = c; id[p][1] = id[p][0]; id[p][0] = i; } else if (c < cost[p][1]) cost[p][1] = c, id[p][1] = i; } NN = (1 << 9) - 1; for (int i = 1; i <= NN; i++) for (int j = 1; j <= NN; j++) { if (i != j && cost[i][0] != 1061109567 && cost[j][0] != 1061109567) work(i, j); if (i == j && cost[i][0] != 1061109567 && cost[j][1] != 1061109567) work(i, j); } printf("%d %d", x, y); return 0; }
2,100
CPP
n, l, r = map(int, input().split()) minn = 0 maxx =0 minn += (n - l + 1) l -= 1 while l > 0: minn += 2**l l -= 1 temp = r r -= 1 while r >=0: maxx += 2**r r -=1 #print(maxx) n -= temp maxx += n*(2**(temp-1)) print("{} {}".format(minn, maxx))
900
PYTHON3
n = int(input()) groups = [int(x) for x in input().split(' ')] taxi = 0 groupCount = {1:0, 2:0, 3:0, 4:0} for group in groups: groupCount[group] += 1 taxi += groupCount[4] groupCount[1] -= groupCount[3] taxi += groupCount[3] if groupCount[2] % 2 == 0: taxi += groupCount[2]//2 else: groupCount[1] -= 2 taxi += groupCount[2]//2 + 1 if groupCount[1] > 0: if groupCount[1] % 4 == 0: taxi += groupCount[1] // 4 else: taxi += groupCount[1] // 4 + 1 print(taxi)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; template <size_t N, size_t M> struct Suffix_Automata { int maxlen[N], trans[N][M], link[N], Size, Last, isclone[N]; Suffix_Automata() { Size = Last = 1; } void Extend(int id) { int cur = (++Size), p; maxlen[cur] = maxlen[Last] + 1; for (p = Last; p && !trans[p][id]; p = link[p]) trans[p][id] = cur; if (!p) link[cur] = 1; else { int q = trans[p][id]; if (maxlen[q] == maxlen[p] + 1) link[cur] = q; else { int clone = (++Size); maxlen[clone] = maxlen[p] + 1; memcpy(trans[clone], trans[q], sizeof(trans[q])); link[clone] = link[q]; for (; p && trans[p][id] == q; p = link[p]) trans[p][id] = clone; link[cur] = link[q] = clone; isclone[clone] = true; } } isclone[cur] = false; Last = cur; } int cnt[N], in[N]; vector<int> G[N]; long long calc() { long long res = 0; for (int i = 1; i <= Size; i++) cnt[i] = !isclone[i]; for (int i = 1; i <= Size; i++) { G[i].clear(); G[i].push_back(link[i]); ++in[link[i]]; } queue<int> Q; for (int i = 1; i <= Size; i++) { if (!in[i]) { Q.push(i); } } while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int v : G[u]) { cnt[v] += cnt[u]; if (!(--in[v])) { Q.push(v); } } } for (int i = 1; i <= Size; i++) { res += 1LL * cnt[i] * cnt[i] * (maxlen[i] - maxlen[link[i]]); } return res; } }; Suffix_Automata<300000, 26> SAM; char s[300000]; int main() { int T; scanf("%d", &T); for (int _ = 0; _ < T; _++) { memset(SAM.trans, 0, sizeof(SAM.trans)); SAM.Last = SAM.Size = 1; scanf("%s", s + 1); int n = strlen(s + 1); for (int i = 1; i <= n; i++) { SAM.Extend(s[i] - 'a'); } printf("%lld\n", SAM.calc()); } return 0; }
2,300
CPP
a = int(input()) out = 0 for x in range(0,a): out += pow(2,x+1) print(out)
1,100
PYTHON3
n = int(input()) count = 0 for i in range(n): a, b, c = map(int, input().split()) while a <= c and b <= c: if a < b: a += b else: b +=a count += 1 print(count) count = 0
800
PYTHON3
#include <bits/stdc++.h> template <class T> inline T read() { register T data = 0, w = 1; register char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) data = data * 10 + ch - '0', ch = getchar(); return data * w; } template <class T> inline T read(register T& x) { return x = read<T>(); } const int N = 2020, P1 = 1e9 + 7, P2 = 1e9 + 9; int n, m, ans; int tm1[N], tm2[N], tm1_[N], tm2_[N]; int f[2][N][N]; struct dd { int x, y; bool operator==(const dd& n) const { return x == n.x && y == n.y; } } t1, t2, t3, t4; struct cc { char s[N]; int len, pre1[N], pre2[N], suf1[N], suf2[N]; void read() { scanf("%s", s + 1), len = strlen(s + 1); for (int i = 1; i <= len; ++i) { pre1[i] = ((long long)tm1[i - 1] * s[i] + pre1[i - 1]) % P1; pre2[i] = ((long long)tm2[i - 1] * s[i] + pre2[i - 1]) % P2; } for (int i = len; i; --i) { suf1[i] = ((long long)tm1[len - i] * s[i] + suf1[i + 1]) % P1; suf2[i] = ((long long)tm2[len - i] * s[i] + suf2[i + 1]) % P2; } } dd get_hash(int l, int r) { dd t; if (l <= r) { t.x = (long long)tm1_[l - 1] * (pre1[r] + P1 - pre1[l - 1]) % P1; t.y = (long long)tm2_[l - 1] * (pre2[r] + P2 - pre2[l - 1]) % P2; } else { t.x = (long long)tm1_[len - l] * (suf1[r] + P1 - suf1[l + 1]) % P1; t.y = (long long)tm2_[len - l] * (suf2[r] + P2 - suf2[l + 1]) % P2; } return t; } } s1, s2, w; int ksm(int x, int y, int P) { int z = 1; for (; y; y >>= 1, x = (long long)x * x % P) if (y & 1) z = (long long)z * x % P; return z; } void init() { int n = 2000; tm1[0] = tm2[0] = tm1_[0] = tm2_[0] = 1; for (int i = 1; i <= n; ++i) tm1[i] = 31LL * tm1[i - 1] % P1, tm2[i] = 31LL * tm2[i - 1] % P2; tm1_[n] = ksm(tm1[n], P1 - 2, P1), tm2_[n] = ksm(tm2[n], P2 - 2, P2); for (int i = n - 1; i; --i) tm1_[i] = 31LL * tm1_[i + 1] % P1, tm2_[i] = 31LL * tm2_[i + 1] % P2; s1.read(), s2.read(), w.read(); } int main() { init(); n = s1.len, m = w.len; f[0][n + 1][0] = f[1][n + 1][0] = 1; for (int i = n; i; --i) { f[0][i][0] = f[1][i][0] = 1; for (int k = 2; k + k <= m && i + k - 1 <= n; ++k) { t1 = s1.get_hash(i, i + k - 1), t2 = s2.get_hash(i, i + k - 1); t3 = w.get_hash(m, m - k + 1), t4 = w.get_hash(m - k - k + 1, m - k); if (t1 == t3 && t2 == t4) f[1][i][k + k] = 1; if (t2 == t3 && t1 == t4) f[0][i][k + k] = 1; } } for (int i = n; i; --i) for (int k = 1; k <= m; ++k) { if (s1.s[i] == w.s[m - k + 1]) (f[0][i][k] += f[0][i + 1][k - 1]) %= P1; if (s2.s[i] == w.s[m - k + 1]) (f[1][i][k] += f[1][i + 1][k - 1]) %= P1; if (k > 1 && s1.s[i] == w.s[m - k + 1] && s2.s[i] == w.s[m - k + 2]) (f[0][i][k] += f[1][i + 1][k - 2]) %= P1; if (k > 1 && s2.s[i] == w.s[m - k + 1] && s1.s[i] == w.s[m - k + 2]) (f[1][i][k] += f[0][i + 1][k - 2]) %= P1; } for (int i = 1; i <= n + 1; ++i) { (ans += f[0][i][m]) %= P1; (ans += f[1][i][m]) %= P1; for (int k = 2; k + k <= m && k < i; ++k) { t1 = s1.get_hash(i - k, i - 1), t2 = s2.get_hash(i - k, i - 1); t3 = w.get_hash(k, 1), t4 = w.get_hash(k + 1, k + k); if (t1 == t3 && t2 == t4) (ans += f[1][i][m - k - k]) %= P1; if (t2 == t3 && t1 == t4) (ans += f[0][i][m - k - k]) %= P1; } } if (m == 1) return printf("%d\n", ans), 0; memset(f, 0, sizeof f); f[0][n + 1][0] = f[1][n + 1][0] = 1; for (int i = n; i; --i) { f[0][i][0] = f[1][i][0] = 1; for (int k = 2; k + k < m && i + k - 1 <= n; ++k) { t1 = s1.get_hash(i, i + k - 1), t2 = s2.get_hash(i, i + k - 1); t3 = w.get_hash(1, k), t4 = w.get_hash(k + k, k + 1); if (t1 == t3 && t2 == t4) f[1][i][k + k] = 1; if (t2 == t3 && t1 == t4) f[0][i][k + k] = 1; } } for (int i = n; i; --i) for (int k = 1; k <= m; ++k) { if (s1.s[i] == w.s[k]) (f[0][i][k] += f[0][i + 1][k - 1]) %= P1; if (s2.s[i] == w.s[k]) (f[1][i][k] += f[1][i + 1][k - 1]) %= P1; if (m > 2 && k > 1 && s1.s[i] == w.s[k] && s2.s[i] == w.s[k - 1]) (f[0][i][k] += f[1][i + 1][k - 2]) %= P1; if (m > 2 && k > 1 && s2.s[i] == w.s[k] && s1.s[i] == w.s[k - 1]) (f[1][i][k] += f[0][i + 1][k - 2]) %= P1; } for (int i = 1; i <= n + 1; ++i) { (ans += f[0][i][m]) %= P1; (ans += f[1][i][m]) %= P1; for (int k = 2; k + k < m && k < i; ++k) { t1 = s1.get_hash(i - k, i - 1), t2 = s2.get_hash(i - k, i - 1); t3 = w.get_hash(m - k + 1, m), t4 = w.get_hash(m - k, m - k - k + 1); if (t1 == t3 && t2 == t4) (ans += f[1][i][m - k - k]) %= P1; if (t2 == t3 && t1 == t4) (ans += f[0][i][m - k - k]) %= P1; } } return printf("%d\n", ans), 0; }
3,200
CPP
#include <bits/stdc++.h> using namespace std; bool seen[101]; int num[200 + 2]; int dir[200 + 2]; int cnt, n, swapPos; bool check() { for (int i = 0; i < 2 * n; i++) if (dir[i] != 0) return false; return true; } void FILL() { memset(seen, 0, sizeof seen); for (int i = 0; i < 2 * n; i++) { if (seen[num[i]]) dir[i] = -1; else dir[i] = 1, seen[num[i]] = true; if (i != 2 * n - 1) { if (num[i] == num[i + 1]) dir[i] = dir[i + 1] = 0; } } for (int i = 0; i < 2 * n - 1; i++) { if (num[i] == num[i + 1]) dir[i] = dir[i + 1] = 0; } } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < 2 * n; i++) cin >> num[i]; FILL(); while (true) { bool f = false; for (int i = swapPos; i < 2 * n - 1; i++) { if ((dir[i] == 1 && dir[i + 1] == 0) || (dir[i] == 1 && dir[i + 1] == -1)) { int temp; temp = num[i]; num[i] = num[i + 1]; num[i + 1] = temp; swapPos = i; f = true; break; } } FILL(); if (!f) { if (!check()) { swapPos = 0; cnt--; } else { break; } } cnt++; } cout << cnt << endl; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[1001][1001]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; vector<int> stolv(m + 1); vector<int> stoln(m + 1); vector<int> strv(n + 1); vector<int> strn(n + 1); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (a[j][i] == 1 && stolv[i] == 0) { stolv[i] = j; } } } for (int i = 1; i <= m; i++) { for (int j = n; j >= 1; j--) { if (a[j][i] == 1 && stoln[i] == 0) { stoln[i] = j; } } } for (int i = 1; i <= n; i++) { for (int j = m; j >= 1; j--) { if (a[i][j] == 1 && strn[i] == 0) { strn[i] = j; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == 1 && strv[i] == 0) { strv[i] = j; } } } int count = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == 0) { if (strv[i] < j && strv[i] != 0) count += 1; if (strn[i] > j && strn[i] != 0) count += 1; if (stolv[j] < i && stolv[j] != 0) count += 1; if (stoln[j] > i && stoln[j] != 0) count += 1; } } } cout << count; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; char v[n]; cin >> s; strcpy(v, s.c_str()); int t = 1, d = 0; if (n == 1) { cout << 1; return 0; } for (int i = 1; i <= n - 1; i++) { if (v[i] != v[i - 1]) t++; } for (int i = 1; i <= n - 1; i++) { if (v[i] == v[i - 1]) d++; } cout << (t + min(d, 2)); return 0; }
1,600
CPP
def read_ints(): return map(int, input().split()) t_n = int(input()) for i_t in range(t_n): n = int(input()) a = list(read_ints()) deltas = [x2 - x1 for x1, x2 in zip(a, a[1:])] r = [0] * n acc = 0 for i, delta in enumerate(deltas): if delta > 0: acc += delta r[i+1] += acc acc = 0 for i, delta in list(enumerate(deltas))[::-1]: if delta < 0: acc += abs(delta) r[i] += acc if all(x >= rx for x, rx in zip(a, r)): print("YES") else: print("NO")
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, i, j, flag; cin >> n; int arr[100], a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); arr[0] = 1; int c = 1; for (i = 1; i < n; i++) { flag = 0; for (j = 0; j < c; j++) { if (arr[j] <= a[i]) { arr[j]++; flag = 1; break; } } if (flag == 0) { arr[c] = 1; c++; } } cout << c << "\n"; }
1,400
CPP
if __name__ == "__main__": n = int(input()) counter = 0 for i in [100,20,10,5,1]: #print('---- n %d' %n) counter += n//i #print(counter) n = n%i #print('end') print(counter)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const int mod = 1e9 + 7; const int sqr = 317; long long n, m; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cin >> n >> m; long long x = ceil((long double)n / 2) - m; if (x < 0) cout << "0 "; else cout << x * 2 - (n % 2 && x) << " "; if (!m) cout << n << endl, exit(0); long long y = 0, c = 0; for (int i = 0; i < n; i++) { y += c++; if (y >= m) { cout << n - i - 1 << endl; exit(0); } } cout << "0\n"; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string s1, s2; cin >> s1; cin >> s2; if (((s1[0] == '<') && (s2[0] == '^')) || ((s1[0] == '>') && (s2[m - 1] == '^')) || ((s1[n - 1] == '<') && (s2[0] == 'v')) || ((s1[n - 1] == '>') && (s2[m - 1] == 'v'))) cout << "NO"; else cout << "YES"; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; const int MAXL = 123, MOD = 12345, MAXN = 26 + 5, CNTALP = 26; struct Matrix { int num[MAXL][MAXL]; Matrix() { memset(num, 0, sizeof(num)); } Matrix operator*(const Matrix &b) const { Matrix c; for (int i = 0; i < MAXL; ++i) for (int j = 0; j < MAXL; ++j) for (int k = 0; k < MAXL; ++k) c.num[i][j] = (c.num[i][j] + num[i][k] * b.num[k][j]) % MOD; return c; } void Print() const { for (int i = 0; i < MAXL; ++i, puts("")) for (int j = 0; j < MAXL; ++j) printf("%d ", num[i][j]); } } B; Matrix Power(Matrix a, long long b) { Matrix d; for (int i = 0; i < MAXL; ++i) d.num[i][i] = 1; for (; b; d = (b & 1) ? d * a : d, a = a * a, b /= 2) ; return d; } int pdt[MAXN], b[MAXN], m, N; long long n; bool used[MAXN]; vector<int> fac[MAXN]; char str[10]; int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int lcm(int x, int y) { return x * y / gcd(x, y); } void init() { cin >> n >> m; for (int i = 0; i < CNTALP; ++i) b[i] = 1; memset(used, 0, sizeof(used)); for (int i = 1; i <= m; ++i) { int x, id; scanf("%s%d", str, &x); id = str[0] - 'A'; used[id] = true; fac[id].push_back(x); b[id] = lcm(b[id], x); } pdt[0] = 1; for (int i = 1; i <= CNTALP; ++i) pdt[i] = pdt[i - 1] * b[i - 1]; N = pdt[CNTALP]; } void solve() { for (int i = 0; i < CNTALP; ++i) if (used[i]) for (int k = 0; k < N; ++k) { int tmp = k % pdt[i + 1] / pdt[i]; ++B.num[k][k - tmp * pdt[i] + (tmp + 1) % b[i] * pdt[i]]; } B = Power(B, n); int CC = 0; for (int i = 0; i < N; ++i) { bool flag = true; for (int j = 0; j < CNTALP; ++j) if (used[j]) { bool flag1 = true; for (int k = 0; k < (int)fac[j].size(); ++k) if (i % pdt[j + 1] / pdt[j] % fac[j][k] == 0) { flag1 = false; break; } if (flag1) { flag = false; break; } } if (flag) CC = (CC + B.num[0][i]) % MOD; } cout << CC << endl; } int main() { init(); solve(); fclose(stdin); fclose(stdout); return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k1, k2, a, p1 = 0, p2 = 0; cin >> n >> k1 >> k2; for (int i = 0; i < k1; i++) { cin >> a; p1 = max(a, p1); } for (int i = 0; i < k2; i++) { cin >> a; p2 = max(a, p2); } if (p1 > p2) cout << "YES\n"; else cout << "NO\n"; } return 0; }
800
CPP
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); if (n & 1) n = 0; n /= 2; n -= 1; n /= 2; printf("%d\n", n); return 0; }
1,000
CPP
n,m=map(int,input().split()) a=min(n,m) b=max(n,m) print(a,(b-a)//2)
800
PYTHON3
#include <bits/stdc++.h> #pragma optimize("-O2") using namespace std; long long n, ans, cand; char str[1111]; int main() { scanf("%lld", &n); scanf("%s", str); int len = strlen(str); str[len] = '0'; len++; for (int i = 0; i < len; i++) { if (str[i] == '1') cand++; else ans = ans * 10LL + cand, cand = 0; } printf("%lld\n", ans); }
1,100
CPP
n = int(input()) sum = 0 for _ in range(n): line = input().split(' ').count('1') if line >= 2: sum += 1 print(sum)
800
PYTHON3
def main(): len_s=int(input()) s=input() x_cnt=0 rm_cnt=0 for i in range(len_s): if s[i]=="x": x_cnt+=1 else: rm_cnt+=max(0,x_cnt-2) x_cnt=0 rm_cnt+=max(0,x_cnt-2) print(rm_cnt) if __name__=="__main__": main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve(string s, int n) { map<pair<int, int>, int> pos; map<pair<int, int>, bool> mep; pair<int, int> at = {0, 0}; pos[at] = 0; mep[at] = true; int ans = 202020; int lans = -1, rans = -1; for (int i = 0; i < n; i++) { if (s[i] == 'U') at.second++; if (s[i] == 'D') at.second--; if (s[i] == 'L') at.first--; if (s[i] == 'R') at.first++; if (mep[at]) { if ((i + 1) - pos[at] < ans) { ans = (i + 1) - pos[at]; lans = pos[at] + 1; rans = i + 1; } } mep[at] = true; pos[at] = i + 1; } if (lans == rans) cout << "-1\n"; else cout << lans << " " << rans << "\n"; return; } int main() { int t; cin >> t; while (t--) { string s; int n; cin >> n; cin >> s; solve(s, n); } return 0; }
1,500
CPP
from math import ceil a, b, c = map(int, input().split()) num = ceil(a / c) * ceil(b / c) print(num)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1 << 17; const int MOD = 1e9 + 7, mod2 = 1e9 + 9; const int inv2 = (MOD + 1) / 2; int n, m, u, v, w, vis[N], dfn[N], y, ans, c[44], t; pair<int, int> now[N]; vector<pair<int, int> > e[N]; long long cnt[44][N], ex[44][N], res; void FWT_xor(long long *a, int opt, int MOD) { for (int i = 1; i < N; i <<= 1) for (int p = i << 1, j = 0; j < N; j += p) for (int k = 0; k < i; ++k) { int X = a[j + k], Y = a[i + j + k]; a[j + k] = (X + Y) % MOD; a[i + j + k] = (X + MOD - Y) % MOD; if (opt == -1) a[j + k] = 1ll * a[j + k] * inv2 % MOD, a[i + j + k] = 1ll * a[i + j + k] * inv2 % MOD; } } void dfs(int u, int fa, int ww) { vis[u] = 1; now[++y] = pair<int, int>(u, ww); dfn[u] = y; for (auto &x : e[u]) { int v = x.first, w = x.second; if (v == fa) continue; if (!vis[v]) { dfs(v, u, w); } else if (vis[v] == 1) { ++t; cnt[t][w]++; ex[t][w]++; for (int i = dfn[u]; i > dfn[v]; --i) { int z = now[i].second; cnt[t][z]++; ex[t][z]++; } } } --y; vis[u] = 2; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { scanf("%d%d%d", &u, &v, &w); ans ^= w; e[u].push_back(pair<int, int>(v, w)); e[v].push_back(pair<int, int>(u, w)); } for (int i = 1; i <= n; ++i) { if (!vis[i]) { dfs(i, -1, 0); } } cnt[0][ans] = ex[0][ans] = 1; FWT_xor(cnt[0], 1, MOD); FWT_xor(ex[0], 1, mod2); for (int i = 1; i <= t; ++i) { FWT_xor(cnt[i], 1, MOD); FWT_xor(ex[i], 1, mod2); for (int j = 0; j < N; ++j) { cnt[i][j] = 1ll * cnt[i - 1][j] * cnt[i][j] % MOD; ex[i][j] = 1ll * ex[i - 1][j] * ex[i][j] % mod2; } } FWT_xor(cnt[t], -1, MOD); FWT_xor(ex[t], -1, mod2); for (int i = 0; i < N; ++i) { if (!cnt[t][i] && !ex[t][i]) continue; printf("%d %d", i, cnt[t][i]); break; } return 0; }
2,400
CPP
k,l,m,n,d = int(input()),int(input()),int(input()),int(input()),int(input()) q = [(i%k and i%l and i%m and i%n) == 0 for i in range(1, d+1)] print(sum(q))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long INF = ((long long)1 << 59); int main() { std::ios::sync_with_stdio(false); int n, q, op; int move; scanf("%d%d", &n, &q); int odd_s = 0; int even_s = 1; int o_e_pos = 0; vector<int> res(n); for (int j = 0; j < q; j++) { scanf("%d", &op); if (op == 1) { scanf("%d", &move); even_s = ((even_s + move) + n) % n; odd_s = ((odd_s + move) + n) % n; } else { if (odd_s % 2 == 0) { odd_s = (odd_s + 1) % n; even_s = ((even_s - 1) + n) % n; } else { odd_s = ((odd_s - 1) + n) % n; even_s = (even_s + 1) % n; } } } int s_o = 1; int s_e = 2; for (int i = 0; i < n; i += 2) { res[(odd_s + i) % n] = s_o; res[(even_s + i) % n] = s_e; s_o += 2; s_e += 2; } printf("%d", res[0]); for (int i = 1; i < n; ++i) { printf(" %d", res[i]); } return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 1, x = 0; 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; } void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int MAXN = 1e6 + 5; int n, m, h[MAXN], k[MAXN], s[MAXN]; int main() { ios::sync_with_stdio(0); n = read(); m = read(); for (int i = 1; i <= m; ++i) h[i] = read(); int sum = 0; for (int i = 1; i < m; ++i) { sum += h[i]; k[i] = sum % n; } k[m] = n; sort(k + 1, k + m + 1); for (int i = 1; i <= m; ++i) { s[i] = k[i] - k[i - 1]; } int t = 0, cur = 1; vector<int> a; for (int i = 1; i <= m; ++i) { while (h[i] > 0) { a.push_back(i); h[i] -= s[cur++]; if (cur == m + 1) cur = 1, t++; } } if (cur != 1) t++; while (a.size() % m != 0) a.push_back(1); write(t); puts(""); for (int i = 1; i <= m; ++i) write(s[i]), putchar(' '); puts(""); for (int i = 0; i < (int)a.size(); ++i) { write(a[i]), putchar(' '); if (i % m == m - 1) puts(""); } return 0; }
3,100
CPP
import math import os import random import re import sys if __name__ == '__main__': n = int(input()) coordinates = list(map(int, input().split())) if n == 1: print(0) else: odd_coordinates = len(list(filter(lambda x: x % 2 != 0, coordinates))) even_coordinates = n - odd_coordinates if odd_coordinates == 0 or even_coordinates == 0: print(0) else: print(min(even_coordinates, odd_coordinates))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int K = 100007; const int N = 800007; char st[N], buf[N]; int n, k; string A[K], B[K]; struct node { int nxt[26]; vector<int> mas; node() { memset(nxt, -1, sizeof nxt); } } T[N]; int dep[N], size = 1; void add_string(string &st, int idx) { int root = 0; for (char c : st) { if (T[root].nxt[c - 'a'] == -1) { T[root].nxt[c - 'a'] = size++; dep[T[root].nxt[c - 'a']] = dep[root] + 1; } root = T[root].nxt[c - 'a']; } T[root].mas.push_back(idx); } long long ans = 0; int pr[K + K]; pair<vector<int> *, vector<int> *> dfs(int u) { vector<int> *A = NULL, *B = NULL; vector<vector<int> *> As, Bs; for (int i = 0; i < (26); ++i) { if (T[u].nxt[i] != -1) { auto r = dfs(T[u].nxt[i]); As.push_back(r.first); Bs.push_back(r.second); } } if ((int)(As).size() == 0) { A = new vector<int>(); B = new vector<int>(); } else { int ba = 0; for (int i = 0; i < ((int)(As).size()); ++i) { if (As[i]->size() > As[ba]->size()) ba = i; } int bb = 0; for (int i = 0; i < ((int)(Bs).size()); ++i) { if (Bs[i]->size() > Bs[bb]->size()) bb = i; } A = As[ba]; B = Bs[bb]; for (int i = 0; i < ((int)(As).size()); ++i) { if (i == ba) continue; vector<int> &v = *As[i]; for (auto x : v) A->push_back(x); As[i]->clear(); As[i]->shrink_to_fit(); } for (int i = 0; i < ((int)(Bs).size()); ++i) { if (i == bb) continue; vector<int> &v = *Bs[i]; for (auto x : v) B->push_back(x); Bs[i]->clear(); Bs[i]->shrink_to_fit(); } } for (int idx : T[u].mas) { if (idx < k) A->push_back(idx); else B->push_back(idx); } while (A->size() && B->size()) { ans += dep[u]; pr[A->back()] = B->back(); A->pop_back(); B->pop_back(); } return make_pair(A, B); } void solve() { scanf("%d", &k); for (int i = 0; i < (k); ++i) { scanf("%s", buf); A[i] = string(buf, buf + strlen(buf)); add_string(A[i], i); } for (int i = 0; i < (k); ++i) { scanf("%s", buf); B[i] = string(buf, buf + strlen(buf)); add_string(B[i], i + k); } auto p = dfs(0); printf("%lld\n", ans); for (int i = 0; i < (k); ++i) { printf("%d %d\n", i + 1, pr[i] + 1 - k); } } void testGen() { FILE *f = fopen("input.txt", "w"); fclose(f); } int main() { cerr << fixed; cerr.precision(3); cout << fixed; cout.precision(9); solve(); return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, pos; cin >> n; cin >> a; cin >> b; pos = n - a; if (pos <= b) { cout << pos << endl; } else { cout << b + 1 << endl; } return 0; }
1,000
CPP
import sys data = sys.stdin.read().splitlines() x, y, z = map(int, data[0].split()) a, b, c = map(int, data[1].split()) # Andrew only green # Dmitry green and purple # Michal any types if a >= x and (a - x) + b >= y and c + (a - x) + b - y >= z: print('YES') else: print('NO')
800
PYTHON3
r, c = list(map(int,input().split())) m1 = [list(map(int,input().split())) for i in range(r)] m2 = [list(map(int,input().split())) for i in range(r)] wiersze1 = [] wiersze2 = [] kolumny1 = [] kolumny2 = [] for i in range(r): wiersze1.append(sum(m1[i])) wiersze2.append(sum(m2[i])) m1tr = [[m1[a][b] for a in range(r)] for b in range(c)] m2tr = [[m2[a][b] for a in range(r)] for b in range(c)] for i in range(c): kolumny1.append(sum(m1tr[i])) kolumny2.append(sum(m2tr[i])) import sys for i in range(c): if (kolumny1[i] - kolumny2[i]) % 2 != 0: print("NO") sys.exit(0) for i in range(r): if (wiersze1[i] - wiersze2[i]) % 2 != 0: print("NO") sys.exit(0) print("YES")
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long n, long long lelo) { if (n == 0) return 1; if (n == 1) return a; if (n == 2) return (a * a) % lelo; if (n % 2) return (a * power(a, n - 1, lelo)) % lelo; else return power(power(a, n / 2, lelo), 2, lelo) % lelo; } void swap(long long &a, long long &b) { auto tm = a; a = b; b = tm; } const long long N = 1e5 + 5; const long long INF = 1e18; long long b, w; map<pair<long long, long long>, long long> m; vector<pair<long long, long long> > ans; void doit(long long x, long long y) { if (x < 1 || y < 1 || x > 1e9 || y > 1e9) return; if (m[{x, y}]) return; m[{x, y}] = 1; if ((x + y) % 2) { if (b > 0) { b--; ans.push_back({x, y}); } else return; } else { if (w > 0) { w--; ans.push_back({x, y}); } else return; } doit(x - 1, y); doit(x + 1, y); doit(x, y - 1); doit(x, y + 1); } void solve() { ans.clear(); m.clear(); cin >> b >> w; doit(1e7, 1e7); if (b != 0 || w != 0) { cout << "NO"; return; } cout << "YES" << "\n"; for (auto &(i) : (ans)) cout << i.first << " " << i.second << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long double tt = 1.0 * clock(); long long t = 1; cin >> t; for (long long(i) = (1); (i) < (t + 1); (i)++) { solve(); if (i < t) cout << "\n"; } tt = 1.000000000 * (clock() - tt) / CLOCKS_PER_SEC; return 0; }
1,800
CPP
word1=str(input()) word2=str(input()) if word1!=word2[::-1]: print('NO') else: print('YES')
800
PYTHON3
# ========= /\ /| |====/| # | / \ | | / | # | /____\ | | / | # | / \ | | / | # ========= / \ ===== |/====| # code if __name__ == "__main__": s = [] k = int(input()) for i in range(1,k+1): s.append(i) s = ''.join(map(str,s)) print(s[k-1])
1,000
PYTHON3
#include <bits/stdc++.h> int main() { int a, b; scanf("%d %d", &a, &b); int fc, uc, df, sum = 0; fc = a; uc = 0; df = b; while (1) { sum += fc; if (fc == 0 && uc < df) break; uc += fc; fc = uc / df; uc = uc % df; } printf("%d\n", sum); return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; using vb = vector<bool>; using ll = long long; int main() { int n; cin >> n; int k = 0; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; k = max(k, x + y); } cout << k << endl; }
900
CPP
#include <bits/stdc++.h> int main() { int n, t; scanf("%d%d", &n, &t); printf("%.12f\n", n * pow(1.000000011, t)); }
1,200
CPP
#include <bits/stdc++.h> using namespace std; map<string, int> mp; set<string> st; map<string, string> nt; int main() { ios_base::sync_with_stdio(false); string a, ans; int n, q; cin >> n; for (int i = 0; i < n; i++) { cin >> a; st.clear(); for (int i = 0; i < a.size(); i++) { ans = ""; for (int j = i; j < a.size(); j++) { ans += a[j]; nt[ans] = a; st.insert(ans); } } for (set<string>::iterator it = st.begin(); it != st.end(); it++) mp[*it]++; } cin >> q; for (int i = 0; i < q; i++) { cin >> a; int pans = 0; pans = mp[a]; cout << pans << " "; if (pans == 0) cout << "-\n"; else cout << nt[a] << "\n"; } return 0; }
1,500
CPP
def gcd(a, b): if b == 0: return a return gcd(b, a%b) # least common multiple def lcm(a,b): return (a*b) / gcd(a,b) def read(func = int): return map(func, input().split()) t = int(input()) for _ in range(t): input() l = list(read()) a = [] for i in l: if i not in a: print(i, end= ' ') a.append(i) print()
800
PYTHON3
def f(n): print(n) t = int(input()) for i in range(t): n=int(input()) f(n)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int n, x, y; string s; cin >> n >> x >> y >> s; int stanje = -1, broj = 0; for (char c : s) { if (stanje == -1) { if (c == '0') stanje = 0, broj = 1; else stanje = 1, broj = 0; } else if (stanje == 0) { if (c == '1') stanje = 1; } else if (stanje == 1) { if (c == '0') broj++, stanje = 0; } } cout << (broj ? (broj - 1ll) * min(x, y) + y : 0) << '\n'; }
1,500
CPP
import sys, os import heapq, functools, collections import math, random from collections import Counter, defaultdict # available on Google, not available on Codeforces # import numpy as np # import scipy def solve_(lst): # your solution here res1 = 0 res0 = 0 for a,b in zip(lst, lst[1:]): if a == b == "1": res1 += 1 if a == b == "0": res0 += 1 return max(res1, res0) def console(*args): # print on terminal in different color print('\033[36m', *args, '\033[0m', file=sys.stderr) pass # if Codeforces environment if os.path.exists('input.txt'): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") def console(*args): pass def solve(*args): # screen input console("----- solving ------") console(*args) console("----- ------- ------") return solve_(*args) if True: # if memory is not a constraint inp = iter(sys.stdin.buffer.readlines()) input = lambda: next(inp) else: # if memory is a constraint input = sys.stdin.buffer.readline for case_num in range(int(input())): # read line as a string _ = input() strr = input() strr = [chr(x) for x in strr.strip()] # read line as an integer # k = int(input()) # read one line and parse each word as a string # lst = input().split() # read one line and parse each word as an integer # lst = list(map(int,input().split())) # read matrix and parse as integers (after reading read nrows) # lst = list(map(int,input().split())) # nrows = lst[0] # index containing information, please change # grid = [] # for _ in range(nrows): # grid.append(list(map(int,input().split()))) res = solve(strr) # please change # post processing methods # res = [str(x) for x in res] # res = " ".join(res) # print result # Google - case number required # print("Case #{}: {}".format(case_num+1, res)) # Codeforces - no case number required print(res)
1,200
PYTHON3
n = int(input()) dp = [0] * (n + 1) dp[1] = 1 f = 1 for i in range(2, n + 1): f *= i f %= 998244353 dp[i] = dp[i - 1] * i + f - i dp[i] %= 998244353 print(dp[n])
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a[100100]; long long Acc[100100]; int main() { int n, k; int ans = 0, Max = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; if (i == 0) Acc[i] = a[i]; else Acc[i] = Acc[i - 1] + a[i]; } sort(a, a + n); for (int i = 0; i < n; i++) { if (i == 0) Acc[i] = a[i]; else Acc[i] = Acc[i - 1] + a[i]; } for (int i = 0; i < n; i++) { int l = 0, r = i; long long sum = Acc[i] - a[i]; while (l < r) { int mid = (l + r) / 2; long long s = sum - Acc[mid] + a[mid]; if ((i - mid) * a[i] - s <= k) r = mid; else l = mid + 1; } if (i - l + 1 > Max) { Max = i - l + 1; ans = a[i]; } } cout << Max << ' ' << ans << endl; return 0; }
1,600
CPP
#include <bits/stdc++.h> int n, h; long long f[45][45]; signed main() { scanf("%d%d", &n, &h); f[0][0] = 1; f[1][0] = f[1][1] = 1; for (int i = 2; i <= n; i++) { for (int j = 1; j <= h; j++) { for (int k = 0; k < i; k++) f[i][j] += f[k][j - 1] * f[i - 1 - k][0] + f[k][0] * f[i - 1 - k][j - 1] - f[k][j - 1] * f[i - k - 1][j - 1]; } f[i][0] = f[i][1]; } printf("%lld\n", f[n][h]); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; void solve(); int main(int argc, char const* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); } bool comp(pair<int, int>& a, pair<int, int>& b) { if (a.first < b.first) return true; if (a.first == b.first) return a.second > b.second; return false; } void solve() { int v; cin >> v; int n = 9; pair<int, int> a[n]; for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i + 1; } sort(a, a + n, comp); if (v < a[0].first) { cout << -1 << endl; return; } int d = v / a[0].first; string ans = string(d, char(a[0].second + '0')); v %= a[0].first; for (int dig = 0; dig < d; dig++) { int prev = a[0].first; for (int i = 8; i >= 0; i--) { if (v + prev >= a[i].first && a[i].second > ans[dig] - '0') { ans[dig] = char(a[i].second + '0'); v = v + prev - a[i].first; prev = a[i].first; } } } cout << ans << endl; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; long long int m, n, s1; long long int v[200100]; bool f(int mid) { long long int c = 0, sum = 0; for (int i = 0; i < n; ++i) { if (i && i % mid == 0) c++; sum += max((long long int)0, v[i] - c); } if (sum >= m) return true; else return false; } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { scanf("%lld", &v[i]); } sort(v, v + n); reverse(v, v + n); int l = 1; int r = n; int mid = (l + r) / 2; long long int ans = -1; while (l <= r) { mid = (l + r) / 2; if (f(mid)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } printf("%lld\n", ans); return 0; }
1,700
CPP
n = int(input()) wn = list(map(int, input().split())) if sum(wn) % 200 == 0 and n % 2 != 1: print("YES") elif sum(wn) // n == 200: print("NO") elif sum(wn) % 200 != 0: print("NO") else: print("YES")
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(void) { int a, b, c, n; cin >> a >> b >> c >> n; if (a >= n) { cout << -1 << endl; } else if (b >= n) { cout << -1 << endl; } else if (c >= n || c > a || c > b) { cout << -1 << endl; } else if ((n - a - b + c) < 1) { cout << -1 << endl; } else { cout << n - a - b + c << endl; } return 0; }
1,000
CPP
n = int(input()) s = input() a = list(map(int, s.split(' '))) a.sort() if (a[0] + a[-1]) // 2 in a: print((a[0] + a[-1]) // 2) else: #just to be sure for i in range(1, 91): t = (a[0] + a[-1]) // 2 if t + i in a: print(t + i) break elif t - i in a: print(t - i) break
800
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 gcd(b%a, a) return b ## prime factorization def primefs(n): ## if n == 1 if n == 1: return 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())) ################# ---------------- TEMPLATE ENDS HERE ---------------- ################# for _ in range(int(input())): a,b,c,d = int_array(); print(*(a,c,c))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int pr[1000111]; int main() { long long x2, x0 = 100000000; cin >> x2; for (int i = 2; i < x2; i++) { if (pr[i] == 0) { for (int j = i + i; j <= x2; j += i) { pr[j] = i - 1; } } } for (long long x1 = x2 - pr[x2]; x1 <= x2; x1++) { x0 = min(x0, x1 - pr[x1]); } cout << x0; }
1,700
CPP
for _ in range(int(input())): sz=int(input()) l=list(map(int,input().split())) condn=True even=0 odd=0 for i in range(sz): if((i%2==0)and(l[i]%2==0))or((i%2!=0)and(l[i]%2!=0)): continue else: condn=False if(i%2==0): even+=1 else: odd+=1 if(condn==False): if(odd==even): print(odd) else: print(-1) elif(condn==True): print(0)
800
PYTHON3
a = int(input()) while (sum(map(int, list(str(a))))%4 != 0): a += 1 print(a)
800
PYTHON3
n,m = list(map(int,input().split())) mat = [] ans = -m*n for i in range(n): mat.append(list(map(int,input().split()))) total = sum(mat[-1]) ans+=(2**total-1) ans+=((2**(m-total))-1) for i in range(m): total = 0 for j in range(n): total+=mat[j][i] ans+=(2**total-1) ans+=((2**(n-total))-1) print(ans)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; bool hb[100], hg[100]; int main() { int b, g; cin >> b >> g; int h; cin >> h; while (h--) { int a; cin >> a; hb[a] = 1; } cin >> h; while (h--) { int a; cin >> a; hg[a] = 1; } for (int i = 0; i <= 1000000; i++) { int tb = i % b; int tg = i % g; if (hb[tb] == 1 || hg[tg] == 1) { hb[tb] = 1; hg[tg] = 1; } } int res = 0; for (int i = 0; i < b; i++) { if (hb[i] == 1) res++; } for (int i = 0; i < g; i++) { if (hg[i] == 1) res++; } if (res == b + g) { cout << "YES"; } else cout << "NO"; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rnd64( chrono::high_resolution_clock::now().time_since_epoch().count()); long long power(long long a, long long m, long long mod) { long long ans = 1; while (m) { if (m % 2) ans *= a, ans %= (mod); a = (a * a) % (mod); m >>= 1; } return ans; } void init(long long* a, long long n, long long val) { for (long long i = 0; i < n; i++) a[i] = val; } long long dp[2001][2001][2][2]; string a, b; long long m, d; long long func(long long ind, long long mod, long long s1, long long s2) { long long i; if (ind == a.length()) return (!mod); if (dp[ind][mod][s1][s2] != -1) return dp[ind][mod][s1][s2]; long long k = 0; long long mn = 0; if (s1) mn = a[ind] - '0'; long long mx = 9; if (s2) mx = b[ind] - '0'; if (ind % 2) { if (!(s1 && d < a[ind] - '0' || s2 && d > b[ind] - '0')) k += func(ind + 1, (mod * 10 + d) % m, s1 & (d == (a[ind] - '0')), s2 & (d == (b[ind] - '0'))); } else for (i = mn; i < mx + 1; i++) { if (i == d) continue; k += func(ind + 1, (mod * 10 + i) % m, s1 & (i == (a[ind] - '0')), s2 & (i == (b[ind] - '0'))); k %= 1000000007; } return dp[ind][mod][s1][s2] = k; } void solve() { long long i, j, _; cin >> m >> d >> a >> b; memset(dp, -1, sizeof(dp)); cout << func(0, 0, 1, 1) << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long T = 1; while (T--) { solve(); } return 0; }
2,200
CPP
import sys import math def I(): return int(sys.stdin.readline()) def IL(): return list(map(int,sys.stdin.readline().strip().split())) def ILS(): return list(map(str,sys.stdin.readline().strip().split())) t = I() for iii in range(t): n,m = IL() ans = 0 extra = 0 if n%2 ==0: x = n//2 y = m elif m%2==0: y = m//2 x = n else : x = (n-1)//2 y = m-1 extra = n//2 + m//2 + 1 x,y,extra = int(x),int(y),int(extra) ans = x*y + extra print(ans) pass
800
PYTHON3
def my_fun(n): a = [n] for i in range(1, n): a.append(i) return a print(*my_fun(int(input())))
1,000
PYTHON3
import sys sys.setrecursionlimit(2147483647) input=sys.stdin.readline # 6,10,14,15,21,22,26,33 # 36,40,44 def solve(n): if n % 4 == 0: res = n // 4 return '9'*(n-res)+'8'*res else: res = n // 4 + 1 return '9'*(n-res)+'8'*res def main(): t = int(input()) arr = [] for _ in range(t): arr.append(solve(int(input()))) for a in arr: print(a) if __name__=='__main__': main()
1,000
PYTHON3
s=input() a1,a2,a3,a4,a5=0,0,0,0,0 for i in range(len(s)): if s[i]=='h' or a1==1: a1=1 if s[i]=='e' or a2==1: a2=1 if s[i]=='l' or a3==1: if a3==0: a3=1 continue else: if s[i]=='l' or a4==1: a4=1 if s[i]=='o' or a5==1: a5=1 if a5==1:print("YES") else: print("NO")
1,000
PYTHON3
a,b,c = sorted(map(int, input().rstrip().split())) print(2*a+2*b if a+b<=c else a+b+c)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n; set<long long> lol; long long f(long long s, long long r, long long a, long long b, long long l) { if (s == r) { if (l <= n && lol.count(l) == 0) { lol.insert(l); return 1; } return 0; } long long p = 0; if (l <= n && lol.count(l) == 0) { lol.insert(l); ++p; } p += f(s + 1, r, a, b, l * 10 + a) + f(s + 1, r, a, b, l * 10 + b); return p; } int main() { cin >> n; long long m = 0; for (long long i = 0; i < 10; ++i) { for (long long j = i + 1; j < 10; ++j) { m += f(0, 10, i, j, 0); } } cout << m - 1 << '\n'; }
1,600
CPP
#include <bits/stdc++.h> int main() { int a, b = 0, c, i, j, k, ar[1000], br[1000]; scanf("%d", &a); for (i = 1; i <= a; i++) { scanf("%d", &ar[i]); } for (i = 1; i <= a; i++) { br[i] = ar[i]; } for (i = 1; i <= a; i++) { for (j = i; j < a; j++) { if (ar[i] < ar[j + 1]) { c = ar[j + 1]; ar[j + 1] = ar[i]; ar[i] = c; } } } for (i = 1; i <= (a / 2); i++) { for (j = 1; j <= a; j++) { if (ar[a - i + 1] == br[j]) { printf("%d ", j); br[j] = -9; break; } } for (j = 1; j <= a; j++) { if (ar[i] == br[j]) { printf("%d\n", j); br[j] = -9; break; } } } return 0; }
800
CPP
import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') import math,datetime,functools,itertools,operator,bisect,fractions from collections import deque,defaultdict,OrderedDict from fractions import Fraction from decimal import Decimal def main(): starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") for _ in range(1): n=ri() s=ria() q=ri() prefsum=[0] c=0 for i in range(n): c+=s[i] prefsum.append(c) for i in range(q): l,r=ria() print((prefsum[r]-prefsum[l-1])//10) #<--Solving Area Ends endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") class FastReader(io.IOBase): newlines = 0 def __init__(self, fd, chunk_size=1024 * 8): self._fd = fd self._chunk_size = chunk_size self.buffer = io.BytesIO() def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size)) 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, size=-1): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size)) 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() class FastWriter(io.IOBase): def __init__(self, fd): self._fd = fd self.buffer = io.BytesIO() self.write = self.buffer.write def flush(self): os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class FastStdin(io.IOBase): def __init__(self, fd=0): self.buffer = FastReader(fd) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") class FastStdout(io.IOBase): def __init__(self, fd=1): self.buffer = FastWriter(fd) self.write = lambda s: self.buffer.write(s.encode("ascii")) self.flush = self.buffer.flush if __name__ == '__main__': sys.stdin = FastStdin() sys.stdout = FastStdout() main()
1,400
PYTHON3
input() data = list(map(int, input().split())) d1 = [v2 - v1 for v1, v2 in zip(data, data[1:])] d2 = [v2 - v1 for v1, v2 in zip(data, data[2:])] print(max(d1 + [min(d2)]))
900
PYTHON3
def anti(s): return "".join(['+' if s[i] == '*' else '*' for i in range(len(s))]) k = int(input()) result = [[] for i in range(k + 2)] result[0] = [ '+' ] for i in range(1, k + 1): for e in result[i - 1]: result[i] += [e + e] result[i] += [e + anti(e)] print("\n".join(result[k]))
1,800
PYTHON3
import collections def path_duration(path): """ x right > y up ^ """ duration = 0 position = (0, 0) position_paths = collections.defaultdict(set) for direction in path: if direction == 'S': new_position = position[0], position[1] - 1 elif direction == 'N': new_position = position[0], position[1] + 1 elif direction == 'W': new_position = position[0] - 1, position[1] elif direction == 'E': new_position = position[0] + 1, position[1] if new_position in position_paths[position] or position in position_paths[new_position]: speed = 1 else: speed = 5 duration += speed position_paths[position].add(new_position) position = new_position return duration def main(): t = int(input()) for _ in range(t): path = input() print(path_duration(path)) assert path_duration('NNN') == 15 assert path_duration('NS') == 6 assert path_duration('WWEN') == 16 assert path_duration('WWEE') == 12 assert path_duration('NWNWS') == 25 assert path_duration('ESWN') == 20 assert path_duration('WSESNN') == 26 main()
1,400
PYTHON3