solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
using namespace std;
namespace fft {
struct num {
double x, y;
num() { x = y = 0; }
num(double x, double y) : x(x), y(y) {}
};
inline num operator+(num a, num b) { return num(a.x + b.x, a.y + b.y); }
inline num operator-(num a, num b) { return num(a.x - b.x, a.y - b.y); }
inline num operator*(num a, num b) {
return num(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
inline num conj(num a) { return num(a.x, -a.y); }
int base = 1;
vector<num> roots = {{0, 0}, {1, 0}};
vector<int> rev = {0, 1};
const double PI = acosl(-1.0);
void ensure_base(int nbase) {
if (nbase <= base) {
return;
}
rev.resize(1 << nbase);
for (int i = 0; i < (1 << nbase); i++) {
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
}
roots.resize(1 << nbase);
while (base < nbase) {
double angle = 2 * PI / (1 << (base + 1));
for (int i = 1 << (base - 1); i < (1 << base); i++) {
roots[i << 1] = roots[i];
double angle_i = angle * (2 * i + 1 - (1 << base));
roots[(i << 1) + 1] = num(cos(angle_i), sin(angle_i));
}
base++;
}
}
void fft(vector<num> &a, int n = -1) {
if (n == -1) {
n = a.size();
}
assert((n & (n - 1)) == 0);
int zeros = __builtin_ctz(n);
ensure_base(zeros);
int shift = base - zeros;
for (int i = 0; i < n; i++) {
if (i < (rev[i] >> shift)) {
swap(a[i], a[rev[i] >> shift]);
}
}
for (int k = 1; k < n; k <<= 1) {
for (int i = 0; i < n; i += 2 * k) {
for (int j = 0; j < k; j++) {
num z = a[i + j + k] * roots[j + k];
a[i + j + k] = a[i + j] - z;
a[i + j] = a[i + j] + z;
}
}
}
}
vector<num> fa, fb;
vector<long long> multiply(vector<int> &a, vector<int> &b) {
int need = a.size() + b.size() - 1;
int nbase = 0;
while ((1 << nbase) < need) nbase++;
ensure_base(nbase);
int sz = 1 << nbase;
if (sz > (int)fa.size()) {
fa.resize(sz);
}
for (int i = 0; i < sz; i++) {
int x = (i < (int)a.size() ? a[i] : 0);
int y = (i < (int)b.size() ? b[i] : 0);
fa[i] = num(x, y);
}
fft(fa, sz);
num r(0, -0.25 / sz);
for (int i = 0; i <= (sz >> 1); i++) {
int j = (sz - i) & (sz - 1);
num z = (fa[j] * fa[j] - conj(fa[i] * fa[i])) * r;
if (i != j) {
fa[j] = (fa[i] * fa[i] - conj(fa[j] * fa[j])) * r;
}
fa[i] = z;
}
fft(fa, sz);
vector<long long> res(need);
for (int i = 0; i < need; i++) {
res[i] = fa[i].x + 0.5;
}
return res;
}
vector<int> multiply_mod(vector<int> &a, vector<int> &b, int m, int eq = 0) {
int need = a.size() + b.size() - 1;
int nbase = 0;
while ((1 << nbase) < need) nbase++;
ensure_base(nbase);
int sz = 1 << nbase;
if (sz > (int)fa.size()) {
fa.resize(sz);
}
for (int i = 0; i < (int)a.size(); i++) {
int x = (a[i] % m + m) % m;
fa[i] = num(x & ((1 << 15) - 1), x >> 15);
}
fill(fa.begin() + a.size(), fa.begin() + sz, num{0, 0});
fft(fa, sz);
if (eq) {
copy(fa.begin(), fa.begin() + sz, fb.begin());
} else {
if (sz > (int)fb.size()) {
fb.resize(sz);
}
for (int i = 0; i < (int)b.size(); i++) {
int x = (b[i] % m + m) % m;
fb[i] = num(x & ((1 << 15) - 1), x >> 15);
}
fill(fb.begin() + b.size(), fb.begin() + sz, num{0, 0});
fft(fb, sz);
}
double ratio = 0.25 / sz;
num r2(0, -1);
num r3(ratio, 0);
num r4(0, -ratio);
num r5(0, 1);
for (int i = 0; i <= (sz >> 1); i++) {
int j = (sz - i) & (sz - 1);
num a1 = (fa[i] + conj(fa[j]));
num a2 = (fa[i] - conj(fa[j])) * r2;
num b1 = (fb[i] + conj(fb[j])) * r3;
num b2 = (fb[i] - conj(fb[j])) * r4;
if (i != j) {
num c1 = (fa[j] + conj(fa[i]));
num c2 = (fa[j] - conj(fa[i])) * r2;
num d1 = (fb[j] + conj(fb[i])) * r3;
num d2 = (fb[j] - conj(fb[i])) * r4;
fa[i] = c1 * d1 + c2 * d2 * r5;
fb[i] = c1 * d2 + c2 * d1;
}
fa[j] = a1 * b1 + a2 * b2 * r5;
fb[j] = a1 * b2 + a2 * b1;
}
fft(fa, sz);
fft(fb, sz);
vector<int> res(need);
for (int i = 0; i < need; i++) {
long long aa = fa[i].x + 0.5;
long long bb = fb[i].x + 0.5;
long long cc = fa[i].y + 0.5;
res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m;
}
return res;
}
vector<int> square_mod(vector<int> &a, int m) {
return multiply_mod(a, a, m, 1);
}
}; // namespace fft
const int N = 1234567;
char s[N];
int main() {
int tt;
scanf("%d", &tt);
while (tt--) {
int n;
scanf("%d", &n);
scanf("%s", s);
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
if (s[i] == 'V') {
a[i] = 1;
}
if (s[i] == 'K') {
b[n - i - 1] = 1;
}
}
vector<long long> c = fft::multiply(a, b);
vector<int> res;
for (int i = 1; i <= n; i++) {
bool ok = true;
for (int j = i; j < n; j += i) {
if (c[n - 1 - j] != 0 || c[n - 1 + j] != 0) {
ok = false;
break;
}
}
if (ok) {
res.push_back(i);
}
}
int sz = res.size();
printf("%d\n", sz);
for (int i = 0; i < sz; i++) {
if (i > 0) putchar(' ');
printf("%d", res[i]);
}
printf("\n");
}
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > gr;
vector<vector<int> > grs;
vector<int> col;
int k = 0;
int gcd(int a, int b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int f(int c) {
if (k == 0) return c;
return (c % k + k) % k;
}
const int MAX = 1e9;
void dfs(int v, int c) {
if (col[v] == -MAX) {
col[v] = c;
for (int i = 0; i < gr[v].size(); ++i) dfs(gr[v][i], c + grs[v][i]);
} else if (f(col[v]) != f(c)) {
k = gcd(k, abs(col[v] - c));
}
}
int main() {
int n, m;
cin >> n >> m;
gr.resize(n);
grs.resize(n);
col.assign(n, -MAX);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
gr[a].push_back(b);
grs[a].push_back(1);
gr[b].push_back(a);
grs[b].push_back(-1);
}
for (int i = 0; i < n; ++i)
if (col[i] == -MAX) dfs(i, 0);
if (k)
cout << k << endl;
else
cout << n << endl;
return 0;
}
| 14 |
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main() {
ll t;
cin>>t;
//t=1;
while(t--)
{
ll n,m,k,j,i,y,x,z,a,b,c,d,o;
ll count=0,ans=0,maxi=LLONG_MIN,mini=LLONG_MAX,flag=0;
char ch;
vector<long long int> vecy,vect;
string str,strs;
map<ll,ll> mp;
stack <long long int> stck;
set<ll > se;
cin>>n;
cin>>str;
x=0;
int arr[n];arr[0]=0;
for(i=1;i<n;i++)
{
if(str[i]!=str[i-1])
{
x++;
arr[i]=x;
}
else
{
x=0;
arr[i]=0;
}
}
x=0;
int arrs[n];
arrs[n-1]=0;
for(i=n-2;i>=0;i--)
{
if(str[i]!=str[i+1])
{
x++;
arrs[i]=x;
}
else
{
x=0;
arrs[i]=0;
}
}
if(str[0]=='L')
cout<<1<<" ";
else
cout<<arrs[0]+arr[0]+2<<" ";
for(i=1;i<n;i++)
{
if(str[i]=='R'&&str[i-1]=='L')
cout<<arr[i]+arrs[i]+2<<" ";
else if(str[i]=='R')
cout<<arrs[i]+2<<" ";
else if(str[i-1]=='L')
cout<<arr[i-1]+2<<" ";
else
cout<<1<<" ";
}
if(str[n-1]=='R')
cout<<1<<" ";
else
cout<<arrs[n-1]+arr[n-1]+2<<" ";
cout<<"\n";
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int N, x[5005], y[5005], L = 1e9, R = -1e9, D = 1e9, U = -1e9;
int main() {
scanf("%d", &N);
for (int i = 1, X, Y; i <= N; i++)
scanf("%d%d", &X, &Y), x[i] = X - Y, y[i] = X + Y, L = min(L, x[i]),
R = max(R, x[i]), D = min(D, y[i]), U = max(U, y[i]);
int a = 0, b = 0, c, ans = 2;
for (int i = 1; i <= N; i++)
a = max(a, min(max(x[i] - L, y[i] - D), max(R - x[i], U - y[i]))),
b = max(b, min(max(x[i] - L, U - y[i]), max(R - x[i], y[i] - D)));
c = min(a, b);
for (int i = 1; i <= N; i++)
if (max(max(x[i] - L, R - x[i]), max(y[i] - D, U - y[i])) <= c)
(ans <<= 1) %= 1000000007;
if (a == b && L + c < R && D + c < U) (ans <<= 1) %= 1000000007;
printf("%d\n%d\n", c, ans);
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int cnt[5] = {0, 0, n * 2, 0, n};
for (int i = 0; i < k; i++) {
int a;
cin >> a;
while (cnt[4] and a >= 4) a -= 4, cnt[4]--;
while (cnt[2] and a >= 2) a -= 2, cnt[2]--;
while ((cnt[1] or cnt[2] or cnt[4]) and a) {
if (cnt[4] and a >= 2)
cnt[4]--, cnt[1]++, a -= 2;
else if (cnt[4] and a)
cnt[4]--, cnt[2]++, a--;
else if (cnt[2] and a >= 2)
cnt[2]--, a -= 2;
else if (cnt[2] and a)
cnt[2]--, a--;
else if (cnt[1] and a)
a--, cnt[1]--;
}
if (a) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000;
int a[1 + MAXN], b[1 + MAXN];
int v[1 + MAXN];
void Try(int la, int lb, int c) {
int i = 1;
while (i <= la && i <= lb && a[i] == b[i]) i++;
if (i > la) {
v[0]++;
return;
}
if (i > lb) {
printf("-1\n");
exit(0);
}
int x = a[i], y = b[i];
if (x < y) {
v[0]++;
v[c - y + 1]--;
if (x != 1) v[c - x + 1]++;
} else {
v[c - x + 1]++;
v[c - y + 1]--;
}
}
int main() {
int n, c, la, lb;
scanf("%d%d", &n, &c);
scanf("%d", &la);
for (int i = 1; i <= la; i++) scanf("%d", &a[i]);
for (int j = 2; j <= n; j++) {
scanf("%d", &lb);
for (int i = 1; i <= lb; i++) scanf("%d", &b[i]);
Try(la, lb, c);
la = lb;
for (int i = 1; i <= la; i++) a[i] = b[i];
}
int x = 0;
for (int i = 0; i < c; i++) {
x += v[i];
if (x == n - 1) {
printf("%d\n", i);
return 0;
}
}
printf("-1\n");
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
inline void boost() {
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
}
const long long maxn = 2e5 + 123;
const long long inf = 1e9 + 123;
const long long linf = 1e18 + 123;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double pi = acos(-1);
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};
int main() {
boost();
string a, b;
cin >> a >> b;
int ans = 0;
for (int i = 1; i < a.size(); i++) {
if (a[i - 1] == '0' && b[i - 1] == '0' && b[i] == '0') {
ans++;
a[i - 1] = 'X';
b[i - 1] = 'X';
b[i] = 'X';
} else if (a[i - 1] == '0' && b[i - 1] == '0' && a[i] == '0') {
ans++;
a[i - 1] = 'X';
b[i - 1] = 'X';
a[i] = 'X';
} else if (a[i] == '0' && b[i] == '0' && a[i - 1] == '0') {
ans++;
a[i] = 'X';
b[i] = 'X';
a[i - 1] = 'X';
} else if (a[i] == '0' && b[i] == '0' && b[i - 1] == '0') {
ans++;
a[i] = 'X';
b[i] = 'X';
b[i - 1] = 'X';
}
}
cout << ans;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1000000000;
vector<int> build(int n) {
vector<int> g(n, 1);
if (n == 1) {
g[0] = 1;
} else if (n == 2) {
g[0] = 3;
g[1] = 4;
} else if (n % 2) {
g[0] = 2;
g[n - 1] = (n + 1) / 2;
} else {
g[n - 1] = (n - 1) / 2;
}
return g;
}
int main() {
int n, m;
cin >> n >> m;
auto a = build(n), b = build(m);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cout << a[i] * b[j] << " ";
}
cout << endl;
}
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, t, mod, i;
cin >> n >> m;
for (i = 1; i <= 500000; i++) {
if (n > m) {
n -= 2;
m -= 1;
} else {
m -= 2;
n -= 1;
}
if (n < 0 or m < 0) return cout << i - 1, 0;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int i;
if (s.size() > 2) {
for (i = 0; i < s.size() - 2; i++) {
if (s[i] != '.' && s[i + 1] != '.' && s[i + 2] != '.') {
if (s[i] != s[i + 1] && s[i + 1] != s[i + 2] && s[i + 2] != s[i]) {
cout << "Yes";
return 0;
}
}
}
}
cout << "No";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, l, r;
int main() {
cin >> n;
cin >> l;
r = l;
long long ans = 0;
for (int i = 1; i <= n; ++i) {
int L, R;
cin >> L >> R;
if (L <= l && r <= R) continue;
if (R <= l) {
ans += l - R;
r = l;
l = R;
} else if (r < L) {
ans += L - r;
l = r;
r = L;
} else {
l = max(L, l);
r = min(r, R);
}
}
cout << ans << endl;
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int num;
cin >> num;
string str;
cin >> str;
int n = str.length();
char a = '4', b = '7';
for (int i = 0; i < n; i++) {
if (str[i] != a and str[i] != b) {
cout << "NO";
return 0;
}
}
int count1 = 0, count2 = 0;
for (int i = 0; i < n; i++) {
if (i < n / 2) {
char b = str[i];
count1 += (b - 48);
} else {
char b = str[i];
count2 += (b - 48);
}
}
if (count1 == count2)
cout << "YES";
else
cout << "NO";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int arr[102], arr2[102];
int freq[2000];
int main() {
int tests;
cin >> tests;
string s;
cin >> s;
for (int i = 0; i < tests; i++) {
cin >> arr[i] >> arr2[i];
}
vector<int> time;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
for (int j = 0; j < arr2[i]; j++) {
time.push_back(j);
}
for (int k = arr2[i] + arr[i]; k < 1000; k += arr[i] * 2) {
for (int h = 0; h < arr[i]; h++) {
time.push_back(h + k);
}
}
} else {
for (int j = arr2[i]; j < 1000; j += arr[i] * 2)
for (int h = 0; h < arr[i]; h++) {
time.push_back(h + j);
}
}
}
for (int i = 0; i < time.size(); i++) {
freq[time[i]]++;
}
int maxi = 0;
for (int i = 0; i < 1000; i++) {
maxi = max(maxi, freq[i]);
}
cout << maxi;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, a, b, arr[6] = {0};
bool flag;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a >> b;
arr[a]++;
arr[b]++;
}
flag = false;
for (i = 1; i < 6; i++) {
if (arr[i] >= 3 || arr[i] <= 1) {
flag = true;
break;
}
}
if (flag) {
cout << "WIN";
} else {
cout << "FAIL";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int solve(int watps, int dowps, int tlength) {
if (watps <= dowps) return 0;
int start = 0;
int end = (watps * tlength) / dowps;
int res = 0;
while (start <= end) {
int mid = start + (end - start) / 2;
if (watps * tlength == dowps * (tlength + mid))
return mid;
else if (watps * tlength < dowps * (tlength + mid)) {
res = mid;
end = mid - 1;
} else
start = mid + 1;
}
return res;
}
int main() {
int watps, dowps, tlength;
cin >> watps >> dowps >> tlength;
int ans = solve(watps, dowps, tlength);
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-O3")
const long long MAXN = (long long)1e5;
long long tab[MAXN];
long long n, k;
long long eval(long long x) { return -3 * x * x - 3 * x - 1; }
void input() {
cin >> n >> k;
for (long long i = 0; i < (n); ++i) cin >> tab[i];
}
long long ile(long long A, long long val) {
if (val >= A) return 0;
long long p = 0;
long long q = A - 1;
while (p != q) {
long long s = (p + q + 1) / 2;
if (A + eval(s) > val)
p = s;
else
q = s - 1;
}
return p + 1;
}
bool ok(long long val) {
long long z = 0;
for (long long i = 0; i < (n); ++i) z += ile(tab[i], val);
return z >= k;
}
int32_t main() {
ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10),
cin.tie(0), cout.tie(0);
;
input();
long long p = -4e18;
long long q = 3e9;
while (p != q) {
long long s = (p + q + 1) / 2;
if (ok(s))
p = s;
else
q = s - 1;
}
long long val = p;
vector<long long> odp(n);
for (long long i = 0; i < (n); ++i) odp[i] = ile(tab[i], val);
long long a = accumulate((odp).begin(), (odp).end(), 0ll);
for (long long i = 0; i < (n); ++i)
if (a > k && ile(tab[i], val) != ile(tab[i], val + 1)) odp[i]--, a--;
for (long long i = 0; i < (n); ++i) cout << odp[i] << " ";
cout << "\n";
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, n, m;
cin >> a >> b;
n = min(a, b);
m = max(a, b);
if (n == 1)
cout << m << endl;
else if (n == 2)
cout << 2 * (2 * (m / 4) + min(2, m % 4));
else
cout << (n * m) - ((n * m) / 2);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = 50005;
map<int, int> v;
int x;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
v.clear();
scanf("%d", &n);
int f = 0;
for (int i = 1; i <= n; i++) {
cin >> x;
if (v[x] == 0) {
v[x] = i;
} else {
int p = i - v[x];
if (p > 1) f = 1;
}
}
if (f == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int i, i0, n, m, a[200005], maxi;
map<int, int> dic;
int main() {
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++) scanf("%d", &a[i]);
for (i = 0; i < n; i++) dic[a[i]] = dic[a[i] - 1] + 1;
maxi = 0;
for (i = 0; i < n; i++)
if (dic[maxi] <= dic[a[i]]) maxi = a[i];
printf("%d\n", dic[maxi]);
int p = maxi - dic[maxi] + 1;
for (i = 0; i < n; i++) {
if (a[i] == p) {
printf("%d", i + 1);
p++;
if (p > maxi) {
printf("\n");
break;
} else
printf(" ");
}
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
cout << (m * (m - 1)) / 2 << '\n';
for (int i = 1; i < m; ++i) {
for (int j = i + 1; j <= m; ++j) {
if (k)
cout << j << ' ' << i;
else
cout << i << ' ' << j;
cout << '\n';
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
struct node {
int dep, diamater, deplca, depa, atolca, btolca;
node() {}
node(int dep, int diamater, int deplca, int depa, int atolca, int btolca)
: dep(dep),
diamater(diamater),
deplca(deplca),
depa(depa),
atolca(atolca),
btolca(btolca) {}
node operator+(const node& oth) {
node res;
res.dep = dep + oth.dep;
res.deplca = min(deplca, dep + oth.deplca);
res.depa = max(depa, dep + oth.depa);
res.atolca =
max({atolca, oth.atolca - dep, -2 * dep - 2 * oth.deplca + depa});
res.btolca = max({btolca, oth.btolca - dep, -2 * deplca + oth.depa + dep});
res.diamater = max({diamater, oth.diamater, depa + oth.btolca - dep,
atolca + oth.depa + dep});
return res;
}
} tr[4 * maxn];
int N, Q;
void upd(int pos, int v, int id = 1, int l = 1, int r = N) {
if (l == r) {
if (v == 1) {
tr[id] = node(1, 1, 0, 1, 0, 1);
} else {
tr[id] = node(-1, 1, -1, 0, 2, 1);
}
return;
}
int mid = (l + r) / 2;
if (pos <= mid)
upd(pos, v, id << 1, l, mid);
else
upd(pos, v, id << 1 | 1, mid + 1, r);
tr[id] = tr[id << 1] + tr[id << 1 | 1];
}
int type(char x) {
if (x == '(') return 1;
return -1;
}
signed main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
string str;
cin >> str;
N = str.size();
str = " " + str;
for (int i = 1; i <= N; ++i) {
upd(i, type(str[i]));
}
cout << tr[1].diamater << '\n';
while (Q--) {
int l, r;
cin >> l >> r;
swap(str[l], str[r]);
upd(l, type(str[l]));
upd(r, type(str[r]));
cout << tr[1].diamater << '\n';
}
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
const double eps(1e-12);
const double pi(3.14159265358979);
const int N = 5050, T = 5050;
int n, m, t[N] = {};
double u1[T] = {}, p[N] = {}, u2[T] = {}, ans = 0;
void init() {
cin >> n >> m;
for (int i = 1, x = 0; i <= n; ++i) {
cin >> x >> t[i];
p[i] = x / 100.0;
}
}
void work() {
double *f = u1, *s = u2;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
double p1 = 1;
for (int j = 1; j <= t[i]; ++j) p1 *= 1 - p[i];
if (p1 < eps) p1 = 0;
s[0] = 0;
for (int j = 1; j < t[i]; ++j) {
s[j] = s[j - 1] * (1 - p[i]) + f[j - 1] * p[i];
if (s[j] < eps) s[j] = 0;
}
s[t[i]] = s[t[i] - 1] * (1 - p[i]) + f[t[i] - 1] * p[i] + p1 * f[0];
if (s[t[i]] < eps) s[t[i]] = 0;
for (int j = t[i] + 1; j <= m; ++j) {
s[j] = s[j - 1] * (1 - p[i]) + f[j - 1] * p[i] +
p1 * (f[j - t[i]] - f[j - t[i] - 1]);
if (s[j] < eps) s[j] = 0;
}
for (int j = 0; j <= m; ++j) ans += s[j];
swap(f, s);
}
printf("%.9f\n", ans);
}
int main() {
init();
work();
return 0;
}
| 16 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:32777216")
using namespace std;
int n, rr, v;
double x, y;
double bp(double l, double r) {
for (int i = 0; i < 100; i++) {
double x = (l + r) / 2;
if (2 * rr * x + rr * cos(acos(-1.0) / 2 - x) -
rr * cos(acos(-1.0) / 2 + x) >
y)
r = x;
else
l = x;
}
return r;
}
int main() {
cin >> n >> rr >> v;
for (int i = 0; i < n; i++) {
cin >> x >> y;
y -= x;
double z = (int)((y + .0) / (2.0 * acos(-1.0) * rr));
y -= 2 * z * acos(-1.0) * rr;
printf("%.14lf\n",
z * (2.0 * acos(-1.0) * rr) / v + 2 * rr * bp(0, acos(-1.0)) / v);
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000;
int a[maxn];
int main() {
int n, k;
long long sum = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (int i = 0; i < k; i++) {
sum += a[i];
}
cout << sum;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[3005];
int dist[3005][3005];
int n;
priority_queue<pair<int, int>> max1[3005], max2[3005];
vector<pair<int, int>> max11[3005], max12[3005];
bool visit[3005];
void bfs(int t) {
memset(visit + 1, 0, n);
queue<int> que;
visit[t] = true;
dist[t][t] = 0;
que.push(t);
while (que.empty() == false) {
auto x = que.front();
que.pop();
for (int nxt : graph[x]) {
if (visit[nxt]) continue;
dist[t][nxt] = dist[t][x] + 1;
visit[nxt] = true;
que.push(nxt);
}
}
}
int main() {
int m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
graph[a].push_back(b);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dist[i][j] = 1e8;
}
}
for (int i = 1; i <= n; i++) {
bfs(i);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
if (dist[i][j] < (int)1e7) max1[i].emplace(-dist[i][j], j);
if (dist[j][i] < (int)1e7) max2[i].emplace(-dist[j][i], j);
while (max1[i].size() > 2) max1[i].pop();
while (max2[i].size() > 2) max2[i].pop();
}
}
for (int i = 1; i <= n; i++) {
while (max1[i].empty() == false) {
max11[i].push_back(max1[i].top());
max1[i].pop();
}
while (max2[i].empty() == false) {
max12[i].push_back(max2[i].top());
max2[i].pop();
}
}
tuple<int, int, int, int> ans1;
int maxdist = -1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
if (dist[i][j] >= (int)1e7) continue;
for (int a = 0; a < max12[i].size(); a++) {
for (int b = 0; b < max11[j].size(); b++) {
if (max12[i][a].second == j ||
max12[i][a].second == max11[j][b].second ||
max11[j][b].second == i)
continue;
int total = dist[i][j] - max12[i][a].first - max11[j][b].first;
if (total > maxdist) {
maxdist = total;
ans1 = make_tuple(max12[i][a].second, i, j, max11[j][b].second);
}
}
}
}
}
printf("%d %d %d %d", get<0>(ans1), get<1>(ans1), get<2>(ans1), get<3>(ans1));
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
return a ? gcd(b % a, a) : b;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
const int MAXN = 1e3;
int N;
int A[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin >> N;
priority_queue<int> vals;
priority_queue<int> found;
for (int i = 0; i < N * N; i++) {
int v;
cin >> v;
vals.push(v);
}
for (int i = 0; i < N; i++) {
while (!vals.empty() && !found.empty() && vals.top() == found.top()) {
vals.pop();
found.pop();
}
A[i] = vals.top();
vals.pop();
for (int j = 0; j < i; j++) {
int g = gcd(A[i], A[j]);
found.push(g);
found.push(g);
}
}
assert(found.size() == vals.size());
for (int i = 0; i < N; i++) cout << A[i] << ' ';
cout << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 10;
inline int bin_pow(int base, int exp) {
if (!exp) return 1;
if (exp & 1)
return (unsigned long long)bin_pow(base, exp - 1) * base % MOD;
else
return bin_pow((unsigned long long)base * base % MOD, exp >> 1);
}
unsigned long long fact[MAXN];
int binom(int n, int k) {
return fact[n] * bin_pow(fact[k], MOD - 2) % MOD *
bin_pow(fact[n - k], MOD - 2) % MOD;
}
void init_fact() {
fact[0] = 1;
for (int i = 1; i < MAXN; ++i) fact[i] = fact[i - 1] * i % MOD;
}
int main() {
ios::sync_with_stdio(false);
init_fact();
string input;
cin >> input;
auto n = input.size();
vector<int> rightBrackets(n);
rightBrackets[n - 1] = input[n - 1] == ')';
for (int i = n - 2; i >= 0; --i)
rightBrackets[i] = (input[i] == ')') + rightBrackets[i + 1];
int leftBrackets = 0;
int res = 0;
for (int i = 0; i < n; ++i) {
if (input[i] == '(') {
res += binom(leftBrackets + rightBrackets[i], leftBrackets + 1);
++leftBrackets;
res %= MOD;
}
}
cout << res << "\n";
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, z, m;
cin >> n;
z = 0;
m = n;
for (i = 1; i <= n; i++) {
j = m + (m - 1) * (i - 1);
m--;
z += j;
}
cout << z << endl;
return 0;
}
| 2 |
/*
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
ordered_set -> less -> less_equal
ordered_set find_by_order(k) -> k element
ordered_set order_of_key(k) -> returns the number of elements in a set strictly smaller than k
*/
#include <bits/stdc++.h>
#include <random>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
//template<typename T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long maxn = 3e5 + 123;
const long long inf = 1e9 + 123;
const long long linf = 1e18 + 123;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double pi = acos(-1);
int dx[8] = {0, 1, -1, 0, 1, -1, 1, -1};
int dy[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int ans[maxn];
int main(){
#ifndef ONLINE_JUDGE
freopen("input.cpp", "r", stdin);
freopen("output.cpp", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T --){
int n, k;
cin >> n >> k;
int cntr = 1, kol = 0;
int last = k;
for (int i = k + 1; i <= n; i++){
kol += cntr;
last--;
cntr++;
}
cntr = k - 2;
int kol1 = k - 1;
for (int i = k + 1; i <= n; i++){
kol1 += cntr * 2;
cntr--;
}
int mx = kol - kol1;
last--;
for (int i = 1; i <= last; i++){
ans[i] = i;
}
int cur = k;
for (int i = last + 1; i <= k; i++){
ans[i] = cur;
cur--;
}
for (int i = 1; i <= k; i++){
cout << ans[i] << " ";
}
cout << "\n";
}
exit(0);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
int a[MAX];
unordered_map<int, int> cont;
int main() {
int n, k;
char no[] = "Nothing";
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
set<int> maxi;
for (int i = 0; i < k; i++) {
int &c = cont[a[i]];
if (c == 1) maxi.erase(-a[i]);
if (c == 0) maxi.insert(-a[i]);
c++;
}
if (!maxi.size())
printf("%s\n", no);
else
printf("%d\n", -(*maxi.begin()));
for (int i = k; i < n; i++) {
int &c = cont[a[i - k]];
if (c == 1) {
maxi.erase(-a[i - k]);
}
if (c == 2) {
maxi.insert(-a[i - k]);
}
c--;
int &cur = cont[a[i]];
if (cur == 1) maxi.erase(-a[i]);
if (cur == 0) maxi.insert(-a[i]);
cur++;
if (!maxi.size())
printf("%s\n", no);
else
printf("%d\n", -(*maxi.begin()));
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n % 2 ? "contest" : "home") << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const long long maxn = 200051;
const long long inf = 1000000007;
const long long mod = 1000000007;
long long m, w;
long long bpow(long long x, long long n) {
if (n == 0) return 1;
if (n % 2 == 1)
return (x * bpow(x, n - 1)) % m;
else {
long long tans = bpow(x, n / 2);
return (tans * tans) % m;
}
}
int n, x, y;
long long st[maxn], st1[maxn], ans;
vector<pair<int, long long> > v[maxn];
pair<unordered_map<long long, long long>, unordered_map<long long, long long> >
ma[maxn];
int dfs(int ver, long long a, long long b, long long d = 0, int pr = -1) {
int now = ver;
ma[now].first[a]++;
ma[now].second[(b * st1[d]) % m]++;
for (int i = 0; i < v[ver].size(); i++) {
if (v[ver][i].first == pr) continue;
long long ta = (a + (v[ver][i].second * st[d]) % m) % m;
long long tb = ((b * 10) % m + v[ver][i].second) % m;
int p = dfs(v[ver][i].first, ta, tb, d + 1, ver);
if (ma[p].first.size() > ma[now].first.size()) {
swap(now, p);
}
for (auto it = ma[p].first.begin(); it != ma[p].first.end(); it++) {
long long cur = it->first;
long long cnt = it->second;
cur = cur - a;
if (cur < 0) cur += m;
cur = (cur * st1[2 * d]) % m;
cur = -cur;
if (cur < 0) cur += m;
cur = (cur + (b * st1[d]) % m) % m;
if (ma[now].second.count(cur)) {
ans += cnt * (ma[now].second[cur]);
}
}
for (auto it = ma[p].second.begin(); it != ma[p].second.end(); it++) {
long long cur = it->first;
long long cnt = it->second;
cur = cur - (b * st1[d]) % m;
if (cur < 0) cur += m;
cur = (cur * st[2 * d]) % m;
cur = -cur;
if (cur < 0) cur += m;
cur = (cur + a) % m;
if (ma[now].first.count(cur)) {
ans += cnt * (ma[now].first[cur]);
}
}
for (auto it = ma[p].first.begin(); it != ma[p].first.end(); it++) {
ma[now].first[it->first] += it->second;
}
for (auto it = ma[p].second.begin(); it != ma[p].second.end(); it++) {
ma[now].second[it->first] += it->second;
}
}
return now;
}
long long fi(long long mo) {
long long z = mo;
for (long long i = 2; i * i <= m; i++) {
if (mo % i == 0) {
z = z - z / i;
}
while (mo % i == 0) {
mo /= i;
}
}
if (mo != 1) {
z = z - z / mo;
}
return z;
}
int main() {
scanf("%d %I64d", &n, &m);
for (int i = 0; i < n - 1; i++) {
scanf("%d %d %I64d", &x, &y, &w);
v[x].push_back(make_pair(y, w));
v[y].push_back(make_pair(x, w));
}
if (m == 0) {
cout << (long long)n * ((long long)n - 1);
return 0;
}
st[0] = 1;
for (int i = 1; i < maxn; i++) {
st[i] = (st[i - 1] * 10) % m;
}
long long tmp = bpow(10, fi(m) - 1);
st1[0] = 1;
for (int i = 1; i < maxn; i++) {
st1[i] = (st1[i - 1] * tmp) % m;
}
dfs(0, 0, 0);
printf("%I64d", ans);
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int head[N], cnt, fa[N];
vector<int> G[N];
void dfs(int u, int f) {
fa[u] = min(u, fa[f]);
for (auto it : G[u])
if (it != f) dfs(it, u);
}
int main() {
fa[0] = 1e9;
int n, q;
scanf("%d %d", &n, &q);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
int las = 0;
int root = 0;
int x;
scanf("%d", &x);
scanf("%d", &x);
x = (x + las) % n + 1;
dfs(root = x, 0);
q--;
int mx = n + 1;
while (q--) {
int op, x;
scanf("%d %d", &op, &x);
x = (x + las) % n + 1;
if (op == 1)
mx = min(mx, fa[x]);
else
printf("%d\n", las = min(mx, fa[x]));
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
char _;
const long long N = 1e6 + 5;
const long long INF = 1e9;
const long long mod = 1e9 + 7;
const long long LOGN = 15;
int a[N][10];
void solve() {
int n, m, k1;
cin >> n >> m >> k1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> a[i][j];
multiset<int> data[10];
int ans[10] = {0};
int len = 0;
for (int i = 1, j = 1; j <= n; j++) {
long long sum = 0;
for (int k = 1; k <= m; k++) {
data[k].insert(a[j][k]);
sum += *data[k].rbegin();
}
while (sum > k1) {
sum = 0;
for (int k = 1; k <= m; k++) {
data[k].erase(data[k].find(a[i][k]));
if (data[k].size()) sum += *data[k].rbegin();
}
i++;
}
if (len < j - i + 1) {
len = j - i + 1;
for (int k = 1; k <= m; k++) ans[k] = *data[k].rbegin();
}
}
for (auto i = 1; i <= m; i++) cout << ans[i] << " ";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) solve();
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T abs(T a) {
return a < 0 ? -a : a;
}
template <typename T>
T sqr(T a) {
return a * a;
}
const int INF = (int)1e9;
const long double EPS = 1e-9;
const long double PI = 3.1415926535897932384626433832795;
const int N = 2000000;
int n, m;
long long c[N];
int main() {
cin >> n >> m;
long long ans = (n * 1ll * (n - 1) * 1ll * (n - 2)) / 6;
for (int i = 0; i < int(m); ++i) {
int x, y;
scanf("%d %d", &x, &y);
x--;
y--;
ans -= n - c[x] - c[y] - 2;
c[x]++;
c[y]++;
}
cout << ans << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long bty[1000001];
int main() {
long long n, k;
cin >> n >> k;
long long sum = 0;
for (long long i = 1; i <= n; i++) {
cin >> bty[i];
sum += bty[i];
}
long long res = 0;
for (long long i = 1; i <= k; i++) {
long long x;
cin >> x;
sum -= bty[x];
res += sum * bty[x];
bty[x] = 0;
}
for (long long i = 1; i <= n; i++) {
res += bty[i] * bty[i + 1];
}
res += bty[1] * bty[n];
cout << res << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2e5 + 10;
int seg[4 * N], light[N];
bool seg2[4 * N];
pair<int, int> pos[N];
bool mark[N];
vector<int> adj[N], dfs;
int n;
int dfs_sort(int v) {
int sub = 1;
pos[v].first = dfs.size();
dfs.push_back(v);
mark[v] = true;
for (auto i : adj[v])
if (!mark[i]) sub += dfs_sort(i);
pos[v].second = sub;
return sub;
}
void make(int s = 0, int e = n, int x = 1) {
if (e == s + 1) {
seg[x] = light[dfs[s]];
return;
}
int q = (s + e) / 2;
make(s, q, 2 * x);
make(q, e, 2 * x + 1);
seg[x] = seg[2 * x] + seg[2 * x + 1];
}
int get(int l, int r, int s = 0, int e = n, int x = 1, bool rev = false) {
if (l <= s && e <= r) {
if (!rev) return seg[x];
return e - s - seg[x];
}
int q = (s + e) / 2, ret = 0;
if (l < q) ret += get(l, r, s, q, 2 * x, rev ^ seg2[x]);
if (q < r) ret += get(l, r, q, e, 2 * x + 1, rev ^ seg2[x]);
return ret;
}
void rev(int l, int r, int s = 0, int e = n, int x = 1) {
if (l <= s && e <= r) {
seg[x] = e - s - seg[x];
seg2[x] = !seg2[x];
return;
}
int q = (s + e) / 2;
if (l < q) rev(l, r, s, q, 2 * x);
if (q < r) rev(l, r, q, e, 2 * x + 1);
seg[x] = seg[2 * x] + seg[2 * x + 1];
if (seg2[x]) seg[x] = e - s - seg[x];
}
int main() {
cin >> n;
int x;
for (int i = 2; i <= n; i++) {
cin >> x;
adj[i].push_back(x);
adj[x].push_back(i);
}
dfs_sort(1);
for (int i = 1; i <= n; i++) cin >> light[i];
int q;
make();
cin >> q;
for (int i = 0; i < q; i++) {
char a, b, c;
int v;
cin >> a >> b >> c >> v;
if (a == 'g')
cout << get(pos[v].first, pos[v].first + pos[v].second) << endl;
else
rev(pos[v].first, pos[v].first + pos[v].second);
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
class abc {
public:
int n, k;
bool check(float);
void get();
};
bool abc::check(float f) {
if ((k - f) > 0.5)
return false;
else
return true;
}
void abc::get() {
cin >> n;
cin >> k;
float arr[100];
for (int i = 0; i < n; i++) cin >> arr[i];
float avg = 0;
float sum = 0;
int cnt = 0;
for (int i = 0; i < n; i++) sum += arr[i];
avg = sum / n;
bool res;
res = check(avg);
while (res == false) {
n++;
cnt++;
sum += k;
avg = sum / n;
res = check(avg);
}
cout << cnt;
}
int main() {
abc ab;
ab.get();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline int size(const T& t) {
return t.size();
}
int N;
int lewy[100001], prawy[100001], ojciec[100001];
int val[100001];
int korzen;
pair<int, int> wewn[100001];
int lm[100001], rm[100001];
int lmnr[100001];
int poz[100001];
long long suma[100001];
int ile_w = 0;
void szuk(int nr) {
nr = lower_bound(wewn, wewn + ile_w, make_pair(nr, 0)) - wewn - 1;
if (nr < 0)
nr = lmnr[korzen];
else {
nr = wewn[nr].second;
nr = lmnr[prawy[nr]];
}
double res = suma[nr];
res /= poz[nr];
printf("%.9f\n", res);
}
void top_down() {
queue<int> Q;
poz[korzen] = 0;
suma[korzen] = 0;
Q.push(korzen);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
if (lewy[x] < 0) continue;
poz[lewy[x]] = poz[prawy[x]] = poz[x] + 1;
suma[lewy[x]] = suma[x] + lm[prawy[x]];
suma[prawy[x]] = suma[x] + rm[lewy[x]];
Q.push(lewy[x]);
Q.push(prawy[x]);
}
}
void bottom_up() {
for (int a = 0; a < (N); ++a)
if (lewy[a] < 0) {
int b = a;
for (;;) {
lm[b] = val[a];
lmnr[b] = a;
if (ojciec[b] < 0 || lewy[ojciec[b]] != b) break;
b = ojciec[b];
}
b = a;
for (;;) {
rm[b] = val[a];
if (ojciec[b] < 0 || prawy[ojciec[b]] != b) break;
b = ojciec[b];
}
}
}
int main() {
scanf("%d", &N);
for (int a = 0; a < (N); ++a) {
scanf("%d%d", &ojciec[a], &val[a]);
if (ojciec[a] >= 0)
--ojciec[a];
else
korzen = a;
lewy[a] = prawy[a] = -1;
}
for (int a = 0; a < (N); ++a)
if (ojciec[a] >= 0) {
if (val[a] < val[ojciec[a]])
lewy[ojciec[a]] = a;
else
prawy[ojciec[a]] = a;
}
for (int a = 0; a < (N); ++a)
if (lewy[a] >= 0) {
wewn[ile_w].first = val[a];
wewn[ile_w].second = a;
++ile_w;
}
sort(wewn, wewn + ile_w);
bottom_up();
top_down();
int K;
scanf("%d", &K);
for (int a = 0; a < (K); ++a) {
int nr;
scanf("%d", &nr);
szuk(nr);
}
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} debug;
int n;
int main() {
do {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
} while (0);
int n, tmp;
int a[100005];
cin >> n;
for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
i += 1 - 2 * ((0) > (n))) {
cin >> tmp;
a[i] = tmp;
}
int i = 1, j = 0, k = 0, max_d = 0;
int valid[3] = {0, a[0], 0};
for (; i < n; ++i) {
if (a[i] == valid[1]) continue;
if ((a[i] == (valid[1] + 1)) && !valid[0]) {
valid[2] = a[i];
continue;
}
if ((a[i] == (valid[1] - 1)) && !valid[2]) {
valid[0] = a[i];
continue;
}
max_d = max(max_d, i - j);
j = i - 1;
while (abs(a[i] - a[j]) <= 1) {
--j;
}
++j;
valid[0] = (a[i] == a[j] - 1) ? a[i] : 0;
valid[1] = a[j];
valid[2] = (a[i] == a[j] + 1) ? a[i] : 0;
}
max_d = max(max_d, i - j);
cout << max_d << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
double a[N];
int b[N];
int n;
long long sum1, sum2;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%lf", &a[i]);
if (a[i] >= 0)
sum1 += a[i];
else
sum2 += a[i];
b[i] = a[i];
}
if (sum1 == abs(sum2)) {
for (int i = 0; i < n; i++) {
printf("%d\n", b[i]);
}
} else if (sum1 > abs(sum2)) {
for (int i = 0; i < n; i++) {
if (a[i] < 0 && fabs(a[i] - b[i]) != 0) {
b[i] -= 1;
sum2--;
}
if (sum1 == abs(sum2)) break;
}
for (int i = 0; i < n; i++) {
printf("%d\n", b[i]);
}
} else if (sum1 < abs(sum2)) {
for (int i = 0; i < n; i++) {
if (a[i] > 0 && fabs(a[i] - b[i]) != 0) {
b[i] += 1;
sum1++;
}
if (sum1 == abs(sum2)) break;
}
for (int i = 0; i < n; i++) {
printf("%d\n", b[i]);
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
int n = s.size();
vector<pair<char, int>> a;
for (int i = 0; i < n; i++) {
if (i > 0 && a.back().first == s[i]) {
a.back().second++;
} else {
a.push_back({s[i], 1});
}
}
int m = a.size();
if (m % 2 == 0) {
cout << 0 << "\n";
return 0;
}
bool flag = true;
flag = flag && (a[m / 2].second >= 2);
for (int i = 0; i < m / 2; i++) {
flag = flag && (a[i].first == a[m - 1 - i].first);
flag = flag && (a[i].second + a[m - 1 - i].second >= 3);
}
if (flag) {
cout << a[m / 2].second + 1 << "\n";
} else {
cout << 0 << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9, mxn = 100010;
int color[mxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
memset(color, -1, sizeof color);
int n, m;
cin >> n >> m;
while (m--) {
int A[3];
cin >> A[0] >> A[1] >> A[2];
A[0]--;
A[1]--;
A[2]--;
int i = 0;
while (i < 3) {
if (color[A[i]] != -1) {
color[A[(i + 1) % 3]] = (color[A[i]] + 1) % 3;
color[A[(i + 2) % 3]] = (color[A[i]] + 2) % 3;
break;
}
i++;
}
if (i == 3) {
for (int i = (0); i < (3); ++i) color[A[i]] = i;
}
}
for (int i = (0); i < (n); ++i) cout << color[i] + 1 << " ";
cout << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
stack<char> st;
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
if (!st.empty() && st.top() == s[i]) {
st.pop();
cnt++;
} else {
st.push(s[i]);
}
}
if (cnt & 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[100100];
bool cmp(const int &a, const int &b) { return a > b; }
int main() {
int n, n1, n2, i;
cin >> n >> n1 >> n2;
for (i = 0; i < n; i++) scanf("%d", a + i);
sort(a, a + n, cmp);
int tp = min(n1, n2);
double res = 0;
double sum = 0;
for (i = 0; i < tp; i++) sum += a[i];
res = sum / tp;
sum = 0;
for (i = tp; i < n1 + n2; i++) sum += a[i];
res += sum / (n1 + n2 - tp);
printf("%.8f\n", res);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 0.000000001;
int vp, vd, t, f;
double c;
vector<double> s1, s2;
bool isEqual(double a, double b) { return abs(a - b) < eps; }
bool isLesser(double a, double b) {
return (a < b) && (isEqual(a, b) == false);
}
int main() {
cin >> vp >> vd >> t >> f >> c;
if (vp >= vd) {
cout << 0 << endl;
return 0;
}
s1.push_back(0);
s2.push_back(vp * t);
int N = 1;
while (true) {
bool flag = false;
s1.push_back((double)(s2[N - 1] * vd) / (double)(vd - vp));
if (isLesser(s1[N], c) == false) {
flag = true;
}
if (flag == true) {
break;
}
s2.push_back(s1[N] + (double)vp * ((double)s1[N] / (double)vd + (double)f));
N++;
}
cout << N - 1 << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
int a[] = {0, 0, 1, 1, 1, 2, 1, 1, 14, 8, 4, 2, 41, 13, 21, 1, 22,
4, 19, 19, 4, 2, 1, 16, 13, 4, 35, 19, 4, 2, 41, 4, 87, 41,
115, 2, 59, 31, 49, 8, 28, 4, 31, 44, 50, 13, 151, 16, 91, 56, 14},
n;
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) printf("%d ", (long long)a[n] >> i & 1);
puts("1");
for (int i = 0; i < n; i++) printf("%d ", 1);
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 9;
long long sup[MAXN];
long long x[MAXN], y[MAXN], del[MAXN];
map<pair<long long, long long>, int> mp;
int be_supported(long long x, long long y) {
int cnt = 0;
for (int j = -1; j < 2; j++) {
long long nx = x + j, ny = y - 1;
int id = mp[make_pair(nx, ny)];
if (id == 0 || del[id]) continue;
cnt++;
}
return cnt;
}
bool check(long long x, long long y) {
for (int j = -1; j < 2; j++) {
long long nx = x + j, ny = y + 1;
int id = mp[make_pair(nx, ny)];
if (id == 0 || del[id]) continue;
if (be_supported(nx, ny) < 2) return false;
}
return true;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%I64d%I64d", x + i, y + i);
mp[make_pair(x[i], y[i])] = i;
}
del[0] = true;
set<int> que;
for (int i = 1; i <= n; i++)
for (int j = -1; j < 2; j++) {
long long nx = x[i] + j, ny = y[i] + 1;
if (mp[make_pair(nx, ny)]) sup[i]++;
}
for (int i = 1; i <= n; i++)
if (sup[i] == 0 || check(x[i], y[i])) que.insert(i);
long long ans = 0;
int cnt = 0;
while (!que.empty()) {
int i;
if (cnt % 2 == 0)
i = *que.rbegin(), que.erase(--que.end());
else
i = *que.begin(), que.erase(que.begin());
if (sup[i] == 0 || check(x[i], y[i])) {
ans = (ans * n + i - 1) % MOD;
del[i] = true;
for (int j = -1; j < 2; j++) {
long long nx = x[i] + j, ny = y[i] - 1;
int id = mp[make_pair(nx, ny)];
if (id && !del[id])
if ((--sup[id]) == 0 || check(x[id], y[id])) que.insert(id);
}
cnt++;
}
}
cout << ans << endl;
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int uf[2001];
int find(int a) {
if (uf[a] == a)
return a;
else
return uf[a] = find(uf[a]);
}
void uni(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
uf[a] = b;
}
int main(void) {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n;
int x[2001];
int y[2001];
int c[2001];
int k[2001];
vector<pair<long long, pair<int, int> > > edges;
vector<int> p;
vector<pair<int, int> > e;
cin >> n;
for (int i = 1; i <= n; i++) uf[i] = i;
for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
for (int i = 1; i <= n; i++) cin >> c[i];
for (int i = 1; i <= n; i++) cin >> k[i];
for (int i = 1; i <= n; i++) edges.push_back({c[i], {0, i}});
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
edges.push_back(
{1LL * (abs(x[i] - x[j]) + abs(y[i] - y[j])) * (k[i] + k[j]),
{i, j}});
sort(edges.begin(), edges.end());
long long res = 0;
for (int i = 0; i < edges.size(); i++) {
long long cost = edges[i].first;
int a = edges[i].second.first;
int b = edges[i].second.second;
if (find(a) == find(b)) continue;
uni(a, b);
res += cost;
if (a == 0)
p.push_back(b);
else if (b == 0)
p.push_back(a);
else
e.push_back({a, b});
}
cout << res << '\n';
cout << p.size() << '\n';
for (int i = 0; i < p.size(); i++) cout << p[i] << ' ';
cout << '\n';
cout << e.size() << '\n';
for (int i = 0; i < e.size(); i++)
cout << e[i].first << ' ' << e[i].second << '\n';
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long double n, m, k, l;
cin >> n >> m >> k >> l;
long double up = (l + k);
long double x = up / m;
long long int val = ceil(x);
long long int val2 = m * val;
if (val2 > n) {
puts("-1");
} else {
printf("%lld\n", val);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n + 1];
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
int cnt = 0;
int final[n + 1];
int k = 0;
for (int i = 1; i <= n; i++) {
if (arr[i] == 1) {
cnt++;
final[k++] = arr[i - 1];
}
}
final[k++] = arr[n];
cout << cnt << endl;
for (int i = 1; i < k; i++) {
cout << final[i] << " ";
}
cout << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) {
if (y)
return gcd(y, x % y);
else
return x;
}
int c[1000000][26];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, x, y, i, j, g, l;
string a, b;
cin >> n >> n >> a >> b;
x = a.size();
y = b.size();
g = gcd(x, y);
l = x / g * y;
n = n * y / l;
for (i = 0; i < g; i++)
for (j = 0; j < 26; j++) c[i][j] = 0;
for (i = 0; i < x; i++) c[i % g][a[i] - 97]++;
for (i = 0; i < y; i++) l -= c[i % g][b[i] - 97];
cout << n * l;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long c[200][200], k, dp[200][20], n;
int status[20], ans[200];
int tim;
long long calc(int len) {
memset(dp, 0, sizeof(dp));
for (int i = 0; i <= min(len, status[0]); i++) dp[i][0] = 1;
for (int i = 0; i <= len; i++) {
for (int j = 1; j < 16; j++) {
for (int k = 0; k <= min(i, status[j]); k++) {
dp[i][j] += dp[i - k][j - 1] * c[i][k];
}
}
}
return dp[len][15];
}
int main() {
long long tmpk;
scanf("%I64d%d", &k, &tim);
n = tim * 16;
for (int i = 0; i <= n; i++) {
c[i][0] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
}
int len;
tmpk = k;
for (int i = 0; i < 16; i++) status[i] = tim;
status[1]--;
calc(n - 1);
status[1]++;
for (int i = 1; i <= n; i++) {
long long res = 15LL * dp[i - 1][15];
if (res >= tmpk) {
len = i;
break;
} else
tmpk -= res;
}
k = tmpk;
for (int i = 1; i <= len; i++) {
long long res = 0;
for (int j = 1 - (i != 1); j < 16; j++) {
if (status[j]) {
status[j]--;
long long tmp = calc(len - i);
if (res + tmp < k) {
res += tmp;
} else {
ans[i] = j;
break;
}
status[j]++;
}
}
k -= res;
}
for (int i = 1; i <= len; i++) {
if (ans[i] < 10)
printf("%d", ans[i]);
else
printf("%c", 'a' + ans[i] - 10);
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
inline long long max3(long long a, long long b, long long c) {
return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long min3(long long a, long long b, long long c) {
return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
int main() {
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
vector<pair<long long int, long long int>> v2;
for (long long int(i) = 0; (i) < (n); (i)++) {
long long int temp;
cin >> temp;
v2.push_back(make_pair(temp, i + 1));
}
long long int flag = 0;
long long int p = v2.size();
long long int mini = 1e18;
sort(v2.begin(), v2.end());
for (long long int(i) = 0; (i) < (p - 1); (i)++) {
if (v2[i].first == v2[i + 1].first) {
flag = 1;
long long int r = llabs(v2[i].second - v2[i + 1].second);
mini = ((mini > r) ? r : mini);
}
}
if (flag != 1 || n == 1)
cout << "-1" << endl;
else
cout << mini + 1 << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 1;
int n, k, cnt, mark[322], mx, m, uncp, unct, l;
string s, was;
char flag;
pair<int, char> pp[M];
int main() {
cin >> s >> k;
n = (int)s.size();
if (k >= n) {
cout << 0 << "\n";
cout << "";
return 0;
}
for (int i = 0; i < n; i++) {
mark[(int)s[i]]++;
}
for (int i = 'a'; i <= 'z'; i++) {
if (mark[i] != 0) pp[cnt++] = make_pair(mark[i], i);
}
sort(pp, pp + cnt);
for (int i = 0; i <= cnt; i++) {
if (pp[i].first <= k) {
k -= pp[i].first;
was += pp[i].second;
} else {
unct = pp[i].first - k;
uncp = i;
break;
}
}
cerr << was;
cnt = 0;
l = 0;
for (int i = 0; i < n; i++) {
if (s[i] == uncp && unct) {
s[i] = '.';
unct--;
} else if (was.find(s[i]) != string::npos) {
s[i] = '.';
}
}
memset(mark, 0, sizeof(mark));
for (int i = 0; i < n; i++) {
if (!mark[(int)s[i]] && s[i] != '.') m++;
mark[(int)s[i]] = 1;
}
cout << m << "\n";
for (int i = 0; i < n; i++) {
if (s[i] != '.') cout << s[i];
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
long long mpow(long long a, long long n, long long mod) {
long long ret = 1;
long long b = a;
while (n) {
if (n & 1) ret = (ret * b) % mod;
b = (b * b) % mod;
n >>= 1;
}
return (long long)ret;
}
inline void read(long long &x) {
x = 0;
register char c = getchar();
for (; c < '0' || c > '9'; c = getchar())
;
for (; c >= '0' && c <= '9'; c = getchar())
x = (x << 3) + (x << 1) + (c - '0');
}
inline void write(long long x) {
register char buffor[35];
register long long i = 0;
do {
buffor[i++] = (x % 10) + '0';
x /= 10;
} while (x);
i--;
while (i >= 0) putchar(buffor[i--]);
putchar('\n');
}
inline long long in() {
long long x;
read(x);
return x;
}
void solve();
long long dbg = 1;
using namespace std;
int main() {
long long t = 1;
for (long long i = 0; i < t; i++) {
solve();
}
return 0;
}
long long arr[5005];
char c[5005];
long long mod = 1e9 + 7;
void solve() {
long long n;
cin >> n;
arr[0] = 1;
for (long long i = 0; i < n; i++) cin >> c[i];
for (long long i = 0; i < n; i++) {
if (i && c[i - 1] == 'f') {
for (long long j = n; j > 0; j--) arr[j] = arr[j - 1];
arr[0] = 0;
} else {
for (long long j = n - 1; j >= 0; j--) {
arr[j] += arr[j + 1];
arr[j] %= mod;
}
}
}
for (long long j = n - 1; j >= 0; j--) {
arr[j] += arr[j + 1];
arr[j] %= mod;
}
cout << arr[0] << endl;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int start;
int end;
int sec;
int d;
cin >> start >> end >> sec >> d;
int a = max(start, end);
int b = min(start, end);
int m = sec - 2;
int sum = a + b;
for (int i = 1; i <= sec - 2; i++) {
a += d;
end:
if (a - (m * d) > b) {
a--;
goto end;
}
m--;
sum += a;
}
cout << sum;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MXN = (int)1e5 + 7;
const int MOD = (int)1e9 + 7;
int n, k;
long long num = 1, pref, ans;
vector<long long> v;
map<long long, int> kek;
int main() {
cin >> n >> k;
if (k == -1) v.push_back(-1);
v.push_back(1);
if (abs(k) > 1) {
while (abs(num) < 1e14) {
num *= k;
v.push_back(num);
}
}
kek[0] = 1;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
pref += x;
for (long long j : v) {
long long need = (j - pref) * -1;
ans += kek[need];
}
kek[pref]++;
}
cout << ans;
return 0;
}
| 10 |
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef long int li;
typedef long long ll;
typedef long double ld;
typedef vector<lli> vlli;
typedef vector<li> vli;
typedef vector<int> vi;
typedef vector<ld> vld;
#define all(x) (x).begin(),(x).end()
lli MOD=1e9+7;
/*vlli fun(lli q)
{
vlli ret;
for(lli i = 2; i*i <= q; i++)
{
if( q % i==0)
{
if(i<=q/i)
{
ret.push_back(i);
break;
}
else
break;
}
}
sort(ret.rbegin(),ret.rend());
return ret;
}*/
/*int prime(int a)
{
for(lli i=2; i<=sqrt(a); i++)
{
if(a%i==0)
return 0;
}
return 1;
}*/
//lli fac[300001] ;
/*
lli power(lli x, lli y, lli MOD)
{
if(y == 0) return 1 ;
lli p = power(x, y/2, MOD) % MOD ;
p = (p * p) % MOD ;
return (!(y & 1))? p : (x * p) % MOD ;
}
lli modInverse(lli a, lli MOD)
{
return power(a, MOD - 2, MOD) ;
}
lli nCr(lli n,lli r)
{
if(n < r) return 0 ;
else if(r == 0) return 1 ;
lli ret{fac[n]%MOD} ;
ret *= modInverse(fac[r], MOD) ;
ret %= MOD ;
ret *= modInverse(fac[n-r], MOD) ;
ret %= MOD ;
return ret ;
}*/
/*lli n=50000000;/range of number;
vlli c;
void sieve()
{
vector<bool>is_prime(n+1,true);
for(lli i=2; i<=n; i++)
{
if(is_prime[i]==true)
{
is_prime[i]=false;
c.push_back(i);
for(lli j=i*i; j<=n; j+=i)
{
is_prime[j]=false;
}
}
}
}
*/
int main()
{
//cin.tie(nullptr)->sync_with_stdio(false) ;
lli t{1};
cin>>t;
cout<<setprecision(15);
// fac[0] = 1;
/*for(lli i{1} ; i <= 300000 ; i++)
fac[i] = (fac[i-1]*i)%MOD ;*/
while(t--)
{
lli n{};
cin >> n;
vlli a(n);
for(lli i = 0; i < n; i++)
cin >> a[i];
lli cnt1=count(a.begin(),a.end(),1);
lli cnt3=count(a.begin(),a.end(),3);
lli cnt2=n-cnt1-cnt3;
cout << max(cnt1+cnt3,lli(0))<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int angles[190];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int x;
cin >> x;
int ans = -1;
for (int i = 3; i <= 360; i++) {
int g = x * i;
if (g % 180 == 0) {
g /= 180;
if (g > i - 2) continue;
ans = i;
break;
}
}
assert(ans != -1);
cout << ans << "\n";
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int maxn = 3e5 + 10;
struct segment {
int l, r, val;
} t[maxn * 4];
long long a[maxn];
int v[maxn];
int n, m;
int sign(long long x) {
if (x > 0) return 1;
if (x < 0)
return -1;
else
return 0;
}
void cal(int rt, int l, int r) {
int mid = (l + r) / 2;
t[rt].val = std::max(t[rt << 1].val, t[rt << 1 | 1].val);
t[rt].l = t[rt << 1].l;
t[rt].r = t[rt << 1 | 1].r;
if (!!a[mid] && !!a[mid + 1] && sign(a[mid]) >= sign(a[mid + 1])) {
t[rt].val = std::max(t[rt].val, t[rt << 1].r + t[rt << 1 | 1].l);
if (t[rt << 1].val == mid - l + 1) t[rt].l = t[rt << 1 | 1].l + mid - l + 1;
if (t[rt << 1 | 1].val == r - mid) t[rt].r = t[rt << 1].r + r - mid;
}
}
void build(int rt, int l, int r) {
if (l == r) {
int temp = !!a[l];
t[rt].val = temp, t[rt].l = temp, t[rt].r = temp;
return;
}
int mid = (l + r) / 2;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
cal(rt, l, r);
}
void update(int rt, int l, int r, int id, int d) {
if (l == r) {
a[id] += d;
int temp = !!a[id];
t[rt].val = temp, t[rt].l = temp, t[rt].r = temp;
return;
}
int mid = (l + r) / 2;
if (mid >= id)
update(rt << 1, l, mid, id, d);
else
update(rt << 1 | 1, mid + 1, r, id, d);
cal(rt, l, r);
}
int ll, rr, dd;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", v + i);
for (int i = 1; i < n; i++) a[i] = v[i + 1] - v[i];
if (n != 1) build(1, 1, n - 1);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &ll, &rr, &dd);
if (n == 1) {
printf("1\n");
continue;
}
if (ll > 1) update(1, 1, n - 1, ll - 1, dd);
if (rr < n) update(1, 1, n - 1, rr, -dd);
printf("%d\n", t[1].val + 1);
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long inf = 2e18 + 42;
long long a[N], skip[N];
int main() {
int n;
long long k;
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
long long ans = 0;
for (int i = n; i >= 1; --i) {
if (a[i] == 1) {
skip[i] = skip[i + 1] + 1;
}
}
for (int i = 1; i <= n; ++i) {
long long sum = 0, prod = 1;
for (int j = i; j <= n; ++j) {
if (double(prod) * a[j] > inf) {
break;
}
prod *= a[j];
sum += a[j];
if (prod == sum * k) {
++ans;
}
if (skip[j] > 1) {
if (prod % k == 0) {
long long temp = prod / k;
if (sum < temp and temp <= sum + skip[j] - 1) {
++ans;
}
}
sum += skip[j] - 1;
j += skip[j] - 1;
}
}
}
cout << ans;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
bool f = false;
int i;
string s;
while (getline(cin, s)) {
bool t = false;
for (i = 0; i < s.size(); i++)
if (s[i] != ' ')
if (s[i] == '#') {
t = true;
break;
} else
break;
if (t) {
if (f) {
cout << endl;
f = false;
}
cout << s << endl;
} else {
int l = 0;
for (i = 0; i < s.size(); i++) {
while (l < s.size() && s[l] != ' ') l++;
if (l < i && s[l] == ' ' && s[i] != ' ') {
s[l] = s[i];
l++;
s[i] = ' ';
}
}
s.erase(l, s.size() - l);
cout << s;
f = true;
}
}
if (f) cout << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100010, MOD = 1e9 + 7;
int m;
int f[NMAX];
vector<int> fact[NMAX];
int prime[NMAX], p_cnt;
set<int> pri[NMAX];
int prime_init() {
bitset<NMAX> flag;
flag.reset();
for (int i = 2; i < NMAX; i++) {
if (!flag[i]) {
prime[p_cnt++] = i;
}
for (int j = 0; j < p_cnt; j++) {
if (i * prime[j] >= NMAX) break;
flag[i * prime[j]] = true;
if (i % prime[j] == 0) break;
}
}
for (int i = 1; i < NMAX; i++) {
for (int j = i * 2; j < NMAX; j += i) {
fact[j].push_back(i);
}
if (flag[i] == false && i != 1) {
for (int j = i; j < NMAX; j += i) {
pri[j].insert(i);
}
}
}
return 0;
}
int gcd(int x, int y) {
if (y) return gcd(y, x % y);
return x;
}
int Power(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1LL * ret * x % MOD;
x = 1LL * x * x % MOD;
y >>= 1;
}
return ret;
}
int inv(int x) { return Power(x, MOD - 2); }
int calc(int a, int b) {
int p[10], cnt = 0;
int tmpa = a / b;
cnt = pri[tmpa].size();
auto iter = pri[tmpa].begin();
for (int i = 0; i < cnt; i++) {
p[i] = *iter;
iter++;
}
int sum = 0;
for (int i = 0; i < (1 << cnt); i++) {
int mult = 1, sign = 1;
for (int j = 0; j < cnt; j++) {
if (i & (1 << j)) {
mult *= p[j];
sign *= -1;
}
}
sum += (m / b) / mult * sign;
}
return sum;
}
int main() {
prime_init();
scanf("%d", &m);
f[1] = 1;
for (int i = 2; i <= m; i++) {
int sum = 0;
for (int j : fact[i]) {
sum = (sum + 1LL * f[j] * calc(i, j) % MOD * inv(m) % MOD) % MOD;
}
f[i] = 1LL * (1 + sum) * (1LL * m * inv(m - calc(i, i)) % MOD) % MOD;
}
int sum = 0;
for (int i = 1; i <= m; i++) sum = (sum + 1LL * f[i] * inv(m) % MOD) % MOD;
printf("%d\n", sum);
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
return x * x;
}
const int inf = ~0u >> 1;
const double eps = 1e-8;
const double pi = acos(-1.0);
int sgn(double x) { return fabs(x) < eps ? 0 : x < -eps ? -1 : 1; }
void rotate(long long &x, long long &y, int p) {
if (p == 1) {
swap(x, y);
x = -x;
} else if (p == 2) {
x = -x;
y = -y;
} else if (p == 3) {
swap(x, y);
y = -y;
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long x[4], y[4], a[4], b[4], vx[4], vy[4], vxr[4], vyr[4], nx[4],
ny[4], ex[4], ey[4];
for (int j = 0; j < 4; j++) {
cin >> x[j] >> y[j] >> a[j] >> b[j];
vx[j] = x[j] - a[j];
vy[j] = y[j] - b[j];
}
int ans = -1;
for (int p = 0; p < 4; p++) {
for (int q = 0; q < 4; q++) {
for (int r = 0; r < 4; r++) {
for (int s = 0; s < 4; s++) {
for (int k = 0; k < 4; k++) {
vxr[k] = vx[k];
vyr[k] = vy[k];
}
rotate(vxr[0], vyr[0], p);
rotate(vxr[1], vyr[1], q);
rotate(vxr[2], vyr[2], r);
rotate(vxr[3], vyr[3], s);
for (int k = 0; k < 4; k++) {
nx[k] = a[k] + vxr[k];
ny[k] = b[k] + vyr[k];
}
bool flag = true;
for (int k = 0; k < 4 && flag; k++) {
for (int nk = k + 1; nk < 4 && flag; nk++) {
if (nx[k] == nx[nk] && ny[k] == ny[nk]) {
flag = false;
}
}
}
ex[0] = ey[0] = 0;
for (int k = 1; k < 4 && flag; k++) {
ex[k] = nx[k] - nx[0];
ey[k] = ny[k] - ny[0];
}
int corner = 0;
for (int k = 1; k < 4 && flag; k++) {
if (sqr(ex[k]) + sqr(ey[k]) == 0) {
flag = false;
}
if (sqr(ex[corner]) + sqr(ey[corner]) < sqr(ex[k]) + sqr(ey[k])) {
corner = k;
}
}
vector<pair<long long, long long> > nodes(4);
nodes[0] = make_pair(nx[0], ny[0]);
nodes[2] = make_pair(nx[corner], ny[corner]);
int idx = 1;
for (int k = 1; k < 4 && flag; k++) {
if (k != corner) {
nodes[idx] = make_pair(nx[k], ny[k]);
idx += 2;
}
}
for (int k = 0; k < 4 && flag; k++) {
int nk = (k + 1) % 4;
ex[k] = nodes[nk].first - nodes[k].first,
ey[k] = nodes[nk].second - nodes[k].second;
}
for (int k = 0; k < 4 && flag; k++) {
int nk = (k + 1) % 4;
if (ex[k] * ex[nk] + ey[k] * ey[nk] != 0 ||
sqr(ex[k]) + sqr(ey[k]) != sqr(ex[nk]) + sqr(ey[nk])) {
flag = false;
}
}
if (flag) {
if (ans == -1 || ans > p + q + r + s) {
ans = p + q + r + s;
}
}
}
}
}
}
printf("%d\n", ans);
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 300003;
int n, q, p;
vector<int> g[MAX];
int cnt[MAX], centroid[MAX], parent[MAX];
int dfs1(int u) {
int c = 1;
for (int i = 0; i < g[u].size(); i++) c += dfs1(g[u][i]);
cnt[u] = c;
return c;
}
bool iscentroid(int u, int y) {
if (cnt[u] % 2 == 0 && cnt[y] >= cnt[u] / 2)
return true;
else if (cnt[u] % 2 == 1 && cnt[y] >= cnt[u] / 2 + 1)
return true;
return false;
}
int dfs2(int u) {
int x = u;
for (int i = 0; i < g[u].size(); i++) {
int y = dfs2(g[u][i]);
while (!iscentroid(u, y)) y = parent[y];
if (cnt[x] > cnt[y]) x = y;
}
centroid[u] = x;
return x;
}
int main() {
cin >> n >> q;
for (int i = 2; i <= n; i++) {
cin >> p;
g[p].push_back(i);
parent[i] = p;
}
dfs1(1);
for (int i = 1; i <= n; i++) centroid[i] = i;
dfs2(1);
for (int i = 1; i <= q; i++) {
cin >> p;
cout << centroid[p] << endl;
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const long double pi = acos(-1);
const int MOD = 1e9 + 7;
int g[22];
int n, m;
int dp[1 << 22];
int MAXI;
int solve(int mask) {
if (mask == MAXI) return 0;
if (dp[mask] != -1) return dp[mask];
int ret = INF;
for (int i = 0; i < n; i++) {
if ((1 << i) & mask) {
int nmask = mask | g[i];
if (nmask != mask) ret = min(ret, solve(nmask) + 1);
}
}
return dp[mask] = ret;
}
vector<int> r;
void rec(int mask) {
for (int i = 0; i < n; i++) {
if ((1 << i) & mask) {
int nmask = mask | g[i];
if (nmask != mask and solve(nmask) + 1 == solve(mask)) {
r.push_back(i + 1);
rec(nmask);
return;
}
}
}
}
int main() {
scanf("%d %d", &n, &m);
MAXI = (1 << n) - 1;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--, v--;
g[u] |= (1 << v);
g[v] |= (1 << u);
}
for (int i = 0; i < n; i++) g[i] |= (1 << i);
memset(dp, -1, sizeof(dp));
int resp = INF;
if (2 * m == (n * (n - 1))) {
printf("%d\n", 0);
return 0;
}
int idx = -1;
for (int i = 0; i < n; i++)
if (solve(1 << i) < resp) resp = solve(1 << i), idx = i;
printf("%d\n", resp);
rec(1 << idx);
for (int x : r) printf("%d\n", x);
return 0;
}
| 16 |
#include <bits/stdc++.h>
int val[262149];
int main() {
int n, r;
scanf("%d%d", &n, &r);
long long sum(0LL);
for (int i = 0; i < (1 << n); i++) {
scanf("%d", &val[i]);
sum += val[i];
}
printf("%.10lf\n", (sum * 1.0) / (1 << n));
for (int i = 0; i < r; i++) {
int a, b;
scanf("%d%d", &a, &b);
sum += b - val[a];
printf("%.10lf\n", (sum * 1.0) / (1 << n));
val[a] = b;
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MAX = 1005;
const int MASK = 130;
int ct1[MASK];
int ct2[MASK];
int dig = 0;
int dig2 = 0;
int f(int n, int mode) {
vector<int> v;
if (n == 0) {
v.push_back(0);
}
while (n > 0) {
v.push_back(n % 7);
n /= 7;
}
int mask = 0;
int sz = v.size();
if (mode == 1) {
for (int i = 0; i < dig - sz; i++) {
v.push_back(0);
}
}
if (mode == 2) {
for (int i = 0; i < dig2 - sz; i++) {
v.push_back(0);
}
}
for (int i : v) {
if (mask & (1 << i)) {
return 129;
}
mask = mask | (1 << i);
}
return mask;
}
int sz(int n) {
vector<int> v;
if (n == 0) {
v.push_back(0);
}
while (n > 0) {
v.push_back(n % 7);
n /= 7;
}
return v.size();
}
int ok(int m1, int m2) {
for (int i = 0; i < 7; i++) {
if ((m1 & (1 << i)) && (m2 & (1 << i))) {
return false;
}
}
return true;
}
int main() {
long long tot = 0;
int n, m;
cin >> n >> m;
dig = sz(n - 1);
dig2 = sz(m - 1);
for (int i = 0; i < min(1000000, n); i++) {
ct1[f(i, 1)]++;
}
for (int i = 0; i < min(1000000, m); i++) {
ct2[f(i, 2)]++;
}
for (int i = 0; i < 1 << 7; i++) {
for (int j = 0; j < 1 << 7; j++) {
if (!ok(i, j)) {
continue;
}
long long add = (long long)ct1[i] * (long long)ct2[j];
tot += add;
}
}
cout << tot << endl;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int a[100000], b[100000];
int main() {
int n, x, w = 0;
scanf("%d%d", &n, &x);
for (int i = 0; i < n; ++i) scanf("%d", a + i);
for (int i = 0; i < n; ++i) scanf("%d", b + i);
sort(a, a + n);
sort(b, b + n);
int l = n - 1, h = 0;
while (l != -1 && h != n) {
if (a[l] + b[h] >= x) {
++w;
++h;
--l;
} else
++h;
}
printf("1 %d", w);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
std::vector<long long int> v;
long long int arr[300005];
long long int sum = 0;
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
if (i == 1) continue;
long long int dif = arr[i] - arr[i - 1];
v.push_back(dif);
sum += dif;
}
int cnt = 0;
sort(v.begin(), v.end());
for (int i = v.size() - 1; cnt < k - 1 && i >= 0; i--) {
sum -= v[i];
cnt++;
}
cout << sum << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
const double eps = 1e-9;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int N, M, K;
char str[maxn];
int main() {
scanf("%s", str);
int ans = strlen(str);
int l = strlen(str);
for (int i = 0; str[i]; i++) {
if (str[i] == 'a') ans++;
}
bool flag = 1;
if (ans & 1)
flag = 0;
else {
ans /= 2;
int cnt = ans;
for (int i = 0; i < ans && flag; i++) {
if (str[i] == 'a') continue;
if (cnt < l && str[i] == str[cnt]) {
cnt++;
continue;
}
flag = 0;
}
if (cnt < l) flag = 0;
}
if (flag)
for (int i = 0; i < ans; i++) printf("%c", str[i]);
else
puts(":(");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T gcd(T a, T b) {
if (b) return gcd(b, a % b);
return a;
}
template <typename T>
inline T ext_gcd(T a, T b, T& x, T& y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
T x1, y1;
T g = ext_gcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return g;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long a, b, c;
cin >> a >> b >> c;
long long x, y;
long long g = ext_gcd(a, b, x, y);
long long change = (c % g != 0) ? -1 : -c / g;
if (c % g)
cout << -1 << '\n';
else
cout << x * change << " " << y * change << '\n';
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Pii = pair<int, int>;
int uplg(int n) { return 32 - __builtin_clz(n); }
int uplg(ll n) { return 64 - __builtin_clzll(n); }
void run();
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(18);
run();
return 0;
}
void run() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
bool ok = 1;
for (int i = (k); i < (n); i++) {
int j = i % k;
if (s[i] != '?') {
if (s[j] != '?' && s[i] != s[j]) {
ok = 0;
break;
}
s[j] = s[i];
}
}
if (ok) {
int cnt[2] = {};
for (int i = (0); i < (k); i++)
if (s[i] != '?') cnt[s[i] - '0']++;
if (cnt[0] > k / 2 || cnt[1] > k / 2) {
ok = 0;
}
}
cout << (ok ? "YES\n" : "NO\n");
}
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int maxn = 2e5;
const int INF = 1e9;
const long long MOD = 998244353;
const int base = 31;
const int UP = 1e8;
long long n, m;
long long dp[maxn + 5];
struct EuclidReturn {
long long u, v, d;
EuclidReturn(long long _u, long long _v, long long _d) {
u = _u;
v = _v;
d = _d;
}
};
EuclidReturn Extended_Euclid(long long a, long long b) {
if (b == 0) return EuclidReturn(1, 0, a);
EuclidReturn aux = Extended_Euclid(b, a % b);
long long v = aux.u - (a / b) * aux.v;
return EuclidReturn(aux.v, v, aux.d);
}
long long modular_inverse(long long a) {
EuclidReturn aux = Extended_Euclid(a, MOD);
return ((aux.u / aux.d) % MOD + MOD) % MOD;
}
long long mulmod(long long a, long long b, long long c) {
long long x = 0, y = a % c;
while (b > 0) {
if (b & 1) x = (x + y) % c;
y = (y << 1) % c;
b = b >> 1;
}
return x;
}
long long pow(long long a, long long b, long long c) {
long long x = 1, y = a;
while (b > 0) {
if (b & 1) x = mulmod(x, y, c);
y = mulmod(y, y, c);
b >>= 1;
}
return x;
}
void init() {}
void Time_Decreasing() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void enter() { scanf("%lld %lld", &n, &m); }
void solve() {
if (n == 2) {
printf("0");
return;
}
dp[0] = 1;
for (int i = 1; i <= m; i++) dp[i] = mulmod(dp[i - 1], i, MOD);
long long res = dp[m];
res = mulmod(res, modular_inverse(dp[m - n + 1]), MOD);
res = mulmod(res, modular_inverse(dp[n - 1]), MOD);
res = mulmod(res, n - 2, MOD);
res = mulmod(res, pow(2, n - 3, MOD), MOD);
printf("%lld", res);
}
int main() {
init();
Time_Decreasing();
enter();
solve();
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 100005;
int n, b[maxn], mapping[maxn];
int C[maxn];
int lowbit(int x) { return x & (-x); }
void update(int i, int k) {
while (i <= n) {
C[i] += k;
i += lowbit(i);
}
}
int getsum(int i) {
int res = 0;
while (i > 0) {
res += C[i];
i -= lowbit(i);
}
return res;
}
int main() {
cin >> n;
int x;
for (int i = 1; i <= n; i++) {
cin >> x;
mapping[x] = i;
}
for (int i = 1; i <= n; i++) {
cin >> x;
b[i] = mapping[x];
}
int cnt = 0;
for (int i = n; i >= 1; i--) {
if (getsum(b[i]) != 0) cnt++;
update(b[i], 1);
}
cout << cnt << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
struct edge {
int t, next;
} g[410001];
long long ans;
int i, j, k, n, c1, c2, h[210001], tot, dep[210001], md, f[210001][2], d2;
void addedge(int x, int y) {
g[++tot].t = y;
g[tot].next = h[x];
h[x] = tot;
g[++tot].t = x;
g[tot].next = h[y];
h[y] = tot;
}
void work(int x, int fa) {
int i, t1, t2, k = 0, s = 0, j;
md = max(md, dep[x]);
if (dep[x] == 2) d2++;
for (i = h[x], t1 = t2 = inf; i; i = g[i].next) {
if (fa == g[i].t) continue;
dep[g[i].t] = dep[x] + 1;
k++;
work(g[i].t, x);
s += min(f[g[i].t][0], f[g[i].t][1]);
j = f[g[i].t][1] - min(f[g[i].t][0], f[g[i].t][1]);
if (j < t1)
t2 = t1, t1 = j;
else
t2 = min(t2, j);
}
if (!k) {
f[x][0] = f[x][1] = 1;
return;
}
if (k == 1) {
f[x][0] = f[x][1] = s + min(1, t1);
return;
}
f[x][0] = s + t1 + t2 - 1;
f[x][1] = min(1 + s, s + t1);
}
int main() {
scanf("%d%d%d", &n, &c1, &c2);
tot = 0;
memset(h, 0, sizeof(h));
for (i = 1; i < n; i++) scanf("%d%d", &j, &k), addedge(j, k);
dep[1] = 1;
md = d2 = 0;
work(1, 0);
if (c1 <= c2) {
k = min(f[1][0], f[1][1]);
ans =
(long long)(k - 1) * (long long)c2 + (long long)(n - k) * (long long)c1;
} else {
ans = (long long)(n - 1) * (long long)c2;
if (md == 2 || md == 3 && d2 == 1) ans += c1 - c2;
}
cout << ans << endl;
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
if (n == 3) {
cout << 1;
return 0;
}
if (n == 1) {
cout << 2;
return 0;
}
if (n == 4) {
cout << 2;
return 0;
}
if (n == 2) {
cout << 3;
return 0;
}
if (n == 5) {
cout << 1;
return 0;
}
while (1)
;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int MAXN(5e5 + 100);
struct Node {
int left, right, val, lazy;
} tree[MAXN << 2];
int n, nx[MAXN], in[MAXN], ans[MAXN];
vector<int> G[MAXN];
inline int read() {
int f = 1, x = 0;
char ch;
do {
ch = getchar();
if (ch == '-') f = -1;
} while (ch < '0' || ch > '9');
do {
x = x * 10 + ch - '0';
ch = getchar();
} while (ch >= '0' && ch <= '9');
return f * x;
}
void pushdown(int v) {
if (tree[v].lazy) {
tree[(v << 1)].val = tree[(v << 1)].lazy = tree[v].lazy;
tree[(v << 1 | 1)].val = tree[(v << 1 | 1)].lazy = tree[v].lazy;
}
tree[v].lazy = 0;
}
void build(int l, int r, int v) {
tree[v].left = l;
tree[v].right = r;
tree[v].val = tree[v].lazy = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(l, mid, (v << 1));
build(mid + 1, r, (v << 1 | 1));
}
void update(int l, int r, int v, int x) {
if (l == tree[v].left && r == tree[v].right) {
tree[v].val = tree[v].lazy = x;
return;
}
pushdown(v);
if (l <= tree[(v << 1)].right) {
if (r <= tree[(v << 1)].right)
update(l, r, (v << 1), x);
else
update(l, tree[(v << 1)].right, (v << 1), x);
}
if (r >= tree[(v << 1 | 1)].left) {
if (l >= tree[(v << 1 | 1)].left)
update(l, r, (v << 1 | 1), x);
else
update(tree[(v << 1 | 1)].left, r, (v << 1 | 1), x);
}
}
int query(int v, int pos) {
while (tree[v].left != tree[v].right) {
pushdown(v);
if (pos <= tree[(v << 1)].right)
v = (v << 1);
else
v = (v << 1 | 1);
}
return tree[v].val;
}
void addedge(int u, int v) {
G[u].push_back(v);
in[v]++;
}
bool toposort() {
int ct = 0;
queue<int> que;
for (int i = 1; i <= n; i++)
if (!in[i]) que.push(i);
while (!que.empty()) {
int u = que.front();
que.pop();
ans[u] = ++ct;
for (int v : G[u]) {
in[v]--;
if (!in[v]) que.push(v);
}
}
for (int i = 1; i <= n; i++)
if (!ans[i] && !in[i]) ans[i] = ++ct;
return ct == n;
}
void init() {
n = read();
fill(in, in + n + 10, 0);
fill(ans, ans + n + 10, 0);
for (int i = 1; i <= n; i++) {
nx[i] = read();
G[i].clear();
}
build(1, n, 1);
}
void solve() {
for (int i = 1; i <= n; i++) {
if (nx[i] - i >= 2) update(i + 1, nx[i] - 1, 1, i);
if (nx[i] != -1 && nx[i] <= n) addedge(i, nx[i]);
int pre = query(1, i);
if (pre) addedge(i, pre);
}
if (toposort())
for (int i = 1; i <= n; i++) printf("%d%c", ans[i], i < n ? ' ' : '\n');
else
printf("-1\n");
}
int main() {
int t = read();
while (t--) {
init();
solve();
}
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int a[N], af[N], ab[N];
vector<int> seg[4 * N];
void build(int l, int r, int node) {
if (l == r) {
seg[node].push_back(ab[l]);
return;
}
int mid = (l + r) / 2;
build(l, mid, 2 * node);
build(mid + 1, r, 2 * node + 1);
merge(seg[2 * node].begin(), seg[2 * node].end(), seg[2 * node + 1].begin(),
seg[2 * node + 1].end(), back_inserter(seg[node]));
}
int query(int l, int r, int node, int ql, int qr, int val) {
if (l > qr || r < ql) return 0;
if (l >= ql && r <= qr) {
int x = lower_bound(seg[node].begin(), seg[node].end(), val) -
seg[node].begin();
return x;
}
int mid = (l + r) / 2;
return query(l, mid, 2 * node, ql, qr, val) +
query(mid + 1, r, 2 * node + 1, ql, qr, val);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
unordered_map<long long, vector<int> > mp;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mp[a[i]].push_back(i);
}
for (int i = 1; i < n + 1; i++) {
af[i] = upper_bound(mp[a[i]].begin(), mp[a[i]].end(), i) -
lower_bound(mp[a[i]].begin(), mp[a[i]].end(), 0);
}
for (int i = 1; i < n + 1; i++) {
ab[i] = upper_bound(mp[a[i]].begin(), mp[a[i]].end(), n) -
lower_bound(mp[a[i]].begin(), mp[a[i]].end(), i);
}
build(1, n, 1);
long long sum = 0;
for (int i = 2; i <= n; i++) {
int x = query(1, n, 1, i, n, af[i - 1]);
sum += x;
}
cout << sum;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
string s[10];
bool a[10][10][10];
int n;
int main() {
n = 8;
for (int i = 0; i < n; i++) {
getline(cin, s[i]);
}
memset(a, false, sizeof(a));
a[n - 1][0][0] = true;
for (int t = 1; t <= n; t++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j][t] |= a[i][j][t - 1];
if (i > 0 && j > 0) a[i][j][t] |= a[i - 1][j - 1][t - 1];
if (i > 0) a[i][j][t] |= a[i - 1][j][t - 1];
if (i > 0 && j < n - 1) a[i][j][t] |= a[i - 1][j + 1][t - 1];
if (j < n - 1) a[i][j][t] |= a[i][j + 1][t - 1];
if (i < n - 1 && j < n - 1) a[i][j][t] |= a[i + 1][j + 1][t - 1];
if (i < n - 1) a[i][j][t] |= a[i + 1][j][t - 1];
if (i < n - 1 && j > 0) a[i][j][t] |= a[i + 1][j - 1][t - 1];
if (j > 0) a[i][j][t] |= a[i][j - 1][t - 1];
if (i - t >= 0 && s[i - t][j] == 'S') a[i][j][t] = false;
if (i - t + 1 >= 0 && s[i - t + 1][j] == 'S') a[i][j][t] = false;
if (t == n && a[i][j][t]) {
printf("WIN");
return 0;
}
}
}
}
printf("LOSE");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int treeArr[1048576];
int result[524288];
int resLoc[524288];
struct QueryNode {
int loc;
int id;
bool operator<(const QueryNode other) const { return loc < other.loc; }
void read(int v_id) {
scanf("%d", &loc);
id = v_id;
}
} queryNodes[524288];
int qlen;
int mxRgt;
struct PairNode {
bool lr;
int pos;
int belong;
bool operator<(const PairNode other) const {
if (pos != other.pos)
return pos < other.pos;
else
return lr || (!other.lr);
}
void set(int v_pos, bool v_lr, int v_belong) {
pos = v_pos;
lr = v_lr;
belong = v_belong;
if (lr == false && pos > mxRgt) mxRgt = pos;
}
} pairNodes[524288 << 1];
struct Pair {
int left, right;
void read(int i);
} pairs[524288];
void Pair::read(int i) {
scanf("%d%d", &pairs[i].left, &pairs[i].right);
pairNodes[i * 2].set(left, true, i);
pairNodes[i * 2 + 1].set(right, false, i);
}
void add(int pos) {
while (pos <= mxRgt) {
treeArr[pos]++;
pos += (pos & (-pos));
}
}
void sub(int pos) {
while (pos <= mxRgt) {
treeArr[pos]--;
pos += (pos & (-pos));
}
}
int sum(int pos) {
int rtn = 0;
while (pos) {
rtn += treeArr[pos];
pos -= (pos & (-pos));
}
return rtn;
}
void getResult(int j) {
if (resLoc[queryNodes[j].id] == -1) {
result[queryNodes[j].id] = sum(queryNodes[j].loc);
resLoc[queryNodes[j].id] = queryNodes[j].loc;
} else {
result[queryNodes[j].id] +=
sum(queryNodes[j].loc) - sum(resLoc[queryNodes[j].id]);
resLoc[queryNodes[j].id] = queryNodes[j].loc;
}
}
void solve(int n) {
int i, j;
i = j = 0;
while (i < n && j < qlen) {
if (pairNodes[i].pos < queryNodes[j].loc ||
pairNodes[i].pos == queryNodes[j].loc && pairNodes[i].lr) {
pairNodes[i].lr ? add(pairNodes[i].pos)
: sub(pairs[pairNodes[i].belong].left);
i++;
} else {
getResult(j++);
}
}
while (j < qlen) {
getResult(j++);
}
}
void output(int m) {
int i;
for (i = 0; i < m; i++) {
printf("%d\n", result[i]);
}
}
int main() {
int n, m;
while (~scanf("%d%d", &n, &m)) {
int cnt, i, j;
mxRgt = 0;
for (i = 0; i < n; i++) {
pairs[i].read(i);
}
sort(pairNodes, pairNodes + n * 2);
for (j = 0; j < m; j++) {
scanf("%d", &cnt);
for (i = 0; i < cnt; i++) {
queryNodes[qlen++].read(j);
}
}
sort(queryNodes, queryNodes + qlen);
memset(treeArr, 0, sizeof(treeArr));
memset(resLoc, -1, sizeof(resLoc));
solve(n << 1);
output(m);
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
scanf("%I64d", &n);
long long i = 1, Rec = 0;
while (Rec + i < n) {
Rec = Rec + i;
i++;
}
printf("%I64d\n", n - Rec);
return 0;
}
| 2 |
#include <bits/stdc++.h>
int N;
long long T[200002];
int segL[200002], segR[200002], G;
int f[200002], g[200002], from[200002][2];
int S_not_given[200002];
long long min_select(long long L1, long long R1, long long L2, long long R2,
long long need) {
L1 = N - L1 + 1;
L2 = N - L2 + 1;
R1 = N - R1 + 1;
R2 = N - R2 + 1;
std::swap(L1, R1);
std::swap(L2, R2);
long long ans = -1000000000;
for (long long number = 0; number <= R1 - L1 + 1; number++) {
long long low = need - (long long)(R1 + R1 - number + 1) * number / 2;
long long upp = need - (long long)(L1 + L1 + number - 1) * number / 2;
if (std::max(low, L2) <= std::min(upp, R2))
ans = std::max(ans, std::min(upp, R2));
}
return ans < 0 ? -1 : N - ans + 1;
}
int O[200002];
void construct(long long L, long long R, long long need) {
if (need == 0) return;
for (long long number = 0; number <= R - L + 1; number++) {
long long low = ((N - R + 1) + (N - R + 1) + number - 1) * number / 2;
long long upp = ((N - L + 1) + (N - L + 1) - number + 1) * number / 2;
if (low <= need && need <= upp) {
long long lnum = number - 1;
long long S;
while ((S = ((N - L + 1) + (N - L + 1) - lnum + 1) * lnum / 2 +
((N - R + 1) + (N - R + 1) + number - lnum - 1) *
(number - lnum) / 2) > need)
lnum--;
for (int i = L; i <= L + lnum - 1; i++) {
O[i] = 1;
need -= N - i + 1;
}
for (int i = R - (number - lnum) + 2; i <= R; i++) {
O[i] = 1;
need -= N - i + 1;
}
O[N - need + 1] = 1;
return;
}
}
}
void get_O(int i, int ty) {
if (!i) return;
O[(ty ? segR : f)[i]] = 1;
construct((from[i][ty] ? segR : f)[i - 1] + 1, segL[i] - 1,
T[(ty ? segR : segL)[i]] - T[(from[i][ty] ? segR : segL)[i - 1]] +
ty - from[i][ty] - (N - (ty ? segR : f)[i] + 1));
get_O(i - 1, from[i][ty]);
}
int try_final_ans(int ty) {
int L1 = (ty ? segR : f)[G - 1] + 1, R1 = segL[G] - 1;
if (T[segL[G]] == T[segR[G]]) {
long long need = T[segL[G]] - T[(ty ? segR : segL)[G - 1]] - ty;
if (need == 0) {
get_O(G - 1, ty);
for (int i = 1; i <= N; i++) putchar(O[i] + '0');
puts("");
return 1;
}
for (int number = 0; number <= R1 - L1 + 1; number++) {
long long low =
(long long)((N - R1 + 1) + (N - R1 + 1) + number - 1) * number / 2;
long long upp =
(long long)((N - L1 + 1) + (N - L1 + 1) - number + 1) * number / 2;
low = N + 1 - (need - low);
upp = N + 1 - (need - upp);
low = std::max(low, (long long)segL[G]);
upp = std::min(upp, (long long)N);
if (low <= upp && S_not_given[upp] - S_not_given[low - 1] > 0) {
int pos = low;
while (T[pos] != 0) pos++;
O[pos] = 1;
construct(L1, R1, need - (N - pos + 1));
get_O(G - 1, ty);
for (int i = 1; i <= N; i++) putchar(O[i] + '0');
puts("");
return 1;
}
}
}
if (segL[G] == segR[G] || T[segL[G]] - T[segR[G]] == 1) {
long long need = T[segR[G]] - T[(ty ? segR : segL)[G - 1]] + 1 - ty;
for (int number = 0; number <= R1 - L1 + 1; number++) {
long long low =
(long long)((N - R1 + 1) + (N - R1 + 1) + number - 1) * number / 2;
long long upp =
(long long)((N - L1 + 1) + (N - L1 + 1) - number + 1) * number / 2;
low = N + 1 - (need - low);
upp = N + 1 - (need - upp);
if (low <= segR[G] && segR[G] <= upp) {
O[segR[G]] = 1;
construct(L1, R1, need - (N - segR[G] + 1));
get_O(G - 1, ty);
for (int i = 1; i <= N; i++) putchar(O[i] + '0');
puts("");
return 1;
}
}
}
return 0;
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%lld", T + i);
for (int i = 1; i <= N; i++)
S_not_given[i] = S_not_given[i - 1] + (T[i] == 0);
for (int i = 1; i <= N; i++)
if (T[i]) {
if (!G || T[segR[G]] < T[i]) {
segL[++G] = i;
segR[G] = i;
} else
segR[G] = i;
}
if (G == 0) {
for (int i = 1; i <= N; i++) putchar('0');
return 0;
}
f[0] = -1;
g[0] = 1;
segR[0] = 0;
T[0] = 0;
for (int i = 1; i <= G - 1; i++) {
int f_from_f = -1, f_from_g = -1, g_from_f = 0, g_from_g = 0;
if (T[segL[i]] == T[segR[i]]) {
if (f[i - 1] != -1)
f_from_f = min_select(f[i - 1] + 1, segL[i] - 1, segR[i] + 1,
segL[i + 1] - 1, T[segL[i]] - T[segL[i - 1]]);
if (g[i - 1])
f_from_g = min_select(segR[i - 1] + 1, segL[i] - 1, segR[i] + 1,
segL[i + 1] - 1, T[segL[i]] - T[segR[i - 1]] - 1);
}
if (segL[i] == segR[i] || T[segL[i]] - T[segR[i]] == 1) {
if (f[i - 1] != -1)
g_from_f = min_select(f[i - 1] + 1, segL[i] - 1, segR[i], segR[i],
T[segR[i]] - T[segL[i - 1]] + 1) != -1;
if (g[i - 1])
g_from_g = min_select(segR[i - 1] + 1, segL[i] - 1, segR[i], segR[i],
T[segR[i]] - T[segR[i - 1]]) != -1;
}
if (f_from_f == -1 && f_from_g == -1)
f[i] = -1;
else if (f_from_g == -1 || f_from_f != -1 && f_from_f <= f_from_g) {
f[i] = f_from_f;
from[i][0] = 0;
} else if (f_from_f == -1) {
f[i] = f_from_g;
from[i][0] = 1;
}
g[i] = g_from_f | g_from_g;
from[i][1] = !g_from_f;
}
if (f[G - 1] != -1)
if (try_final_ans(0)) return 0;
if (g[G - 1] != -1)
if (try_final_ans(1)) return 0;
}
| 27 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define value(x) cout<<#x<<" "<<x<<endl;
void solve(){
int w,h,n;
cin>>w>>h>>n;
int cnt = 1;
while(!(w&1) || !(h&1)){
if(w % 2 == 0){
cnt *= 2;
w = w / 2;
}
if(h % 2 == 0){
cnt *= 2;
h = h / 2;
}
}
if(cnt >= n){
cout<<"Yes";
}
else
{
cout<<"No";
}
cout<<endl;
}
int main(){
int t;
cin>>t;
while(t--)
solve();
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
string S[maxn];
map<long long, int> Map;
long long f(string A) {
long long res = 0;
for (int i = 0; i < A.size(); i++) {
res ^= 1 << (A[i] - 'a');
}
return res;
}
int main() {
std::ios::sync_with_stdio(0);
int n;
cin >> n;
Map.clear();
long long ans = 0;
for (int i = 0; i < n; i++) {
cin >> S[i];
long long dig = f(S[i]), p = 0;
for (int j = 0; j < 26; j++) {
p = dig ^ (1 << j);
ans += Map[p];
}
ans += Map[dig];
Map[dig]++;
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 50005;
const int INF = 1111111111;
int C[MX], Cn;
vector<int> conV[MX];
struct Edge {
int id;
int u, v, c, t;
void in() {
scanf("%d%d%d%d", &u, &v, &c, &t);
u--, v--;
conV[u].push_back(id);
conV[v].push_back(id);
C[Cn++] = c;
}
} edge[MX];
int N, M;
vector<int> con[6 * MX], rcon[6 * MX], gcon[6 * MX];
int group[6 * MX], vis[6 * MX], Gn;
int Not(int u) { return (u + 3 * MX) % (6 * MX); }
void add(int u, int v) {
con[u].push_back(v);
con[Not(v)].push_back(Not(u));
rcon[v].push_back(u);
rcon[Not(u)].push_back(Not(v));
}
bool preprocess() {
int frq[MX] = {0}, ege[MX], egn;
int newV = MX;
for (int i = 0; i < N; i++) {
egn = 0;
for (auto it : conV[i]) {
if (frq[edge[it].c] == i + 1) ege[egn++] = it;
frq[edge[it].c] = i + 1;
}
if (egn > 1) return false;
if (egn)
for (auto it : conV[i])
if (it != ege[0] && edge[it].c == edge[ege[0]].c) ege[egn++] = it;
assert(egn == 0 || egn == 2);
if (egn) {
add(ege[0], Not(ege[1]));
add(Not(ege[0]), ege[1]);
}
int ii = 0, sz = conV[i].size();
for (auto it : conV[i]) {
add(it, newV);
if (ii < sz - 1) {
add(newV, newV + 1);
}
if (ii) {
add(it, Not(newV - 1));
}
ii++, newV++;
}
}
return true;
}
int que[6 * MX], qn;
void dfs(int u) {
vis[u] = true;
for (auto it : con[u])
if (!vis[it]) dfs(it);
que[qn++] = u;
}
void DFS(int u) {
vis[u] = true;
group[u] = Gn;
for (auto it : rcon[u])
if (!vis[it]) DFS(it);
}
void SCC() {
for (int i = 0; i < 6 * MX; i++)
if (!vis[i]) dfs(i);
for (int i = 0; i < 6 * MX; i++) vis[i] = 0;
for (int i = qn - 1; i >= 0; i--)
if (!vis[que[i]]) Gn++, DFS(que[i]);
}
int val[6 * MX], gval[6 * MX];
bool gdfs(int u) {
vis[u] = 1;
if (gval[u] != -1) {
if (!gval[u])
return false;
else
return true;
}
gval[u] = 1;
for (auto it : gcon[u]) {
if (!gdfs(it)) return false;
}
return true;
}
bool can(int Max) {
for (int i = 0; i < 6 * MX; i++) val[i] = gval[i] = -1, vis[i] = 0;
for (int i = 1; i <= M; i++)
if (edge[i].t > Max) {
if (gval[group[i]] != -1) {
if (gval[group[i]] == 1)
return false;
else
continue;
}
gval[group[i]] = 0;
if (!gdfs(group[Not(i)])) return false;
}
return true;
}
bool process() {
SCC();
for (int i = 0; i < 6 * MX; i++)
if (group[i] == group[Not(i)]) {
return false;
}
for (int i = 0; i < 6 * MX; i++) {
for (auto it : con[i]) {
if (group[i] == group[it]) continue;
gcon[group[i]].push_back(group[it]);
}
}
int st = 0, en = INF;
for (int it = 0; it < 50; it++) {
int md = st + en >> 1;
if (can(md))
en = md;
else
st = md;
}
can(en);
for (int i = 1; i <= M; i++) {
if (gval[group[i]] != -1)
val[i] = gval[group[i]];
else {
if (gval[group[Not(i)]] != -1)
val[i] = !gval[group[Not(i)]];
else {
int u = group[i], v = group[Not(i)];
val[i] = gval[u] = u > v, gval[v] = v > u;
}
}
}
int cnt = 0;
for (int i = 1; i <= M; i++) cnt += val[i];
puts("Yes");
printf("%d %d\n", en, cnt);
int flg = 0;
for (int i = 1; i <= M; i++)
if (val[i]) {
if (!flg)
flg = 1;
else
putchar(' ');
printf("%d", i);
}
puts("");
return true;
}
int main() {
scanf("%d%d", &N, &M);
for (int i = 0; i < MX; i++) edge[i].id = i;
for (int i = 1; i <= M; i++) edge[i].in();
sort(C, C + Cn);
Cn = unique(C, C + Cn) - C;
for (int i = 1; i <= M; i++)
edge[i].c = lower_bound(C, C + Cn, edge[i].c) - C;
if (!preprocess()) {
puts("No");
return 0;
}
if (!process()) {
puts("No");
return 0;
}
return 0;
}
| 23 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g, r;
int n;
vector<int> color, st, scc;
int nSCC;
void dfs(int v, int flag) {
color[v] = 1;
for (int u : (flag ? g[v] : r[v]))
if (!color[u]) dfs(u, flag);
if (flag)
st.push_back(v);
else
scc[v] = nSCC;
}
void traverse() {
color = vector<int>(n, 0);
st.clear();
for (int i = 0; i < n; i++)
if (!color[i]) dfs(i, 1);
color = vector<int>(n, 0);
scc = vector<int>(n);
nSCC = 0;
for (int i = n - 1; i >= 0; i--)
if (!color[st[i]]) {
dfs(st[i], 0);
nSCC++;
}
}
vector<int> hour, szSCC, valid;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int m, h, px, py, x, y;
set<pair<int, int> > st;
while (cin >> n >> m >> h) {
hour = vector<int>(n);
for (int i = 0; i < n; i++) cin >> hour[i];
st.clear();
g = vector<vector<int> >(n);
r = vector<vector<int> >(n);
for (int i = 0; i < m; i++) {
cin >> px >> py;
px--;
py--;
x = min(px, py);
y = max(px, py);
if (st.count({x, y})) continue;
st.insert({x, y});
if ((hour[x] + 1) % h == hour[y]) {
g[x].push_back(y);
r[y].push_back(x);
}
if ((hour[y] + 1) % h == hour[x]) {
g[y].push_back(x);
r[x].push_back(y);
}
}
traverse();
szSCC = vector<int>(nSCC, 0);
valid = vector<int>(nSCC, 1);
for (int v = 0; v < n; v++) {
szSCC[scc[v]]++;
for (int u : g[v])
if (scc[u] != scc[v]) valid[scc[v]] = 0;
}
int ans = n + 1, pos = -1;
for (int i = 0; i < nSCC; i++)
if (valid[i] && szSCC[i] < ans) {
ans = szSCC[i];
pos = i;
}
cout << ans << '\n';
for (int i = 0; i < n; i++)
if (scc[i] == pos) cout << i + 1 << " ";
cout << '\n';
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
long long int v[100004];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int t = 1;
while (t--) {
long long int n;
cin >> n;
string s, t;
cin >> s >> t;
long long int ct00 = 0, ct01 = 0, ct10 = 0, ct11 = 0, a = 0, b = 0;
for (int i = 0; i < 2 * n; i++) {
if (s[i] == '0' && t[i] == '0') ct00++;
if (s[i] == '0' && t[i] == '1') ct01++;
if (s[i] == '1' && t[i] == '0') ct10++;
if (s[i] == '1' && t[i] == '1') ct11++;
}
if (ct11 % 2 == 0) {
a = b = ct11 / 2;
} else {
b = ct11 / 2;
a = ct11 - b;
if (ct01 > 0) {
b++;
ct01--;
} else if (ct10 > 0) {
ct10--;
} else if (ct00 > 0) {
ct00--;
}
}
ct11 = 0;
long long int j = 0;
while (1) {
if (ct01 == 0 && ct10 == 0) break;
if (j % 2 == 0) {
if (ct10 > 0) {
a++;
ct10--;
} else if (ct01 > 0) {
ct01--;
} else if (ct00 > 0) {
ct00--;
}
} else {
if (ct01 > 0) {
b++;
ct01--;
} else if (ct10 > 0) {
ct10--;
} else if (ct00 > 0) {
ct00--;
}
}
j++;
}
if (a > b)
cout << "First" << endl;
else if (a < b)
cout << "Second" << endl;
else
cout << "Draw" << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m, h[500005], tot, v[500005];
struct edge {
int to, nxt;
} e[500005 << 2];
void add(int x, int y) {
e[++tot].nxt = h[x];
h[x] = tot;
e[tot].to = y;
}
vector<int> c[500005];
bool dfs(int x, int fa, int d) {
c[d].push_back(x);
v[x] = 1;
if (d * 2 >= n) {
printf("PATH\n%d\n%d ", d, x);
return true;
}
for (int i = h[x]; i; i = e[i].nxt)
if (e[i].to != fa && !v[e[i].to]) {
if (dfs(e[i].to, x, d + 1)) {
printf("%d ", x);
return true;
}
}
return false;
}
void solve() {
scanf("%d%d", &n, &m);
tot = 0;
for (int i = 1; i <= n; i++) h[i] = v[i] = 0, c[i].clear();
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
if (dfs(1, 0, 1)) {
putchar('\n');
return;
}
int res = ((n + 1) / 2 + 1) / 2;
printf("PAIRING\n%d\n", res);
for (int i = n / 2; i >= 1; i--) {
for (int j = 0; res && j + 1 < (int)c[i].size(); j += 2, res--)
printf("%d %d\n", c[i][j], c[i][j + 1]);
if (!res) break;
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) solve();
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int curr, prev, i;
};
node a[7005], b[7005];
int p[20][7005], mul, f[7005];
int v[7005];
int lcp(int x, int y, int lvl, int n) {
int i, ans = 0;
for (i = lvl; i >= 0 && x < n && y < n; i--)
if (p[i][x] == p[i][y]) {
ans += (1 << i);
x += (1 << i);
y += (1 << i);
}
return ans;
}
int lc[7005], ran[7005], z[7005], z1[7005];
set<unsigned long long> ss[2005];
void zfunc(string t, string pp) {
int i, j, k, l, r;
string a = pp + (char)('$');
a = a + t;
z[0] = 0;
l = r = 0;
for (k = 1; k < a.size(); k++) {
if (k > r) {
l = r = k;
while (r < a.size() && a[r - l] == a[r]) r++;
z[k] = r - l;
r--;
} else {
int p = k - l;
if (z[p] + k <= r)
z[k] = z[p];
else {
l = k;
while (r < a.size() && a[r - l] == a[r]) r++;
z[k] = r - l;
r--;
}
}
}
for (i = a.size() - 1, j = t.size() - 1; i > pp.size(); i--, j--) {
if (z[i] == pp.size()) z1[j]++;
}
}
int main() {
string s, s1, s2, s3;
int i, j, n, lvl, c, t, k, l1, l2;
cin >> s3 >> s1 >> s2;
zfunc(s3, s2);
s = s3 + char('z' + 1);
s = s + s1;
n = s.length();
mul = max(35, n + 1);
for (i = 0; i < n; i++) p[0][i] = s[i] - 'a' + 1;
lvl = 1;
c = 1;
while (c < n) {
for (i = 0; i < n; i++) {
a[i].curr = p[lvl - 1][i];
if (i + c < n)
a[i].prev = p[lvl - 1][i + c];
else
a[i].prev = 0;
a[i].i = i;
}
memset(v, 0, sizeof(v[0]) * mul);
for (i = 0; i < n; i++) v[a[i].prev]++;
for (i = 1; i < mul; i++) v[i] += v[i - 1];
for (i = n - 1; i >= 0; i--) {
v[a[i].prev]--;
b[v[a[i].prev]] = a[i];
}
memset(v, 0, sizeof(v[0]) * mul);
for (i = 0; i < n; i++) v[b[i].curr]++;
for (i = 1; i < mul; i++) v[i] += v[i - 1];
for (i = n - 1; i >= 0; i--) {
v[b[i].curr]--;
a[v[b[i].curr]] = b[i];
}
for (i = 0; i < n; i++) {
if (i > 0 && a[i].curr == a[i - 1].curr && a[i].prev == a[i - 1].prev)
p[lvl][a[i].i] = p[lvl][a[i - 1].i];
else
p[lvl][a[i].i] = i + 1;
}
lvl++;
c *= 2;
}
lvl--;
for (i = 0; i < n; i++) f[p[lvl][i] - 1] = i;
j = p[lvl][s3.length() + 1] - 1;
int ans = 0;
for (i = j + 1; i < n; i++) {
int temp = lcp(s3.length() + 1, f[i], lvl, n);
if (temp != s1.length()) break;
unsigned long long tt = 0;
for (j = f[i]; j < s3.length(); j++) {
tt = tt * 31 + s3[j];
if (z1[j] == 1 &&
j + s2.length() - f[i] >= max(s1.length(), s2.length()) &&
j + s2.length() <= s3.length()) {
ss[j + s2.length() - f[i] - s1.length()].insert(tt);
}
}
}
for (i = 0; i < 2005; i++) ans += ss[i].size();
cout << ans;
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n + 1);
int u, v;
for (int i = 0; i < n - 1; i++) {
cin >> u >> v;
a[u]++;
a[v]++;
}
bool check = false;
for (int i = 1; i <= n; i++)
if (a[i] == 2) check = true;
if (check)
cout << "NO";
else
cout << "YES";
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, x, y;
cin >> a >> b >> x >> y;
x = abs(a - x);
y = abs(b - y);
(x > y) ? cout << x << endl : cout << y << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
return getchar();
static char buf[100000], *l = buf, *r = buf;
return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)
? EOF
: *l++;
}
template <class T>
void rd(T &x) {
x = 0;
int f = 1, ch = gc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = gc();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 - '0' + ch;
ch = gc();
}
x *= f;
}
template <class T>
inline bool Cmax(T &x, T y) {
return x < y ? x = y, 1 : 0;
}
template <class T>
inline bool Cmin(T &x, T y) {
return x > y ? x = y, 1 : 0;
}
const int maxb = 1e5, maxg = 1e5, maxd = maxb + maxg;
int n, m, b, g, d;
int r[2], len[2];
map<int, pair<int, int> > s[2][maxd];
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
void exgcd(int a, int b, int &x, int &y) {
if (b == 0)
x = 1, y = 0;
else
exgcd(b, a % b, y, x), y -= a / b * x;
}
inline int inver(int a, int mod) {
int x, y;
exgcd(a, mod, x, y);
if (x < 0) x += mod;
return x;
}
void ins(int k, int x, pair<int, int> t) {
int a = x % d, b = (long long)(x - a) * r[k] % len[k] / d;
if (s[k][a].count(b))
Cmin(s[k][a][b], t);
else
s[k][a][b] = t;
}
long long sol(int k, map<int, pair<int, int> > &s) {
if (s.empty()) return -1;
long long re = 0;
for (map<int, pair<int, int> >::iterator it = s.begin(); it != s.end();
++it) {
int nex = ++it == s.end() ? len[k] / d + s.begin()->first : it->first;
--it;
if (nex == it->first + 1 && !it->second.second) continue;
Cmax(re, it->second.first + (long long)(nex - it->first - 1) * len[k ^ 1]);
}
return re;
}
int main() {
rd(n), rd(m), d = gcd(n, m);
if (d > 2e5) {
puts("-1");
return 0;
}
r[0] = inver(m, n), len[0] = n;
r[1] = inver(n, m), len[1] = m;
rd(b);
for (int i = 1; i <= b; ++i) {
int x;
rd(x);
ins(0, x, make_pair(x, 0)), ins(1, x % m, make_pair(x, 1));
}
rd(g);
for (int i = 1; i <= g; ++i) {
int y;
rd(y);
ins(1, y, make_pair(y, 0)), ins(0, y % n, make_pair(y, 1));
}
long long an = 0;
for (int k = 0; k < 2; ++k)
for (int i = 0; i < d; ++i) {
long long d = sol(k, s[k][i]);
if (d == -1) {
puts("-1");
return 0;
}
Cmax(an, d);
}
printf("%lld\n", an);
return 0;
}
| 23 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long p = 0;
long long a[n];
pair<long long, long long> k[5];
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (long long i = 0; i < 5; i++) {
cin >> k[i].first;
k[i].second = i;
}
sort(k, k + 5);
reverse(k, k + 5);
long long maax, indx;
long long ans[] = {0, 0, 0, 0, 0};
for (long long i = 0; i < n; i++) {
p += a[i];
for (long long j = 0; j < 5; j++) {
ans[k[j].second] += p / k[j].first;
p = p % k[j].first;
}
}
for (long long i = 0; i < 5; i++) {
cout << ans[i] << " ";
}
cout << p;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10, T = 3;
int n, m;
char grid[N][N];
int sx = -1, sy = -1;
int tx = -1, ty = -1;
bool solve() {
int lsy, rsy;
for (int j = sy; j < m; j++) {
if (grid[sx][j] == 'T') return true;
if (grid[sx][j] == '*') break;
rsy = j;
}
for (int j = sy; j >= 0; j--) {
if (grid[sx][j] == 'T') return true;
if (grid[sx][j] == '*') break;
lsy = j;
}
int lty, rty;
for (int j = ty; j < m; j++) {
if (grid[tx][j] == '*') break;
rty = j;
}
for (int j = ty; j >= 0; j--) {
if (grid[tx][j] == '*') break;
lty = j;
}
int ly = max(lty, lsy), ry = min(rty, rsy);
for (int j = ly; j <= ry; j++) {
bool poss = true;
for (int i = min(sx, tx); i <= max(sx, tx); i++) {
if (grid[i][j] == '*') poss = false;
}
if (poss) return true;
}
int lsx, rsx;
for (int i = sx; i < n; i++) {
if (grid[i][sy] == 'T') return true;
if (grid[i][sy] == '*') break;
rsx = i;
}
for (int i = sx; i >= 0; i--) {
if (grid[i][sy] == 'T') return true;
if (grid[i][sy] == '*') break;
lsx = i;
}
int ltx, rtx;
for (int i = tx; i < n; i++) {
if (grid[i][ty] == '*') break;
rtx = i;
}
for (int i = tx; i >= 0; i--) {
if (grid[i][ty] == '*') break;
ltx = i;
}
int lx = max(ltx, lsx), rx = min(rtx, rsx);
for (int i = lx; i <= rx; i++) {
bool poss = true;
for (int j = min(sy, ty); j <= max(sy, ty); j++) {
if (grid[i][j] == '*') poss = false;
}
if (poss) return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> grid[i];
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'S') sx = i, sy = j;
if (grid[i][j] == 'T') tx = i, ty = j;
}
}
cout << (solve() ? "YES" : "NO") << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int N = 1e6 + 5, M = 1e3 + 5;
int n, k, a[N], dis[M << 1];
int bfs() {
std::queue<int> q;
for (int i = 1; i <= n; ++i) dis[a[i] + M] = 1, q.push(a[i] + M);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 1; i <= n; ++i) {
int v = u + a[i];
if (v < 0 || v > 2000) continue;
if (!dis[v]) dis[v] = dis[u] + 1, q.push(v);
}
}
return dis[M] ? dis[M] : -1;
}
int main() {
scanf("%d%d", &k, &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[i] -= k;
std::sort(a + 1, a + n + 1);
n = std::unique(a + 1, a + n + 1) - (a + 1);
printf("%d\n", bfs());
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1, s2;
getline(cin, s1);
getline(cin, s2);
string answer(s1.size(), '0');
int diff = 0, like = 0;
int a1 = 0, a2 = 0;
for (int i = 0; i < s1.size(); ++i) {
if (s1[i] != s2[i]) {
diff++;
if (a1 < a2) {
a1++;
answer[i] = s1[i];
} else {
a2++;
answer[i] = s2[i];
}
} else {
like++;
answer[i] = s1[i];
}
}
if (a1 != a2)
cout << "impossible" << endl;
else
cout << answer << endl;
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
#define Going_supersonic ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define all(a) a.begin(),a.end()
#define allr(v) v.rbegin(),v.rend()
#define sz() size()
#define l() length()
#define ll long long int
#define Ed endl
#define ed endl
#define iv vector<long long int>
#define bg() begin()
#define nd() end()
#define ff first
ll GCD(ll a,ll b){return b?GCD(b,a%b):a;}
#define ss second
ll n,m;
int main(){
Going_supersonic
ll T=1;
cin>>T;
while(T--){
ll i=0,j=0,k=0,sm=0,val=0;
cin>>n;
iv a(n);
map<ll,bool> mp;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
mp[abs(a[j]-a[i])]=true;
}
}
cout<<mp.size()<<Ed;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n, m;
cin >> n >> m;
vector<int> a(n), b(n);
set<int> s;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) cin >> b[i];
sort((b).begin(), (b).end());
sort((a).begin(), (a).end());
int val, temp = a[0];
for (int i = 0; i < n; ++i) {
if (temp > b[i])
s.insert(m - (temp - b[i]));
else
s.insert(b[i] - temp);
}
std::vector<int> ans;
for (auto x : s) {
for (int i = 0; i < n; ++i) {
ans.push_back((a[i] + x) % m);
}
sort((ans).begin(), (ans).end());
if (ans == b) {
cout << x << '\n';
break;
}
ans.clear();
}
return 0;
}
| 7 |