solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long n,l,t,x,w,ans[100001],p,s;
int main(){
scanf("%lld%lld%lld",&n,&l,&t);
for(long long i=1;i<=n;i++){
scanf("%lld%lld",&x,&w);
if(w==2)p=x-t;
else p=x+t;
if(p>0)s=(s+p/l)%n;
if(p<0)s=(s+(p+1)/l-1)%n;
ans[i]=(p%l+l)%l;
}
sort(ans+1,ans+n+1);
if(s>=0)s=s%n;
else{
p=(s+1)/n+1;
s=s+p*n;
}
for(long long i=s+1;i<=n;i++)printf("%lld\n",ans[i]);
for(long long i=1;i<=s;i++)printf("%lld\n",ans[i]);
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, emrow = 0, emcol = 0;
char a[1005][1005];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void In() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> a[i][j];
}
void checkrow() {
for (int i = 1; i <= n; i++) {
int tmp = 0;
for (int j = 1; j <= m; j++) {
if (a[i][j] == '#' && (j == 1 || a[i][j - 1] == '.')) tmp++;
}
if (tmp > 1) {
cout << -1;
exit(0);
}
if (tmp == 0) emrow++;
}
}
void checkcol() {
for (int i = 1; i <= m; i++) {
int tmp = 0;
for (int j = 1; j <= n; j++) {
if (a[j][i] == '#' && (j == 1 || a[j - 1][i] == '.')) tmp++;
}
if (tmp > 1) {
cout << -1;
exit(0);
}
if (tmp == 0) emcol++;
}
}
void Solve() {
checkrow();
checkcol();
if ((emcol > 0) != (emrow > 0)) {
cout << -1;
exit(0);
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == '#') {
ans++;
queue<pair<int, int> > qu;
qu.emplace(i, j);
while (!qu.empty()) {
int x = qu.front().first;
int y = qu.front().second;
qu.pop();
for (int t = 0; t < 4; t++) {
int u = x + dx[t];
int v = y + dy[t];
if (u >= 1 && v >= 1 && v <= m && u <= n && a[u][v] == '#') {
a[u][v] = '.';
qu.emplace(u, v);
}
}
}
}
cout << ans;
}
int main() {
In();
Solve();
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long pw(long long a, int n) {
if (n == 0) return 1;
if (n & 1) return pw(a, n - 1) * a;
long long b = pw(a, n >> 1);
return b * b;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << ((x2 - x1 + 1) * (y2 - y1 + 1) + 1) / 2;
return 0;
}
| 11 | CPP |
for _t in range(int(input())):
n = int(input())
s = input()
for i in range(n - 1):
if s[i] != s[i + 1]:
print(i + 1, i + 2)
break
else:
print(-1, -1)
| 7 | PYTHON3 |
n = int(input())
ans = 0
for i in range(1, n+1):
ans += i*(n//i)*(1+n//i)//2
print(ans)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
int main(void) {
int n, w;
int el[(4 * 100 * 1000)], be[(4 * 100 * 1000)];
int k[(4 * 100 * 1000)];
int i, j, z;
scanf("%d %d", &n, &w);
if (w == 1) {
printf("%d\n", n);
return 0;
}
n--;
for (i = 0; i <= n; i++) {
if (i == 0) {
scanf("%d", &z);
} else {
scanf("%d", &j);
be[i - 1] = j - z;
z = j;
}
}
w--;
for (i = 0; i <= w; i++) {
if (i == 0) {
scanf("%d", &z);
} else {
scanf("%d", &j);
el[i - 1] = j - z;
z = j;
}
}
i = 0;
j = -1;
k[0] = -1;
while (i < w) {
while (j >= 0 && el[i] != el[j]) j = k[j];
i++;
j++;
k[i] = j;
}
i = 0;
j = 0;
z = 0;
while (i < n) {
while (j >= 0 && be[i] != el[j]) j = k[j];
i++;
j++;
if (j == w) {
z++;
j = k[j];
}
}
printf("%d\n", z);
return 0;
}
| 10 | CPP |
for n in[*open(0)][1:]:
n=int(n);a=c=0
while n>=0:a+=1;n-=c*(c+1)//2;c=c*2+1
print(a-2) | 8 | PYTHON3 |
from collections import deque
def solve():
M = int(input())
G = [[] for i in range(M)]
deg = [0]*M
N = int(input())
for i in range(N):
x, y = map(int, input().split())
G[x-1].append(y-1)
deg[y-1] += 1
que = deque()
ans = []
for i in range(M):
if deg[i] == 0:
que.append(i)
while que:
v = que.popleft()
ans.append(v+1)
for w in G[v]:
deg[w] -= 1
if deg[w] == 0:
que.append(w)
print(*ans, sep='\n')
solve()
| 0 | PYTHON3 |
n,m = map(int,input().split())
print((m+n-1)//n) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int> > v(2 * n);
for (int i = 0; i <= n - 1; i++) {
int a, b;
cin >> a >> b;
v[2 * i] = make_pair(a, 1);
v[2 * i + 1] = make_pair(b + 1, -1);
}
sort(v.begin(), v.end());
bool ans = 1;
int cnt = 0;
for (typeof(v.begin()) it = v.begin(); it != v.end(); it++) {
if (it->second == 1)
cnt++;
else
cnt--;
if (cnt > 2) {
ans = 0;
break;
}
}
cout << ((ans == 1) ? "YES" : "NO");
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int as[MAX_N], cs[MAX_N];
int main() {
int n;
scanf("%d", &n);
int maxa = 0, maxc = 0;
for (int i = 0; i < n; i++) {
scanf("%d", as + i);
as[i]--;
cs[as[i]]++;
if (maxa < as[i]) maxa = as[i];
if (maxc < cs[as[i]]) maxc = cs[as[i]];
}
for (int a = 1; a <= maxa; a++)
if (cs[a - 1] < cs[a]) {
puts("-1");
return 0;
}
printf("%d\n", maxc);
for (int i = 0; i < n; i++) {
if (i) putchar(' ');
printf("%d", cs[as[i]]--);
}
putchar('\n');
return 0;
}
| 10 | CPP |
#include <iostream>
#include <queue>
#include <map>
using namespace std;
int main() {
queue <string> Q;
map <string, int> M;
string start = "01234567";
Q.push( start );
M[start] = 0;
int dr[4] = { 0, 0, 1, -1 };
int dc[4] = { 1, -1, 0, 0 };
while ( !Q.empty() ) {
string node = Q.front();
Q.pop();
for ( int i = 0; i < 8; i++ ) {
if ( node[i] != '0' ) continue;
for ( int k = 0; k < 4; k++ ) {
string next_node = node;
int c = i%4;
int r = i/4;
int nr = r + dr[k];
int nc = c + dc[k];
if ( nr < 0 || nr >= 2 || nc < 0 || nc >= 4 ) continue;
int ni = nr * 4 + nc;
swap( next_node[i], next_node[ni] );
if ( M.count( next_node ) ) continue;
M[next_node] = M[node] + 1;
Q.push( next_node );
}
}
}
int C[8];
while ( cin >> C[0] ) {
for ( int i = 1; i < 8; i++ ) cin >> C[i];
string s;
for ( int i = 0; i < 8; i++ ) s += (char)( C[i] + '0' );
cout << M[s] << endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool solve(string& s, string& t) {
int n = s.length();
vector<int> diffs;
for (int i = 0; i < n; ++i) {
if (s[i] != t[i]) {
diffs.push_back(i);
}
}
if (diffs.empty()) {
return true;
} else if (diffs.size() == 2) {
int a = diffs[0], b = diffs[1];
if (s[a] == s[b] && t[b] == t[a]) {
return true;
} else {
return false;
}
} else {
return false;
}
}
};
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0);
Solution sol;
int t;
cin >> t;
while (t-- > 0) {
int n;
cin >> n;
string s, t;
cin >> s >> t;
cout << (sol.solve(s, t) ? "Yes" : "No") << "\n";
}
return 0;
}
| 8 | CPP |
from sys import stdin
n,m=map(int,input().split())
s=[list(map(float,stdin.readline().strip().split()))[::-1] for i in range(n)]
s.sort()
s1=[int(i[1]) for i in s]
dp=[1 for i in range(n)]
ans=n+1
for i in range(n-1,-1,-1):
for j in range(i+1,n):
if s1[j]>=s1[i]:
dp[i]=max(dp[i],1+dp[j])
x=n-dp[i]
if x<ans:
ans=x
print(ans)
| 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int kMaxStrLen = 100000 + 16;
const int kMaxUnt = 1000 + 16;
struct Unit {
int coefficient;
bool pre;
bool operator<(const Unit& tmp_u) const {
return coefficient < tmp_u.coefficient;
}
};
int a;
char str[kMaxStrLen];
int u_cnt;
Unit unts[kMaxUnt];
int main() {
scanf("%d", &a);
scanf("%s", str);
int len = strlen(str);
u_cnt = 0;
for (int i = 0; i < len; i += 3) {
int fu = 1;
if (str[i] == '-') {
fu = -1;
i++;
} else if (str[i] == '+' &&
((i + 1 < len && str[i + 1] != '+') ||
(i + 2 < len && str[i + 1] == '+' && str[i + 2] == '+'))) {
fu = 1;
i++;
}
bool have_num = false;
int tmp_val = 0;
while (i < len && str[i] >= '0' && str[i] <= '9') {
have_num = true;
tmp_val = tmp_val * 10 + str[i] - '0';
i++;
}
if (have_num == false) {
tmp_val = 1;
}
unts[u_cnt].coefficient = fu * tmp_val;
if (i < len && str[i] == '*') {
i++;
}
if (i < len && str[i] == 'a') {
unts[u_cnt].pre = false;
} else {
unts[u_cnt].pre = true;
}
u_cnt++;
}
sort(unts, unts + u_cnt);
int ans = 0;
for (int i = 0; i < u_cnt; i++) {
ans += unts[i].coefficient * a;
if (unts[i].pre == true) {
ans += unts[i].coefficient;
}
a++;
}
printf("%d\n", ans);
return 0;
}
| 7 | CPP |
def solve(a, b):
count = 0;
while a % 8 == 0 and a // 8 >= b:
count += 1
a = a // 8
if (a < b):
break
while a % 4 == 0 and a // 4 >= b:
count += 1
a = a // 4
if (a < b):
break
while a % 2 == 0 and a // 2 >= b:
count += 1
a = a // 2
if (a < b):
break
if a != b:
return -1
return count
def solution():
i = input()
a = int(i.split(" ")[0])
b = int(i.split(" ")[1])
if a > b:
print(solve(a, b))
elif a < b:
print(solve(b, a))
elif a == b:
print(0)
t = int(input())
for i in range(t):
solution()
# input 10 8
# input 11 44
# input 17 21
# input 21 17
# input 1 1 | 7 | PYTHON3 |
t=int(input())
bar=[False for x in range(t)]
def turn(index):
if bar[index]==True:
return
bar[index]=True
num1=list(map(int,input().split()[1:]))
for i in num1:
turn(i-1)
num2=list(map(int,input().split()[1:]))
for i in num2:
turn(i-1)
if False not in bar:
print("I become the guy.")
else:
print("Oh, my keyboard!")
| 7 | PYTHON3 |
n = int(input())
if n < 3:
print(n)
elif n & 1:
print(n * (n - 1) * (n - 2))
elif n % 3:
print(n * (n - 1) * (n - 3))
else:
print((n - 1) * (n - 2) * (n - 3))
| 7 | PYTHON3 |
from math import gcd
n = int(input())
arr = list(map(int,input().split()))
def getans():
cnt1s = arr.count(1)
if cnt1s>0: return n-cnt1s
minlen = n
for i in range(n):
g = arr[i]
for j in range(i+1,n):
g = gcd(g,arr[j])
if g==1:
minlen = min(minlen,j-i)
break
if minlen==n: return -1
return (minlen+n-1)
print(getans()) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = int(1e9) + 7;
int n, dp[200005][2], sum[200005], arr[200005];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", arr + i);
dp[0][0] = 1;
for (int i = 1; i <= n; ++i) {
dp[i][0] = (dp[i - 1][1] + dp[i - 1][0]) % mod;
if (arr[i] != arr[i - 1]) {
dp[i][1] = sum[arr[i]];
sum[arr[i]] = (sum[arr[i]] + dp[i][0]) % mod;
}
}
printf("%d\n", (dp[n][0] + dp[n][1]) % mod);
} | 0 | CPP |
A=list(map(int,input().split()))
a=A[0]
b=A[1]
n=A[2]
while n>=0:
k=n
if n>a:k=a
while k>0:
if a%k==0 and n%k==0:
n-=k
break
k-=1
if n<0:
print("1")
break
if n==0:
print("0")
break
k=n
if n>b:k=b
while k>0:
if b%k==0 and n%k==0:
n-=k
break
k-=1
if n<0:
print("0")
break
if n==0:
print("1")
break | 7 | PYTHON3 |
n = int(input())
for i in range(n):
x,y = [int(i) for i in input().split(' ')]
a,b = [int(i) for i in input().split(' ')]
if 2*a<b:
print((x+y)*a)
elif x<y:
print(x*b+(y-x)*a)
else:
print(y*b+(x-y)*a) | 7 | PYTHON3 |
for _ in range(int(input())):
n, m, a, b = map(int, input().split())
if n * a != m * b:
print("No")
continue
print("Yes")
t = '1' * a + '0' * (m - a)
for i in range(n):
print(t)
t = t[m - a:] + t[:m - a] | 13 | PYTHON3 |
s=input()
s1=s.replace("144","***")
s2=s1.replace("14","**")
s3=s2.replace("1","*")
#print(s3)
for i in s3:
if(i!="*"):
print("NO")
break
else:
print("YES") | 7 | PYTHON3 |
str1=input().lower()
str2=input().lower()
for i in range(len(str1)):
if ord(str1[i])-ord(str2[i])>0:
print(1)
exit()
elif ord(str1[i])-ord(str2[i])<0:
print(-1)
exit()
print(0)
| 7 | PYTHON3 |
t=int(input())
for i in range(t):
n=int(input())
if n%2:
print(int((n+1)/2))
else:
print(int(n/2+1)) | 7 | PYTHON3 |
z,zz,dgraphs=input,lambda:list(map(int,z().split())),{}
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2))
def prime(x):
p=ceil(x**.5)+1
for i in range(2,p):
if x%i==0 and x!=2:return 0
return 1
def dfs(u,visit,graph):
visit[u]=True
for i in graph[u]:
if not visit[i]:
dfs(i,visit,graph)
################################################################################
"""
led=(6,2,5,5,4,5,6,3,7,6)
vowel={'a':0,'e':0,'i':0,'o':0,'u':0}
color4=["G", "GB", "YGB", "YGBI", "OYGBI" ,"OYGBIV",'ROYGBIV' ]
"""
###########################---START-CODING---####################################
num=int(z())
for _ in range(num):
n=int(z())
for i in range(n,0,-1):
print(i,end=' ')
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }
typedef long long ll;
int const inf = 1<<29;
enum Face : int { Top=0, Front=1, Right=2, Left=3, Back=4, Bottom=5 };
class cube {
private:
int f[6];
unordered_map<int, string> name_;
public:
cube(){}
/*
1(Top)
4(Left) 2(Front) 3(Right) 5(Back)
6(Bottom)
*/
cube(vector<int> const& vs){ assert(vs.size() == 6); for(int i=0; i<6; i++) f[i] = vs[i]; }
cube(vector<string> const& vs) { assert(vs.size() == 6); for(int i=0; i<6; i++) f[i] = i, name_[i] = vs[i]; }
void roll_z() { roll(1, 2, 4, 3); }
void roll_y() { roll(0, 2, 5, 3); }
void roll_x() { roll(0, 1, 5, 4); }
void roll(int i, int j, int k, int l) {
int t = f[i]; f[i] = f[j]; f[j] = f[k]; f[k] = f[l]; f[l] = t;
}
const int& operator[] (int idx) const { return f[idx]; }
const string& name(int idx) { return name_[f[idx]]; }
void roll(int k) {
if(k == 0) roll_y();
if(k == 1) roll_x();
if(k == 2) for(int i=0; i<3; i++) roll_y();
if(k == 3) for(int i=0; i<3; i++) roll_x();
}
bool roll_top_front(int top, int front) {
for(int k = 0; k < 6; (k & 1 ? roll_y() : roll_x()), k++)
for(int i = 0; i < 4; roll_z(), ++i)
if(top == f[Top] && front == f[Front]) return true;
return false;
}
bool operator== (cube const& rhs) const {
for(int i=0; i<6; i++)
if(f[i] != rhs.f[i]) return false;
return true;
}
bool isomorphic(cube rhs) const {
for(int k = 0; k < 6; (k & 1 ? rhs.roll_y() : rhs.roll_x()), k++)
for(int i = 0; i < 4; rhs.roll_z(), ++i)
if(operator==(rhs)) return true;
return false;
}
};
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
template<class T> constexpr bool in_range(T y, T x, T H, T W) { return 0<=y&&y<H&&0<=x&&x<W; }
int main() {
/*
top red
front magenta
right blue
left yellow
back green
bottom cyan
*/
/*
1(Top)
4(Left) 2(Front) 3(Right) 5(Back)
6(Bottom)
*/
for(int W, H; cin >> W >> H && (W|H);) {
vector<string> G(H); rep(i, H) cin >> G[i];
string S; cin >> S;
int sy, sx;
rep(i, H) rep(j, W)
if(G[i][j] == '#') sy = i, sx = j;
G[sy][sx] = 'w';
queue<tuple<cube, int, int, int>> q;
cube c({"red", "magenta", "blue", "yellow", "green", "cyan"});
q.emplace(c, sy, sx, 0);
static int dist[33][33][7][7][8];
rep(i, 33) rep(j, 33) rep(k, 7) rep(l, 7) rep(m, 8) dist[i][j][k][l][m] = inf;
dist[sy][sx][c[Top]][c[Front]][0] = 0;
while(!q.empty()) {
cube c; int y, x, col; tie(c, y, x, col) = q.front(); q.pop();
const int& Dist = dist[y][x][c[Top]][c[Front]][col];
const char& Target = S[col];
if(col == 6) {
cout << Dist << endl;
goto next_f;
}
rep(k, 4) {
const int ny = y + dy[k], nx = x + dx[k];
if(!in_range(ny, nx, H, W)) continue;
const char& NCell = G[ny][nx];
if(NCell == 'k') continue;
cube NCube = c; NCube.roll(k);
const char& NTopChar = NCube.name(Top)[0];
const int& NTopID = NCube[Top];
const int& NFrontID = NCube[Front];
if(NCell != 'w' && (NCell != Target || NTopChar != Target)) continue;
const int ncol = col + (NCell == Target && NTopChar == Target);
int& NDist = dist[ny][nx][NTopID][NFrontID][ncol];
if(NDist < inf) continue;
bool ng = 0;
rep(t, 7) rep(f, 7) rep(k, ncol) {
ng |= NCell != 'w' && (dist[ny][nx][t][f][k] < inf);
}
if(ng) continue;
NDist = Dist + 1;
q.emplace(NCube, ny, nx, ncol);
}
}
cout << "unreachable" << endl;
next_f:;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, x, a[511][511], m, pr[111111], sz, row[555], col[555], ans, cur,
p[222222];
void calc() {
for (int i = 2; i <= 200000; i++) {
bool t = true;
for (int j = 2; j <= int(sqrt(i)); j++) {
if (i % j == 0) {
t = false;
break;
}
}
if (t) {
sz++;
pr[sz] = i;
p[i] = 1;
}
}
}
int main() {
calc();
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
if (p[a[i][j]])
cur = 0;
else
cur = (pr[upper_bound(pr + 1, pr + sz + 1, a[i][j]) - pr] - a[i][j]);
col[j] += cur;
row[i] += cur;
}
ans = *min_element(col + 1, col + m + 1);
ans = min(ans, *min_element(row + 1, row + n + 1));
cout << ans;
return 0;
}
| 8 | CPP |
from collections import Counter
import operator
for t in range(int(input())):
n, k = [*map(int, input().split())]
cnt = Counter([e % k for e in [*map(int, input().split())]])
mod, times = 0, 0
for key, v in cnt.items():
if key != 0:
if v > times:
mod = key
times = v
elif v == times:
mod = min(mod, key)
if mod == 0:
print(0)
else:
print((times - 1) * k + (k - mod + 1)) | 10 | PYTHON3 |
def main():
n,m = input().split()
n,m = int(n),int(m)
i =1
while i <= n:
if i%m == 0:
n += 1
i +=1
print(n)
if __name__ == "__main__" : main() | 7 | PYTHON3 |
m=input()
if(m==m.upper()):
print(m.lower())
elif(m[1:]==m[1:].upper()):
print(m[0].upper()+m[1:].lower())
else:
print(m)
| 7 | PYTHON3 |
signcount=0
result=0
while True: ##cycle until done basically
try:
a=input()
except:
break
d=0
if a[0]=='+':
signcount+=1 #Checking for signs and counting
elif a[0]=='-':
signcount-=1
else:
for i in range (0,len(a)):
if a[i]==':' and d==0: #checking if no : is present, if so print result
result+=(len(a)-i-1)*signcount
d=1
print(result) | 7 | PYTHON3 |
n = int(input())
deno = [100, 20, 10, 5, 1]
i = 0
c = 0
while(n):
c += n // deno[i]
n %= deno[i]
i += 1
print(c) | 7 | PYTHON3 |
n = int(input())
lst = [int(i) for i in input().split()]
a = [-1] * n
visited = [-1] * 100001
flag = 0
for i in range(n):
if lst[i] > i+1:
flag = 1
break
visited[lst[i]]=1
not_v = list()
for i in range(100001):
if visited[i] == -1:
not_v.append(i)
for i in range(1, n):
if lst[i] != lst[i-1]:
a[i]=lst[i-1]
j = 0
for i in range(n):
if a[i]==-1:
a[i] = not_v[j]
j+=1
print(*a) | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int a[maxn], p[maxn];
int ft[maxn], n;
void update(int x, int dx) {
for (; x <= n; x += x & (-x)) ft[x] += dx;
}
int sum(int x) {
int res = 0;
for (; x; x -= x & (-x)) res += ft[x];
return res;
}
int ask(int a, int b) {
if (a > b) return 0;
return sum(b) - sum(a - 1);
}
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", p + i);
sort(p + 1, p + n + 1);
for (int i = 1; i <= n; i++) update(i, 1);
int lim = 29;
for (int i = 1; i <= n; i++) {
int lo = 1, hi = n;
for (int bit = lim; bit >= 0; bit--) {
int L = lo, R = hi;
while (L < R) {
int M = (L + R + 1) / 2;
if ((1 << bit) & p[M])
R = M - 1;
else
L = M;
}
if (p[L] & (1 << bit))
lo = L;
else if (ask(lo, L) <= 0)
lo = L + 1;
else if (ask(L + 1, hi) <= 0)
hi = L;
else if (a[i] & (1 << bit))
lo = L + 1;
else
hi = L;
}
printf("%d ", a[i] ^ p[lo]);
update(lo, -1);
}
printf("\n");
}
| 10 | CPP |
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <random>
#include <cstdlib>
#include <cmath>
#include <sstream>
using namespace std;
long long int dp[11][1001] = { 0 };
void q97() {
dp[0][0] = 1;
for (int i = 0; i <= 100; i++) {
for (int j = 10; j > 0; j--) {
for (int k = 1000; k >= i; k--) {
dp[j][k] += dp[j - 1][k - i];
}
}
}
int n, s;
for (; cin >> n >> s;) {
if (!n && !s) break;
cout << dp[n][s] << endl;
}
}
int main() {
q97();
return 0;
} | 0 | CPP |
def read_int():
return int(input().strip())
def read_ints():
return list(map(int, input().strip().split(' ')))
def solve():
M, N = read_ints()
return M*N//2
if __name__ == '__main__':
print(solve())
| 7 | PYTHON3 |
#include <iostream>
using namespace std;
int main(){
int N,x,s,l,t;
cin >> N;
int ans=N;
for(int i=0;i<=N;i++){
s=0;
l=i;
while(l>0){
t=l%6;
l=(l-t)/6;
s+=t;}
l=N-i;
while(l>0){
t=l%9;
l=(l-t)/9;
s+=t;}
if (s<ans){
ans=s;
}}
cout << ans << endl;
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, max = 0, c = 1;
cin >> n;
long long a[n];
cin >> a[0];
for (int i = 1; i < n; i++) {
cin >> a[i];
if (a[i] > a[i - 1])
c++;
else {
if (max < c) max = c;
c = 1;
}
}
if (max < c) max = c;
cout << max;
}
| 7 | CPP |
t = int(input())
for _ in range(t):
a, b, c, d = [int(x) for x in input().split()]
print("%d %d %d"% (b, c, c)) | 7 | PYTHON3 |
#71A Way too Long Word########
#16 march 21
# for _ in range(int(input())):
# a=input()
# n=len(a)
# if n<=10:
# print(a)
# else:
# print(a[0]+str(n-2)+a[n-1])
#1A Theater quare
#16 march 21
# import math
# n,m,a=map(int,input().split())
# print((math.ceil(n/a))*(math.ceil(m/a)))
#268A Games
#16 March 21
# s,a=0,[input(). split() for _ in range(int(input()))]
# for i in a:
# for j in a:
# if i[0]==j[1]: s+=1
# print(s)
#996A Hit the Lottery
#16 March 21
# n=int(input())
# a=[100,20,10,5,1]
# i=0
# c=0
# while (n!=0):
# if n>=a[i]:
# if n%a[i]!=0:
# n=n-a[i]
# # print(n)
# c+=1
# else :
# c=n//a[i]
# # print(a[i])
# break
# else:
# i+=1
# print(c)
# n=int(input())
# a=[100,20,10,5,1]
# s=0
# for i in range(len(a)):
# s+=n//a[i]
# n=n%a[i]
# print(s)
#1472A Cards for Friends
#17 3 21
# for _ in range(int(input())):
# a,b,n=map(int,input().split())
# c=1
# while(a%2==0):
# c*=2
# a=a//2
# while(b%2==0):
# c*=2
# b=b//2
# if n<=c:
# print("YES")
# else:
# print("NO")
#1369A FashionabLee
#17 3 21
# for _ in range(int(input())):
# n=int(input())
# if n%4==0:
# print("YES")
# else:
# print("NO")
#151A Soft Drinking
#17 3 21
a,b,c,d,e,f,g,h=map(int,input().split())
x=b*c
y=x//g
z=min(y,(d*e),(f//h))
print(int(z//a)) | 7 | PYTHON3 |
import sys
input = lambda : sys.stdin.readline().rstrip()
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))
# sys.setrecursionlimit(3*10**5+10)
def fs(n):
s = set()
for i in range(1,int(n**0.5)+2):
if n%i==0:
s.add(i)
s.add(n//i)
l = sorted(list(s))
return l
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = max(a).bit_length()
from math import gcd
vs = [0]*b
for v in a:
for i in range(b):
if v>>i&1:
vs[i] += 1
g = 0
for val in vs:
g = gcd(g, val)
if g==0:
res = list(range(1,n+1))
else:
res = fs(g)
write(" ".join(map(str, res))) | 7 | PYTHON3 |
#include <bits/stdc++.h>
const int N = 300000 + 5;
int n, m;
long long t;
int A[N];
bool dir[N];
int B[N];
std::pair<int, int> rel[N];
int answer[N];
long long calc(int a, int b) {
int tmp = b - a;
if (tmp < 0) tmp += m;
if (tmp >= 2 * t) return 0;
return (t * 2 - 1 - tmp) / m + 1;
}
void work() {
std::sort(rel, rel + n);
for (int i = 0; i < n; ++i) {
if (rel[i].second == 0) {
std::rotate(rel, rel + i, rel + n);
break;
}
}
for (int i = 0; i < n; ++i) {
if (dir[i] == 0) {
B[i] = ((A[i] - t) % m + m) % m;
} else {
B[i] = (A[i] + t) % m;
}
}
int val = B[0];
std::sort(B, B + n);
std::rotate(B, std::find(B, B + n, val), B + n);
if (B[0] == B[1] && dir[0] == 0) {
std::rotate(B, B + 1, B + n);
}
int count = 0;
for (int i = 1; i < n; ++i) {
if (dir[i] != dir[0]) {
long long tmp = 0;
if (dir[0]) {
tmp = calc(A[0], A[i]);
} else {
tmp = calc(A[i], A[0]);
}
count += tmp % n;
count %= n;
}
}
if (dir[0] == true) {
count = (n - count) % n;
}
std::rotate(B, B + count, B + n);
for (int i = 0; i < n; ++i) {
answer[rel[i].second] = B[i];
}
}
int main() {
scanf("%d%d%I64d", &n, &m, &t);
for (int i = 0; i < n; ++i) {
char str[2];
scanf("%d%s", A + i, str);
A[i]--;
dir[i] = str[0] == 'R';
rel[i] = std::make_pair(A[i], i);
}
work();
for (int i = 0; i < n; ++i) {
if (i) putchar(' ');
printf("%d", answer[i] + 1);
}
puts("");
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e6 + 10;
long long n, k, L, a[N], rank_[N], num[N], sum[N], sum2[N];
vector<long long> d[N];
vector<pair<long long, int> > st;
int main() {
cin >> n >> L >> k;
for (int i = 0; i < (int)k + 1; i++) {
for (int j = 0; j < (int)n + 1; j++) d[i].push_back(0);
}
for (int i = 0; i < (int)n; i++)
scanf("%I64d", &a[i]), st.push_back(pair<long long, int>(a[i], i));
sort(st.begin(), st.end());
int cnt = 0, cc = 0;
for (int i = 0; i < (int)n; i++) {
if (!i || st[i - 1].first != st[i].first) {
num[cnt] = cc;
cnt++;
cc = 1;
} else {
cc++;
}
rank_[st[i].second] = cnt;
}
num[cnt] = cc;
for (int i = 0; i <= k; i++)
for (int j = 1; j <= cnt; j++) {
if (i == 0) {
d[i][j] = 1;
continue;
}
d[i][j] = (d[i][j] + num[j] * d[i - 1][j]) % mod;
if (j != 1) d[i][j] = (d[i][j] + d[i][j - 1]) % mod;
}
long long all = L / n, yu = L % n, ans = 0;
for (int i = 1; i <= k; i++) {
sum[i] = sum2[i] = 0;
for (int j = 0; j < n; j++) sum[i] = (sum[i] + d[i - 1][rank_[j]]) % mod;
for (int j = 0; j < yu; j++) sum2[i] = (sum2[i] + d[i - 1][rank_[j]]) % mod;
}
for (int i = 1; i <= k; i++) {
ans = (ans + sum[i] * (max((long long)0, all - i + 1) % mod) % mod) % mod;
if (yu && all + 1 >= i) ans = (ans + sum2[i]) % mod;
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
q = int(input())
for i in range(q):
n = int(input())
l = list(map(int, input().split()))
l.sort()
done = 0
for j in range(n-1):
if(l[j]+1==l[j+1]):
done = 1
break
if done:
print(2)
else:
print(1)
| 7 | PYTHON3 |
# AOJ 0103: Baseball Simulation
# Python3 2018.6.17 bal4u
for i in range(int(input())):
p1 = p2 = p3 = out = cnt = 0;
while True:
s = input()
if s == 'HIT':
if p3 > 0: cnt += 1
p3, p2, p1 = p2, p1, 1
elif s == 'HOMERUN':
cnt += p1 + p2 + p3 + 1
p1 = p2 = p3 = 0
else:
out += 1
if out == 3: break
print(cnt)
| 0 | PYTHON3 |
primes = [False] * 2 + [True] * 999999
for p in range(2, 1000):
if primes[p]:
for i in range(2 * p, 1000001, p):
primes[i] = False
n = int(input())
lst = list(map(int, input().split()))
for each in lst:
a = each
b = int(a ** 0.5)
if a == b ** 2 and a != 1 and primes[b]:
print('YES')
else:
print('NO') | 8 | PYTHON3 |
from math import sin,pi
n,r = list(map(int, input().split()))
c = sin(pi/n)
print('%.7f' % (c/(1-c)*r))
| 9 | PYTHON3 |
import math
n=int(input())
k=n
i=2
l=[]
while i**2<=n:
while n%i==0:
n//=i
l.append(i)
i+=1
if n>1:
l.append(n)
print(k,end="")
print(":",*l)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool checkValid(char str[], int maxNum, int mode);
int main() {
char input[1000];
cin.get(input, 1000, '\n');
int numOfChar = 0;
int posOfChar;
char userName[1000], domain[1000], resource[1000];
int count = 0;
for (int i = 0; i < strlen(input); i++) {
if (input[i] == '@') {
numOfChar++;
posOfChar = i;
break;
} else if (numOfChar == 0) {
userName[count] = input[i];
count++;
}
}
userName[count] = '\0';
if (numOfChar == 0 || !checkValid(userName, 16, 1)) {
cout << "NO";
return 0;
}
count = 0;
numOfChar = 0;
for (int i = posOfChar + 1; i < strlen(input); i++) {
if (input[i] == '/') {
numOfChar++;
posOfChar = i;
break;
} else if (numOfChar == 0) {
domain[count] = input[i];
count++;
}
}
domain[count] = '\0';
if (!checkValid(domain, 32, 2)) {
cout << "NO";
return 0;
}
int countBeforeDot = 0;
int numberOfDot = 0;
for (int i = 0; i < strlen(domain); i++) {
if (domain[i] != '.') {
countBeforeDot++;
if (countBeforeDot > 16) {
cout << "NO";
return 0;
}
} else {
if (i == 0) {
cout << "NO";
return 0;
}
if (domain[i - 1] == '.') {
cout << "NO";
return 0;
}
countBeforeDot = 0;
numberOfDot++;
}
}
if (domain[count - 1] == '.') {
cout << "NO";
return 0;
}
if (numOfChar == 0) {
cout << "YES";
return 0;
}
count = 0;
for (int i = posOfChar + 1; i < strlen(input); i++) {
if (input[i] == '/') {
cout << "NO";
return 0;
} else if (numOfChar != 0) {
resource[count] = input[i];
count++;
}
}
resource[count] = '\0';
if (!checkValid(resource, 16, 1))
cout << "NO";
else
cout << "YES";
}
bool checkValid(char str[], int maxNum, int mode) {
if (!(strlen(str) >= 1 && strlen(str) <= maxNum)) return false;
for (int i = 0; i < strlen(str); i++)
if (mode == 1) {
if (!(((str[i] >= 65 && str[i] <= 90)) ||
(str[i] >= 97 && str[i] <= 122) || str[i] == '_' ||
(str[i] >= 48 && str[i] <= 57)))
return false;
} else if (!(((str[i] >= 65 && str[i] <= 90)) ||
(str[i] >= 97 && str[i] <= 122) || str[i] == '_' ||
str[i] == '.' || (str[i] >= 48 && str[i] <= 57)))
return false;
return true;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int b[32];
int f[10000];
int main() {
int n, c, i, j, k, l, r;
vector<long long> v[32];
vector<long long> w;
scanf("%d", &n);
int sum = 0;
for (i = 0;; i++) {
long long x = 1LL << i;
if (x > n * n * 2) break;
for (j = 0;; j++, x *= 3) {
long long y = x;
if (y > n * n * 2) break;
for (k = 0;; k++, y *= 5) {
long long z = y;
if (z > n * n * 2) break;
for (l = 0;; l++, z *= 7) {
long long w = z;
if (w > n * n * 2) break;
for (r = 0;; r++, w *= 11) {
int c = 0;
if (w > n * n * 2) break;
if (i > 0) c |= 1;
if (j > 0) c |= 2;
if (k > 0) c |= 4;
if (l > 0) c |= 8;
if (r > 0) c |= 16;
v[c].push_back(w);
}
}
}
}
}
if (n <= 50) {
c = 4;
} else if (n <= 300) {
c = 8;
} else if (n <= 2000) {
c = 16;
} else {
c = 32;
}
while (w.size() < n) {
int f = 0;
for (i = 1; i < c; i++) {
if (b[i] == v[i].size()) continue;
if (w.size() < n) {
w.push_back(v[i][b[i]++]);
f = 1;
}
}
if (f == 0) break;
}
for (i = 0; i < w.size(); i++) {
if (i > 0) putchar(' ');
printf("%I64d", w[i]);
}
puts("");
return 0;
}
| 9 | CPP |
months=['January','February','March','April','May',
'June','July','August','September','October',
'November','December']
month=input()
idx=months.index(month)
to=(int(input())+idx)%12
print(months[to])
| 7 | PYTHON3 |
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n,i,j;
long long r=0,g=0,b=0,res;
string s;
cin >> n >> s;
for(i=0;i<n;i++){
if(s[i]=='R') r++;
else if(s[i]=='G') g++;
else b++;
}
res=r*g*b;
for(i=0;i<n;i++)
for(j=1;i+2*j<n;j++)
if(s[i]!=s[i+j]&&s[i]!=s[i+2*j]&&s[i+j]!=s[i+2*j])
res--;
cout << res << endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MX = 100;
char s[MX][MX + 1];
int dp[MX][MX];
int main() {
int n, m;
ignore = scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) ignore = scanf("%s", s[i]);
dp[0][0] = (s[0][0] == '.' ? 0 : 1);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (i == 0 && j == 0) continue;
dp[i][j] = n * m;
if (i > 0) {
dp[i][j] = min(dp[i][j], dp[i - 1][j] + (s[i][j] != s[i - 1][j] ? 1 : 0));
}
if (j > 0) {
dp[i][j] = min(dp[i][j], dp[i][j - 1] + (s[i][j] != s[i][j - 1] ? 1 : 0));
}
}
int ans = (dp[n - 1][m - 1] + 1) / 2;
printf("%d\n", ans);
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename num_t>
inline void addmod(num_t& a, const long long& b, const int& m) {
a = (a + b) % m;
if (a < 0) a += m;
}
template <typename num_t>
inline void update_max(num_t& a, const num_t& b) {
a = max(a, b);
}
template <typename num_t>
inline void update_min(num_t& a, const num_t& b) {
a = min(a, b);
}
template <typename num_t>
num_t gcd(num_t lhs, num_t rhs) {
return !lhs ? rhs : gcd(rhs % lhs, lhs);
}
template <typename num_t>
num_t pw(num_t n, num_t k, num_t mod) {
long long res = 1;
for (; k; k >>= 1) {
if (k & 1) res = res * n % mod;
n = 1ll * n * n % mod;
}
return (num_t)res;
}
const int inf = 1e9 + 7;
const long long ll_inf = 1ll * inf * inf;
const int MAX_N = 300000 + 7;
const int mod = 998244353;
int n;
vector<int> adj[MAX_N];
long long even[MAX_N];
long long odd[MAX_N][2];
void dfs(int u, int r) {
for (int v : adj[u])
if (v != r) {
dfs(v, u);
}
static long long temp[2][2];
for (int p = (0), _b = (2); p < _b; ++p)
for (int q = (0), _b = (2); q < _b; ++q) temp[p][q] = 0;
temp[0][0] = 1;
temp[0][1] = 1;
for (int v : adj[u])
if (v != r) {
long long cur[2][2];
cur[0][0] = temp[0][0] * (even[v] + odd[v][0]) % mod;
cur[0][1] = temp[0][1] * (even[v] * 2 + odd[v][0]) % mod;
cur[1][0] = (temp[1][0] * (even[v] * 2 + odd[v][0]) +
temp[0][1] * (odd[v][0] + odd[v][1])) %
mod;
for (int p = (0), _b = (2); p < _b; ++p)
for (int q = (0), _b = (2); q < _b; ++q) temp[p][q] = cur[p][q];
}
even[u] = temp[1][0];
odd[u][0] = temp[0][0];
odd[u][1] = temp[0][1];
addmod(odd[u][1], -odd[u][0], mod);
}
void solve() {
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1);
int u = 1;
cout << (even[u] + odd[u][0]) % mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
const bool multiple_test = false;
int test = 1;
if (multiple_test) cin >> test;
for (int i = 0; i < test; ++i) {
solve();
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, n, k;
cin >> t;
while (t--) {
cin >> n >> k;
long long int a[n], b[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i] + k;
}
long long int *mini = min_element(b, b + n);
int flag = 0;
for (long long int i = 0; i < n; i++) {
if (abs(a[i] - (*mini)) <= k)
;
else
flag = -1;
}
if (flag == 0)
cout << *mini << "\n";
else
cout << "-1\n";
}
}
| 8 | CPP |
x,y=map(int,input() .split())
l,g='<>'
if x>y:x,y,l,g=y,x,g,l
c=g
if x==y or (x,y)==(2,4):c='='
elif x==1or (x,y)==(2,3):c=l
print(c)
| 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
const double EPS = 1e-8, PI = acos(-1);
inline bool eq(double a, double b)
{
return abs(b - a) < EPS;
}
#define curr(P, i) P[i]
#define next(P, i) P[(i+1)%P.size()]
#define prev(P, i) P[(i+P.size()-1) % P.size()]
enum { OUT, ON, IN };
namespace Geometory
{
struct Point
{
double x, y;
Point()
{
};
Point(double x, double y) : x(x), y(y)
{
};
Point operator+(const Point& b) const
{
return Point(x + b.x, y + b.y);
}
Point operator-(const Point& b) const
{
return Point(x - b.x, y - b.y);
}
Point operator*(const double b) const
{
return Point(x * b, y * b);
}
Point operator*(const Point& b) const
{
return Point(x * b.x - y * b.y, x * b.y + y * b.x);
}
Point operator/(const double b) const
{
return Point(x / b, y / b);
}
bool operator<(const Point& b) const
{
return x != b.x ? x < b.x : y < b.y;
}
bool operator==(const Point& b) const
{
return eq(x, b.x) && eq(y, b.y);
}
double norm()
{
return x * x + y * y;
}
double arg()
{
return atan2(x, y);
}
double abs()
{
return sqrt(norm());
}
Point rotate(double theta)
{
return Point(cos(theta) * x - sin(theta) * y, sin(theta) * x + cos(theta) * y);
}
Point rotate90()
{
return Point(-y, x);
}
friend ostream& operator<<(ostream& os, Point& p)
{
return os << "(" << p.x << "," << p.y << ")";
}
friend istream& operator>>(istream& is, Point& a)
{
return is >> a.x >> a.y;
}
};
struct Line
{
Point a, b;
Line()
{
};
Line(Point a, Point b) : a(a), b(b)
{
};
friend ostream& operator<<(ostream& os, Line& p)
{
return os << "(" << p.a.x << "," << p.a.y << ") to (" << p.b.x << "," << p.b.y << ")";
}
friend istream& operator>>(istream& is, Line& a)
{
return is >> a.a.x >> a.a.y >> a.b.x >> a.b.y;
}
};
struct Segment
{
Point a, b;
Segment()
{
};
Segment(Point a, Point b) : a(a), b(b)
{
};
friend ostream& operator<<(ostream& os, Segment& p)
{
return os << "(" << p.a.x << "," << p.a.y << ") to (" << p.b.x << "," << p.b.y << ")";
}
friend istream& operator>>(istream& is, Segment& a)
{
return is >> a.a.x >> a.a.y >> a.b.x >> a.b.y;
}
};
typedef vector< Point > Polygon;
typedef vector< Segment > Segments;
typedef vector< Line > Lines;
typedef pair< Point, Point > PointPoint;
double cross(const Point& a, const Point& b)
{
return a.x * b.y - a.y * b.x;
}
double dot(const Point& a, const Point& b)
{
return a.x * b.x + a.y * b.y;
}
int ccw(const Point& a, Point b, Point c)
{
b = b - a, c = c - a;
if(cross(b, c) > EPS) return +1; // a ??? b ???§ ????????¨??????????????????? c
if(cross(b, c) < -EPS) return -1; // a ??? b ???§ ?????¨??????????????????? c
if(dot(b, c) < 0) return +2; // c -- a -- b???§??????´??????
if(b.norm() < c.norm()) return -2; // a -- b -- c???§??????´??????
return 0; // a -- c -- b???§??????´??????
}
Point Projection(const Line& l, const Point& p)
{
double t = dot(p - l.a, l.a - l.b) / (l.a - l.b).norm();
return l.a + (l.a - l.b) * t;
}
Point Projection(const Segment& l, const Point& p)
{
double t = dot(p - l.a, l.a - l.b) / (l.a - l.b).norm();
return l.a + (l.a - l.b) * t;
}
Point Reflection(const Line& l, const Point& p)
{
return p + (Projection(l, p) - p) * 2.0;
}
double Distance(const Line& l, const Point& p)
{ //OK
return (p - Projection(l, p)).abs();
}
bool Intersect(const Line& l, const Line& m)
{
return abs(cross(l.b - l.a, m.b - m.a)) > EPS || abs(cross(l.b - l.a, m.b - l.a)) < EPS;
}
bool Intersect(const Line& l, const Segment& s)
{
return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}
bool Intersect(const Line& l, const Point& p)
{
return abs(ccw(l.a, l.b, p)) != -1;
}
bool Intersect(const Segment& s, const Segment& t)
{
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
bool Intersect(const Segment& s, const Point& p)
{
return ccw(s.a, s.b, p) == 0;
}
Point Crosspoint(const Segment& l, const Segment& m)
{ //OK
double A = cross(l.b - l.a, m.b - m.a);
double B = cross(l.b - l.a, l.b - m.a);
if(abs(A) < EPS && abs(B) < EPS) return m.a; // same line
return m.a + (m.b - m.a) * B / A;
}
Point Crosspoint(const Line& l, const Line& m)
{ //OK
double A = cross(l.b - l.a, m.b - m.a);
double B = cross(l.b - l.a, l.b - m.a);
if(abs(A) < EPS && abs(B) < EPS) return m.a;
return m.a + (m.b - m.a) * B / A;
}
};
int main()
{
int N, M;
cin >> N >> M;
vector< Geometory::Polygon > polygons(N);
for(int i = 0; i < N; i++) {
int L;
cin >> L;
polygons[i].resize(L);
for(int j = 0; j < L; j++) {
cin >> polygons[i][j];
}
}
vector< Geometory::Point > points(M);
for(int i = 0; i < M; i++) {
cin >> points[i];
}
vector< Geometory::Line > lines;
for(int i = 0; i < M; ++i) {
for(int j = 0; j < N; ++j) {
for(int l = 0; l < polygons[j].size(); ++l) {
lines.emplace_back(Geometory::Line(points[i], polygons[j][l]));
}
}
}
int ret = 1;
for(int i = 0; i < lines.size(); ++i) {
for(int j = 0; j < i; ++j) {
if(Geometory::Intersect(lines[i], lines[j])) {
auto pp = Geometory::Crosspoint(lines[i], lines[j]);
int cost = 0;
for(int m = 0; m < M; m++) {
auto seg = Geometory::Segment(pp, points[m]);
bool iscross = false;
for(int n = 0; n < N; n++) {
for(int o = 0; o < polygons[n].size(); o++) {
if(Geometory::Intersect(Geometory::Segment(curr(polygons[n], o), next(polygons[n], o)), seg)) {
auto qq = Geometory::Crosspoint(Geometory::Segment(curr(polygons[n], o), next(polygons[n], o)), seg);
if(curr(polygons[n], o) == qq) continue;
if(next(polygons[n], o) == qq) continue;
if(seg.a == qq || seg.b == qq) continue;
iscross = true;
}
}
}
cost += iscross ^ 1;
}
ret = max(ret, cost);
}
}
}
cout << ret << endl;
} | 0 | CPP |
lucky_list= [4,7,47,74,444,447,474,477,744,747,774,777]
n= int(input())
res="NO"
for i in lucky_list:
if n%i==0:
res= "YES"
break
print(res) | 7 | PYTHON3 |
def nod(a,b):
while a != b:
a, b = max(a,b), min(a,b)
if a % b == 0:
a -= (a // b - 1) * b
else:
a -= (a // b) * b
return a
for t in range(int(input())):
x, y, n = [int(x) for x in input().split()]
print(y + x * ((n-y)//x))
| 7 | PYTHON3 |
import sys,collections
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
t = inp()
for _ in range(t):
s = input()
cnt = [[0 for _ in range(10)] for i in range(10)]
fl = [[False for _ in range(10)] for i in range(10)]
for i,x in enumerate(s):
now = int(x)
for j in range(10):
if now == j: continue
fl[now][j] = True
for j in range(10):
if now == j: continue
if fl[j][now]:
cnt[j][now] += 2
fl[j][now] = False
# print(cnt)
res1 = 0
for i in range(10):
for j in range(10):
res1 = max(res1,cnt[i][j])
c = collections.Counter(s)
res2 = max(c.values())
# print(res1,res2)
res = max(res1,res2,2)
print(len(s) - res)
| 9 | PYTHON3 |
#------------------------------warmup----------------------------
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)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
from collections import deque
n=int(input())
l=list(map(int,input().split()))
d=deque([])
l.sort()
for i in range(n):
if i%2==0:
d.append(l[i])
else:
d.appendleft(l[i])
d=list(d)
f=0
for i in range(n):
if d[i]>=d[i-1]+d[(i+1)%n]:
f=1
break
if f==1:
print("NO")
else:
print("YES")
print(*d,sep=" ") | 8 | PYTHON3 |
t = list(map(int, input().split(" ")))
n = t[0]
k = t[1]
a = list(map(int, input().split(" ")))
t = set(a)
t = list(t)
m = 1
for i in range(len(t)):
if a.count(t[i])>k:
temp = a.count(t[i])//k
if temp*k!=a.count(t[i]):
temp = temp + 1
if temp>m:
m = temp
print(m*k*len(t)-n) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long int p, k;
vector<long long int> v;
long long int ans[80];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> p >> k;
v.resize(80);
int cur = 0;
while (p) {
v[cur++] = p % k;
p /= k;
}
for (int i = 1; i < 79; i += 2) {
if (v[i]) {
v[i] = k - v[i];
v[i + 1] += 1;
}
}
for (int i = 0; i < 79; i++) {
if (v[i] < 0) {
v[i] += k;
v[i + 1] += 1;
}
v[i + 1] -= (v[i] / k);
v[i] = v[i] % k;
ans[i] = v[i];
}
int d = 78;
for (int i = d; i >= 0; i--) {
if (ans[i]) {
d = i;
break;
}
}
cout << (d + 1) << '\n';
for (int i = 0; i <= d; i++) cout << ans[i] << ' ';
cout << '\n';
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int root[maxn];
int tin[maxn];
int tout[maxn];
bool vis[maxn];
vector<int> kids[maxn];
pair<int, int> docs[maxn];
pair<int, int> queries[maxn];
int timer = 0;
int get_root(int x) {
if (x == root[x]) return x;
return root[x] = get_root(root[x]);
}
void dfs(int v) {
if (vis[v]) return;
vis[v] = true;
tin[v] = timer++;
for (int u : kids[v]) dfs(u);
tout[v] = timer++;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) root[i] = i;
int dcnt = 0;
int qcnt = 0;
while (m--) {
int t;
cin >> t;
if (t == 1) {
int x, y;
cin >> x >> y;
x--;
y--;
kids[y].push_back(x);
root[x] = get_root(y);
} else if (t == 2) {
int x;
cin >> x;
x--;
docs[dcnt++] = {x, get_root(x)};
} else {
int x, i;
cin >> x >> i;
x--;
i--;
queries[qcnt++] = {x, i};
}
}
for (int i = 0; i < n; i++) dfs(get_root(i));
for (int q = 0; q < qcnt; q++) {
int x = queries[q].first;
int i = queries[q].second;
int c = docs[i].first;
int p = docs[i].second;
if (tin[p] <= tin[x] and tin[x] <= tout[p] and tin[x] <= tin[c] and
tin[c] <= tout[x])
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 11 | CPP |
t=int(input())
for _ in range(t):
n=int(input())
s=input()
ans=0
clockwiseflag=1
antiflag=1
for i in range(n):
if s[i]=='>':
antiflag=0
if s[i]=='<':
clockwiseflag=0
if clockwiseflag or antiflag:
print(n)
continue
for i in range(n):
if (s[i]=='-' or s[(i+1)%n]=='-') :
ans+=1
print(ans) | 8 | PYTHON3 |
n=int(input())
a=list(map(int,input().split()))
m=min(a)
m2=max(a)
if(m==m2):
print(0,(n*(n-1))//2)
else:
print(m2-m,a.count(m)*a.count(m2))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int marked[1000], viz[1000];
string cuvant[200];
vector<pair<char, char> > v;
vector<pair<int, int> > p;
bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
int main() {
int n, valid = 1;
cin >> n;
for (int i = 0; i < n; ++i) cin >> cuvant[i];
for (int i = 0; i < n - 1; ++i) {
int j = 0;
while (cuvant[i][j] == cuvant[i + 1][j] && j < cuvant[i].size() &&
j < cuvant[i + 1].size()) {
++j;
}
if (j == cuvant[i + 1].size() && j < cuvant[i].size()) valid = 0;
if (j != cuvant[i + 1].size() && j != cuvant[i].size()) {
char a = cuvant[i][j];
char b = cuvant[i + 1][j];
v.push_back(make_pair(a, b));
}
}
int next = 1;
int okk = 1;
while (okk == 1) {
okk = 0;
int t = 1;
for (char c = 'a'; c <= 'z' && t == 1; ++c) {
int ok = 1;
if (viz[c - 'a'] == 0) {
for (int j = 0; j < v.size(); ++j)
if (v[j].second == c && marked[j] == 0) ok = 0;
} else
ok = 0;
if (ok == 1) {
viz[c - 'a'] = next++;
for (int j = 0; j < v.size(); ++j) {
if (v[j].first == c) marked[j] = 1;
}
okk = 1;
t = 0;
}
}
}
for (int i = 0; i < 26; ++i) {
if (viz[i] == 0) valid = 0;
}
for (int i = 0; i < 26; ++i) {
p.push_back(make_pair(i, viz[i]));
}
sort(p.begin(), p.end(), cmp);
for (int i = 0; i < v.size(); ++i) {
if (viz[v[i].first - 'a'] > viz[v[i].second - 'a']) valid = 0;
}
if (valid == 0) {
cout << "Impossible";
return 0;
}
for (int i = 0; i < p.size(); ++i) cout << (char)('a' + p[i].first);
}
| 7 | CPP |
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
for(int n, q; cin >> n >> q, n;) {
vector<string> name(n);
vector<int> based(n), western(n);
for(int i = 0; i < n; ++i)
cin >> name[i] >> based[i] >> western[i];
for(int i = 0; i < q; ++i) {
int year;
cin >> year;
for(int i = 0; i < n; ++i)
if(western[i] - based[i] < year && year <= western[i]) {
cout << name[i] << " " << year - western[i] + based[i] << endl;
goto next;
}
cout << "Unknown" << endl;
next:;
}
}
return EXIT_SUCCESS;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
int cnt[26] = {0};
for (int i = 0; i < h * w; ++i) {
char c;
cin >> c;
cnt[c % 26]++;
}
int a = 0, b = 0;
for (int i = 0; i < 26; ++i) {
if (cnt[i] & 1) {
a++;
cnt[i]--;
}
if (cnt[i] % 4) {
b++;
}
}
int c = (h & 1) && (w & 1);
int d = (w & 1) * (h / 2) + (h & 1) * (w / 2);
cout << (a == c && b <= d ? "Yes" : "No") << endl;
}
| 0 | CPP |
t=int(input())
for _ in range(t):
S=input()
used=[0]*26
ans=[0]*26+[S[0]]+[0]*26
y_n="YES"
used[ord(S[0])-97]=1
i=26
for s in S[1:]:
if used[ord(s)-97]==1:
if ans[i+1]==s:
i+=1
elif ans[i-1]==s:
i-=1
else:
y_n="NO"
break
else:
if ans[i+1]==0:
ans[i+1]=s
i+=1
used[ord(s)-97]=1
elif ans[i-1]==0:
ans[i-1]=s
i-=1
used[ord(s)-97]=1
else:
y_n="NO"
break
if y_n=="NO":
print(y_n)
else:
print(y_n)
ans_=""
for a in ans:
if a!=0:
ans_+=a
for k in range(26):
if used[k]==0:
ans_+=chr(k+97)
print(ans_) | 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
bool num[maxn];
int main() {
int N;
scanf("%d", &N);
memset(num, false, sizeof(num));
int max_num = 0;
int flag_num = 0;
for (int i = 0; i < N * 2; i++) {
int flag;
scanf("%d", &flag);
if (num[flag] == false) {
num[flag] = true;
flag_num++;
} else {
num[flag] = false;
flag_num--;
}
max_num = max(max_num, flag_num);
}
printf("%d\n", max_num);
return 0;
}
| 7 | CPP |
#include "bits/stdc++.h"
using namespace std;
int main() {
long long A, B;
cin >> A >> B;
for (int i = 0; i < 2000; i++) {
if (i * 2 / 25 == A && i / 10 == B) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
} | 0 | CPP |
n=int(input())
lucky=0
while(n):
if n%10==4 or n%10==7:
lucky=lucky+1
n=n//10
if lucky==7 or lucky==4:
print('YES')
else:
print('NO')
| 7 | PYTHON3 |
n,k = input().split()
n = int(n)
k = int(k)
list = input().split()
s = 0
t = 0
m = k-1
p = list[m]
p = int(p)
while n >0:
a = list[s]
a = int(a)
if a >= p and a >0:
t = t+1
n = n-1
s = s+1
print(t)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 7;
const int mod = 1e9 + 7;
const int infi = (2e9 + 69);
const long long infy = (2e16 + 69);
const int base = 311;
const long long MM = 7ll * mod * mod;
const int addition = 1e5;
const double eps = 1e-8;
const string numd[10] = {"1110111", "0010010", "1011101", "1011011", "0111010",
"1101011", "1101111", "1010010", "1111111", "1111011"};
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const double pi = 3.14159265358980;
const string P1 = "Alice";
const string P2 = "Bob";
const string spmcode =
"My power does not end. Ask for aid and you will receive it.\n";
template <typename Num>
void maximize(Num &x, Num y) {
x = mx(x, y);
}
template <typename Num>
void minimize(Num &x, Num y) {
x = min(x, y);
}
void exit_with_runtime() {
double x = 1.0 * clock() / CLOCKS_PER_SEC;
cerr << "\n\n---------------------\nTime elapsed: " << x << " s.\n\n";
exit(0);
}
void decorate() {
cerr << "KienNguyen246\n";
chrono::system_clock::time_point today = chrono::system_clock::now();
time_t tt = chrono::system_clock::to_time_t(today);
cerr << ctime(&tt);
cerr << "\nProcessing...\n\n";
}
int m, n, a[505][505], f[505][505][20];
int G(int pos, int code) { return (((code >> pos) & 1) ? 15 : code); }
void Main_Process() {
cin >> m >> n;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++) {
char x;
cin >> x;
a[i][j] = (x == '*');
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= 9; k++) f[i][j][k] = -1;
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
f[i][j][1] =
a[i][j] + a[i][j + 1] * 2 + a[i + 1][j] * 4 + a[i + 1][j + 1] * 8;
for (int k = 2; (1 << k) <= min(m, n); k++)
for (int i = 1; i + (1 << k) - 1 <= m; i++)
for (int j = 1; j + (1 << k) - 1 <= n; j++)
for (int x = 0; x <= 15; x++) {
if (f[i][j][k - 1] == G(0, x) &&
f[i][j + (1 << (k - 1))][k - 1] == G(1, x) &&
f[i + (1 << (k - 1))][j][k - 1] == G(2, x) &&
f[i + (1 << (k - 1))][j + (1 << (k - 1))][k - 1] == G(3, x)) {
int g0 = G(0, x), g1 = G(1, x), g2 = G(2, x), g3 = G(3, x);
if (g0 == 0)
f[i][j][k] = 0;
else {
if (g0 == 15 && g1 == 15 && g2 == 15 && g3 == 15)
f[i][j][k] = 15;
else {
if (g0 != 15)
f[i][j][k] = g0;
else if (g1 != 15)
f[i][j][k] = g1;
else if (g2 != 15)
f[i][j][k] = g2;
else if (g3 != 15)
f[i][j][k] = g3;
}
}
}
}
int res = 0;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
for (int k = 2; k <= 9; k++) res += (f[i][j][k] != -1);
cout << res;
}
int main() {
ios_base::sync_with_stdio(0);
decorate();
int test = 1;
while (test--) {
Main_Process();
}
exit_with_runtime();
}
| 9 | CPP |
n = int(input())
ans = []
if n & 1 == 1:
for i in range(n):
if i + 1 != n:
ans.append((i+1, n))
n -= 1
for i in range(n):
for j in range(i+1, n+1):
if j != n - i and j != i + 1:
ans.append((i+1, j))
print(len(ans))
for i in range(len(ans)):
print(ans[i][0], ans[i][1]) | 0 | PYTHON3 |
#include<iostream>
using namespace std;
typedef struct Ant{
int position;
bool left;
}Ant;
int main(){
int n,l;
char c;
while(cin>>n>>l,n){
Ant a[n];
for(int i=0;i<n;i++){
cin>>c>>a[i].position;
if(c == 'L')
a[i].left = true;
else
a[i].left = false;
}
bool flag=false;
int ans=0;
int num;
while(1){
if(flag == true)
break;
//cout<<"KJLJ"<<endl;
flag = true;
ans++;
//for(int i=0;i<n;i++){
//cout<<"a["<<i<<"].left" << a[i].left<<endl;
//}
for(int i=0;i<n;i++){
if(a[i].left==false && (a[i].position!=l && a[i].position!=0)){
a[i].position++;
flag = false;
num = i;
//cout<<"i "<<i<<" ant pos "<<a[i].position<<" flag "<<a[i].left<<endl;
}
}
for(int i=0;i<n;i++){
if(a[i].left == true && a[i].position != l && a[i].position!=0){
a[i].position--;
flag = false;
num = i;
//cout<<"i "<<i<<" ant pos "<<a[i].position<<" flag "<<a[i].left<<endl;
}
//for i end
}
for(int j=0;j<n && flag == false;j++){
for(int k=j+1;k<n;k++){
if(a[j].position == a[k].position){
//cout<<"j "<<j<<" k "<<k<<endl;
a[j].left = !a[j].left;
a[k].left = !a[k].left;
}
}
}
//while(1) end
}
cout<<ans-1<<" "<<num+1<<endl;
}
return 0;
} | 0 | CPP |
t = int(input())
for i in range(t):
n = int(input())
sticks = n // 2
if n % 2 != 0:
sticks = sticks + 1
print(int(sticks)) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fr(i, a, b) for (int i=a; i<(b); i++)
#define FOR(i, a) for (int i=0; i<(a); i++)
#define bck(i,a,b) for (int i = (b)-1; i >= a; i--)
#define BACK(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
bool comp(vector<ll>&a, vector<ll>&b){
return a[1] < b[1] ;
}
void solve(){
int n ; cin>> n;
string s,t; cin>> s>>t ;
int one = 0 , zero= 0 ;
vector<int>pos(n,0) ;
int i= 0 ;
for (auto x : s ) {(x=='0' ? zero ++ : one ++ ); if (zero==one) pos[i] =1 ; i+=1 ;}
bool flag = 0 ;
for (int i= n-1 ;i>=0; i-=1) {
if (pos[i]==1) {
if (flag) {
if (s[i] == t[i]) flag = 0 ;
}
else {
if (s[i] != t[i]) flag =1 ;
}
}
else {
if (flag) {
if (s[i] == t[i]) {
cout << "NO"<<endl;
return ;
}
}
else {
if (s[i] != t[i]) {
cout << "NO"<<endl;
return ;
}
}
}
}
cout << "YES" <<endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t ;
// t=1 ;
cin>>t ;
// int i=1 ;
while (t--){
solve();
// cout <<endl;
// i+=1 ;
}
} | 8 | CPP |
n = int(input())
s = input()
a,d = s.count('A'),s.count('D')
if a==d:
print("Friendship")
elif a>d:
print("Anton")
else:
print("Danik") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x;
cin >> n >> x;
int a[n];
int b[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
sort(a, a + n);
sort(b, b + n, greater<int>());
int f = 0;
for (int i = 0; i < n; i++) {
if (a[i] + b[i] > x) {
f = 1;
break;
}
}
if (f == 1)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
}
| 7 | CPP |
n = int(input())
given = list(map(int, input().split()))
target = sorted(given)
result = 0
pos = 0
while pos < n:
result += 1
min_value_given = max_value_given = given[pos]
min_count_given = max_count_given = 1
min_value_target = max_value_target = target[pos]
min_count_target = max_count_target = 1
while min_value_given != min_value_target or \
min_count_given != min_count_target or \
max_value_given != max_value_target or \
max_count_given != max_count_target:
pos += 1
if pos == n:
break
g, t = given[pos], target[pos]
if g < min_value_given:
min_value_given = g
min_count_given = 1
elif g == min_value_given:
min_count_given += 1
if g > max_value_given:
max_value_given = g
max_count_given = 1
elif g == max_value_given:
max_count_given += 1
if t < min_value_target:
min_value_target = t
min_count_target = 1
elif t == min_value_target:
min_count_target += 1
if t > max_value_target:
max_value_target = t
max_count_target = 1
elif t == max_value_target:
max_count_target += 1
pos += 1
print(result)
| 9 | PYTHON3 |
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define MOD 1000000007
using namespace std;
int n, m, k;
int quickPow(int n, int m) {
int res = 1;
while (m) {
if (m & 1) res = 1LL * res * n % MOD;
m >>= 1;
n = 1LL * n * n % MOD;
}
return res;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
if (m > k) swap(m, k);
int ans = quickPow(3, m + k), tmp1 = 1, tmp2 = 1;
for (int p = n + 1, e, u = 1, v = 1; p <= n + m + k; ++p) {
e = quickPow(3, n + m + k - p);
v = 1LL * v * (p - 1) % MOD * quickPow(p - n, MOD - 2) % MOD;
if (p <= n + m) u = u * 2 % MOD;
else if (p <= n + k) {
tmp1 = p - n - 1 == m ? 1 : 1LL * tmp1 * (p - n - 1) % MOD * quickPow(p - n - m - 1, MOD - 2) % MOD;
u = (2LL * u - tmp1 + MOD) % MOD;
}
else {
tmp1 = p - n - 1 == m ? 1 : 1LL * tmp1 * (p - n - 1) % MOD * quickPow(p - n - m - 1, MOD - 2) % MOD;
tmp2 = p == (1 + n + k) ? 1 : 1LL * tmp2 * (p - 1 - n) % MOD * quickPow(p - n - 1 - k, MOD - 2) % MOD;
u = (2LL * u - tmp1 - tmp2 + MOD + MOD) % MOD;
}
ans = (ans + 1LL * e * v % MOD * u % MOD) % MOD;
}
printf("%d\n", ans);
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, c = 0;
cin >> n >> x;
for (int i = 1; i <= n; i++) {
if (x % i == 0 and x / i <= n) {
c++;
}
}
cout << c << endl;
return 0;
}
| 7 | CPP |
t = int(input())
for i in range(t):
a,b = map(int,input().split())
if a==1:
print(0)
elif a ==2:
print(b)
else:
print(b*2) | 7 | PYTHON3 |
n = int(input())
a = 0
b = 0
for i in range(n):
l = list(map(int,input().split()))
if l[0] > l[1]:
a += 1
elif l[0] < l[1]:
b += 1
else:
continue
if a > b:
print("Mishka")
elif a < b:
print("Chris")
else:
print("Friendship is magic!^^") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long int INF = 1000000000 + 7;
const double pi = 3.141592653589;
long long int power(int base, int power) {
long long int res = 1;
while (power > 0) {
if (power % 2 == 1) res = (res * base) % INF;
power = power / 2;
base = (base * base) % INF;
}
return res;
}
long long int gcd(long long int A, long long int B) {
if (B == 0)
return A;
else
return gcd(B, A % B);
}
long long int lcm(long long int A, long long int B) {
return (A * B) / gcd(A, B);
}
int main() {
int n;
scanf("%d", &n);
int a[5] = {0}, x;
for (long long int i = 0; i < n; i++) {
scanf("%d", &x);
a[x]++;
}
int cars = a[4];
int k = min(a[1], a[3]);
cars += k;
a[1] -= k;
a[3] -= k;
cars += a[3];
cars += (a[2] / 2);
if (a[2] % 2 != 0) {
cars++;
a[1] = a[1] - min(a[1], 2);
}
cars = cars + (a[1] + 3) / 4;
cout << cars;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int INF = 10000000000000;
long long int a, b, x, y;
long long int gcd(long long int a, long long int b) {
long long int r = a % b;
while (r) {
a = b;
b = r;
r = a % b;
}
return b;
}
void input() {
cin >> a >> b >> x >> y;
long long int tmp_x = x / gcd(x, y);
long long int tmp_y = y / gcd(x, y);
long long int l = 0, r = 2e9 + 7;
while (l <= r) {
long long int mid = (l + r) >> 1;
if (mid * tmp_x <= a && mid * tmp_y <= b)
l = mid + 1;
else
r = mid - 1;
}
cout << tmp_x * (l - 1) << " " << tmp_y * (l - 1);
}
int main() {
input();
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
int a[MAXN];
int b[31];
vector<int> pows[31];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
for (int j = 31; j >= 0; --j) {
if ((a[i] & (1 << j)) > 0) {
pows[j].push_back(a[i]);
}
}
}
for (int j = 30; j >= 0; --j) {
if (pows[j].size()) {
sort(pows[j].begin(), pows[j].end());
pows[j].erase(unique(pows[j].begin(), pows[j].end()), pows[j].end());
int mand = pows[j][0];
for (int i = 0; i < (int)pows[j].size(); ++i) {
mand &= pows[j][i];
}
assert(mand > 0);
if ((mand % (1 << j)) == 0) {
cout << pows[j].size() << endl;
for (int i = 0; i < (int)pows[j].size(); ++i) {
cout << pows[j][i] << " ";
}
return 0;
}
}
}
cout << -1 << endl;
return 0;
}
| 9 | CPP |
for i in range(int(input())):
n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
scc, sz = [-1] * n, [0] * n
for i in range(n):
u = i
while scc[u] == -1:
scc[u] = i
sz[i] += 1
u = p[u]
print(*[sz[scc[x]] for x in range(n)])
| 8 | PYTHON3 |
# cook your dish here
# code
# ___________________________________
# | |
# | |
# | _, _ _ ,_ |
# | .-'` / \'-'/ \ `'-. |
# | / | | | | \ |
# | ; \_ _/ \_ _/ ; |
# | | `` `` | |
# | | | |
# | ; .-. .-. .-. .-. ; |
# | \ ( '.' \ / '.' ) / |
# | '-.; V ;.-' |
# | ` ` |
# | |
# |___________________________________|
# | |
# | Author : Ramzz |
# | Created On : 21-07-2020 |
# |___________________________________|
#
# _ __ __ _ _ __ ___ ________
# | '__/ _` | '_ ` _ \|_ /_ /
# | | | (_| | | | | | |/ / / /
# |_| \__,_|_| |_| |_/___/___|
#
import math
import collections
from sys import stdin,stdout,setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2**20)
t = 1
t = int(stdin.readline())
for _ in range(t):
#n = int(stdin.readline())
#s = stdin.readline().strip('\n')
x1,y1,z1 = list(map(int, stdin.readline().rstrip().split()))
x2,y2,z2 = list(map(int, stdin.readline().rstrip().split()))
ans = 0
ans += 2*min(z1,y2)
rem1 = y2 - min(z1,y2)
y1 -= (rem1+x2)
if(y1<=0):
print(ans)
continue
else:
print(ans-(2*y1))
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long l, r, k, tmp, ans;
void dfs(long long i, long long sum, int now, int cnt) {
if (cnt > k) return;
if (i == r + 1) {
if (ans > sum && cnt != 0) {
ans = sum;
tmp = now;
}
return;
}
dfs(i + 1, sum, now, cnt);
dfs(i + 1, sum ^ i, now | (1 << (i - l)), cnt + 1);
}
int main() {
scanf("%I64d%I64d%I64d", &l, &r, &k);
if (r - l + 1 <= 10) {
int count = 0, t;
ans = inf;
dfs(l, 0, 0, 0);
t = tmp;
printf("%I64d\n", ans);
while (t != 0) {
if (t & 1) ++count;
t >>= 1;
}
printf("%d\n", count);
count = 0;
while (tmp != 0) {
if (tmp & 1) printf("%I64d ", l + count);
tmp >>= 1;
++count;
}
printf("\n");
} else {
if (k == 1) printf("%I64d\n1\n%I64d\n", l, l);
if (k == 2) {
if (l & 1) l++;
printf("1\n2\n%I64d %I64d\n", l, l + 1);
}
if (k == 3) {
long long x, y, z, m, u;
x = 1;
m = 0;
y = 1;
u = 1;
while (u <= l) {
u *= 2;
++m;
}
x = u + u / 2;
y = u - 1;
z = u + (u / 2 - 1);
if (x <= r)
printf("0\n3\n%I64d %I64d %I64d\n", y, z, x);
else {
if (l & 1) ++l;
printf("1\n2\n%I64d %I64d\n", l, l + 1);
}
}
if (k >= 4) {
if (l & 1) ++l;
printf("0\n4\n%I64d %I64d %I64d %I64d\n", l, l + 1, l + 2, l + 3);
}
}
return 0;
}
| 10 | CPP |
n = int(input())
l = list(map(int, input().split()))
positive = 0
negative = 0
for i in l:
if(i>0):
positive += 1
elif(i<0):
negative += 1
required = 0
if (n%2 == 0):
required = n/2
else:
required = n//2 + 1
if(positive >= required):
print (1)
elif(negative >= required):
print (-1)
else:
print (0) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long f = 1, sum = 0;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
sum = sum * 10 + c - '0';
c = getchar();
}
return sum * f;
}
const int MAXN = 400010;
struct node {
unsigned long long hshst, hshed;
int len;
} f[4 * MAXN];
void build(int root, int left, int right) {
f[root].len = right - left + 1;
if (left == right) return;
int mid = (left + right) >> 1;
build(2 * root, left, mid);
build(2 * root + 1, mid + 1, right);
}
unsigned long long Pow[MAXN];
void up(int root) {
f[root].hshst =
f[2 * root].hshst * Pow[f[2 * root + 1].len] + f[2 * root + 1].hshst;
f[root].hshed =
f[2 * root + 1].hshed * Pow[f[2 * root].len] + f[2 * root].hshed;
}
void update(int root, int left, int right, int x, char c) {
if (left == right) {
f[root].hshst = f[root].hshed = c - 'a' + 1;
return;
}
int mid = (left + right) >> 1;
if (x <= mid)
update(2 * root, left, mid, x, c);
else
update(2 * root + 1, mid + 1, right, x, c);
up(root);
}
node query(int root, int left, int right, int qleft, int qright) {
if (qleft <= left && right <= qright) return f[root];
int mid = (left + right) >> 1;
if (qright <= mid)
return query(2 * root, left, mid, qleft, qright);
else if (qleft > mid)
return query(2 * root + 1, mid + 1, right, qleft, qright);
else {
node ret1, ret2;
ret1 = query(2 * root, left, mid, qleft, mid);
ret2 = query(2 * root + 1, mid + 1, right, mid + 1, qright);
node ret;
ret.len = ret1.len + ret2.len;
ret.hshst = ret1.hshst * Pow[ret2.len] + ret2.hshst;
ret.hshed = ret2.hshed * Pow[ret1.len] + ret1.hshed;
return ret;
}
}
char s[MAXN];
char ans[MAXN];
bool get_ans;
int n, d;
void dfs(int x, bool small) {
if (x == n + 1) {
get_ans = small;
return;
}
for (char c = (small ? 'a' : s[x]); !get_ans && c <= 'z'; c++) {
update(1, 1, n, x, c);
ans[x] = c;
if (x >= d) {
node ret = query(1, 1, n, x - d + 1, x);
if (ret.hshst == ret.hshed) continue;
}
if (x >= d + 1) {
node ret = query(1, 1, n, x - d, x);
if (ret.hshst == ret.hshed) continue;
}
dfs(x + 1, small || (c > s[x]));
}
}
int main() {
d = read();
scanf("%s", s + 1);
n = strlen(s + 1);
Pow[0] = 1;
for (int i = 1; i <= n; i++) Pow[i] = Pow[i - 1] * 23333;
build(1, 1, n);
dfs(1, 0);
if (get_ans)
cout << ans + 1;
else
cout << "Impossible";
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int h, w;
cin >> h >> w;
set<pair<int, int>> p;
multiset<int> val;
for (int i = 1; i <= w; ++i) {
p.insert({i, i});
val.insert(0);
}
for (int i = 1; i <= h; ++i) {
int a, b;
cin >> a >> b;
auto l = p.lower_bound({a, 0});
auto r = p.upper_bound({b, w+5});
int end = b+1, start = -1;
if (b+1 > w) end = 1e15;
while (l != r) {
int s = (*l).first, f = (*l).second;
l = p.erase(l);
val.erase(val.find(s-f));
start = max(start, f);
}
if (~start) {
val.insert(end-start);
p.insert({end, start});
}
int ans = *val.begin();
if (ans > 1e11) ans = -1;
else ans += i;
cout << ans << '\n';
}
}
| 0 | CPP |
t=int(input())
for tc in range(t):
n=int(input())
print(n if len(set(list(map(int,input().split()))))==1 else 1) | 7 | PYTHON3 |
length = input()
length = int(length)
a = input()
a = str(a)
first = a[0]
last = a[length-1]
def thefirst(string):
counter = 0
while counter < length:
if first == string[counter]:
counter += 1
else:
break
return counter
def thelast(string):
counter = length-1
while counter > -1:
if last == string[counter]:
counter -= 1
else:
break
#print (counter)
return length-(counter+1)
#print (thefirst(a))
#print (thelast(a))
if first == last:
print (((thefirst(a)+1)*(thelast(a)+1)) % 998244353)
else:
print ((thefirst(a)+thelast(a)+1) % 998244353) | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int a, b, c, i;
} p[100100], d[1001000];
bool cmp(node a, node b) {
if (a.a == b.a) {
if (a.b == b.b) return a.c > b.c;
return a.b < b.b;
}
return a.a < b.a;
}
int vis[100100];
int main() {
int n;
scanf("%d", &n);
int ans1 = 0, flag1, ans = 0, flag2, flag3;
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &p[i].a, &p[i].b, &p[i].c), p[i].i = i + 1;
if (min(min(p[i].a, p[i].b), p[i].c) > ans1)
ans1 = min(min(p[i].a, p[i].b), p[i].c), flag1 = i + 1;
}
long long aa[3];
int sum = 0;
for (int i = 0; i < n; i++) {
aa[0] = p[i].a, aa[1] = p[i].b, aa[2] = p[i].c;
sort(aa, aa + 3);
int a = aa[0], b = aa[1], c = aa[2];
d[sum].a = a, d[sum].b = b, d[sum].i = i, d[sum++].c = c;
d[sum].a = a, d[sum].b = c, d[sum].i = i, d[sum++].c = b;
d[sum].a = c, d[sum].b = b, d[sum].i = i, d[sum++].c = a;
}
sort(d, d + sum, cmp);
int xx = d[0].a, yy = d[0].b, l = 0;
for (int i = 1; i < sum; i++) {
int a = d[i].a, b = d[i].b;
if (a != xx || b != yy || i == sum - 1) {
if (i == sum - 1 && a == xx && b == yy) i++;
int mx = -1, mmx = -1, mj, mjj;
for (int j = l; j < i; j++) {
if (vis[d[j].i]) continue;
vis[d[j].i] = 1;
if (d[j].c >= mx)
mmx = mx, mx = d[j].c, mjj = mj, mj = d[j].i;
else if (d[j].c > mmx)
mmx = d[j].c, mjj = d[j].i;
}
for (int j = l; j < i; j++) vis[d[j].i] = 0;
if (mx != -1 && mmx != -1) {
mx += mmx;
if (ans < min(min(xx, yy), mx))
ans = min(min(xx, yy), mx), flag2 = mj, flag3 = mjj;
}
l = i;
xx = a, yy = b;
}
}
if (ans == 0 || ans < ans1)
cout << 1 << '\n' << flag1 << '\n';
else
cout << 2 << '\n' << flag2 + 1 << " " << flag3 + 1 << '\n';
return 0;
}
| 10 | CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.