solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<cstdio>
#define mod 1000000007
#define int long long
int x,y,fac[20010000];
int quickpow(int x,int y){
if(y==0)return 1;
if(y%2==0)return quickpow(x*x%mod,y/2);
else return quickpow(x*x%mod,y/2)*x%mod;
}
int C(int n,int m){
if(m==0||n==m)return 1;
return fac[n]*quickpow(fac[m],mod-2)%mod*quickpow(fac[n-m],mod-2)%mod;
}
signed main(){
scanf("%lld%lld",&x,&y);
fac[0]=1;
for(int i=1;i<=20000000;i++)
fac[i]=fac[i-1]*i%mod;
if((x+y)%3!=0)return puts("0"),0;
if(2*y<x||2*x<y)return puts("0"),0;
int X=(2*y-x)/3,Y=(2*x-y)/3;
printf("%lld\n",C(X+Y,X));
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int x1[100];
int ololo[100];
int x2[100];
int y2[100];
int m[100];
int main(void) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d%d%d", &x1[i], &ololo[i], &x2[i], &y2[i]);
m[i] = abs(((x1[i] - x2[i]) * (x1[i] - x2[i]) * (x1[i] - x2[i])));
}
for (int i = 1; i < n; i++) {
long long cx = 0;
long long cy = 0;
long long sm = 0;
for (int j = i; j >= 1; j--) {
cx += (x1[j] + x2[j]) * m[j];
cy += (ololo[j] + y2[j]) * m[j];
sm += m[j];
if ((cx - 2 * sm * x2[j - 1]) * (cx - 2 * sm * x1[j - 1]) > 0 ||
(cy - 2 * sm * y2[j - 1]) * (cy - 2 * sm * ololo[j - 1]) > 0) {
printf("%d\n", i);
return 0;
}
}
}
printf("%d\n", n);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int M = 110;
const int N = 101000 + M;
const int MOD = (int)1e9 + 7;
int a[N], F[N], Finv[N], Inv[N], n, m, Array[N][M];
void init() {
Inv[1] = 1;
F[0] = Finv[0] = 1;
for (int i = 2; i < N; i++) {
Inv[i] = (long long)Inv[MOD % i] * (MOD - MOD / i) % MOD;
}
for (int i = 1; i < N; i++) {
F[i] = (long long)F[i - 1] * i % MOD;
Finv[i] = (long long)Finv[i - 1] * Inv[i] % MOD;
}
}
int C(int a, int b) {
if (a < b) return 0;
return (long long)F[a] * Finv[b] % MOD * Finv[a - b] % MOD;
}
int main() {
init();
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < m; i++) {
int l, r, K;
scanf("%d%d%d", &l, &r, &K);
l--;
(Array[l][K] += 1) %= MOD;
int val = 1;
for (int j = K; j >= 0; j--) {
(Array[r][j] -= val) %= MOD;
val = (long long)val * (r - l + K - j) % MOD * Inv[K - j + 1] % MOD;
}
}
for (int i = 0; i < n; i++) {
for (int j = 100; j >= 0; j--) {
if (i) (Array[i][j] += Array[i - 1][j]) %= MOD;
if (j != 100) (Array[i][j] += Array[i][j + 1]) %= MOD;
}
printf("%d\n", ((a[i] + Array[i][0]) % MOD + MOD) % MOD);
}
return 0;
}
| 5 |
#include <iostream>
#include <cmath>
using namespace std;
int X, Y, Z, V[5], e[51], a[51];
// dp[マス][お金] := 確率
double dp[51][5000];
int main(){
while( cin >> X >> Y >> Z, X || Y || Z ){
// 初期化
for(int i = 0; i < 51; i++){
e[i] = a[i] = 0;
for(int j = 0; j < 5000; j++){
dp[i][j] = 0.0;
}
}
for(int i = 0; i < X; i++) cin >> V[i];
for(int i = 0; i < Z; i++){
int N, E, A;
cin >> N >> E >> A;
e[N] = E;
a[N] = A;
}
dp[0][0] = 1.0;
for(int i = 0; i < Y ; i++){
for(int j = 0; j < 5000; j++){
if( dp[i][j] > 0.0 ){
for(int k = 0; k < X; k++){
int next = i + V[k], next_money = j;
if( e[next] == 1 ){
next += a[next];
}else if( e[next] == 2 ){
next_money += a[next];
}else if( e[next] == 3 ){
next_money -= a[next];
}
if( next_money < 0 ) next_money = 0;
if( Y <= next ) next = Y;
dp[next][next_money] += dp[i][j] * (1.0 / X);
}
}
}
}
double res = 0.0;
for(int i = 0; i < 5000; i++) res += i * dp[Y][i];
cout << ((int)floor(res)) << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int max_N = 300 + 20;
vector<pair<int, int> > adj[max_N];
bool mark[max_N];
int pt[max_N], par[max_N];
pair<int, int> a[max_N];
vector<int> path;
void Eulerian_Tour(int v) {
while (pt[v] < (int)adj[v].size()) {
if (!mark[adj[v][pt[v]].second]) {
mark[adj[v][pt[v]].second] = true;
Eulerian_Tour(adj[v][pt[v]].first);
path.push_back(v);
}
pt[v]++;
}
return;
}
void dfs(int v) {
mark[v] = true;
for (int i = 0; i < (int)adj[v].size(); i++) {
if (!mark[adj[v][i].first]) dfs(adj[v][i].first);
}
}
int main() {
int n, cnt(0), u, v;
cin >> n;
for (int i = 7; i < n + 7; i++) {
cin >> a[i].first >> a[i].second;
u = v = a[i].first;
adj[a[i].first].push_back(make_pair(i, cnt));
adj[a[i].second].push_back(make_pair(i, cnt + 1));
adj[i].push_back(make_pair(a[i].first, cnt));
adj[i].push_back(make_pair(a[i].second, cnt + 1));
cnt += 2;
}
dfs(7);
for (int i = 8; i < n + 7; i++) {
if (!mark[i]) {
cout << "No solution";
return 0;
}
}
for (int i = 0; i < n * 2; i++) mark[i] = false;
int odd(0);
bool flag = false;
for (int i = 0; i < 7; i++) {
if (adj[i].size() % 2 == 1) {
odd++;
if (!flag) {
v = i;
flag = true;
} else
u = i;
}
}
if (odd > 2) {
cout << "No solution";
return 0;
}
path.push_back(v);
Eulerian_Tour(u);
for (int i = 1; i < (int)path.size(); i++) {
if (path[i] >= 7) {
cout << path[i] - 6 << ' ';
if (path[i - 1] == a[path[i]].first)
cout << '+' << endl;
else
cout << '-' << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long N, k;
cin >> N >> k;
if (k > N * (N - 1) / 2)
cout << "Impossible" << endl;
else {
long long n = (-1 + sqrt(1 + 8 * k)) / 2;
if (n * (n + 1) / 2 == k && n + 1 <= N) {
for (int i = 0; i <= n; i++) {
cout << "(";
}
for (int i = 0; i <= n; i++) {
cout << ")";
}
for (int i = 1; i < N - n; i++) {
cout << "()";
}
} else if (n + 2 <= N) {
for (int i = 0; i <= n; i++) {
if (i == k - n * (n + 1) / 2)
cout << "()(";
else
cout << "(";
}
for (int i = 0; i <= n; i++) {
cout << ")";
}
for (int i = 1; i < N - n - 1; i++) {
cout << "()";
}
} else
cout << "Impossible" << endl;
}
}
| 3 |
#include <bits/stdc++.h>
const long long INF = (long long)1e18 + 123;
const int inf = (int)2e9 + 123;
const int mod = 1e9 + 7;
using namespace std;
pair<long long, long long> a[100011];
long long n, mx_p[100011], mn_p[100011], mx_s[100011], mn_s[100011];
long long get(int r, int l) {
return (a[r].first - a[l].first) * (a[r].first - a[l].first);
}
int get_r(int x, long long dist) {
int l = x, r = n, mx = l;
while (l <= r) {
int mid = (l + r) / 2;
if (get(mid, x) <= dist && abs(a[x].first) >= abs(a[mid].first)) {
mx = max(mx, mid);
l = mid + 1;
} else
r = mid - 1;
}
return mx;
}
int get_l(int x, long long dist) {
int l = 1, r = x, mn = r;
while (l <= r) {
int mid = (l + r) / 2;
if (get(x, mid) <= dist && abs(a[x].first) >= abs(a[mid].first)) {
mn = min(mn, mid);
r = mid - 1;
} else
l = mid + 1;
}
return mn;
}
bool check(long long dist) {
for (int i = 1; i <= n; i++) {
int j;
if (a[i].first > 0)
j = get_l(i, dist);
else
j = get_r(i, dist);
int x = min(i, j), y = max(i, j);
long long mx = 0;
if (x > 1) {
mx = max(mx, max(abs(mn_p[x - 1]), abs(mx_p[x - 1])));
}
if (y < n) mx = max(mx, max(abs(mn_s[y + 1]), abs(mx_s[y + 1])));
long long mn = min(mn_s[y + 1], mn_p[x - 1]),
MX = max(mx_p[x - 1], mx_s[y + 1]);
if (a[i].first * a[i].first + mx * mx <= dist &&
1ll * (MX - mn) * (MX - mn) <= dist) {
return 1;
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + 1 + n);
mx_p[0] = -inf;
mx_s[n + 1] = -inf;
mn_p[0] = inf;
mn_s[n + 1] = inf;
for (int i = 1; i <= n; i++) {
mx_p[i] = max(mx_p[i - 1], a[i].second);
mn_p[i] = min(mn_p[i - 1], a[i].second);
}
for (int i = n; i >= 1; i--) {
mx_s[i] = max(mx_s[i + 1], a[i].second);
mn_s[i] = min(mn_s[i + 1], a[i].second);
}
long long l = 0, r = INF;
long long ans = INF;
ans = min(ans, 1ll * (a[n].first - a[1].first) * (a[n].first - a[1].first));
long long mn = inf, mx = -inf;
for (int i = 1; i <= n; i++) {
mn = min(mn, a[i].second);
mx = max(mx, a[i].second);
}
ans = min(ans, 1ll * (mx - mn) * (mx - mn));
while (l <= r) {
long long mid = (l + r) / 2;
if (check(mid)) {
ans = min(ans, mid);
r = mid - 1;
} else
l = mid + 1;
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> L[1010];
int dist[1010];
int foi0[1010];
int foi[1010];
int n, m;
int solve(int a) {
vector<int> v;
queue<int> Q;
Q.push(a);
foi0[a] = 1;
while (Q.size()) {
int b = Q.front();
v.push_back(b);
Q.pop();
for (int i = 0; i < L[b].size(); i++)
if (!foi0[L[b][i]]) {
foi0[L[b][i]] = 1;
Q.push(L[b][i]);
}
}
int ret = -1;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v.size(); j++) foi[v[j]] = 0;
int b = v[i];
foi[b] = 1;
while (Q.size()) Q.pop();
Q.push(b);
dist[b] = 0;
int dmax = 0;
int ok = 1;
while (Q.size()) {
int c = Q.front();
Q.pop();
for (int j = 0; j < L[c].size(); j++)
if (!foi[L[c][j]]) {
foi[L[c][j]] = 1;
dist[L[c][j]] = 1 + dist[c];
dmax = dist[L[c][j]];
Q.push(L[c][j]);
} else {
if (abs(dist[c] - dist[L[c][j]]) != 1) ok = 0;
}
}
if (ok) ret = max(ret, dmax);
}
return ret;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b), a--, b--;
L[a].push_back(b);
L[b].push_back(a);
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (foi0[i]) continue;
int u = solve(i);
if (u == -1) {
printf("-1\n");
return 0;
}
ans += u;
}
printf("%d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n, degree[MAXN], m;
double mark[MAXN], dn[MAXN][3], dp[MAXN];
pair<int, int> ar[MAXN], node[MAXN];
bool leaf[MAXN];
void g(int p, int k) {
dn[p][0] = dn[p][1] = mark[p];
degree[p] = k;
if (ar[p].first) {
g(ar[p].first, k + 1);
g(ar[p].second, k + 1);
dn[p][0] = dn[ar[p].first][0];
dn[p][1] = dn[ar[p].second][1];
}
}
void f(int p) {
dp[ar[p].first] = dp[p] + dn[ar[p].second][0];
dp[ar[p].second] = dp[p] + dn[ar[p].first][1];
leaf[p] = true;
if (ar[p].first) {
leaf[p] = false;
f(ar[p].first);
f(ar[p].second);
}
}
int main() {
int a, b, root;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a >> b;
mark[i] = b;
node[i] = pair<int, int>(b, i);
if (a == -1)
root = i;
else if (!ar[a].first)
ar[a].first = i;
else
ar[a].second = i;
}
for (int i = 1; i <= n; i++)
if (ar[i].first && mark[ar[i].first] > mark[ar[i].second])
swap(ar[i].first, ar[i].second);
g(root, 0);
f(root);
sort(node + 1, node + n + 1);
cin >> m;
for (int i = 1, v; i <= m; i++) {
cin >> v;
int tmp = lower_bound(node + 1, node + n + 1, pair<int, int>(v, 0)) - node;
if (leaf[node[tmp].second] == false || tmp == n + 1) tmp--;
printf("%.20lf\n", dp[node[tmp].second] / degree[node[tmp].second]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> par;
UnionFind(int N) : par(N) {
for (int i = 0; i < N; i++) par[i] = i;
}
int root(int x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry) return;
par[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
int main() {
int N, M;
cin >> N >> M;
vector<int> p(N);
for (int i = 0; i < N; i++) {
cin >> p[i];
p[i]--;
}
UnionFind UF(N);
for (int i = 0; i < M; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
UF.unite(x, y);
}
int ans = 0;
for (int i = 0; i < N; i++) {
ans += UF.same(i, p[i]);
}
cout << ans << endl;
} | 0 |
#include<iostream>
using namespace std;
int N, M, Q;
int sum[555][555];
int main() {
int N, M, Q;
cin >> N >> M >> Q;
for (int i = 0;i < M;i++) {
int L, R;
cin >> L >> R;
sum[L][R]++;
}
for (int i = 0;i < N;i++) {
for (int j = 0;j < N;j++) {
sum[i + 1][j + 1] += sum[i + 1][j] + sum[i][j + 1] - sum[i][j];
}
}
for (int i = 0;i < Q;i++) {
int p, q;
cin >> p >> q;
cout << sum[q][q] - sum[p - 1][q] - sum[q][p - 1] + sum[p - 1][p - 1] << endl;
}
} | 0 |
#include <bits/stdc++.h>
#define ll long long
#define lsb(x) x & -x
using namespace std;
const int MOD = 998244353;
int raise(int n, int p) {
int ans = 1;
while(p) {
if(p % 2)
ans = (1LL * ans * n) % MOD;
n = (1LL * n * n) % MOD;
p /= 2;
}
return ans;
}
int invMod(int x) {
return raise(x, MOD - 2);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int ntests;
cin >> ntests;
while(ntests --) {
int n;
string s;
cin >> n >> s;
n ++;
vector<int> fact(n + 1, 1);
for(int i = 1; i <= n; i ++)
fact[i] = (1LL * fact[i - 1] * i) % MOD;
auto comb = [&](int n, int k) -> int {
if(n < k)
return 0;
return (1LL * ((1LL * fact[n] * invMod(fact[n - k])) % MOD) * invMod(fact[k])) % MOD;
};
int sz = 1, cnt = 0;
for(int i = 0; i < s.size(); i ++) {
if (i + 1 < n && s[i] == '1' && s[i + 1] == '1') {
i ++;
cnt ++;
} else if(s[i] == '0') {
sz ++;
}
}
cout << comb(cnt + sz - 1, sz - 1) << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using namespace std;
inline long long pow_mod(long long X, long long N, long long mod) {
long long res = 1;
while (N > 0) {
if (N & 1) res = res * X % mod;
X = X * X % mod;
N >>= 1;
}
return res;
}
const long long SI = 2e5 + 100;
const bool DBG = 0;
const long long mod = 1e9 + 7;
set<int> have[SI];
int belong[SI];
int Val[SI];
int num[SI];
map<int, int> id;
map<int, int> idbelong;
int h, n, m;
long long ans = 0;
int main() {
memset(belong, -1, sizeof(belong));
scanf("%d %d %d", &h, &m, &n);
for (int i = 0; i < h; i++) {
if (belong[i] >= 0) continue;
if (DBG)
cout << "i"
<< "=" << i << '\n';
;
for (int j = i, k = 0; belong[j] == -1; j += m, j %= h, k++) {
belong[j] = i;
num[i]++;
Val[j] = k;
if (DBG)
cout << "j"
<< "=" << j << ' ';
;
}
if (DBG)
cout << "num[i]"
<< "=" << num[i] << '\n';
;
for (int j = 0; j < num[i]; j++) have[i].insert(j);
}
for (int i = 0; i < h; i++) {
if (DBG)
cout << "i"
<< "=" << i << ' ';
;
if (DBG)
cout << "Val[i]"
<< "=" << Val[i] << '\n';
;
}
char qr[2];
int hs, wh;
set<int>::iterator it;
while (n--) {
scanf("%s", qr);
if (qr[0] == '+') {
scanf("%d %d", &wh, &hs);
it = have[belong[hs]].lower_bound(Val[hs]);
if (it == have[belong[hs]].end()) {
it = have[belong[hs]].begin();
ans += *it + num[belong[hs]] - Val[hs];
} else {
ans += *it - Val[hs];
}
id[wh] = *it;
idbelong[wh] = belong[hs];
have[belong[hs]].erase(it);
} else {
scanf("%d", &wh);
have[idbelong[wh]].insert(id[wh]);
}
}
printf("%I64d", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long s = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) s = s * 10 + ch - '0';
return s;
}
const int MAXN = 1e3 + 10;
int sum[MAXN];
int ans[50][50];
int n, num;
int main() {
scanf("%d", &n);
int N = n * n, x;
for (int i = 1; i <= N; i++) {
scanf("%d", &x);
sum[x]++;
}
int flag = true;
int mid = (n + 1) / 2;
int num = 1;
if (n % 2 == 0) {
for (int i = 1; i <= mid; i++) {
for (int j = 1; j <= mid; j++) {
for (; num <= 1000; num++) {
if (sum[num]) {
if (sum[num] % 4) {
flag = false;
break;
}
ans[i][j] = ans[i][n - j + 1] = ans[n - i + 1][j] =
ans[n - i + 1][n - j + 1] = num;
sum[num] -= 4;
break;
}
}
if (num > 1000) flag = false;
if (!flag) break;
}
if (!flag) break;
}
} else {
flag = true;
num = 1;
for (; num <= 1000; num++) {
if (sum[num] % 2) {
ans[mid][mid] = num;
sum[num]--;
break;
}
}
if (num > 1000) {
flag = false;
} else {
num = 1;
for (int i = 1; i < mid; i++) {
for (int j = 1; j < mid; j++) {
for (; num <= 1000; num++) {
if (sum[num] == 0) continue;
if (sum[num] / 4 < 1) continue;
ans[i][j] = ans[i][n - j + 1] = ans[n - i + 1][j] =
ans[n - i + 1][n - j + 1] = num;
sum[num] -= 4;
break;
}
if (num > 1000) flag = false;
if (!flag) break;
}
if (!flag) break;
}
num = 1;
for (int i = 1; i < mid; i++) {
for (; num <= 1000; num++) {
if (sum[num] / 2 >= 1) {
ans[mid][i] = ans[mid][n - i + 1] = num;
sum[num] -= 2;
break;
}
}
if (num > 1000) {
flag = false;
break;
}
}
num = 1;
for (int i = 1; i < mid; i++) {
for (; num <= 1000; num++) {
if (sum[num] / 2 >= 1) {
ans[i][mid] = ans[n - i + 1][mid] = num;
sum[num] -= 2;
break;
}
}
if (num > 1000) {
flag = false;
break;
}
}
}
}
if (!flag)
puts("NO");
else {
puts("YES");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) printf("%d ", ans[i][j]);
puts("");
}
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)
#define PB push_back
#define ar2 array<int,2>
#define vs vector<string>
typedef long long LL;
const LL P = 1e9+7;
const int N = 2e5+5;
mt19937 rng(time(0));
vector<int> g[N];
int n, x, y, a[N], r[4];
void dfs(int x, int fa, int num){
a[x] = num;
for(auto u:g[x])
if(u!=fa) dfs(u, x, 1-num);
}
int main() {
scanf("%d",&n);
rep(i,1,n-1){
scanf("%d%d",&x,&y);
g[x].PB(y), g[y].PB(x);
}
dfs(1,0,1);
int cnt = 0;
rep(i,1,n) cnt += a[i];
if(cnt*2>n){
rep(i,1,n) a[i] = 1-a[i];
cnt = n - cnt;
}
if(cnt*3<=n){
rep(i,1,n) if(a[i]==1) a[i] = 3;
r[1] = (n+2)/3, r[2] = (n+1)/3, r[3] = n/3-cnt;
rep(i,1,n) if(a[i]==0){
rep(j,1,3) if(r[j]){
r[j]--, a[i] = j;
break;
}
}
}else{
rep(i,1,n) if(a[i]==0) a[i] = 1; else a[i] = 2;
r[1] = n - cnt - (n+2)/3, r[2] = cnt - (n+1)/3;
rep(i,1,n){
int x = a[i];
if(!r[x]) continue;
r[x]--, a[i] = 3;
}
}
rep(i,1,3) r[i] = i;
rep(i,1,n) x = r[a[i]], r[a[i]] += 3, a[i] = x;
rep(i,1,n) printf("%d ",a[i]);
return 0;
} | 0 |
/*input
7 11
1 2 2
1 3 1
2 4 2
2 5 2
2 6 1
3 4 1
3 5 1
3 6 1
4 7 2
5 7 2
6 7 1
*/
#include <bits/stdc++.h>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N 1505
#define bit(x,y) ((x>>y)&1LL)
int n, m;
vector<vector<pair<int, int> > > a(N);
int dis[N][N];
bool inqueue[N];
deque<int> q;
int mxans = 0;
int mx[N];
int suminQueue = 0;
void SPFA(int root) {
#define f dis[root]
f[root] = 0;
q.push_back(root); inqueue[root] = true;
while (!q.empty()) {
while (f[q.front()]*q.size() > suminQueue) {
q.push_back(q.front());
q.pop_front();
}
int u = q.front(); q.pop_front(); inqueue[u] = false; suminQueue -= f[u];
for (auto v : a[u]) {
if (f[v.fi] > f[u] + v.se) {
f[v.fi] = f[u] + v.se;
if (!inqueue[v.fi]) {
inqueue[v.fi] = true;
if (!q.empty() && f[v.fi] < f[q.front()]) {
q.push_front(v.fi);
}
else q.push_back(v.fi);
suminQueue += f[v.fi];
}
}
}
}
for (int i = 1; i <= n; i++) mxans = max(mxans, dis[root][i]);
}
bool visited[N]; bool mark[N];
void dfs(int u, int root) {
if (visited[u]) return;
visited[u] = true;
mx[u] = dis[root][u];
for (auto v : a[u]) {
if (dis[root][v.fi] == dis[root][u] + v.se) {
dfs(v.fi, root); mx[u] = max(mx[u], mx[v.fi]);
}
}
if (mx[u] == mxans)
mark[u] = 1;
}
vector<int> order;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef in1code
freopen("1test.inp", "r", stdin);
#endif
memset(dis, 127, sizeof(dis));
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, c;
cin >> u >> v >> c;
a[u].push_back(mp(v, c));
a[v].push_back(mp(u, c));
}
for (int i = 1; i <= n; i++) SPFA(i);
for (int i = 1; i <= n; i++) {
memset(visited, 0, sizeof(visited)); dfs(i, i);
}
for (int i = 1; i <= n; i++) {
if (!mark[i]) order.push_back(i);
}
cout << order.size() << endl;
for (auto it : order) cout << it << endl;
}
| 0 |
/* Noob */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 1000000000;
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.second < b.second;
}
void solve() {
ll n;
cin >> n;
ll a[n];
ll op[110] = {0};
for (int i = 0 ; i < n; i++)cin >> a[i], op[a[i]]++;
vector<int>ans;
for (int i = 0 ; i < n; i++) {
for (int j = 0 ; j < 110; j++) {
if (op[j] > 0) {ans.push_back(j); op[j]--;}
}
}
for (int i = 0 ; i < n ; i++)cout << ans[i] << " ";
cout << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int best = 0;
long long int m, b;
cin >> m >> b;
for (int i = b; i >= 0; i--) {
long long int x = (b - i) * m;
long long int t =
((x + 1) * (i * (i + 1)) / 2) + ((i + 1) * (x * (x + 1)) / 2);
best = max(best, t);
}
cout << best << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int l = 0, r = 0;
int q = 0;
for (int i = 0; i < n / 2; i++) {
if (s[i] == '?')
q++;
else
l += s[i] - '0';
}
for (int i = n / 2; i < n; i++) {
if (s[i] == '?')
q--;
else
r += s[i] - '0';
}
if (q / 2 * 9 == r - l)
cout << "Bicarp" << endl;
else
cout << "Monocarp" << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> first(n);
vector<int> second(n);
bool flag = false;
int ans = 0;
vector<int> one;
for (int i = 0; i < n; ++i) {
cout << "? " << 0 << " " << i << endl;
fflush(stdout);
cin >> first[i];
}
for (int i = 0; i < n; ++i) {
cout << "? " << i << " " << 0 << endl;
fflush(stdout);
cin >> second[i];
}
for (int fb = 0; fb < n; ++fb) {
vector<int> b(n);
vector<int> p(n);
b[0] = fb;
p[0] = first[0] ^ fb;
for (int i = 1; i < n; ++i) {
b[i] = second[i] ^ p[0];
p[i] = first[i] ^ b[0];
}
bool f = true;
for (int i = 0; i < n; ++i) {
if (p[b[i]] != i) {
f = false;
break;
}
}
if (f) {
++ans;
if (!flag) {
flag = true;
one = b;
}
}
}
cout << "! " << ans << endl;
for (auto elem : one) {
cout << elem << " ";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 200002;
const long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<long long> pow;
pow.push_back(1);
for (int i = 1; i <= n; i++) {
pow.push_back(pow.back() * 2);
pow[i] %= MOD;
}
int leafs = n;
vector<int> deg(n + 1);
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
if (deg[a] == 1) leafs--;
if (deg[b] == 1) leafs--;
deg[a]++;
deg[b]++;
}
cout << (pow[n - leafs] * ((n - leafs) + 2ll * leafs)) % MOD << '\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T &a, const T &b) {
a = a > b ? a : b;
}
template <typename T>
void chkmin(T &a, const T &b) {
a = a < b ? a : b;
}
const int MAXN = 1005, MAXM = 2005, MAXX = 500005;
const long long INF = 2e18;
long long f[2][MAXN], suf[MAXN], K;
int par[MAXM], len[MAXM], son[MAXM][26], n, m, tot, lst;
int pos[MAXM], lft[MAXX], rgt[MAXX], rch[MAXN];
char str[MAXN];
void extend(int c) {
int np = ++tot, p = lst;
len[np] = len[p] + 1;
for (; p && !son[p][c]; p = par[p]) son[p][c] = np;
if (p > 0) {
int q = son[p][c];
if (len[q] != len[p] + 1) {
int nq = ++tot;
len[nq] = len[p] + 1;
pos[nq] = pos[q];
memcpy(son[nq], son[q], sizeof(son[nq]));
par[nq] = par[q];
par[q] = par[np] = nq;
for (; p && son[p][c] == q; p = par[p]) son[p][c] = nq;
} else
par[np] = q;
} else
par[np] = 1;
lst = np;
}
void dfs(int u, int d) {
lft[tot] = pos[u] - d + 1;
rgt[tot] = pos[u];
++tot;
for (int i = 0; i < 26; i++)
if (son[u][i]) dfs(son[u][i], d + 1);
}
long long check(int mid) {
for (int i = 1; i <= n; i++) {
pos[i] = -1;
for (int a = lft[mid], b = i; a <= rgt[mid] && b <= n; ++a, ++b) {
if (str[a] != str[b]) {
pos[i] = str[a] > str[b] ? -1 : b;
break;
} else if (a == rgt[mid]) {
pos[i] = b;
break;
}
}
}
memset(f, 0, sizeof(f));
f[0][n + 1] = 1;
for (int i = 1; i <= m; i++) {
int now = i & 1, lst = !now;
memset(f[now], 0, sizeof(f[now]));
for (int j = n; j > 0; j--) chkmin(f[lst][j] += f[lst][j + 1], INF);
for (int j = n - i + 1; j > 0; j--)
if (pos[j] > 0) f[now][j] = f[lst][pos[j] + 1];
}
return f[m & 1][1];
}
int main() {
scanf("%d%d%lld", &n, &m, &K);
scanf("%s", str + 1);
tot = lst = 1;
for (int i = 1; i <= n; i++) {
extend(str[i] - 'a');
pos[lst] = i;
}
tot = 0;
dfs(1, 0);
int l = 1, r = tot;
while (l + 1 < r) {
int mid = (l + r) >> 1;
if (check(mid) >= K)
l = mid;
else
r = mid;
}
for (int i = lft[l]; i <= rgt[l]; i++) putchar(str[i]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char s[500005], ans[500005];
int k, n;
int dp[500005][2];
int main() {
scanf("%d%d%s", &n, &k, s);
if (k == 2) {
int tol1 = 0, tol2 = 0;
for (int i = 0; i < n; i++) {
if (s[i] != i % 2 + 'A') tol1++;
if (s[i] != (i + 1) % 2 + 'A') tol2++;
}
printf("%d\n", min(tol1, tol2));
int g = tol1 < tol2 ? 0 : 1;
for (int i = 0; i < n; i++) printf("%c", 'A' + (i + g) % 2);
printf("\n");
return 0;
}
dp[0][0] = 0;
dp[0][1] = 1;
for (int i = 1; s[i]; i++) {
dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + 1;
if (s[i] == s[i - 1])
dp[i][0] = dp[i - 1][1];
else
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]);
}
printf("%d\n", min(dp[n - 1][0], dp[n - 1][1]));
int j = dp[n - 1][0] < dp[n - 1][1] ? 0 : 1;
for (int i = n - 1; i >= 0; i--) {
if (j == 1) {
ans[i] = '?';
if (i == 0) break;
j = dp[i - 1][0] < dp[i - 1][1] ? 0 : 1;
} else {
ans[i] = s[i];
if (i == 0) break;
if (s[i] == s[i - 1])
j = 1;
else
j = dp[i - 1][0] < dp[i - 1][1] ? 0 : 1;
}
}
for (int i = 0; i < n; i++)
if (ans[i] == '?') {
ans[i] = 'A';
while (ans[i] == ans[i + 1] || (i > 0 && ans[i] == ans[i - 1])) ans[i]++;
}
printf("%s\n", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
int t, n, a[N];
vector<int> v;
int main() {
scanf("%d", &t);
while (t--) {
v.clear();
a[0] = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int f = 0;
if (a[n] > a[1])
f = 0;
else
f = 2;
if (f == 2)
puts("NO");
else
puts("YES");
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
ll power(ll x, ll y);
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 18;
const int inf = 2e9;
const ll N = 1e3 + 3;
ll pt[N][2];
ll n, f;
vector<ll> vis(N, 0);
map<ll, vector<ll> > X;
map<ll, vector<ll> > Y;
vector<ll> com[6];
void dfs(ll s, ll t) {
vis[s] = f;
com[f].emplace_back(s);
auto x = pt[s][0];
auto y = pt[s][1];
for (auto i : X[x])
if (!vis[i] && llabs(pt[i][1] - y) <= t) dfs(i, t);
for (auto i : Y[y])
if (!vis[i] && llabs(pt[i][0] - x) <= t) dfs(i, t);
}
bool chk(ll t) {
for (int i = 0; i < 6; i++) com[i].clear();
vis.assign(N, 0);
f = 0;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
f++;
dfs(i, t);
if (f > 4) break;
}
if (f == 1) {
return 1;
} else if (f == 2) {
for (auto i : com[1]) {
for (auto j : com[2]) {
if (pt[i][0] == pt[j][0] && llabs(pt[i][1] - pt[j][1]) <= 2 * t)
return 1;
if (pt[i][1] == pt[j][1] && llabs(pt[i][0] - pt[j][0]) <= 2 * t)
return 1;
if (llabs(pt[i][0] - pt[j][0]) <= t && llabs(pt[i][1] - pt[j][1]) <= t)
return 1;
}
}
} else if (f == 3) {
int comb[][3] = {{1, 2, 3}, {1, 3, 2}, {2, 3, 1}};
for (int loop = 0; loop < 3; loop++) {
auto a = comb[loop][0];
auto b = comb[loop][1];
auto c = comb[loop][2];
vector<pll> segx, segy;
for (auto i : com[a]) {
for (auto j : com[b]) {
if (pt[i][0] == pt[j][0] && llabs(pt[i][1] - pt[j][1]) <= 2 * t)
segx.emplace_back(i, j);
if (pt[i][1] == pt[j][1] && llabs(pt[i][0] - pt[j][0]) <= 2 * t)
segy.emplace_back(i, j);
}
}
for (auto seg : segy) {
for (auto p : com[c]) {
auto x1 = min(pt[seg.first][0], pt[seg.second][0]);
auto x2 = max(pt[seg.first][0], pt[seg.second][0]);
if (pt[p][0] - x1 <= t && x2 - pt[p][0] <= t &&
llabs(pt[seg.first][1] - pt[p][1]) <= t)
return 1;
}
}
for (auto seg : segx) {
for (auto p : com[c]) {
auto y1 = min(pt[seg.first][1], pt[seg.second][1]);
auto y2 = max(pt[seg.first][1], pt[seg.second][1]);
if (pt[p][1] - y1 <= t && y2 - pt[p][1] <= t &&
llabs(pt[seg.first][0] - pt[p][0]) <= t)
return 1;
}
}
}
} else if (f == 4) {
int comb[][4] = {{1, 2, 3, 4}, {1, 3, 2, 4}, {1, 4, 2, 3}};
for (int loop = 0; loop < 3; loop++) {
auto a = comb[loop][0];
auto b = comb[loop][1];
auto c = comb[loop][2];
auto d = comb[loop][3];
vector<pll> segx[2], segy[2];
for (auto i : com[a]) {
for (auto j : com[b]) {
if (pt[i][0] == pt[j][0] && llabs(pt[i][1] - pt[j][1]) <= 2 * t)
segx[0].emplace_back(i, j);
if (pt[i][1] == pt[j][1] && llabs(pt[i][0] - pt[j][0]) <= 2 * t)
segy[0].emplace_back(i, j);
}
}
for (auto i : com[c]) {
for (auto j : com[d]) {
if (pt[i][0] == pt[j][0] && llabs(pt[i][1] - pt[j][1]) <= 2 * t)
segx[1].emplace_back(i, j);
if (pt[i][1] == pt[j][1] && llabs(pt[i][0] - pt[j][0]) <= 2 * t)
segy[1].emplace_back(i, j);
}
}
for (auto s0 : segy[0]) {
for (auto s1 : segx[1]) {
auto x1 = min(pt[s0.first][0], pt[s0.second][0]);
auto x2 = max(pt[s0.first][0], pt[s0.second][0]);
auto x3 = pt[s1.first][0];
auto y3 = min(pt[s1.first][1], pt[s1.second][1]);
auto y4 = max(pt[s1.first][1], pt[s1.second][1]);
auto y1 = pt[s0.first][1];
if (x3 - x1 <= t && x2 - x3 <= t && y4 - y1 <= t && y1 - y3 <= t)
return 1;
}
}
for (auto s0 : segy[1]) {
for (auto s1 : segx[0]) {
auto x1 = min(pt[s0.first][0], pt[s0.second][0]);
auto x2 = max(pt[s0.first][0], pt[s0.second][0]);
auto x3 = pt[s1.first][0];
auto y3 = min(pt[s1.first][1], pt[s1.second][1]);
auto y4 = max(pt[s1.first][1], pt[s1.second][1]);
auto y1 = pt[s0.first][1];
if (x3 - x1 <= t && x2 - x3 <= t && y4 - y1 <= t && y1 - y3 <= t)
return 1;
}
}
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> pt[i][0] >> pt[i][1];
for (int i = 1; i <= n; i++) {
auto x = pt[i][0];
auto y = pt[i][1];
X[x].emplace_back(i);
Y[y].emplace_back(i);
}
ll L = 0, R = 2e9 + 9;
while (L <= R) {
ll mid = (L + R) / 2;
if (chk(mid))
R = mid - 1;
else
L = mid + 1;
}
if (L > (ll)2e9) L = -1;
cout << L;
}
ll power(ll x, ll y) {
ll res = 1;
x %= MOD;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1, x = (x * x) % MOD;
}
return res;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define ul unsigned long long int
const int n = 10006;
int32_t main()
{
IOS;
int T, N, y, x;
cin >> T;
for (int ts = 0; ts < T; ++ts){
cin >> x >> y;
cout << x-1 << " " << y << "\n";
}
return 0;
}
/* Passing as reference reflects changes in the original variable effective in
space and time (not using this can cause runtime error in some programs for large data like strings)*/
| 3 |
#include<bits/stdc++.h>
using namespace std;
string s,s1;
int len,len1;
int main()
{
cin>>s>>s1;
int len=s.length();
for(int i=0;i<len;i++)
{
cout<<s[i];
if(s1[i])
cout<<s1[i];
}
cout<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e5, Pow = 18;
int n, x, a[MaxN + 5];
int main() {
srand((unsigned)time(NULL));
while (~scanf("%d%d", &n, &x)) {
for (int i = 1; i <= n; i++) a[i] = 0;
if (n == 1) {
printf("YES\n%d\n", x);
} else if (n == 2) {
if (x == 0)
printf("NO\n");
else
printf("YES\n%d %d\n", 0, x);
} else {
a[1] = 1 << 18;
a[2] = 1 << 17;
int sum = a[1] ^ a[2];
for (int i = 3; i <= n - 1; i++) a[i] = i, sum ^= i;
a[n] = sum ^ x;
printf("YES\n");
for (int i = 1; i <= n; i++) printf("%d ", a[i]);
printf("\n");
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, tmp;
cin >> n >> m;
bool flag = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
scanf("%d", &tmp);
if ((i == 1 || i == n || j == 1 || j == m) && tmp) {
flag = 1;
}
}
}
if (flag)
cout << "2" << endl;
else
cout << "4" << endl;
return 0;
}
| 1 |
#include <iostream>
#include <vector>
#include <climits>
int main() {
int n;
std::cin >> n;
std::vector<std::vector<int>> mat(n + 1, std::vector<int>(n + 1, 0));
for (auto i = 1; i <= n; ++i) {
for (auto j = 1; j <= n; ++j) {
std::cin >> mat[i][j];
mat[i][j] += mat[i][j - 1] + mat[i - 1][j] - mat[i - 1][j - 1];
}
}
int max = INT_MIN;
for (auto left = 0; left < n; ++left) {
for (auto right = left + 1; right <= n; ++right) {
for (auto top = 0; top < n; ++top) {
for (auto bottom = top + 1; bottom <= n; ++bottom) {
if (max < mat[right][bottom] - mat[right][top] - mat[left][bottom] + mat[left][top])
max = mat[right][bottom] - mat[right][top] - mat[left][bottom] + mat[left][top];
}
}
}
}
std::cout << max << std::endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dp[201][201][2];
int main() {
string s;
cin >> s;
int n=s.size();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)dp[i][j][0]=1<<29,dp[i][j][1]=-(1<<29);
if(isdigit(s[i]))dp[i][i][0]=dp[i][i][1]=s[i]-'0';
}
for(int i=0; i<n; i++){
for(int j=0;j<n-i;j++){
if(s[j]=='(')dp[j][j+i][0]=min(dp[j][j+i][0],dp[j+1][j+i][0]),dp[j][j+i][1]=max(dp[j][j+i][1],dp[j+1][j+i][1]);
if(s[j+i]==')')dp[j][j+i][0]=min(dp[j][j+i][0],dp[j][j+i-1][0]),dp[j][j+i][1]=max(dp[j][j+i][1],dp[j][j+i-1][1]);
}
for(int j=0;j<n-i;j++){
if(s[j]=='+'||s[j]=='-'||s[j+i]=='+'||s[j+i]=='-')continue;
for(int k=j+1;k<=j+i-1;k++){
if((j+2<=k&&s[k-2]=='(')||(k<=j+i-2&&s[k+2]==')'))continue;
if(s[k]=='+'||s[k]=='-'){
for(int x=0;x<2;x++)for(int y=0;y<2;y++){
if(abs(dp[j][k-1][x])>=(1<<29)||abs(dp[k+1][j+i][y])>=(1<<29))continue;
dp[j][j+i][0]=min(dp[j][j+i][0],dp[j][k-1][x]+dp[k+1][j+i][y]*(s[k]=='+'?1:-1));
dp[j][j+i][1]=max(dp[j][j+i][1],dp[j][k-1][x]+dp[k+1][j+i][y]*(s[k]=='+'?1:-1));
}
}
}
}
}
cout<<dp[0][n-1][1]<<endl;
return 0;
}
| 0 |
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
#include <list>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;
#include <complex>
typedef complex<double> P;
typedef vector<P> vp;
typedef vector<double> vd;
#define LT(x,y) ((x)-(y)<=-EPS)
#define LE(x,y) ((x)-(y)<+EPS)
double dot(P a, P b) {
return a.real()*b.real()+a.imag()*b.imag();
}
double cross(P a, P b) {
return a.real()*b.imag()-a.imag()*b.real();
}
double dist(P a, P b) {
return abs(b-a);
}
double distance_ls_p(P a, P b, P c) {
if(dot(b-a, c-a) < EPS) return abs(c-a);
if(dot(a-b, c-b) < EPS) return abs(c-b);
return abs(cross(b-a, c-a))/abs(b-a);
}
bool is_inner_circle_ls(P w, double r, P a, P b) {
if(LT(dist(w, a), r) && LT(dist(w, b), r)) {
return true;
}
return false;
}
bool is_cross_circle_ls(P w, double r, P a, P b) {
if(is_inner_circle_ls(w, r, a, b)) {
return false;
} else {
if(LE(distance_ls_p(a, b, w), r)) {
return true;
}
}
return false;
}
int main() {
int n;
while(cin >> n, n) {
vp w(n);
vd r(n);
double x, y;
REP(i, n) {
cin >> x >> y >> r[i];
w[i] = P(x, y);
}
int m;
cin >> m;
REP(i, m) {
P t, s;
cin >> x >> y;
t = P(x, y);
cin >> x >> y;
s = P(x, y);
bool safe = false;
REP(i, n) {
if(is_cross_circle_ls(w[i], r[i], t, s)) {
safe = true;
break;
}
}
cout << (safe ? "Safe" : "Danger") << endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long m, n, ta, tb, k, A[200001], B[200001], i, j, b, r = -1, bou;
int main() {
for (cin >> n >> m >> ta >> tb >> k; i < n; i++) cin >> A[i], A[i] += ta;
for (i = 0; i < m; i++) cin >> B[i];
if (n <= k || m <= k) return cout << -1, 0;
for (i = 0; i <= k; i++) {
b = A[i];
if (B[m - (k - i) - 1] < b) return cout << -1, 0;
bou = lower_bound(B, B + m, b) - B;
r = max(r, B[bou + k - i] + tb);
}
cout << r;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const long long INFLL = 1001001001001001001LL;
template <typename T>
void pv(T a, T b) {
for (T i = a; i != b; ++i) cout << *i << " ";
cout << endl;
}
template <typename T>
void chmin(T& a, T b) {
if (a > b) a = b;
}
template <typename T>
void chmax(T& a, T b) {
if (a < b) a = b;
}
long long cnt[30][128];
int main() {
int b, d;
cin >> b >> d;
string a, c;
cin >> a >> c;
string ta = "";
for (int i = 0; i < 101; ++i) {
ta += a;
}
for (int i = 0; i < a.size(); ++i) {
int cur = 0, pos = i;
while (pos < ta.size() && cur < c.size()) {
if (ta[pos] == c[cur]) {
++cur;
}
++pos;
}
if (cur == c.size()) {
cnt[0][i] = pos - i;
} else {
cnt[0][i] = INF;
}
}
for (int i = 1; i < 30; ++i) {
for (int j = 0; j < a.size(); ++j) {
if (cnt[i - 1][j] < INF) {
cnt[i][j] = cnt[i - 1][j];
cnt[i][j] += cnt[i - 1][(j + cnt[i - 1][j]) % a.size()];
if (cnt[i][j] > INF) cnt[i][j] = INF;
} else {
cnt[i][j] = INF;
}
}
}
long long P = 0;
long long cons = 0;
for (int i = 29; i >= 0; --i) {
if (cons + cnt[i][cons % a.size()] > a.size() * b) {
continue;
}
P += 1 << i;
cons += cnt[i][cons % a.size()];
}
cout << P / d << endl;
return 0;
}
| 2 |
/*
convex_hull verified on 2020/3/13
https://atcoder.jp/contests/agc021/submissions/10802949
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/* snippet starts */
#include <complex>
#include <numeric>
#include <iomanip>
#define X real()
#define Y imag()
// operator overload for complex<value_t>
namespace std {
using value_t = double;
inline bool operator<(const complex<value_t> l, const complex<value_t> r) {
return (l.X != r.X ? l.X < r.X : l.Y < r.Y);
}
inline bool operator>(const complex<value_t> l, const complex<value_t> r) {
return (l.X != r.X ? l.X > r.X : l.Y > r.Y);
}
inline bool operator<=(const complex<value_t> l, const complex<value_t> r) { return !(l > r); }
inline bool operator>=(const complex<value_t> l, const complex<value_t> r) { return !(l < r); }
istream &operator>>(istream &is, complex<value_t> &p) {
value_t a, b;
is >> a >> b;
p = complex<value_t>(a, b);
return is;
}
ostream &operator<<(ostream &os, const complex<value_t> &p) {
return os << fixed << setprecision(10) << p.X << ' ' << p.Y;
}
}
namespace geometry2d {
using value_t = std::value_t;
using point_t = complex<value_t>;
// 内積, 外積, 単位ベクトル, なす角のcos
inline value_t dot(point_t a, point_t b) { return a.X * b.X + a.Y * b.Y; }
inline value_t cross(point_t a, point_t b) { return a.X * b.Y - a.Y * b.X; }
inline point_t unit(point_t p) { return p / abs(p); }
inline value_t cos_between(point_t a, point_t b) { return dot(unit(a), unit(b)); }
// 点集合pointsに対して凸包を作る
// ansに凸包の各点(pointsでのインデックス)が入る
// 戻り値はansのサイズ
int convex_hull(vector<point_t> &points, vector<int> &ans) {
int n = (int)points.size();
size_t k = 0;
vector<size_t> lid((size_t)n), _ans((size_t)n * 2);
// pointsを辞書順ソートしたときのインデックスの順列を作る
iota(lid.begin(), lid.end(), 0);
sort(lid.begin(), lid.end(), [&points](size_t l, size_t r) {
return points[l] < points[r];
});
// 下側
for (int i=0; i<n; i++) {
while (k > 1 && cross(points[_ans[k-1]] - points[_ans[k-2]], points[lid[(size_t)i]] - points[_ans[k-1]]) <= 0)
k--;
_ans[k] = lid[(size_t)i];
k++;
}
// 上側
for (int i=n-2, s=(int)k; i>=0; i--) {
while ((int)k > s && cross(points[_ans[k-1]] - points[_ans[k-2]], points[lid[(size_t)i]] - points[_ans[k-1]]) <= 0)
k--;
_ans[k] = lid[(size_t)i];
k++;
}
for (size_t i=0; i<k-1; i++) ans.push_back((int)_ans[i]);
return (int)k-1;
}
// 円を表す構造体
struct circle {
point_t c; // center
value_t r; // radius
};
pair<point_t, point_t> cross_point(circle a, circle b) {
point_t vec_ab = b.c - a.c;
value_t d = abs(vec_ab);
value_t theta = arg(vec_ab);
value_t alpha = acos((a.r * a.r + d * d - b.r * b.r) / (2 * a.r * d));
point_t p1 = a.c + polar(a.r, theta + alpha);
point_t p2 = a.c + polar(a.r, theta - alpha);
return {p1, p2};
}
} // namespace geometry2d
/* snippet ends */
// #define CONVEX_HULL // https://atcoder.jp/contests/agc021/tasks/agc021_b
#define CROSS_POINT // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E&lang=ja
int main(){
#ifdef CONVEX_HULL
int n;
cin >> n;
vector<geometry2d::point_t> points(n);
for (auto &pi:points) cin >> pi;
vector<int> ch;
int m = geometry2d::convex_hull(points, ch);
double ans[n];
fill(ans, ans+n, 0);
geometry2d::point_t prev = points[ch[m-1]];
for (int i=0; i<m; i++) {
geometry2d::point_t cur = points[ch[i]];
geometry2d::point_t next = points[ch[(i+1)%m]];
ans[ch[i]] = acos(geometry2d::cos_between(cur - prev, next - cur)) / (2 * M_PI);
prev = cur;
}
for (auto &ai:ans) cout << fixed << setprecision(15) << ai << '\n';
#endif
#ifdef CROSS_POINT
geometry2d::circle a, b;
cin >> a.c >> a.r >> b.c >> b.r;
auto p = geometry2d::cross_point(a, b);
if (p.first > p.second) swap(p.first, p.second);
cout << p.first << ' ' << p.second << '\n';
#endif
return 0;
}
| 0 |
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll n,k;
int main(){
cin>>n>>k;
ll ans=0;
for (ll b=n;b>k;b--){
ll l=k,r=b-1;
ll c=(n+1)/b;
ans+=c*(r-l+1);
if (l==0) ans--;
l=k+c*b;
if (l<=n){
ans+=n-l+1;
}
}
cout<<ans;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int zero_fact(int n) {
int x = 5;
int zero = 0;
while (x <= n) {
zero += n / x;
x *= 5;
}
return zero;
}
int main() {
vector<int> v;
int m;
cin >> m;
for (int i = 5;; i++) {
if (zero_fact(i) == m) {
v.push_back(i);
}
if (zero_fact(i) > m) break;
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const long long LB = 0;
const long long RB = (1LL << 60LL) - 1;
vector<tuple<int, long long, long long>> reduced[2];
vector<pair<long long, long long>> all[2][65];
inline void insert(int id, int depth, long long l, long long r, long long qs,
long long qe) {
if (l > qe || r < qs) {
return;
}
all[id][depth].push_back({l, r});
if (l >= qs && r <= qe) {
reduced[id].push_back(make_tuple(depth, l, r));
return;
}
long long mid = (l + r) / 2;
insert(id, depth - 1, l, mid, qs, qe);
insert(id, depth - 1, mid + 1, r, qs, qe);
}
int main() {
int na;
cin >> na;
for (int i = 0; i < na; i++) {
long long l, r;
cin >> l >> r;
insert(0, 60, LB, RB, l, r);
}
int nb;
cin >> nb;
for (int i = 0; i < nb; i++) {
long long l, r;
cin >> l >> r;
insert(1, 60, LB, RB, l, r);
}
vector<pair<long long, long long>> unnormalized;
for (int cur = 0; cur < 2; cur++) {
for (auto [depth, l1, r1] : reduced[cur]) {
for (auto [l2, r2] : all[1 - cur][depth]) {
long long l_res = (l1 ^ l2) & (RB ^ ((1LL << depth) - 1));
long long r_res = l_res + (1LL << depth) - 1;
unnormalized.push_back({l_res, r_res});
}
}
}
assert(unnormalized.size() <= 100 * 100 * 60 * 8);
sort(unnormalized.begin(), unnormalized.end());
vector<pair<long long, long long>> normalized_intervals;
for (auto [l, r] : unnormalized) {
if (normalized_intervals.empty() ||
normalized_intervals.back().second < l) {
normalized_intervals.push_back({l, r});
} else {
normalized_intervals.back().second =
max(normalized_intervals.back().second, r);
}
}
long long sol = 0;
for (auto [l, r] : normalized_intervals) {
long long reduced_r = r % MOD;
long long reduced_l = l % MOD;
sol += (((reduced_r * (reduced_r + 1)) / 2) % MOD);
sol %= MOD;
sol -= (((reduced_l * (reduced_l - 1)) / 2) % MOD);
sol += MOD;
sol %= MOD;
}
cout << sol << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[101], i, d = 0, mx = 30000;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 2; i <= n; i++) {
d = max(d, a[i] - a[i - 1]);
}
for (i = 2; i < n; i++) {
mx = min(mx, max(d, a[i + 1] - a[i - 1]));
}
cout << mx;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int ar[n+3];
for(int i=0;i<n;i++)cin>>ar[i];
sort(ar,ar+n);
int sum=0;
for(int i=0;i<m;i++)sum+=ar[i];
cout<<sum<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int t,n;
cin>>t;
while(t--){
int o=1,e=2,l,i=0;
cin>>n;
l=n*n;
if(n==1) cout<<'1'<<endl;
else if(n==2)cout<<"-1"<<endl;
else{
while(o<=l){
cout<<o<<' ';
i++;
if(i==n) {
cout<<endl;
i=0;
}
o=o+2;
}
while(e<=l){
cout<<e<<' ';
i++;
if(i==n) {
cout<<endl;
i=0;
}
e=e+2;
}
}
}
} | 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T sqr(T a) {
return a * a;
};
template <typename T>
void dprint(T begin, T end) {
for (auto i = begin; i != end; i++) cerr << (*i) << " ";
cerr << "\n";
};
const int maxn = 2500 + 10;
int n, m, q, hs;
map<pair<pair<int, int>, pair<int, int> >, long long> mp;
struct bit {
long long a[maxn][maxn];
inline int lowbit(int x) { return x & (-x); }
inline void add(int x, int y, long long t) {
int i, j;
for (i = x; i < maxn; i += lowbit(i)) {
for (j = y; j < maxn; j += lowbit(j)) a[i][j] += t;
}
}
inline long long get(int x, int y) {
long long ans = 0;
int i, j;
for (i = x; i > 0; i -= lowbit(i)) {
for (j = y; j > 0; j -= lowbit(j)) ans += a[i][j];
}
return ans;
}
} a, b, c, d;
inline void add(int x1, int y1, int x2, int y2, long long t) {
a.add(x1, y1, t), a.add(x1, y2 + 1, -t);
a.add(x2 + 1, y1, -t), a.add(x2 + 1, y2 + 1, t);
b.add(x1, y1, t * x1);
b.add(x2 + 1, y1, -t * (x2 + 1));
b.add(x1, y2 + 1, -t * x1);
b.add(x2 + 1, y2 + 1, t * (x2 + 1));
c.add(x1, y1, t * y1);
c.add(x2 + 1, y1, -t * y1);
c.add(x1, y2 + 1, -t * (y2 + 1));
c.add(x2 + 1, y2 + 1, t * (y2 + 1));
d.add(x1, y1, t * x1 * y1);
d.add(x2 + 1, y1, -t * (x2 + 1) * y1);
d.add(x1, y2 + 1, -t * x1 * (y2 + 1));
d.add(x2 + 1, y2 + 1, t * (x2 + 1) * (y2 + 1));
}
inline long long get(int x, int y) {
return a.get(x, y) * (x + 1) * (y + 1) - b.get(x, y) * (y + 1) -
(x + 1) * c.get(x, y) + d.get(x, y);
}
inline long long get(int x1, int y1, int x2, int y2) {
return get(x2, y2) - get(x2, y1 - 1) - get(x1 - 1, y2) + get(x1 - 1, y1 - 1);
}
inline long long cal(int a, int b, int c, int d) {
if (mp.find(make_pair(pair<int, int>(a, b), pair<int, int>(c, d))) !=
mp.end())
return mp[make_pair(pair<int, int>(a, b), pair<int, int>(c, d))];
else
return mp[make_pair(pair<int, int>(a, b), pair<int, int>(c, d))] =
rand() << 15 | rand();
}
int main() {
std::srand(std::time(0));
scanf("%d%d%d", &n, &m, &q);
int t, r1, r2, c1, c2;
for (int i = 0; i < q; i++) {
scanf("%d", &t);
scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
if (t == 1) {
add(r1, c1, r2, c2, cal(r1, c1, r2, c2));
} else if (t == 2) {
add(r1, c1, r2, c2, -cal(r1, c1, r2, c2));
} else {
if (get(r1, c1, r1, c1) == get(r2, c2, r2, c2))
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int h, c, t;
cin >> h >> c >> t;
if (h + c >= 2 * t) {
cout << "2\n";
} else {
int k = (h - t) / (2 * t - h - c);
if (abs((k + 1) * h + k * c - t * (2 * k + 1)) * (2 * k + 3) >
abs((k + 2) * h + (k + 1) * c - t * (2 * k + 3)) * (2 * k + 1))
cout << 2 * (k + 1) + 1 << '\n';
else
cout << 2 * k + 1 << '\n';
}
}
}
| 3 |
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<stack>
using namespace std;
typedef long long ll;
#define N 1000015
const int p=1e9+7;
int n,dp[N],s[N];
int main()
{
scanf("%d",&n);
dp[n]=n;s[n]=n;
dp[n-1]=1ll*n*n%p;s[n-1]=(s[n]+dp[n-1])%p;
for(int i=n-2;i>0;i--)
{
dp[i]=(dp[i]+dp[i+1])%p;
dp[i]=(dp[i]+1ll*(n-1)*(n-1)%p)%p;
dp[i]=(dp[i]+s[i+3])%p;
dp[i]=(dp[i]+i+1)%p;
s[i]=(s[i+1]+dp[i])%p;
}
printf("%d\n",dp[1]);
}
| 0 |
#include <iostream>
using namespace std;int main(){string s;long n;cin>>n;while(n--){s=char(97+n%26)+s;n/=26;}cout<<s;} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string inp;
cin >> inp;
int elt1 = inp[0] - 64, elt2 = inp[1] - 64;
bool out = true;
for (int i = 2; i < inp.size(); i++) {
if ((elt1 + elt2) % 26 == (inp[i] - 64 + 1) % 26) {
elt1 = elt2;
elt2 = inp[i] - 64;
} else {
out = false;
break;
}
}
out ? cout << "YES\n" : cout << "NO\n";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct RTC {
~RTC() { cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC << " seconds\n"; }
} runtimecount;
int n, m;
string tab[60];
bool vis[60][60];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
bool dfs(int i, int j, int pi, int pj, char col) {
vis[i][j] = true;
bool ans = false;
for (int k = 0; k < 4; k++) {
int ii = i + dx[k];
int jj = j + dy[k];
if (ii < 0 || ii >= n || jj < 0 || jj >= m || (ii == pi && jj == pj) ||
tab[ii][jj] != col)
continue;
if (!vis[ii][jj])
ans |= dfs(ii, jj, i, j, col);
else
ans = true;
if (ans) break;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < (n); i++) cin >> tab[i];
memset(vis, false, sizeof(vis));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!vis[i][j])
if (dfs(i, j, -1, -1, tab[i][j])) {
cout << "Yes" << endl;
return 0;
}
cout << "No" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 350000 + 100;
int data[maxn];
int dp[2][maxn];
int hash1[maxn];
int tree[maxn * 4];
int flag[maxn * 4];
int prev_pos[maxn];
void inc(int root, int l, int r, int a, int b, int delta) {
if (a == l && b == r) {
flag[root] += delta;
tree[root] += delta;
return;
}
const int lc = root << 1, rc = lc + 1, mid = l + r >> 1;
if (flag[root] != 0) {
const int t = flag[root];
flag[lc] += t, tree[lc] += t, flag[rc] += t, tree[rc] += t, flag[root] = 0;
}
if (b <= mid)
inc(lc, l, mid, a, b, delta);
else if (a > mid)
inc(rc, mid + 1, r, a, b, delta);
else {
inc(lc, l, mid, a, mid, delta);
inc(rc, mid + 1, r, mid + 1, b, delta);
}
tree[root] = max(tree[lc], tree[rc]);
}
int query(int root, int l, int r, int a, int b) {
if (l == a && r == b) {
return tree[root];
}
const int lc = root << 1, rc = lc + 1, mid = l + r >> 1;
if (flag[root] != 0) {
const int t = flag[root];
flag[lc] += t, tree[lc] += t, flag[rc] += t, tree[rc] += t, flag[root] = 0;
}
if (b <= mid) return query(lc, l, mid, a, b);
if (a > mid) return query(rc, mid + 1, r, a, b);
const int u = query(lc, l, mid, a, mid);
const int v = query(rc, mid + 1, r, mid + 1, b);
return max(u, v);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", data + i);
int* from = dp[0];
int* to = dp[1];
for (int x = 1; x <= k; ++x) {
if (x == 1) {
int v = 0;
for (int i = 1; i <= n; ++i) {
int orz = hash1[data[i]];
if (orz == 0) {
++v;
}
to[i] = v;
prev_pos[i] = orz;
hash1[data[i]] = i;
}
} else {
memset(tree, 0, sizeof tree);
memset(flag, 0, sizeof flag);
for (int i = x; i <= n; ++i) {
const int me = from[i - 1];
const int pos = prev_pos[i];
inc(1, 1, n, i - 1, i - 1, me);
inc(1, 1, n, max(x - 1, pos), i - 1, 1);
to[i] = query(1, 1, n, x - 1, i - 1);
}
}
swap(from, to);
}
printf("%d\n", from[n]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a;
cin >> n;
bool flag = 1;
for (int i = 0; i < n; i++) {
scanf("%d", &a);
if (a == 1) flag = 0;
}
if (flag)
cout << "EASY" << endl;
else
cout << "HARD" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int t = 1;
cin >> t;
for (int w = 1; w <= t; w++) {
long long int n, k;
cin >> n >> k;
if ((n % 2 == 0 && k % 2 != 0) || k * k > n || (n % 2 != 0 && k % 2 == 0))
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int INF = (int)1e9 + 7;
const long long LINF = (long long)1e18 + 7;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5 + 7;
const double EPS = (double)1e-6;
void file() {}
string A[1007];
vector<bool> G[1007][1007];
int n, m;
void dijkstra(int x, int y, int xk, int yk) {
priority_queue<pair<pair<int, int>, pair<int, int> > > Q;
vector<vector<vector<int> > > d(
1007, vector<vector<int> >(1007, vector<int>(4, INF)));
d[x][y][0] = 0;
d[x][y][1] = 1;
d[x][y][2] = 2;
d[x][y][3] = 3;
Q.push(make_pair(make_pair(0, 0), make_pair(x, y)));
Q.push(make_pair(make_pair(-1, 1), make_pair(x, y)));
Q.push(make_pair(make_pair(-2, 2), make_pair(x, y)));
Q.push(make_pair(make_pair(-3, 3), make_pair(x, y)));
vector<bool> ff(4), ffc(4);
while (!Q.empty()) {
if (*max_element(d[xk][yk].begin(), d[xk][yk].end()) != INF) {
printf("%d", *min_element(d[xk][yk].begin(), d[xk][yk].end()));
exit(0);
}
auto V = Q.top();
Q.pop();
int kx = V.second.first, ky = V.second.second;
int time = -V.first.first, kolp = V.first.second;
for (int i = 0; i < (int)4; ++i) {
int ko = (kolp + i) % 4;
for (int j = 0; j < (int)4; ++j) {
ff[j] = G[kx][ky][(j - ko + 4) % 4];
}
if (kx - 1 >= 0) {
for (int j = 0; j < (int)4; ++j) {
ffc[j] = G[kx - 1][ky][(j - ko + 4) % 4];
}
if (ffc[2] && ff[0]) {
if (d[kx - 1][ky][ko] > time + 1) {
d[kx - 1][ky][ko] = time + 1;
Q.push(make_pair(make_pair(-time - 1, ko), make_pair(kx - 1, ky)));
}
}
}
if (ky + 1 < m) {
for (int j = 0; j < (int)4; ++j) {
ffc[j] = G[kx][ky + 1][(j - ko + 4) % 4];
}
if (ffc[3] && ff[1]) {
if (d[kx][ky + 1][ko] > time + 1) {
d[kx][ky + 1][ko] = time + 1;
Q.push(make_pair(make_pair(-time - 1, ko), make_pair(kx, ky + 1)));
}
}
}
if (kx + 1 < n) {
for (int j = 0; j < (int)4; ++j) {
ffc[j] = G[kx + 1][ky][(j - ko + 4) % 4];
}
if (ffc[0] && ff[2]) {
if (d[kx + 1][ky][ko] > time + 1) {
d[kx + 1][ky][ko] = time + 1;
Q.push(make_pair(make_pair(-time - 1, ko), make_pair(kx + 1, ky)));
}
}
}
if (ky - 1 >= 0) {
for (int j = 0; j < (int)4; ++j) {
ffc[j] = G[kx][ky - 1][(j - ko + 4) % 4];
}
if (ffc[1] && ff[3]) {
if (d[kx][ky - 1][ko] > time + 1) {
d[kx][ky - 1][ko] = time + 1;
Q.push(make_pair(make_pair(-time - 1, ko), make_pair(kx, ky - 1)));
}
}
}
++time;
}
}
if (*min_element(d[xk][yk].begin(), d[xk][yk].end()) == INF) {
puts("-1");
} else {
printf("%d", *min_element(d[xk][yk].begin(), d[xk][yk].end()));
}
}
int main() {
file();
scanf("%d%d", &n, &m);
getline(cin, A[0]);
for (int i = 0; i < (int)n; ++i) {
getline(cin, A[i]);
}
for (int i = 0; i < (int)n; ++i) {
for (int j = 0; j < (int)m; ++j) {
G[i][j].resize(4);
}
}
for (int i = 0; i < (int)n; ++i) {
for (int j = 0; j < (int)m; ++j) {
if (A[i][j] == '+' || A[i][j] == '|' || A[i][j] == '^' ||
('A' < A[i][j] && A[i][j] < 'Z' && A[i][j] != 'U')) {
G[i][j][0] = 1;
} else {
G[i][j][0] = 0;
}
if (A[i][j] == '+' || A[i][j] == '-' || A[i][j] == '>' ||
('A' < A[i][j] && A[i][j] < 'Z' && A[i][j] != 'R')) {
G[i][j][1] = 1;
} else {
G[i][j][1] = 0;
}
if (A[i][j] == '+' || A[i][j] == '|' || A[i][j] == 'v' ||
('A' < A[i][j] && A[i][j] < 'Z' && A[i][j] != 'D')) {
G[i][j][2] = 1;
} else {
G[i][j][2] = 0;
}
if (A[i][j] == '+' || A[i][j] == '-' || A[i][j] == '<' ||
('A' < A[i][j] && A[i][j] < 'Z' && A[i][j] != 'L')) {
G[i][j][3] = 1;
} else {
G[i][j][3] = 0;
}
}
}
int xt, yt, xm, ym;
scanf("%d%d", &xt, &yt);
scanf("%d%d", &xm, &ym);
--xt, --yt, --xm, --ym;
dijkstra(xt, yt, xm, ym);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 100100;
const int N = 15;
const int M = (1 << 14) + 7;
bool g[N][N];
int n, m;
int edgesToMask[N][M];
vector<int> pathInMask[M][N][N];
int dp[M];
int par[M][3];
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int v, u;
scanf("%d%d", &v, &u);
v--;
u--;
g[v][u] = g[u][v] = 1;
}
for (int v = 0; v < n; v++)
for (int mask = 0; mask < (1 << n); mask++)
for (int u = 0; u < n; u++) {
if (((mask >> u) & 1) == 0) continue;
if (g[v][u]) edgesToMask[v][mask]++;
}
for (int v = 0; v < n; v++) pathInMask[1 << v][v][v].push_back(v);
for (int mask = 0; mask < (1 << n); mask++) {
for (int v = 0; v < n; v++)
for (int u = 0; u < n; u++) {
if (pathInMask[mask][v][u].empty()) continue;
for (int w = 0; w < n; w++) {
if ((mask >> w) & 1) continue;
if (!g[u][w]) continue;
int nmask = mask | (1 << w);
if (!pathInMask[nmask][v][w].empty()) continue;
pathInMask[nmask][v][w] = pathInMask[mask][v][u];
pathInMask[nmask][v][w].push_back(w);
}
}
}
for (int mask = 0; mask < (1 << n); mask++) dp[mask] = INF;
dp[1] = 0;
for (int mask = 0; mask < (1 << n); mask++) {
if (dp[mask] == INF) continue;
for (int v = 0; v < n; v++) {
if ((mask >> v) & 1) continue;
if (edgesToMask[v][mask] >= 2) {
int nmask = mask | (1 << v);
if (dp[nmask] > dp[mask] + 2) {
dp[nmask] = dp[mask] + 2;
par[nmask][0] = mask;
par[nmask][1] = par[nmask][2] = v;
}
}
if (edgesToMask[v][mask] == 0) continue;
for (int u = v + 1; u < n; u++) {
if ((mask >> u) & 1) continue;
if (edgesToMask[u][mask] == 0) continue;
int all = ((1 << n) - 1) ^ mask ^ (1 << v) ^ (1 << u);
for (int smask = all;; smask = (smask - 1) & all) {
int nmask = smask | (1 << v) | (1 << u);
if (!pathInMask[nmask][v][u].empty()) {
int w = dp[mask] + (int)pathInMask[nmask][v][u].size() + 1;
if (w < dp[mask | nmask]) {
dp[mask | nmask] = w;
par[mask | nmask][0] = mask;
par[mask | nmask][1] = v;
par[mask | nmask][2] = u;
}
}
if (smask == 0) break;
}
}
}
}
printf("%d\n", dp[(1 << n) - 1]);
int mask = (1 << n) - 1;
while (mask > 1) {
int nmask = par[mask][0];
int v = par[mask][1];
int u = par[mask][2];
if (v == u) {
int cnt = 0;
for (int z = 0; cnt < 2 && z < n; z++) {
if (((nmask >> z) & 1) == 0) continue;
if (!g[v][z]) continue;
cnt++;
printf("%d %d\n", v + 1, z + 1);
}
if (cnt != 2) throw;
} else {
for (int z = 0; z < n; z++) {
if (((nmask >> z) & 1) == 0) continue;
if (!g[v][z]) continue;
printf("%d %d\n", v + 1, z + 1);
break;
}
for (int z = 0; z < n; z++) {
if (((nmask >> z) & 1) == 0) continue;
if (!g[u][z]) continue;
printf("%d %d\n", u + 1, z + 1);
break;
}
vector<int> z = pathInMask[mask ^ nmask][v][u];
for (int i = 0; i < (int)z.size() - 1; i++) {
printf("%d %d\n", z[i] + 1, z[i + 1] + 1);
}
}
mask = nmask;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define INF 100000000
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100000
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define SI 2002
#define ADD(X,Y) (X) = ((X)+(Y))%MOD
int n, k;
ll dp[SI][SI];
int main(){
cin >> n >> k;
if(n == 1){
cout << 1 << endl;
return 0;
}
for(int i = 2; i <= n; i++) dp[1][i] = 1;
for(int i = 2; i <= k-1; i++){
ll sum = 0;
for(int j = n; j >= 2; j--){
if(i <= n-j+1) ADD(dp[i][j],dp[i-1][j]);
ADD(dp[i][j],sum);
ADD(sum,dp[i-1][j]);
}
}
ll ans = 0;
if(k == 1){
ans = 1;
} else{
for(int i = 2; i <= n; i++) ADD(ans,dp[k-1][i]);
}
rep(i,n-k-1) ADD(ans,ans);
cout << ans << endl;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int N = 200000;
int h[200005],n;
long long a[200005],BIT[200005];
int LS(int x){
return x&-x;
}
void update(int x, long long val){
for(int i = x; i<=N;i+=LS(i)){
BIT[i] = max(BIT[i],val);
}
}
long long query(int x){
long long ans = 0;
for(int i= x; i>=1;i-=LS(i)){
ans = max(ans,BIT[i]);
}
return ans;
}
int main(){
memset(BIT,0,sizeof BIT);
scanf("%d",&n);
for (int i = 0;i <n;i++){
scanf("%d",&h[i]);
}
for(int i = 0;i <n;i++){
scanf("%d",&a[i]);
}
long long ans = 0;
for (int i = 0;i<n;i++){
int val = h[i];
long long maxi = query(val);
update(val,a[i]+maxi);
ans = max(ans, a[i]+maxi);
}
printf("%lld\n",ans);
return 0;
} | 0 |
#include<bits/stdc++.h>
#define mx 100005
#define md 1000000007
using namespace std;
map<string, int> mp;
string s;
long long n, ans;
int main(){
cin >> n >> s;
for(int i = 0; i < 2; i++){
for(int j = 0; j < (1<<n); j++){
int l = 0, r = n;
char t[20]; t[n+1] = '\0';
for(int k = 0; k < n; k++){
if((j>>k)&1) t[l++] = s[i ? 2*n-1-k : k];
else t[r--] = s[i ? 2*n-1-k : k];
}
t[l] = 'A';
if(i) ans += mp[(string)t];
else mp[(string)t]++;
}
}
printf("%lld\n", ans);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000006;
bool vis[N];
int n, k, a[N];
vector<int> v;
int dfs(int x) {
vis[x] = 1;
if (vis[a[x]]) return 1;
return 1 + dfs(a[x]);
}
bitset<1000006> cur;
map<int, int> mp;
int getmin() {
cur.set(0);
for (int x : v) ++mp[x];
for (auto& t : mp) {
for (int i = 1; i <= t.second; i <<= 1) {
t.second -= i;
cur |= cur << (t.first * i);
if (cur[k]) return k;
}
if (t.second) {
cur |= cur << (t.first * t.second);
if (cur[k]) return k;
}
}
return k + 1;
}
int getmax() {
int tmp, ans = 0;
for (int& x : v) {
tmp = min(x >> 1, k);
ans += tmp << 1;
k -= tmp;
x -= tmp << 1;
if (!k) break;
}
if (k) {
for (int& x : v)
if (x) {
++ans;
--k;
if (!k) break;
}
}
return ans;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
for (int i = 1; i <= n; ++i)
if (!vis[i]) v.push_back(dfs(i));
sort(v.begin(), v.end());
int m = getmin();
int M = getmax();
printf("%d %d\n", m, M);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long first = 0;
long long second = 0;
bool isSame = true;
bool cheek = true;
long long dif;
for (int i = 0; i < n; i++) {
long long value;
cin >> value;
if (i == 0)
dif = value;
else if (dif != value)
isSame = false;
if (value > first) {
second = first;
first = value;
} else if (value > second && (value != first)) {
second = value;
}
if ((first != 0 && second != 0) && !(first / 2 >= second)) {
cheek = false;
}
}
bool flag = true;
if (!(first / 2 >= second)) {
flag = false;
}
if (first == 1 && second == 1) {
flag = true;
}
if (isSame) {
cout << "NO\n";
} else {
if (flag && cheek) {
cout << "NO\n";
} else {
cout << "YES\n";
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
template <typename T>
void MACRO_VAR_Scan(T& t) {
std::cin >> t;
}
template <typename First, typename... Rest>
void MACRO_VAR_Scan(First& first, Rest&... rest) {
std::cin >> first;
MACRO_VAR_Scan(rest...);
}
template <typename T>
void MACRO_VEC_ROW_Init(int n, T& t) {
t.resize(n);
}
template <typename First, typename... Rest>
void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) {
first.resize(n);
MACRO_VEC_ROW_Init(n, rest...);
}
template <typename T>
void MACRO_VEC_ROW_Scan(int p, T& t) {
std::cin >> t[p];
}
template <typename First, typename... Rest>
void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) {
std::cin >> first[p];
MACRO_VEC_ROW_Scan(p, rest...);
}
template <class T>
inline T CHMAX(T& a, const T b) {
return a = (a < b) ? b : a;
}
template <class T>
inline T CHMIN(T& a, const T b) {
return a = (a > b) ? b : a;
}
template <class T>
std::vector<std::vector<T>> VV(int n, int m, T init = T()) {
return std::vector<std::vector<T>>(n, std::vector<T>(m, init));
}
template <typename S, typename T>
std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {
os << "(" << p.first << ", " << p.second << ")";
return os;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using PAIR = std::pair<ll, ll>;
using PAIRLL = std::pair<ll, ll>;
constexpr ll INFINT = 1 << 30;
constexpr ll INFINT_LIM = (1LL << 31) - 1;
constexpr ll INFLL = 1LL << 60;
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);
constexpr double EPS = 1e-10;
constexpr ll MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;
template <class T, size_t N>
void FILL(T (&a)[N], const T& val) {
for (auto& x : a) x = val;
}
template <class ARY, size_t N, size_t M, class T>
void FILL(ARY (&a)[N][M], const T& val) {
for (auto& b : a) FILL(b, val);
}
template <class T>
void FILL(std::vector<T>& a, const T& val) {
for (auto& x : a) x = val;
}
template <class ARY, class T>
void FILL(std::vector<std::vector<ARY>>& a, const T& val) {
for (auto& b : a) FILL(b, val);
}
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
;
ll n;
MACRO_VAR_Scan(n);
;
std::set<ll> sell, buy, unknown;
ll ans = 1;
for (ll _ = 0; _ < ll(n); ++_) {
std::string type;
MACRO_VAR_Scan(type);
;
ll p;
MACRO_VAR_Scan(p);
;
if (type.back() == 'D') {
if (!sell.empty() && *sell.begin() < p)
sell.insert(p);
else if (!buy.empty() && p < *buy.rbegin())
buy.insert(p);
else
unknown.insert(p);
} else {
if (sell.count(p)) {
if (p != *sell.begin()) {
ans = 0;
break;
}
sell.erase(p);
} else if (buy.count(p)) {
if (p != *buy.rbegin()) {
ans = 0;
break;
}
buy.erase(p);
} else if (unknown.count(p)) {
(ans *= 2) %= MOD;
unknown.erase(p);
}
for (auto& x : unknown) {
if (x < p)
buy.insert(x);
else if (p < x)
sell.insert(x);
}
unknown.clear();
}
}
(ans *= (ll)unknown.size() + 1) %= MOD;
std::cout << (ans);
std::cout << "\n";
;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[101], i, j = 0, k = 1, buf[101 - 1], n;
cin >> n;
for (i = 1; i < n + 1; i++) a[i] = i;
for (i = 1; i < n + 1; i++) {
k += i;
if (k % n == 0)
buf[j] = n;
else
buf[j] = k % n;
j++;
}
for (i = 0; i < n - 1; i++) cout << buf[i] << " ";
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){
ll a=0,b=getchar(),c=1;
while(!isdigit(b))c=b=='-'?-1:1,b=getchar();
while(isdigit(b))a=a*10+b-'0',b=getchar();
return a*c;
}
ll n,m,ans,a[100005],b[200005];
ll check(ll x){
ll res=0;
for(int i=0;i<n;i++){
ll k=lower_bound(a,a+n,x-a[i])-a;
res+=n-k;
}
return res;
}
int main(){
n=read(),m=read();
for(int i=0;i<n;i++)
a[i]=read();
sort(a,a+n),b[0]=a[0];
for(int i=0;i<n;i++)
b[i+1]=a[i]+b[i];
ll l=0,r=200001;
while(l<r-1){
ll mid=(l+r)>>1;
if(check(mid)>=m)l=mid;
else r=mid;
}
ll res=0;
for(int i=0;i<n;i++){
ll k=lower_bound(a,a+n,l+1-a[i])-a;
res+=n-k,ans+=b[n]-b[k]+a[i]*(n-k);
}
ans+=(m-res)*l;
printf("%lld",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef struct {
long long int len, sz;
} Box;
bool cmp(Box x, Box y) { return (x.len < y.len); }
long long int Pow(int base, long long int p) {
int ans = 1;
for (int i = 0; i < p; i++) {
ans *= base;
if (ans > 1e9) return 1e9 + 1;
}
return ans;
}
int main() {
int n;
Box a[100005];
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].len >> a[i].sz;
}
sort(a + 1, a + n + 1, cmp);
long long int t = a[n].len + 1;
for (long long int i = t; i <= t + 100; i++) {
bool flag = true;
for (int j = 1; j <= n; j++) {
if (a[j].sz > Pow(4, i - a[j].len)) {
flag = false;
break;
}
}
if (flag == true) {
cout << i << endl;
return 0;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int check = 0;
if (n == 1)
cout << -1 << endl;
else {
cout << 6;
for (long long int i = 1; i < n; i++) {
cout << 7;
}
cout << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 2e5 + 10;
int n, a[MOD], root[MOD];
bool bVisit[MOD];
int oneroot = -1;
int ans = 0;
int findroot(int x) {
int f = a[x];
if (f == x) {
if (x != oneroot) {
a[x] = oneroot;
ans++;
}
return a[x];
}
if (!bVisit[f]) {
bVisit[f] = true;
return root[f] = findroot(f);
}
if (root[f] == -1) {
if (oneroot == -1) {
oneroot = x;
}
a[x] = oneroot;
ans++;
return a[x];
}
return root[f];
}
int main() {
int i;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
if (i == a[i]) oneroot = i;
root[i] = -1;
bVisit[i] = false;
}
for (i = 1; i <= n; i++) {
if (bVisit[i]) continue;
bVisit[i] = true;
root[i] = findroot(i);
}
cout << ans << endl;
for (i = 1; i <= n; i++) cout << a[i] << " ";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,k,count=0;
cin>>n>>k;
for(int i=0;i<n;i++){
int x;
cin>>x;
if(k<=x) count++;
}cout<<count<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
int main() {
int n, k;
scanf("%d%d", &n, &k);
k = min(n / 2, k);
long long sum = 0;
for (int i = 0, j = 1; i < k; i++, j += 2) sum += 2 * (n - j) - 1;
printf("%lld\n", sum);
return 0;
}
| 2 |
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;
int solve(string& s, int& i)
{
int a = 0;
int b = 0;
char prev = '+';
for(;;){
int x = 0;
if(s[i] == '('){
++ i;
x += solve(s, i);
++ i;
}else{
while('0' <= s[i] && s[i] <= '9'){
x *= 10;
x += s[i] - '0';
++ i;
}
}
if(prev == '+'){
a += b;
b = x;
}else if(prev == '-'){
a += b;
b = -x;
}else if(prev == '*'){
b *= x;
}else{
b /= x;
}
if(s[i] == ')' || s[i] == '=')
break;
prev = s[i];
++ i;
}
return a + b;
}
int main()
{
int n;
cin >> n;
while(--n >= 0){
string s;
cin >> s;
int i = 0;
cout << solve(s, i) << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline string getStr() {
char ch[500];
scanf("%s", ch);
return ch;
}
const long long MOD = 1e9 + 7;
const long long base = 29;
const long long MAX_N = 2e3 + 10;
const long long MAX_T = MAX_N;
inline long long divide(long long a, long long b) { return (a + b - 1) / b; }
long double dp[MAX_T][MAX_N];
long double sum = 0;
int32_t main() {
dp[0][0] = 1;
long long n, t;
double p;
cin >> n >> p >> t;
for (long long i = 0; i < t; i++) {
for (long long x = 0; x <= min(i, n); x++) {
if (x < n) {
dp[i + 1][x + 1] += (double(dp[i][x])) * p;
dp[i + 1][x] += (double(dp[i][x])) * (1.0 - p);
} else
dp[i + 1][x] += dp[i][x];
}
}
for (long long i = 0; i <= n; i++) {
sum += dp[t][i] * i;
}
cout.precision(10);
cout << fixed << sum << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long ans, dis[2020], a[2020][2020];
int n, frm, tot, b[2020], k[2020], x[2020], y[2020], pre[2020];
bool flag[2020];
int main() {
memset(flag, false, sizeof(flag));
memset(dis, 0x3f, sizeof(dis));
memset(a, 0x3f, sizeof(a));
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &a[0][i]);
for (int i = 1; i <= n; i++) scanf("%d", &k[i]);
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
a[i][j] = a[j][i] = ((long long)(abs(x[i] - x[j]) + abs(y[i] - y[j]))) *
((long long)(k[i] + k[j]));
dis[0] = 0, frm = 0, flag[0] = true;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (flag[j] == false && a[frm][j] < dis[j])
pre[j] = frm, dis[j] = a[frm][j];
long long min = 1e16;
for (int j = 1; j <= n; j++)
if (flag[j] == false && dis[j] < min) min = dis[j], frm = j;
ans += min, flag[frm] = true;
}
printf("%lld\n", ans);
for (int i = 1; i <= n; i++)
if (pre[i] == 0) b[++tot] = i;
printf("%d\n", tot);
for (int i = 1; i <= tot; i++) printf("%d%c", b[i], (i != tot) ? ' ' : '\n');
printf("%d\n", n - tot);
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if (pre[j] == i || pre[i] == j) printf("%d %d\n", i, j);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
const int NIL = -1;
static mt19937 _g(time(nullptr));
inline ll randint(ll a, ll b) {
ll w = (_g() << 31LL) ^ _g();
return a + w % (b - a + 1);
}
inline void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
};
template <typename T>
inline T sign(T x) {
return T(x > 0) - T(x < 0);
}
template <typename T, typename S>
inline ostream& operator<<(ostream& os, const pair<T, S> p) {
cout << "[" << p.first << ";" << p.second << "]";
return os;
}
template <typename T>
inline ostream& operator<<(ostream& os, const vector<T>& v) {
for (auto el : v) cout << el << " ";
return os;
}
template <typename T>
inline T fetch() {
T ret;
cin >> ret;
return ret;
}
template <typename T>
inline void goodbye(const T& val) {
cout << val << '\n';
exit(0);
}
const int MAX_N = (int)1e6 + 777;
const int MOD = (int)1e9 + 7;
int add(int x, int y) { return (x + y >= MOD ? x + y - MOD : x + y); }
int mult(int x, int y) { return (x * 1LL * y) % MOD; }
vector<pii> g[MAX_N];
int n, m, last;
int dist[MAX_N], __Abacaba[MAX_N], head = 0;
int answ[MAX_N], met[MAX_N];
vector<int> layers[MAX_N];
int cls[MAX_N];
pii pr[MAX_N];
void add_edge(int v, int u, int digit) { g[v].emplace_back(u, digit); }
void build_long_edge(int v, int u, int id) {
vector<int> digits;
for (; id > 0; id /= 10) digits.push_back(id % 10);
reverse(digits.begin(), digits.end());
vector<int> path = {v};
for (int i = 0; i < ((int)(digits).size()) - 1; ++i) path.push_back(last++);
path.push_back(u);
for (int i = 0; i + 1 < ((int)(path).size()); ++i)
add_edge(path[i], path[i + 1], digits[i]);
}
void run_bfs(int src) {
for (int v = 0; v < last; ++v) dist[v] = INT_INF;
dist[src] = 0;
__Abacaba[head++] = src;
layers[0].push_back(src);
for (int j = 0; j < head; ++j) {
int v = __Abacaba[j];
for (auto& ed : g[v]) {
int u = ed.first;
if (dist[u] > dist[v] + 1) {
dist[u] = dist[v] + 1;
layers[dist[u]].push_back(u);
__Abacaba[head++] = u;
}
}
}
}
void build_cls() {
for (int lay = 1; !layers[lay].empty(); ++lay) {
for (auto& v : layers[lay]) pr[v] = make_pair(INT_INF, INT_INF);
for (auto& parent : layers[lay - 1]) {
for (auto& ed : g[parent]) {
int v = ed.first;
if (dist[v] == lay)
pr[v] = min(pr[v], make_pair(cls[parent], ed.second));
}
}
vector<pii> ord;
for (auto& v : layers[lay]) ord.push_back(pr[v]);
sort(ord.begin(), ord.end());
for (auto& v : layers[lay])
cls[v] = lower_bound(ord.begin(), ord.end(), pr[v]) - ord.begin();
}
}
void build_answ(int v, int cur) {
met[v] = 1;
answ[v] = cur;
for (auto& ed : g[v]) {
int u = ed.first;
if (dist[u] != dist[v] + 1 || met[u]) continue;
if (pr[u] == make_pair(cls[v], ed.second))
build_answ(u, add(mult(cur, 10), ed.second));
}
}
void solve() {
cin >> n >> m;
last = n;
for (int i = 0; i < m; ++i) {
int v, u;
cin >> v >> u;
--v, --u;
build_long_edge(v, u, i + 1);
build_long_edge(u, v, i + 1);
}
run_bfs(0);
build_cls();
build_answ(0, 0);
for (int v = 1; v < n; ++v) cout << answ[v] << '\n';
}
int main() {
fast_io();
solve();
return 0;
}
| 6 |
#include<iostream>
using namespace std;
int main(){
int s;
cin>>s;
cout<<s*s*s;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int wall[110][110] = {0};
int r, c;
scanf("%d %d%*c", &r, &c);
char a[110];
for (int i = 1; i < r; i++) gets(a);
int flag = 0;
int seg = 0;
for (int i = 0; i < c; i++) {
char x;
scanf("%c", &x);
if (flag == 0) {
if (x == 'B') {
flag++;
seg++;
} else
continue;
} else {
if (x == 'B')
continue;
else
flag = 0;
}
}
printf("%d\n", seg);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
bool flg = false;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') flg = true;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return flg ? -x : x;
}
int n, m;
int bin[50010], bintop;
vector<int> d[50010];
struct Rec {
int u, v, c, t;
} r[50010];
bool cmp(const int &x, const int &y) { return r[x].c < r[y].c; }
bool cmp2(const int &x, const int &y) { return r[x].t < r[y].t; }
vector<int> e[50010];
int findcoltot(int x, int c) {
r[0].c = c;
int pos = lower_bound(e[x].begin(), e[x].end(), 0, cmp) - e[x].begin();
if (pos >= e[x].size()) return 0;
if (r[e[x][pos]].c ^ c) return 0;
if (pos + 1 >= e[x].size() || r[e[x][pos + 1]].c ^ c) return 1;
return 2;
}
int getcolpos(int x, int c) {
r[0].c = c;
int pos = lower_bound(e[x].begin(), e[x].end(), 0, cmp) - e[x].begin();
return pos;
}
struct Edge {
int to, nxt;
} edge[5000000];
int cnt, last[1000010];
int nodetot;
inline void addedge(int x, int y) {
edge[++cnt] = (Edge){y, last[x]}, last[x] = cnt;
}
int vis[1000010], tim;
int edgeid[50010], etop;
void dfs1(int x, int c) {
vis[x] = tim;
int pos = getcolpos(x, c), v = r[e[x][pos]].u ^ r[e[x][pos]].v ^ x;
if (vis[v] ^ tim) edgeid[++etop] = e[x][pos], dfs1(v, c);
pos++;
if (pos >= e[x].size() || r[e[x][pos]].c ^ c) return;
v = r[e[x][pos]].u ^ r[e[x][pos]].v ^ x;
if (vis[v] ^ tim) edgeid[++etop] = e[x][pos], dfs1(v, c);
}
void work(int x) {
int pos;
for (int i = 0, _t = e[x].size(); i < _t; i++) {
int id = e[x][i];
++nodetot;
if (i) addedge(pos, nodetot), addedge(pos, id);
pos = nodetot;
addedge(id + m, pos);
}
for (int i = 0, _t = e[x].size(); i < _t; i++) {
int id = e[x][i];
++nodetot;
if (i) addedge(nodetot, pos), addedge(id + m, pos);
pos = nodetot;
addedge(pos, id);
}
for (int i = e[x].size() - 1; i >= 0; i--) {
int id = e[x][i];
++nodetot;
if (i + 1 < e[x].size()) addedge(pos, nodetot), addedge(pos, id);
pos = nodetot;
addedge(id + m, pos);
}
for (int i = e[x].size() - 1; i >= 0; i--) {
int id = e[x][i];
++nodetot;
if (i + 1 < e[x].size()) addedge(nodetot, pos), addedge(id + m, pos);
pos = nodetot;
addedge(pos, id);
}
for (int i = 0, _t = e[x].size(); i < _t; i++) {
int u = e[x][i], v;
if (i) {
v = e[x][i - 1];
if (r[u].c == r[v].c) addedge(u, v + m), addedge(v, u + m);
}
}
}
int dfn[1000010], low[1000010];
int stk[1000010];
int coltot, col[1000010];
bool isM;
bool visited_146;
void tarjan(int x) {
dfn[x] = low[x] = ++*dfn;
vis[x] = tim;
stk[++*stk] = x;
for (int i = last[x], v; i; i = edge[i].nxt)
if (!dfn[v = edge[i].to]) {
tarjan(v);
low[x] = min(low[x], low[v]);
} else if (vis[v] == tim)
low[x] = min(low[x], dfn[v]);
if (dfn[x] ^ low[x]) return;
++coltot;
do {
int u = stk[*stk];
vis[u] = 0;
col[u] = coltot;
} while (stk[(*stk)--] ^ x);
}
int ansbin[50010], ansbintop;
int id[50010];
bool check(int M) {
nodetot = m + m;
cnt = 0;
for (int i = M + 1; i <= m; i++) addedge(m + id[i], id[i]);
for (int i = 1; i <= bintop; i++) {
++tim;
for (int j = 0, _t = d[i].size(); j < _t; j++) {
if (vis[d[i][j]] ^ tim) {
if (findcoltot(d[i][j], i) == 2) continue;
etop = 0;
dfs1(d[i][j], i);
int t = ++nodetot;
for (int u = 2; u <= etop; u += 2) addedge(t, m + edgeid[u]);
for (int u = 1; u <= etop; u += 2) addedge(edgeid[u], t);
t = ++nodetot;
for (int u = 1; u <= etop; u += 2) addedge(t, m + edgeid[u]);
for (int u = 2; u <= etop; u += 2) addedge(edgeid[u], t);
}
}
for (int j = 0, _t = d[i].size(); j < _t; j++) {
if (vis[d[i][j]] ^ tim) {
etop = 0;
dfs1(d[i][j], i);
int t = ++nodetot;
for (int u = 2; u <= etop; u += 2) addedge(t, m + edgeid[u]);
for (int u = 1; u <= etop; u += 2) addedge(edgeid[u], t);
t = ++nodetot;
for (int u = 1; u <= etop; u += 2) addedge(t, m + edgeid[u]);
for (int u = 2; u <= etop; u += 2) addedge(edgeid[u], t);
}
}
}
for (int i = 1; i <= n; i++) work(i);
++tim;
coltot = 0;
for (int i = 0; i <= nodetot; i++) dfn[i] = 0;
for (int i = 1; i <= nodetot; i++)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= nodetot; i++) last[i] = 0;
for (int i = 1; i <= m; i++)
if (col[i] == col[i + m]) return 0;
ansbintop = 0;
for (int i = 1; i <= m; i++)
if (col[i + m] < col[i]) ansbin[++ansbintop] = i;
return 1;
}
int main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
int u = read(), v = read(), c = read(), t = read();
r[i] = (Rec){u, v, c, t};
e[u].push_back(i);
e[v].push_back(i);
bin[++bintop] = c;
id[i] = i;
}
sort(bin + 1, bin + bintop + 1);
bintop = unique(bin + 1, bin + bintop + 1) - bin - 1;
for (int i = 1; i <= m; i++) {
int &c = r[i].c;
c = lower_bound(bin + 1, bin + bintop + 1, c) - bin;
d[c].push_back(r[i].u);
d[c].push_back(r[i].v);
}
for (int i = 1; i <= bintop; i++) {
sort(d[i].begin(), d[i].end());
int t = unique(d[i].begin(), d[i].end()) - d[i].begin();
d[i].resize(t);
}
sort(id + 1, id + m + 1, cmp2);
for (int i = 1; i <= n; i++) {
sort(e[i].begin(), e[i].end(), cmp);
for (int j = 0, _t = e[i].size(); j < _t; j++)
if (j + 2 < _t && !cmp(e[i][j], e[i][j + 2])) return puts("No"), 0;
}
int L = 0, R = m, Mid, ans = -1;
while (L <= R) {
Mid = L + R >> 1;
if (check(Mid))
ans = Mid, R = Mid - 1;
else
L = Mid + 1;
}
if (!~ans) return puts("No"), 0;
puts("Yes");
printf("%d %d\n", r[id[ans]].t, ansbintop);
for (int i = 1; i <= ansbintop; i++) printf("%d ", ansbin[i]);
puts("");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int l, r, n, T, L;
char a[2000010];
int find() {
for (int i = L; i <= r; i++)
if (a[i] >= '5' && a[i] != '.') return i;
return 0;
}
void print() {
int r = n;
while (a[r] == '0') r--;
for (int i = 1; i <= r; i++) putchar(a[i]);
}
int main() {
scanf("%d%d", &n, &T);
l = 1;
r = n;
scanf("%s", a + 1);
for (int i = 1; i <= n; i++)
if (a[i] == '.') L = i + 1;
int t = 0;
a[0] = '0';
int pos = find();
if (pos == 0) {
print();
return 0;
}
for (int i = pos; i >= L; i--) {
if (t >= T) break;
if (a[i] >= '5') {
if (a[i - 1] == '.') i--;
a[i - 1] += 1;
int x = i - 1;
while (a[x] > '9') {
a[x] = a[x] - 10;
if (a[x - 1] == '.') x--;
a[x - 1] += 1;
x--;
}
t++;
pos = i;
}
}
if (a[0] == '1') l = 0;
for (int i = l; i < pos; i++) putchar(a[i]);
}
| 1 |
#define _USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
using namespace std;
int main(){
int n,m;
while(cin>>n>>m,n!=0||m!=0){
int s1=0,s2=0;
vector<int> x(n);
vector<int> y(m);
for(int i=0;i<n;i++){
cin>>x[i];
s1+=x[i];
}
for(int i=0;i<m;i++){
cin>>y[i];
s2+=y[i];
}
int f=0;
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(s1-x[i]+y[j]==s2-y[j]+x[i]){
f=1;
break;
}
}
if(f==1)
break;
}
if(f==1)
cout<<x[i]<<" "<<y[j]<<endl;
else
cout<<"-1"<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e6 + 1;
const int mod = 1e9 + 7;
int n;
int dp[nmax][20][2];
int32_t f(int x, int y) {
int res = (y == 1) ? 3 : 1;
return ((int)n / (res * (1 << x))) % mod;
}
int DP(int idx, int two, int thr) {
if (idx > n) return 1;
if (dp[idx][two][thr] != -1) return dp[idx][two][thr];
long long cnt = f(two, thr);
long long res = (cnt - idx + 1 > 0)
? (DP(idx + 1, two, thr) * (cnt - idx + 1 + mod)) % mod
: 0;
if (two && idx != 1)
(res +=
(DP(idx + 1, two - 1, thr) * (f(two - 1, thr) - cnt + mod)) % mod) %= mod;
if (thr && idx != 1)
(res +=
(DP(idx + 1, two, thr - 1) * (f(two, thr - 1) - cnt + mod)) % mod) %= mod;
return dp[idx][two][thr] = res;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
int raise = log2(n);
pair<int, int> a[2];
a[0] = make_pair(raise, 0);
if ((1 << (raise - 1)) * 3 <= n) {
a[1] = make_pair(raise - 1, 1);
}
long long res = 0;
for (int i = (int)0; i <= (int)1; i++)
if (a[i].first + a[i].second) {
for (int j = (int)0; j <= (int)n; j++)
for (int k = (int)0; k <= (int)a[i].first; k++)
for (int h = (int)0; h <= (int)a[i].second; h++) dp[j][k][h] = -1;
(res += DP(1, a[i].first, a[i].second)) %= mod;
}
cout << res;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const int MAXN = 2 * 1e5;
int main() {
int n;
cin >> n;
int a[MAXN];
int cnt = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum = sum + a[i];
if (a[i] == 0) cnt++;
}
if (cnt == n)
cout << "NO";
else {
cout << "YES" << endl;
if (sum != 0)
cout << 1 << endl << 1 << ' ' << n;
else {
int pos = 0;
while (a[pos] == 0) pos++;
cout << 2 << endl << 1 << ' ' << pos + 1 << endl << pos + 2 << ' ' << n;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
int gcd(int a, int b){if(b==0)return a;return gcd(b, a%b);}
int lcm(int a, int b){return a*b/gcd(a, b);}
int fexp(int a, int b){int ans = 1;while(b){if(b&1) ans = ans*a%mod; b/=2;a=a*a%mod;}return ans;}
int inverse(int a, int p){return fexp(a, p-2);}
struct Comparator
{
bool operator() (const std::pair<int, int>& lhs,
const std::pair<int, int>& rhs) const
{
if (lhs.first == rhs.first)
return lhs.second < rhs.second;
else
return lhs.first > rhs.first;
}
};
void solve(){
int n;cin>>n;
vector<int>a(n);
for(auto &i:a){
cin>>i;
}
sort(all(a),greater<int>());
int alice=0;
int bob=0;
rep(i,0,n){
if(i%2==0){
if(a[i]%2==0)alice+=a[i];
}
else{
if(a[i]%2==1)bob+=a[i];
}
// cout<<alice<<" "<<bob<<endl;
}
if(alice==bob)cout<<"Tie\n";
else if(alice>bob)cout<<"Alice\n";
else cout<<"Bob\n";
}
signed main(){
ios_base::sync_with_stdio(0) ;
cin.tie(0) ; cout.tie(0) ;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#endif
int t;
t=1;
cin>>t;
while(t--){
solve();
}
// #ifndef ONLINE_JUDGE
// cout<<"\nTime Elapsed: "<<1.0*clock()/ CLOCKS_PER_SEC<<" Sec\n";
// #endif
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
void ex_gcd(long long a, long long b, long long &d, long long &x,
long long &y) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
ex_gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
long long max(long long a) { return a > 0 ? a : -a; }
int main() {
long long A, Afh, B, Bfh, C, x, y, z;
while (cin >> A >> B >> C) {
z = 0;
if (A == 0) {
if (C % B == 0) {
x = 0;
y = -C / B;
cout << x << " " << y << endl;
} else
cout << -1 << endl;
} else if (B == 0) {
if (C % A == 0) {
y = 0;
x = -C / A;
cout << x << " " << y << endl;
} else
cout << -1 << endl;
} else {
Afh = Bfh = 1;
if (A < 0) Afh = -1;
if (B < 0) Bfh = -1;
if (C > 0) {
Afh *= -1;
Bfh *= -1;
}
A = max(A);
B = max(B);
C = max(C);
long long g, X, Y;
ex_gcd(A, B, g, X, Y);
if (C % g != 0)
cout << -1 << endl;
else {
x = X * (C / g) * Afh;
y = Y * (C / g) * Bfh;
cout << x << " " << y << endl;
}
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
if (y == 0 || (y - x) > 1)
cout << "No";
else if (y == 1 && x > 0)
cout << "No";
else if (y == 1 && !(x > 0))
cout << "Yes";
else if ((x - (y - 1)) % 2 == 0)
cout << "Yes";
else
cout << "No";
cout << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int main() {
int u, v, w, n, m;
cin >> n >> m;
int maxn = 100001;
int path[maxn];
long long dis[maxn];
vector<pair<long long, int>> G[maxn];
for (int i = 0; i < m; i++) {
cin >> u >> v >> w;
G[u].push_back(pair<int, int>(v, w));
G[v].push_back(pair<int, int>(u, w));
}
fill(dis, dis + maxn, INF);
dis[1] = 0;
priority_queue<pair<long long, int>> q;
q.push(pair<int, int>(0, 1));
while (!q.empty()) {
int x = q.top().second;
q.pop();
int len = G[x].size();
for (int i = 0; i < len; i++) {
int y = G[x][i].first;
long long distancia = G[x][i].second;
if (dis[y] > dis[x] + distancia) {
dis[y] = dis[x] + distancia;
q.push(pair<int, int>(-dis[y], y));
path[y] = x;
}
}
}
if (dis[n] == INF)
cout << -1 << endl;
else {
vector<int> ans;
while (n) {
ans.push_back(n);
n = path[n];
}
int len = ans.size();
for (int i = len - 1; i >= 0; i--) {
cout << ans[i] << ' ';
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<long long, long long>> vec;
const int N = 1e6 + 5;
bool check(long long x) {
x--;
if (x & 1) return (x + 1) * x / 2 + (x + 1) / 2 <= n;
return (x + 1) * x / 2 + 1 <= n;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
vec.push_back({1e9, 1e9});
for (auto i = 0; i < (long long)(m); i++) {
long long a, b;
cin >> a >> b;
vec.push_back({b, a});
}
sort(vec.rbegin(), vec.rend());
long long res = 0;
for (auto i = 1; i <= (long long)(m); i++) {
if (!check(i)) break;
res += vec[i].first;
}
cout << res;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void dmin(T& x, T y) {
y < x ? x = y : 0;
}
template <class T>
inline void dmax(T& x, T y) {
y > x ? x = y : 0;
}
template <class T>
inline void dmin(T& x, vector<T> y) {
for (auto t : y) t < x ? x = t : 0;
}
template <class T>
inline void dmax(T& x, vector<T> y) {
for (auto t : y) t > x ? x = t : 0;
}
namespace IO {
const int sz = 1 << 20;
char a[sz + 5], b[sz + 5], *p1 = a, *p2 = a, *t = b, p[105];
bool flag = 0;
void change_flag(bool s) { flag = s; }
inline char gc() {
return p1 == p2 ? (flag ? getchar()
: (p2 = (p1 = a) + fread(a, 1, sz, stdin),
p1 == p2 ? EOF : *p1++))
: *p1++;
}
template <class T>
void gi(T& ret) {
ret = 0;
char c = gc();
int f = 1;
for (; c < '0' || c > '9'; c = gc())
if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = gc()) ret = ret * 10 + (c - '0');
ret *= f;
}
inline void flush() { fwrite(b, 1, t - b, stdout), t = b; }
inline void pc(char x) {
*t++ = x;
if (t - b == sz) flush();
}
template <class T>
void pi(T x, char c = '\n') {
if (x < 0) pc('-'), x = -x;
if (x == 0) pc('0');
int t = 0;
for (; x; x /= 10) p[++t] = x % 10 + '0';
for (; t; --t) pc(p[t]);
pc(c);
if (flag) flush();
}
struct F {
~F() { flush(); }
} f;
} // namespace IO
namespace SB_MATH {
struct ModOperator {
int MOD;
ModOperator(int mod) { MOD = mod; }
inline int fpow(int x, int n, int ret = 1) {
for (; n; n >>= 1, x = 1ll * x * x % MOD)
n& 1 ? ret = 1ll * ret * x % MOD : 0;
return ret;
}
inline void mo(int& x) { x >= MOD ? x -= MOD : 0; }
inline int mo1(int x) { return x >= MOD ? x - MOD : x; }
inline int mo2(int x) { return x < 0 ? x + MOD : x; }
inline int mo3(int x) { return mo1(mo2(x)); }
inline int mul(int x, int y) { return 1ll * x * y % MOD; }
inline int add(int x, int y) { return mo1(x + y); }
inline int sub(int x, int y) { return mo2(x - y); }
inline int div(int x, int y) { return fpow(y, MOD - 2, x); }
inline vector<int> inv(vector<int> x) {
vector<int> y;
y.resize(x.size());
int s = 1, s1 = 1, s2 = 1;
for (auto t : x) s = 1ll * s * t % MOD;
if (s == 0) assert(0);
s = fpow(s, MOD - 2);
for (int i = 0; i < x.size(); ++i) {
y[i] = 1ll * s * s1 % MOD;
s1 = 1ll * s1 * x[i] % MOD;
}
for (int i = (int)x.size() - 1; i >= 0; --i) {
y[i] = 1ll * y[i] * s2 % MOD;
s2 = 1ll * s2 * x[i] % MOD;
}
return y;
}
};
struct Comb {
vector<int> fac, inv, fac_inv;
ModOperator* P;
int MOD;
Comb(ModOperator* s, int sz) {
P = s;
MOD = P->MOD;
fac.resize(sz);
inv.resize(sz);
fac_inv.resize(sz);
inv[1] = fac[0] = fac_inv[0] = 1;
for (int i = 1; i < sz; ++i) {
fac[i] = 1ll * fac[i - 1] * i % MOD;
if (i > 1) inv[i] = 1ll * (MOD - MOD / i) * inv[MOD % i] % MOD;
fac_inv[i] = 1ll * fac_inv[i - 1] * inv[i] % MOD;
}
}
int operator()(int n, int m) {
if (m < 0) return 0;
if (n >= 0) {
if (m > n) return 0;
return 1ll * fac[n] * fac_inv[m] % MOD * fac_inv[n - m] % MOD;
} else {
int ret =
1ll * fac[-n + m - 1] * fac_inv[m] % MOD * fac_inv[-n - 1] % MOD;
if (m & 1) ret = P->mo2(-ret);
return ret;
}
}
};
struct Comb2 {
vector<vector<int> > c;
Comb2(ModOperator* P, int sz) {
c.resize(sz + 1);
for (int i = 0; i <= sz; ++i) {
c[i].resize(i + 1);
c[i][0] = 1;
for (int j = 0; j <= i; ++j)
c[i][j] = P->add(c[i - 1][j], c[i - 1][j - 1]);
}
}
int operator()(int n, int m) { return c[n][m]; }
};
} // namespace SB_MATH
namespace GEO {
long double EPS = 1e-10, PI = acos(-1);
template <class T>
int sn(T x) {
return x < -EPS ? -1 : (x > EPS);
}
template <class T>
struct P {
T x, y;
P(T _x = T(), T _y = T()) { x = _x, y = _y; }
inline P<T> operator+(P<T> b) { return P<T>(x + b.x, y + b.y); }
inline P<T> operator-(P<T> b) { return P<T>(x - b.x, y - b.y); }
inline P<T> operator*(P<T> b) {
return P<T>(x * b.x - y * b.y, x * b.y + y * b.x);
}
inline P<T> operator/(T t) { return P<T>(x / t, y / t); }
inline T cp(P<T> b) { return x * b.y - y * b.x; }
inline T dot(P<T> b) { return x * b.x + y * b.y; }
inline P<T> rot90() { return P<T>(-y, x); }
inline P<T> rot(T x) { return (*this) * P<T>(cos(x), sin(x)); }
inline int qua() {
return sn(y) >= 0 ? (sn(x) >= 0 ? 1 : 2) : (sn(x) >= 0 ? 4 : 3);
}
};
template <class T>
struct Line {
P<T> a[2];
inline P<T>& operator[](int x) { return a[x]; }
inline P<T> v() { return a[1] - a[0]; }
inline int include(P<T> x) { return sn(v().cp(x - a[0])); }
};
template <class T>
bool para(P<T> x, P<T> y) {
return sn(x.cp(y)) == 0;
}
template <class T>
bool para(Line<T> x, Line<T> y) {
return para(x.v(), y.v());
}
template <class T>
P<T> getIP(Line<T> a, Line<T> b) {
if (para(a, b)) assert(0);
T w0 = (a[0] - b[0]).cp(b[1] - b[0]);
T w1 = (b[1] - b[0]).cp(a[1] - b[0]);
return (a[0] * w1 + a[1] * w0) / (w0 + w1);
}
template <class T>
Line<T> getPL(P<T> x, P<T> y) {
P<T> z = (x + y) / 2;
return (Line<T>){z, z + (y - z).rot90()};
}
} // namespace GEO
const int MAX_N = 4005, INF = 1e9;
int g[MAX_N], h[MAX_N], f[MAX_N][MAX_N], l[MAX_N], s[MAX_N], c[MAX_N];
int add(int x, int y) {
if (x == -INF || y == -INF)
return -INF;
else
return x + y;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &l[i]);
for (int i = 1; i <= n; ++i) scanf("%d", &s[i]);
for (int i = 1; i <= n + m; ++i) scanf("%d", &c[i]);
reverse(l + 1, l + n + 1);
reverse(s + 1, s + n + 1);
for (int i = 0; i <= n + m; ++i)
for (int j = 0; j <= n + m; ++j) f[i][j] = -INF;
for (int i = 1; i <= n + m; ++i) f[i][0] = 0;
for (int i = 1; i <= n; ++i) {
g[0] = -INF;
for (int cnt = 1; cnt <= n + m; ++cnt) {
g[cnt] = add(f[l[i]][cnt - 1], c[l[i]] - s[i]);
}
int top = n + m;
for (int j = 0; l[i] + j <= n + m; ++j) {
for (int k = 0; k <= top; ++k) dmax(f[l[i] + j][k], g[k]);
for (int k = 0; k <= top; ++k) h[k] = -INF;
for (int k = 0; k <= top; ++k)
dmax(h[k / 2], add(g[k], c[l[i] + j + 1] * (k / 2)));
top >>= 1;
for (int k = 0; k <= top; ++k) g[k] = h[k];
}
}
int ans = 0;
for (int cnt = 0; cnt <= n + m; ++cnt) dmax(ans, f[n + m][cnt]);
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct otr {
int typ;
int x1, x2, ver;
};
bool operator<(otr a, otr b) {
return (a.x1 < b.x1 || (a.x1 == b.x1 && a.typ < b.typ));
}
int main() {
int n, m;
int x, h, l, r;
scanf("%d%d", &n, &m);
otr* mas = new otr[4 * n + m + 2];
int sz = 0;
otr tmp;
for (int i = 0; i < (int)n; ++i) {
scanf("%d%d%d%d", &x, &h, &l, &r);
tmp.typ = -1;
tmp.x1 = x - h;
tmp.x2 = x - 1;
tmp.ver = l;
mas[sz++] = tmp;
tmp.typ = 1;
tmp.x1 = x - 1;
tmp.x2 = x - h;
tmp.ver = l;
mas[sz++] = tmp;
tmp.typ = -1;
tmp.x1 = x + 1;
tmp.x2 = x + h;
tmp.ver = r;
mas[sz++] = tmp;
tmp.typ = 1;
tmp.x1 = x + h;
tmp.x2 = x + 1;
tmp.ver = r;
mas[sz++] = tmp;
}
for (int i = 0; i < (int)m; ++i) {
scanf("%d%d", &x, &h);
tmp.typ = 0;
tmp.x1 = x;
tmp.x2 = h;
mas[sz++] = tmp;
}
sort(mas, mas + sz);
multiset<pair<int, int> > ar;
double curv = 0.0;
double ans = 0.0;
int fl = 0;
for (int i = 0; i < (int)sz; ++i) {
if (mas[i].typ == -1) {
ar.insert(make_pair(mas[i].x1, mas[i].ver));
if (mas[i].ver == 100) {
fl++;
continue;
}
curv += log((100 - mas[i].ver) / 100.0);
} else if (mas[i].typ == 0 && !fl) {
ans += mas[i].x2 * exp(curv);
} else if (mas[i].typ == 1) {
ar.erase(make_pair(mas[i].x1, mas[i].ver));
if (mas[i].ver == 100) {
fl--;
continue;
}
curv -= log((100 - mas[i].ver) / 100.0);
}
}
printf("%.8lf", ans);
return 0;
}
| 3 |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;
#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
vector<string> s;
string buf = "";
int ci = 0, cj = 0;
void input() {
string l;
while (1) {
getline(cin, l);
if (l == "END_OF_TEXT") {
break;
}
s.push_back(l);
}
}
// カーソルを現在の行の先頭文字に移動する
void op_a() { cj = 0; }
// カーソルを現在の行の行末に移動する
void op_e() { cj = s[ci].size(); }
// 上に行があれば、カーソルを上の行の先頭文字に移動する。
// 上に行がなければ、カーソルを先頭文字に移動する。
void op_p() {
if (ci != 0)
--ci;
op_a();
}
// 下に行があれば、カーソルを下の行の先頭文字に移動する。
// 下に行がなければ、カーソルを先頭文字に移動する。
void op_n() {
if (ci != s.size() - 1)
++ci;
op_a();
}
// カーソルが行末にない場合、カーソルを1つ右に移動する
// カーソルが行末にありかつ下に行がある場合は、
// カーソルを下の行の先頭文字へ移動する
// それ以外の場合は何もしない
void op_f() {
if (cj != s[ci].size()) {
++cj;
} else if (cj == s[ci].size() && ci != s.size() - 1) {
op_n();
}
}
// カーソルが先頭文字でないならば、カーソルを1つ左に移動する。
// カーソルが先頭文字でありかつ上に行がある場合は、
// カーソルを上の行の行末へ移動する。
// それ以外の場合は何もしない。
void op_b() {
if (cj != 0) {
--cj;
} else if (cj == 0 && ci != 0) {
--ci;
op_e();
}
}
// カーソルが文字を指す場合はその文字を削除する。
// 削除された文字の右側の文字列は左にシフトされる。
// カーソルが行末を指し下に行がある場合は、
// 下の行をそのままカーソルの位置に繋げる。以下の行は上にシフトされる。
// それ以外の場合は何もしない。
void op_d() {
if (cj != s[ci].size()) {
// prev
// s[ci].erase(cj, 1);
s[ci] = s[ci].substr(0, cj) + s[ci].substr(cj + 1);
} else if (cj == s[ci].size() && ci != s.size() - 1) {
// prev
// s[ci].erase(cj, 1);
// s[ci] += s[ci+1];
// s.erase(s.begin() + ci + 1);
s[ci] += s[ci+1];
s.erase(s.begin() + ci + 1);
}
}
// カーソルが行末にある場合、下に行があれば、
// d と同じ操作をしバッファに1つの改行を記録する。
// カーソルが行末にない場合、カーソルが指す文字を含めた
// 右側すべての文字を切り取りそれをバッファに記録する。
// カーソルは元の行の行末を指すようになる。全ての記録においてバッファは上書きされる。
// それ以外の場合は何もしない。(clarification at 14:20)
void op_k() {
if (cj == s[ci].size() && ci != s.size() - 1) {
// prev
// s[ci].erase(cj, 1);
// s[ci] += s[ci+1];
// s.erase(s.begin() + ci + 1);
s[ci] += s[ci+1];
s.erase(s.begin() + ci + 1);
buf = "\n";
} else {
// prev
// buf = s[ci].substr(cj);
// s[ci].erase(cj);
buf = s[ci].substr(cj);
s[ci] = s[ci].substr(0, cj);
}
}
// バッファが空の場合はなにもしない。
// バッファの内容が改行の場合、カーソルが指す文字の直前で改行を行う。
// カーソルは新しい行の先頭文字へ移動する。
// バッファの内容が文字列の場合、カーソルの位置にバッファの内容を挿入する。
// カーソルの位置はもともと指していた文字または行末の場所へ移動する。
void op_y() {
if (buf == "") return;
if (buf == "\n") {
// prev
// inserter(s, s.begin() + ci + 1) = s[ci].substr(cj);
// s[ci].erase(cj);
// ++ci; cj = 0;
vector<string>::iterator itr = s.begin();
rep(i,ci+1) ++itr;
s.insert(itr, s[ci].substr(cj));
s[ci] = s[ci].substr(0, cj);
++ci; cj = 0;
} else {
// prev
// s[ci].insert(cj, buf.c_str());
// cj += buf.size();
s[ci] = s[ci].substr(0, cj) + buf + s[ci].substr(cj);
cj += buf.size();
}
}
void interpret() {
string op;
while (1) {
getline(cin, op);
if (op == "-") break;
if (op == "a") {
op_a();
} else if (op == "e") {
op_e();
} else if (op == "p") {
op_p();
} else if (op == "n") {
op_n();
} else if (op == "f") {
op_f();
} else if (op == "b") {
op_b();
} else if (op == "d") {
op_d();
} else if (op == "k") {
op_k();
} else if (op == "y") {
op_y();
}
}
rep(i,s.size()) {
// printf("%s\n", s[i].c_str());
cout << s[i] << endl;
}
}
int main() {
input();
interpret();
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long m, ans, lef, rig, mid, sum, mm, i;
int main() {
scanf("%llu", &m);
lef = 0;
rig = 1e19;
while (lef <= rig) {
mid = (lef + rig) >> 1;
for (i = 2, sum = 0; i < 2000000; i++) {
if (mid >= i * i * i)
sum += mid / (i * i * i);
else
break;
}
if (sum >= m)
rig = mid - 1, ans = mid, mm = sum;
else
lef = mid + 1;
}
if (mm == m)
printf("%llu\n", ans);
else
printf("-1\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int oo = 0x3f3f3f3f;
const long long ooLL = 0x3f3f3f3f3f3f3f3fLL;
int main() {
set<int> p;
for (int x; scanf("%d%*c", &x) == 1; p.insert(x))
;
int pp = -10;
int pf = -10;
p.insert(10000);
bool f = true;
for (set<int>::const_iterator it = p.begin(); it != p.end(); ++it) {
if (pp < 0) {
pp = pf = *it;
} else if (*it == pp + 1) {
++pp;
} else {
if (!f) printf(",");
f = false;
if (pp == pf)
printf("%d", pp);
else
printf("%d-%d", pf, pp);
pp = pf = *it;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int m[500100][5];
int main() {
int i, j, st, ed, n, ans = 0;
set<pair<int, int> > s;
set<pair<int, int> >::iterator it;
vector<pair<int, pair<int, int> > > v;
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (i = 0; i < 3; i++)
for (j = 0; j < n; j++) cin >> m[j][i];
for (i = 0; i < n; i++) {
v.push_back(make_pair(m[i][0], make_pair(m[i][1], m[i][2])));
}
sort(v.begin(), v.end());
st = ed = n - 1;
while (ed >= 0) {
while (st >= 0 && v[st].first == v[ed].first) st--;
for (i = ed; i > st; i--) {
it = s.upper_bound(make_pair(v[i].second.first, 1000100100));
if (it != s.end() && (*it).second > v[i].second.second) ans++;
}
for (i = ed; i > st; i--) {
if (s.empty()) {
s.insert(v[i].second);
continue;
}
it = s.upper_bound(make_pair(v[i].second.first, 1000100100));
if (it == s.end() || (*it).second < v[i].second.second) {
while (it != s.begin()) {
it--;
if ((*it).second > v[i].second.second) break;
it = s.erase(it);
}
if (s.size() && (*it).first == v[i].second.first &&
(*it).second >= v[i].second.second)
continue;
else
s.insert(v[i].second);
}
}
ed = st;
}
cout << ans;
}
| 4 |
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
struct Node
{
int key;
Node* parent;
Node* left;
Node* right;
};
Node* root;
void insert(int val)
{
Node* x = new Node();
x->key = val;
x->left = nullptr;
x->right = nullptr;
Node *q = nullptr;
Node *p = root;
while (p != nullptr)
{
q = p;
if (x->key < p->key)
p = p->left;
else
p = p->right;
}
x->parent = q;
if (q == nullptr)
root = x;
else if (x->key < q->key)
q->left = x;
else
q->right = x;
}
void inorder(Node* root)
{
if (root == nullptr)
return;
inorder(root->left);
printf(" %d", root->key);
inorder(root->right);
}
void preorder(Node* root)
{
if (root == nullptr)
return;
printf(" %d", root->key);
preorder(root->left);
preorder(root->right);
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
string s;
int t;
cin >> s;
if (s[0] == 'i')
{
cin >> t;
insert(t);
}
else if (s[0] == 'p')
{
inorder(root);
cout << endl;
preorder(root);
cout << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 9;
long long n, k;
long long a[maxn];
int main() {
cin >> n >> k;
long long ret(0);
for (long long i = 1; i <= k; ++i) a[i] = i, ret += a[i];
if (ret > n) {
puts("NO");
return 0;
}
long long tmp(n - ret);
if (tmp >= k) {
long long lun(tmp / k);
for (long long i = 1; i <= k; ++i) a[i] += lun;
ret += lun * k;
}
long long up(0);
while (true) {
tmp = n - ret;
if (!tmp) break;
long long now(std::max(k - tmp + 1, up));
if (a[now - 1] * 2 < a[now] + 1) {
long long f(0);
for (long long j = now + 1; j <= k; ++j) {
if (a[j - 1] * 2 >= a[j] + 1) {
f = j;
break;
}
}
if (!f) {
puts("NO");
return 0;
}
up = f;
for (long long j = f; j <= k; ++j) ++a[j];
ret += k - f + 1;
} else {
for (long long i = now; i <= k; ++i) ++a[i];
ret += k - now + 1;
}
}
puts("YES");
for (long long i = 1; i <= k; ++i) cout << a[i] << ' ';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << min(a, min(b, (a + b) / 3));
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct union_find {
vector<int> p;
union_find(int n) : p(n, -1) {}
bool join(int u, int v) {
if ((u = root(u)) == (v = root(v))) return false;
if (p[u] > p[v]) swap(u, v);
p[u] += p[v];
p[v] = u;
return true;
}
int root(int u) { return p[u] < 0 ? u : p[u] = root(p[u]); }
int size(int u) { return -p[root(u)]; }
};
template <class node>
struct link_cut_tree {
bool connected(node* u, node* v) { return lca(u, v) != NULL; }
int depth(node* u) {
access(u);
return get_sz(u->ch[0]);
}
node* get_root(node* u) {
access(u);
while (u->ch[0]) u = u->ch[0], u->push();
return access(u), u;
}
node* ancestor(node* u, int k) {
k = depth(u) - k;
assert(k >= 0);
for (;; u->push()) {
int sz = get_sz(u->ch[0]);
if (sz == k) return access(u), u;
if (sz < k)
k -= sz + 1, u = u->ch[1];
else
u = u->ch[0];
}
assert(0);
}
node* lca(node* u, node* v) {
if (u == v) return u;
access(u);
access(v);
if (!u->p) return NULL;
u->splay();
return u->p ?: u;
}
void link(node* u, node* v) {
assert(!connected(u, v));
make_root(v);
access(u);
set_link(v, u, 0);
v->update();
}
void cut(node* u) {
access(u);
u->ch[0]->p = NULL;
u->ch[0] = NULL;
u->update();
}
void cut(node* u, node* v) { cut(depth(u) > depth(v) ? u : v); }
void make_root(node* u) {
access(u);
u->rev ^= 1;
access(u);
assert(!u->ch[0] && !u->ch[1]);
}
void access(node* u) {
for (node *v = u, *pre = NULL; v; v = v->p) {
v->splay();
if (pre) v->update_vsub(pre, false);
if (v->ch[1]) v->update_vsub(v->ch[1], true);
v->ch[1] = pre;
v->update();
pre = v;
}
u->splay();
assert(!u->ch[1]);
}
node* operator[](int i) { return &data[i]; }
int operator[](node* i) { return i - &data[0]; }
vector<node> data;
link_cut_tree(int n) : data(n) {}
};
template <typename pnode>
struct splay_tree {
pnode ch[2], p;
bool rev;
int sz;
splay_tree() {
ch[0] = ch[1] = p = NULL;
rev = 0;
sz = 1;
}
friend int get_sz(pnode u) { return u ? u->sz : 0; }
virtual void update() { sz = 1 + get_sz(ch[0]) + get_sz(ch[1]); }
virtual void push() {
if (rev) {
if (ch[0]) ch[0]->rev ^= 1;
if (ch[1]) ch[1]->rev ^= 1;
swap(ch[0], ch[1]);
rev = 0;
}
}
int dir() {
if (!p) return -2;
if (p->ch[0] == this) return 0;
if (p->ch[1] == this) return 1;
return -1;
}
bool is_root() { return dir() < 0; }
friend void set_link(pnode u, pnode v, int d) {
if (v) v->p = u;
if (d >= 0) u->ch[d] = v;
}
void rotate() {
assert(!is_root());
int x = dir();
pnode g = p;
set_link(g->p, static_cast<pnode>(this), g->dir());
set_link(g, ch[x ^ 1], x);
set_link(static_cast<pnode>(this), g, x ^ 1);
g->update();
update();
}
void splay() {
while (!is_root() && !p->is_root()) {
p->p->push(), p->push(), push();
dir() == p->dir() ? p->rotate() : rotate();
rotate();
}
if (!is_root()) p->push(), push(), rotate();
push();
}
};
struct node : splay_tree<node*> {
int x, sub;
priority_queue<int> vsub, del;
node() : splay_tree() { x = sub = 0; }
int get() {
while (!del.empty() && vsub.top() == del.top()) vsub.pop(), del.pop();
if (vsub.empty()) return 0;
return vsub.top();
}
void update() override {
splay_tree::update();
sub = x;
sub = max(x, get());
if (ch[0]) sub = max(sub, ch[0]->sub);
if (ch[1]) sub = max(sub, ch[1]->sub);
}
void update_vsub(node* v, bool add) {
if (v->sub == 0) return;
if (add)
vsub.push(v->sub);
else
del.push(v->sub), get();
}
};
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<int> p(n), who(n + 1);
link_cut_tree<node> lct(n);
for (auto& i : p) {
int u = &i - &p[0];
cin >> i;
who[i] = u;
lct[u]->x = lct[u]->sub = i;
}
vector<pair<int, int>> edges(m);
for (auto& i : edges) cin >> i.first >> i.second, --i.first, --i.second;
vector<pair<int, int>> prio(m);
for (int i = 0; i < m; ++i) prio[i] = {q + 1, i};
vector<pair<int, int>> queries(q);
for (auto& i : queries) {
cin >> i.first >> i.second, --i.second;
if (i.first == 2) prio[i.second].first = &i - &queries[0];
}
vector<bool> alive(m);
union_find uf(n);
sort(prio.rbegin(), prio.rend());
for (auto i : prio) {
int u, v;
tie(u, v) = edges[i.second];
if (uf.join(u, v)) {
alive[i.second] = true;
lct.link(lct[u], lct[v]);
}
}
for (auto que : queries) {
if (que.first == 1) {
auto u = lct[que.second];
lct.access(u);
cout << u->sub << "\n";
if (u->sub != 0) {
auto v = lct[who[u->sub]];
lct.access(v);
v->x = 0;
v->update();
}
} else if (alive[que.second]) {
int u, v;
tie(u, v) = edges[que.second];
lct.cut(lct[u], lct[v]);
}
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
int main(){
string s;
cin >> s;
set<char> cs;
for(int i = 0;i < s.size();i++) cs.emplace(s[i]);
int ans = 1000;
for(auto& c : cs){
int count = 0;
int an = 0;
for(int i = 0;i < s.size();i++){
if(s[i] == c) count = 0;
else count++;
an = max(an, count);
}
ans = min(ans, an);
}
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long p = (long long)2e18;
int n;
long long k;
vector<long long> v;
bool ok() {
for (int i = 0; i < n; ++i)
if (v[i] >= k) return true;
return false;
}
long long mu(long long x, long long y) {
if (y == 0) return 0;
return (x >= 1 + (long long)(p / y) ? p : x * y);
}
bool okk(long long m, long long a, long long b, long long c) {
return (mu(mu(m, m + 1), a) + mu(m, 2 * b) + 2 * c >= 2 * k);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
v.resize(n);
for (int i = 0; i < n; ++i) cin >> v[i];
reverse(v.begin(), v.end());
while (!v.back()) v.pop_back();
reverse(v.begin(), v.end());
n = v.size();
if (ok()) {
cout << 0 << endl;
return 0;
}
if (n == 2) {
long long a = v[0];
long long b = v[1];
cout << (k - b + a - 1) / a << endl;
return 0;
}
if (n == 3) {
long long l = -1;
long long r = p;
while (r - l > 1) {
long long mid = (r + l) / 2;
(okk(mid, v[0], v[1], v[2]) ? r : l) = mid;
}
cout << r << endl;
return 0;
}
int res = 0;
while (true) {
for (int i = 1; i < n; ++i) v[i] += v[i - 1];
++res;
if (ok()) {
cout << res << endl;
return 0;
}
}
return 0;
}
| 6 |
#include <iostream>
using namespace std;
int main() {
int x, y;
while (cin >> x >> y, (x || y)) {
cout << min(x, y) << " " << max(x, y) << endl;
}
} | 0 |
#include<iostream>
#define M 100000007
using namespace std;
int r,c;
int dp[2][10][3][100000]; //dp[r][c][左からn本][上からn本]
int g[20][20];
char z;
int po3[21];
int main(){
cin >> r >> c;
po3[0] = 1;
for(int i=0;i<c;i++)po3[i+1] = po3[i]*3;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++){
cin >> z;
if(z=='.')g[i][j] = 0;
else g[i][j] = z-'0';
}
for(int i=0;i<c;i++)
for(int j=0;j<3;j++)
for(int k=0;k<po3[c];k++)dp[0][i][j][k] = 0;
dp[0][0][0][0] = 1;
for(int i=0;i<r;i++){
int id = i&1, nxt = 1-id;
for(int j=0;j<c;j++)
for(int k=0;k<3;k++)
for(int l=0;l<po3[c];l++)dp[nxt][j][k][l] = 0;
for(int j=0;j<c-1;j++){
if(!g[i][j]){
for(int k=0;k<3;k++){
for(int l=0;l<po3[c];l++){
int m = l/po3[j]%3;
if(m && k)continue;
dp[id][j+1][k][l] += dp[id][j][k][l];
dp[id][j+1][k][l] %= M;
}
}
}else{
for(int k=0;k<3;k++){
for(int l=0;l<po3[c];l++){
int m = l/po3[j]%3;
int n = g[i][j] - m - k;
if(n<0 || n>4)continue;
for(int x=0;x<3;x++){
if(n-x<0 || n-x>2)continue;
int w = l - m*po3[j] + (n-x)*po3[j];
dp[id][j+1][x][w] += dp[id][j][k][l];
dp[id][j+1][x][w] %= M;
}
}
}
}
}
if(!g[i][c-1]){
for(int l=0;l<po3[c];l++){
dp[nxt][0][0][l] += dp[id][c-1][0][l];
dp[nxt][0][0][l] %= M;
}
}else{
for(int k=0;k<3;k++){
for(int l=0;l<po3[c];l++){
int m = l/po3[c-1]%3;
int n = g[i][c-1] - m - k;
if(n<0 || n>2)continue;
int w = l - m*po3[c-1] + n*po3[c-1];
dp[nxt][0][0][w] += dp[id][c-1][k][l];
dp[nxt][0][0][w] %= M;
}
}
}
}
cout << dp[r&1][0][0][0] << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int sz = 2e5 + 100;
vector<bool> t;
vector<int> g[sz], p, ans;
int dp[sz];
int rgcd(int a, int b, int& x, int& y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
int curx, cury, g = rgcd(b % a, a, curx, cury);
x = cury - b / a * curx;
y = curx;
return g;
}
int main() {
ios_base::sync_with_stdio(false);
int n, m, x, y, i, j, cur;
cin >> n >> m;
t.resize(m, true);
p.resize(m);
for (i = 0; i < n; i++) {
cin >> cur;
t[cur] = false;
}
for (i = 1; i < m; i++)
if (t[i]) g[rgcd(i, m, x, y)].push_back(i);
for (i = m - 1; i > 0; i--) {
int mx = 0;
p[i] = i;
for (j = i + i; j < m; j += i)
if (dp[p[i]] < dp[j]) p[i] = j;
dp[i] = dp[p[i]] + g[i].size();
if (p[i] == i) p[i] = -1;
}
int z = 0, pr = 1, prg = 1;
for (i = 1; i < m; i++)
if (dp[i] > dp[z]) z = i;
if (z == 0) z = -1;
for (z; z != -1; z = p[z])
for (j = 0; j < g[z].size(); j++) {
int curpr = pr, curm = m;
long long curto = g[z][j];
curpr /= prg;
curto /= prg;
curm /= prg;
int curg = rgcd(curpr, curm, x, y);
x = (x % curm + curm) % curm;
curto *= x;
curto %= m;
ans.push_back(curto);
pr = g[z][j];
prg = z;
}
if (t[0]) ans.push_back(0);
cout << ans.size() << endl;
for (i = 0; i < ans.size(); i++) cout << ans[i] << " ";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long T, n, m, k, t;
long long s[10000009];
int main() {
int ncase = 0;
while (scanf("%I64d", &n) == 1) {
if (n % 3) {
printf("0\n");
continue;
}
n /= 3;
int cnt = 0;
for (long long i = 2; i * i <= n; i++)
if (n % i == 0) {
s[cnt++] = i;
}
long long ans = 0;
for (int i = 0; i < cnt; ++i)
for (int j = i; j < cnt; j++) {
long long a = s[i], b = s[j];
long long c = n / (a * b);
if (a * b * c != n) continue;
if (a + b < c + 2) continue;
if ((a + b + c) % 2) continue;
if (c < b) break;
if (a == b && a == c)
ans++;
else if (a == b || b == c)
ans += 3;
else
ans += 6;
}
cout << ans << endl;
}
return 0;
}
| 5 |
#include<iostream>
using namespace std;
char s1[101],s2[110],s3[101];
int main()
{
cin>>s1>>s2>>s3;
cout<<s1[0]<<s2[0]<<s3[0];
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct Aho {
struct vertex {
vector<int> next, go;
int p, link;
char pch;
int leaf;
vertex(int p = -1, char pch = -1)
: next(26, -1), go(26, -1), p(p), pch(pch), link(-1), leaf(0) {}
};
struct STree {
vector<int> t;
int n;
STree() {}
STree(int n) : n(n), t(2 * n + 5, 0) {}
void upd(int p, int value) {
for (t[p += n] += value; p > 1; p >>= 1) t[p >> 1] = (t[p] + t[p ^ 1]);
}
int query(int l, int r) {
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = (res + t[l++]);
if (r & 1) res = (res + t[--r]);
}
return res;
}
};
vector<vertex> t;
Aho() { t.push_back(vertex()); }
int add_string(string &s) {
int v = 0;
for (char c : s) {
if (t[v].next[c - 'a'] < 0) {
t[v].next[c - 'a'] = t.size();
t.push_back(vertex(v, c));
}
v = t[v].next[c - 'a'];
}
t[v].leaf++;
return v;
}
int get_link(int v) {
if (t[v].link < 0)
if (!v || !t[v].p)
t[v].link = 0;
else
t[v].link = go(get_link(t[v].p), t[v].pch);
return t[v].link;
}
int go(int v, char c) {
if (t[v].go[c - 'a'] < 0)
if (t[v].next[c - 'a'] >= 0)
t[v].go[c - 'a'] = t[v].next[c - 'a'];
else
t[v].go[c - 'a'] = v == 0 ? 0 : go(get_link(v), c);
return t[v].go[c - 'a'];
}
vector<int> dp, tim, sz;
vector<vector<int>> g;
int id;
STree st;
int solve(int pos) {
int &r = dp[pos];
if (r >= 0) return r;
r = t[pos].leaf;
if (pos) r += solve(get_link(pos));
return r;
}
int dfs(int pos) {
tim[pos] = id++;
sz[pos] = 1;
for (auto x : g[pos]) sz[pos] += dfs(x);
return sz[pos];
}
void prec() {
id = 0;
dp.resize(int(t.size()), -1);
tim.resize(int(t.size()));
sz.resize(int(t.size()));
g.resize(int(t.size()));
for (int i = 1, ThxDem = int(t.size()); i < ThxDem; ++i) {
g[get_link(i)].push_back(i);
}
dfs(0);
st = STree(int(t.size()));
}
int get(string &s) {
int now = 0, ans = 0;
for (auto x : s) {
now = go(now, x);
ans += solve(now);
}
return ans;
}
void push(string &s) {
if (!int(t.size())) return;
int now = 0;
for (auto x : s) {
now = go(now, x);
st.upd(tim[now], 1);
}
}
void clean(string &s) {
if (!int(t.size())) return;
int now = 0;
for (auto x : s) {
now = go(now, x);
st.upd(tim[now], -1);
}
}
};
const int MAGIC = 320;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vector<string> s(n);
vector<vector<pair<pair<int, int>, int>>> ops(n);
for (int i = 0, ThxDem = n; i < ThxDem; ++i) cin >> s[i];
for (int i = 0, ThxDem = q; i < ThxDem; ++i) {
int l, r, k;
cin >> l >> r >> k;
l--;
r--;
k--;
ops[k].push_back({{l, r}, i});
}
vector<pair<int, int>> wh(n);
int tot = (n + MAGIC - 1) / MAGIC;
vector<Aho> my(tot);
for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
wh[i] = {i / MAGIC, my[i / MAGIC].add_string(s[i])};
}
for (int i = 0, ThxDem = tot; i < ThxDem; ++i) my[i].prec();
vector<long long> ans(q);
for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
vector<int> ne;
vector<int> in(tot);
for (auto x : ops[i]) {
int l = x.first.first, r = x.first.second, id = x.second;
ne.push_back(r / MAGIC);
if (l) ne.push_back((l - 1) / MAGIC);
}
sort(ne.begin(), ne.end());
ne.erase(unique(ne.begin(), ne.end()), ne.end());
for (auto x : ne) my[x].push(s[i]);
for (int j = 0, ThxDem = tot; j < ThxDem; ++j) in[j] = my[j].get(s[i]);
for (auto x : ops[i]) {
int l = x.first.first, r = x.first.second, id = x.second;
int en = r / MAGIC;
for (int x = 0, ThxDem = en; x < ThxDem; ++x) ans[id] += in[x];
for (int j = en * MAGIC, ThxDem = r + 1; j < ThxDem; ++j) {
int tt = my[wh[j].first].tim[wh[j].second];
int zz = my[wh[j].first].sz[wh[j].second];
ans[id] += my[wh[j].first].st.query(tt, tt + zz);
}
if (l) {
en = (l - 1) / MAGIC;
for (int x = 0, ThxDem = en; x < ThxDem; ++x) ans[id] -= in[x];
for (int j = en * MAGIC, ThxDem = l; j < ThxDem; ++j) {
int tt = my[wh[j].first].tim[wh[j].second];
int zz = my[wh[j].first].sz[wh[j].second];
ans[id] -= my[wh[j].first].st.query(tt, tt + zz);
}
}
}
for (auto x : ne) my[x].clean(s[i]);
}
for (int i = 0, ThxDem = q; i < ThxDem; ++i) cout << ans[i] << "\n";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:240000000")
const int INF = 2147483647;
const long long LLINF = 9223372036854775807LL;
const int ST = 100010;
const int ST1 = 1000010;
bool myf2(int i, int j) { return (i > j); }
int ABS(int a) {
if (a < 0)
return a * (-1);
else
return a;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
ios_base::sync_with_stdio(0);
string s1, s2;
cin >> s1 >> s2;
vector<int> mas;
int n1 = (int)((s1).size());
int n2 = (int)((s2).size());
int q1 = n1;
int q2 = n2;
int ee = min(q1, q2);
for (int i = 1; i <= sqrt(1.0 * ee); i++) {
if (q1 % i == 0 && q2 % i == 0) mas.push_back(i);
if (ee % i == 0 && q1 % (ee / i) == 0 && q2 % (ee / i) == 0 && ee / i != i)
mas.push_back(ee / i);
}
int n = (int)((mas).size());
long long ans = 0;
for (int i = 0; i < n; ++i) {
bool pr1 = true, pr2 = true;
int w = mas[i];
string ss1 = "", ss2 = "";
for (int j = 0; j < w; ++j) {
ss1 += s1[j];
ss2 += s2[j];
}
if (ss1 != ss2)
continue;
else {
int kl1 = (int)((s1).size()) / (int)((ss1).size());
int kl2 = (int)((s2).size()) / (int)((ss2).size());
int f1 = (int)((ss1).size());
int f2 = (int)((ss2).size());
int e = 1;
while (f1 * e < n1) {
string sb = s1.substr(f1 * e, f1);
if (ss1 != sb) {
pr1 = false;
break;
}
e++;
}
if (pr1) {
e = 1;
while (f2 * e < n2) {
string sb = s2.substr(f2 * e, f2);
if (ss2 != sb) {
pr2 = false;
break;
}
e++;
}
}
if (pr1 && pr2) ans++;
}
}
cout << ans;
}
| 4 |