solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N; cin >> N;
string ans = "No";
for (int i=0; i*7<=N; ++i) if ((N-7*i)%4==0) ans = "Yes";
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int a, b, c;
cin >> a >> b >> c;
int x = a, y = b, z = c;
int sum = 0;
int sum1 = 0;
if (b / 2 > a) {
sum += a + 2 * a;
b = b - 2 * a;
} else {
sum += b / 2 + b / 2 * 2;
b = b - b / 2 * 2;
}
if (c / 2 > b) {
sum += b + 2 * b;
} else
sum += c / 2 + c / 2 * 2;
if (z / 2 > y) {
sum1 += y + 2 * y;
y = 0;
} else {
sum1 += z / 2 + z / 2 * 2;
y = y - z / 2;
}
if (y / 2 > x) {
sum1 += x + 2 * x;
} else
sum1 += y / 2 + y / 2 * 2;
cout << max(sum, sum1) << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const int INF = 1 << 28;
const long long INFL = 1LL << 62;
string str[] = {"YES", "NO"};
int main() {
int n;
cin >> n;
int x[7];
for (int i = 0; i < 7; i++) cin >> x[i];
for (int i = 0; true; i++) {
n -= x[i % 7];
if (n <= 0) {
cout << (i % 7) + 1 << endl;
return 0;
}
}
}
| 1 |
#include <iostream>
using namespace std;
int main() {
long long int data[10][1001] = {1};
for (int k = 0; k <= 100; ++k)
for (int i = 9; 1 <= i; --i)
for (int j = k; j <= 1000; ++j)
data[i][j] += data[i-1][j-k];
int n, s;
while (cin >> n >> s) {
if (n == 0)
break;
cout << data[n][s] << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> myodd, myeven;
bool cmp(int a, int b) { return a > b; }
int main() {
scanf("%d", &n);
int tmp;
int sum = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &tmp);
sum += tmp;
if (tmp % 2 == 0) {
myeven.push_back(tmp);
} else {
myodd.push_back(tmp);
}
}
sort(myeven.begin(), myeven.end(), cmp);
sort(myodd.begin(), myodd.end(), cmp);
int ans1 = 0;
int len1 = myeven.size(), len2 = myodd.size();
int i = 0, j = 0;
for (; i < len1 && j < len2; ++i, ++j) {
ans1 += myeven[i];
ans1 += myodd[j];
}
if (i < len1) {
ans1 += myeven[i];
}
if (j < len2) {
ans1 += myodd[j];
}
int ans2 = 0;
i = 0;
j = 0;
for (; i < len1 && j < len2; ++i, ++j) {
ans2 += myodd[j];
ans2 += myeven[i];
}
if (j < len2) {
ans2 += myodd[j];
}
if (i < len1) {
ans2 += myeven[i];
}
printf("%d\n", sum - max(ans1, ans2));
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxpq = priority_queue<T>;
inline long long CC(long long n, long long y) {
return (((n) + (y - 1) - 1) / (y - 1)) - (((n) + (y)-1) / (y));
}
inline long long FF(long long n, long long y) { return n / y - n / (y + 1); }
inline int add(int a, int b, int p = 1000000007) {
int c = a + b;
if (c >= p) c -= p;
return c;
}
inline int sub(int a, int b, int p = 1000000007) {
int c = a - b;
if (c < 0) c += p;
return c;
}
inline int mul(int a, int b, int p = 1000000007) { return (a * 1ll * b) % p; }
int __gcd(int a, int b) { return !b ? a : __gcd(b, a % b); }
int power(int a, int b, int p = 1000000007) {
int res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a, p);
a = mul(a, a, p);
b >>= 1;
}
return res;
}
inline int Fermat(int a, int p = 1000000007) { return power(a, p - 2, p); }
inline int Div(int a, int b, int p) { return mul(a, Fermat(b, p), p); }
vector<int> factorize(int p) {
vector<int> v;
for (int i = 2; i * i <= p; i++)
if (p % i == 0) {
v.push_back(i);
while (p % i == 0) p /= i;
}
if (p != 1) v.push_back(p);
return v;
}
int Tot_fun(int n) {
int phi = n;
vector<int> v = factorize(n);
for (int i : v) phi /= i, phi *= (i - 1);
return phi;
}
int generator(int p, bool isPrime = true) {
if (p == 2) return 1;
int phi;
phi = isPrime ? p - 1 : Tot_fun(p);
vector<int> v = factorize(phi);
for (int g = 2; g <= p; g++) {
bool ok = true;
if (!isPrime && __gcd(p, g) != 1) continue;
for (int i = 0; i < v.size() && ok; i++)
ok &= (power(g, phi / v[i], p) != 1);
if (ok) {
return g;
}
}
return -1;
}
bool isPrime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
int n, g[100005];
void prep() {
int gen = generator(n);
g[0] = 1;
for (int i = (1); i < (n); i++) {
g[i] = mul(g[i - 1], gen, n);
}
}
int32_t main() {
clock_t clk = clock();
cerr << "I will return...\n";
cin >> n;
if (n == 1)
cout << "YES\n1\n";
else if (n == 4)
cout << "YES\n1\n3\n2\n4\n";
else if (isPrime(n)) {
cout << "YES\n";
prep();
for (int i = (1); i < (n + 1); i++) {
if (i == n)
cout << n << '\n';
else if (i % 2)
cout << g[n - i] << '\n';
else
cout << g[i - 1] << '\n';
}
} else
cout << "NO\n";
cerr << "...don't you ever hang your head.\n";
cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC
<< '\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int numlet = 26;
int dp[maxn][numlet];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
for (int i = 1; i <= n; ++i) {
int idx = (s[i - 1] - 'a');
for (int j = 0; j < 26; ++j) {
if (j == idx) {
dp[i][j] = dp[i - 1][j] + 1;
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
int m;
cin >> m;
while (m--) {
string t;
cin >> t;
vector<int> cur(26, 0);
for (auto& v : t) {
cur[(int)(v - 'a')]++;
}
int lo = 0, hi = n;
while (lo + 1 < hi) {
int mid = (lo + hi) >> 1;
bool can = true;
for (int i = 0; i < 26; ++i) {
if (dp[mid][i] < cur[i]) {
can = false;
break;
}
}
if (can) {
hi = mid;
} else {
lo = mid;
}
}
cout << hi << '\n';
}
return 0;
}
| 2 |
#include<iostream>
//#include<string>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
if(a<b&&b<c){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
long long map[5][5];
long long dp[40 + 5][8][8];
long long min(long long a, long long b) {
if (a > b)
return b;
else
return a;
}
int main() {
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++) scanf("%I64d", &map[i][j]);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 3; k++) {
if (k == j)
continue;
else {
int f;
for (int s = 1; s <= 3; s++)
if (s == j || s == k)
continue;
else
f = s;
dp[i][j][k] = min(dp[i - 1][j][f] + map[j][k] + dp[i - 1][f][k],
dp[i - 1][j][k] + map[j][f] + dp[i - 1][k][j] +
map[f][k] + dp[i - 1][j][k]);
}
}
}
printf("%I64d\n", dp[n][1][3]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, t, r;
int a[310];
cin >> m >> t >> r;
for (int i = 0; i < m; i++) cin >> a[i];
sort(a, a + m);
int ans = 0;
set<int> cand;
for (int i = 0; i < m; i++) {
while (!cand.empty() && *(cand.begin()) <= a[i]) cand.erase(cand.begin());
int x = max(0, (int)(r - cand.size()));
int val = t, j = 0;
while (val >= 1 && j < x) {
if (cand.find(a[i] + val) == cand.end()) {
cand.insert(a[i] + val);
j++;
}
val--;
}
if (j < x) {
ans = -1;
break;
}
ans += x;
}
cout << ans;
return 0;
}
| 3 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 100005
using namespace std;
int n, ans, c = 1, a[N];
int main()
{
int i;
cin >> n;
for (i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
for (i = 0; i < n; i++) {
if (a[i] == a[i + 1]) c++;
else ans += c < a[i] ? c : c - a[i], c = 1;
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, i, y, x, s, xx, yy, j;
cin >> a >> b;
for (x = a - 1; x >= 1; x--) {
s = a * a - x * x;
y = (int)sqrt(s);
if (y * y != s) continue;
for (j = b - 1; j >= 1; j--) {
s = b * b - j * j;
yy = (int)sqrt(s);
if (yy * yy != s) continue;
xx = -j;
if (yy != y &&
(x - xx) * (x - xx) + (y - yy) * (y - yy) == a * a + b * b) {
cout << "YES" << endl;
cout << "0 0" << endl
<< x << " " << y << endl
<< xx << " " << yy << endl;
return 0;
}
}
}
cout << "NO";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
long long lower(long long x) {
long long ret = 0;
for (long long i = 1; i <= n; i++) ret += min(m, x / i);
return ret;
}
int main() {
scanf("%I64d%I64d%I64d", &n, &m, &k);
long long l = 1, r = n * m + 1;
while (l < r) {
long long mid = (l + r) / 2;
long long now = lower(mid);
if (now >= k)
r = mid;
else
l = mid + 1;
}
printf("%I64d", l);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s; cin >> s;
cout << s.substr(0, s.length() - 8) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int t = 0;
for (int i = 0; i < n;) {
int j = i + 1;
if (s[i] == 'A')
while (j < n && s[j] == 'P') ++j;
t = max(t, j - i - 1);
i = j;
}
cout << t << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int M = 110, N = 20020;
const double pi = acos(-1.0), INF = 1e9;
int n, m, T, X, u, v, w, L, o, rv[N << 2], d[M][M];
double p[M][N], f[M][N], g[M][N], s[M][N];
char bu[100100], *cl(bu), *cr(bu);
struct edge {
int u, v, w;
} E[M];
struct cpx {
double x, y;
cpx(double _x = 0, double _y = 0) { x = _x, y = _y; }
cpx operator+(const cpx &a) const { return cpx(x + a.x, y + a.y); }
cpx operator-(const cpx &a) const { return cpx(x - a.x, y - a.y); }
cpx operator*(const cpx &a) const {
return cpx(x * a.x - y * a.y, x * a.y + y * a.x);
}
} W[N << 2], a[N << 2], b[N << 2];
int read() {
char ch = cl == cr && (cr = (cl = bu) + fread(bu, 1, 100000, stdin), cl == cr)
? EOF
: *cl++;
int x = 0;
for (; ch < '0' || '9' < ch;
ch = cl == cr && (cr = (cl = bu) + fread(bu, 1, 100000, stdin), cl == cr)
? EOF
: *cl++)
;
for (; '0' <= ch && ch <= '9';
ch = cl == cr && (cr = (cl = bu) + fread(bu, 1, 100000, stdin), cl == cr)
? EOF
: *cl++)
x = x * 10 + (ch ^ 48);
return x;
}
void ini(int n) {
for (L = 1, o = 0; L <= n; L <<= 1, o++)
;
for (int i = 1; i < L; i++) rv[i] = rv[i >> 1] >> 1 | (i & 1) << (o - 1);
cpx W1(cos(2 * pi / L), sin(2 * pi / L));
W[L >> 1] = 1;
for (int i = (L >> 1) + 1; i < L; i++) W[i] = W[i - 1] * W1;
for (int i = (L >> 1) - 1; i >= 1; i--) W[i] = W[i << 1];
}
void DFT(cpx *A, int L) {
static cpx B[N << 2], t;
int u = o - __builtin_ctz(L);
for (int i = 0; i < L; i++) B[i] = A[rv[i] >> u];
for (int i = 1; i < L; i <<= 1)
for (int j = 0, s = i << 1; j < L; j += s)
for (int k = 0; k < i; k++)
t = B[i + j + k] * W[i + k], B[i + j + k] = B[j + k] - t,
B[j + k] = B[j + k] + t;
for (int i = 0; i < L; i++) A[i] = B[i];
}
void IFT(cpx *A, int L) {
reverse(A + 1, A + L);
DFT(A, L);
for (int i = 0; i < L; i++) A[i].x /= L, A[i].y /= L;
}
void cal(int t) {
for (int i = 1; i < n; i++) f[i][t] = INF;
for (int i = 1; i <= m; i++) {
u = E[i].u, v = E[i].v, w = E[i].w;
f[u][t] = min(f[u][t], w + g[i][t] + s[i][t + 1] * (d[v][n] + X));
}
}
int upl(int n) { return 1 << (32 - __builtin_clz(n)); }
void con(int l, int r, double *F, double *G, double *H) {
int md = (l + r) >> 1, L = upl(r - l);
for (int i = 0; i < L; i++) a[i] = cpx(i <= md - l ? F[i + l] : 0, 0);
for (int i = 0; i < L; i++) b[i] = cpx(i <= r - l ? G[i] : 0, 0);
DFT(a, L);
DFT(b, L);
for (int i = 0; i < L; i++) a[i] = a[i] * b[i];
IFT(a, L);
for (int i = md + 1; i <= r; i++) H[i] += a[i - l].x;
}
void sol(int l, int r) {
if (l == r) return cal(l);
int md = (l + r) >> 1;
sol(l, md);
for (int i = 1; i <= m; i++) con(l, r, f[E[i].v], p[i], g[i]);
sol(md + 1, r);
}
int main() {
n = read();
m = read();
T = read();
X = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) d[i][j] = i == j ? 0 : INF;
for (int i = 1; i <= m; i++) {
u = read();
v = read();
w = read();
d[u][v] = min(d[u][v], w);
E[i] = (edge){u, v, w};
for (int j = 1; j <= T; j++) p[i][j] = read(), p[i][j] /= 1e5;
for (int j = T; j >= 1; j--) s[i][j] = s[i][j + 1] + p[i][j];
if (u == n) i--, m--;
}
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]);
ini(T << 1);
sol(0, T);
printf("%.9lf", f[1][T]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
const int INF = 10101010;
int main(){
int cc = 0;
int n;
while(cin >>n,n){
vector<string> s(n);
vector<int> p(n),l(n);
rep(i,n){
cin >>s[i] >>p[i];
l[i] = s[i].size();
}
string t;
cin >>t;
int T = t.size();
vector<int> dp(T+1,-INF);
dp[0] = 0;
rep(i,T){
rep(j,n){
if(i+l[j] > T) continue;
if(s[j] == t.substr(i,l[j])){
dp[i+l[j]] = max(dp[i+l[j]], dp[i]+p[j]);
}
}
}
int V = dp[T];
dp = vector<int>(V+1, INF);
dp[0] = 0;
rep(i,V){
rep(j,n){
int ni = i+p[j];
if(ni > V) continue;
dp[ni] = min(dp[ni], dp[i]+l[j]);
}
}
cout << "Case " << cc+1 << ": " << dp[V] << "\n";
++cc;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
stack<char> st;
set<char> se;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
string s;
int i, j, k, x, y, z, a, b, c;
cin >> s;
se.insert('a');
se.insert('e');
se.insert('i');
se.insert('o');
se.insert('u');
for (i = 0; s[i]; ++i) {
if (se.find(s[i]) != se.end()) {
st.push(s[i]);
x = y = 0;
} else if (st.empty()) {
st.push(s[i]);
x = y = 1;
} else {
x++;
if (s[i] == st.top()) {
y++;
} else {
y = 1;
}
if (x >= 3 && y < 3) {
string str = "";
while (!st.empty()) {
str += st.top();
st.pop();
}
reverse(str.begin(), str.end());
cout << str << " ";
x = y = 0;
i--;
} else {
st.push(s[i]);
}
}
}
if (!st.empty()) {
string str = "";
while (!st.empty()) {
str += st.top();
st.pop();
}
reverse(str.begin(), str.end());
cout << str << " ";
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
#define ce(i, r) i==r?'\n':' '
using namespace std;
const int N = 405, M = 605, MOD = 998244353;
int head[N], ver[M << 1], Next[M << 1], tot;
int d[N][N], cnt[N][N], n, m;
queue<int> q;
inline void add(int x, int y) {
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
inline void bfs(int s) {
memset(d[s], 0x3f, sizeof(int) * (n + 1));
d[s][s] = 0, cnt[s][s] = 1, q.push(s);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = head[x], y; i; i = Next[i]) {
y = ver[i];
if (d[s][y] > d[s][x] + 1) {
d[s][y] = d[s][x] + 1;
cnt[s][y] = cnt[s][x];
q.push(y);
} else if (d[s][y] == d[s][x] + 1)
cnt[s][y] += cnt[s][x];
}
}
}
inline int calc(int x, int y) {
int res = 1;
for (int u = 1, t, v; u <= n; u++) {
if (d[x][u] + d[u][y] == d[x][y])continue;
t = 0;
for (int i = head[u]; i; i = Next[i]) {
v = ver[i];
if (d[x][v] + 1 == d[x][u] && d[y][v] + 1 == d[y][u])
t++;
}
res = (1ll * res * t) % MOD;
}
return res;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1, x, y; i <= m; i++) {
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
for (int x = 1; x <= n; x++)bfs(x);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
printf("%d%c", cnt[i][j] > 1 ? 0 : calc(i, j), ce(j, n));
return 0;
} | 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native")
using namespace std;
long long gcd(long long i, long long j) {
if (j == 0)
return i;
else
return gcd(j, i % j);
}
template <typename T>
inline T getint() {
T val = 0;
char c;
bool neg = false;
while ((c = getchar()) && !(c >= '0' && c <= '9')) {
neg |= c == '-';
}
do {
val = (val * 10) + c - '0';
} while ((c = getchar()) && (c >= '0' && c <= '9'));
return val * (neg ? -1 : 1);
}
const long long INF = 1e9 + 100;
const int mod = 998244353;
const double eps = 1e-11, pi = acos(-1);
const long long maxN = 13, maxT = 600100, A = 179, K = 170;
mt19937 mt_rand(time(0));
long long bp(long long et, long long b) {
b %= mod - 1;
et %= mod;
long long res = 1;
for (int i = 30; i >= 0; --i) {
res = (res * res) % mod;
if ((b & (1 << i)) != 0) res = (res * et) % mod;
}
return res;
}
void panic() {
cout << "-1\n";
exit(0);
}
long long c[maxN][maxN];
long long f[maxN];
void build() {
f[0] = 1;
for (int i = 1; i < maxN; ++i) f[i] = f[i - 1] * i;
for (int i = 0; i < maxN; ++i) {
c[i][0] = c[0][i] = 1;
}
for (int i = 1; i < maxN; ++i) {
for (int j = 1; j < maxN; ++j) {
c[i][j] = c[i - 1][j] + c[i][j - 1];
}
}
}
long long C(int n, int k) { return c[n - k][k]; }
long long AA(int n, int k) { return f[n] / f[n - k]; }
int g[maxN][maxN];
int n;
int used[maxN];
set<vector<int>> clr[maxN];
vector<pair<int, int>> pr;
vector<int> col;
void rec1(int lvl, int clrr = 1) {
if (lvl == n / 2) {
clr[clrr - 1].insert(col);
return;
}
for (int i = 1; i <= clrr; ++i) {
col[pr[lvl].first - 1] = col[pr[lvl].second - 1] = i;
rec1(lvl + 1, clrr + (i == clrr));
}
}
void rec(int lvl) {
if (lvl == n / 2) {
vector<int> tmp(n / 2);
for (int i = 0; i < n / 2; ++i) tmp[i] = i;
bool gg = 0;
do {
bool gd = 1;
for (int i = 1; i < n / 2; ++i) {
if (!g[pr[tmp[i - 1]].first][pr[tmp[i]].first] ||
!g[pr[tmp[i - 1]].second][pr[tmp[i]].second]) {
if (!g[pr[tmp[i - 1]].first][pr[tmp[i]].second] ||
!g[pr[tmp[i - 1]].second][pr[tmp[i]].first]) {
gd = 0;
break;
}
}
}
gd &= g[pr[tmp.back()].first][pr[tmp.back()].second];
if (gd) {
gg = 1;
break;
}
} while (next_permutation(tmp.begin(), tmp.end()));
if (!gg) return;
col.assign(n, 0);
rec1(0);
return;
}
int st = 1;
while (used[st]) ++st;
for (int to = st + 1; to <= n; ++to) {
if (used[to]) continue;
used[st] = used[to] = 1;
pr.push_back({st, to});
rec(lvl + 1);
pr.pop_back();
used[st] = used[to] = 0;
}
}
void solve() {
build();
int m, k;
cin >> n >> m >> k;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
g[a][b] = g[b][a] = 1;
}
rec(0);
long long ans = 0;
for (int i = 1; i <= k; ++i) {
long long tmp = AA(k, i);
ans += (long long)clr[i].size() * tmp;
}
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(20);
srand(time(0));
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<pair<int, int>, int> > A;
int n, m;
int B[1000000];
int C[10];
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
pair<pair<int, int>, int> a;
cin >> a.first.first >> a.first.second;
a.second = i;
A.push_back(a);
}
sort(A.begin(), A.end());
int t = 0;
for (int i = 0; i < A.size(); i++) {
t = A[i].first.first;
while (i + 1 < A.size() && A[i + 1].first.first == t) {
B[A[i].second]++;
B[A[i + 1].second]++;
i++;
}
}
for (int i = 0; i < A.size(); i++) {
swap(A[i].first.first, A[i].first.second);
}
sort(A.begin(), A.end());
t = 0;
for (int i = 0; i < A.size(); i++) {
t = A[i].first.first;
while (i + 1 < A.size() && A[i + 1].first.first == t) {
B[A[i].second]++;
B[A[i + 1].second]++;
i++;
}
}
for (int i = 0; i < A.size(); i++) {
int a = A[i].first.first + A[i].first.second;
int b = A[i].first.first - A[i].first.second;
A[i].first.first = a;
A[i].first.second = b;
}
sort(A.begin(), A.end());
t = 0;
for (int i = 0; i < A.size(); i++) {
t = A[i].first.first;
while (i + 1 < A.size() && A[i + 1].first.first == t) {
B[A[i].second]++;
B[A[i + 1].second]++;
i++;
}
}
for (int i = 0; i < A.size(); i++) {
swap(A[i].first.first, A[i].first.second);
}
sort(A.begin(), A.end());
t = 0;
for (int i = 0; i < A.size(); i++) {
t = A[i].first.first;
while (i + 1 < A.size() && A[i + 1].first.first == t) {
B[A[i].second]++;
B[A[i + 1].second]++;
i++;
}
}
for (int i = 0; i < A.size(); i++) C[B[i]]++;
for (int i = 0; i < 9; i++) cout << C[i] << " ";
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int a, b, p, x;
long long int fexp(long long int k, int e, long long int mod = p) {
long long int ans = 1;
while (e) {
if (e & 1) {
ans = k * ans % mod;
}
k = k * k % mod;
e >>= 1;
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> a >> b >> p >> x;
long long int ans = 0;
if (p == 2) {
ans = (x + 1) / 2;
cout << ans;
return 0;
}
for (int k2 = 0; k2 < p - 1; k2++) {
long long int ak2 = fexp(a, k2, p);
long long int k1 = (b * fexp(ak2, p - 2, p)) % p;
long long int m = p * (p - 1);
long long int x1 = fexp(p - 1, p - 2, p);
long long int x2 = fexp(p, p - 3, p - 1);
long long int M1 = p - 1;
long long int M2 = p;
long long int sol = (k1 * M1 * x1 + k2 * M2 * x2) % m;
ans += x / m;
if (x % m >= sol) {
ans++;
}
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long MAXN = 1e5 + 20, MOD = 1e9 + 9;
long long n, m, cmp[MAXN], ans = 1;
vector<long long> c[MAXN];
void dsu(long long u, long long v) {
u = cmp[u], v = cmp[v];
if (u == v) return;
if (c[u].size() > c[v].size()) swap(u, v);
for (auto x : c[u]) cmp[x] = v, c[v].push_back(x);
c[u].clear();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i = 0; i < n; i++) cmp[i] = i, c[i].push_back(i);
for (long long i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
u--, v--;
ans %= MOD;
ans *= (1 + (bool)(cmp[u] == cmp[v]));
ans %= MOD;
cout << (ans - 1) % MOD << endl;
dsu(u, v);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const ld pi = acos(0) * 2;
template <typename T>
inline void read(T &x) {
char c;
bool nega = 0;
while ((!isdigit(c = getchar())) && (c != '-'))
;
if (c == '-') {
nega = 1;
c = getchar();
}
x = c - 48;
while (isdigit(c = getchar())) x = x * 10 + c - 48;
if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
if (x > 9) writep(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
writep(x);
}
template <typename T>
inline void writeln(T x) {
write(x);
putchar('\n');
}
int t, n;
string s;
int main() {
cin >> t;
while (t--) {
cin >> n >> s;
reverse(s.begin(), s.end());
while ((!s.empty()) && (s.back() != '8')) s.pop_back();
if (s.size() < 11)
puts("NO");
else
puts("YES");
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long BL[N], a[N];
struct query {
long long l, r, id;
};
bool cmp(query a, query b) {
if (BL[a.l] != BL[b.l]) return BL[a.l] < BL[b.l];
return BL[a.l] % 2 ? a.r > b.r : a.r < b.r;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= n; i++) cin >> a[i];
vector<query> v(m);
for (long long i = 0; i < m; i++) {
long long l, r;
cin >> v[i].l >> v[i].r;
v[i].id = i;
}
long long size = sqrt(n);
for (long long i = 1; i <= n; i++) BL[i] = (i - 1) / size + 1;
sort(v.begin(), v.end(), cmp);
long long cl = v[0].l, cr = v[0].l - 1;
unordered_map<long long, long long> mp;
long long ans = 0;
long long out[m];
for (long long i = 0; i < m; i++) {
while (cl < v[i].l) {
if (mp[a[cl]] == a[cl]) ans--;
mp[a[cl]]--;
if (mp[a[cl]] == a[cl]) ans++;
cl++;
}
while (cl > v[i].l) {
cl--;
if (mp[a[cl]] == a[cl]) ans--;
mp[a[cl]]++;
if (mp[a[cl]] == a[cl]) ans++;
}
while (cr < v[i].r) {
cr++;
if (mp[a[cr]] == a[cr]) ans--;
mp[a[cr]]++;
if (mp[a[cr]] == a[cr]) ans++;
}
while (cr > v[i].r) {
if (mp[a[cr]] == a[cr]) ans--;
mp[a[cr]]--;
if (mp[a[cr]] == a[cr]) ans++;
cr--;
}
out[v[i].id] = ans;
}
for (long long i = 0; i < m; i++) cout << out[i] << '\n';
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void minimize(X &x, const Y &y) {
if (x > y) x = y;
}
template <class X, class Y>
void maximize(X &x, const Y &y) {
if (x < y) x = y;
}
template <class T>
T Abs(const T &x) {
return (x < 0 ? -x : x);
}
class FenwickTree {
private:
vector<int> v;
int n;
int get(int x) const {
int res = 0;
for (; x >= 1; x &= x - 1) res += v[x];
return res;
}
public:
FenwickTree(int n = 0) {
this->n = n;
v.assign(n + 7, 0);
}
void update(int x, int d) {
for (; x <= n; x += x & -x) v[x] += d;
}
int get(int l, int r) const {
if (l > r) return 0;
return l == 1 ? get(r) : get(r) - get(l - 1);
}
};
int a[200200], n;
void init(void) {
scanf("%d", &n);
for (int i = (1), _b = (n); i <= _b; i++) scanf("%d", &a[i]);
for (int i = (1), _b = (n); i <= _b; i++) minimize(a[i], n);
}
bool compareValue(const int &x, const int &y) { return a[x] > a[y]; }
void process(void) {
vector<int> indices;
for (int i = (1), _b = (n); i <= _b; i++) indices.push_back(i);
sort((indices).begin(), (indices).end(), compareValue);
long long res = 0;
FenwickTree bit(n);
int j = 0;
for (int i = (n), _a = (1); i >= _a; i--) {
while (j < indices.size() && a[indices[j]] >= i) {
bit.update(indices[j], 1);
j++;
}
res += bit.get(i + 1, a[i]);
}
cout << res << endl;
}
int main(void) {
init();
process();
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
#define fr(i, n) for(ll i = 0; i < n; i++)
#define frr(i, n) for(ll i = 1; i <= n; i++)
#define frm(i, n) for(ll i = n-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
typedef long long ll;
typedef pair<ll,ll> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> ponto;
typedef vector<vector<ll>> matrix;
#define mem(v, k) memset(v, k, sizeof(v));
#define db cout << "Debug" << endl;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mp make_pair
#define pq priority_queue
#define mx(a, b) a = max(a, b);
#define mod(a, b) a = a%b;
#define MAXN 1000010
#define MOD 1000000007
#define MAXL 30
#define ROOT 1
#define INF 1000000000000000100
vector<pii> adj[MAXN];
ll pr[MAXN];
ll k, l, r, t, x, y;
ll dfs(ll v, ll tempo){
pr[v] = 1;
if(v == x + 1) return 0;
ll ret = 1;
for(auto x : adj[v]){
ll u = x.f, w = x.s;
if(tempo + w <= t && !pr[u]){
ret *= dfs(u, tempo + w);
}
}
return ret;
}
int main(){
fastio;
cin >> k >> l >> r >> t >> x >> y;
if(x > y){
if(k + y <= r) k += y;
if(k - x < l){
cout << "No" << endl;
return 0;
}
k -= x;
t--;
if((k - l)/(x - y) + 1 <= t) cout << "No\n";
else cout << "Yes\n";
return 0;
}
if(x == y){
if(k + y <= r) k += y;
if(k - x < l){
cout << "No" << endl;
return 0;
}
cout << "Yes\n";
return 0;
}
if(l + x + y <= r + 1){
cout << "Yes\n";
return 0;
}
if(l + y > r && x > 1){
cout << "No\n";
return 0;
}
if(x == 1){
if(r - y < l && k - t < l) cout << "No\n";
else cout << "Yes\n";
return 0;
}
ll i;
for(i = l%x; (i + y)%x != (r + 1)%x; i = (i + 1)%x){
adj[i].pb({(i + y)%x, y/x});
// cout << i << " " << (i + y)%x << " " << y/x << endl;
}
for(; i != l%x; i = (i + 1)%x){
adj[i].pb({x + 1, (ll)1});
// cout << i << " " << x + 1 << " " << 1 << endl;
}
adj[x].pb({k%x, (k - l)/x});
//cout << x << " " << k%x << " " << (k - l)/x << endl;
/*
Temos que x < y
*/
if(dfs(x, 0)){
cout << "Yes\n";
}
else cout << "No\n";
} | 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> a(26, 0);
for (int i = 0; i < n; i++) {
a[s[i] - 65]++;
}
sort(a.begin(), a.end());
int sum = 0;
long long v = 0;
for (int i = 25; i >= 0; i--) {
if (sum + a[i] <= k) {
sum += a[i];
v += (long long)a[i] * a[i];
} else
break;
}
if (sum < s.size()) {
v += (long long)(k - sum) * (k - sum);
}
cout << v;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y, m, w, i = 0, j = 0;
cin >> n;
if (n == 0 || n == 1) {
cin >> x >> y;
cout << "-1" << endl;
}
int v[4][2];
for (i = 0; i < n; ++i)
for (j = 0; j < 2; ++j) cin >> v[i][j];
if (n == 2) {
if (v[0][0] == v[1][0] || v[1][1] == v[0][1])
cout << "-1" << endl;
else {
int a = v[1][0] - v[0][0];
int b = v[1][1] - v[0][1];
int s = a * b;
if (s > 0)
cout << s << endl;
else
cout << s * -1 << endl;
}
}
if (n == 3 || n == 4) {
for (i = 0; i < n; ++i)
for (j = i + 1; j < n; ++j)
if (v[i][0] == v[j][0]) m = v[i][1] - v[j][1];
for (int g = 0; g < n; ++g)
for (int h = g + 1; h < n; ++h)
if (v[g][1] == v[h][1]) w = v[g][0] - v[h][0];
int k = m * w;
if (k == 0) cout << "-1" << endl;
if (k > 0) cout << k << endl;
if (k < 0) cout << k * -1 << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
typedef pair<int,int> P;
const int INF=numeric_limits<int>::max();
const int N=1e5+5;
int n,m,dist[N];
vector<P> adj[N];
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
for(int i=0;i<m;++i){
int l,r,d;
cin>>l>>r>>d;
adj[l].emplace_back(r,d);
adj[r].emplace_back(l,-d);
}
for(int i=1;i<=n;++i) dist[i]=INF;
for(int i=1;i<=n;++i)
if(dist[i]==INF){
dist[i]=0;
queue<int> Q;
Q.push(i);
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(P nxt:adj[u]){
int v=nxt.first;
int d=nxt.second;
if(dist[v]==INF){
dist[v]=dist[u]+d;
Q.push(v);
}
else if(dist[v]!=dist[u]+d){
return cout<<"No\n",0;
}
}
}
}
cout<<"Yes\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
(ios_base::sync_with_stdio(false), cin.tie(NULL));
int n;
cin >> n;
vector<int> p(n);
for (int i = 0; i < (int)(n); ++i) cin >> p[i];
int l, r, mx, mn;
l = r = 0;
mn = mx = p[0];
multiset<int> s;
s.insert(p[0]);
int ans = 1;
while (r < n) {
if (mx - mn <= 1) {
++r;
if (r < n) s.insert(p[r]);
} else {
multiset<int>::iterator it = s.find(p[l]);
s.erase(it);
++l;
}
mn = *(s.begin()), mx = *(s.rbegin());
if (mx - mn <= 1 && r < n) ans = max(ans, r - l + 1);
}
cout << ans;
getchar();
getchar();
return 0;
}
| 2 |
#include <bits/stdc++.h>
long long a[100000 + 20];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
long long maxi = 0, max = -999999;
long long k = 1;
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
if (abs(a[i]) < abs(-1 - a[i]) && -1 - a[i] != 0) a[i] = -1 - a[i];
if (a[i] >= 0)
k *= 1;
else
k = -1 * k;
if (abs(a[i]) >= max) {
max = abs(a[i]);
maxi = i;
}
}
if (k > 0) {
for (int i = 0; i < n; i++) printf("%d ", a[i]);
puts("");
} else {
a[maxi] = -1 - a[maxi];
for (int i = 0; i < n; i++) printf("%d ", a[i]);
puts("");
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int N = 500001;
vector<int> g[N];
set<int> s[N];
int a[N], d[N];
vector<int> ord;
void dfs(int v) {
if (d[v] || s[v].size() < a[v] - 1) return;
d[v] = 1;
ord.push_back(v);
for (int u : g[v]) {
if (a[u] == a[v]) {
cout << -1;
exit(0);
}
if (a[v] < a[u]) s[u].insert(a[v]);
dfs(u);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, u, v;
cin >> n >> m;
for (int i = 0; i < (m); i++) {
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < (n); i++) cin >> a[i];
for (int i = 0; i < (n); i++) dfs(i);
for (int i = 0; i < (n); i++)
if (!d[i]) {
cout << -1;
return 0;
}
for (int i : ord) cout << i + 1 << ' ';
}
| 4 |
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<(n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) (s.substr(f-1,t-f+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
struct Grid{ll x,y,t;};
struct Edge{ll to,cost;};
struct Graph{vector<vector<Edge>>E;int V;
const ll Inf = llINF;const int MAX_V=201010;vector<ll>d;
Graph(int n):E(n){d.resize(MAX_V);E.resize(n);V=n;}
void init(){for(int i=0;i<MAX_V;i++)d[i]=Inf;}
void add_edge(ll from,ll to,ll cost){E[from].pb({to,cost});}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll n;cin>>n;
vi stone(pow(2,n));
for(auto &a:stone)cin>>a;
for(int i=n;i>=1;i--){
vi hoge;
for(int j=0;j<stone.size();j+=2){
if(stone[j]==stone[j+1]){
hoge.pb(stone[j]);
}else{
hoge.pb(abs(stone[j]-stone[j+1]));
}
}
stone.clear();
for(auto a:hoge)stone.pb(a);
}
cout<<stone[0]<<endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
int a[n];
for(int i = 0; i < n; ++i) {
cin >> a[i];
a[i] -= i + 1;
}
sort(a, a + n);
long long sum = 0, mid = a[n/2];
for(int i = 0; i < n; ++i) sum += abs(a[i] - mid);
cout << sum;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dig(long long n) {
int ans = 0;
while (n > 0) {
n /= 10;
ans++;
}
return ans;
}
set<long long> s;
int main() {
long long n;
while (cin >> n) {
int sz = dig(n);
s.clear();
for (int i = 0; i <= 9; i++) {
for (int j = 0; j <= 9; j++) {
for (int k = 1; k < (1 << sz); k++) {
long long p = 1;
long long num = 0;
int kk = k;
while (kk > 0) {
if (kk & 1)
num += i * p;
else
num += j * p;
kk >>= 1;
p *= 10;
}
if (num <= n && num > 0) {
s.insert(num);
}
}
}
}
cout << s.size() << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
double ans;
vector<int> vec[1001000];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
vec[x].push_back(i);
}
for (int i = 1; i <= 1000000; i++) {
double cnt = 0;
for (int j = 0; j < vec[i].size(); j++) {
double l = 0, r;
if (j) l = vec[i][j - 1] + 1;
r = vec[i][j] - 1;
cnt += max(0.0, (r - l + 1) * (r - l + 1));
}
if (!vec[i].empty())
cnt += max(0.0, (double)(n - vec[i][vec[i].size() - 1] - 1) *
(double)(n - vec[i][vec[i].size() - 1] - 1));
ans += ((double)n * (double)n - cnt) * (!vec[i].empty());
}
cout << fixed << setprecision(9);
cout << ans / n / n << endl;
}
| 6 |
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int main(){
int q,lim,query,x;
while(1){
deque<int> l;
cin >> q >> lim;
if(!q && !lim)break;
for(int i=0;i<q;i++){
cin >> query >> x;
if(query==0){
l.push_back(x);
if((int)l.size()>lim)l.pop_front();
}
if(query==1)l.erase(l.begin()+x-1);
if(query==2)cout << *(l.begin()+x-1) << endl;
if(query==3)l.erase(find(l.begin(),l.end(),x));
}
cout << "end" << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int fa[100001];
inline int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); }
int main() {
int m, n;
cin >> m >> n;
vector<int> a(m), b(n);
for (int i = 0; i < (m); i++) cin >> a[i];
for (int i = 0; i < (n); i++) cin >> b[i];
vector<vector<int>> e(m);
for (int i = 0; i < (m); i++) {
int s;
cin >> s;
for (int j = 0; j < (s); j++) {
int te;
cin >> te;
te--;
e[i].push_back(te);
}
}
for (int i = 0; i < (n); i++) fa[i] = i;
vector<pair<int, P>> ppv;
for (int i = 0; i < (m); i++)
for (int j : e[i]) ppv.push_back(make_pair(a[i] + b[j], make_pair(i, j)));
sort(ppv.rbegin(), ppv.rend());
vector<int> par(m, -1);
ll tot = 0;
for (auto pp : ppv) {
int c = pp.first, i = pp.second.first, j = pp.second.second;
bool cut = false;
if (par[i] == -1)
par[i] = j;
else {
int u = get(par[i]), v = get(j);
if (u != v)
fa[v] = u;
else
cut = true;
}
if (cut) tot += c;
}
cout << tot << endl;
return 0;
}
| 5 |
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define maxn (111)
int a[maxn][maxn], xor_[maxn << 1];
signed main()
{
int t; cin >> t; while(t--) {
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%lld", &a[i][j]);
memset(xor_, 0, sizeof(xor_));
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
xor_[i + j] ^= a[i][j];
}
}
int ok = 1;
for(int i = 0; i < maxn * 2; i++)
if(xor_[i]) {ok = 0; break;}
cout << (ok? "Jeel": "Ashish") << endl;
}
return 0;
} | 6 |
#include <bits/stdc++.h>
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline void sub(long long &a, long long b) {
a -= b;
if (a < 0) a += 1000000007;
}
inline void add(long long &a, long long b) {
a += b;
if (a >= 1000000007) a -= 1000000007;
}
template <typename T>
inline T const &MAX(T const &a, T const &b) {
return a > b ? a : b;
}
template <typename T>
inline T const &MIN(T const &a, T const &b) {
return a < b ? a : b;
}
inline long long qp(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % 1000000007;
a = a * a % 1000000007, b >>= 1;
}
return ans;
}
inline long long qp(long long a, long long b, long long c) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % c;
a = a * a % c, b >>= 1;
}
return ans;
}
using namespace std;
const unsigned long long ba = 233;
const double eps = 1e-5;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int N = 20000 + 10, maxn = 1000000 + 10, inf = 0x3f3f3f3f;
int a[N], n, ans;
void gao(int x) {
for (int i = 1; i <= x; i++) {
int sum = 0, k = 0;
for (int j = i; j <= n; j += x, k++) sum += a[j];
if (k >= 3) ans = max(ans, sum);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
ans = -inf;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
gao(i);
if (i * i != n) gao(n / i);
}
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include<iostream>
#include<string>
int main(){
int month,day;
const int monthday[12]={31,29,31,30,31,30,31,31,30,31,30,31};
const std::string week[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
const int a=3;
while(1){
std::cin>>month>>day;
if(month==0)break;
int sum=day+a;
for(int i=0;i<month-1;i++)sum+=monthday[i];
std::cout<<week[(sum-1)%7]<<std::endl;
}
return 0;
/*
月曜日 Monday
火曜日 Tuesday
水曜日 Wednesday
木曜日 Thursday
金曜日 Friday
土曜日 Saturday
日曜日 Sunday
*/
} | 0 |
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline long long lin() {
long long x;
scanf("%lld", &x);
return x;
}
inline int mul(int a, int b) {
long long x = a;
x *= (long long)b;
if (x >= 1000000007) x %= 1000000007;
return x;
}
inline int add(int a, int b) {
return (a + b) >= 1000000007 ? a + b - 1000000007 : a + b;
}
inline int sub(int a, int b) {
return (a - b) < 0 ? 1000000007 - b + a : a - b;
}
const int MAX = INT_MAX;
const int MIN = INT_MIN;
char c[100005];
int y[100005];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> c[i];
int d = 0, d1 = 0;
for (int i = 0; i < n; i++) {
y[i] = c[i] - '0';
if (y[i] == 1) d++;
if (y[i] == 0) d1++;
}
int f = 1;
int f2 = 0;
for (int i = 1; i < n; i++) {
if (y[i - 1] != y[i]) f++;
}
for (int i = 0; i < n - 1; i++) {
if (y[i] == y[i + 1]) {
f2 = 1;
break;
}
}
cout << min(f + 2, n);
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
#define rg register
using namespace std;
typedef long long LL;
const int MAXN = 10000007;
const int MOD = 998244353;
template <typename T> inline void read(T &AKNOI) {
T x = 0, flag = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') flag = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
AKNOI = flag * x;
}
namespace ModCalculator {
inline void Inc(int &x, int y) {
x += y; if (x >= MOD) x -= MOD;
}
inline void Dec(int &x, int y) {
x -= y; if (x < 0) x += MOD;
}
inline int Add(int x, int y) {
Inc(x, y); return x;
}
inline int Sub(int x, int y) {
Dec(x, y); return x;
}
inline int Mul(int x, int y) {
return 1LL * x * y % MOD;
}
}
using namespace ModCalculator;
inline int ksm(int x, int k) {
int ret = 1;
while (k) {
if (k & 1) ret = Mul(ret, x);
x = Mul(x, x);
k >>= 1;
}
return ret;
}
int n, a, b;
int fac[MAXN], finv[MAXN];
inline int C(int x, int y) {
return Mul(fac[x], Mul(finv[y], finv[x - y]));
}
inline int H(int x, int y) {
return Sub(C(x + y, y), C(x + y, y - 1));
}
void init() {
read(n); read(a); read(b);
if (b == 0) {
printf("1\n");
exit(0);
}
fac[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = Mul(fac[i - 1], i);
}
finv[n] = ksm(fac[n], MOD - 2);
for (int i = n - 1; i >= 0; --i) {
finv[i] = Mul(finv[i + 1], i + 1);
}
}
void solve() {
int ans = 0;
int kmx = min(min(a, b - 1), n - b);
for (int k = 0; k <= kmx; ++k) {
int h = H(b - 1, k);
if (k + b == n) {
if (a == k) Inc(ans, h);
} else {
int x = n - b - k - 1;
int y = a - k + 1;
Inc(ans, Mul(h, C(x + y - 1, x)));
}
}
printf("%d\n", ans);
}
int main() {
init();
solve();
return 0;
}
| 0 |
#include <cstdio>
#include <algorithm>
#define N 10000
using namespace std;
/** Application main entry point. */
int
main (
int argc,
char * argv[ ]
)
{
int i, j;
for ( ; ; )
{
int d[ N * 2 ];
int n, m;
int res;
scanf ( "%d%d", &n, &m );
if ( !( n | m ) ) break ;
m += n;
for ( i = 0; i < n; ++i )
{
scanf ( "%d", &d[ i ] );
}
for ( i = n; i < m; ++i )
{
scanf ( "%d", &d[ i ] );
}
inplace_merge ( d, d + n, d + m );
res = d[ 0 ];
for ( i = 1; i < m; ++i )
{
res = max( res, d[ i ] - d[ i - 1 ] );
}
printf ( "%d\n", res );
}
return ( 0 );
} | 0 |
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
ll b;
while (cin >> b, b)
{
ll st, gt;
for (ll len = 1; len * len <= 3 * b; len++)
{
if ((2 * b) % len != 0)
continue;
ll tmp = 2 * b / len;
tmp += 1 - len;
if (tmp % 2)
continue;
tmp /= 2;
if (tmp <= 0)
continue;
st = tmp;
gt = len;
}
cout << st << " " << gt << endl;
}
}
| 0 |
#include <bits/stdc++.h>
#define M 1000000007
using namespace std;
long long n, kk, dp[51][51][65], ans[51][51], temp[51][51], res;
int main() {
cin>>n>>kk;
for (int i=0; i<n; i++) {
ans[i][i]=1;
for (int j=0; j<n; j++) cin>>dp[i][j][0];
}
for (int k=1; k<64; k++) {
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
for (int l=0; l<n; l++) {
dp[i][j][k]+=(dp[i][l][k-1]*dp[l][j][k-1])%M;
dp[i][j][k]%=M;
}
}
}
}
for (int k=0; k<64; k++) if (kk&(1ll<<k)) {
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
temp[i][j]=0;
for (int l=0; l<n; l++) {
temp[i][j]+=(ans[i][l]*dp[l][j][k])%M;
temp[i][j]%=M;
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
ans[i][j]=temp[i][j];
}
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
res+=ans[i][j];
res%=M;
}
}
cout<<res<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int LIM_N = 3e5 + 5;
set<int> ans;
int pr[LIM_N];
vector<pair<int, int> > adj[LIM_N];
bool usd[LIM_N];
void dfs(const int pos = 0, const int pv = -1, const int pe = -1) {
usd[pos] = true;
for (const auto nxt : adj[pos]) {
if (!usd[nxt.second]) dfs(nxt.second, pos, nxt.first);
}
if (pr[pos]) {
pr[pv] ^= 1;
ans.insert(pe);
}
}
int main() {
int N, E;
int lastNeg = -1;
int p = 0;
scanf("%d%d", &N, &E);
for (int i = 0; i < N; i++) {
scanf("%d", &pr[i]);
if (pr[i] == -1)
lastNeg = i;
else
p ^= pr[i];
}
for (int i = 0, a, b; i < E; i++) {
scanf("%d%d", &a, &b);
a--, b--;
adj[a].emplace_back(i, b);
adj[b].emplace_back(i, a);
}
if (p && lastNeg == -1) {
printf("-1\n");
return 0;
}
if (p) pr[lastNeg] = 1;
for (int i = 0; i < N; i++)
if (pr[i] == -1) pr[i] = 0;
dfs();
printf("%d\n", (int)ans.size());
for (const int a : ans) printf("%d\n", a + 1);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T MIN_3(T a, T b, T c) {
return min(min(a, b), c);
}
template <class T>
inline T MAX_3(T a, T b, T c) {
return max(max(a, b), c);
}
template <class T>
inline T BIGMOD(T n, T m, T mod) {
long long int ans = 1;
long long int k = n;
while (m) {
if (m & 1) {
ans *= k;
if (ans > mod) ans %= mod;
}
k *= k;
if (k > mod) k %= mod;
m >>= 1;
}
return ans;
}
inline int DBLCMP(double a, double b) {
if (fabs(a - b) <= 1e-11) return 0;
if (a < b) return -1;
return 1;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline int countbit(T n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class T>
inline T euclide(T a, T b, T &x, T &y) {
if (a < 0) {
T d = euclide(-a, b, x, y);
x = -x;
return d;
}
if (b < 0) {
T d = euclide(a, -b, x, y);
y = -y;
return d;
}
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
T d = euclide(b, a % b, x, y);
T t = x;
x = y;
y = t - (a / b) * y;
return d;
}
}
template <class T>
string toString(T n) {
ostringstream ost;
ost << n;
ost.flush();
return ost.str();
}
template <class T>
string toBinary(T n) {
string ret = "";
while (n) {
if (n % 2 == 1)
ret += '1';
else
ret += '0';
n >>= 1;
}
reverse(ret.begin(), ret.end());
return ret;
}
void combination(int n, vector<vector<int> > &ret) {
ret.resize(n + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= n; i++) {
ret[i][0] = ret[i][i] = 1;
for (int j = 1; j < i; j++) {
ret[i][j] = ret[i - 1][j] + ret[i - 1][j - 1];
}
}
}
int toInt(string s) {
int r = 0;
istringstream sin(s);
sin >> r;
return r;
}
long long int toLInt(string s) {
long long int r = 0;
istringstream sin(s);
sin >> r;
return r;
}
double toDouble(string s) {
double r = 0;
istringstream sin(s);
sin >> r;
return r;
}
vector<string> parse(string temp) {
vector<string> ans;
ans.clear();
string s;
istringstream iss(temp);
while (iss >> s) ans.push_back(s);
return ans;
}
template <class T>
inline T gcd(T a, T b) {
if (a < 0) return gcd(-a, b);
if (b < 0) return gcd(a, -b);
return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
inline T lcm(T a, T b) {
if (a < 0) return lcm(-a, b);
if (b < 0) return lcm(a, -b);
return a * (b / gcd(a, b));
}
template <class T>
inline T power(T b, T p) {
if (p < 0) return -1;
if (b <= 0) return -2;
if (!p) return 1;
return b * power(b, p - 1);
}
int main() {
int n, m, mi, mx, a, b, c;
int x, y;
x = 1010, y = -10;
cin >> n >> m >> mi >> mx;
a = b = 0;
for (int i = 0; i < m; i++) {
cin >> c;
x = min(x, c);
y = max(y, c);
}
if (x < mi || y > mx)
puts("Incorrect");
else if (x == mi && y == mx)
puts("Correct");
else if (x == mi && n - m >= 1)
puts("Correct");
else if (y == mx && n - m >= 1)
puts("Correct");
else if (n - m >= 2)
puts("Correct");
else
puts("Incorrect");
}
| 1 |
#include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
using namespace std ;
typedef pair<int,int> P ;
#define F first
#define S second
int N , table[100100] ;
priority_queue<P,vector<P>,greater<P> > minPQ ;
map<int,int> MP ;
int main(){
cin >> N ;
for( int i=0 ; i<N ; i++ ) cin >> table[i] ;
minPQ.push( P(0,0) ) ;
int pos , num ;
while( minPQ.size() ){
num = minPQ.top().F ;
pos = minPQ.top().S ;
minPQ.pop() ;
if( pos == N-1 ) break ;
if( MP[pos] ) continue ;
else MP[pos] = 1 ;
if( table[pos] ){
if( table[pos]>0 ) minPQ.push( P( num , min(N-1,pos+table[pos]) ) ) ;
else minPQ.push( P( num , max( 0 ,pos+table[pos]) ) ) ;
}
else {
for( int i=1 ; i<=6 ; i++ ){
minPQ.push( P( num+1 , min(N-1,pos+i) ) ) ;
}
}
}
cout << num << endl ;
} | 0 |
#include <iostream>
using namespace std;
int main() {
string O, E;
cin >> O >> E;
for (int i = 0; i < O.size(); ++i) {
cout << O[i];
if (i < E.size()) cout << E[i];
}
cout << endl;
return 0;
} | 0 |
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int n, k;
vector<int> w;
bool okrec(int P, int m, int l, int cur) {
if (m == n)
return true;
else if (l == k)
return false;
else if (P - cur >= w[m])
return okrec(P, m + 1, l, cur + w[m]);
else
return okrec(P, m, l + 1, 0);
}
bool ok(int P) {
return okrec(P, 0, 0, 0);
}
int main() {
cin >> n >> k;
w.resize(n);
for (int i = 0; i < n; i++)
cin >> w[i];
int Pl = 1, Pr = accumulate(w.begin(), w.end(), 0) + 1;
while (Pl != Pr) {
int Pm = (Pl+Pr)/2;
if (ok(Pm))
Pr = Pm;
else
Pl = Pm + 1;
}
cout << Pl << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string to_string1(long long inp) {
ostringstream ss;
ss << inp;
return ss.str();
}
bool isLucky(long long inp) {
string str = to_string1(inp);
int numLucky = 0;
for (int i = 0; i < str.length(); i++) {
if (str[i] == '4' || str[i] == '7') {
numLucky++;
}
}
return (numLucky == str.length());
}
long long numLuckyDigits(long long inp) {
string str = to_string1(inp);
long long numLuckyDigits = 0;
for (int i = 0; i < str.length(); i++) {
if (str[i] == '4' || str[i] == '7') {
numLuckyDigits++;
}
}
return numLuckyDigits;
}
int main() {
long long num;
long long numLucky;
cin >> num;
if (isLucky(numLuckyDigits(num))) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
| 1 |
#include<iostream>
using namespace std;
int main(){
int s1,s2,s3;
cin>>s1>>s2;
cout<<6-(s1+s2)<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 5;
const long long MAX = 1e16;
long long D[MAXN], num[MAXN][2];
int dp[MAXN][5];
int calc(long long a, long long b, int n) {
if (a > b) swap(a, b);
if (a == b) return 0;
if (n == 1 || n == 2) return 1;
int type = -1;
int ans = 0;
if (a == 1 && b == D[n])
type = 0;
else if (a == 1 && b == num[n][0])
type = 1;
else if (a == 1 && b == num[n][1])
type = 2;
else if (b == D[n] && a == num[n][0])
type = 3;
else if (b == D[n] && a == num[n][1])
type = 4;
if (type != -1 && dp[n][type] != -1) return dp[n][type];
if (a <= D[n - 1] && b >= D[n - 1] + 1)
ans = min(calc(1, a, n - 1), calc(a, D[n - 1], n - 1)) +
calc(1, b - D[n - 1], n - 2) + 1;
else if (a >= D[n - 1] + 1 && b >= D[n - 1] + 1)
ans = calc(a - D[n - 1], b - D[n - 1], n - 2);
else {
int tmp1 = calc(a, b, n - 1);
int tmp2 = min(calc(1, a, n - 1), calc(a, D[n - 1], n - 1));
int tmp3 = min(calc(1, b, n - 1), calc(b, D[n - 1], n - 1));
ans = min(tmp1, tmp2 + tmp3 + 2);
}
if (type != -1) dp[n][type] = ans;
return ans;
}
int main() {
int t, n;
scanf("%d%d", &t, &n);
D[0] = 1, D[1] = 2;
int pos = 2;
while (true) {
D[pos] = D[pos - 1] + D[pos - 2];
if (D[pos] > MAX) break;
pos++;
}
n = min(n, pos);
for (int i = 0; i < t; i++) {
long long a, b;
scanf("%I64d%I64d", &a, &b);
if (a > b) swap(a, b);
long long q1 = a, q2 = b;
for (int j = n; j >= 0; j--) {
if (q1 > D[j]) q1 -= D[j];
if (q2 > D[j]) q2 -= D[j];
num[j][0] = q1;
num[j][1] = q2;
dp[j][0] = dp[j][1] = dp[j][2] = dp[j][3] = dp[j][4] = -1;
}
printf("%d\n", calc(a, b, n));
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = INT_MAX - 1;
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
bool arr1[n];
bool arr2[n];
for (int i = 0; i < n; i++) {
char c;
cin >> c;
if (c == '1') {
arr1[i] = true;
} else {
arr1[i] = false;
}
}
for (int i = 0; i < n; i++) {
char c;
cin >> c;
if (c == '1') {
arr2[i] = true;
} else {
arr2[i] = false;
}
}
vector<int> ans;
int cur = 0;
int compare = n - 1;
for (int i = 0; i < n; i++) {
bool item = 0;
if (i % 2 == 0) {
item = arr1[cur];
} else {
item = arr1[n - cur - 1];
}
if (i % 2 == 0) {
if (item == arr2[compare]) {
ans.push_back(1);
}
ans.push_back(compare + 1);
} else {
if (item != arr2[compare]) {
ans.push_back(1);
}
ans.push_back(compare + 1);
}
compare--;
if (i % 2 == 1) {
cur++;
}
}
cout << ans.size() << " ";
for (int i : ans) {
cout << i << " ";
}
cout << "\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long const MOD = 998244353;
void yes() { cout << "YES" << '\n'; }
void no() { cout << "NO" << '\n'; }
long long fact(int n) {
long long ans = 1;
for (int i = 2; i <= n; i++) {
ans = (ans * i) % MOD;
}
return ans;
}
long long powM(int n, int x) {
if (x == 0) {
return 1;
}
if (x == 1) {
return n % MOD;
}
if (x % 2 == 0) {
long long a = powM(n, x / 2);
return (a * a) % MOD;
} else {
return (n * powM(n, x - 1)) % MOD;
}
}
long long C(int n, int k) {
return (fact(n) * powM((fact(k) * fact(n - k)) % MOD, MOD - 2)) % MOD;
}
long long A(int n, int k) {
return (fact(n) * powM(fact(n - k) % MOD, MOD - 2)) % MOD;
}
void add_self(int &a, int b) {
a += b;
if (a >= MOD) {
a -= MOD;
}
}
void sub_self(int &a, int b) {
a -= b;
if (a < 0) {
a += MOD;
}
}
struct segtree {
vector<long long> tree;
int size;
void init(int n) {
size = 1;
while (size < n) size *= 2;
tree.assign(2 * size - 1, 0);
}
void build(vector<int> &a, int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx < a.size()) tree[x] = a[lx];
} else {
int m = (lx + rx) / 2;
build(a, 2 * x + 1, lx, m);
build(a, 2 * x + 2, m, rx);
tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
}
}
void build(vector<int> &a) {
init(a.size());
build(a, 0, 0, size);
}
void set(int i, int v, int x, int lx, int rx) {
if (rx - lx == 1) {
tree[x] = v;
return;
}
int m = (lx + rx) / 2;
if (i < m) {
set(i, v, 2 * x + 1, lx, m);
} else {
set(i, v, 2 * x + 2, m, rx);
}
tree[x] = tree[2 * x + 1] + tree[2 * x + 2];
}
void set(int i, int v) { set(i, v, 0, 0, size); }
long long sum(int l, int r, int x, int lx, int rx) {
if (l >= rx || lx >= r) {
return 0;
}
if (lx >= l && rx <= r) {
return tree[x];
}
int m = (rx + lx) / 2;
long long s1 = sum(l, r, 2 * x + 1, lx, m);
long long s2 = sum(l, r, 2 * x + 2, m, rx);
return s1 + s2;
}
long long sum(int l, int r) { return sum(l, r, 0, 0, size); }
};
void solve() {
vector<pair<long long, int>> a(3);
cin >> a[0].first >> a[1].first >> a[2].first;
a[0].second = 1;
a[1].second = 2;
a[2].second = 3;
sort((a).begin(), (a).end());
long long y = 2 * a[2].first - a[0].first - a[1].first;
cout << "First" << endl;
fflush(stdout);
cout << y << endl;
fflush(stdout);
int x;
cin >> x;
for (int i = 0; i < 3; i++) {
if (a[i].second == x) {
if (i == 0) {
y = a[2].first - a[1].first;
cout << y << endl;
fflush(stdout);
cin >> x;
return;
}
if (i == 1) {
y = a[2].first - a[0].first;
cout << y << endl;
fflush(stdout);
cin >> x;
return;
}
if (i == 2) {
a[2].first += y;
y = 2 * a[2].first - a[0].first - a[1].first;
cout << y << endl;
fflush(stdout);
cin >> x;
if (x == a[0].second) {
y = a[2].first - a[1].first;
cout << y << endl;
fflush(stdout);
cin >> x;
return;
} else {
y = a[2].first - a[0].first;
cout << y << endl;
fflush(stdout);
cin >> x;
return;
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
int n;
long long K;
int size[100001], head[100001], nxt[200001], b[200001], k, mx[100001], root;
int d[100001], dfn[100001], dep[100001], now, Mx[100001], ord[100001];
int se[1000001];
int max(int a, int b) { return dep[a] > dep[b] ? a : b; }
void build(int root, int l, int r) {
if (l == r) {
se[root] = ord[l];
return;
}
build(root << 1, l, (l + r) >> 1);
build(root << 1 | 1, ((l + r) >> 1) + 1, r);
se[root] = max(se[root << 1], se[root << 1 | 1]);
}
int query(int root, int l, int r, int el, int er) {
if (el > r || er < l) return 0;
if (el <= l && er >= r) return se[root];
return max(query(root << 1, l, (l + r) >> 1, el, er),
query(root << 1 | 1, ((l + r) >> 1) + 1, r, el, er));
}
void update(int root, int l, int r, int e) {
if (l == r) {
se[root] = 0;
return;
}
if ((l + r) >> 1 >= e)
update(root << 1, l, (l + r) >> 1, e);
else
update(root << 1 | 1, ((l + r) >> 1) + 1, r, e);
se[root] = max(se[root << 1], se[root << 1 | 1]);
}
void push(int s, int t) {
nxt[++k] = head[s];
head[s] = k;
b[k] = t;
}
void getroot(int x, int f) {
mx[x] = 0, size[x] = 1;
for (int i = head[x]; i; i = nxt[i])
if (b[i] != f) {
getroot(b[i], x);
size[x] += size[b[i]];
mx[x] = std::max(mx[x], size[b[i]]);
}
mx[x] = std::max(mx[x], n - size[x]);
if (mx[x] < mx[root]) root = x;
}
void dfs(int x, int f) {
size[x] = 1;
dfn[x] = ++now;
ord[now] = x;
dep[x] = dep[f] + 1;
for (int i = head[x]; i; i = nxt[i])
if (b[i] != f) {
dfs(b[i], x);
size[x] += size[b[i]];
}
Mx[x] = now;
}
std::priority_queue<std::pair<int, int> > heap;
std::vector<int> nleaf[100001], vec[100001];
bool vis[100001];
int con[100001][2], top;
void add(int u, int v) {
++top;
con[top][0] = u;
con[top][1] = v;
}
void del(int x) {
vis[x] = 1;
for (int i = head[x]; i; i = nxt[i]) --d[b[i]];
d[x] = -1;
update(1, 1, n, dfn[x]);
}
bool cmp(int a, int b) { return dep[a] < dep[b]; }
int main() {
scanf("%d%lld", &n, &K);
for (int i = 1, u, v; i < n; i++)
scanf("%d%d", &u, &v), push(u, v), push(v, u), ++d[u], ++d[v];
mx[0] = n;
getroot(1, 0);
dep[0] = -1;
dfs(root, 0);
long long sum = 0;
for (int i = 1; i <= n; i++) sum += dep[i];
for (int i = head[root]; i; i = nxt[i]) {
heap.push(std::make_pair(size[b[i]], b[i]));
for (int j = dfn[b[i]]; j <= Mx[b[i]]; j++) nleaf[b[i]].push_back(ord[j]);
vec[b[i]] = nleaf[b[i]];
std::sort(nleaf[b[i]].begin(), nleaf[b[i]].end(), cmp);
}
build(1, 1, n);
for (; sum > K;) {
int now = heap.top().second, s = heap.top().first;
heap.pop();
while (!nleaf[now].empty() && (d[nleaf[now].back()] <= 1 ||
2ll * dep[nleaf[now].back()] + K > sum)) {
nleaf[now].pop_back();
}
if (nleaf[now].empty()) break;
int x = nleaf[now].back();
sum -= 2ll * dep[x];
int p1 = 0, p2 = 0;
while (head[x] && vis[b[head[x]]]) head[x] = nxt[head[x]];
for (int i = head[x]; i; i = nxt[i]) {
if (dep[b[i]] >= dep[x]) (p1 ? p2 : p1) = b[i];
if (p2) break;
while (nxt[i] && vis[b[nxt[i]]]) nxt[i] = nxt[nxt[i]];
}
if (p2) {
int u = query(1, 1, n, dfn[p2], Mx[p2]),
v = query(1, 1, n, dfn[p1], Mx[p1]);
if (!u) {
for (int i = dfn[p2]; i <= Mx[p2]; i++) printf("%d ", vis[ord[i]]);
putchar('\n');
return 0;
}
del(u), del(v);
add(u, v);
} else {
int u = query(1, 1, n, dfn[p1], Mx[p1]);
del(u), del(x);
add(u, x);
}
if (s - 2) heap.push(std::make_pair(s - 2, now));
}
if (sum != K) {
puts("NO");
return 0;
}
while (!heap.empty()) heap.pop();
for (int i = 1; i <= n; i++) {
nleaf[i] = vec[i];
vec[i].clear();
for (int j = 0; j < (int)nleaf[i].size(); j++)
if (!vis[nleaf[i][j]]) vec[i].push_back(nleaf[i][j]);
if (vec[i].size()) heap.push(std::make_pair(vec[i].size(), i));
}
vec[root].push_back(root);
heap.push(std::make_pair(1, root));
puts("YES");
for (int i = 1; i <= top; i++) printf("%d %d\n", con[i][0], con[i][1]);
while (!heap.empty()) {
auto now1 = heap.top().second;
heap.pop();
auto now2 = heap.top().second;
heap.pop();
printf("%d %d\n", vec[now1].back(), vec[now2].back());
vec[now1].pop_back(), vec[now2].pop_back();
if (vec[now1].size()) heap.push(std::make_pair(vec[now1].size(), now1));
if (vec[now2].size()) heap.push(std::make_pair(vec[now2].size(), now2));
}
}
| 5 |
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <utility>
#include <cstdlib>
#include <queue>
#include <iomanip>
#include <cstdio>
using namespace std;
int vis[50][100010];
int n;
void dfs(int rem, int dist){
if(dist > 45) return;
if(vis[dist][rem]) return;
vis[dist][rem] = 1;
for(int i = 0; i < 10; i++)
dfs((rem*10+i)%n, dist+i);
}
int main(){
cin >> n;
for(int i = 1; i < 10; i++)
dfs(i, i);
for(int i = 1; i <= 45; i++)
if(vis[i][0]){
cout << i << endl;
return 0;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, m, x, y;
long long k;
scanf("%d%d%I64d%d%d", &n, &m, &k, &x, &y);
int once = n == 1 ? m : (n + n - 2) * m;
long long cnt_full = k / once;
k -= cnt_full * once;
long long ans[100][100];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i][j] = cnt_full * 2;
}
}
for (int j = 0; j < m; ++j) {
ans[0][j] = cnt_full;
ans[n - 1][j] = cnt_full;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (--k >= 0)
++ans[i][j];
else
break;
}
}
for (int i = n - 2; i > 0; --i) {
for (int j = 0; j < m; ++j) {
if (--k >= 0)
++ans[i][j];
else
break;
}
}
long long mn = ans[0][0], mx = ans[0][0];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
mn = min(mn, ans[i][j]);
mx = max(mx, ans[i][j]);
}
}
printf("%I64d %I64d %I64d\n", mx, mn, ans[x - 1][y - 1]);
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
std::vector<char> str(n);
int cnt[2] = {0, 0};
for (int i = 0; i < n; i++) {
std::cin >> str[i];
str[i] = str[i] == 'B' ? 0 : 1;
cnt[str[i]]++;
}
if (cnt[0] % 2 && cnt[1] % 2) {
printf("-1\n");
return 0;
}
bool need = (cnt[0] % 2) ? 0 : 1;
std::vector<int> ans;
for (int i = 0; i < n; i++) {
if (str[i] != need) {
str[i] = !str[i];
str[i + 1] = !str[i + 1];
ans.push_back(i);
}
}
printf("%d\n", (int)ans.size());
for (auto x : ans) {
printf("%d ", x + 1);
}
puts("");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1E-6;
const int LEN = 110;
long long n, m, k, x, y;
long long num[LEN][LEN];
int main() {
while (cin >> n >> m >> k >> x >> y) {
memset(num, 0, sizeof num);
if (n == 2) {
m *= 2;
n = 1;
if (x == 2) y += m / 2;
x = 1;
}
if (n == 1) {
cout << k / m + (k % m ? 1 : 0) << ' ' << k / m << ' ';
if (k % m >= y)
cout << k / m + 1 << endl;
else
cout << k / m << endl;
continue;
}
long long sz = n * m * 2 - 2 * m;
long long tc = k / sz;
long long lc = k % sz;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (i == 0 || i == n - 1)
num[i][j] = tc;
else
num[i][j] = 2 * tc;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (lc-- > 0)
num[i][j]++;
else
break;
}
for (int i = n - 2; i > 0; i--)
for (int j = 0; j < m; j++) {
if (lc-- > 0)
num[i][j]++;
else
break;
}
long long Max = -INF, Min = INF;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Max = max(Max, num[i][j]);
Min = min(Min, num[i][j]);
}
}
cout << Max << ' ' << Min << ' ' << num[x - 1][y - 1] << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int cnta[100005][2], cntb[100005][2];
int q[100005][2];
char s[100005];
int dp[100005][2];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) {
if (s[i] == 'a')
cnta[i][i % 2]++;
else if (s[i] == 'b')
cntb[i][i % 2]++;
else
q[i][i % 2]++;
cnta[i][0] += cnta[i - 1][0];
cnta[i][1] += cnta[i - 1][1];
cntb[i][0] += cntb[i - 1][0];
cntb[i][1] += cntb[i - 1][1];
q[i][0] += q[i - 1][0];
q[i][1] += q[i - 1][1];
}
int m;
scanf("%d", &m);
int ca = (m + 1) / 2;
int cb = m / 2;
int last = m % 2;
for (int i = m; i <= n; i++) {
if (last == 1) {
int a = i % 2;
int b = 1 - a;
if (cnta[i][a] - cnta[i - m][a] + q[i][a] - q[i - m][a] == ca &&
cntb[i][b] - cntb[i - m][b] + q[i][b] - q[i - m][b] == cb) {
int cq = q[i][a] - q[i - m][a] + q[i][b] - q[i - m][b];
dp[i][0] = 1 + dp[i - m][0];
dp[i][1] = cq + dp[i - m][1];
}
} else {
int b = i % 2;
int a = 1 - b;
if (cnta[i][a] - cnta[i - m][a] + q[i][a] - q[i - m][a] == ca &&
cntb[i][b] - cntb[i - m][b] + q[i][b] - q[i - m][b] == cb) {
int cq = q[i][a] - q[i - m][a] + q[i][b] - q[i - m][b];
dp[i][0] = 1 + dp[i - m][0];
dp[i][1] = cq + dp[i - m][1];
}
}
if (dp[i][0] < dp[i - 1][0] ||
(dp[i][0] == dp[i - 1][0] && dp[i][1] > dp[i - 1][1])) {
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][1];
}
}
printf("%d\n", dp[n][1]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\n"; }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
for (int t = (0), t_end_ = (T); t < t_end_; t++) {
int n, k;
cin >> n >> k;
vector<vector<int>> S(k);
vector<bool> used(n);
for (int i = (0), i_end_ = (k); i < i_end_; i++) {
int c;
cin >> c;
for (int j = (0), j_end_ = (c); j < j_end_; j++) {
int x;
cin >> x;
S[i].push_back(x);
x--;
used[x] = true;
}
}
vector<int> subset;
for (int i = (0), i_end_ = (n); i < i_end_; i++)
if (used[i]) subset.push_back(i + 1);
cout << "? " << subset.size() << " ";
for (int i = (0), i_end_ = (subset.size()); i < i_end_; i++)
cout << subset[i] << (i != subset.size() - 1 ? " " : "");
cout << "\n";
cout.flush();
int subsetmax;
cin >> subsetmax;
int l = 0;
int r = k;
while (r - l > 1) {
int m = (l + r) / 2;
subset.clear();
for (int i = (l), i_end_ = (m); i < i_end_; i++) {
for (int j = (0), j_end_ = (S[i].size()); j < j_end_; j++)
subset.push_back(S[i][j]);
}
cout << "? " << subset.size() << " ";
for (int i = (0), i_end_ = (subset.size()); i < i_end_; i++)
cout << subset[i] << (i != subset.size() - 1 ? " " : "");
cout << "\n";
cout.flush();
int ret;
cin >> ret;
if (ret == subsetmax) {
r = m;
} else {
l = m;
}
}
subset.clear();
for (int i = (0), i_end_ = (k); i < i_end_; i++) {
if (i == l) continue;
for (int j = (0), j_end_ = (S[i].size()); j < j_end_; j++)
subset.push_back(S[i][j]);
}
for (int i = (0), i_end_ = (n); i < i_end_; i++)
if (!used[i]) subset.push_back(i + 1);
cout << "? " << subset.size() << " ";
for (int i = (0), i_end_ = (subset.size()); i < i_end_; i++)
cout << subset[i] << (i != subset.size() - 1 ? " " : "");
cout << "\n";
cout.flush();
int ret;
cin >> ret;
if (ret > subsetmax) subsetmax = ret;
cout << "! ";
for (int i = (0), i_end_ = (k); i < i_end_; i++) {
if (i == l) {
cout << ret;
} else {
cout << subsetmax;
}
cout << (i != k - 1 ? " " : "");
}
cout << "\n";
cout.flush();
string s;
cin >> s;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int countKangroo(vector<int> &arr) {
int res = arr.size();
int i = 0, j = (arr.size() + 1) / 2;
sort(arr.rbegin(), arr.rend());
while (j < arr.size()) {
if (arr[i] < arr[j] * 2) {
++j;
} else {
--res;
++i;
++j;
}
}
return res;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
cout << countKangroo(arr) << endl;
}
| 1 |
#include<cstdio>
using namespace std;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
if (n%500<=m) printf("Yes");
else printf("No");
} | 0 |
#include <iostream>
using namespace std;
int main()
{
string s;cin>>s;
int ans =700;
for(auto c :s){
if(c=='o')ans+=100;
}
cout<<ans<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q, l1, r1, l2, r2;
cin >> q;
while (q--) {
cin >> l1 >> r1 >> l2 >> r2;
if (r2 != l1) {
cout << l1 << ' ' << r2 << '\n';
} else {
if (r1 != l2) {
cout << r1 << ' ' << l2 << '\n';
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int n;
long long h;
cin >> n >> h;
priority_queue<pair<long long, bool>> q;
for (int i = 0; i < n; i++) {
long long a, b;
cin >> a >> b;
q.push(make_pair(a, false));
q.push(make_pair(b, true));
}
long long count = 0;
while (h > 0) {
auto t = q.top();
if (t.second == true) {
q.pop();
}
h -= t.first;
count++;
}
cout << count << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int oo = (int)1e9;
const int mod = (int)1e9 + 7;
const int maxn = 1e3 + 5;
int dr[] = {0, 0, 1, -1};
int dc[] = {1, -1, 0, 0};
void myMain() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int &i : a) cin >> i;
sort(a.begin(), a.end());
vector<long long> answer;
long long cur = 0;
for (int i = 0; i < n; i++) {
cur += a[i];
answer.push_back(cur);
if (i >= m) answer.back() += answer[i - m];
}
for (auto i : answer) cout << i << ' ';
}
int32_t main() {
ios_base::sync_with_stdio(0);
int testcases = 1;
while (testcases--) {
myMain();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long N, x, y;
long long vals[111111];
int main() {
cin >> N >> x >> y;
long long tot = 0, totot = 0;
for (int i = 0; i < N - 1; i++) {
tot++;
totot++;
}
if (tot >= y)
cout << -1 << endl;
else {
int temp = y - tot;
totot += temp * (long long)temp;
if (totot >= x) {
for (int i = 0; i < N - 1; i++) cout << 1 << endl;
cout << temp << endl;
} else
cout << -1 << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double PI =
3.141592653589793238462643383279502884197169399375105820974944592307816406286;
int n, a[400004], b[400004];
bool check() {
int x = a[0];
int i = 0;
while (b[i] != x) i++;
for (int j = i; j < n + i - 1; j++)
if (a[j - i] != b[j]) return false;
return true;
}
int main() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> a[i];
a[n + i - 1] = a[i];
if (a[i] == 0) i--;
}
for (int i = 0; i < n - 1; i++) {
cin >> b[i];
b[n + i - 1] = b[i];
if (b[i] == 0) i--;
}
if (check())
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int check_prime(long long int num) {
int flag = 1;
if (num % 2 == 0 && num != 2)
flag = 0;
else {
for (long long int i = 3; i <= sqrt(num); i += 2) {
if (num % i == 0) {
flag = 0;
break;
}
}
}
return flag;
}
int main() {
long long int t = 1;
ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int sum = 0;
vector<long long int> a(n);
map<long long int, long long int> mp;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
mp[a[i]] = i + 1;
}
if (!check_prime(sum)) {
cout << n << "\n";
for (long long int i = 0; i < n; i++) cout << mp[a[i]] << " ";
cout << "\n";
continue;
}
for (long long int i = 0; i < n; i++) {
if (a[i] & 1) {
remove(a.begin(), a.end(), a[i]);
break;
}
}
cout << n - 1 << "\n";
for (long long int i = 0; i < n - 1; i++) cout << mp[a[i]] << " ";
cout << "\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int a[n + 1];
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
if (n % 2 == 0) {
for (int i = 1; i <= n; i += 2) {
printf("%d %d ", -1 * a[i + 1], a[i]);
}
printf("\n");
} else {
if (a[2] + a[3] != 0) {
printf("%d %d %d ", a[2] + a[3], -1 * a[1], -1 * a[1]);
} else {
printf("%d %d %d ", a[2] - a[3], -1 * a[1], a[1]);
}
for (int i = 4; i <= n; i += 2) {
printf("%d %d ", -1 * a[i + 1], a[i]);
}
printf("\n");
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
//using namespace std;
typedef long long ll;
typedef long double ld;
//std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
//int randi = std::uniform_int_distribution<int>(0, 999)(rng);
const double EPS = 1e-9;
const double PI = acos(-1.0);
const ll OO = 2e18 + 10;
const int oo = 1e9 + 10;
const int MOD = 1e9 + 7;
struct DSU {
std::vector<int> parent, size;
void build(int n) {
parent.resize(n);
size.resize(n);
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
int find(int v) {
if (parent[v] == v)
return v;
return parent[v] = find(parent[v]);
}
bool unite(int v, int u) {
v = find(v);
u = find(u);
if (v == u)
return false;
if (size[u] > size[v])
std::swap(v, u);
parent[u] = v;
size[v] += size[u];
return true;
}
};
void solve() {
int n, m;
std::cin >> n >> m;
DSU dsu;
dsu.build(m + 1);
std::set<int> s;
for (int i = 0; i < n; i++) {
int k;
std::cin >> k;
if (k == 1) {
int x;
std::cin >> x;
if (dsu.find(x) != dsu.find(0)) {
dsu.unite(x, 0);
s.insert(i);
}
} else {
int x1, x2;
std::cin >> x1 >> x2;
if (dsu.find(x1) != dsu.find(x2)) {
dsu.unite(x1, x2);
s.insert(i);
}
}
}
ll ans = 1;
for (int i = 0; i < s.size(); i++) {
ans = (ans * 2) % MOD;
}
std::cout << ans << " " << s.size() << "\n";
for (auto &i : s) {
std::cout << (i + 1) << " ";
}
std::cout << "\n";
}
int main() {
std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);
// std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
int tests = 1;
// std::cin >> tests;
while (tests--) {
solve();
}
// std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
// std::cout << "time in micros: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[600005];
set<long long> se;
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", s);
int l = strlen(s);
long long sum = 0, mul = 1;
for (int j = 0; j < l; j++) {
sum += mul * (s[j] - 'a' + 1);
mul = (mul * 60887LL) % 13151715131LL;
}
mul = 1;
for (int j = 0; j < l; j++) {
long long tmp = sum - mul * (s[j] - 'a' + 1);
if (s[j] != 'a') se.insert(tmp + mul);
if (s[j] != 'b') se.insert(tmp + mul * 2);
if (s[j] != 'c') se.insert(tmp + mul * 3);
mul = (mul * 60887LL) % 13151715131LL;
}
}
for (int i = 0; i < m; i++) {
scanf("%s", s);
int l = strlen(s);
long long sum = 0, mul = 1;
for (int j = 0; j < l; j++) {
sum += mul * (s[j] - 'a' + 1);
mul = (mul * 60887LL) % 13151715131LL;
}
printf(se.count(sum) ? "YES\n" : "NO\n");
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(ll i=0;i<n;i++)
const ll INF=1e15;
const int inf=1e9;
const ll MOD=1e9+7;
int par[100000],depth[100000];
vector<queue<int>> a(100000);
int count;
int findpar(int i){
if(par[i]==-1) return ::count;
else{
::count++;
findpar(par[i]);
}
return ::count;
}
int main(){
int N;cin>>N;
rep(i,N)par[i]=-1;
rep(i,N){
int id,k;cin>>id>>k;
rep(j,k){
int c;cin>>c;
a[id].emplace(c);
par[c]=id;
}
}
rep(i,N){
::count=0;
depth[i]=findpar(i);
}
rep(i,N){
if(par[i]==i)par[i]=-1;
cout<<"node "<<i<<": parent = "<<par[i]<<", depth = "<<depth[i]<<", ";
if(par[i]==-1)cout<<"root";
else if(a[i].size())cout<<"internal node";
else cout<<"leaf";
cout<<", [";
while(a[i].size()){
cout<<a[i].front();
if(a[i].size()!=1)cout<<", ";
a[i].pop();
}
cout<<"]"<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {}
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
}
int mod = 1e9 + 7;
long long fpow(long long x, long long p) {
if (!p) return 1;
long long sq = fpow(x, p / 2);
sq *= sq;
sq %= mod;
if (p & 1) sq *= x;
return sq % mod;
}
const int N = 2e3 + 9;
int mem[N];
vector<int> adjL[N];
int n, m, k;
int solve(int idx) {
if (idx >= n) return 1;
if (mem[idx] == 2) return 0;
if (mem[idx]) return 1;
mem[idx] = 1;
int ch = 1;
for (auto& it : adjL[idx]) ch = min(ch, solve(it));
return ch;
}
int main() {
file();
fast();
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
if (i + k - 1 >= n) break;
for (int j = 0; j < k / 2; j++)
adjL[j + i].push_back(k - j - 1 + i),
adjL[k - j - 1 + i].push_back(j + i);
}
long long ans = 1;
for (int i = 0; i < n; i++) {
ans *= max(1, solve(i) * m);
ans %= mod;
for (int j = 0; j < n; j++) mem[j] = (mem[j] ? 2 : 0);
}
cout << ans << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:32505856")
template <class T>
inline void gmin(T &a, T b) {
if (a > b) a = b;
}
template <class T>
inline void gmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
inline T sqr(T a) {
return a * a;
}
inline int sign(const double &a) { return a > 1e-9 ? 1 : (a < -1e-9 ? -1 : 0); }
struct Initializer {
Initializer() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
~Initializer() {}
} initializer;
int main() {
int n, k;
scanf("%d%d", &n, &k);
if (n * (n - 1) <= 2 * k) return puts("no solution"), 0;
int second = -1000000000;
for (int i = n; i >= 1; i--) printf("%d %d\n", 0, (second += i));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int Read() {
char c;
while (c = getchar(), (c != '-') && (c < '0' || c > '9'))
;
bool neg = (c == '-');
int ret = (neg ? 0 : c - 48);
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + c - 48;
return neg ? -ret : ret;
}
const int MAXN = 200005, MOD = 7 + 1e9;
vector<int> e[MAXN];
int size[MAXN], dep[MAXN], suc[MAXN], f[MAXN], fa[MAXN];
int N, cnt, st[MAXN], nxt[MAXN << 1], lnk[MAXN << 1], deg[MAXN];
void init() {
scanf("%d", &N), N += N;
for (int i = 2; i <= N; i++) {
int u = Read(), v = Read();
++deg[u], ++deg[v];
lnk[++cnt] = v, nxt[cnt] = st[u], st[u] = cnt;
lnk[++cnt] = u, nxt[cnt] = st[v], st[v] = cnt;
}
}
void DFS(int x) {
size[x] = 1;
for (int i = st[x], y = lnk[i]; i; i = nxt[i], y = lnk[i])
if (y != fa[x]) {
fa[y] = x, dep[y] = dep[x] + 1, DFS(y);
e[x].push_back(y), size[x] += size[y], suc[x] = suc[y];
}
e[x].push_back(0), e[x].push_back(0);
if (deg[x] != 2) suc[x] = x;
}
int Duo(int, int);
int Solo(int x) {
if (!x) return 1;
if (size[x] & 1) return 0;
if (f[x] >= 0) return f[x];
if (deg[suc[x]] == 1) return size[x] / 2;
int w = suc[x], h = dep[w] - dep[x];
f[x] = 0;
for (int i = 0; i <= 1; i++) {
int y = e[w][i];
if (size[y] == h - 1 && deg[suc[y]] <= 1)
(f[x] += Solo(e[w][i ^ 1])) %= MOD;
else
for (int j = 0; j <= (e[y][0] > 0); j++) {
int u = e[y][j], v = e[y][j ^ 1];
if (size[u] == h && deg[suc[u]] <= 1)
(f[x] += Duo(v, e[w][i ^ 1])) %= MOD;
}
}
if (deg[x] == 2 && deg[e[x][0]] <= 2) (f[x] += Solo(e[e[x][0]][0])) %= MOD;
return f[x];
}
int Duo(int x, int y) {
if ((size[x] + size[y]) & 1) return 0;
if (!x) return Solo(y);
if (!y) return Solo(x);
if (deg[x] > 2 || deg[y] > 2) return 0;
return Duo(e[x][0], e[y][0]);
}
int Trio(int s1, int s2, int s3) {
if (deg[s1] == 1) return (long long)Solo(s2) * Solo(s3) % MOD;
int ret = 0;
for (int i = 0; i <= 1; i++)
(ret += (long long)Duo(s2, e[s1][i]) * Duo(s3, e[s1][i ^ 1]) % MOD) %= MOD;
return ret;
}
int work() {
memset(f, -1, sizeof(f));
if (N == 2) return 2;
for (int i = 1; i <= N; i++)
if (deg[i] > 3) return 0;
for (int i = 1; i <= N; i++)
if (deg[i] == 3) {
DFS(i);
int ans = 0, s1 = lnk[st[i]], s2 = lnk[nxt[st[i]]],
s3 = lnk[nxt[nxt[st[i]]]];
(ans += Trio(s1, s2, s3)) %= MOD;
(ans += Trio(s1, s3, s2)) %= MOD;
(ans += Trio(s2, s1, s3)) %= MOD;
(ans += Trio(s2, s3, s1)) %= MOD;
(ans += Trio(s3, s1, s2)) %= MOD;
(ans += Trio(s3, s2, s1)) %= MOD;
return (ans + ans) % MOD;
}
return N /= 2, 2 * ((long long)N * N - N + 2) % MOD;
}
int main() {
init();
printf("%d\n", work());
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int INF = 1e9 + 7;
const int mod = 998244353;
int n, a, b;
long long sum, res;
struct node {
long long hp, dam;
bool operator<(const node &p) const {
if ((hp - dam) == (p.hp - p.dam)) return p.dam < dam;
return (hp - dam) > (p.hp - p.dam);
}
} d[maxn], g[maxn];
int main() {
sum = 0;
scanf("%d%d%d", &n, &a, &b);
for (int i = 1; i <= n; i++)
scanf("%lld%lld", &d[i].hp, &d[i].dam), res += d[i].dam;
if (b == 0)
printf("%lld\n", res);
else {
sort(d + 1, d + 1 + n);
b = min(b, n);
for (int i = 1; i <= b; i++)
if (d[i].hp > d[i].dam) res += (d[i].hp - d[i].dam);
long long ans = res;
for (int i = 1; i <= n; i++) {
long long gs = ans;
if (i <= b) {
gs += max(d[i].hp << a, d[i].dam) - d[i].dam;
gs -= max(0ll, d[i].hp - d[i].dam);
} else {
gs += max(d[i].hp << a, d[i].dam) - d[i].dam;
gs -= max(0ll, d[b].hp - d[b].dam);
}
res = max(res, gs);
}
printf("%lld\n", res);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 210;
const int INF = 0x3f3f3f3f;
int n, a[MAXN];
void run() {
cin >> n;
int ans;
if (n <= 2)
ans = 1;
else
ans = (n - 1) / 2 + 1;
cout << (ans) << '\n';
}
int main() {
ios::sync_with_stdio(false);
int T;
for (cin >> T; T--;) run();
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class X>
void input(vector<X>& a, int N) {
X temp;
for (long long int i = 0; i < N; i++) {
cin >> temp;
a.push_back(temp);
}
}
void solve() {
int team1[2][2];
int team2[2][2];
for (long long int i = 0; i < 4; i++) {
cin >> team1[i / 2][i % 2];
}
for (long long int i = 0; i < 4; i++) {
cin >> team2[i / 2][i % 2];
}
int attack1 = team1[0][1], defense1 = team1[1][0];
if (attack1 > max(team2[0][0], team2[1][0]) &&
defense1 > max(team2[0][1], team2[1][1])) {
cout << "Team 1";
return;
}
attack1 = team1[1][1], defense1 = team1[0][0];
if (attack1 > max(team2[0][0], team2[1][0]) &&
defense1 > max(team2[0][1], team2[1][1])) {
cout << "Team 1";
return;
}
if (!((attack1 < team2[0][0] && defense1 < team2[1][1]) ||
(attack1 < team2[1][0] && defense1 < team2[0][1]))) {
cout << "Draw";
return;
}
attack1 = team1[0][1], defense1 = team1[1][0];
if (!((attack1 < team2[0][0] && defense1 < team2[1][1]) ||
(attack1 < team2[1][0] && defense1 < team2[0][1]))) {
cout << "Draw";
return;
}
cout << "Team 2";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include<iostream>
using namespace std;
int main()
{
char a,b;cin>>a>>b;
if(a>b)cout<<">";
else if(a<b)cout<<"<";
else cout<<"=";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 11;
const long long MOD = 1e9 + 7;
long long fac[MAXN + 5];
long long nfac[MAXN + 5];
long long q_mod(long long a, long long b, long long mod) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
void init() {
fac[0] = 1;
for (long long i = 1; i <= MAXN; ++i) fac[i] = fac[i - 1] * i % MOD;
nfac[MAXN] = q_mod(fac[MAXN], MOD - 2, MOD) % MOD;
for (long long i = MAXN - 1; i >= 0; --i)
nfac[i] = nfac[i + 1] * (i + 1) % MOD;
}
long long C(long long n, long long m) {
if (m > n) return 0;
return fac[n] * nfac[m] % MOD * nfac[n - m] % MOD;
}
int arr[MAXN];
struct node {
int val, cnt;
};
vector<node> v;
bool judge(int num) {
while (num) {
if (num % 10 != 4 && num % 10 != 7) return false;
num /= 10;
}
return true;
}
long long dp[2][MAXN];
int main() {
init();
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
sort(arr + 1, arr + 1 + n);
int sz;
int lucky_cnt = 0;
v.push_back(node{0, 0});
for (int i = 1; i <= n; i++) {
if (judge(arr[i])) {
lucky_cnt++;
sz = v.size();
if (v[sz - 1].val != arr[i])
v.push_back(node{arr[i], 1});
else
v[sz - 1].cnt++;
}
}
dp[0][0] = 1;
sz = v.size() - 1;
int last = 1, now = 0;
for (int i = 1; i <= sz; i++) {
last = 1 - last;
now = 1 - now;
dp[now][0] = 1;
for (int j = 1; j <= sz; j++) {
dp[now][j] = dp[last][j] + dp[last][j - 1] * v[i].cnt;
dp[now][j] %= MOD;
}
}
int unlucky_cnt = n - lucky_cnt;
long long ans = 0;
for (int i = 0; i <= unlucky_cnt; i++) {
if (k - i >= 0) {
ans += dp[now][k - i] * C(unlucky_cnt, i);
ans %= MOD;
} else
break;
}
ans %= MOD;
ans += MOD;
ans %= MOD;
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int x, i;
for (i = x = 0; i < s.size(); i++)
if (s[i] == 'x')
x++;
else
x--;
for (i = 1; i <= x; i++) cout << 'x';
for (i = -1; i >= x; i--) cout << 'y';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
int n;
cin >> n;
int last = 0;
vector<pair<int, int> > v;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
if (k == 0) {
long long cnt = 0;
sort(v.begin(), v.end());
for (last; last < i; last++) {
int q = v.size() - 1;
if (last == v[q--].second) {
cout << "pushFront\n";
continue;
}
if (q == -1) {
cout << "pushBack\n";
continue;
}
if (last == v[q--].second) {
cout << "pushQueue\n";
continue;
}
if (q == -1) {
cout << "pushBack\n";
continue;
}
if (last == v[q--].second) {
cout << "pushStack\n";
continue;
}
cout << "pushBack\n";
}
cout << min(int(v.size()), 3);
if (v.size() > 0) cout << " popFront";
if (v.size() > 1) cout << " popQueue";
if (v.size() > 2) cout << " popStack";
v.clear();
cout << "\n";
last++;
} else
v.push_back(make_pair(k, i));
}
sort(v.begin(), v.end());
for (last; last < n; last++) {
int q = v.size() - 1;
if (last == v[q--].second) {
cout << "pushFront\n";
continue;
}
if (q == -1) {
cout << "pushBack\n";
continue;
}
if (last == v[q--].second) {
cout << "pushQueue\n";
continue;
}
if (q == -1) {
cout << "pushBack\n";
continue;
}
if (last == v[q--].second) {
cout << "pushStack\n";
continue;
}
cout << "pushBack\n";
}
return 0;
}
| 3 |
#include <iostream>
using namespace std;
int main(void){
int a,b,c,d,e,f,g,h,i,j;
cin >>a>>b>>c>>d>>e>>f>>g>>h;
i = a * e + b * f;
if(a >= 10){
i = i + g * (a/10);
}
if(b >= 20){
i = i + h * (b/20);
}
j = c * e + d * f;
if(c >= 10){
j = j + g * (c/10);
}
if(d >= 20){
j = j + h*(d/20);
}
if(i > j){
cout<<"hiroshi"<<endl;
}else if(i < j){
cout <<"kenjiro"<<endl;
}else{
cout<<"even"<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const int mm = 2e5 + 5;
string s;
int n;
int a[3];
void nhap() {
getline(cin, s);
n = s.size();
}
void xuli() {
for (int i = 0; i < n; i++) {
if (s[i] - '?')
a[s[i] - '0']++;
else
a[2]++;
}
if (a[0] + a[2] - 2 >= n / 2 - 1) printf("00\n");
if ((s[n - 1] == '1' && a[0] + a[2] - 1 >= n / 2 - 1 &&
a[1] + a[2] - 1 >= n / 2 - 1 + n % 2) ||
(s[n - 1] == '?' && a[0] + a[2] - 2 >= n / 2 - 1 &&
a[1] + a[2] - 1 >= n / 2 - 1 + n % 2))
printf("01\n");
if ((s[n - 1] == '0' && a[0] + a[2] - 1 >= n / 2 - 1 &&
a[1] + a[2] - 1 >= n / 2 - 1 + n % 2) ||
(s[n - 1] == '?' && a[0] + a[2] - 1 >= n / 2 - 1 &&
a[1] + a[2] - 2 >= n / 2 - 1 + n % 2))
printf("10\n");
if (a[1] + a[2] - 2 >= n / 2 - 1 + n % 2) printf("11\n");
}
int main() {
nhap();
xuli();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a, b;
int main(void) {
cin >> a >> b;
for (int i = 1;; i++) {
if (i & 1) {
a -= i;
if (a < 0) {
puts("Vladik");
return 0;
}
} else {
b -= i;
if (b < 0) {
puts("Valera");
return 0;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, x, y, c;
long long calc(long long t) {
long long int ret = 0;
ret += 2 * t * (t + 1) - 4 * t + 1;
if (t > x) {
ret -= (t - x) * (t - x);
}
if (t > n - x + 1) {
ret -= (t - n + x - 1) * (t - n + x - 1);
}
if (t > y) {
ret -= (t - y) * (t - y);
}
if (t > n - y + 1) {
ret -= (t - n + y - 1) * (t - n + y - 1);
}
if (t > x + y) {
ret += ((t - x - y) * (t - x - y + 1) / 2);
}
if (t > x + n - y + 1) {
ret += ((t - x - n + y - 1) * (t - x - n + y - 1 + 1) / 2);
}
if (t > n - x + 1 + y) {
ret += ((t - n + x - 1 - y) * (t - n + x - 1 - y + 1) / 2);
}
if (t > n - x + 1 + n - y + 1) {
ret += ((t - n + x - 1 - n + y - 1) * (t - n + x - 1 - n + y - 1 + 1) / 2);
}
return ret;
}
int main() {
cin >> n >> x >> y >> c;
int l = 0, r = 1000000002;
while (r - l > 1) {
if (calc((r + l) / 2) < c) {
l = (r + l) / 2;
} else {
r = (r + l) / 2;
}
}
cout << r - 1 << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, r, z, a[100100], ans[100100][5], b[100100][5], d[100100],
bb[5];
vector<int> db[5], c[100100];
bool u[200200];
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= n; i++)
if (!u[i]) {
k = 0;
for (j = i; !u[j]; j = a[j], k++) {
u[j] = true;
c[m].push_back(j);
}
if (k % 4 <= 1) {
if (k > 1)
for (int i = 0; i < k / 4; i++, r++) {
d[r] = (i == k / 4 - 1 && k % 4 == 0) ? 4 : 5;
ans[r][0] = c[m][0];
ans[r][1] = c[m][i * 4 + 1];
ans[r][2] = c[m][i * 4 + 2];
ans[r][3] = c[m][i * 4 + 3];
if (d[r] == 5) ans[r][4] = c[m][i * 4 + 4];
b[r][0] = ans[r][1];
b[r][1] = ans[r][2];
b[r][2] = ans[r][3];
if (d[r] == 5)
b[r][3] = ans[r][4];
else
b[r][3] = ans[r][0];
if (d[r] == 5) b[r][4] = ans[r][0];
}
c[m].clear();
} else {
for (int i = 0; i < k / 4; i++, r++) {
d[r] = 5;
ans[r][0] = c[m][0];
ans[r][1] = c[m][i * 4 + 1];
ans[r][2] = c[m][i * 4 + 2];
ans[r][3] = c[m][i * 4 + 3];
ans[r][4] = c[m][i * 4 + 4];
b[r][0] = ans[r][1];
b[r][1] = ans[r][2];
b[r][2] = ans[r][3];
b[r][3] = ans[r][4];
b[r][4] = ans[r][0];
}
db[k % 4].push_back(m++);
bb[k % 4]++;
}
}
while (bb[2] > 0 && bb[3] > 0) {
d[r] = 5;
int d1 = db[2][bb[2] - 1], d2 = db[3][bb[3] - 1];
ans[r][0] = c[d1][0];
ans[r][1] = c[d1][c[d1].size() - 1];
ans[r][2] = c[d2][0];
ans[r][3] = c[d2][c[d2].size() - 2];
ans[r][4] = c[d2][c[d2].size() - 1];
b[r][0] = ans[r][1];
b[r][1] = ans[r][0];
b[r][2] = ans[r][3];
b[r][3] = ans[r][4];
b[r][4] = ans[r][2];
bb[2]--;
bb[3]--;
r++;
}
while (bb[2] > 1) {
d[r] = 4;
int d1 = db[2][bb[2] - 1], d2 = db[2][bb[2] - 2];
ans[r][0] = c[d1][0];
ans[r][1] = c[d1][c[d1].size() - 1];
ans[r][2] = c[d2][0];
ans[r][3] = c[d2][c[d2].size() - 1];
b[r][0] = ans[r][1];
b[r][1] = ans[r][0];
b[r][2] = ans[r][3];
b[r][3] = ans[r][2];
bb[2] -= 2;
r++;
}
while (bb[3] > 2) {
d[r] = 5;
int d1 = db[3][bb[3] - 1], d2 = db[3][bb[3] - 2];
ans[r][0] = c[d1][0];
ans[r][1] = c[d1][c[d1].size() - 2];
ans[r][2] = c[d1][c[d1].size() - 1];
ans[r][3] = c[d2][0];
ans[r][4] = c[d2][c[d2].size() - 2];
b[r][0] = ans[r][1];
b[r][1] = ans[r][2];
b[r][2] = ans[r][0];
b[r][3] = ans[r][4];
b[r][4] = ans[r][3];
bb[3]--;
d[++r] = 5;
d2 = db[3][bb[3] - 1], d1 = db[3][bb[3] - 2];
ans[r][0] = c[d1][0];
ans[r][1] = c[d1][c[d1].size() - 2];
ans[r][2] = c[d1][c[d1].size() - 1];
ans[r][3] = c[d2][0];
ans[r][4] = c[d2][c[d2].size() - 1];
b[r][0] = ans[r][1];
b[r][1] = ans[r][2];
b[r][2] = ans[r][0];
b[r][3] = ans[r][4];
b[r][4] = ans[r][3];
bb[3] -= 2;
r++;
}
while (bb[3] > 0) {
d[r] = 3;
int d1 = db[3][bb[3] - 1];
ans[r][0] = c[d1][0];
ans[r][1] = c[d1][c[d1].size() - 2];
ans[r][2] = c[d1][c[d1].size() - 1];
b[r][0] = ans[r][1];
b[r][1] = ans[r][2];
b[r][2] = ans[r][0];
bb[3]--;
r++;
}
if (bb[2] > 0) {
d[r] = 2;
int d1 = db[2][bb[2] - 1];
ans[r][0] = c[d1][0];
ans[r][1] = c[d1][c[d1].size() - 1];
b[r][0] = ans[r][1];
b[r][1] = ans[r][0];
bb[2]--;
r++;
}
printf("%d\n", r);
for (i = 0; i < r; i++) {
printf("%d\n", d[i]);
for (j = 0; j < d[i]; j++) {
if (j) putchar(' ');
printf("%d", ans[i][j]);
}
puts("");
for (j = 0; j < d[i]; j++) {
if (j) putchar(' ');
printf("%d", b[i][j]);
}
puts("");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
signed main()
{
char a,b;
cin >> a >> b;
cout << (a==b ? 'H' : 'D');
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, i, j, k, ans = 0;
cin >> n >> m;
vector<int> w(n + 1), b(m + 1), last(n + 1, 0), touch(n + 1, 0);
for (i = 1; i <= n; i++) cin >> w[i];
for (i = 1; i <= m; i++) cin >> b[i];
ans = 0;
for (i = 1; i <= m; i++) {
for (j = last[b[i]] + 1; j < i; j++) {
if (touch[b[j]] < i) {
touch[b[j]] = i;
ans += w[b[j]];
}
}
last[b[i]] = i;
}
cout << ans << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
const long long MOD = 1e9 + 7;
const long long MOD_2 = MOD - 1;
struct mat {
long long M[N][N];
mat() { memset(M, 0, sizeof M); }
void print() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << M[i][j] << " ";
}
cout << "\n";
;
}
}
};
mat ONE() {
mat mat;
for (int i = 0; i < N; i++) mat.M[i][i] = 1;
return mat;
}
mat MUL(mat a, mat b) {
mat sol;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
sol.M[i][j] += a.M[i][k] * b.M[k][j];
sol.M[i][j] %= MOD_2;
}
}
return sol;
}
mat POW(mat a, long long n) {
mat res = ONE();
for (; n > 0; n >>= 1) {
if (n & 1) {
res = MUL(res, a);
}
a = MUL(a, a);
}
return res;
}
vector<long long> Solve(mat &a, vector<long long> &v) {
vector<long long> solution;
for (int i = 0; i < N; i++) {
long long sum = 0;
for (int j = 0; j < N; j++) sum = (sum + a.M[i][j] * v[j]) % MOD_2;
solution.push_back(sum);
}
return solution;
}
long long mul(long long a, long long b) { return a * b % MOD; }
int mpow(long long a, long long n) {
int res = 1;
for (; n > 0; n >>= 1) {
if (n & 1) res = mul(res, a);
a = mul(a, a);
}
return res;
}
void fix(long long &x) {
x = x % MOD;
if (x < 0) x += MOD;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
mat init;
for (int i = 0; i + 2 <= 11; i += 3) {
init.M[0 + i][1 + i] = 1;
init.M[1 + i][2 + i] = 1;
init.M[2 + i][2 + i] = init.M[2 + i][0 + i] = init.M[2 + i][1 + i] = 1;
}
init.M[11][12] = 1;
init.M[12][12] = 1;
init.M[12][13] = 2;
init.M[13][13] = 1;
long long n, f1, f2, f3, c;
cin >> n >> f1 >> f2 >> f3 >> c;
mat p = POW(init, n - 3);
vector<long long> v(N);
v[0] = 1;
v[4] = 1;
v[8] = 1;
v[12] = 2;
v[13] = 1;
vector<long long> res = Solve(p, v);
fix(res[2]);
fix(res[5]);
fix(res[8]);
fix(res[11]);
int ans = mul(mpow(f1, res[2]), mpow(f2, res[5]));
ans = mul(ans, mpow(f3, res[8]));
ans = mul(ans, mpow(c, res[11]));
cout << ans << "\n";
;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 150;
int n, m, k, ar[MAXN][MAXN], sum[MAXN][MAXN], ans1, ans2, tmpans1, tmpans2;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &ar[i][j]);
}
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
sum[i][j] = sum[i][j - 1] + ar[j][i];
}
}
for (int i = 1; i <= m; i++) {
tmpans1 = tmpans2 = 0;
for (int j = 1; j <= n; j++) {
int r = min(n, j + k - 1);
if (sum[i][r] - sum[i][j - 1] > tmpans1) {
tmpans1 = sum[i][r] - sum[i][j - 1];
tmpans2 = sum[i][j - 1];
}
}
ans1 += tmpans1;
ans2 += tmpans2;
}
printf("%d %d", ans1, ans2);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
double dp1[5000][201], dp2[5000][201];
double p1, p2;
int l1, r1, l2, r2, DT1, DT2;
void precomp(int hp1, int hp2) {
dp1[0][hp1] = 1;
for (int k = 0; k < 5000 - 1; k++) {
for (int i = hp1; i >= 0; i--) {
if (dp1[k][i] < 1e-8) continue;
double diff = 1.0 / (r2 - l2 + 1);
for (int j = l2; j <= r2; j++) {
dp1[k + 1][max(i - j, 0)] += dp1[k][i] * diff * (1 - p2);
dp1[k + 1][i] += dp1[k][i] * diff * p2;
}
}
}
dp2[0][hp2] = 1;
for (int k = 0; k < 5000 - 1; k++) {
for (int i = hp2; i >= 0; i--) {
if (dp2[k][i] < 1e-8) continue;
double diff = 1.0 / (r1 - l1 + 1);
for (int j = l1; j <= r1; j++) {
dp2[k + 1][max(i - j, 0)] += dp2[k][i] * diff * (1 - p1);
dp2[k + 1][i] += dp2[k][i] * diff * p1;
}
}
}
}
int main() {
int hp1, hp2;
cin >> hp1 >> DT1 >> l1 >> r1 >> p1;
cin >> hp2 >> DT2 >> l2 >> r2 >> p2;
if (p1 == 100) {
cout << 0 << endl;
return 0;
}
if (p2 == 100) {
cout << 1 << endl;
return 0;
}
p1 /= 100;
p2 /= 100;
precomp(hp1, hp2);
int s1 = 0, s2 = 0;
double ans = 0, carry = 1;
for (int dt = 0; s1 < 5000 - 1 && s2 < 5000; dt++) {
if (dt % DT1 == 0) {
s1++;
ans += (1 - dp1[s2][0]) * (dp2[s1][0] - dp2[s1 - 1][0]);
}
if (dt % DT2 == 0) {
s2++;
}
}
cout.precision(6);
cout << fixed << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
set<string> myset;
int main() {
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
string t;
for (int j = i; j < s.size(); j++) t += s[j];
for (int j = 0; j < i; j++) t += s[j];
myset.insert(t);
}
cout << myset.size() << endl;
}
| 1 |
//
// _oo0oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/`---'\___
// .' \\| |// '.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' |_/ |
// \ .-\__ '-' ___/-. /
// ___'. .' /--.--\ `. .'___
// ."" '< `.___\_<|>_/___.' >' "".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `_. \_ __\ /__ _/ .-` / /
// =====`-.____`.___ \_____/___.-`___.-'=====
// `=---='
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB emplace_back
#define PF emplace_front
#define FILL(a,b) memset(a,b,sizeof(a))
#define lwb lower_bound
#define upb upper_bound
#define lc (x<<1)
#define rc ((x<<1)|1)
using namespace std;
int it[9][6];
int t[505][505],mod=998244353;
int f[54][54];
struct matrix{
int s[505][505],n,m;
void mul(matrix a){
FOR(i,1,n){
FOR(j,1,m){
FOR(k,1,m){
t[i][j]=(s[i][k]*a.s[k][j]+t[i][j])%mod;
}
}
}
FOR(i,1,n)FOR(j,1,m)s[i][j]=t[i][j],t[i][j]=0;
}
}tx,T;
int m,n,l[9];
char c[9][6];
P<int,int> p[54];
bool check(P<int,int> x,P<int,int> y){
for(int i=x.S,j=y.S;i<=l[x.F]&&j<=l[y.F];i++,j++){
if(c[x.F][i]!=c[y.F][j])RE 0;
}
RE 1;
}
int k;
int nxt(int i){
P<int,int> tp=p[i];
tp.S++;
if(tp.S==l[tp.F]){
RE k;
}else RE it[tp.F][tp.S];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
int cnt=0;
FOR(i,1,n){
string s;
cin>>s;
l[i]=s.size();
FOR(j,1,l[i])c[i][j]=s[j-1];
rep(j,1,l[i])it[i][j]=++cnt,p[cnt]=MP(i,j);
}
it[0][0]=++cnt;p[cnt]=MP(0,0);
k=cnt;
cnt=0;
FOR(i,1,k){
FOR(j,i,k){
if(i==k||j==k||check(p[i],p[j]))f[i][j]=f[j][i]=++cnt;
}
}
rep(i,1,k){
rep(j,i,k){
if(f[i][j])tx.s[f[i][j]][f[nxt(i)][nxt(j)]]++;
}
}
tx.n=cnt;
tx.m=cnt;
rep(i,1,k){
FOR(j,1,n){
if(f[nxt(i)][it[j][1]?it[j][1]:k]&&c[j][1]==c[p[i].F][p[i].S+1]){
tx.s[f[i][k]][f[nxt(i)][it[j][1]?it[j][1]:k]]++;
}
}
}
FOR(i,1,n){
FOR(j,1,n){
if(c[i][1]==c[j][1]&&f[it[i][1]?it[i][1]:k][it[j][1]?it[j][1]:k]){
tx.s[f[k][k]][f[it[i][1]?it[i][1]:k][it[j][1]?it[j][1]:k]]++;
}
}
}
T.n=1;
T.m=cnt;
T.s[1][f[k][k]]=1;
while(m){
if(m&1)T.mul(tx);
tx.mul(tx);
m/=2;
}
cout<<T.s[1][f[k][k]];
RE 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long x, y, z;
cin >> x >> y >> z;
long long sum = x + y;
cout << sum / z << ' ';
long long xx = x % z, yy = y % z;
if (xx + yy < z) {
cout << 0;
return 0;
}
cout << min(abs(z - (x % z)), abs(z - (y % z)));
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.