solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
x=list(bin(int(input())))
x.remove('b')
a=[int(i) for i in x]
print(sum(a))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main(){
string S;
cin >> S;
if (S[0]==S[1] || S[1]==S[2] || S[2]==S[3]){
cout << "Bad";
}
else cout << "Good";
}
| 0 | CPP |
n=int(input())
a=list(map(int,input().split()))
m=10**9
for i in range(min(a),max(a)+1):
s=0
for j in a:
s+=(i-j)**2
m=min(m,s)
print(m) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> memo(2e5 + 10, vector<long long>(11, -1));
long long ks(vector<vector<vector<long long>>> &v, long long n, long long c) {
if (n >= v.size()) return 0;
if (memo[n][c] != -1) return memo[n][c];
long long n11, n12, n13, n2, n21, n3;
sort(v[n][1].rbegin(), v[n][1].rend());
sort(v[n][2].rbegin(), v[n][2].rend());
sort(v[n][3].rbegin(), v[n][3].rend());
n11 = n12 = n13 = n2 = n21 = n3 = 0;
if (v[n][1].size() >= 1) {
n11 = v[n][1][0] + ks(v, n + 1, (c + 1) % 10);
if (c + 1 >= 10) n11 += v[n][1][0];
}
if (v[n][1].size() >= 2) {
n12 = v[n][1][0] + v[n][1][1] + ks(v, n + 1, (c + 2) % 10);
if (c + 2 >= 10) n12 += v[n][1][0];
}
if (v[n][1].size() >= 3) {
n13 = v[n][1][0] + v[n][1][1] + v[n][1][2] + ks(v, n + 1, (c + 3) % 10);
if (c + 3 >= 10) n13 += v[n][1][0];
}
if (v[n][2].size() >= 1) {
n2 = v[n][2][0] + ks(v, n + 1, (c + 1) % 10);
if (c + 1 >= 10) n2 += v[n][2][0];
}
if (v[n][2].size() >= 1 && v[n][1].size() >= 1) {
n21 = v[n][2][0] + ks(v, n + 1, (c + 2) % 10) + v[n][1][0];
if (c + 2 >= 10) n21 += max(v[n][2][0], v[n][1][0]);
}
if (v[n][3].size() >= 1) {
n3 = v[n][3][0] + ks(v, n + 1, (c + 1) % 10);
if (c + 1 >= 10) n3 += v[n][3][0];
}
long long no = ks(v, n + 1, c);
memo[n][c] = max(n11, n12);
memo[n][c] = max(memo[n][c], n13);
memo[n][c] = max(memo[n][c], n2);
memo[n][c] = max(memo[n][c], n21);
memo[n][c] = max(memo[n][c], n3);
memo[n][c] = max(memo[n][c], no);
return memo[n][c];
}
signed main() {
long long n;
cin >> n;
vector<vector<vector<long long>>> v(n, vector<vector<long long>>(4));
for (long long i = 0; i < n; i++) {
long long k;
cin >> k;
for (long long j = 0; j < k; j++) {
long long a, b;
cin >> a >> b;
v[i][a].push_back(b);
}
}
cout << ks(v, 0, 0);
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
map<int, vector<int>> m;
vector<bool> c;
bool bb = true;
int a[500000];
vector<int> p, q;
void find(int i, int j) {
if (c[i - 1] && a[i - 1] != j % 2) {
bb = false;
return;
} else if (c[i - 1])
return;
c[i - 1] = true;
a[i - 1] = j % 2;
if (j % 2 == 0)
p.push_back(i);
else
q.push_back(i);
int k;
for (k = 0; k < m[i].size(); k++) {
find(m[i][k], j + 1);
}
}
int main() {
int n;
int k;
cin >> n >> k;
int f, s;
int i, j;
for (i = 0; i < k; i++) {
cin >> f >> s;
m[s].push_back(f);
m[f].push_back(s);
}
for (i = 0; i < n; i++) c.push_back(false);
for (i = 0; i < n; i++) a[i] = -1;
for (i = 1; i < n; i++) {
if (!c[i - 1]) find(i, 0);
if (!bb) {
cout << -1;
return 0;
}
}
cout << p.size() << "\n";
for (i = 0; i < p.size(); i++) cout << p[i] << " ";
cout << "\n";
cout << q.size() << "\n";
for (i = 0; i < q.size(); i++) cout << q[i] << " ";
}
| 7 | CPP |
#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
#define rep(i, n) for(Int i = 0; i < (Int)(n); ++i)
#define guard(x) if( not (x) ) continue;
int main() {
Int v, e;
std::cin >> v >> e;
std::vector<Int> ss(e), ts(e), ws(e);
rep(i, e) std::cin >> ss[i] >> ts[i] >> ws[i];
std::vector<std::pair<Int, Int>> xs;
rep(i, e) xs.emplace_back(ws[i], i);
std::sort(xs.begin(), xs.end());
std::vector<Int> ps(v, -1), ns(v,1);
auto find = [&](Int x) {
while( ps[x] != -1 ) {
x = ps[x];
}
return x;
};
auto unite = [&](Int x, Int y) {
x = find(x), y = find(y);
if( x != y ) {
if( not ( ns[x] <= ns[y] ) ) std::swap(x, y);
ps[x] = y;
ns[y] += ns[x];
}
};
Int res = 0;
rep(i, e) {
Int k = xs[i].second;
Int s = ss[k], t = ts[k];
guard( find(s) != find(t) );
unite(s, t);
res += ws[k];
}
printf("%ld\n", res);
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long INFL = LLONG_MAX;
const long double pi = acos(-1);
class SumSegTree {
private:
vector<long long> sum, lazy;
int N;
void check_index(int i) {
if (i < 0 || N <= i) {
throw "index out of bound";
}
}
public:
SumSegTree(int N) {
this->N = 1;
while (this->N < N) this->N <<= 1;
this->sum.resize(this->N << 1);
this->lazy.resize(this->N << 1);
}
long long get_sum(int l, int r) {
check_index(l);
check_index(r);
return get_sum(l, r, 0, N - 1, 1);
}
void add(int l, int r, int val) {
check_index(l);
check_index(r);
add(l, r, val, 0, N - 1, 1);
}
void delegate(int a, int b, int n) {
sum[n] += (b - a + 1) * lazy[n];
if (a < b) {
lazy[n * 2] += lazy[n];
lazy[n * 2 + 1] += lazy[n];
}
lazy[n] = 0;
}
long long get_sum(int l, int r, int a, int b, int n) {
if (b < l || r < a) return 0;
if (lazy[n] != 0) delegate(a, b, n);
if (l <= a && b <= r) return sum[n];
int m = (a + b) / 2;
return get_sum(l, r, a, m, n * 2) + get_sum(l, r, m + 1, b, n * 2 + 1);
}
void add(int l, int r, long long val, int a, int b, int n) {
if (b < l || r < a) return;
if (l <= a && b <= r) {
if (a == b)
sum[n] += val;
else
lazy[n] += val;
} else {
int m = (a + b) / 2;
if (lazy[n] != 0) delegate(a, b, n);
sum[n] += (min(b, r) - max(a, l) + 1) * val;
add(l, r, val, a, m, n * 2);
add(l, r, val, m + 1, b, n * 2 + 1);
}
}
};
int N, M;
vector<int> adj[300100];
vector<pair<int, long long> > query[300100];
SumSegTree segt(300005);
long long ans[300100];
void dfs(int u, int prev, int h) {
for (int(i) = 0; (i) < (int((query[u]).size())); (i)++) {
int dist = query[u][i].first;
long long value = query[u][i].second;
segt.add(h, min(N, h + dist), value);
}
ans[u] = segt.get_sum(h, h);
for (int(i) = 0; (i) < (int((adj[u]).size())); (i)++) {
int v = adj[u][i];
if (v == prev) continue;
dfs(v, u, h + 1);
}
for (int(i) = 0; (i) < (int((query[u]).size())); (i)++) {
int dist = query[u][i].first;
long long value = query[u][i].second;
segt.add(h, min(N, h + dist), -value);
}
}
int main() {
ios_base::sync_with_stdio(0);
cout.precision(15);
cout << fixed;
cout.tie(0);
cin.tie(0);
cin >> N;
for (int(i) = 1; (i) <= (N - 1); (i)++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin >> M;
for (int(i) = 1; (i) <= (M); (i)++) {
int v, d;
long long x;
cin >> v >> d >> x;
query[v].push_back(make_pair(d, x));
}
dfs(1, 0, 0);
for (int(i) = 1; (i) <= (N); (i)++)
cout << ans[i] << (i + 1 <= N ? ' ' : '\n');
}
| 11 | CPP |
// 2011/08/26 Tazoe
#include <iostream>
using namespace std;
void DFS(char M[104][104], int y, int x, int C[104][104], int c)
{
C[y][x] = c;
if(M[y-1][x]=='.'&&C[y-1][x]==-1)
DFS(M, y-1, x, C, c);
if(M[y+1][x]=='.'&&C[y+1][x]==-1)
DFS(M, y+1, x, C, c);
if(M[y][x-1]=='.'&&C[y][x-1]==-1)
DFS(M, y, x-1, C, c);
if(M[y][x+1]=='.'&&C[y][x+1]==-1)
DFS(M, y, x+1, C, c);
}
void DFS2(char M[104][104], int y, int x, int C[104][104], int c)
{
C[y][x] = c;
if(M[y-1][x]=='.'&&C[y-1][x]==-1)
DFS(M, y-1, x, C, c);
if(M[y+1][x]=='.'&&C[y+1][x]==-1)
DFS(M, y+1, x, C, c);
if(M[y][x-1]=='.'&&C[y][x-1]==-1)
DFS(M, y, x-1, C, c);
if(M[y][x+1]=='.'&&C[y][x+1]==-1)
DFS(M, y, x+1, C, c);
if(M[y-1][x]=='#'&&C[y-1][x]==-1)
DFS2(M, y-1, x, C, c);
if(M[y+1][x]=='#'&&C[y+1][x]==-1)
DFS2(M, y+1, x, C, c);
if(M[y][x-1]=='#'&&C[y][x-1]==-1)
DFS2(M, y, x-1, C, c);
if(M[y][x+1]=='#'&&C[y][x+1]==-1)
DFS2(M, y, x+1, C, c);
}
int main()
{
while(true){
int n, m;
cin >> n >> m;
if(n==0&&m==0)
break;
char M[104][104];
for(int y=0; y<=m+3; y++)
for(int x=0; x<=n+3; x++)
M[y][x] = '#';
for(int y=1; y<=m+2; y++)
for(int x=1; x<=n+2; x++)
M[y][x] = '.';
int Y = -1;
int X = -1;
for(int y=2; y<=m+1; y++){
for(int x=2; x<=n+1; x++){
cin >> M[y][x];
if(M[y][x]=='&'){
Y = y;
X = x;
M[y][x] = '.';
}
}
}
// cout << Y << ' ' << X << endl;
int C[104][104];
for(int y=0; y<=m+3; y++)
for(int x=0; x<=n+3; x++)
C[y][x] = -1;
int cnt = 0;
DFS(M, Y, X, C, cnt);
if(C[1][1]!=-1){
cout << C[1][1] << endl;
continue;
}
while(true){
cnt++;
for(int y=2; y<=m+1; y++){
for(int x=2; x<=n+1; x++){
if(M[y][x]=='#'&&C[y][x]==-1&&M[y-1][x]=='.'&&C[y-1][x]==cnt-1)
DFS2(M, y, x, C, cnt);
if(M[y][x]=='#'&&C[y][x]==-1&&M[y+1][x]=='.'&&C[y+1][x]==cnt-1)
DFS2(M, y, x, C, cnt);
if(M[y][x]=='#'&&C[y][x]==-1&&M[y][x-1]=='.'&&C[y][x-1]==cnt-1)
DFS2(M, y, x, C, cnt);
if(M[y][x]=='#'&&C[y][x]==-1&&M[y][x+1]=='.'&&C[y][x+1]==cnt-1)
DFS2(M, y, x, C, cnt);
}
}
if(C[1][1]!=-1){
cout << C[1][1] << endl;
break;
}
}
/*
for(int y=0; y<=m+3; y++){
for(int x=0; x<=n+3; x++){
if(C[y][x]==-1)
cout << '-';
else
cout << C[y][x];
}
cout << endl;
}
*/
}
return 0;
} | 0 | CPP |
R=lambda:map(int,input().split())
for _ in[0]*int(input()):n,k=R();a=[*R()];l,m=min(a),max(a);print((l+k,-1)[m-l>2*k])
| 8 | PYTHON3 |
a=int(input())
b=list(map(int, input().split()))
c=int(input())
d=list(map(int, input().split()))
for i in b:
for j in d:
if ((i+j) not in b) and ((i+j) not in d):
exit(print(i,j)) | 7 | PYTHON3 |
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int n;
while(cin >> n) {
printf("3C%02d\n", (n - 1) % 39 + 1);
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9 + 7;
int n;
string rotate(string s) {
char grid[11][11];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) grid[i][j] = s[i * n + j];
for (int i = 0; i < n; ++i)
for (int j = i; j < n; ++j) swap(grid[i][j], grid[j][i]);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n / 2; ++j) swap(grid[i][j], grid[i][n - j - 1]);
string ret;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) ret += grid[i][j];
return ret;
}
string flipVer(string s) {
char grid[11][11];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) grid[i][j] = s[i * n + j];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n / 2; ++j) swap(grid[i][j], grid[i][n - j - 1]);
string ret;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) ret += grid[i][j];
return ret;
}
string flipHor(string s) {
char grid[11][11];
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) grid[i][j] = s[i * n + j];
for (int j = 0; j < n; ++j)
for (int i = 0; i < n / 2; ++i) swap(grid[i][j], grid[n - i - 1][j]);
string ret;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) ret += grid[i][j];
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
string s1, s2;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
s1 += s;
}
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
s2 += s;
}
vector<string> v1, v2;
v1.push_back(s1);
v1.push_back(flipHor(s1));
v1.push_back(flipVer(s1));
v1.push_back(rotate(s1));
v1.push_back(rotate(v1.back()));
v1.push_back(rotate(v1.back()));
v2.push_back(s2);
v2.push_back(flipHor(s2));
v2.push_back(flipVer(s2));
v2.push_back(rotate(s2));
v2.push_back(rotate(v2.back()));
v2.push_back(rotate(v2.back()));
for (int i = 0; i < (int)(v1).size(); ++i) {
for (int j = 0; j < (int)(v2).size(); ++j) {
if (v1[i] == v2[j]) {
cout << "YES";
return 0;
}
}
}
cout << "NO";
cin.ignore();
cin.get();
}
| 7 | CPP |
#include <bits/stdc++.h>
std::vector<int> dec2Bin(long n) {
std::vector<int> output;
while (n > 0) {
output.push_back(n % 2);
n /= 2;
}
return output;
}
long readDec(std::vector<int> input) {
long output = 0;
for (int k = input.size() - 1; k >= 0; k--) {
output = 10 * output + input[k];
}
return output;
}
int main() {
const int M = 1024;
long n;
scanf("%ld\n", &n);
int output = 0;
while (readDec(dec2Bin(++output)) <= n)
;
printf("%d\n", --output);
return 0;
}
| 9 | CPP |
terminal = input() + input() + input()
if (terminal[0] == terminal[8]) and (terminal[1] == terminal[7]) and (terminal[2] == terminal[6]) and (terminal[3] == terminal[5]):
print("YES")
else:
print("NO")
| 7 | PYTHON3 |
n=int(input())
a=list(map(int,input().split()))
s1=0
s2=0
for i in range(n):
if(a[i]%2==0):
s1+=1
x=i+1
else:
s2+=1
y=i+1
if(s1>s2):
print(y)
else:
print(x) | 7 | PYTHON3 |
from heapq import heappush, heappop
def dijkstra(edges, size, source):
distance = [float('inf')] * size
distance[source] = 0
visited = [False] * size
pq = []
heappush(pq, (0, source))
while pq:
dist_u, u = heappop(pq)
visited[u] = True
for v, weight, _ in edges[u]:
if not visited[v]:
new_dist = dist_u + weight
if distance[v] > new_dist:
distance[v] = new_dist
heappush(pq, (new_dist, v))
return distance
while True:
N, M = map(int, input().split())
if N == M == 0:
break
edges = [[] for i in range(N)]
for i in range(M):
u, v, d, c = map(int, input().split())
u -= 1
v -= 1
edges[u].append((v, d, c))
edges[v].append((u, d, c))
dist = dijkstra(edges, N, 0)
ans = 0
for u in range(1, N):
cost = 1000
for v, d, c in edges[u]:
if dist[u] == dist[v] + d:
cost = min(cost, c)
ans += cost
print(ans)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 13;
const long long INF = 2e18;
long long mod = -1;
bool isprime(long long n) {
for (long long i = 2; i * i <= n; ++i) {
if (n % i == 0) return 0;
}
return 1;
}
long long bp(long long a, long long n) {
if (n == 0) return 1;
if (n & 1) return (bp(a, n - 1) * a) % mod;
long long x = bp(a, n / 2);
return (x * x) % mod;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
vector<long long> a(n);
mod = n;
if (n == 1) {
cout << "YES\n";
cout << 1 << "\n";
return 0;
}
if (n == 2) {
cout << "YES\n1\n2\n";
return 0;
}
if (n == 3) {
cout << "YES\n1\n2\n3\n";
return 0;
}
if (n == 4) {
cout << "YES\n";
cout << "1 3 2 4\n";
return 0;
}
if (!isprime(n)) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
cout << "1 \n";
a[0] = 1;
a[n - 1] = n;
for (long long i = 2; i <= n - 1; ++i) {
a[i - 1] = (i * bp(i - 1, n - 2)) % n;
}
for (long long i = 1; i < n; ++i) cout << a[i] << "\n";
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
long long m, a[200010], last;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cout << (a[i] + last) / m << " ";
last = (a[i] + last) % m;
}
return 0;
}
| 7 | CPP |
import math
list = input().split()
n = int(list[0])
m = int(list[1])
a = int(list[2])
x1 = math.ceil(m / a)
x2 = math.ceil(n / a)
print(x1 * x2) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
static const int MOD = 1000000007;
static const double PI = 3.14159265358979;
template <typename T>
void pv(T a, T b) {
for (T i = a; i != b; i++) cout << *i << " ";
cout << endl;
}
template <typename T, typename U>
inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
if (x < y) x = y;
}
int in() {
int _x;
scanf("%d", &_x);
return _x;
}
long long lin() {
long long _x;
scanf("%lld", &_x);
return _x;
};
int constmat[1001][1001];
int mat[1001][1001];
int node[1001][1001];
int n, m;
int ok(int k, int sx, int sy) {
for (int(i) = 0; (i) < (int)(1001); (i)++)
for (int(j) = 0; (j) < (int)(1001); (j)++) node[i][j] = 0;
for (int y = sy; y < n; y += k)
for (int x = sx; x < m; x += k) {
if ((mat[y][x] & 1) == 0) continue;
node[y][x] |= 64;
if (y + k < n && (mat[y + k][x] & 1)) {
int j = 0;
for (int i = 1; i < k; i++) j += mat[y + i][x] & 1;
if (j != 0 && j != k - 1) return 0;
if (j == k - 1) {
node[y][x] |= 1;
node[y + k][x] |= 2;
}
}
if (x + k < m && (mat[y][x + k] & 1)) {
int j = 0;
for (int i = 1; i < k; i++) j += mat[y][x + i] & 1;
if (j != 0 && j != k - 1) return 0;
if (j == k - 1) {
node[y][x] |= 4;
node[y][x + k] |= 8;
}
}
}
int cnt = 0;
for (int(y) = 0; (y) < (int)(n); (y)++)
for (int(x) = 0; (x) < (int)(m); (x)++)
cnt += (__builtin_popcount(node[y][x] & 15) % 2);
if (cnt != 0 && cnt != 2) return 0;
queue<int> q;
int y, x;
for (y = 0; y < n; y++)
for (x = 0; x < m; x++)
if (node[y][x]) goto out;
if (y >= n) return 0;
out:
if ((node[y][x] & 15) == 0) return 0;
q.push(y * 10000 + x);
node[y][x] |= 32;
while (!q.empty()) {
int kk = q.front();
q.pop();
int cx = kk % 10000, cy = kk / 10000;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
for (int(i) = 0; (i) < (int)(4); (i)++) {
if ((node[cy][cx] & (1 << i)) == 0) continue;
int tx = cx + dx[i] * k, ty = cy + dy[i] * k;
if (tx < 0 || ty < 0 || tx >= m || ty >= n) continue;
if (node[ty][tx] & 32) continue;
node[ty][tx] |= 32;
q.push(ty * 10000 + tx);
}
}
for (int(y) = 0; (y) < (int)(n); (y)++)
for (int(x) = 0; (x) < (int)(m); (x)++)
if ((node[y][x] & 64) && ((node[y][x] & 32) == 0)) return 0;
return 1;
}
signed main() {
cin >> n >> m;
int sy, sx;
for (int(y) = 0; (y) < (int)(n); (y)++)
for (int(x) = 0; (x) < (int)(m); (x)++) cin >> constmat[y][x];
for (int(y) = 0; (y) < (int)(n); (y)++)
for (int(x) = 0; (x) < (int)(m); (x)++)
if (constmat[y][x]) sy = y, sx = x;
for (int k = min(n, m); k >= 2; k--) {
int f = 0;
memmove(mat, constmat, sizeof(mat));
for (int y = sy % k; y < n; y += k)
for (int x = sx % k; x < m; x += k) {
if (y + k < n)
for (int i = 0; i <= k; i++) mat[y + i][x] |= 2;
if (x + k < m)
for (int i = 0; i <= k; i++) mat[y][x + i] |= 2;
mat[y][x] |= 4;
}
for (int(y) = 0; (y) < (int)(n); (y)++)
if (f == 0)
for (int(x) = 0; (x) < (int)(n); (x)++)
if (mat[y][x] == 1) f = 1;
if (f) continue;
if (ok(k, sx % k, sy % k)) {
for (int i = 2; i <= k; i++)
if (k % i == 0) cout << i << ' ';
cout << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| 11 | CPP |
n = int(input())
res = 0
for _ in range(n):
if input().count('1') > 1:
res += 1
print(res)
| 7 | PYTHON3 |
def fun(b,g,n):
if(b>=n and g>=n):
return n+1
elif(b<n and g>=n):
return b+1
elif(b>=n and g<n):
return g+1
else:
count = 0
for i in range(n+1):
if (i<=b and n-i<=g):
count += 1
return count
b = int(input())
g = int(input())
n = int(input())
print( fun(b,g,n))
| 8 | PYTHON3 |
#include<queue>
#include<cstdio>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
struct Info{
int t,load;
Info(int T,int L):t(T),load(L){}
bool operator>(const Info &in)const{
if(t==in.t) return load>in.load;
return t>in.t;
}
};
int main(){
for(int n;scanf("%d",&n),n;){
priority_queue< Info,vector<Info>,greater<Info> > pq;
rep(i,n){
int m,a,b; scanf("%d%d%d",&m,&a,&b);
pq.push(Info(a,m));
pq.push(Info(b,-m));
}
int total=0;
bool ok=true;
while(!pq.empty()){
Info a=pq.top(); pq.pop();
total+=a.load;
if(total>150){ ok=false; break; }
}
puts(ok?"OK":"NG");
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
using VI = vector<int>;
struct vec {
LL x, y;
vec() : x(0), y(0) {}
vec(LL a, LL b) : x(a), y(b) {}
vec operator-(const vec& other) const {
return vec(x - other.x, y - other.y);
}
int quad() const {
if (x > 0 && y >= 0) return 1;
if (y > 0 && x <= 0) return 2;
if (x < 0 && y <= 0) return 3;
return 4;
}
LL cross(const vec& v) const { return x * v.y - y * v.x; }
bool operator<(const vec& v) const {
if (quad() != v.quad()) {
return quad() < v.quad();
}
return cross(v) > 0;
}
};
LL choose(int n, int r) {
if (n < r) return 0;
LL num = 1, den = 1;
for (int i = 1; i <= r; i++) {
num *= (n - i + 1);
den *= i;
}
return num / den;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<vec> pts(n);
for (auto& p : pts) {
cin >> p.x >> p.y;
}
LL ans = 0;
for (int i = 0; i < n; i++) {
auto p = pts[i];
vector<vec> sub;
for (int j = 0; j < n; j++) {
if (i == j) continue;
sub.push_back(pts[j] - p);
}
sort(sub.begin(), sub.end());
for (int j = 0; j < n - 1; j++) {
sub.push_back(sub[j]);
}
LL cur = choose(n - 1, 4);
for (int j = 0, k = 0; j < n - 1; j++) {
while (k + 1 < j + n - 1 && sub[j].cross(sub[k + 1]) >= 0) {
k++;
}
cur -= choose(k - j, 3);
}
ans += cur;
}
cout << ans << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long n;
cin >> n;
vector<long long> v(n + 1);
for (long long i = 2; i <= n; i++) {
if (v[i]) continue;
v[i] = 1;
for (long long j = i * i; j <= n; j += i) {
if (!v[j]) v[j] = j / i;
}
}
sort(v.begin(), v.end());
for (int i = 2; i <= n; i++) {
cout << v[i] << ' ';
}
return 0;
}
| 12 | CPP |
if __name__== "__main__":
for _ in range(int(input())):
x1, y1, x2, y2 = map(int, input().split())
print((x2-x1)*(y2-y1)+1)
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
vector<double> mod(vector<double> p1, vector<double> p2) {
int deg1 = p1.size(), deg2 = p2.size();
for (int i = deg1 - 1; i >= deg2 - 1; i--) {
double d = p1[i] / p2.back();
for (int j = deg2 - 1; j >= 0; j--) p1[i - (deg2 - 1 - j)] -= d * p2[j];
}
p1.resize(p2.size() - 1);
return p1;
}
bool check(vector<double> p1, vector<double> p2) {
int cnt = 0, deg = p1.size();
for (int i = 0; i < deg - 1; i++) {
swap(p1, p2);
p2 = mod(p2, p1);
cnt++;
if (abs(p2.back()) < eps) break;
}
if (cnt == deg - 1) return 1;
return 0;
}
int main() {
srand(time(NULL));
int n;
cin >> n;
vector<int> p1, p2;
while (true) {
p1.clear();
p2.clear();
for (int i = 0; i <= n; i++) p1.push_back(rand() % 3 - 1);
for (int i = 0; i <= n - 1; i++) p2.push_back(rand() % 3 - 1);
p1[p1.size() - 1] = 1;
p2[p2.size() - 1] = 1;
vector<double> dp1, dp2;
for (auto i : p1) dp1.push_back(i);
for (auto i : p2) dp2.push_back(i);
if (check(dp1, dp2)) {
cout << n << "\n";
for (int j : p1) cout << j << " ";
cout << "\n";
cout << n - 1 << "\n";
for (int j : p2) cout << j << " ";
cout << "\n";
return 0;
}
}
cout << -1 << "\n";
return 0;
}
| 8 | CPP |
"""https://codeforces.com/contest/1101/problem/C"""
def do_intersect( a, b ): return min(a[1],b[1]) >= max( a[0],b[0])
for _ in range(int(input())):
n = int(input())
inps = [ tuple(map(int,input().split())) for _ in range(n) ]
org = [ pair for pair in inps ]
inps.sort()
seta = inps[0]
setb = (float('inf'), float('-inf'))
for i in range(1, n):
if do_intersect( seta, inps[i] ):
seta = min( inps[i][0], seta[0] ), max(inps[i][1], seta[1])
else:
setb = min( inps[i][0], setb[0] ), max(inps[i][1], setb[1])
break
if setb == (float('inf'), float('-inf')):
print(-1)
else:
ans =[]
for inp in org:
if do_intersect( inp, seta ):
ans.append(1)
else:
ans.append(2)
print(*ans) | 9 | PYTHON3 |
#include<bits/stdc++.h>
#define mod 998244353
#define maxn 200005
using namespace std;
long long n,x,m,tt=99999999999ll;
int vis[maxn];
long long arr[maxn];
int main()
{
long long sum=0;
cin>>n>>x>>m;
for (int i=1;i<=n;i++)
{
if (vis[x]!=0)
{
arr[i]=arr[i-1]+x;
long long zhouqihe,zhouqi;
zhouqihe=arr[i]-arr[vis[x]];
zhouqi=i-vis[x];
long long lef=n-i+1;
long long tim=lef/zhouqi;
sum+=zhouqihe*tim;
tt=i+tim*zhouqi;
break;
}
sum+=x;
arr[i]=arr[i-1]+x;
vis[x]=i;
x=x*x%m;
}
for (tt;tt<=n;tt++)
{
sum+=x;
x=x*x%m;
}
cout<<sum<<endl;
} | 0 | CPP |
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define INF ( 1 << 30 )
#define fr first
#define sc second
typedef pair < int , int > Pi;
typedef pair < Pi , Pi > Pii;
const int dy[] = { 0, 1, -1, 0}, dx[] = { 1, 0, 0, -1};
int h, w, Sx, Sy, Gx, Gy;
vector< Pi > hoge[5];
int dp[5][1000], num;
char c;
int dis(Pi x1,Pi x2){
return abs(x1.fr - x2.fr) + abs(x1.sc - x2.sc);
}
int rec(int now,int java){
if(dp[now][java] != -1) return dp[now][java];
int ret = INF, next = ( now + 1 ) % 5;
if(now == num) ret = dis(hoge[now][java],Pi(Gy,Gx));
else for(int i = 0 ; i < hoge[next].size() ; i++ ){
ret = min( ret, rec( next, i) + dis(hoge[now][java],hoge[next][i]));
}
return dp[now][java] = ret;
}
int main(){
while(cin >> w >> h , w){
for(int i = 0 ; i < 5 ; i++ ) hoge[i].clear();
for(int i = 0 ; i < h ; i++ ){
for(int j = 0 ; j < w ; j++ ){
cin >> c;
if(c == 'S') Sy = i, Sx = j;
else if(c == 'G') Gy = i, Gx = j;
else if(c != '.') hoge[c-'1'].push_back(Pi(i,j));
}
}
Pi ret = Pi(INF,INF);
for(int i = 0 ; i < 5 ; i++ ){
for(int j = 0 ; j < 5 ; j++ )
for(int k = 0 ; k < 1000 ; k++ ) dp[j][k] = -1;
num = ( i + 4 ) % 5;
int next = ( i + 1 ) % 5, tmp = INF;
for(int j = 0 ; j < hoge[next].size() ; j++ ){
tmp = min( tmp, rec( next, j) + dis(Pi(Sy,Sx),hoge[next][j]));
}
ret = min( ret, Pi( tmp, i + 1));
}
if( ret.fr == INF ) cout << "NA" << endl;
else cout << ret.sc << " " << ret.fr << endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t, n;
cin >> t;
while (t--) {
cin >> n;
long long a[2 * n];
vector<long long> v;
map<long long, long long> mp;
for (long long i = 0; i < 2 * n; i++) {
cin >> a[i];
if (mp[a[i]] == 0) {
v.push_back(a[i]);
mp[a[i]] = 1;
}
}
for (long long i = 0; i < n; i++) {
cout << v[i] << " ";
}
cout << endl;
}
return 0;
}
| 8 | CPP |
n, m = map(int, input().split())
x = set()
y = set()
for i in range(m):
xx, yy = map(int, input().split())
x.add(xx);
y.add(yy)
print((n - len(x)) * (n - len(y)))
| 8 | PYTHON3 |
a, za, zb, sa, sb = map(int,input().split())
x = a * za + sa * 2
c = a * zb + sb * 2
if x == c:
print('Friendship')
elif x < c:
print('First')
else:
print('Second') | 7 | PYTHON3 |
matrix = []
for i in range(5):
matrix += [[int(n) for n in input().strip().split()]]
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if matrix[i][j] == 1:
row = i
column = j
break
else:
pass
moves = 0
while row != 2:
if row < 2:
row += 1
moves += 1
elif row > 2:
row -= 1
moves += 1
while column != 2:
if column < 2:
column += 1
moves += 1
elif column > 2:
column -= 1
moves += 1
print(moves) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;
const double eps = 1e-10;
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;}
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
double a, b;
cin >> a >> b;
for(double t = 0;t <= 180; t+= 0.5){
double aa = a+t, bb = b-t;
if(aa >= 360) aa-=360;
if(bb < 0) bb += 360;
if(aa == bb){
cout << aa << endl;
return 0;
}
aa = a-t; bb = b+t;
if(aa < 0) aa+=360;
if(bb >= 360) bb-=360;
if(aa == bb){
cout << aa << endl;
return 0;
}
}
}
| 0 | CPP |
x=int(input())
for i in range(-200,200):
for j in range(-200,200):
if x==(i**5-j**5):
a=i
b=j
break
print(a,b) | 0 | PYTHON3 |
import math
t = int(input())
for _ in range(t):
a, b, q = map(int, input().split())
lr = []
ans = []
for i in range(q):
lr.append(list(map(int, input().split())))
for i in lr:
if i[1] < a or i[1] < b or b%a == 0 or a%b == 0:
ans.append(0)
else:
lc = (a*b)//math.gcd(a, b)
lb = max(i[0], max(a, b))
rb = i[1]
if lb%lc < max(a, b):
lb += max(a, b)-(lb%lc)
if rb%lc < max(a, b):
rb = rb-(rb%lc)-1
m = rb//lc - lb//lc
m *= max(a,b)
if lb%lc < max(a,b):
m -= lb%lc
if rb%lc < max(a,b):
m -= max(a, b) - rb%lc - 1
ans.append(1+rb-lb-m)
print(" ".join(map(str, ans)))
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long a[1000005];
void solve() {
long long n, k;
cin >> n >> k;
for (long long i = 1; i < n + 1; i++) a[i] = i;
if (k < n * (n + 1) / 2) {
cout << -1;
return;
}
long long d = n * (n + 1) / 2, t = n - 1, l = 1, r = n;
while (k - d >= t && t > 0 && l < r) {
d += t;
t -= 2;
swap(a[l], a[r]);
r--;
l++;
}
if (k > d && k - d < t) {
long long z = k - d;
d += z;
swap(a[l], a[l + z]);
}
cout << d << "\n";
for (long long i = 1; i < n + 1; i++) cout << i << " ";
cout << "\n";
for (long long i = 1; i < n + 1; i++) cout << a[i] << " ";
}
signed main() {
ios_base::sync_with_stdio(false);
long long t = 1;
while (t--) solve();
return 0;
}
| 13 | CPP |
a,b=input().split()
a=int(a)
b=int(b)
mini=min(a,b)
maxi=int(abs(a-b)/2)
print(str(mini)+' '+str(maxi)) | 7 | PYTHON3 |
import sys
cases = False
# Pre-defined function
# Begin
def fast_pow(a:int, b:int):
res = 1
while b > 0:
if b & 1:
res *= a
a *= a
b >>= 1
return res
def c2(n):
return n * (n-1) // 2
def get():
return list(map(int, input().split()))
def bits(n: int):
return list(bin(n)).count('1')
def main(test_case = False):
n = int(input()) if test_case else 1
for _ in range(n):
test()
def flush():
sys.stdout.flush()
def parr(arr):
print(*arr, sep=' ')
def gcd(a, b):
while b:
if b % a == 0:
break
tmp = a
a = b % a
b = tmp
return a
def ext_gcd(a: int, b: int):
if (b == 0):
return [a, [1, 0]]
res = ext_gcd(b, a % b)
g = res[0]
x1 = res[1][0]
y1 = res[1][1]
x = y1
y = x1 - y1 * (a // b)
return [g, [x, y]]
# End
b = []
p = []
n = m = a = 0
def check(cnt):
if cnt == 0:
return True
x = b[-cnt:]
y = p[:cnt]
s = a
i = 0
while i < cnt and s >= 0:
s -= max(0, y[i]-x[i])
i += 1
return s >= 0
def test():
global n, m, a, b, p
n, m, a = get()
b = sorted(get())
p = sorted(get())
left = 0
right = min(n, m)
ans = -1
while left <= right:
mid = (left + right) // 2
if check(mid):
ans = max(ans, mid)
left = mid + 1
else:
right = mid - 1
if ans == -1:
print(0, 0)
return
# print(ans)
t = 0
x = b[-ans:]
y = p[:ans]
i = 0
while i < ans:
t += min(x[i], y[i])
a -= max(0, y[i]-x[i])
i += 1
print(ans, max(0, t-a))
main(cases) | 10 | PYTHON3 |
import copy
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
# n = 0
# m = 0
# n = int(input())
# li = [int(i) for i in input().split()]
# s = sorted(li)
mo = 998244353
def exgcd(a, b):
if not b:
return 1, 0
y, x = exgcd(b, a % b)
y -= a//b * x
return x, y
def getinv(a, m):
x, y = exgcd(a, m)
return -(-1) if x == 1 else x % m
def comb(n, b):
res = 1
b = min(b, n-b)
for i in range(b):
res = res*(n-i)*getinv(i+1, mo) % mo
# res %= mo
return res % mo
def quickpower(a,n):
res = 1
while n:
if n&1:
res = res * a % mo
n >>= 1
a = a*a%mo
return res
t = int(input())
for ti in range(t):
a, b = map(int, input().split())
if a == b:
print(a+b)
else:
print(max(a, b)*2-1)
| 7 | PYTHON3 |
weight = int(input())
if (weight-2) % 2 == 0 and weight > 3:
print("YES")
else:
print("NO")
| 7 | PYTHON3 |
n, k = map(int, input().split())
i = 2
nn = n
a = []
for i in range(2, n+1):
while nn % i == 0:
if k > 1:
a.append(i)
nn //= i
elif i == nn:
a.append(i)
nn//=i
else:
break
k -= 1
if k == 0:
for i in a:
print(i, end = ' ')
else:
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
string p = "";
for (int k = 0; k < 2 * n - 1; k = k + 2) {
p = p + s[k];
}
cout << p << endl;
}
return 0;
}
| 7 | CPP |
/*
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2331961#2
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2331969#2
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2331970#2
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2332101#2
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2333449#2
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <tuple>
#include <algorithm>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i,n) for(int i=0;i<n;i++)
#define srep(i,a,n) for(int i=a;i<n;i++)
#define REP(i,n) for(int i=0;i<=n;i++)
#define SREP(i,a,n) for(int i=a;i<=n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define all(a) (a).begin(),(a).end()
#define mp(a,b) make_pair(a,b)
#define mt make_tuple
#define fst first
#define scn second
const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-9;
const int val[] = { 1,5,10,50,100,500 };
int main() {
while (true) {
int p; cin >> p;
vint a(6, 0);
rep(i, 6) cin >> a[i];
if (p == 0) break;
int ret = mod;
for (int i = p; i <= p + 500; i++) {
//i?????????
int left = i;
int tmp = 0;
for (int j = 5; j >= 0; j--) {
int add = min(left / val[j], a[j]);
tmp += add;
left -= add*val[j];
}
if (left != 0) tmp = mod;
left = i - p;
for (int j = 5; j >= 0; j--) {
int add = left / val[j];
tmp += add;
left -= add*val[j];
}
ret = min(ret, tmp);
}
cout << ret << endl;
}
return 0;
} | 0 | CPP |
import sys
N = int(input())
nums = list(map(int, input().split(' ')))
res = []
curr = 0
left = 0
right = N - 1
while True:
if nums[left] <= curr or nums[right] <= curr or nums[left] == nums[right]:
break
if (nums[left] < nums[right]):
res.append('L')
curr = nums[left]
left += 1
elif (nums[left] > nums[right]):
res.append('R')
curr = nums[right]
right -= 1
if left == right and nums[left] > curr:
res.append('L')
print(len(res))
print(''.join(res))
sys.exit(0)
s = [0, 0]
temp_left = left
temp_curr = curr
while True:
if temp_left >= right:
break
if nums[temp_left] > temp_curr:
s[0] += 1
temp_curr = nums[temp_left]
temp_left += 1
else:
break
temp_right = right
temp_curr = curr
while True:
if temp_right <= left:
break
if nums[temp_right] > temp_curr:
s[1] += 1
temp_curr = nums[temp_right]
temp_right -= 1
else:
break
if s[0] > s[1]:
res.append('L' * s[0])
else:
res.append('R' * s[1])
res = ''.join(res)
print(len(res))
print(res)
| 9 | PYTHON3 |
import math
t = int(input())
def helper(x):
return x * x - 4 * x
for _ in range(t):
d = int(input())
delta = helper(d)
if delta < 0:
print('N')
else:
line = ['Y']
a = (d - math.sqrt(delta)) / 2
b = d - a
line.append(str(a))
line.append(str(b))
print(' '.join(line))
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 5;
const long long LINF = LLONG_MAX;
long long powmod(long long a, long long b) {
long long res = 1;
a %= MOD;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
}
return res;
}
void solve() {
int n;
string num, a = "", b = "";
cin >> n >> num;
for (int i = 0; i < n; ++i) {
if (num[i] == '2') {
a += "1";
b += "1";
} else if (num[i] == '0') {
a += "0";
b += "0";
} else {
a += "1";
b += "0";
for (int j = i + 1; j < n; ++j) {
a += "0";
b += num[j];
}
break;
}
}
cout << a << '\n' << b << '\n';
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 9 | CPP |
l=[0]*10**5
for i in range(1,100):
l[i]=(2**i-1)*2**(i-1)
n=int(input())
ans=0
for i in range(1,100):
if n%l[i]==0:
ans=l[i]
print(ans) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
static string s;
static int k;
static const int maxn = 1e3 + 10;
static int cnt[maxn], sn[maxn];
static long long mod = 1e9 + 7;
static long long dp[3][maxn][maxn][10];
long long dfs(long long pos, int pre, int num, bool flag) {
if (pos == -1) return cnt[num] == k;
if (!flag && dp[pre][pos][num][k] != -1) return dp[pre][pos][num][k];
int up = flag ? sn[pos] : 1;
long long ret = 0;
for (int i = 0; i <= up; i++) {
ret = (ret + dfs(pos - 1, i, num + (i == 1), flag && i == up) % mod) % mod;
}
if (!flag) dp[pre][pos][num][k] = ret;
return ret;
}
int getNums(int x) {
if (x == 1) return 0;
int cnts = 0;
while (x) {
if (x % 2) cnts++;
x /= 2;
}
return 1 + getNums(cnts);
}
void init() {
for (int i = 1; i < maxn; i++) {
cnt[i] = getNums(i) + 1;
}
}
void solve() {
reverse(s.begin(), s.end());
for (int i = 0; i < s.length(); i++) sn[i] = s[i] -= '0';
long long ans = dfs(s.length() - 1, 0, 0, true);
ans = (ans % mod + mod) % mod;
if (k == 1 && ans) --ans;
printf("%I64d\n", ans);
}
int main() {
while (cin >> s >> k) {
memset(dp, -1, sizeof(dp));
init();
solve();
}
return 0;
}
| 9 | CPP |
x=int(input())
q=0
for i in range(x):
a,b=map(int,input().split())
if b-a>=2:
q+=1
print(q) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int edges[100010];
vector<int> larger[100010];
void print() {
for (int i = 1; i <= n; ++i) {
cout << i << ", larger_size: " << larger[i].size() << endl;
}
}
int main() {
int m;
scanf("%d%d", &n, &m);
while (m--) {
int u, v;
scanf("%d%d", &u, &v);
++edges[u];
++edges[v];
if (u > v)
larger[v].push_back(u);
else
larger[u].push_back(v);
}
unsigned int cnt = 0;
for (int i = 1; i <= n; ++i) {
unsigned int smaller_size = edges[i] - larger[i].size();
cnt += larger[i].size() * smaller_size;
}
cout << cnt << endl;
int q;
scanf("%d", &q);
while (q--) {
int x;
scanf("%d", &x);
unsigned int smaller_size = edges[x] - larger[x].size();
cnt -= larger[x].size() * smaller_size;
for (int b : larger[x]) {
cnt -= larger[b].size();
larger[b].push_back(x);
cnt += edges[b] - larger[b].size();
}
larger[x].clear();
printf("%u\n", cnt);
}
return 0;
}
| 12 | CPP |
from sys import stdin
def solve():
for _ in range(int(stdin.readline())):
A,B=map(int,stdin.readline().split())
b,cnt=9,0
while(b<=B):
b*=10
b+=9
cnt+=1
print(A*cnt)
solve()
| 8 | PYTHON3 |
[n, m] = [int(i) for i in input().split()]
if m == n:
print (0)
elif m == 0:
print (1)
elif m * 2 <= n:
print(m)
else:
print(n - m) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int upper(vector<pair<long long int, long long int> > &a, long long int val) {
int b = 0, e = a.size() - 1;
while (b <= e) {
int m = b + (e - b) / 2;
if (a[m].first <= val)
b = m + 1;
else
e = m - 1;
}
return b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
long long int n, m;
cin >> n >> m;
vector<pair<long long int, long long int> > a(m);
for (int i = 0; i < m; i++) {
pair<long long int, long long int> p;
cin >> p.first >> p.second;
a[i] = p;
}
sort(a.begin(), a.end());
vector<long long int> pre(m);
pre[0] = a[0].first;
for (int i = 1; i < m; i++) pre[i] = pre[i - 1] + a[i].first;
long long int res = 0;
for (int i = 0; i < m; i++) {
int idx = upper(a, a[i].second);
if (idx == m) {
res = max(res, a[i].first + (n - 1) * a[i].second);
} else {
long long int x = pre[m - 1], len = m - idx, k = n;
if (len > k) idx = m - k;
if (idx - 1 >= 0) {
x = x - pre[idx - 1];
k = k - m + idx;
} else
k = k - m;
if (idx > i && k) {
x = x + a[i].first;
k--;
}
res = max(res, x + k * a[i].second);
}
}
cout << res << endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
set<char> se;
vector<char> v;
int main() {
string s;
while (s[s.size() - 1] != '}') {
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] >= 'a' && s[i] <= 'z') {
se.insert(s[i]);
}
}
}
cout << se.size() << endl;
se.clear();
return 0;
}
| 7 | CPP |
n=int(input())
a=(n//7)*2
if n%7==6:
a=a+1
b=a+1
elif n%7==0:
b=a
elif n%7==1:
b=a+1
else:
b=a+2
print(a,b)
| 7 | PYTHON3 |
n = int(input())
arr = [int(i) for i in input().split(' ')]
p1 = 0
p2 = len(arr)-1
sum1 = arr[p1]
sum2 = arr[p2]
final_sum = 0
for _ in arr:
if p1 >= p2:
break
if sum1 > sum2:
p2 -= 1
sum2 += arr[p2]
elif sum1 < sum2:
p1 += 1
sum1 += arr[p1]
else:
final_sum = sum1
p2 -=1
sum2 += arr[p2]
print(final_sum) | 9 | PYTHON3 |
N =int(input())
List = [int(x) for x in input().split()]
if(N == 1):
print(1,1)
print(-List[0])
print(1,1)
print(0)
print(1,1)
print(0)
else:
Needed = []
print(1,1)
print(N - (List[0]%N))
Needed.append(N-(List[0]%N))
print(2,N)
for i in range(1,N):
Needed.append( N - (List[i]%N) )
print(*[(N-1)*(N-i) for i in Needed[1:N]])
print(1,N)
New = []
print(-Needed[0]-List[0],end = " ")
for i in range(1,N):
New.append(-(List[i] + (N-Needed[i])*(N-1)))
print(*New)
| 7 | PYTHON3 |
// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
// }}}
// #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#include<bitset>
int n, k;
char buf[(1<<20)+10];
int dp[1<<21][21];
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0);
cin >> n >> k;
for(int i = 0; i <= n; i++) {
cin >> buf;
for(int j = 0; j < (1 << i); j++) {
if(buf[j] == '1') {
int x = (1 << i) | j;
dp[x][0] = 1;
}
}
}
for(int L = n; L > 0; --L) {
for(int i = 0; i < (1 << L); i++) {
int x = (1 << L) | i;
int next0[21], next1[21];
// dump(bitset<8>());
// dump(bitset<8>(x));
next0[L] = next1[L] = L;
for(int h = L - 1; h >= 0; h--) {
int f = (i >> (L - h - 1)) & 1;
next0[h] = f == 0 ? h : next0[h+1];
next1[h] = f == 1 ? h : next1[h+1];
}
for(int h = 0; h < L; h++) {
int nx = x >> (L - h);
// dump(h);
// dump(dp[x][h]);
// dump(bitset<8>(nx));
// dump(h, next0[h+1], next1[h+1]);
dp[nx][h] += dp[x][h];
if(next0[h] < L) {
int j0 = (i >> (L - h) << (L - next0[h])) | (i & ((1 << (L - next0[h])) - 1));
int x0 = (1 << (L - next0[h] + h)) | j0;
dp[x0][h+1] += dp[x][h];
}
if(next1[h] < L) {
int j0 = (i >> (L - h) << (L - next1[h])) | (i & ((1 << (L - next1[h])) - 1));
int x0 = (1 << (L - next1[h] + h)) | j0;
dp[x0][h+1] += dp[x][h];
}
}
}
}
for(int L = n; L >= 0; --L) {
for(int i = 0; i < (1 << L); i++) {
int x = (1 << L) | i;
if(dp[x][L] >= k) {
for(int h = L - 1; h >= 0; h--) {
cout << ((i >> h) & 1);
}
cout << endl;
return 0;
}
}
}
return 0;
}
| 0 | CPP |
n=int(input())
a=[]
b=[]
c=[]
for i in range(n):
x,y,z=map(int,input().split())
a.insert(i,x)
b.insert(i,y)
c.insert(i,z)
x,y,z=0,0,0
for i in range(n):
x+=int(a[i])
y+=int(b[i])
z+=int(c[i])
if x==0 and y==0 and z==0:
print("YES")
else:
print("NO")
| 7 | PYTHON3 |
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in a:
buf=i
ans+=format(buf,'b')[::-1].find('1')
print(ans)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const long long mod = 1000000007;
int main(){
int n;
cin >> n;
vector<int> a(1000001,0);
rep(i,n) {
int A;
cin >> A;
a[A] += 1;
}
//pairwise
bool flag1 = 1;
//setwise
bool flag2 = 1;
for(int i = 2; i <= 1000000; i++){
int cnt = 0;
for(int j = i; j <= 1000000; j += i){
if(a[j] > 0) cnt += a[j];
}
if(cnt >= 2) flag1 = 0;
if(cnt == n) flag2 = 0;
}
if(flag1) cout << "pairwise coprime" << endl;
else if (flag2) cout << "setwise coprime" << endl;
else cout << "not coprime" << endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const double pi = 3.141592653589793238462;
const int nmax = 200000;
const int hash_base = 119;
const long long inf = 1e9;
int n, a[nmax], b[nmax];
vector<pair<int, int> > ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
for (int i = 1; i <= n; i++)
if (a[i] != b[i]) {
for (int j = i + 1; j <= n; j++)
if (a[i] == b[j]) {
for (int k = j; k > i; k--) {
swap(b[k], b[k - 1]);
ans.push_back(make_pair(k - 1, k));
};
break;
};
};
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++)
printf("%d %d\n", ans[i].first, ans[i].second);
return 0;
}
| 10 | CPP |
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
#define SEG_NUM (1<<20)
pair<long, long> seg[SEG_NUM*2];
long find(int i){
i += SEG_NUM;
long ans = 2147483647;
int time = 0;
while(i > 0){
if(time < seg[i].first) {
ans = seg[i].second;
time = seg[i].first;
}
i /= 2;
}
return ans;
}
void update(int s, int t, long x, long time){
s += SEG_NUM;
t += SEG_NUM;
while(s < t){
if(s % 2 == 1){
seg[s].first = time;
seg[s].second = x;
s++;
}
s /= 2;
if(t % 2 == 1){
seg[t-1].first = time;
seg[t-1].second = x;
t--;
}
t /= 2;
}
}
int main() {
for(long i=0;i<2*SEG_NUM;++i){
seg[i] = make_pair(0, 2147483647);
}
int n, q;
cin>>n>>q;
long query, s, t, x, i;
for(int time=0;time<q;++time){
cin>>query;
if(query){
cin>>i;
cout<<find(i)<<endl;
}
else {
cin>>s>>t>>x;
update(s, t+1, x, time+1);
}
}
}
| 0 | CPP |
n = int(input())
A = [int(a) for a in input().split()]
x = max(A)
num = x/2
for i in range(n):
A[i] = [abs(A[i]-num), A[i]]
A.sort()
y = A[0][1]
print(x, y) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T power(T base, int p) {
T f = 1;
for (int i = 1; i <= p; i++) f *= base;
return f;
}
template <typename T>
string NumberToString(T Number) {
ostringstream ss;
ss << Number;
return ss.str();
}
int setbit(int N, int pos) { return N = N | (1 << pos); }
int resetbit(int N, int pos) { return N = N & ~(1 << pos); }
bool checkbit(int N, int pos) { return (bool)(N & 1 << pos); }
pair<long long int, long long int> extendedEuclid(int a, int b) {
if (b == 0) return pair<long long int, long long int>(1, 0);
pair<long long int, long long int> d = extendedEuclid(b, a % b);
return pair<long long int, long long int>(d.second,
d.first - d.second * (a / b));
}
int modularInverse(int a, int m) {
pair<long long int, long long int> ret = extendedEuclid(a, m);
return ret.first % m;
}
long long int gcd(long long int a, long long int b) {
long long int temp;
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
vector<long long int> v;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
long long int a;
cin >> a;
v.push_back(a);
}
long long int gd = v[0];
for (int i = 1; i < v.size(); i++) {
gd = gcd(gd, v[i]);
}
for (int i = 0; i < v.size(); i++) {
long long int a = v[i] / gd;
while (a % 2 == 0) a /= 2;
while (a % 3 == 0) a /= 3;
if (a != 1) {
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
}
| 9 | CPP |
mod = 998244353
N,A,B,K = map(int,input().split())
def cmb(n,k):
return fac[n]*inv_fac[n-k]*inv_fac[k] % mod
fac = [1]
for i in range(1,N+1):
fac.append(fac[-1]*i % mod)
inv_fac = [pow(fac[N],mod-2,mod)]
for i in range(1,N+1)[::-1]:
inv_fac.append(inv_fac[-1]*i % mod)
inv_fac = inv_fac[::-1]
ans = 0
for i in range(N+1):
if i*A > K:
break
if (K-i*A) % B == 0:
j = (K-i*A)//B
if j <= N:
ans += cmb(N,i)*cmb(N,j)
ans %= mod
print(ans) | 0 | PYTHON3 |
l=input("")
x=0
y=0
for i in l:
if i.islower():
x=x+1
else:
y=y+1
if x>=y:
print(l.lower())
else:
print(l.upper())
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int t, a;
scanf("%d", &t);
for (; t--;) {
scanf("%d", &a);
int cnt = __builtin_popcount(a);
printf("%d\n", 1 << cnt);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
char ch = getchar();
int f = 0;
x = 0;
while (!isdigit(ch)) {
if (ch == '-') f = 1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
if (f) x = -x;
}
const int N = 2e5 + 5;
int p[N], c[N], vis[N];
vector<int> v;
void dfs(int u, int fr, int k) {
vis[u] = 1;
if (u == fr && !k) return;
v.push_back(u), dfs(p[u], fr, 0);
}
signed main() {
int T;
read(T);
while (T--) {
int n, res = 1e9;
read(n);
for (int i = 1; i <= n; ++i) read(p[i]);
for (int i = 1; i <= n; ++i) read(c[i]);
for (int i = 1; i <= n; ++i) vis[i] = 0;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
dfs(i, i, 1);
int len = v.size();
for (int j = 1; j <= len; ++j) {
if (len % j) continue;
for (int k = 1; k <= j; ++k) {
int ok = 1, co = 0;
for (int q = k; q <= len; q += j) {
if (!co)
co = c[v[q - 1]];
else if (c[v[q - 1]] != co) {
ok = 0;
break;
}
}
if (ok) {
res = min(res, j);
break;
}
}
if (res <= j) break;
}
v.clear();
}
}
printf("%d\n", res);
}
return 0;
}
| 10 | CPP |
rng = int(input())
instr = list(input().lower())
chars = []
for i in range(26):
chars.append(0)
for char in range(rng):
chars[ord(instr[char])-97] += 1
isyes = True
for i in range(26):
if chars[i] == 0:
print('NO')
isyes = False
break
if isyes:
print('YES')
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int xa, ya, xb, yb, h;
char auxxa, auxxb;
string s[20];
int main() {
scanf("%c %d %c %d", &auxxa, &ya, &auxxb, &yb);
xa = auxxa - 96;
xb = auxxb - 96;
while (xa != xb && ya != yb) {
if (xb > xa) {
if (yb > ya) {
s[h] = "RU";
ya++;
xa++;
} else {
s[h] = "RD";
ya--;
xa++;
}
} else {
if (yb > ya) {
s[h] = "LU";
ya++;
xa--;
} else {
s[h] = "LD";
ya--;
xa--;
}
}
h++;
}
if (xa == xb) {
while (yb > ya) {
s[h] = "U";
ya++;
h++;
}
while (yb < ya) {
s[h] = "D";
ya--;
h++;
}
} else {
while (xb > xa) {
s[h] = "R";
xa++;
h++;
}
while (xb < xa) {
s[h] = "L";
xa--;
h++;
}
}
cout << h << endl;
for (int i = 0; i < h; i++) {
cout << s[i] << endl;
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long double x, y, z;
long double mx;
int ans;
int main() {
ios::sync_with_stdio(false);
cin >> x >> y >> z;
mx = INT_MIN;
long double val;
val = pow(y, z) * log(x);
;
if (val > mx) {
mx = val;
ans = 1;
}
val = pow(z, y) * log(x);
;
if (val > mx) {
mx = val;
ans = 2;
}
val = y * z * log(x);
;
if (val > mx) {
mx = val;
ans = 3;
}
val = pow(x, z) * log(y);
;
if (val > mx) {
mx = val;
ans = 5;
}
val = pow(z, x) * log(y);
;
if (val > mx) {
mx = val;
ans = 6;
}
val = x * z * log(y);
;
if (val > mx) {
mx = val;
ans = 7;
}
val = pow(x, y) * log(z);
;
if (val > mx) {
mx = val;
ans = 9;
}
val = pow(y, x) * log(z);
;
if (val > mx) {
mx = val;
ans = 10;
}
val = x * y * log(z);
;
if (val > mx) {
mx = val;
ans = 11;
}
switch (ans) {
case 1:
cout << "x^y^z" << endl;
break;
case 2:
cout << "x^z^y" << endl;
break;
case 3:
cout << "(x^y)^z" << endl;
break;
case 5:
cout << "y^x^z" << endl;
break;
case 6:
cout << "y^z^x" << endl;
break;
case 7:
cout << "(y^x)^z" << endl;
break;
case 9:
cout << "z^x^y" << endl;
break;
case 10:
cout << "z^y^x" << endl;
break;
case 11:
cout << "(z^x)^y" << endl;
break;
}
}
| 10 | CPP |
s = input()
ans = False
m = ["H", "Q", "9"]
for i in s:
if(i in m):
ans = True
break
if(ans):
print("YES")
else:
print("NO") | 7 | PYTHON3 |
t = int(input())
for _ in range(t):
n = int(input())
p = 3
while True:
if n % p == 0:
print(n // p)
break
p = p + p + 1
| 7 | PYTHON3 |
n=int(input())
x=input()
positionat = 0
space= 1;
while (positionat < n):
print(x[positionat],end="")
positionat=positionat+space
space=space+1
| 7 | PYTHON3 |
t=int(input())
for _ in range(t):
n=int(input())
l=[]
for i in range(1,n+1):
l.append(i)
print(*l) | 7 | PYTHON3 |
n=int(input())
for i in range(n):
s=abs(n//2-i)
print(s*'*'+((n//2-s)*2+1)*'D'+s*'*') | 7 | PYTHON3 |
def fact(x):
if x==1:
return 1
else:
return fact(x-1)*x
n, m = map(int, input().split())
if n > m:
print(fact(m))
else:
print(fact(n)) | 7 | PYTHON3 |
import sys
from sys import stdin
import math
#Find Set LSB = (x&(-x)), isPowerOfTwo = (x & (x-1))
def iinput():
return int(input())
def minput():
return map(int,input().split())
def linput():
return list(map(int,input().split()))
def fiinput():
return int(stdin.readline())
def fminput():
return map(int,stdin.readline().strip().split())
def flinput():
return list(map(int,stdin.readline().strip().split()))
n=iinput()
list1=linput()
dict1={}
for i in range(n):
if(list1[i] in dict1):
dict1[list1[i]]+=1
else:
dict1[list1[i]]=1
m=0
ele=0
for i in dict1:
if(dict1[i]>m):
m=dict1[i]
ele=i
ii=0
for i in range(n):
if(list1[i]==ele):
ii=i
break
ll=[]
for j in range(ii+1,n):
if(list1[j]!=ele):
if(list1[j]<list1[j-1]):
list1[j]=ele
ll.append([1,j+1,j])
else:
list1[j]=ele
ll.append([2,j+1,j])
for j in range(ii-1,-1,-1):
if(list1[j]!=ele):
if(list1[j]<list1[j+1]):
list1[j]=ele
ll.append([1,j+1,j+2])
else:
list1[j]=ele
ll.append([2,j+1,j+2])
print(len(ll))
for i in range(len(ll)):
print(*ll[i]) | 10 | PYTHON3 |
# b = input()
# t = []
# for i in range(int(b)):
# t.append(input())
# for a in t:
# l = len(a)
# if l <= 10:
# print(a)
# elif l > 10 & l < 100:
# first = a[0]
# last = a[-1]
# x = len(a) - 2
# z = "".join(str(first) + str(x) + str(last))
# print(z)
#
# x, y = map(int, input().split())
# t = input().split()
# k = []
# for a in t:
# if int(a) > y:
# k.append(a)
# j = []
#
# for a in t:
# if int(a) < y:
# result = 0
#
# for a in t:
# if int(a) == y:
# j.append(a)
# result = max(len(j), len(k))
#
# for a in t:
# if x == y:
# j.append(a)
# result = max(len(j), len(k))
#
# for a in t:
# if int(a) < y:
# j.append(a)
# result = len(j)-1
# print (result)
# x, y = map(int, input().split())
# t = input().split()
# k = []
# for a in t:
# if int(a) > y:
# k.append(a)
# l = []
# for i in t:
# cout = 0
# for j in t:
# l.append(j)
# if j == i:
# result = min(len(l), len(k))
# elif j == y:
# result = min(len(l), len(k))
# print (len(l), len(k))
#
# print (result)
# n, k = map(int, input().split())
# a = list(map(int, input().split()))
# ans = 0
# for x in a:
# if x > 0:
# ans += 1
#
# for x in a:
# cnt = 0
# for y in a:
# if y >= x:
# cnt += 1
# if cnt >= k:
# ans = min(ans, cnt)
#
# print(ans)
# k = input()
# k = k.lower()
# for char in 'aoyeui':
# k = k.replace(char, '')
# print ('.' + '.'.join(k))
# a, b = map(int, input().split())
# print (int((a * b) / 2))
n = input()
t = []
for i in range(int(n)):
a, b, c = map(int, input().split())
i = a, b, c
t.append(i)
k = 0
for j in t:
if sum(j) >= 2:
k += 1
print (k)
# for i in range(int(b)):
# t.append(input())
| 7 | PYTHON3 |
n,k =[*map(int, input().split())]
ans = int(k/n)
if k %n != 0:
ans+=1
print(ans) | 7 | PYTHON3 |
from collections import deque
h, w = map(int, input().split())
a = [input() + '#' for _ in range(h)]
b = ['#' * (w + 1)]
r = 0
for i in range(h):
for j in range(w):
if a[i][j] == '.':
b[:-1] = map(list, a)
b[i][j] = 0
q = deque([(i, j)])
while(q):
i, j = q.popleft()
r = max(r, b[i][j])
d = 0
for i1 in range(i - 1, i + 2):
for j1 in range(j - d, j + 2, 2):
if b[i1][j1] == '.':
b[i1][j1] = b[i][j] + 1
q.append((i1, j1))
d ^= 1
print(r)
| 0 | PYTHON3 |
x = 0
for i in range(int(input())):
s = input()
if("++" in s):
x += 1
else:
x -= 1
print(x) | 7 | PYTHON3 |
n,k=map(int,input().split())
l=list(map(int,input().split()))
ans=l[k-1]-l[0]+min(abs(l[k-1]),abs(l[0]))
for i in range(1,n-k+1):
ans=min(ans,l[i+k-1]-l[i]+min(abs(l[i+k-1]),abs(l[i])))
print(ans) | 0 | PYTHON3 |
N,M = map(int,input().split())
ans = 0
if M>=N*2:
ans = N
M -= N*2
else:
ans = M//2
M = 0
print(ans+M//4) | 0 | PYTHON3 |
def is_same(l1,l2):
if(len(l1)==len(l2)):
for i in range(0,len(l1)):
if(l1[i]!=l2[i]):
if(l1[i]>l2[i]):
return 'first'
else:
return 'second'
return True
else:
for i in range(0,min(len(l1),len(l2))):
if(l1[i]!=l2[i]):
if(l1[i]>l2[i]):
return 'first'
else:
return 'second'
t=int(input())
l1=[]#first
sum1=0
l2=[]#second
sum2=0
inp=0
for i in range(0,t):
inp=int(input())
if(inp<0):
sum2+=abs(inp)
l2.append(abs(inp))
else:
sum1+=inp
l1.append(inp)
if(sum2>sum1):
print('second')
elif(sum1>sum2):
print("first")
else:
check=is_same(l1,l2)
if(check==True):
if(inp>0):
print('first')
else:
print('second')
else:
print(check)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using ll = unsigned long long;
using namespace std;
int n, k;
ll fac[22], ans, tmp;
char s[22];
int cnt[22];
void dfs(int id, int sum, ll v) {
if (id >= 10) {
ans += fac[sum] / v;
return;
}
for (int i = 1; i <= cnt[id]; i++) {
dfs(id + 1, sum + i, v * fac[i]);
}
if (cnt[id] == 0) dfs(id + 1, sum, v);
}
void dfs2(int id, int sum, ll v) {
if (id >= 10) {
ans += fac[sum] / v;
return;
}
for (int i = 1; i <= cnt[id]; i++) {
dfs(id + 1, sum + i, v * fac[i]);
}
if (cnt[id] == 0 || id == 0) dfs(id + 1, sum, v);
}
int main() {
fac[0] = 1;
for (int i = 1; i <= 21; i++) fac[i] = fac[i - 1] * i;
scanf("%s", s);
for (int i = 0; s[i]; i++) cnt[s[i] - '0']++;
dfs(0, 0, 1);
tmp = ans;
if (!cnt[0]) return printf("%lld\n", ans), 0;
ans = 0;
cnt[0]--;
dfs2(0, 0, 1);
printf("%lld\n", tmp - ans);
}
| 11 | CPP |
n = int(input())
x, y, res, ans, ok = 0, n - 1, 0, 0, 0
a = list(map(int, input().split()))
while x <= y:
if ok == 0:
ok = 1
if a[x] > a[y]:
res += a[x]
x += 1
else:
res += a[y]
y -= 1
else:
ok = 0
if a[x] < a[y]:
ans += a[y]
y -= 1
else:
ans += a[x]
x += 1
print(res, ans) | 7 | PYTHON3 |
#include<stdio.h>
int main(void){
int H,W,a,b,c=0;
while(1){
scanf("%d %d",&H,&W);
if(c==1){printf("\n");}
else{c=1;}
for(a=0;a<H;a++){
for(b=0;b<W;b++){
if(a==0||a==H-1||b==0||b==W-1){
printf("#");}
else{printf(".");}
}
printf("\n");
}
if(H==0&&W==0){
break;}
}
return 0;
} | 0 | CPP |
t=int(input())
lista=[]
lista2=[]
for i in range(t):
evens=[]
odds=[]
x=int(input())
if x%4==2:
lista.append("NO")
continue
else:
lista.append("YES")
evens=[int(q) for q in range(1,x+1) if q%2==0]
for i in range(x//2):
if i<x//4:
odds.append(evens[i]-1)
else:
odds.append(evens[i]+1)
evens.extend(odds)
lista2.append(evens)
h=0
for i in lista:
print(i)
if i == "YES":
print(' '.join(map(str, lista2[h])))
h+=1
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mN = 110;
int c[mN], n;
void outall() {
int i, j;
for (i = 1; i <= n; i++)
for (j = 0; j < c[i]; j++) cout << i << ' ' << i << ' ';
}
int main() {
int i, w, tot = 0;
cin >> n >> w;
int p = 0;
for (i = 1; i <= n; i++) {
cin >> c[i];
if (c[i] == 1) p = i;
tot += c[i];
}
if (w <= 0 || w > tot || (n == 1 && w != tot) || (w == 1 && !p)) {
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
if (n == 1 && w == tot) {
outall();
cout << endl;
return 0;
}
int k;
if (p) {
c[p]--;
cout << p << ' ';
w = tot - w;
for (i = 1; i <= n; i++) {
if (c[i] >= w) {
c[i] -= w;
for (k = 0; k < w; k++) cout << i << ' ' << i << ' ';
w = 0;
break;
}
w -= c[i];
for (k = 0; k < c[i]; k++) cout << i << ' ' << i << ' ';
c[i] = 0;
}
cout << p << ' ';
outall();
cout << endl;
return 0;
}
cout << 1 << ' ';
w = tot - w;
c[1]--;
c[2]--;
for (i = n; i >= 2; i--) {
if (c[i] >= w) {
c[i] -= w;
for (k = 0; k < w; k++) cout << i << ' ' << i << ' ';
w = 0;
break;
}
w -= c[i];
for (k = 0; k < c[i]; k++) cout << i << ' ' << i << ' ';
c[i] = 0;
}
cout << 1 << ' ' << 2 << ' ';
if (w > 0) {
for (k = 0; k < w; k++) cout << 1 << ' ' << 1 << ' ';
c[1] -= w;
}
cout << 2 << ' ';
outall();
cout << endl;
return 0;
}
| 11 | CPP |
#from math import *
#from bisect import *
from collections import *
#from random import *
#from decimal import *"""
#from heapq import *
import sys
input=sys.stdin.readline
def inp():
return int(input())
def st():
return input().rstrip('\n')
def lis():
return list(map(int,input().split()))
def ma():
return map(int,input().split())
t=inp()
while(t):
t-=1
s=st()
n=len(s)
rem=''
vis=set()
for i in range(len(s)-1,-1,-1):
if(s[i] not in vis):
rem+=s[i]
vis.add(s[i])
rem=rem[::-1]
f=Counter(s)
flag=0
should=[0 for i in range(26)]
for i in range(len(rem)):
z=f[rem[i]]
if(z%(i+1)):
flag=1
break
should[ord(rem[i])-97]=z//(i+1)
if(flag):
print(-1)
continue
have=[0 for i in range(26)]
put=set(rem)
cur=''
for i in s:
if(len(put)==0):
break
cur+=i
z=ord(i)-97
have[z]+=1
if(have[z]==should[z]):
put.remove(i)
if(have[z]>should[z]):
flag=1
break
if(flag or len(put)):
print(-1)
continue
final=set()
lflag=0
cur1=cur[:]
for i in range(len(rem)):
final.add(rem[i])
for k in cur1:
if(k not in final):
cur+=k
if(len(cur)>n):
lflag=1
break
if(lflag):
break
if(cur==s):
print(cur1,rem)
else:
print(-1)
| 11 | PYTHON3 |
#include<bits/stdc++.h>
#define MAX 100000
#define MOD 1000000007
using namespace std;
int v[MAX],a[MAX];
bool check(int n)
{
for(int i=0;i<n;++i)
if(a[i]^v[i])
return false;
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
for(int i=0,x;i<n;++i)
{
cin>>x;
a[x]++;
++v[abs(n-1-(i<<1))];
}
if(!check(n))
cout<<"0"<<endl;
else
{
n >>= 1;
long long ans = 1;
for(int i=1;i<=n;++i)
ans <<=1, ans %= MOD;
cout<<ans<<endl;
}
return 0;
} | 0 | CPP |
n, m = map(int, input().split())
x = 0
y = 0
k = 0
for i in range(1, n + 1):
s = input()
if 'B' in s:
if k == 0:
x = i
y = s.find('B') + 1
k += 1
print(x + k // 2, y + k // 2)
| 7 | PYTHON3 |
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n ,a[10]={0}, ba=0;
char card[100];
while(1){
cin >> n;
if(n==0) break;
cin>>card;
for(int i=0;i<100;i++){
if(card[i]=='M') a[i%n]++;
if(card[i]=='S'){
ba+=a[i%n]++;
ba++;
a[i%n]=0;
}
if(card[i]=='L'){
a[i%n]+=ba;
a[i%n]++;
ba=0;
}
}
sort(a,a+n);
for(int i=0;i<n;i++){
cout << a[i] << " ";
a[i]=0;
}
cout << ba << endl;
ba=0;
}
return 0;
} | 0 | CPP |
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n;
long long ans = 0, p;
cin >> n;
for (long long i = 1; i <= n; i++) {
p = n/i;
ans += p*(p+1)/2*i;
}
cout << ans << endl;
} | 0 | CPP |
otv = int(input())
flag = True
n = otv
while flag:
if n % 10 != n // 1000 and n % 10 != n // 10 % 10 and n % 10 != n // 100 % 10 and n // 10 % 10 != n // 100 % 10 and n // 10 % 10 != n // 1000 and n // 100 % 10 != n // 1000 and n > otv:
print(n)
flag = False
else:
n += 1
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
void work() {
vector<unordered_set<int> > init;
int n, m;
cin >> n >> m;
init.resize(n);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
--x;
--y;
init[x].insert(y);
init[y].insert(x);
}
vector<int> perm;
for (int i = 0; i < n; ++i) {
perm.push_back(i);
}
vector<int> ans;
int best = inf;
srand(clock() + time(nullptr));
while (clock() < 0.95 * CLOCKS_PER_SEC) {
random_shuffle(perm.begin(), perm.end());
vector<int> temp;
vector<unordered_set<int> > now = init;
int left = n * (n - 1) / 2 - m;
for (int i = 0; i < n; ++i) {
if (left == 0) {
break;
}
int prev = left;
for (int j : now[perm[i]]) {
for (int k : now[perm[i]]) {
if (j == k || now[j].count(k)) {
continue;
}
now[j].insert(k);
now[k].insert(j);
--left;
}
}
if (prev > left) {
temp.push_back(perm[i]);
}
}
if (temp.size() < best) {
best = temp.size();
ans = temp;
}
}
cout << best << "\n";
for (int& x : ans) {
cout << x + 1 << " ";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
work();
}
| 9 | CPP |
Subsets and Splits