solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
return {a.first + b.first, a.second + b.second};
}
pair<int, int> operator-(pair<int, int> a, pair<int, int> b) {
return {a.first - b.first, a.second - b.second};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<int, int> > a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
}
vector<pair<int, int> > b(n);
for (int i = 0; i < n; ++i) {
cin >> b[i].first >> b[i].second;
}
for (int i = 0; i < n; ++i) {
pair<int, int> res = a[0] + b[i];
map<pair<int, int>, int> mapa;
for (int j = 0; j < n; ++j) {
if (i == j) {
continue;
}
++mapa[b[j]];
}
bool ok = true;
for (int j = 1; j < n; ++j) {
pair<int, int> x = res - a[j];
if (!mapa.count(x)) {
ok = false;
break;
}
--mapa[x];
if (mapa[x] == 0) {
mapa.erase(x);
}
}
if (ok) {
cout << res.first << " " << res.second << "\n";
}
}
}
| 1,200 | CPP |
n , t = map(int,input().split())
mini = 10**(n-1)
maxi = (10**n)-1
counter = mini // t
while 1:
if (t*counter <=maxi) and (t*counter>=mini):
print(t*counter)
break
elif t*counter > maxi:
print(-1)
break
counter += 1
| 1,000 | PYTHON3 |
LETTERS='abcdefghijklmnopqrstuvwxyz'
i=int(input())
A=input().lower()
i=0
while i < 26:
if LETTERS[i] in A:
i+=1
else:
break
if i==26:
print('YES')
else:
print('NO')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t, i;
cin >> t;
for (i = 0; i < t; ++i) {
long long int n;
cin >> n;
long long int cnt2 = 0, cnt3 = 0, cnt5 = 0;
while (n % 2 == 0) {
n /= 2;
cnt2++;
}
while (n % 3 == 0) {
n /= 3;
cnt3++;
}
while (n % 5 == 0) {
n /= 5;
cnt5++;
}
if (n != 1) {
cout << -1 << endl;
} else {
cout << cnt2 + cnt3 * 2 + cnt5 * 3 << endl;
}
}
return 0;
}
| 800 | CPP |
for _ in range(int(input())):
n, m = map(int, input().split())
l = []
for i in range(n):
l.append(list(input()))
c = -1
for i in range(n-1):
if l[i][-1] != "D":
c += 1
for i in range(m):
if l[-1][i] != "R":
c += 1
print(c) | 800 | PYTHON3 |
from sys import stdin
n,q=map(int,stdin.readline().split())
a=[[0]*n for _ in range(2)]
res = True
block=0
def f(x,y):
global block
delta = 1 if a[x][y]==0 else -1
a[x][y] = 1 if a[x][y]==0 else 0
for dy in range(-1,2):
if y+dy>=0 and y + dy <n and a[1-x][y+dy]==1:
block+=delta
return True if block==0 else False
for _ in range(q):
x,y=map(lambda x: int(x)-1,stdin.readline().split())
print("YES" if f(x,y) else "NO")
| 1,400 | PYTHON3 |
import sys
input = sys.stdin.readline
I = lambda : list(map(int,input().split()))
t,=I()
for _ in range(t):
a,b,c,d=I()
an=abs(a-c)+abs(d-b)
if a!=c and b!=d:
an+=2
print(an) | 800 | PYTHON3 |
a,b, c, d = map(int, input().split())
for i in range(a):
for j in range(b):
x = (c+i)%a if (c+i)%a else a
y = (d+j)%b if (d+j)%b else b
print(x,y)
d -=1 | 1,100 | PYTHON3 |
row = 0
col = 0
for i in range(5):
a = input().split()
try:
a.index("1")
row = i
col = a.index("1")
except:
continue
print(abs(2-row) + abs(2-col))
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int func1(string input) {
char c = 'r';
int rr = 0, rb = 0, br = 0, bb = 0;
for (int i = 0; i < input.length(); i++) {
if (input[i] == 'r' && c == 'r')
rr++;
else if (input[i] == 'r' && c == 'b')
rb++;
else if (input[i] == 'b' && c == 'r')
br++;
else if (input[i] == 'b' && c == 'b')
bb++;
if (c == 'r')
c = 'b';
else
c = 'r';
}
return max(rb, br);
}
int func2(string input) {
char c = 'b';
int rr = 0, rb = 0, br = 0, bb = 0;
for (int i = 0; i < input.length(); i++) {
if (input[i] == 'r' && c == 'r')
rr++;
else if (input[i] == 'r' && c == 'b')
rb++;
else if (input[i] == 'b' && c == 'r')
br++;
else if (input[i] == 'b' && c == 'b')
bb++;
if (c == 'r')
c = 'b';
else
c = 'r';
}
return max(rb, br);
}
int main() {
int len;
string input;
cin >> len >> input;
cout << min(func1(input), func2(input)) << endl;
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
using namespace rel_ops;
using ll = int64_t;
using Vi = vector<int>;
using Pii = pair<int, int>;
void debx(...) {}
template <class T, class U = int, class... V>
void debx(T s, U a, V... b) {
while (*s && *s != ',') cerr << *s++;
cerr << ": " << a << *s++;
debx(s, b...);
}
template <class T, class U = int, class... V>
auto operator<<(ostream& o, T x) -> decltype(&T::print, cout) {
x.print();
return o;
}
template <class T, class U = int, class... V>
auto operator<<(ostream& o, pair<T, U> x) -> decltype(0, cout) {
return o << "(" << x.first << ", " << x.second << ")";
}
template <class T, class U = int, class... V>
auto operator<<(ostream& o, T x) -> decltype(T().begin(), cout) {
o << "[";
for (auto& e : (x)) o << e << ", ";
return o << "]";
}
struct Vert {
Vi edges, invEdges;
int coproc, out{0};
};
int V, E;
vector<Vert> verts;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
cin >> V >> E;
verts.resize(V);
for (auto& v : (verts)) cin >> v.coproc;
for (int i = (0); i < (E); i++) {
int a, b;
cin >> a >> b;
verts[a].edges.push_back(b);
verts[b].invEdges.push_back(a);
verts[a].out++;
}
queue<int> avail, coproc;
for (int i = (0); i < (V); i++)
if (verts[i].out == 0) {
if (verts[i].coproc)
coproc.push(i);
else
avail.push(i);
}
int ret = 0;
while (true) {
while (!avail.empty()) {
int v = avail.front();
avail.pop();
for (auto& e : (verts[v].invEdges))
if (--verts[e].out == 0) {
if (verts[e].coproc)
coproc.push(e);
else
avail.push(e);
}
}
if (coproc.empty()) break;
ret++;
while (!coproc.empty()) {
int v = coproc.front();
coproc.pop();
for (auto& e : (verts[v].invEdges))
if (--verts[e].out == 0) {
if (verts[e].coproc)
coproc.push(e);
else
avail.push(e);
}
}
}
cout << ret << endl;
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int _sg[30] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6, 2, 1, 8, 7, 5,
9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12};
bool vis[40000];
int main() {
int n, sg = 0, tot;
scanf("%d", &n);
tot = n;
for (int base = 2; base * base <= n; ++base) {
if (vis[base]) continue;
int v = base, cnt = 0;
while (true) {
if (v < 40000) vis[v] = true;
++cnt, --tot;
if (v > n / base) break;
v *= base;
}
sg ^= _sg[cnt];
}
sg ^= tot & 1;
puts(sg ? "Vasya" : "Petya");
return 0;
}
| 2,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf32 = 0x3f3f3f3f;
const long long inf64 = 0x3f3f3f3f3f3f3f3f;
const int xn = 1e5 + 1;
int n, k, a[xn], c[xn], ml = 0, mr = -1;
long long f[xn], g[xn], sum;
void add(int x) { sum += c[a[x]]++; }
void del(int x) { sum -= --c[a[x]]; }
long long w(int l, int r) {
--r;
while (mr < r) add(++mr);
while (ml > l) add(--ml);
while (mr > r) del(mr--);
while (ml < l) del(ml++);
return sum;
}
void divide(int l, int r, int x, int y) {
if (r - l == 0) return;
int m = (l + r) / 2, fm = -1;
for (int i = (x); i < (min(y, m)); ++i) {
long long v = f[i] + w(i, m);
if (v < g[m]) g[m] = v, fm = i;
}
divide(l, m, x, fm + 1);
divide(m + 1, r, fm, y);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = (0); i < (n); ++i) cin >> a[i], --a[i];
for (int i = (0); i < (n + 1); ++i) f[i] = inf64;
f[0] = 0;
while (k--) {
for (int i = (0); i < (n + 1); ++i) g[i] = inf64;
divide(1, n + 1, 0, n), swap(f, g);
}
cout << f[n] << '\n';
return 0;
}
| 2,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> g[N];
int uni[N], n, a, b, k;
long long ans;
int dfs(int s = 1, int p = 0) {
int sub = uni[s];
for (int &i : g[s]) {
if (i == p) continue;
sub += dfs(i, s);
}
if (p) ans += min(sub, 2 * k - sub);
return sub;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= 2 * k; ++i) {
scanf("%d", &a);
uni[a] = 1;
}
for (int i = 1; i < n; ++i) {
scanf("%d%d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
dfs();
return printf("%lld\n", ans), 0;
}
| 1,800 | CPP |
t=int(input())
for i in range(t):
n=int(input())
s=""
b=0
for i in range(n):
a=input()
s=s+a
for i in range(n*n-n):
if((i+1) % n !=0):
if (s[i]=="1"):
if(s[i+1]=="0" and s[i+n]=="0"):
b=1
print("NO")
break
if (b==0):
print("YES")
| 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[2010];
int b[2010];
int main() {
long long a, b;
cin >> a >> b;
if (b == a) {
cout << 1 << endl;
} else {
if (b - a >= 10) {
cout << 0 << endl;
} else {
long long x = b - a;
long long ans = 1;
while (x--) {
long long y = b % 10;
ans *= y;
ans %= 10;
b--;
}
cout << ans << endl;
}
}
return 0;
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int INFi = 2e9;
const int maxN = 1e5 + 5;
const int md = 998244353;
const int md2 = 1e9 + 7;
double getTime() { return clock() / (double)CLOCKS_PER_SEC; };
void solve() {
int n;
int h, m, k;
cin >> n >> h >> m >> k;
vector<pair<int, int>> vo(n);
for (int i = 0; i < n; ++i) cin >> vo[i].first >> vo[i].second;
vector<int> times;
for (int i = 0; i < n; ++i) {
times.push_back(vo[i].second);
if (vo[i].second < m / 2) {
times.push_back(vo[i].second + m / 2);
} else {
times.push_back(vo[i].second - m / 2);
}
}
sort((times).begin(), (times).end());
int len = m / 2 - k;
int l = 0;
pair<int, pair<int, int>> ans = {0, {0, 0}};
for (int r = 0; r < times.size(); ++r) {
while (times[r] - times[l] > len) {
l++;
}
ans = max(ans, {r - l + 1, {l, r}});
}
int start = times[ans.second.first] % (m / 2);
cout << n - ans.first << " " << start << "\n";
vector<int> answers;
for (int i = 0; i < n; ++i) {
if (m - vo[i].second + start < k) {
answers.push_back(i);
continue;
}
if (start > vo[i].second && start - k < vo[i].second) {
answers.push_back(i);
continue;
}
if (start + m / 2 > vo[i].second && start + m / 2 - k < vo[i].second) {
answers.push_back(i);
continue;
}
}
for (auto &x : answers) cout << x + 1 << " ";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tests = 1;
for (int _ = 0; _ < tests; ++_) {
solve();
}
return 0;
}
| 2,300 | CPP |
n=int(input())
X=0
for i in range(n):
a=input("")
if(a=="++X"):
X=X+1
elif(a=="X++"):
X=X+1
elif(a=="--X"):
X=X-1
elif(a=="X--"):
X=X-1
print(X)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 100;
long long n, m, fa[N];
long long read() {
int f = 0, t = 1;
char s = getchar();
while (s < '0' || s > '9') s = getchar();
while (s >= '0' && s <= '9') {
f = f * 10 + s - '0';
s = getchar();
}
return f;
}
long long dep[N], pos[N][22], _l, _r, len;
long long LCA(long long u, long long v) {
if (u == v) return u;
if (dep[u] < dep[v]) swap(u, v);
for (long long i = 20; i >= 0; i--)
if (dep[pos[u][i]] >= dep[v]) u = pos[u][i];
if (u == v) return u;
for (long long i = 20; i >= 0; i--)
if (pos[u][i] != pos[v][i]) u = pos[u][i], v = pos[v][i];
return pos[u][0];
}
int main() {
pos[2][0] = pos[3][0] = pos[4][0] = 1;
dep[2] = dep[3] = dep[4] = 1;
_l = 2;
_r = 3;
len = 2;
cin >> m;
int T = 4;
while (m--) {
int u = read();
pos[T + 1][0] = pos[T + 2][0] = u;
for (long long i = 1; i <= 20; i++) {
pos[T + 1][i] = pos[pos[T + 1][i - 1]][i - 1];
pos[T + 2][i] = pos[pos[T + 2][i - 1]][i - 1];
}
dep[T + 1] = dep[u] + 1;
dep[T + 2] = dep[u] + 1;
int lca1 = LCA(_l, T + 1);
int lca2 = LCA(_r, T + 1);
int dis1 = dep[_l] + dep[T + 1] - 2 * dep[lca1];
int dis2 = dep[_r] + dep[T + 1] - 2 * dep[lca2];
if (dis1 > len) {
len = dis1;
_r = T + 1;
}
if (dis2 > len) {
len = dis2;
_l = T + 1;
}
T += 2;
printf("%d\n", len);
}
}
| 2,400 | CPP |
n = int(input())
T = list(map(int, input().split()))
maximum = result = 1
for i in range(len(T)-1):
if T[i] < T[i+1]:
result += 1
else:
result = 1
maximum = max(result, maximum)
print(maximum) | 800 | PYTHON3 |
cases=int(input())
for _ in range(cases):
a,b,c,d=map(int,input().split())
x,y,x1,y1,x2,y2=map(int,input().split())
r1=x-a+b
r2=y-c+d
if r1>=x1 and r1<=x2 and r2>=y1 and r2<=y2 and (x2>x1 or a+b==0) and (y2>y1 or c+d==0):
print("Yes")
else:
print("No")
| 1,100 | PYTHON3 |
n, b = map(int, input().split())
a = list(map(int, input().split()))
r = []
eo = [0, 0]
for i in range(len(a)):
if eo[0] == eo[1] and eo[0] != 0:
r.append(abs(a[i - 1] - a[i]))
eo = [0, 0]
eo[a[i] % 2] += 1
r.sort()
r.append(b + 1)
i = 0
while b >= 0:
b -= r[i]
i += 1
print(i - 1) | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
if (!b) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
long long int temp = gcd(a, b);
return temp ? (a / temp * b) : 0;
}
vector<int> adj[105];
int vis[105];
int cnt, tmp;
bool flag;
void dfs(int node) {
vis[node] = 1;
if (node == tmp && cnt > 0) {
flag = true;
return;
}
if (cnt > 105) return;
for (int i = 0; i < adj[node].size(); i++) {
int v = adj[node][i];
cnt++;
dfs(v);
}
return;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
adj[i].push_back(a);
}
vector<int> v;
for (int i = 1; i <= n; i++) {
cnt = 0;
flag = false;
tmp = i;
if (vis[i]) continue;
dfs(i);
if (flag) {
if (cnt % 2 == 0) cnt /= 2;
v.push_back(cnt);
} else {
cout << -1;
return 0;
}
}
long long int ans = std::accumulate(v.begin(), v.end(), 1, lcm);
cout << ans << '\n';
return 0;
}
| 1,600 | CPP |
n, m = list(map(int, input().split()))
med = int(n/2) + n % 2
if m - 1 < med:
print((m-1)*2 + 1)
else:
print((m-med)*2)
print() | 900 | PYTHON3 |
n = int(input())
a = list(map(int, input().strip().split()))
max_elem = max(a)
max_count = 1
curr_count = 0
for idx, elem in enumerate(a):
if elem == max_elem:
curr_count += 1
if curr_count > max_count:
max_count = curr_count
else:
curr_count = 0
# print(curr_count)
print(max_count) | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
for (size_t i = 0; i < a.length(); ++i) {
if (a[i] == b[i])
a[i] = '0';
else
a[i] = '1';
}
cout << a << endl;
return 0;
}
| 800 | CPP |
x = 0
n = int(input())
for i in range(n):
z = input()
if '+' in z:
x = x + 1
else:
x = x - 1
print(x)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e4 + 7;
const int inf = 0x3f3f3f3f;
struct Edge {
int from, to, cap, flow;
};
struct Dinic {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n) {
this->n = n;
for (int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
void addedge(int from, int to, int cap, int c = 0) {
edges.push_back(Edge{from, to, cap, 0});
edges.push_back(Edge{to, from, c, 0});
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[u] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int u, int dist) {
if (u == t || dist == 0) return dist;
int flow = 0, f;
for (int& i = cur[u]; i < G[u].size(); i++) {
Edge& e = edges[G[u][i]];
if (d[u] + 1 == d[e.to] &&
(f = DFS(e.to, min(dist, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[u][i] ^ 1].flow -= f;
flow += f;
dist -= f;
if (!dist) break;
}
}
return flow;
}
int Maxflow(int s, int t) {
this->s = s;
this->t = t;
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, inf);
}
return flow;
}
} net;
int id[220][220];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> mp(n);
for (int i = 0; i < n; ++i) cin >> mp[i];
int s = 1, tot = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (mp[i][j] == '#') id[i][j] = s++, tot++;
net.init(s + 1);
int ans = 0;
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < m; ++j) {
if (id[i][j] && id[i + 1][j]) {
ans += 2;
net.addedge(0, id[i][j], 1);
net.addedge(0, id[i + 1][j], 1);
net.addedge(id[i + 1][j], id[i][j], 1);
net.addedge(id[i][j], id[i + 1][j], 1);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m - 1; ++j) {
if (id[i][j] && id[i][j + 1]) {
ans += 2;
net.addedge(id[i][j], s, 1);
net.addedge(id[i][j + 1], s, 1);
net.addedge(id[i][j + 1], id[i][j], 1);
net.addedge(id[i][j], id[i][j + 1], 1);
}
}
}
ans -= net.Maxflow(0, s);
cout << tot - ans / 2 << '\n';
return 0;
}
| 2,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool Isvalidrow(int arr[], int s) {
for (int i = 0; i < s; i++) {
if (arr[i] == -1) {
return false;
}
}
return true;
}
bool IsvalidCol(int **arr, int r, int c, int j) {
for (int i = 0; i < r; i++) {
if (arr[i][j] == -1) return false;
}
return true;
}
int main() {
int r, c;
cin >> r >> c;
char arr[r][c];
cin.ignore();
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> arr[i][j];
}
}
int **sol = new int *[r];
for (int i = 0; i < r; i++) sol[i] = new int[c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (arr[i][j] == 'S') {
sol[i][j] = -1;
} else
sol[i][j] = 1;
}
}
int count = 0;
for (int i = 0; i < r; i++) {
if (Isvalidrow(sol[i], c)) {
for (int j = 0; j < c; j++) {
sol[i][j] = 0;
count++;
}
}
}
for (int j = 0; j < c; j++) {
for (int i = 0; i < r; i++) {
if (IsvalidCol(sol, r, c, j)) {
if (sol[i][j] == 1) {
count++;
}
}
}
}
cout << count;
return 0;
}
| 800 | CPP |
for _ in range(int(input())):
n = int(input())
arr = list(map(int,input().split()))
DK_1 = -1
for i in range(len(arr)):
if arr[i] < i:
break
DK_1 = i
DK_2 = n
i = n-1
while i >= 0:
if arr[i] < n -i -1:
break
DK_2 = i
i-=1
if DK_2 <= DK_1:
print("YES")
else:
print("NO") | 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
bool s[1000 * 1000];
int n, r;
int main() {
cin >> n >> r;
long long res = INF, x;
for (int i = 0; i <= r; i++) {
int moveNum = 0, wMove = 0, a = i, b = r;
while (a != 0) {
moveNum += b / a;
wMove += b / a - 1;
if (a == 1 && b != 1) wMove--;
b %= a;
swap(a, b);
}
if (b == 1 && moveNum == n && wMove < res) x = i, res = wMove;
}
if (res == INF)
cout << "IMPOSSIBLE\n";
else {
for (int i = n - 1; i >= 0; i--)
if (x < r) {
s[i] = 1;
r -= x;
} else
x -= r;
cout << res << '\n';
ios::sync_with_stdio(false);
if (s[1]) {
for (int i = 0; i < n; i++) cout << (s[i] ? 'B' : 'T');
} else {
cout << 'T';
for (int i = 1; i < n; i++) cout << (s[i] ? 'T' : 'B');
}
cout << '\n';
}
return 0;
}
| 2,100 | CPP |
s = input()
a = list(map(int, s.split()))
if a[1] + a[2] > a[0]:
if a[1] - a[2] <= 1:
for i in range(1, a[0] + 1):
if i == a[1] and i != a[1] + a[2] + 1:
print('(' + str(i) + ')', end=' ')
elif i == a[1] + a[2] + 1:
continue
else:
print(i, end=' ')
else:
print("<<", end=' ')
for i in range(a[1] - a[2], a[0] + 1):
if i == a[1] and i != a[1] + a[2] + 1:
print('(' + str(i) + ')', end=' ')
else:
print(i, end=' ')
elif a[1] + a[2] == a[0]:
if a[1] - a[2] <= 1:
for i in range(1, a[1] + a[2] + 1):
if i == a[1] and i != a[1] + a[2] + 1:
print('(' + str(i) + ')', end=' ')
elif i == a[1] + a[2] + 1:
continue
else:
print(i, end=' ')
else:
print("<<", end=' ')
for i in range(a[1] - a[2], a[1] + a[2] + 1):
if i == a[1] and i != a[1] + a[2] + 1:
print('(' + str(i) + ')', end=' ')
else:
print(i, end=' ')
else:
if a[1] - a[2] <= 1:
for i in range(1 , a[1] + a[2] + 1):
if i == a[1] and i != a[1] + a[2] + 1:
print('(' + str(i) + ')', end=' ')
elif i == a[1] + a[2] + 1:
continue
else:
print(i, end=' ')
print(">>", end=' ')
else:
print("<<", end=' ')
for i in range(a[1] - a[2], a[1] + a[2] + 1):
if i == a[1] and i != a[1] + a[2] + 1:
print('(' + str(i) + ')', end=' ')
else:
print(i, end=' ')
print(">>", end=' ') | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, d;
int inc[6], on[6], has[6];
int edges[6][6];
bool check() {
for (int i = 1; i < (1 << d) - 1; i++) {
int sizes = 1;
for (int j = 0; j < d; j++)
if (i & (1 << j)) sizes += has[j];
for (int j = 0; j < d; j++)
if (!(i & (1 << j)) && inc[j]) {
sizes -= 1;
break;
}
for (int j = 0; j < d; j++)
for (int k = j; k < d; k++) {
if ((i & (1 << j)) || (i & (1 << k))) sizes -= edges[j][k];
}
if (sizes > 0) return false;
}
return true;
}
int main() {
scanf("%d", &n);
d = to_string(n).size();
char one[10], two[10];
for (int i = 0; i < n - 1; i++) {
scanf(" %s %s", &one, &two);
if (strlen(one) > strlen(two)) swap(one, two);
edges[strlen(one) - 1][strlen(two) - 1] += 1;
}
on[0] = 1;
for (int i = 1; i < d; i++) on[i] = on[i - 1] * 10;
inc[0] = 1;
on[0] += 1;
for (int i = 0; i < d; i++) has[i] = on[i + 1] - on[i];
has[d - 1] = n - on[d - 1] + 1;
if (!check()) return printf("-1\n"), 0;
for (int r = 0; r < n; r++) {
for (int i = 0; i < d; i++)
for (int j = 0; j < d; j++) {
int& edge = edges[min(i, j)][max(i, j)];
if (inc[i] && has[j] && edge) {
has[j] -= 1;
edge -= 1;
bool add = !inc[j];
if (add) inc[j] = on[j];
if (check())
printf("%d %d\n", inc[i], on[j]++);
else {
if (add) inc[j] = 0;
has[j] += 1;
edge += 1;
}
}
}
}
return 0;
}
| 3,200 | CPP |
from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def lsi():
x=list(stdin.readline())
x.pop()
return x
def si(): return stdin.readline()
res=['YES', 'NO']
############# CODE STARTS HERE #############
a=lsi()
l=len(a)
kmp=[-1]*l
j=-1
#print(a)
for i in range(1, l):
while j>=0 and a[i]!=a[j+1]:
j=kmp[j]
if a[i]==a[j+1]:
j+=1
kmp[i]=j
m=kmp[-1]
if kmp.count(m)==1:
m=kmp[m]
#print(kmp, m)
if m<0:
print('Just a legend')
else:
print(*a[:m+1], sep='')
| 1,700 | PYTHON3 |
mas = input(). split()
n = int (mas [0])
m = int (mas [1])
a = int (mas [2])
if n % a > 0:
c = n//a+1
else:
c = n//a
if m % a > 0:
d = m//a+1
else:
d = m//a
print(c*d) | 1,000 | PYTHON3 |
i = int(input())
l =sorted(input())
t = l[::i]*i
if sorted(t) == l:
print(''.join(t))
else:
print(-1) | 1,000 | PYTHON3 |
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
for _ in range(int(input())):
h, n = map(int, input().split())
p = list(map(int, input().split()))
ans = 0
i = 1
while i < n:
if i < n - 1:
if p[i] - p[i+1] == 1:
i += 1
else:
ans += 1
else:
if p[i] != 1:
ans += 1
i += 1
print(ans)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
| 1,600 | PYTHON3 |
n=input()
l=len(n)
flag=0
for i in n:
if int(i)%8==0:
print('YES')
print(i)
flag=1
break
if flag==0:
for i in range(l):
for j in range(i+1,l):
if int(n[i]+n[j])%8==0:
flag=1
print('YES')
print(n[i]+n[j])
break
if flag==1:
break
if flag==0:
for i in range(l):
for j in range(i+1,l):
for k in range(j+1,l):
if int(n[i]+n[j]+n[k])%8==0:
ans=n[i]+n[j]+n[k]
print('YES')
print(ans)
flag=1
break
if flag==1:
break
if flag==1:
break
if flag==0:
print('NO')
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
void chmin(T1 &a, T2 b) {
if (b < a) a = b;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
vector<long long> divisors(long long n) {
vector<long long> ds;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ds.push_back(i);
if (n / i != i) ds.push_back(n / i);
}
}
sort(ds.begin(), ds.end());
return ds;
}
int main() {
int n;
long long K;
cin >> n >> K;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
scanf("%I64d", &a[i]);
}
if (K == 1) {
cout << 1 << endl;
cout << min_element(a.begin(), a.end()) - a.begin() + 1 << endl;
return 0;
}
vector<int> small(1e6 + 10), large(1e6 + 10);
vector<long long> ds = divisors(K);
int m = ds.size();
for (int i = 0; i < m; i++) {
if (ds[i] <= K / ds[i]) {
small[ds[i]] = i;
} else {
large[K / ds[i]] = i;
}
}
static pair<pair<int, long long>, pair<int, int> > dp[1010][7000];
fill_n(*dp, 1010 * 7000,
make_pair(pair<int, long long>(1e8, 1e17), pair<int, int>(-1, -1)));
dp[0][0] = make_pair(pair<int, long long>(0, 0), pair<int, int>(-1, -1));
for (int i = 0; i < n; i++) {
long long A = gcd(a[i], K);
for (int j = 0; j < m; j++)
if (dp[i][j].first.first < n) {
long long g = A * gcd(ds[j], K / A);
int nj = g <= K / g ? small[g] : large[K / g];
chmin(dp[i + 1][j], dp[i][j]);
chmin(dp[i + 1][nj],
make_pair(pair<int, long long>(dp[i][j].first.first + 1,
dp[i][j].first.second + a[i]),
pair<int, int>(i, j)));
}
}
if (dp[n][m - 1].second.first == -1) {
cout << -1 << endl;
return 0;
}
vector<int> ans;
pair<int, int> p = dp[n][m - 1].second;
while (p.first != -1) {
ans.push_back(p.first);
p = dp[p.first][p.second].second;
}
cout << ans.size() << endl;
for (int x : ans) cout << x + 1 << " ";
cout << endl;
}
| 2,600 | CPP |
n = int(input())
a = sorted(list(map(int, input().split())))
chet = list(i for i in a if i % 2 == 0)
nechet = list(i for i in a if i % 2 != 0)
if len(chet) == len(nechet) or abs(len(chet) - len(nechet)) == 1:
print(0)
elif len(chet) > len(nechet):
x = len(chet) - len(nechet) - 1
print(sum(chet[:x]))
else:
x = len(nechet) - len(chet) - 1
print(sum(nechet[:x]))
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, x, a[100001];
bool dx[100001];
bool dx1[100001];
int main() {
cin >> n >> x;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (dx[a[i]]) {
cout << 0;
return 0;
}
dx[a[i]] = true;
}
for (int i = 1; i <= n; i++) {
int p = a[i] & x;
if (dx[p] && p != a[i]) {
cout << 1;
return 0;
}
}
for (int i = 1; i <= n; i++) {
int p = a[i] & x;
if (dx1[p]) {
cout << 2;
return 0;
}
dx1[p] = true;
}
cout << -1;
}
| 1,200 | CPP |
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
print(sum(i >= y[x[1]-1] and i>0 for i in y)) | 800 | PYTHON3 |
t = int(input())
for i in range(t):
n, k = map(int, input().split())
step = 0
while(n > 0):
if n % k == 0:
n = n // k
step += 1
else:
step += n % k
n -= (n % k)
print(step)
| 900 | PYTHON3 |
import math
n,k=map(int,input().split(" "))
red=math.ceil((n*2)/k)
green=math.ceil((n*5)/k)
blue=math.ceil((n*8)/k)
print(red+blue+green) | 800 | PYTHON3 |
l = []
defeated = 0
s,n = map(int,input().split())
for _ in range(n):
p = list(map(int,input().split()))
l.append(p)
l = sorted(l)
for a,b in l:
if s > a:
defeated += 1
s = (s+b)
else:
break
if defeated == n:
print("YES")
else:
print("NO") | 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n;
set<int> s, t;
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
s.insert(a);
t.insert(b);
}
int ans = min((int)s.size(), (int)t.size());
cout << ans << "\n";
return 0;
}
| 0 | CPP |
a = int(input())
l = list(map(int,input().split()))
r = "EASY"
for i in range(len(l)):
if l[i] == 1:
r = "HARD"
print(r)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100, M = 1010, D = 32;
int i, j, k, n, m, q, o, x, y, ch, Pn, En;
int a[N], h[N], l[N], r[N], p[N], Cnt[65536];
struct Bitset {
unsigned int a[D];
int count() {
int i, cnt = 0;
for (i = 0; i < D; i++) cnt += Cnt[a[i] >> 16] + Cnt[a[i] & 65535];
return cnt;
}
void clear() { memset(a, 0, sizeof a); }
void up(const Bitset &x, const Bitset &y) {
for (int i = 0; i < D; i++) a[i] = x.a[i] | y.a[i];
}
void operator|=(const int &n) { a[n >> 5] |= 1u << (n & 31); }
void operator|=(const Bitset &n) {
for (int i = 0; i < D; i++) a[i] |= n.a[i];
}
void operator&=(const Bitset &n) {
for (int i = 0; i < D; i++) a[i] &= n.a[i];
}
void operator+=(int n) {
Bitset t;
t = *this;
clear();
int x = n >> 5, y = n & 31, z = 32 - y, i;
if (!y) {
for (i = 0; x + i < D; i++) a[x + i] |= t.a[i];
} else {
a[x] |= t.a[0] << y;
for (i = 1; x + i < D; i++) a[x + i] |= t.a[i] << y | t.a[i - 1] >> z;
}
n = m - n;
x = n >> 5, y = n & 31, z = 32 - y;
if (!y) {
for (i = 0; x + i < D; i++) a[i] |= t.a[x + i];
} else {
for (i = 0; x + i + 1 < D; i++)
a[i] |= t.a[x + i] >> y | t.a[x + i + 1] << z;
a[i] |= t.a[x + i] >> y;
}
}
} pr, Ans;
struct tree {
int ad;
Bitset Bit;
} T[N << 2];
struct edge {
int s, n;
} E[N << 1];
void R(int &x) {
x = 0;
ch = getchar();
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
}
void W(int x) {
if (x >= 10) W(x / 10);
putchar(x % 10 + '0');
}
void pre(int m) {
for (int i = 2, j; i < m; i++) {
for (j = 2; j * j <= i; j++)
if (i % j == 0) break;
if (j * j > i) pr |= i;
}
for (int i = 0; i < 65536; i++) {
int j = i;
while (j) Cnt[i] += j & 1, j >>= 1;
}
}
void E_add(int x, int y) {
E[++En].s = y;
E[En].n = h[x];
h[x] = En;
E[++En].s = x;
E[En].n = h[y];
h[y] = En;
}
void dfs(int x, int F) {
l[x] = ++Pn;
p[Pn] = x;
for (int k = h[x]; k; k = E[k].n)
if (E[k].s != F) dfs(E[k].s, x);
r[x] = Pn;
}
void down(int k) {
if (T[k].ad) {
T[k << 1].ad = (T[k << 1].ad + T[k].ad) % m;
T[k << 1 | 1].ad = (T[k << 1 | 1].ad + T[k].ad) % m;
T[k << 1].Bit += T[k].ad;
T[k << 1 | 1].Bit += T[k].ad;
T[k].ad = 0;
}
}
void T_build(int L, int R, int k) {
if (L == R) {
T[k].ad = 0;
T[k].Bit |= a[p[L]];
return;
}
int mid = (L + R) >> 1;
T_build(L, mid, k << 1);
T_build(mid + 1, R, k << 1 | 1);
T[k].Bit.up(T[k << 1].Bit, T[k << 1 | 1].Bit);
}
void T_add(int L, int R, int l, int r, int ad, int k) {
if (L == l && R == r) {
T[k].ad = (T[k].ad + ad) % m;
T[k].Bit += ad;
return;
}
down(k);
int mid = (L + R) >> 1;
if (r <= mid)
T_add(L, mid, l, r, ad, k << 1);
else {
if (l > mid)
T_add(mid + 1, R, l, r, ad, k << 1 | 1);
else
T_add(L, mid, l, mid, ad, k << 1),
T_add(mid + 1, R, mid + 1, r, ad, k << 1 | 1);
}
T[k].Bit.up(T[k << 1].Bit, T[k << 1 | 1].Bit);
}
void T_query(int L, int R, int l, int r, int k) {
if (L == l && R == r) {
Ans |= T[k].Bit;
return;
}
down(k);
int mid = (L + R) >> 1;
if (r <= mid)
T_query(L, mid, l, r, k << 1);
else {
if (l > mid)
T_query(mid + 1, R, l, r, k << 1 | 1);
else
T_query(L, mid, l, mid, k << 1),
T_query(mid + 1, R, mid + 1, r, k << 1 | 1);
}
}
int main() {
R(n);
R(m);
pre(m);
for (i = 1; i <= n; i++) R(a[i]), a[i] %= m;
for (i = 1; i < n; i++) R(x), R(y), E_add(x, y);
dfs(1, 0);
T_build(1, n, 1);
R(q);
for (i = 1; i <= q; i++) {
R(o);
if (o == 1)
R(x), R(y), T_add(1, n, l[x], r[x], y % m, 1);
else {
Ans.clear();
R(x);
T_query(1, n, l[x], r[x], 1);
Ans &= pr;
W(Ans.count()), puts("");
}
}
}
| 2,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[3], sum[3];
int main() {
int n, t, x, y;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &t, &x, &y);
a[t] += x;
sum[t] += 10;
}
for (int i = 1; i < 3; i++) {
bool live = false;
if (sum[i] % 2 == 0) {
if (a[i] >= sum[i] / 2) live = true;
} else {
if (a[i] > sum[i] / 2) live = true;
}
if (live)
printf("LIVE\n");
else
printf("DEAD\n");
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long answer(long long val) {
if (!val) return 0;
long long i, j, sol = 0, val2 = 1;
for (i = 1; (val2 << i) <= val; i++) sol += i - 1;
for (j = i - 2; j >= 0 && ((val2 << j) & val); j--) sol++;
if (j > -1 && (((val2 << j) - 1) & val) == ((val2 << j) - 1)) sol++;
return sol;
}
long long a, b;
int main() {
scanf("%I64d%I64d", &a, &b);
printf("%I64d\n", answer(b) - answer(a - 1));
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[12][12];
int isunique(int n, int m) {
int first = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i][j] > 0) {
if (first == 0) {
first = a[i][j];
} else {
if (a[i][j] != first) {
return 0;
}
}
}
}
}
return first;
};
int single_intersection(pair<int, int>& p1, pair<int, int>& p2) {
if (p1.first == p2.first and p1.second == p2.second) {
return 0;
}
if (p1.first == p2.second and p1.second == p2.first) {
return 0;
}
if (p1.first == p2.first or p1.first == p2.second) {
return p1.first;
}
if (p1.second == p2.first or p1.second == p2.second) {
return p1.second;
}
return 0;
}
int main() {
int n, m;
cin >> n >> m;
pair<int, int> set1[n];
pair<int, int> set2[m];
for (int i = 0; i < n; ++i) {
cin >> set1[i].first >> set1[i].second;
}
for (int i = 0; i < m; ++i) {
cin >> set2[i].first >> set2[i].second;
}
int inter;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
a[i][j] = single_intersection(set1[i], set2[j]);
}
}
int val = isunique(n, m);
if (val) {
cout << val << "\n";
return 0;
} else {
int currcount = 0;
int first = 0;
for (int i = 0; i < n; ++i) {
currcount = 0;
first = 0;
for (int j = 0; j < m; ++j) {
if (a[i][j] > 0) {
if (first == 0) {
first = a[i][j];
currcount += 1;
} else {
if (first != a[i][j]) {
currcount += 1;
}
}
}
}
if (currcount >= 2) {
cout << -1 << "\n";
return 0;
}
}
for (int j = 0; j < m; ++j) {
currcount = 0;
first = 0;
for (int i = 0; i < n; ++i) {
if (a[i][j] > 0) {
if (first == 0) {
first = a[i][j];
currcount += 1;
} else {
if (first != a[i][j]) {
currcount += 1;
}
}
}
}
if (currcount >= 2) {
cout << -1 << "\n";
return 0;
}
}
cout << 0 << "\n";
return 0;
}
}
| 1,900 | CPP |
#!/usr/bin/env python
#from __future__ import division, print_function
import math
import os
import sys
from io import BytesIO, IOBase
M = 10 ** 9 + 7
import heapq
import bisect
from functools import lru_cache
from collections import *
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from future_builtins import ascii, filter, hex, map, oct, zip
# sys.setrecursionlimit(10**6)
# 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")
def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input
def out(var): sys.stdout.write(str(var)) # for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
def fsep(): return map(float, inp().split())
def inpu(): return int(inp())
def valid(arr,n,c,mid):
s=0
p=arr[0]
for i in range(1,n):
temp=arr[i]-p
if temp>=mid:
s+=1
p=arr[i]
#print(s)
if s>=c-1:
return True
return False
def fun(n):
s=str(n)
ans=0
for i in range(len(s)):
p=int(s[i])
ans+=math.factorial(p)
ans=str(ans)
res=0
for i in range(len(ans)):
res+=int(ans[i])
return res
@lru_cache(None)
def main():
t=1
t=inpu()
for _ in range(t):
a=inp()
b=inp()
i=len(a)-1
j=len(b)-1
while(i>=0 and j>=0):
if a[i]==b[j]:
i-=1
j-=1
else:
i-=2
#print(j)
if j==-1:
print("YES")
else:
print("NO")
if __name__ == '__main__':
main()
| 1,500 | PYTHON3 |
import math
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
l,r = [int(x) for x in input("").split()]
exists = False
for i in range(l, r+1):
for j in range(i, r+1):
for k in range(j, r+1):
if i == j or i == k or j == k:
continue
if gcd(i, j) == 1 and gcd(j, k) == 1 and gcd(i, k) != 1:
print(i, j, k)
exit()
print(-1)
| 1,100 | PYTHON3 |
n,k=input().strip().split(' ')
n,k=int(n),int(k)
if(2*n-1<k):
print(0)
elif(k>n):
x=k-n
cnt=1
cnt+=n-x
print(cnt//2)
else:
cnt=(k-1)//2
print(cnt)
| 1,000 | PYTHON3 |
import sys
def str_in_sequence(sequence, str1, str2):
start1 = sequence.find(str1)
if start1 == -1:
return False
start2 = sequence.find(str2, start1 + len(str1))
if start2 == -1:
return False
return True
def main(sinp=sys.stdin, sout=sys.stdout):
flags, vision1, vision2 = sinp.read().split()
forward = str_in_sequence(flags, vision1, vision2)
backward = str_in_sequence(flags[::-1], vision1, vision2)
if forward:
if backward:
result = 'both'
else:
result = 'forward'
else:
if backward:
result = 'backward'
else:
result = 'fantasy'
sout.write(result)
if __name__ == '__main__':
main()
| 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
int pre[3000001], last[14005], other[3000001], cap[3000001];
int d[14005], que[14005], a[14005], val[14005];
int n, m, l, g, S, T, ans;
inline int read(void) {
int x = 0;
char ch = getchar();
while (ch > '9' || ch < '0') ch = getchar();
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
void connect(int x, int y, int z) {
l++;
pre[l] = last[x];
last[x] = l;
other[l] = y;
cap[l] = z;
swap(x, y);
l++;
pre[l] = last[x];
last[x] = l;
other[l] = y;
cap[l] = 0;
}
bool bfs(void) {
memset(d, 0, sizeof d);
int h = 1, t = 1;
que[1] = S;
d[S] = 1;
while (h <= t) {
int u = que[h];
h++;
for (int p = last[u]; p; p = pre[p]) {
int v = other[p];
if (d[v] || cap[p] == 0) continue;
d[v] = d[u] + 1;
que[++t] = v;
if (v == T) return 1;
}
}
return 0;
}
int dinic(int u, int flow) {
if (u == T) return flow;
int res = flow;
for (int p = last[u]; p; p = pre[p]) {
int v = other[p];
if (res > 0 && cap[p] > 0 && d[v] == d[u] + 1) {
int tmp = dinic(v, min(res, cap[p]));
res -= tmp;
cap[p] -= tmp;
cap[p ^ 1] += tmp;
if (tmp == 0) d[v] = 0;
}
}
return flow - res;
}
int main() {
n = read();
m = read();
g = read();
l = 1;
S = n + m + 1;
T = n + m + 2;
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n; i++) val[i] = read();
for (int i = 1; i <= n; i++)
a[i] ? connect(i, T, val[i]) : connect(S, i, val[i]);
for (int i = 1; i <= m; i++) {
int opt = read(), w = read();
ans += w;
int k = read();
for (int j = 1; j <= k; j++) {
int x = read();
opt ? connect(x, n + i, INF) : connect(n + i, x, INF);
}
int pd = read();
if (pd == 1) w += g;
opt ? connect(n + i, T, w) : connect(S, n + i, w);
}
while (bfs()) ans -= dinic(S, INF);
printf("%d\n", ans);
return 0;
}
| 2,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int gcd(int n, int m) {
if (m == 0) return n;
return gcd(m, n % m);
}
int fact(int n) {
if (n == 1) return 1;
return n * fact(n - 1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, m;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
cin >> m;
int b[m];
for (int i = 0; i < m; i++) cin >> b[i];
sort(a, a + n);
sort(b, b + m);
int i = 0, j = 0;
int count = 0;
while (i < n and j < m) {
if (abs(a[i] - b[j]) <= 1) {
count++;
i++, j++;
} else if (a[i] > b[j])
j++;
else
i++;
}
cout << count;
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void _R(T &x) {
cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%" PRId64, &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 <typename T, typename... U>
void R(T &head, U &...tail) {
_R(head);
R(tail...);
}
template <typename T>
void _W(const T &x) {
cout << x;
}
void _W(const int &x) { printf("%d", x); }
template <typename T>
void _W(const vector<T> &x) {
for (auto i = x.cbegin(); i != x.cend(); i++) {
if (i != x.cbegin()) putchar(' ');
_W(*i);
}
}
void W() {}
template <typename T, typename... U>
void W(const T &head, const U &...tail) {
_W(head);
putchar(sizeof...(tail) ? ' ' : '\n');
W(tail...);
}
const int N = 2e5 + 10;
const int M = 2e5 + 10;
int n, m, a[N], c[M], g[M];
bool greedy(int x) {
int r = m, y = g[m];
while (y >= x) {
if (r >= y * c[y])
r -= y * c[y];
else
r %= y;
y = g[min(r, y - 1)];
}
if (r >= x) {
r -= x;
y = min(y, g[r]);
}
while (y != 0) {
if (r >= y * c[y])
r -= y * c[y];
else
r %= y;
y = g[min(r, y - 1)];
}
return r == 0;
}
int main() {
R(m, n);
for (int i = 0; i < int(n); i++) R(a[i]);
for (int i = 0; i < int(n); i++) c[a[i]]++;
for (int i = (1); i <= int(m); i++) c[i] = min(c[i], m / i);
for (int i = (1); i <= int(m); i++) {
if (c[i])
g[i] = i;
else
g[i] = g[i - 1];
}
for (int i = (1); i <= int(m - 1); i++)
if (!greedy(i)) {
W(i);
return 0;
}
W("Greed is good");
return 0;
}
| 2,600 | CPP |
s = input()
for i in range(len(s)-1):
if s[i] not in "aouien":
if s[i+1] not in "aouie": print("NO") ; break
else: print("NO" if s[-1] not in "aouien" else "YES") | 900 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int d[] = {2, 7, 2, 3, 3, 4, 2, 5, 1, 2}, x;
std::cin >> x;
std::cout << d[x / 10] * d[x % 10];
return 0;
}
| 1,100 | CPP |
x,y=input().split(' ')
z=int(x)
for i in range(int(y)):
if z%10!=0:
z-=1
else:
z/=10
print(int(z)) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void cnt(int n);
int a[1000006], b[1000006], bit[30];
int main() {
ios::sync_with_stdio(false);
int n, m, c = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
cnt(a[i]);
}
for (int i = 0; i < m; i++) cin >> b[i];
sort(b, b + m);
for (int i = 0; i < m; i++) {
if (bit[b[i]]) {
bit[b[i]]--;
c++;
} else {
bool q = false;
for (int j = b[i] + 1; j <= 30; j++)
if (bit[j]) {
bit[j]--;
for (int k = b[i]; k < j; k++) bit[k]++;
bit[b[i]]++;
q = true;
break;
}
if (q) {
bit[b[i]]--;
c++;
} else
break;
}
}
cout << c << endl;
}
void cnt(int n) {
int i = 0;
while (n) {
if (n & 1) bit[i]++;
i++;
n /= 2;
}
}
| 1,900 | CPP |
I = input
A, B = I(), I()
d = dict(zip(A + A.upper(), B + B.upper()))
for c in I():
print(d[c] if c in d else c, end='') | 800 | PYTHON3 |
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <chrono>
#include <random>
#define all(v) (v).begin(), (v).end()
#define unq(v) (v).erase(unique(all(v)), (v).end())
#define vec vector
#define pii pair <int, int>
#define pli pair <ll, int>
#define pil pair <int, ll>
#define pll2 pair <ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define MULTI int T; cin >> T; while(T--)
#define sqr(x) ((x) * (x))
#define yesno(x) cout << ((x) ? "Yes" : "No") << endl
#define test cerr << '!' << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void main0 ();
int main () {
clock_t start, end;
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
start = clock();
#endif
ios::sync_with_stdio(false); cin.tie(0);
main0();
#ifndef ONLINE_JUDGE
end = clock();
cout << "( " << (end - start) << " ms )" << endl;
fclose(stdin);
#endif
return 0;
}
const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 2e5 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
const double Pi = acos(-1.0);
std::mt19937 rnd((unsigned int)chrono::steady_clock::now().time_since_epoch().count());
int n, m, p;
string s[N];
ll a[N], b[N];
ll f[N];
void main0 () {
cin >> n >> m >> p;
for (int i = 1;i <= n;++i) {
cin >> s[i];
for (int j = 0;j < m;++j) {
if (s[i][j] == '1') a[i] += (1ll << j);
}
}
ll maxs = 0;
for (int _ = 1;_ <= 20;++_) {
int pos = rnd() % n + 1;
vector <int> d;
for (int i = 0;i < m;++i) {
if (a[pos] & (1ll << i)) d.pb(i);
}
int S = d.size();
for (int s = 0;s <= (1 << S) - 1;++s) {
f[s] = 0;
}
for (int i = 1;i <= n;++i) {
b[i] = 0;
for (int j = 0;j < S;++j) {
if (a[i] & (1ll << d[j])) b[i] += (1 << j);
}
f[b[i]]++;
}
for (int i = 0;i < S;++i) {
for (int s = 0;s <= (1 << S) - 1;++s) {
if (s & (1 << i)) f[s ^ (1 << i)] += f[s];
//if (s & (1 << i)) cout << s << "<-" << (s ^ (1 << i)) << endl;
}
}
//cout << (bitset<15>) a[pos] << endl;
for (int s = 0;s <= (1 << S) - 1;++s) {
//cout << s << ':' << f[s] << endl;
if (f[s] < (n + 1) / 2) continue;
if (__builtin_popcountll(s) >= __builtin_popcountll(maxs)) {
maxs = 0;
for (int i = 0;i < S;++i) {
if (s & (1 << i)) maxs += (1ll << d[i]);
}
}
}
}
for (int i = 0;i < m;++i) {
cout << !!(maxs & (1ll << i));
}
cout << endl;
} | 2,400 | CPP |
a=int(input())
a1=str(a)
s=list(a1)
c=s.count('4')
d=s.count('7')
if c+d==7 or c+d ==4:
print("YES")
else:
print('NO')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
x = 0;
char c = getchar();
bool flag = false;
while (!isdigit(c)) {
if (c == '-') flag = true;
c = getchar();
}
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
if (flag) x = -x;
}
using namespace std;
const int P = 1e9 + 7;
inline void MIN(int &a, int b) {
if (b < a) a = b;
}
inline void MAX(int &a, int b) {
if (a < b) a = b;
}
inline void ADD(int &a, int b) {
a += b;
if (a >= P) a -= P;
}
inline int Mod(int x) { return x >= P ? x - P : x; }
int n, K;
int X[67], Y[67], W[67];
int h[67], htot, mx;
namespace jzpac {
long long jie[1001000];
int h[67][67];
int hangcnt[67], liecnt[67];
int hmn[67], hmx[67];
unordered_map<int, int> mp[2][67];
int id[67], itot, mask[67];
inline void sol() {
jie[0] = 1;
for (int i = 1; i <= n; ++i) jie[i] = jie[i - 1] * i % P;
for (int i = 1; i <= K; ++i) ++hangcnt[X[i]], ++liecnt[Y[i]];
int hct = 0, lct = 0;
for (int i = 1; i <= mx; ++i) hct += (hangcnt[i] > 1), lct += (liecnt[i] > 1);
if (hct > lct)
for (int i = 1; i <= K; ++i) swap(X[i], Y[i]);
memset(hmn, 0x3f, sizeof(hmn));
for (int i = 1; i <= K; ++i)
MIN(hmn[X[i] - 1], Y[i] - 1), MAX(hmx[X[i] - 1], Y[i] - 1);
for (int i = 0; i < mx; ++i) id[i] = 29;
for (int i = 0; i < mx; ++i)
if (hmn[i] < hmx[i]) mask[hmx[i]] ^= (1 << itot), id[i] = itot++;
for (int i = 0; i < mx; ++i) mask[i] ^= (1 << itot) - 1;
for (int i = 1; i <= K; ++i) h[X[i] - 1][Y[i] - 1] = Mod(W[i] - 1 + P);
int nwt = 0;
mp[nwt][0][0] = 1;
for (int i = 0; i < mx; ++i) {
nwt ^= 1;
for (int j = 0; j <= i; ++j) mp[nwt][j].clear();
for (int j = 0; j <= i; ++j) {
for (unordered_map<int, int>::iterator it = mp[nwt ^ 1][j].begin();
it != mp[nwt ^ 1][j].end(); ++it) {
int s = it->first, v = it->second;
ADD(mp[nwt][j][s & mask[i]], v);
for (int k = 0; k < mx; ++k)
if (h[k][i]) {
if ((s >> id[k]) & 1) continue;
ADD(mp[nwt][j + 1][(s ^ (1 << id[k])) & mask[i]],
1ll * v * h[k][i] % P);
}
}
}
}
long long res = 0;
for (int j = 0; j <= mx; ++j) {
res += mp[nwt][j][0] * jie[n - j] % P;
}
printf("%lld\n", (res % P + P) % P);
}
} // namespace jzpac
int main() {
read(n), read(K);
for (int i = 1; i <= K; ++i) {
read(X[i]), read(Y[i]), read(W[i]);
h[++htot] = X[i];
}
sort(h + 1, h + 1 + htot);
htot = unique(h + 1, h + 1 + htot) - h - 1;
for (int i = 1; i <= K; ++i)
X[i] = lower_bound(h + 1, h + 1 + htot, X[i]) - h;
mx = htot;
htot = 0;
for (int i = 1; i <= K; ++i) h[++htot] = Y[i];
sort(h + 1, h + 1 + htot);
htot = unique(h + 1, h + 1 + htot) - h - 1;
for (int i = 1; i <= K; ++i)
Y[i] = lower_bound(h + 1, h + 1 + htot, Y[i]) - h;
if (htot > mx) mx = htot;
jzpac::sol();
return 0;
}
| 3,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
cout << (N / 2) + 1 << endl;
return 0;
}
| 800 | CPP |
w= input()
w =int(w)
if w%2 == 1 or w==2:
print("no")
else:print('yes')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
#define make_unique(x) sort(x.begin(),x.end()); (x).resize(unique(x.begin(),x.end()) - (x).begin())
#define forn(i, l, r) for(int i = int(l); i < int(r); i++)
typedef long long ll;
using namespace std;
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--) {
ll n,l,r,s;
cin >> n >> l >> r >> s;
ll kol = r-l+1;
if (s < kol*(kol+1)/2 || s > n*(n+1)/2 - (n-kol)*(n-kol+1)/2) {
cout << -1 << endl;
continue;
}
vector<int> otr;
vector<int> nototr;
for (int i = n; i >= 1;--i) {
ll min_s = (kol-1)*kol/2 + i;
ll max_s = i*(i+1)/2 - (i-kol)*(i-kol+1)/2;
if (min_s <= s) {
otr.push_back(i);
s-=i;
kol--;
} else
nototr.push_back(i);
}
int num = 0;
forn(i,1,n+1) {
if (i < l || i > r) {
cout << nototr[num] << " ";
num++;
continue;
}
if (i == l) {
forn(j,0,otr.size())
cout << otr[j] << " ";
continue;
}
}
cout << endl;
}
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, s;
string a;
s = 0;
unordered_map<string, int> mp;
cin >> n;
mp["Tetrahedron"] = 4;
mp["Cube"] = 6;
mp["Octahedron"] = 8;
mp["Dodecahedron"] = 12;
mp["Icosahedron"] = 20;
for (i = 0; i < n; i++) {
cin >> a;
s = s + mp[a];
}
cout << s;
}
| 800 | CPP |
n=int(input())
s=input()
cnt=0
for i in range(1,n):
if s[i]==s[i-1]:
cnt+=1
print(cnt)
| 800 | PYTHON3 |
n = int(input(""))
words = []
finish = []
for i in range(n):
words.append(input(""))
for i in words:
if len(i) > 10:
finish.append(str(i[0])+str(len(i)-2)+str(i[len(i)-1]))
else:
finish.append(i)
for i in finish:
print(i) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, f[maxn << 4], cnt;
int grid[10][maxn];
struct Node {
int Lh[10], Rh[10];
int sum;
};
Node mem[maxn << 2];
int Find_root(int u) {
if (f[u] == u)
return u;
else
return f[u] = Find_root(f[u]);
}
Node update(Node L, Node R, int md) {
Node res;
res.sum = L.sum + R.sum;
for (int i = 0; i < n; i++) {
f[L.Lh[i]] = L.Lh[i];
f[L.Rh[i]] = L.Rh[i];
f[R.Lh[i]] = R.Lh[i];
f[R.Rh[i]] = R.Rh[i];
}
for (int i = 0; i < n; i++)
if (grid[i][md] == grid[i][md + 1]) {
int dx = Find_root(L.Rh[i]), dy = Find_root(R.Lh[i]);
if (dx != dy) {
f[dx] = dy;
res.sum--;
}
}
for (int i = 0; i < n; i++) {
res.Lh[i] = Find_root(L.Lh[i]);
res.Rh[i] = Find_root(R.Rh[i]);
}
return res;
}
void build(int u, int L, int R) {
if (L == R) {
mem[u].sum = 1;
mem[u].Lh[0] = mem[u].Rh[0] = cnt++;
for (int i = 1; i < n; i++) {
if (grid[i][L] != grid[i - 1][L]) {
mem[u].Lh[i] = mem[u].Rh[i] = cnt++;
mem[u].sum++;
} else
mem[u].Lh[i] = mem[u].Rh[i] = mem[u].Lh[i - 1];
}
return;
}
int md = L + R >> 1, ls = u << 1, rs = ls | 1;
build(ls, L, md);
build(rs, md + 1, R);
mem[u] = update(mem[ls], mem[rs], md);
}
Node calc(int u, int L, int R, int l, int r) {
if (l <= L and R <= r) return mem[u];
int md = L + R >> 1, ls = u << 1, rs = ls | 1;
if (r <= md)
return calc(ls, L, md, l, r);
else if (md < l)
return calc(rs, md + 1, R, l, r);
else
return update(calc(ls, L, md, l, r), calc(rs, md + 1, R, l, r), md);
}
int m, q, l, r;
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 0; i < n; i++) {
for (int j = 1; j <= m; j++) scanf("%d", &grid[i][j]);
}
build(1, 1, m);
while (q--) {
scanf("%d%d", &l, &r);
printf("%d\n", calc(1, 1, m, l, r).sum);
}
return 0;
}
| 2,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
int t, n, m, k;
int main() {
cin >> t;
while (t--) {
cin >> n >> m >> k;
int kiri = 0;
int kanan = k;
int ans = 1000000000;
while (kiri <= kanan) {
int mid = (kiri + kanan) / 2;
if (n + mid > m + k - mid) {
ans = mid;
kanan = mid - 1;
} else {
kiri = mid + 1;
}
}
if (ans == 1e9) {
printf("0\n");
continue;
}
printf("%d\n", k - ans + 1);
}
}
| 1,300 | CPP |
#include <bits/stdc++.h>
void exgcd(int a, int b, int &x, int &y) {
if (b == 0)
x = 1, y = 0;
else
exgcd(b, a % b, y, x), y -= a / b * x;
}
int main() {
int a, b, p;
long long x;
scanf("%d%d%d%I64d", &a, &b, &p, &x);
int ak = 1, x0, y;
long long pp = (long long)p * (p - 1), ans = 0;
for (int k = 1; k < p; k++) {
ak = (long long)ak * a % p;
exgcd(ak, p, x0, y);
x0 = ((long long)x0 * b % p + p) % p;
long long t1 = (long long)x0 * (p - 1) % pp * (p - 1) % pp;
long long t2 = (long long)k * p % pp;
long long t = (t1 + t2) % pp;
ans += (x - t + pp) / pp;
}
printf("%I64d\n", ans);
return 0;
}
| 2,100 | CPP |
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
c = [i for i in range(n)]
c.sort(key=lambda x:arr[x])
res = c[n - m * k:]
res.sort()
total = 0
for i in res:
total += arr[i]
print(total)
for i in range(m - 1, len(res)-1, m):
print(res[i] + 1, end=' ') | 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long int l1, l2, r1, r2, k;
unsigned long long int mx, mn;
cin >> l1 >> r1 >> l2 >> r2 >> k;
if (l1 > r2 || l2 > r1) {
cout << "0";
} else {
mx = max(l1, l2);
mn = min(r1, r2);
unsigned long long int c;
if (mn > mx)
c = mn - mx + 1;
else
c = mx - mn + 1;
if (k >= mx && k <= mn) c = c - 1;
cout << c;
}
}
| 1,100 | CPP |
n=int(input())
s=input()
i=0
iteration=0
for ss in s:
if iteration==n-1:
print(i)
break
if ss==s[iteration+1]:
i+=1
if ss!=s[iteration+1]:
i+=0
iteration=iteration+1 | 800 | PYTHON3 |
def ok(a,b,n):
for i in range(n):
if(a[i]>b[i]):
return False
return True
MAX = 100001
parent = [0] * MAX
Rank = [0] * MAX
def find(x):
if parent[x] == x:
return x
else:
return find(parent[x])
def merge(r1, r2):
if (r1 != r2):
if (Rank[r1] > Rank[r2]):
parent[r2] = r1
Rank[r1] += Rank[r2]
else:
parent[r1] = r2
Rank[r2] += Rank[r1]
def minimumOperations(s1, s2):
for i in range(1, 26 + 1):
parent[i] = i
Rank[i] = 1
ans = []
for i in range(len(s1)):
if (s1[i] != s2[i]):
if (find(ord(s1[i]) - 96) !=
find(ord(s2[i]) - 96)):
x = find(ord(s1[i]) - 96)
y = find(ord(s2[i]) - 96)
merge(x, y)
ans.append([s1[i], s2[i]])
print(len(ans))
for _ in range(int(input())):
n = int(input())
s = [i for i in input()]
t = [i for i in input()]
if(ok(s,t,n)):
minimumOperations("".join(s),"".join(t))
else:
print(-1) | 1,700 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t, k, d;
cin >> n >> t >> k >> d;
int m = d % t == 0 ? d / t : d / t + 1;
if (n <= m * k)
cout << "NO\n";
else if (n <= (m + 1) * k)
if (d % t == 0)
cout << "NO\n";
else
cout << "YES\n";
else
cout << "YES\n";
return 0;
}
| 1,100 | CPP |
n, x = map(int, input().split())
a = set(map(int, input().split()))
res = 0
for i in range(0, x):
if not (i in a):
res += 1
if x in a:
res += 1
print(res) | 1,000 | PYTHON3 |
def lim21(k,b):
if(k<10):
return b+k
res=b
res+=k//10*10
res+=lim21(k//10+k%10,0)
return res
t=int(input())
for i in range(t):
n=int(input())
print(lim21(n,0))
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int primes[3005];
void sieve() {
memset(primes, 0, sizeof(primes));
for (int i = 2; i <= 3000; i++) {
if (primes[i] == 0) {
for (int j = 2 * i; j <= 3000; j += i) primes[j]++;
}
}
for (int i = 1; i <= 3000; i++)
if (primes[i] == 0) primes[i]++;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
vector<long long> factors;
;
for (long long i = 1; i < sqrt(n); i++) {
if (n % i == 0) {
factors.push_back(i);
}
}
sort(factors.begin(), factors.end());
long long tt = factors.size() * 2;
if ((long long)sqrt(n) * (long long)sqrt(n) == n) tt++;
if (k > tt) {
cout << "-1\n";
return 0;
}
if (factors.size() >= k) {
cout << factors[k - 1];
} else {
if ((long long)sqrt(n) * (long long)sqrt(n) == n && k == factors.size() + 1)
cout << (long long)sqrt(n);
else {
k -= factors.size();
if ((long long)sqrt(n) * (long long)sqrt(n) == n) k--;
cout << n / factors[factors.size() - k];
}
}
return 0;
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
string c[6];
void solve(string &x) {
int i = 0;
while (i < x.size()) {
if ((x[i] == '-') || (x[i] == ';') || (x[i] == '_')) {
x.erase(i, 1);
--i;
} else if ((x[i] >= 'A') && (x[i] <= 'Z'))
x[i] += 32;
++i;
}
}
string res(string &x) {
if ((x == c[0]) || (x == c[1]) || (x == c[2]) || (x == c[3]) || (x == c[4]) ||
(x == c[5]))
return "ACC";
return "WA";
}
int main() {
string s[3];
for (int i = 0; i < 3; ++i) {
cin >> s[i];
solve(s[i]);
}
c[0] = s[0] + s[1] + s[2];
c[1] = s[0] + s[2] + s[1];
c[2] = s[1] + s[0] + s[2];
c[3] = s[1] + s[2] + s[0];
c[4] = s[2] + s[0] + s[1];
c[5] = s[2] + s[1] + s[0];
int n;
cin >> n;
string tmp;
cin.ignore();
for (int i = 0; i < n; ++i) {
getline(cin, tmp);
solve(tmp);
cout << res(tmp) << endl;
}
return 0;
}
| 1,300 | CPP |
s,k=map(int,input().split())
arr=[0,1];k1=1;visited=[];mini=0
while(arr[-1]<s):
arr.append(sum(arr[-k::]))
while(s>0):
for i in arr:
if(i<=s):mini=i;
else:break;
s-=mini;visited.append(mini);
if(len(visited)==1):visited.append(0);
print(len(visited))
print(*visited) | 1,600 | PYTHON3 |
n,m=map(int,input().split())
a=[0]*10**6
a[1],a[2]=2,4
for i in range(3,max(n,m)+1):
a[i]=a[i-1]+a[i-2]
print((a[n]+a[m]-2)%(10**9+7))
| 1,700 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
if (n < 4) {
cout << "NO\n";
} else if (n % 4 == 0 || n % 4 == 2) {
cout << "YES\n";
for (i = n; i != 4; i -= 2) cout << i << " - " << i - 1 << " = 1\n";
for (i = 0; i < (n - 4) / 2; i++) cout << "1 * 1 = 1\n";
cout << "1 * 2 = 2\n";
cout << "2 * 3 = 6\n";
cout << "6 * 4 = 24\n";
} else if (n % 4 == 3) {
cout << "YES\n";
for (i = n; i != 7; i -= 2) cout << i << " - " << i - 1 << " = 1\n";
for (i = 0; i < (n - 7) / 2; i++) cout << "1 * 1 = 1\n";
cout << "7 + 6 = 13\n";
cout << "13 + 5 = 18\n";
cout << "18 + 4 = 22\n";
cout << "3 - 2 = 1\n";
cout << "1 + 1 = 2\n";
cout << "22 + 2 = 24\n";
} else if (n % 4 == 1) {
cout << "YES\n";
for (i = n; i != 5; i -= 2) cout << i << " - " << i - 1 << " = 1\n";
for (i = 0; i < (n - 5) / 2; i++) cout << "1 * 1 = 1\n";
cout << "5 - 3 = 2\n";
cout << "2 * 4 = 8\n";
cout << "1 + 2 = 3\n";
cout << "8 * 3 = 24\n";
}
return 0;
}
| 1,500 | CPP |
import sys
import math
#import random
sys.setrecursionlimit(100000)
input = sys.stdin.readline
############ ---- USER DEFINED INPUT FUNCTIONS ---- ############
def inp():
return(int(input()))
def inara():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
################################################################
############ ---- THE ACTUAL CODE STARTS BELOW ---- ############
n,k=invr()
ara=inara()
ara.sort()
ans=0
for i in range(n//2,n):
if ara[i]<=k:
ans+=(k-ara[i])
for i in range(n//2+1):
if ara[i]>=k:
ans+=(ara[i]-k)
print(ans)
| 1,300 | PYTHON3 |
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
# import time,random,resource
# sys.setrecursionlimit(10**6)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
def IF(c, t, f): return t if c else f
def YES(c): return IF(c, "YES", "NO")
def Yes(c): return IF(c, "Yes", "No")
def main():
t = 1
rr = []
for _ in range(t):
n = I()
a = LI()
a.sort()
t = 0
r = 0
for c in a:
for k in range(c-1,c+2):
if k > t:
r += 1
t = k
break
rr.append(r)
return JA(rr, "\n")
print(main())
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
long long arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
long long count[n];
count[0] = 1;
map<long long, long long> m;
m[arr[0]] = 0;
for (int i = 1; i < n; i++) {
if (m.count(arr[i]) > 0) {
count[i] = count[i - 1] + (i - m[arr[i]]);
m[arr[i]] = i;
continue;
}
count[i] = count[i - 1] + i + 1;
m[arr[i]] = i;
}
long double sum = 0;
for (int i = 0; i < n; i++) sum += count[i];
cout << fixed << setprecision(6) << (2 * sum - n) / (n * n) << endl;
}
| 1,800 | CPP |
n=input()
m=len(n)
list1=[]
for i in range(int(m)):
list1.append(n[i])
list2=list(list1)
for item in range(len(list1)):
if list1[item]=='a' or list1[item]=='A' or list1[item]=='o' or list1[item]=='O' or list1[item]=='y'or list1[item]=='Y' or list1[item]=='e' or list1[item]=='E' or list1[item]=='u' or list1[item]=='U' or list1[item]=='i' or list1[item]=='I':
list2.remove(list1[item])
else:
continue
o='.'.join(list2)
k=o.lower()
print('.'+k) | 1,000 | PYTHON3 |
for _ in range(int(input())):
n=int(input());a=list(map(int,input().split()));c1=0;c2=0
for j in range(n):
if j%2==0 and a[j]%2!=0:c1+=1
elif j%2!=0 and a[j]%2==0:c2+=1
print(-1) if c1!=c2 else print(c1)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int ninf = 0xc0c0c0c0;
const long long linf = 0x3f3f3f3f3f3f3f3fll;
const long long lninf = 0xc0c0c0c0c0c0c0c0ll;
int a[1005], b[1005];
int main() {
ios::sync_with_stdio(false);
int m, n;
double l = 0, r = 2e9;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
for (int i = 1; i <= 100; i++) {
bool f = true;
double mid = (l + r) / 2;
double cur = m + mid;
for (int j = 1; j <= n; j++) {
if (j > 1) cur -= cur / b[j];
if (j < n) cur -= cur / a[j];
}
cur -= cur / a[n];
cur -= cur / b[1];
if (cur >= m)
r = mid;
else
l = mid;
}
if (l == 2e9)
cout << -1 << endl;
else
cout << fixed << setprecision(9) << l << endl;
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
if (n % 2 == 0) {
long long x = n / 2;
if (x % 2 == 1)
cout << 1 << endl;
else
cout << 0 << endl;
} else {
long long x = n - 1;
x = n / 2;
if (x % 2 == 0)
cout << 1 << endl;
else
cout << 0 << endl;
}
}
| 800 | CPP |
from collections import defaultdict as dc
import sys
import math
def inp():
p=sys.stdin.readline()
return p
a=list(map(int,inp().split()))
a.sort()
x,y,z,k=a
if x+y+z==k or x+z==y+k or z+y==x+k:
print('YES')
else:
print('NO')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
char v[110][11000];
int e[110][11000], d[110][11000], r[110][11000];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf(" %c", &v[i][j]);
}
}
for (int i = 1; i <= n; i++) {
int tem = 0;
for (int j = 1; j <= m; j++) {
if (v[i][j] == '1') {
tem = 1;
break;
}
}
if (!tem) {
printf("-1\n");
return 0;
}
}
for (int i = 0; i <= n; i++) e[i][0] = -1;
for (int i = 0; i <= m; i++) e[0][i] = -1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (v[i][j] == '1')
e[i][j] = 0;
else if (e[i][j - 1] == -1)
e[i][j] = -1;
else
e[i][j] = e[i][j - 1] + 1;
}
}
for (int i = 0; i <= n; i++) d[i][m + 1] = -1;
for (int i = 0; i <= m; i++) d[n + 1][i] = -1;
for (int i = n; i >= 1; i--) {
for (int j = m; j >= 1; j--) {
if (v[i][j] == '1')
d[i][j] = 0;
else if (d[i][j + 1] == -1)
d[i][j] = -1;
else
d[i][j] = d[i][j + 1] + 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (e[i][j] == -1) e[i][j] = e[i][m] + j;
}
}
for (int i = n; i >= 1; i--) {
for (int j = m; j >= 1; j--) {
if (d[i][j] == -1) d[i][j] = d[i][1] + m - j + 1;
}
}
int res = n * m * 2;
for (int j = 1; j <= m; j++) {
int at = 0;
for (int i = 1; i <= n; i++) {
at += min(e[i][j], d[i][j]);
}
res = min(res, at);
}
if (res == n * m * 2)
printf("-1\n");
else
printf("%d\n", res);
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
void printVector(vector<string> &v) {
for (auto x : v) cout << x << "\n";
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int t;
t = 1;
while (t--) {
int n;
cin >> n;
vector<string> phnos(n);
for (int i = 0; i < n; i++) cin >> phnos[i];
vector<string> ans(n);
vector<int> visited(n, 0);
for (int i = 1; i <= 9; i++) {
map<string, int> mp;
for (int j = 0; j < n; j++) {
for (int k = 0; k < 10 - i; k++) {
string s = phnos[j].substr(k, i);
if (mp.find(s) == mp.end())
mp.insert({s, j});
else if (mp[s] != j)
mp[s] = -1;
}
}
for (auto x : mp) {
if (x.second != -1 && visited[x.second] == 0) {
visited[x.second] = 1;
ans[x.second] = x.first;
}
}
}
printVector(ans);
}
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1;
cin >> s1;
string a = s1;
reverse(s1.begin(), s1.end());
if (s1 != a)
cout << s1.size() << "\n";
else {
a.erase(unique(a.begin(), a.end()), a.end());
if (a.size() == 1) {
cout << 0 << "\n";
} else {
cout << s1.size() - 1 << "\n";
}
}
return 0;
}
| 900 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
vector<int> temp(n, 1);
vector<vector<int> > g(2, temp);
int r, c;
int blocked_pairs = 0;
for (int i = 0; i < q; i++) {
cin >> r >> c;
r--;
c--;
g[r][c] = 1 - g[r][c];
int delta = (g[r][c] == 1) ? -1 : +1;
for (int j = -1; j <= 1; j++) {
if (c + j < 0 || c + j >= n) {
continue;
}
if (g[1 - r][c + j] == 0) {
blocked_pairs += delta;
}
}
cout << ((blocked_pairs == 0) ? "Yes\n" : "No\n");
}
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
struct node {
long long S, H;
int id;
bool operator<(const node& other) const { return S * other.H > other.S * H; }
} no[MAXN];
string s[MAXN];
string t;
int main() {
int N;
cin >> N;
int cntH = 0, cntS = 0, size = 0;
for (int i = 0; i < N; ++i) {
cin >> s[i];
int len = s[i].size();
size += len;
no[i].S = no[i].H = 0;
no[i].id = i;
for (int j = 0; j < len; ++j)
if (s[i][j] == 's')
no[i].S++;
else
no[i].H++;
cntS += no[i].S;
cntH += no[i].H;
}
sort(no, no + N);
for (int i = 0; i < N; ++i) t += s[no[i].id];
long long ans = 0;
for (int i = 0; i < size; ++i) {
if (t[i] == 's')
ans += cntH;
else
--cntH;
}
printf("%lld\n", ans);
return 0;
}
| 1,800 | CPP |