solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int INF = 0x3f3f3f3f;
int a[110], b[110];
set<int> st[110];
set<int>::iterator it;
int main() {
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
b[i] = x;
st[x].insert(a[i]);
}
int ans = 0;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
int s = b[x];
it = st[s].end(), it--;
if (*it != a[x]) ans++;
}
cout << ans << endl;
return 0;
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200 + 5;
int n, m;
int he[maxn];
bool cur[maxn];
int main() {
scanf("%d%d", &m, &n);
memset(cur, false, 103 * sizeof(bool));
for (int i = 1; i <= m; i++) {
int k, kk;
scanf("%d", &k);
for (int j = 1; j <= k; j++) {
scanf("%d", &kk);
cur[kk] += 1;
}
}
bool ss = 1;
for (int i = 1; i <= n; i++) {
if (cur[i] == 0) ss = 0;
}
if (ss) {
printf("Yes");
exit(0);
}
printf("No");
return 0;
}
| 800 | CPP |
n = int(input())
sum = 0
while n > 0:
sum = sum + (n % 2)
n = n // 2
print(sum)
| 1,000 | PYTHON3 |
l=int(input())
s=input()
a=[]
for i in range(0,l-1):
a.append(s[i]+s[i+1])
a.sort()
fs=""
m,c=0,1
#print(a)
for i in range(len(a)):
count = a.count(a[i])
if count>m:
m=count
fs=a[i]
"""for i in range(1,len(a)):
if a[i] == a[i-1]:
c+=1
if c>=m:
m=c
fs=a[i]
else:
c=1"""
print(fs)
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long i, t, s = 0, c = 0;
cin >> t;
int ar[t];
for (i = 0; i < t; i++) cin >> ar[i];
sort(ar, ar + t);
for (i = 0; i < t; i++) {
if (s <= ar[i]) {
c++;
s = s + ar[i];
}
}
cout << c;
}
| 1,300 | CPP |
a = [-1] * 16
a[0] = 0
for i in range(1,16):
for j in [4, 6, 9]:
if i >= j and a[i - j] != -1:
a[i] = max(a[i], a[i - j] + 1)
for _ in range(int(input())):
n = int(input())
if n < 16:
print(a[n])
else:
t = (n - 16) // 4 + 1
print(a[n - t * 4] + t) | 1,300 | PYTHON3 |
n,m=map(int,input().split())
CAN0=n-m
CAN1=max(1,m)
print(min(CAN0,CAN1))
| 900 | PYTHON3 |
a,b=[],[]
for _ in range(int(input())):
x,y=map(int,input().split())
a.append((x,y))
a.sort()
ans=a[0][1]
for i in range(1,len(a)):
x,y=a[i]
if y>=ans:ans=y
else:ans=x
print(ans) | 1,400 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <int MOD>
struct Modular {
int value;
Modular(const Modular& k) : value(k.value) {}
template <typename V>
Modular(const V& v) : value(v % MOD) {
if (value < 0) {
value += MOD;
}
}
Modular() : Modular(0) {}
Modular& operator+=(Modular const& b) {
value += b.value;
if (value >= MOD) value -= MOD;
return *this;
}
Modular& operator-=(Modular const& b) {
value -= b.value;
if (value < 0) value += MOD;
return *this;
}
Modular& operator*=(Modular const& b) {
value = (long long)value * b.value % MOD;
return *this;
}
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) { return *this += 1; }
Modular operator--(int) { return *this -= 1; }
friend Modular inverse(Modular a) { return mexp(a, MOD - 2); }
Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
friend Modular operator+(Modular a, Modular const b) { return a += b; }
friend Modular operator-(Modular a, Modular const b) { return a -= b; }
friend Modular operator-(Modular const a) { return 0 - a; }
friend Modular operator*(Modular a, Modular const b) { return a *= b; }
friend Modular operator/(Modular a, Modular const b) { return a /= b; }
friend std::ostream& operator<<(std::ostream& os, Modular const& a) {
return os << a.value;
}
friend bool operator==(Modular const& a, Modular const& b) {
return a.value == b.value;
}
friend bool operator!=(Modular const& a, Modular const& b) {
return a.value != b.value;
}
};
template <int T, typename V>
Modular<T> mexp(Modular<T> a, V e) {
if (a == 0) return e == 0 ? 1 : 0;
Modular<T> res = 1;
while (e) {
if (e & 1) res *= a;
a *= a;
e >>= 1;
}
return res;
}
using Mint = Modular<998244353>;
int main() {
int n, m, k;
cin >> m >> n >> k;
vector<Mint> dp(k + 1, 0);
vector<Mint> pw_inv(k + 1, 1);
vector<Mint> pw_m(k + 1, 1);
vector<vector<Mint> > choose(k + 1, vector<Mint>(k + 1));
vector<Mint> m_choose(k + 1);
choose[0][0] = 1;
for (int i = 1; i <= k; ++i) {
choose[i][0] = 1;
choose[0][i] = 0;
}
for (int i = 1; i <= k; ++i) {
for (int j = 1; j <= i; ++j) {
choose[i][j] = choose[i - 1][j] + choose[i - 1][j - 1];
}
}
m_choose[0] = 1;
for (int i = 1; i <= min(k, m); ++i) {
m_choose[i] = m_choose[i - 1] * Mint(m - i + 1) / i;
}
Mint inv = inverse(Mint(n));
for (int i = 1; i <= k; ++i) {
pw_inv[i] = inv * pw_inv[i - 1];
pw_m[i] = m * pw_m[i - 1];
}
dp[1] = 1;
for (int i = 2; i <= k; ++i) {
dp[i] = mexp(Mint(i), k);
for (int j = 1; j < i; ++j) {
dp[i] -= dp[j] * choose[i][j];
}
}
Mint ans = 0;
for (int j = 1; j <= min(k, m); ++j) {
ans += m_choose[j] * dp[j] * pw_inv[j];
}
cout << ans << "\n";
return 0;
}
| 2,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
long long k;
cin >> n >> k;
vector<vector<vector<long long>>> dp(
n, vector<vector<long long>>(2, vector<long long>(2 * n + 1, 0)));
vector<int> cor(n);
for (int i = 0; i < n; i++) {
cin >> cor[i];
}
if (cor[0] == cor[n - 1]) {
dp[n - 1][0][n] = k - 1;
dp[n - 1][1][n + 1] = 1;
} else {
dp[n - 1][0][n + 1] = 1;
dp[n - 1][0][n] = k - 2;
dp[n - 1][1][n] = 1;
}
for (int i = n - 1; i > 0; i--) {
if (cor[i] == cor[i - 1]) {
for (int j = 0; j < 2 * n + 1; j++) {
if (dp[i][0][j] != 0) {
(dp[i - 1][0][j] += ((k - 1) * dp[i][0][j]) % MOD) %= MOD;
(dp[i - 1][1][j + 1] += dp[i][0][j]) %= MOD;
}
if (dp[i][1][j] != 0) {
(dp[i - 1][0][j - 1] += ((k - 1) * dp[i][1][j]) % MOD) %= MOD;
(dp[i - 1][1][j] += dp[i][1][j]) %= MOD;
}
}
} else {
for (int j = 0; j < 2 * n + 1; j++) {
if (dp[i][0][j] != 0) {
(dp[i - 1][1][j] += dp[i][0][j]) %= MOD;
(dp[i - 1][0][j + 1] += dp[i][0][j]) %= MOD;
(dp[i - 1][0][j] += ((k - 2) * dp[i][0][j]) % MOD) %= MOD;
}
if (dp[i][1][j] != 0) {
(dp[i - 1][1][j - 1] += dp[i][1][j]) %= MOD;
(dp[i - 1][0][j] += dp[i][1][j]) %= MOD;
(dp[i - 1][0][j - 1] += ((k - 2) * dp[i][1][j]) % MOD) %= MOD;
}
}
}
}
long long ans = 0;
for (int i = n + 1; i <= 2 * n; i++) {
(ans += dp[0][0][i]) %= MOD;
if (i + 1 <= 2 * n) (ans += dp[0][1][i + 1]) %= MOD;
}
cout << ans << endl;
return 0;
}
| 2,200 | CPP |
n = int(input())
j = 0
temp = 0
for i in range(n):
k, t = map(int, input().split())
j += t - k
temp = max(temp, j)
print(temp) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char str[][10] = {"vaporeon", "jolteon", "flareon", "espeon",
"umbreon", "leafeon", "glaceon", "sylveon"};
vector<string> s;
string ans;
int n, flag = 0, i, j;
char c[10];
for (i = 0; i < 8; i++) s.push_back(str[i]);
cin >> n >> c;
for (i = 0; i < 8; i++) {
flag = 0;
for (j = 0; j < s[i].size(); j++)
if (s[i][j] != c[j] && c[j] != '.') {
flag = 1;
break;
}
if (!flag) {
ans = s[i];
break;
}
}
cout << ans;
return 0;
}
| 1,000 | CPP |
y = int(input()) + 1
while not len(set(list(str(y)))) == len(list(str(y))):
y += 1
print(y)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
map<string, vector<string>> mymap;
map<string, bool> vis;
int dfs(string v) {
vis[v] = true;
int m = 0;
for (auto i : mymap[v]) {
if (!vis[i]) {
m = max(m, dfs(i));
}
}
return m + 1;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string a, b;
cin >> a;
int len = a.length();
for (int j = 0; j < len; j++) a[j] = toupper(a[j]);
cin >> b;
cin >> b;
len = b.length();
for (int j = 0; j < len; j++) b[j] = toupper(b[j]);
mymap[b].push_back(a);
vis[a] = false;
vis[b] = false;
}
int m = 0;
for (auto i : mymap) {
if (!vis[i.first]) {
m = max(m, dfs("POLYCARP"));
}
}
cout << m;
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
const int md = 1e9 + 7;
const long long inf = 4e18;
const int OO = 0;
const int OOO = 1;
using namespace std;
const int M = 5050505;
int sieve[M] = {};
void prep() {
for (int i = 2; i * i <= M; i++) {
if (!sieve[i]) {
sieve[i] = i;
for (int j = i * i; j < M; j += i) sieve[j] = i;
}
}
}
vector<int> factor(int v) {
vector<int> rtn;
int p, c;
while (sieve[v]) {
p = sieve[v];
c = 0;
while (v % p == 0) v /= p, c++;
if (c & 1) rtn.push_back(p);
}
if (v > 1) rtn.push_back(v);
return rtn;
}
void factors(const vector<int> &f, int i, int val, int extra,
vector<pair<int, int>> &res) {
if (i == f.size()) {
res.emplace_back(val, extra);
return;
}
factors(f, i + 1, val, extra + 1, res);
factors(f, i + 1, val * f[i], extra, res);
}
int best[M][8];
int n, q;
vector<int> a;
int R[200005][15];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
prep();
for (int i = 0; i < M; i++)
for (int j = 0; j < 8; j++) best[i][j] = md;
cin >> n >> q;
a.resize(n);
for (auto &i : a) cin >> i;
for (int j = 0; j < 15; j++) R[n - 1][j] = md;
vector<int> f = factor(a[n - 1]);
vector<pair<int, int>> sub;
factors(f, 0, 1, 0, sub);
for (const auto &i : sub)
best[i.first][i.second] = min(best[i.first][i.second], n - 1);
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < 15; j++) R[i][j] = R[i + 1][j];
f = factor(a[i]);
sub.clear();
factors(f, 0, 1, 0, sub);
for (const auto &p : sub) {
for (int j = 0; j < 8; j++)
R[i][p.second + j] = min(R[i][p.second + j], best[p.first][j]);
}
for (const auto &p : sub)
best[p.first][p.second] = min(best[p.first][p.second], i);
}
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
for (int i = 0; i < 15; i++)
if (R[l][i] <= r) {
cout << i << '\n';
break;
}
}
}
| 2,900 | CPP |
number, count = input().split(' ')
count = int(count)
for _ in range(count):
figure = int(number[-1])
if figure == 0:
number = number[:len(number)-1]
else:
number = number[:len(number)-1] + str(figure-1)
print(number) | 800 | PYTHON3 |
import re
import math
n=int(input())
for i in range(n):
r=input()
s=re.findall(r'[0-9]+|[A-Z]+', r)
if len(s) == 2:
c = []
t = len(s[0])
for i in range(t):
c.append(ord(s[0][i])-64)
column = 0
for i in range(t):
column += c[i] * 26**(t - i - 1)
l = ['R', 'C']
l.insert(1,int(s[1]))
l.insert(3, column)
if len(s) == 4:
c = []
column = int(s[3])
k=0
while 26*(26**k-1)//25<column:
k+=1
c=[]
while k>=1:
t=column%26
if t==0:
zimu='Z'
column=(column-26)//26
else:
zimu=chr(t+64)
column = (column - t) // 26
c.insert(0,zimu)
k-=1
p = ''
for i in range(len(c)):
p += c[i]
l = []
l.append(p)
l.append(int(s[1]))
q=''
for i in range(len(l)):
q+=str(l[i])
print(q)
| 1,600 | PYTHON3 |
num = int(input())
n = input()
l = map(int,n.split())
main = list(l)
maxi= main[0]
mini = main[0]
amazing = 0
for i in main:
if i > maxi:
maxi = i
amazing += 1
elif i < mini:
mini = i
amazing += 1
print(amazing)
| 800 | PYTHON3 |
n, k = map(int, input().split())
s = list(map(int, input().split()))
print((len([x for x in s if x + k <= 5])) // 3)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int N;
int Triangle[305];
bool Use[305];
struct Element {
int nodes[5], cnt;
} Sol[900005], Sol2[900005];
int K;
bool E[900005];
void Erase(int pos) {
swap(Sol[pos], Sol[K]);
--K;
}
void Insert(int pos, int v[], int cnt) {
for (int i = 1; i <= cnt; i++) Sol[pos].nodes[i] = v[i];
Sol[pos].cnt = cnt;
}
void SolveEven() {
Sol[1].nodes[1] = 1;
Sol[1].nodes[2] = 2;
Sol[1].nodes[3] = 3;
Sol[1].cnt = 3;
Sol[2].nodes[1] = 1;
Sol[2].nodes[2] = 2;
Sol[2].nodes[3] = 4;
Sol[2].cnt = 3;
Sol[3].nodes[1] = 3;
Sol[3].nodes[2] = 4;
Sol[3].nodes[3] = 1;
Sol[3].cnt = 3;
Sol[4].nodes[1] = 3;
Sol[4].nodes[2] = 4;
Sol[4].nodes[3] = 2;
Sol[4].cnt = 3;
Triangle[1] = 3;
Triangle[3] = 1;
K = 4;
for (int i = 5; i <= N; i += 2) {
int a = i, b = i + 1;
memset(Use, 0, sizeof(Use));
memset(E, 0, sizeof(E));
int init = K;
for (int k = 1; k <= init; k++) {
if (Sol[k].cnt == 3 && Use[Sol[k].nodes[1]] == 0) {
int p = Sol[k].nodes[1], q = Sol[k].nodes[2], r = Sol[k].nodes[3];
Use[Sol[k].nodes[1]] = 1;
int v[] = {0, p, a, q, r};
E[k] = 1;
++K;
Insert(K, v, 4);
++K;
v[4] = b;
Insert(K, v, 4);
++K;
int v2[] = {0, p, q, b, 0};
Insert(K, v2, 3);
}
}
for (int k = 1; k <= K; k++) {
if (Sol[k].cnt == 4 && Sol[k].nodes[1] == 1 && Sol[k].nodes[2] == a &&
Sol[k].nodes[3] == 2 && Sol[k].nodes[4] == b) {
E[k] = 1;
++K;
int v[] = {0, a, b, 1};
Insert(K, v, 3);
++K;
v[3] = 2;
Insert(K, v, 3);
break;
}
}
int cnt = 0;
for (int k = 1; k <= K; k++)
if (E[k] == 0) Sol2[++cnt] = Sol[k];
K = cnt;
for (int k = 1; k <= K; k++) Sol[k] = Sol2[k];
}
}
void SolveOdd() {
Sol[1].nodes[1] = 2;
Sol[1].nodes[2] = 3;
Sol[1].nodes[3] = 1;
Sol[1].cnt = 3;
Sol[2].nodes[1] = 2;
Sol[2].nodes[2] = 3;
Sol[2].nodes[3] = 1;
Sol[2].cnt = 3;
K = 2;
for (int i = 4; i <= N; i += 2) {
int a = i, b = i + 1;
memset(Use, 0, sizeof(Use));
memset(E, 0, sizeof(E));
int init = K;
for (int k = 1; k <= init; k++) {
if (Sol[k].cnt == 3 && Use[Sol[k].nodes[1]] == 0) {
int p = Sol[k].nodes[1], q = Sol[k].nodes[2], r = Sol[k].nodes[3];
Use[Sol[k].nodes[1]] = 1;
int v[] = {0, p, a, q, r};
E[k] = 1;
++K;
Insert(K, v, 4);
++K;
v[4] = b;
Insert(K, v, 4);
++K;
int v2[] = {0, p, q, b, 0};
Insert(K, v2, 3);
}
}
int v[] = {0, a, b, 1};
++K;
Insert(K, v, 3);
++K;
Insert(K, v, 3);
int cnt = 0;
for (int k = 1; k <= K; k++)
if (E[k] == 0) Sol2[++cnt] = Sol[k];
K = cnt;
for (int k = 1; k <= K; k++) Sol[k] = Sol2[k];
}
}
int main() {
cin >> N;
if (N % 2 == 0)
SolveEven();
else
SolveOdd();
printf("%d\n", K);
for (int i = 1; i <= K; i++) {
printf("%d ", Sol[i].cnt);
for (int j = 1; j <= Sol[i].cnt; j++) printf("%d ", Sol[i].nodes[j]);
printf("\n");
}
return 0;
}
| 2,800 | CPP |
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n=int(input())
L=list(map(int,input().split()))
if L[0]<L[-1]:
print('YES')
else:
print('NO')
| 1,400 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int M = 33;
const int K = 670000;
const int LIT = 2500;
const int INF = 1 << 28;
const int base = 137;
const double eps = 1e-10;
const int dir[5][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
const int dir2[10][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1},
{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
const int ABS(int a) { return a > 0 ? a : -a; }
string s;
int cnt, n, pr[N];
void init() {
n = s.size();
cnt = 1;
memset(pr, 0, sizeof(pr));
pr[1] = 1;
for (int i = 2; i <= n; i++) {
if (pr[i]) continue;
int num = ++cnt;
for (int j = 1;; j++) {
int tmp = i * j;
if (tmp > n) break;
if (!pr[tmp])
pr[tmp] = num;
else {
for (int k = 2; k < tmp; k++)
if (pr[k] == num) pr[k] = pr[tmp];
num = pr[tmp];
}
}
}
}
void solve() {
int num[M] = {0};
for (int i = 0; i < n; i++) num[s[i] - 'a']++;
vector<pair<long long, long long> > v;
for (int i = 1; i <= cnt; i++) {
int mid = 0;
for (int j = 1; j <= n; j++)
if (pr[j] == i) mid++;
if (!mid) continue;
v.push_back(make_pair(mid, i));
}
sort(v.begin(), v.end());
string ret = "";
for (int i = 0; i < n; i++) ret += " ";
int m = v.size();
for (int i = m - 1; i >= 0; i--) {
int amt = v[i].first, ps = v[i].second, j;
for (j = 0; j < 26; j++) {
if (num[j] >= amt) break;
}
if (j >= 26) {
cout << "NO" << endl;
return;
}
num[j] -= amt;
int ch = j;
for (j = 1; j <= n; j++) {
if (pr[j] == ps) ret[j - 1] = ch + 'a';
}
}
cout << "YES" << endl;
cout << ret << endl;
}
int main() {
while (cin >> s) {
init();
solve();
}
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <int maxn>
class Seg {
private:
struct Node {
ll maxi, sum;
};
Node join(Node const& a, Node const& b) {
return {max(a.maxi, b.maxi), a.sum + b.sum};
}
int n;
Node tree[maxn * 4];
void build(vector<int> const& v, int no, int l, int r) {
if (l == r) {
tree[no] = {v[l], v[l]};
return;
}
int m = (l + r) / 2;
build(v, no * 2 + 1, l, m);
build(v, no * 2 + 2, m + 1, r);
tree[no] = join(tree[no * 2 + 1], tree[no * 2 + 2]);
}
void upd(int no, int l, int r, int pos, int val) {
if (l == r) {
tree[no] = {val, val};
return;
}
int m = (l + r) / 2;
if (pos <= m)
upd(no * 2 + 1, l, m, pos, val);
else
upd(no * 2 + 2, m + 1, r, pos, val);
tree[no] = join(tree[no * 2 + 1], tree[no * 2 + 2]);
}
int get(int no, int l, int r, ll sum) {
if (l == r) {
if (tree[no].sum == sum) return l;
return -1;
}
int m = (l + r) / 2;
ll lsum = tree[no * 2 + 1].sum + sum;
ll rmax = tree[no * 2 + 2].maxi;
ll t = -1;
if (rmax >= lsum) t = get(no * 2 + 2, m + 1, r, lsum);
if (t >= 0) return t;
return get(no * 2 + 1, l, m, sum);
}
public:
Seg(){};
void init(int n_, vector<int> const& v) {
n = n_;
build(v, 0, 1, n);
}
void upd(int pos, int val) { upd(0, 1, n, pos, val); }
int query() { return get(0, 1, n, 0); }
};
const int maxn = 202020;
Seg<maxn> seg;
int main() {
int n, q;
cin >> n >> q;
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) cin >> v[i];
seg.init(n, v);
while (q--) {
int pos, val;
cin >> pos >> val;
seg.upd(pos, val);
cout << seg.query() << "\n";
}
}
| 2,500 | CPP |
#include <bits/stdc++.h>
inline long long Max(long long x, long long y) { return x > y ? x : y; }
inline long long Min(long long x, long long y) { return x < y ? x : y; }
inline long long Abs(long long x) { return x < 0 ? ~x + 1 : x; }
inline long long read() {
long long r = 0;
bool w = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
r = (r << 3) + (r << 1) + (ch ^ 48);
ch = getchar();
}
return w ? ~r + 1 : r;
}
const int N = 1e6 + 10;
int n, k;
long long a[N], sum, maxx, minn, b[N];
signed main() {
int T = read();
while (T--) {
n = read();
k = read();
for (int i = 1; i <= n; ++i) a[i] = read();
std::sort(a + 1, a + n + 1);
sum = 0;
++k;
for (int i = n; i >= 1; --i) {
sum += a[i];
--k;
if (!k) break;
}
printf("%lld\n", sum);
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename tp>
tp read() {
char c = getchar();
tp x = 0;
bool sgn = 0;
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') sgn = 1, c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return sgn ? -x : x;
}
template <typename tp>
void write(tp x) {
if (x < 0)
putchar('-'), write(-x);
else {
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
}
long long n, m, w, a[100005], b[100005], cnta, cntb;
signed main() {
n = read<long long>(), m = read<long long>(), w = read<long long>();
for (long long i = 1; i <= n; i++) {
long long x = read<long long>(), v = read<long long>();
if (v == 1)
a[++cnta] = x;
else
b[++cntb] = x;
}
sort(a + 1, a + cnta + 1);
sort(b + 1, b + cntb + 1);
long long ANS = 0;
for (long long i = 1; i <= cnta; i++) {
long long l = 1, r = cntb, mid, ans = cntb + 1;
while (l <= r) {
mid = (l + r) >> 1;
if (abs(a[i] + b[mid] + m) < w * (b[mid] - a[i] + m)) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
ANS += cntb - ans + 1;
}
write(ANS);
return 0;
}
| 2,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[100007];
int val[100007], n, k;
map<int, int> id;
set<int> S;
vector<int> adj[100007];
bool d[100007];
void dfs(int u) {
cout << val[u] << " ";
d[u] = 1;
for (auto v : adj[u])
if (!d[v]) dfs(v);
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i].first >> a[i].second, S.insert(a[i].first),
S.insert(a[i].second);
int t = 1;
while (S.size()) {
id[*S.begin()] = t;
val[t++] = *S.begin();
S.erase(S.begin());
}
for (int i = 1; i <= n; ++i)
adj[id[a[i].first]].push_back(id[a[i].second]),
adj[id[a[i].second]].push_back(id[a[i].first]);
for (int i = 1; i <= t; ++i)
if (adj[i].size() == 1) {
k = i;
break;
}
dfs(k);
return 0;
}
| 1,700 | CPP |
t=int(input())
for i in range(t):
n, m = map(int, input().split())
sp=list(map(int, input().split()))
print(min(m, sum(sp)))
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e6 + 9;
long long p[maxn];
long long sz[maxn];
long long lst[maxn];
pair<long long, long long> q[maxn];
long long n;
long long find_p(long long i) {
if (p[i] != i) p[i] = find_p(p[i]);
return p[i];
}
void merg(long long i, long long e) {
i = find_p(i);
e = find_p(e);
if (i == e) return;
if (sz[i] < sz[e]) swap(i, e);
sz[i] += sz[e];
p[e] = i;
lst[i] = max(lst[i], lst[e]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> n >> s;
long long c0 = 0, c1 = 0;
for (long long i = n - 1; i >= 0; i--) {
if (s[i] == '0') {
c0++;
c1 = 0;
} else if (s[i] == '1') {
c1++;
c0 = 0;
} else {
c0++;
c1++;
}
q[i] = make_pair(max(c0, c1), i);
}
sort(q, q + n);
for (long long i = 0; i < maxn; i++) {
p[i] = i;
sz[i] = 1;
lst[i] = i;
}
long long pt = 0;
for (long long j = 1; j <= n; j++) {
while (pt < n && q[pt].first < j) {
merg(q[pt].second, q[pt].second + 1);
pt++;
}
long long ans = 0;
for (long long e = lst[find_p(0)]; e < n; e = lst[find_p(e + j)]) ans++;
cout << ans << " ";
}
}
| 2,500 | CPP |
s=input( )
a=0
for i in range(len(s)):
if s[i] is 'h' and a==0:
a=1
continue
if s[i] is 'e' and a==1:
a=2
continue
if s[i] is 'l' and a==2:
a=3
continue
if s[i] is 'l' and a==3:
a=4
continue
if s[i] is 'o' and a==4:
a=5
continue
if a==5:
print('YES')
else:
print('NO')
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void _print(bool t) { cerr << t; }
void _print(long long t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(double t) { cerr << t; }
template <class T>
void _print(vector<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]\n";
}
template <class T, class V>
void _print(pair<T, V> p) {
cerr << "{";
_print(p.first);
cerr << ", ";
_print(p.second);
cerr << "}";
}
template <class T>
void _print(set<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]\n";
}
template <class T>
void _print(multiset<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]\n";
}
template <class T, class V>
void _print(map<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]\n";
}
template <class T>
void _print(T v[], long long n) {
cerr << "[ ";
for (long long i = 0; i < n; i++) {
_print(v[i]);
cerr << " ";
}
cerr << "]\n";
}
struct BIT {
vector<long long> values;
long long N, LOGN;
void init(long long n) {
N = n;
LOGN = ceil(log2(N));
values.assign(N + 1, 0);
}
void update(long long idx, long long val) {
while (idx <= N) {
values[idx] += val;
idx += idx & -idx;
}
}
long long sum(long long idx) {
long long sum = 0;
while (idx > 0) {
sum += values[idx];
idx -= idx & -idx;
}
return sum;
}
long long rangeSum(long long i, long long j) {
return sum(j) - (i == 1 ? 0 : sum(i - 1));
}
};
void run() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
if (n == 1) {
cout << "YES\n";
return;
}
if (n == 2) {
if (a[1] >= a[0])
cout << "YES\n";
else
cout << "NO\n";
return;
}
set<long long> s;
for (long long i = 0; i < n; i++) s.insert(a[i]);
if (s.size() != n) {
cout << "YES\n";
return;
}
BIT ds;
ds.init(n + 10);
long long ans = 0;
for (long long i = n - 1; i >= 0; i--) {
ans += ds.sum(a[i] - 1);
ds.update(a[i], 1);
}
if (ans % 2 == 0)
cout << "YES\n";
else
cout << "NO\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie();
long long t;
cin >> t;
for (long long tc = 1; tc <= t; tc++) {
run();
}
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int tc, i, j;
cin >> tc;
while (tc--) {
long long int n, s = 0, b;
cin >> n;
for (i = 0; i < n; i++) {
cin >> b;
if (b <= 2048) s += b;
}
s >= 2048 ? cout << "YES\n" : cout << "NO\n";
}
}
| 1,000 | CPP |
n = int(input())
d = dict()
for each_n in range(n):
s = input()
if s in d:
print(s + str(d[s]))
d[s] += 1
else:
print("OK")
d[s] = 1
| 1,300 | PYTHON3 |
n,s,t = list(map(int,input().split()))
p = list(map(int,input().split()))
path = [s]
path_d={s:0}
while(True):
# print(path_d)
# print(p[path[-1]-1])
try:
check = path_d[p[path[-1]-1]]==0
break
except:
pass
path_d[p[path[-1]-1]]=0
path.append(p[path[-1]-1])
if path[-1] == t:
break
if s==t:
print(0)
elif path[-1]==t:
print(len(path)-1)
else:
print(-1) | 1,200 | PYTHON3 |
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
INF = 1 << 60
#γγγγζΈγε§γγ
n = int(input())
s = input()
cnt1 = s.count("A")
cnt2 = n - cnt1
if cnt1 > cnt2:
print("Anton")
elif cnt1 == cnt2:
print("Friendship")
else:
print("Danik") | 800 | PYTHON3 |
import math
a,b=(int(n) for n in input().split())
ans=1
for i in range (1,min(a,b)+1):
ans*=i
print(ans) | 800 | PYTHON3 |
def solve():
from math import gcd
r, y, k = map(int, input().split())
b = max(r, y)
a = min(r, y)
c = b - gcd(a, b) - 1
x = 1 + c // a
if x >= k:
print('REBEL')
else:
print('OBEY')
t = eval(input())
for i in range(t):
solve() | 1,700 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, pair<long long, int> > > g[303030];
vector<int> gg[303030];
int used[303030];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
g[a].push_back({b, {c, i + 1}});
g[b].push_back({a, {c, i + 1}});
}
int u = 1;
priority_queue<pair<pair<long long, long long>, pair<int, int> > > dij;
dij.push({{0, 0}, {u, 0}});
long long v = 0;
vector<int> vv;
while (!dij.empty()) {
auto x = dij.top();
dij.pop();
if (used[x.second.first]) continue;
used[x.second.first] = 1;
if (x.second.second > 0) vv.push_back(x.second.second);
v += -x.first.second;
for (auto nx : g[x.second.first]) {
dij.push({{x.first.first - nx.second.first, -nx.second.first},
{nx.first, nx.second.second}});
}
}
cout << min((long long)vv.size(), (long long)k) << endl;
int cnt = 0;
for (int vvv : vv) {
if (cnt == k) break;
cnt++;
cout << vvv << " ";
}
}
| 1,800 | CPP |
n = input()
leng = len(n)
for i in range(leng):
if(i is 0 and n[i] is '9' ):
continue
elif(n[i]>'4'):
n = n.replace(n[i],str(9-int(n[i])))
n = [i for i in n]
if n[0] is '0':
n[0] = '9'
print(''.join(n)) | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
using namespace std;
signed main() {
long long n;
cin >> n;
vector<long long> v1(n);
vector<long long> v2(n);
for (long long i = 0; i < (long long)(n); ++i) {
cin >> v1[i] >> v2[i];
}
long long c = 0;
for (long long i = 0; i < (long long)(n); ++i) {
for (long long j = 0; j < (long long)(n); ++j) {
if (v1[i] == v2[j]) c++;
}
}
cout << c << "\n";
}
| 800 | CPP |
n = int(input())
str = input()
a=[]
for v in str:
if v not in a:
a.append(v)
print(n-len(a) if n < 27 else -1)
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long p[1000010], a[1000010], b[1000010];
int main() {
int n, m;
scanf("%d%d", &n, &m);
p[0] = 1;
for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * 999983;
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
a[u] += p[v], a[v] += p[u];
}
for (int i = 0; i <= n; ++i) b[i] = a[i] + p[i];
sort(a + 1, a + n + 1);
sort(b + 1, b + n + 1);
long long ans = 0, sum = 0;
for (int i = 2; i <= n; ++i) {
if (a[i] == a[i - 1])
ans += ++sum;
else
sum = 0;
}
sum = 0;
for (int i = 2; i <= n; ++i) {
if (b[i] == b[i - 1])
ans += ++sum;
else
sum = 0;
}
printf("%I64d\n", ans);
return 0;
}
| 2,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, a, i = 1, j, b;
int main() {
scanf("%d", &n);
while (i < 1100000000) {
n++;
b = n;
while (b != 0) {
a = b % 10;
if (a == 8 || a == -8) {
printf("%d\n", i);
return 0;
}
b /= 10;
}
i++;
}
return 0;
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y;
vector<pair<int, int> > v, ve;
set<int> setx, sety;
for (int i = 0; i < 3; i++) {
cin >> x >> y;
v.push_back(make_pair(x, y));
setx.insert(x);
sety.insert(y);
}
if (setx.size() == 1 || sety.size() == 1) {
cout << "1" << endl;
} else if (setx.size() == 2) {
if (sety.size() == 2) {
cout << "2" << endl;
} else {
for (int j = 0; j < 3; j++) {
ve.push_back(make_pair(v[j].second, v[j].first));
}
sort(ve.begin(), ve.end());
if (ve[0].second == ve[1].second || ve[1].second == ve[2].second) {
cout << "2" << endl;
} else {
cout << "3" << endl;
}
}
} else if (sety.size() == 2) {
if (setx.size() == 2) {
cout << "2" << endl;
} else {
for (int j = 0; j < 3; j++) {
ve.push_back(make_pair(v[j].first, v[j].second));
}
sort(ve.begin(), ve.end());
if (ve[0].second == ve[1].second || ve[1].second == ve[2].second) {
cout << "2" << endl;
} else {
cout << "3" << endl;
}
}
} else {
cout << "3" << endl;
}
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int s = 1;
char c;
cin >> c;
while (cin >> c) s += (c - 49 ? c - 48 : 10);
cout << s;
}
| 1,900 | CPP |
n, m = map(int, input().split())
ans = n
while n // m > 0:
ans += n // m
n = n % m + n // m
print(ans) | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
long long int i;
long long int a[n];
for (long long int i = 0; i < n; i++) cin >> a[i];
vector<char> v;
long long int ma = 0;
long long int j = n - 1;
i = 0;
if (a[i] > a[j]) {
if (a[j] > ma) {
ma = a[j];
j--;
v.push_back('R');
}
} else {
ma = a[i];
i++;
v.push_back('L');
}
long long int f = 0;
while (i <= j) {
f = 0;
if (a[i] < a[j]) {
if (a[i] > ma) {
ma = a[i];
i++;
f++;
v.push_back('L');
} else if (a[j] > ma) {
ma = a[j];
j--;
f++;
v.push_back('R');
}
} else {
if (a[j] > ma) {
ma = a[j];
j--;
f++;
v.push_back('R');
} else if (a[i] > ma) {
ma = a[i];
i++;
f++;
v.push_back('L');
}
}
if (f == 0) {
break;
}
}
cout << v.size() << endl;
for (auto j : v) {
cout << j;
}
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
int DV(char c) {
int z;
if (c == '0')
z = 0;
else if (c == '1')
z = 1;
else if (c == '2')
z = 2;
else if (c == '3')
z = 3;
else if (c == '4')
z = 4;
else if (c == '5')
z = 5;
else if (c == '6')
z = 6;
else if (c == '7')
z = 7;
else if (c == '8')
z = 8;
else if (c == '9')
z = 9;
return z;
}
int main() {
int sum = 0, cn = 1;
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
sum += DV(s[i]);
}
int cnt = 0, n, k;
if (sum < 10 && s.size() == 1)
cout << cnt << endl;
else if (sum < 10) {
cnt++;
cout << cnt << endl;
} else {
while (sum >= 10) {
n = sum;
sum = 0;
cn++;
while (n > 0) {
k = n % 10;
sum += k;
n /= 10;
}
}
cout << cn << endl;
}
}
| 1,000 | CPP |
d,t=map(int,input().split())
a = [list(map(int, input().split())) for i in range(d)]
b = list(zip(*a))
mn = sum(b[0])
zx = sum(b[1])
if mn<=t<=zx:
print('YES')
x = t - mn
for i, j in a:
y = min(j - i, x)
x -= y
print(i + max(0, y), end=' ')
else:
print('NO')
| 1,200 | PYTHON3 |
n, x = map(int, input().split())
a = 0
b = 1000
for i in range(n):
c, d = map(int, input().split())
a = max(a, min(c, d))
b = min(b, max(c, d))
if a > b:
print(-1)
exit(0)
if a <= x <= b:
print(0)
else:
print(min(abs(a - x), abs(b - x)))
| 1,000 | PYTHON3 |
n = int(input())
step = 0
while n != 0:
if n >= 5:
n -= 5
step += 1
elif n >= 4:
n -= 4
step += 1
elif n >= 3:
n -= 3
step += 1
elif n >= 2:
n -= 2
step += 1
elif n >= 1:
n -= 1
step += 1
print(step)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long seed = 233;
const long long mod = 998244353;
const long long P = 1e9 + 7;
char s[2][2010], w[2010];
int n, m;
int dp[2][2010][2010], ans;
long long Pow[2010];
struct node {
long long h[2010];
void make(int n, char* s) {
for (int i = 1; i <= n; i++) {
h[i] = h[i - 1] * seed + s[i] - 'a';
h[i] %= mod;
}
}
long long cut(int l, int r) {
return (h[r] - h[l - 1] * Pow[r - l + 1] % mod + mod) % mod;
}
} pre[2], suf[2], ss;
void add(int& x, int y) {
x += y;
if (x > P) x -= P;
}
int solve(bool first) {
int ret = 0;
memset(dp, 0, sizeof(dp));
for (int j = 1; j <= n; j++) {
dp[0][j][0] = dp[1][j][0] = 1;
for (int i = 0; i < 2; i++)
for (int k = 2; k <= min(n - j + 1, m / 2); k++)
if (ss.cut(m - 2 * k + 1, m - k) == pre[i].cut(j, j + k - 1) &&
ss.cut(m - k + 1, m) ==
suf[i ^ 1].cut(n - (j + k - 1) + 1, n - j + 1))
if (2 * k != m || first) add(ret, dp[i][j][m - 2 * k]);
for (int i = 0; i < 2; i++)
for (int k = 2; k <= min(j, m / 2); k++)
if (ss.cut(k + 1, 2 * k) == pre[i].cut(j - k + 1, j) &&
ss.cut(1, k) == suf[i ^ 1].cut(n - j + 1, n - (j - k + 1) + 1))
if (2 * k != m || first) add(dp[i][j + 1][2 * k], 1);
for (int i = 0; i < 2; i++)
for (int k = 0; k < m; k++)
if (s[i][j] == w[k + 1]) {
add(dp[i][j + 1][k + 1], dp[i][j][k]);
if (k + 2 <= m && s[i ^ 1][j] == w[k + 2])
add(dp[i ^ 1][j + 1][k + 2], dp[i][j][k]);
}
for (int i = 0; i < 2; i++) add(ret, dp[i][j + 1][m]);
}
return ret;
}
int main() {
Pow[0] = 1;
for (int i = 1; i <= 2000; i++) Pow[i] = Pow[i - 1] * seed % mod;
scanf("%s", s[0] + 1);
scanf("%s", s[1] + 1);
n = strlen(s[0] + 1);
for (int i = 0; i < 2; i++) {
pre[i].make(n, s[i]);
reverse(s[i] + 1, s[i] + n + 1);
suf[i].make(n, s[i]);
reverse(s[i] + 1, s[i] + n + 1);
}
scanf("%s", w + 1);
m = strlen(w + 1);
ss.make(m, w);
add(ans, solve(1));
if (m > 1) {
reverse(w + 1, w + m + 1);
ss.make(m, w);
add(ans, solve(0));
if (m == 2) {
for (int j = 1; j <= n; j++)
for (int i = 0; i < 2; i++)
if (s[i][j] == w[1] && s[i ^ 1][j] == w[2]) add(ans, P - 1);
}
}
printf("%d\n", ans);
return 0;
}
| 3,200 | CPP |
angka=input()
x=len(angka)
total=0
for i in range(x):
if angka[i]=="4" or angka[i]=="7":
total+=1
if total==4 or total==7 or total==47 or total==744:
print("YES")
else:
print("NO")
| 800 | PYTHON3 |
ins = map(int, input().split())
n, m = next(ins), next(ins)
cnt = [0] * 20010
cancel = False
for _ in range(m):
line = list(map(int, input().split()))
done = False
for i in range(1, len(line)):
if line[i] > 0:
cnt[line[i] + 10000] = 1
if cnt[line[i]] == 1:
done = True
break
else:
cnt[abs(line[i])] = 1
if cnt[abs(line[i]) + 10000] == 1:
done = True
break
if not done:
cancel = True
for i in range(1, len(line)):
cnt[abs(line[i])], cnt[abs(line[i]) + 10000] = 0, 0
if cancel:
print("YES")
else:
print("NO") | 1,300 | PYTHON3 |
"""
~~ Author : Bhaskar
~~ Dated : 08~06~2020
"""
import sys
from bisect import *
from math import floor,sqrt,ceil,factorial as F,gcd,pi
from itertools import chain,combinations,permutations,accumulate
from collections import Counter,defaultdict,OrderedDict,deque
from array import array
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)-1
mod = 1000000007
ch = "abcdefghijklmnopqrstuvwxyz"
lcm = lambda a,b : (a*b)//gcd(a,b)
setbit = lambda x : bin(x)[2:].count("1")
input = lambda : sys.stdin.readline()
def check(x):
s,e = None,None
for i in range(len(x)):
if x[i] > 0:
s = i
break
for i in range(len(x)-1,-1,-1):
if x[i] > 0:
e = i
break
if x[s:e+1].count(x[s]) == e+1-s:
print("YES")
else:
print("NO")
def solve():
T = int(input())
for _ in range(T):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
diff = []
for i in range(n):
if b[i] - a[i] >= 0:
diff.append(b[i]-a[i])
else:
diff.append("neg")
if "neg" in diff:
print("NO")
else:
if len(set(diff)) == 1:
print("YES")
else:
check(diff)
if __name__ == "__main__":
try:
sys.stdin = open("input.txt","r")
except:
pass
solve()
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long gcdll(long long a, long long b) {
if (!b) return a;
return gcdll(b, a % b);
}
void seive(int n) {
vector<bool> is_prime(n + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
}
vector<int> constructZFunc(string s) {
int n = s.size();
vector<int> z(n);
int l, r;
l = r = 0;
z[0] = 0;
for (int i = 1; i <= n - 1; i++) {
z[i] = 0;
if (i <= r) {
z[i] = min(r - i + 1, z[i - l]);
}
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) {
r = i + z[i] - 1;
l = i;
}
}
return z;
}
const int N = 1e5 + 5;
vector<vector<int>> g(N);
set<int> s;
void dfs(int v, int p, bool par, int &mx) {
int cnt = 0;
for (auto u : g[v]) {
if (g[u].size() == 1) cnt++;
if (u == p) continue;
dfs(u, v, !par, mx);
}
if (g[v].size() == 1)
s.insert(par);
else if (cnt)
mx -= (cnt - 1);
}
void solve() {
int n, u, v, mx, mn;
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
mx = n - 1;
mn = 1;
dfs(1, 1, 0, mx);
if (s.size() > 1) mn = 3;
cout << mn << " " << mx;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 1,800 | CPP |
#include <bits/stdc++.h>
int MAX_DIST = 200000000;
struct node_s {
int id;
int dist;
bool visited;
};
struct edge_s {
int length;
bool visited;
};
struct edge_lst {
int node;
edge_s* edge;
edge_lst* nxt;
};
bool compare_nodes(node_s* n1, node_s* n2) {
if (n1->dist == n2->dist)
return n1->id < n2->id;
else
return n1->dist < n2->dist;
}
int main(int argc, char* argv[]) {
int n, m, s, L;
std::cin >> n >> m >> s;
s--;
node_s* nodes = new node_s[n];
edge_lst** lst = new edge_lst*[n];
for (int i = 0; i < n; i++) {
nodes[i].id = i;
nodes[i].dist = MAX_DIST;
nodes[i].visited = false;
lst[i] = NULL;
}
for (int i = 0; i < m; i++) {
int v, u, w;
std::cin >> v >> u >> w;
v--;
u--;
edge_s* edge = new edge_s;
edge->length = w;
edge->visited = false;
edge_lst* ed_l = new edge_lst;
ed_l->node = v;
ed_l->edge = edge;
ed_l->nxt = lst[u];
lst[u] = ed_l;
ed_l = new edge_lst;
ed_l->node = u;
ed_l->edge = edge;
ed_l->nxt = lst[v];
lst[v] = ed_l;
}
std::cin >> L;
nodes[s].dist = 0;
std::set<node_s*, bool (*)(node_s*, node_s*)> que(&compare_nodes);
for (int i = 0; i < n; i++) que.insert(&nodes[i]);
int num_vis = 0;
while (!que.empty()) {
node_s* nod = *que.begin();
que.erase(que.begin());
nod->visited = true;
num_vis++;
edge_lst* ed_l = lst[nod->id];
while (ed_l != NULL) {
if (!nodes[ed_l->node].visited) {
int d = nod->dist + ed_l->edge->length;
if (nodes[ed_l->node].dist > d) {
que.erase(&nodes[ed_l->node]);
nodes[ed_l->node].dist = d;
que.insert(&nodes[ed_l->node]);
}
}
ed_l = ed_l->nxt;
}
}
int num_shafts = 0;
for (int i = 0; i < n; i++)
if (nodes[i].dist == L) num_shafts++;
for (int i = 0; i < n; i++) {
edge_lst* ed_l = lst[i];
while (ed_l != NULL) {
if (!ed_l->edge->visited) {
int ed_len = ed_l->edge->length;
ed_l->edge->visited = true;
int from = L - nodes[i].dist;
int to = ed_len - (L - nodes[ed_l->node].dist);
if (from > 0 && to < ed_len) {
if (from < to) num_shafts += 2;
if (from == to) num_shafts++;
}
if ((from > 0 && from < ed_len && to >= ed_len) ||
(from <= 0 && to > 0 && to < ed_len))
num_shafts++;
}
ed_l = ed_l->nxt;
}
}
std::cout << num_shafts;
delete[] nodes;
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int memo[30010][1600];
int n, d;
int p[30010];
int base;
int dfs(int cur, int pre) {
if (cur > 30000) return 0;
if (memo[cur][pre + base] != -1) return memo[cur][pre + base];
int ret = p[cur];
int plus = 0;
for (int i = -1; i <= 1; i++) {
int npre = pre + i;
if (npre <= 0) continue;
if (cur + npre > 30000) continue;
plus = max(plus, dfs(cur + npre, npre));
}
ret += plus;
return memo[cur][pre + base] = ret;
}
int main(void) {
cin >> n >> d;
base = 800 - d;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
p[x]++;
}
for (int i = 0; i < 30010; i++)
for (int j = 0; j < 1600; j++) {
memo[i][j] = -1;
}
cout << dfs(d, d) << endl;
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000 * 1000 * 1000 + 7;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int sum[4] = {3, -3, 1, -1};
int n, m, i, j;
string s[1005];
bool b[1005][1005];
vector<int> a;
int p[300][300];
int v[300], g[300];
int dp[300][300][2][2];
int rec(int i, int j, int t, int e) {
if (j == m) {
return 1;
}
if (i == n) {
if (t == 0) {
int ans = rec(0, j + 1, 0, 0);
if (j + 2 < m) ans += rec(0, j + 1, 1, 0);
ans %= mod;
dp[i][j][t][e] = ans;
return ans;
} else {
if (e == 0) return 0;
int ans = rec(0, j + 2, 0, 0);
if (j + 3 < m) ans += rec(0, j + 2, 1, 0);
ans %= mod;
dp[i][j][t][e] = ans;
return ans;
}
}
if (dp[i][j][t][e] != -1) return dp[i][j][t][e];
int ans = 0;
if (t == 0) {
if ((i + 1 < n) && (v[p[i][j]] + v[p[i + 1][j]] == 2))
ans = rec(i + 2, j, t, 0);
ans %= mod;
} else if ((t == 1) && (j + 1 < m)) {
if (g[p[i][j]] + g[p[i][j + 1]] == 2) ans += rec(i + 1, j, t, 1);
ans %= mod;
if (i + 1 < n) {
if (v[p[i][j]] + v[p[i + 1][j]] + v[p[i][j + 1]] + v[p[i + 1][j + 1]] ==
4)
ans += rec(i + 2, j, t, e);
}
ans %= mod;
}
dp[i][j][t][e] = ans;
return ans;
}
int dfs(int i, int j, int k) {
int ans = 0;
if (s[i][j] == 'O') ans += k * k;
b[i][j] = 1;
int t;
for (t = 0; t < 4; t++) {
int x = i + dx[t];
int y = j + dy[t];
if ((x >= 0) && (x < n) && (y >= 0) && (y < m) && (s[x][y] != '#') &&
(b[x][y] == 0))
ans += dfs(x, y, k + sum[t]);
}
return ans;
}
int main() {
cin >> n >> m;
n = 4 * n + 1;
m = 4 * m + 1;
for (i = 0; i < n; i++) cin >> s[i];
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if ((s[i][j] != '#') && (b[i][j] == 0)) {
a.push_back(dfs(i, j, 1));
}
n = (n - 1) / 4;
m = (m - 1) / 4;
for (i = 0; i < a.size(); i++) p[i / m][i % m] = a[i];
v[0] = 1;
v[25] = 1;
v[58] = 1;
v[83] = 1;
v[140] = 1;
v[165] = 1;
v[192] = 1;
g[0] = 1;
g[25] = 1;
g[82] = 1;
g[107] = 1;
g[140] = 1;
g[165] = 1;
g[208] = 1;
memset(dp, -1, sizeof(dp));
int ans = rec(0, 0, 0, 0);
if (1 < m) ans += rec(0, 0, 1, 0);
ans %= mod;
cout << ans;
return 0;
}
| 2,300 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
void __never(int a) { printf("\nOPS %d", a); }
int dx[] = {-1, 0, 1, 0, -1, 1};
int dy[] = {0, -1, 0, 1, -1, 1};
bool bad[5][5];
int nums[5][5];
int Code(vector<vector<int> > m) {
int mask = 0, cc = 0;
for (int(i) = (0); (i) <= (4); ++(i))
for (int(j) = (0); (j) <= (4); ++(j))
if (!bad[i][j]) {
if (m[i][j]) mask |= (1 << cc);
cc++;
}
{
if (!(cc == 19)) {
__never(41);
cout.flush();
cerr.flush();
abort();
}
};
return mask;
}
vector<vector<int> > Decode(int mask) {
int cc = 0;
vector<vector<int> > m(5, vector<int>(5, 0));
for (int(i) = (0); (i) <= (4); ++(i))
for (int(j) = (0); (j) <= (4); ++(j))
if (!bad[i][j]) {
if (mask & (1 << cc))
m[i][j] = 1;
else
m[i][j] = 0;
cc++;
}
{
if (!(cc == 19)) {
__never(54);
cout.flush();
cerr.flush();
abort();
}
};
return m;
}
bool was[1 << 20];
bool win[1 << 20];
bool go(int mask) {
if (was[mask]) return win[mask];
win[mask] = false;
was[mask] = true;
vector<vector<int> > mm = Decode(mask);
for (int(i) = (0); (i) <= (4); ++(i))
for (int(j) = (0); (j) <= (4); ++(j))
if (!bad[i][j] && mm[i][j]) {
for (int(k) = (0); (k) <= (5); ++(k)) {
int next = mask;
int cx = i, cy = j;
while (0 <= cx && cx < 5 && 0 <= cy && cy < 5 && !bad[cx][cy] &&
mm[cx][cy]) {
next -= (1 << nums[cx][cy]);
if (!go(next)) return win[mask] = true;
cx += dx[k];
cy += dy[k];
}
}
}
return win[mask] = false;
}
void init() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
void sol() {
bad[0][3] = bad[0][4] = bad[1][4] = true;
bad[3][0] = bad[4][0] = bad[4][1] = true;
int cc = 0;
for (int(i) = (0); (i) <= (4); ++(i))
for (int(j) = (0); (j) <= (4); ++(j))
if (!bad[i][j]) nums[i][j] = cc++;
int start = 0;
string s;
for (int(i) = (0); (i) <= (18); ++(i)) {
cin >> s;
if (s == "O")
start |= (1 << i);
else {
if (!(s == ".")) {
__never(107);
cout.flush();
cerr.flush();
abort();
}
};
}
if (go(start))
printf("Karlsson");
else
printf("Lillebror");
}
int main() {
sol();
return 0;
}
| 2,000 | CPP |
s = input()
c = 'aeiou'
ans = "@"
cn = 0
sm = 1
for i in s:
if(i in c):
cn = 0
sm = 1
elif(i == ans[-1]):
sm += 1
cn += 1
elif(i != ans[-1]):
cn += 1
if(cn == sm or cn <3):
ans += i
elif(cn>2):
ans += " "
ans += i
cn = 1
sm = 1
print(ans[1:]) | 1,500 | PYTHON3 |
import math
mac=10**9+7
def comb(a,b):
return math.factorial(a)//(math.factorial(b)*math.factorial(a-b))
n,m=map(int,input().split())
print(comb(2*m+n-1,2*m)%mac)
| 1,600 | PYTHON3 |
class Solution:
# @param A : tuple of strings
# @return a list of list of integers
def __init__(self):
self.travelled_city = dict()
def twobrackets(self , st):
p_stack = []
q_stack = []
count = 0
for e in st:
if e == '[':
p_stack.append(1)
elif len(p_stack)!=0 and e == ']':
p_stack.pop()
count += 1
if e == '(':
q_stack.append(1)
elif len(q_stack)!=0 and e == ')':
q_stack.pop()
count += 1
return count
if __name__ == "__main__":
cases = int(input())
while cases:
sol = Solution()
st = input()
steps = sol.twobrackets(st)
print(steps)
cases -= 1 | 800 | PYTHON3 |
input_l = input()
input_var = input_l.split()
input_ = int(input_var[0])
iter = int(input_var[1])
while(iter > 0):
last = input_ % 10
if(last == 0):
input_ = input_ // 10
else:
input_ = input_ - 1
iter -= 1
print(input_) | 800 | PYTHON3 |
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<vector<int>> matrix;
vector<int> arr;
pair<int, int> comb[6]={{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
pair<int, int> comb2[6]={{0, 1}, {0, 2}, {1, 0}, {1, 2}, {2, 0}, {2, 1}};
int n, m;
int chk(bool flag){
int ret=0, mx=0;
for (int i=0;i<n;i++){
int cnt=0;
for (int j=0;j<m;j++) if (arr[j]!=matrix[i][j]) cnt++;
if (flag){
if (cnt>=4) return -1;
else if (cnt==3){
bool flag2=1;
for (int j=0;j<m;j++) if (arr[j]==-1){
flag2=0; arr[j]=matrix[i][j];
}
if (flag2) return -1;
}
continue;
}
if (cnt>=5) return -1;
else if (cnt>2 && cnt>mx){
ret=i;
mx=cnt;
}
}
return ret;
}
vector<int> comp(int idx1, int idx2){
vector<int> ret;
for (int i=0;i<m;i++) if (matrix[idx1][i]!=matrix[idx2][i]) ret.push_back(i);
return ret;
}
void solve(int idx, vector<int> &v){
for (int z=0;z<6;z++){
int c1=v[comb2[z].first], c2=v[comb2[z].second];
for (int pos:v){
if(c1==pos) arr[pos]=matrix[0][c1];
else if (c2==pos) arr[pos]=matrix[idx][c2];
else arr[pos]=-1;
}
if (chk(1)==0){
printf("Yes\n");
for (int x:arr){
if (x!=-1) printf("%d ", x);
else printf("1 ");
}
return;
}
}
printf("No\n");
}
int main(){
scanf("%d %d", &n, &m);
for (int i=0;i<n;i++){
vector<int> v;
matrix.push_back(v);
for (int j=0;j<m;j++){
int tmp;
scanf("%d", &tmp);
matrix[i].push_back(tmp);
}
}
arr=matrix[0];
int idx=chk(0);
if (idx==-1){
printf("No\n");
return 0;
}
else if (idx==0){
printf("Yes\n");
for (int x:arr) printf("%d ", x);
return 0;
}
vector<int> v=comp(0, idx);
if ((int)v.size()==3){
solve(idx, v);
return 0;
}
for (int z=0;z<6;z++){
int c1=v[comb[z].first], c2=-1;
if ((int)v.size()>comb[z].second) c2=v[comb[z].second];
for (int pos:v){
if (c1==pos || c2==pos) arr[pos]=matrix[idx][pos];
else arr[pos]=matrix[0][pos];
}
if (chk(0)==0){
printf("Yes\n");
for (int x:arr) printf("%d ", x);
return 0;
}
}
printf("No\n");
return 0;
}
| 2,500 | CPP |
n,b=map(int,input().split())
lis=[-1]
for i in range((n+1)//2,n+1):
if i%b==0:
lis.append(i)
break
print(max(lis)) | 1,000 | PYTHON3 |
l,m=map(int,input().split())
dic1=input().split()
dic2=input().split()
a=[]
for i in range(int(input())):
inp=int(input())
j=inp%l;r=inp%m
if j==0:
j=len(dic1)
if r==0:
r=len(dic2)
a.append(dic1[j-1]+dic2[r-1])
for i in a:
print(i)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
int n;
struct poly {
unsigned long long p[10];
poly() { memset(p, 0, sizeof(p)); }
poly operator*(poly b) {
poly ret;
for (int o = 0; o < 10; o++) ret.p[o] = 0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
if (i + j >= 10)
ret.p[i + j - 10] += p[i] * b.p[j];
else
ret.p[i + j] += p[i] * b.p[j];
return ret;
}
poly operator-(poly b) {
poly ret;
for (int o = 0; o < 10; o++) ret.p[o] = 0;
for (int i = 0; i < 10; i++) ret.p[i] -= p[i] + b.p[i];
return ret;
}
poly operator+(poly b) {
poly ret;
for (int o = 0; o < 10; o++) ret.p[o] = 0;
for (int i = 0; i < 10; i++) ret.p[i] += p[i] + b.p[i];
return ret;
}
poly evx(int x, int pp) {
if (pp == 0) {
poly ret;
for (int o = 0; o < 10; o++) ret.p[o] = 0;
for (int i = 0; i < 10; i++) {
if (i + x >= 10)
ret.p[i + x - 10] = p[i];
else
ret.p[i + x] = p[i];
}
return ret;
} else {
poly ret;
for (int o = 0; o < 10; o++) ret.p[o] = 0;
for (int i = 0; i < 10; i++) {
if (i - x < 0)
ret.p[i - x + 10] = p[i];
else
ret.p[i - x] = p[i];
}
return ret;
}
}
void ispis() {
for (int i = 0; i < 10; i++) printf("%lld ", p[i]);
printf(" ISPIS \n");
}
} niz[10], pol[100010];
int br;
poly step(poly a, int pw) {
poly ret;
for (int o = 0; o < 10; o++) ret.p[o] = 0;
ret.p[0] = 1;
while (pw) {
if (pw & 1) ret = a * ret;
pw >>= 1;
a = a * a;
}
return ret;
}
void fft(poly a[], bool invert) {
int n = maxn;
for (int len = n; len >= 10; len /= 10) {
int hlen = len / 10;
for (int i = 0; i < n; i += len) {
for (int j = 0; j < hlen; j++) {
br = 0;
for (int k = 0; k < len; k += hlen) niz[br++] = a[i + j + k];
for (int k = 0, b = 0; k < len; k += hlen, b++) {
for (int o = 0; o < 10; o++) a[i + j + k].p[o] = 0;
int c = 0;
for (int o = 0; o < 10; o++) {
a[i + j + k] = a[i + j + k] + niz[o].evx(c, invert);
c += b;
if (c >= 10) c -= 10;
}
}
}
}
}
}
void mul(poly a[], int pw) {
int n = maxn;
fft(a, 0);
for (int i = 0; i < n; i++) {
a[i] = step(a[i], pw);
}
fft(a, 1);
for (int i = 0; i < n; i++)
for (int j = 9; j >= 4; j--) {
unsigned long long pom = -a[i].p[j];
a[i].p[j] += pom;
a[i].p[j - 1] -= pom;
a[i].p[j - 2] += pom;
a[i].p[j - 3] -= pom;
a[i].p[j - 4] += pom;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 5; k++)
a[i].p[j] *= (unsigned long long)14757395258967641293;
for (int k = 0; k < 5; k++) a[i].p[j] /= 2;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
pol[a].p[0]++;
}
mul(pol, n);
unsigned long long mod = (((unsigned long long)1) << 58);
for (int i = 0; i < n; i++) printf("%I64d ", pol[i].p[0] % mod);
return 0;
}
| 3,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
typedef struct item* pitem;
vector<int> x[100001];
int used[100001][2], p[100001][2], v = -1;
bool loop = false;
void DFS(int u, int m) {
used[u][m] = 1;
if (x[u].size() == 0 && m == 1) v = u;
for (int i = 0; i < x[u].size(); i++) {
if (!used[x[u][i]][m ^ 1]) {
p[x[u][i]][m ^ 1] = u;
DFS(x[u][i], m ^ 1);
} else if (used[x[u][i]][m ^ 1] == 1)
loop = true;
}
used[u][m] = 2;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, s;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int c;
cin >> c;
for (int j = 0; j < c; j++) {
int tmp;
cin >> tmp;
x[i].push_back(tmp);
}
}
cin >> s;
p[s][0] = -1;
p[s][1] = -1;
DFS(s, 0);
if (v == -1) {
if (loop)
cout << "Draw";
else
cout << "Lose";
return 0;
}
cout << "Win\n";
vector<int> ans;
for (int pos = v, mode = 1; pos != -1; pos = p[pos][mode], mode ^= 1)
ans.push_back(pos);
reverse((ans).begin(), (ans).end());
for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
}
| 2,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
const int INF = 1e9 + 7;
const long long LINF = 1e18 + 7;
vector<int> adj[MAX];
int vis[MAX] = {};
int cnt[MAX] = {};
int dfs(int u) {
vis[u] = 1;
if (adj[u].size() == 1 && u != 1) cnt[u] += 1;
for (int i = 0; i < adj[u].size(); i++) {
int c = adj[u][i];
if (vis[c] == 0) {
cnt[u] += dfs(c);
}
}
return cnt[u];
}
int main() {
ios::sync_with_stdio(0);
long long n;
cin >> n;
if (n == 1) {
cout << 1 << endl;
} else {
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
adj[i].push_back(x);
adj[x].push_back(i);
}
dfs(1);
sort(cnt, cnt + n + 1);
for (int i = 1; i <= n; i++) {
cout << cnt[i] << " ";
}
}
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
char ans[110000], str[110000];
int P[2][110000];
int main() {
int Len, i, j, t, K, now, then;
while (scanf("%s", str + 1) != EOF) {
str[0] = '$';
Len = strlen(str);
str[Len] = '$';
P[0][0] = P[1][0] = 0;
for (i = 0; i < Len - 1; i++) P[0][++P[0][0]] = i;
scanf("%d", &K);
for (i = 0;; i++) {
now = i % 2, then = 1 - now;
for (j = 'a'; j <= 'z'; j++) {
long long less = 0, same = 0;
for (t = 1; t <= P[now][0]; t++) {
int pos = P[now][t];
if (str[pos + 1] < j)
less += Len - (pos + 1);
else if (str[pos + 1] == j)
same += Len - (pos + 1);
}
if (K > less && K <= less + same) {
ans[i] = j;
K -= less;
P[then][0] = 0;
for (t = 1; t <= P[now][0]; t++) {
int pos = P[now][t];
if (str[pos + 1] == j) P[then][++P[then][0]] = pos + 1;
}
K -= P[then][0];
break;
}
}
if (j > 'z') break;
if (K <= 0) break;
}
ans[i + 1] = 0;
if (K > 0)
printf("No such line.\n");
else
printf("%s\n", ans);
Len--;
if (K > 0 && K <= Len * (Len + 1) / 2)
while (1)
;
}
}
| 2,100 | CPP |
t = int(input())
for _ in range(t):
n = int(input())
D = {}
DC = {}
DS = {}
A = list(map(int, input().split()))
for i in range(n):
if A[i] in D:
D[A[i]].append(i)
DS[A[i]][i] = 1
DC[A[i]] += 1
else:
D[A[i]] = [i]
DS[A[i]] = [0 for __ in range(n)]
DS[A[i]][i] = 1
DC[A[i]] = 1
for i in DS:
for k in range(n-1):
DS[i][k+1] += DS[i][k]
M = max(DC[i] for i in DC)
for i in D:
m = DC[i]
for j in D:
if i != j:
for k in range(1, m//2+1):
l, r = D[i][k-1], D[i][-k]
if l != 0:
cnt = DS[j][r-1] - DS[j][l-1]
else:
cnt = DS[j][r-1]
if M < cnt + k * 2:
M = cnt + k * 2
print(M) | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
double get(double x1, double x2, double y1, double y2) {
double p = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
return sqrt(p);
}
int main() {
int n;
double k;
cin >> n >> k;
double a, b;
pair<double, double> p[n];
for (int i = 0; i < n; i++) {
cin >> a >> b;
p[i].first = a;
p[i].second = b;
}
double sum = 0;
for (int i = 0; i < n - 1; i++) {
sum += get(p[i].first, p[i + 1].first, p[i].second, p[i + 1].second);
}
cout << setprecision(15) << fixed << (sum * k) / 50;
}
| 900 | CPP |
import math
def get_primes(prime_supr):
is_prime = [0]*2 + [1]*prime_supr
for i in range(2,int(math.sqrt(prime_supr)) + 1):
if is_prime[i]:
for j in range(i * i, prime_supr + 1, i):
is_prime[j] = 0
return is_prime
get_int = lambda: map(int, input().split())
r, c = get_int()
ans = 0
tmp = 0
for i in range(c):
if '0' not in input():
ans = max(ans, tmp)
tmp = 0
else:
tmp += 1
print(max(ans,tmp))
| 800 | PYTHON3 |
n = int(input())
a = list(map(int,input().split()))
s = {} #dic
d = {} #dic
p = set() #set
mx = - 10**15
mi = -mx
ans = 2
for i in range(n) :
if a[i] in s :
s[a[i]] += 1
else :
s[a[i]] = 1
mx = max(mx,a[i])
mi = min(mi,a[i])
if 0 in s : ans = max(ans,s[0])
for i in range(n) :
for j in range(n) :
if (j==i) or (a[i]==0 and a[j]==0) or ((a[i], a[j]) in p) : continue
f0 = a[i]; f1 = a[j]; ansi = 2; f2 = 0;
d = {}
p.add((a[i], a[j]))
while True :
f2 = f1 + f0
f0 = f1
f1 = f2
if (f2 > mx) or (f2 < mi) : break
if f2 in s :
d[f2] = s[f2]
else : break
f0 = a[i]; f1 = a[j];
if f1==f0 :
d[f0] = s[f0] - 2
else :
d[f0] = s[f0] - 1
d[f1] = s[f1] - 1
while True :
f2 = f1 + f0
f0 = f1
f1 = f2
if (f2 > mx) or (f2 < mi) : break
if (f2 in d) and (d[f2] > 0) :
ansi += 1
d[f2] -= 1
else : break
ans = max(ans, ansi)
print(ans)
| 2,000 | PYTHON3 |
n = int(input())
if(n&1 or n==2):
print('NO')
else:
print('YES') | 800 | PYTHON3 |
import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
from collections import *
ORDA = 97 # a
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return [int(i) for i in input().split()]
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def revn(n): return str(n)[::-1]
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def sieve(n):
if n < 2: return list()
prime = [True for _ in range(n + 1)]
p = 3
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 2
r = [2]
for p in range(3, n + 1, 2):
if prime[p]:
r.append(p)
return r
def divs(n, start=2):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def divn(n, primes):
divs_number = 1
for i in primes:
if n == 1:
return divs_number
t = 1
while n % i == 0:
t += 1
n //= i
divs_number *= t
def prime(n):
if n == 2: return True
if n % 2 == 0 or n <= 1: return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0: return False
return True
def convn(number, base):
new_number = 0
while number > 0:
new_number += number % base
number //= base
return new_number
def cdiv(n, k): return n // k + (n % k != 0)
def ispal(s): # Palindrome
for i in range(len(s) // 2 + 1):
if s[i] != s[-i - 1]:
return False
return True
# a = [1,2,3,4,5] -----> print(*a) ----> list print krne ka new way
def function(arr, size,dp):
if size == 1 or size == 0:
return 1
key = ''.join(map(str, arr[:size]))
if dp[key]:
return dp[key]
output = function(arr, size-1, dp)
key = ''.join(map(str, arr[:size-1]))
dp[key] = output
if (arr[size-2]*10 + arr[size-1]) <= 26:
output += function(arr, size-2, dp)
key = ''.join(map(str, arr[:size-2]))
dp[key] = output
return output
def main():
for _ in range(ii()):
n = ii()
arr = li()
arr = sorted(arr)
if n%2 == 0:
i = n//2-1
j = n//2
while i >= 0:
print(arr[i], arr[j], end = ' ')
i -= 1
j += 1
print()
else:
i =n//2
j = i+1
while i >= 0 and j < n:
print(arr[i], arr[j], end = ' ')
i -= 1
j += 1
print(arr[i])
main() | 1,200 | PYTHON3 |
a=input().split()
nos=int(a[0])
tot=int(a[1])
l=[]
b=input().split()
for i in range(nos):
l.append(int(b[i]))
p=sum(l)
m=p+(nos-1)*10
if m>tot:
print(-1)
else:
n=(tot-p)//5
print(n) | 900 | PYTHON3 |
#include <bits/stdc++.h>
class DSU {
public:
void reset(int n) {
v.clear();
v.resize(n);
for (int i = 0; i < n; ++i) v[i] = i;
}
void merge(int a, int b) {
a = get(a), b = get(b);
v[a] = b;
}
int get(int a) { return v[a] == a ? a : v[a] = get(v[a]); }
private:
std::vector<int> v;
};
struct pii {
int a, b;
};
const int MOD = 1E9 + 7;
struct Problem {
DSU ds;
int n, m;
std::vector<std::set<int> > e;
std::vector<int> b;
bool dfs(int i, int prev, int d) {
if (b[i]) return b[i] % 2 == d % 2;
b[i] = d;
for (int next : e[i]) {
if (next == prev) continue;
if (!dfs(next, i, d + 1)) return false;
}
return true;
}
void GetInput() {
scanf("%d %d", &n, &m);
e.resize(n);
ds.reset(n);
b.resize(n, 0);
std::vector<pii> t;
for (int i = 0; i < m; ++i) {
int x, y, l;
scanf("%d %d %d", &x, &y, &l);
x--, y--;
if (l)
ds.merge(x, y);
else
t.push_back({x, y});
}
for (pii p : t) {
int x = ds.get(p.a), y = ds.get(p.b);
e[x].insert(y);
e[y].insert(x);
}
}
void Solve() {
GetInput();
int ans = 1;
bool div = true;
for (int j = 0; j < n; ++j) {
int i = ds.get(j);
if (b[i]) continue;
if (dfs(i, -1, 1))
ans = ans * 2 % MOD;
else {
printf("0");
return;
}
if (div) div = false, ans /= 2;
}
printf("%d", ans);
}
};
int main() {
Problem().Solve();
return 0;
}
| 2,200 | CPP |
n = int(input())
s = '8'*(n//2)+'4'*(n%2)
if len(s)>18:
print(-1)
else:
print(s) | 1,200 | PYTHON3 |
import sys
input = sys.stdin.readline
from math import gcd
t=int(input())
for tests in range(t):
n=int(input())
A=list(map(int,input().split()))
NOW=0
ANS=[]
while A:
MAX=-1
NEXT=-1
NEXTIND=-1
for i in range(len(A)):
a=A[i]
if gcd(NOW,a)>MAX:
MAX=gcd(NOW,a)
NEXT=a
NEXTIND=i
if MAX!=1:
ANS.append(NEXT)
A.pop(NEXTIND)
NOW=gcd(NOW,NEXT)
#print(ANS,A,MAX)
else:
ANS+=A
break
print(*ANS)
| 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int d[maxn << 1], p[maxn];
int main() {
int T, n, k;
scanf("%d", &T);
while (T--) {
memset(d, 0, sizeof(d));
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
for (int i = 1; i <= n / 2; i++) {
int sum = p[i] + p[n - i + 1];
int mi = min(p[i], p[n - i + 1]), mx = max(p[i], p[n - i + 1]);
d[2] += 2;
d[mi + 1]--;
d[mx + k + 1]++;
d[sum]--;
d[sum + 1]++;
}
int ans = inf, num = 0;
for (int i = 2; i <= (k << 1); i++) {
ans = min(ans, num += d[i]);
}
printf("%d\n", ans);
}
return 0;
}
| 800 | CPP |
n=int(input())
l=list(map(int,input().split()))
if len(l)==1:
print(1)
else:
l.sort()
l=l[::-1]
for i in range(1,n):
a=sum(l[:i])
b=sum(l[i:])
if a>b:
i=i-1
break
print(i+1)
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n;
cin >> n;
vector<long long int> arr(n, 0);
for (long long int i = 0; i < n; i++) {
cin >> arr[i];
}
map<long long int, vector<long long int>> ans;
for (long long int i = 0; i < n; i++) {
long long int x = arr[i], c = 0;
while ((x % 2) == 0) {
c++;
x >>= 1;
}
ans[c].push_back(i);
}
long long int maxx = 0;
long long int ind = -1;
for (auto i : ans) {
if (i.second.size() > maxx) {
maxx = i.second.size();
ind = i.first;
}
}
cout << n - maxx << endl;
for (auto i : ans) {
if (ind == i.first) continue;
for (auto j : i.second) cout << arr[j] << " ";
}
cout << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t = 1;
while (t--) {
solve();
}
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, ans;
string inp[100005];
struct Node {
int cnt, scnt, cap;
vector<pair<char, int> > son;
map<char, int> mmp;
multiset<int, greater<int> > mts;
} a[100005];
int tot = 1;
bool mk[100005];
void Insert(string s, int len) {
int cur = 1;
for (int i = 1; i <= len; i++) {
a[cur].scnt++;
if (a[cur].mmp[s[i]] == 0) {
a[cur].mmp[s[i]] = ++tot;
a[cur].son.push_back(make_pair(s[i], tot));
}
cur = a[cur].mmp[s[i]];
}
a[cur].cnt++;
a[cur].scnt++;
a[cur].mts.insert(len);
}
void dfs(int p, int d) {
for (int i = 0; i < a[p].son.size(); i++) {
int& to = a[p].son[i].second;
dfs(to, d + 1);
multiset<int, greater<int> > mts = a[to].mts;
while (!mts.empty()) {
a[p].mts.insert(*mts.begin());
mts.erase(mts.begin());
}
}
if (p != 1 && a[p].cnt == 0) {
ans -= *a[p].mts.begin() - d;
a[p].mts.erase(a[p].mts.begin());
a[p].mts.insert(d);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> inp[i];
Insert(" " + inp[i], inp[i].length());
ans += inp[i].length();
}
dfs(1, 0);
cout << ans;
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n;
int c[12] = {4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292};
int main() {
cin >> n;
if (n >= 12)
cout << (n - 11) * 49 + 292;
else
cout << c[n - 1];
return 0;
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
char next_char(char c) {
const int mod = 'z' - 'a' + 1;
return 'a' + (c - 'a' + 1) % mod;
}
int main() {
int n, t;
scanf("%d %d", &n, &t);
vector<char> s1(n + 1, ' '), s2(n + 1, ' '), s3(n + 1, ' ');
scanf("%s", s1.data());
scanf("%s", s2.data());
int same = 0;
for (int i = 0; i < n; ++i) {
if (s1[i] == s2[i]) {
++same;
}
}
int diff = n - same;
if (t < (diff + 1) / 2) {
printf("-1\n");
return 0;
}
int t1, t2;
t1 = t2 = n - t;
int x = min(n - t, same);
for (int i = 0; i < n; ++i) {
if (s1[i] == s2[i] && t1 > 0 && t2 > 0) {
s3[i] = s1[i];
--t1;
--t2;
--x;
} else {
if (s1[i] != s2[i]) {
if (t1 > x) {
s3[i] = s1[i];
--t1;
continue;
}
if (t2 > x) {
s3[i] = s2[i];
--t2;
continue;
}
}
const int mod = 'z' - 'a' + 1;
char next = next_char(s1[i]);
if (next == s2[i]) next = next_char(s2[i]);
s3[i] = next;
}
}
s3[n] = '\0';
printf("%s\n", s3.data());
}
| 1,700 | CPP |
n,m=map(int,input().split())
if m==0:
print("1")
else:
print(min(m,n-m)) | 900 | PYTHON3 |
n = int(input())
s = input()
t1 = s.count('L')
t2 = s.count('R')
print(t1+t2+1)
| 800 | PYTHON3 |
t=int(input())
a=[]
b=[]
for i in range(0,t):
n,m=map(int,input().split())
a.append(n)
b.append(m)
c=list(a)
c.sort()
c=c[::-1]
p=0
for i in range(0,len(a)):
if a[i]!=b[i]:
p=1
break
if p==1:
print("rated")
else:
if c==a:
print("maybe")
else:
print("unrated")
| 900 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const int N = 1e6 + 10;
int nxt[N];
pair<int, int> seg[N * 4];
int mp[N];
void upd(int nod, int l, int r, int id, int val) {
if (r - l == 1) {
seg[nod] = {mp[l], val};
return;
}
int mid = (r + l) / 2;
if (mid > id)
upd(nod * 2, l, mid, id, val);
else
upd(nod * 2 + 1, mid, r, id, val);
seg[nod].second = max(seg[nod * 2].second, seg[nod * 2 + 1].second);
if (seg[nod].second == seg[nod * 2].second)
seg[nod] = seg[nod * 2];
else
seg[nod] = seg[nod * 2 + 1];
return;
}
pair<int, int> get(int nod, int l, int r, int L, int R) {
if (l >= R || L >= r) return {0, 0};
if (l >= L && r <= R) return seg[nod];
int mid = (r + l) / 2;
pair<int, int> a = get(nod * 2, l, mid, L, R);
pair<int, int> b = get(nod * 2 + 1, mid, r, L, R);
if (a.second > b.second) return a;
return b;
}
vector<int> g[N];
int st[N], fn[N], hi[N], ts = 1;
void dfs(int v, int p, int h) {
st[v] = ts;
hi[v] = h;
for (auto u : g[v]) {
if (u == p) continue;
ts++;
dfs(u, v, h + 1);
}
fn[v] = ts;
}
int a[N];
set<pair<int, int> > s;
int par[N];
int ans[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
a[n + 1] = n + 1;
vector<int> agh;
agh.push_back(n + 1);
for (int i = n; i; i--) {
while (a[agh.back()] <= a[i]) agh.pop_back();
nxt[i] = agh.back();
g[nxt[i]].push_back(i);
agh.push_back(i);
}
dfs(n + 1, n + 1, 1);
for (int i = 1; i <= n + 1; i++) mp[st[i]] = i;
for (int i = 1; i <= k; i++) {
upd(1, 1, N, st[i], hi[i]);
pair<int, int> t = get(1, 1, N, st[i], fn[i] + 1);
for (auto u : g[i]) {
s.erase({ans[u], u});
ans[u] = -1;
}
par[t.first] = i;
ans[i] = t.second - hi[i];
s.insert({ans[i], i});
}
for (int i = k + 1; i <= n + 1; i++) {
cout << s.rbegin()->first + 1 << " ";
int p = par[i - k];
s.erase({ans[p], p});
upd(1, 1, N, st[i - k], 0);
if (p > i - k && ans[p] != -1) {
pair<int, int> z = get(1, 1, N, st[p], fn[p] + 1);
par[z.first] = p;
ans[p] = z.second - hi[p];
s.insert({ans[p], p});
}
upd(1, 1, N, st[i], hi[i]);
pair<int, int> z = get(1, 1, N, st[i], fn[i] + 1);
for (auto u : g[i]) {
s.erase({ans[u], u});
ans[u] = -1;
}
par[z.first] = i;
ans[i] = z.second - hi[i];
s.insert({ans[i], i});
}
}
| 2,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
string inttostr(long x) {
string tmp = "";
while (x > 0) {
char ch = (x % 10) + 48;
tmp = ch + tmp;
x /= 10;
}
return tmp;
}
int main() {
long n;
string m;
cin >> n >> m;
long a[11];
memset(a, 0, sizeof(a));
long s = 0;
while (n > 0) {
s++;
a[s] = n % 10;
n /= 10;
}
for (int i = 1; i <= s; i++)
for (int j = i + 1; j <= s; j++) {
if (a[i] > a[j]) {
long tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
long q = 1;
while ((q <= s) && (a[q] == 0)) q++;
if (q > s) {
if (m == "0") {
cout << "OK" << endl;
return 0;
}
if (m != "0") {
cout << "WRONG_ANSWER" << endl;
return 0;
}
}
long v = a[q];
for (int i = 1; i < q; i++) v *= 10;
for (int i = q + 1; i <= s; i++) v = v * 10 + a[i];
if (inttostr(v) == m)
cout << "OK" << endl;
else
cout << "WRONG_ANSWER" << endl;
return 0;
}
| 1,100 | CPP |
n,h,m=map(int,input().split())
ans=[-1]*(n+1)
while m:
l,r,x=map(int,input().split())
for i in range(l,r+1):
if ans[i]==-1:
ans[i]=x**2
else:
if ans[i]>x**2:
ans[i]=x**2
m-=1
for i in range(1,n+1):
if ans[i]==-1:
ans[i]=h**2
print(sum(ans[1:]))
| 800 | PYTHON3 |
import sys
def main():
nums = [int(i) for i in sys.stdin.readline().split("+")]
print("+".join([str(n) for n in sorted(nums)]))
if __name__ == '__main__':
main() | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
namespace _buff {
const size_t buff_size = 1e5;
char buff[buff_size], *begin = buff, *end = buff;
char getc() {
if (begin == end) {
end = (begin = buff) + fread(buff, 1, buff_size, stdin);
}
return begin == end ? -1 : *begin++;
}
} // namespace _buff
ll read() {
using namespace _buff;
ll ret = 0;
bool pos = true;
char c = getc();
for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
assert(~c);
}
if (c == '-') {
pos = false;
c = getc();
}
for (; c >= '0' && c <= '9'; c = getc()) {
ret = (ret << 3) + (ret << 1) + (c ^ 48);
}
return pos ? ret : -ret;
}
const size_t maxn = 2e5 + 5;
int n, c, x[maxn];
int cnt[maxn];
vector<int> vc;
bool check(int add) {
bool flag = false;
int cur = c;
auto lst = vc.end();
while (cur) {
auto it = upper_bound(vc.begin(), lst, cur);
if (it == vc.begin()) return true;
int x = *--it;
if (!flag && x < add && add < cur) {
cur -= add;
flag = true;
} else {
cur -= min(cur / x, cnt[x]) * x;
lst = it;
}
}
return false;
}
int main() {
c = read();
n = read();
for (int i = 0; i < n; ++i) {
x[i] = read();
++cnt[x[i]];
vc.emplace_back(x[i]);
}
sort(vc.begin(), vc.end());
vc.erase(unique(vc.begin(), vc.end()), vc.end());
for (int i = 1; i <= c; ++i) {
if (check(i)) {
return printf("%d\n", i), 0;
}
}
puts("Greed is good");
}
| 2,600 | CPP |
data = list(map(int, input().split()))
ndigits = data[0]
d_sum = data[1]
if d_sum == 0:
if ndigits > 1:
print("-1 -1")
else:
print("0 0")
elif 9 * ndigits < d_sum:
print("-1 -1")
else:
to_minimal = d_sum
minimal = ""
for x in range(ndigits-1):
for number in range(9,-1,-1):
if to_minimal - number > 0:
to_minimal -= number
minimal += str(number)
break
minimal += str(to_minimal)
maximal = ""
for x in range(ndigits-1):
for number in range(9,-1,-1):
if d_sum - number > -1:
d_sum -= number
maximal += str(number)
break
maximal += str(d_sum)
for index in range(ndigits-1,-1,-1):
print(minimal[index],end="")
print(" " + str(maximal)) | 1,400 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int vladik;
int valera;
int num = 1;
bool flag = true;
scanf("%d %d", &vladik, &valera);
while (true) {
if (flag) {
vladik -= num;
num++;
flag = false;
if (vladik < 0) {
printf("Vladik\n");
break;
}
} else {
valera -= num;
num++;
flag = true;
if (valera < 0) {
printf("Valera\n");
break;
}
}
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const int INF = 1e9;
int t, n, m, cpm, cnt, degout[MAXN], tp[MAXN], num[MAXN], low[MAXN];
vector<int> E[MAXN], r1, r2;
stack<int> s;
void reset() {
cpm = 0;
cnt = 0;
for (long long i = 1; i <= n; i++) {
E[i].clear();
num[i] = 0;
low[i] = 0;
degout[i] = 0;
tp[i] = 0;
}
while (s.size()) s.pop();
r1.clear();
r2.clear();
}
void visit(int u) {
s.push(u);
num[u] = low[u] = ++cnt;
for (auto v : E[u]) {
if (!num[v]) {
visit(v);
low[u] = min(low[u], low[v]);
} else {
low[u] = min(low[u], num[v]);
}
}
if (low[u] == num[u]) {
cpm++;
int v;
do {
v = s.top();
s.pop();
num[v] = INF;
low[v] = INF;
tp[v] = cpm;
} while (v != u);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
reset();
cin >> n >> m;
for (long long i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
if (u == v) continue;
E[u].push_back(v);
}
for (long long i = 1; i <= n; i++) {
if (!num[i]) visit(i);
}
for (long long u = 1; u <= n; u++) {
for (auto v : E[u]) {
if (tp[u] == tp[v]) continue;
degout[tp[u]]++;
}
}
if (cpm <= 1) {
cout << "No" << endl;
continue;
}
int vertex, val = INF;
for (long long i = 1; i <= cpm; i++) {
if (degout[i] < val) {
val = degout[i];
vertex = i;
}
}
for (long long i = 1; i <= n; i++) {
if (tp[i] == vertex) {
r1.push_back(i);
} else {
r2.push_back(i);
}
}
cout << "Yes" << endl;
cout << (int)r1.size() << ' ' << (int)r2.size() << endl;
for (long long i = 0; i <= (int)r1.size() - 1; i++) cout << r1[i] << ' ';
cout << endl;
for (long long i = 0; i <= (int)r2.size() - 1; i++) cout << r2[i] << ' ';
cout << endl;
}
}
| 2,400 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
using namespace std;
long long spf[301];
long long fac[301];
void sieve() {
spf[1] = 1;
for (long long i = 2; i < 301; i++) spf[i] = i;
for (long long i = 4; i < 301; i += 2) spf[i] = 2;
for (long long i = 3; i * i < 301; i++) {
if (spf[i] == i) {
for (long long j = i * i; j < 301; j += i)
if (spf[j] == j) spf[j] = i;
}
}
}
map<long long, long long> getfactor(long long a) {
map<long long, long long> m;
while (a > 1) {
m[spf[a]]++;
a /= spf[a];
}
return m;
}
long long power(long long x, long long y, long long p) {
long long res = 1;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long inverse(long long a, long long p) { return power(a, p - 2, p); }
long long ncr(long long n, long long r, long long p) {
if (r > n) return 0;
if (r == 0) return 1;
return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p;
}
long long seg[4000009];
void updateseg(long long v, long long l, long long r, long long pos,
long long val) {
if (l == r) {
seg[v] = min(val, seg[v]);
return;
}
long long mid = (l + r) / 2;
if (pos <= mid)
updateseg(v * 2, l, mid, pos, val);
else
updateseg(v * 2 + 1, mid + 1, r, pos, val);
seg[v] = min(seg[v * 2], seg[v * 2 + 1]);
}
long long queryseg(long long v, long long l, long long r, long long posl,
long long posr) {
if (l == posl && r == posr)
return seg[v];
else if (posl > posr)
return 10000000000;
long long mid = (l + r) / 2;
return min(queryseg(v * 2, l, mid, posl, min(posr, mid)),
queryseg(v * 2 + 1, mid + 1, r, max(posl, mid + 1), posr));
}
long long seg2[4000009] = {0};
void updateseg2(long long v, long long l, long long r, long long pos,
long long val) {
if (l == r) {
seg2[v] = max(val, seg2[v]);
return;
}
long long mid = (l + r) / 2;
if (pos <= mid)
updateseg2(v * 2, l, mid, pos, val);
else
updateseg2(v * 2 + 1, mid + 1, r, pos, val);
seg2[v] = max(seg2[v * 2], seg2[v * 2 + 1]);
}
long long queryseg2(long long v, long long l, long long r, long long posl,
long long posr) {
if (l == posl && r == posr)
return seg2[v];
else if (posl > posr)
return -10000000000;
long long mid = (l + r) / 2;
return max(queryseg2(v * 2, l, mid, posl, min(posr, mid)),
queryseg2(v * 2 + 1, mid + 1, r, max(posl, mid + 1), posr));
}
void solve() {
for (long long i = 0; i <= 4000008; i++) seg[i] = 1e9;
long long n, x;
cin >> n >> x;
long long a[n + 1];
for (long long i = 1; i <= n; i++) {
cin >> a[i];
updateseg2(1, 1, x, a[i], i);
updateseg(1, 1, x, a[i], i);
}
if (x == 1) {
cout << 1 << '\n';
return;
}
long long l = 1, r = x;
while (r > 2) {
if (queryseg2(1, 1, x, r - 1, r - 1) < queryseg(1, 1, x, r, x))
r--;
else
break;
}
long long ans = x + 2 - r;
while (r <= x + 1 && l <= x) {
if (l + 1 == r) r++;
if (r <= x) {
while (r <= x && queryseg2(1, 1, x, l, l) >= queryseg(1, 1, x, r, x)) r++;
}
if (queryseg(1, 1, x, l, l) > queryseg2(1, 1, x, 1, l - 1))
ans += x + 2 - r;
else
break;
l++;
}
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long test = 1;
while (test--) {
solve();
}
}
| 2,100 | CPP |
import sys
mod = 1000000007
def get_array(): return list(map(int, sys.stdin.readline().split()))
def get_ints(): return map(int, sys.stdin.readline().split())
def input(): return sys.stdin.readline()
def print_array(a): print(" ".join(map(str, a)))
def main():
for _ in range(int(input())):
n, a, b = get_ints()
s = input()
cost, cnt, i = n * (a + b) + b, 0, 0
for i in range(n - 1):
if s[i] == '1' or s[i + 1] == '1': cnt += 1
if cnt > 0:
cost += 2 * a + cnt * b
i = s.index('1')
j = n - 1
while s[j] == '0': j -= 1
cnt = 0
while i <= j:
if s[i] == '0': cnt += 1
else:
if cnt > 0: cost += min((cnt - 1) * b, 2 * a)
cnt = 0
i += 1
print(cost)
if __name__ == "__main__":
main() | 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> tr[100001];
int dp[100001][11][3], k, x, n, m, tmp[11][3], md = 1e9 + 7;
inline void ad(int &x, int y) {
if ((x += y) >= md) x -= md;
}
void go(int v = 1, int p = 0) {
dp[v][0][0] = k - 1;
dp[v][1][1] = 1;
dp[v][0][2] = m - k;
int(*z)[3] = dp[v];
for (int i = 0; i < (int)tr[v].size(); ++i) {
int u = tr[v][i];
if (u == p) continue;
go(u, v);
for (int i = 0; i < (int)11; ++i)
for (int j = 0; j < (int)3; ++j) tmp[i][j] = z[i][j], z[i][j] = 0;
for (int szv = 0; szv < (int)x + 1; ++szv)
for (int szu = 0; szu < (int)x - szv + 1; ++szu) {
int nsz = szv + szu;
ad(z[nsz][0],
tmp[szv][0] *
((long long)dp[u][szu][0] + dp[u][szu][1] + dp[u][szu][2]) % md);
ad(z[nsz][1], tmp[szv][1] * (long long)dp[u][szu][0] % md);
ad(z[nsz][2],
tmp[szv][2] * (long long)(dp[u][szu][0] + dp[u][szu][2]) % md);
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i < (int)n; ++i) {
int a, b;
scanf("%d%d", &a, &b);
tr[a].push_back(b);
tr[b].push_back(a);
}
scanf("%d%d", &k, &x);
go();
int an = 0;
for (int i = 0; i < (int)x + 1; ++i)
for (int j = 0; j < (int)3; ++j) ad(an, dp[1][i][j]);
printf("%d\n", an);
}
| 2,000 | CPP |