solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
from collections import Counter as co
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
d ={}
for i in range(n):
d[b[i]]=i
v =[]
for i in range(len(a)):
e=d[a[i]]-i
if e<0:e+=n
v.append(e)
print(max(co(v).values())) | 9 | PYTHON3 |
n = int(input())
nums = list(map(int, input().split()))
left = [1] * n
right = [1] * n
res = 1
for i in range(1, n):
if nums[i - 1] < nums[i]:
left[i] = left[i - 1] + 1
res = max(res, left[i])
for i in range(n - 2, -1, -1):
if nums[i] < nums[i + 1]:
right[i] = right[i + 1] + 1
res = max(res, right[i])
if res != n:
res += 1
for i in range(1, n - 1):
if nums[i - 1] + 1 < nums[i + 1]:
res = max(res, left[i - 1] + right[i + 1] + 1)
print(res) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long exp(long long x, long long y, long long p) {
long long res = 1;
while (y) {
if (y % 2) res = (res * x % p) % p;
x = (x * x) % p;
y /= 2;
}
return res;
}
long long expm(long long x, long long y) {
long long res = 1;
while (y) {
if (y % 2) res = (res * x % ((long long)1e9 + 7)) % ((long long)1e9 + 7);
x = (x * x) % ((long long)1e9 + 7);
y /= 2;
}
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
if ((n & 1) == (k & 1) && n >= k * k)
cout << "YES"
<< "\n";
else {
cout << "NO"
<< "\n";
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec;
int i, j, u, v, w, l;
int n;
scanf("%d %d", &n, &l);
vec.resize(n + 1, 0);
for (i = 0; i < l; i++) {
scanf("%d %d", &v, &w);
vec[v] = 1;
vec[w] = 1;
}
printf("%d\n", n - 1);
for (i = 1; i <= n; i++) {
if (vec[i] == 0) break;
}
for (j = 1; j < i; j++) printf("%d %d\n", i, j);
for (j = i + 1; j <= n; j++) printf("%d %d\n", i, j);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
int main() {
int n, i;
scanf("%d", &n);
int sum[n], a, b, c, d;
for (i = 0; i < n; i++) {
scanf("%d %d %d %d", &a, &b, &c, &d);
sum[i] = a + b + c + d;
}
int k = 0;
for (i = 1; i < n; i++) {
if (sum[0] < sum[i]) k++;
}
printf("%d\n", k + 1);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5;
const int inf = 0x3f3f3f3f;
long long c[55][55];
int v[55];
long long solve(int now) {
long long ans = 1;
int num = 0;
for (int i = now + 1; i >= 1; --i)
if (v[i]) {
ans *= c[now - i + 1 - num][v[i]];
num += v[i];
}
return ans;
}
int main() {
for (int i = 0; i < 55; ++i) c[i][i] = c[i][0] = 1;
for (int i = 1; i < 55; ++i)
for (int j = 1; j < i; ++j) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
int t;
scanf("%d", &t);
while (t--) {
long long x;
int now = 2, num = 0;
scanf("%lld", &x);
memset(v, 0, sizeof(v));
while (x) {
++v[x % now];
x /= now;
++now;
++num;
}
long long ans = solve(num);
if (v[0]) ans -= solve(num - 1);
printf("%lld\n", ans - 1);
}
return 0;
}
| 17 | CPP |
#include <cstdio> // printf(), scanf()
#include <set>
using namespace std;
const int MAX_N = 10000;
const int MAX_R = 5000;
void
solve(int n)
{
int bins[MAX_N / 2];
set<int> remains;
int re[MAX_R];
int ix = 0;
for (int i = 1; i < n; ++i)
{
int r = (i * i) % n;
if (remains.find(r) == remains.end())
{
remains.insert(r);
re[ix++] = r;
}
}
int m = (n - 1) / 2;
for (int i = 1; i <= m; ++i)
bins[i] = 0;
for (int i = 0; i < ix; ++i)
{
for (int j = i + 1; j < ix; ++j)
{
if (i != j)
{
int d = re[i] - re[j];
if (d < 0)
d += n;
if (d > m)
d = n - d;
bins[d]++;
}
}
}
for (int i = 1; i <= m; ++i)
printf("%d\n", 2 * bins[i]);
}
int
main(int argc, char **argv)
{
while (true)
{
int n;
scanf("%d", &n);
if (n == 0)
break;
solve(n);
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> primes;
bool isPrime(long long x){
for(long long i = 2; i * i <= x; i++) if(x % i == 0) return false;
return true;
}
void init(){
for(int i = 2; i <= 3000; i++) if(isPrime(i)) primes.push_back(i);
}
int main(){
init();
int n;
cin >> n;
map<long long, long long> rev, occ;
for(int i = 0; i < n; i++){
long long x; cin >> x;
long long Norm = 1, Pair = 1;
for(int v : primes){
int cnt = 0;
while(x % v == 0) x /= v, cnt++;
cnt %= 3;
if(!cnt) continue;
if(cnt == 1){
Norm *= v;
Pair *= v;
Pair *= v;
} else {
Norm *= v;
Norm *= v;
Pair *= v;
}
}
if(x > 1){
long long sq = sqrtl(x);
while(sq * sq <= x) sq++;
while(sq * sq > x) sq--;
if(sq * sq == x){
Norm *= x;
Pair *= sq;
} else {
Norm *= x;
Pair *= x;
Pair *= x;
}
}
rev[Norm] = Pair;
rev[Pair] = Norm;
occ[Norm]++;
occ[Pair];
}
int ans = 0;
for(auto it : occ) if(it.first != 1) ans += max(it.second, occ[rev[it.first]]);
ans /= 2;
if(occ[1]) ans++;
cout << ans << endl;
return 0;
}
| 0 | CPP |
def dis(a,b,c):
return abs(a-b)+abs(b-c)+abs(a-c);
t=int(input());
while(t>0):
t-=1;
l=list(map(int,input().split()));
a=l[0];
b=l[1];
c=l[2];
ans=dis(a,b,c);
for i in range(-1,2):
for j in range(-1,2):
for k in range(-1,2):
x=a+i;
y=b+j;
z=c+k;
s=dis(x,y,z);
if(s<ans):
ans=s;
print(ans); | 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int n;
int t[3];
int u[3];
int func(int k){
if(t[k]==0)return 0;
for(int i=0;i<3;i++)u[i]=t[i];
int sum=k,cnt=t[0]+1;
t[k]--;
while(1){
if(sum==2){
if(t[2]==0){
if(t[1]>0)cnt++;
break;
}
t[2]--;
cnt++;
sum=1;
}else if(sum==1){
if(t[1]==0){
if(t[2]>0)cnt++;
break;
}
t[1]--;
cnt++;
sum=2;
}
}
for(int i=0;i<3;i++)t[i]=u[i];
return cnt;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
int a;
scanf("%d",&a);
a%=3;
t[a]++;
}
if(t[0]==n){
printf("1\n");
}else{
printf("%d\n",max(func(1),func(2)));
}
return 0;
} | 0 | CPP |
f=input();print("YNEOS"[f.count('o')+7<len(f)::2]) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct ed {
int u, v, cost, idx;
ed() {}
ed(int uu, int vv, int cc, int idx) : u(uu), v(vv), cost(cc), idx(idx) {}
bool operator<(const ed& a) const { return cost < a.cost; }
};
struct edge {
int next, cost;
edge() {}
edge(int a, int c) : next(a), cost(c) {}
};
struct node {
int height;
int par[23];
int chainNum;
int heavyNode;
int segNum;
int cost[23];
int childcnt;
};
int n, m;
node info[200001];
int chainHead[200001];
vector<edge> adj[200001];
bool visited[200001];
void dfs(int cur, int height) {
int cnt = 1;
int maxV = 0;
int maxnode = 0;
for (auto& a : adj[cur]) {
if (!visited[a.next]) {
visited[a.next] = true;
info[a.next].height = height + 1;
info[a.next].par[0] = cur;
info[a.next].cost[0] = a.cost;
dfs(a.next, height + 1);
if (info[a.next].childcnt > maxV) {
maxV = info[a.next].childcnt;
maxnode = a.next;
}
cnt += info[a.next].childcnt;
}
}
info[cur].heavyNode = maxnode;
info[cur].childcnt = cnt;
return;
}
void processLca(int n) {
for (int i = 1; i < 23; i++)
for (int j = 1; j <= n; j++) {
info[j].par[i] = info[info[j].par[i - 1]].par[i - 1];
info[j].cost[i] =
max(info[info[j].par[i - 1]].cost[i - 1], info[j].cost[i - 1]);
}
}
vector<int> seg;
vector<int> lazy;
void lazy_update(int idx, int s, int e) {
if (lazy[idx] == (1987654321)) return;
if (lazy[idx] < seg[idx]) {
seg[idx] = lazy[idx];
lazy[idx] = (1987654321);
if (s != e) {
lazy[idx * 2] = min(lazy[idx * 2], seg[idx]);
lazy[idx * 2 + 1] = min(lazy[idx * 2 + 1], seg[idx]);
}
}
return;
}
void seg_update(int idx, int s, int e, int l, int r, int val) {
lazy_update(idx, s, e);
if (e < l || s > r) return;
if (l <= s && e <= r) {
seg[idx] = min(seg[idx], val);
if (s != e) {
lazy[idx * 2] = min(lazy[idx * 2], val);
lazy[idx * 2 + 1] = min(lazy[idx * 2 + 1], val);
}
return;
}
int mid = (s + e) / 2;
seg_update(idx * 2, s, mid, l, r, val);
seg_update(idx * 2 + 1, mid + 1, e, l, r, val);
return;
}
int seg_min(int idx, int s, int e, int l, int r) {
lazy_update(idx, s, e);
if (e < l || s > r) return (1987654321);
if (l <= s && e <= r) return seg[idx];
int mid = (s + e) >> 1;
int a = seg_min(idx * 2, s, mid, l, r);
int b = seg_min(idx * 2 + 1, mid + 1, e, l, r);
return min(a, b);
}
int chainCnt = 1;
int segcnt = 1;
int NodeNum[200001];
void HLD(int cur) {
NodeNum[segcnt] = cur;
info[cur].segNum = segcnt++;
info[cur].chainNum = chainCnt;
if (info[cur].heavyNode != 0) {
visited[info[cur].heavyNode] = true;
HLD(info[cur].heavyNode);
}
for (auto& a : adj[cur]) {
if (!visited[a.next]) {
visited[a.next] = true;
chainCnt++;
chainHead[chainCnt] = a.next;
HLD(a.next);
}
}
return;
}
int getLca(int a, int b) {
int ha = info[a].height;
int hb = info[b].height;
if (ha != hb) {
int cha;
if (ha > hb) {
swap(hb, ha);
swap(a, b);
}
cha = hb - ha;
for (int i = 22; i >= 0; i--) {
if (cha & (1 << i)) {
b = info[b].par[i];
}
}
}
if (a == b)
return a;
else {
for (int i = 22; i >= 0; i--) {
if (info[a].par[i] != info[b].par[i])
a = info[a].par[i], b = info[b].par[i];
}
}
return info[a].par[0];
}
int getMax(int lca, int b) {
int hlc = info[lca].height;
int hb = info[b].height;
int cha = hb - hlc;
int maxV = 0;
for (int i = 23; i >= 0; i--) {
if (cha & (1 << i)) {
maxV = max(maxV, info[b].cost[i]);
b = info[b].par[i];
}
}
return maxV;
}
void update(int lca, int b, int v) {
while (info[lca].chainNum != info[b].chainNum) {
seg_update(1, 1, n, info[chainHead[info[b].chainNum]].segNum,
info[b].segNum, v);
b = info[chainHead[info[b].chainNum]].par[0];
}
if (b == lca) return;
seg_update(1, 1, n, info[lca].segNum + 1, info[b].segNum, v);
}
int getMin(int a) { return seg_min(1, 1, n, info[a].segNum, info[a].segNum); }
ed edL[200001];
int par[200001];
int rr[200001];
bool inMst[200001];
int find(int a) {
if (par[a] == a) return a;
return par[a] = find(par[a]);
}
void merge(int a, int b) {
int pa = find(a);
int pb = find(b);
if (rr[pa] > rr[pb]) swap(pa, pb);
par[pa] = pb;
if (rr[pa] == rr[pb]) rr[pb]++;
}
void MakeKruscal() {
for (int i = 0; i < m; i++) {
if (find(edL[i].u) == find(edL[i].v))
inMst[edL[i].idx] = false;
else {
inMst[edL[i].idx] = true;
merge(edL[i].u, edL[i].v);
adj[edL[i].u].push_back(edge(edL[i].v, edL[i].cost));
adj[edL[i].v].push_back(edge(edL[i].u, edL[i].cost));
}
}
}
int ans[200001];
int main(void) {
scanf("%d %d", &n, &m);
for (int i = 0, a, b, c; i < m; i++) {
scanf("%d %d %d", &a, &b, &c);
edL[i] = ed(a, b, c, i);
}
sort(edL, edL + m);
for (int i = 1; i <= n; i++) par[i] = i;
MakeKruscal();
visited[1] = true;
info[1].height = 1;
dfs(1, 1);
processLca(n);
memset(visited, false, sizeof(visited));
visited[1] = true;
chainHead[chainCnt] = 1;
HLD(1);
seg.resize(533333, (1987654321));
lazy.resize(533333, (1987654321));
for (int i = 0; i < m; i++) {
if (!inMst[edL[i].idx]) {
int lc = getLca(edL[i].u, edL[i].v);
ans[edL[i].idx] = max(getMax(lc, edL[i].u), getMax(lc, edL[i].v));
update(lc, edL[i].u, edL[i].cost);
update(lc, edL[i].v, edL[i].cost);
}
}
for (int i = 0; i < m; i++) {
if (inMst[edL[i].idx]) {
if (info[edL[i].u].par[0] == edL[i].v)
ans[edL[i].idx] = getMin(edL[i].u);
else
ans[edL[i].idx] = getMin(edL[i].v);
}
}
for (int i = 0; i < m; i++) {
if (ans[i] == (1987654321))
printf("-1 ");
else
printf("%d ", ans[i] - 1);
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
void openFile() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int maxN = 4e3 + 5;
const int maxM = 1e6 + 5;
const long long INF = 1e9 + 7;
int N;
int a[maxN];
long long dp[maxN][maxN];
long long fac[maxN];
void enter() {
cin >> N;
for (int i = 1, _b = N; i <= _b; ++i) {
cin >> a[i];
}
}
int getf(int n, int k) {
long long &res = dp[n][k];
if (res != -1) return res;
res = 0;
if (k == 0) return res = fac[n];
if (k == 1) return res = 1ll * n * fac[n] % INF;
res = (res + 1ll * n * getf(n, k - 1)) % INF;
res = (res + 1ll * (k - 1) * getf(n + 1, k - 2)) % INF;
return res;
}
void calcFac() {
fac[0] = 1;
for (int i = 1, _b = N * 2; i <= _b; ++i) {
fac[i] = fac[i - 1] * i % INF;
}
}
void solve() {
calcFac();
int n = 0, k = 0;
for (int i = 1, _b = N; i <= _b; ++i) {
if (a[i] == -1) {
bool f = false;
for (int j = 1, _b = N; j <= _b; ++j)
if (a[j] == i) {
f = true;
}
if (f)
++n;
else
++k;
}
}
memset(dp, -1, sizeof(dp));
cout << getf(n, k) << endl;
}
int main() {
openFile();
enter();
solve();
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, K = 60;
long long a[N], b[K][N];
int f[K][N];
int n;
inline int getsum(int k, long long x) {
long long w = 1ll << (k - 1);
if (x < w)
return upper_bound(b[k], b[k] + n, x + w) - upper_bound(b[k], b[k] + n, x);
else
return (upper_bound(b[k], b[k] + n, x - w) - b[k]) +
(b[k] + n - upper_bound(b[k], b[k] + n, x));
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
for (int k = 1; k < K; ++k) {
long long U = (1ll << k) - 1;
for (int i = 0; i < n; i++) {
b[k][i] = a[i] & U;
}
sort(b[k], b[k] + n);
}
for (int k = 0; k < K; k++)
for (int i = 0; i <= n; i++) f[k][i] = 0x3f3f3f3f;
f[0][n] = 0;
for (int k = 1; k < K; k++) {
for (int i = 0; i <= n; i++) {
if (f[k - 1][i] == 0x3f3f3f3f) continue;
if (i == 0 && b[k - 1][i] == 0) continue;
for (long long bit = 0; bit < 2; bit++) {
long long x = (i == 0 ? 0 : b[k - 1][i - 1]) + (bit << (k - 1));
int pos = upper_bound(b[k], b[k] + n, x) - b[k];
f[k][pos] = min(f[k][pos], f[k - 1][i] + getsum(k, x));
}
}
}
cout << f[K - 1][n] << endl;
}
| 10 | CPP |
n = int(input())
for i in range(n):
h, m = [int(i)for i in input().split()]
h *= 60
total = h + m
print(1440 - total) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int compare(const void* a, const void* b) {
return ((*(pair<long long, long long>*)a).first -
(*(pair<long long, long long>*)b).first);
}
string alfabet = "abcdefghijklmnopqrstuvwxyz";
int main() {
long long n, x1, x2, k, b;
cin >> n;
cin >> x1 >> x2;
vector<pair<double, double> > lines;
pair<double, double> line;
for (int i = 0; i < n; ++i) {
cin >> k >> b;
line.first = k * x1 + b;
line.second = k * x2 + b;
lines.push_back(line);
}
sort(lines.begin(), lines.end());
for (int i = 0; i < n - 1; ++i) {
if (lines[i].second > lines[i + 1].second &&
lines[i].first != lines[i + 1].first) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
const int M = 100005;
using namespace std;
bool canWin[M], canLose[M];
int T[M][26];
int n, k, m;
char s[M];
int main() {
scanf("%d %d", &n, &k);
m = 1;
for (int i = 1; i <= n; i++) {
scanf("%s\n", s);
int pos = 1;
char c;
for (int j = 0; c = s[j]; j++) {
c -= 'a';
if (T[pos][c] == 0) T[pos][c] = ++m;
pos = T[pos][c];
}
}
for (int i = m; i; i--) {
canLose[i] = count(T[i], T[i] + 26, 0) == 26;
canWin[i] = false;
for (int j = 0; j < 26; j++) {
if (T[i][j] == 0) continue;
canLose[i] |= !canLose[T[i][j]];
canWin[i] |= !canWin[T[i][j]];
}
}
if (!canWin[1]) {
printf("Second");
return 0;
}
if (canLose[1]) {
printf("First");
return 0;
}
if (k & 1)
printf("First");
else
printf("Second");
return 0;
}
| 10 | CPP |
start=input()
q=0
cards=input().split()
for i in range(5):
if cards[i][0]==start[0] or cards[i][1]==start[1]:
print("YES")
q=1
break
if q==0:
print("NO")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
vector<ll> a(N);
for (auto &x : a) cin >> x;
ll ans = LLONG_MAX;
for (int tmp = 0; tmp < (1 << N); ++tmp) {
bitset<20> BIT(tmp);
if (!BIT.test(0)) continue;
if (BIT.count() < K) continue;
ll cost = 0;
ll h = a.at(0);
for (int i = 1; i < N; ++i) {
h = max(h, a.at(i - 1));
if (!BIT.test(i)) continue;
if (a.at(i) <= h) {
cost += h + 1 - a.at(i);
h++;
}
}
ans = min(ans, cost);
}
cout << ans << endl;
return 0;
} | 0 | CPP |
for test_case in range(int(input())):
a, b, n = map(int, input().split())
operation = 0
big = max(a, b)
small = min(a, b)
while big <= n:
result = big + small
small = big
big = result
operation += 1
print(operation)
| 7 | PYTHON3 |
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
struct Node{
int key;
Node *p, *l, *r;
};
Node *root, *NIL;
void insert(int key){
Node *y = NIL;
Node *x = root;
Node *z;
z = (Node *)malloc(sizeof(Node));
z->key = key;
z->l = NIL;
z->r = NIL;
while(x != NIL){
y = x;
if(z->key < x->key) x = x->l;
else x = x->r;
}
z->p = y;
if(y == NIL)
root = z;
else{
if(z->key < y->key) y->l = z;
else y->r = z;
}
}
void preParse(Node *u){
if(u == NIL) return;
cout << " " << u->key;
preParse(u->l);
preParse(u->r);
}
void inParse(Node *u){
if(u == NIL) return;
inParse(u->l);
cout << " " << u->key;
inParse(u->r);
}
Node * find(Node *u, int key){
while(u != NIL && key != u->key){
if(key < u->key) u = u->l;
else u = u->r;
}
return u;
}
int main(){
int n, temp;
char s[10];
cin >> n;
for(int i = 0; i < n; i++){
cin >> s;
if(s[0] == 'i'){
cin >> temp;
insert(temp);
}
else if(s[0] == 'f'){
cin >> temp;
Node *flag = find(root, temp);
if(flag != NIL) cout << "yes" << endl;
else cout << "no" << endl;
}
else{
inParse(root);
cout << endl;
preParse(root);
cout << endl;
}
}
return 0;
} | 0 | CPP |
#include <stdio.h>
int main(){
int x, y;
scanf("%d%d", &x, &y);
printf("a");
if(x > y) printf(" > ");
else if(x < y) printf(" < ");
else printf(" == ");
printf("b\n");
}
| 0 | CPP |
def Rearranging(string):
if (len(set(string))) == 1:
return "-1"
elif string == string[::-1]:
for j in range(len(string)//2):
newString = string[:j] + string[j + 1] + string[j] + string[j + 2:]
if string != newString:
return newString
else: print(string)
for i in range(int(input())):
string = input()
if(Rearranging(string) != None):
print(Rearranging(string)) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 == x2) {
int k = y2 - y1;
if (k < 0) k = -k;
k++;
cout << 2 * k + 4;
} else if (y1 == y2) {
int k = x2 - x1;
if (k < 0) k = -k;
k++;
cout << 2 * k + 4;
} else {
int k = x2 - x1;
int l = y2 - y1;
if (k < 0) k = -k;
if (l < 0) l = -l;
l++;
k++;
cout << 2 * l + 2 * k;
}
return 0;
}
| 20 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, k, nn;
cin >> n >> k;
if (k == n - 1) {
cout << "2\n";
for (i = 1; i < n; i++) {
cout << i << " " << n << "\n";
}
return 0;
}
if (k == n - 2) {
cout << "3\n";
for (i = 1; i < k; i++) {
cout << i << " " << n << "\n";
}
cout << k << " " << n - 1 << "\n";
cout << n - 1 << " " << n << "\n";
return 0;
}
if (2 * k > n) {
cout << "4\n";
nn = n - k;
for (i = 2; i <= nn; i++) {
cout << 1 << " " << i << "\n";
}
for (; i < 2 * nn; i++) {
cout << i << " " << (i - nn + 1) << "\n";
}
for (; i <= n; i++) {
cout << 1 << " " << i << "\n";
}
return 0;
}
for (i = 0; i < n; i++) {
nn = n - 1;
if (nn % k == 0) {
nn = 2 * (nn / k);
} else {
if (nn % k == 1)
nn = 2 * (nn / k) + 1;
else
nn = 2 * (nn / k) + 2;
}
cout << nn << endl;
for (i = 1; i <= k; i++) {
cout << i << " " << n << "\n";
}
for (i = k + 1; i < n; i++) {
cout << i << " " << (i - k) << "\n";
}
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
long long MOD = 1e6 + 3;
struct Matrix {
long long mat[105][105];
};
Matrix matMul(Matrix a, Matrix b) {
Matrix ans;
int i, j, k;
for (i = 0; i < 105; i++)
for (j = 0; j < 105; j++)
for (ans.mat[i][j] = k = 0; k < 105; k++)
ans.mat[i][j] =
(ans.mat[i][j] + (a.mat[i][k] * b.mat[k][j]) % MOD) % MOD;
return ans;
}
Matrix matPow(Matrix base, long long p) {
Matrix ans;
int i, j;
for (i = 0; i < 105; i++)
for (j = 0; j < 105; j++) ans.mat[i][j] = (i == j);
while (p) {
if (p & 1) ans = matMul(ans, base);
base = matMul(base, base);
p >>= 1;
}
return ans;
}
Matrix a, b;
long long c, w, h;
int main() {
cin >> c >> w >> h;
for (int i = 0; i < w + 1; i++) b.mat[i][0] = 1;
for (int i = 0; i < w; i++) b.mat[i][i + 1] = h;
b = matPow(b, c);
long long res = 0;
for (int i = 0; i < w + 1; i++) res = (res + b.mat[0][i]) % MOD;
cout << res << endl;
return 0;
}
| 10 | CPP |
a= input().split(" ")
n = int(a[0])
k = int(a[1])
b = n*k
if b%2==0:
print(int((b/2)))
else:
print(int(((b-1)/2)))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
string ar[33];
int main() {
ar[0] = "111111101010101111100101001111111";
ar[1] = "100000100000000001010110001000001";
ar[2] = "101110100110110000011010001011101";
ar[3] = "101110101011001001111101001011101";
ar[4] = "101110101100011000111100101011101";
ar[5] = "100000101010101011010000101000001";
ar[6] = "111111101010101010101010101111111";
ar[7] = "000000001111101111100111100000000";
ar[8] = "100010111100100001011110111111001";
ar[9] = "110111001111111100100001000101100";
ar[10] = "011100111010000101000111010001010";
ar[11] = "011110000110001111110101100000011";
ar[12] = "111111111111111000111001001011000";
ar[13] = "111000010111010011010011010100100";
ar[14] = "101010100010110010110101010000010";
ar[15] = "101100000101010001111101000000000";
ar[16] = "000010100011001101000111101011010";
ar[17] = "101001001111101111000101010001110";
ar[18] = "101101111111000100100001110001000";
ar[19] = "000010011000100110000011010000010";
ar[20] = "001101101001101110010010011011000";
ar[21] = "011101011010001000111101010100110";
ar[22] = "111010100110011101001101000001110";
ar[23] = "110001010010101111000101111111000";
ar[24] = "001000111011100001010110111110000";
ar[25] = "000000001110010110100010100010110";
ar[26] = "111111101000101111000110101011010";
ar[27] = "100000100111010101111100100011011";
ar[28] = "101110101001010000101000111111000";
ar[29] = "101110100011010010010111111011010";
ar[30] = "101110100100011011110110101110000";
ar[31] = "100000100110011001111100111100000";
ar[32] = "111111101101000101001101110010001";
int a, b;
cin >> a >> b;
cout << ar[a][b] << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int m[1000001];
const int size = 4001;
int a[size], dp[size][size];
int main() {
int n;
cin >> n;
int k = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (m[a[i]] == 0) m[a[i]] = k++;
}
int ans = 1;
for (int i = n; i >= 1; i--) {
int c = m[a[i]];
for (int j = 0; j < k; j++) {
dp[c][j] = max(dp[c][j], dp[j][c] + 1);
ans = max(ans, dp[c][j]);
}
}
cout << ans;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
struct Point {
int x, y;
Point(int _x = 0, int _y = 0) {
x = _x;
y = _y;
}
Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }
int operator^(const Point &b) const { return x * b.y - y * b.x; }
int operator*(const Point &b) const { return x * b.x + y * b.y; }
};
Point p[100100];
int main() {
int n;
scanf("%d", &n);
n++;
int x, y;
for (int i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
p[i] = Point(x, y);
}
int last;
int ans = 0;
for (int i = 2; i < n; i++) {
y = (p[i - 2] - p[i]) ^ (p[i - 1] - p[i]);
if (y > 0) {
ans++;
}
}
printf("%d\n", ans);
return 0;
}
| 10 | CPP |
n,S = map(int,input().split())
st = input().split(' ')
cost = [int(num) for num in st]
l,r,Smin,k = 0,n+1,0,0
for _ in range(20):
k = int((l+r)/2)
a = list()
for i in range(n):
a.append(cost[i] + k*(i+1))
a.sort()
s = 0
for i in range(k):
s += a[i]
if(s <= S):
l = k
Smin = s;
else:
r = k
print(k,int(Smin))
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n, k, temp;
cin >> n >> k;
if (n % 2 == 0) {
if (k % 2 == 1) {
cout << "NO";
} else {
temp = sqrt(n);
if (temp >= k) {
cout << "YES";
} else {
cout << "NO";
}
}
} else {
if (k % 2 == 0) {
cout << "NO";
} else {
temp = sqrt(n);
if (temp >= k) {
cout << "YES";
} else {
cout << "NO";
}
}
}
cout << endl;
}
return 0;
}
| 7 | CPP |
def is_ok(x: int) -> bool:
d = []
for i in range(4):
d.append(x%10)
x //= 10
if d[0]!=d[1] and d[0]!=d[2] and d[0]!=d[3] and d[1]!=d[2] and d[1]!=d[3] and d[2]!=d[3]:
return True
return False
y = int(input())
i = y+1
while True:
if is_ok(i):
print(i)
break
i+=1
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void test(){
int n,k;
cin>>n>>k;
for(int i=1;i<=k-3;i++){
cout<<"1 ";
n--;
}
if(n%2==1){
cout<<"1 "<<n/2<<" "<<n/2<<"\n";
}
else if(n%4==2){
cout<<(n/2)-1<<" "<<(n/2)-1<<" "<<"2\n";
}
else{
cout<<n/4<<" "<<n/4<<" "<<n/2<<"\n";
}
}
int main(){
int t;
cin>>t;
while(t--){
test();
}
return 0;
} | 9 | CPP |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cin>>N;
long long ans=1;
for(int i=1; i<=N; i++){
ans=(ans*i)%(1000000000+7);}
cout<<ans<<endl;
} | 0 | CPP |
n,l=map(int,input().split())
a=0
b=float("inf")
for i in range(n):
a+=i+l
if abs(b)>abs(i+l):
b=i+l
print(a-b)
| 0 | PYTHON3 |
s = input()
tmp = s.split('.')
l = len(tmp[0])
fl = 0;
if tmp[0][l-1] == '9':
print("GOTO Vasilisa.")
fl = 1;
if int(tmp[1][0]) < 5 and fl == 0:
print(tmp[0])
fl = 1;
if int(tmp[1][0]) >= 5 and fl == 0:
print(int(tmp[0]) + 1)
fl = 1;
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long tree[400005];
int n;
long long r[100005], h[100005], v[100005];
pair<long long, int> p[100005];
int mp[100005];
void update(int nd, int bb, int ee, int ii, long long vv) {
if (bb == ee && bb == ii) {
tree[nd] = vv;
return;
}
if (bb > ii || ee < ii) return;
int ll = nd << 1, rr = ll | 1, mm = (bb + ee) >> 1;
update(ll, bb, mm, ii, vv);
update(rr, mm + 1, ee, ii, vv);
tree[nd] = max(tree[ll], tree[rr]);
return;
}
long long query(int nd, int bb, int ee, int ii, int jj) {
if (jj < ii) return 0;
if (bb >= ii && ee <= jj) return tree[nd];
if (bb > jj || ee < ii) return 0;
int ll = nd << 1, rr = ll | 1, mm = (bb + ee) >> 1;
return max(query(ll, bb, mm, ii, jj), query(rr, mm + 1, ee, ii, jj));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> r[i] >> h[i];
p[i] = {r[i] * r[i] * h[i], i};
}
sort(p, p + n);
for (int i = 0, ii = 0; i < n; i++) {
if (i > 0 && p[i].first == p[i - 1].first) ii--;
mp[p[i].second] = ii;
ii++;
}
for (int i = 0; i < n; i++) {
update(1, 0, n - 1, mp[i],
r[i] * r[i] * h[i] + query(1, 0, n - 1, 0, mp[i] - 1));
}
cout << fixed << setprecision(9) << double(tree[1]) * acos(-1.0) << "\n";
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n, a[N];
signed main() {
long long T;
scanf("%lld", &T);
while (T--) {
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
long long sum = 0, res = 0;
for (long long i = n; i > 1; i--) sum += a[i], res = max(res, sum);
printf("%lld\n", res);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b;
cin >> n >> a >> b;
{
for (long long i = 0; i <= 10000 * 1000 && a * i <= n; ++i) {
if ((n - a * i) % b == 0 && (n - a * i) / b >= 0) {
cout << "YES\n";
cout << i << " " << (n - a * i) / b << "\n";
return 0;
}
}
cout << "NO\n";
}
}
| 8 | CPP |
#include<bits/stdc++.h>
using namespace std;
const int maxn=105;
const int mod=1e9+7;
int A,B,C,a,b,c,s;
int valAB,valAC,valBC,valABC;
int f[maxn],ch[maxn][maxn];
void Add(int &a,int b){
a+=b;
if(a>=mod)a-=mod;
}
void Sub(int &a,int b){
a-=b;
if(a<0)a+=mod;
}
int power(int x,int pow){
int res=1;
for(;pow;pow>>=1){
if(pow&1)res=1LL*res*x%mod;
x=1LL*x*x%mod;
}
return res;
}
int main(){
scanf("%d%d%d%d%d%d",&a,&b,&c,&A,&B,&C);
if(A%a||B%b||C%c){
puts("0");
return 0;
}
Add(valAB,1LL*a*power(b,A/a)%mod);
Add(valAB,1LL*b*power(a,B/b)%mod);
Sub(valAB,1LL*a*b%mod);
Add(valAC,1LL*a*power(c,A/a)%mod);
Add(valAC,1LL*c*power(a,C/c)%mod);
Sub(valAC,1LL*a*c%mod);
Add(valBC,1LL*b*power(c,B/b)%mod);
Add(valBC,1LL*c*power(b,C/c)%mod);
Sub(valBC,1LL*b*c%mod);
Add(valABC,1LL*a*power(valBC,A/a)%mod);
Add(valABC,1LL*b*power(valAC,B/b)%mod);
Add(valABC,1LL*c*power(valAB,C/c)%mod);
Sub(valABC,1LL*a*b%mod*power(c,A/a*B/b)%mod);
Sub(valABC,1LL*a*c%mod*power(b,A/a*C/c)%mod);
Sub(valABC,1LL*b*c%mod*power(a,B/b*C/c)%mod);
Add(valABC,1LL*a*b*c%mod);
s=max(A/a,B/b);
for(int i=0;i<=s;i++)ch[i][0]=1;
for(int i=1;i<=s;i++)for(int j=1;j<=s;j++){
ch[i][j]=ch[i-1][j-1];
Add(ch[i][j],ch[i-1][j]);
}
for(int j=1;j<B/b;j++){
for(int i=A/a-1;i>=1;i--){
int coef=0;
int cura=power(b,i),curb=power(a,j);
Add(coef,power((cura+curb)%mod+mod-1,C/c));
Sub(coef,power(cura,C/c));
Sub(coef,power(curb,C/c));
Add(coef,1);
f[i]=1LL*power(power(c,A/a-i)+mod-1,B/b-j)*ch[A/a][i]%mod;
for(int k=i+1;k<A/a;k++)Sub(f[i],1LL*f[k]*ch[k][i]%mod);
Add(valABC,1LL*f[i]*ch[B/b][j]%mod*c%mod*coef%mod*a%mod*b%mod);
}
}
printf("%d\n",valABC);
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
#include<algorithm>
using namespace std;
int main() {
int n,x;
int a[100];
cin>>n>>x;
for(int i=0; i<n; i++){
cin>>a[i];
}
sort(a,a+n);
int j=0;
while(x>0 && j<n){
x-=a[j];
j++;
}
if(x!=0)j--;
cout<<j<<endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int m;
cin >> m;
for (int e = 0; e < m; e++) {
int k, p;
cin >> k >> p;
vector<int> b;
for (int i = 0; i < k; i++) {
b.push_back(a[i]);
}
for (int i = k; i < n; i++) {
int MIN = 1000000007, pos = 0;
for (int j = 0; j < k; j++) {
if (b[j] <= MIN) {
MIN = b[j];
pos = j;
}
}
if (a[i] > MIN) {
b.erase(b.begin() + pos);
b.push_back(a[i]);
}
}
cout << b[p - 1] << endl;
}
}
| 10 | CPP |
s=input("")
l=[]
for i in range(0,len(s)):
try:
l.append(s[i*2])
except:
pass
l.sort()
for i in range(0,len(l)):
if(i!=len(l)-1):
print(l[i] + "+",end="")
else:
print(l[i]) | 7 | PYTHON3 |
n, m = list(map(int, input().split()))
maxt = 0
for i in range(m+1):
type1 = min(n//2, i)
nn = n-type1 * 2
mm = m-type1
type2 = min(nn, mm // 2)
total = type1 + type2
maxt = max(total, maxt)
print(maxt) | 9 | PYTHON3 |
#include <bits/stdc++.h>
const double PI = acos(-1.0);
const double EPS = 1E-9;
const int INF = (int)1E9;
using namespace std;
int main() {
for (int i = (0); i <= (8); i++) printf("%d??<>%d\n", i, i + 1);
printf("9??>>??0\n");
printf("??<>1\n");
for (int i = (0); i <= (9); i++) printf("?%d>>%d?\n", i, i);
printf("9?>>??0\n");
for (int i = (0); i <= (8); i++) printf("%d?<>%d\n", i, i + 1);
printf(">>?");
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int main() {
vector<int> V1;
vector<int> V2;
int n;
scanf("%d", &n);
long long sum = 0;
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
if (a % 2)
V1.push_back(a);
else
V2.push_back(a);
sum += a;
}
if (sum % 2 == 0) {
printf("%lld\n", sum);
} else {
sort(V1.begin(), V1.end());
sum -= V1[0];
printf("%lld\n", sum);
}
return 0;
}
| 7 | CPP |
n, c = map(int, input().split())
p = sum(list(map(int, input().split())))
x = p // (n+1)
if p % (n+1) == 0:
print(x)
else:
print(x+1)
| 0 | PYTHON3 |
s=input()
lis=[]
for i in range(len(s)):
if(s[i].isdigit()==True):
lis.append(s[i])
lis.sort()
for j in range(len(lis)):
if(j!=len(lis)-1):
print(lis[j]+"+",end='')
else:
print(lis[j] , end='')
| 7 | PYTHON3 |
import sys
from collections import Counter
def fmax(n,m,a):
c = [0]*m
b = [0]*n
if n ==3*m:
for i in range(n//3):
for j in range(3):
b[a[i][j]-1]= j+1
for i in range(m):
flag =1
for j in range(3):
if b[a[i][j]-1] != 0 :
index = b[a[i][j]-1]
if j==0 and index==1:
continue
b[a[i][index-1]-1] = j+1
continue
b[a[i][j]-1] = j+1
return b
if __name__ == '__main__':
input = sys.stdin.read()
data = list(map(int, input.split()))
n = data[0]
m = data[1]
l = 0
a = []
for i in range(m):
a.append(list(map(int,(data[l+2:l+5]))))
l = l+3
print(*fmax(n,m,a)) | 8 | PYTHON3 |
'''
4
2 1 3 4
'''
n=int(input())
p=input().rstrip().split(' ')
C=list(p)
G=1;
C.sort(key=int)
for i in range(0,len(p)):
if int(p[i])==int(C[i]):
continue;
else:
G=1;
break;
if G==0:
print(1)
print(1,1)
else:
V=0;
for i in range(0,len(p)-1):
if int(p[i]) > int(p[i+1]):
continue;
else:
V=1;
break;
if V==0:
print("yes")
print(1,len(p))
else:
F=0;
for i in range(0,len(p)-1):
if G==1 and int(p[i]) > int(p[i+1]):
F=i;
break;
H=-41;
for i in range(F,len(p)-1):
if int(p[i]) < int(p[i+1]):
H=i;
break;
# print(F,H)
if H==-41:
H=len(p)-1;
if(1):
B=p[F:H+1]
B.reverse();
Q=[]
S=0;
for i in range(0,len(p)):
if i>=F and i<=H and S<len(B):
Q.append(str(B[S]))
S+=1;
else:
Q.append(str(p[i]))
if Q==C:
print("yes")
print(F+1,H+1)
else:
print("no") | 8 | PYTHON3 |
array = input()
array = array.split()
array = [int(i) for i in array]
axis1 = -(-array[0]//array[2])
axis2 = -(-array[1]//array[2])
print(axis1*axis2)
| 7 | PYTHON3 |
#include <set>
#include <cstdio>
#include <cstring>
using namespace std;
int v[9999];
int main(){
int i,n;
for(;scanf("%d",&n),n;){
set<int>s;
memset(v,0,sizeof(v));
for(i=1;i<n;i++)s.insert(i*i%n);
set<int>::iterator is,iS;
for(is=s.begin();is!=s.end();is++)
for(iS=is,iS++;iS!=s.end();iS++){
i=*iS-*is;
if(i>(n-1)/2)i=n-i;
v[i]++;
}
for(i=1;i<=(n-1)/2;i++)printf("%d\n",v[i]*2);
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int w, h, u, d, uu, dd;
cin >> w >> h >> u >> d >> uu >> dd;
for (int i = h; i >= 1; i--) {
w += i;
if (i == d) w = max(0, w - u);
if (i == dd) w = max(0, w - uu);
}
cout << w << endl;
}
| 7 | CPP |
#include <bits/stdc++.h>
const int oo = 2139063143;
const int N = 110;
const int P = 1000000007;
using namespace std;
template <typename T>
inline void sc(T &x) {
x = 0;
static int p;
p = 1;
static char c;
c = getchar();
while (!isdigit(c)) {
if (c == '-') p = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c - 48);
c = getchar();
}
x *= p;
}
template <typename T>
inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
inline void pr(T x) {
print(x), putchar(' ');
}
int del(int x) { return x >= P ? x - P : x; }
void add(int &x, int y) { x = del(x + y); }
int ksm(int a, int b) {
int ans = 1;
while (b) {
if (b & 1) ans = (long long)ans * a % P;
a = (long long)a * a % P, b >>= 1;
}
return ans;
}
int a[N][N];
int gauss(int n, int op) {
int ans = 1;
for (int i = 1; i <= n; i++) {
if (!a[i][i])
for (int j = i + 1; j <= n; j++)
if (a[j][i]) {
for (int k = i; k <= n + 1; k++) swap(a[i][k], a[j][k]);
break;
}
for (int j = i + 1; j <= n; j++)
if (a[j][i]) {
int p = (long long)a[j][i] * ksm(a[i][i], P - 2) % P;
for (int k = i; k <= n + 1; k++)
add(a[j][k], P - (long long)p * a[i][k] % P);
}
ans = (long long)ans * a[i][i] % P;
}
if (op) {
for (int i = n; i >= 1; i--) {
a[i][n + 1] = (long long)a[i][n + 1] * ksm(a[i][i], P - 2) % P,
a[i][i] = 1;
for (int j = i - 1; j >= 1; j--)
add(a[j][n + 1], P - (long long)a[i][n + 1] * a[j][i] % P), a[j][i] = 0;
}
}
return ans;
}
int b[N];
bool e[N][N];
int main() {
int n;
sc(n);
for (int i = 1; i < n; i++) {
int x, y;
sc(x), sc(y);
e[x][y] = e[y][x] = 1;
}
for (int x = 1; x <= n; x++) {
for (int i = 1; i <= n; i++) memset(a[i], 0, sizeof(int) * (n + 1));
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++) {
int g = e[i][j] ? x : 1;
add(a[i][i], g), add(a[j][j], g);
add(a[i][j], P - g), add(a[j][i], P - g);
}
b[x] = gauss(n - 1, 0);
}
for (int i = 1; i <= n; i++) {
a[i][1] = 1;
for (int j = 1; j < n; j++) a[i][j + 1] = (long long)a[i][j] * i % P;
a[i][n + 1] = b[i];
}
gauss(n, 1);
for (int i = 1; i <= n; i++) pr(a[i][n + 1]);
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b;
cin >> n >> a >> b;
long long ans = LONG_LONG_MIN;
for (long long i = 1; i < n; i++) {
long long one = i;
long long two = n - i;
ans = max(ans, min(a / one, b / two));
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
#include<iostream>
#include<cstring>
using namespace std;
bool change(string *s, int x, int y){
// std::cout << x << " " << y << std::endl;
if(s[y][x] != s[y][x + 1]){
char tmp = s[y][x];
s[y][x] = s[y][x + 1];
s[y][x + 1] = tmp;
return true;
}
return false;
}
void drop(string *s, int w, int h){
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int k = h - 1; k > i ; k--) {
if(s[k][j] == '.'){
char tmp = s[k][j];
s[k][j] = s[k - 1][j];
s[k - 1][j] = tmp;
}
}
}
}
}
bool erace(string *s, int w, int h, int n){
int count;
int mark[h][w];
memset(mark, 0,sizeof(mark));
char prev;
bool res = false;
for (int i = 0; i < h; i++) {
count = 1;
prev = s[i][0];
for (int j = 1; j <= w; j++) {
if(j < w && prev == s[i][j] && prev != '.'){
count++;
}else{
if(count >= n){
for (int k = 1; k <= count; k++)
mark[i][j - k] = 1;
}
count = 1;
if(j < w)prev = s[i][j];
}
}
}
for (int i = 0; i < w; i++){
count = 1;
prev = s[0][i];
for (int j = 1; j <= h; j++){
if(j < h && prev == s[j][i] && prev != '.'){
count++;
}else{
if(count >= n){
for (int k = 1; k <= count; k++)
mark[j - k][i] = 1;
}
count = 1;
if(j < h)prev = s[j][i];
}
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if(mark[i][j] == 1){
res = true;
s[i][j] = '.';
}
}
}
return res;
}
int main(){
int n, h, w;
cin >> h >> w >> n;
string s[40];
for (int i = 0; i < h; i++){
cin >> s[i];
}
string tmp[40];
for (int i = 0; i < h; i++){
for (int j = 0; j < w - 1; j++){
for (int k = 0; k < h; k++) {
tmp[k] = s[k];
}
if(change(tmp, j, i)){
drop(tmp, w, h);
while(erace(tmp, w, h, n)){
drop(tmp, w, h);
}
// std::cout << "after drop" << std::endl;
// for (int i = 0; i < h; i++) {
// std::cout << tmp[i] << std::endl;
// }
// std::cout << std::endl;
int flag = 1;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if(tmp[y][x] != '.'){
flag = 0;
}
}
}
if(flag){
std::cout << "YES" << std::endl;
return 0;
}
}
}
}
std::cout << "NO" << std::endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
long long ans = 1;
long long m = 1;
for (long long i = 0; i < n - 1; i++) {
if (arr[i] * arr[i + 1] > ans) ans = arr[i] * arr[i + 1];
}
cout << ans << "\n";
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5, MOD = 1e9 + 7;
int a[N], p[N], inv[N], whos[N], pw[N], id[N], n, q;
int mul(int a, int b) { return 1ll * a * b % MOD; }
int sum(int a, int b) { return a + b - (a + b >= MOD ? MOD : 0); }
int Pow(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = mul(a, a))
if (b & 1) res = mul(res, a);
return res;
}
struct Node {
int Val = 0, Cnt = 0, L = 0, R = 0;
} seg[N << 2];
void Merge(Node &A, Node &B, Node &C) {
C.Val = sum(sum(mul(A.Val, pw[B.Cnt]), mul(B.Val, pw[A.Cnt])), mul(A.L, B.R));
C.Cnt = A.Cnt + B.Cnt;
C.L = sum(A.L, mul(B.L, pw[A.Cnt]));
C.R = sum(B.R, mul(A.R, pw[B.Cnt]));
}
void change(int l, int L = 0, int R = n + q, int id = 1) {
if (R - L == 1) {
if (seg[id].Cnt)
seg[id].L = seg[id].R = seg[id].Cnt = 0;
else
seg[id].L = seg[id].R = a[p[l]], seg[id].Cnt = 1;
return;
}
int mid = (L + R) >> 1;
if (l < mid)
change(l, L, mid, id << 1);
else
change(l, mid, R, id << 1 | 1);
Merge(seg[id << 1], seg[id << 1 | 1], seg[id]);
return;
}
bool cmp(int i, int j) { return a[i] < a[j]; }
int main() {
pw[0] = 1;
for (int i = 1; i < N; i++) pw[i] = mul(pw[i - 1], 2);
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i], whos[i] = p[i] = i;
cin >> q;
for (int i = 0; i < q; i++) {
int pos;
cin >> pos, pos--;
id[n + i] = whos[pos], whos[pos] = n + i;
cin >> a[n + i], p[n + i] = n + i;
}
sort(p, p + n + q, cmp);
for (int i = 0; i < n + q; i++) inv[p[i]] = i;
for (int i = 0; i < n + q; i++) {
if (i >= n) change(inv[id[i]]);
change(inv[i]);
if (i >= n - 1) cout << mul(seg[1].Val, Pow(pw[n], MOD - 2)) << "\n";
}
return 0;
}
| 12 | CPP |
n,m,k=map(int,input().split())
if k<=n-1 :
print(k+1,1)
exit()
r=m-1
ma=n*r
k1=k-n+1
if k1>=ma :
print(1,2)
exit()
p=k1//r
if k1%r!=0 :
p+=1
k1-=(m-1)*(p-1)
if abs(n-p)%2!=0 :
print(n-p+1,k1+1)
else :
print(n-p+1,m-k1+1)
| 8 | PYTHON3 |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <set>
#define REP(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
int main() {
int N, M;
while (cin >> N >> M, N + M) {
vector<vector<int>> G(N);
REP (i, 0, M) {
int u, v;
cin >> u >> v;
u--;v--;
G[u].push_back(v);
G[v].push_back(u);
}
vector<int> color(N, 0);
function<bool(int, int)> dfs = [&](int ind, int c) {
if (color[ind] != 0) {
return color[ind] == c;
}
color[ind] = c;
REP (i, 0, G[ind].size()) {
bool res = dfs(G[ind][i], -1 * c);
if (!res) {
return false;
}
}
return true;
};
set<int> s;
REP (i, 0, 2) {
color.assign(N, 0);
bool res = dfs(0, 2 * i - 1);
if (res) {
int a = 0, b = 0;
REP (j, 0, N) {
if (color[j] == 1) {
a++;
} else {
b++;
}
}
if (a % 2 == 0) {
s.insert(a / 2);
}
if (b % 2 == 0) {
s.insert(b / 2);
}
}
}
cout << s.size() << endl;
for (auto i : s) {
cout << i << endl;
}
}
return 0;
}
| 0 | CPP |
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, m = map(int, (input().split()))
dic = defaultdict(list)
seen = [-1] * (n+1)
for _ in range(m):
a, b = map(int, input().split())
dic[a].append(b)
dic[b].append(a)
def dfs(v, todo, color):
if seen[v] == color: # εγθ²γ§ζ»γ£γ¦γγ = εΆγ΅γ€γ―γ«
return
elif seen[v] == 1-color: # η°γͺγθ²γ§ζ»γ£γ¦γγ = ε₯γ΅γ€γ―γ«
ans = 0.5 * n * (n-1) - m
print(int(ans))
sys.exit()
seen[v] = color
for new_v in todo:
new_todo = dic[new_v]
dfs(new_v, new_todo, 1-color)
dfs(1, dic[1], 0)
black = seen.count(0)
white = seen.count(1)
ans = black * white - m
print(int(ans)) | 0 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
const int MOD = 1000000007;
const long long MOD2 = 998244353;
const char nl = '\n';
int modulo(int n, int M) { return ((n % M) + M) % M; }
template <class T>
void print1D(T& a) {
for (int j = 0; j < a.size(); j++) {
cout << a[j] << " ";
}
cout << endl;
}
template <class T>
void print2D(T& a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[0].size(); j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
int mul(long long a, long long b, long long mod) { return (a * b) % mod; }
void solve() {
int n, m;
cin >> n >> m;
vector<int> dp(n + 1, 0);
dp[1] = 1;
int dp_sum = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp_sum;
for (int j = 1; j * j <= i; j++) {
dp[i] = (dp[i] + mul(dp[j], (i / j) - (i / (j + 1)), m)) % m;
if (j != i / j and j > 1) {
dp[i] = (dp[i] + dp[i / j]) % m;
}
}
dp_sum = (dp_sum + dp[i]) % m;
}
cout << dp[n] << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
for (int t = 1; t <= tc; t++) {
solve();
}
}
| 10 | CPP |
N = int(input())
A = list(map(int,input().split()))
B = [a-i-1 for i,a in enumerate(A)]
med = list(sorted(B))[N//2]
print(sum([abs(a-med) for a in B]))
| 0 | PYTHON3 |
#include <bits/stdc++.h>
const double PI = acos(-1.0);
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <class T>
inline T Min(T a, T b) {
return a < b ? a : b;
}
template <class T>
inline T Max(T a, T b) {
return a > b ? a : b;
}
using namespace std;
template <class T>
T Mint(T a, T b, T c) {
if (a > b) {
if (c > b) return b;
return c;
}
if (c > a) return a;
return c;
}
template <class T>
T Maxt(T a, T b, T c) {
if (a > b) {
if (c > a) return c;
return a;
} else if (c > b)
return c;
return b;
}
const int maxn = 110;
int n;
int a[2][maxn];
int b[maxn];
int c[maxn];
int main() {
while (~scanf("%d", &n)) {
int suma = 0, sumb = 0;
for (int i = (0); i < 2; i++) {
for (int j = (0); j < n - 1; j++) {
scanf("%d", &a[i][j]);
if (i == 1) sumb += a[i][j];
}
}
for (int i = (0); i < n; i++) {
scanf("%d", &b[i]);
}
for (int i = (0); i < n; i++) {
c[i] = suma + sumb + b[i];
sumb -= a[1][i];
suma += a[0][i];
}
sort(c, c + n);
printf("%d\n", c[0] + c[1]);
}
return 0;
}
| 8 | CPP |
n=int(input())
print('YNEOS'[all(n%x for x in[4,7,47,74,447,474,477,747,777])::2]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100000;
const long long INF = 1000000000000000000LL;
struct Info {
long long s, k;
Info() { s = k = 0; }
Info(long long x, long long y) {
s = x;
k = y;
}
Info operator+(const Info& other) const { return {s + other.s, k + other.k}; }
};
long long v[MAXN + 5];
Info best(Info a, Info b) {
if (a.s > b.s) return a;
if (a.s < b.s) return b;
if (a.k < b.k) return a;
return b;
}
Info check(long long n, long long c) {
Info dp0 = {0, 0};
Info dp1 = {-INF, 0};
for (long long i = 1; i <= n; ++i) {
Info aux0, aux1;
aux0 = best(dp0, dp1 + Info(v[i], 0));
aux1 = best(dp0 + Info(-v[i] - c, 1), dp1);
dp0 = aux0;
dp1 = aux1;
}
return best(dp0, dp1);
}
long long solve() {
long long n, k;
scanf("%lld%lld", &n, &k);
for (long long i = 1; i <= n; ++i) scanf("%lld", &v[i]);
long long l = 0, r = INF, last;
while (l <= r) {
long long mid = ((unsigned long long)l + r) / 2;
if (check(n, mid).k <= k) {
last = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return check(n, last).s + k * last;
}
int main() {
printf("%lld\n", solve());
return 0;
}
| 12 | CPP |
str = input()
print("ARC" if(str[1] == "B") else "ABC") | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005, M = 60;
struct Node {
int tm[M];
Node(int x = 2) {
for (int i = 0; i < (M); ++i) {
int cur = i;
while (cur % x == 0) ++cur;
tm[i] = cur - i + 1;
}
}
Node operator+(const Node& y) const {
Node res;
for (int i = 0; i < (M); ++i) res.tm[i] = tm[i] + y.tm[(i + tm[i]) % M];
return res;
}
} seg[N << 2], res;
int n, m;
void modify(int c, int l, int r, int p, int x) {
if (l == r)
seg[c] = Node(x);
else {
int m = (l + r) >> 1;
if (p <= m)
modify(c << 1, l, m, p, x);
else
modify(c << 1 | 1, m + 1, r, p, x);
seg[c] = seg[c << 1] + seg[c << 1 | 1];
}
}
void query(int c, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr)
res = res + seg[c];
else {
int m = (l + r) >> 1;
if (ql <= m) query(c << 1, l, m, ql, qr);
if (qr > m) query(c << 1 | 1, m + 1, r, ql, qr);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < (n + 1); ++i) scanf("%d", &m), modify(1, 1, n, i, m);
scanf("%d", &m);
for (int i = 0; i < (m); ++i) {
char s[10];
int l, r;
scanf("%s%d%d", s, &l, &r);
if (s[0] == 'C')
modify(1, 1, n, l, r);
else {
for (int i = 0; i < (M); ++i) res.tm[i] = 0;
query(1, 1, n, l, r - 1);
printf("%d\n", res.tm[0]);
}
}
return 0;
}
| 10 | CPP |
#231A Team, bruteforce/greedy, 800, x94000+
#http://codeforces.com/problemset/problem/231/A
n = int(input())
s = 0
for i in range(n):
r = sum(map(int, input().split()))
if r > 1:
s += 1
print(s)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n], i;
for (i = 0; i < n; i++) {
cin >> arr[i];
}
int countt = 0;
for (i = 1; i < n - 1; i++) {
if (arr[i] < arr[i + 1] && arr[i] < arr[i - 1])
countt++;
else if (arr[i] > arr[i + 1] && arr[i] > arr[i - 1])
countt++;
}
cout << countt << endl;
}
| 7 | CPP |
x = 0
numOperations = int(input())
for _ in range(numOperations):
operation = input()
if operation == "X++" or operation == "++X":
x += 1
elif operation == "X--" or operation == "--X":
x -= 1
print(x)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
const int maxn = 200 + 5;
const int maxm = 3e6 + 5;
template <class T>
inline void dbg(T a[], int n) {
for (int i = (0); i < (n); i++) cout << a[i] << (i == n - 1 ? "\n" : " ");
}
template <class T>
inline void dbg(T a) {
cout << a << endl;
}
namespace fastIO {
bool IOerror = 0;
inline char nc() {
static char buf[100005], *p1 = buf + 100005, *pend = buf + 100005;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, 100005, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
inline void read(int &x) {
char ch;
while (blank(ch = nc()))
;
if (IOerror) return;
for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0')
;
}
}; // namespace fastIO
using namespace fastIO;
int main() {
int t;
scanf("%d", &t);
while (t--) {
double a, b;
scanf("%lf%lf", &a, &b);
if (a == 0. && b == 0.)
printf("1.0\n");
else if (a == 0.)
printf("0.5\n");
else if (a >= 4. * b)
printf("%f\n", (a - b) / a);
else
printf("%f\n", (a / 8. + b) / (2. * b));
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
long double sum[MAXN], ps[MAXN], s, ans;
int main() {
int n, m;
cin >> n >> m;
if (m == 1) {
int x;
cin >> x;
cout << x << endl;
return 0;
}
fill(ps, ps + MAXN, m - 1);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
for (int i = 1; i <= n * m; i++)
sum[i] = ((ps[i - 1] - (i - x < 0 ? 0. : ps[i - x])) +
((i - x - 1 < 0 ? 0. : ps[i - x - 1]) -
(i - x - 1 - (m - x) < 0 ? 0 : ps[i - x - 1 - (m - x)]))) /
(m - 1);
ps[0] = 0;
for (int i = 1; i <= n * m; i++) ps[i] = ps[i - 1] + sum[i];
s += x;
}
for (int i = 1; i < s; i++) ans += sum[i];
cout << setprecision(15) << fixed << ans + 1 << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, tot[26], p[maxn];
char s[maxn], t[maxn];
vector<int> pos[26];
int val[maxn];
inline void Add(int x) {
for (; x <= n; x += x & -x) ++val[x];
}
inline int Ask(int x) {
int res = 0;
for (; x; x -= x & -x) res += val[x];
return res;
}
int main() {
scanf("%d %s", &n, s + 1);
for (int i = 1; i <= n; i++) {
pos[s[i] - 'a'].push_back(i);
t[n - i + 1] = s[i];
}
for (int i = 1; i <= n; i++) p[i] = pos[t[i] - 'a'][tot[t[i] - 'a']++];
long long ans = 0;
for (int i = n; i; i--) {
ans += Ask(p[i]);
Add(p[i]);
}
printf("%lld\n", ans);
return 0;
}
| 11 | CPP |
import sys
import math
input=sys.stdin.readline
def sum(n):
i = 1
s = 0.0
for i in range(1, n+1):
s = s + 1/i;
return s;
# Driver Code
n=int(input())
print( round(sum(n), 6))
| 8 | PYTHON3 |
n=int(input())
s=0
for x in range(n):
a,b,c=input().split()
a,b,c=int(a),int(b),int(c)
if a+b+c>1:
s=s+1
else:
pass
print(s)
| 7 | PYTHON3 |
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
struct Info{
int cicle[51],cicile_index,cicle_num,wait_time,tmp_time;
bool is_queue;
};
int N,Time_Limit;
Info info[100];
void func(){
int tmp;
for(int i = 0; i < N; i++){
info[i].cicile_index = 0;
info[i].wait_time = 0;
info[i].tmp_time = 0;
info[i].is_queue = false;
for(int k = 0; ;k++){
scanf("%d",&tmp);
if(tmp != 0){
info[i].cicle[k] = tmp;
}else{
info[i].cicle_num = k;
break;
}
}
}
queue<int> Q;
bool Finished;
for(int time = 0; time < Time_Limit; time++){
Finished = false;
for(int i = 0; i < N; i++){
if(info[i].is_queue == false){
info[i].tmp_time++;
if(info[i].tmp_time == info[i].cicle[info[i].cicile_index]){
info[i].tmp_time = 0;
info[i].cicile_index++;
Q.push(i);
info[i].is_queue = true;
}
}else{
if(i == Q.front()){
info[i].tmp_time++;
if(info[i].tmp_time == info[i].cicle[info[i].cicile_index]){
info[i].tmp_time = 0;
if(info[i].cicile_index == info[i].cicle_num-1){
info[i].cicile_index = 0;
}else{
info[i].cicile_index++;
}
info[i].is_queue = false;
Finished = true;
}
}else{
info[i].wait_time++;
}
}
}
if(Finished)Q.pop();
}
int ans = 0;
for(int i = 0; i < N; i++){
ans += info[i].wait_time;
}
printf("%d\n",ans);
}
int main(){
while(true){
scanf("%d %d",&N,&Time_Limit);
if(N == 0 && Time_Limit == 0)break;
func();
}
return 0;
} | 0 | CPP |
#include <stdio.h>
int main(){
char in[12345];
while(scanf("%s",in)!=EOF){
int JOI=0,IOI=0;
for(int i=0;in[i]!='\0';i++){
if(in[i]=='J'&&in[i+1]=='O'&&in[i+2]=='I') JOI++;
if(in[i]=='I'&&in[i+1]=='O'&&in[i+2]=='I') IOI++;
}
printf("%d\n%d\n",JOI,IOI);
}
return 0;
} | 0 | CPP |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
string S;
cin >>N;
cin>>S;
long long int r=0;int g=0;int b=0;
for (int i = 0; i < N; i++){
if(S[i]=='R')r++;
else if(S[i]=='G')g++;
else b++;
}
long long int ans=r*g*b;
for (int step = 1; step < N; step++){
for (int i = 0; i < N; i++){
if(i+step*2>=N)break;
if(S[i]!=S[i+step]&&S[i]!=S[i+step*2]&&S[i+step]!=S[i+step*2]){
ans--;
}
}
}
cout<<ans<<endl;
}; | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int arr[N];
int tr[30 * N][2];
int compute(int l, int m, int r) {
tr[0][0] = tr[0][1] = -1;
int ptr = 1;
for (int i = l; i <= m; i++) {
int rt = 0;
for (int j = 29; j >= 0; j--) {
if (arr[i] & (1 << j)) {
if (tr[rt][1] == -1) {
tr[ptr][0] = tr[ptr][1] = -1;
tr[rt][1] = ptr++;
}
rt = tr[rt][1];
} else {
if (tr[rt][0] == -1) {
tr[ptr][0] = tr[ptr][1] = -1;
tr[rt][0] = ptr++;
}
rt = tr[rt][0];
}
}
}
int ret = arr[l] ^ arr[r];
for (int i = m + 1; i <= r; i++) {
int rt = 0;
int tmp = 0;
for (int j = 29; j >= 0; j--) {
if (arr[i] & (1 << j)) {
if (tr[rt][1] != -1) {
rt = tr[rt][1];
} else {
rt = tr[rt][0];
tmp ^= (1 << j);
}
} else {
if (tr[rt][0] != -1) {
rt = tr[rt][0];
} else {
rt = tr[rt][1];
tmp ^= (1 << j);
}
}
}
ret = min(ret, tmp);
}
return ret;
}
long long dnc(int l, int r, int ind) {
if (ind == -1) return 0;
if (arr[r] < (1 << ind)) return dnc(l, r, ind - 1);
if (arr[l] >= (1 << ind)) {
for (int i = l; i <= r; i++) {
arr[i] ^= (1 << ind);
}
return dnc(l, r, ind - 1);
}
int lo = l, hi = r;
while (lo < hi) {
int mi = lo + (hi - lo + 1) / 2;
if (arr[mi] < (1 << ind)) {
lo = mi;
} else {
hi = mi - 1;
}
}
long long ret = compute(l, lo, r);
if (ind != 0) {
for (int i = lo + 1; i <= r; i++) {
arr[i] ^= (1 << ind);
}
ret += dnc(l, lo, ind - 1);
ret += dnc(lo + 1, r, ind - 1);
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << setprecision(32);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
sort(arr + 1, arr + n + 1);
cout << dnc(1, n, 29) << endl;
return 0;
}
| 13 | CPP |
from collections import Counter
n=int(input())
s=input()
k=Counter(s)
if k['A']>k['D']:
print("Anton")
elif k['A']<k['D']:
print("Danik")
else:
print("Friendship") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector <int> V;
int n,x,y,z,k;
int dp[100010][3];
int main()
{
cin >> n;
for (int i=1;i<=n;i++){
cin >> x >> y >> z;
dp[i][0]=x+max(dp[i-1][1],dp[i-1][2]);
dp[i][1]=y+max(dp[i-1][0],dp[i-1][2]);
dp[i][2]=z+max(dp[i-1][0],dp[i-1][1]);
}
cout << max(max(dp[n][0],dp[n][1]),dp[n][2]);
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
double P[2000000];
double DP[2000000];
double result[2000000];
int main() {
int N, K;
scanf("%d%d", &N, &K);
for (int i = 0; i < (N); ++i) scanf("%lf", &P[i]);
int K2 = 0;
for (int i = 0; i < (N); ++i)
if (P[i] > 1e-3) ++K2;
K = min(K, K2);
DP[0] = 1.0;
for (int i = 0; i < ((1 << N) + 1); ++i) {
int bc = 0;
double total = 0.0;
for (int j = 0; j < (N); ++j) {
if (i & (1 << j))
++bc;
else
total += P[j];
}
if (bc == K) {
for (int j = 0; j < (N); ++j)
if (i & (1 << j)) result[j] += DP[i];
} else if (bc < K) {
for (int j = 0; j < (N); ++j)
if (!(i & (1 << j))) {
DP[i | (1 << j)] += DP[i] * P[j] / total;
}
}
}
for (int i = 0; i < (N); ++i) printf("%0.9lf ", result[i]);
printf("\n");
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[1010];
int main() {
int n, m, x;
cin >> n >> m;
while (m--) {
scanf("%d", &x);
a[x]++;
}
cout << *min_element(a + 1, a + n + 1) << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 110000;
int n;
double A, D, t[MaxN], v[MaxN], Ans[MaxN];
int main() {
scanf("%d%lf%lf", &n, &A, &D);
for (int i = 1; i <= n; ++i) {
scanf("%lf%lf", t + i, v + i);
double T = v[i] / A;
if (A * T * T / 2.0 > D)
Ans[i] = sqrt(2.0 * D / A);
else
Ans[i] = T + (D - A * T * T / 2.0) / v[i];
Ans[i] += t[i];
}
for (int i = 1; i <= n; ++i) {
Ans[i] = max(Ans[i], Ans[i - 1]);
printf("%.10lf\n", Ans[i]);
}
return 0;
}
| 7 | CPP |
a = input()
a = [int(s) for s in a.split()]
while a[1]!=0:
if a[0] % 10 != 0:
a[0] = a[0] -1
else:
a[0] = a[0]/10
a[1] = a[1] -1
print(int(a[0])) | 7 | PYTHON3 |
a,b,c=input().split();print('NYOE S'[a[-1]==b[0] and b[-1]==c[0]::2]) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
const int MX = 1000001;
int n, p[MX];
vector<vector<int> > e;
int dp[MX];
set<pair<int, int> > memo[MX];
int main() {
scanf("%d", &n);
n++;
e.resize(n);
for (int i = 1; i < n; i++) {
scanf("%d", p + i);
p[i]--;
e[p[i]].push_back(i);
e[i].push_back(p[i]);
}
int ans = 0;
for (int it = 1; it < n; it++) {
int v = it;
dp[v] = 1;
for (; p[v] != 0; v = p[v]) {
vector<int> u;
int prev = dp[p[v]];
int next;
memo[p[v]].erase(make_pair(-dp[v] + 1, v));
memo[p[v]].insert(make_pair(-dp[v], v));
set<pair<int, int> >::iterator it = memo[p[v]].begin();
next = -it->first;
if (memo[p[v]].size() > 1) next = max(next, -(++it)->first + 1);
if (prev == next) break;
dp[p[v]] = next;
}
if (p[v] == 0) ans = max(ans, dp[v]);
printf("%d%c", ans, v == n - 1 ? '\n' : ' ');
}
return 0;
}
| 10 | CPP |
import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
# 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")
stdin, stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
mod = pow(10, 9) + 7
mod2 = 998244353
def inp(): return stdin.readline().strip()
def iinp(): return int(inp())
def out(var, end="\n"): stdout.write(str(var)+"\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)]
def ceil(a, b): return (a+b-1)//b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def isprime(x):
if x<=1: return False
if x in (2, 3): return True
if x%2 == 0: return False
for i in range(3, int(sqrt(x))+1, 2):
if x%i == 0: return False
return True
for _ in range(iinp()):
n = iinp()
arr = lmp()
c = arr.count(1)
ml = l1d(c, inf)
for i in range(7):
if arr[i]:
x = 0
for j in range(i, i+7):
if arr[j%7]:
ml[x] = min(ml[x], j-i+1)
x += 1
print((n-1)//c*7 + ml[(n-1)%c]) | 13 | PYTHON3 |
n = int(input())
arr = list(map(int, input().split()))
arr.append(0)
op = 0
i = 0
lastzero = False
while True:
if arr[i] == 1:
op += 1
if arr[i + 1] == 0:
op += 1
if i + 1 != len(arr):
i += 1
else:
break
if op < 1:
print(0)
else:
print(op - 1)
| 8 | PYTHON3 |
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 110
#define MAXM 32800
#define MO 1000000007
int f[MAXM],pow2[MAXN],n,m,tot,G[20];
int Count(int x)
{
int ret=0;
while(x)
{
ret+=x&1;
x>>=1;
}
return ret;
}
int main()
{
int x,y;
scanf("%d%d",&n,&m);
tot=1<<n;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
x--,y--;
G[x]|=(1<<y);
}
f[0]=1;pow2[0]=1;
for(int i=1;i<=m;i++)
pow2[i]=2LL*pow2[i-1]%MO;
for(int S=1;S<tot;S++)
if((S&1)==((S>>1)&1))
{
for(int U=S;U;U=(U-1)&S)
if((U&1)==((U>>1)&1))
{
int w=1;
for(int i=0;i<n;i++)
if((S>>i)&1)
{
if((U>>i)&1) w=1LL*w*pow2[Count(G[i]&(S^U))]%MO;
else w=1LL*w*(pow2[Count(G[i]&U)]-1)%MO;
}
f[S]=(f[S]+1LL*w*f[S^U]%MO)%MO;
}
}
int ans=((pow2[m]-f[tot-1])%MO+MO)%MO;
printf("%d\n",ans);
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const double PI = 2 * acos(0.0);
const long double eps = 1e-12;
int diri[] = {0, 1, 0, -1};
int dirk[] = {1, 0, -1, 0};
long long MOD = 1e6 + 3;
int main() {
long long n, res;
while (cin >> n) {
res = 1;
for (int i = 0; i < (int)(n - 1); ++i) {
res *= 3;
res %= MOD;
}
cout << res << endl;
}
return 0;
}
| 7 | CPP |
n = int(input())
temp = input()
a = []
for i in range(n):
if temp[i] == "8":
a.append(1)
else:
a.append(0)
count = 0
for i in range(n):
if a[i] == 1 and count < (n-11)//2:
a[i] = 2
count += 1
i = (n-11)//2 + 1
j = 0
ans = True
while i > 0 and j < n:
if a[j] == 1:
print("YES")
ans = False
break
elif a[j] == 2:
j += 1
continue
j += 1
i -= 1
if ans:
print("NO")
| 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n, f[101000], las[3];
char s[101000];
int main(){
cin>>s+1; n=strlen(s+1);
for (int i=2;;++i){
if (i>n) return puts("1"), 0;
if (s[i]==s[i-1]) break;
}
las[0]=n+1; las[1]=las[2]=n+2;
int sum=0; f[n+1]=1;
for (int i=n;i>=1;--i){
sum=(sum+1+(s[i]=='b'))%3;
f[i]=f[i+1];
int aim=(sum-1-(s[i]=='a')+3)%3;
f[i]=(f[i]+f[las[aim]])%mod;
las[sum]=i;
if (!sum&&i>1) f[i]=(f[i]+1)%mod;
}
cout<<f[1]<<endl;
} | 0 | CPP |
for _ in range(int(input())):
n = int(input())
a = [int(i) for i in input().split()]
ones = a.count(1)
if ones == 1:
print(0)
else:
num_of_gaps = 0
flag = 0
for a_i in a:
if flag == 0:
if a_i == 1:
ones -= 1
flag = 1
else:
if a_i == 0 and ones != 0:
num_of_gaps += 1
elif a_i == 1:
ones -= 1
print(num_of_gaps)
| 8 | PYTHON3 |
n=int(input())
for i in range(n):
k=int(input())
l=list(map(int,input().split()))
print(3*k)
for j in range(0,(k),2):
print(1,"",j+1,"",j+2)
print(2,"",j+1,"",j+2)
print(1,"",j+1,"",j+2)
print(2,"",j+1,"",j+2)
print(1,"",j+1,"",j+2)
print(2,"",j+1,"",j+2)
| 8 | PYTHON3 |
n = int(input())
a = {}
c = 0
vector = []
for i in range(n):
tabela = [j for j in input().split()]
if not(tabela[0] in a):
a.update({tabela[0]:c,c:tabela[0]})
c+=1
vector.append([])
indice = a[tabela[0]]
if(vector[indice] == []):
vector[indice].append(tabela[2])
for j in range(2,int(tabela[1])+2):
tn = len(tabela[j])
chave = True
for k in range(len(vector[indice])):
tv = len(vector[indice][k])
if(tv >= tn and vector[indice][k][-tn::] == tabela[j]):
chave = False
break
elif(tabela[j][-tv::] == vector[indice][k]):
chave = False
del(vector[indice][k])
vector[indice].append(tabela[j])
break
if chave:
vector[indice].append(tabela[j])
print(int(c))
for i in range(c):
print("%s %d" % (a[i],len(vector[i])),end="")
for j in vector[i]:
print(" %s" % j,end="")
print()
| 9 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int n, k;
int num[100000 + 22];
bool flag;
while (~scanf("%d %d", &n, &k)) {
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
flag = true;
for (int i = k + 1; i <= n; i++) {
if (num[i] != num[k]) {
flag = false;
break;
}
}
if (!flag) {
printf("-1\n");
continue;
}
int ans;
for (ans = k - 1; ans > 0; ans--) {
if (num[ans] != num[k]) break;
}
printf("%d\n", ans);
}
return 0;
}
| 7 | CPP |
#A. Kitahara Haruki's Gift
n = input()
dic = {100:0,
200:0}
a = list(map(int,input().split()))
for i in a:
dic[i]+=1
#case when both are even
if dic[100]%2==0 and dic[200]%2==0:
print('YES')
elif dic[200]%2!=0 and dic[100]%2==0 and dic[100]>=2 :
print('YES')
else:
print('NO') | 7 | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.