solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long md = 1000000007;
int n;
const int MX = 211111;
long long a[MX];
long long inv[MX], fac[MX], facinv[MX];
long long k[MX];
long long O[MX], E[MX], A[MX];
void wk() {
inv[1] = 1;
for (int i = 2; i <= n; i++) inv[i] = inv[md % i] * (md - md / i) % md;
fac[0] = facinv[0] = 1;
for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % md;
for (int i = 1; i <= n; i++) facinv[i] = facinv[i - 1] * inv[i] % md;
if (n & 1) {
for (int i = 1; i <= n - 2; i += 2)
O[i] = E[i] = (fac[(n - 2) / 2] * facinv[i / 2] % md *
facinv[(n - 2) / 2 - i / 2] % md);
for (int i = 1; i <= n - 2; i += 2) O[i + 1] = O[i], E[i + 1] = md - E[i];
if (n % 4 == 1) {
for (int i = 2; i <= n; i++) O[i] = (O[i] + md - E[i - 1]) % md;
} else {
for (int i = 2; i <= n; i++) O[i] = (O[i] + E[i - 1]) % md;
}
long long ans = 0LL;
for (int i = 1; i <= n; i++) ans = (ans + O[i] * a[i] % md) % md;
printf("%lld\n", ans);
} else {
for (int i = 1; i <= n - 1; i += 2)
O[i] = E[i] = (fac[(n - 1) / 2] * facinv[i / 2] % md *
facinv[(n - 1) / 2 - i / 2] % md);
if (n % 4 == 0) {
for (int i = 1; i <= n - 1; i += 2) O[i + 1] = md - E[i];
} else {
for (int i = 1; i <= n - 1; i += 2) O[i + 1] = E[i];
}
long long ans = 0LL;
for (int i = 1; i <= n; i++) ans = (ans + O[i] * a[i] % md) % md;
printf("%lld\n", ans);
}
}
void bf() {
long long k = 1;
for (int i = n - 1; i; i--)
for (int j = 1; j <= i; j++) a[j] = (a[j] + md + k * a[j + 1]) % md, k = -k;
printf("%lld\n", a[1]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
if (n <= 5000)
bf();
else
wk();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int vis[100000 + 2];
vector<int> G[100000 + 2];
void dfs(int u, bool &e) {
vis[u] = 1;
for (int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if (vis[v] == 0) {
dfs(v, e);
} else if (vis[v] == 1) {
e = 1;
}
}
vis[u] = 2;
}
void f(int u, vector<long long> &v) {
v.push_back(u);
for (int i = 0; i < (int)G[u].size(); i++) {
int t = G[u][i];
f(t, v);
}
return;
}
int degree[100000 + 2];
int main() {
int n, q, x, y;
long long t;
cin >> n >> q >> t;
vector<int> A(n);
for (int i = 0; i < n; i++) cin >> A[i];
for (int i = 0; i < q; i++) {
cin >> x >> y;
x--, y--;
G[x].push_back(y);
degree[y]++;
}
bool e = 0;
for (int i = 0; i < n; i++) {
if (vis[i] == 0) {
dfs(i, e);
}
}
if (e == 1) {
puts("0");
return 0;
}
vector<long long> v;
for (int i = 0; i < n; i++) {
vector<long long> v2;
if (degree[i] == 0) {
f(i, v2);
long long s = 0;
for (int j = 0; j < (int)v2.size(); j++) {
t -= s;
s += A[v2[j]];
v.push_back(s);
}
}
}
if (t < 0) {
puts("0");
return 0;
}
n = (int)v.size();
vector<vector<long long> > DP(n + 1, vector<long long>(t + 1));
for (int i = n; i >= 0; i--) {
for (int j = 0; j <= t; j++) {
long long &dev = DP[i][j] = 0;
if (i == n) {
if (j == 0) dev = 1;
continue;
}
if (j == 0) {
dev = 1;
continue;
}
dev = (dev + DP[i + 1][j]) % 1000000007;
if (j - v[i] >= 0) dev = (dev + DP[i][j - v[i]]) % 1000000007;
}
}
cout << DP[0][t] << '\n';
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<ll> v(n);
for(int i=0;i<n;i++) cin>>v[i];
ll ans=v[0]*n+v[1]*n;
ll sum = v[0]+v[1], a=v[0],b=v[1];
for(int i=2;i<n;i++){
sum+=v[i];
if(i%2==0) a = min(a,v[i]);
else b = min(b,v[i]);
ll tmp = sum-a-b;
if(i%2==0) tmp = tmp+a*(n-i/2)+b*(n-i/2+1);
else tmp = tmp+a*(n-i/2)+b*(n-i/2);
ans = min(ans,tmp);
}
cout<<ans<<"\n";
}
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, s[401000], a[401000], vis[401000], answ[401000];
vector<pair<int, int> > edge[401000];
int dfs(int u, int f) {
vis[u] = 1;
for (auto v : edge[u])
if (!vis[v.first]) {
int d = dfs(v.first, u);
answ[abs(v.second)] = v.second > 0 ? d : -d;
s[u] += d;
}
return s[u];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &s[i]), s[0] += s[i];
if (s[0]) return puts("Impossible"), 0;
scanf("%d", &m);
for (int i = 1, u, v; i <= m; ++i)
scanf("%d%d", &u, &v), edge[u].push_back(pair<int, int>(v, i)),
edge[v].push_back(pair<int, int>(u, -i));
puts("Possible");
dfs(1, 0);
for (int i = 1; i <= m; ++i) printf("%d\n", answ[i]);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5 + 5;
vector<int> v, s;
string str;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, t, ans = 0;
cin >> n;
v.resize(n), s.resize(n);
for (auto &i : v) cin >> i;
s[0] = v[0];
for (int i = 1; i < n; ++i) s[i] = s[i - 1] + v[i];
cin >> str;
for (int i = 0; i < str.size(); ++i)
if (str[i] == '1') {
if (i > 0)
ans = max(ans + v[i], s[i - 1]);
else
ans = v[i];
}
cout << ans << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
int i = 1;
while (k % 2 == 0) {
k /= 2;
i++;
}
cout << i;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long big = 2e17 + 5;
const long long md = 1e9 + 7;
long long bw(long long x, long long n) {
long long res = 1;
while (n > 1) {
if (n % 2 == 0)
n /= 2, x = (x * x) % md;
else
n--, res = (res * x) % md;
}
return res;
}
int f[1000][1000];
int main() {
ios_base ::sync_with_stdio(0);
cin.tie();
cout.tie();
int n, m, d;
cin >> n >> d;
cin >> m;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
f[i][j] = 0;
if (i + j >= d && i + j <= n + n - d && i - j <= d && j - i <= d)
f[i][j] = 1;
}
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if (f[x][y] == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj(200001);
long long a[200001], c[200001][2], dp[200001][2], s[200001];
long long Solve(int i, int k) {
long long ans = 0;
for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 2; ++y) {
long long a = x == 0 ? (1000000007 - dp[i][x]) % 1000000007 : dp[i][x];
long long b = x == 0 ? dp[k][y] : (1000000007 - dp[k][y]) % 1000000007;
long long ansx = (c[i][x] * b + c[k][y] * a) % 1000000007;
ans = (ans + ansx) % 1000000007;
}
}
return ans;
}
void DFS(int i, int p) {
c[i][1] = 1;
dp[i][1] = a[i];
long long ans = 0;
for (auto k : adj[i]) {
if (k == p) continue;
DFS(k, i);
ans = (ans + Solve(i, k) + Solve(k, i)) % 1000000007;
for (int x = 0; x < 2; ++x) {
c[i][x] += c[k][1 - x];
dp[i][x] = (dp[i][x] + c[k][1 - x] * a[i] + 1000000007 - dp[k][1 - x]) %
1000000007;
}
}
s[i] = ans;
}
int main() {
int n;
scanf("%d", &n);
long long ans = 0;
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
a[i] = (1000000007 + a[i]) % 1000000007;
ans = (ans + a[i]) % 1000000007;
}
for (int x = 1; x < n; ++x) {
int i, j;
scanf("%d%d", &i, &j);
adj[i].push_back(j);
adj[j].push_back(i);
}
DFS(1, 0);
for (int i = 1; i <= n; ++i) ans = (ans + s[i]) % 1000000007;
printf("%lld\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
int n;
int x;
while (t--) {
cin >> n >> x;
int a[n];
int flag = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == x) flag = 1;
}
if (flag == 1) {
cout << 1 << endl;
} else {
sort(a, a + n);
if (a[n - 1] >= ceil((float)x / 2)) {
cout << 2 << endl;
} else {
if (x % a[n - 1] == 0) {
cout << x / a[n - 1] << endl;
} else
cout << (x / a[n - 1]) + 1 << endl;
}
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int a = (n - k) / 2;
int count = 0;
while (count < n) {
for (int i = 1; i <= a; i++) {
cout << '0';
count++;
if (count == n) return 0;
}
cout << '1';
count++;
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long xl, xr, xm, yl, yr, ym, n, xt, yt;
cin >> n;
xl = 1;
xr = n;
xm = n;
yl = 1;
yr = n;
ym = n;
int p;
while (true) {
long double sa = ((long double)xm + 1 - xl) * ((long double)ym + 1 - yl);
long double sb = ((long double)yr - ym) * ((long double)xm + 1 - xl);
long double sc = ((long double)xr - xm) * ((long double)ym + 1 - yl);
if (sb <= sa + sc || sc <= sa + sb) {
xt = (xl + xm) / 2;
yt = (yl + ym) / 2;
cout << xt << ' ' << yt << endl;
fflush(stdout);
cout.flush();
cin >> p;
if (1 == p) {
xl = xt + 1;
if (xm + 1 == xl) {
xm = xr;
yr = ym;
}
} else if (2 == p) {
yl = yt + 1;
if (ym + 1 == yl) {
xr = xm;
ym = yr;
}
} else if (3 == p) {
xm = xt;
ym = yt;
} else {
break;
}
}
if (sb > sa + sc) {
xt = (xl + xm) / 2;
yt = ym;
cout << xt << ' ' << yt << endl;
fflush(stdout);
cout.flush();
cin >> p;
if (1 == p) {
xl = xt + 1;
if (xm + 1 == xl) {
xm = xr;
yr = ym;
}
} else if (2 == p) {
yl = yt + 1;
ym = yr;
xr = xm;
} else if (3 == p) {
xm = xt;
} else {
break;
}
}
if (sc > sa + sb) {
xt = xm;
yt = (yl + ym) / 2;
cout << xt << ' ' << yt << endl;
fflush(stdout);
cout.flush();
cin >> p;
if (1 == p) {
xl = xt + 1;
yr = ym;
xm = xr;
} else if (2 == p) {
yl = yt + 1;
if (ym + 1 == yl) {
xr = xm;
ym = yr;
}
} else if (3 == p) {
ym = yt;
} else {
break;
}
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n, ans, q;
int main() {
cin >> n;
int i = 1;
while (q < n) {
if (i % 2)
q++, ans++;
else if (q < n - 1)
q += 2, ans++;
else
break;
i++;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long n, v0, v1, x, y, line, row, ans = 1LL;
template <typename T>
inline T read(T &x) {
T flg = 1;
x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') flg = -flg;
ch = getchar();
}
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return x *= flg;
}
template <typename T>
inline void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
inline void writeln(T x) {
write(x);
puts("");
}
template <typename T>
inline void writesp(T x) {
write(x);
putchar(' ');
}
inline long long ksm(long long b, long long p) {
long long res = 1;
while (p) {
if (p & 1) res = res * b % MOD;
b = b * b % MOD;
p >>= 1;
}
return res;
}
signed main() {
read(n);
for (register long long i = 1; i <= n; ++i) {
read(x);
if (x == 1)
++v1;
else
++v0;
}
for (register long long i = 2; i <= v0 + v1; ++i) ans = ans * i % MOD;
if (v1 < 3) {
write(ans);
return 0;
}
x = y = 1LL;
for (register long long i = 3; i <= v1; ++i) {
line = (x + y) % MOD;
row = x * i % MOD;
ans = ans * ksm(row, MOD - 2) % MOD * line % MOD;
x = line;
y = row;
}
write(ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MAXW = 5000000;
int n, cnt[MAXW];
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int w;
scanf("%d", &w);
++cnt[w];
}
int ans = 0;
for (int t = 0; t < 5000000; ++t) {
while (cnt[t] >= 2) {
cnt[t + 1] += 1;
cnt[t] -= 2;
}
if (cnt[t]) ++ans;
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int cnt, ls, rs;
} t[5000005];
int n, cnt = 0;
int a[100005], ocr[100005];
int nxt[100005], root[100005];
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9') {
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - 48;
c = getchar();
}
return x;
}
void add(int pos, int l, int r, int pl, int del) {
if (l == r) {
t[pos].cnt += del;
return;
}
int mid = (l + r) >> 1;
if (pl <= mid) {
int pre = t[pos].ls;
t[pos].ls = ++cnt;
t[t[pos].ls].cnt = t[pre].cnt;
t[t[pos].ls].ls = t[pre].ls;
t[t[pos].ls].rs = t[pre].rs;
add(t[pos].ls, l, mid, pl, del);
} else {
int pre = t[pos].rs;
t[pos].rs = ++cnt;
t[t[pos].rs].cnt = t[pre].cnt;
t[t[pos].rs].ls = t[pre].ls;
t[t[pos].rs].rs = t[pre].rs;
add(t[pos].rs, mid + 1, r, pl, del);
}
t[pos].cnt = t[t[pos].ls].cnt + t[t[pos].rs].cnt;
}
int find(int pos, int l, int r, int num) {
if (num > t[pos].cnt) {
return n + 1;
}
if (l == r) {
return l;
}
int mid = (l + r) >> 1;
if (t[t[pos].ls].cnt >= num) {
return find(t[pos].ls, l, mid, num);
} else {
return find(t[pos].rs, mid + 1, r, num - t[t[pos].ls].cnt);
}
}
int main() {
int i;
n = read();
for (i = 1; i <= n; i++) {
a[i] = read();
}
for (i = 1; i <= n; i++) {
ocr[i] = n + 1;
}
for (i = n; i >= 1; i--) {
nxt[i] = ocr[a[i]];
ocr[a[i]] = i;
}
root[1] = ++cnt;
for (i = 1; i <= n; i++) {
if (ocr[i] <= n) {
add(root[1], 1, n, ocr[i], 1);
}
}
for (i = 2; i <= n; i++) {
root[i] = ++cnt;
t[root[i]].cnt = t[root[i - 1]].cnt;
t[root[i]].ls = t[root[i - 1]].ls;
t[root[i]].rs = t[root[i - 1]].rs;
add(root[i], 1, n, i - 1, -1);
if (nxt[i - 1] <= n) {
add(root[i], 1, n, nxt[i - 1], 1);
}
}
for (i = 1; i <= n; i++) {
int ans = 0, pos = 1;
while (pos <= n) {
pos = find(root[pos], 1, n, i + 1);
ans++;
}
printf("%d ", ans);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
long long fp[1010], fc[1010], inv[1010];
int n, a[1010], m;
long long f[1010][1010];
long long fpm(long long x, int y) {
long long res = 1;
for (; y; y >>= 1, x = x * x % mod)
if (y & 1) res = res * x % mod;
return res;
}
void update(long long& x, long long y) {
x += y;
if (x >= mod) x -= mod;
}
long long C[2010][2010];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i <= 2000; i++) C[i][0] = 1;
for (int i = 1; i <= 2000; i++)
for (int j = 1; j <= 2000; j++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
long long ans = 1;
int s = 0;
for (int i = 1; i <= n; i++) {
ans *= C[s + a[i] - 1][s], ans %= mod;
s += a[i];
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAX = 1e6 + 2;
vector<int> p;
long long total = 0;
void go(int i, int taken, int d, int lim) {
if (lim == 0) return;
if (i == p.size()) {
if (taken & 1) {
total -= lim / d;
} else {
total += lim / d;
}
return;
}
go(i + 1, taken + 1, d * p[i], lim);
go(i + 1, taken, d, lim);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
bitset<MAX> is_prime;
is_prime.set();
vector<int> divisor(MAX);
for (int i = 2; i < MAX; i++) {
if (is_prime[i]) {
for (int j = i; j < MAX; j += i) {
divisor[j] = i;
is_prime[j] = false;
}
}
}
int t;
cin >> t;
while (t--) {
int x, y, k;
cin >> x >> y >> k;
p.clear();
while (y != 1) {
p.push_back(divisor[y]);
int divi = divisor[y];
while (y % divi == 0) y /= divi;
}
int low = x + 1, high = 1e9;
while (low <= high) {
int mid = (low + high) / 2;
total = 0;
go(0, 0, 1, mid);
long long all = total;
total = 0;
go(0, 0, 1, x);
long long real = all - total;
if (real >= k)
high = mid - 1;
else
low = mid + 1;
}
cout << low << '\n';
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
int a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
int cnt = 0;
while (k--) {
int c, r = 0;
cin >> c;
c--;
while (1) {
if (a[r][c] == 1) {
a[r][c] = 2;
c++;
} else if (a[r][c] == 2) {
r++;
} else if (a[r][c] == 3) {
a[r][c] = 2;
c--;
}
if (r >= n) {
cout << c + 1 << " ";
break;
} else if (c < 0) {
cout << 1 << " ";
break;
} else if (c >= m) {
cout << c << " ";
break;
}
}
}
while (k > 0) {
int c;
cin >> c;
cout << c << " ";
k--;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct point {
double x, y;
point() { x = 0.0, y = 0.0; }
point(double _x, double _y) { x = _x, y = _y; }
bool operator<(point other) const {
if (fabs(x - other.x) > 1e-9) return x < other.x;
return y < other.y;
}
};
struct line {
double a, b, c;
line(point p1, point p2) {
if (fabs(p1.x - p2.x) < 1e-9) {
a = 1.0;
b = 0.0;
c = -p1.x;
} else {
a = -(double)(p1.y - p2.y) / (p1.x - p2.x);
b = 1.0;
c = -(double)(a * p1.x) - p1.y;
}
}
};
bool areParallel(line l1, line l2) {
return (fabs(l1.a - l2.a) < 1e-9) && (fabs(l1.b - l2.b) < 1e-9);
}
bool areSame(line l1, line l2) {
return (areParallel(l1, l2)) && (fabs(l1.c - l2.c) < 1e-9);
}
bool areIntersect(line l1, line l2, point& p) {
if (areParallel(l1, l2)) return false;
p.x = (l1.c * l2.b - l1.b * l2.c) / (l2.a * l1.b - l2.b * l1.a);
if (fabs(l1.b) > 1e-9)
p.y = -(l1.a * p.x + l1.c);
else
p.y = -(l2.a * p.x + l2.c);
return true;
}
double dist(point a, point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + ((a.y - b.y) * (a.y - b.y)));
}
vector<string> split(const string& s, char delimiter) {
vector<string> tokens;
string token;
istringstream tokenStream(s);
while (getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
struct vec {
double x, y;
vec(double _x, double _y) : x(_x), y(_y) {}
};
vec toVec(point a, point b) { return vec(b.x - a.x, b.y - a.y); }
vec scale(vec v, double s) { return vec(v.x * s, v.y * s); }
point translate(point p, vec v) { return point(p.x + v.x, p.y + v.y); }
double dot(vec a, vec b) { return (a.x * b.x + a.y * b.y); }
double cross(vec a, vec b) { return a.x * b.y - a.y * b.x; }
double norm_sq(vec v) { return v.x * v.x + v.y * v.y; }
point rot(point p, double theta) {
double rad = (theta * 3.14159265358979323846) / 180.0;
point p2(p.x * cos(rad) - p.y * sin(rad), p.x * sin(rad) + p.y * cos(rad));
if (abs(p2.x) < 1e-9) p2.x = 0.0;
if (abs(p2.y) < 1e-9) p2.y = 0.0;
return p2;
}
double angle(point a, point o, point b) {
vec oa = toVec(o, a), ob = toVec(o, b);
return acos(dot(oa, ob) / sqrt(norm_sq(oa) * norm_sq(ob)));
}
int main() {
point p;
cin >> p.x >> p.y;
vec v(0.0, 0.0);
cin >> v.x >> v.y;
int a, b, c, d;
cin >> a >> b >> c >> d;
point arr[7];
double mag = sqrt(norm_sq(v));
double ang = atan2(v.y / mag, v.x / mag);
arr[0] = point(p.x + b * cos(ang), p.y + b * sin(ang));
double angr = fmod((ang - 3.14159265358979323846 / 2.0),
2 * 3.14159265358979323846),
angl = fmod((ang + 3.14159265358979323846 / 2.0),
2 * 3.14159265358979323846),
angd =
fmod((ang + 3.14159265358979323846), 2 * 3.14159265358979323846);
arr[6] = point(p.x + (a / 2.0) * cos(angr), p.y + (a / 2.0) * sin(angr));
arr[1] = point(p.x + (a / 2.0) * cos(angl), p.y + (a / 2.0) * sin(angl));
arr[5] = point(p.x + (c / 2.0) * cos(angr), p.y + (c / 2.0) * sin(angr));
arr[2] = point(p.x + (c / 2.0) * cos(angl), p.y + (c / 2.0) * sin(angl));
arr[4] = point(arr[5].x + d * cos(angd), arr[5].y + d * sin(angd));
arr[3] = point(arr[2].x + d * cos(angd), arr[2].y + d * sin(angd));
for (int i = 0; i < 7; i++)
cout << fixed << setprecision(12) << arr[i].x << " " << arr[i].y << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int n, q, b, w;
bool sw;
vector<pair<int, int>> a;
void ans() {
printf("YES\n");
for (auto i : a) {
printf("%d %d\n", i.first + 1 + sw, i.second);
}
return;
}
int main() {
scanf("%d", &q);
while (q--) {
a.clear();
sw = false;
scanf("%d%d", &b, &w);
if (b > w) {
swap(b, w);
sw = true;
}
int tot = 1;
a.push_back({2, 2});
a.push_back({1, 2});
a.push_back({3, 2});
a.push_back({2, 3});
a.push_back({2, 1});
vector<pair<int, int>> black;
black.push_back({1, 1});
black.push_back({3, 1});
black.push_back({1, 3});
black.push_back({3, 3});
if (w <= 4) {
int res = 4 - w;
while (res--) {
a.pop_back();
}
for (int i = 0; i < b - 1; i++) {
a.push_back(black[i]);
}
ans();
continue;
}
w -= 4;
int x = 2, y = 4;
while (w > 3) {
tot++;
a.push_back({x, y});
a.push_back({x, y + 1});
a.push_back({x - 1, y});
a.push_back({x + 1, y});
black.push_back({x - 1, y + 1});
black.push_back({x + 1, y + 1});
y += 2;
w -= 3;
}
if (w) {
tot++;
a.push_back({x, y});
a.push_back({x, y + 1});
a.push_back({x - 1, y});
a.push_back({x + 1, y});
int res = 3 - w;
for (int j = 0; j < res; j++) a.pop_back();
black.push_back({x - 1, y + 1});
black.push_back({x + 1, y + 1});
}
if (tot > b) {
printf("NO\n");
continue;
}
int res = b - tot;
while (res--) {
a.push_back(black.back());
black.pop_back();
}
printf("YES\n");
for (auto i : a) {
printf("%d %d\n", i.first + sw + 1, i.second);
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const int n_ = 1e5 + 5;
unordered_map<int, int> dp;
int calc(int msk) {
if (dp.count(msk)) return dp[msk];
set<int> x;
int nmsk;
for (int i = 0; (1ll << i) <= msk; i++) {
if (msk & (1 << i)) {
nmsk = msk;
for (int j = i; (1ll << j) <= msk; j += (i + 1)) {
if (msk & (1 << j)) nmsk ^= (1 << j);
}
x.insert(calc(nmsk));
}
}
int ret = 0;
while (x.count(ret)) ++ret;
return dp[msk] = ret;
}
int g[] = {0, 1, 2, 1, 4, 3, 2, 1, 5, 6, 2, 1, 8, 7, 5, 9,
8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12, 14};
long long n, ans;
unordered_map<long long, int> f;
int main() {
scanf("%lld", &n);
long long tmp = n;
for (long long i = 2; i * i <= n; i++) {
if (f[i]) continue;
int sz = 0;
for (long long j = i; j <= n; j *= i) ++sz, f[j] = 1;
ans ^= g[sz];
tmp -= sz;
}
tmp &= 1;
ans ^= tmp;
puts(ans ? "Vasya" : "Petya");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
for (int i = n; i > n - k; i--) {
cout << i << " ";
}
for (int i = 1; i <= n - k; i++) {
cout << i << " ";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long a[100003] = {0}, b[100003] = {0}, c[100003] = {0}, ans = 0;
int main() {
int n, m, k, d;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; i++) scanf("%I64d%I64d%I64d", &a[i], &b[i], &c[i]);
for (int t = 0; t < k; t++) {
scanf("%d", &d);
for (int i = 0; i < m; i++) {
if (a[i] <= d && d <= b[i]) ans += c[i] - a[i] + d;
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int mod = 1e9 + 7;
const int inf = 2e9;
int ma, mi, n, k, a[N], w, f[N], tra[N * 4], tri[N * 4], tr[N * 4], l1, r1, s,
l, pos;
void build(int l, int r, int now) {
if (l == r) {
tra[now] = a[l];
tri[now] = a[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, now * 2);
build(mid + 1, r, now * 2 + 1);
tra[now] = max(tra[now * 2], tra[now * 2 + 1]);
tri[now] = min(tri[now * 2], tri[now * 2 + 1]);
}
void change(int l, int r, int now) {
if (l1 <= l && r1 >= r) {
tr[now] = w;
return;
}
int mid = (l + r) >> 1;
if (l1 <= mid) change(l, mid, now * 2);
if (r1 > mid) change(mid + 1, r, now * 2 + 1);
tr[now] = min(tr[now * 2], tr[now * 2 + 1]);
}
void ask(int l, int r, int now) {
if (l1 <= l && r1 >= r) {
ma = max(ma, tra[now]);
mi = min(mi, tri[now]);
return;
}
int mid = (l + r) >> 1;
if (l1 <= mid) ask(l, mid, now * 2);
if (r1 > mid) ask(mid + 1, r, now * 2 + 1);
}
void ask2(int l, int r, int now) {
if (l1 <= l && r1 >= r) {
w = min(w, tr[now]);
return;
}
int mid = (l + r) >> 1;
if (l1 <= mid) ask2(l, mid, now * 2);
if (r1 > mid) ask2(mid + 1, r, now * 2 + 1);
}
int main() {
scanf("%d%d%d", &n, &s, &l);
for (int i = (1); i < (n + 1); i++) scanf("%d", &a[i]);
pos = 1;
build(1, n, 1);
for (int i = (1); i < (n + 1); i++) {
while (1) {
l1 = pos;
r1 = i;
ma = -inf;
mi = inf;
ask(1, n, 1);
if (ma - mi > s)
pos++;
else
break;
}
if (i - pos + 1 < l)
f[i] = inf;
else {
l1 = pos - 1;
r1 = i - l;
w = inf;
if (l1)
ask2(1, n, 1);
else
w = 0;
if (w != inf)
f[i] = w + 1;
else
f[i] = inf;
}
l1 = r1 = i;
w = f[i];
change(1, n, 1);
}
if (f[n] == inf)
printf("-1");
else
printf("%d", f[n]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int x = s.length(), res = 0;
sort(s.begin(), s.end());
for (int i = 0; i < x; i++) {
if (s[i] == s[x - 1] && s[i - 1] != s[x - 1]) {
res = i;
}
}
for (int i = res; i < x; i++) {
cout << s[i];
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
char a[100010][31], s[100010];
int pos[100010][31], f[100010], len[100010], n, A[100010];
inline int find(int x) {
if (x == f[x]) return x;
return f[x] = find(f[x]);
}
inline int merge(int x, int y, int ch) {
int fx = find(x), fy = find(y);
if (fx == fy) return 1;
f[fy] = fx;
int pos1 = pos[fx][ch], pos2 = pos[fy][ch];
if (pos1 < pos2) {
for (int i = 1; i <= len[fy]; i++) s[i] = a[fy][i];
for (int i = pos2 - pos1 + 1, j = 1; i <= pos2 - pos1 + len[fx]; i++, j++) {
if (i <= len[fy]) {
if (s[i] != a[fx][j]) return 0;
} else
s[i] = a[fx][j];
}
len[fx] = max(len[fy], pos2 - pos1 + len[fx]);
if (len[fx] > 26) return 0;
memset(pos[fx], 0, sizeof(pos[fx]));
for (int i = 1; i <= len[fx]; i++) {
a[fx][i] = s[i];
if (pos[fx][a[fx][i] - 'a'] == 0)
pos[fx][a[fx][i] - 'a'] = i;
else
return 0;
}
} else {
for (int i = 1; i <= len[fx]; i++) s[i] = a[fx][i];
for (int i = pos1 - pos2 + 1, j = 1; i <= pos1 - pos2 + len[fy]; i++, j++) {
if (i <= len[fx]) {
if (s[i] != a[fy][j]) return 0;
} else
s[i] = a[fy][j];
}
len[fx] = max(len[fy] + pos1 - pos2, len[fx]);
if (len[fx] > 26) return 0;
memset(pos[fx], 0, sizeof(pos[fx]));
for (int i = 1; i <= len[fx]; i++) {
a[fx][i] = s[i];
if (pos[fx][a[fx][i] - 'a'] == 0)
pos[fx][a[fx][i] - 'a'] = i;
else
return 0;
}
}
return 1;
}
inline bool cmp(const int &x, const int &y) { return a[x][1] < a[y][1]; }
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
int m = strlen(s + 1);
len[i] = m;
if (m > 26) {
cout << "NO" << endl;
return 0;
}
for (int j = 1; j <= m; j++) {
a[i][j] = s[j];
if (pos[i][a[i][j] - 'a'] == 0)
pos[i][a[i][j] - 'a'] = j;
else {
cout << "NO" << endl;
return 0;
}
}
}
for (int i = 1; i <= n; i++) f[i] = i;
for (int ch = 0; ch < 26; ch++) {
int last = 0;
for (int i = 1; i <= n; i++)
if (pos[i][ch] != 0) {
if (last != 0) {
if (merge(last, i, ch) == 0) {
cout << "NO" << endl;
return 0;
}
}
last = i;
}
}
int tot = 0;
for (int i = 1; i <= n; i++)
if (find(i) == i) A[++tot] = i;
sort(A + 1, A + tot + 1, cmp);
for (int i = 1; i <= tot; i++)
for (int j = 1; j <= len[A[i]]; j++) cout << a[A[i]][j];
return 0;
}
| 6 |
#include <bits/stdc++.h>
char s[1024 * 100];
bool res[4] = {0, 0, 0, 0};
int main() {
gets(s);
int minr = 0, maxr = 0;
int i;
for (i = 0; s[i]; i++) {
if (s[i] == '1') {
minr++;
maxr++;
} else if (s[i] == '0') {
minr--;
maxr--;
} else {
maxr++;
minr--;
}
}
res[0] = (minr < 0);
res[3] = (maxr >= 2);
res[1] =
(minr <= 0 && maxr >= 0 || minr <= 1 && maxr >= 1) && (s[i - 1] == '1') ||
(minr + 2 <= 0 && maxr >= 0 || minr + 2 <= 1 && maxr >= 1) &&
(s[i - 1] == '?');
res[2] =
(minr <= 0 && maxr >= 0 || minr <= 1 && maxr >= 1) && (s[i - 1] == '0') ||
(minr <= 0 && maxr - 2 >= 0 || minr <= 1 && maxr - 2 >= 1) &&
(s[i - 1] == '?');
for (i = 0; i < 4; i++) {
if (res[i]) printf("%d%d\n", (i & 2) >> 1, i & 1);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, l, i, j, t, a, b, c, res, d[10005];
string s;
int main() {
for (i = 2; i < 100; i++) {
if (d[i] == 0)
for (j = 2 * i; j < 1000; j += i) {
d[j] = 1;
}
}
for (i = 2; i <= 50; i++) {
if (k > 1) {
cout << "composite" << endl;
return 0;
}
if (d[i] == 0)
cout << i << endl;
else
continue;
cin >> s;
if (s == "yes") {
d[i * i] = 0;
k++;
}
}
cout << "prime" << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, vis[10000005];
string mp[1000005];
bool dfs(int x, int y) {
if (x == n || y == m || x < 0 || y < 0 || mp[x][y] == '#' || vis[x * m + y])
return false;
if (x == n - 1 && y == m - 1) return true;
if (x || y) vis[x * m + y] = 1;
return dfs(x + 1, y) || dfs(x, y + 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
cin >> mp[i];
}
if (!dfs(0, 0)) {
printf("0");
return 0;
}
if (!dfs(0, 0)) {
printf("1");
return 0;
} else {
printf("2");
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, m;
struct bian {
int from, to;
};
struct tree {
bian eg[N << 2];
int num, front[N << 1];
int zong;
int fa[N << 1], siz[N << 1];
void add(int x, int y) {
eg[++num].to = y;
eg[num].from = front[x];
front[x] = num;
}
void init() {
zong = n;
for (int i = 1; i <= zong; ++i) fa[i] = i, siz[i] = 1;
}
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int tot, now;
int son[N << 4][2];
long long tag[N << 4];
int root[N];
void merge(int x, int y) {
int d1 = find(x), d2 = find(y);
fa[++zong] = zong;
add(zong, d1);
add(zong, d2);
siz[zong] = siz[find(x)] + siz[find(y)];
fa[d1] = zong;
fa[d2] = zong;
}
int ll[N << 1], rr[N << 1], ids[N], p;
void dfs(int o) {
if (o <= n) {
ids[o] = ++p;
ll[o] = rr[o] = ids[o];
return;
}
ll[o] = 1e9;
for (int i = front[o]; i; i = eg[i].from) {
int to = eg[i].to;
dfs(to);
ll[o] = min(ll[o], ll[to]);
rr[o] = max(rr[o], rr[to]);
}
}
void opt1(int be, int &o, int l, int r, int L, int R, int w) {
if (o == 0) o = ++tot;
tag[o] = tag[be];
if (L <= l && r <= R) {
tag[o] += w;
son[o][0] = son[be][0];
son[o][1] = son[be][1];
return;
}
int mid = (l + r) >> 1;
if (mid >= L)
opt1(son[be][0], son[o][0], l, mid, L, R, w);
else
son[o][0] = son[be][0];
if (mid < R)
opt1(son[be][1], son[o][1], mid + 1, r, L, R, w);
else
son[o][1] = son[be][1];
}
long long opt2(int o, int l, int r, int i, long long now) {
if (o == 0) return now;
if (l == r) return now + tag[o];
int mid = (l + r) >> 1;
if (mid >= i)
return opt2(son[o][0], l, mid, i, now + tag[o]);
else
return opt2(son[o][1], mid + 1, r, i, now + tag[o]);
}
} T1;
struct tree_ {
bian eg[N << 2];
int num, front[N << 1];
int zong;
int fa[N << 1];
void add(int x, int y) {
eg[++num].to = y;
eg[num].from = front[x];
front[x] = num;
}
void init() {
zong = n;
for (int i = 1; i <= zong; ++i) fa[i] = i;
}
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
int d1 = find(x), d2 = find(y);
fa[++zong] = zong;
add(zong, d1);
add(zong, d2);
fa[d1] = zong;
fa[d2] = zong;
}
int ll[N << 1], rr[N << 1], ids[N], p;
void dfs(int o) {
if (o <= n) {
ids[o] = ++p;
ll[o] = rr[o] = ids[o];
return;
}
ll[o] = 1e9;
for (int i = front[o]; i; i = eg[i].from) {
int to = eg[i].to;
dfs(to);
ll[o] = min(ll[o], ll[to]);
rr[o] = max(rr[o], rr[to]);
}
}
int col[N << 2];
void opt3(int o, int l, int r, int L, int R, int w) {
if (L <= l && r <= R) {
col[o] = w;
return;
}
int mid = (l + r) >> 1;
if (col[o]) {
col[o << 1] = col[o << 1 | 1] = col[o];
col[o] = 0;
}
if (mid >= L) opt3(o << 1, l, mid, L, R, w);
if (mid < R) opt3(o << 1 | 1, mid + 1, r, L, R, w);
}
int opt4(int o, int l, int r, int i) {
if (l == r) return col[o];
int mid = (l + r) >> 1;
if (col[o]) {
col[o << 1] = col[o << 1 | 1] = col[o];
col[o] = 0;
}
if (mid >= i)
return opt4(o << 1, l, mid, i);
else
return opt4(o << 1 | 1, mid + 1, r, i);
}
} T2;
int que[N][3];
struct b {
int fa[N << 1], zong;
void init() {
zong = n;
for (int i = 1; i <= n; ++i) fa[i] = i;
}
int find(int o) { return fa[o] == o ? o : fa[o] = find(fa[o]); }
void merge(int x, int y) {
int d1 = find(x), d2 = find(y);
fa[++zong] = zong;
fa[d1] = zong;
fa[d2] = zong;
}
} B1, B2;
int main() {
scanf("%d%d", &n, &m);
T1.init();
T2.init();
for (int i = 1; i <= m; ++i) {
char s[10];
scanf("%s", s);
if (s[0] == 'U') {
int x, y;
scanf("%d%d", &x, &y);
que[i][0] = 1;
que[i][1] = x;
que[i][2] = y;
T1.merge(x, y);
}
if (s[0] == 'M') {
int x, y;
scanf("%d%d", &x, &y);
que[i][0] = 2;
que[i][1] = x;
que[i][2] = y;
T2.merge(x, y);
}
if (s[0] == 'A') {
int x;
scanf("%d", &x);
que[i][0] = 3;
que[i][1] = x;
}
if (s[0] == 'Z') {
int x;
scanf("%d", &x);
que[i][0] = 4;
que[i][1] = x;
}
if (s[0] == 'Q') {
int x;
scanf("%d", &x);
que[i][0] = 5;
que[i][1] = x;
}
}
for (int i = 1; i <= T1.zong; ++i)
if (T1.find(i) == i) T1.dfs(i);
for (int i = 1; i <= T2.zong; ++i)
if (T2.find(i) == i) T2.dfs(i);
B1.init();
B2.init();
for (int i = 1; i <= m; ++i) {
if (que[i][0] == 1) {
B1.merge(que[i][1], que[i][2]);
}
if (que[i][0] == 2) {
B2.merge(que[i][1], que[i][2]);
}
if (que[i][0] == 3) {
++T1.now;
T1.opt1(T1.root[T1.now - 1], T1.root[T1.now], 1, n,
T1.ll[B1.find(que[i][1])], T1.rr[B1.find(que[i][1])],
T1.siz[B1.find(que[i][1])]);
}
if (que[i][0] == 4) {
T2.opt3(1, 1, n, T2.ll[B2.find(que[i][1])], T2.rr[B2.find(que[i][1])],
T1.now);
}
if (que[i][0] == 5) {
printf("%lld\n", T1.opt2(T1.root[T1.now], 1, n, T1.ids[que[i][1]], 0) -
T1.opt2(T1.root[T2.opt4(1, 1, n, T2.ids[que[i][1]])],
1, n, T1.ids[que[i][1]], 0));
}
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<int> v(n);
set<int> st;
for (int i = 0; i < n; i++) {
cin >> v[i];
st.insert(v[i]);
}
sort(v.begin(), v.end());
if (st.size() == 1) {
cout << 0 << " ";
cout << (n * (n - 1)) / (long long)2;
} else {
long long cnt1 = 1, cnt2 = 1, i = 1, j = n - 2;
while (v[i] == v[0] && i < n) {
cnt1++;
i++;
}
while (v[j] == v[n - 1] && j >= 0) {
cnt2++;
j--;
}
cout << v[n - 1] - v[0] << " ";
cout << cnt1 * cnt2 << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c2 = 0, i, j, n, t, c3, sum1, c;
cin >> c3;
while (c3--) {
cin >> t;
n = 10;
c = 0, b = 0;
while (t > 0) {
c += t;
b += t;
t = b / n;
b = b % n;
}
cout << c << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_queue = priority_queue<T, vector<T>, greater<T>>;
const long long MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T-- > 0) {
long long a, b, c;
cin >> a >> b >> c;
cout << (a < c ? 1 : -1) << ' ';
cout << (c < a * b ? b : -1) << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x).size()
#define tr(container,it) for(auto it=container.begin();it!=container.end();it++)
using pi = pair <int, int>;
using vi = vector <int>;
using vvi = vector<vector<int> >;
//const int infl = 9e18;
int mod = 1e9 + 7;
long long int mul(long long int x, long long int y)
{
long long int one = 1;
return (x * one * y) % mod;
}
long long int binpow(long long int x, long long int y)
{
int z = 1;
while (y)
{
if (y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
/*int inv(int x)
{
return binpow(x, mod - 2);
}*/
/*int divide(int x, int y)
{
return mul(x, inv(y));
}*/
/*int fact[200005];
void precalc()
{
fact[0] = 1;
for(int i = 1; i < 200005; i++)
{
fact[i] = mul(fact[i - 1], i);
}
//fact[i]=fact[i-1]*i;
}*/
//cout << "Case #" << m << ": " << 1 << endl;
bool comp(pair<long long int, long long int> &a, pair<long long int, long long int> &b)
{
return a.second > b.second;
}
signed main()
{ long long int t, n;
cin >> t;
while (t--)
{ cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++)
{
cin >> a[i];
}
long long int count = 0;
for (long long int i = 0; i < n; i++)
{
long long int j = a[i] - i - 2;
while (j < n)
{
if (j > i && a[i]*a[j] == (i + j + 2) && j < n)
{
count++;
}
j += a[i];
}
}
cout << count << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void get_next(string p, char nex[]) {
int q, k;
int m = p.length();
nex[0] = 0;
for (q = 1, k = 0; q < m; ++q) {
while (k > 0 && p[q] != p[k]) k = nex[k - 1];
if (p[q] == p[k]) {
k++;
}
nex[q] = k;
}
}
int main() {
string p;
char a[100] = {0};
int n, k;
cin >> n >> k;
k--;
cin >> p;
get_next(p, a);
int len = a[n - 1];
while (k--) p = p + p.substr(len, n - len);
cout << p;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
#pragma GCC optimize "-O3"
int n;
struct kek {
int x0, y0;
int x1, y1;
};
vector<kek> k;
kek f(kek a, kek b) {
kek ans;
ans.x0 = max(a.x0, b.x0);
ans.x1 = min(a.x1, b.x1);
ans.y0 = max(a.y0, b.y0);
ans.y1 = min(a.y1, b.y1);
return ans;
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < (n); i++) {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
kek z;
z.x0 = x0;
z.x1 = x1;
z.y0 = y0;
z.y1 = y1;
k.push_back(z);
}
vector<kek> pref, suf;
pref.resize(n);
suf.resize(n);
pref[0] = k[0];
for (int i = 1; i < n; i++) {
pref[i] = f(pref[i - 1], k[i]);
}
suf[n - 1] = k[n - 1];
for (int i = n - 2; i >= 0; i--) {
suf[i] = f(suf[i + 1], k[i]);
}
for (int i = 1; i <= n - 2; i++) {
kek z;
z = f(pref[i - 1], suf[i + 1]);
if (z.x0 > z.x1 || z.y0 > z.y1)
continue;
else {
cout << z.x0 << " " << z.y0;
return 0;
}
}
kek z;
z = suf[1];
if (z.x0 > z.x1 || z.y0 > z.y1)
;
else {
cout << z.x0 << " " << z.y0;
return 0;
}
z = pref[n - 2];
if (z.x0 > z.x1 || z.y0 > z.y1)
;
else {
cout << z.x0 << " " << z.y0;
return 0;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
vector<long long int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
int m = n / 2;
int i = 0;
int j = m;
while (i < m && j < n) {
if (a[j] >= 2 * a[i]) {
i++;
j++;
} else {
j++;
}
}
cout << n - i << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 10;
const long long int mod = 1e9 + 7;
clock_t startTime;
double getCurrenttime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
void fastio() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
}
bool comp(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
long long int mod_exp(long long int x, long long int y, long long int mm) {
if (y <= 0)
return (1);
else if (y % 2 == 0)
return (mod_exp((x * x) % mm, y / 2, mm));
else
return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm);
}
bool powerof2(long long int n) {
return (int)(ceil(log2(n))) == (int(floor(log2(n))));
}
int modstring(string s, int k) {
int ans = 0;
for (int i = 0; i < (int)s.length(); i++) {
ans = (ans * 10 + s[i] - '0') % k;
}
return ans;
}
void solve() {
string s;
cin >> s;
int k = modstring(s, 4);
cout << ((int)(1 + pow(2, k) + pow(3, k) + pow(4, k))) % 5 << '\n';
}
int main() {
fastio();
int t = 1;
while (t--) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, i, j, f = 1;
cin >> n >> k;
cout << 1 << " ";
j = 1;
for (i = k; i >= 1; i--) {
if (f == 1) {
cout << j + i << " ";
j = j + i;
f = -1;
} else {
cout << j - i << " ";
f = 1;
j = j - i;
}
}
for (i = k + 2; i <= n; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MX = 5e5 + 5;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
int pct(int x) { return __builtin_popcount(x); }
int bit(int x) { return 31 - __builtin_clz(x); }
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
namespace input {
template <class T>
void re(complex<T>& x);
template <class T1, class T2>
void re(pair<T1, T2>& p);
template <class T>
void re(vector<T>& a);
template <class T, size_t SZ>
void re(array<T, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& x) {
string t;
re(t);
x = stod(t);
}
void re(long double& x) {
string t;
re(t);
x = stold(t);
}
template <class T, class... Ts>
void re(T& t, Ts&... ts) {
re(t);
re(ts...);
}
template <class T>
void re(complex<T>& x) {
T a, b;
re(a, b);
x = {a, b};
}
template <class T1, class T2>
void re(pair<T1, T2>& p) {
re(p.first, p.second);
}
template <class T>
void re(vector<T>& a) {
for (int i = (0); i < ((int)a.size()); ++i) re(a[i]);
}
template <class T, size_t SZ>
void re(array<T, SZ>& a) {
for (int i = (0); i < (SZ); ++i) re(a[i]);
}
} // namespace input
using namespace input;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(long long x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(long double x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.first, ", ", x.second, "}");
}
template <class T>
void pr(const T& x) {
pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void ps() { pr("\n"); }
template <class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
void pc() { pr("]\n"); }
template <class T, class... Ts>
void pc(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
} // namespace output
using namespace output;
namespace io {
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void setIO(string second = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if ((int)second.size()) {
setIn(second + ".in"), setOut(second + ".out");
}
}
} // namespace io
using namespace io;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
struct DSU {
vector<pair<int, int> > par;
void init(int n) {
par.resize(n);
for (int i = (0); i < (n); ++i) par[i] = {i, 0};
}
pair<int, int> get(int x) {
if (par[x].first == x) return par[x];
pair<int, int> p = get(par[x].first);
p.second ^= par[x].second;
return par[x] = p;
}
void unite(int x, int y, int z) {
pair<int, int> X = get(x), Y = get(y);
if (X.first == Y.first) {
if ((X.second ^ Y.second) != z) {
ps(-1);
exit(0);
}
} else {
par[Y.first] = {X.first, X.second ^ Y.second ^ z};
}
}
};
template <int SZ>
struct LCA {
static const int BITS = 32 - __builtin_clz(SZ);
int N, R = 1, par[BITS][SZ], depth[SZ];
vector<int> adj[SZ];
void ae(int u, int v) { adj[u].push_back(v), adj[v].push_back(u); }
void dfs(int u, int prev) {
par[0][u] = prev;
depth[u] = depth[prev] + 1;
for (auto& v : adj[u])
if (v != prev) dfs(v, u);
}
void init(int _N) {
N = _N;
dfs(R, 0);
for (int k = (1); k < (BITS); ++k)
for (int i = (1); i < (N + 1); ++i) par[k][i] = par[k - 1][par[k - 1][i]];
}
int getPar(int a, int b) {
for (int k = (BITS)-1; k >= (0); --k)
if (b & (1 << k)) a = par[k][a];
return a;
}
int lca(int u, int v) {
if (depth[u] < depth[v]) swap(u, v);
u = getPar(u, depth[u] - depth[v]);
for (int k = (BITS)-1; k >= (0); --k)
if (par[k][u] != par[k][v]) u = par[k][u], v = par[k][v];
return u == v ? u : par[0][u];
}
int dist(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }
};
LCA<MX> L;
int n, m, mn[MX], c[MX], mid;
bool bad;
DSU D;
bool par[MX], tmpPar[MX];
pair<int, int> stor[MX];
bool fin;
void dfs(int x) {
for (auto& t : L.adj[x])
if (t != L.par[0][x]) {
dfs(t);
if (bad) return;
}
for (auto& t : L.adj[x])
if (t != L.par[0][x]) {
stor[D.get(t).first].first = MOD;
}
pair<int, int> p = D.get(x);
stor[p.first] = {1, mid};
vector<int> todo = {p.first};
for (auto& t : L.adj[x])
if (t != L.par[0][x]) {
pair<int, int> p = D.get(t);
if (stor[p.first].first == MOD) {
stor[p.first] = {1, mid};
todo.push_back(p.first);
}
if (p.second == 0)
ckmax(stor[p.first].first, mn[t] + 1);
else
ckmin(stor[p.first].second, mid - mn[t]);
}
pair<int, int> P = stor[p.first];
if (p.second) P = {mid + 1 - P.second, mid + 1 - P.first};
pair<int, int> ban = {MOD, -MOD};
int LL = P.first, RR = P.second;
for (int i = (1); i < ((int)todo.size()); ++i) {
pair<int, int> z = stor[todo[i]];
if (z.first > z.second) {
bad = 1;
return;
}
pair<int, int> Z = {mid + 1 - z.second, mid + 1 - z.first};
if (z > Z) swap(z, Z);
ckmax(LL, z.first), ckmin(RR, Z.second);
if (z.second + 1 < Z.first) {
ckmin(ban.first, z.second + 1), ckmax(ban.second, Z.first - 1);
}
}
if (LL > RR) {
bad = 1;
return;
}
int val;
if (LL < ban.first)
val = LL;
else if (LL > ban.second)
val = LL;
else if (ban.second < RR)
val = ban.second + 1;
else {
bad = 1;
return;
}
mn[x] = val;
tmpPar[p.first] = p.second;
for (int i = (1); i < ((int)todo.size()); ++i) {
pair<int, int> t = stor[todo[i]];
if (t.first <= val && val <= t.second) {
tmpPar[todo[i]] = 0;
} else {
tmpPar[todo[i]] = 1;
}
}
if (fin) {
for (auto& t : L.adj[x])
if (t != L.par[0][x]) {
pair<int, int> p = D.get(t);
par[t] = tmpPar[p.first] ^ p.second;
}
}
}
bool ok() {
bad = 0;
dfs(1);
return !bad;
}
void genAns(int x) {
if (par[x] == 0) {
c[x] = mn[x];
} else {
c[x] = mid + 1 - mn[x];
}
for (auto& t : L.adj[x])
if (t != L.par[0][x]) {
par[t] ^= par[x];
genAns(t);
}
}
int ad[MX];
void path(int a, int b) {
int x = L.lca(a, b);
int A, B;
if (a != x) {
A = L.getPar(a, L.depth[a] - L.depth[x] - 1);
ad[a]++;
ad[A]--;
}
if (b != x) {
B = L.getPar(b, L.depth[b] - L.depth[x] - 1);
ad[b]++;
ad[B]--;
}
if (a != x && b != x) {
D.unite(A, B, 1);
}
}
void genAd(int x) {
for (auto& t : L.adj[x])
if (t != L.par[0][x]) {
genAd(t);
ad[x] += ad[t];
}
if (ad[x]) {
D.unite(x, L.par[0][x], 0);
}
}
namespace FastIO {
const int BSZ = 1 << 15;
char ibuf[BSZ];
int ipos, ilen;
char nc() {
if (ipos == ilen) {
ipos = 0;
ilen = fread(ibuf, 1, BSZ, stdin);
if (!ilen) return EOF;
}
return ibuf[ipos++];
}
void rs(string& x) {
char ch;
while (isspace(ch = nc()))
;
do {
x += ch;
} while (!isspace(ch = nc()) && ch != EOF);
}
template <class T>
void ri(T& x) {
char ch;
int sgn = 1;
while (!isdigit(ch = nc()))
if (ch == '-') sgn *= -1;
x = ch - '0';
while (isdigit(ch = nc())) x = x * 10 + (ch - '0');
x *= sgn;
}
template <class T, class... Ts>
void ri(T& t, Ts&... ts) {
ri(t);
ri(ts...);
}
char obuf[BSZ], numBuf[100];
int opos;
void flushOut() {
fwrite(obuf, 1, opos, stdout);
opos = 0;
}
void wc(char c) {
if (opos == BSZ) flushOut();
obuf[opos++] = c;
}
void ws(string second) {
for (auto& c : second) wc(c);
}
template <class T>
void wi(T x, char after = '\0') {
if (x < 0) wc('-'), x *= -1;
int len = 0;
for (; x >= 10; x /= 10) numBuf[len++] = '0' + (x % 10);
wc('0' + x);
for (int i = (len)-1; i >= (0); --i) wc(numBuf[i]);
if (after) wc(after);
}
void initO() { assert(atexit(flushOut) == 0); }
} // namespace FastIO
using namespace FastIO;
void init() {
initO();
ri(n, m);
D.init(n + 1);
for (int i = (0); i < (n - 1); ++i) {
int a, b;
ri(a, b);
L.ae(a, b);
}
L.init(n);
for (int i = (0); i < (m); ++i) {
int a, b;
ri(a, b);
path(a, b);
}
genAd(1);
}
int main() {
init();
mid = n;
int lo = 1, hi = n;
while (lo < hi) {
mid = (lo + hi) / 2;
if (ok())
hi = mid;
else
lo = mid + 1;
}
mid = lo;
fin = 1;
assert(ok());
ps(mid);
genAns(1);
for (int i = (1); i < (n + 1); ++i) {
if (i < n)
wi(c[i], ' ');
else
wi(c[i], '\n');
}
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k;
string s;
void read_input() { cin >> n >> k >> s; }
int change_to_number(char ch) {
if (ch == '0') return 0;
if (ch == '1') return 1;
return 2;
}
bool ok(int cnt0, int cnt1, int cnt2) {
return (cnt2 >= max(cnt1, cnt0) - min(cnt1, cnt0)
? (cnt2 - max(cnt1, cnt0) - min(cnt1, cnt0)) % 2 == 0
: false);
}
void solve() {
vector<int> V(k, 2);
for (int i = 0; i < n; i++) {
if ((V[i % k] == 1 && s[i] == '0') || (V[i % k] == 0 && s[i] == '1')) {
cout << "NO" << '\n';
return;
}
V[i % k] = (V[i % k] == 2 ? change_to_number(s[i]) : V[i % k]);
}
for (int i = 0; i < k; i++) {
for (int j = i; j < n; j += k) {
s[j] = (char)(V[i % k] + '0');
}
}
int cnt0 = 0, cnt1 = 0, cnt2 = 0;
for (int i = 0; i < k; i++) {
cnt0 += (s[i] == '0'), cnt1 += (s[i] == '1'), cnt2 += (s[i] == '2');
}
if (!ok(cnt0, cnt1, cnt2)) {
cout << "NO" << '\n';
return;
}
for (int i = 1; i + k <= n; i++) {
cnt1 += (s[i + k - 1] == '1') - (s[i - 1] == '1');
cnt0 += (s[i + k - 1] == '0') - (s[i - 1] == '0');
cnt2 += (s[i + k - 1] == '2') - (s[i - 1] == '2');
if (!ok(cnt0, cnt1, cnt2)) {
cout << "NO" << '\n';
return;
}
}
cout << "YES" << '\n';
}
void write_output() {}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
read_input();
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, i, j;
cin >> n >> k;
if (k == 1)
for (i = 0; i < n; i++) putchar('0' + !i);
else {
k = (n - k) / 2;
for (i = 0; i < n; i = j)
for (j = i; j <= i + k && j < n; j++) putchar('0' + (j == i));
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
char a[1000], b[1000];
int main() {
int l;
cin >> l;
for (int j = 1; j <= l; j++) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
int o = 0;
for (int i = 2; i < n; i++) {
if (a[i] == '1' && b[i] == '1') {
cout << "NO";
o++;
break;
}
}
if (o == 0) cout << "YES";
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
const int INF = 1e9 + 7;
int main() {
int n, m;
cin >> n >> m;
int lenA = 2 * n;
int lenB = 3 * m;
int kA = lenA / 6;
int kB = lenB / 6;
while (kA != 0 && kB != 0) {
if (lenA <= lenB) {
lenA += 2;
kA--;
kB--;
if (lenA % 6 == 0) {
kA++;
}
} else {
lenB += 3;
kA--;
kB--;
if (lenB % 6 == 0) {
kB++;
}
}
}
cout << max(lenA, lenB);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long base, long long pow, long long mo) {
long long res = 1;
while (pow) {
if (pow & 1) res = (res * base) % mo;
base = (base * base) % mo;
pow /= 2;
}
return res;
}
long long Mul(long long a, long long b, long long mo) {
long long res = 0;
a %= mo;
while (b) {
if (b & 1) res = (res + a) % mo;
a = (2 * a) % mo;
b >>= 1;
}
return res;
}
bool comp_functor(const pair<int, int> &a, const pair<int, int> &b) {
if (a.second == b.second) {
return a.first < b.first;
}
return a.second > b.second;
}
void Prime(long long n) {
while (n % 2 == 0) {
cout << 2 << " ";
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
cout << i << " ";
n = n / i;
}
}
if (n > 2) cout << n << " ";
}
void Divisors(int n) {
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
printf("%d ", i);
else
printf("%d %d ", i, n / i);
}
}
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long min(long long a, long long b) {
if (a < b) {
return a;
} else {
return b;
}
}
long long max(long long a, long long b) {
if (a > b) {
return a;
} else {
return b;
}
}
void NcR(long long n, long long r) {
long long p = 1, k = 1;
if (n - r < r) r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
long long m = gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
} else
p = 1;
cout << p << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t, n, i, j, k, mini, l1, l2, l3, r1, r2, c, d, maxi, x, y, l, m, r,
ans, last, flag, lastind, q;
t = 1;
cin >> t;
while (t--) {
cin >> n;
int a[n + 1];
for (int i = 0; i < n; i++) cin >> a[i];
;
c = 0;
vector<int> v;
for (i = 0; i < n; i++) {
if (a[i] == 1) {
c++;
}
v.push_back(a[i]);
}
vector<int> v2;
j = -1;
if (c == 0 || c == 1) {
cout << n - c << "\n";
for (i = 0; i < n; i++) {
if (a[i] == 0) {
cout << 0 << " ";
}
}
cout << "\n";
continue;
}
if (c % 2 == 1) {
for (i = v.size() - 1; i >= 0; i--) {
if (v[i] == 1) {
j = i;
break;
}
}
}
for (i = 0; i < v.size(); i++) {
if (i != j) {
v2.push_back(v[i]);
}
}
vector<int> final;
j = -1;
k = -1;
for (i = 0; i < v2.size(); i++) {
if (v2[i] == 1) {
j = i;
break;
}
}
for (i = v2.size() - 1; i >= 0; i--) {
if (v2[i] == 1) {
k = i;
break;
}
}
for (i = 0; i < j; i++) {
final.push_back(0);
}
c = 0;
for (i = j; i <= k; i++) {
if (v2[i] == 1) {
if (c % 2 == 1) {
final.pop_back();
}
c = 0;
final.push_back(1);
} else {
c++;
final.push_back(0);
}
}
for (i = k + 1; i < v2.size(); i++) {
final.push_back(0);
}
cout << final.size() << "\n";
for (i = 0; i < final.size(); i++) {
cout << final[i] << " ";
}
cout << "\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 400010;
const long double X = 1e6;
const long double eps = 1e-13;
template <typename T>
inline void read(T &AKNOI) {
T x = 0, flag = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') flag = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
AKNOI = flag * x;
}
inline int sgn(long double x) {
if (x > eps) return 1;
if (x < -eps) return -1;
return 0;
}
int n, id[MAXN], collinear, tot;
long double pol[MAXN];
struct point {
long double x, y;
point() {}
point(long double _x, long double _y) : x(_x), y(_y) {}
friend point operator+(point a, point b) {
return point(a.x + b.x, a.y + b.y);
}
friend point operator-(point a, point b) {
return point(a.x - b.x, a.y - b.y);
}
friend point operator*(point a, long double b) {
return point(a.x * b, a.y * b);
}
friend long double operator^(point a, point b) {
return a.x * b.y - a.y * b.x;
}
} p[MAXN], p2[MAXN];
inline bool polar_cmp(int x, int y) {
if (sgn(pol[x] - pol[y]) == 0) collinear = 1;
return pol[x] < pol[y];
}
struct line {
point u, v;
long double ang;
line() {}
line(point _u, point _v) : u(_u), v(_v) { ang = atan2(v.y - u.y, v.x - u.x); }
friend bool operator<(line a, line b) {
return (sgn(a.ang - b.ang) == 0) ? (sgn((a.v - a.u) ^ (b.v - a.u)) < 0)
: (a.ang < b.ang);
}
} l[MAXN], q[MAXN];
inline point Intersect(line a, line b) {
long double k = -((a.u - b.u) ^ (b.v - b.u)) / ((a.v - a.u) ^ (b.v - b.u));
return a.u + (a.v - a.u) * k;
}
inline bool Check(line a, line b, line c) {
point inter = Intersect(a, b);
return sgn((c.v - c.u) ^ (inter - c.u)) < 0;
}
long double HalfPlane() {
sort(l + 1, l + tot + 1);
int tmp = 0, head, tail;
for (int i = 1; i <= tot; ++i) {
if (i == 1 || sgn(l[i].ang - l[i - 1].ang)) {
l[++tmp] = l[i];
}
}
tot = tmp;
q[head = tail = 1] = l[1];
for (int i = 2; i <= tot; ++i) {
while (head < tail && Check(q[tail - 1], q[tail], l[i])) --tail;
while (head < tail && Check(q[head + 1], q[head], l[i])) ++head;
q[++tail] = l[i];
}
while (head < tail && Check(q[tail - 1], q[tail], q[head])) --tail;
while (head < tail && Check(q[head + 1], q[head], q[tail])) ++head;
tot = 0;
q[tail + 1] = q[head];
for (int i = head; i <= tail; ++i) {
p[++tot] = Intersect(q[i], q[i + 1]);
}
long double ret = 0;
p[tot + 1] = p[1];
for (int i = 1; i <= tot; ++i) {
ret += (p[i] ^ p[i + 1]);
}
return fabs(ret * 0.5);
}
void init() {
read(n);
for (int i = 1, x, y; i <= n; ++i) {
read(x);
read(y);
p[i] = point(x, y);
}
}
void solve() {
for (int i = 2; i <= n; ++i) {
p2[i] = p[i] - p[1];
pol[i] = atan2(p2[i].y, p2[i].x);
id[i] = i;
}
tot = collinear = 0;
sort(id + 2, id + n + 1, polar_cmp);
if (collinear) {
printf("0.0000000000\n");
return;
}
for (int i = 2; i <= n; ++i) {
id[n + i - 1] = id[i];
}
for (int i = 2, j = i; i <= n; ++i) {
if (j < i) j = i;
while (j + 1 < n + i - 1 && sgn(p2[id[i]] ^ p2[id[j + 1]]) >= 0) {
++j;
}
if (j > i) {
if (sgn(p2[id[i]] ^ p2[id[j]]) == 0) {
printf("0.0000000000\n");
return;
}
l[++tot] = line(p[id[i]], p[id[i + 1]]);
l[++tot] = line(p[id[i]], p[id[j]]);
}
}
l[++tot] = line(point(X, X), point(-X, X));
l[++tot] = line(point(-X, X), point(-X, -X));
l[++tot] = line(point(-X, -X), point(X, -X));
l[++tot] = line(point(X, -X), point(X, X));
printf("%.10lf\n", (double)HalfPlane());
}
int main() {
int T;
read(T);
while (T--) {
init();
solve();
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const int MAX = 555;
int in[MAX], out[MAX], v[MAX], s[MAX], w[MAX], n;
int pd[MAX][1010], ind[MAX];
bool cmp(int a, int b) {
if (in[a] != in[b]) return in[a] < in[b];
return out[a] > out[b];
}
int solve(int x, int S, int u, vector<int>& dp);
int go(int id, int max_w) {
if (id == n) return 0;
if (~pd[id][max_w]) return pd[id][max_w];
int ret = 0, u = ind[id];
int n_max = min(s[u], max_w - w[u]);
vector<int> dp(n, -1);
for (int i = id + 1; i < n; ++i) {
int v = ind[i];
if (out[v] <= out[u] && w[v] <= n_max) {
ret = max(ret, solve(i, n_max, u, dp));
}
}
ret = ret + v[u];
return pd[id][max_w] = ret;
}
int solve(int x, int S, int u, vector<int>& dp) {
if (~dp[x]) return dp[x];
dp[x] = 0;
for (int i = x + 1; i < n; ++i)
if (in[ind[i]] >= out[ind[x]] && out[ind[i]] <= out[u] && w[ind[i]] <= S) {
dp[x] = max(dp[x], solve(i, S, u, dp));
}
return dp[x] = dp[x] + go(x, S);
}
int main() {
int S;
scanf("%d %d", &n, &S);
for (int(i) = (0); (i) < (n); ++(i))
scanf("%d %d %d %d %d", &in[i], &out[i], &w[i], &s[i], &v[i]);
for (int(i) = (0); (i) < (n); ++(i)) ind[i] = i;
sort(ind, ind + n, cmp);
memset((pd), (-1), sizeof(pd));
vector<int> dp(n, -1);
int rsp = 0;
out[n] = 2 * n + 1;
for (int(i) = (0); (i) < (n); ++(i)) {
if (w[ind[i]] <= S) rsp = max(rsp, solve(i, S, n, dp));
}
printf("%d\n", rsp);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void smain();
int main() {
smain();
return 0;
}
const long long INF = 10000000000;
struct sNode {
sNode *left, *right;
long long val, min, max;
} nodes[1000000];
long long nodesCnt = 0;
sNode* addNode() {
sNode* node = nodes + nodesCnt;
node->left = 0;
node->right = 0;
node->val = -1;
node->max = -1;
node->min = INF;
nodesCnt++;
return node;
}
long long getMin(sNode* node) {
if (node == 0) return INF;
return node->min;
}
long long getMax(sNode* node) {
if (node == 0) return -1;
return node->max;
}
void update(sNode* node) {
if (node) {
if (node->val == -1) {
node->min = min(getMin(node->left), getMin(node->right));
node->max = max(getMax(node->left), getMax(node->right));
} else {
node->min = node->max = node->val;
}
}
}
void push(sNode*& node, long long val, long long pos, long long ll,
long long rr) {
if (ll > rr || pos < ll || pos > rr) return;
if (node == 0) node = addNode();
if (ll < rr) {
long long mm = (ll + rr) / 2;
if (pos <= mm)
push(node->left, val, pos, ll, mm);
else
push(node->right, val, pos, mm + 1, rr);
} else
node->val = val;
update(node);
}
long long getValMin(sNode* node, long long lpos, long long rpos, long long ll,
long long rr) {
if (!node || lpos > rr || rpos < ll) return INF;
if (ll >= lpos && rr <= rpos) return node->min;
long long mm = (ll + rr) / 2;
return min(getValMin(node->left, lpos, rpos, ll, mm),
getValMin(node->right, lpos, rpos, mm + 1, rr));
}
long long getValMax(sNode* node, long long lpos, long long rpos, long long ll,
long long rr) {
if (!node || lpos > rr || rpos < ll) return -1;
if (ll >= lpos && rr <= rpos) return node->max;
long long mm = (ll + rr) / 2;
return max(getValMax(node->left, lpos, rpos, ll, mm),
getValMax(node->right, lpos, rpos, mm + 1, rr));
}
sNode* tree = 0;
void smain() {
long long n;
cin >> n;
vector<pair<long long, long long> > events(n);
vector<long long> res(n, -1);
for (long long i = 0; i < n; i++) {
cin >> events[i].first;
events[i].second = i;
}
sort(events.begin(), events.end());
long long ll = 0, rr = 0;
long long left, right;
while (ll < n) {
rr = ll;
while (rr < n && events[ll].first == events[rr].first) rr++;
rr--;
for (long long i = ll; i <= rr; i++) {
left = getValMax(tree, 0, events[i].second - 1, 0, n - 1);
right = getValMin(tree, events[i].second + 1, n - 1, 0, n - 1);
if (left == -1)
left = 0;
else
left++;
if (right == INF)
right = n - 1;
else
right--;
res[right - left] = max(res[right - left], events[i].first);
}
for (long long i = ll; i <= rr; i++) {
push(tree, events[i].second, events[i].second, 0, n - 1);
}
ll = rr + 1;
}
long long val = res[n - 1];
for (long long i = n - 1; i >= 0; i--) {
val = max(val, res[i]);
res[i] = val;
}
for (long long i = 0; i < n; i++) cout << res[i] << ' ';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
if (a == b)
cout << a;
else
cout << 1;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int a[n];
vector<int> v;
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = n - 1; i >= 0;) {
v.push_back(a[i]);
i = i - a[i];
}
printf("%d\n", v.size());
for (int i = v.size() - 1; i >= 0; i--) printf("%d ", v[i]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct NodeData {
long long td, tu, sz, szU, lvl;
vector<int> T;
vector<int> adj;
NodeData() { T = vector<int>(21, -1); }
};
vector<NodeData> V;
int lca(int u, int v) {
if (V[u].lvl > V[v].lvl) swap(u, v);
for (int i = 20; i >= 0; --i) {
if (V[v].lvl - V[u].lvl >= (1 << i)) {
v = V[v].T[i];
}
}
if (v == u) return u;
for (int i = 20; i >= 0; --i) {
if (V[v].T[i] != V[u].T[i]) {
u = V[u].T[i];
v = V[v].T[i];
}
}
return V[u].T[0];
}
void dfsD(int u, int p = -1) {
V[u].sz = 1;
V[u].td = 0;
V[u].T[0] = p;
V[u].lvl = p == -1 ? 0 : (V[p].lvl + 1);
for (int i = 0; i < V[u].adj.size(); ++i) {
int v = V[u].adj[i];
if (v == p) continue;
dfsD(v, u);
V[u].sz += V[v].sz;
V[u].td += V[v].td + V[v].sz;
}
}
void dfsU(int u, int p = -1) {
if (p == -1) {
V[u].tu = 0;
V[u].szU = 0;
} else {
V[u].szU = V[p].szU + V[p].sz - V[u].sz;
V[u].tu = V[p].tu + (V[p].td - (V[u].td + V[u].sz)) + V[u].szU;
}
for (int i = 0; i < V[u].adj.size(); ++i) {
int v = V[u].adj[i];
if (v == p) continue;
dfsU(v, u);
}
}
double solveDiff(int u, int v, int lc) {
int d = V[u].lvl + V[v].lvl - 2 * V[lc].lvl;
double e = V[u].td * V[v].sz + V[v].td * V[u].sz;
return e / (V[u].sz * V[v].sz) + d + 1;
}
double solveSame(int u, int v, int lc) {
if (V[v].lvl < V[u].lvl) swap(u, v);
int d = V[u].lvl + V[v].lvl - 2 * V[lc].lvl;
int w = v;
for (int i = 20; i >= 0; --i) {
int pp = V[w].T[i];
if (pp != -1 && V[pp].lvl > V[u].lvl) w = pp;
}
double e = V[w].tu * V[v].sz + V[v].td * V[w].szU;
return e / (V[w].szU * V[v].sz) + d;
}
int main() {
int N, M;
cin >> N >> M;
V = vector<NodeData>(N);
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
u--;
v--;
V[u].adj.push_back(v);
V[v].adj.push_back(u);
}
dfsD(0);
dfsU(0);
for (int j = 1; j < 20; ++j) {
for (int i = 0; i < N; ++i) {
int pp = V[i].T[j - 1];
if (pp == -1) continue;
V[i].T[j] = V[pp].T[j - 1];
}
}
for (int i = 0; i < M; ++i) {
int u, v;
cin >> u >> v;
u--;
v--;
int lc = lca(u, v);
if (lc == u || lc == v) {
printf("%0.10lf\n", solveSame(u, v, lc));
} else {
printf("%0.10lf\n", solveDiff(u, v, lc));
}
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long X;
int size_num = 0;
vector<int> num;
vector<pair<int, long long> > result;
cin >> X;
if (X < 10) {
cout << X << endl;
return 0;
}
long long ind = X;
int curr_sum = 0;
while (ind != 0) {
num.push_back(ind % 10);
++size_num;
curr_sum += ind % 10;
ind /= 10;
}
long long n_1_choice = 9;
int sum_n_1_choice = 9 * (size_num - 1);
int des = 1;
for (int i = 0; i < size_num - 2; ++i) {
n_1_choice *= 10;
n_1_choice += 9;
}
long long s_choice = num[size_num - 1];
s_choice *= 10;
s_choice += num[size_num - 2] - 1;
for (int i = 0; i < size_num - 2; ++i) {
s_choice *= 10;
s_choice += 9;
}
int s_sum = 0;
long long ind_s = s_choice;
while (ind_s != 0) {
s_sum += ind_s % 10;
ind_s /= 10;
}
long long f_choice = num[size_num - 1] - 1;
for (int i = 0; i < size_num - 1; ++i) {
f_choice *= 10;
f_choice += 9;
}
int f_sum = 0;
long long ind_f = f_choice;
while (ind_f != 0) {
f_sum += ind_f % 10;
ind_f /= 10;
}
long long t_choice = num[size_num - 1];
int t_sum = 0;
int pos = 1;
vector<int> l;
for (int i = num.size() - 1; i >= 0; --i) l.push_back(num[i]);
while (pos < size_num && l[pos] == 9) ++pos;
if ((pos != size_num) && (pos != 1)) {
for (int k = 1; k < pos - 1; ++k) {
t_choice *= 10;
t_choice += 9;
}
t_choice *= 10;
t_choice += 8;
for (int k = pos; k < size_num; ++k) {
t_choice *= 10;
t_choice += 9;
}
long long c = t_choice;
while (c != 0) {
t_sum += c % 10;
c /= 10;
}
result.push_back(make_pair(t_sum, t_choice));
}
result.push_back(make_pair(s_sum, s_choice));
result.push_back(make_pair(sum_n_1_choice, n_1_choice));
result.push_back(make_pair(curr_sum, X));
result.push_back(make_pair(f_sum, f_choice));
sort(result.begin(), result.end());
cout << result[result.size() - 1].second << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 2;
const long long INF = 1e9 + 2;
const long long MOD = 1e9 + 7;
long long n, k, dp[maxn][maxn];
long long mem(int x, int num) {
if (dp[x][num] != -1) return dp[x][num];
if (num == 0) return dp[x][num] = 1, 1;
dp[x][num] = 0;
for (int i = 1; i * x <= n; i++)
dp[x][num] = (mem(x * i, num - 1) + dp[x][num]) % MOD;
return dp[x][num];
}
int main() {
cin >> n >> k;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= k; j++) dp[i][j] = -1;
cout << mem(1, k);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5;
long long N, K, A[MAXN], Left, Right, L[MAXN], R[MAXN], Cnt, Ans;
deque<long long> QMAX, QMIN;
long long QP(long long B, long long K, long long Mod) {
long long Ans = 1;
for (; K; K >>= 1, B = B * B % Mod)
if (K & 1) Ans = Ans * B % Mod;
return Ans;
}
template <class T>
void Read(T &X) {
X = 0;
int F = 0;
char Ch = getchar();
while (Ch < '0' || Ch > '9') {
F |= (Ch == '-');
Ch = getchar();
}
while (Ch >= '0' && Ch <= '9') {
X = X * 10 + (Ch ^ 48);
Ch = getchar();
}
X = F ? -X : X;
}
long long GCD(long long X, long long Y) { return Y == 0 ? X : GCD(Y, X % Y); }
int main() {
Read(N);
Read(K);
for (long long i = 1; i <= N; i++) Read(A[i]);
Left = 1;
Right = 0;
while (Right < N) {
Right++;
while (!QMAX.empty() && A[QMAX.back()] <= A[Right]) QMAX.pop_back();
while (!QMIN.empty() && A[QMIN.back()] >= A[Right]) QMIN.pop_back();
QMAX.push_back(Right);
QMIN.push_back(Right);
while (!QMAX.empty() && !QMIN.empty() &&
A[QMAX.front()] - A[QMIN.front()] > K) {
if (QMAX.front() < QMIN.front()) {
Left = QMAX.front() + 1;
QMAX.pop_front();
} else {
Left = QMIN.front() + 1;
QMIN.pop_front();
}
}
if (Right - Left + 1 > Ans) {
Ans = Right - Left + 1;
Cnt = 1;
L[Cnt] = Left;
R[Cnt] = Right;
} else if (Right - Left + 1 == Ans) {
Cnt++;
L[Cnt] = Left;
R[Cnt] = Right;
}
}
printf("%lld %lld\n", Ans, Cnt);
for (long long i = 1; i <= Cnt; i++) {
printf("%lld %lld\n", L[i], R[i]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 2e5 + 2;
vector<int> g[N], g2[N];
int lvl[N], k, p[N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
g[--x].push_back(--y);
g2[y].push_back(x);
}
cin >> k;
for (int i = 0; i < k; i++) cin >> p[i], p[i]--;
for (int i = 0; i < n; i++) lvl[i] = 1e9;
queue<int> q;
lvl[p[k - 1]] = 0;
q.push(p[k - 1]);
while (q.size()) {
int node = q.front();
q.pop();
for (auto to : g2[node]) {
if (lvl[to] == 1e9) {
lvl[to] = lvl[node] + 1;
q.push(to);
}
}
}
int mn = 0, mx = 0;
for (int i = 0; i < k - 1; i++) {
int cnt = 0;
for (auto to : g[p[i]]) {
if (lvl[p[i]] == lvl[to] + 1) cnt++;
}
if (lvl[p[i]] <= lvl[p[i + 1]])
mn++, mx++;
else if (cnt > 1)
mx++;
}
cout << mn << ' ' << mx;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long m = 1000003) {
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
vector<long long> a;
long long inverse(long long x) { return power(x, 1000003 - 2); }
long long forwardElim(long long mat[11][11 + 1]);
vector<long long> backSub(long long mat[11][11 + 1]);
vector<long long> gaussianElimination(long long mat[11][11 + 1]) {
forwardElim(mat);
return backSub(mat);
}
void swap_row(long long mat[11][11 + 1], long long i, long long j) {
for (int k = 0; k <= 11; k++) swap(mat[i][k], mat[j][k]);
}
long long forwardElim(long long mat[11][11 + 1]) {
for (int k = 0; k < 11; k++) {
long long i_max = k;
long long v_max = mat[i_max][k];
for (int i = k + 1; i < 11; i++)
if (abs(mat[i][k]) > v_max) v_max = mat[i][k], i_max = i;
if (!mat[k][i_max]) return k;
if (i_max != k) swap_row(mat, k, i_max);
for (int i = k + 1; i < 11; i++) {
long long f = mat[i][k] * inverse(mat[k][k]);
f %= 1000003;
for (int j = k + 1; j <= 11; j++) {
mat[i][j] -= mat[k][j] * f;
mat[i][j] %= 1000003;
if (mat[i][j] < 0) mat[i][j] += 1000003;
}
mat[i][k] = 0;
}
}
}
vector<long long> backSub(long long mat[11][11 + 1]) {
vector<long long> x(11);
for (int i = 11 - 1; i >= 0; i--) {
x[i] = mat[i][11];
for (int j = i + 1; j < 11; j++) {
x[i] -= mat[i][j] * x[j];
x[i] %= 1000003;
if (x[i] < 0) x[i] += 1000003;
}
x[i] = x[i] * inverse(mat[i][i]);
x[i] %= 1000003;
}
return x;
}
int query(int x) {
cout << "? " << x << endl;
fflush(stdout);
int res;
cin >> res;
return res;
}
int main() {
long long mat[11][11 + 1];
for (int i = 0; i < 11; i++) {
long long res = query(i);
long long t = 1;
for (int j = 0; j < 11; j++) {
mat[i][j] = t;
t = (t * i) % 1000003;
}
mat[i][11] = res;
}
a = gaussianElimination(mat);
for (int i = 0; i < 1000003; i++) {
long long res = 0;
for (int j = 11 - 1; j >= 0; j--) {
res = i * res + a[j];
res %= 1000003;
if (res < 0) res += 1000003;
}
if (res == 0) {
cout << "! " << i << endl;
fflush(stdout);
return 0;
}
}
cout << "! " << -1 << endl;
fflush(stdout);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
int L, b, f;
struct car {
int s, t, i;
};
list<car> car;
int park(int x, int i) {
int x0 = -b;
for (auto it = car.begin(); it != car.end(); it++) {
if (x0 + b + x + f <= it->s) {
car.insert(it, {x0 + b, x0 + b + x, i});
return x0 + b;
}
x0 = it->t;
}
if (x0 + b + x <= L) {
car.push_back({x0 + b, x0 + b + x, i});
return x0 + b;
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed;
int n;
cin >> L >> b >> f >> n;
for (int i = 1; i <= n; i++) {
int t, x;
cin >> t >> x;
if (t == 1) {
cout << park(x, i) << endl;
} else {
assert(t == 2);
for (auto it = car.begin(); it != car.end(); it++) {
if (it->i == x) {
car.erase(it);
break;
}
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long cmax(long long a, long long b) { return (a > b ? a : b); }
long long cmax(long long a, long long b, long long c) {
return cmax(cmax(a, b), c);
}
long long cmin(long long a, long long b) { return (a < b ? a : b); }
long long cmin(long long a, long long b, long long c) {
return cmin(cmin(a, b), c);
}
int dfs(vector<long long> *g, bool *v, long long curr, long long p) {
v[curr] = true;
int cycle = 0;
for (long long i = 0; i < g[curr].size(); i++) {
if (v[g[curr][i]]) {
if (g[curr][i] != p) {
cycle = 1;
}
} else {
cycle = dfs(g, v, g[curr][i], curr) || cycle;
}
}
return cycle;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
vector<long long> *g = new vector<long long>[n + 1];
for (long long i = 0; i < m; i++) {
long long x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
bool *v = new bool[n + 1];
for (long long i = 1; i <= n; i++) {
v[i] = false;
}
bool done = false;
long long ans = 0, first_unv = 1;
while (!done) {
ans += !dfs(g, v, first_unv, -1);
while (v[first_unv]) {
++first_unv;
if (first_unv == n + 1) {
done = true;
break;
}
}
}
cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using vint = vector<long long>;
using vvint = vector<vint>;
using pint = pair<long long, long long>;
using vpint = vector<pint>;
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr long long DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr long long DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
long long sign(long long x) { return (x > 0) - (x < 0); }
long long gcd(long long a, long long b) {
while (b) {
swap(a %= b, b);
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long cdiv(long long a, long long b) { return (a - 1 + b) / b; }
template <typename T>
void fin(T mes) {
cout << mes << endl;
exit(0);
}
template <typename T>
T sq(T x) {
return x * x;
}
template <typename T, typename U>
bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T, typename U>
bool chmin(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
os << "(" << rhs.first << ", " << rhs.second << ")";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &rhs) {
os << "{";
for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
os << *itr << (next(itr) != rhs.end() ? ", " : "");
}
os << "}";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const deque<T> &rhs) {
os << "{";
for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
os << *itr << (next(itr) != rhs.end() ? ", " : "");
}
os << "}";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &rhs) {
os << "{";
for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
os << *itr << (next(itr) != rhs.end() ? ", " : "");
}
os << "}";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &rhs) {
os << "{";
for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
os << *itr << (next(itr) != rhs.end() ? ", " : "");
}
os << "}";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &rhs) {
os << "{";
for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
os << *itr << (next(itr) != rhs.end() ? ", " : "");
}
os << "}";
return os;
}
struct setup {
static constexpr long long PREC = 20;
setup() {
cout << fixed << setprecision(PREC);
cerr << fixed << setprecision(PREC);
};
} setup;
template <long long MOD = 1000000007>
struct mod_int {
long long val;
mod_int(long long val_ = 0) : val(val_ % MOD) {
if (val < 0) {
val += MOD;
}
}
bool operator==(const mod_int &rhs) const { return val == rhs.val; }
bool operator!=(const mod_int &rhs) const {
return std::rel_ops::operator!=(*this, rhs);
}
mod_int &operator+=(const mod_int &rhs) {
if ((val += rhs.val) >= MOD) {
val -= MOD;
}
return *this;
}
mod_int &operator-=(const mod_int &rhs) {
if ((val += MOD - rhs.val) >= MOD) {
val -= MOD;
}
return *this;
}
mod_int &operator*=(const mod_int &rhs) {
val = (long long)((long long)val * rhs.val % MOD);
return *this;
}
mod_int &operator/=(const mod_int &rhs) { return *this *= rhs.inv(); }
mod_int operator+() const { return *this; }
mod_int operator-() const { return -val; }
mod_int operator++() { return *this += 1; }
mod_int operator--() { return *this -= 1; }
mod_int operator++(signed) {
const mod_int ret(*this);
++*this;
return ret;
}
mod_int operator--(signed) {
const mod_int ret(*this);
--*this;
return ret;
}
mod_int operator+(const mod_int &rhs) const { return mod_int(*this) += rhs; }
mod_int operator-(const mod_int &rhs) const { return mod_int(*this) -= rhs; }
mod_int operator*(const mod_int &rhs) const { return mod_int(*this) *= rhs; }
mod_int operator/(const mod_int &rhs) const { return mod_int(*this) /= rhs; }
mod_int inv() const {
assert(val != 0);
long long a = val, b = MOD, x = 1, u = 0;
while (b) {
long long t = a / b;
std::swap(a -= t * b, b);
std::swap(x -= t * u, u);
}
return x;
}
mod_int pow(long long n) const {
if (n < 0) {
return pow(-n).inv();
}
mod_int ret = 1, mul = *this;
while (n) {
if (n & 1) {
ret *= mul;
}
mul *= mul;
n >>= 1;
}
return ret;
}
friend std::istream &operator>>(std::istream &is, mod_int &rhs) {
long long v;
is >> v;
rhs = v;
return is;
}
friend std::ostream &operator<<(std::ostream &os, const mod_int &rhs) {
return os << rhs.val;
}
struct combination {
std::vector<mod_int> fact{1, 1}, f_inv{1, 1}, inv{0, 1};
void calc(long long n) {
while (fact.size() <= n) {
long long i = fact.size();
fact.push_back(fact[i - 1] * i);
inv.push_back(-inv[MOD % i] * (MOD / i));
f_inv.push_back(f_inv[i - 1] * inv[i]);
}
}
mod_int P(long long n, long long r) {
return r < 0 || n < r ? 0 : (calc(n), fact[n] * f_inv[n - r]);
}
mod_int C(long long n, long long r) {
return r < 0 || n < r ? 0 : (calc(n), fact[n] * f_inv[r] * f_inv[n - r]);
}
mod_int H(long long n, long long r) { return C(n + r - 1, r); }
};
};
using mint = mod_int<998244353>;
signed main() {
long long n, m;
cin >> n >> m;
vint d(n), a(m), b(m);
for (long long i = 0; i < (long long)(n); i++) {
cin >> d[i];
}
for (long long i = 0; i < (long long)(m); i++) {
cin >> a[i] >> b[i];
}
vector<tuple<long long, long long, long long>> ev;
for (long long i = 0; i < (long long)(n); i++) {
ev.emplace_back(d[i], 1, i);
}
for (long long i = 0; i < (long long)(m); i++) {
ev.emplace_back(b[i], 0, i);
}
sort((ev).begin(), (ev).end());
vector<mint> ans(m);
long long big = n;
mint X = 0, Y = 0;
for (long long i = 0; i < (long long)(n); i++) {
X += d[i];
}
for (auto [val, type, idx] : ev) {
if (type == 1) {
big--;
X -= val, Y += val;
} else {
if (big == 0) {
ans[idx] = 0;
} else {
ans[idx] = X * max(0LL, big - a[idx]) / big +
Y * max(0LL, big + 1 - a[idx]) / (big + 1);
}
}
}
for (mint x : ans) {
cout << x << endl;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const int MOD = 998244353;
inline int add(int u, int v) {
u += v;
if (u >= MOD) u -= MOD;
return u;
}
inline int sub(int u, int v) {
u -= v;
if (u < 0) u += MOD;
return u;
}
inline int mul(int u, int v) { return (long long)u * v % MOD; }
inline int power(int u, int v) {
int res = 1;
while (v) {
if (v & 1) res = mul(res, u);
u = mul(u, u);
v >>= 1;
}
return res;
}
inline int inv(int u) { return power(u, MOD - 2); }
int n, k;
int sz;
struct NTT {
int base = 1;
int maxBase = 0;
int root = 2;
vector<int> w = {0, 1};
vector<int> rev = {0, 1};
NTT() {
int u = MOD - 1;
while (u % 2 == 0) {
u >>= 1;
maxBase++;
}
while (1) {
if (power(root, 1 << maxBase) == 1 &&
power(root, 1 << (maxBase - 1)) != 1) {
break;
}
root++;
}
}
void ensure(int curBase) {
assert(curBase <= maxBase);
if (curBase <= base) return;
rev.resize(1 << curBase);
for (int i = 0; i < (1 << curBase); i++) {
rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (curBase - 1));
}
w.resize(1 << curBase);
for (; base < curBase; base++) {
int wc = power(root, 1 << (maxBase - base - 1));
for (int i = 1 << (base - 1); i < (1 << base); i++) {
w[i << 1] = w[i];
w[i << 1 | 1] = mul(w[i], wc);
}
}
}
void fft(vector<int> &a) {
int n = a.size();
int curBase = 0;
while ((1 << curBase) < n) curBase++;
int shift = base - curBase;
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 < k; i++) {
for (int j = i; j < n; j += k * 2) {
int foo = a[j];
int bar = mul(a[j + k], w[i + k]);
a[j] = add(foo, bar);
a[j + k] = sub(foo, bar);
}
}
}
}
vector<int> mult(vector<int> a, vector<int> b) {
int nResult = a.size() + b.size() - 1;
int curBase = 0;
while ((1 << curBase) < nResult) curBase++;
ensure(curBase);
a.resize(1 << curBase), b.resize(1 << curBase);
fft(a);
fft(b);
for (int i = 0; i < (1 << curBase); i++) {
a[i] = mul(mul(a[i], b[i]), inv(1 << curBase));
}
reverse(a.begin() + 1, a.end());
fft(a);
a.resize(nResult);
return a;
}
vector<int> sqr(vector<int> a) {
int nResult = a.size() + a.size() - 1;
int curBase = 0;
while ((1 << curBase) < nResult) curBase++;
ensure(curBase);
a.resize(1 << curBase);
fft(a);
for (int i = 0; i < (1 << curBase); i++) {
a[i] = mul(mul(a[i], a[i]), inv(1 << curBase));
}
reverse(a.begin() + 1, a.end());
fft(a);
a.resize(nResult);
while (!a.empty() && !a.back()) a.pop_back();
return a;
}
} ntt;
vector<int> go(vector<int> &u, int v) {
if (v == 1) return u;
vector<int> foo = go(u, v >> 1);
vector<int> res = ntt.sqr(foo);
if (v & 1) {
vector<int> now(res.size() + u.size() - 1, 0);
for (int i = 0; i < res.size(); i++) {
for (int j = 0; j < u.size(); j++) {
now[i + j] = add(now[i + j], mul(res[i], u[j]));
}
}
return now;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
sz = 9 * n / 2 + 1;
vector<int> a(10, 0);
for (int i = 1; i <= k; i++) {
int u;
cin >> u;
a[u] = 1;
}
vector<int> res = go(a, n / 2);
int ans = 0;
for (int i = 0; i < res.size(); i++) {
ans = add(ans, mul(res[i], res[i]));
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
template <typename Tp>
inline void read(Tp &x) {
static char c;
static bool neg;
x = 0, c = getchar(), neg = false;
for (; !isdigit(c); c = getchar()) {
if (c == '-') {
neg = true;
}
}
for (; isdigit(c); c = getchar()) {
x = x * 10 + c - '0';
}
if (neg) {
x = -x;
}
}
const int INF = 0x3f3f3f3f;
const int N = 4;
const int dx[] = {0, 0, 1, 1};
const int dy[] = {0, 1, 1, 0};
inline void work() {
static int x[N], y[N], p[N], dis[N], nx[N], ny[N];
static int ansX[N], ansY[N];
for (int i = 0; i < N; ++i) {
read(x[i]), read(y[i]);
}
vector<int> vec;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
vec.emplace_back(std::abs(x[i] - x[j]));
vec.emplace_back(std::abs(y[i] - y[j]));
}
}
int ans = INF;
for (const auto &d : vec) {
vector<pair<int, int> > points;
for (int i = 0; i < N; ++i) {
for (int di = 0; di < 2; ++di) {
for (int j = 0; j < N; ++j) {
for (int dj = 0; dj < 2; ++dj) {
points.emplace_back((pair<int, int>){x[i] - di * d, y[j] - dj * d});
}
}
}
}
for (int i = 0; i < N; ++i) {
p[i] = i;
}
do {
for (int i = 0; i < N; ++i) {
nx[i] = x[p[i]] - dx[i] * d;
ny[i] = y[p[i]] - dy[i] * d;
}
auto px = minmax_element(nx, nx + N), py = minmax_element(ny, ny + N);
if (*px.first == *px.second) {
points.emplace_back(*px.first, (*py.first + *py.second) / 2);
}
if (*py.first == *py.second) {
points.emplace_back((*px.first + *px.second) / 2, *py.first);
}
} while (next_permutation(p, p + N));
for (const auto &pir : points) {
int tx = pir.first, ty = pir.second;
for (int i = 0; i < N; ++i) {
p[i] = i;
}
do {
bool valid = true;
for (int i = 0, x0, y0, x1, y1; i < N; ++i) {
x0 = tx + dx[i] * d, y0 = ty + dy[i] * d;
x1 = x[p[i]], y1 = y[p[i]];
if (x0 == x1) {
dis[i] = std::abs(y0 - y1);
} else if (y0 == y1) {
dis[i] = std::abs(x0 - x1);
} else {
valid = false;
break;
}
}
if (valid) {
int res = *max_element(dis, dis + N);
if (ans > res) {
ans = res;
for (int i = 0; i < N; ++i) {
ansX[p[i]] = tx + dx[i] * d;
ansY[p[i]] = ty + dy[i] * d;
}
}
}
} while (next_permutation(p, p + N));
}
}
if (ans == INF) {
puts("-1");
} else {
printf("%d\n", ans);
for (int i = 0; i < N; ++i) {
printf("%d %d\n", ansX[i], ansY[i]);
}
}
}
int T;
int main() {
read(T);
for (int cas = 0; cas < T; ++cas) {
work();
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = double;
const int MQ = 1e5 + 100;
const int MV = 1e9 + 100;
int p[MQ << 1], P;
const ld EPS = 1e-4;
bool equ(ld a, ld b) { return fabs(a - b) < EPS; }
struct DT {
public:
int l, r;
ll v, u;
int z;
DT() : l(0), r(0), v(0LL), u(0LL), z(MV) {}
DT(int l, int r) : l(l), r(r), v(0LL), u(0LL), z(MV) {}
void set(int l, int r) { this->l = l, this->r = r; }
ld operator[](ll& q) const {
if (q == 0) return l;
assert(q + v <= 0);
return l + static_cast<ld>(q) * (r - l) / (-v);
}
};
template <int MS = MQ << 3>
struct ST {
public:
DT v[MS];
int s;
void sv(int n, int d) {
v[n].v = static_cast<ll>(v[n].r - v[n].l) * d;
v[n].u = d < 0 ? v[n].v : 0LL;
}
void sz(int n, int d) {
sv(n, d);
v[n].z = d;
}
void down(int n, int l, int r) {
if (v[n].z == MV) return;
if (r - l > 1) sz(n << 1, v[n].z), sz(n << 1 | 1, v[n].z);
v[n].z = MV;
}
void up(int n) {
v[n].v = v[n << 1].v + v[n << 1 | 1].v;
v[n].u = min(v[n << 1].u, v[n << 1].v + v[n << 1 | 1].u);
}
void init(int n, int l, int r) {
if (r - l > 1) {
int m = l + ((r - l) >> 1);
init(n << 1, l, m), init(n << 1 | 1, m, r);
}
v[n].set(p[l], p[r]);
}
void init(int S) {
s = S;
init(1, 0, s);
}
void upd(int n, int l, int r, int ql, int qr, int q) {
if (ql <= l && r <= qr)
sz(n, q);
else {
down(n, l, r);
int m = l + ((r - l) >> 1);
if (ql < m) upd(n << 1, l, m, ql, qr, q);
if (m < qr) upd(n << 1 | 1, m, r, ql, qr, q);
up(n);
}
}
void upd(int ql, int qr, int q) { upd(1, 0, s, ql, qr, q); }
ld qry(int n, int l, int r, int ql, int qr, ll& q) {
down(n, l, r);
if (ql <= l && r <= qr) {
if (q + v[n].u > 0) return q += v[n].v, -1.0;
if (r - l > 1) {
int m = l + ((r - l) >> 1);
if (q + v[n << 1].u <= 0)
return qry(n << 1, l, m, ql, qr, q);
else
return qry(n << 1 | 1, m, r, ql, qr, q += v[n << 1].v);
} else
return v[n][q];
} else {
int m = l + ((r - l) >> 1);
ld f = -1.0;
if (ql < m) {
f = qry(n << 1, l, m, ql, qr, q);
if (!equ(f, -1.0)) return f;
}
if (m < qr) return qry(n << 1 | 1, m, r, ql, qr, q);
return -1.0;
}
}
ld qry(int ql, int qr, ll& q) { return qry(1, 0, s, ql, qr, q); }
};
ST<> st;
int Q;
int q[MQ][4];
void cmpr() {
sort(p, p + P);
P = unique(p, p + P) - p;
for (int i = 0; i < Q; i++) {
q[i][1] = lower_bound(p, p + P, q[i][1]) - p;
if (q[i][0] == 3) q[i][2] = lower_bound(p, p + P, q[i][2]) - p;
}
p[P] = MV;
}
using i2 = array<int, 2>;
bool operator<(const i2& a, const i2& b) {
return a[0] < b[0] || (!(b[0] < a[0]) && a[1] < b[1]);
}
using si = set<i2>;
using sit = si::iterator;
si v;
void ins(int t, int s) {
sit l = v.insert({t, s}).first;
st.upd(t, (++l)->at(0), s);
}
void rem(int t) {
sit p = v.lower_bound({t, -MV}), n = p;
assert(p->at(0) == t);
n = v.erase(n), p = n;
if (p == v.begin())
st.upd(t, n->at(0), 0);
else
st.upd(t, n->at(0), (--p)->at(1));
}
int qry(int l, int r, ll& q) {
if (q <= 0) return printf("%d\n", p[l]);
l = v.lower_bound({l, -MV})->at(0);
if (l >= r) return printf("-1\n"), 0;
ld x = st.qry(l, r, q);
if (equ(x, -1))
printf("-1\n");
else
printf("%.15f\n", x);
return 0;
}
void solve() {
v.insert({P, -1});
ll t;
for (int i = 0; i < Q; i++) {
int* c = q[i];
if (c[0] == 1) ins(c[1], c[2]);
if (c[0] == 2) rem(c[1]);
if (c[0] == 3) qry(c[1], c[2], t = static_cast<ll>(c[3]));
}
}
int main() {
scanf("%d", &Q);
P = 0;
for (int i = 0; i < Q; i++) {
scanf("%d", q[i]);
if (q[i][0] == 1) scanf("%d%d", q[i] + 1, q[i] + 2), p[P++] = q[i][1];
if (q[i][0] == 2) scanf("%d", q[i] + 1);
if (q[i][0] == 3)
scanf("%d%d%d", q[i] + 1, q[i] + 2, q[i] + 3), p[P++] = q[i][1],
p[P++] = q[i][2];
}
cmpr();
st.init(P);
solve();
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
const int M = N * 10;
const long double INF = 1e18;
int n;
long double c, t;
pair<int, int> a[N];
long double dp[2][N][M];
long double p[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
p[0] = 1.;
for (int i = 1; i < N; i++) {
p[i] = p[i - 1] * 10. / 9.;
}
int tc;
cin >> tc;
while (tc--) {
cin >> n;
cin >> c >> t;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + n + 1, greater<pair<int, int> >());
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n * 10; j++) {
dp[0][i][j] = dp[1][i][j] = INF;
}
}
dp[0][0][0] = 0.;
int cur = 1;
for (int i = 1; i <= n; i++, cur ^= 1) {
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n * 10; k++) {
dp[cur][j][k] = dp[cur ^ 1][j][k];
if (j > 0 && k >= a[i].second) {
dp[cur][j][k] =
min(dp[cur][j][k],
dp[cur ^ 1][j - 1][k - a[i].second] + p[j] * a[i].first);
}
}
}
}
cur ^= 1;
int res = 0;
for (int j = 0; j <= n; j++) {
for (int k = 0; k <= n * 10; k++) {
long double A = dp[cur][j][k];
long double x = max((sqrt(A * c) - 1.) / c, (long double)0.);
long double tot = (long double)10. * j + x + A / (c * x + 1.);
if (tot <= t) {
res = max(res, k);
}
}
}
cout << res << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
const long long INF = 1e18;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) {
long long n;
cin >> n;
map<long long, long long> mp;
long long mini = INF;
for (long long i = 0; i < n; i++) {
long long a;
cin >> a;
mp[a]++;
}
for (auto& e : mp) {
mini = min(mini, e.second);
}
long long ans = INF;
for (long long i = 1; i <= mini + 1; i++) {
long long fg = 1;
long long curr = 0;
for (auto& e : mp) {
long long x = (e.second + i - 1) / i;
long long y = e.second / i;
y = i - e.second + y * i;
if (y == i) {
y = 0;
}
if (x < y) {
fg = -1;
} else {
curr = curr + x;
}
}
if (fg == 1) {
ans = min(ans, curr);
}
}
cout << ans << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int sol[10005];
char ans[10005];
int main() {
string s, t;
cin >> s;
cout << "? ";
for (int i = 0; i < s.size(); ++i) {
char val = 'a' + (i % 26);
cout << val;
}
cout << endl;
cin >> t;
for (int i = 0; i < s.size(); ++i) {
sol[i] += t[i] - 'a';
}
cout << "? ";
for (int i = 0; i < s.size(); ++i) {
char val = 'a' + (i / 26) % 26;
cout << val;
}
cout << endl;
cin >> t;
for (int i = 0; i < s.size(); ++i) {
sol[i] += (t[i] - 'a') * 26;
}
cout << "? ";
for (int i = 0; i < s.size(); ++i) {
char val = 'a' + (i / (26 * 26)) % 26;
cout << val;
}
cout << endl;
cin >> t;
for (int i = 0; i < s.size(); ++i) {
sol[i] += (t[i] - 'a') * 26 * 26;
}
for (int i = 0; i < s.size(); ++i) {
ans[sol[i]] = s[i];
}
cout << "! " << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n, l, k;
cin >> n >> l;
int arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
cin >> k;
long long int maxx, sum;
maxx = 0;
for (int z = 0; z < 2; z++) {
sum = 0;
multiset<int> s1, s2;
if (z == 1) {
for (int i = 0; i < n; i++) arr[i] = -arr[i];
}
for (int i = 0; i < n; i++) {
if (arr[i] >= 0)
sum += arr[i];
else if (s1.size() < k) {
sum += -arr[i];
s1.insert(arr[i]);
} else if (k && arr[i] < *(--s1.end())) {
sum += -arr[i];
int tmp = *(--s1.end());
s1.erase(s1.find(tmp));
s1.insert(arr[i]);
sum += 2 * tmp;
s2.insert(tmp);
} else {
s2.insert(arr[i]);
sum += arr[i];
}
int ind = i - l;
if (ind >= 0) {
if (arr[ind] >= 0)
sum -= arr[ind];
else if (s1.find(arr[ind]) != s1.end()) {
s1.erase(s1.find(arr[ind]));
sum += arr[ind];
if (!s2.empty()) {
int tmp = *(s2.begin());
s2.erase(s2.find(tmp));
s1.insert(tmp);
sum += -2 * tmp;
}
} else {
s2.erase(s2.find(arr[ind]));
sum += -arr[ind];
}
}
if (i + 1 >= l) maxx = max(maxx, sum);
}
}
cout << maxx << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, k, i, ans, sum, m, j;
int a[100005];
int main() {
cin >> n >> k;
m = 0;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
ans = 1000000000;
for (i = 1; i <= k; i++) {
sum = 0;
for (j = i; j <= n; j += k) {
sum += a[j];
}
if (sum < ans) {
m = i;
ans = sum;
}
}
cout << m << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int arr[150][150];
void solve(int x, int y) {
if (arr[x][y] < 3)
arr[x][y]++;
else {
arr[x][y] = 0;
solve(x + 1, y);
solve(x - 1, y);
solve(x, y + 1);
solve(x, y - 1);
}
}
int main() {
int n, t, x, y;
cin >> n >> t;
for (int i = 0; i < n; i++) solve(65, 65);
for (int i = 0; i < t; i++) {
cin >> x >> y;
if (x < (-65) || x > 65 || y < (-65) || y > 65)
cout << 0 << endl;
else
cout << arr[x + 65][y + 65] << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1), EPS = 1e-9;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, N = 2e5 + 5;
string a, b;
string smallest(string s) {
if (s.length() % 2 == 1) return s;
string s1 = smallest(s.substr(0, s.size() / 2));
string s2 = smallest(s.substr(s.size() / 2, s.size()));
if (s1 < s2)
return s1 + s2;
else
return s2 + s1;
}
int main() {
ios::sync_with_stdio(false);
;
cin >> a >> b;
cout << (smallest(a) == smallest(b) ? "YES" : "NO") << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n, k, sum = 0;
cin >> n >> k;
while (k--) {
int m;
cin >> m;
int a[m];
for (int i = 0; i < m; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
if (a[0] != 1) {
sum += 2 * m - 1;
break;
} else if (a[i] != i + 1) {
sum += 2 * (m - i);
break;
}
}
}
cout << sum;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
T ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
template <class T>
inline T lcm(T a, T b) {
a = abs(a);
b = abs(b);
return (a / gcd(a, b)) * b;
}
template <class T, class X>
inline bool getbit(T a, X i) {
T t = 1;
return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
T t = 1;
return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
T t = 1;
return (a & (~(t << i)));
}
inline long long power(long long a, long long b) {
long long multiply = 1;
for (int i = (1); i <= (b); i++) {
multiply *= a;
}
return multiply;
}
inline long long ABS(long long a) { return (a >= 0) ? a : -a; }
int n, arr[1005], dp[1005][(1 << 8)], lower, cnt[8], where[8][2007], come[1007];
int call(int pos, int mask) {
if (__builtin_popcount(mask) == 8) return 0;
if (pos == n + 1) return -(1 << 30) - 1;
if (dp[pos][mask] != -1) return dp[pos][mask];
int ret = -(1 << 30) - 1;
ret = max(ret, call(pos + 1, mask));
if (getbit(mask, arr[pos]) == 0) {
if (where[arr[pos]][come[pos] + lower - 1] != -1)
ret = max(ret, lower + call(where[arr[pos]][come[pos] + lower - 1] + 1,
setbit(mask, arr[pos])));
if (where[arr[pos]][come[pos] + lower] != -1)
ret = max(ret, lower + 1 +
call(where[arr[pos]][come[pos] + lower] + 1,
setbit(mask, arr[pos])));
}
return dp[pos][mask] = ret;
}
int main() {
scanf("%d", &n);
memset(cnt, 0, sizeof(cnt));
for (int i = (1); i <= (n); i++) {
scanf("%d", &arr[i]);
arr[i]--;
cnt[arr[i]]++;
}
int temp = 0;
int lo = 1, hi = (1 << 30) - 1, mid, best, ans = -(1 << 30) - 1, now;
for (int i = (0); i <= (7); i++) temp += (cnt[i] > 0);
for (int i = (0); i <= (7); i++) hi = min(hi, cnt[i]);
for (int i = (0); i <= (7); i++) cnt[i] = 1;
memset(where, -1, sizeof(where));
for (int i = (1); i <= (n); i++) {
where[arr[i]][cnt[arr[i]]] = i;
come[i] = cnt[arr[i]];
cnt[arr[i]]++;
}
while (hi >= lo) {
mid = (hi + lo) / 2;
lower = mid;
memset(dp, -1, sizeof(dp));
now = call(1, 0);
ans = max(now, ans);
if (now < 0)
hi = mid - 1;
else
lo = mid + 1;
}
ans = max(ans, temp);
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, a, b, c, d, x, y, ans1, ans2;
cin >> n >> a >> b >> c >> d;
x = (a + b);
y = (c + d);
x = abs(x - y);
ans1 = n * (n - x);
x = a + c;
y = b + d;
x = abs(x - y);
ans2 = n * (n - x);
if (min(ans1, ans2) > -1)
cout << min(ans1, ans2) << endl;
else
cout << 0 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T& num) {
char CH;
bool F = false;
for (CH = getchar(); CH < '0' || CH > '9'; F = CH == '-', CH = getchar())
;
for (num = 0; CH >= '0' && CH <= '9';
num = num * 10 + CH - '0', CH = getchar())
;
F && (num = -num);
}
int stk[70], tp;
template <class T>
inline void print(T p) {
if (!p) {
puts("0");
return;
}
while (p) stk[++tp] = p % 10, p /= 10;
while (tp) putchar(stk[tp--] + '0');
putchar('\n');
}
const long long mod = 1e6;
const double PI = acos(-1.0);
const int inf = 1e8;
const int N = 1e4 + 10;
const int maxn = 1005;
const double eps = 1e-10;
char mp[1003][1003];
int vis[1003][1003], dis[1003][1003], dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}, n,
m;
int ans = 0, ex, ey, sx, sy;
struct no {
int x, y;
};
no po;
void makepair(int a, int b) {
po.x = a;
po.y = b;
}
queue<no> qu;
void bfs() {
makepair(ex, ey);
qu.push(po);
memset(dis, -1, sizeof(dis));
;
vis[ex][ey] = 1;
dis[ex][ey] = 0;
while (!qu.empty()) {
no temp = qu.front();
qu.pop();
for (int i = 0; i < 4; i++) {
int fx = temp.x + dir[i][0], fy = temp.y + dir[i][1];
if (!vis[fx][fy] && mp[fx][fy] != 'T' && fx > 0 && fx <= n && fy > 0 &&
fy <= m) {
makepair(fx, fy);
vis[fx][fy] = 1;
qu.push(po);
dis[fx][fy] = dis[temp.x][temp.y] + 1;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] >= '0' && mp[i][j] <= '9' && dis[i][j] != -1 &&
dis[i][j] <= dis[sx][sy])
ans += mp[i][j] - '0';
}
}
}
int main() {
read(n);
read(m);
for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (mp[i][j] == 'E')
ex = i, ey = j;
else if (mp[i][j] == 'S')
sx = i, sy = j;
}
bfs();
cout << ans << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
istream& operator>>(istream& in, __int128& a) {
int64_t b;
in >> b;
a = b;
return in;
}
ostream& operator<<(ostream& out, const __int128 a) {
unsigned __int128 b = a < 0 ? -a : a;
char buf[128];
char* c = end(buf);
do {
--c;
*c = "0123456789"[b % 10];
b /= 10;
} while (b);
if (a < 0) {
--c;
*c = '-';
}
int64_t len = end(buf) - c;
out.rdbuf()->sputn(c, len);
return out;
}
template <class T>
void mini(T& a, T b) {
a = min(a, b);
}
template <class T>
void maxi(T& a, T b) {
a = max(a, b);
}
template <class T>
T gcd(T a, T b) {
return b == 0 ? a : gcd(b, a % b);
}
template <class T1, class T2>
ostream& operator<<(ostream& os, pair<T1, T2> const& a) {
return os << "[" << a.first << ", " << a.second << "]";
}
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os,
Container const& a) {
os << "{ ";
for (auto& i : a) os << i << " ";
return os << "}";
}
template <class T>
void out(T a) {
cout << a << endl;
}
template <class T, class... Args>
void out(T a, Args... args) {
cout << a << " ";
out(args...);
}
template <class T>
void out(std::initializer_list<T> a) {
for (auto i : a) {
cout << i << " ";
}
cout << endl;
}
template <class T>
void out(vector<vector<T>> a) {
for (vector<T> i : a) out(i);
}
template <class T>
void out_(T a[], int l, int r) {
for (int i = l; i <= r; i++) cout << a[i] << " ";
cout << endl;
}
void out() { cout << "OK" << endl; }
template <class T>
void sort(vector<T>& a) {
sort(a.begin(), a.end());
}
template <class T>
void rever(vector<T>& a) {
reverse(a.begin(), a.end());
}
template <class T>
void uniq(vector<T>& a) {
sort(a);
a.erase(unique(a.begin(), a.end()), a.end());
}
template <class T>
vector<T> set_int(vector<T> a, vector<T> b) {
sort(a);
sort(b);
vector<T> res;
set_intersection(a.begin(), a.end(), b.begin(), b.end(), back_inserter(res));
return res;
}
clock_t start_time;
void start_timer() { start_time = clock(); }
double get_time() { return (double)(clock() - start_time) / CLOCKS_PER_SEC; }
template <class T>
using min_pque = priority_queue<T, vector<T>, greater<T>>;
void Solve() {
int n, k;
cin >> n >> k;
vector<int> a = {1};
while (a.back() * k < n) {
a.push_back(a.back() * k);
}
cout << a.size() << "\n";
for (int i = 0; i <= n - 1; i++) {
for (int j = i + 1; j <= n - 1; j++) {
for (int t = a.size() - 1; t >= 0; t--) {
if (i / a[t] != j / a[t]) {
cout << t + 1 << " ";
break;
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
start_timer();
Solve();
return 0;
}
| 8 |
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<vector>
#include<string>
#include<string.h>
#include<queue>
#include<math.h>
#include<utility>
#include<stack>
#include<unordered_map>
#include<map>
#include<set>
#include<unordered_set>
#include<algorithm>
#include<sstream>
#include<stdlib.h>
#include<bitset>
#define ff first
#define ss second
#define mod 1000000007LL
#define PI 3.14159265
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int test;
cin>>test;
while(test--)
{
int n;
cin>>n;
vector<int>f(3,0);
for(int i=0;i<n;i++)
{
int x;
cin>>x;
f[x%3]++;
}
int e=n/3;
int ans=0;
for(int t=1;t<=2;t++)
{
for(int i=0;i<=2;i++)
{
if(f[i]>e)
{
f[(i+1)%3]+=f[i]-e;
ans+=f[i]-e;
f[i]=e;
}
}
}
cout<<ans<<"\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void ckmax(T& x, T y) {
x = (y > x ? y : x);
}
template <typename T>
inline void ckmin(T& x, T y) {
x = (y < x ? y : x);
}
namespace Fread {
const int SIZE = 1 << 21;
char buf[SIZE], *S, *T;
inline char getchar() {
if (S == T) {
T = (S = buf) + fread(buf, 1, SIZE, stdin);
if (S == T) return '\n';
}
return *S++;
}
} // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 21;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
fwrite(buf, 1, S - buf, stdout);
S = buf;
}
inline void putchar(char c) {
*S++ = c;
if (S == T) flush();
}
struct NTR {
~NTR() { flush(); }
} ztr;
} // namespace Fwrite
namespace Fastio {
struct Reader {
template <typename T>
Reader& operator>>(T& x) {
char c = Fread ::getchar();
T f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = Fread ::getchar();
}
x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = Fread ::getchar();
}
x *= f;
return *this;
}
Reader& operator>>(char& c) {
c = Fread ::getchar();
while (c == '\n' || c == ' ') c = Fread ::getchar();
return *this;
}
Reader& operator>>(char* str) {
int len = 0;
char c = Fread ::getchar();
while (c == '\n' || c == ' ') c = Fread ::getchar();
while (c != '\n' && c != ' ') {
str[len++] = c;
c = Fread ::getchar();
}
str[len] = '\0';
return *this;
}
Reader() {}
} cin;
const char endl = '\n';
struct Writer {
template <typename T>
Writer& operator<<(T x) {
if (x == 0) {
Fwrite ::putchar('0');
return *this;
}
if (x < 0) {
Fwrite ::putchar('-');
x = -x;
}
static int sta[45];
int top = 0;
while (x) {
sta[++top] = x % 10;
x /= 10;
}
while (top) {
Fwrite ::putchar(sta[top] + '0');
--top;
}
return *this;
}
Writer& operator<<(char c) {
Fwrite ::putchar(c);
return *this;
}
Writer& operator<<(char* str) {
int cur = 0;
while (str[cur]) Fwrite ::putchar(str[cur++]);
return *this;
}
Writer& operator<<(const char* str) {
int cur = 0;
while (str[cur]) Fwrite ::putchar(str[cur++]);
return *this;
}
Writer() {}
} cout;
} // namespace Fastio
const int MAXN = 5e5;
const int MOD = 1e9 + 7;
inline int mod1(int x) { return x < MOD ? x : x - MOD; }
inline int mod2(int x) { return x < 0 ? x + MOD : x; }
inline void add(int& x, int y) { x = mod1(x + y); }
inline void sub(int& x, int y) { x = mod2(x - y); }
inline int pow_mod(int x, int i) {
int y = 1;
while (i) {
if (i & 1) y = (long long)y * x % MOD;
x = (long long)x * x % MOD;
i >>= 1;
}
return y;
}
int n, m, K;
long long a[MAXN + 5];
pair<int, int> e[MAXN + 5];
long long vals[MAXN + 5];
int cnt_val;
int nodes[MAXN * 2 + 5], cnt_node;
vector<pair<int, int> > e_of_w[MAXN + 5];
int fa[MAXN + 5], sz[MAXN + 5];
int get_fa(int u) { return (u == fa[u]) ? u : (fa[u] = get_fa(fa[u])); }
void unite(int u, int v) {
u = get_fa(u);
v = get_fa(v);
if (u != v) {
if (sz[u] > sz[v]) swap(u, v);
fa[u] = v;
sz[v] += sz[u];
}
}
int main() {
Fastio ::cin >> n >> m >> K;
for (int i = 1; i <= n; ++i) {
Fastio ::cin >> a[i];
}
for (int i = 1; i <= m; ++i) {
Fastio ::cin >> e[i].first >> e[i].second;
vals[++cnt_val] = (a[e[i].first] ^ a[e[i].second]);
}
sort(vals + 1, vals + cnt_val + 1);
cnt_val = unique(vals + 1, vals + cnt_val + 1) - (vals + 1);
int ans = ((1LL << K) - cnt_val) % MOD * pow_mod(2, n) % MOD;
for (int i = 1; i <= m; ++i) {
int w = lower_bound(vals + 1, vals + cnt_val + 1,
(a[e[i].first] ^ a[e[i].second])) -
vals;
e_of_w[w].push_back(e[i]);
}
for (int i = 1; i <= cnt_val; ++i) {
cnt_node = 0;
for (int j = 0; j < ((int)(e_of_w[i]).size()); ++j) {
int u = e_of_w[i][j].first, v = e_of_w[i][j].second;
nodes[++cnt_node] = u;
nodes[++cnt_node] = v;
}
sort(nodes + 1, nodes + cnt_node + 1);
cnt_node = unique(nodes + 1, nodes + cnt_node + 1) - (nodes + 1);
for (int j = 1; j <= cnt_node; ++j) {
int u = nodes[j];
fa[u] = u;
sz[u] = 1;
}
for (int j = 0; j < ((int)(e_of_w[i]).size()); ++j) {
int u = e_of_w[i][j].first, v = e_of_w[i][j].second;
unite(u, v);
}
int cnt = n - cnt_node;
for (int j = 1; j <= cnt_node; ++j) {
int u = nodes[j];
cnt += (get_fa(u) == u);
}
add(ans, pow_mod(2, cnt));
}
Fastio ::cout << ans << Fastio ::endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
if (n >= 3) {
cout << "YES" << endl;
cout << "2" << endl;
cout << s[0] << " " << s.substr(1, n - 1) << endl;
} else {
if (s[0] < s[1]) {
cout << "YES" << endl;
cout << "2" << endl;
cout << s[0] << " " << s[1] << endl;
} else {
cout << "NO" << endl;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100 * 1000 + 23, MAXK = 10 + 3;
long long n, k, a, dp[MAXN][MAXK], jav, seg[MAXK][MAXN << 2], m;
long long ans(long long, long long, long long, long long, long long);
void add(long long, long long, long long);
int32_t main() {
cin >> n >> k;
m = (1 << ((long long)ceil(log2(n))));
for (long long i = 0; i < n; i++) {
cin >> a;
for (long long j = 0; j <= k; j++)
dp[i + 1][j + 1] = ((j == 0) ? 1 : ans(1, j, a, 0, m));
for (long long j = 0; j < k; j++) add(i + 1, j + 1, a);
jav += dp[i + 1][k + 1];
}
cout << jav;
return 0;
}
long long ans(long long z, long long l, long long v, long long st,
long long ed) {
if (st >= v) return 0;
if (ed == v) return seg[l][z];
long long ret = 0;
long long mid = ((st + ed) >> 1);
ret += ans(z << 1, l, min(mid, v), st, mid);
ret += ans((z << 1) + 1, l, v, mid, ed);
return ret;
}
void add(long long q, long long l, long long v) {
long long f = v + m - 1;
while (f) {
seg[l][f] += dp[q][l];
f >>= 1;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline long long solve(long long a, long long b) {
long long ans = a / b;
a %= b;
if (a == 0) return ans;
if (a == 1) return ans + b;
ans += b / a;
ans += solve(a, b % a);
return ans;
}
int main() {
long long a, b;
cin >> a >> b;
cout << solve(a, b) << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = (int)5E5 + 10;
const int INF = (int)1E9 + 10;
int n, k;
int a[MAX];
int vmin, vmax;
int main() {
scanf("%d%d", &n, &k);
if (n == 1) {
printf("0");
return 0;
}
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
long long sum;
sum = 0;
for (int i = n - 2; i >= 0; i--) {
sum += (long long)(a[i + 1] - a[i]) * (n - i - 1);
if (sum >= k) {
int t = k - (sum - (long long)(a[i + 1] - a[i]) * (n - i - 1));
vmax = a[i + 1] - t / (n - i - 1);
break;
}
if (i == 0) vmax = a[i];
}
sum = 0;
for (int i = 1; i < n; i++) {
sum += (long long)(a[i] - a[i - 1]) * i;
if (sum >= k) {
int t = k - (sum - (long long)(a[i] - a[i - 1]) * i);
vmin = a[i - 1] + t / i;
break;
}
if (i == n - 1) vmin = a[i];
}
sum = 0;
for (int i = 0; i < n; i++) sum += a[i];
if (vmin < vmax)
printf("%d", vmax - vmin);
else
printf("%d", 1 - (sum % n == 0));
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, q;
int tp;
int fa[MAXN];
int dep[MAXN];
int c[MAXN];
int st[MAXN * 2];
map<pair<int, int>, int> mp;
vector<pair<int, int> > vec[4 * MAXN];
int find(int x) { return x == fa[x] ? x : find(fa[x]); }
int dis(int x) { return x == fa[x] ? 0 : (c[x] ^ dis(fa[x])); }
void merge(int u, int v, int ct) {
if (dep[u] < dep[v]) swap(u, v);
fa[v] = u;
c[v] = ct;
if (dep[u] == dep[v]) st[++tp] = -u, dep[u]++;
st[++tp] = v;
}
void back(int t) {
while (tp > t) {
if (st[tp] < 0)
dep[-st[tp]]--;
else
fa[st[tp]] = st[tp], c[st[tp]] = 0;
tp--;
}
}
void ask(int i, int l, int r) {
int cur = tp;
for (int j = 0; j < vec[i].size(); j++) {
int x = vec[i][j].first, y = vec[i][j].second;
int flag = dis(x) ^ dis(y) ^ 1;
if (find(x) == find(y)) {
if (flag) {
for (int i = l; i <= r; i++) puts("NO");
back(cur);
return;
}
} else {
merge(find(x), find(y), flag);
}
}
if (l == r) {
puts("YES");
back(cur);
return;
}
int mid = (l + r) >> 1;
ask(i << 1, l, mid);
ask(i << 1 | 1, mid + 1, r);
back(cur);
}
void ins(int i, int l, int r, int L, int R, pair<int, int> x) {
if (L > r || R < l) return;
if (L <= l && R >= r) {
vec[i].push_back(x);
return;
}
int mid = (l + r) >> 1;
ins(i << 1, l, mid, L, R, x);
ins(i << 1 | 1, mid + 1, r, L, R, x);
}
int main() {
cin >> n >> q;
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= q; i++) {
int u, v;
cin >> u >> v;
pair<int, int> t = make_pair(u, v);
if (mp.count(t)) {
ins(1, 1, q, mp[t], i - 1, t);
mp.erase(t);
} else
mp[t] = i;
}
for (auto item : mp) ins(1, 1, q, item.second, q, item.first);
ask(1, 1, q);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
char s[100050];
long long dp[2050][2060];
int main() {
int n, m, i, j;
while (scanf("%d%d", &n, &m) != EOF) {
scanf("%s", s + 1);
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (i = 1; i <= n - m; i++) {
for (j = 0; j <= i; j++) {
if (j > 0) {
dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % 1000000007;
}
dp[i][j] = (dp[i][j] + dp[i - 1][j + 1]) % 1000000007;
if (dp[i][j] >= 1000000007) dp[i][j] -= 1000000007;
}
}
int minx = 99999999;
int now = 0;
for (i = 1; i <= m; i++) {
if (s[i] == '(')
now++;
else
now--;
minx = min(minx, now);
}
long long sum = 0;
for (i = 0; i <= n - m; i++) {
for (j = 0; j <= i; j++) {
if (j >= -minx && j + now <= n - m - i) {
sum = (sum + dp[i][j] * dp[n - m - i][j + now]) % 1000000007;
}
}
}
printf("%I64d\n", sum);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int Map[101][101];
int n, m, i, j, cnt, ans[101][10001];
int main() {
cin >> n >> m;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) cin >> Map[i][j];
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (Map[i][j]) {
if (Map[i + 1][j] && Map[i][j + 1] && Map[i + 1][j + 1]) {
Map[i + 1][j] = Map[i][j + 1] = Map[i + 1][j + 1] = 2;
ans[1][++cnt] = i;
ans[2][cnt] = j;
} else if (Map[i][j] == 1) {
cout << "-1" << endl;
return 0;
}
}
cout << cnt << endl;
for (i = 1; i <= cnt; i++) cout << ans[1][i] << " " << ans[2][i] << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1e5 + 5;
int sgn(double x) {
if (fabs(x) < eps) return 0;
if (x < 0)
return -1;
else
return 1;
}
long long mv1[65], mv2[65];
int main() {
int Q;
scanf("%d", &Q);
int op, dep;
long long x, k, sz, mv;
while (Q--) {
scanf("%d", &op);
if (op == 1) {
scanf("%lld%lld", &x, &k);
dep = 0;
while (x > 0) {
x /= 2;
dep++;
}
sz = 1ll << (dep - 1);
mv1[dep] = ((mv1[dep] + k) % sz + sz) % sz;
} else if (op == 2) {
scanf("%lld%lld", &x, &k);
dep = 0;
while (x > 0) {
x /= 2;
dep++;
}
sz = 1ll << (dep - 1);
mv2[dep] = ((mv2[dep] + k) % sz + sz) % sz;
} else {
scanf("%lld", &x);
long long t = x;
dep = 0;
while (t) {
t /= 2;
dep++;
}
while (x) {
printf("%lld ", x);
sz = 1ll << (dep - 1);
mv = (mv1[dep] + mv2[dep]) % sz;
x += mv;
if (x >= 2 * sz) x -= sz;
x /= 2;
dep--;
sz = 1ll << (dep - 1);
x -= mv1[dep];
if (x < sz) x += sz;
}
printf("\n");
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, p;
cin >> n >> p;
int a[n][2];
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i][0] >> a[i][1];
sum += a[i][1] - a[i][0] + 1;
}
int x = 1;
for (int i = 0; i < n; i++) {
sum += (a[i][0] - x) % p;
x = a[i][1] + 1;
}
cout << sum;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int height[100010];
int main() {
int n, h, k, i;
long long cnt = 0;
long long sum = 0;
scanf("%d%d%d", &n, &h, &k);
for (i = 1; i <= n; ++i) scanf("%d", &height[i]);
i = 1;
sum = height[1];
while (sum) {
if (sum <= h) {
if (sum >= k) {
cnt += sum / k;
sum = sum % k;
}
i++;
if (height[i])
sum += height[i];
else {
if (sum < k && sum > 0) cnt++;
break;
}
} else {
sum -= height[i];
cnt += sum / k;
if ((sum % k + height[i]) > h) {
cnt++;
sum = height[i];
} else
sum = sum % k + height[i];
}
}
printf("%I64d\n", cnt);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
const int M = 1e5 + 10;
const long long mod = 1e9 + 7;
const double PI = acos(-1);
inline long long ab(long long x) { return x < 0 ? -x : x; }
inline long long mm(long long x) {
return x >= mod ? x - mod : x < 0 ? x + mod : x;
}
struct SAM {
static const int KN = N << 1;
static const int KM = 26;
int fail[KN], net[KN][KM], len[KN], cnt, root, mark[KN];
int newnode(int _len) {
memset(net[cnt], -1, sizeof(net[cnt]));
fail[cnt] = -1;
len[cnt] = _len;
return cnt++;
}
void init() {
cnt = 0;
root = newnode(0);
}
int add(int p, int x) {
int np = newnode(len[p] + 1);
while (~p && net[p][x] == -1) net[p][x] = np, p = fail[p];
if (p == -1)
fail[np] = root;
else {
int q = net[p][x];
if (len[q] == len[p] + 1)
fail[np] = q;
else {
int nq = newnode(len[p] + 1);
memcpy(net[nq], net[q], sizeof(net[q]));
fail[nq] = fail[q];
fail[q] = fail[np] = nq;
while (~p && net[p][x] == q) net[p][x] = nq, p = fail[p];
}
}
mark[np] = 1;
return np;
}
void debug() {
for (int now = 0; now < cnt; ++now) {
puts(".....................");
printf("now = %d\n", now);
printf("len = %d, fail = %d\n", len[now], fail[now]);
for (int i = 0; i < KM; ++i) printf("net[%d] = %d ", i, net[now][i]);
puts("");
puts(".....................");
}
}
void debug(int id) {
puts(".....................");
printf("now = %d\n", id);
printf("len = %d, fail = %d, mark = %d\n", len[id], fail[id], mark[id]);
for (int i = 0; i < KM; ++i) printf("net[%d] = %d ", i, net[id][i]);
puts("");
puts(".....................");
}
void build(char *s, char ch) {
int now = root;
for (int i = 0; s[i]; ++i) now = add(now, s[i] - ch);
}
int ord[KN], pri[KN], vis[KN];
void topo() {
int maxVal = 0;
memset(pri, 0, sizeof(pri));
for (int i = 0; i < cnt; ++i) maxVal = max(maxVal, len[i]), ++pri[len[i]];
for (int i = 1; i <= maxVal; ++i) pri[i] += pri[i - 1];
for (int i = 0; i < cnt; ++i) ord[--pri[len[i]]] = i;
}
void dfs() {
vis[0] = 0;
for (int i = cnt - 1; i >= 1; --i)
mark[fail[ord[i]]] += mark[ord[i]], vis[i] = 0;
}
void go(int &x, int _len) {
while (len[fail[x]] >= _len) x = fail[x];
}
void solve(char *s, char ch, int id) {
int now = root, ret = 0, _len = strlen(s), __len = 0;
for (int i = 0; i < _len; ++i) {
while (now != root && net[now][s[i] - ch] == -1) now = fail[now];
__len = min(__len, len[now]);
if (net[now][s[i] - ch] != -1) now = net[now][s[i] - ch], __len++;
}
for (int i = 0; i < _len; ++i) {
while (now != root && net[now][s[i] - ch] == -1) now = fail[now];
__len = min(__len, len[now]);
if (net[now][s[i] - ch] != -1) now = net[now][s[i] - ch], __len++;
go(now, _len);
__len = min(__len, len[now]);
if (__len >= _len && vis[now] != id) ret += mark[now], vis[now] = id;
}
printf("%d\n", ret);
}
} sam;
char s[N];
int main() {
scanf("%s", s);
sam.init();
sam.build(s, 'a');
sam.topo();
sam.dfs();
int q;
scanf("%d", &q);
for (int i = 1; i <= q; ++i) {
scanf("%s", s);
sam.solve(s, 'a', i);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int mxn = 200000;
int n, m;
int vis[mxn];
vector<pair<int, pair<int, int> > > v;
vector<int> g[mxn];
bool dfs(int c, int p) {
vis[c] = -1;
vector<int> e;
for (int i : g[c])
if (i != p) {
if (vis[i] ? !~vis[i] : dfs(i, c)) e.push_back(i);
}
vis[c] = 1;
for (int i = 1; i < e.size(); i += 2) v.push_back({c, {e[i], e[i - 1]}});
if ((e.size() & 1) && ~p) {
v.push_back({c, {e.back(), p}});
return 0;
}
return 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--, y--;
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 0; i < n; i++)
if (!vis[i]) dfs(i, -1);
cout << v.size() << '\n';
for (pair<int, pair<int, int> > i : v) {
cout << (i.second.first + 1) << " " << (i.first + 1) << " "
<< (i.second.second + 1) << '\n';
}
return 0;
}
| 7 |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
const LL INF = 1LL<<60;
int N, Q;
LL C[31];
void MAIN() {
scanf("%d%d", &N, &Q);
REP (i, N) scanf("%lld", C+i);
REP ($, Q) {
char op[11];
int x;
LL k;
scanf("%s%d%lld", op, &x, &k);
if (*op == '1') {
C[x] = k;
} else {
LL now = 0, grow = 0;
for (int y=0; y<=x; y++) {
now += C[y];
grow += ((1LL<<y) - 1) * C[y];
amin(grow, k);
}
LL ans = INF;
if (now >= k) ans = 0;
else {
k -= now;
if (grow >= k) amin(ans, k);
LL cost = 0;
for (int y=x+1; y<N; y++) {
LL rate = 1LL<<(y-x);
LL g = min(C[y], k / rate);
k -= g * rate;
cost += g * (rate - 1);
grow += min(k, ((1LL<<x) - 1) * g) * rate;
amin(grow, k);
if (grow >= k) amin(ans, k + cost);
if (C[y] > g) {
assert(k < grow + rate);
LL tmp_k = k;
LL tmp_grow = grow;
LL tmp_cost = cost;
while (1) {
//eprintf("%lld %lld %lld\n", tmp_k, tmp_grow, rate);
if (tmp_k <= tmp_grow) {
amin(ans, tmp_k + tmp_cost);
break;
}
if (tmp_k >= rate / 2) {
tmp_k -= rate / 2;
tmp_cost += rate / 2;
tmp_grow += ((1LL<<x) - 1) * (rate / 2);
amin(tmp_grow, k);
} else {
tmp_cost++;
}
rate /= 2;
}
}
}
}
if (ans == INF) {
puts("-1");
} else {
printf("%lld\n", ans);
}
}
}
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> lucky;
void gen(long long num) {
if (num > 1000000000) return;
if (num) lucky.push_back(num);
gen(num * 10 + 4);
gen(num * 10 + 7);
}
map<long long, long long> mark;
long long cnt[1034];
long long dp[1034][1034];
long long fact[100005];
long long call(int idx, int cntt) {
if (idx > (int)lucky.size()) {
if (cntt == 0) return 1;
return 0;
}
long long &ret = dp[idx][cntt];
if (ret != -1) return ret;
long long p = 0, q = 0;
if (cntt) {
p = (cnt[idx] * (call(idx + 1, cntt - 1))) % 1000000007;
}
q = call(idx + 1, cntt);
return ret = (p + q) % 1000000007;
}
long long bigmod(long long n, long long p) {
long long ret = 1;
while (p > 0) {
if (p % 2) ret = (ret * n) % 1000000007;
n = (n * n) % 1000000007;
p /= 2;
}
return ret;
}
long long nCr(long long n, long long r) {
long long up = fact[n];
long long down = (fact[r] * fact[n - r]) % 1000000007;
down = bigmod(down, 1000000007 - 2);
long long ret = (up * down) % 1000000007;
return ret;
}
int main() {
gen(0);
sort(lucky.begin(), lucky.end());
for (int i = 0; i < (int)lucky.size(); i++) {
mark[lucky[i]] = i + 1;
}
int n, k;
scanf("%d %d", &n, &k);
long long others = 0;
int total_lucky = 0;
for (int i = 0; i < n; i++) {
long long a;
scanf("%lld", &a);
if (mark[a]) {
if (cnt[mark[a]] == 0) total_lucky++;
cnt[mark[a]]++;
} else
others++;
}
fact[0] = 1;
for (long long i = 1; i < 100005; i++)
fact[i] = (fact[i - 1] * i) % 1000000007;
if (others + total_lucky < k) {
printf("0\n");
return 0;
}
long long ans = 0;
memset(dp, -1, sizeof(dp));
for (int i = 0; i <= total_lucky; i++) {
long long x = k - i;
if (x <= others && x >= 0) {
long long temp = nCr(others, x);
long long temp1 = (call(1, i)) % 1000000007;
temp = (temp * temp1) % 1000000007;
ans = (ans + temp) % 1000000007;
}
}
printf("%lld\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
int t[200020];
int p[200020];
int main() {
cin.tie(0);
cout << fixed << setprecision(10);
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i < (n + 1); ++i) cin >> t[i];
int res = 0;
for (int i = 1; i < (n + 1); ++i) {
if (p[t[i]] != 0) {
p[i] = p[t[i]];
p[t[i]] = 0;
} else {
p[i] = ++res;
}
}
cout << res << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
n += 2;
m += 2;
char a[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || j == 0 || i == n - 1 || j == m - 1) {
a[i][j] = '.';
} else {
cin >> a[i][j];
}
}
}
int lo = 0, hi = n + m;
while (hi - lo > 1) {
int t = (lo + hi) / 2;
int pr[n + 1][m + 1];
memset(pr, 0, sizeof(pr));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '.') {
int x1 = max(0, i - t), y1 = max(0, j - t),
x2 = min(n - 1, i + t) + 1, y2 = min(m - 1, j + t) + 1;
pr[x1][y1]++;
pr[x2][y2]++;
pr[x1][y2]--;
pr[x2][y1]--;
}
}
}
vector<pair<int, int>> to;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i) pr[i][j] += pr[i - 1][j];
if (j) pr[i][j] += pr[i][j - 1];
if (i && j) pr[i][j] -= pr[i - 1][j - 1];
if (!pr[i][j]) to.push_back({i, j});
}
}
memset(pr, 0, sizeof(pr));
for (auto e : to) {
int x1 = e.first - t, y1 = e.second - t, x2 = e.first + t + 1,
y2 = e.second + t + 1;
pr[x1][y1]++;
pr[x1][y2]--;
pr[x2][y1]--;
pr[x2][y2]++;
}
bool ok = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i) pr[i][j] += pr[i - 1][j];
if (j) pr[i][j] += pr[i][j - 1];
if (i && j) pr[i][j] -= pr[i - 1][j - 1];
if ((pr[i][j] && a[i][j] == '.') || (!pr[i][j] && a[i][j] == 'X'))
ok = 0;
}
}
if (ok) {
lo = t;
} else {
hi = t;
}
}
int t = lo;
int pr[n + 1][m + 1];
memset(pr, 0, sizeof(pr));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '.') {
int x1 = max(0, i - t), y1 = max(0, j - t), x2 = min(n - 1, i + t) + 1,
y2 = min(m - 1, j + t) + 1;
pr[x1][y1]++;
pr[x1][y2]--;
pr[x2][y1]--;
pr[x2][y2]++;
}
}
}
cout << t << '\n';
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i) pr[i][j] += pr[i - 1][j];
if (j) pr[i][j] += pr[i][j - 1];
if (i && j) pr[i][j] -= pr[i - 1][j - 1];
}
}
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < m - 1; j++) {
cout << (pr[i][j] ? '.' : 'X');
}
cout << '\n';
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x1, x2;
cin >> n >> x1 >> x2;
pair<long long, long long> P[n];
long long k, b, i;
for (i = 0; i < n; i++) {
cin >> k >> b;
P[i] = make_pair(k * x1 + b, k * x2 + b);
}
sort(P, P + n);
bool flag = true;
for (i = 1; i < n; i++)
if (P[i].second < P[i - 1].second) flag = false;
cout << (flag ? "NO" : "YES");
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int maxn = 1e6 + 7;
int n;
int a[maxn];
int b[maxn];
char ans[maxn];
bool solve() {
if (a[n - 1] != 0) return false;
for (int i = 0; i < n; i++) b[i] = a[i];
if (n == 1) {
ans[0] = '0';
return true;
}
int p = -1;
for (int i = n - 2; i >= 0; i--) {
if (a[i] == 1) {
p = i;
break;
}
if (i > 0 && a[i - 1] == 1 && a[i] == 0)
a[i - 1] = 0;
else
a[i - 1] = 1;
}
if (p < 0) return false;
int j = 0;
for (int i = 0; i < p; i++) {
ans[j++] = b[i] + '0';
ans[j++] = '-';
ans[j++] = '>';
}
int cnt = 0;
for (int i = p; i < n - 2; i++) {
cnt++;
ans[j++] = '(';
ans[j++] = b[i] + '0';
ans[j++] = '-';
ans[j++] = '>';
}
ans[j++] = b[n - 2] + '0';
for (int i = 0; i < cnt; i++) ans[j++] = ')';
ans[j++] = '-';
ans[j++] = '>';
ans[j++] = b[n - 1] + '0';
return true;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
if (solve())
cout << "YES\n" << ans << endl;
else
cout << "NO" << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
long double k;
cin >> n >> m >> k;
cout << std::setprecision(5);
vector<pair<string, int> > vect;
for (int i = 0; i < n + m; i++) {
string skill;
cin >> skill;
if (i < n) {
long double lvl;
cin >> lvl;
lvl = k * lvl;
if (lvl < 100) {
lvl = -1;
vect.push_back(make_pair(skill, -1));
} else {
vect.push_back(make_pair(skill, lvl + 0.001));
}
} else {
vect.push_back(make_pair(skill, 0));
}
}
sort(vect.begin(), vect.end());
int no = 0;
for (int i = 0; i < n + m; i++) {
int j = i;
while (j + 1 < n + m && vect[i].first == vect[j + 1].first &&
vect[i].second > -1) {
vect[i].second += vect[j + 1].second;
vect[j + 1].second = -4;
vect[j + 1].first = '-1';
j++;
}
if (vect[i].second > -1) {
no++;
}
}
cout << no << endl;
for (int i = 0; i < n + m; i++) {
if (vect[i].second >= 0) {
cout << std::setprecision(5) << vect[i].first << " " << vect[i].second
<< endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline bool valid(int x, int n) { return 0 <= x && x < n; }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
template <typename T>
inline T pop(queue<T>& q) {
T front = q.front();
q.pop();
return front;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int x, y;
cin >> x >> y;
if (x >= 4)
cout << "YES\n";
else if (x >= y)
cout << "YES\n";
else if (x == 2 && y == 3)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, a[100001][3] = {{0}};
string s;
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
cin >> s >> m;
n = s.size();
for (long long i = 0; i < n; i++) {
if (s[i] == 'z') a[i + 1][0]++;
if (s[i] == 'y') a[i + 1][1]++;
if (s[i] == 'x') a[i + 1][2]++;
}
for (long long i = 1; i < n + 1; i++) {
for (long long j = 0; j < 3; j++) a[i][j] += (a[i - 1][j]);
}
while (m--) {
long long l, r, cntz = 0, cnty = 0, cntx = 0;
cin >> l >> r;
cntz = a[r][0] - a[l - 1][0];
cnty = a[r][1] - a[l - 1][1];
cntx = a[r][2] - a[l - 1][2];
if (r - l + 1 < 3 ||
max(cntz, max(cnty, cntx)) - min(cntx, min(cntz, cnty)) <= 1)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long A[300000];
long long DP[5001][5001];
long long n, k;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> A[i];
sort(A, A + n);
long long num = n / k;
long long extra = n % k;
for (long long i = 1; i <= k; i++) {
for (long long j = 0; j <= min(i, extra); j++) {
if (j == 0)
DP[i][j] = DP[i - 1][j] + A[i * num - 1] - A[(i - 1) * num];
else if (i == j)
DP[i][j] =
DP[i - 1][j - 1] + A[i * num - 1 + j] - A[(i - 1) * num + j - 1];
else
DP[i][j] = min(
DP[i - 1][j] + A[i * num - 1 + j] - A[(i - 1) * num + j],
DP[i - 1][j - 1] + A[i * num - 1 + j] - A[(i - 1) * num + j - 1]);
}
}
cout << DP[k][extra] << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int main(int argc, char** argv) {
scanf("%d%d", &n, &k);
int d = 1;
for (int j = 0; j < k; ++j) {
for (int i = 1; i < n; ++i) {
int id = i + d;
id = id > n ? n : id;
printf("%d ", id);
}
printf("%d\n", n);
d <<= 1;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
int main() {
ios_base::sync_with_stdio(false);
int n, m, r, i;
i = 1;
cin >> n >> m;
while (true) {
if ((n * i) % 10 == 0 or (n * i) % 10 == m) {
r = i;
break;
}
i++;
}
cout << r << endl;
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.