solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
def act(x, n, m):
y = (x // 2) + 10
z = x - 10
if (n > 0 and y < x):
n -= 1
return y, n, m, 0
elif m > 0:
m -= 1
return z, n, m, 0
else:
return x, n, m, 1
for _ in range(int(input())):
x, n, m = map(int, input().split())
while x > 0 and (n > 0 or m > 0):
x, n, m, f = act(x, n, m)
if f:
break
if x > 0:
print("NO")
else:
print("YES")
| 8 | PYTHON3 |
n = list(map(int, input().rstrip().split()))
m = max(n)
l = len(n)
n.remove(m)
for i in range(l-1):
print((m-n[i]),end=' ') | 7 | PYTHON3 |
s = input()
ans = 0
for i in range(len(s)//2):
if s[:i] == s[i:2*i]:
ans = i
print(2 * ans) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int findAns(vector<int> arr) {
int diff = arr[1] - arr[0], totalOperations = 0;
for (int i = 2; i < arr.size(); i++) {
if (abs(arr[i] - arr[i - 1] - diff) > 1)
return -1;
else {
totalOperations += abs(arr[i] - arr[i - 1] - diff);
arr[i] = arr[i - 1] + diff;
}
}
return totalOperations;
}
int main() {
int size;
cin >> size;
vector<int> arr(size);
for (int i = 0; i < size; i++) cin >> arr[i];
if (size <= 2) {
cout << 0 << endl;
return 0;
}
int lim = 1000000000, minOperations = lim;
int first = arr[0], second = arr[1];
for (int i = 1; i >= -1; i--)
for (int j = 1; j >= -1; j--) {
arr[0] = first + i;
arr[1] = second + j;
int currAns = findAns(arr);
if (currAns != -1)
minOperations = min(minOperations, currAns + abs(i) + abs(j));
}
if (minOperations != lim)
cout << minOperations << endl;
else
cout << "-1" << endl;
return 0;
}
| 10 | CPP |
n=int(input())
while 1:
if sum([int(i) for i in list(str(n))])%4==0:
print(n)
break
n=n+1 | 7 | PYTHON3 |
n=int(input())
for i in range(n):
board=[]
check=[[0 for a in range(9)] for b in range(9)]
for j in range(9):
board.append(list(map(int,input().split())))
board_r = [list(x) for x in zip(*board)]
for j in range(9):
for k in range(9):
if board[j].count(board[j][k])>1:check[j][k]=1
for j in range(9):
for k in range(9):
if board_r[j].count(board_r[j][k])>1:check[k][j]=1
for j in range(3):
for k in range(3):
ch_list=[]
for l in range(9):
ch_list.append(board[j*3+l%3][k*3+l//3])
for l in range(9):
if ch_list.count(ch_list[l])>1:check[j*3+l%3][k*3+l//3]=1
for j in range(9):
for k in range(9):
if check[j][k]==1:print("*",end="")
else:print(" ",end="")
print(board[j][k],end="")
print()
if i<n-1:print()
| 0 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
#define long long long
const int MOD=1e9+7;
long dp[1000001][13];
int main(){
string S;
cin >> S;
dp[0][0]=1;
for(int i=0; i<S.size(); i++){
int c;
if(S[i]=='?') c=-1;
else c=S[i]-'0';
for(int j=0; j<10; j++){
if(c!=-1 && c!=j) continue;
for(int k=0; k<13; k++){
dp[i+1][(k*10+j)%13]=(dp[i+1][(k*10+j)%13]+dp[i][k]) % MOD;
}
}
}
cout << dp[S.size()][5] << endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename G>
struct triple {
G first, second, T;
};
vector<int> query(vector<int> v) {
cout << "next";
for (auto &i : v) cout << " " << i;
cout << '\n';
cout.flush();
vector<int> w(10);
int k;
cin >> k;
for (int i = 0; i < k; i++) {
string s;
cin >> s;
for (auto &j : s) w[j - '0'] = i;
}
return w;
}
int main() {
while (true) {
query({0, 1});
auto v = query({1});
if (v[0] == v[1]) break;
}
while (true) {
auto v = query({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
bool ok = true;
for (int i = 0; i < 10; i++) ok &= (v[i] == 0);
if (ok) break;
}
cout << "done\n";
cout.flush();
return 0;
}
| 12 | CPP |
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 22 12:22:38 2020
@author: Harshal
"""
a,b=map(int,input().split())
c=0
while a<=b:
a=a*3
b=b*2
c+=1
print(c) | 7 | PYTHON3 |
#include<iostream>
using namespace std;
int main(){
int a;
cin>>a;
cout<<(1+a)*a/2<<endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > G;
vector<int> order, used;
int n, m, cur;
bool dfs(int u) {
bool f = false;
used[u] = 1;
for (int i = 0; i < (int)G[u].size(); ++i) {
int to = G[u][i];
if (used[to] == 1) {
return true;
}
if (!used[to]) {
f = f | dfs(to);
}
}
if (f) {
return true;
}
used[u] = 2;
order.push_back(u);
return false;
}
int main() {
int i, j, k, u;
scanf("%d%d", &n, &m);
G.resize(4 * n * m);
used.resize(G.size(), 0);
cur = m;
for (i = 0; i < n; ++i) {
vector<pair<int, int> > v(m);
for (j = 0; j < m; ++j) {
scanf("%d", &v[j].first);
v[j].second = j;
}
sort((v).begin(), (v).end());
j = 0;
while (j < (int)v.size() && v[j].first == -1) {
++j;
}
for (; j < (int)v.size();) {
k = j;
while (k < (int)v.size() && v[k].first == v[j].first) {
++k;
}
for (u = k - 1; u >= j; --u) {
G[cur].push_back(v[u].second);
G[v[u].second].push_back(cur + 1);
}
j = k;
++cur;
}
++cur;
}
for (i = 0; i <= cur; ++i) {
if (!used[i]) {
bool f = dfs(i);
if (f) {
puts("-1");
return 0;
}
}
}
reverse((order).begin(), (order).end());
for (i = 0; i < (int)order.size(); ++i) {
if (order[i] < m) {
printf("%d ", order[i] + 1);
}
}
puts("");
return 0;
}
| 10 | CPP |
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().strip().split()))
if(a[0] >= a[n-1]):
print("NO")
else:
c = 0
for i in range(1, n):
if(a[0] < a[i] and c<0):
c+=1
elif(a[0] >= a[i] and a[-1] <= a[i]):
c-=1
if(c == 0):
print("YES")
else:
print("NO") | 9 | PYTHON3 |
for t in range(int(input())):
a,b=map(int,input().split())
if a==b:
print(0)
elif a>b:
if (a-b)%2==0:
print(1)
else:
print(2)
else:
if (a-b)%2==0:
print(2)
else:
print(1) | 7 | PYTHON3 |
n = int(input())
for _ in range(n):
cost1, cost2 = 0, 0
n, a, b = map(int,input().split())
cost1 = n*a
cost2 = int(n/2)*b + (n%2)*a
print(min(cost1,cost2)) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long p, long long mod) {
long long ret = 1;
while (p) {
if (p & 1) ret = (ret * a) % mod;
a = (a * a) % mod;
p /= 2;
}
return ret;
}
long long power(long long a, long long p) {
long long ret = 1;
while (p) {
if (p & 1) ret = (ret * a);
a = (a * a);
p /= 2;
}
return ret;
}
int p[1000011] = {0};
vector<int> prms;
void sieve(int n) {
int i, j;
prms.push_back(2);
;
for (i = 3; i < n; i++) {
if ((i % 2 == 0) || p[i]) continue;
prms.push_back(i);
p[i] = 1;
for (j = 2 * i; j < n; j += i) p[j] = 1;
}
}
vector<pair<int, int> > app;
vector<int> e;
int d[100011] = {0};
int main() {
int i, j, k, tmp;
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n;
cin >> n;
sieve(n / 2 + 1);
int l = (int)(prms.size());
j = 1;
while (j < l) {
e.clear();
for (i = prms[j]; i <= n; i += prms[j])
if (!d[i]) {
e.push_back(i);
d[i] = 1;
}
if ((int)(e.size()) % 2) {
if ((int)(e.size()) >= 3) app.push_back(make_pair(e[2], e[0]));
d[2 * prms[j]] = 0;
for (i = 4; i < (int)(e.size()); i += 2)
app.push_back(make_pair(e[i], e[i - 1]));
} else
for (i = 1; i < (int)(e.size()); i += 2)
app.push_back(make_pair(e[i], e[i - 1]));
j++;
}
e.clear();
for (i = 2; i <= n; i += 2)
if (!d[i]) {
e.push_back(i);
d[i] = 1;
}
for (i = 1; i < (int)(e.size()); i += 2)
app.push_back(make_pair(e[i], e[i - 1]));
int s = (int)(app.size());
cout << s << endl;
for (i = 0; i < s; i++) cout << app[i].first << " " << app[i].second << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[101],cr[101],b[26];
int main(){
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",a+i);
n=100;
for(int i=51;i<=100;i++)if(a[i]<0)a[i]=0;
for(int i=1;i<=25;i++)for(int j=i+i;j<=25;j+=i)b[i]|=1<<j-1;
long long ans=0;
for(int tt=0;tt<1<<25;tt++){
bool bad=false;
long long a2=0;
for(int i=1;i<=25;i++)if((tt&1<<i-1)&&(tt&b[i])){
bad=true;break;
}
if(bad)continue;
for(int i=1;i<=n;i++)cr[i]=a[i];
for(int i=1;i<=25;i++)if(tt&1<<i-1)for(int j=i;j<=n;j+=i)cr[j]=0;
for(int i=26;i<=50;i++){
if(i%2==0 && 3*i/2 <= 50){
long long t[4]={
0LL+cr[i]+cr[3*i/2]+cr[i+i]+cr[i+i+i],
cr[i]+cr[i+i],
cr[3*i/2],
0
};
int x=max_element(t,t+4)-t;
if(x&1)cr[3*i/2]=cr[i+i+i]=0;
if(x>=2)cr[i]=cr[i+i]=cr[i+i+i]=0;
}
else if(3*i <= 100){
if(0LL+cr[i]+cr[i+i]+cr[i+i+i]<0)cr[i]=cr[i+i]=cr[i+i+i]=0;
}
else{
if(cr[i]+cr[i+i]<0)cr[i]=cr[i+i]=0;
}
}
for(int i=1;i<=100;i++)a2+=cr[i];
ans=max(ans,a2);
}
printf("%lld\n",ans);
} | 0 | CPP |
#include <bits/stdc++.h>
int main() {
int n, k = 0;
scanf("%d", &n);
int *mas = new int[n];
for (int i = 0; i < n; i++) scanf("%d", &mas[i]);
for (int i = 1; i < n - 1; i++)
if ((mas[i] < mas[i - 1] && mas[i] < mas[i + 1]) ||
(mas[i] > mas[i - 1] && mas[i] > mas[i + 1]))
k++;
printf("%d", k);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int expo(int a) {
if (a == 0) return 1;
if (a % 2 == 1) {
long long int ans = expo(a - 1);
return ((ans % 998244353) * (2 % 998244353)) % 998244353;
} else {
long long int ans = expo(a / 2);
return ((ans % 998244353) * (ans % 998244353)) % 998244353;
}
}
int main() {
int n, i;
cin >> n;
int a[n];
map<int, int> m;
m.clear();
for (i = 0; i < n; i++) {
cin >> a[i];
m[a[i]] = i;
}
int ans = 0;
int f = 0;
for (i = 0; i < n; i++) {
if (m[a[i]] > f) {
f = m[a[i]];
} else if (m[a[i]] == f && i == f) {
ans++;
f = i + 1;
}
}
cout << expo(ans - 1);
return 0;
}
| 11 | CPP |
t = int(input())
for _ in range(t):
solarr=[]
a, b, c = map(int,input().split())
if a >= c:
solarr.append(-1)
if c/b<a:
solarr.append(b)
else:
solarr.append(b*(a//c+1))
elif a < c:
solarr.append(1)
if c/b >= a:
solarr.append(-1)
else:
solarr.append(b)
print(*solarr) | 7 | PYTHON3 |
// A - Diagonal String
#include <bits/stdc++.h>
using namespace std;
int main(){
string a,b,c; cin>>a>>b>>c;
cout<< string{a[0],b[1],c[2]} <<endl;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void printv(vector<T> v) {
for (auto e : v) {
cout << e << " ";
}
cout << "\n";
}
template <typename T>
void printvv(vector<T> vv) {
for (long long i = 0; i < vv.size(); i++) {
cout << i << ": ";
for (auto e : vv[i]) {
cout << e << " ";
}
cout << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long t;
cin >> t;
while (t--) {
long long l, r;
cin >> l >> r;
if (r < 2 * l) {
cout << "YES"
<< "\n";
} else {
cout << "NO"
<< "\n";
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
ifstream f("test.in");
ofstream g("test.out");
long long x, k, b, n, t, nr;
int main() {
cin >> k >> b >> n >> t;
x = 1;
while (x <= t) {
nr++;
x = x * k + b;
}
cout << ((n - nr + 1 > 0) ? n - nr + 1 : 0);
return 0;
}
| 9 | CPP |
from collections import deque
def dfs(path):
global visited, A
q = deque([])
q.append(path)
for d in A[1]:
q.append(d)
while q:
p = q.pop()
for d in A[p]:
if visited[d] == True:
pass
else:
visited[d] = True
if __name__ == '__main__':
N, M = map(int, input().split())
A = [[] for _ in range(N+1)]
for _ in range(M):
a, b = map(int, input().split())
A[a].append(b)
A[b].append(a)
visited = [False] * (N+1)
dfs(1)
if visited[N]:
print("POSSIBLE")
else:
print("IMPOSSIBLE")
| 0 | PYTHON3 |
count=0
temp=0
for _ in range(int(input())):
x,y=map(int,input().split())
if(x>y):
count+=1
elif(x<y):
temp+=1
if(count>temp):
print("Mishka")
elif(count==temp):
print("Friendship is magic!^^")
else:
print("Chris") | 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
char x,y;cin>>x>>y;
cout<<(x<y?'<':x>y?'>':'=');
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 5e3 + 10;
const int maxl = 12;
const int maxs = 1 << maxl;
bool vis[maxn][maxs];
int n, k;
char s[maxn];
int main() {
scanf("%s", s);
n = strlen(s);
k = 0;
while ((1 << k) <= n) ++k;
k -= 1;
vis[0][(1 << k) - 1] = true;
vector<pair<int, int> > q;
q.push_back(make_pair(0, (1 << k) - 1));
for (;;) {
int sz = q.size();
char nxt = 'z' + 1;
for (int i = 0; i < q.size(); ++i) {
int cur = q[i].first;
int mask = q[i].second;
nxt = min(nxt, s[cur]);
for (int i = 0; i < k; ++i)
if ((mask >> i) & 1) {
int nxt = cur + (1 << i);
int nxtm = mask ^ (1 << i);
if (nxt <= n && !vis[nxt][nxtm]) {
vis[nxt][nxtm] = true;
q.push_back(make_pair(nxt, nxtm));
}
}
}
if (!nxt) break;
vector<pair<int, int> > nq;
for (auto e : q) {
if (s[e.first] == nxt) {
nq.push_back(make_pair(e.first + 1, e.second));
vis[e.first + 1][e.second] = true;
}
}
swap(q, nq);
putchar(nxt);
}
putchar('\n');
}
| 12 | CPP |
x = [int(y) for y in input().split()]
if x[0] == x[1]:
print("0 6 0")
else:
q = 0
r = 0
s = 0
for n in range(1,7):
if abs(n-x[0]) < abs(n-x[1]):
q += 1
elif abs(n-x[0]) == abs(n-x[1]):
r+=1
else:
s+=1
pr = str(q) + " " + str(r) + " " + str(s)
print(pr) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main (){
long long int n;
cin>>n;
vector<int> v(n);
long long int i,k=0;
for(i=0;i<n;i++){
cin>>v[i];
k+=v[i];
}
long long int m =2020202020,l=0;
for(i=0;i<n;i++){
l+=v[i];
k-=v[i];
m = min(abs(k-l),m);
}
cout<<m;
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, t, t1, t2, diferentes = 0, iguales = 0, i, j;
string cad1, cad2, ans = "";
cin >> n >> t;
cin >> cad1 >> cad2;
vector<int> dif(n, 0);
for (i = 0; i < n; i++) {
if (cad1[i] == cad2[i])
iguales++;
else {
diferentes++;
dif[i] = 1;
}
}
if (diferentes > 2 * t)
cout << -1 << '\n';
else {
ans = "";
for (i = 0; i < n; i++) {
ans.push_back(cad1[i]);
}
if (diferentes <= t) {
for (i = 0; i < n; i++) {
if (dif[i] == 1) {
t--;
for (j = 0; j < 3; j++) {
if (cad1[i] != 'a' + j && cad2[i] != 'a' + j) {
ans[i] = 'a' + j;
break;
}
}
}
}
for (i = 0; i < n; i++) {
if (dif[i] == 0 && t > 0) {
t--;
for (j = 0; j < 2; j++) {
if (cad1[i] != 'a' + j && cad2[i] != 'a' + j) {
ans[i] = 'a' + j;
break;
}
}
}
}
} else {
t1 = diferentes - t;
t2 = t1;
t = t - t1;
for (i = 0; i < n; i++) {
if (dif[i] == 1 && t1 > 0) {
dif[i] = 2;
t1--;
ans[i] = cad2[i];
}
}
for (i = 0; i < n; i++) {
if (dif[i] == 1 && t2 > 0) {
dif[i] = 2;
t2--;
ans[i] = cad1[i];
}
}
for (i = 0; i < n; i++) {
if (dif[i] == 1 && t > 0) {
dif[i] = 2;
t--;
for (j = 0; j < 3; j++) {
if (cad1[i] != 'a' + j && cad2[i] != 'a' + j) {
ans[i] = 'a' + j;
break;
}
}
}
}
}
cout << ans << '\n';
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1000000007;
long long int inverse(long long int i) {
if (i == 1) return 1;
return (MOD - ((MOD / i) * inverse(MOD % i)) % MOD + MOD) % MOD;
}
string sum1(string d1, string d2) {
int s, c = 0;
int n1 = d1.size(), n2 = d2.size();
int pos = 1;
string res;
while (pos <= max(n1, n2)) {
int sum1 = 0, carry1 = 0;
if (n1 - pos > -1) sum1 += (d1[n1 - pos] - '0');
if (n2 - pos > -1) sum1 += (d2[n2 - pos] - '0');
sum1 += c;
res.push_back(sum1 % 10 + '0');
c = sum1 / 10;
pos++;
}
if (c != 0) res.push_back(c + '0');
string res2;
for (int i = res.size() - 1; i > -1; --i) res2.push_back(res[i]);
return res2;
}
int main() {
int l;
cin >> l;
string s1;
cin >> s1;
int min1 = l;
int ans1, ans2 = -1;
for (int i = 1; i < l; ++i) {
if (s1[i] != '0') {
if (max(i, l - i) < min1) {
ans1 = i;
min1 = max(i, l - i);
}
}
}
for (int i = 1; i < l; ++i) {
if (s1[i] != '0') {
if (max(i, l - i) == min1 && i != ans1) {
ans2 = i;
break;
}
}
}
string d1, d2;
for (int i = 0; i < ans1; ++i) d1.push_back(s1[i]);
for (int i = ans1; i < l; ++i) d2.push_back(s1[i]);
string s4 = sum1(d1, d2);
string s5;
if (ans2 != -1) {
d1.clear();
d2.clear();
for (int i = 0; i < ans2; ++i) d1.push_back(s1[i]);
for (int i = ans2; i < l; ++i) d2.push_back(s1[i]);
s5 = sum1(d1, d2);
}
if (ans2 == -1)
cout << s4;
else {
if (s4 < s5)
cout << s4;
else
cout << s5;
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7ll;
const int N = (int)2e5 + 7;
vector<int> adj[N];
int visit[N];
map<int, int> mp;
int flag = 1, cntn = 0;
void dfs(int nd) {
if (mp[nd] != 2) flag = 0;
visit[nd] = 1;
cntn++;
for (int it : adj[nd]) {
if (!visit[it]) dfs(it);
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
i += 1 - 2 * ((0) > (m))) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
mp[a]++;
mp[b]++;
}
int ans = 0;
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n))) {
if (!visit[i]) {
dfs(i);
if (cntn > 2) ans += flag;
flag = 1;
}
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned n;
cin >> n;
unsigned c[5] = {0};
while (n--) {
unsigned current;
cin >> current;
++c[current];
}
unsigned c1 = c[1];
unsigned c2 = c[2];
unsigned c3 = c[3];
unsigned c4 = c[4];
unsigned sum = c4 + c3;
c1 -= min(c1, c3);
sum += c2 / 2;
if (c2 % 2 == 1) {
++sum;
if (c1 >= 2)
c1 -= 2;
else
c1 = 0;
}
sum += c1 / 4 + !!(c1 % 4);
cout << sum << '\n';
return 0;
}
| 8 | CPP |
t=int(input())
for i in range(t):
n,x,m=map(int,input().split())
ll=x
rr=x
for j in range(m):
l,r=map(int,input().split())
if l<=ll<=r or l<=rr<=r:
ll=min(l,ll)
rr=max(r,rr)
print(rr-ll+1)
"""
3
6 4 3
1 6
2 3
5 5
4 1 2
2 4
1 2
3 3 2
2 3
1 2
----------
6
2
3
"""
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime[1200000];
int cntPrime[1200000];
int cntHuiwen[1200000];
FILE *out1 = fopen("out1.txt", "w");
FILE *out2 = fopen("out2.txt", "w");
bool ishuiwen(int n) {
int t = 0;
int a = n;
while (n) {
t = t * 10 + n % 10;
n /= 10;
}
return t == a;
}
void init() {
int i, j;
memset(isPrime, true, sizeof(isPrime));
isPrime[0] = isPrime[1] = false;
for (int i = 2; i < 1200000; i++) {
if (isPrime[i]) {
for (int j = 2; i * j < 1200000; j++) {
isPrime[i * j] = false;
}
}
}
cntPrime[0] = 0;
for (int i = 1; i < 1200000; i++) {
cntPrime[i] = cntPrime[i - 1];
if (isPrime[i]) cntPrime[i]++;
}
}
void init2() {
cntHuiwen[0] = 0;
for (int i = 1; i < 1200000; i++) {
cntHuiwen[i] = cntHuiwen[i - 1];
if (ishuiwen(i)) cntHuiwen[i]++;
}
}
int main() {
init();
init2();
int q, p;
while (cin >> p >> q) {
int s = 1;
int e = 1200000 - 1;
while (s <= e) {
int m = (s + e) / 2;
if (cntPrime[m] * q > cntHuiwen[m] * p)
e = m - 1;
else
s = m + 1;
}
s = s - 1;
int ans = s;
int len = 20;
for (int i = 1; i <= 8888; i++) {
if (cntPrime[s + i] * q <= cntHuiwen[s + i] * p) ans = s + i;
}
cout << ans << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 6;
vector<pair<int, int> > white;
int n, m;
string s[N];
void Ok(int x, int y, int d) {
if (x + d >= n || y + d >= m) return;
for (auto a : white) {
if (a.first != x && a.first != (x + d) && a.second != y &&
a.second != (y + d))
return;
}
for (int i = 0; i <= d; i++) {
if (s[x + i][y] == '.') s[x + i][y] = '+';
if (s[x][y + i] == '.') s[x][y + i] = '+';
if (s[x + d][i + y] == '.') s[x + d][y + i] = '+';
if (s[x + i][d + y] == '.') s[x + i][y + d] = '+';
}
for (int i = 0; i < n; i++) cout << s[i] << "\n";
exit(0);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
int mnX = n + 2, mxX = 0, mnY = m + 2, mxY = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == 'w') {
white.push_back(make_pair(i, j));
mnX = min(mnX, i);
mxX = max(mxX, i);
mnY = min(mnY, j);
mxY = max(mxY, j);
}
}
}
int d = max(mxX - mnX, mxY - mnY);
Ok(mnX, mnY, d);
Ok(mnX, max(0, mxY - d), d);
Ok(max(0, mxX - d), mnY, d);
cout << "-1\n";
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, t, tp, tu, td;
int v[305][305], s[305][305][4];
int add(int x, int y) {
if (x > y) return tu;
if (x < y) return td;
return tp;
}
int main() {
cin >> n >> m >> t >> tp >> tu >> td;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> v[i][j];
}
}
for (int j = 1; j < m; ++j) {
for (int i = 0; i < n; ++i) {
s[i][j][0] = s[i][j - 1][0] + add(v[i][j], v[i][j - 1]);
}
}
for (int j = 1; j < m; ++j) {
for (int i = 0; i < n; ++i) {
s[i][j][1] = s[i][j - 1][1] + add(v[i][j - 1], v[i][j]);
}
}
for (int j = 0; j < m; ++j) {
for (int i = 1; i < n; ++i) {
s[i][j][2] = s[i - 1][j][2] + add(v[i][j], v[i - 1][j]);
}
}
for (int j = 0; j < m; ++j) {
for (int i = 1; i < n; ++i) {
s[i][j][3] = s[i - 1][j][3] + add(v[i - 1][j], v[i][j]);
}
}
int anst = INT_MAX, sum;
int a0, a1, a2, a3;
for (int x1 = 0; x1 < n - 2; ++x1) {
for (int y1 = 0; y1 < m - 2; ++y1) {
for (int x2 = x1 + 2; x2 < n; ++x2) {
for (int y2 = y1 + 2; y2 < m; ++y2) {
sum = s[x1][y2][0] - s[x1][y1][0] + s[x2][y2][1] - s[x2][y1][1] +
s[x2][y2][2] - s[x1][y2][2] + s[x2][y1][3] - s[x1][y1][3];
if (sum == t) {
printf("%d %d %d %d", x1 + 1, y1 + 1, x2 + 1, y2 + 1);
return 0;
}
int temp = abs(sum - t);
if (temp < anst) {
anst = temp;
a0 = x1 + 1;
a1 = y1 + 1;
a2 = x2 + 1;
a3 = y2 + 1;
}
}
}
}
}
printf("%d %d %d %d", a0, a1, a2, a3);
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, c0, c1;
long long C(int x, int y) {
if (x < y || y < 0) return 0;
long double res = 1;
if (x - y < y) y = x - y;
for (int i = 1; i <= y; ++i) {
if (res >= 1e11 / (long double)(x - i + 1)) return 1e11;
res /= i, res *= (x - i + 1);
}
return (long long)(res + 0.5);
}
long long Cr(int x, int y) {
if (x < y || y < 0) return 0;
long long res = 1;
if (x - y < y) y = x - y;
for (int i = 1; i <= y; ++i) res = res * (x - i + 1) / i;
return res;
}
long long check(long long x) {
long long cnt = 0, lim;
for (int i = 0; i <= 30; ++i) {
if (1ll * c1 * i > x) break;
lim = (x - 1ll * c1 * i) / c0, cnt += C(i + lim + 1, i + 1);
if (cnt >= 1e11) return cnt;
}
return cnt;
}
int main() {
cin >> n >> c0 >> c1;
if (c0 > c1) swap(c0, c1);
--n;
if (!n) puts("0"), exit(0);
if (c0 == 0) {
cout << 1ll * n * c1 << endl;
return 0;
}
long long L = 0, R = 30ll * c1;
while (L < R - 1) {
long long mid = (L + R) >> 1;
(check(mid) >= n) ? R = mid : L = mid;
}
long long cnt = 0, ans = 0;
for (int i = 0; i <= 30; ++i) {
if (1ll * c1 * i > R - 1) break;
long long lim = (R - 1 - 1ll * c1 * i) / c0, tmp = Cr(lim + i + 1, i + 1);
cnt += tmp, ans += tmp * i * c1 + Cr(lim + i + 1, i + 2) * (i + 1) * c0;
}
cout << ans + (n - cnt) * R + n * (c0 + c1);
return 0;
}
| 8 | CPP |
# import sys
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w+')
for _ in range (0, int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pf,nf = 0,0
ans = 1
l = ["NO","YES"]
for i in range (0,n):
diff = b[i]-a[i]
if (diff>0 and pf==1) or (diff<0 and nf==1) or (diff==0):
ans = 1
else:
ans = 0
break
if a[i]==-1:
nf = 1
if a[i]==1:
pf = 1
print (l[ans]) | 8 | PYTHON3 |
n = int(input())
w = str(input().strip())
ones = w.count('1')
zeros = w.count('0')
print(ones+zeros-2*min(ones,zeros))
| 7 | PYTHON3 |
n=int(input())
l=[]
for i in range(n):
l.append(input())
for j in l:
if(j[-2:]=='po'):
print('FILIPINO')
elif(j[-4:]=='desu'or j[-4:]=='masu'):
print('JAPANESE')
elif(j[-5:]=='mnida'):
print('KOREAN') | 7 | PYTHON3 |
x = int(input())
print(1-x) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
vector<int> G[maxn];
int dfn[maxn], coc;
stack<int> s;
bool ins[maxn];
int bel[maxn], blo;
int crash[maxn][2];
int tarjan(int u, int f) {
int lowu = dfn[u] = ++coc;
ins[u] = true;
s.push(u);
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == f) continue;
if (!dfn[v]) {
int lowv = tarjan(v, u);
lowu = min(lowv, lowu);
} else if (ins[v])
lowu = min(lowu, dfn[v]);
}
if (lowu == dfn[u]) {
crash[blo][0] = crash[blo][1] = u;
int size = 0;
while (ins[u]) {
int v = s.top();
s.pop();
size++;
ins[v] = false;
bel[v] = blo;
crash[blo][0] = min(crash[blo][0], v);
crash[blo][1] = max(crash[blo][1], v);
}
if (size != 1) blo++;
}
return lowu;
}
set<int> lp;
vector<int> rp[maxn];
int maxr[maxn];
pair<int, int> query[maxn];
int id[maxn];
long long ans[maxn];
bool cmp(int x, int y) { return (query[x] > query[y]); }
namespace Seg {
long long sum[maxn << 2], all[maxn << 2];
void updata(int p, int l, int r, int lq, int rq, long long v) {
if (l == lq && r == rq) return void(all[p] += v);
sum[p] += (rq - lq + 1) * v;
int mid = l + r >> 1;
if (rq <= mid)
updata((p << 1), l, mid, lq, rq, v);
else if (lq > mid)
updata((p << 1 | 1), mid + 1, r, lq, rq, v);
else
updata((p << 1), l, mid, lq, mid, v),
updata((p << 1 | 1), mid + 1, r, mid + 1, rq, v);
}
long long query(int p, int l, int r, int lq, int rq) {
long long rtn = all[p] * (rq - lq + 1);
if (l == lq && r == rq) return rtn + sum[p];
int mid = l + r >> 1;
if (rq <= mid)
return query((p << 1), l, mid, lq, rq) + rtn;
else if (lq > mid)
return query((p << 1 | 1), mid + 1, r, lq, rq) + rtn;
else
return query((p << 1), l, mid, lq, mid) + rtn +
query((p << 1 | 1), mid + 1, r, mid + 1, rq);
}
} // namespace Seg
int main() {
int n, m;
scanf("%d%d", &n, &m);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0);
for (int i = 0; i < blo; i++) {
rp[crash[i][0]].push_back(crash[i][1]);
lp.insert(crash[i][1]);
}
for (int i = 1; i <= n; i++) {
maxr[i] = lp.size() ? *lp.begin() - 1 : n;
for (int j = 0; j < rp[i].size(); j++) lp.erase(rp[i][j]);
}
int q;
scanf("%d", &q);
for (int i = 1; i <= q; i++)
scanf("%d%d", &query[i].first, &query[i].second), id[i] = i;
sort(id + 1, id + 1 + q, cmp);
int minl = n + 1;
for (int i = 1; i <= q; i++) {
while (minl > query[id[i]].first)
Seg::updata(1, 1, n, minl - 1, maxr[minl - 1], 1), --minl;
ans[id[i]] = Seg::query(1, 1, n, query[id[i]].first, query[id[i]].second);
}
for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}
| 11 | CPP |
(a, b) = map(int, input().split())
print(a * b // 2) | 7 | PYTHON3 |
for i in range(int(input())):
s=input()
if len(s)>10:
print(s[0],end="")
print(len(s)-2,end="")
print(s[len(s)-1])
else:
print(s) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, dp[4005][4005], dps[4005], comb[4005][4005];
int main() {
scanf("%d", &n);
dp[0][0] = 1;
dps[0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++) {
dp[i][j] = (dp[i - 1][j - 1] + 1LL * j * dp[i - 1][j]) % 1000000007;
dps[i] += dp[i][j];
while (dps[i] >= 1000000007) dps[i] -= 1000000007;
}
comb[0][0] = 1;
for (int i = 1; i <= n; i++) {
comb[i][0] = 1;
for (int j = 1; j <= i; j++) {
comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
while (comb[i][j] >= 1000000007) comb[i][j] -= 1000000007;
}
}
int sol = 0;
for (int i = 0; i < n; i++)
sol = (sol + 1LL * comb[n][n - i] * dps[i]) % 1000000007;
printf("%d\n", sol);
return 0;
}
| 8 | CPP |
for _ in range(int(input())):
l, r, m = map(int, input().split())
if l == r == m:
print(l, r, m)
continue
for a in range(l, r + 1):
x = m % a
y = a - x
if m > a:
if x <= r - l:
print(a, r, r - x)
break
elif y <= r - l:
print(a, r - y, r)
break
if m < a:
print(a, l, l + y)
break
| 8 | PYTHON3 |
import sys
import math
input = sys.stdin.readline
T = int(input())
for i in range (0,T):
n = int(input())
string = input()
leng = len(string)
st = en = -1
for i in range (0,leng):
if string[i]=='1':
if st == -1:
st = en = i
else :
en = i
if st == -1:
print(n)
else:
shift = st
if en != -1:
shift = min(n-1-en,shift)
print(max(n,(n-shift)*2)) | 8 | PYTHON3 |
from collections import*
input()
a=[*map(int,input().split())]
print(max(Counter(x+y for x in a for y in a if x!=y).values())//2) | 8 | PYTHON3 |
import sys
t=int(sys.stdin.readline())
for _ in range(t):
ans=0
n,m=map(int,input().split())
if (n==1):
ans=0
elif(n==2):
ans=m
else:
ans=2*m
print(ans)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
inline long long read() {
long long x = 0;
char c = getchar(), f = 1;
for (; c < '0' || '9' < c; c = getchar())
if (c == '-') f = -1;
for (; '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * f;
}
inline void write(long long x) {
static char buf[20];
int len = 0;
if (x < 0) putchar('-'), x = -x;
for (; x; x /= 10) buf[len++] = x % 10 + '0';
if (!len)
putchar('0');
else
while (len) putchar(buf[--len]);
}
inline void writesp(long long x) {
write(x);
putchar(' ');
}
inline void writeln(long long x) {
write(x);
putchar('\n');
}
struct edge {
int to, nxt, d;
} e[2 * 200010];
int fir[200010], mark[200010], dist[200010], from[200010], size[200010];
double f[200010];
int w[200010];
int n, tot;
void add_edge(int x, int y, int z) {
e[tot].to = y;
e[tot].d = z;
e[tot].nxt = fir[x];
fir[x] = tot++;
}
void dfs(int now, int fa) {
for (int i = fir[now]; ~i; i = e[i].nxt)
if (e[i].to != fa) {
dist[e[i].to] = dist[now] + e[i].d;
from[e[i].to] = from[now];
dfs(e[i].to, now);
}
}
void prepare(int now, int fa) {
size[now] = 1;
for (int i = fir[now]; ~i; i = e[i].nxt)
if (e[i].to != fa && !mark[e[i].to]) {
prepare(e[i].to, now);
size[now] += size[e[i].to];
}
}
int find_root(int now, int fa, int S) {
int mx = 0;
for (int i = fir[now]; ~i; i = e[i].nxt)
if (e[i].to != fa && !mark[e[i].to]) {
int tmp = find_root(e[i].to, now, S);
if (tmp) return tmp;
mx = std::max(mx, size[e[i].to]);
}
if (std::max(mx, S - size[now]) <= S / 2)
return now;
else
return 0;
}
struct Data {
double val;
int id;
friend bool operator<(Data a, Data b) { return a.val < b.val; }
};
Data solve(int now) {
mark[now] = 1;
dist[now] = 0;
for (int i = fir[now]; ~i; i = e[i].nxt) {
dist[e[i].to] = e[i].d;
from[e[i].to] = e[i].to;
f[e[i].to] = 0;
dfs(e[i].to, now);
}
Data ret = (Data){0, now};
for (int i = 1; i <= n; i++) {
f[from[i]] += 1.5 * sqrt(dist[i]) * w[i];
ret.val += dist[i] * sqrt(dist[i]) * w[i];
}
double sum = 0;
for (int i = fir[now]; ~i; i = e[i].nxt) sum += f[e[i].to];
Data mn = (Data){1e36, 0};
for (int i = fir[now]; ~i; i = e[i].nxt) {
Data tmp = (Data){sum - 2 * f[e[i].to], e[i].to};
if (tmp < mn) mn = tmp;
}
if (!mark[mn.id]) {
prepare(mn.id, now);
Data tmp = solve(find_root(mn.id, now, size[mn.id]));
if (tmp < ret) ret = tmp;
}
return ret;
}
int main() {
n = read();
for (int i = 1; i <= n; i++) w[i] = read();
memset(fir, 255, sizeof(fir));
tot = 0;
for (int i = 1; i < n; i++) {
int x = read(), y = read(), z = read();
add_edge(x, y, z);
add_edge(y, x, z);
}
Data ans = solve(1);
printf("%d %.10lf\n", ans.id, ans.val);
return 0;
}
| 9 | CPP |
N = int(input())
Xmi, Xma, Ymi, Yma, DYmi, DYma, UYmi, UYma, RXmi, RXma, LXmi, LXma = [(10**10), -(10**10)] * 6
for _ in range(N):
x, y, d = input().split()
x, y = int(x), int(y)
if d == "R":
RXmi = min(RXmi, x)
RXma = max(RXma, x)
elif d == "L":
LXmi = min(LXmi, x)
LXma = max(LXma, x)
else:
Xmi = min(Xmi, x)
Xma = max(Xma, x)
if d == "U":
UYmi = min(UYmi, y)
UYma = max(UYma, y)
elif d == "D":
DYmi = min(DYmi, y)
DYma = max(DYma, y)
else:
Ymi = min(Ymi, y)
Yma = max(Yma, y)
def calc(t):
xmi = min(RXmi+t, LXmi-t, Xmi)
xma = max(RXma+t, LXma-t, Xma)
ymi = min(UYmi+t, DYmi-t, Ymi)
yma = max(UYma+t, DYma-t, Yma)
return max(yma-ymi, 0)*max(xma-xmi, 0)
def di(t):
a = 1
return calc(t+a) - calc(t-a)
def dii(t):
a = 0.0000001
return calc(t+a) - calc(t-a)
l = 0
r = 10**10
for _ in range(100):
m = (l+r)//2
if di(m) > 0:
r = m
else:
l = m
for _ in range(100):
m = (l+r)/2
if dii(m) > 0:
r = m
else:
l = m
print(calc(m))
| 0 | PYTHON3 |
#C - こだわり者いろはちゃん 2
def checkDigit(n):
d = []
for i in range(9):
if n%10 in D: return False
n //= 10
if n==0: return True
def solve(n):
for i in range(N,1000000):
if checkDigit(i): return i
N, K = [int(x) for x in input().split()]
D = [int(x) for x in input().split()]
#print(N,K,D)
print(solve(N))
| 0 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int d[100001],a[100001],sum[100001];
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
d[i]=c-'0';
}
for(int i=1;i<=n;i++)
{
a[i]=d[i-1]^d[i];
}
sum[0]=0;
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1]+a[i];
}
int b=0;
int maxn=0;
for(int i=1;i<=n;i++)
{
b^=a[i];
int x=upper_bound(sum,sum+n+1,2*k-(!b)+sum[i])-sum;
maxn=max(maxn,x-i);
}
cout<<maxn<<endl;
return 0;
} | 0 | CPP |
def get_sieve_of_eratosthenes_new(n):
import math
if not isinstance(n, int):
raise TypeError('n is int type.')
if n < 2:
raise ValueError('n is more than 2')
prime = []
limit = math.sqrt(n)
data = [i + 1 for i in range(1, n)]
while True:
p = data[0]
if limit <= p:
return prime + data
prime.append(p)
data = [e for e in data if e % p != 0]
prime=get_sieve_of_eratosthenes_new(2160)
def ind(b,n):
res=0
while n%b==0:
res+=1
n//=b
return res
import sys
input=sys.stdin.readline
N=int(input())
dic={}
inverse={}
for i in range(N):
s=int(input())
news=1
invs=1
for p in prime:
a=ind(p,s)
s//=p**a
news*=p**(a%3)
invs*=p**((-a)%3)
if s==1 or int(s**0.5)**2!=s:
news*=s
invs*=s**2
if news not in dic:
dic[news]=0
inverse[news]=0
dic[news]+=1
inverse[news]=invs
else:
news*=s
invs*=int(s**0.5)
if news not in dic:
dic[news]=0
inverse[news]=0
dic[news]+=1
inverse[news]=invs
one=0
double=0
for i in dic:
if i!=1:
if inverse[i] in dic:
double+=max(dic[i],dic[inverse[i]])
else:
one+=dic[i]
double//=2
ans=one+double
if 1 in dic:
ans+=1
print(ans) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, h, i, j, k;
cin >> n >> h;
int a[n + 1];
for (i = 0; i < n; i++) cin >> a[i];
for (i = 1; i < n; i++) {
sort(a, a + i + 1);
int s = 0;
for (j = i; j >= 0; j -= 2) {
s += a[j];
}
if (s > h) {
cout << i << endl;
return 0;
}
}
cout << n << endl;
}
| 8 | CPP |
na,nb = map(int,input().split())
k,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a = a[0:k]
b = b[len(b)-m:len(b)]
if b[0] > a[len(a)-1]:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, a, b;
scanf("%d%d%d", &k, &a, &b);
int ans = 0;
ans += a / k;
ans += b / k;
if ((a / k == 0 && b / k == 0) || (a / k != 0 && a % k != 0 && b / k == 0) ||
(b / k != 0 && b % k != 0 && a / k == 0)) {
printf("-1\n");
} else
printf("%d\n", ans);
return 0;
}
| 9 | CPP |
a="+".join(sorted(input().split("+")))
print (a) | 7 | PYTHON3 |
(n, L, a) = map(int, input().split())
customers = [list(map(int, input().split())) for i in range(n)]
breaks_cnt = 0
customers.insert(0, (0, 0))
customers.insert(len(customers), (L, 0))
for i in range(len(customers)-1):
finished = customers[i][0] + customers[i][1]
free_time = customers[i + 1][0] - finished
breaks_cnt += free_time // a
print(int(breaks_cnt))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, k, h;
long long m[100002];
long long v[100002];
vector<pair<long long, pair<long long, long long> > > vp;
vector<long long> ret;
vector<long long> working;
int ok(double m) {
vector<long long> temp;
int l = 1;
for (int i = 0; i < n; i++) {
if (vp[i].second.first * m > l * h * (1.0 - 1E-9)) {
temp.push_back(vp[i].second.second);
l++;
}
if (l == k + 1) {
working = temp;
return true;
}
}
return false;
}
int main() {
cout << setprecision(9);
cin >> n >> k >> h;
for (int i = 0; i < n; i++) cin >> m[i];
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n; i++) vp.push_back(make_pair(m[i], make_pair(v[i], i)));
sort(vp.begin(), vp.end());
double l = 0;
double r = 1E9;
for (int i = 0; i < 200; i++) {
double m = (l + r) / 2;
if (ok(m)) {
r = m;
} else {
l = m;
}
}
for (int i = 0; i < k; i++) cout << 1 + working[i] << " ";
cout << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
const long long N = 1e4 + 5;
const long long demo = 10;
using namespace std;
const long long cfmod = 998244353;
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long dx[4] = {0, 0, -1, 1};
long long dy[4] = {-1, 1, 0, 0};
char button[4] = {'L', 'R', 'U', 'D'};
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q = 1;
cin >> q;
while (q--) {
long long n;
cin >> n;
if (n % 2) {
set<long long> s;
pair<long long, long long> ans;
ans = {1, n - 1};
for (long long i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
long long x = n / i;
if (lcm(i, n - i) < lcm(ans.first, ans.second)) {
ans = {i, n - i};
}
if (lcm(x, n - x) < lcm(ans.first, ans.second)) {
ans = {x, n - x};
}
}
}
cout << ans.first << " " << ans.second << "\n";
} else {
cout << n / 2 << " " << n / 2 << "\n";
}
}
}
| 8 | CPP |
#include<bits/stdc++.h>
#define N 100005
#define M 105
using namespace std;
int n,m,c,a[M];
void GG(){ printf("Impossible"); exit(0);}
int main()
{
int i;
scanf("%d %d",&n,&m);
for(i=1;i<=m;i++)
scanf("%d",&a[i]),c+=(a[i]&1);
if(n==1){ printf("1\n1\n1"); return 0;}
if(m==1){
printf("%d\n2\n%d %d",n,1,n-1);
return 0;
}
if(n%2==1&&c>1) GG();
if(n%2==0&&c>2) GG();
sort(a+1,a+m+1);
if(n%2==0&&c==2){
for(i=1;i<=n;i++) if(a[i]&1) break;
while(i>1) swap(a[i],a[i-1]),i--;
for(i=2;i<=n;i++) if(a[i]&1) break;
while(i<m) swap(a[i],a[i+1]),i++;
}
if(n&1){
for(i=1;i<=n;i++) if(a[i]&1) break;
while(i>1) swap(a[i],a[i-1]),i--;
}
for(i=1;i<=m;i++) printf("%d ",a[i]);
a[1]--,a[m]++;
printf("\n%d\n",m-(a[1]==0));
for(i=1;i<=m;i++)
if(a[i]) printf("%d ",a[i]);
return 0;
}
| 0 | CPP |
n=str(input())
for i in range(len(n)-1,-1,-1):
if n[i]!="0":
break
k=i+1
f=0
for i in range(k//2):
if n[i]!=n[k-1-i]:
f=1
break
if f==1:
print("NO")
else:
print("YES") | 7 | PYTHON3 |
if __name__ == '__main__':
n = int(input().strip())
lst = list(map(int,input().strip().split()))
new_lst = sorted(lst)
# if n % 2 != 0:
# giua = new_lst.index(new_lst[n//2])
# print(new_lst[giua::])
# print(new_lst[:giua])
# sum1 = sum(new_lst[giua::])
# sum2 = sum(new_lst[:giua])
# print(sum1*sum1 + sum2*sum2)
# else:
sum1 = sum(new_lst[n//2::])
sum2 = sum(new_lst[:n//2])
print(sum1 * sum1 + sum2 * sum2) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int x;
int pos1 = 0, pos2;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x == 0) {
pos2 = i;
for (int j = pos1 + 1; j <= pos2; j++) {
if (pos1 != 0) {
if (abs(pos1 - j) < abs(pos2 - j))
cout << abs(pos1 - j) << ' ';
else
cout << abs(pos2 - j) << ' ';
} else
cout << pos2 - j << ' ';
}
pos1 = i;
}
}
for (int i = pos2 + 1; i <= n; i++) cout << i - pos2 << ' ';
return 0;
}
| 8 | CPP |
#include "bits/stdc++.h"
using namespace std;
int main(void){
int a,b,c,d;
cin >> a >> b >> c>> d;
cout << max(0,min(b,d)-max(a,c));
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
if (n == 1)
cout << "-1" << endl;
else if (n == 3)
cout << "2 7 42" << endl;
else
cout << n << " " << (n + 1) << " " << (n * (n + 1)) << endl;
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
string str[n];
for (auto &i : str) cin >> i;
map<char, int> cnt;
for (auto i : str) {
for (auto j : i) cnt[j]++;
}
bool ok = 1;
for (auto i : cnt) ok &= (i.second % n == 0);
cout << (ok ? "YES" : "NO") << endl;
}
return 0;
}
| 7 | CPP |
t = int(input())
for _ in range(t):
n = int(input())
p = list(map(int, input().split()))
ans = []
i = 2 * n - 1
while i >= 0:
if p[i] == 2 * n:
ans.append(2*n-i)
break
i -= 1
i -= 1
while i >= 0:
ind = i - p.index(max(p[:i+1])) + 1
ans.append(ind)
i -= ind
ans_len = len(ans)
dp = [[False] * (n+1) for i in range(ans_len+1)]
for i in range(ans_len+1):
dp[i][0] = True
for i in range(n+1):
dp[0][i] = False
for i in range(1, ans_len + 1):
for j in range(1, n+1):
if ans[i-1] > j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = (dp[i-1][j] or dp[i-1][j-ans[i-1]])
if dp[-1][-1]:
print("YES")
else:
print("NO") | 8 | PYTHON3 |
import re
s=input().strip()
r1=re.match("(.*)h(.*)e(.*)l(.*)l(.*)o(.*)",s)
if r1:
print('YES')
else:
print('NO') | 7 | PYTHON3 |
n,c = map(int,input().split())
s = input()
ans = 0;first = n-1;last = 0
for i in range(n//2):
if s[i] != s[n-i-1]:
first = min(first,i)
last = max(last,i)
a = ord(s[i])-97;b = ord(s[n-i-1])-97
change = min(a,b) + 26 - max(a,b)
#print(change,s[i])
ans += min(abs(b-a),change)
#print(first,last,ans)
if c == n//2:
ans += n//2 - first
elif c < n//2:
c-=1
a = c-min(c,first)
b = max(c,last)-c
#print(a,b)
ans += a+b+min(a,b)
else:
c = n-c
a = c-min(c,first)
b = max(c,last)-c
#print(a,b)
ans += a+b+min(a,b)
print(max(0,ans))
| 9 | PYTHON3 |
t=int(input())
for _ in range(t):
n=int(input())
s=input()
t=list()
m=list()
c=1
for i in range(len(s)):
if(s[i]=='T'):
t.append(i)
else:
m.append(i)
if(len(t)!=2*len(m)):
c=0
else:
for i in range(len(m)):
if(m[i]<t[i] or m[i]>t[i+len(m)]):
c=0
break
if(c==0):
print('NO')
else:
print('YES')
| 8 | PYTHON3 |
n, m = map(int, input().split())
rows, columns = set(), set()
resp = ""
for i in range(m):
num = n ** 2
row, column = map(int, input().split())
rows.add(row)
columns.add(column)
pre = (len(rows) * n) + (len(columns) * n)
rep = len(rows) * len(columns)
resp += str(num - (pre - rep)) + " "
print(resp)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int prime[5][9] = {{2, 3, 5, 7, 11, 13, 17, 19, 23},
{29, 31, 37, 41, 43},
{47, 53, 59, 61},
{67, 71, 73, 79},
{83, 89, 97}};
const long long val[5] = {223092870, 58642669, 600662303, 27433619, 716539};
int n, m, len, sl;
long long A[10010];
vector<long long> dp[5][17];
void init() {
for (int T = 0; T < 5; T++) {
for (int i = 2; i <= 16; i++) {
int t = 1, prod = 1;
while (t <= n) t *= i, prod *= (i + 1);
dp[T][i].resize(prod, -1);
}
}
}
char s[35];
int b[35];
inline int calc(int d, int p) {
int x = 0;
for (int i = 0; i < len; i++) x = x * (d + 1) + b[i];
if (dp[p][d][x] != -1) return x;
dp[p][d][x] = 1;
for (int i = 0; i < len; i++) {
if (b[i] == d) {
for (int j = 0; j < d; j++) {
b[i] = j;
dp[p][d][x] = dp[p][d][x] * dp[p][d][calc(d, p)] % val[p];
}
b[i] = d;
return x;
}
}
int y = 0;
for (int i = len - 1; i >= 0; i--) y = y * d + b[i];
if (y < n) dp[p][d][x] = A[y] % val[p];
return x;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", &A[i]);
init();
scanf("%d", &m);
while (m--) {
int d;
long long c;
scanf("%d%s%lld", &d, s + 1, &c);
len = 0, sl = strlen(s + 1);
for (int t = 1; t < n; len++, t *= d)
;
for (int i = 0; i < len; i++) {
if (s[sl - i] == '?')
b[i] = d;
else if (s[sl - i] <= '9')
b[i] = s[sl - i] - '0';
else
b[i] = s[sl - i] - 'A' + 10;
}
bool flag = 1;
for (int i = 0; i < 5; i++) {
int p = calc(d, i);
int ans = (dp[i][d][p] + c) % val[i];
for (int j = 0; j < 9; j++) {
if (prime[i][j] && ans % prime[i][j] == 0) {
printf("%d\n", prime[i][j]);
flag = 0;
break;
}
}
if (!flag) break;
}
if (flag) puts("-1");
}
return 0;
}
| 11 | CPP |
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
for i in range(n):
for j in range(i+1,n):
for k in range(j+1,n):
if(l[k]<l[i]+l[j] and l[i]!=l[j] and l[j]!=l[k]):
ans+=1
print(ans) | 0 | PYTHON3 |
n,k=map(int,input().split())
l=list(map(int,input().split()))
a=l[k-1]
c=0
for i in l:
if(i>=a and i!=0):
c+=1
print(c) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000;
int n, D[MAXN][MAXN];
template <int N, int M, class T>
struct LCA {
std::vector<std::pair<int, T> > e[N];
int d[N], p[N][M];
T l[N];
void init(int n) {
for (int i = 0; i < n; ++i) {
e[i].clear();
}
}
void add(int u, int v, T w) {
e[u].push_back(std::make_pair(v, w));
e[v].push_back(std::make_pair(u, w));
}
void dfs(int u, int f) {
p[u][0] = f;
for (int i = 1; i < M; ++i) {
p[u][i] = p[p[u][i - 1]][i - 1];
}
for (int i = 0; i < (int)e[u].size(); ++i) {
int v = e[u][i].first;
if (v != f) {
d[v] = d[u] + 1;
l[v] = l[u] + e[u][i].second;
dfs(v, u);
}
}
}
int up(int u, int m) {
for (int i = 0; i < M; ++i) {
if ((m >> i) & 1) {
u = p[u][i];
}
}
return u;
}
int lca(int u, int v) {
if (d[u] > d[v]) {
std::swap(u, v);
}
v = up(v, d[v] - d[u]);
if (u == v) {
return u;
} else {
for (int i = M - 1; i >= 0; --i) {
if (p[u][i] != p[v][i]) {
u = p[u][i];
v = p[v][i];
}
}
return p[u][0];
}
}
T howfar(int u, int v) {
int w = lca(u, v);
return l[u] + l[v] - 2 * l[w];
}
void build() {
d[0] = l[0] = 0;
dfs(0, 0);
}
};
LCA<MAXN, 11, int> lca;
vector<pair<int, pair<int, int> > > V;
int P[MAXN];
int getp(int x) { return x == P[x] ? x : (P[x] = getp(P[x])); }
bool judge() {
for (int i = 0; i < n; ++i) {
if (D[i][i] != 0) {
return false;
}
for (int j = i + 1; j < n; ++j) {
if (D[i][j] != D[j][i]) {
return false;
}
if (D[i][j] == 0) {
return false;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
V.push_back(make_pair(D[i][j], make_pair(i, j)));
}
}
sort(V.begin(), V.end());
for (int i = 0; i < n; ++i) {
P[i] = i;
}
lca.init(n);
for (int i = 0; i < (int)V.size(); ++i) {
int a = V[i].second.first;
int b = V[i].second.second;
int x = getp(a);
int y = getp(b);
if (x != y) {
P[x] = y;
lca.add(a, b, D[a][b]);
}
}
for (int i = 1; i < n; ++i) {
if (getp(0) != getp(i)) {
return false;
}
}
lca.build();
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (lca.howfar(i, j) != D[i][j]) {
return false;
}
}
}
return true;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%d", &D[i][j]);
}
}
puts(judge() ? "YES" : "NO");
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000 + 5;
int n, dp[maxn * maxn], a[maxn];
int t[maxn], m;
int lowbit(int x) { return x & (-x); }
void add(int x, int d) {
for (; x <= n; x += lowbit(x)) t[x] += d;
}
int ask(int x) {
int res = 0;
while (x) {
res += t[x];
x -= lowbit(x);
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
m += (ask(n) - ask(a[i]));
add(a[i], 1);
}
if (m == 0)
puts("0.000000");
else {
printf("%.6lf", 1.0 * (m & 1 ? ((m / 2) * 4 + 1) : ((m / 2) * 4)));
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t != 0) {
int n = 0;
cin >> n;
vector<int> v;
v.resize(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (v[i] - v[j] == 1 || v[i] - v[j] == -1) count++;
}
}
if (count == 0) {
cout << "1" << endl;
} else
cout << "2" << endl;
t--;
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int d = 0;
int min_diff = a[n - 1] - a[0];
for (int i = 0; i < n - 1; i++) {
d = max(d, a[i + 1] - a[i]);
}
for (int i = 1; i < n - 1; i++) {
min_diff = min(max(d, a[i + 1] - a[i - 1]), min_diff);
}
cout << min_diff << endl;
}
| 7 | CPP |
n, m = map(int, input().split())
for i in range(n):
if i % 2 == 0:
print('#'*m)
elif (i//2) % 2 == 0:
print('.'*(m-1) + '#')
else:
print('#' + '.'*(m-1)) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = pow(10, 9) + 7;
long long fac[1001];
template <typename T, typename U>
static inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
if (x < y) x = y;
}
void SieveOfEratosthenes(long long n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (long long p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (long long p = 2; p <= n; p++)
if (prime[p]) {
}
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long modInverse(long long n, long long p) { return power(n, p - 2, p); }
long long nCrModPFermat(long long n, long long r, long long p) {
if (n < r) return 0;
if (r == 0) return 1;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
p;
}
void dfs(long long v, vector<vector<long long>> &adj,
vector<long long> &visited) {
for (long long u : adj[v]) {
if (!visited[u]) {
visited[u] = 1;
}
}
}
void solve() {
int n;
cin >> n;
if (n == 1) {
cout << "a"
<< "\n";
return;
}
if (n == 2) {
cout << "ab"
<< "\n";
return;
}
if (n == 3) {
cout << "abc"
<< "\n";
return;
}
long long i;
if (n & 1) {
int cnt = (n - 2) / 2;
string s = "";
for (i = 0; i < cnt; i++) {
s += 'a';
}
s += 'b';
s += 'c';
for (i = 0; i < cnt + 1; i++) {
s += 'a';
}
cout << s << "\n";
} else {
int cnt = (n - 1) / 2;
string s = "";
for (i = 0; i < cnt; i++) {
s += 'a';
}
s += 'b';
for (i = 0; i < cnt + 1; i++) {
s += 'a';
}
cout << s << "\n";
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long test = 2;
switch (test) {
case 1:
solve();
break;
case 2: {
long long t;
cin >> t;
while (t--) {
solve();
}
}; break;
}
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long r[250], g[250], b[250];
long long dp[210][210][210];
int main() {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
for (int i = 1; i <= x; i++) {
scanf("%lld", &r[i]);
}
sort(r + 1, r + x + 1);
for (int i = 1; i <= y; i++) {
scanf("%lld", &g[i]);
}
sort(g + 1, g + y + 1);
for (int i = 1; i <= z; i++) {
scanf("%lld", &b[i]);
}
sort(b + 1, b + z + 1);
long long ans = 0;
memset(dp, -1, sizeof(dp));
dp[x][y][z] = 0;
for (int i = x; i >= 0; i--) {
for (int j = y; j >= 0; j--) {
for (int k = z; k >= 0; k--) {
if (dp[i + 1][j + 1][k] >= 0) {
dp[i][j][k] =
max(dp[i][j][k], dp[i + 1][j + 1][k] + r[i + 1] * g[j + 1]);
}
if (dp[i][j + 1][k + 1] >= 0) {
dp[i][j][k] =
max(dp[i][j][k], dp[i][j + 1][k + 1] + g[j + 1] * b[k + 1]);
}
if (dp[i + 1][j][k + 1] >= 0) {
dp[i][j][k] =
max(dp[i][j][k], dp[i + 1][j][k + 1] + r[i + 1] * b[k + 1]);
}
ans = max(ans, dp[i][j][k]);
}
}
}
printf("%lld\n", ans);
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
typedef long long ll;
const ll inf = 1e9;
int main(){
int a, b, c;
cin >> a >> b >> c;
cout << min(a, min(b, c)) << " " << max(a, max(b, c)) << endl;
return 0;
}
| 0 | CPP |
N = int(input())
M = 10**9
y = -(-N//M)
x = (M - N%M) % M
print(0,0,M,1,x,y) | 0 | PYTHON3 |
s = input()
S = s[2:-1]
if s[0]=='A' and S.count('C')==1 and s[1:].replace("C","",1).islower():
print('AC')
else:
print('WA') | 0 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
#define Rep(i,a,b) for(register int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(register int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(register int i=(a);i<int(b);++i)
#define mem(x,v) memset(x,v,sizeof(x))
#define pc putchar
#define gc getchar
inline ll read(){
register ll x=0,f=1;register char c=gc();
for(;!isdigit(c);c=gc())if(c=='-')f=-1;
for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
return x*f;
}
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void wri(ll x){write(x),pc(' ');}
void writeln(ll x){write(x);pc('\n');}
const int maxn = 2e5+233;
const int mod = 1e9+7;
int fac[maxn],ifac[maxn],n,i,ans,ct[maxn],ct2,X,Y,m11,m12,c11,m2[maxn],c[maxn],w[maxn];
bool b[maxn];
inline int qpow(int a,int b){
int ans=1;
for(;b;b>>=1,a=1ll*a*a%mod)
if(b&1) ans=1ll*ans*a%mod;
return ans;
}
int main(){
fac[0] = 1;rep(i,1,maxn) fac[i] = 1ll * fac[i-1] * i % mod;
ifac[maxn - 1] = qpow(fac[maxn-1],mod-2);
Dep(i,maxn-2,0) ifac[i] = 1ll * ifac[i+1] * (i+1) % mod;
n = read(),Y = read(),X = read();
Rep(i,1,n) m2[i]=1<<30,m11=m12=1<<30;
Rep(i,1,n){
c[i]=read(),w[i]=read();
m2[c[i]]=min(m2[c[i]],w[i]);
if(w[i]<m11) m12=m11,m11=w[i],c11=c[i]; else
if(w[i]<m12) m12=w[i];
}
Rep(i,1,n)if(c11!=c[i]&&m11+w[i]<=X || c11==c[i]&&m12+w[i]<=X)
++ct[c[i]],++ct2,b[i]=1;
Rep(i,1,n)if(!b[i] && ct[c[i]] && m2[c[i]]+w[i]<=Y)
++ct[c[i]],++ct2;
int ans=fac[ct2];
Rep(i,1,n) ans=1ll*ans*ifac[ct[i]]%mod;
writeln(ans);
} | 0 | CPP |
from math import *
for u in range(int(input())):
n=int(input())
X=pi/(2*n)
print(cos(X/2)/sin(X)) | 9 | PYTHON3 |
N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(N):
ans ^= a[i]
if(ans == 0):
print("Yes")
else:
print("No")
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int inf = 1 << 30;
int n, t;
int main() {
cin >> n;
int v;
set<int> s;
for (int i = 0; i < n; i++) {
cin >> v;
if (v != 0) s.insert(v);
}
int a = s.size();
if (a != 0)
cout << a << endl;
else
cout << 1 << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, sum = 0;
cin >> n >> m;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n; i++) {
sum += arr[i];
}
if (sum != m) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
}
return 0;
}
| 7 | CPP |
n = int(input())
m = (2*n-1)
for i in range(1,1000):
if 2*i-1>=n:
ans=i
break
print(ans)
c=0
for i in range(1,ans+1):
print(1,i)
c+=1
if c>=n:
exit()
for i in range(2,ans+1):
print(ans,i)
c+=1
if c>=n:
break
| 8 | PYTHON3 |
import sys
import math
input = sys.stdin.readline
N = int(input())
k = N
ans = set()
while True:
if k == 1 : break
q = k
for i in range(2,int(math.sqrt(k))+2):
if k % i == 0:
ans.add(i)
k //= i
break
if k == q:
ans.add(k)
break
if N == 1:
print(1)
elif len(ans) == 1:
print(*ans)
else:
print(1) | 7 | PYTHON3 |
#include<iostream>
using namespace std;
int main(){
int n, w, r[205], b[205], ans = 0;
cin >> n;
for(int i=0; i<201; ++i) r[i] = b[i] = 201;
for(int i=0; i<n; ++i) {cin >> w; cin >> r[w];}
for(int i=0; i<n; ++i) {cin >> w; cin >> b[w];}
for(int i=1; i<200; ++i){
if(b[i] != 201){
int rmaxi = 201;
for(int k=0; k<i; ++k){
if(r[k] != 201 && r[k] < b[i]){
if(rmaxi == 201) rmaxi = k;
if(r[rmaxi] < r[k]){
rmaxi = k;
}
}
}
if(rmaxi != 201){
r[rmaxi] = 201;
++ans;
}
}
}
cout << ans << endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3 * 10000 * 10 + 100;
long long n, m, a[MAXN], b[MAXN], ans;
long long find(long long k) {
long long ans = 0;
b[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) b[i] = min(a[i], b[i + 1] - k);
if (b[0] < (k * (k - 1) / 2 * n + k)) return 0;
long long o = (k - 1) * m - (k - 1) * (k) / 2 * n + k - 1;
long long pos0 = (b[0] <= o ? 1 : b[0] - o);
long long empties = m - pos0 + 1 - k * n;
o = k * (k - 1) / 2 * n + k * pos0;
for (int i = 0; i < n; i++) {
ans += o;
b[i] -= o;
o += k;
}
if (!empties) return ans;
long long t = b[0];
ans += t * n;
for (int i = 0; i < n; i++) {
b[i] -= t;
ans += min(b[i], empties);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 1; i <= m / n; i++) ans = max(ans, find(i));
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10, LOG = 20;
pair<pair<int, int>, int> e[MAXN];
struct Graph {
int id, next, dis;
bool flag;
} g[MAXN * LOG * 2];
int n, N, m, tot, st[MAXN * LOG], dis[MAXN * LOG], size[MAXN * LOG],
bel[MAXN * LOG], opp[MAXN];
bool b[MAXN * LOG];
void Add(int first, int second, int z) {
g[tot].id = second, g[tot].dis = z, g[tot].flag = true,
g[tot].next = st[first], st[first] = tot++;
g[tot].id = first, g[tot].dis = z, g[tot].flag = true,
g[tot].next = st[second], st[second] = tot++;
}
void init() {
scanf("%d%d", &n, &m);
N = n;
tot = 0;
for (int i = 1; i <= m; ++i) {
int first;
scanf("%d", &first);
b[first] = true;
}
for (int i = 1; i <= n; ++i) bel[i] = i;
memset(st, -1, sizeof(st));
tot = 0;
for (int i = 1; i < n; ++i) {
int first, second, z;
scanf("%d%d%d", &first, &second, &z);
Add(first, second, z);
e[i] = make_pair(make_pair(first, second), z);
}
}
int Find_G(int first, int fa, int n) {
size[first] = 1;
bool flag = true;
for (int i = st[first]; i != -1; i = g[i].next)
if (g[i].flag && g[i].id != fa) {
int second = Find_G(g[i].id, first, n);
if (second != -1) return second;
size[first] += size[g[i].id];
if (size[g[i].id] > n / 2) flag = false;
}
if (flag && n - size[first] <= n / 2) return first;
return -1;
}
const int INF = 0x3f3f3f3f;
pair<int, int> DFS(int first, int fa) {
size[first] = 1;
pair<int, int> Tohka =
b[first] ? make_pair(dis[first], first) : make_pair(-INF, 0);
for (int i = st[first]; i != -1; i = g[i].next)
if (g[i].flag && g[i].id != fa) {
dis[g[i].id] = dis[first] + g[i].dis;
pair<int, int> second = DFS(g[i].id, first);
size[first] += size[g[i].id];
if (second.first == Tohka.first) Tohka.second = first;
if (second.first > Tohka.first) Tohka = second;
}
return Tohka;
}
void solve(int dep, int first, int n) {
if (n == 2) {
for (int i = st[first]; i != -1; i = g[i].next)
if (g[i].flag) {
if (first < MAXN) opp[first] = g[i].id;
if (g[i].id < MAXN) opp[g[i].id] = first;
break;
}
return;
}
int G = Find_G(first, 0, n);
pair<pair<int, int>, int> ent[3];
memset(ent, 0, sizeof(ent));
for (int i = 0; i < 3; ++i) ent[i].first.first = -INF;
if (b[G]) ent[0].first = make_pair(0, G);
for (int i = st[G]; i != -1; i = g[i].next)
if (g[i].flag) {
dis[g[i].id] = g[i].dis;
pair<pair<int, int>, int> info = make_pair(DFS(g[i].id, G), g[i].id);
if (ent[0] < info)
ent[2] = ent[1], ent[1] = ent[0], ent[0] = info;
else if (ent[1] < info)
ent[2] = ent[1], ent[1] = info;
else if (ent[2] < info)
ent[2] = info;
}
int ty = (ent[0].first.first == ent[1].first.first) +
(ent[0].first.first == ent[2].first.first);
if (G < MAXN)
if (ent[0].first.first != ent[1].first.first)
opp[G] = ent[0].first.second;
else
opp[G] = G;
for (int i = st[G]; i != -1; i = g[i].next)
if (g[i].flag) {
if (ent[0].first.first >= 0)
if (!ty) {
++N;
b[N] = true;
if (g[i].id == ent[0].second) {
if (ent[1].first.first >= 0)
bel[N] = ent[1].first.second,
Add(g[i].id, N, ent[1].first.first + g[i].dis);
} else
bel[N] = ent[0].first.second,
Add(g[i].id, N, ent[0].first.first + g[i].dis);
} else if (ty == 1) {
++N;
b[N] = true;
if (g[i].id != ent[0].second && g[i].id != ent[1].second)
bel[N] = G, Add(g[i].id, N, ent[0].first.first + g[i].dis);
else {
int p = (g[i].id == ent[0].second);
bel[N] = ent[p].first.second;
Add(g[i].id, N, ent[0].first.first + g[i].dis);
}
} else {
++N;
b[N] = true;
bel[N] = G;
Add(g[i].id, N, ent[0].first.first + g[i].dis);
}
g[i].flag = g[i ^ 1].flag = false;
solve(dep + 1, g[i].id, size[g[i].id] + 1);
}
}
int get_bel(int first) {
if (bel[first] <= n) return bel[first];
bel[first] = get_bel(bel[first]);
return bel[first];
}
int f[MAXN][LOG], Tohka[MAXN], dep[MAXN], c[MAXN];
void Vis(int first, int fa) {
for (int i = st[first]; i != -1; i = g[i].next)
if (g[i].id != fa) {
dep[g[i].id] = dep[first] + 1;
f[g[i].id][0] = first;
for (int j = 1; j < LOG; ++j) f[g[i].id][j] = f[f[g[i].id][j - 1]][j - 1];
Vis(g[i].id, first);
}
}
int LCA(int first, int second) {
if (dep[first] > dep[second]) swap(first, second);
for (int i = LOG - 1; i >= 0; --i)
if (dep[f[second][i]] >= dep[first]) second = f[second][i];
if (first == second) return first;
for (int i = LOG - 1; i >= 0; --i)
if (f[first][i] != f[second][i]) first = f[first][i], second = f[second][i];
return f[first][0];
}
void Vis2(int first, int fa) {
for (int i = st[first]; i != -1; i = g[i].next)
if (g[i].id != fa) {
Vis2(g[i].id, first);
c[first] += c[g[i].id];
}
Tohka[first] += c[first];
}
void solve() {
solve(0, 1, n);
for (int i = 1; i <= n; ++i) opp[i] = get_bel(opp[i]);
tot = 0;
memset(st, -1, sizeof(st));
for (int i = 1; i < n; ++i)
Add(e[i].first.first, e[i].first.second, e[i].second);
Vis(1, 0);
for (int i = 1; i <= n; ++i)
if (b[i]) {
int first = i, second = opp[i], lca = LCA(first, second);
if (first == second) continue;
++Tohka[lca], c[lca] -= 2, ++c[first], ++c[second];
}
Vis2(1, 0);
int Houki = 0, Kurumi = 0;
for (int i = 1; i <= n; ++i)
if (!b[i]) {
if (Tohka[i] > Houki) Houki = Tohka[i], Kurumi = 0;
Kurumi += (Houki == Tohka[i]);
}
cout << Houki << " " << Kurumi << endl;
}
int main() {
init();
solve();
fclose(stdin);
fclose(stdout);
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[4];
int dp[4][200111];
int e, s, n, m;
int f(int a, int b) {
int x = lower_bound(v[a].begin(), v[a].end(), b) - v[a].begin();
return dp[a][x] + max(0, v[a][x] - b - s);
}
int main() {
scanf("%d%d%d%d", &e, &s, &n, &m);
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
if (b >= e) continue;
for (int j = 1; j <= a; j++) v[j].push_back(b);
}
for (int i = 1; i <= 3; i++) {
v[i].push_back(e);
sort(v[i].begin(), v[i].end());
for (int j = v[i].size() - 1 - 1; j >= 0; j--)
dp[i][j] = dp[i][j + 1] + max(0, v[i][j + 1] - v[i][j] - s);
}
while (m--) {
int a;
scanf("%d", &a);
if (f(1, a) != 0)
printf("-1 -1\n");
else
printf("%d %d\n", f(2, a) - f(1, a), f(3, a) - f(2, a));
}
}
| 11 | CPP |
t=int(input())
for i in range(t):
a,b,n=map(int,input().split())
l=[a,b]
c=a^b
l.append(c)
print(l[n%3]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << '=' << h << ',';
_dbg(sdbg + 1, a...);
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <class Iterable,
class = typename enable_if<!is_same<string, Iterable>::value>::type>
auto operator<<(ostream &os, Iterable v) -> decltype(os << *begin(v)) {
os << "[";
for (auto vv : v) os << vv << ", ";
return os << "]";
}
const int inf = 0x3f3f3f3f;
const long long infll = 0x3f3f3f3f3f3f3f3fll;
template <class T>
int sign(T x) {
return (x > 0) - (x < 0);
}
template <class T>
T abs(const T &x) {
return (x < T(0)) ? -x : x;
}
const long long mod = (119 << 23) + 1, root = 62;
long long modpow(long long b, long long e) {
long long ans = 1;
for (; e; b = b * b % mod, e /= 2)
if (e & 1) ans = ans * b % mod;
return ans;
}
void ntt(vector<long long> &a, vector<long long> &rt, vector<long long> &rev,
int n) {
for (int i = 0; i < n; i++)
if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int k = 1; k < n; k *= 2)
for (int i = 0; i < n; i += 2 * k)
for (int j = 0; j < k; j++) {
long long z = rt[j + k] * a[i + j + k] % mod, &ai = a[i + j];
a[i + j + k] = (z > ai ? ai - z + mod : ai - z);
ai += (ai + z >= mod ? z - mod : z);
}
}
vector<long long> conv(const vector<long long> &a, const vector<long long> &b) {
if (a.empty() || b.empty()) return {};
int s = ((int)(a).size()) + ((int)(b).size()) - 1, B = 32 - __builtin_clz(s),
n = 1 << B;
vector<long long> L(a), R(b), out(n), rt(n, 1), rev(n);
L.resize(n), R.resize(n);
for (int i = 0; i < n; i++) rev[i] = (rev[i / 2] | (i & 1) << B) / 2;
long long curL = mod / 2, inv = modpow(n, mod - 2);
for (int k = 2; k < n; k *= 2) {
long long z[] = {1, modpow(root, curL /= 2)};
for (int i = k; i < 2 * k; i++) rt[i] = rt[i / 2] * z[i & 1] % mod;
}
ntt(L, rt, rev, n);
ntt(R, rt, rev, n);
for (int i = 0; i < n; i++) out[-i & (n - 1)] = L[i] * R[i] % mod * inv % mod;
ntt(out, rt, rev, n);
return {out.begin(), out.begin() + s};
}
struct term {
vector<long long> p;
bool operator<(const term &rhs) const { return p.size() < rhs.p.size(); }
};
vector<long long> productory(vector<vector<long long>> polys) {
multiset<term> s;
for (auto &p : polys) s.insert({p});
while (s.size() > 1) {
auto it = s.begin();
vector<long long> a = it->p;
it = s.erase(it);
vector<long long> b = it->p;
s.erase(it);
vector<long long> prod = conv(a, b);
while (prod.back() == 0) prod.pop_back();
s.insert({prod});
}
return s.begin()->p;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int n, p;
cin >> n;
map<int, int> rdm;
for (int i = 0; i < n; i++) {
cin >> p;
rdm[p]++;
}
vector<vector<long long>> polys;
for (auto x : rdm) {
vector<long long> tmp(x.second + 1, 1);
polys.push_back(tmp);
}
vector<long long> res = productory(polys);
cout << res[n / 2] << endl;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 5;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
string s, t;
cin >> s >> t;
long long possible = 0, res = 0, first = 0, second = 0;
long long i = 0;
for (i = 0; i < n && possible < k; i++) {
first *= 2ll;
second *= 2ll;
first += s[i] - 'a';
second += t[i] - 'a';
possible = second - first + 1;
res += min(possible, k);
}
i = n - i;
res += k * i;
cout << res << "\n";
return 0;
}
| 11 | CPP |