solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
long long n, k;
void solve() {
cin >> n >> k;
long long m = n - 1;
long long d = m / k;
long long ans = 2 * d;
if (m % k == 1)
ans++;
else if (m % k > 1)
ans += 2;
cout << ans << endl;
for (long long i = 1; i < n; i++) {
if (i <= k)
cout << 1 << " " << i + 1 << endl;
else
cout << i + 1 << " " << (i + 1) - k << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
| 8 | CPP |
stones = input()
instructions = input()
num_instructions = len(instructions)
pointer = 0
n = 0
# RGB
# RRR
# 2
while pointer != num_instructions:
if stones[n] == instructions[pointer]:
n += 1
pointer += 1
continue
pointer += 1
print(n+1) | 7 | PYTHON3 |
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<double> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip ;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int sosu[1000001]={1,1,0};
int main(){
for(int i=2;i*i<=1000001;i++)if(sosu[i]==0)
for(int j=i*2;j<1000001;j+=i)sosu[j]=true;
rep(i,1000001)sosu[i]^=1;
rep(i,1000000)sosu[i+1]+=sosu[i];
int n;
while(cin>>n,n){
int out=0;
rep(i,n){
int a,b;
cin>>a>>b;
int s=max(0,a-b-1),t=min(a+b,1000000);
int co=sosu[t]-sosu[s];
if(co==0)out--;
else out+=co-1;
}
cout<<out<<endl;
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int A[N];
int side[N];
char gt(int s) { return s == 0 ? 'L' : 'R'; }
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> A[i];
sort(A + 1, A + 1 + n);
int need = 0;
for (int i = 1; i <= n; ++i) {
char ch;
cin >> ch;
if (ch == 'L')
side[i] = 0;
else
side[i] = 1;
if (i > 1 && side[i] != side[i - 1]) ++need;
}
int ptr1 = n - need + 1, ptr2 = n - need;
int s1 = side[1] ^ 1, s2 = side[1];
for (int i = 1; i <= n; ++i) {
if (i > 1 && side[i] != side[i - 1]) {
s2 ^= 1;
cout << A[++ptr2] << ' ' << gt(s2) << '\n';
} else {
s1 ^= 1;
cout << A[--ptr1] << ' ' << gt(s1) << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
while (t--) solve();
}
| 7 | CPP |
t = int(input())
for _ in range(t):
n = int(input())
mass = [int(i) for i in input().split()]
b = []
k = 0
for i in range(len(mass)):
if mass[i] % 2:
b.append(i+1)
k += 1
if k // 2 == n-1:
for j in range(0, len(b)-1, 2):
print(b[j], b[j+1])
break
else:
if k % 2:
b.pop()
k -= 1
i = 0
while k // 2 != n-1:
if mass[i] % 2 == 0:
b.append(i+1)
k += 1
i += 1
for j in range(0, len(b)-1, 2):
print(b[j], b[j + 1])
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t = 1;
while (t--) {
long long n;
cin >> n;
long long x;
long long k = 0;
for (long long i = 0; i < n; i++) {
cin >> x;
if (x % 2 == 0)
cout << x / 2 << endl;
else if (k % 2 == 0) {
cout << (x + 1) / 2 << endl;
k++;
} else {
cout << (x - 1) / 2 << endl;
k++;
}
}
}
}
| 7 | CPP |
import sys,math
from collections import defaultdict
def get_ints():
return map(int, I().split())
def get_list():
return list(map(int, I().split()))
def I():
return sys.stdin.readline().strip()
for _ in range(int(I())):
a,b,n=get_ints()
temp=min(a,b)
count=0
while a<=n:
a,b=a+b,max(a,b)
count+=1
print(count)
| 7 | PYTHON3 |
n=int(input())
arr=[int(i) for i in input().split()]
brr=arr[:]
brr.sort()
sarr=[];sbrr=[];s1=0;s2=0
for i in range(n):
s1+=arr[i]
s2+=brr[i]
sarr.append(s1)
sbrr.append(s2)
m=int(input())
for i in range(m):
a,l,r=map(int,input().split())
if a==1:
if l==1:
print(sarr[r-1])
else:
print(sarr[r-1]-sarr[l-2])
else:
if l==1:
print(sbrr[r-1])
else:
print(sbrr[r-1]-sbrr[l-2])
| 8 | PYTHON3 |
t=int(input())
for _ in range(t):
a,b=map(int,input().split())
c=0
large=max(a,b)
small=min(a,b)
while(small<large):
if large%2==0:
large=large//2
c+=1
else:
break
#print(small,large)
if large==small:
if c%3==0:
print(c//3)
else:
print((c//3)+1)
else:
print(-1)
| 7 | PYTHON3 |
# Crazy Town (2 points on same/opposite side of a line)
# input
k1 = list(map(int, input().split()));
(x1,y1) = (k1[0],k1[1])
k2 = list(map(int, input().split()));
(x2,y2) = (k2[0],k2[1])
t = int(input())
d = 0;
for i in range(t):
k3 = list(map(int, input().split()));
(a,b,c) = (k3[0],k3[1],k3[2])
#
k = (a*(x1) + b*(y1) + c)/ (a*(x2) + b*(y2) + c)
if k<0:
d +=1;
print(d);
| 7 | PYTHON3 |
string=input()
lower=0
upper=0
for _ in string:
if(_==_.lower()):
lower+=1
else:
upper+=1
if(upper>lower):
for _ in string:
print(_.upper(),end='')
else:
for _ in string:
print(_.lower(),end='') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 4;
const long long MOD = 1e9 + 7;
const long long MX = (long long)1e18;
long long a, b, c, d;
long long get(long long k) {
if (k < 0) return 0;
long long ans = (k * (k + 1) * d * b) / 2ll - (k + 1ll) * a;
return max(0ll, -ans);
}
int main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int ttt;
cin >> ttt;
while (ttt--) {
cin >> a >> b >> c >> d;
if (a > c * b) {
cout << -1 << endl;
continue;
}
long double minima =
-((long double)d * (long double)b - (long double)2 * (long double)a) /
((long double)2 * (long double)d * (long double)b);
long long day1 = 0;
long long day2 = c / d;
long long day3 = min(day2, (long long)minima);
long long day4 = min(day2, day3 + 1);
cout << max({get(day1), get(day2), get(day3), get(day4)}) << endl;
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 51;
int n, k, s, t;
ll C[N][N], ways[N];
vector <int> a;
ll solve(vector <int> &b){
ll ret = 0;
for(int mask = 0 ; mask <= (t ^ s) ; mask++){
if((mask & (t ^ s)) != mask) continue;
int sign = (__builtin_popcount(mask) % 2 ? -1 : 1);
int cnt = 0;
for(auto &i : b){
cnt += (i & mask) == 0;
}
ret += sign * ways[cnt];
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
C[0][0] = 1;
for(int i = 1 ; i < N ; i++){
for(int j = 0 ; j < N ; j++){
C[i][j] = (j == 0 ? 0 : C[i - 1][j - 1]) + C[i - 1][j];
}
}
cin >> n >> k >> s >> t;
k--;
if((s & t) != s) finish(0);
for(int i = 0 ; i < n ; i++){
int x;
cin >> x;
if((x & s) != s) continue;
if((x | t) != t) continue;
a.push_back(x);
}
n = a.size();
for(int i = 0 ; i < N ; i++){
for(int j = 0 ; j <= k ; j++){
ways[i] += C[i][j];
}
}
ll ans = 0;
for(int i = 0 ; i < n ; i++){
vector <int> b;
for(int j = i + 1 ; j < n ; j++){
b.push_back(a[i] ^ a[j]);
}
ans += solve(b);
}
cout << ans << endl;
}
| 0 | CPP |
for _ in range(int(input())):
N,K=map(int,input().split())
L=list(map(int,input().split()))
S=set()
for i in L:
S.add(i)
if len(S)>K:
print(-1)
continue
print(N*K)
for i in range(N):
for j in S:
print(j,end=" ")
for k in range(0,K-len(S)):
print(1,end=" ")
print() | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long INF = (long long)10 * MOD;
long double dp[110][110][110];
int main() {
std::ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<pair<int, int> > x(n);
for (int i = 0; i < n; ++i) {
cin >> x[i].first;
x[i].second = i + 1;
}
sort(x.begin(), x.end());
vector<int> a(n), toInit(n), fromInit(n + 1);
for (int i = 0; i < n; ++i) {
toInit[i] = x[i].second;
fromInit[x[i].second] = i;
a[i] = x[i].first;
}
while (m--) {
int current, l;
cin >> current >> l;
current = fromInit[current];
auto it = upper_bound(a.begin() + current, a.end(), a[current] + l);
int pos = (--it) - a.begin();
l -= a[pos] - a[current];
current = pos;
bool isRightStep = false;
bool wasPrev = true;
while (true) {
int pos;
if (isRightStep) {
auto it = upper_bound(a.begin(), a.end(), a[current] + l);
pos = (--it) - a.begin();
} else {
pos = lower_bound(a.begin(), a.end(), a[current] - l) - a.begin();
}
int d = abs(a[pos] - a[current]);
int cnt = 0;
if (d > 0) {
cnt = l / d;
if (cnt & 1) {
isRightStep ^= true;
current = pos;
}
l %= d;
} else {
isRightStep ^= true;
}
if (cnt > 0) {
wasPrev = true;
} else {
if (!wasPrev) {
break;
}
wasPrev = false;
}
}
cout << toInit[current] << endl;
}
return 0;
}
| 10 | CPP |
n = int(input())
print(str(n) + ' ' + ' '.join(str(i) for i in range(1, n))) | 7 | PYTHON3 |
n=input()
n1=input()
n=n.lower()
n1=n1.lower()
if n<n1:
print('-1')
elif n>n1:
print('1')
elif n==n1:
print('0') | 7 | PYTHON3 |
n=int(input())
a=list(map(int,input().split()))
s,q,z=sum(a),0,0
for i in range(n-1):
s-=a[i]
q+=a[i]
if s==q:z+=1
print(z) | 9 | PYTHON3 |
n = int(input())
s = input()
cnt = 0
for i in s:
if i == '0':
cnt += 1
if '1' in s:
print('1',end = '')
for i in range(cnt):
print('0',end = '')
else:
print('0',end = '') | 7 | PYTHON3 |
n, x, y = map(int, input().split())
s = sorted(list(map(int, input().split())))
if x <= y:
c = 0
for i in range(n):
if s[i] <= x:
c += 1
print((c+1)//2)
else:
print(n)
| 9 | PYTHON3 |
import math
from collections import defaultdict
n = int(input())
ipt = [list(map(int,input().split())) for i in range(n)]
xy = sorted(ipt)
convh1 = [xy[0],xy[1]]
def slope(xy1,xy2,const = 1):
x1,y1 = xy1
x2,y2 = xy2
dx,dy = x2-x1,y2-y1
if dx == 0:
return 10**18*(y2-y1)*const//abs(y2-y1)
else:
return dy/dx
def convex(xy1,xy2,xy3,const = 1):
x1,y1 = xy1
x2,y2 = xy2
x3,y3 = xy3
if slope([x1,y1],[x2,y2],const) < slope([x2,y2],[x3,y3],const):
return True
return False
for i in range(2,n):
x,y = xy[i]
while len(convh1) >= 2 and not convex(convh1[-2],convh1[-1],[x,y]):
convh1.pop()
convh1.append(xy[i])
convh2 = [xy[-1],xy[-2]]
for i in range(n-2)[::-1]:
x,y = xy[i]
while len(convh2) >= 2 and not convex(convh2[-2],convh2[-1],[x,y],-1):
convh2.pop()
convh2.append(xy[i])
convh = convh1+convh2[1:-1]
prob = [0 for i in convh]
dc = defaultdict(int)
for i,xy in enumerate(convh):
x,y = xy
px,py = convh[i-1]
nx,ny = convh[(i+1)%len(convh)]
px -= x
py -= y
nx -= x
ny -= y
rad = math.atan2(ny,nx)-math.atan2(py,px)
dc[(x,y)] = (math.pi-min(abs(rad),2*math.pi-abs(rad)))/(2*math.pi)
for x,y in ipt:
print(dc[(x,y)]) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int H, W, N;
cin >> H >> W >> N;
map<pair<long long int, long long int>, long long int>MLL;
for( int i = 0; i < N; i++ ) {
long long int a, b;
cin >> a >> b;
a--; b--;
for( int j = -1; j <= 1; j++ ) {
for( int k = -1; k <= 1; k++ ) {
MLL[make_pair( a + j, b + k )]++;
}
}
}
vector<long long int>ans( 10 );
for( auto n : MLL ) {
if( 1 <= n.first.first&&n.first.first < H - 1 && 1 <= n.first.second&&n.first.second < W - 1 ) {
//cout << n.first.first << " " << n.first.second << endl;
ans[n.second]++;
}
}
ans[0] = (H - 2)*(W - 2);
for( size_t i = 1; i < 10; i++ ) {
ans[0] -= ans[i];
}
for( size_t i = 0; i < 10; i++ ) {
cout << ans[i] << endl;
}
} | 0 | CPP |
def main():
w = int(input())
if w % 2 == 0:
if w != 2:
print('YES')
else:
print('NO')
else:
print('NO')
main()
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[301], t[301];
int dp[2][150001];
int sp[18][150001];
int n, m, d, ans = 1e9, tt;
long long vv = 0;
int rr[150001];
void build(int x) {
for (int i = 1; i <= n; i++) sp[0][i] = dp[x][i];
int temp = 31 - __builtin_clz(n);
for (int i = 1; i <= temp; i++)
for (int j = 1; j + (1 << i) - 1 <= n; j++)
sp[i][j] = min(sp[i - 1][j], sp[i - 1][j + (1 << (i - 1))]);
}
int f2(int x, int y) {
int temp = rr[y - x + 1];
return min(sp[temp][x], sp[temp][y - (1 << temp) + 1]);
}
int main() {
scanf("%d %d %d", &n, &m, &d);
for (int i = 1; i <= m; i++) scanf("%d %d %d", &a[i], &tt, &t[i]), vv += tt;
for (int i = 0; i <= 17; i++) rr[1 << i] = 1;
rr[0] = -1;
for (int i = 1; i <= n; i++) rr[i] += rr[i - 1];
for (int i = 1; i <= n; i++) dp[1][i] = abs(a[1] - i);
for (int i = 2; i <= m; i++) {
build((i - 1) & 1);
int tt = min((long long)n, (long long)d * (t[i] - t[i - 1]));
for (int j = 1; j <= n; j++)
dp[i & 1][j] = f2(max(1, j - tt), min(n, j + tt)) + abs(a[i] - j);
}
printf("%I64d\n", vv - *min_element(dp[m & 1] + 1, dp[m & 1] + n + 1));
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class V, int ME>
class BIT {
public:
V bit[1 << ME];
V operator[](int e) {
if (e < 0) return 0;
V s = 0;
e++;
while (e) s += bit[e - 1], e -= e & -e;
return s;
}
void add(int e, V v) {
e++;
while (e <= 1 << ME) bit[e - 1] += v, e += e & -e;
}
};
BIT<signed long long, 20> bt;
template <class V, int NV>
class SegTree_1 {
public:
vector<V> val;
static V const def = 1LL << 60;
V comp(V l, V r) { return min(l, r); };
SegTree_1() { val = vector<V>(NV * 2, 1LL << 60); };
V getval(int x, int y, int l = 0, int r = NV, int k = 1) {
if (r <= x || y <= l) return def;
if (x <= l && r <= y) return val[k];
return comp(getval(x, y, l, (l + r) / 2, k * 2),
getval(x, y, (l + r) / 2, r, k * 2 + 1));
}
void update(int entry, V v) {
entry += NV;
val[entry] = v;
while (entry > 1)
entry >>= 1, val[entry] = comp(val[entry * 2], val[entry * 2 + 1]);
}
};
int N, Q;
signed long long AS;
set<int> S;
SegTree_1<signed long long, 1 << 20> penalty;
void update(int cur) {
if (cur <= 1 || cur >= N) return;
S.erase(cur);
penalty.update(cur, 1LL << 60);
if (bt[cur] < bt[cur - 1] && bt[cur] < bt[cur + 1]) {
S.insert(cur);
} else {
penalty.update(cur,
max({0LL, bt[cur - 1] - bt[cur], bt[cur + 1] - bt[cur]}));
}
}
signed long long hoge(int cur, int add) {
signed long long ret = 0;
ret -= abs(bt[cur] - bt[cur - 1]);
ret += abs(bt[cur] + add - bt[cur - 1]);
ret -= abs(bt[cur] - bt[cur + 1]);
ret += abs(bt[cur] + add - bt[cur + 1]);
return ret;
}
void solve() {
int i, j, k, l, r, x, y;
string s;
scanf("%d", &N);
int pre = 0;
for (i = 1; i <= N; i++) {
scanf("%d", &x);
bt.add(i, x - pre);
if (i > 1) AS += abs(x - pre);
pre = x;
}
for (i = 2; i <= N - 1; i++) update(i);
S.insert(N + 1);
S.insert(N + 2);
scanf("%d", &Q);
while (Q--) {
int T, L, R, X;
scanf("%d%d%d%d", &T, &L, &R, &X);
if (T == 1) {
signed long long ret = AS + max(hoge(L, X), hoge(R, X));
if (L < R) ret = max(ret, AS + max(hoge(L + 1, X), hoge(R - 1, X)));
auto it = S.lower_bound(L);
if (*it <= R) ret = max(ret, AS + hoge(*it, X));
signed long long p = penalty.getval(L, R + 1);
if (p < X) ret = max(ret, AS + X + X - 2 * p);
cout << ret << endl;
} else {
AS -= abs(bt[L] - bt[L - 1]);
AS -= abs(bt[R] - bt[R + 1]);
bt.add(L, X);
bt.add(R + 1, -X);
AS += abs(bt[L] - bt[L - 1]);
AS += abs(bt[R] - bt[R + 1]);
update(L);
update(L - 1);
update(R);
update(R + 1);
}
}
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const int N = 2e5 + 10;
char str[N];
vector<int> v[N];
int main() {
for (int i = 0; i < N; i++) v[i].push_back(-INF);
int q, len;
scanf("%d%d", &q, &len);
scanf("%s", str + 1);
for (int i = 1; i <= len; i++) {
v[str[i]].push_back(i);
}
for (int i = 0; i < N; i++) v[i].push_back(INF);
while (q--) {
scanf("%s", str + 1);
int m = strlen(str + 1);
long long sum = 0;
for (int i = 1; i <= m; i++) {
if (v[str[i]].size() == 2) {
sum += m;
continue;
}
auto iter = upper_bound(v[str[i]].begin(), v[str[i]].end(), i);
int r = *iter;
iter--;
int l = *iter;
sum += min(i - l, r - i);
}
printf("%lld\n", sum);
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, m, ans = 0;
inline void change(long long x) {
if (x == 0) {
ans = 1;
return;
}
while (x > 0) {
long long ret = x % 16;
switch (ret) {
case 0: {
ans += 1;
break;
}
case 1: {
break;
}
case 2: {
break;
}
case 3: {
break;
}
case 4: {
ans += 1;
break;
}
case 5: {
break;
}
case 6: {
ans += 1;
break;
}
case 7: {
break;
}
case 8: {
ans += 2;
break;
}
case 9: {
ans += 1;
break;
}
case 10: {
ans += 1;
break;
}
case 11: {
ans += 2;
break;
}
case 12: {
break;
}
case 13: {
ans += 1;
break;
}
case 14: {
break;
}
case 15: {
break;
}
}
x = x / 16;
}
return;
}
int main() {
cin >> n;
change(n);
cout << ans << endl;
return 0;
}
| 8 | CPP |
# 2 1 5 5S 5C
# 42 13 24
# 41
# y * k+k = totalsticks
# x = x-1
# x=x+k
import math
if __name__ == '__main__':
testCases = input()
for i in range(int(testCases)):
x,y,k = map(int,input().split())
totalSticks = (y*k)+k
totalSticks -=1
exchange = (totalSticks)//(x-1)
if(totalSticks%(x-1)!=0):
exchange +=1
print(exchange+k)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200200;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
int a[MAX_N];
pair<int, long long> buffer[MAX_N];
priority_queue<long long> pq;
void solve() {
int n, k, b, c;
scanf("%d%d%d%d", &n, &k, &b, &c);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i] += INF;
}
long long answer = LINF;
b = min(b, 5 * c);
for (int r = 0; r < 5; r++) {
while (pq.size()) {
pq.pop();
}
for (int i = 0; i < n; i++) {
long long cost = 0;
int fi = a[i] / 5;
int se = a[i] % 5;
if (se <= r) {
cost += c * (r - se);
} else {
cost += c * (5 + r - se);
fi += 1;
}
buffer[i] = make_pair(fi, cost - fi * 1ll * b);
}
sort(buffer, buffer + n);
long long optimal_sum = 0;
for (int i = 0; i < n; i++) {
if (pq.size() == k - 1) {
long long candidate = buffer[i].first * 1ll * k * 1ll * b +
optimal_sum + buffer[i].second;
answer = min(answer, candidate);
}
optimal_sum += buffer[i].second;
pq.push(buffer[i].second);
if (pq.size() > k - 1) {
optimal_sum -= pq.top();
pq.pop();
}
}
}
printf("%I64d\n", answer);
}
int main() {
int cases;
cases = 1;
for (int i = 0; i < cases; i++) {
solve();
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
const int N = 1e4 + 10;
const int M = 3e6 + 10;
const int maxn = 1e6 + 10;
const int mod = 1000173169;
const double eps = 1e-10;
const double pi = acos(-1.0);
struct Tree {
int num[N << 2], sum[N << 2];
void add(int rt, int l, int r, int p) {
num[rt]++;
sum[rt] += p;
if (l == r) return;
int mid = ((l + r) >> 1);
if (p <= mid)
add(rt << 1, l, mid, p);
else
add(rt << 1 | 1, mid + 1, r, p);
}
int getPos(int rt, int l, int r, int& val) {
if (l == r) return l;
int mid = ((l + r) >> 1);
if (num[rt << 1] >= val)
return getPos(rt << 1, l, mid, val);
else {
val -= num[rt << 1];
return getPos(rt << 1 | 1, mid + 1, r, val);
}
}
int getSum(int rt, int l, int r, int pos) {
if (r <= pos) return sum[rt];
int mid = ((l + r) >> 1);
int res = 0;
if (l <= pos) res = getSum(rt << 1, l, mid, pos);
if (mid < pos) res += getSum(rt << 1 | 1, mid + 1, r, pos);
return res;
}
} seg;
int n, m, k;
vector<int> id;
vector<pair<int, int> > a[3], b, p, res;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
int mx = 1e4;
for (int i = 1; i <= n; i++) {
int t, x, y;
cin >> t >> x >> y;
x = x << 1 | y;
if (x == 3)
b.emplace_back(t, i);
else if (x)
a[x].emplace_back(t, i);
else {
seg.add(1, 1, mx, t);
res.emplace_back(t, i);
}
}
sort(a[1].begin(), a[1].end());
sort(a[2].begin(), a[2].end());
sort(b.begin(), b.end());
int cnt1 = 0, cnt2 = 0;
int t1 = min(min(((int)a[1].size()), ((int)a[2].size())), k);
int cur = 0;
for (int i = 0; i < t1; i++) {
cur += a[1][i].first + a[2][i].first;
p.emplace_back(a[1][i].first, a[2][i].first);
cnt1++;
cnt2 += 2;
}
for (int i = t1; i < max(((int)a[1].size()), ((int)a[2].size())); i++) {
if (i < ((int)a[1].size()))
seg.add(1, 1, mx, a[1][i].first), res.push_back(a[1][i]);
if (i < ((int)a[2].size()))
seg.add(1, 1, mx, a[2][i].first), res.push_back(a[2][i]);
}
for (int i = k; i < ((int)b.size()); i++) {
seg.add(1, 1, mx, b[i].first);
res.push_back(b[i]);
}
int P = -1;
long long ans = INF;
int t2 = min(((int)b.size()), k);
for (int i = 0; i <= t2; i++) {
if (cnt1 == k && cnt2 <= m) {
long long res = INF;
if (cnt2 == m)
res = cur;
else if (cnt2 + seg.num[1] >= m) {
int val = m - cnt2;
int pos = seg.getPos(1, 1, mx, val);
res = pos * val + cur;
if (pos > 1) res += seg.getSum(1, 1, mx, pos - 1);
}
if (ans > res) {
ans = res;
P = i;
}
}
if (i < t2) {
if (cnt1 < k) {
cur += b[i].first;
cnt1++;
cnt2++;
} else if (!p.empty()) {
pair<int, int> t = p.back();
p.pop_back();
cur -= t.first + t.second;
seg.add(1, 1, mx, t.first);
seg.add(1, 1, mx, t.second);
cur += b[i].first;
cnt2--;
}
}
}
if (ans == INF)
cout << "-1\n";
else {
cout << ans << '\n';
for (int i = 0; i < P; i++) id.push_back(b[i].second);
for (int i = 0; i < t1; i++) {
if (P + i + 1 <= k) {
id.push_back(a[1][i].second);
id.push_back(a[2][i].second);
} else {
res.push_back(a[1][i]);
res.push_back(a[2][i]);
}
}
sort(res.begin(), res.end());
for (int i = 0; i < ((int)res.size()); i++) {
if (((int)id.size()) == m) break;
id.push_back(res[i].second);
}
for (int v : id) cout << v << ' ';
cout << '\n';
assert(((int)id.size()) == m);
}
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
char c[8][8];
int sum = 0;
int summ = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cin >> c[i][j];
switch (c[i][j]) {
case 'P': {
sum = sum + 1;
break;
}
case 'N': {
sum = sum + 3;
break;
}
case 'B': {
sum = sum + 3;
break;
}
case 'R': {
sum = sum + 5;
break;
}
case 'Q': {
sum = sum + 9;
break;
}
case 'K': {
sum = sum + 0;
break;
}
case 'p': {
summ = summ + 1;
break;
}
case 'n': {
summ = summ + 3;
break;
}
case 'b': {
summ = summ + 3;
break;
}
case 'r': {
summ = summ + 5;
break;
}
case 'q': {
summ = summ + 9;
break;
}
case 'k': {
summ = summ + 0;
break;
}
}
}
}
if (sum > summ) {
cout << "White";
} else if (sum < summ) {
cout << "Black";
} else if (sum == summ) {
cout << "Draw";
}
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long d[200500];
string a[200500];
void solve() {
int n;
scanf("%d\n", &n);
for (int i = 0; i < n; i++) getline(cin, a[i]);
long long l = 1, u = 1;
for (int i = n - 1; i >= 0; i--) {
string s = a[i];
if (s == "UL") l++;
if (s == "UR") u++;
if (s == "DL") u++;
if (s == "DR") l++;
if (s == "ULDR") l++, u++;
}
cout << l * u << endl;
}
int main() { solve(); }
| 7 | CPP |
n, m = [int(i) for i in input().split()]
nums = [0] * n
c = [int(i) for i in input().split()]
for i in range(m):
nums[c[i] - 1] += 1
scores = 0
min = min(nums)
if min != 0: scores = min
print(scores)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
namespace ucp {
template <typename T>
void Min(T& a, T& b) {
if (a > b) a = b;
}
template <typename T>
void Max(T& a, T& b) {
if (a < b) a = b;
}
inline int read() {
int sign, data = 0;
char ch = 0;
while ((ch != '-') && ch < '0' || ch > '9') ch = getchar();
if (ch == '-') {
sign = -1;
ch = getchar();
} else
sign = 1;
while (ch >= '0' && ch <= '9') {
data = data * 10 + ch - '0';
ch = getchar();
}
return data * sign;
}
} // namespace ucp
using namespace ucp;
const int mxint = 0x3f3f3f3f;
const long long mxll = 1e15;
const int lim = 5e5 + 10;
const int maxn = 4e5 + 10;
const int N = 2e4 + 1000;
const long long mod = 998244353;
int n, m, k;
struct enode {
int v, nxt;
long long w;
} edge[maxn * 2];
int head[maxn], tot;
void add(int u, int v, long long w) {
edge[++tot].v = v;
edge[tot].nxt = head[u];
edge[tot].w = w;
head[u] = tot;
}
long long dis[maxn];
int vis[maxn];
int pre[maxn];
void Dijskdtra(int st) {
priority_queue<pair<long long, long long>,
vector<pair<long long, long long> >,
greater<pair<long long, long long> > >
pq;
for (int i = 1; i <= n; ++i) {
vis[i] = 0;
dis[i] = mxll;
pre[i] = -1;
}
dis[st] = 0;
pq.push(make_pair(dis[st], st));
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (vis[u]) continue;
vis[u] = 1;
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v;
long long w = edge[i].w;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
pre[v] = u;
if (!vis[v]) pq.push(make_pair(dis[v], v));
}
}
}
}
vector<int> ans;
void bfs() {
queue<int> q;
q.push(1);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].v;
if (u == pre[v] && ans.size() < k) {
q.push(v);
ans.push_back((i - 1) / 2 + 1);
}
}
if (ans.size() >= k) break;
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
long long w;
scanf("%lld", &w);
add(u, v, w);
add(v, u, w);
}
Dijskdtra(1);
bfs();
int len = ans.size();
printf("%d\n", len);
for (int i = 0; i < len; ++i)
printf("%d%c", ans[i], i == len - 1 ? '\n' : ' ');
return 0;
}
| 10 | CPP |
n=int(input())
for i in range(n):
m=int(input())
a=list(map(int,input().split(" ")))
a.reverse()
print(*a) | 7 | PYTHON3 |
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 1 << 29;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int m;
while(cin >> m && m){
int n;
cin >> n;
int d[300];
for(int i = 1; i <= n; ++i){
cin >> d[i];
}
bool g[300][300];
for(int i = 0; i <= n + 1; ++i){
for(int j = 0; j <= n + 1; ++j){
g[i][j] = false;
}
}
for(int i = 0; i < n + 1; ++i){
for(int j = 1; j <= m; ++j){
if(i + j >= n + 1){
g[i][n + 1] = true;
}
else {
g[i][max(0, min(n + 1, i + j + d[i + j]))] = true;
}
}
}
for(int k = 0; k <= n + 1; ++k){
for(int i = 0; i <= n + 1; ++i){
for(int j = 0; j <= n + 1; ++j){
if(g[i][k] && g[k][j]){
g[i][j] = true;
}
}
}
}
bool check = g[0][n + 1];
for(int i = 1; i <= n; ++i){
//cout << g[0][i] << " " << g[i][n + 1] << endl;
if(g[0][i] && !g[i][n + 1]){
check = false;
}
}
cout << (check ? "OK" : "NG") << endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int compare(const void* a, const void* b) { return *(int*)a - *(int*)b; }
int main() {
int n, d, m, total = 0;
cin >> n >> d;
int* arr = new int[n];
for (int i = 0; i < n; i++) cin >> arr[i];
cin >> m;
qsort(arr, n, sizeof(int), compare);
if (m <= n)
for (int i = 0; i < m; i++) total += arr[i];
else {
for (int i = 0; i < n; i++) total += arr[i];
for (int i = 0; i < m - n; i++) total -= d;
}
cout << total;
return 0;
}
| 7 | CPP |
a=int(input())
b=sum(map(int,input().split()))
print((1/a)*b)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, c = 0;
cin >> n;
int a[n];
for (i = 0; i < n; i++) cin >> a[i];
for (i = 1; i < n - 1; i++) {
if (a[i] < a[i - 1] && a[i] < a[i + 1])
c++;
else if (a[i] > a[i - 1] && a[i] > a[i + 1])
c++;
}
cout << c;
}
| 7 | CPP |
if __name__ == '__main__':
print(input().upper())
| 0 | PYTHON3 |
#include<iostream>
#include<utility>
#include<algorithm>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
typedef pair<int,int> pii;
int main(){
int n,a,b,ap,bp;
cin>>n;
int t[n];
rep(i,n)t[i]=0;
rep(i,n*(n-1)/2){
cin>>a>>b>>ap>>bp;
if(ap>bp)t[a-1]+=3;
else if(ap<bp)t[b-1]+=3;
else{
t[a-1]++;t[b-1]++;
}
}
pair<int,int> r[n],j[n];
rep(i,n)r[i]=pair<int,int>(t[i],i+1);
sort(r,r+n);
for(int i=n-1,s=1,t=1;i>=0;i--){
j[i]=pii(r[i].second,s);
if(i==0)s++;
t++;
if(r[i].first!=r[i-1].first)s=t;
}
sort(j,j+n);
rep(i,n)cout<<j[i].second<<endl;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, c = 0;
int main() {
cin >> n >> m;
for (int i = 0; i < n * m; i++) {
cin >> a >> b;
if (a == 1 || b == 1) {
c++;
}
}
cout << c;
return 0;
}
| 7 | CPP |
# -*- coding: utf-8 -*-
# @Date : 2019-07-12 20:23:58
# @Author : raj lath ([email protected])
# @Link : link
# @Version : 1.0.0
import sys
sys.setrecursionlimit(10**5+1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
ins = int(input())
mode = ins % 4
if mode == 1:print(0, 'A')
elif mode == 2:print(1, 'B')
elif mode == 3:print(2, 'A')
else:print(1, "A")
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long double eps = 0.0000001;
struct Mn {
long double num;
vector<long double> v;
};
Mn del(Mn a, Mn b) {
long double diff = a.num - b.num + 1;
for (long double i = 0; i < diff; i++) {
long double degree = a.num - i;
long double raz = degree - b.num;
long double st = a.v[degree] / b.v[b.num];
for (long double j = 0; j <= b.num; j++) {
long double now = j + raz;
long double coeff = st * b.v[j];
a.v[now] -= coeff;
}
}
long double old = a.num;
for (long double i = a.num; i >= 0; i--) {
if (abs(a.v[i]) > eps) {
a.num = i;
break;
}
}
if (a.num == old) a.num = 0;
return a;
}
long double gcd(Mn a, Mn b) {
if (b.num == 0 && abs(b.v[0]) < eps) return 0;
Mn res = del(a, b);
return gcd(b, res) + 1;
}
signed main() {
long double n;
cin >> n;
srand(time(NULL));
while (true) {
Mn f, s;
f.num = -1;
s.num = -1;
vector<long double> a, b;
for (long double i = 0; i < n; i++) {
long double X = rand() % 2;
long double Y = rand() % 2;
if (X == 0) a.push_back(1);
if (X == 1) a.push_back(-1);
if (X == 2) a.push_back(0);
if (i != n - 1) {
if (Y == 0) b.push_back(1);
if (Y == 1) b.push_back(-1);
if (Y == 2) b.push_back(0);
}
}
a.push_back(1);
b.push_back(1);
f.v = a;
s.v = b;
for (long double i = n; i >= 0; i--) {
if (f.v[i] != 0) {
f.num = i;
break;
}
}
for (long double i = n - 1; i >= 0; i--) {
if (s.v[i] != 0) {
s.num = i;
break;
}
}
Mn f1 = f, s1 = s;
long double g = gcd(f, s);
if (g == n) {
cout << f1.num << endl;
for (long double i = 0; i <= f1.num; i++) cout << f1.v[i] << " ";
cout << endl;
cout << s1.num << endl;
for (long double i = 0; i <= s1.num; i++) cout << s1.v[i] << " ";
cout << endl;
return 0;
}
}
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long m, n, a, b;
long long ans;
long long beginx, beginy;
long long endx, endy;
int main() {
ans = 3;
cin >> m >> n >> a >> b;
beginx = (a - 1) / n;
beginy = (a - 1) % n;
endx = (b - 1) / n;
endy = (b - 1) % n;
if (beginx == endx || a == b)
ans = 1;
else if (endx - beginx == 1) {
if (beginy == 0 && (endy == n - 1 || b == m))
ans = 1;
else
ans = 2;
} else if (beginy == endy + 1)
ans = 2;
else if (beginy == 0 && (endy == n - 1 || b == m))
ans = 1;
else if (beginy == 0 || endy == n - 1 || b == m)
ans = 2;
else
ans = 3;
cout << ans << endl;
return 0;
}
| 7 | CPP |
#include<iostream>
#include<vector>
using namespace std;
int main(){
while(1){
int a,b,c,n,p[1002],q[1002],r[1002],s[1002],Q[302];
for(int i=0;i<302;i++)Q[i]=2;
scanf("%d%d%d",&a,&b,&c);
if(a==0&&b==0&&c==0)break;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d%d%d",&p[i],&q[i],&r[i],&s[i]);
if(s[i]==1){Q[p[i]]=1; Q[q[i]]=1; Q[r[i]]=1;}
}
for(int i=0;i<n;i++){
if(s[i]==0){
if(Q[p[i]]==1&&Q[q[i]]==1)Q[r[i]]=0;
if(Q[p[i]]==1&&Q[r[i]]==1)Q[q[i]]=0;
if(Q[r[i]]==1&&Q[q[i]]==1)Q[p[i]]=0;
}
}
for(int i=1;i<=a+b+c;i++){
printf("%d\n",Q[i]);
}
}
} | 0 | CPP |
n = int(input())
prev = 0
answer = 0
for i in range(n):
new = int(input())
if (new == prev):
prev = new
else:
prev = new
answer += 1
print(answer)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int a, b, c;
} d[200005];
bool cmp(const node& p, const node& q) { return p.c > q.c; }
int main() {
int n, k, i, ans;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) scanf("%d", &d[i].a);
for (i = 1; i <= n; i++) scanf("%d", &d[i].b), d[i].c = d[i].b - d[i].a;
sort(d + 1, d + 1 + n, cmp);
for (ans = 0, i = 1; i <= k; i++) ans += d[i].a;
for (; i <= n; i++) ans += min(d[i].a, d[i].b);
printf("%d\n", ans);
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, i, j, l, o, m[55][55];
int main() {
cin >> n;
while (i < n) {
while (j < n) {
cin >> m[i][j];
j++;
}
i++;
j = 0;
}
i = 0;
while (i < n) {
while (j < n) {
while (l < n) {
while (o < n) {
if (m[i][o] + m[l][j] == m[i][j]) c++;
o++;
}
l++;
o = 0;
}
if (c == 0 && m[i][j] != 1) return cout << "No", 0;
j++;
l = 0;
c = 0;
}
i++;
j = 0;
}
cout << "Yes";
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
return a < b ? a : b;
}
const char lf = '\n';
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
if (s == t) {
t = (s = buf) + fread(buf, 1, bufl, stdin);
if (s == t) return EOF;
}
return *s++;
}
inline int ty() {
int a = 0;
int b = 1, c = fetch();
while (!isdigit(c)) b ^= c == '-', c = fetch();
while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
return b ? a : -a;
}
} // namespace ae86
using ae86::ty;
const int _ = 300007, lglg = 20;
vector<int> e1[_], e2[_];
int n;
int bfa[_] = {0};
int findbfa(int x) { return !bfa[x] ? x : bfa[x] = findbfa(bfa[x]); }
int dep[_]{0}, pfa[_][lglg + 1] = {0};
void dfs1(int x, int ff) {
dep[x] = dep[ff] + 1, pfa[x][0] = ff;
for (int i = 1; i <= lglg; i++) pfa[x][i] = pfa[pfa[x][i - 1]][i - 1];
for (auto b : e1[x])
if (b != ff) dfs1(b, x);
}
void linka(int a, int b) {
a = findbfa(a), b = findbfa(b);
for (int i = lglg; i >= 0; i--)
if (dep[findbfa(pfa[a][i])] > dep[b]) a = findbfa(pfa[a][i]);
if (findbfa(pfa[a][0]) == b)
a = findbfa(a);
else
a = findbfa(b);
bfa[a] = pfa[a][0], cout << a << ' ' << bfa[a] << ' ';
}
void dfs2(int x, int ff) {
for (auto b : e2[x])
if (b != ff) dfs2(b, x), linka(x, b), cout << x << ' ' << b << lf;
}
int main() {
ios::sync_with_stdio(0), cout.tie(nullptr);
n = ty();
for (int i = 1, a, b; i < n; i++)
a = ty(), b = ty(), e1[a].emplace_back(b), e1[b].emplace_back(a);
for (int i = 1, a, b; i < n; i++)
a = ty(), b = ty(), e2[a].emplace_back(b), e2[b].emplace_back(a);
cout << n - 1 << lf;
dfs1(1, 0), dfs2(1, 0);
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, i, ans = 0, sum = 0;
cin >> n;
long long a[n], b[n];
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (long long i = 0; i < n; i++) {
if (a[i] >= sum) {
ans++;
sum += a[i];
}
}
cout << ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long test;
test = 1;
while (test--) {
solve();
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int t, n, a[200010];
int A[200010], B[200010];
int main() {
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
B[1] = a[1], A[1] = 0;
for (int i = 2; i <= n; i++) {
B[i] = min(B[i - 1], a[i] - A[i - 1]);
A[i] = a[i] - B[i];
}
bool f = 0;
for (int i = 1; i <= n; i++)
if (B[i] < 0) f = 1;
if (f == 1)
cout << "NO" << '\n';
else
cout << "YES" << '\n';
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
char c[1005][1005];
int d[1005][1005];
struct po {
int x;
int y;
};
queue<po> q;
int ex, ey, sx, sy;
void bfs() {
int xx, yy, i, j, x, y;
memset(d, -1, sizeof(d));
d[ex][ey] = 0;
q.push((po){ex, ey});
while (!q.empty()) {
xx = q.front().x;
yy = q.front().y;
q.pop();
for (i = 0; i < 4; i++) {
x = xx + dx[i];
y = yy + dy[i];
if ((c[x][y] == 'S' || (c[x][y] >= '0' && c[x][y] <= '9')) &&
d[x][y] == -1) {
d[x][y] = d[xx][yy] + 1;
q.push((po){x, y});
}
}
}
}
int main(void) {
int n, m, i, j, ans = 0;
cin >> n >> m;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) cin >> c[i][j];
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (c[i][j] == 'E') {
ex = i;
ey = j;
}
if (c[i][j] == 'S') {
sx = i;
sy = j;
}
}
}
bfs();
for (i = 0; i <= n; i++) {
for (j = 0; j <= m; j++) {
if (c[i][j] != 'E' && c[i][j] != 'S' && d[i][j] != -1 &&
d[i][j] <= d[sx][sy])
ans += c[i][j] - '0';
}
}
cout << ans;
return 0;
}
| 8 | CPP |
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
int n;
while(cin>>s){
for(string::iterator it=s.begin();it!=s.end();++it){
n=1;
if(*it=='@'){
++it;
n=*it-'0';
++it;
}
while(n--) cout<<*it;
}
cout<<endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l, i;
cin >> n;
string s;
int *arr = new int[14];
arr[0] = 4;
arr[1] = 7;
arr[2] = 44;
arr[3] = 47;
arr[4] = 74;
arr[5] = 77;
arr[6] = 444;
arr[7] = 447;
arr[8] = 474;
arr[9] = 477;
arr[10] = 744;
arr[11] = 747;
arr[12] = 774;
arr[13] = 777;
s = n;
l = s.length();
for (i = 0; i < 14; i++) {
if (n < arr[i]) {
cout << "NO";
return 0;
}
if (n % arr[i] == 0) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
inline long long max3(long long a, long long b, long long c) {
return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long min3(long long a, long long b, long long c) {
return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
void solve() {
long long int n, m, t = 0, k = 0, x = 0, y = 0, z = 0, ex, che, var, yd,
f = 9223372036854775807;
cin >> n >> k;
if (k >= n) {
cout << "1\n";
return;
}
if (n % 2 == 0 && k >= n / 2) {
cout << 2 << "\n";
return;
}
if (isPrime(n)) {
cout << n << "\n";
return;
}
if (k == 1) {
cout << n << "\n";
return;
}
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if ((n / i) <= k) {
x = n / i;
break;
}
if (i <= k) x = i;
}
}
cout << n / x << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int T = 1, t = 0;
cin >> T;
while (t++ < T) {
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 2;
const long long MOD = 1000000007;
long long powMod(long long a, long long b) {
if (b == 0)
return 1;
else {
long long tmp = powMod(a, b / 2);
if (b % 2 == 0)
return tmp * tmp % MOD;
else
return tmp * tmp % MOD * a % MOD;
}
}
long long C[MAX];
long long a4, a7;
int an[MAX], n, k;
int resi;
int lnum;
long long qe(int sd) {
if (sd > resi || sd < 0)
return 0;
else {
return C[sd];
}
}
unordered_map<int, int> mm;
bool luck(int val) {
bool ok = 1;
while (val) {
int r = val % 10;
if (r != 4 && r != 7) {
ok = 0;
break;
}
val /= 10;
}
return ok;
}
int ln[MAX];
int lcnt;
long long dp[1024][1024];
long long sol(int pos, int sel) {
auto &ele = dp[pos][sel];
if (ele != -1) return ele;
if (pos == 0) {
if (sel == 0)
return ele = 1;
else {
if (sel == 1)
return ele = ln[pos];
else
return ele = 0;
}
} else {
if (sel == 0) {
return ele = 1;
} else {
return ele = (sol(pos - 1, sel) + ln[pos] * sol(pos - 1, sel - 1)) % MOD;
}
}
}
int main() {
scanf("%d", &n), scanf("%d", &k);
for (int i = (1); i <= (n); ++i) scanf("%d", &an[i]);
for (int i = (1); i <= (n); ++i) {
if (luck(an[i])) {
lnum++;
mm[an[i]]++;
}
}
lcnt = 0;
for (auto &ele : mm) {
ln[lcnt++] = ele.second;
}
resi = n - lnum;
for (int i = (0); i <= (resi); ++i) {
if (i == 0)
C[i] = 1;
else {
C[i] = C[i - 1] * (resi - i + 1) % MOD * powMod(i, MOD - 2) % MOD;
}
}
if (lcnt == 0) {
printf("%lld\n", C[k]);
} else {
memset(dp, -1, sizeof(dp));
for (int sel = (0); sel <= (min(k, lcnt)); ++sel) {
sol(lcnt - 1, sel);
}
long long ans = 0;
for (int sel = (0); sel <= (min(k, lcnt)); ++sel) {
ans = (ans + qe(k - sel) * sol(lcnt - 1, sel)) % MOD;
}
printf("%lld\n", ans);
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1005;
int n, k, a[MAX], b[MAX];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &b[i]);
a[i] = i;
}
for (int i = n - k; i >= 1; i--) {
int num = b[i], t = i;
while (num) {
swap(a[t], a[t + 1]);
if (a[t] >= i + k) num--;
t++;
}
}
for (int i = 1; i <= n; i++) {
if (i == 1)
printf("%d", a[i]);
else
printf(" %d", a[i]);
}
printf("\n");
}
| 8 | CPP |
# 118A - String Task
# http://codeforces.com/problemset/problem/118/A
v = 'aoyeui'
s = input().lower()
ans = [f'.{x}' for x in s if x not in v]
print(''.join(ans))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long qpow(long long a, long long n) {
long long res = 1;
while (n) {
if (n & 1) res = res * a % (998244353);
a = a * a % (998244353);
n >>= 1;
}
return res;
}
int n, cnt;
int a[105], b[105], tmp[105];
long long C[105], g[105], inv[105];
void init() {
inv[1] = 1;
for (int i = 2; i <= n; i++) {
inv[i] =
((998244353) - (998244353) / i) * inv[(998244353) % i] % (998244353);
}
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a[i], &b[i]);
tmp[++cnt] = a[i], tmp[++cnt] = ++b[i];
}
sort(tmp + 1, tmp + cnt + 1);
cnt = unique(tmp + 1, tmp + cnt + 1) - tmp - 1;
for (int i = 1; i <= n; i++) {
a[i] = lower_bound(tmp + 1, tmp + cnt + 1, a[i]) - tmp;
b[i] = lower_bound(tmp + 1, tmp + cnt + 1, b[i]) - tmp;
}
}
int main() {
cin >> n;
init();
C[0] = g[0] = 1;
for (int j = cnt - 1; j >= 1; j--) {
int l = tmp[j + 1] - tmp[j];
for (int i = 1; i <= n; i++) {
C[i] = C[i - 1] * (l + i - 1) % (998244353) * inv[i] % (998244353);
}
for (int i = n; i >= 1; i--) {
if (a[i] > j || b[i] <= j) continue;
int m = 1;
for (int k = i - 1; k >= 0; k--) {
g[i] = (g[i] + C[m] * g[k] % (998244353)) % (998244353);
if (a[k] > j || b[k] <= j) break;
m++;
}
}
}
long long ans = g[n], t = 1;
for (int i = 1; i <= n; i++) {
t = t * (tmp[b[i]] - tmp[a[i]]) % (998244353);
}
ans = (ans * qpow(t, (998244353) - 2)) % (998244353);
cout << ans << endl;
return 0;
}
| 12 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long a, b, n;
cin >> a >> b;
n = a + b + 1;
for (int i = b + 1; i <= n; i++) cout << i << " ";
for (int i = b; i >= 1; i--) cout << i << " ";
}
| 7 | CPP |
n = int(input())
lst = list(map(int,input().split()))
lst.append(-1)
total = []
count = 0
temp = 1
for i in range(n):
if lst[i] < lst[i+1]:
temp += 1
else:
total.append(temp)
temp = 1
count += 1
print(count)
print(*total)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
long long T, n, A, B, a, b, lcm, l, i;
cin >> T;
while (T--) {
cin >> n;
A = n - 1, B = 1, lcm = n - 1;
for (i = 2; i * i <= n; i++)
if (n % i == 0) {
a = i;
b = n - a;
l = a * b / gcd(a, b);
if (l < lcm) {
A = a;
B = b;
lcm = l;
}
a = n / i;
b = n - a;
l = a * b / gcd(a, b);
if (l < lcm) {
A = a;
B = b;
lcm = l;
}
}
cout << A << " " << B << endl;
}
return 0;
}
| 8 | CPP |
t = int(input())
for i in range(t):
k = int(input())
if k == 2:
print(2)
else:
print(k % 2)
| 7 | PYTHON3 |
#include "bits/stdc++.h"
#include<unordered_map>
#pragma warning(disable:4996)
using namespace std;
const long long int My_LInf=9223372036854775807;
struct aUnionFind {
vector<pair<int, long long int>> data;
aUnionFind(int size) : data(size, make_pair(-1, 0)) { }
//y is w bigger than x
bool unionSet(const int x, const int y, const long long int w) {
const int rx(root(x).first), ry(root(y).first);
if (rx != ry) {
data[rx].first += data[ry].first; data[ry].first = rx;
data[ry].second = w + data[x].second - data[y].second;
return true;
}
else {
return root(x).second + w == root(y).second;
}
}
bool findSet(const int x, const int y) {
return root(x) == root(y);
}
//x is belong to first
//x is second bigger than first
pair<int, long long int> root(const int x) {
if (data[x].first < 0) {
return make_pair(x, 0);
}
else {
pair<int, long long int>ndata;
ndata.first = root(data[x].first).first;
ndata.second = data[x].second + root(data[x].first).second;
return data[x] = ndata;
}
}
int size(const int x) {
return -data[root(x).first].first;
}
};
int HorseNum, M;
vector<long long int>sps;
struct aa {
int fstfinid;
long long int fstfintime;
int lasfinid;
long long int lasfintime;
int fststaid;
long long int fststatime;
int lasstaid;
long long int lasstatime;
};
long long int check(const vector<vector<bool>>&changes, const vector<vector<long long int>>×) {
vector<int>seq(HorseNum);
iota(seq.begin(), seq.end(), 0);
aUnionFind uf(HorseNum);
for (int wide = 0; wide < M; ++wide) {
for (int change = 0; change < HorseNum - 1; ++change) {
if (changes[wide][change]) {
const int c0 = seq[change];
const int c1 = seq[change + 1];
const long long int sa = times[c0][wide] - times[c1][wide];
if (sa > 0) {
if (!uf.unionSet(c0, c1, sa)) {
return My_LInf;
}
}
else {
return My_LInf;
}
swap(seq[change], seq[change + 1]);
}
}
}
map<int, aa>mp;
for (int i = 0; i < HorseNum; ++i) {
if (!mp.count(uf.root(i).first)) {
mp[uf.root(i).first] = aa{
i,uf.root(i).second + times[i][M],
i,uf.root(i).second + times[i][M],
i,0,
i,0,
};
}
else {
if (mp[uf.root(i).first].fstfintime > uf.root(i).second + times[i][M]) {
mp[uf.root(i).first].fstfinid = i;
mp[uf.root(i).first].fstfintime = uf.root(i).second + times[i][M];
}
if (mp[uf.root(i).first].lasfintime < uf.root(i).second + times[i][M]) {
mp[uf.root(i).first].lasfinid = i;
mp[uf.root(i).first].lasfintime = uf.root(i).second + times[i][M];
}
mp[uf.root(i).first].lasstaid = i;
mp[uf.root(i).first].lasstatime = uf.root(i).second;
}
}
for (auto it = mp.begin(); it != mp.end(); ++it) {
if (it != mp.begin()) {
aa afrom(prev(it)->second);
aa ato(it->second);
if (afrom.lasfintime - afrom.lasstatime>ato.fstfintime-ato.fststatime) {
uf.unionSet(afrom.lasfinid, ato.fstfinid, times[afrom.lasfinid][M] - times[ato.fstfinid][M]);
}
else {
uf.unionSet(afrom.lasstaid, ato.fststaid, 1);
}
/*aUnionFind auf(uf);
aUnionFind buf(uf);
{
int from = (prev(it))->second.lasfinid;
int to = (it)->second.fstfinid;
if (sps[from] > sps[to]) {
auf.unionSet(from, to, times[from][M] - times[to][M]);
}
else {
auf.unionSet((prev(it))->second.lasstaid, (it)->second.fststaid, 1);
}
}
{
int from = (prev(it))->second.lasstaid;
int to = (it)->second.fststaid;
buf.unionSet(from, to, 1);
}
if (buf.root((it)->second.fststaid).second > auf.root((it)->second.fststaid).second) {
uf = buf;
}
else {
uf = auf;
}*/
}
}
for (int i = 0; i < HorseNum - 1; ++i) {
if (uf.root(i).second >= uf.root(i + 1).second)return My_LInf;
}
for (int i = 0; i < HorseNum - 1; ++i) {
if (uf.root(seq[i]).second+times[seq[i]][M] > uf.root(seq[i + 1]).second + times[seq[i+1]][M])return My_LInf;
}
long long int amax = 0;
for (int i = 0; i <HorseNum; ++i) {
amax = max(amax, uf.root(i).second + times[i][M] - uf.root(0).second);
}
return amax;
}
int main() {
aUnionFind uu(10);
uu.unionSet(0, 1, 3);
uu.unionSet(0, 2, 4);
uu.unionSet(2, 4, 5);
uu.unionSet(6, 7, 1);
uu.unionSet(7, 8, 2);
uu.unionSet(0, 8, 0);
long long int dist;
cin >> dist;
cin >> HorseNum;
for (int i = 0; i <HorseNum; ++i) {
int S; cin >> S;
sps.push_back(S);
}
cin >> M;
vector<long long int>ds;
for (int i = 0; i < M; ++i) {
int D; cin >> D;
ds.push_back(D);
}
sort(ds.begin(), ds.end());
vector<vector<long long int>>times(HorseNum, vector<long long int>(M + 1));
for (int i = 0; i <HorseNum; ++i) {
for (int j = 0; j < M; ++j) {
times[i][j] = sps[i] * ds[j];
}
times[i][M] = sps[i] * dist;
}
long long int ans = My_LInf;
for (int i = 0; i < 1<<((HorseNum-1)*M); ++i) {
bitset<20>bs(i);
vector<vector<bool>>changes(M, vector<bool>(HorseNum -1));
bool ok = true;
for (int j = 0; j < M; ++j) {
for (int k = 0; k < HorseNum - 1; ++k) {
changes[j][k] = bs[j*(HorseNum - 1) + k];
}
}
for (int j = 0; j < M; ++j) {
for (int k = 0; k < HorseNum - 2; ++k) {
if (changes[j][k] && changes[j][k + 1]) {
ok = false;
break;
}
}
}
if (!ok)continue;
long long int a = check(changes, times);
if (a < ans) {
ans = a;
}
}
cout << ans << endl;
return 0;
} | 0 | CPP |
k=int(input())
l=int(input())
m=int(input())
n=int(input())
d=int(input())
increase=0
count=0
for i in range(1,d+1):
if i%k==0:
increase=1
if(increase!=1) and i%l==0 :
increase=1
if(increase!=1) and i%m==0 :
increase=1
if(increase!=1) and i%n==0 :
increase=1
count+=increase
increase=0
print(count)
| 7 | PYTHON3 |
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
num = [a[0]]
cnt = [1]
for i in range(1, len(a)):
if a[i] == num[-1]:
cnt[-1] += 1
continue
num.append(a[i])
cnt.append(1)
ans = "Agasa"
for i in cnt:
if i%2 == 1:
ans = "Conan"
print(ans)
| 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000 * 100 + 100;
int a[MAX], b[MAX];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(a, a + n);
int cnt = 0;
for (int i = 0; i < n; i++)
if (a[i] != b[i]) cnt++;
if (cnt <= 2)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 7 | CPP |
s=input().strip()
k=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(len(s)):
ans+=(i+1)*a[(ord(s[i])-97)]
m=max(a)
for i in range(len(s),len(s)+k):
ans+=m*(i+1)
print(ans) | 8 | PYTHON3 |
# n = int(input())
n,m = map(int,input().split())
# m = list(map(int,input().split()))
# m = [tuple(map(int,input().split())) for _ in range(n)]
# s = input()
import math
print(math.factorial(min(n,m))) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
std::vector<long long> arr(4);
long long a, b;
for (int i = 0; i < 4; ++i) {
cin >> arr[i];
}
cin >> a >> b;
sort(arr.begin(), arr.end());
if (arr[0] > a) {
cout << min(arr[0] - a, b - a + 1) << endl;
} else {
cout << 0 << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
| 7 | CPP |
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))
#define EPS (1e-10)
#define EQ(a,b) (abs((a)-(b)) < EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define PI (acos(-1.0))
#define POSARG(a) (arg((a)) > 0.0? arg((a)) : 2.0*PI + arg((a)))
using namespace std;
typedef complex<double> P;
int n, r, q, center;//center...回転の中心となる点のある添字
vector<P> vc;
P rotate_p(P a, P c, double t)
{
a = a - c;
a = P(a.real()*cos(t) - a.imag()*sin(t),
a.real()*sin(t) + a.imag()*cos(t));
return a + c;
}
vector<P> rotate_vc(vector<P> vc, P c, double t)
{
for(int i = 0; i < (int)vc.size(); i++)
{
vc[i] = rotate_p(vc[i], c, t);
}
return vc;
}
void solve()
{
FOR(k, 0, q)
{
double upper = 2.0*PI, lower = 0;
FOR(j, 0, 50)
{
double mid = (upper+lower)/2;
vector<P> vc_r = rotate_vc(vc, vc[center], mid);
bool out = false;
FOR(i, 0, n)
{
if( abs(vc_r[i]) > r )
{
out = true;
}
}
if(out)
{
upper = mid;
}
else
{
lower = mid;
}
}
vector<P> vc_r = rotate_vc(vc, vc[center], lower);
int newcenter = -1;
FOR(i, 0, n)
{
if( i != center && EQ(abs(vc_r[i]), r) )
{
if( newcenter < 0 ||
abs(vc_r[i] - vc[center]) > abs(vc_r[newcenter] - vc[center]) )
{
newcenter = i;
}
}
}
printf("%.12lf %.12lf\n", vc_r[newcenter].real(), vc_r[newcenter].imag());
vc = vc_r;
center = newcenter;
}
return;
}
int main()
{
while(cin >> n >> r >> q, (n||r||q))
{
vc.clear();
FOR(i, 0, n)
{
double x, y;
cin >> x >> y;
vc.push_back(P(x, y));
if( EQ(x*x+y*y, r*r) )
center = i;
}
solve();
}
return 0;
} | 0 | CPP |
for _ in range(int(input())):
n=int(input())
s=input()
if n<11:
print('NO')
continue
if s[0]=='8' and n==11:
print('YES')
continue
rem=n-10
if '8' in s[:rem]:
print('YES')
else:
print('NO') | 7 | PYTHON3 |
#import sys
#import math
#sys.stdout=open("C:/Users/pipal/OneDrive/Desktop/VS code/python/output.txt","w")
#sys.stdin=open("C:/Users/pipal/OneDrive/Desktop/VS code/python/input.txt","r")
#t=int(input())
#for i in range(t):
n=int(input())
l=list(map(int,input().split()))
y=max(l)
x=min(l)
print(y-x+1-n)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, a, b; cin >> n >> a >> b;
cout << n/(a+b) * a + min(a, (n%(a+b)));
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n;
double arr[200005], temp[200005], dp[200005];
double x;
double minuss, pluss;
void check(double x) {
minuss = 10000.0, pluss = -10000.0;
for (int i = 1; i <= n; i++) dp[i] = 0.0;
for (int i = 1; i <= n; i++) temp[i] = arr[i] - x;
for (int i = 1; i <= n; i++) dp[i] = max(temp[i], dp[i - 1] + temp[i]);
for (int i = 1; i <= n; i++) pluss = max(pluss, dp[i]);
for (int i = 1; i <= n; i++) dp[i] = 0.0;
for (int i = 1; i <= n; i++) dp[i] = min(temp[i], dp[i - 1] + temp[i]);
for (int i = 1; i <= n; i++) minuss = min(minuss, dp[i]);
minuss = abs(minuss);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lf", &arr[i]);
cout.precision(10);
double l = -10000.0;
double r = 10000.0;
int cnt = 0;
while (l <= r && cnt < 100) {
double mid = (l + r) / 2.0;
check(mid);
if (pluss > minuss)
l = mid;
else if (minuss > pluss)
r = mid;
else
break;
cnt++;
}
cout << max(minuss, pluss) << endl;
return 0;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
class gfg {
public:
unsigned int countSetBits(int n) {
unsigned int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
} g;
vector<long long> prime;
long long int fact(int n);
int hexadecimalToDecimal(char hexVal[]);
vector<vector<long long int> > power(vector<vector<long long int> > &a,
long long int n, long long int p);
long long int fact(long long int n, long long int p);
long long int power(long long int x, long long int n, long long int p);
long long int nCr(long long int n, long long int r, long long int p);
vector<vector<long long int> > multiply(vector<vector<long long int> > &a,
vector<vector<long long int> > &b,
long long int p);
void manipulated_seive(long long int N);
int gcd(int a, int b);
void generatePrimeFactors();
long long int calculateNoOFactors(long long int n);
void solve() {
long long int answer;
string n;
cin >> n;
vector<char> A;
for (long long int i = 0; i < (n.size()); i++) {
A.push_back((n[i]));
}
long long int ele = 1, countt = 0;
for (long long int i = 1; i < A.size(); i++) {
if (A[i] == A[i - 1] && ele < 5) {
ele++;
} else {
ele = 1;
countt++;
}
}
cout << ++countt;
}
void template1() {
long long int t;
cin >> t;
while (t--) {
solve();
cout << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
cout << "\n";
return 0;
}
long long int fact(int n) { return (n == 1 || n == 0) ? 1 : n * fact(n - 1); }
int hexadecimalToDecimal(char hexVal[]) {
int len = strlen(hexVal);
int base = 1;
int dec_val = 0;
for (int i = len - 1; i >= 0; i--) {
if (hexVal[i] >= '0' && hexVal[i] <= '9') {
dec_val += (hexVal[i] - 48) * base;
base = base * 16;
} else if (hexVal[i] >= 'A' && hexVal[i] <= 'F') {
dec_val += (hexVal[i] - 55) * base;
base = base * 16;
}
}
return dec_val;
}
long long int fact(long long int n, long long int p) {
long long int ans = 1;
for (long long int i = 1; i <= n; i++) {
ans *= i;
ans %= p;
}
return ans;
}
long long int power(long long int x, long long int n, long long int p) {
long long int res = 1;
if (n == 1) return x % p;
if (n % 2 == 1) res = x % p;
long long int temp = power(x, n / 2, p);
return res * ((temp * temp) % p) % p;
}
long long int inv(long long int a, long long int p) {
return power(a, p - 2, p);
}
long long int nCr(long long int n, long long int r, long long int p) {
if (r > n) return 0;
long long int t1 = fact(n, p);
long long int t2 = inv(fact(r, p), p);
long long int t3 = inv(fact(n - r, p), p);
return (((t1 * t2) % p) * t3) % p;
}
vector<vector<long long int> > multiply(vector<vector<long long int> > &a,
vector<vector<long long int> > &b,
long long int p) {
int r1, c1, r2, c2;
r1 = a.size();
c1 = a[0].size();
r2 = b.size();
c2 = b[0].size();
vector<vector<long long int> > result(r1, vector<long long int>(c2, 0));
if (c1 != r2) return result;
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
result[i][j] += a[i][k] * b[k][j];
result[i][j] %= p;
}
}
}
return result;
}
vector<vector<long long int> > power(vector<vector<long long int> > &a,
long long int n, long long int p) {
int r = a.size();
vector<vector<long long int> > result(r, vector<long long int>(r, 0));
for (int i = 0; i < r; i++) result[i][i] = 1;
while (n > 0) {
if (n % 2 == 1) {
result = multiply(result, a, p);
}
n /= 2;
a = multiply(a, a, p);
}
return result;
}
const long long MAX_SIZE = 1000001;
vector<long long> isprime(MAX_SIZE, true);
vector<long long> SPF(MAX_SIZE);
void manipulated_seive(long long int N) {
isprime[0] = isprime[1] = false;
for (long long int i = 2; i < N; i++) {
if (isprime[i]) {
prime.push_back(i);
SPF[i] = i;
}
for (long long int j = 0;
j < (int)prime.size() && i * prime[j] < N && prime[j] <= SPF[i]; j++) {
isprime[i * prime[j]] = false;
SPF[i * prime[j]] = prime[j];
}
}
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
const int MAX = 1000001;
int factor[MAX] = {0};
void generatePrimeFactors() {
factor[1] = 1;
for (int i = 2; i < MAX; i++) factor[i] = i;
for (int i = 4; i < MAX; i += 2) factor[i] = 2;
for (int i = 3; i * i < MAX; i++) {
if (factor[i] == i) {
for (int j = i * i; j < MAX; j += i) {
if (factor[j] == j) factor[j] = i;
}
}
}
}
long long int calculateNoOFactors(long long int n) {
if (n == 1) return 1;
int ans = 1;
int dup = factor[n];
int c = 1;
int j = n / factor[n];
while (j != 1) {
if (factor[j] == dup)
c += 1;
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
j = j / factor[j];
}
ans = ans * (c + 1);
return ans;
}
| 7 | CPP |
n=int(input())
s=input()
if len(set(s))==1:
print('NO')
else:
k=str()
for i in range(n-1):
if len(set(s[i:i+2]))!=1:
k=s[i:i+2]
break
print('YES')
print(k)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
inline long long Getint() {
char ch = getchar();
long long x = 0, fh = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') fh = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
(x *= 10) += ch ^ 48;
ch = getchar();
}
return x * fh;
}
const int Mod = 998244353;
const int N = 55;
const int M = 3005;
int n, m, su, A[N], W[N];
int nw, cnt, top[2];
int vis[M][N * 2];
long long f[2][M][N * 2];
struct nod {
int x, y;
nod() {}
nod(int a, int b) {
x = a;
y = b;
}
} q[2][M * N * 2];
inline void Up(int x, int y, long long w) {
if (vis[x][y] == cnt)
(f[nw][x][y] += w) %= Mod;
else
vis[x][y] = cnt, f[nw][x][y] = w % Mod, q[nw][++top[nw]] = nod(x, y);
}
inline void Clear() {
cnt++;
top[nw] = 0;
}
long long inv[M * N];
long long Ans[N];
inline long long Solve(int x) {
nw = 0;
Clear();
int bsu = 0;
for (int i = 1; i <= n; i++) {
if (A[i] == 0) bsu += W[i];
}
Up(bsu, W[x], 1);
for (int t = 1; t <= m; t++) {
nw ^= 1;
Clear();
for (int i = 1; i <= top[nw ^ 1]; i++) {
nod w = q[nw ^ 1][i];
int gd = t - 1 - (bsu - w.x);
int wsu = su + gd - (bsu - w.x);
long long p = w.y * inv[wsu] % Mod;
if (A[x] == 0) {
long long badres = w.x - w.y;
long long godres = wsu - w.x;
long long p1 = badres * inv[wsu] % Mod;
long long p2 = godres * inv[wsu] % Mod;
if (badres) Up(w.x - 1, w.y, f[nw ^ 1][w.x][w.y] * p1 % Mod);
if (w.y) Up(w.x - 1, w.y - 1, f[nw ^ 1][w.x][w.y] * p % Mod);
Up(w.x, w.y, f[nw ^ 1][w.x][w.y] * p2 % Mod);
} else {
long long badres = w.x;
long long godres = wsu - w.x - w.y;
long long p1 = badres * inv[wsu] % Mod;
long long p2 = godres * inv[wsu] % Mod;
if (badres) Up(w.x - 1, w.y, f[nw ^ 1][w.x][w.y] * p1 % Mod);
Up(w.x, w.y, f[nw ^ 1][w.x][w.y] * p2 % Mod);
Up(w.x, w.y + 1, f[nw ^ 1][w.x][w.y] * p % Mod);
}
}
}
for (int i = 1; i <= top[nw]; i++) {
nod w = q[nw][i];
Ans[x] += w.y * f[nw][w.x][w.y] % Mod;
}
return (Ans[x] % Mod + Mod) % Mod;
}
int main() {
n = Getint();
m = Getint();
inv[0] = inv[1] = 1;
for (int i = 2; i <= 5000; i++) {
inv[i] = (Mod - Mod / i) * inv[Mod % i] % Mod;
}
for (int i = 1; i <= n; i++) A[i] = Getint();
for (int i = 1; i <= n; i++) W[i] = Getint(), su += W[i];
for (int i = 1; i <= n; i++) cout << Solve(i) << '\n';
return 0;
}
| 9 | CPP |
x = input()
if x[0]==x[1] or x[1]==x[2] or x[2]==x[3]:
print("Bad")
else:
print("Good") | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1100;
const long long inf = (long long)1e18 + 1e9;
long long a[maxn];
long long b[maxn];
long long s[maxn];
vector<int> v[2];
vector<int> res;
int n;
inline void merge(int i, int j) {
for(int k = j - 2; k >= i + 2; k -= 2) res.push_back(k);
res.push_back(i + 1);
}
inline void print(const vector<int>& v) {
int sz = v.size();
for(int i = n - 1; i > v[sz - 1]; -- i) res.push_back(i);
for(int i = sz - 1; i >= 1; -- i) merge(v[i - 1], v[i]);
for(int i = 0; i < v[0]; ++ i) res.push_back(0);
cout << res.size() << "\n";
for(auto x : res) cout << x + 1 << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0; i < n; ++ i) {
cin >> a[i];
if(a[i] > 0) v[i & 1].push_back(i);
}
long long ans1 = 0, ans2 = 0;
for(auto x : v[0]) ans1 += a[x];
for(auto x : v[1]) ans2 += a[x];
if(ans1 > ans2) {
cout << ans1 << "\n";
print(v[0]);
} else {
if(ans2 == 0) {
ans2 = -inf; int pos;
for(int i = 0; i < n; ++ i) {
if(ans2 < a[i]) {
ans2 = a[i];
pos = i;
}
}
v[1].push_back(pos);
}
cout << ans2 << "\n";
print(v[1]);
}
return 0;
}
| 0 | CPP |
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
using namespace std;
#define _int64 long long
#define mo 1000000007
int a[400];
int b[400];
int can[400][400];
int num[400][400];
int used[400];
int sum[400][400][200];
int d[400][400][200];
int c[400][400];
int main()
{
int i,j,l,n,o,p,k,tmp;
for (i=0;i<400;i++)
{
c[i][0]=1;c[i][i]=1;
for (j=1;j<i;j++)
{
c[i][j]=c[i-1][j-1]+c[i-1][j];
if (c[i][j]>=mo) c[i][j]-=mo;
}
}
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for (i=0;i<n;i++)
scanf("%d",&b[i]);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
{
can[i][j]=1;
for (k=0;k<=i;k++)
if (a[k]==b[j]) can[i][j]=0;
for (k=0;k<=j;k++)
if (a[i]==b[k]) can[i][j]=0;
num[i][j]=0;
memset(used,0,sizeof(used));
for (k=i+1;k<n;k++)
used[a[k]]=1;
for (k=j+1;k<n;k++)
if (used[b[k]]==1) num[i][j]++;
}
for (i=n-1;i>=0;i--)
for (j=n-1;j>=0;j--)
for (k=0;k<n/3;k++)
{
if ((can[i][j]==0)||(num[i][j]<k+1)) d[i][j][k]=0;
else
{
if ((k==0)||(i==n-1)||(j==n-1)) tmp=1;
else tmp=sum[i+1][j+1][k-1];
tmp=(_int64)tmp*c[k*3+2][2]%mo*2%mo*(num[i][j]-k*3)%mo;
d[i][j][k]=tmp;
//cout<<i<<" "<<j<<" "<<k<<" "<<d[i][j][k]<<endl;
}
sum[i][j][k]=0;
if (i+1<n)
{
sum[i][j][k]+=sum[i+1][j][k];
if (sum[i][j][k]>=mo) sum[i][j][k]-=mo;
}
if (j+1<n)
{
sum[i][j][k]+=sum[i][j+1][k];
if (sum[i][j][k]>=mo) sum[i][j][k]-=mo;
}
if ((i+1<n)&&(j+1<n))
{
sum[i][j][k]-=sum[i+1][j+1][k];
if (sum[i][j][k]<0) sum[i][j][k]+=mo;
}
sum[i][j][k]+=d[i][j][k];
if (sum[i][j][k]>=mo) sum[i][j][k]-=mo;
}
printf("%d\n",d[0][0][n/3-1]);
return 0;
} | 0 | CPP |
from functools import *
from operator import *
n,m = (int(x) for x in input().split())
a = [list(int(x) for x in input().split()) for i in range(n)]
r = list(0 for i in a)
x = reduce(xor, [a[i][r[i]] for i in range(n)], 0)
for i in range(n):
for j in range(m):
x = x ^ a[i][r[i]] ^ a[i][j]
r[i] = j
if x > 0:
print("TAK")
print(*(x+1 for x in r))
exit(0)
print("NIE")
| 8 | PYTHON3 |
#include <iostream>
using namespace std;
int main()
{
int a,b;
int road[101];
for(int i=0;i<101;i++){
road[i] = 0;
}
while(cin >> a >> b){
if(!a&&!b){
bool flag = true;
if(road[1]%2 == 0 || road[2]%2 == 0)
flag = false;
for(int i=3;i<=100;i++){
if(road[i]%2 == 1)
flag = false;
}
if(flag)
cout << "OK" << endl;
else
cout << "NG" << endl;
for(int i=0;i<101;i++){
road[i] = 0;
}
}
else{
road[a]++;
road[b]++;
}
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
ofstream g("date.out");
int t;
vector<int> diviz[100005];
int main() {
cin >> t;
for (int i = 1; i <= 100000; i++) {
for (int j = 2 * i; j <= 100000; j += i) {
diviz[j].push_back(i);
}
}
while (t--) {
int n, sol = 0;
cin >> n;
vector<int> v(n + 5, 0);
vector<int> dp(n + 5, 0);
for (int i = 1; i <= n; i++) {
cin >> v[i];
dp[i] = 1;
for (int j = 0; j < diviz[i].size(); j++) {
int val = diviz[i][j];
if (v[i] > v[val]) {
dp[i] = max(dp[i], dp[val] + 1);
}
}
sol = max(sol, dp[i]);
}
cout << sol << '\n';
}
}
| 8 | CPP |
import math
elem = int(input())
for i in range(elem):
num = int(input())
ar = []
for i in input().split(' '):
ar.append(int(i))
curr = 0
for i in range(len(ar) - 1):
if ar[i] > ar[i+1]:
temp = ar[i] - ar[i+1]
ar[i+1] = ar[i]
curr = max(curr, temp)
print(math.ceil(math.log(curr+1, 2))) | 9 | PYTHON3 |
#!/usr/bin/env python
import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import (
accumulate, combinations, combinations_with_replacement, groupby,
permutations, product)
from math import (
acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians,
sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits
def inp():
return(int(input()))
def inlist():
return(list(map(int, input().split())))
def instr():
s = input()
return(list(s[:len(s)]))
def invr():
return(map(int, input().split()))
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
def input(): return sys.stdin.readline().rstrip("\r\n")
# endregion
n, m = invr()
a = []
for _ in range(n):
a.append(input())
count = defaultdict(lambda: 0)
found = ""
for i in a:
count[i] += 1
rev = i[::-1]
if rev == i:
found = rev
else:
count[rev] += 1
for k in count:
if count[k] == 2:
rev = k[::-1]
found = k + found + rev
count[k] = -1
count[rev] = -1
print(len(found))
print(found)
| 8 | PYTHON3 |
for _ in range(int(input())):
n,m = [int(x) for x in input().split()]
if n != m:
print('Happy Alex')
exit()
print('Poor Alex') | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c;
cin >> c;
while (c--) {
cin >> a >> b;
if (a == b) {
cout << 0 << endl;
} else if (b > a) {
if (b % 2 == 0) {
if (a % 2 == 0) {
cout << 2 << endl;
} else {
cout << 1 << endl;
}
} else {
if (a % 2 == 0) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
}
} else {
if (b % 2 == 0) {
if (a % 2 == 0) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else {
if (a % 2 == 0) {
cout << 2 << endl;
} else {
cout << 1 << endl;
}
}
}
}
}
| 7 | CPP |
s=input()
n=len(s)
def if_palindrome(s):
x=list(s)
y=list(s)
y.reverse()
if(x==y):
return True
return False
done=False
x=list(s)
x=list(set(x))
nn=len(x)
r=0
for i in range(nn):
if(s.count(x[i])%2==1):
r+=1
if(r>1):
break
if(r<=1):
print("First")
done=True
if(not done):
n=len(s)
if(n%2==1):
print("First")
else:
print("Second")
| 8 | PYTHON3 |
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
stack<double>X;
double f(string s){
double a,b;
b=X.top();X.pop();
a=X.top();X.pop();
if(s=="+")a+=b;
else if(s=="-")a-=b;
else if(s=="*")a*=b;
else if(s=="/")a/=b;
return a;
}
int main(){
string s;
while(getline(cin,s)){
stringstream ss(s);
while(ss>>s)X.push((s=="+"||s=="-"||s=="*"||s=="/")?f(s):atoi(s.c_str()));
printf("%.6f\n",X.top());
X.pop();
}
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n;
while (cin >> n, n) {
vector<string> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int minx = -1e9 - 2, maxx = 1e9 + 2;
bool none = false;
for (int i = 0; i < n; i++) {
if (a[i] == "x") {
if ((i > 0 && a[i - 1] == "x") || (i < n - 1 && a[i + 1] == "x")) {
none = true;
break;
} else if (i & 1) {
if (i > 0) minx = max(minx, atoi(a[i - 1].c_str()) + 1);
if (i < n - 1) minx = max(minx, atoi(a[i + 1].c_str()) + 1);
} else {
if (i > 0) maxx = min(maxx, atoi(a[i - 1].c_str()) - 1);
if (i < n - 1) maxx = min(maxx, atoi(a[i + 1].c_str()) - 1);
}
} else if (i & 1) {
if (i > 0 && a[i - 1] != "x" && atoi(a[i - 1].c_str()) >= atoi(a[i].c_str())) {
none = true;
} else if (i < n - 1 && a[i + 1] != "x" && atoi(a[i].c_str()) <= atoi(a[i + 1].c_str())) {
none = true;
}
if (none) break;
} else {
if (i > 0 && a[i - 1] != "x" && atoi(a[i - 1].c_str()) <= atoi(a[i].c_str())) {
none = true;
} else if (i < n - 1 && a[i + 1] != "x" && atoi(a[i].c_str()) >= atoi(a[i + 1].c_str())) {
none = true;
}
if (none) break;
}
}
if (none || minx - maxx > 0) {
cout << "none\n";
} else if (minx == maxx) {
cout << minx << endl;
} else {
cout << "ambiguous\n";
}
}
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
string mtx;
int n, m;
set<int> s;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> mtx;
int x = mtx.find("G");
int y = mtx.find("S");
s.insert(y - x);
}
if (*s.begin() < 0)
cout << -1 << endl;
else
cout << s.size() << endl;
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[100100];
int n, m;
char ss[100100];
int s[100100];
int main() {
scanf("%s", ss);
n = strlen(ss);
for (int i = 1; i <= n; i++) a[i] = ss[i - 1] - '0';
scanf("%s", ss);
m = strlen(ss);
for (int i = 1; i <= m; i++) s[i] = ss[i - 1] - '0';
sort(s + 1, s + m + 1);
int st = 1;
for (int j = m; j >= 1; j--) {
if (st > n) break;
while (a[st] >= s[j] && st <= n) {
st++;
}
if (st > n) break;
a[st] = s[j];
st++;
}
for (int i = 1; i <= n; i++) printf("%d", a[i]);
}
| 8 | CPP |
moji = input()
moji = moji.replace("?","D")
print(moji) | 0 | PYTHON3 |
n=int(input())
steps=0
steps+=n//5
steps+=(n%5)//4
steps+=((n%5)%4)//3
steps+=(((n%5)%4)%3)//2
steps+=((((n%5)%4)%3)%2)//1
print(steps) | 7 | PYTHON3 |
watermelon = int(input())
if (watermelon==2):
print("NO")
else:
ans = watermelon%2
if (ans==0):
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, k, m, n, q, a, b;
cin >> n >> k >> a >> b;
j = 0;
long long int ans = 0;
if (k == 1) {
cout << (n - 1) * a;
return 0;
}
while (n != 1) {
if (n % k == 0) {
q = n - n / k;
ans += min(q * a, b);
n /= k;
} else {
q = n % k;
ans += q * a;
n -= q;
if (n == 0) {
ans -= a;
break;
}
}
}
cout << ans;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
int pri[26][26][5005];
int bara[26][5005];
int main() {
int n, i, j, k, p, q, x, y, z;
string str;
cin >> str;
n = str.size();
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) pri[str[i] - 'a'][str[(i + j) % n] - 'a'][j]++;
}
x = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (pri[str[i] - 'a'][str[(i + j) % n] - 'a'][j] == 1)
bara[str[i] - 'a'][j]++;
}
}
for (i = 0; i < 26; i++) {
y = 0;
for (j = 0; j < 5005; j++) y = max(y, bara[i][j]);
x += y;
}
double ans = (x + 0.0) / n;
printf("%.10lf\n", ans);
return 0;
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
void ckmin(T &a, U b) {
if (a > b) a = b;
}
template <class T, class U>
void ckmax(T &a, U b) {
if (a < b) a = b;
}
int N, M;
int arr[1000013];
pair<int, int> range[513];
int parent[513];
vector<int> edge[513];
bitset<513> vis[513];
int stor[20][1000013];
int dp[513][513];
vector<int> ord;
int ans = 1;
int ad(int a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
return a;
}
int sb(int a, int b) {
a -= b;
if (a < 0) a += 998244353;
return a;
}
int mul(int a, int b) { return (long long)a * b % 998244353; }
bool cmp(int a, int b) { return range[a].first < range[b].first; }
int solve(int lt, int rt) {
if (lt > rt) return 1;
if (vis[lt][rt]) return dp[lt][rt];
vis[lt][rt] = true;
int mn = lt;
for (auto i = (lt); i < (rt + 1); i++) {
if (ord[i] < ord[mn]) mn = i;
}
int sl = 0, sr = 0;
for (auto i = (lt); i < (mn + 1); i++) {
sl = ad(sl, mul(solve(lt, i - 1), solve(i, mn - 1)));
}
for (auto i = (mn); i < (rt + 1); i++) {
sr = ad(sr, mul(solve(mn + 1, i), solve(i + 1, rt)));
}
dp[lt][rt] = mul(sl, sr);
return dp[lt][rt];
}
void dfs(int u) {
sort((edge[u]).begin(), (edge[u]).end(), cmp);
for (int v : edge[u]) {
dfs(v);
}
for (auto i = (0); i < (((int)(edge[u]).size())); i++) {
ord.push_back(edge[u][i]);
if (i == ((int)(edge[u]).size()) - 1 ||
range[edge[u][i + 1]].first != range[edge[u][i]].second + 1) {
int n = ((int)(ord).size());
for (auto i = (0); i < (n); i++) {
for (auto j = (i); j < (n); j++) {
vis[i][j] = false;
}
}
ans = mul(ans, solve(0, n - 1));
ord.clear();
}
}
return;
}
int query(int l, int r) {
int sz = 31 - __builtin_clz(r - l + 1);
return min(stor[sz][l], stor[sz][r - (1 << sz) + 1]);
}
int32_t main() {
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
N++;
for (auto i = (1); i < (N + 1); i++) {
range[i] = {M, -1};
}
range[0] = {0, M + 1};
for (auto i = (1); i < (M + 1); i++) {
int x;
cin >> x;
arr[i] = x;
ckmax(range[x].second, i);
ckmin(range[x].first, i);
}
M += 2;
for (auto i = (0); i < (M); i++) {
stor[0][i] = arr[i];
}
for (auto i = (1); i < (20); i++) {
for (auto j = (0); j < (M - (1 << i) + 1); j++) {
stor[i][j] = min(stor[i - 1][j], stor[i - 1][j + (1 << (i - 1))]);
}
}
for (auto i = (0); i < (N); i++) {
int mn = query(range[i].first, range[i].second);
if (mn < i) {
cout << "0\n";
return 0;
}
}
for (auto i = (N)-1; i >= (1); i--) {
for (auto j = (0); j < (i); j++) {
if (range[j].second < range[i].first || range[j].first > range[i].second)
continue;
if (range[j].first < range[i].first &&
range[i].second < range[j].second) {
parent[i] = j;
}
}
edge[parent[i]].push_back(i);
}
dfs(0);
cout << ans << '\n';
return 0;
}
| 12 | CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.