solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
return x > 0 ? x : -x;
}
const long double pi = acos(-1.0);
int n;
int m;
char ans[2000100];
int l = 0;
void push_back(char ch) { ans[l++] = ch; }
int main() {
scanf("%d%d", &n, &m);
if (n <= m) {
int k = m - n;
if (k <= n + 2) {
int p = min(k, n);
for (int i = 0; i < p; i++) {
push_back('1');
push_back('1');
push_back('0');
}
for (int i = 0; i < n - p; i++) {
push_back('1');
push_back('0');
}
if (k == n + 1) {
push_back('1');
}
if (k == n + 2) {
push_back('1');
push_back('1');
}
} else {
printf("-1\n");
return 0;
}
} else {
if (n == m + 1) {
push_back('0');
for (int i = 0; i < m; i++) {
push_back('1');
push_back('0');
}
} else {
printf("-1\n");
return 0;
}
}
puts(&ans[0]);
return 0;
}
| 9 | CPP |
# @author Nayara Souza
# UFCG - Universidade Federal de Campina Grande
# AA - Basico
n = int(input())
for i in range(n):
x = int(input())
r = 1
for j in range(2, min(x, 10**5)):
if (x % j == 0):
r = x//j
break
print(r, x-r)
| 8 | PYTHON3 |
#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
const int mod = 1e9+7;
const int N = 3e5+5;
const int INF = 1e9;
using namespace std;
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << " ";
err(++it, args...);
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
os << "[";
for (int i = 0; i < v.size(); ++i) {
os << v[i];
if (i != v.size() - 1)
os << ", ";
}
os << "]\n";
return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T,U>& v)
{
os << "[";
os << v.fi << ", "<< v.se;
os << "]";
return os;
}
template <typename T, typename U>
void smin(T & x, U y) {
if(x > y)
x = y;
}
template <typename T, typename U>
void smax(T & x, U y)
{
if(y > x)
x = y;
}
typedef long long ll;
ll n,m,k,q,Q,l,r,p,x,pos;
ll arr[N],brr[N];
const int md = 998244353;
ll binpow(ll a, ll n)
{
ll res = 1;
while(n)
{
if(n&1)
res = res * a % mod;
n/=2;
a = a * a % mod;
}
return res;
}
ll digsum(ll n)
{
ll ans = 0;
while(n)
{
ans += n%10;
n/=10;
}
return ans;
}
void solve()
{
cin >> n >> m;
multiset<int> ms;
int x;
for(int i = 0; i < n; i++)
{
cin >> x;
ms.insert(x);
}
int ans = 0;
while(ms.size() > 0)
{
int sz = m;
while(1)
{
auto it = ms.upper_bound(sz);
if(it == ms.begin())
break;
else --it;
ll p = *it;
ms.erase(it);
sz -= p;
}
ans++;
}
cout << ans << "\n";
}
int main()
{ ios_base::sync_with_stdio(false);cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("fi.txt","r",stdin);
freopen("fo.txt","w",stdout);
freopen("err.txt","w",stderr);
#endif
int t;
t=1;
cin>>t;
int a = 0;
while(t--)
{ //cout<<"Case #"<<++a<<": ";
solve();
}
return 0;
} | 8 | CPP |
#https://codeforces.com/contest/1341/problem/A
for _ in range(int(input())):
n,a,b,c,d=map(int,input().split())
if (a+b)*n >=(c-d) and (a-b)*n <=(c+d):
print('YES')
else:
print('NO')
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int sta[100010], top, n, x, ans;
int main() {
scanf("%d", &n);
while (n--) {
scanf("%d", &x);
while (top) {
ans = max(ans, sta[top] ^ x);
if (sta[top] < x)
top--;
else
break;
}
sta[++top] = x;
}
printf("%d\n", ans);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int a = 0; a <= n; a += 1234567) {
for (int b = 0; b <= n - a; b += 123456) {
if ((n - a - b) % 1234 == 0) {
cout << "YES";
return 0;
}
}
}
cout << "NO";
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long int n, k;
cin >> n >> k;
long long int arr[n];
long long int max = INT_MIN;
long long int min = INT_MAX;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (max < arr[i]) max = arr[i];
if (min > arr[i]) min = arr[i];
}
long long int d = min + k;
long long int j = 0;
for (int i = 0; i < n; i++) {
if (abs(arr[i] - d) <= k) {
j++;
}
}
if (j == n)
cout << d << '\n';
else
cout << -1 << '\n';
}
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int boss[200000 + 10];
int main() {
int n, s;
while (~scanf("%d%d", &n, &s)) {
int ans = 0, maxd = 1;
;
for (int i = 0; i < n; i++) {
scanf("%d", &boss[i]);
}
if (boss[s - 1] != 0) {
ans++;
boss[s - 1] = 0;
}
sort(boss, boss + n);
for (int i = 1, j = n - 1; i < n; i++) {
if (i > j) break;
if (boss[i] == 0) {
ans++;
maxd++;
continue;
}
if (boss[i] == boss[i - 1]) {
continue;
}
if (boss[i] <= maxd) {
maxd++;
continue;
}
if (boss[i] > maxd) {
maxd++;
ans++;
j--;
i--;
}
}
printf("%d\n", ans);
}
return 0;
}
| 9 | CPP |
n =int(input())
ar = list(map(int,input().split()))
odd = [i for i in ar if i%2==1]
even =[i for i in ar if i%2==0]
odd.sort(reverse = True)
even.sort(reverse = True)
sum_even = 0
for i in even:
if i>0:
sum_even+=i
preSum = []
preSum.append(odd[0])
for i in range(1,len(odd)):
preSum.append(preSum[i-1]+odd[i])
max_odd_sum = preSum[0]
for i in range(2,len(odd),2):
max_odd_sum = max(max_odd_sum,preSum[i])
print(max(max_odd_sum+sum_even,max_odd_sum))
# print(odd)
# print(even)
# print(preSum)
| 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
scanf("%d",&n);
vector<int> x(n), y(n);
for(int i=0;i<n;i++){
scanf("%d%d",&x[i],&y[i]);
}
if(n%2){
puts("NA");
}else{
set< pair<int,int> > med;
for(int i=0;i<n/2;i++){
med.insert( make_pair( x[i]+x[i+n/2], y[i]+y[i+n/2] ) );
}
if(med.size() != 1){
puts("NA");
}else{
pair<int,int> ans = *med.begin();
printf("%.2f %.2f\n",ans.first/2.0,ans.second/2.0);
}
}
} | 0 | CPP |
line1 = input().split()
n = int(line1[0])
c = int(line1[1])
a = list(map(int, input().split()))
count = 1
for i in range(1,n):
if a[i] - a[i-1] > c:
count = 1
else:
count = count+1
print(count)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, m;
cin >> N >> m;
vector<pair<int, int>> cd(m);
int c, d;
for(int i = 0; i < m; i++){
cin >> c >> d;
cd[i] = make_pair(c, d);
}
sort(cd.begin(), cd.end());
int ans = N + 1;
int start = 0;
int end = 0;
for(int i = 0; i < m; i++){
auto temp = cd[i];
if(temp.first < end){
end = max(end, temp.second);
}
else{
ans += (end - start) * 2;
start = temp.first;
end = temp.second;
}
}
ans += (end - start) * 2;
cout << ans << endl;
}
| 0 | CPP |
from collections import defaultdict
I = lambda: int(input())
IL = lambda: list(map(int, input().split()))
n, m = IL()
LH = defaultdict(list)
for i in range(n):
A = sorted(IL()[1:])
for l in A:
LH[l].append(i)
G = defaultdict(set)
for c in LH.values():
for h2 in c[1:]:
G[c[0]].add(h2)
G[h2].add(c[0])
v = set()
ans = -bool(LH)
for i in range(n):
if i not in v:
v.add(i)
ans += 1
neigh = G[i]
while neigh:
v = v.union(neigh)
neigh = {h1 for h in neigh for h1 in G[h]} - v
print(ans) | 9 | PYTHON3 |
ε
₯ε = input()
for i in range(len(ε
₯ε)-1):
if ε
₯ε[i]==ε
₯ε[i+1]:
print(i+1, i+2)
exit()
elif i<len(ε
₯ε)-2 and ε
₯ε[i]==ε
₯ε[i+2]:
print(i+1, i+3)
exit()
print(-1, -1) | 0 | PYTHON3 |
#include <bits/stdc++.h>
template <class Type>
constexpr bool cmin(Type &a, const Type &b) {
return b < a ? (a = b, 1) : 0;
}
template <class Type>
constexpr bool cmax(Type &a, const Type &b) {
return a < b ? (a = b, 1) : 0;
}
constexpr int N = 1e5 + 1, M = 2e5, P = 1e9 + 7;
struct Edge {
int to;
long long w;
Edge *next;
};
struct LinearBase {
int cnt;
long long base[64], possible;
void insert(long long x) {
while (x) {
long long &cur = base[63 - __builtin_clzll(x)];
if (!cur) {
++cnt;
cur = x;
possible |= x;
break;
}
x ^= cur;
}
}
};
bool vis[N];
int n, m, size, cnt[64];
long long d[N];
Edge edge[M << 1], *head[N];
LinearBase base;
void add(int u, int v, long long t) {
static Edge *e = edge;
*e = (Edge){v, t, head[u]};
head[u] = e++;
}
void dfs(int x, long long xorsum) {
d[x] = xorsum;
++size;
for (int i = 0; i < 64; ++i) cnt[i] += xorsum >> i & 1;
vis[x] = 1;
for (Edge *i = head[x]; i; i = i->next) {
if (vis[i->to]) {
base.insert(xorsum ^ d[i->to] ^ i->w);
} else {
dfs(i->to, xorsum ^ i->w);
}
}
}
int main() {
std::cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v;
long long t;
std::cin >> u >> v >> t;
add(u, v, t);
add(v, u, t);
}
int ans = 0;
for (int i = 1; i <= n; ++i)
if (!vis[i]) {
size = 0;
memset(cnt, 0, sizeof(cnt));
memset(&base, 0, sizeof(base));
dfs(i, 0);
for (int j = 0; j < 64; ++j)
if (base.possible >> j & 1)
ans = (ans + (1ll << base.cnt - 1) % P * ((1ll << j) % P) % P *
(size * (size - 1ll) / 2 % P)) %
P;
else
ans = (ans + (1ll << base.cnt) % P * ((1ll << j) % P) % P * cnt[j] %
P * (size - cnt[j])) %
P;
}
std::cout << ans;
return 0;
}
| 13 | CPP |
x=int(input())
t=0
for i in range(x):
a=list(map(int,input().split()))
if sum(a)>=2:
t=t+1
print(t)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int ma = 0;
bool p[10000000];
vector<int> primes;
void Prime() {
memset(p, -1, sizeof p);
p[0] = 0;
p[1] = 1;
for (int i = 2; i < 10000000; i++) {
if (p[i]) {
primes.push_back(i);
for (int j = i * 2; j < 10000000; j += i) {
p[j] = 0;
}
}
}
}
int Divisors2(int n) {
int d = 1, e = 0;
for (int p : primes) {
if (p * p > n) break;
while (n % p == 0) {
e++;
n /= p;
}
d *= (e + 1);
e = 0;
}
if (n != 1) d *= 2;
return d;
}
int main() {
Prime();
int a, b, c;
long long sum = 0;
cin >> a >> b >> c;
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
for (int k = 1; k <= c; k++) {
v.push_back(i * j * k);
}
}
}
for (int i = 0; i < (int)v.size(); i++) {
sum += Divisors2(v[i]);
}
cout << sum;
return 0;
}
| 11 | CPP |
#include <iostream>
using namespace std;
int main(){
int n, ans = 0;
while (cin >> n){
ans = 0;
for(int i=0;i<=9;i++){
for(int j=0;j<=9;j++){
for(int k=0;k<=9;k++) {
for(int l=0;l<=9;l++){
if (i + j + k + l == n)ans++;
}
}
}
}
cout << ans << endl;
}
} | 0 | CPP |
nbtest = int(input())
for i in range(nbtest):
nbelement = int(input())
liste = input().split(" ")
for i in range(nbelement):
liste[i]=int(liste[i])
somme = liste[0]
instant = liste[0]
for i in range(1,nbelement):
if instant*liste[i]>0:
somme = somme +max(instant,liste[i])-instant
instant = max(instant,liste[i])
else:
somme+=liste[i]
instant = liste[i]
print(somme)
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[1005], b[1005], c[1005], d[1005], posa[1005], posb[1005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int q = 1; q <= n; q++) {
int x;
cin >> x;
a[x] = q;
}
for (int q = 1; q <= n; q++) {
int x;
cin >> x;
b[x] = q;
}
for (int q = 1; q <= n; q++) {
c[q] = d[q] = posa[q] = posb[q] = q;
}
vector<array<int, 4>> v;
for (int q = 1; q <= n; q++) {
if (c[q] == a[q] && d[q] == b[q]) continue;
v.push_back({q, posb[b[q]], posa[a[q]], q});
posb[d[q]] = posb[b[q]];
posa[c[q]] = posa[a[q]];
d[posb[d[q]]] = d[q];
c[posa[c[q]]] = c[q];
}
cout << ((int)((v).size())) << '\n';
for (int q = 0; q < ((int)((v).size())); q++) {
for (int w = 0; w < 4; w++) cout << v[q][w] << ' ';
cout << '\n';
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct node {
vector<int> to;
int con = -1;
};
vector<node> graph;
int cost = 0;
void walk(int curr, int father) {
vector<int> notCon, con;
for (int& next : graph[curr].to) {
if (next != father) {
walk(next, curr);
if (graph[next].con == -1)
notCon.push_back(next);
else
con.push_back(next);
}
}
if (notCon.size() > 0) {
notCon.push_back(curr);
int s = notCon.size();
cost += 2 * s - 2;
for (int i = 0; i < s; i++) {
graph[notCon[i]].con = notCon[(i + 1) % s];
}
} else if (father == -1) {
int child = con[0];
graph[curr].con = graph[child].con;
graph[child].con = curr;
cost += 2;
}
}
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
graph.resize(n + 1);
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
graph[a].to.push_back(b);
graph[b].to.push_back(a);
}
walk(1, -1);
cout << cost << "\n";
for (int i = 1; i <= n; i++) cout << graph[i].con << " ";
cout << endl;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int a, b;
long long ans;
int main() {
ios_base::sync_with_stdio(0);
cin >> a >> b;
for (int i = 0; i <= a; i++) {
int l = i, r = a - i;
int can = min(l, r);
for (int j = 1; j <= b; j++) {
int len = j * 2;
if (len <= b) ans += 1ll * (b - len + 1) * can;
}
}
cout << ans;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
map<pair<int, int>, int> m;
int n;
string s;
cin >> n >> s;
int x = 0, y = 0;
m[{x, y}] = -1;
int best = 1e9;
pair<int, int> ans;
for (int i = 0; i < n; i++) {
x += (s[i] == 'R') - (s[i] == 'L');
y += (s[i] == 'U') - (s[i] == 'D');
if (m.count({x, y})) {
int j = m[{x, y}];
if (i - j < best) {
best = i - j;
ans = {j, i};
}
}
m[{x, y}] = i;
}
if (best == 1e9) {
cout << -1 << endl;
} else {
cout << ans.first + 2 << ' ' << ans.second + 1 << endl;
}
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct thanks {
int x, y, id;
} p[100005];
bool cmp(thanks x, thanks y) {
if (x.x != y.x)
return x.x < y.x;
else
return x.y < y.y;
}
bool ok(thanks x, thanks y, thanks z) {
if (1LL * (y.y - x.y) * (z.x - y.x) != 1LL * (z.y - y.y) * (y.x - x.x))
return 1;
else
return 0;
}
int main() {
int i, j, n, m;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].id = i;
}
sort(p + 1, p + n + 1, cmp);
for (i = 3; i <= n; i++) {
if (ok(p[i - 2], p[i - 1], p[i])) {
printf("%d %d %d\n", p[i - 2].id, p[i - 1].id, p[i].id);
return 0;
}
}
return 0;
}
| 9 | CPP |
n = int(input())
a = list(map(int, input().split()))
inf = 10 ** 9
ans = [inf] * n
l = inf
for i in range(n):
if not a[i]:
l = 0
ans[i] = min(ans[i], l)
l += 1
l = inf
for i in range(n - 1, -1, -1):
if not a[i]:
l = 0
ans[i] = min(ans[i], l)
l += 1
print(' '.join(map(str, ans))) | 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;
int _,a[N],cnt[2];
int n,q,ans,len;
char s[N];
int u,r,d,l;
bool ok(int x) {
return x>=0&&x<=n-2;
}
int main() {
for(scanf("%d",&_); _; _--) {
scanf("%d%d%d%d%d",&n,&u,&r,&d,&l);
int f=0;
for(int s=0;s<16;s++) {
int a=(s)&1;
int b=(s>>1)&1;
int c=(s>>2)&1;
int dd=(s>>3)&1;
int U=u,R=r,D=d,L=l;
if(a) U--,L--;
if(b) U--,R--;
if(c) R--,D--;
if(dd) D--,L--;
if(ok(U)&&ok(R)&&ok(D)&&ok(L)) {
f=1;
}
}
if(f) puts("YES");
else puts("NO");
}
}
| 8 | CPP |
a, b = map(int, input().split())
if a == b:
print(str(a) + "0" +" " + str(b) + "1")
elif a + 1 == b:
print(a, b)
elif a == 9 and b == 1:
print(99, 100)
else:
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smin(T &a, T b) {
if (a > b) a = b;
}
long long d, k, a, b, t, n, res;
int main() {
long long ans, st, ed, mi1, mi2, mid1, mid2;
scanf("%I64d %I64d %I64d %I64d %I64d", &d, &k, &a, &b, &t);
res = d % k;
n = d / k;
if (d <= k) {
ans = a * d;
} else {
ans = n * (k * a + t) + res * a;
if (res * b <= t + res * a) {
ans = ans - t - res * a + res * b;
if (k * a + t >= k * b) {
ans = ans - (n - 1) * (k * a + t) + (n - 1) * k * b;
}
}
}
printf("%I64d\n", ans);
return 0;
}
| 10 | CPP |
word = str(input())
number = int(len(word))
vowels = {'a','u','e','o','i','y'}
word = str.lower(word)
blank = ''
for i in range (number):
a = word[i]
if a in vowels:
a=''
blank = blank+a
word = blank
n=int(len(word))
f=''
for i in range(n):
b='.'
a= word[i]
f=f+b+a
print(f) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, tmp, sum = 0, sum2 = 0;
cin >> n;
int arr[2 * n];
for (int i = 0; i < 2 * n; i++) cin >> arr[i];
sort(arr, arr + 2 * n);
for (int i = 0; i < n; i++) sum += arr[i];
for (int i = n; i < 2 * n; i++) sum2 += arr[i];
if (sum == sum2) return cout << "-1\n", 0;
for (int i = 0; i < 2 * n; i++) cout << arr[i] << " ";
return 0;
}
| 7 | CPP |
tracker = {}
username = input()
for i in username:
if tracker.get(i) != None:
tracker[i] += 1
else:
tracker[i] = 1
count = 0
for i in tracker:
count += 1
print("CHAT WITH HER!" if count % 2 == 0 else "IGNORE HIM!") | 7 | PYTHON3 |
a,b=map(int,input().split())
i=0
while a<=b:
a*=3
b*=2
i=i+1
print(i) | 7 | PYTHON3 |
ip_no_days=int(input())
data_diff_days=input()
countSF=0
countFS=0
for i in range(1,len(data_diff_days)):
if data_diff_days[i-1]=='S' and data_diff_days[i]=='F':
countSF+=1
elif data_diff_days[i-1]=='F' and data_diff_days[i]=='S':
countFS+=1
else:
continue
if countSF>countFS:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c, k;
cin >> n >> c >> k;
vector<int> t(n);
for (auto &e : t) {
cin >> e;
}
sort(begin(t), end(t));
int m = 0, a = 0, b = 0;
for (auto e : t) {
if (m == 0 || c <= a || b + k < e) {
m++;
a = 1;
b = e;
} else {
a++;
}
}
cout << m << endl;
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double pi=acos(-1);
int main(){
double a, b, h, m;
cin >> a >> b >> h >> m;
double theta=pi*abs(h/6+m/360-m/30);
cout << fixed << setprecision(20);
cout << sqrt(a*a+b*b-2*a*b*cos(theta)) << endl;
} | 0 | CPP |
a ,b ,c = map(int,input().split())
if (a%2)*(b%2)*(c%2)== 0:
print(0)
else:
print(min(a*b,b*c,c*a)) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, INF = 1e9;
const int mx = 1e5 + 5;
void solve() {
int n, k, c1 = 0, c2 = 0;
cin >> n >> k;
string s;
cin >> s;
for (int i = 0; i < k; i++) {
int mask = 0;
for (int j = i; j < n; j += k) {
if (s[j] == '1') mask |= 2;
if (s[j] == '0') mask |= 1;
}
if (mask == 3) {
cout << "NO" << endl;
return;
}
if (mask == 1)
c1++;
else if (mask == 2)
c2++;
}
if (2 * c1 > k || 2 * c2 > k)
cout << "NO" << endl;
else
cout << "YES" << endl;
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 7 | CPP |
n, ar = int(input()), [int(x) for x in input().split()][::-1]
ans = 0
for i in range(1, n):
if ar[i] > ar[i - 1]:
ans = n - i
break
print(ans)
| 8 | PYTHON3 |
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#define pi 2*asin(1)
using namespace std;
int dpm[10009];
int dpl[10009];
int xia[10009];
struct node
{
int s,e,l,m;
} a[10009];
int main()
{
int i,j;
while(~scanf("%d%d",&i,&j),(i||j))
{
if(i%2==0||j%2==0)
printf("yes\n");
else printf("no\n");
}
return 0;
} | 0 | CPP |
#include <iostream>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if(0==a%3||0==b%3||0==(a+b)%3){
cout<<"Possible";
}else{
cout<<"Impossible";
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
int main() {
char arr[4][4];
int flag = 0;
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
scanf("%c", &arr[i][j]);
if (arr[i][j] == '\n') {
j--;
}
}
if (arr[i][j] == '\n') {
i--;
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if ((arr[i][j] == arr[i][j + 1]) &&
((arr[i][j] == arr[i + 1][j]) || (arr[i][j] == arr[i + 1][j + 1]))) {
flag = 1;
} else if ((arr[i][j] == arr[i + 1][j]) &&
(arr[i][j] == arr[i + 1][j + 1])) {
flag = 1;
} else if ((arr[i][j + 1] == arr[i + 1][j]) &&
(arr[i][j + 1] == arr[i + 1][j + 1])) {
flag = 1;
}
}
}
if (flag == 1) {
printf("YES");
} else {
printf("NO");
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool compareRank(pair<int, int> x, pair<int, int> y) {
return x.first == y.first ? x.second < y.second : x.first > y.first;
}
int main() {
int n, k;
cin >> n >> k;
pair<int, int> arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i].first >> arr[i].second;
}
sort(arr, arr + n, compareRank);
cout << count(arr, arr + n, arr[k - 1]);
}
| 7 | CPP |
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int,int> P;
vector<P> V;
const int MAX_N=100000;
int C[MAX_N];
bool b[MAX_N];
int main(){
int N,Q;
cin>>N>>Q;
for(int i=0;i<Q;i++){
int A,B;
cin>>A>>B;
A--;B--;
V.emplace_back(A,B);
}
for(int i=0;i<N;i++){
C[i]=i;
}
for(int i=Q-1;i>=0;i--){
int A=V[i].first, B=V[i].second;
swap(C[A],C[B]);
}
int p=0;
b[C[p]]=true;
b[C[p+1]]=true;
for(int i=0;i<Q;i++){
int A=V[i].first, B=V[i].second;
if(A==p){
p=B;
}else if(B==p){
p=A;
}
swap(C[A],C[B]);
if(p>0){
b[C[p-1]]=true;
}
if(p<N-1){
b[C[p+1]]=true;
}
}
int ans=0;
for(int i=0;i<N;i++){
if(b[i]) ans++;
}
cout<<ans<<endl;
return 0;
} | 0 | CPP |
p = int(input())
ans = 0
*A,= map(int,input().split())
for j,i in enumerate(A):
if A[A[j]-1]==j+1:
ans+=1
print(ans//2) | 0 | PYTHON3 |
from sys import stdin,stdout
def input():
return stdin.readline()
def ans(n,m,l,f,low,high):
for i in range(low,high+1):
mi=0
ma=0
for j in l:
if(j<=i):
mi+=1
if(j>i):
ma+=1
rig=f-i-ma
lef=i+1-mi
if(lef-rig==1 or rig ==lef):
return i
def main():
for _ in range(int(input())):
n,m=map(int,input().split())
f=2**m-1
l=[]
for i in range(n):
l.append(int(input().strip(),2))
med=(2**(m-1)-1)
if(med-n<0):
low=0
else:
low=med-n
if(med+n>f-1):
high=f
else:
high=med+n
k=bin(ans(n,m,l,f,low,high))[2:]
print("0"*(m-len(k))+k)
if __name__=="__main__":
main()
| 14 | PYTHON3 |
n=input()
k=int(input())
flag1=flag2=flag3=0
for i in range(k):
s=input()
if s==n:
flag1=1
flag2=1
elif s[0]==n[1] and s[1]==n[0]:
flag3=1
elif s[0]==n[1]:
flag1=1
elif s[1]==n[0]:
flag2=1
if(flag1 ==1 and flag2==1 or flag3==1):
print("YES")
else:
print("NO") | 7 | PYTHON3 |
import collections
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
def dfs(n):
if n == 4:
return (1, 1, 2)
if n % 2 == 1:
return (1, n // 2, n // 2)
else:
r1, r2, r3 = dfs(n // 2)
return (r1 * 2, r2 * 2, r3 * 2)
r1, r2, r3 = dfs(n - (k - 3))
res = [1 for _ in range(k - 3)]
res.append(r1)
res.append(r2)
res.append(r3)
print(*res, sep=' ') | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100000 + 10;
const long long mod = 1000 * 1000 * 1000 + 9;
int n, m, par[maxN], size[maxN];
long long ans;
int getpar(int x) { return (par[x] == x) ? x : par[x] = getpar(par[x]); }
void merge(int x, int y) {
x = getpar(x);
y = getpar(y);
if (x == y) {
ans = (ans * 2 + 1) % mod;
return;
}
if (size[x] < size[y]) swap(x, y);
size[x] += size[y];
par[y] = x;
return;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
par[i] = i;
size[i] = 1;
}
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
merge(u, v);
cout << ans << endl;
}
return 0;
}
| 9 | CPP |
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int a[11][11];
int main()
{
int n,d,cnt=0;
cin>>n>>d;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=d;j++)
{
cin>>a[i][j];
}
}
for(int i=1;i<=n-1;i++)
for(int j=i+1;j<=n;j++)
{
double p=0;
int dd=0;
for(int k=1;k<=d;k++)
{
dd=a[i][k]-a[j][k];
p+=pow(dd,2);
}
p=sqrt(p);
if(floor(p)==p)
cnt++;
}
cout<<cnt;
return 0;
} | 0 | CPP |
input()
print(['NO','YES'][all((c<a)==(b<a)for a,b,c in zip(*[map(int,input().split(' '))for _ in'abc']))]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A& a, B b) {
if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A& a, B b) {
if (a < b) a = b;
}
using ll = long long;
using Pi = pair<long long, long long>;
using Pl = pair<ll, ll>;
using Ti = tuple<long long, long long, long long>;
using Tl = tuple<ll, ll, ll>;
const long long INF = 1LL << 55;
struct edge {
long long to, capacity, cost, rev;
edge() {}
edge(long long to, long long capacity, long long cost, long long rev)
: to(to), capacity(capacity), cost(cost), rev(rev) {}
};
struct PrimalDual {
vector<vector<edge> > graph;
vector<long long> potential, mincost, prevv, preve;
PrimalDual(long long V)
: graph(V), potential(V), mincost(V), prevv(V), preve(V) {}
void add_edge(long long from, long long to, long long capacity,
long long cost) {
graph[from].push_back(
edge(to, capacity, cost, (long long)graph[to].size()));
graph[to].push_back(
edge(from, 0, -cost, (long long)graph[from].size() - 1));
}
long long min_cost_flow(long long source, long long sink, long long f) {
long long ret = 0;
fill(potential.begin(), potential.end(), 0);
fill(prevv.begin(), prevv.end(), -1);
fill(preve.begin(), preve.end(), -1);
while (f > 0) {
priority_queue<Pi, vector<Pi>, greater<Pi> > que;
fill(mincost.begin(), mincost.end(), INF);
mincost[source] = 0;
que.push(Pi(0, source));
while (!que.empty()) {
Pi p = que.top();
que.pop();
long long v = p.second;
if (mincost[v] < p.first) continue;
for (long long i = 0; i < (long long)graph[v].size(); i++) {
edge& e = graph[v][i];
long long dual_cost =
mincost[v] + e.cost + potential[v] - potential[e.to];
if (e.capacity > 0 && dual_cost < mincost[e.to]) {
mincost[e.to] = dual_cost;
prevv[e.to] = v;
preve[e.to] = i;
que.push(Pi(mincost[e.to], e.to));
}
}
}
if (mincost[sink] == INF) return -1;
for (long long v = 0; v < (long long)graph.size(); v++)
potential[v] += mincost[v];
long long d = f;
for (long long v = sink; v != source; v = prevv[v])
d = min(d, graph[prevv[v]][preve[v]].capacity);
f -= d;
ret += d * potential[sink];
for (long long v = sink; v != source; v = prevv[v]) {
edge& e = graph[prevv[v]][preve[v]];
e.capacity -= d;
graph[v][e.rev].capacity += d;
}
}
return ret;
}
};
signed main() {
long long N, K;
cin >> N >> K;
vector<long long> A(N), B(N);
for (long long i = 0; i < (N); i++) cin >> A[i];
for (long long i = 0; i < (N); i++) cin >> B[i];
PrimalDual mcf(5000);
long long src = 4998, snk = 4999;
long long INF = 1001001001;
for (long long i = 0; i < (N); i++) {
mcf.add_edge(src, i, 1, A[i]);
mcf.add_edge(i, i + N, INF, 0);
mcf.add_edge(i + N, snk, 1, B[i]);
if (i + 1 != N) mcf.add_edge(i + N, i + 1 + N, INF, 0);
}
cout << mcf.min_cost_flow(src, snk, K) << endl;
return 0;
}
| 21 | CPP |
#include <bits/stdc++.h>
int main() {
int i, j, n, t, k, a[110], b[110], x;
while (scanf("%d", &n) != EOF) {
for (i = 0, x = 1, t = 1, k = 1, j = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] == 0)
b[j++] = 0;
else if (((a[i] % 10) == 0) && t && a[i] != 100) {
t = 0;
b[j++] = a[i];
} else if (a[i] == 100 && x) {
x = 0;
b[j++] = a[i];
} else if ((a[i] / 10) == 0 && k) {
k = 0;
b[j++] = a[i];
}
}
if (n == 1 || j == 0) {
printf("1\n%d\n", a[0]);
continue;
}
if (t && k)
for (i = 0; i < n; i++)
if (a[i] > 10 && a[i] < 100 && a[i] % 10 != 0) {
b[j++] = a[i];
break;
}
printf("%d\n%d", j, b[0]);
for (i = 1; i < j; i++) printf(" %d", b[i]);
printf("\n");
}
return 0;
}
| 7 | CPP |
s=input()
a=""
for i in range(len(s)):
if i==0:
a+=s[i].upper()
else:
a+=s[i]
print(a) | 7 | PYTHON3 |
# your code goes here
# your code goes here
n,m=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
f=0
if l1[0]==0:
print("NO")
elif l1[m-1]==1:
print("YES")
elif l2[m-1]==0:
print("NO")
else:
for i in range(m-1,n):
f=2
if l1[i]==1 and l2[i]==1:
print("YES")
f=1
break
if f==2:
print("NO") | 7 | PYTHON3 |
n = input()
ans = 1000;
ln = len(n)
for i in range(1 , 2 ** ln):
bn = str(bin(i))[2:]
s = ''
for j in range(len(bn)):
if(bn[j] == '1'):
s += n[j + (len(n) - len(bn))]
if(s[0] != '0'):
s = int(s)
sqr = int(s ** .5)
if(sqr * sqr == s and i != 0):
ans = min(ans , len(n) - bn.count('1'))
if(ans == 1000):
print(-1)
else:
print(ans)
| 9 | PYTHON3 |
for t in range(int(input())):
a,b,c,d=map(int,input().split())
if a>b and d>=c:
print(-1)
else:
if b>=a:
print(b)
else:
if (a-b)%(c-d)==0:
x=(a-b)//(c-d)
else:
x=(a-b)//(c-d)+1
print(b+x*c) | 7 | PYTHON3 |
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
m=1000000
visited=[0]*n
for i in range(n-1,-1,-1):
m=min(m,i-a[i])
if(i>m):
visited[i]=1
print(*visited) | 8 | PYTHON3 |
s = input()
vowels = ['a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y']
answer = ''
for i in s:
if i not in vowels:
answer += '.' + i.lower()
print(answer) | 7 | PYTHON3 |
from sys import stdin
n = int(stdin.readline())
heights = []
nums = [int(x) for x in stdin.readline().split()]
heights.append(max(nums))
for i in range(n-1):
nums = [int(x) for x in stdin.readline().split()]
if max(nums) <= heights[i]:
heights.append(max(nums))
elif min(nums) <= heights[i]:
heights.append(min(nums))
else:
print("NO")
quit()
print("YES")
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long modd = 1000 * 1000 * 1000 + 7;
int main() {
long long a, b;
long long sum = 0, s, s1;
cin >> a >> b;
for (int i = (1); i < (a + 1); i++) {
s = (i * b + 1) % modd;
s1 = ((b * (b - 1)) / 2) % modd;
s = (s1 * s) % modd;
sum += s;
sum %= modd;
}
cout << sum << endl;
return 0;
}
| 7 | CPP |
S=input()
N=len(S)
mod=998244353
red=[0]*N
blue=[0]*N
for i in range(N):
if S[i]=="0":
red[i]+=2
elif S[i]=="1":
red[i]+=1
blue[i]+=1
else:
blue[i]+=2
if i!=0:
red[i]+=red[i-1]
blue[i]+=blue[i-1]
R=red[-1]
B=blue[-1]
dp=[[0 for i in range(2*N+1)] for i in range(2*N+1)]
dp[2*N][R]=1
for i in range(2*N-1,-1,-1):
for j in range(2*N+1):
if i>=N:
r=R-j
b=B-(i-j)
if r and 2*N>=j+1:
dp[i][j]+=dp[i+1][j+1]
dp[i][j]%=mod
if b:
dp[i][j]+=dp[i+1][j]
dp[i][j]%=mod
else:
r=red[i]-j
b=blue[i]-(i-j)
if r and 2*N>=j+1:
dp[i][j]+=dp[i+1][j+1]
dp[i][j]%=mod
if b:
dp[i][j]+=dp[i+1][j]
dp[i][j]%=mod
print(dp[0][0]) | 0 | PYTHON3 |
print('TGCA'['ACGT'.index(input())])
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int nb, ns, nc;
int pb, ps, pc;
int bo{}, so{}, co{};
long long int price_for(long long int r) {
long long int answer{};
answer = (max(0ll, bo * r - nb) * pb) + (max(0ll, so * r - ns) * ps) +
(max(0ll, co * r - nc) * pc);
return answer;
}
int main() {
string s;
cin >> s;
cin >> nb >> ns >> nc;
cin >> pb >> ps >> pc;
long long int r;
cin >> r;
for (auto chars : s) {
if (chars == 'B') {
++bo;
} else if (chars == 'S') {
++so;
} else {
++co;
}
}
long long int answer{}, f_a;
long long int l{}, right{1000000000101};
while (l <= right) {
answer = l + (right - l) / 2;
f_a = price_for(answer);
if (f_a == r) {
cout << answer << endl;
return 0;
} else if (f_a > r) {
right = answer - 1;
} else {
l = answer + 1;
}
}
cout << l - 1 << endl;
return 0;
}
| 9 | CPP |
a,b=map(int, input().split())
if a < b:
op = '<'
elif a > b:
op = '>'
else:
op = '=='
print('a', op, 'b') | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x, y, z, k;
while (scanf("%I64d%I64d%I64d", &n, &x, &y) != EOF) {
if (y < n) {
printf("-1\n");
} else {
z = y - n + 1;
k = z * z;
if (k + n - 1 >= x) {
printf("%I64d\n", z);
for (int i = 1; i < n; i++) printf("1\n");
} else {
printf("-1\n");
}
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long int INF = (1LL << 58);
const bool DEBUG = 0;
const int MAX_LIMIT = 1000006;
int rows, cols;
std::vector<vector<pair<int, int> > > row, col;
std::vector<int> adj[MAX_LIMIT];
std::vector<vector<int> > mat, idMatrix;
int val[MAX_LIMIT];
void dfs(int node) {
int i, limit = adj[node].size();
if (limit == 0) val[node] = 1;
for (i = 0; i < limit; i++) {
int v = adj[node][i];
if (!val[v]) dfs(v);
val[node] = max(val[node], val[v] + 1);
}
return;
}
const int NODES_LIMIT = 1000006;
stack<int> st;
int nodes;
bool vis[NODES_LIMIT];
void topological_sort(int node) {
if (vis[node]) return;
vis[node] = 1;
int limit = adj[node].size();
int i;
for (i = 0; i < limit; i++) topological_sort(adj[node][i]);
st.push(node);
}
void drive_topological_sort() {
int i;
for (i = 1; i < nodes + 1; i++)
if (!vis[i]) topological_sort(i);
while (!st.empty()) {
int ele = st.top();
st.pop();
dfs(ele);
}
return;
}
const int ITEMS_NO = 1000006;
int par[ITEMS_NO], size[ITEMS_NO];
void initDSU(int noOfItems) {
for (int i = 0; i < noOfItems; i++) {
par[i] = i;
size[i] = 1;
}
}
int getRoot(int x) {
if (par[x] == x) {
return x;
}
return par[x] = getRoot(par[x]);
}
void merge(int x, int y) {
int root1 = getRoot(x);
int root2 = getRoot(y);
if (root1 == root2) {
return;
}
if (size[root1] < size[root2]) {
swap(root1, root2);
}
size[root1] += size[root2];
par[root2] = root1;
}
map<int, int> rowPresence[MAX_LIMIT], colPresence[MAX_LIMIT];
int idMapping[MAX_LIMIT];
int getCellId(int x, int y) { return (x * cols + y) + 1; }
int main() {
int i, j;
scanf("%d", &rows);
scanf("%d", &cols);
initDSU(rows * cols + 2);
row.resize(rows);
col.resize(cols);
mat.resize(rows);
idMatrix.resize(rows);
for (i = 0; i < rows; i++) {
idMatrix[i].resize(cols);
mat[i].resize(cols);
}
int id = 1;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
int temp;
scanf("%d", &temp);
mat[i][j] = temp;
rowPresence[i][temp] = getCellId(i, j);
colPresence[j][temp] = getCellId(i, j);
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
int x = mat[i][j];
int thisCellId = getCellId(i, j);
int toMergeWith = rowPresence[i][x];
merge(thisCellId, toMergeWith);
toMergeWith = colPresence[j][x];
merge(thisCellId, toMergeWith);
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
int x = mat[i][j];
int thisCellId = getCellId(i, j);
if (thisCellId == getRoot(thisCellId)) idMapping[thisCellId] = id++;
}
}
for (i = 0; i < rows; i++) rowPresence[i].clear();
for (j = 0; j < cols; j++) colPresence[j].clear();
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
int x = mat[i][j];
int thisCellId = getCellId(i, j);
idMatrix[i][j] = idMapping[getRoot(getCellId(i, j))];
row[i].push_back(make_pair(mat[i][j], idMatrix[i][j]));
col[j].push_back(make_pair(mat[i][j], idMatrix[i][j]));
}
}
mat.clear();
for (i = 0; i < rows; i++) {
sort(row[i].begin(), row[i].end());
for (j = 0; j < cols - 1; j++) {
int u = row[i][j + 1].second;
int v = row[i][j].second;
if (u != v) adj[u].push_back(v);
}
}
row.clear();
for (j = 0; j < cols; j++) {
sort(col[j].begin(), col[j].end());
for (i = 0; i < rows - 1; i++) {
int u = col[j][i + 1].second;
int v = col[j][i].second;
if (u != v) adj[u].push_back(v);
}
}
col.clear();
nodes = id - 1;
drive_topological_sort();
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) printf("%d ", val[idMatrix[i][j]]);
printf("\n");
}
return 0;
}
| 9 | CPP |
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
char dd[] = {'N', 'E', 'S', 'W'};
typedef vector<ll> vl;
typedef vector<vl> vvl;
int main() {
ll H, W, L;
while(cin >> H >> W >> L, H) {
vs field(H);
REP(i, H) {
cin >> field[i];
}
int cx, cy, cd;
REP(i, H) {
REP(j, W) {
if(field[i][j] != '.' && field[i][j] != '#') {
cx = j;
cy = i;
switch(field[i][j]) {
case 'N':
cd = 0;
break;
case 'E':
cd = 1;
break;
case 'S':
cd = 2;
break;
case 'W':
cd = 3;
break;
}
break;
}
}
}
vector<vvl> turn(H, vvl(W, vl(4)));
bool looped = false;
for(ll t = 0; t < L; t++) {
if(!looped && turn[cy][cx][cd] != 0) {
looped = true;
ll cycle = (t-turn[cy][cx][cd]);
t += ((L-t)/cycle)*cycle;
if(t == L) {
t -= cycle;
}
}
int nx = cx+dx[cd];
int ny = cy+dy[cd];
if(ny<0 || H<=ny || nx<0 || W<=nx || field[ny][nx]=='#') {
cd = (cd+1)%4;
t--;
continue;
}
turn[cy][cx][cd] = t;
cy = ny;
cx = nx;
}
cout << cy+1 << " " << cx+1 << " " << dd[cd] << endl;
}
return 0;
} | 0 | CPP |
n, p = map(int, input().split())
t = [ord(c) - 97 for c in input()] + [27, 27]
for k in range(n - 1, -1, -1):
for i in range(t[k] + 1, p):
if i - t[k - 1] and i - t[k - 2]:
a, b = min(t[k - 1], 2), min(i, 2)
if a == b: a = 1
t = t[: k] + [i] + [3 - a - b, a, b] * (n // 3 + 1)
print(''.join(chr(i + 97) for i in t)[: n])
exit(0)
print('NO') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int buffer_size = 1e5 + 5;
char buf[buffer_size], *S, *T;
bool flag_EOF;
inline char read_char() {
if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin);
return S != T ? *(S++) : EOF;
}
inline int read_int() {
bool flag = false;
char c = read_char();
while (c < '0' || c > '9') {
if (c == EOF) {
flag_EOF = true;
return 0;
}
flag = (c == '-' ? true : flag);
c = read_char();
}
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c ^ 48);
c = read_char();
}
return flag ? -x : x;
}
const int max_cnt = 1e3 + 5;
int l[max_cnt], p[max_cnt];
bool mark[max_cnt];
vector<int> a[2], b[2];
const int max_size = 5e5 + 5;
bitset<max_size> f[2];
bool dp[max_size];
int val[max_size];
inline int cmp1(int x, int y) { return abs(x) < abs(y); }
inline int cmp2(int x, int y) { return abs(x) > abs(y); }
int main() {
int t;
scanf("%d", &t);
while (t--) {
int h, v;
scanf("%d", &h);
for (int i = 1; i <= h; ++i) scanf("%d", l + i);
scanf("%d", &v);
for (int i = 1; i <= v; ++i) scanf("%d", p + i);
if (h != v) {
printf("No\n");
continue;
}
int sum1 = 0, sum2 = 0;
for (int i = 1; i <= h; ++i) sum1 += l[i];
for (int i = 1; i <= v; ++i) sum2 += p[i];
if ((sum1 & 1) || (sum2 & 1)) {
printf("No\n");
continue;
}
random_shuffle(l + 1, l + h + 1);
for (int i = 0; i <= sum1; ++i) f[0][i] = 0;
f[0][0] = 1;
int pos1 = -1;
for (int i = 1; i <= h; ++i) {
int x = (i & 1) ^ 1, y = i & 1;
f[y] = f[x] | (f[x] << l[i]);
if (f[y][sum1 >> 1]) {
pos1 = i;
break;
}
}
if (pos1 == -1) {
printf("No\n");
continue;
}
random_shuffle(p + 1, p + v + 1);
for (int i = 0; i <= sum2; ++i) f[0][i] = 0;
f[0][0] = 1;
int pos2 = -1;
for (int i = 1; i <= v; ++i) {
int x = (i & 1) ^ 1, y = i & 1;
f[y] = f[x] | (f[x] << p[i]);
if (f[y][sum2 >> 1]) {
pos2 = i;
break;
}
}
if (pos2 == -1) {
printf("No\n");
continue;
}
a[0].clear(), a[1].clear();
int tot1 = sum1 >> 1;
for (int i = 1; i <= h; ++i) mark[i] = false;
for (int i = pos1 + 1; i <= h; ++i) {
a[0].push_back(l[i]);
mark[i] = true;
tot1 -= l[i];
}
for (int i = 0; i <= tot1; ++i) dp[i] = false;
dp[0] = true;
for (int i = 1; i <= pos1; ++i)
for (int j = tot1 - l[i]; j >= 0; --j)
if (dp[j] && !dp[j + l[i]]) {
dp[j + l[i]] = true;
val[j + l[i]] = i;
}
int now = tot1;
while (now) {
a[0].push_back(l[val[now]]);
mark[val[now]] = true;
now -= l[val[now]];
}
int cnt1 = 0;
for (int i = 1; i <= h; ++i)
if (!mark[i]) {
a[1].push_back(l[i]);
++cnt1;
}
b[0].clear(), b[1].clear();
int tot2 = sum2 >> 1;
for (int i = 1; i <= v; ++i) mark[i] = false;
for (int i = pos2 + 1; i <= v; ++i) {
b[0].push_back(p[i]);
mark[i] = true;
tot2 -= p[i];
}
for (int i = 0; i <= tot2; ++i) dp[i] = false;
dp[0] = true;
for (int i = 1; i <= pos2; ++i)
for (int j = tot2 - p[i]; j >= 0; --j)
if (dp[j] && !dp[j + p[i]]) {
dp[j + p[i]] = true;
val[j + p[i]] = i;
}
now = tot2;
while (now) {
b[0].push_back(p[val[now]]);
mark[val[now]] = true;
now -= p[val[now]];
}
int cnt2 = 0;
for (int i = 1; i <= v; ++i)
if (!mark[i]) {
b[1].push_back(p[i]);
++cnt2;
}
printf("Yes\n");
int x = 0, y = 0;
if (a[0].size() < a[1].size()) swap(a[0], a[1]);
if (b[0].size() > b[1].size()) swap(b[0], b[1]);
sort(a[1].begin(), a[1].end());
sort(b[1].begin(), b[1].end(), greater<int>());
while (a[1].size()) {
x += a[1].back();
printf("%d %d\n", x, y);
y += b[1].back();
printf("%d %d\n", x, y);
a[1].pop_back();
b[1].pop_back();
}
while (b[1].size()) {
x -= a[0].back();
printf("%d %d\n", x, y);
y += b[1].back();
printf("%d %d\n", x, y);
a[0].pop_back();
b[1].pop_back();
}
sort(a[0].begin(), a[0].end());
sort(b[0].begin(), b[0].end(), greater<int>());
while (b[0].size()) {
x -= a[0].back();
printf("%d %d\n", x, y);
y -= b[0].back();
printf("%d %d\n", x, y);
a[0].pop_back();
b[0].pop_back();
}
}
return 0;
}
| 10 | CPP |
a = input()
k = 0
for i in range(1,len(a)):
if a[i-k] == "/" and a[i-1-k] == "/":
a = a[:i-k] + a[i-k+1:]
k += 1
if a[-1] == "/" and len(a) > 1:
a = a[:-1]
print(a) | 7 | PYTHON3 |
n = int(input())
l = ''
for i in range(n):
l += input()
print(l.count('00')+l.count('11')+1)
| 7 | PYTHON3 |
from math import gcd
from functools import reduce
n, k = [int(aa) for aa in input().split()]
a = [int(aa) for aa in input().split()]
g = reduce(gcd, a)%k
answ = [0] * k
pos = g
count = 0
while answ[pos]==0:
answ[pos]=1
pos = (pos+g)%k
count+=1
print(count)
for i in range(len(answ)):
if answ[i]==1:
print(i, end=' ')
| 9 | PYTHON3 |
s = input()
numbers = [0, 0, 0]
for c in s:
if c == '1':
numbers[0] += 1
elif c == '2':
numbers[1] += 1
elif c == '3':
numbers[2] += 1
ones = '+'.join('1' for _ in range(numbers[0]))
twos = '+'.join('2' for _ in range(numbers[1]))
threes = '+'.join('3' for _ in range(numbers[2]))
word = ones
if word and twos:
word = word + '+' + twos
elif not word and twos:
word = twos
if word and threes:
word = word + '+' + threes
elif not word and threes:
word = threes
print(word) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 0;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = 1;
ch = getchar();
}
while (ch <= '9' && ch >= '0') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return f ? -x : x;
}
int gcd(int x, int y) {
if (!x) return y;
if (!y) return x;
return gcd(y, x % y);
}
int a[100020], sum, ans;
int main(int argc, char const *argv[]) {
int n = read();
for (int i = 1; i <= n; i++) sum += (a[i] = read()) & 1;
int gg = a[1];
for (int i = 2; i <= n; i++) gg = gcd(gg, a[i]);
if (gg > 1) return puts("YES\n0") & 0;
for (int i = 1; i <= n - 1; i++)
if ((a[i] & 1) && (a[i + 1] & 1)) {
a[i] ^= 1;
a[i + 1] ^= 1;
sum -= 2, ans++;
}
printf("YES\n%d\n", ans + sum * 2);
return 0;
}
| 9 | CPP |
t = int(input())
for _ in range(t):
x = input()
if len(x)<=10:
print(x)
else:
print(x[0] + str(len(x)-2) + x[-1]) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> v;
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
v.push_back(a);
}
multiset<long long> unused, minus, plus;
long long profit = 0;
for (long long x : v) {
long long a = *(plus.begin());
long long c = *(unused.begin());
if (unused.empty() == false && x > c) {
if (plus.empty() == true || (c < a)) {
plus.insert(x);
minus.insert(c);
unused.erase(unused.begin());
profit += (x - c);
continue;
}
}
if (plus.empty() == false && x > a) {
plus.erase(plus.begin());
plus.insert(x);
unused.insert(a);
profit += (x - a);
continue;
}
unused.insert(x);
}
cout << profit << "\n";
}
| 11 | CPP |
def test_1():
# A - World Cup
import math
n = list(map(int, input().split(" ")))
s = input().strip().split()
a = list(map(set, s))
a = list(map(sorted, a))
b = list(map(''.join, a))
print(len(set(b)))
test_1()
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, c1, c2, t;
cin >> t;
while (t--) {
cin >> n;
c1 = n / 3;
c2 = c1;
if (n % 3 == 1)
c1++;
else if (n % 3 == 2)
c2++;
cout << c1 << " " << c2 << "\n";
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int read() {
char c = getchar();
int d = 0, f = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; d = d * 10 + c - 48, c = getchar())
;
return d * f;
}
const int N = 500010;
int n, m, k, dep[N], fa[N], stk[N], ch[N][2];
bool mk[N];
vector<int> e[N];
void add(int x, int y) {
e[x].push_back(y);
e[y].push_back(x);
}
void dfs(int f, int x) {
stk[dep[x]] = x;
fa[x] = f;
if (dep[x] >= (n - 1) / k + 1) {
puts("PATH");
printf("%d\n", dep[x]);
for (int i = 1; i <= dep[x]; ++i) printf("%d ", stk[i]);
puts("");
exit(0);
}
bool fg = 0;
for (int i = 0; i <= ((int)e[x].size()) - 1; ++i) {
int y = e[x][i];
if (y == f) continue;
if (dep[y]) {
if (!ch[x][0])
ch[x][0] = y;
else if (!ch[x][1])
ch[x][1] = y;
continue;
}
fg = 1;
dep[y] = dep[x] + 1;
dfs(x, y);
}
mk[x] = fg;
}
void output(int x, int y) {
for (; x != y; x = fa[x]) printf("%d ", x);
printf("%d ", y);
}
int main() {
n = read(), m = read(), k = read();
for (int i = 1; i <= m; ++i) add(read(), read());
dep[1] = 1;
dfs(0, 1);
puts("CYCLES");
int t = 0;
for (int i = 1; i <= n; ++i)
if (mk[i] == 0) {
t++;
if (t > k) break;
int x = ch[i][0], y = ch[i][1];
if ((dep[i] - dep[x] + 1) % 3) {
printf("%d\n", dep[i] - dep[x] + 1);
output(i, x);
} else if ((dep[i] - dep[y] + 1) % 3) {
printf("%d\n", dep[i] - dep[y] + 1);
output(i, y);
} else {
if (dep[x] < dep[y]) swap(x, y);
printf("%d\n", dep[x] - dep[y] + 2);
output(x, y);
printf("%d ", i);
}
puts("");
}
}
| 11 | CPP |
from sys import stdin,setrecursionlimit
def arr_inp(n):
if n == 1:
return [int(x) for x in stdin.readline().split()]
elif n == 2:
return [float(x) for x in stdin.readline().split()]
else:
return list(stdin.readline()[:-1])
setrecursionlimit(10000)
def dp(i, val):
if i == n:
return 0
if mem[i][val] != -1:
return mem[i][val]
v1, v2 = (a[i] + val) % h, (a[i] - 1 + val) % h
mem[i][val] = max((v1 >= l and v1 <= r) + dp(i + 1, v1), (v2 >= l and v2 <= r) + dp(i + 1, v2))
return mem[i][val]
n, h, l, r = arr_inp(1)
a, ans = arr_inp(1), 0
mem = [[-1 for i in range(h + 1)] for j in range(n + 2)]
# mem[0][0], ans = 0, 0
dp(0, 0)
print(mem[0][0])
# for i in range(n):
# for j in range(h):
# if mem[i][j] != -1:
# val, val2 = (a[i] + j) % h, (a[i] - 1 + j) % h
# mem[i + 1][val] = max(mem[i + 1][val], mem[i][j] + (val >= l and val <= r))
# mem[i + 1][val2] = max(mem[i + 1][val2], mem[i][j] + (val2 >= l and val2 <= r))
# ans = max(ans, mem[i + 1][val], mem[i + 1][val2])
# print(ans)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int N;
vector<vector<int>> G;
vector<int> B, W;
void recur(int n, int prev)
{
long long b = 1, w = 1;
for (int k : G[n]) {
if (k == prev) continue;
recur(k, n);
b = b * W[k] % MOD;
w = w * (W[k] + B[k]) % MOD;
}
B[n] = b;
W[n] = w;
}
int main()
{
cin >> N;
G.resize(N);
B.resize(N);
W.resize(N);
for (int i = 0; i < N - 1; i++) {
int x, y; cin >> x >> y; x--, y--;
G[x].push_back(y);
G[y].push_back(x);
}
recur(0, -1);
cout << (B[0] + W[0]) % MOD << endl;
return 0;
} | 0 | CPP |
# coding: utf-8
n = int(input())
d = {}
for i in range(n):
temp = input()
if temp in d.keys():
print(temp+str(d[temp]))
d[temp] += 1
else:
print('OK')
d[temp] = 1
| 9 | PYTHON3 |
k, A, h = map(int, input().split());team = 1 << k;mod = 998244353;POW = [1]
for i in range(100):POW.append(POW[-1]*A%mod)
ANS = [(1 << (k - 1)) + 1] * (team // 2);X = tuple([i for i in range(team // 2)]);ALLLIST = []
def half_all(ANS, X, Y, i):
if i == 0:ALLLIST.append(tuple(ANS));return ANS
if X == tuple():X = Y;Y = tuple();half_all(ANS, X, Y, i - 1);return
X = list(X);ANS = list(ANS);p = X.pop();q = X.pop()
ANS[p] = ANS[p] // 2 + 1;half_all(tuple(ANS), tuple(X), Y + (p,), i);ANS[p] = ANS[p] * 2 - 1;ANS[q] = ANS[q] // 2 + 1;half_all(tuple(ANS), tuple(X), Y + (q,), i)
half_all(ANS,X,tuple(),k-1)
SCORE01=[0]*len(ALLLIST)
SCORE02=[0]*len(ALLLIST)
SCORE11=[0]*len(ALLLIST)
SCORE12=[0]*len(ALLLIST)
for i in range(len(ALLLIST)):
LIST=ALLLIST[i]
sc01=sc02=sc11=sc12=0
#print(LIST)
for j in range(team//2):
#print(sc01,sc02,sc11,sc12)
if LIST[j]!=2:
sc01+=(j+1)*POW[LIST[j]]%mod
sc02+=(j+1)*POW[LIST[j]]%mod
sc01%=mod
sc02%=mod
sc11+=(j+1+team//2)*POW[LIST[j]]%mod
sc12+=(j+1+team//2)*POW[LIST[j]]%mod
sc11%=mod
sc12%=mod
else:
sc01+=(j+1)*POW[1]%mod
sc02+=(j+1)*POW[2]%mod
sc01%=mod
sc02%=mod
sc11+=(j+1+team//2)*POW[1]%mod
sc12+=(j+1+team//2)*POW[2]%mod
sc11%=mod
sc12%=mod
#print(sc01,sc02,sc11,sc12)
SCORE01[i]=sc01
SCORE02[i]=sc02
SCORE11[i]=sc11
SCORE12[i]=sc12
SCSET11=set(SCORE11)
SCSET12=set(SCORE12)
for i in range(len(ALLLIST)):
if (h-SCORE01[i])%mod in SCSET12:
k=SCORE12.index((h-SCORE01[i])%mod)
#print(i,k)
LANS=list(ALLLIST[i]+ALLLIST[k])
for j in range(len(LANS)):
if LANS[j]==2:
LANS[j]=1
break
print(*LANS)
break
if (h-SCORE02[i])%mod in SCSET11:
k=SCORE11.index((h-SCORE02[i])%mod)
#print(i,k)
LANS=list(ALLLIST[i]+ALLLIST[k])
for j in range(len(LANS)-1,-1,-1):
if LANS[j]==2:
LANS[j]=1
break
print(*LANS)
break
else:
print(-1) | 11 | PYTHON3 |
ll=lambda:map(int,input().split())
t=lambda:int(input())
ss=lambda:input()
#from math import log10 ,log2,ceil,factorial as f,gcd
#from itertools import combinations_with_replacement as cs
#from functools import reduce
#from math import gcd
n,c,d=map(int,input().split())
a,b=max(c,d),min(c,d)
c=a
d=b
for ls in map(len,input().split('*')):
if a and ls:
a-=min(max(1,ls//2),a)
if b and ls>1:
b-=min(max(ls//2,1),b)
#print(a,b,ls)
if ls>1 and ls%2:
if a>=b and a and c:
a-=1
#print(" ",b)
if not a and not b:
break
a,b=max(a,b),min(a,b)
print(c-a+d-b) | 8 | PYTHON3 |
a, b = map(int, input().split())
print((a // 2 * b) + (a % 2 * b // 2)) | 7 | PYTHON3 |
n=input()
m=input()
p=m.split()
f=[int(a)for a in p]
p=f
p.sort()
a=''
for i in p:
a=a+str(i)+' '
a.rstrip()
print(a)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
double r, x1, y1, x2, y2;
cin >> r >> x1 >> y1 >> x2 >> y2;
double d = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
double turns = d / (2 * r);
cout << ceil(turns);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline bool SR(int &x) { return scanf("%d", &x) == 1; }
inline bool SR(long long &x) { return scanf("%lld", &x) == 1; }
inline bool SR(double &x) { return scanf("%lf", &x) == 1; }
inline bool SR(char *s) { return scanf("%s", s) == 1; }
inline bool RI() { return true; }
template <typename I, typename... T>
inline bool RI(I &x, T &...tail) {
return SR(x) && RI(tail...);
}
inline void SP(const int x) { printf("%d", x); }
inline void SP(const long long x) { printf("%lld", x); }
inline void SP(const double x) { printf("%.16lf", x); }
inline void SP(const char *s) { printf("%s", s); }
inline void PL() { puts(""); }
template <typename I, typename... T>
inline void PL(const I x, const T... tail) {
SP(x);
if (sizeof...(tail)) putchar(' ');
PL(tail...);
}
template <typename I>
void _DOING(const char *s, I &&x) {
cerr << s << " = " << x << endl;
}
template <typename I, typename... T>
void _DOING(const char *s, I &&x, T &&...tail) {
int c = 0;
while (*s != ',' || c != 0) {
if (*s == '(' || *s == '[' || *s == '{') c++;
if (*s == ')' || *s == ']' || *s == '}') c--;
cerr << *s++;
}
cerr << " = " << x << " , ";
_DOING(s + 1, tail...);
}
inline int RAND() {
static int x = 880301;
return (x = x * 0xdefaced + 1) & 0x7fffffff;
}
const int MAX_N = 52 + 10;
const int INF = 0x3f3f3f3f;
const int MAX_M = 2000000 + 10;
int n;
int slack[MAX_N];
int mx[MAX_N], my[MAX_N];
bool vis_x[MAX_N], vis_y[MAX_N];
int lx[MAX_N], ly[MAX_N], w[MAX_N][MAX_N];
bool dfs(int x) {
vis_x[x] = 1;
for (int y = 0; y < int(n); y++) {
if (vis_y[y]) continue;
int t = lx[x] + ly[y] - w[x][y];
if (t == 0) {
vis_y[y] = 1;
if (my[y] == -1 || dfs(my[y])) {
mx[x] = y, my[y] = x;
return 1;
}
} else
slack[y] = min(slack[y], t);
}
return 0;
}
inline void modify() {
int t = INF;
for (int i = 0; i < int(n); i++)
if (!vis_y[i]) t = min(t, slack[i]);
for (int i = 0; i < int(n); i++) {
if (vis_x[i]) lx[i] -= t;
if (vis_y[i]) ly[i] += t;
}
}
inline int km() {
memset(mx, (-1), sizeof(mx));
memset(my, (-1), sizeof(my));
for (int i = 0; i < int(n); i++) {
lx[i] = 0, ly[i] = 0;
for (int j = 0; j < int(n); j++) lx[i] = max(lx[i], w[i][j]);
}
for (int i = 0; i < int(n); i++) {
while (1) {
memset(vis_x, (0), sizeof(vis_x));
memset(vis_y, (0), sizeof(vis_y));
memset(slack, (0x3f), sizeof(slack));
if (dfs(i))
break;
else
modify();
}
}
int res = 0;
for (int i = 0; i < int(n); i++) res += w[i][mx[i]];
return res;
}
char s1[MAX_M], s2[MAX_M];
inline int to_id(char c) {
if ('a' <= c && c <= 'z')
return c - 'a';
else
return c - 'A' + 26;
}
inline char to_char(int id) {
if (id < 26)
return 'a' + id;
else
return id - 26 + 'A';
}
int main() {
int m;
RI(m, n);
RI(s1, s2);
for (int i = 0; i < int(m); i++) w[to_id(s1[i])][to_id(s2[i])]++;
PL(km());
for (int i = 0; i < int(n); i++) putchar(to_char(mx[i]));
PL();
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n;
string str[maxn];
long long cal(string a) {
int len = a.length();
long long ans = 0, num = 0;
for (int i = 0; i < len; i++) {
if (a[i] == 's') {
num++;
} else if (a[i] == 'h') {
ans += num;
}
}
return ans;
}
bool cmp(string a, string b) {
string temp1 = "", temp2 = "";
temp1 += a, temp1 += b;
temp2 += b, temp2 += a;
return cal(temp1) > cal(temp2);
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> str[i];
}
sort(str, str + n, cmp);
string endStr = "";
for (int i = 0; i < n; i++) {
endStr += str[i];
}
cout << cal(endStr) << endl;
return 0;
}
| 10 | CPP |
#include<cstdio>
#include<algorithm>
#define int long long
using std::max;
const int mod=1000000007;
int n,m,s[3010],right[3010];
int x,y,f[3010][3010];
char S[3010];
signed main(){
scanf("%lld%lld",&n,&m);
scanf("%s",S+1);
for(int i=1;i<=n;i++)
s[i]=S[i]-'0';
for(int i=1;i<=n+1;i++)
s[i]+=s[i-1];
for(int i=1;i<=n+1;i++)
right[i]=i;
for(int i=1;i<=m;i++){
scanf("%lld%lld",&x,&y);
right[x]=max(right[x],y);
}
for(int i=1;i<=n;i++)
right[i]=max(right[i],right[i-1]);
f[1][s[right[1]]]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<=n;j++)
if(f[i][j]){
int l=right[i]+1,r=right[i+1];
int bo1=j,bo2=right[i]-i+1-j;
if(bo2)(f[i+1][j+s[r]-s[l-1]]+=f[i][j])%=mod;
if(bo1)(f[i+1][j-1+s[r]-s[l-1]]+=f[i][j])%=mod;
}
printf("%lld\n",f[n+1][0]);
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int chkpower2(int x) { return (x && !(x & x - 1)); }
long long int n, k;
bool chk(long long int x) {
long long int sum = 0, k1 = 1;
while (n / k1 > 0) {
sum += x / k1;
if (sum >= n) {
return true;
}
k1 = k1 * k;
}
if (sum >= n) {
return true;
} else {
return false;
}
}
void solve() {
cin >> n >> k;
long long int low = 1, high = n;
while (high != low) {
long long int mid = (high + low + 1) / 2;
if (chk(mid)) {
high = mid;
} else {
low = mid;
}
if (high - low == 1 && chk(mid)) {
break;
}
}
cout << high;
return;
}
int main() {
int t;
t = 1;
while (t--) {
solve();
cout << "\n";
}
return 0;
}
| 8 | CPP |
import sys
import math
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_list(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip()
for t in range(int(input())):
n=int(input())
arr=get_list()
ans=[0]*n
for i in range(n):
multi=2
ans[n-i-1]=1
while (n-i)*multi - 1<n:
if arr[n-i-1]<arr[(n-i)*multi-1]:
ans[n-i-1]=max(ans[n-i-1],1+ans[(n-i)*multi-1])
multi+=1
print(max(ans)) | 8 | PYTHON3 |
# n = int(input())
# for i in range(n):
# data = input()
# if(len(data)>10):
# s=""
# s=s+data[0]
# s=s+str(len(data[1:len(data)-1]))
# s=s+data[len(data)-1]
# print(s)
# else:
# print(data)
# #Watermelon
# n=int(input())
# if n>3:
# if n%2==0:
# print("YES")
# else: print("NO")
# else:
# print("NO")
# #Bit++
# n=int(input())
# x=0
# while n>0:
# data=input()
# if data=="++X"
#Football
# n=list(map(int,input()))
# c0,c1=0,0
# for i in n:
# if i==0:
# c0+=1
# c1=0
# if c0==7:
# break
# else:
# c1+=1
# c0=0
# if c1==7:
# break
# if c1==7 or c0==7:
# print("YES")
# else: print("NO")
# #Domino Pilings
# m,n=map(int,(input().split()))
# if (m*n)%2==0:
# print((m*n)//2)
# else: print(((m*n)-1)//2)
# #Petya and Strings
# s1=input().lower()
# s2=input().lower()
# c=0
# for i in range(len(s1)):
# if s1[i]>s2[i]:
# print(1)
# c=1
# break
# elif s1[i]<s2[i]:
# print(-1)
# c=1
# break
# if c==0:
# print(0)
#Tram
# t=int(input())
# x=0
# lst=[]
# while t>0:
# a,b=map(int,input().split())
# x=abs(x-a+b)
# lst.append(x)
# t-=1
# print(max(lst))
#Boy or Girl
# lst=input().lower()
# data=list("abcdefghijklmnopqrstuvwxyz")
# dat=[]
# for i in lst:
# if i in data and i not in dat:
# dat.append(i)
# if(len(dat)%2==0):
# print("CHAT WITH HER!")
# else:
# print("IGNORE HIM!")
#Lucky Numbers
# n=int(input())
# if n%4==0 or n%7==0:
# print("YES")
# else:
# lst=list(str(n))
# l1=[]
# for i in lst:
# if i=="4" or i=="7":
# l1.append(i)
# if len(lst)==len(l1):
# print("YES")
# else: print("NO")
# #Capslock
# n=input().lower()
# print(n[0].upper()+n[1:])
#Even Odds
a,b=map(int,input().split())
c=b-(a+1)//2
print([2*b-1,2*c][c>0]) | 7 | PYTHON3 |
k, p = map(int, input().split())
total = 0
for half_zcy in range(1,k+1):
current_zcy = int(str(half_zcy) + str(half_zcy)[::-1])
total += current_zcy
print(total % p)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using ld = long double;
const int mod = (int)1e9 + 9;
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
map<pii, int> who;
vector<pii> co(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
who[{x, y}] = i;
co[i] = {x, y};
}
vi deg(n, 0);
for (int i = 0; i < n; i++) {
for (int d = -1; d < 2; d++) {
if (who.count({co[i].first + d, co[i].second - 1})) {
deg[i]++;
}
}
}
set<int> Q;
for (int i = 0; i < n; i++) {
bool ok = true;
for (int d = -1; d < 2; d++) {
int x = co[i].first + d, y = co[i].second + 1;
ok &= (!who.count({x, y}) || deg[who[{x, y}]] != 1);
}
if (ok) {
Q.insert(i);
}
}
vi res;
for (int i = 0; i < n; i++) {
int j;
if (!(i & 1)) {
j = *--Q.end();
Q.erase(--Q.end());
} else {
j = *Q.begin();
Q.erase(Q.begin());
}
res.push_back(j);
pii t = co[j];
who.erase(t);
for (int d = -1; d < 2; d++) {
pii q = {t.first + d, t.second + 1};
if (who.count(q)) {
int u = who[q];
if (--deg[u] == 1) {
for (int e = -1; e < 2; e++) {
pii r = {q.first + e, q.second - 1};
if (who.count(r)) {
Q.erase(who[r]);
}
}
}
}
}
for (int d = -1; d < 2; d++) {
pii q = {t.first + d, t.second - 1};
if (who.count(q)) {
int u = who[q];
bool ok = true;
for (int e = -1; e < 2 && ok; e++) {
pii r = {q.first + e, q.second + 1};
if (who.count(r) && deg[who[r]] == 1) {
ok = false;
}
}
if (ok) {
Q.insert(u);
}
}
}
}
int ans = 0, pw = 1;
for (int i = (int)((res).size()) - 1; i >= 0; i--) {
ans = (ans + 1LL * pw * res[i]) % mod;
pw = 1LL * n * pw % mod;
}
cout << ans;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int T, n, p[200005];
char s[200005];
vector<int> pos;
inline void getMin() {
for (int i = 1; i <= n; i++) p[i] = 0;
int pt = n;
for (int i = 2; i <= n; i++)
if (s[i] == '<' && s[i + 1] != '<') {
for (int j = i; s[j] == '<'; j--) p[j] = pt--;
}
for (int i = 1; i <= n; i++)
if (!p[i]) p[i] = pt--;
for (int i = 1; i <= n; i++) printf("%d%c", p[i], i == n ? '\n' : ' ');
return;
}
void getMax() {
for (int i = 1; i <= n; i++) p[i] = 0;
pos.clear();
pos.push_back(1);
for (int i = 2; i <= n; ++i)
if (s[i] == '<') pos.push_back(i);
pos.push_back(n + 1);
int pt = n - pos.size() + 2;
for (int i = 1; i < (int)pos.size(); ++i) {
int pt = pos[i];
for (int j = pos[i - 1]; j < pos[i]; ++j) p[j] = --pt;
}
for (int i = 1; i <= n; i++) printf("%d%c", p[i], i == n ? '\n' : ' ');
return;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
scanf("%s", s + 2);
getMin();
getMax();
}
return 0;
}
| 10 | CPP |
n, h = map(int, input().split())
print(n + len([i for i in map(int, input().split()) if i > h]))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
namespace debug {
void __(short x) { cout << x; }
void __(int x) { cout << x; }
void __(long long x) { cout << x; }
void __(unsigned long long x) { cout << x; }
void __(double x) { cout << x; }
void __(long double x) { cout << x; }
void __(char x) { cout << x; }
void __(const char* x) { cout << x; }
void __(const string& x) { cout << x; }
void __(bool x) { cout << (x ? "true" : "false"); }
template <class P1, class P2>
void __(const pair<P1, P2>& x) {
__("("), __(x.first), __(", "), __(x.second), __(")");
}
template <class T>
void __(const vector<T>& x) {
__("{");
bool _ffs = 0;
for (const auto& v : x) __(_ffs ? ", " : ""), __(v), _ffs = 1;
__("}");
}
void screm() { __("\n"); }
template <class T1, class... T2>
void screm(const T1& t1, const T2&... t2) {
__(t1);
if (sizeof...(t2))
__(" | "), screm(t2...);
else
__("\n");
}
void pr() { __("\n"); }
template <class T1, class... T2>
void pr(const T1& t1, const T2&... t2) {
__(t1);
if (sizeof...(t2))
__(" "), pr(t2...);
else
__("\n");
}
} // namespace debug
using namespace debug;
const int MN = 2e5 + 5;
int N, i, x, y, sz[MN], mx[MN], cc[MN], par[MN];
vector<int> adj[MN], c;
pair<int, int> e;
struct op {
int a, b, c;
};
vector<op> ans;
int dfs1(int n, int p) {
sz[n] = 1;
for (auto v : adj[n]) {
if (v == p) continue;
int ss = dfs1(v, n);
sz[n] += ss;
mx[n] = max(mx[n], ss);
}
return sz[n];
}
void dfs2(int n, int p) {
if (2 * mx[n] <= N && 2 * (N - sz[n]) <= N) cc[n] = 1, c.push_back(n);
for (auto v : adj[n]) {
if (v == p) continue;
dfs2(v, n);
}
}
void solve(int n, int p, int r) {
if (p != r) {
ans.push_back({e.first, e.second, n});
e.second = n;
ans.push_back({n, p, r});
}
for (auto v : adj[n])
if (v != p) solve(v, n, r);
}
int main() {
for (scanf("%d", &N), i = 1; i < N; i++) {
scanf("%d%d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs1(1, 0);
dfs2(1, 0);
for (auto cent : c) {
for (auto rt : adj[cent]) {
if (cc[rt]) continue;
e = {cent, rt};
for (auto v : adj[rt])
if (v != cent) solve(v, rt, rt);
ans.push_back({e.first, e.second, rt});
}
}
printf("%d\n", ans.size());
for (auto v : ans) printf("%d %d %d\n", v.a, v.b, v.c);
return 0;
}
| 11 | CPP |
#include <iostream>
#include <algorithm>
using namespace std;
long long memo[101][10][1001];
long long dfs(int i, int j, int k)
{
long long res = 0;
if(j == 0 && k == 0) {
return 1;
} else if(i > 100 || j < 0 || k < 0){
return 0;
} else if(memo[i][j][k] >= 0){
return memo[i][j][k];
} else {
res += dfs(i + 1, j, k);
res += dfs(i + 1, j - 1, k - i);
}
return memo[i][j][k] = res;
}
int main()
{
fill((long long*)memo, (long long*)(memo + 101), -1);
int n, s;
while(cin>> n >> s, n || s){
cout << dfs(0, n, s) << endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXn = 3000;
int c[MAXn + 2][MAXn + 2];
int main() {
int t, delta = 1000 * 1000 * 1000 + 7;
string s;
for (int i = 0; i <= MAXn; i++) c[i][0] = c[i][i] = 1;
for (int i = 1; i <= MAXn; i++)
for (int j = 1; j < i; j++)
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % delta;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> s;
int sum = 0;
for (int j = 0; j < (int)s.size(); j++) sum += s[j] - 'a';
int ans = 0;
for (int j = 0; j <= (int)s.size(); j++) {
long long co = 1;
if (j % 2) co *= -1;
if (sum - j * 26 >= 0)
co = (co *
((long long)c[(int)s.size()][j] *
c[sum - j * 26 + (int)s.size() - 1][(int)s.size() - 1]) %
delta) %
delta;
else
co = 0;
ans = (ans + co) % delta;
if (ans < 0) ans += delta;
}
ans = (ans - 1) % delta;
if (ans < 0) ans += delta;
cout << ans << endl;
}
return 0;
}
| 9 | CPP |
Subsets and Splits