solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
char A[1000010];
char B[1000010];
char C[1000010];
int dyn[1000000];
char dir[4] = {'N', 'E', 'W', 'S'};
int main() {
int n;
scanf("%d", &n);
scanf("%s", A);
scanf("%s", B);
n--;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
if (B[i] == dir[j]) C[n - 1 - i] = dir[3 - j];
}
}
dyn[0] = 0;
dyn[1] = 0;
for (int i = 2; i < n; i++) {
int bef = dyn[i - 1];
while (bef != 0 && C[bef] != C[i - 1]) {
bef = dyn[bef];
}
if (C[bef] == C[i - 1]) {
dyn[i] = bef + 1;
} else {
dyn[i] = 0;
}
}
int cur = 0;
for (int i = 0; i < n; i++) {
if (C[cur] == A[i]) {
cur++;
} else {
while (C[cur] != A[i] && cur != 0) {
cur = dyn[cur];
}
if (C[cur] == A[i]) {
cur++;
}
}
}
if (cur > 0)
printf("NO");
else
printf("YES");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char a[111];
int minn(int a, int b) {
if (a > b) {
return b;
} else {
return a;
}
}
int main() {
int i, k, r;
while (~scanf("%s", &a[1])) {
a[0] = 'a';
int l = strlen(a);
int sum = 0, min;
for (int i = 1; i < l; i++) {
min = minn(abs(a[i] - a[i - 1]), 26 - abs(a[i] - a[i - 1]));
sum += min;
}
cout << sum << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
if (n == 1) {
cout << "0" << endl;
} else if (n == 2) {
cout << m << endl;
} else {
cout << 2 * m << endl;
}
}
}
| 1 |
#include<iostream>
#include<algorithm>
#include<cmath>
#define LL long long
LL n, s;
int main() {
std::cin >> n >> s;
int q = std::sqrt(n);
if (n == s) {
std::cout << s + 1 << std::endl;
return 0;
}
for (LL i = 2; i <= q; i++) {
LL copy = n;
LL sum = 0;
while (copy) {
sum += copy % i;
copy /= i;
}
if (s == sum) {
std::cout << i << std::endl;
return 0;
}
}
for (int i = q; i > 0; i--) {
LL b = (n - s) / i + 1;
LL sum = i + (n - i * b);
if (b <= i || n - i * b >= b || n - i * b < 0)continue;
if (s == sum) {
std::cout << b << std::endl;
return 0;
}
}
std::cout << -1 << std::endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
return b ? gcd(b, a % b) : a;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
long long h[100000];
long long l[100000];
long long r[100000];
int main() {
long long n;
cin >> n;
for (long long i = 0; i < n; ++i) cin >> h[i];
l[0] = h[0] > 0 ? 1 : 0;
for (long long i = 1; i < n; ++i) l[i] = min(l[i - 1] + 1, h[i]);
r[n - 1] = h[n - 1] > 0 ? 1 : 0;
for (long long i = n - 2; i >= 0; --i) r[i] = min(r[i + 1] + 1, h[i]);
long long m = 0;
for (long long i = 0; i < n; ++i) {
m = max(m, min(l[i], r[i]));
}
cout << m << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string a, b;
cin >> a >> b;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int cnt = 0;
while (cnt != a.size() && cnt != b.size() && a[cnt] == b[cnt]) {
cnt++;
}
cout << a.size() - cnt + b.size() - cnt << endl;
return 0;
}
| 2 |
#include <stdio.h>
int main(){
int i,j,n,m,a,b,c;
int matrixA[101][101];
int matrixB[101];
scanf("%d %d",&n,&m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&a);
matrixA[i][j] = a;
}
}
for(j=0;j<m;j++){
scanf("%d",&b);
matrixB[j] = b;
}
c=0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
c += matrixA[i][j] * matrixB[j];
}
printf("%d\n",c);
c=0;
}
} | 0 |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int a[100][10010];
int maxn=0;
void dfs(int x)
{
if(x==n+1)
{
int t=0;
for(int i=0;i<m;i++)
{
int s=0;
for(int j=0;j<n;j++)
if(a[j][i]==1)
s++;
t+=max(s,n-s);
}
maxn=max(maxn,t);
return ;
}
dfs(x+1);
for(int j=0;j<m;j++)
{
a[x][j]=!a[x][j];
}
dfs(x+1);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&(n+m))
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
scanf("%d",&a[i][j]);
}
dfs(0);
printf("%d\n",maxn);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string s;
int i;
int main() {
cin >> s;
for (i = 0; i < s.size(); i++)
if (s[i] == '0') {
s.erase(i, 1);
cout << s;
return 0;
}
for (i = 1; i < s.size(); i++) cout << s[i];
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
const int maxn = 510000;
int n, K;
bool flag;
char A[maxn], B[maxn];
namespace Ninit {
void init() {
int i;
scanf("%d%s", &K, A + 1);
n = strlen(A + 1);
for (i = n; i >= 1; --i)
if (A[i] == 'z')
A[i] = 'a';
else {
++A[i];
break;
}
i ? flag = true : flag = false;
}
} // namespace Ninit
namespace Nsolve {
int G[maxn], F[maxn], pw[maxn];
int ok(int y, int l) {
int x = y - l;
if (x < 0) return true;
return (G[y] - G[x] * pw[l]) * pw[x + 1] != F[y] - F[x];
}
bool dfs(int x, bool y) {
if (x > n) return true;
char i;
for (i = y ? 'a' : A[x]; i <= 'z'; ++i) {
B[x] = i, F[x] = F[x - 1] + i * pw[x], G[x] = G[x - 1] * P + i;
if (ok(x, K) && ok(x, K + 1) && dfs(x + 1, y || i > A[x])) return true;
}
return false;
}
void solve() {
int i;
if (!flag) return printf("Impossible\n"), void();
for (pw[0] = 1, i = 1; i <= n; ++i) pw[i] = pw[i - 1] * P;
dfs(1, 0) ? printf("%s\n", B + 1) : printf("Impossible\n");
}
} // namespace Nsolve
int main() {
Ninit::init();
Nsolve::solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void smax(T &x, T y) {
x = max((x), (y));
}
template <class T>
inline void smin(T &x, T y) {
x = min((x), (y));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
long long a, b, c, d, t = 0, l, ans;
for (int i = 0; i < n; i++) {
t = 0;
cin >> a >> b >> c >> d;
ans = (a / b) - d / b + (c - 1) / b;
cout << ans << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
vector<vector<int> > v(100001);
vector<int> vis(100001);
long long binpow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res % mod;
}
int dfs(int src) {
stack<int> st;
int cnt = 0;
st.push(src);
vis[src] = 1;
cnt++;
while (!st.empty()) {
int x = st.top();
st.pop();
if (!vis[x]) {
cnt++;
vis[x] = 1;
}
for (int i = 0; i < v[x].size(); i++) {
if (vis[v[x][i]] == 0) st.push(v[x][i]);
}
}
return cnt;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
fill(vis.begin(), vis.end(), 0);
for (int i = 0; i < v.size(); i++) v[i].clear();
for (int i = 0; i < n - 1; i++) {
int x, y, c;
cin >> x >> y >> c;
if (c == 0) {
v[x].push_back(y);
v[y].push_back(x);
}
}
long long ans = binpow(n, k);
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
ans = (mod + ans - binpow(dfs(i), k)) % mod;
}
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
const int inf = 0x3f3f3f3f;
int n, m;
int g[maxn][maxn];
int d[maxn][maxn];
int cnt[maxn][maxn];
int ans[maxn][maxn];
int main() {
int a, b, c;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i != j)
g[i][j] = inf;
else
g[i][j] = 0;
while (m--) {
scanf("%d%d%d", &a, &b, &c);
g[a][b] = g[b][a] = min(g[a][b], c);
}
memcpy(d, g, sizeof g);
memset(cnt, 0, sizeof cnt);
memset(ans, 0, sizeof ans);
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (i != j && k != j)
if (d[i][j] != inf && d[i][k] + g[k][j] == d[i][j]) cnt[i][j]++;
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (d[i][j] != inf && d[i][k] + d[k][j] == d[i][j])
ans[i][j] += cnt[i][k];
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j) printf("%d ", ans[i][j]);
puts("");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const string NAME = "645D";
const int INF = 1e9 + 7;
const int N = 1e5 + 7;
int n, m, f[N], L[N], Time;
bool vis[N];
vector<pair<int, int> > a[N];
void Enter() {
cin >> n >> m;
int u, v;
for (int i = (1), _b = (m); i <= _b; ++i) {
cin >> u >> v;
a[v].push_back(pair<int, int>(u, i));
}
}
void DFS(int u) {
vis[u] = true;
for (int i = 0, _a = (((int)(a[u]).size())); i < ((int)(a[u]).size()); ++i)
if (!vis[a[u][i].first]) DFS(a[u][i].first);
L[Time--] = u;
}
void TopoSort() {
Time = n;
for (int i = (1), _b = (n); i <= _b; ++i)
if (!vis[i]) DFS(i);
}
bool Check(int T) {
memset(f, 0, sizeof(f));
for (int z = (1), _b = (n); z <= _b; ++z) {
int u = L[z];
for (int i = 0, _a = (((int)(a[u]).size())); i < ((int)(a[u]).size()); ++i)
if (a[u][i].second <= T) {
int v = a[u][i].first;
f[v] = max(f[v], f[u] + 1);
}
}
return *max_element(f + 1, f + 1 + n) == n - 1;
}
void Process() {
TopoSort();
int l = 0, h = m + 1;
while (l + 1 < h) {
int mid = (l + h) / 2;
if (Check(mid))
h = mid;
else
l = mid;
}
if (h == m + 1)
cout << -1;
else
cout << h;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Enter();
Process();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n], i;
for (i = 0; i < n; i++) {
cin >> a[i];
}
int x = 0;
int m = a[n - 1];
for (i = n - 2; i >= 0; i--) {
if (a[i] > m) x++;
m = min(m, a[i]);
}
cout << x << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, mini, res, R;
cin >> n >> mini >> R;
res = R - mini;
mini = min(mini, R);
for (int i = 2; i < n; i++) {
cin >> R;
res = max(res, R - mini);
mini = min(mini, R);
}
cout << res << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) {
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int main() {
int n;
cin >> n;
vector<string> vect1, vect2;
vect1.push_back("Power");
vect1.push_back("Time");
vect1.push_back("Space");
vect1.push_back("Soul");
vect1.push_back("Reality");
vect1.push_back("Mind");
vect2.push_back("purple");
vect2.push_back("green");
vect2.push_back("blue");
vect2.push_back("orange");
vect2.push_back("red");
vect2.push_back("yellow");
int arr[6];
for (int i = 0; i < 6; i++) {
arr[i] = 0;
}
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < 6; j++) {
if (vect2[j] == s) {
arr[j] = 1;
break;
}
}
}
int count = 0;
for (int i = 0; i < 6; i++) {
if (arr[i] == 0) {
count += 1;
}
}
cout << count << "\n";
for (int i = 0; i < 6; i++) {
if (arr[i] == 0) {
cout << vect1[i] << "\n";
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int hash[26] = {0};
string str;
cin >> str;
int flag = 0;
if (n == 1) {
cout << "Yes" << endl;
return 0;
}
for (int i = 0; i < n; i++) {
hash[str[i] - 'a']++;
if (hash[str[i] - 'a'] == 2) {
flag = 1;
break;
}
}
if (flag == 0) {
cout << "No" << endl;
} else
cout << "Yes" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct edge {
int to, next;
} e1[300005], e2[300005];
int idx1[150005], idx2[150005];
int top1 = 0;
int top2 = 0;
int color[150005];
int cnt[150005];
int n, m;
int dis[150005];
void add1(int x, int y) {
e1[++top1].to = y;
e1[top1].next = idx1[x];
idx1[x] = top1;
}
void add2(int x, int y) {
e2[++top2].to = y;
e2[top2].next = idx2[x];
idx2[x] = top2;
}
void dfs(int start, int fa) {
for (int i = idx2[start]; i; i = e2[i].next) {
int p = e2[i].to;
if (p != fa) {
dis[p] = dis[start] + 1;
dfs(p, start);
}
}
}
int q[150005];
void work1() {
int head = 0;
int tail = 0;
for (int i = 1; i <= n; i++) {
if (color[i] == 0 && cnt[i] == 1) {
q[tail++] = i;
color[i] = 2;
}
}
while (head < tail) {
int p = q[head++];
for (int i = idx1[p]; i; i = e1[i].next) {
int dd = e1[i].to;
if (color[dd] == 0) {
cnt[dd]--;
if (cnt[dd] == 1) {
q[tail++] = dd;
color[dd] = 2;
}
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
add1(x, y);
add1(y, x);
cnt[x]++;
cnt[y]++;
}
for (int i = 1; i <= m; i++) {
int x;
scanf("%d", &x);
color[x] = 1;
}
work1();
for (int i = 1; i <= n; i++) {
if (color[i] != 2) {
for (int j = idx1[i]; j; j = e1[j].next) {
int p = e1[j].to;
if (color[p] != 2) add2(i, p);
}
}
}
int start = -1;
for (int i = 1; i <= n; i++)
if (color[i] == 1) {
start = i;
break;
}
memset(dis, -1, sizeof(dis));
dis[start] = 0;
dfs(start, 0);
int maxx = 0, pos = start;
for (int i = 1; i <= n; i++) {
if (dis[i] > maxx) {
maxx = dis[i];
pos = i;
}
}
memset(dis, -1, sizeof(dis));
dis[pos] = 0;
dfs(pos, 0);
maxx = 0;
int ans1 = pos;
for (int i = 1; i <= n; i++) {
if (dis[i] > maxx) {
maxx = dis[i];
pos = i;
}
}
for (int i = 1; i <= n; i++) {
if (dis[i] == maxx) {
ans1 = min(ans1, i);
}
}
printf("%d\n%d\n", ans1, top2 - maxx);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, inp[1001000][2], firstNotChange[1001000], f[1001000];
string st[1001000];
void dfs(int u) {
if (st[u] == "IN")
f[u] = inp[u][0];
else {
dfs(inp[u][0]);
if (st[u] != "NOT") dfs(inp[u][1]);
if (st[u] == "AND") f[u] = f[inp[u][0]] & f[inp[u][1]];
if (st[u] == "OR") f[u] = f[inp[u][0]] | f[inp[u][1]];
if (st[u] == "XOR") f[u] = f[inp[u][0]] ^ f[inp[u][1]];
if (st[u] == "NOT") f[u] = 1 - f[inp[u][0]];
}
}
void dfs2(int u, int p) {
if (p != -1) {
if (st[p] == "IN") return;
if (st[p] == "NOT") {
firstNotChange[u] = firstNotChange[p];
dfs2(inp[u][0], u);
if (st[u] != "NOT") dfs2(inp[u][1], u);
} else {
int v = inp[p][0], g;
if (v == u) v = inp[p][1];
if (st[p] == "AND") g = f[v] & (1 - f[u]);
if (st[p] == "OR") g = f[v] | (1 - f[u]);
if (st[p] == "XOR") g = f[v] ^ (1 - f[u]);
if (f[p] == g)
firstNotChange[u] = p;
else
firstNotChange[u] = firstNotChange[p];
dfs2(inp[u][0], u);
if (st[u] != "NOT") dfs2(inp[u][1], u);
}
} else {
dfs2(inp[u][0], u);
if (st[u] != "NOT") dfs2(inp[u][1], u);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = (1); i <= (n); ++i) {
cin >> st[i];
if (st[i] == "NOT" || st[i] == "IN")
cin >> inp[i][0];
else
cin >> inp[i][0] >> inp[i][1];
}
dfs(1);
firstNotChange[1] = -1;
dfs2(1, -1);
{
cerr << "firstNotChange"
<< " = {";
for (int _ = (1); _ <= (n); ++_) cerr << ' ' << firstNotChange[_];
cerr << "}\n";
};
for (int i = (1); i <= (n); ++i)
if (st[i] == "IN") {
if (firstNotChange[i] == -1)
cout << 1 - f[1];
else
cout << f[1];
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
return out << "(" << a.first << "," << a.second << ")";
}
template <class A>
ostream &operator<<(ostream &out, const vector<A> &a) {
for (const A &it : a) out << it << " ";
return out;
}
template <class A, class B>
istream &operator>>(istream &in, pair<A, B> &a) {
return in >> a.first >> a.second;
}
template <class A>
istream &operator>>(istream &in, vector<A> &a) {
for (A &i : a) in >> i;
return in;
}
long long poww(const long long &a, long long b,
const long long &m = 1000000007) {
if (b == 0) return 1;
long long x = poww(a, b / 2, m);
x = x * x % m;
if (b & 1) x = x * a % m;
return x;
}
long long ceil(const long long &a, const long long &b) {
return (a + b - 1) / b;
}
long long n, k;
long long v[6][1001];
long long id[6][1001];
long long dp[1001][1001];
long long f(long long i, long long lst) {
if (i > n) return 0;
long long &ans = dp[i][lst];
if (ans != -(1e18)) return ans;
ans = f(i + 1, lst);
bool ok = 1;
for (long long j = 1; j <= k; j++) ok &= (id[j][v[1][lst]] <= id[j][v[1][i]]);
if (ok) ans = max(ans, 1 + f(i + 1, i));
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (long long i = 0; i < 1001; i++)
for (long long j = 0; j < 1001; j++) dp[i][j] = -(1e18);
cin >> n >> k;
for (long long i = 1; i <= k; i++) {
for (long long j = 1; j <= n; j++) {
cin >> v[i][j];
id[i][v[i][j]] = j;
}
}
cout << f(1, 0);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<bitset<100005> > g;
vector<int> f;
int import[65], n, ans[100005], cnt[100005];
long long d[100005];
void gauss(bitset<100005> A, int B) {
int m = f.size();
for (int i = 0; i < m; i++)
if (A[import[i]]) {
A ^= g[i];
B ^= f[i];
}
int index = -1;
for (int i = 1; i <= n; i++)
if (A[i]) {
index = i;
break;
}
if (index == -1) return;
import[m] = index;
for (int i = 0; i < m; i++)
if (g[i][index]) g[i] ^= A, f[i] ^= B;
g.push_back(A);
f.push_back(B);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &d[i]);
for (int i = 61; i >= 0; i--)
for (int j = 1; j <= n; j++) cnt[i] += ((d[j] >> i) & 1);
for (int i = 61; i >= 0; i--)
if (cnt[i] && !(cnt[i] & 1)) {
bitset<100005> A;
A.reset();
for (int j = 1; j <= n; j++) A[j] = ((d[j] >> i) & 1);
gauss(A, 1);
}
for (int i = 61; i >= 0; i--)
if (cnt[i] && (cnt[i] & 1)) {
bitset<100005> A;
A.reset();
for (int j = 1; j <= n; j++) A[j] = ((d[j] >> i) & 1);
gauss(A, 0);
}
int m = f.size();
for (int i = 0; i < m; i++) ans[import[i]] = f[i];
for (int i = 1; i <= n; i++) printf("%d ", 2 - ans[i]);
}
| 4 |
#include <bits/stdc++.h>
int diru[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dirv[] = {-1, 0, 1, -1, 1, -1, 0, 1};
using namespace std;
template <class T>
T sq(T n) {
return n * n;
}
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
template <class T>
bool inside(T a, T b, T c) {
return a <= b && b <= c;
}
template <class T>
void setmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
void setmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
T power(T N, T P) {
return (P == 0) ? 1 : N * power(N, P - 1);
}
int main() {
long long unsigned n, T, t = 1, m, i, j, k, x, y;
while (cin >> n) {
cin >> x >> y;
if (n > y) {
printf("-1\n");
continue;
}
long long unsigned tot = 0;
long long unsigned p = y;
for (i = 0; i < n - 1; i++) {
tot += 1;
p--;
}
tot += p * p;
if (tot >= x) {
p = y;
for (i = 0; i < n - 1; i++) {
printf("1\n");
p--;
}
printf("%llu\n", p);
} else
printf("-1\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> order, order1;
int e = 0, e1 = 0;
for (int i = 0; i < n; i++) {
int tem;
scanf("%d", &tem);
if (tem == 0)
e++;
else
order.push_back(tem);
}
for (int i = 0; i < n; i++) {
int tem;
scanf("%d", &tem);
if (tem == 0)
e1++;
else
order1.push_back(tem);
}
if (e != e1) {
cout << "NO" << endl;
return 0;
};
if (e == n) {
cout << "YES" << endl;
return 0;
};
int ind = -1;
for (int i = 0; i < order1.size(); i++)
if (order1[i] == order[0]) {
ind = i;
break;
}
if (ind == -1) {
cout << "NO" << endl;
return 0;
};
for (int i = 0; i < order1.size(); i++, ind++) {
if (ind == order.size()) ind = 0;
if (order1[ind] != order[i]) {
cout << "NO" << endl;
return 0;
};
}
{
cout << "YES" << endl;
return 0;
};
}
| 1 |
#include <bits/stdc++.h>
const int MAXN = 5e3 + 19;
const long long INF = 1e18;
int n, k, x, a[MAXN];
long long dp[2][MAXN];
int q[MAXN], h, t;
void solve(long long *f, long long *g, int m) {
for (int i = 0; i <= n; ++i) f[i] = -INF;
q[h = t = 0] = m - 1;
for (int i = m; i <= n; ++i) {
while (h <= t && i - q[h] > k) ++h;
if (h > t || g[q[h]] == -INF) break;
f[i] = g[q[h]] + a[i];
while (h <= t && g[q[t]] <= g[i]) --t;
q[++t] = i;
}
}
int main() {
std::scanf("%d%d%d", &n, &k, &x);
for (int i = 1; i <= n; ++i) std::scanf("%d", a + i);
for (int i = 1; i <= n; ++i) dp[0][i] = -INF;
for (int i = 1; i <= x; ++i) solve(dp[i & 1], dp[i & 1 ^ 1], i);
long long ans = -1;
for (int i = n - k + 1; i <= n; ++i) ans = std::max(ans, dp[x & 1][i]);
if (ans == -1)
std::puts("-1");
else
std::printf("%lld\n", ans);
return 0;
return 0;
}
| 6 |
#include <cstdio>
#include <stack>
using namespace std;
typedef struct{
int color;
int num;
} Stone;
int opcolor(int color){
return 1 - color;
}
stack<Stone> st;
int n;
int main(){
while(scanf("%d",&n), n != 0){
//init
st = stack<Stone>();
Stone init_stone = {0,0};
st.push(init_stone);
// put
for(int i = 1; i <= n; i++){
int c;
scanf("%d",&c);
if( i % 2 == 0){
Stone bef = st.top();st.pop();
if(c == bef.color){
bef.num++;
st.push(bef);
}
else{
if(!st.empty()){
Stone befbef = st.top();st.pop();
befbef.num += bef.num + 1;
st.push(befbef);
}
else{
bef.color = c;
bef.num++;
st.push(bef);
}
}
}
else{
Stone bef = st.top();st.pop();
if(c == bef.color){
bef.num++;
st.push(bef);
}
else{
st.push(bef);
Stone ns = {c,1};
st.push(ns);
}
}
}
//count white(0)
int ans = 0;
while(!st.empty()){
Stone s = st.top();st.pop();
if(s.color == 0){
ans += s.num;
}
}
printf("%d\n",ans);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const int MAX_N = 105;
struct State {
int i, j, k;
State(int _i = -1, int _j = -1, int _k = -1) : i(_i), j(_j), k(_k) {}
};
bool operator<(State s, State t) { return false; }
bool operator==(State s, State t) {
return s.i == t.i && s.j == t.j && s.k == t.k;
}
bool operator!=(State s, State t) { return !(s == t); }
pair<int, State> dp[MAX_N][MAX_N][MAX_N];
string s3;
int lps[MAX_N];
void calc_lps() {
int p = s3.size();
int len = 0;
int i = 1;
while (i < p) {
if (s3[i] == s3[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
int get_next_k(int pos, char nxt) {
pos--;
while (pos >= 0 && s3[pos + 1] != nxt) {
pos = lps[pos] - 1;
}
if (pos == -1 && s3[0] != nxt) {
return 0;
}
return pos + 2;
}
int main() {
string s1, s2;
cin >> s1 >> s2 >> s3;
int n = s1.size();
int m = s2.size();
int p = s3.size();
calc_lps();
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= p; k++) {
dp[i][j][k] = make_pair(-INF, State());
}
}
}
dp[0][0][0] = make_pair(0, State());
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < p; k++) {
dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
if (s1[i] == s2[j]) {
int nk = get_next_k(k, s1[i]);
dp[i + 1][j + 1][nk] =
max(dp[i + 1][j + 1][nk],
make_pair(dp[i][j][k].first + 1, State(i, j, k)));
}
}
}
}
pair<int, State> curpr = make_pair(-INF, State());
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
for (int k = 0; k < p; k++) {
curpr = max(curpr, dp[i][j][k]);
}
}
}
auto cur = curpr.second;
deque<char> ans;
while (cur != State()) {
ans.push_front(s1[cur.i]);
auto prv = dp[cur.i][cur.j][cur.k].second;
cur = prv;
}
if (ans.empty()) {
cout << 0 << endl;
} else {
for (char c : ans) {
cout << c;
}
cout << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long ans[100005];
int main() {
long long n, k, i;
scanf("%lld%lld", &n, &k);
if (n == k) {
printf("-1\n");
return 0;
}
for (i = 2; i <= (k + 2 - 1); i++) {
ans[i] = i;
}
long long d = n - i + 1;
long long lim;
if (d % 2 == 0)
lim = n, ans[1] = 1;
else
lim = n - 1, ans[1] = n, ans[n] = 1;
bool f = 1;
for (; i <= lim; i++) {
if (f)
ans[i] = i + 1;
else
ans[i] = i - 1;
f = !f;
}
for (i = 1; i <= n; i++) {
if (i != 1) printf(" ");
printf("%lld", ans[i]);
}
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class t, class u>
void chmax(t& first, u second) {
if (first < second) first = second;
}
template <class t, class u>
void chmin(t& first, u second) {
if (second < first) first = second;
}
template <class t>
using vc = vector<t>;
template <class t>
using vvc = vc<vc<t>>;
using pi = pair<ll, ll>;
using vi = vc<ll>;
template <class t, class u>
ostream& operator<<(ostream& os, const pair<t, u>& p) {
return os << "{" << p.first << "," << p.second << "}";
}
template <class t>
ostream& operator<<(ostream& os, const vc<t>& v) {
os << "{";
for (auto e : v) os << e << ",";
return os << "}";
}
using uint = unsigned;
using ull = unsigned long long;
template <class t, size_t n>
ostream& operator<<(ostream& os, const array<t, n>& first) {
return os << vc<t>(first.begin(), first.end());
}
template <ll i, class T>
void print_tuple(ostream&, const T&) {}
template <ll i, class T, class H, class... Args>
void print_tuple(ostream& os, const T& t) {
if (i) os << ",";
os << get<i>(t);
print_tuple<i + 1, T, Args...>(os, t);
}
template <class... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
os << "{";
print_tuple<0, tuple<Args...>, Args...>(os, t);
return os << "}";
}
template <class t>
void print(t x, ll suc = 1) {
cout << x;
if (suc == 1) cout << "\n";
if (suc == 2) cout << " ";
}
ll read() {
ll i;
cin >> i;
return i;
}
vi readvi(ll n, ll off = 0) {
vi v(n);
for (ll i = ll(0); i < ll(n); i++) v[i] = read() + off;
return v;
}
template <class T>
void print(const vector<T>& v, ll suc = 1) {
for (ll i = ll(0); i < ll(v.size()); i++)
print(v[i], i == ll(v.size()) - 1 ? suc : 2);
}
string readString() {
string s;
cin >> s;
return s;
}
template <class T>
T sq(const T& t) {
return t * t;
}
void yes(bool ex = true) {
cout << "YES"
<< "\n";
if (ex) exit(0);
}
void no(bool ex = true) {
cout << "NO"
<< "\n";
if (ex) exit(0);
}
void possible(bool ex = true) {
cout << "POSSIBLE"
<< "\n";
if (ex) exit(0);
}
void impossible(bool ex = true) {
cout << "IMPOSSIBLE"
<< "\n";
if (ex) exit(0);
}
constexpr ll ten(ll n) { return n == 0 ? 1 : ten(n - 1) * 10; }
const ll infLL = LLONG_MAX / 3;
const ll inf = infLL;
ll topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
ll topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
ll botbit(signed first) { return first == 0 ? 32 : __builtin_ctz(first); }
ll botbit(ll first) { return first == 0 ? 64 : __builtin_ctzll(first); }
ll popcount(signed t) { return __builtin_popcount(t); }
ll popcount(ll t) { return __builtin_popcountll(t); }
bool ispow2(ll i) { return i && (i & -i) == i; }
ll mask(ll i) { return (ll(1) << i) - 1; }
bool inc(ll first, ll second, ll c) { return first <= second && second <= c; }
template <class t>
void mkuni(vc<t>& v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
ll rand_int(ll l, ll r) {
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<ll>(l, r)(gen);
}
template <class t>
void myshuffle(vc<t>& first) {
for (ll i = ll(0); i < ll(ll(first.size())); i++)
swap(first[i], first[rand_int(0, i)]);
}
template <class t>
ll lwb(const vc<t>& v, const t& first) {
return lower_bound(v.begin(), v.end(), first) - v.begin();
}
ll getfreq(string s) {
ll res = 0;
for (auto c : s) {
ll v = c - 'a';
if (res & 1 << v) no();
res += 1 << v;
}
return res;
}
string mg(string s, string t) {
for (ll _ = ll(0); _ < ll(2); _++) {
for (ll i = ll(0); i < ll(ll(s.size())); i++) {
string cur = s;
for (ll j = ll(0); j < ll(ll(t.size())); j++) {
if (i + j < ll(s.size())) {
if (s[i + j] == t[j]) {
} else {
cur = "";
break;
}
} else {
cur += t[j];
}
}
if (ll(cur.size())) return cur;
}
swap(s, t);
}
no();
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
ll n;
cin >> n;
if (n > 500) {
no();
}
vc<string> ss;
vi has;
for (ll i = ll(0); i < ll(n); i++) {
string s;
cin >> s;
ss.push_back(s);
has.push_back(getfreq(s));
}
while (1) {
for (ll i = ll(0); i < ll(n); i++) {
for (ll j = ll(0); j < ll(i); j++)
if (has[i] & has[j]) {
ss[i] = mg(ss[i], ss[j]);
has[i] |= has[j];
ss.erase(ss.begin() + j);
has.erase(has.begin() + j);
n--;
goto waf;
}
}
break;
waf:;
}
sort(ss.begin(), ss.end());
string ans;
for (auto s : ss) ans += s;
getfreq(ans);
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
int count(const std::string& s) {
int c = 0;
std::string v = "aiueo";
int n = s.size();
for (int i = 0; i < n; ++i) {
if (v.find(s[i]) != std::string::npos) {
++c;
}
}
return c;
}
int main() {
std::string line1;
std::getline(std::cin, line1);
std::string line2;
std::getline(std::cin, line2);
std::string line3;
std::getline(std::cin, line3);
if (count(line1) == 5 && count(line2) == 7 && count(line3) == 5) {
std::cout << "YES";
} else {
std::cout << "NO";
}
std::cout << std::endl;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int INF = (int)1e9 + 7;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
int main() {
int n;
long long p;
cin >> n >> p;
vector<pair<long long, long long> > a(n);
for (int i = 0; i < (int)n; ++i) cin >> a[i].first >> a[i].second;
long long kol1, kol2, kol;
double ans = 0;
cout.precision(10);
for (int i = 0; i < (int)n - 1; ++i) {
kol1 = a[i].second / p - a[i].first / p + !(a[i].first % p);
kol2 = a[i + 1].second / p - a[i + 1].first / p + !(a[i + 1].first % p);
kol = kol1 * (a[i + 1].second - a[i + 1].first + 1) +
kol2 * (a[i].second - a[i].first + 1) - kol1 * kol2;
ans += kol * 1000.0 /
((a[i + 1].second - a[i + 1].first + 1) *
(a[i].second - a[i].first + 1));
}
kol1 = a[0].second / p - a[0].first / p + !(a[0].first % p);
kol2 = a[n - 1].second / p - a[n - 1].first / p + !(a[n - 1].first % p);
kol = kol1 * (a[n - 1].second - a[n - 1].first + 1) +
kol2 * (a[0].second - a[0].first + 1) - kol1 * kol2;
ans +=
kol * 1000.0 /
((a[n - 1].second - a[n - 1].first + 1) * (a[0].second - a[0].first + 1));
cout << fixed << ans * 2;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int cnt[205];
vector<int> v[205];
vector<int>::iterator v1, v2;
int main() {
int q;
cin >> q;
while (q--) {
int n, i, j, ans = 0, x, y, a, b, k1, k2, ind1, ind2;
cin >> n;
int ara[n + 1];
map<int, int> mp;
for (i = 1; i <= n; i++) {
scanf("%d", &ara[i]);
cnt[ara[i]]++;
mp[ara[i]] = 1;
v[ara[i]].push_back(i);
}
for (i = 1; i <= 201; i++) {
ans = max(ans, cnt[i]);
}
for (i = 1; i <= 200; i++) {
for (j = 1; j <= 200; j++) {
if (i == j) {
continue;
}
if (v[i].size() < 2 || v[j].size() < 1) {
continue;
}
x = i;
y = j;
a = 0;
b = v[i].size() - 1;
for (a = 0; a < b; a++, b--) {
v1 = upper_bound(v[j].begin(), v[j].end(), v[i][a]);
v2 = lower_bound(v[j].begin(), v[j].end(), v[i][b]);
int l = (v2 - v1);
ans = max(ans, l + 2 * (a + 1));
}
}
}
printf("%d\n", ans);
memset(cnt, 0, sizeof(cnt));
for (i = 1; i <= 200; i++) {
v[i].clear();
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
int x, y;
long long r = 0;
scanf("%d%d%d%d", &n, &x, &m, &y);
r += n + m + 1;
if (x > y) {
swap(x, y);
swap(m, n);
}
for (int i = 1; i <= n; i++) {
if (x + i <= y - m || x + i >= y + m) continue;
if (x + i > y) {
int idis = i - (y - x);
r += 2 * (min(m, 2 * i - 1 - idis) - idis);
} else {
int idis = y - x - i;
r += 2 * min(m - idis, 2 * i - 1) - 1;
}
}
cout << r << endl;
return 0;
}
| 3 |
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
struct node
{
int x,y;
}a[100010];
int n,m;
int f[200010][2];
bool cmp(node x,node y)
{
return x.x<y.x;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+1,a+m+1,cmp);
for (int i=1;i<=m;i++)
{
int t=a[i].y;
f[t][1]=max(f[t+1][1]+1,f[t][1]);
f[t+1][0]=max(f[t][0]+1,f[t+1][0]);
}
printf("%d",f[1][0]+f[1][1]+1);
for (int i=2;i<=n;i++)
{
printf(" %d",f[i][0]+f[i][1]+1);
}
printf("\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int asked = 0;
int ask(int dis) {
printf("%d\n", dis);
fflush(stdout);
int result;
scanf("%d", &result);
asked++;
return result;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
int round[30], r = 0;
for (int i = 0; i < m; i++, r = (r + 1) % m) {
int ans = ask(n);
if (ans == 0) exit(0);
round[r] = ans == -1 ? 1 : -1;
}
int max = n + 1;
int min = 1;
while (asked != 60) {
int mid = (max + min) / 2;
int result = ask(mid) * round[r];
r = (r + 1) % m;
if (result == 1)
min = mid + 1;
else if (result == -1)
max = mid - 1;
else if (result == 0)
exit(0);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 400 + 100;
int a[maxn], b[maxn], sto[maxn];
struct node {
int le;
int ri;
} q[maxn];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= m; i++) {
scanf("%d %d", &q[i].le, &q[i].ri);
}
int maxx = -0x3f3f3f3f, id = 0;
for (int i = 1; i <= n; i++) {
int t1 = -0x3f3f3f3f, t2 = 0x3f3f3f3f;
for (int j = 1; j <= n; j++) {
b[j] = a[j];
}
for (int j = 1; j <= m; j++) {
if (i >= q[j].le && i <= q[j].ri) continue;
for (int k = q[j].le; k <= q[j].ri; k++) {
b[k] -= 1;
}
}
for (int j = 1; j <= n; j++) {
t1 = max(t1, b[j]);
t2 = min(t2, b[j]);
}
if (t1 - t2 > maxx) {
maxx = t1 - t2;
id = i;
}
}
printf("%d\n", maxx);
int num = 0;
for (int i = 1; i <= m; i++) {
if (id >= q[i].le && id <= q[i].ri) continue;
sto[++num] = i;
}
printf("%d\n", num);
for (int i = 1; i <= num; i++) {
if (i == 1)
printf("%d", sto[i]);
else
printf(" %d", sto[i]);
}
printf("\n");
return 0;
}
| 5 |
#include <stdio.h>
#include <algorithm>
using namespace std;
int n;
int m;
bool check[7368792];
int go;
int x;
void reset(){
for(x=0;x<=7368791;x++) check[x] = 0;
}
int i;
int main(){
while(1){
scanf("%d%d",&m,&n);
if(n==0 && m==0) break;
reset();
go = m;
while(n--){
for(i=1;go*i<=7368791;i++){
check[go*i] = 1;
}
while(check[go]) go++;
}
printf("%d\n",go);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long sod(long long inp) {
long long l = inp;
long long s = 0;
while (l != 0) {
s += l % 10;
l = l / 10;
}
return s;
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, s;
cin >> n >> s;
long long beg = n + 1;
for (long long i = s + 1; i <= n; i++) {
long long k = sod(i);
if (k <= i - s) {
beg = i;
break;
}
}
cout << n + 1 - beg << "\n";
return 0;
}
| 3 |
#include <iostream>
using namespace std;
typedef long long ll;
int main() {
ll x,k,d;
cin>>x>>k>>d;
x=abs(x);
ll ans;
if(k<=(x/d)){
ans=x-d*k;
}
else{
if((k-(x/d))%2){
ans=d-x%d;
}
else{
ans=x%d;
}
}
cout<<ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int n, count = 0;
scanf("%d", &n);
while (n--) {
int t = 3, s = 0;
while (t--) {
int a;
scanf("%d", &a);
s = s + a;
}
if (s > 1) {
count++;
}
}
printf("%d", count);
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 200010
using namespace std;
ll n,num[N][30];
string s[N],t[N],st;
vector<pair<string,ll> > vt;
int main(){
ll i,j,l,ans=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>s[i];
t[i]=s[i];
reverse(t[i].begin(),t[i].end());
t[i].erase(t[i].end()-1);
vt.push_back(make_pair(t[i],i));
}
sort(vt.begin(),vt.end());
for(i=0;i<(ll)vt.size();i++)
{
l=vt[i].S;
for(j=0;j<(ll)s[l].size();j++)
{
num[i][s[l][j]-'a']++;
}
}
for(i=0;i<(ll)vt.size();i++)
{
st=vt[i].F+'{';
pair<string,ll> pp=make_pair(st,-LINF);
l=upper_bound(vt.begin(),vt.end(),pp)-vt.begin()-1;
for(j=i+1;j<=l;j++)
{
if(num[j][s[vt[i].S][0]-'a']>=num[i][s[vt[i].S][0]-'a'])
{
ans++;
}
}
}
cout<<ans<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
long long h[N], p[N];
long long n, k, m;
long long phi(long long n) {
long long x = n, y = n;
for (long long i = 1; p[i] * p[i] <= n; i++)
if (y % p[i] == 0) {
x -= x / p[i];
while (y % p[i] == 0) y /= p[i];
}
if (y > 1) x -= x / y;
return x;
}
int main() {
cin >> n >> k;
for (int i = 2; i <= N - 1; i++)
if (!h[i]) {
for (int j = i; j + i < N; h[j + i] = 1, j += i)
;
p[++m] = i;
}
k = (k + 1) / 2;
for (int i = 1; i <= k; i++) {
n = phi(n);
if (n == 1) break;
}
cout << n % 1000000007;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int OFFSET = 5;
const int MAXN = 1000000;
int N;
int heights[MAXN], r[MAXN], l[MAXN], c[MAXN];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &heights[i]);
}
rotate(heights, max_element(heights, heights + N), heights + N);
heights[N] = heights[0];
for (int i = N - 1; i >= 0; i--) {
r[i] = i + 1;
while (r[i] < N && heights[i] > heights[r[i]]) {
r[i] = r[r[i]];
}
if (r[i] < N && heights[i] == heights[r[i]]) {
c[i] = c[r[i]] + 1;
r[i] = r[r[i]];
}
}
for (int i = 1; i <= N; i++) {
l[i] = i - 1;
while (l[i] > 0 && heights[i] >= heights[l[i]]) {
l[i] = l[l[i]];
}
}
long long ans = 0;
for (int i = 0; i < N; i++) {
ans += c[i];
if (heights[i] < heights[0]) {
if (l[i] == 0 && r[i] == N) {
ans++;
} else {
ans += 2;
}
}
}
printf("%I64d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
int main() {
int n, m, i, a[200], l = 0, j, temp, sum = 0, min;
scanf("%d", &n);
scanf("%d", &m);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
min = i;
for (j = i + 1; j < n; j++) {
if (a[j] < a[min]) {
min = j;
}
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
for (i = n - 1; i >= 0; i--) {
sum = sum + a[i];
++l;
if (sum >= m) {
break;
}
}
printf("%d", l);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
string S,T;
cin>>S>>T;
int X=-1;
for(int i=0;i<S.size()-T.size()+1;i++){
bool ans=true;
for(int j=0;j<T.size();j++){
if(S.at(i+j)!=T.at(j)&&S.at(i+j)!='?') ans=false;
}
if(ans) X=i;
}
if(X==-1) cout<<"UNRESTORABLE"<<endl;
else{
for(int i=0;i<S.size();i++){
if(S.at(i)=='?') S.at(i)='a';
}
for(int i=X;i<X+T.size();i++){
S.at(i)=T.at(i-X);
}
cout<<S<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 50;
const int MOD = 1e9 + 9;
int n, k, a, b, c, d, m, L, R, tot = 1;
pair<int, int> cities[MAXN];
struct TreeNode {
int x, l, r, lson, rson;
pair<int, int> v;
} tree[MAXN];
int newNode(int &x) { return x = ++tot; }
pair<int, int> calc(pair<int, int> l, pair<int, int> r) {
return {l.second + r.second, max(l.first + r.second, l.second + r.first) + 1};
}
void build(int o, int l, int r) {
int max_y = -1, max_id = l;
for (int i = l; i <= r; i++) {
if (cities[i].second > max_y) {
max_y = cities[i].second;
max_id = i;
}
}
tree[o].l = cities[l].first;
tree[o].r = cities[r].first;
tree[o].x = cities[max_id].first;
if (l < max_id) build(newNode(tree[o].lson), l, max_id - 1);
if (max_id < r) build(newNode(tree[o].rson), max_id + 1, r);
tree[o].v = calc(tree[tree[o].lson].v, tree[tree[o].rson].v);
}
pair<int, int> query(int o, int l, int r) {
if (o == 0 || l > tree[o].r || r < tree[o].l) return {-1, 0};
if (l <= tree[o].l && tree[o].r <= r) return tree[o].v;
if (r < tree[o].x) return query(tree[o].lson, l, r);
if (l > tree[o].x) return query(tree[o].rson, l, r);
return calc(query(tree[o].lson, l, r), query(tree[o].rson, l, r));
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++) {
scanf("%d%d", &cities[i].first, &cities[i].second);
}
scanf("%d%d%d%d", &a, &b, &c, &d);
for (int i = k; i < n; i++) {
cities[i].first = (1LL * a * cities[i - 1].first + b) % MOD;
cities[i].second = (1LL * c * cities[i - 1].second + d) % MOD;
}
sort(cities, cities + n);
tree[0].v = {-1, 0};
build(1, 0, n - 1);
scanf("%d", &m);
while (m--) {
scanf("%d%d", &L, &R);
printf("%d\n", query(1, L, R).second);
}
}
| 4 |
#include<iostream>
#include<cstdio>
#include<cstring>
const int N=105;
int s[N][N];
int n,m,c;
void go(int &x,int &y)
{
if(x&1)
{
y++;
if(y>m)y=m,x++;
}
else
{
y--;
if(!y)y=1,x++;
}
}
int main()
{
scanf("%d%d%d",&n,&m,&c);
for(int i=1,cnt,x=1,y=1;i<=c;i++)
{
for(scanf("%d",&cnt);cnt--;go(x,y))
s[x][y]=i;
}
for(int i=1;i<=n;i++,printf("\n"))
for(int j=1;j<=m;j++)printf("%d ",s[i][j]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100, mod = 1e9 + 7;
int n, q, cnt[55], dp[N], f[N], fac[N], inv[N], res[55][55];
char s[N];
int id(char c) { return 'A' <= c && c <= 'Z' ? c - 'A' : c - 'a' + 26; }
int ksm(int x, int y) {
int s = 1;
for (; y; y >>= 1, x = (long long)x * x % mod)
if (y & 1) s = (long long)s * x % mod;
return s;
}
void init(int n) {
fac[0] = 1;
for (int i = (1); i <= (n); i++) fac[i] = (long long)fac[i - 1] * i % mod;
inv[n] = ksm(fac[n], mod - 2);
for (int i = n - 1; ~i; i--) inv[i] = (long long)inv[i + 1] * (i + 1) % mod;
}
int main() {
scanf("%s%d", s + 1, &q);
n = strlen(s + 1);
init(n);
for (int i = (1); i <= (n); i++) cnt[id(s[i])]++;
dp[0] = 1;
for (int i = (0); i <= (51); i++)
if (cnt[i])
for (int j = n / 2; j >= cnt[i]; j--)
dp[j] = (dp[j] + dp[j - cnt[i]]) % mod;
int v = (long long)fac[n / 2] * fac[n / 2] % mod;
for (int i = (0); i <= (51); i++) v = (long long)v * inv[cnt[i]] % mod;
for (int x = (0); x <= (51); x++)
for (int y = (0); y <= (51); y++) {
for (int i = (0); i <= (n / 2); i++) f[i] = dp[i];
for (int i = (cnt[x]); i <= (n / 2); i++)
f[i] = (f[i] + mod - f[i - cnt[x]]) % mod;
if (x != y)
for (int i = (cnt[y]); i <= (n / 2); i++)
f[i] = (f[i] + mod - f[i - cnt[y]]) % mod;
res[x][y] = (long long)f[n / 2] * v % mod * 2 % mod;
}
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", res[id(s[x])][id(s[y])]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
ll max_pool[4 * 100000] = {0};
void update(int u, int l, int r, int cur, ll v) {
if ((cur < l) || (cur > r)) {
return;
}
if (l == r) {
max_pool[u] = v;
return;
}
int mid = (l + r) / 2;
update(2 * u, l, mid, cur, v);
update(2 * u + 1, mid + 1, r, cur, v);
max_pool[u] = max(max_pool[2 * u], max_pool[2 * u + 1]);
}
ll query(int u, int l, int r, int ql, int qr) {
if ((ql > r) || (qr < l)) {
return 0;
}
if ((ql <= l) && (qr >= r)) {
return max_pool[u];
}
int mid = (l + r) / 2;
ll res1 = query(2 * u, l, mid, ql, qr);
ll res2 = query(2 * u + 1, mid + 1, r, ql, qr);
return max(res1, res2);
}
int main(int argc, char **argv) {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0), cout.precision(15);
const long double pi = acosl(-1);
int n;
cin >> n;
vector<ll> v(n);
vector<pair<ll, int> > vp(n);
for (int i = 0; i < n; i++) {
ll ri, hi;
cin >> ri >> hi;
v[i] = ri * ri * hi;
vp[i].first = v[i];
vp[i].second = i;
}
sort(vp.begin(), vp.end());
vector<int> b(n);
vector<int> prev(n);
for (int i = 0; i < n; i++) {
b[vp[i].second] = i;
if (i == 0) {
prev[i] = -1;
} else {
if (vp[i].first == vp[i - 1].first) {
prev[i] = prev[i - 1];
} else {
prev[i] = i - 1;
}
}
}
ll res = 0;
for (int i = 0; i < n; i++) {
ll v_sum = v[i];
if (prev[b[i]] >= 0) {
v_sum += query(1, 0, n - 1, 0, prev[b[i]]);
}
res = max(res, v_sum);
update(1, 0, n - 1, b[i], v_sum);
}
cout << fixed << res * pi << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const int MAXN = 100100;
int x[MAXN], y[MAXN];
int N;
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d %d", x + i, y + i);
}
int m = inf;
int M = 0;
for (int i = 0; i < N; ++i) {
m = min(m, x[i]);
M = max(M, y[i]);
}
int ans = -1;
for (int i = 0; i < N; ++i) {
if (x[i] == m && y[i] == M) {
ans = i + 1;
break;
}
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
if (n > m + 1 || 2 * (n + 1) < m)
cout << "-1" << endl;
else {
int v = min(n, m);
int mn = v;
int n1 = n;
int m1 = m;
while (v && m) {
if (mn == n1 && v < m) {
cout << "110";
v--;
m -= 2;
} else if (mn == n1) {
cout << "10";
v--;
m--;
} else {
cout << "01";
v--;
n--;
}
}
if (n1 == mn) {
while (m--) cout << "1";
} else {
if (n) cout << "0";
}
cout << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> x(500000);
long long s = 0;
int mx = 0, mn = 1e9;
long long ss, sss;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> x[i];
s += x[i];
mx = max(x[i], mx);
mn = min(x[i], mn);
}
int l = (s + n - 1) / n, r = mx;
while (l <= r) {
long long sum = 0;
int mid = (l + r) / 2;
for (int i = 0; i < n; i++)
if (x[i] > mid) sum += (x[i] - mid);
if (sum <= k) {
ss = mid;
r = mid - 1;
} else
l = mid + 1;
}
l = mn;
r = s / n;
while (l <= r) {
long long sum = 0;
int mid = (l + r) / 2;
for (int i = 0; i < n; i++)
if (x[i] < mid) sum += (mid - x[i]);
if (sum <= k) {
sss = mid;
l = mid + 1;
} else
r = mid - 1;
}
cout << ss - sss;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f, mod = 998244353;
const double pi = 3.1415926535897932, eps = 1e-6;
void chmax(int &x, int y) {
if (x < y) x = y;
}
void chmin(int &x, int y) {
if (x > y) x = y;
}
int n, m, dp[500005], t[500005];
char s[500005];
struct mat {
int a[2][2];
mat() { a[0][0] = a[0][1] = a[1][0] = a[1][1] = 0; }
} E;
mat operator*(mat a, mat b) {
mat c;
for (int(i) = (0); (i) <= (1); (i)++)
for (int(j) = (0); (j) <= (1); (j)++)
for (int(k) = (0); (k) <= (1); (k)++) {
c.a[i][j] += (long long)a.a[i][k] * b.a[k][j] % mod;
if (c.a[i][j] >= mod) c.a[i][j] -= mod;
}
return c;
}
mat st[500005];
struct segtree {
mat dat[1048580];
int n;
void build(int nn) {
n = 1;
while (n <= nn) n <<= 1;
for (int i = n - 1; i < 2 * n - 1; i++) dat[i] = st[i - n + 1];
for (int i = n - 2; i >= 0; i--)
dat[i] = dat[(i << 1) + 1] * dat[(i << 1) + 2];
}
void update(int k, mat a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = dat[(k << 1) + 1] * dat[(k << 1) + 2];
}
}
mat query(int l, int r) { return query(l, r + 1, 0, 0, n); }
mat query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) return E;
if (a <= l && r <= b) return dat[k];
return query(a, b, (k << 1) + 1, l, (l + r) >> 1) *
query(a, b, (k << 1) + 2, (l + r) >> 1, r);
}
} seg;
int main() {
E.a[0][0] = E.a[1][1] = 1;
scanf("%d%d%s", &n, &m, s + 1);
for (int(i) = (1); (i) <= (n); (i)++) t[i] = s[i] - 48;
st[0] = E;
for (int(i) = (1); (i) <= (n); (i)++) {
int u = t[i] + t[i - 1] * 10;
st[i].a[0][0] = 0;
st[i].a[0][1] = (i > 1 && 10 <= u && u <= 18 ? 19 - u : 0);
st[i].a[1][0] = 1;
st[i].a[1][1] = t[i] + 1;
}
seg.build(n);
for (int(i) = (1); (i) <= (m); (i)++) {
int a, b;
scanf("%d%d", &a, &b);
t[a] = b;
int u1 = t[a] + t[a - 1] * 10, u2 = t[a + 1] + t[a] * 10;
st[a].a[0][1] = (a > 1 && 10 <= u1 && u1 <= 18 ? 19 - u1 : 0);
st[a].a[1][1] = t[a] + 1;
seg.update(a, st[a]);
if (a < n)
st[a + 1].a[0][1] = (10 <= u2 && u2 <= 18 ? 19 - u2 : 0),
seg.update(a + 1, st[a + 1]);
mat res = seg.query(1, n);
printf("%d\n", res.a[1][1]);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n < 1000 ? "ABC" : "ABD") << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int IT = 200;
const int N = 100100;
int n;
double k;
double a[N];
bool solve(double x) {
double bal = 0;
for (int i = 0; i < n; i++) {
if (a[i] >= x)
bal += k * (a[i] - x);
else
bal += a[i] - x;
}
return bal >= 0;
}
int main() {
scanf("%d%lf", &n, &k);
k = 1 - k / 100.;
double l = 0, r = 0;
for (int i = 0; i < n; i++) {
scanf("%lf", &a[i]);
r = max(r, a[i]);
}
for (int it = 0; it < IT; it++) {
double x = (l + r) / 2;
if (solve(x))
l = x;
else
r = x;
}
printf("%.12lf\n", r);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1000000000000000007;
void AC_aaega() {
long long n, m;
cin >> n >> m;
vector<vector<long long> > vv(n);
for (long long i = 0; i < m; i++) {
long long aa, bb;
cin >> aa >> bb;
aa--;
bb--;
vv[aa].push_back(bb);
}
function<void(long long)> fname = [&](long long aa) {
for (long long i = 0; i < ((long long)vv[aa].size()); i++) {
if (vv[aa][i] >= aa) {
vv[aa][i] = vv[aa][i] - aa;
} else {
vv[aa][i] = n - (aa - vv[aa][i]);
}
}
};
for (long long i = 0; i < n; i++) {
fname(i);
sort(vv[i].begin(), vv[i].end());
}
for (long long i = 0; i < n; i++) {
vector<bool> done(n, false);
long long ans = 0;
long long ss = 0;
for (long long j = i;; j++, ss++) {
if (done[j % n]) break;
done[j % n] = true;
long long qw = ((long long)vv[j % n].size()) - 1,
wq = ((vv[j % n].empty()) ? -1 : vv[j % n].front());
if (wq == -1) continue;
ans = max(ans, ss + qw * n + wq);
}
cout << ans << ' ';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
long long testcases = 1;
while (testcases--) {
AC_aaega();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
int main() {
int n;
string s;
cin>>n>>s;
int ans=0;
rep(i,n-1){
vector<bool>a(26);
rep(j,i+1){
a[s[j]-'a']=true;
}
int answer=0;
for(int k=i+1;k<n;k++){
if(a[s[k]-'a']) {
answer++;
a[s[k]-'a']=false;
}
}
ans=max(ans,answer);
}
cout<<ans<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> a;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
a = vector<long long>(6);
int total = 0;
for (int i = 0; i < 6; i++) {
cin >> a[i];
total += a[i];
}
for (int i = 0; i < 6; i++) {
for (int j = i + 1; j < 6; j++) {
for (int k = j + 1; k < 6; k++) {
int sum = a[i] + a[j] + a[k];
if (sum == total - sum) {
cout << "YES" << endl;
return 0;
}
}
}
}
cout << "NO" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
const double E = exp(1.0);
const double pi = 3.1415926;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 5;
using namespace std;
pair<int, int> pa[N];
priority_queue<pair<int, int>, vector<pair<int, int> >, less<pair<int, int> > >
q;
int ans1[N], ans2[N], idx;
int main() {
int t;
scanf("%d", &t);
while (t--) {
memset(ans1, 0, sizeof ans1);
memset(ans2, 0, sizeof ans2);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &pa[i].first);
pa[i].second = i;
q.push(pa[i]);
}
idx = 1;
int ans = 0;
while (1) {
pair<int, int> temp1 = q.top();
q.pop();
pair<int, int> temp2 = q.top();
q.pop();
if (temp2.first == 0 || temp1.first == 0) break;
ans++;
ans1[idx] = temp1.second;
ans2[idx] = temp2.second;
temp1.first--;
temp2.first--;
idx++;
q.push(temp1);
q.push(temp2);
}
printf("%d\n", ans);
for (int i = 1; i < idx; i++) printf("%d %d\n", ans2[i], ans1[i]);
while (!q.empty()) q.pop();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt[9];
int mins = 1e6;
const string t = "Bulbasr";
int main() {
cin >> s;
for (int i = 0; i < s.size(); i++)
for (int j = 0; j < t.size(); j++) {
if (s[i] == t[j]) {
cnt[j]++;
break;
}
}
cnt[1] /= 2;
cnt[4] /= 2;
for (int i = 0; i < t.size(); i++) mins = min(mins, cnt[i]);
cout << mins << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
static struct IO {
char tmp[1 << 10];
char cur;
inline char nextChar() { return cur = getc(stdin); }
inline char peekChar() { return cur; }
inline operator bool() { return peekChar(); }
inline static bool isBlank(char c) { return (c < '-' && c); }
inline bool skipBlanks() {
while (isBlank(nextChar()))
;
return peekChar() != 0;
}
inline IO& operator>>(char& c) {
c = nextChar();
return *this;
}
inline IO& operator>>(char* buf) {
if (skipBlanks()) {
if (peekChar()) {
*(buf++) = peekChar();
while (!isBlank(nextChar())) *(buf++) = peekChar();
}
*(buf++) = 0;
}
return *this;
}
inline IO& operator>>(string& s) {
if (skipBlanks()) {
s.clear();
s += peekChar();
while (!isBlank(nextChar())) s += peekChar();
}
return *this;
}
inline IO& operator>>(double& d) {
if ((*this) >> tmp) sscanf(tmp, "%lf", &d);
return *this;
}
inline IO& operator>>(int& n) {
if (skipBlanks()) {
int sign = +1;
if (peekChar() == '-') {
sign = -1;
n = nextChar() - '0';
} else
n = peekChar() - '0';
while (!isBlank(nextChar())) {
n += n + (n << 3) + peekChar() - 48;
}
n *= sign;
}
return *this;
}
inline IO& operator>>(unsigned int& n) {
if (skipBlanks()) {
int sign = +1;
if (peekChar() == '-') {
sign = -1;
n = nextChar() - '0';
} else
n = peekChar() - '0';
while (!isBlank(nextChar())) {
n += n + (n << 3) + peekChar() - 48;
}
n *= sign;
}
return *this;
}
inline IO& operator>>(long long& n) {
if (skipBlanks()) {
int sign = +1;
if (peekChar() == '-') {
sign = -1;
n = nextChar() - '0';
} else
n = peekChar() - '0';
while (!isBlank(nextChar())) {
n += n + (n << 3) + peekChar() - 48;
}
n *= sign;
}
return *this;
}
inline void putChar(char c) { putc(c, stdout); }
inline IO& operator<<(char c) {
putChar(c);
return *this;
}
inline IO& operator<<(const char* s) {
while (*s) putChar(*s++);
return *this;
}
inline IO& operator<<(const string& s) {
for (int i = 0; i < (int)s.size(); ++i) putChar(s[i]);
return *this;
}
char* toString(double d) {
sprintf(tmp, "%lf%c", d, '\0');
return tmp;
}
inline IO& operator<<(double d) { return (*this) << toString(d); }
inline char* toString(int n) {
char* p = (tmp + 30);
if (n) {
bool isNeg = 0;
if (n < 0) isNeg = 1, n = -n;
while (n) *--p = (n % 10) + '0', n /= 10;
if (isNeg) *--p = '-';
} else
*--p = '0';
return p;
}
inline IO& operator<<(int n) { return (*this) << toString(n); }
inline char* toString(long long n) {
char* p = (tmp + 30);
if (n) {
bool isNeg = 0;
if (n < 0) isNeg = 1, n = -n;
while (n) *--p = (n % 10) + '0', n /= 10;
if (isNeg) *--p = '-';
} else
*--p = '0';
return p;
}
inline IO& operator<<(long long n) { return (*this) << toString(n); }
} __io__;
int n, m, a, b, Count[2], resR, resB;
vector<int> vecR[2];
vector<int> vecB[2];
vector<int> resultB;
vector<int> resultR;
bool wrongR, wrongB;
char c;
vector<pair<int, char> > G[100007];
bitset<100007> visR;
bitset<100007> visB;
int R[100007];
int B[100007];
void dfsR(int v) {
visR[v] = 1;
++Count[R[v]];
vecR[R[v]].push_back(v);
for (auto it : G[v]) {
if (!visR[it.first]) {
if (it.second == 'R')
R[it.first] = R[v];
else
R[it.first] = R[v] ^ 1;
dfsR(it.first);
}
}
}
void dfsB(int v) {
visB[v] = 1;
++Count[B[v]];
vecB[B[v]].push_back(v);
for (auto it : G[v]) {
if (!visB[it.first]) {
if (it.second == 'B')
B[it.first] = B[v];
else
B[it.first] = B[v] ^ 1;
dfsB(it.first);
}
}
}
int main() {
__io__ >> n >> m;
while (m--) {
__io__ >> a >> b >> c;
G[a].emplace_back(b, c);
G[b].emplace_back(a, c);
}
for (int i = 1; i <= n; ++i) {
if (!visR[i]) {
Count[0] = 0;
Count[1] = 0;
vecR[0].clear();
vecR[1].clear();
dfsR(i);
if (Count[0] < Count[1]) {
resultR.insert(resultR.end(), vecR[0].begin(), vecR[0].end());
} else {
resultR.insert(resultR.end(), vecR[1].begin(), vecR[1].end());
}
resR += min(Count[0], Count[1]);
}
if (!visB[i]) {
Count[0] = 0;
Count[1] = 0;
vecB[0].clear();
vecB[1].clear();
dfsB(i);
if (Count[0] < Count[1]) {
resultB.insert(resultB.end(), vecB[0].begin(), vecB[0].end());
} else {
resultB.insert(resultB.end(), vecB[1].begin(), vecB[1].end());
}
resB += min(Count[0], Count[1]);
}
}
for (int i = 1; i <= n; ++i) {
for (auto it : G[i]) {
if (it.second == 'R' && R[i] != R[it.first]) wrongR = 1;
if (it.second == 'B' && R[i] == R[it.first]) wrongR = 1;
if (it.second == 'B' && B[i] != B[it.first]) wrongB = 1;
if (it.second == 'R' && B[i] == B[it.first]) wrongB = 1;
}
}
if (wrongB && wrongR) {
__io__ << -1;
return 0;
}
if (wrongB) resB = 1000000000;
if (wrongR) resR = 1000000000;
if (resR < resB) {
__io__ << resR << ('\n');
for (auto it : resultR) {
__io__ << it << " ";
}
} else {
__io__ << resB << ('\n');
for (auto it : resultB) {
__io__ << it << " ";
}
}
}
| 2 |
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
while(true){
cin>>n;
if(n==0)
break;
int x,y,h,w;
int ans=0;
for(int u=0;u<n;u++){
cin>>x>>y>>h>>w;
int count=0;
if(x+y+h<=160&&w<=25)
count=1600;
if(x+y+h<=140&&w<=20)
count=1400;
if(x+y+h<=120&&w<=15)
count=1200;
if(x+y+h<=100&&w<=10)
count=1000;
if(x+y+h<=80&&w<=5)
count=800;
if(x+y+h<=60&&w<=2)
count=600;
ans+=count;
}
cout<<ans<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int rd() {
int x = 0;
char c = getchar();
while (c > '9' || c < '0') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
return x;
}
void wt(long long x) {
if (x >= 10) wt(x / 10);
putchar(x % 10 + 48);
}
const int N = 3e6 + 10;
struct num {
int p, q;
bool operator<(const num &t) const { return p < t.p; }
};
struct Fac {
num k[20];
int tot;
void clear() {
for (int i = 0; i < 20; i++) k[i].q = k[i].p = 0;
tot = 0;
}
void sort() { ::sort(k + 1, k + tot + 1); }
} A, B, C;
int pri[N], mf[N], n1, n2, n3, s1, s2, s3, m1, m2, m3;
long long n, m, s, ans;
void getp() {
int n = 3e6;
for (int i = 2; i <= n; i++) {
if (!mf[i]) mf[i] = i, pri[++pri[0]] = i;
for (int j = 1; j <= pri[0] && i * pri[j] <= n; j++) {
mf[i * pri[j]] = pri[j];
if (i % pri[j] == 0) break;
}
}
}
int id[N];
void facor(Fac &a, int x) {
for (; mf[x]; x /= mf[x]) {
if (!id[mf[x]]) id[mf[x]] = ++a.tot, a.k[a.tot].p = mf[x];
a.k[id[mf[x]]].q++;
}
}
void dfs(int u, long long sum) {
if (sum > n) return;
if (u > C.tot) {
ans++;
return;
}
dfs(u + 1, sum);
for (int i = 1; i <= C.k[u].q; i++) sum *= C.k[u].p, dfs(u + 1, sum);
}
void dfs2(int x, long long y, int z) {
if (!y) return;
if (x > B.tot) {
ans += y * z;
return;
}
dfs2(x + 1, y, z);
for (int i = 0; i <= B.k[x].q; i++) y /= B.k[x].p;
dfs2(x + 1, y, -z);
}
int main() {
getp();
for (int T = rd(); T; T--) {
ans = 0, A.clear(), B.clear(), C.clear();
n1 = rd(), n2 = rd(), n3 = rd(), n = 1ll * n1 * n2 * n3;
m1 = rd(), m2 = rd(), m3 = rd(), m = 1ll * m1 * m2 * m3;
s1 = rd(), s2 = rd(), s3 = rd(), s = 1ll * s1 * s2 * s3;
memset(id, 0, sizeof id);
facor(A, n1), facor(A, n2), facor(A, n3);
memset(id, 0, sizeof id);
facor(C, 2 * s1), facor(C, s2), facor(C, s3);
A.sort(), C.sort();
dfs(1, 1);
int i = 1, j = 1;
while (i <= A.tot && j <= C.tot) {
if (A.k[i].p == C.k[j].p) {
if (A.k[i].q > C.k[j].q)
B.k[++B.tot].p = C.k[j].p, B.k[B.tot].q = C.k[j].q;
i++, j++;
continue;
}
if (A.k[i].p < C.k[j].p) {
B.k[++B.tot].p = A.k[i].p, B.k[B.tot].q = 0;
i++;
continue;
}
j++;
}
for (; i <= A.tot; i++) B.k[++B.tot].p = A.k[i].p, B.k[B.tot].q = 0;
dfs2(1, m, 1);
wt(ans), putchar(10);
}
return 0;
}
| 5 |
#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<cstdio>
#include <string>
#include <complex>
#include <functional>
using namespace std;
typedef pair<int,int> P;
int main(){
int a,b,i;
int w[100];
while(cin>>a>>b&&a){
int max=1500000;
int min=1,res=0,dan=1;
for(i=0;i<b;i++) {
cin>>w[i];
if(min<w[i]) min=w[i];
}
while(max-min>1){
int hf=(max+min)/2;
int j;
res=0;
dan=1;
for(j=0;j<b;j++){
if(res+w[j]<=hf) res+=w[j];
else{
dan++;
res=w[j];
}
}
if(dan<=a) max=hf;
else min=hf;
}
int ans=min;
res=0,dan=1;
for(i=0;i<b;i++){
if(res+w[i]<=ans) res+=w[i];
else{
dan++;
res=w[i];
}
}
if(dan<=a) cout<<ans<<endl;
else cout<<max<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int N, M, all, all2;
int op[8];
int dp[44][1 << 8][1 << 8], sol;
int gain[1 << 8];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
if (N > M) swap(N, M);
all = 1 << N;
all2 = 1;
for (int I = 0; I < N; I++) all2 *= 5;
for (int I = 0; I <= M; I++)
for (int m = 0; m < all; m++)
for (int m2 = 0; m2 < all; m2++) dp[I][m][m2] = 1e9;
sol = N * M;
for (int m = 0; m < all; m++) gain[m] = __builtin_popcount(m);
dp[0][0][0] = 0;
for (int I = 1; I <= M; I++) {
for (int mop = 0; mop < all2; mop++) {
memset(op, 0, sizeof(op));
int nprev = 0;
int ncurr = 0;
int nnext = 0;
bool ok = true;
int tmp = mop;
for (int K = 0; K < N; K++) {
op[K] = tmp % 5;
tmp /= 5;
if (K == 1 - 1 && op[K] == 0) ok = false;
if (K == N - 1 && op[K] == 2) ok = false;
if (op[K] == 0) ncurr |= (1 << (K - 1));
if (op[K] == 1) nprev |= (1 << (K));
if (op[K] == 2) ncurr |= (1 << (K + 1));
if (op[K] == 3) nnext |= (1 << (K));
if (op[K] == 4) ncurr |= (1 << (K));
}
if (!ok) continue;
for (int prev = 0; prev < all; prev++)
for (int curr = 0; curr < all; curr++) {
int tprev = prev | nprev;
int tcurr = curr | ncurr;
int tnext = nnext;
if (I == 1) {
if (tprev != 0) continue;
}
dp[I][tcurr][tnext] =
min(dp[I][tcurr][tnext], dp[I - 1][prev][curr] + gain[tprev]);
}
}
}
for (int m = 0; m < all; m++)
if (sol > dp[M][m][0] + gain[m]) sol = min(sol, dp[M][m][0] + gain[m]);
cout << (N * M) - sol << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200;
int arr[maxn];
bool cmp(int num1, int num2) { return num1 > num2; }
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n, cmp);
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << arr[i];
}
cout << endl;
}
return 0;
}
| 2 |
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 100005;
int R, C, N, n;
pair<int,int> v[maxn << 1];
stack<int> S;
int trans(int x, int y) {
if(x == 0) return y;
if(y == C) return C + x;
if(x == R) return C + R + C - y;
if(y == 0) return C + R + C + R - x;
return -1;
}
int main() {
scanf("%d%d%d", &R, &C, &N);
for(int i = 1, x1, x2, y1, y2; i <= N; ++i) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
int t1 = trans(x1, y1), t2 = trans(x2, y2);
if(t1 == -1 || t2 == -1) continue;
if(t1 > t2) swap(t1, t2);
v[++n] = make_pair(t1, i);
v[++n] = make_pair(t2, -i);
}
sort(v + 1, v + n + 1);
for(int i = 1; i <= n; ++i) {
if(v[i].second > 0) S.push(v[i].second);
else {
if(-v[i].second != S.top()) {
puts("NO"); return 0;
} S.pop();
}
}
puts("YES");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
scanf("%d", &n);
printf("%d\n", 6 * n * (n - 1) + 1);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long x1, y1, x2, y2, men;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 == x2 && y1 == y2) {
cout << "0\n";
} else if (x1 == x2) {
cout << abs(y2 - y1) << "\n";
} else if (y1 == y2) {
cout << abs(x2 - x1) << "\n";
} else {
if (x1 < x2 && y1 < y2) {
men = min(x2 - x1, y2 - y1);
x2 -= men;
y2 -= men;
} else if (x1 < x2 && y1 > y2) {
men = min(x2 - x1, y1 - y2);
x2 -= men;
y2 += men;
} else if (x1 > x2 && y1 > y2) {
men = min(x1 - x2, y1 - y2);
x2 += men;
y2 += men;
} else if (x1 > x2 && y1 < y2) {
men = min(x1 - x2, y2 - y1);
x2 += men;
y2 -= men;
}
if (x1 == x2 && y1 == y2) {
cout << men << "\n";
} else if (x1 == x2) {
cout << abs(y2 - y1) + men << "\n";
} else if (y1 == y2) {
cout << abs(x2 - x1) + men << "\n";
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int N = 1e3 + 7;
const int inf = 0x3f3f3f3f;
int res[3000];
int main() {
int q;
scanf("%d", &q);
while (q--) {
memset(res, 0, sizeof(res));
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
if (x < 3000) res[x]++;
}
if (res[2048]) {
printf("YES\n");
continue;
}
for (int i = 1; i <= 1024; i *= 2) {
res[i * 2] += res[i] / 2;
}
if (res[2048]) {
printf("YES\n");
} else
printf("NO\n");
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
vector<int>E[100000];
int a[100000], b[100000];
int ans[100000];
void dfs(int v, int p, int d) {
for (int u : E[v]) {
if (u == p)continue;
dfs(u, v, d ^ 1);
a[v] = max(a[v], a[u] - 1);
b[v] = min(b[v], b[u] + 1);
}
if (a[v] % 2 != d)a[v]++;
if (b[v] % 2 != d)b[v]--;
if (a[v] > b[v]) {
puts("No"); exit(0);
}
}
void dfs2(int v, int p) {
for (int u : E[v]) {
if (u == p)continue;
if (a[u] <= ans[v] + 1 && ans[v] + 1 <= b[u]) {
ans[u] = ans[v] + 1; dfs2(u, v);
}
else {
ans[u] = ans[v] - 1; dfs2(u, v);
}
}
}
int main() {
int n; scanf("%d", &n);
rep(i, n - 1) {
int u, v; scanf("%d%d", &u, &v); u--; v--;
E[u].push_back(v); E[v].push_back(u);
}
int k; scanf("%d", &k);
rep(i, n)a[i] = INT_MIN + 1, b[i] = INT_MAX - 1;
int s;
rep(i, k) {
int v, p; scanf("%d%d", &v, &p); v--;
a[v] = b[v] = p;
s = v;
}
dfs(s, -1, a[s] % 2);
ans[s] = a[s];
dfs2(s, -1);
puts("Yes");
rep(i, n)printf("%d\n", ans[i]);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> p1, pair<int, int> p2) {
return p1.second != p2.second ? p1.second < p2.second : p1 < p2;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, l, r, cnt = 0;
cin >> n;
vector<int> ans, vis(n);
vector<pair<int, int> > v;
for (int i = 0; i < n; ++i) {
cin >> l >> r;
v.push_back({min(l, r), max(l, r)});
}
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < n; ++i) {
if (!vis[i]) {
ans.push_back(v[i].second);
for (int j = 0; j < n; ++j)
if (v[j].first <= v[i].second && v[j].second >= v[i].second) vis[j] = 1;
}
}
cout << ans.size() << '\n';
for (auto i : ans) cout << i << " ";
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long long mod = 998244353;
const int inf = INT_MAX;
struct item {
long long n;
item(int x = inf) : n(x) {}
item operator+(const item &oth) {
item r;
r.n = min(n, oth.n);
return r;
}
};
template <class node>
struct segtree {
int n;
vector<node> st;
void init(int _n) {
n = _n;
st.assign(2 * n, node());
}
inline int le(int x) { return 2 * x + 1; }
inline int ri(int x) { return 2 * x + 2; }
void build(vector<int> &a) {
init(a.size());
for (int i = n; i < 2 * n; i++) st[i] = a[i - n];
for (int i = n - 1; i > 0; i--) st[i] = st[i << 1] + st[i << 1 | 1];
}
void set(int i, int x) {
i += n;
for (st[i] = st[i] + x; i >>= 1;) st[i] = st[i << 1] + st[i << 1 | 1];
}
node query(int l, int r) {
node anl, anr;
r++;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) anl = anl + st[l++];
if (r & 1) anr = st[--r] + anr;
}
return anl + anr;
}
};
template <class node>
struct hld {
int n, t;
vector<int> tot, up, tin, tout, down, dep, par;
vector<vector<int>> adj;
segtree<node> stup, stdown;
void init(int _n) {
n = _n;
t = 0;
stup.init(n);
stdown.init(n);
tot.assign(n, 0);
up.assign(n, 0);
tin.assign(n, 0);
tout.assign(n, 0);
down.assign(n, 0);
dep.assign(n, 0);
par.assign(n, 0);
adj.assign(n, vector<int>(0));
}
void dfs_tot(int v = 0, int p = -1) {
tot[v] = 1;
par[v] = p;
for (auto &u : adj[v]) {
if (u == p) continue;
dep[u] = dep[v] + 1;
dfs_tot(u, v);
tot[v] += tot[u];
if (adj[v][0] == p || tot[u] > tot[adj[v][0]]) {
swap(u, adj[v][0]);
}
}
}
void dfs_hld(int v = 0, int p = -1) {
tin[v] = t++;
for (auto u : adj[v]) {
if (u == p) continue;
up[u] = (u == adj[v][0] ? up[v] : u);
down[up[u]] = u;
dfs_hld(u, v);
}
tout[v] = t;
}
void setup(int i, int x) { stup.set(i, x); }
int queryup(int l, int r) { return (stup.query(l, r)).n; }
void setdown(int i, int x) { stdown.set(i, x); }
int querydown(int l, int r) { return (stdown.query(l, r)).n; }
void put(int i, int dis) {
setup(tin[i], dis + dep[i] - dep[up[i]]);
setdown(tin[i], dis + dep[down[up[i]]] - dep[i]);
}
void put(int i) {
int dis = 0;
while (true) {
put(i, dis);
int p = par[up[i]];
if (p == -1) break;
dis += dep[i] - dep[p];
i = p;
}
}
int query(int i) {
int x = inf, dis = 0;
while (true) {
x = min({x - dis,
queryup(tin[i], tin[down[up[i]]]) - (dep[i] - dep[up[i]]),
querydown(tin[up[i]], tin[i]) - (dep[down[up[i]]] - dep[i])});
x += dis;
int p = par[up[i]];
if (p == -1) break;
dis += dep[i] - dep[p];
i = p;
}
return x;
}
};
int n, m, a, b, tag, v;
void solve() {
cin >> n >> m;
hld<item> hl;
hl.init(n);
for (int i = (int)1; i != (int)n; i = i + 1) {
cin >> a >> b;
a--;
b--;
hl.adj[a].push_back(b);
hl.adj[b].push_back(a);
}
hl.dfs_tot();
hl.dfs_hld();
hl.put(0, 0);
while (m--) {
cin >> tag >> v;
v--;
if (tag == 1) {
hl.put(v);
} else {
cout << hl.query(v) << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
t = 1;
while (t--) {
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 1;
const long long INF = 1e9 + 7;
const long long inf = 1e18;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) {
long long n;
string s;
cin >> n;
cin >> s;
long long ct = 0;
for (long long i = 0; i < n - 1; i++) {
if (s[i] == s[i + 1]) {
ct++;
}
}
long long x = ct / 2;
if (ct & 1) {
x++;
}
cout << x << '\n';
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int len[2200];
int main() {
int t;
for (int i = 0; i < 6; i++) {
cin >> t;
len[t]++;
}
int n = 0;
for (int i = 1; i <= 9; i++) {
if (len[i] >= 4) {
n = i;
len[i] -= 4;
}
}
if (n == 0) {
cout << "Alien";
return 0;
}
for (int i = 1; i <= 9; i++) {
if (len[i] == 2) {
cout << "Elephant";
return 0;
}
}
cout << "Bear";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
long long inv[5050 + 10], fac[5050 + 10], ifac[5050 + 10];
void setComb() {
inv[0] = 1;
inv[1] = 1;
fac[1] = 1;
ifac[1] = 1;
fac[0] = 1;
ifac[0] = 1;
for (int i = 2; i < 5050; i++) {
inv[i] = (-MOD / i) * inv[MOD % i] % MOD;
fac[i] = fac[i - 1] * i % MOD;
ifac[i] = ifac[i - 1] * inv[i] % MOD;
inv[i] = (inv[i] + MOD) % MOD;
fac[i] = (fac[i] + MOD) % MOD;
ifac[i] = (ifac[i] + MOD) % MOD;
}
return;
}
long long comb(long long n, long long k) {
if (n < k || n < 0 || k < 0)
return 0;
else
return ((fac[n] * ifac[k] % MOD * ifac[n - k] % MOD + MOD) % MOD);
}
long long hcomb(long long n, long long r) {
if (n == 0 && r == 0)
return 1;
else if (n < 0 || r < 0)
return 0;
else
return comb(n + r - 1, r - 1);
}
int N;
vector<int> A;
long long dp[5050][5050];
long long cnt[5050];
void add(long long &a, long long b) { a = (a + b) % MOD; }
void mul(long long &a, long long b) { a = a * b % MOD; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
A.resize(N);
for (int i = 0; i < (N); ++i) cin >> A[i];
sort(A.begin(), A.end());
for (int i = 0; i < (N); ++i) cnt[A[i]]++;
setComb();
dp[0][0] = 1;
for (int i = 0; i < N; i++) {
long long sum = 0;
for (int j = 0; j <= 5000; j++) {
long long res = sum;
mul(res, cnt[j]);
mul(res, inv[N - i]);
add(dp[i + 1][j], res);
sum += dp[i][j];
}
}
long long ans = 0;
for (int i = 1; i < N; i++) {
for (int j = 0; j <= 5000; j++) {
long long res = dp[i][j];
mul(res, cnt[j] - 1);
mul(res, inv[N - i]);
add(ans, res);
}
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chkmin(T& x, T y) {
return x > y ? x = y, true : false;
}
template <class T>
inline bool chkmax(T& x, T y) {
return x < y ? x = y, true : false;
}
inline long long read(void) {
long long x, f = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return x * f;
}
int n, p, pp, phi, cnt, l, r, q[30];
long long fac[100005], nfac[100005], c[30][100005];
inline long long ppow(long long a, long long b) {
long long ret = 1;
for (; b; b >>= 1) {
if (b & 1) ret = ret * a % p;
a = a * a % p;
}
return ret;
}
long long C(int x, int y) {
if (x > y) return 0;
long long ret = fac[y] * nfac[x] % p * nfac[y - x] % p;
for (int i = (1); i <= (cnt); ++i)
ret = ret * ppow(q[i], c[i][y] - c[i][x] - c[i][y - x]) % p;
return ret;
}
int main(void) {
n = read();
p = read();
l = read();
r = read();
pp = phi = p;
for (int i = 2; i * i <= p; ++i) {
if (p % i == 0) {
q[++cnt] = i;
while (p % i == 0) p /= i;
(phi /= i) *= i - 1;
}
}
if (p != 1) q[++cnt] = p, (phi /= p) *= p - 1;
p = pp;
fac[0] = nfac[0] = 1;
for (int i = (1); i <= (n); ++i) {
int ii = i;
for (int j = (1); j <= (cnt); ++j) c[j][i] = c[j][i - 1];
for (int j = (1); j <= (cnt); ++j)
if (ii % q[j] == 0)
while (ii % q[j] == 0) ii /= q[j], c[j][i]++;
fac[i] = fac[i - 1] * ii % p;
nfac[i] = ppow(fac[i], phi - 1);
}
long long ans = 0;
for (int i = (0); i <= (n); ++i) {
long long p1 = C((i + l + 1) / 2, i), p2 = C((min(i, r) + i) / 2 + 1, i);
p1 = p1 - p2 + p;
p1 %= p;
(ans += C(i, n) * p1 % p) %= p;
}
cout << (ans + p) % p << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, num[120000], type[120000], i, fa[120000], f[120000], id, bo[120000], x;
void dfs(int x) {
if (bo[x]) return;
bo[x] = 1;
if (type[x] == 1) {
f[x] = 0;
return;
}
if (num[x] == 1) {
dfs(fa[x]);
f[x] = f[fa[x]] + 1;
}
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; ++i) scanf("%d", &type[i]);
for (i = 1; i <= n; ++i) scanf("%d", &x), num[x]++, fa[x] = i;
memset(f, 130, sizeof(f));
for (i = 1; i <= n; ++i)
if (!bo[i]) dfs(i);
id = 1;
for (i = 1; i <= n; ++i)
if (f[i] > f[id]) id = i;
printf("%d\n", f[id] + 1);
for (i = id; type[i] == 0; i = fa[i]) printf("%d ", i);
printf("%d\n", i);
}
| 2 |
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
int main() {
int N; int d;
while(~scanf("%d%d", &N, &d)) {
vector<int> p(N);
for(int i = 0; i < N; ++ i)
scanf("%d", &p[i]);
int ans = 0;
rep(i, N)
ans += max(0, p[i] - d);
if(ans == 0)
puts("kusoge");
else
printf("%d\n", ans);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 Rand(seed);
uniform_int_distribution<long long> range(0, 1ll << 32);
inline void ucin() {
ios::sync_with_stdio(0);
cin.tie(0);
}
template <class T>
inline void chkmax(T &x, const T &y) {
if (x < y) x = y;
}
template <class T>
inline void chkmin(T &x, const T &y) {
if (x > y) x = y;
}
const int N = 34;
int n, a[N][N], m, ans;
bool rev[N][N];
inline int calc(int x, int y) { return rev[x][y] ? -a[x][y] : a[x][y]; }
int main() {
scanf("%d", &n);
m = (n + 1) >> 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
for (int s = 0; s < (1 << m); s++) {
for (int i = 0; i <= m - 1; ++i) rev[m][i + 1] = s >> i & 1;
for (int i = m + 1; i <= n; ++i) rev[m][i] = rev[m][m] ^ rev[m][i - m];
int cur = 0;
for (int i = 1; i <= n; ++i) cur += calc(m, i);
for (int i = 1; i <= m - 1; ++i) {
int res = 0;
for (int r = 0; r <= 1; ++r) {
rev[i][m] = r;
rev[m + i][m] = rev[m][m] ^ r;
int now = calc(i, m) + calc(i + m, m);
for (int j = 1; j <= m - 1; ++j) {
int x = 0;
for (int c = 0; c <= 1; ++c) {
rev[i][j] = c;
rev[i][j + m] = rev[i][j] ^ rev[i][m];
rev[i + m][j] = rev[i][j] ^ rev[m][j];
rev[i + m][j + m] = rev[i + m][j] ^ rev[i + m][m];
x = max(x, calc(i, j) + calc(i, j + m) + calc(i + m, j) +
calc(i + m, j + m));
}
now += x;
}
res = max(res, now);
}
cur += res;
}
ans = max(ans, cur);
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int ux[] = {1, -1, 0, 0};
const int uy[] = {0, 0, 1, -1};
char c[1010][1010];
bool was[1010][1010];
int n, m, i, j, X, Y, x2, y2, d[1010][1010], qx[1010 * 1010], qy[1010 * 1010];
int bfs(int x, int y) {
qx[0] = x, qy[0] = y, d[x][y] = 0, was[x][y] = true;
i = 0, j = 1;
for (i = 0; i < j; ++i) {
for (int k = 0; k < 4; ++k) {
int xx = qx[i] + ux[k];
int yy = qy[i] + uy[k];
if (0 < xx && xx <= n && 0 < yy && yy <= m && c[xx][yy] != 'T' &&
!was[xx][yy]) {
qx[j] = xx;
qy[j] = yy;
was[xx][yy] = true;
d[xx][yy] = d[qx[i]][qy[i]] + 1;
++j;
}
}
}
int ans = 0;
for (i = 1; i <= n; ++i)
for (j = 1; j <= m; ++j)
if ('0' <= c[i][j] && c[i][j] <= '9' && was[i][j] == true &&
d[i][j] <= d[X][Y])
ans += int(c[i][j]) - 48;
return ans;
}
int main() {
scanf("%d%d\n", &n, &m);
for (i = 1; i <= n; ++i) {
for (j = 1; j <= m; ++j) {
scanf("%c", &c[i][j]);
if (c[i][j] == 'S') X = i, Y = j;
if (c[i][j] == 'E') x2 = i, y2 = j;
}
scanf("\n");
}
cout << bfs(x2, y2);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> graph[500005];
int nxt;
int lft[500005], rht[500005];
void dfs(int n, int p) {
nxt += (int)(graph[n].size()) + !p;
int ed = nxt;
int lst = ed;
rht[n] = ed;
for (int e : graph[n]) {
if (e != p) {
lft[e] = --lst;
dfs(e, n);
}
}
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i < N; i++) {
int a, b;
cin >> a >> b;
graph[a].emplace_back(b);
graph[b].emplace_back(a);
}
lft[++nxt] = 1;
dfs(1, 0);
for (int i = 1; i <= N; i++) {
cout << lft[i] << " " << rht[i] << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int K = 1e5 + 7, M = 1e9 + 9;
int fp(int x, int y) {
int ret = 1;
for (; y; y >>= 1, x = (long long)x * x % M)
if (y & 1) ret = (long long)ret * x % M;
return ret;
}
int n, a, b, k;
char s[K];
int main() {
scanf("%d%d%d%d", &n, &a, &b, &k);
scanf("%s", s);
int ans = 0;
for (int i = 0; i < k; ++i)
(ans += ((s[i] == '+') ? 1ll : -1ll) * fp(a, n - i) % M * fp(b, i) % M) %=
M;
int after = (long long)fp(fp(a, k), M - 2) * fp(b, k) % M;
if (after > 1)
after = ((long long)(fp(after, (n + 1) / k) - after)) *
fp(after - 1, M - 2) % M;
else
after = (n + 1 - k) / k;
printf("%lld", (((long long)ans * (after + 1) % M) + M) % M);
}
| 1 |
#include <cstdio>
using namespace std;
const int N = 31;
int num[N];
int main() {
int n;
scanf("%d", &n);
int xr = 0;
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
int pw = 0;
xr ^= a;
while (!(a & 1))
pw++, a >>= 1;
num[pw]++;
}
int ans = 0;
for (int i = N - 1; i >= 0; i--) {
if ((xr >> i) & 1) {
if (num[i]) {
ans++;
xr ^= (1 << (i + 1)) - 1;
} else {
puts("-1");
return 0;
}
}
}
printf("%d\n", ans);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
cin >> a >> b >> c;
cout << min({b/a,c});
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 123456789;
const long long inf = 123456789000000000;
const double EPS = 1e-10;
const double PI = 2 * asin(1.0);
const long long mod = 1e9 + 7;
inline int cmp(double x, double y = 0, double tol = EPS) {
return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}
int main() {
string s;
cin >> s;
long long par, impar, ap, ai, bp, bi;
par = impar = ap = ai = bp = bi = 0LL;
for (int i = 0; i < ((int)s.size()); ++i) {
impar++;
if (i % 2 == 0) {
if (s[i] == 'a') {
par += ai;
impar += ap;
ap++;
} else {
par += bi;
impar += bp;
bp++;
}
} else {
if (s[i] == 'a') {
par += ap;
impar += ai;
ai++;
} else {
par += bp;
impar += bi;
bi++;
}
}
}
cout << par << " " << impar << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1005, MOD = 1e7 + 7, eps = 1e-7;
double dp[maxN];
int ans[maxN];
int k, q, d;
void prob() {
for (int x = k; x > 0; --x) {
dp[x] = (x * dp[x] + (k - x + 1) * dp[x - 1]) / k;
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
d = 1;
cin >> k >> q;
dp[0] = 1;
for (int n = 1; d <= 1000; ++n) {
prob();
while (d <= 1000 && 2000 * dp[k] >= (d - eps)) {
ans[d] = n;
d++;
}
dp[0] = 0;
}
while (q--) {
int x;
cin >> x;
cout << ans[x] << "\n";
}
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
if (s.back() == 's') s += "es";
else s += "s";
cout << s << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
const int hinf = 0x3f3f3f3f;
const long long mod = 1000000007;
int n, k;
int len;
string name;
string limit;
char ans[110];
int main() {
cin >> n >> k >> name >> limit;
len = name.length();
memset(ans, 0, sizeof(ans));
int fail = 0;
for (int i = 0; i + len <= n; i++) {
if (limit[i] == '1') {
for (int j = i, k = 0; k < len; k++) {
if (j + k >= n) {
fail = 1;
break;
}
if (ans[j + k] && ans[j + k] != name[k]) {
fail = 1;
break;
} else
ans[j + k] = name[k];
}
if (fail) break;
}
}
for (int i = 0; i < n; i++) {
if (limit[i] == '0') {
int fl = 0;
for (int j = 0; j < len && !fl; j++) {
if (i + j < n) {
if (ans[i + j] == 0 || ans[i + j] != name[j]) {
fl = 1;
if (ans[i + j] == 0) {
if (name[j] == 'a')
ans[i + j] = 'b';
else
ans[i + j] = 'a';
}
}
} else
fl = 1;
}
if (!fl) {
fail = 1;
break;
}
}
}
for (int i = 0; i < n; i++) {
if (ans[i] == 0) {
if (name[0] == 'a')
ans[i] = 'b';
else
ans[i] = 'a';
}
}
if (fail) {
puts("No solution");
return 0;
}
for (int i = 0; i < n; i++) cout << ans[i];
puts("");
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define pb(a) push_back(a)
#define pr(a) cout<<(a)<<endl
#define PR(a,b) cout<<(a)<<" "<<(b)<<endl
#define R cin>>
#define F first
#define S second
#define ll long long
bool check(int n,int m,int x,int y){return (x<0||x>=n||y<0||y>=m)?false:true;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
typedef pair<int,int> P;
vector<double> sieve(int n) {
vector<double> p(n);
REP(i,2,n) p[i]=i;
for(int i=2; i*i<n; i++) {
if(p[i]) {
for(int j=i*i; j<n; j+=i) p[j]=0;
}
}
p.erase(remove(all(p),0),p.end());
return p;
}
int main() {
vector<double> p=sieve(100000);
double n,a,b;
while(R n >> a >> b && n) {
double M=0;
int x,y;
rep(i,p.size()) {
REP(j,i,p.size()) {
if(n<p[i]*p[j]) break;
if(p[i]/p[j]<a/b) break;
if(M<p[i]*p[j]) {
M=p[i]*p[j];
x=p[i],y=p[j];
}
}
}
PR(x,y);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, y, z, a, i, j, c = 0, b, p, q, r;
string s;
cin >> x;
int ar[x];
for (i = 1; i <= x; i++) {
cin >> ar[i];
}
if (ar[x] == 15) {
cout << "DOWN" << endl;
} else if (ar[x] == 0) {
cout << "UP" << endl;
} else if (x == 1) {
cout << -1 << endl;
} else if (ar[x - 1] > ar[x]) {
cout << "DOWN" << endl;
} else if (ar[x - 1] < ar[x]) {
cout << "UP" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void fun(long long int a, long long int b, long long int c, long long int d) {
long long int x = b, y = c, z = c;
cout << x << " " << y << " " << z << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
long long int a, b, c, d;
cin >> a >> b >> c >> d;
fun(a, b, c, d);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 1;
map<long long, long long> mp, pm;
deque<long long> v, vc, ve, vv, vd;
deque<char> p, pe;
deque<string> ts, ss, st;
deque<double> sd, ds;
long long a, b, c, d, e, f, cnt, ans, r, n, l, x, y;
long double q, t, cnt1, cnt2;
string g, h, w, o, m, s;
char z, u;
bool re, rt;
vector<pair<long long, long long> > pr, rp, pp;
map<pair<long long, long long>, long long> aw, wa;
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(0);
cin >> a >> b >> c >> d;
v.push_back(a);
v.push_back(b);
v.push_back(c);
v.push_back(d);
sort(v.rbegin(), v.rend());
if (v[0] + v[1] == v[2] + v[3]) {
cout << "YES";
return 0;
} else if (v[0] + v[3] == v[2] + v[1]) {
cout << "YES";
return 0;
} else if (v[0] == v[1] + v[2] + v[3]) {
cout << "YES";
return 0;
} else if (v[0] + v[1] + v[2] == v[3]) {
cout << "YES";
return 0;
} else {
cout << "NO ";
return 0;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n,m;
vector<int>v[100009];
int dp[100009];
int bt(int node){
if(dp[node]!=-1) return dp[node];
int ans=0;
for(int i=0;i<v[node].size();i++){
ans=max(ans,bt(v[node][i])+1);
}
dp[node]=ans;
return ans;
}
int main(){
memset(dp,-1,sizeof(dp));
cin>>n>>m;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
a--;b--;
v[a].push_back(b);
}
int ans=0;
for(int i=0;i<n;i++){
ans=max(ans,bt(i));
}
cout<<ans<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char ch;
int n, m, x, y;
list<pair<int, int> > l;
vector<list<pair<int, int> >::iterator> v;
scanf("%d%d", &n, &m);
l.push_back(pair<int, int>(m, 0));
v.push_back(l.end());
for (int i = 0; i < n; ++i) {
scanf(" %c%*s%d", &ch, &x);
if (ch == 'a') {
y = 0;
for (list<pair<int, int> >::iterator i = l.begin(); i != l.end(); ++i) {
if (y + x <= i->first) {
printf("%d\n", v.size());
v.push_back(l.insert(i, make_pair(y, x)));
break;
}
y = i->first + i->second;
}
if (y == m) {
puts("NULL");
}
} else if (ch == 'e') {
if (0 < x && x < (int)v.size() && v[x] != l.end()) {
l.erase(v[x]);
v[x] = l.end();
} else {
puts("ILLEGAL_ERASE_ARGUMENT");
}
} else {
y = 0;
for (list<pair<int, int> >::iterator i = l.begin(); i->first < m; ++i) {
i->first = y;
y += i->second;
}
}
}
return 0;
}
| 2 |
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> VI;
int main ( void )
{
int n, q;
while ( cin >> n >> q, n | q ) {
VI date_cnts( 101, 0 );
for ( int i = 0; i < n; ++i ) {
int m;
cin >> m;
for ( int j = 0; j < m; ++j ) {
int date;
cin >> date;
++date_cnts[date];
}
}
int ans = 0;
for ( int i = 1; i < 101; ++i ) {
if ( date_cnts[ans] < date_cnts[i] && q <= date_cnts[i] ) {
ans = i;
}
}
cout << ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
scanf("%I64d", &n);
vector<long long> a(n);
vector<long long> b(n);
for (long long i = 0; i < n; i++) {
scanf("%I64d", &a[i]);
}
for (long long i = 0; i < n; i++) {
scanf("%I64d", &b[i]);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
long long ta = 0;
long long tb = 0;
for (long long i = 0; i < n; i++) {
if (((!a.empty()) && a.back() > b.back()) || b.empty()) {
ta += a.back();
a.pop_back();
} else {
b.pop_back();
}
if (((!b.empty()) && b.back() > a.back()) || a.empty()) {
tb += b.back();
b.pop_back();
} else {
a.pop_back();
}
}
printf("%I64d\n", ta - tb);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, a[100005], sum, ans, ans2;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
ans2 = max(ans2 + a[i], 0);
ans = max(ans, ans2);
}
cout << ans - (sum - ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
long long int power(long long int x, long long int n) {
long long int result = 1;
while (n) {
if (n % 2 == 1) result = result * x;
n = n / 2;
x = x * x;
}
return result;
}
long long int gcd(long long int a, long long int b) {
if (!b) return a;
return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int BS(long long int a[], long long int s, long long int n,
long long int val) {
long long int mid, beg = s, end = n - 1;
while (beg <= end) {
mid = (beg + end) / 2;
if (val == a[mid]) {
break;
} else if (val > a[mid]) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
return mid;
}
inline long long int mul(long long int x, long long int y, long long int m) {
long long int z = 1LL * x * y;
if (z >= m) {
z %= m;
}
return z;
}
long long int powmod(long long int x, long long int y, long long int m) {
long long int r = 1;
while (y) {
if (y & 1) {
r = mul(r, x, m);
}
y >>= 1;
x = mul(x, x, m);
}
return r;
}
using namespace std;
void start() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
}
int main() {
start();
long long int t;
cin >> (t);
while (t--) {
long long int n, k;
cin >> n >> k;
if (n >= k) {
if ((n % 2 == 1 && k % 2 == 0) || (n % 2 == 0 && k % 2 == 1)) {
cout << "1\n";
continue;
}
cout << "0\n";
continue;
}
if (k > n) cout << k - n << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool ind = 0;
const int N = 2e5 + 100, M = 1e9 + 7;
int64_t n, m, ans = 1, vis[N], x, y, s[N], t, nex[N], ans1, val[N], sig;
vector<int64_t> adjt[N], adj[N];
int64_t dfs(int64_t node) {
int64_t cnt = 1;
vis[node] = 2;
for (int64_t i : adjt[node]) cnt += dfs(i);
return cnt;
}
void dfsc(int64_t node, int64_t put = 0) {
vis[node]++;
val[node] = put;
for (int64_t i : adj[node])
if (vis[i] == 1) {
sig = 1;
ans1 = val[node] - val[i] + 1;
} else if (vis[i] == 0)
dfsc(i, put + 1);
vis[node]++;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int64_t i = 1; i <= n; i++) {
cin >> x >> y;
adjt[y].push_back(x);
adj[x].push_back(y);
nex[x] = y;
s[x] = 1;
}
for (int64_t i = 1; i <= n << 1; i++) {
if (s[i] == 0 && adjt[i].size()) ans = (ans * dfs(i)) % M;
}
for (int64_t i = 1; i <= n << 1; i++) {
if (s[i] == 1 && vis[i] == 0) {
sig = 0;
ans1 = 0;
dfsc(i);
if (sig && ans1 > 1) ans = (ans << 1) % M;
}
}
cout << ans << '\n';
}
| 5 |