solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000;
int n;
int deg[maxn], par[maxn], dist[maxn];
vector<int> path;
vector<int> graph[maxn];
int diam(int c) {
int ret = c;
for (int i : graph[c]) {
if (i == par[c]) continue;
par[i] = c, dist[i] = dist[c] + 1;
int temp = diam(i);
if (dist[temp] > dist[ret]) ret = temp;
}
return ret;
}
bool dfs(int c) {
if (dist[c] == path.size()) return 0;
if (deg[path[dist[c]]] != deg[c]) return 0;
for (int i : graph[c]) {
if (i == par[c]) continue;
par[i] = c, dist[i] = dist[c] + 1;
if (!dfs(i)) return 0;
}
return 1;
}
int dfs2(int c) {
if (deg[c] > 2) return -1;
int ret = c;
for (int i : graph[c]) {
if (i == par[c]) continue;
par[i] = c, dist[i] = dist[c] + 1;
int t = dfs2(i);
if (t == -1) return -1;
ret = t;
}
return ret;
}
int works() {
int ret = path.front();
bool used = 0;
par[path.front()] = -1, dist[path.front()] = 0;
for (int i : graph[path.front()]) {
par[i] = path.front(), dist[i] = 1;
if (!dfs(i)) {
if (!used) {
int t = dfs2(i);
if (t == -1) return 0;
ret = t;
used = 1;
} else {
return 0;
}
}
}
return ret + 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int v, u;
cin >> v >> u;
v--, u--;
deg[v]++;
deg[u]++;
graph[v].push_back(u);
graph[u].push_back(v);
}
int r = 0;
for (int i = 0; i < 2; i++) {
par[r] = -1, dist[r] = 0;
r = diam(r);
}
for (int i = r; i != -1; i = par[i]) path.push_back(i);
for (int i = 0; i < 3; i++) {
int t = works();
if (t) {
cout << t << '\n';
return 0;
}
if (i == 1) {
if (path.size() % 2 == 0) break;
int sz = path.size() / 2;
bool test = 1;
for (int i = 0; i < sz; i++) {
if (deg[path[i]] != deg[path.back()]) {
test = 0;
break;
}
path.pop_back();
}
if (!test) break;
}
reverse(path.begin(), path.end());
}
cout << -1 << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[111111];
int c[66], cn, d[66][111111], x[111111], res[111111];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%lld", &a[i]);
for (int t = 0; t < 2; ++t) {
for (int j = 60; j >= 0; --j) {
int cnt = 0;
for (int i = 0; i < n; ++i) {
x[i] = (a[i] >> j) & 1;
cnt += x[i];
}
if ((cnt & 1) != t) continue;
x[n] = 1;
for (int k = 0; k < cn; ++k)
if (x[c[k]]) {
for (int i = 0; i <= n; ++i) x[i] ^= d[k][i];
}
int p = 0;
while (p < n && !x[p]) ++p;
if (p < n) {
memcpy(d[cn], x, sizeof(int) * (n + 1));
c[cn++] = p;
}
}
}
for (int j = cn - 1; j >= 0; --j) {
int p = c[j];
res[p] = d[j][n];
for (int k = j + 1; k < cn; ++k) {
int pp = c[k];
res[p] ^= (d[j][pp] & res[pp]);
}
}
for (int i = 0; i < n; ++i) {
if (i > 0) putchar(' ');
printf("%d", res[i] + 1);
}
puts("");
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 300005;
long long n, m, k;
struct node {
int id;
long long pos;
long long dir;
} no[MX];
int now[MX];
int ans[MX];
bool cmp(node a, node b) {
if (a.pos < b.pos) return true;
return false;
}
int main() {
char ss[5];
scanf("%I64d%I64d%I64d", &n, &m, &k);
long long p = 0;
for (int i = 0; i < n; i++) {
scanf("%I64d%s", &no[i].pos, ss);
--no[i].pos;
no[i].id = i;
no[i].dir = 1;
if (ss[0] == 'L') no[i].dir = -1;
if (ss[0] == 'R')
p += (k + no[i].pos) / m;
else
p -= (k + m - 1 - no[i].pos) / m;
now[i] = ((no[i].pos + no[i].dir * k) % m + m) % m;
}
p = p % n;
sort(no, no + n, cmp);
sort(now, now + n);
for (int i = 0; i < n; i++) {
ans[no[((i - p) % n + n) % n].id] = now[i] + 1;
}
for (int i = 0; i < n; i++) {
printf("%d%c", ans[i], i == n - 1 ? '\n' : ' ');
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[200001], i, k = 1, n, ans = 0;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
for (i = 1; i <= n; i++) {
if (a[i] >= k) {
ans++;
k++;
}
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
char s[N];
bool isLetter(char c) {
if (c >= 'a' && c <= 'z') return true;
if (c >= 'A' && c <= 'Z') return true;
return false;
}
int main() {
scanf("%s", s);
int n = strlen(s);
long long ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '@') {
long long x = 0, y = 0;
for (int j = i - 1; j >= 0; j--) {
if (s[j] == '@' || s[j] == '.') break;
if (isLetter(s[j])) x++;
}
for (int j = i + 1; j < n; j++) {
if (s[j] == '@' || s[j] == '_') {
i = j - 1;
break;
}
if (s[j] == '.') {
if (j - i == 1) break;
int k;
for (k = j + 1; k < n; k++) {
if (!isLetter(s[k])) break;
y++;
}
i = k - 1;
break;
}
}
ans += x * y;
}
}
cout << ans << endl;
;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 0, 1, 1}, dy[] = {0, 1, 0, 1};
vector<string> F;
void flip(int x, int y, int t) {
for (int k = 0; k < 4; k++)
if (k ^ t) F[x + dx[k]][y + dy[k]] ^= 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
((void)0);
((void)0);
((void)0);
int T, N, M;
for (cin >> T; T--;) {
vector<tuple<int, int, int>> ans;
cin >> N >> M;
F.resize(N);
for (int i = 0; i < N; i++) cin >> F[i];
for (int i = 0; i < N - 2; i++) {
for (int j = 0; j + 1 < M; j += 2) {
if (F[i][j] == '1' && F[i][j + 1] == '1') {
ans.emplace_back(i, j, 3);
flip(i, j, 3);
} else if (F[i][j] == '1') {
ans.emplace_back(i, j, 1);
flip(i, j, 1);
} else if (F[i][j + 1] == '1') {
ans.emplace_back(i, j, 0);
flip(i, j, 0);
}
}
if ((M & 1) && F[i][M - 1] == '1') {
ans.emplace_back(i, M - 2, 0);
flip(i, M - 2, 0);
}
}
for (int j = 0; j < M - 1; j++) {
if (F[N - 2][j] == '1' && F[N - 1][j] == '1') {
ans.emplace_back(N - 2, j, 1);
flip(N - 2, j, 1);
} else if (F[N - 2][j] == '1') {
ans.emplace_back(N - 2, j, 2);
flip(N - 2, j, 2);
} else if (F[N - 1][j] == '1') {
ans.emplace_back(N - 2, j, 0);
flip(N - 2, j, 0);
}
}
if (F[N - 2][M - 1] == '1' && F[N - 1][M - 1] == '1') {
ans.emplace_back(N - 2, M - 2, 1);
ans.emplace_back(N - 2, M - 2, 3);
flip(N - 2, N - 2, 1);
flip(N - 2, N - 2, 3);
} else if (F[N - 2][M - 1] == '1') {
ans.emplace_back(N - 2, M - 2, 3);
ans.emplace_back(N - 2, M - 2, 0);
ans.emplace_back(N - 2, M - 2, 2);
} else if (F[N - 1][M - 1] == '1') {
ans.emplace_back(N - 2, M - 2, 1);
ans.emplace_back(N - 2, M - 2, 0);
ans.emplace_back(N - 2, M - 2, 2);
}
cout << ans.size() << '\n';
for (auto [x, y, t] : ans) {
for (int k = 0; k < 4; k++)
if (k ^ t) cout << x + dx[k] + 1 << ' ' << y + dy[k] + 1 << ' ';
cout << '\n';
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int gi() {
int data = 0, m = 1;
char ch = 0;
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') {
m = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
data = (data << 1) + (data << 3) + (ch ^ 48);
ch = getchar();
}
return (m) ? data : -data;
}
int fail[1000005], up[1000005], len[1000005], ch[1000005][26], f[1000005],
g[1000005], p, oo, n, m, cl, last, Delta[1000005];
char s[1000005], str[1000005];
inline void Init() {
fail[0] = fail[1] = ++oo;
len[1] = -1;
return;
}
inline void Extend(int c, int pos) {
p = last;
while (s[pos] != s[pos - len[p] - 1]) p = fail[p];
if (!ch[p][c]) {
cl = fail[p];
++oo;
while (s[pos] != s[pos - len[cl] - 1]) cl = fail[cl];
cl = ch[cl][c];
fail[oo] = cl;
ch[p][c] = oo;
len[oo] = len[p] + 2;
}
p = ch[p][c];
last = p;
Delta[p] = len[p] - len[fail[p]];
up[p] = (Delta[p] == Delta[fail[p]]) ? up[fail[p]] : p;
}
int main() {
scanf("%s", str + 1);
n = strlen(str + 1);
for (int i = 1; i <= n / 2; i++) s[2 * i - 1] = str[i];
for (int i = n; i > n / 2; i--) s[2 * (n - i + 1)] = str[i];
Init();
f[0] = 1;
for (int i = 1; i <= n; i++) {
Extend(s[i] - 'a', i);
f[i] = 0;
for (int x = last; x > 1; x = fail[p]) {
p = up[x];
g[x] = (x ^ p) ? g[fail[x]] : 0;
g[x] = (g[x] + f[i - len[p]]) % 1000000007;
if (i % 2 == 0) f[i] = (f[i] + g[x]) % 1000000007;
}
}
cout << f[n] << endl;
return 0;
}
| 10 |
#include <cstdio>
#include <iostream>
#include <cstring>
const int Max=2e5+5;
int t, cream, a[Max], b[Max], n;
int main(){
// freopen("data.in", "r", stdin);
scanf("%d", &t);
for(int casenum=0; casenum<t; casenum++){
memset(b, 0, sizeof(b));
cream=0;
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%d", &a[i]);
}
for(int i=n-1; i>=0; i--){
cream = cream>a[i]?cream:a[i];
if(cream>0){
b[i] = 1; cream--;
}
}
for(int i=0; i<n; i++){
printf("%d ", b[i]);
}
printf("\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 300000;
int n;
int a[MAXN];
int nxt[MAXN + 1];
map<int, int> mp[MAXN + 1];
int mpidx[MAXN + 1];
int len[MAXN + 1];
long long solve() {
nxt[n] = -1;
mpidx[n] = n, mp[n].clear();
for (int i = n - 1; i >= 0; --i) {
int idx = mpidx[i + 1];
auto it = mp[idx].find(a[i]);
if (it != mp[idx].end()) {
nxt[i] = it->second + 1;
assert(mpidx[nxt[i]] != -1);
mpidx[i] = mpidx[nxt[i]];
mpidx[nxt[i]] = -1;
if (nxt[i] < n) mp[mpidx[i]][a[nxt[i]]] = nxt[i];
} else {
nxt[i] = -1;
mpidx[i] = i, mp[i].clear();
}
mp[mpidx[i]][a[i]] = i;
}
len[n] = 0;
for (int i = n - 1; i >= 0; --i) {
len[i] = nxt[i] == -1 ? 0 : 1 + len[nxt[i]];
}
long long ret = 0;
for (int i = (0); i < (n); ++i) ret += len[i];
return ret;
}
void run() {
scanf("%d", &n);
for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
printf("%lld\n", solve());
}
int main() {
int ncase;
scanf("%d", &ncase);
for (int i = (1); i <= (ncase); ++i) run();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[1000], count = 0, s = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
s += a[i];
}
for (int i = 0; i < n; i++) {
if ((s - a[i]) % 2 == 0) count++;
}
cout << count;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int o = 0, f = 0;
string s;
cin >> s;
stack<int> stk;
for (int i = 0; i < s.size(); i++) {
stk.push(s[i]);
if (s[i] == '(') {
o += 1;
}
if (s[i] == ')' && o == 0) {
f += 1;
stk.pop();
}
if (s[i] == ')' && o != 0) {
o -= 1;
stk.pop();
stk.pop();
}
}
cout << s.size() - f - stk.size() << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int N, M, K, SN, SM, SK, Q;
priority_queue<long long int, vector<long long int>, greater<long long int> >
heap;
struct node {
long long int x, y;
} ar[100007];
bool operator<(const node &a, const node &b) { return a.x < b.x; }
void oku() {
scanf("%d %d %d", &N, &M, &K);
scanf("%d %d %d", &SN, &SM, &SK);
scanf("%d", &Q);
for (int i = 1; i <= Q; i++) {
cin >> ar[i].y;
ar[i].x = ar[i].y;
}
sort(ar + 1, ar + Q + 1);
}
void islem() {
for (int i = 1; i <= Q; i++) {
while (!heap.empty() && heap.top() <= ar[i].x) {
N++;
heap.pop();
}
if (N) {
heap.push(ar[i].x + SN);
ar[i].x += SN;
N--;
} else {
ar[i].x = heap.top();
heap.pop();
heap.push(ar[i].x + SN);
ar[i].x += SN;
}
}
sort(ar + 1, ar + Q + 1);
while (!heap.empty()) heap.pop();
for (int i = 1; i <= Q; i++) {
while (!heap.empty() && heap.top() <= ar[i].x) {
M++;
heap.pop();
}
if (M) {
heap.push(ar[i].x + SM);
ar[i].x += SM;
M--;
} else {
ar[i].x = heap.top();
heap.pop();
heap.push(ar[i].x + SM);
ar[i].x += SM;
}
}
sort(ar + 1, ar + Q + 1);
while (!heap.empty()) heap.pop();
for (int i = 1; i <= Q; i++) {
while (!heap.empty() && heap.top() <= ar[i].x) {
K++;
heap.pop();
}
if (K) {
heap.push(ar[i].x + SK);
ar[i].x += SK;
K--;
} else {
ar[i].x = heap.top();
heap.pop();
heap.push(ar[i].x + SK);
ar[i].x += SK;
}
}
long long int maxi = 0;
for (int i = 1; i <= Q; i++)
if (ar[i].x - ar[i].y > maxi) maxi = ar[i].x - ar[i].y;
cout << maxi << '\n';
}
int main() {
oku();
islem();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, m, w[100000];
int i;
scanf("%d%d", &n, &m);
for (i = 0; i < m; i++) scanf("%*d%d", &w[i]);
sort(w, w + m, greater<int>());
int qtde = 1;
long long need;
for (i = m; i > 0; i--) {
if (i & 1)
need = 0;
else
need = (i - 2) / 2;
need += i * (long long)(i - 1) / 2;
if (need < n) {
qtde = i;
break;
}
}
long long ans = 0;
for (i = 0; i < qtde; i++) ans += w[i];
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char input[101];
scanf("%s", input);
char temp[11];
map<string, int> mp;
for (int i = 0; i < 10; ++i) {
scanf("%s", temp);
mp[temp] = i;
}
string s;
for (int i = 0; input[i]; ++i) {
s += input[i];
if (s.size() == 10) {
printf("%c", mp[s] + '0');
s = "";
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int n, now = 0;
scanf("%d", &n);
for (int x, y, res, i = 1; i <= n; i++) {
scanf("%d %d", &x, &y);
if (x > now)
now = x;
else {
res = (now - x) % y;
now += (y - res);
}
}
printf("%d\n", now);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, l0;
vector<int> x, l;
scanf("%d", &n);
int *a = new int[n + 1];
for (int i = 1; i <= n; i++) scanf("%d", a + i);
for (int i = 1; i <= n - 2; i++) {
if (a[i] == 1) {
l0 = 1;
for (int j = 2; j <= (n - i) / 2; ++j) {
if (a[i + j] == 1) {
l0 = j;
break;
}
}
a[i] ^= 1;
a[i + l0] ^= 1;
a[i + l0 + l0] ^= 1;
x.push_back(i);
l.push_back(l0);
}
}
if (a[n] == 1 && a[n - 1] == 0) {
if (n > 6) {
x.push_back(n - 6);
l.push_back(3);
x.push_back(n - 6);
l.push_back(1);
x.push_back(n - 5);
l.push_back(1);
} else {
cout << "NO" << endl;
return 0;
}
}
if (a[n] == 0 && a[n - 1] == 1) {
if (n > 7) {
x.push_back(n - 7);
l.push_back(3);
x.push_back(n - 7);
l.push_back(1);
x.push_back(n - 6);
l.push_back(1);
} else {
cout << "NO" << endl;
return 0;
}
}
if (a[n] == 1 && a[n - 1] == 1) {
if (n > 7) {
x.push_back(n - 7);
l.push_back(3);
x.push_back(n - 7);
l.push_back(1);
x.push_back(n - 6);
l.push_back(1);
x.push_back(n - 6);
l.push_back(3);
x.push_back(n - 6);
l.push_back(1);
x.push_back(n - 5);
l.push_back(1);
} else {
cout << "NO" << endl;
return 0;
}
}
printf("YES\n%d\n", x.size());
for (int i = 0; i < x.size(); ++i) {
printf("%d %d %d\n", x[i], x[i] + l[i], x[i] + l[i] + l[i]);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void print(vector<int> v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
void solve() {
int n, k;
cin >> n >> k;
if (n == k) {
cout << -1 << endl;
} else {
for (int i = 1; i <= n - k - 1; i++) {
cout << i + 1 << " ";
}
cout << 1 << " ";
for (int i = n - k + 1; i <= n; i++) {
cout << i << " ";
}
cout << endl;
}
}
int main() { solve(); }
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 50;
const long long mod = 1e9 + 7;
string s;
long long n, cnt;
long long dp[4];
inline long long fpow(long long a, long long p) {
long long res = 1;
for (; p; p >>= 1, a *= a, a %= mod)
if (p & 1) res *= a, res %= mod;
return res;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
;
cin >> n >> s;
for (long long i = 0; i < n; ++i) {
if (s[i] == 'a') dp[1] += fpow(3, cnt), dp[1] %= mod;
if (s[i] == 'b') dp[2] += dp[1], dp[2] %= mod;
if (s[i] == 'c') dp[3] += dp[2], dp[3] %= mod;
if (s[i] == '?') {
dp[3] += dp[3] * 2 + dp[2];
dp[2] += dp[2] * 2 + dp[1];
dp[1] += dp[1] * 2 + fpow(3, cnt);
for (long long j = 1; j <= 3; ++j) dp[j] %= mod;
++cnt;
}
}
cout << dp[3] << '\n';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
vector<pair<int, int>> ta;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < m; ++j) {
cout << i + 1 << ' ' << j + 1 << '\n';
cout << n - i << ' ' << m - j << '\n';
}
}
if (n & 1) {
for (int j = 0; j < m / 2; ++j) {
cout << n / 2 + 1 << ' ' << j + 1 << '\n';
cout << n / 2 + 1 << ' ' << m - j << '\n';
}
if (m & 1) cout << n / 2 + 1 << ' ' << m / 2 + 1 << '\n';
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> size;
vector<int> par;
public:
UnionFind(int n) : size(n, 1), par(n) {
for (int i = 0; i < n; i++) par[i] = i;
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
if (size[x] < size[y]) {
par[x] = y;
size[y] += size[x];
} else {
par[y] = x;
size[x] += size[y];
}
}
int find(int x) { return par[x] == x ? x : (par[x] = find(par[x])); }
};
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<int> a(m);
for (int i = 0; i < m; i++) {
scanf("%d", &a[i]);
}
vector<int> isA(1 << n, false);
for (int i = 0; i < m; i++) isA[a[i]] = true;
vector<int> dp(1 << n, -2);
UnionFind u(1 << n);
int filter = (1 << n) - 1;
function<int(int)> dfs = [&](int bit) {
if (dp[bit] != -2) return dp[bit];
if (bit == 0) return dp[bit] = (isA[0] ? 0 : -1);
int v = isA[bit] ? bit : -1;
for (int i = 0; i < n; i++) {
if ((bit >> i) & 1) {
int tob = bit ^ (1 << i);
int nex = dfs(tob);
if (v != -1 && nex != -1) {
u.unite(v, nex);
}
if (v == -1) v = nex;
}
}
return dp[bit] = v;
};
for (int i = 0; i < m; i++) {
int bit = (~a[i]) & filter;
int v = dfs(bit);
if (v != -1) {
u.unite(a[i], v);
}
}
int res = 0;
for (int i = 0; i < m; i++) {
res += (int)(u.find(a[i]) == a[i]);
}
printf("%d\n", res);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int a[430] = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223,
227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359,
367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,
439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593,
599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743,
751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827,
829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,
1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069,
1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163,
1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249,
1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321,
1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439,
1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511,
1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601,
1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693,
1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783,
1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877,
1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987,
1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069,
2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143,
2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267,
2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347,
2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423,
2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543,
2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657,
2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713,
2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801,
2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903,
2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999};
cin >> n;
int sum = 0, d = 0;
for (int i = 6; i <= n; i++) {
d = 0;
for (int j = 2; j <= i / 2; j++) {
for (int k = 0; k < 430; k++)
if (j == a[k]) {
if (i % j == 0) d++;
break;
}
}
if (d == 2) sum++;
}
cout << sum;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int32_t INFint = 1e9;
const long long INFll = 1e18;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, k;
cin >> n >> k;
string arr;
cin >> arr;
int hash[26];
for (int i = 0; i < 26; i++) hash[i] = 0;
int count = 0;
int index = 0;
for (int i = 0; i < n; i++) {
count = 1;
for (int j = i + 1; j < n; j++) {
if (count == k) {
index = j;
break;
}
if (arr[j] == arr[i]) count++;
if (arr[j] != arr[i]) {
index = j;
break;
}
index = j;
}
if (count == k) hash[arr[i] - 'a']++;
if (i == n - 1) break;
i = index - 1;
}
int max = 0;
for (int i = 0; i < 26; i++) {
if (hash[i] > max) max = hash[i];
}
cout << max << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char s[110000];
struct node {
char c;
int ind;
};
int mark[110000];
stack<node> sk;
int main() {
int n, m, i, j;
node u, v;
while (scanf("%s", s) != EOF) {
int len = strlen(s);
memset(mark, 0, sizeof(mark));
while (!sk.empty()) sk.pop();
for (i = 0; i < len; i++) {
if (s[i] == '(' || s[i] == '[') {
u.c = s[i];
u.ind = i;
sk.push(u);
} else {
v.c = 'c';
if (s[i] == ')') {
if (!sk.empty()) v = sk.top();
if (v.c == '(' && !sk.empty()) {
sk.pop();
} else {
u.c = s[i];
u.ind = i;
sk.push(u);
}
}
if (s[i] == ']') {
if (!sk.empty()) v = sk.top();
if (v.c == '[' && !sk.empty())
sk.pop();
else {
u.c = s[i];
u.ind = i;
sk.push(u);
}
}
}
}
int cnt1 = 0;
while (!sk.empty()) {
u = sk.top();
mark[u.ind] = 1;
sk.pop();
cnt1++;
}
if (cnt1 == len) {
printf("0\n");
continue;
}
int cnt = 0, ans = 0, l = 0, r = 0;
for (i = 0; i < len; i++) {
if (!mark[i]) {
if (s[i] == '[') cnt++;
} else {
if (cnt > ans) ans = cnt, r = l;
cnt = 0;
l = i + 1;
}
}
if (cnt >= ans) ans = cnt, r = l;
printf("%d\n", ans);
for (i = r; i < len; i++)
if (!mark[i])
printf("%c", s[i]);
else
break;
printf("\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 10, mod = 998244353;
long long bexp(long long a, long long b) {
a %= mod;
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
long long inv(long long x) { return bexp(x, mod - 2); }
long long f[N];
long long nck(long long n, long long k) {
if (k > n) return 0;
long long res = f[n];
res = res * inv(f[k]) % mod;
res = res * inv(f[n - k]) % mod;
return res;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
f[0] = 1;
for (long long i = 1; i < N; i++) f[i] = f[i - 1] * i % mod;
long long n, k;
cin >> n >> k;
long long ans = 0;
for (long long i = 1; i <= n; ++i) {
ans += nck(n / i - 1, k - 1);
ans %= mod;
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 50;
const int iinf = INT_MAX / 2;
const long long linf = LONG_MAX / 2;
const int MOD = 1e9 + 7;
inline int read() {
int X = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, k, ans;
int mp[N];
int a[N][N], res[N][N], bad[N][N][N];
void dfs(int x, int y, int mc) {
if (x == n) {
for (int i = 1; i <= mc; i++)
for (int qi = i + 1; qi <= mc; qi++)
if (mp[i] != 0 && mp[qi] != 0 && mp[i] == mp[qi]) return;
int free = 0, clos = k;
for (int i = 1; i <= mc; i++)
if (mp[i] == 0)
free++;
else
clos--;
int cur = 1;
for (int i = 1; i <= free; i++) {
cur *= clos;
clos--;
}
ans = (ans + cur) % MOD;
return;
}
for (int c = 1; c <= min(mc + 1, k); c++) {
if (bad[x][y][c]) continue;
for (int X = x; X < n; X++)
for (int Y = y; Y < m; Y++) bad[X][Y][c]++;
res[x][y] = c;
int ok = 1;
if (a[x][y] != 0 && mp[c] != 0 && mp[c] != a[x][y]) ok = 0;
if (ok) {
bool flag = 0;
if (!mp[c]) {
mp[c] = a[x][y];
flag = 1;
}
int nmc = c > mc ? c : mc;
if (y < m - 1)
dfs(x, y + 1, nmc);
else
dfs(x + 1, 0, nmc);
if (flag) mp[c] = 0;
}
for (int X = x; X < n; X++)
for (int Y = y; Y < m; Y++) bad[X][Y][c]--;
}
}
signed main() {
scanf("%d%d%d", &n, &m, &k);
if (n + m - 1 > k) {
cout << 0 << '\n';
return 0;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
memset(bad, 0, sizeof(bad));
memset(mp, 0, sizeof(mp));
dfs(0, 0, 0);
printf("%d", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
int i, n, d[200010];
long long s, A, j, k;
int main() {
scanf("%d%I64d", &n, &A);
for (i = 0; i < n; i++) {
scanf("%d", &d[i]);
s += d[i];
}
for (i = 0; i < n; i++) {
j = n - 1 - A + d[i];
k = A - 1 - s + d[i];
printf("%I64d%c", (j > 0 ? j : 0) + (k > 0 ? k : 0),
i + 1 == n ? '\n' : ' ');
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A &a, B b) {
if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
if (a < b) a = b;
}
signed main() {
long long N;
cin >> N;
vector<long long> cnt(5);
long long sum = 0;
for (long long i = 0; i < (N); i++) {
long long c;
cin >> c;
cnt[c]++;
sum += c;
}
long long ans = LLONG_MAX;
for (long long x = 0; x * 3 <= sum; x++) {
if ((sum - 3 * x) % 4) continue;
long long y = (sum - 3 * x) / 4;
vector<long long> a = cnt;
long long val = 0;
if (a[4] >= y) {
long long t = a[4] - y;
val += t;
a[3] += t;
if (x - a[3] <= a[2])
val += (a[2] - (x - a[3])) * 2 + a[1];
else
val += max(0ll, a[1] - (x - a[3] - a[2]));
} else {
if (a[4] + a[3] >= x + y) {
val = a[1] + a[2] * 2 + (a[4] + a[3] - x - y) * 3;
} else if (a[4] + a[3] + a[2] >= x + y)
val = a[1] + (a[4] + a[3] + a[2] - x - y) * 2;
else
val = a[4] + a[3] + a[2] + a[1] - x - y;
}
chmin(ans, val);
}
if (ans == LLONG_MAX)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:400000000")
int n, a[200010], b[200010], f[200010];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
int x;
cin >> x;
f[x - 1] = i;
}
for (int i = 0; i < n; i++) b[i] = f[a[i] - 1];
for (int i = 1; i < n; i++) {
if (b[i] < b[i - 1]) {
cout << n - i;
return 0;
}
}
cout << 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
struct matrix {
int M[4][4];
matrix() { memset(M, 0, sizeof(M)); }
};
matrix one, stand;
matrix operator*(matrix M1, matrix M2) {
matrix M3;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
for (int k = 0; k < 4; ++k)
M3.M[i][j] = (M3.M[i][j] + 1LL * M1.M[i][k] * M2.M[k][j]) % MOD;
return M3;
}
matrix power(matrix X, long long ex) {
if (ex == 0) return one;
if (ex & 1) return X * power(X, ex - 1);
return power(X * X, ex >> 1);
}
long long N, M;
int Q[100002], C[4];
char A[100002];
int get_times(int size, string S, int lim1, int lim2) {
int qnow = 0;
for (int i = 2; i <= size; ++i) {
while (qnow != 0 && A[i] != A[qnow + 1]) qnow = Q[qnow];
if (A[i] == A[qnow + 1]) ++qnow;
Q[i] = qnow;
}
int matchnow = 0, totmatches = 0;
for (int i = 0; i < int(S.size()); ++i) {
while (matchnow != 0 && S[i] != A[matchnow + 1]) matchnow = Q[matchnow];
if (S[i] == A[matchnow + 1]) ++matchnow;
if (matchnow == size) {
if (i - size + 1 >= lim1 && i - size + 1 <= lim2) ++totmatches;
matchnow = Q[matchnow];
}
}
return totmatches;
}
int main() {
for (int i = 0; i < 4; ++i) one.M[i][i] = 1;
stand.M[0][0] = stand.M[1][0] = stand.M[2][0] = stand.M[0][1] =
stand.M[3][2] = stand.M[2][3] = 1;
cin >> N >> M;
for (int i = 1; i <= M; ++i) {
cin >> (A + 1);
int size = 0;
for (int j = 1; A[j] != '\0'; ++j) ++size;
int fib1 = 1, fib2 = 1, numnow = 0;
while (fib1 < size) {
int auxfib = fib2;
fib2 = fib1 + fib2;
fib1 = auxfib;
++numnow;
}
string s1, s2;
s1 = "a";
s2 = "b";
for (int j = 1; j <= numnow; ++j) {
string saux = s2;
s2 += s1;
s1 = saux;
}
if (N - (numnow + 1) <= 1) {
if (N - (numnow + 1) < 0) {
cout << 0 << '\n';
continue;
}
while (N - (numnow + 1) > 0) {
string saux = s2;
s2 += s1;
s1 = saux;
++numnow;
}
cout << get_times(size, s1, 0, s1.size() - 1) << '\n';
continue;
}
int times_a = get_times(size, s1, 0, s1.size() - 1);
int times_b = get_times(size, s2, 0, s2.size() - 1);
int times_ab =
get_times(size, s1 + s2, s1.size() - size + 1, s1.size() - 1);
int times_ba =
get_times(size, s2 + s1, s2.size() - size + 1, s2.size() - 1);
int times_bb =
get_times(size, s2 + s2, s2.size() - size + 1, s2.size() - 1);
matrix now = power(stand, N - numnow - 2);
int num_a = 0;
C[0] = 0, C[1] = 1, C[2] = 0, C[3] = 0;
for (int j = 0; j < 4; ++j)
num_a = (num_a + 1LL * now.M[j][0] * C[j]) % MOD;
int num_b = 0;
C[0] = 1, C[1] = 0, C[2] = 0, C[3] = 0;
for (int j = 0; j < 4; ++j)
num_b = (num_b + 1LL * now.M[j][0] * C[j]) % MOD;
int num_ab = 0;
C[0] = 0, C[1] = 0, C[2] = 0, C[3] = 1;
for (int j = 0; j < 4; ++j)
num_ab = (num_ab + 1LL * now.M[j][0] * C[j]) % MOD;
now = power(stand, N - numnow - 3);
int num_ba = 0;
C[0] = 1, C[1] = 0, C[2] = 0, C[3] = 0;
for (int j = 0; j < 4; ++j)
num_ba = (num_ba + 1LL * now.M[j][0] * C[j]) % MOD;
int num_bb = 0;
C[0] = 0, C[1] = 0, C[2] = 0, C[3] = 1;
for (int j = 0; j < 4; ++j)
num_bb = (num_bb + 1LL * now.M[j][0] * C[j]) % MOD;
long long result = 1LL * times_ba * num_ba + 1LL * times_ab * num_ab +
1LL * times_bb * num_bb + 1LL * times_a * num_a +
1LL * times_b * num_b;
while (result < 0) result += MOD;
while (result >= MOD) result -= MOD;
cout << result << '\n';
}
}
| 8 |
#include <bits/stdc++.h>
char Input[500020];
using namespace std;
struct Edge {
int to, next;
} E[500020 << 1];
int In[500020], qaq;
int T, N, head[500020], A, qwq, B, i, j, ans, cnt;
inline void Add(int u, int v) {
E[++cnt].to = v, In[v]++;
E[cnt].next = head[u], head[u] = cnt;
E[++cnt].to = u, In[u]++;
E[cnt].next = head[v], head[v] = cnt;
}
int main() {
cin >> T;
while (T--) {
scanf("%d", &N), ++qwq;
fill(In, In + N + 4, 0);
fill(head, head + N + 4, 0), ans = 0, qaq = 0;
for (i = 1; i < N; ++i) scanf("%d%d", &A, &B), Add(A, B);
scanf("%s", Input);
if (N < 3)
puts("Draw");
else if (N == 3) {
for (i = 0; i < N; ++i) ans += Input[i] == 'W';
puts(ans >= 2 ? "White" : "Draw");
} else {
int Linshi = 0;
for (i = 0; i < N; ++i)
if (Input[i] == 'W') {
head[++N] = 0, Add(i + 1, N), In[N] = 3;
}
for (i = 1; i <= N && ans <= 0; ++i) {
if (In[i] > 3)
ans++;
else if (In[i] == 3) {
Linshi = 0;
for (j = head[i]; j; j = E[j].next) Linshi += (In[E[j].to] >= 2);
ans += Linshi > 1, qaq++;
}
}
if (qaq == 2 && (N % 2)) ans++;
puts(ans ? "White" : "Draw");
}
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
set<pair<long, long> > s;
vector<pair<long, long> > v;
set<pair<long, long> >::iterator itr;
pair<long, long> p;
long n, m;
cin >> n >> m;
long a, b;
cin >> a >> b;
for (int i = 1; i <= n; i++) {
long x, y;
cin >> x >> y;
long num = a * x + b * y;
p.first = num;
p.second = i;
v.push_back(p);
}
sort(v.begin(), v.end());
long long sum = 0;
int count = 0, index = -1;
for (int i = 0; i < v.size(); i++) {
if ((v[i].first + sum) <= m) {
index = i;
sum += v[i].first;
} else {
break;
}
}
cout << index + 1 << endl;
for (int i = 0; i <= index; i++) {
cout << v[i].second << " ";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n;
const long long N = 1e6 + 5;
long long Tree[4 * N];
long long query(long long x, long long l, long long r, long long k) {
if (l == r) {
return l;
}
long long mid = (l + r) / 2;
if (Tree[2 * x] >= k) {
return query(2 * x, l, mid, k);
} else {
return query(2 * x + 1, mid + 1, r, k - Tree[2 * x]);
}
}
void update(long long x, long long l, long long r, long long idx,
long long val) {
if (l == r) {
Tree[x] += val;
return;
}
long long mid = (l + r) / 2;
if (idx <= mid) {
update(2 * x, l, mid, idx, val);
} else {
update(2 * x + 1, mid + 1, r, idx, val);
}
Tree[x] = Tree[2 * x] + Tree[2 * x + 1];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
while (n > 0) {
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (abs(x1 - x2) == 0 or abs(y1 - y2) == 0) {
cout << "1\n";
} else {
long long a = abs(x1 - x2);
long long b = abs(y1 - y2);
long long ans = a * b + 1;
cout << a * b + 1 << "\n";
}
n--;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
long long b, d, s;
cin >> b >> d >> s;
if ((b == 0 && d == 0 && s == 0) || (b == 1 && d == 0 && s == 0) ||
(b == 0 && d == 1 && s == 0) || (b == 0 && d == 0 && s == 1)) {
cout << 0;
return 0;
}
if (b >= d && b >= s) {
if (d == s) {
cout << max(b - d - 1, 0ll) * 2;
return 0;
}
if (d > s) {
cout << max(b - d - 1, 0ll) + b - s - 1;
return 0;
}
cout << max(b - s - 1, 0ll) + b - d - 1;
return 0;
}
if (d >= b && d >= s) {
if (b == s) {
cout << max(d - b - 1, 0ll) * 2;
return 0;
}
if (b > s) {
cout << max(d - b - 1, 0ll) + d - s - 1;
return 0;
}
cout << max(d - s - 1, 0ll) + d - b - 1;
return 0;
}
if (b == d) {
cout << max((s - d - 1) * 2, 0ll);
return 0;
}
if (b > d) {
cout << max(s - b - 1, 0ll) + s - d - 1;
return 0;
}
cout << max(s - d - 1, 0ll) + s - b - 1;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int re_ad() {
int x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
return x * f;
}
int n, k, q;
int a[200010], b[200010], c[200010];
int sum[200010], id[200010], L[200010], f[200010], cnt;
int num[1010][16500], ans, tot;
const int K = 400;
inline void change1(int p, int v) {
register int i;
if (!v) return;
for (i = p; i <= n; i += k) {
sum[i] ^= v;
if (sum[i] == 0) --ans;
if (sum[i] == v) ++ans;
}
i = p % k + ((n - p % k) / k) * k;
if (sum[i] == 0) --tot;
if (sum[i] == v) ++tot;
}
inline void change2(int p, int v) {
register int i;
if (!v) return;
for (i = p; id[i] == id[p]; i += k) {
if ((sum[i] ^ f[id[p]]) == 0) ++ans;
--num[id[p]][sum[i]];
sum[i] ^= v;
++num[id[p]][sum[i]];
if ((sum[i] ^ f[id[p]]) == 0) --ans;
}
for (i = id[p] + 1; i <= cnt && L[i] % k == p % k; ++i) {
ans += num[i][f[i]];
f[i] ^= v;
ans -= num[i][f[i]];
}
i = p % k + ((n - p % k) / k) * k;
if ((sum[i] ^ f[id[i]]) == 0) --tot;
if ((sum[i] ^ f[id[i]]) == v) ++tot;
}
inline void change(int pla, int nu) {
if (n / k < K)
change1(pla, nu ^ c[pla]);
else
change2(pla, nu ^ c[pla]);
c[pla] = nu;
}
int main() {
register int i, j;
char s[5];
n = re_ad();
k = re_ad();
q = re_ad();
for (i = 1; i <= n; ++i) a[i] = re_ad();
for (i = 1; i <= n; ++i) b[i] = re_ad();
++n;
if (n / k >= K) {
for (i = 1; i <= k; ++i)
for (j = i; j <= n; j += k) {
if (((j - 1) / k + 1) % K == 1) {
L[++cnt] = j;
num[cnt][0] = min(K, (n - j) / k + 1);
}
id[j] = cnt;
}
}
for (i = 1; i <= n; ++i) change(i, a[i] ^ a[i - 1] ^ b[i] ^ b[i - 1]);
if (tot)
puts("-1");
else
printf("%d\n", ans);
while (q--) {
scanf("%s", s + 1);
i = re_ad();
j = re_ad();
if (s[1] == 'a')
a[i] = j;
else
b[i] = j;
change(i, a[i] ^ a[i - 1] ^ b[i] ^ b[i - 1]);
if (i != n) ++i, change(i, a[i] ^ a[i - 1] ^ b[i] ^ b[i - 1]);
if (tot)
puts("-1");
else
printf("%d\n", ans);
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
void fill_(S& buf, T x) {
fill(reinterpret_cast<T*>(&buf),
reinterpret_cast<T*>(&buf) + sizeof(buf) / sizeof(T), x);
}
template <typename T, int N>
inline int countof(const T (&)[N]) {
return N;
}
template <typename T>
inline vector<T>& operator+=(vector<T>& ret, const T& x) {
ret.push_back(x);
return ret;
}
vector<long long> xs;
long long maxAnimals(const long long a) {
long long acc = 0;
for (int i = int(0), CNT_i = int(int((xs).size())); i < CNT_i; ++i)
acc += min(a, xs[i]);
return acc;
}
int main(int argc, char* argv[]) {
long long N, K;
cin >> N >> K;
xs.assign(int(N), 0);
for (int i = int(0), CNT_i = int(N); i < CNT_i; ++i) {
if (!(cin >> xs[i])) return 0;
}
long long sum = accumulate((xs).begin(), (xs).end(), 0LL);
if (sum <= K) {
if (sum < K) cout << -1 << endl;
return 0;
}
long long l = 0, r = 1000000001;
while (r - l > 1) {
const long long m = (l + r) / 2;
if (maxAnimals(m) <= K) {
l = m;
} else {
r = m;
}
}
K -= maxAnimals(l);
for (int i = int(0), CNT_i = int(N); i < CNT_i; ++i)
xs[i] = max(0LL, xs[i] - l);
int k = 0;
for (; K > 0; ++k)
if (xs[k % N]) {
--xs[k % N];
--K;
}
bool b = false;
for (int i = int(0), CNT_i = int(N); i < CNT_i; ++i)
if (xs[(i + k) % N]) {
if (b) cout << " ";
b = true;
cout << (i + k) % N + 1;
}
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
static inline void canhazfast() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
template <typename T>
T gcd(T a, T b) {
return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T extgcd(T a, T b, T &x, T &y) {
T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
while (b) {
T q = a / b;
a %= b;
swap(a, b);
x0 -= q * x1;
swap(x0, x1);
y0 -= q * y1;
swap(y0, y1);
}
x = x0;
y = y0;
return a;
}
static inline int ctz(unsigned x) { return __builtin_ctz(x); }
static inline int ctzll(unsigned long long x) { return __builtin_ctzll(x); }
static inline int clz(unsigned x) { return __builtin_clz(x); }
static inline int clzll(unsigned long long x) { return __builtin_clzll(x); }
static inline int popcnt(unsigned x) { return __builtin_popcount(x); }
static inline int popcntll(unsigned long long x) {
return __builtin_popcountll(x);
}
static inline int bsr(unsigned x) { return 31 ^ clz(x); }
static inline int bsrll(unsigned long long x) { return 63 ^ clzll(x); }
int get(const string &s) {
int r = 0;
for (char c : s) r |= 1 << (c - 'a');
return r;
}
int main() {
canhazfast();
static int m[100000];
static char t[100000];
int n, req = 0, tot = 0;
int x = -1, fin, cur = (1 << 26) - 1;
string s;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> t[i] >> s;
m[i] = get(s);
}
x = s[0] - 'a';
fin = 1 << x;
for (int i = 0; i < n; ++i) {
if (t[i] == '!') {
++tot;
if (cur != fin) ++req;
cur &= m[i];
} else if (t[i] == '.') {
cur &= ~m[i];
} else if (i + 1 != n) {
++tot;
if (cur != fin) ++req;
cur &= ~m[i];
}
}
cout << tot - req;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ld = double;
const ld eps = 1e-9;
int n , d , b[100010] , c[100010] , traceid[100010][11] , tracemd[100010][11];
ld a[100010] , dp[100010][11];
bool greate(ld a , ld b) {
return a - b > eps;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> d;
for (int i = 0 ; i < n ; ++i) {
int x;
cin >> x;
a[i] = log(x);
b[i] = x % 10;
c[i] = x;
}
for (int i = 0 ; i <= n ; ++i)
for (int j = 0 ; j <= 9 ; ++j) dp[i][j] = (ld)-1e9;
dp[0][1] = 0;
for (int i = 0 ; i < n ; ++i)
for (int j = 0 ; j <= 9 ; ++j) {
ld nw = dp[i][j] + a[i];
int nwm = (j * b[i]) % 10;
if (greate(nw , dp[i + 1][nwm])) {
dp[i + 1][nwm] = nw;
tracemd[i + 1][nwm] = j;
traceid[i + 1][nwm] = 1;
}
if (greate(dp[i][j] , dp[i + 1][j])) {
dp[i + 1][j] = dp[i][j];
traceid[i + 1][j] = 0;
}
}
if (dp[n][d] < 0) {
cout << -1;
exit(0);
}
vector < int > res;
while (n > 0) {
if (traceid[n][d]) {
res.push_back(n - 1);
d = tracemd[n][d];
}
n--;
}
if (res.empty()) {
cout << -1;
exit(0);
}
cout << res.size() << ' ';
for (auto x : res) cout << c[x] << ' ';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline int mod(int n) { return (n % 1000000007); }
int cnt[112345] = {0};
int v[112345];
int n, m;
struct query {
int id, l, r, ans;
bool operator<(const query& b) const {
return l / (int)sqrt(n) < b.l / (int)sqrt(n) ||
l / (int)sqrt(n) == b.l / (int)sqrt(n) && r < b.r;
}
};
struct SqrtDecomposition {
vector<query> q;
void read(int sz) {
q.resize(sz);
for (int i = 0; i < q.size(); i++) {
q[i].id = i;
scanf("%d%d", &q[i].l, &q[i].r);
q[i].l--;
q[i].r--;
}
}
int curr = 0;
void add(int idx) {
if (v[idx] > 100000) return;
if (cnt[v[idx]] == v[idx]) curr--;
cnt[v[idx]]++;
if (cnt[v[idx]] == v[idx]) curr++;
}
void remove(int idx) {
if (v[idx] > 100000) return;
if (cnt[v[idx]] == v[idx]) curr--;
cnt[v[idx]]--;
if (cnt[v[idx]] == v[idx]) curr++;
}
int answer_query() { return curr; }
void calculate() {
sort(q.begin(), q.end());
int l = 0, r = -1;
for (int i = 0; i < q.size(); i++) {
while (q[i].l < l) add(--l);
while (r < q[i].r) add(++r);
while (q[i].l > l) remove(l++);
while (r > q[i].r) remove(r--);
q[i].ans = answer_query();
}
}
void print_ans() {
sort(q.begin(), q.end(),
[](const query& a, const query& b) { return a.id < b.id; });
for (query x : q) {
printf("%d\n", x.ans);
}
}
};
int main() {
scanf("%d%d", &n, &m);
SqrtDecomposition mos;
for (int i = 0; i < n; i++) {
scanf("%d", &v[i]);
}
mos.read(m);
mos.calculate();
mos.print_ans();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int sx, sy;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
cin >> sx >> sy;
int l = 0, r = 0, u = 0, d = 0;
for (int i = 1, x, y; i <= n; ++i) {
cin >> x >> y;
if (x < sx) ++l;
if (x > sx) ++r;
if (y > sy) ++u;
if (y < sy) ++d;
}
int mx = max(l, max(r, max(u, d)));
cout << mx << endl;
if (l == mx) {
cout << sx - 1 << " " << sy << endl;
} else if (r == mx) {
cout << sx + 1 << " " << sy << endl;
} else if (u == mx) {
cout << sx << " " << sy + 1 << endl;
} else {
cout << sx << " " << sy - 1 << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
inline void print(int x) {
write(x);
puts("");
}
struct node {
int x, y, c, next;
} a[3110000], b[3110000];
int len1, last1[1111000], len2, last2[1111000];
void ins_a(int x, int y) {
len1++;
a[len1].x = x;
a[len1].y = y;
a[len1].next = last1[x];
last1[x] = len1;
}
void ins_b(int x, int y, int c) {
len2++;
b[len2].x = x;
b[len2].y = y;
b[len2].c = c;
b[len2].next = last2[x];
last2[x] = len2;
}
int low[410000], dfn[410000], sta[410000], tp, id, cnt;
int r[410000];
void link() {
for (int i = 2; i <= r[0]; i++) ins_b(cnt, r[i], 0), ins_b(r[i], cnt, 0);
ins_b(r[1], cnt, 1);
ins_b(cnt, r[1], 1);
}
void tarjan(int x, int fa) {
low[x] = dfn[x] = ++id;
sta[++tp] = x;
for (int k = last1[x]; k; k = a[k].next)
if (a[k].y != fa) {
int y = a[k].y;
if (dfn[y] == -1) {
tarjan(y, x);
low[x] = min(low[x], low[y]);
if (low[y] > dfn[x])
ins_b(x, y, 1), ins_b(y, x, 1), tp--;
else if (low[y] == dfn[x]) {
cnt++;
r[r[0] = 1] = x;
int i;
do {
i = sta[tp--];
r[++r[0]] = i;
} while (i != y);
link();
}
} else
low[x] = min(low[x], dfn[y]);
}
}
int bin[25], fa[611000][25], sum[611000][25], dep[811000];
void pre_tree_node(int x) {
for (int i = 1; bin[i] <= dep[x]; i++)
fa[x][i] = fa[fa[x][i - 1]][i - 1],
sum[x][i] = sum[x][i - 1] + sum[fa[x][i - 1]][i - 1];
for (int k = last2[x]; k; k = b[k].next) {
int y = b[k].y;
if (y != fa[x][0]) {
fa[y][0] = x;
dep[y] = dep[x] + 1;
sum[y][0] = b[k].c;
pre_tree_node(y);
}
}
}
int pa;
int lca(int x, int y) {
pa = 0;
if (dep[x] < dep[y]) swap(x, y);
for (int i = 20; i >= 0; i--)
if (bin[i] <= dep[x] && dep[fa[x][i]] >= dep[y])
pa += sum[x][i], x = fa[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (bin[i] <= dep[x] && fa[x][i] != fa[y][i])
pa += sum[x][i] + sum[y][i], x = fa[x][i], y = fa[y][i];
pa += sum[x][0] + sum[y][0];
return fa[x][0];
}
int up(int x, int p) {
for (int i = 20; i >= 0; i--)
if (bin[i] <= p) p -= bin[i], x = fa[x][i];
return x;
}
int n, m, Q;
int main() {
bin[0] = 1;
for (int i = 1; i <= 20; i++) bin[i] = bin[i - 1] << 1;
n = read();
m = read();
Q = read();
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
ins_a(x, y);
ins_a(y, x);
}
cnt = n;
memset(dfn, -1, sizeof(dfn));
for (int i = 1; i <= n; i++)
if (dfn[i] == -1) tarjan(i, 0);
for (int i = 1; i <= n; i++)
if (dep[i] == 0) pre_tree_node(i);
while (Q--) {
int x = read(), y = read();
int LA = lca(x, y);
if (LA <= n)
print(pa);
else {
int u = up(x, dep[x] - dep[LA] - 1), v = up(y, dep[y] - dep[LA] - 1);
if (!sum[u][0] && !sum[v][0]) pa++;
print(pa);
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
typedef struct node {
int l;
int r;
int id;
} ss;
int __an = 0;
ss ans[1000006];
int ak[1000006];
int bit[1000006];
map<int, int> my;
int a[1000006];
int b[1000006];
bool cmp(node p, node q) { return p.r < q.r; }
int sum(int i) {
int ask = 0;
while (i > 0) {
ask ^= bit[i];
i -= i & (-i);
}
return ask;
}
void up(int i, int n, int t) {
while (i <= t) {
bit[i] ^= n;
i += i & (-i);
}
}
int main(void) {
int n, m, k;
int i, j;
while (scanf("%d", &k) != EOF) {
my.clear();
__an++;
memset(bit, 0, sizeof(bit));
for (i = 1; i <= k; i++) {
scanf("%d", &ak[i]);
}
memset(a, 0, sizeof(a));
for (i = 1; i <= k; i++) {
a[i] = a[i] ^ ak[i];
a[i] ^= a[i - 1];
}
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &ans[i].l, &ans[i].r);
ans[i].id = i;
}
sort(ans, ans + n, cmp);
for (i = 1; i <= k; i++) {
if (!my[ak[i]]) {
my[ak[i]] = i;
}
up(i, ak[i], k);
}
int ac = 1;
for (i = 0; i < n; i++) {
for (j = ac + 1; j <= ans[i].r; j++) {
int id = my[ak[j]];
if (id != j) {
up(id, ak[j], k);
my[ak[j]] = j;
}
}
ac = ans[i].r;
b[ans[i].id] = a[ans[i].r] ^ a[ans[i].l - 1];
b[ans[i].id] ^= sum(ans[i].r) ^ sum(ans[i].l - 1);
}
if (__an == 98)
printf("WOCAO\n");
else {
for (i = 0; i < n; i++) {
printf("%d\n", b[i]);
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
class BIT {
std::vector<int> bit;
public:
BIT(int size) : bit(size + 1, 0) {}
void add(int i, int x) {
i++;
while (i < (int)bit.size()) {
bit[i] += x;
i += i & -i;
}
}
int sum(int a) {
a++;
int res = 0;
while (0 < a) {
res += bit[a];
a -= a & -a;
}
return res;
}
int sum(int a, int b) { return sum(b) - sum(a - 1); }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n;
cin >> n;
BIT xs(s.size()), ys(s.size()), zs(s.size());
for (int i = 0; i < (s.size()); i++) {
if (s[i] == 'x')
xs.add(i, 1);
else if (s[i] == 'y')
ys.add(i, 1);
else
zs.add(i, 1);
}
for (int i = 0; i < (n); i++) {
int l, r;
cin >> l >> r;
l--;
r--;
int x, y, z;
x = xs.sum(l, r);
y = ys.sum(l, r);
z = zs.sum(l, r);
string ans = "NO";
if (abs(x - y) < 2 && abs(x - z) < 2 && abs(y - z) < 2 || r - l < 2)
ans = "YES";
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct node {
long long v;
bool c;
} arr[12000];
bool comparator(node a, node b) { return a.v < b.v; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long i, j, p, v, l, n = 0, a, ans = 0;
cin >> a;
string s;
cin >> s;
l = s.length();
s = '+' + s;
for (i = 0; i < l; i += 3) {
v = 0;
p = s[i++] == '+' ? 1 : -1;
if (s[i] >= '0' and s[i] <= '9')
for (; s[i] >= '0' and s[i] <= '9'; i++) v = v * 10 + s[i] - 48;
else
v = 1;
if (s[i] == '*') i++;
n++;
arr[n].v = v * p;
arr[n].c = s[i] == 'a';
}
sort(arr + 1, arr + 1 + n, comparator);
for (i = 1; i <= n; i++) {
if (arr[i].c)
ans += arr[i].v * a++;
else
ans += arr[i].v * ++a;
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios ::sync_with_stdio(false);
ll n;
cin >> n;
if (n % 3 != 0) {
cout << "0\n";
return 0;
}
n /= 3;
int ans = 0;
vector<ll> divs;
for (ll i = 1; i * i <= n; i++)
if (n % i == 0) divs.push_back(i);
int m = divs.size();
for (int i = 0; i < m; i++) {
if (divs[i] * divs[i] * divs[i] > n) break;
for (int j = i; j < m; j++) {
if (divs[i] * divs[j] * divs[j] > n) break;
if (n % (divs[i] * divs[j])) continue;
ll x = divs[i], y = divs[j], z = n / divs[i] / divs[j];
if ((x + y + z) & 1) continue;
ll s = (x + y + z) / 2, a = s - x, b = s - y, c = s - z;
if (z >= y && a > 0 && b > 0 && c > 0) {
if (a == b && b == c)
ans++;
else if (a == b || b == c || c == a)
ans += 3;
else
ans += 6;
}
}
}
cout << ans << '\n';
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, c = 0;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < s.length(); i++)
if (s[i] == '1') c++;
int ans = n + c;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '1') {
int w = max(((i + 1) * 2), ((n - i) * 2));
ans = max(w, ans);
}
}
cout << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
const int mod = 1e9 + 7;
long long phi[N], vis[N], prime[N];
vector<int> vv[N];
int cnt;
void init() {
phi[1] = 1;
for (int i = 2; i < N; i++)
if (!phi[i]) {
for (int j = i; j < N; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
}
long long a[N];
void run() {
int n;
scanf("%d", &n);
for (int i = 1; i < N; i++) {
for (int j = i; j < N; j += i) {
vv[j].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
long long ans = 0;
unordered_map<long long, long long> mp;
for (int i = 1; i <= n; i++) {
long long tot = 0;
mp.clear();
for (int j = 1; j <= n / i; j++) {
for (auto it : vv[a[j * i]]) {
mp[it]++;
}
}
for (auto it : mp) {
tot = (tot + phi[it.first] * it.second % mod * it.second % mod) % mod;
}
ans = (ans + tot * phi[i] % mod) % mod;
}
printf("%lld\n", ans);
}
int main() {
cnt = 0;
init();
run();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long len[100005];
long long rem[100005];
long long dif[100005];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = (1); i <= (n); i += (1)) cin >> len[i];
string S;
cin >> S;
long long sta = 0, tme = 0;
bool water = 0, grass = 0;
for (int i = (1); i <= (n); i += (1)) {
if (S[i - 1] == 'W') {
water = true;
tme += len[i] * 3;
sta += len[i];
} else if (S[i - 1] == 'G') {
grass = true;
tme += len[i] * 5;
sta += len[i];
} else {
long long req = len[i];
if (sta >= req)
sta -= req;
else {
if (water)
tme += (req - sta) * 3;
else
tme += (req - sta) * 5;
sta = 0;
}
tme += len[i];
}
rem[i] = sta;
}
memset(dif, 0, sizeof(dif));
for (int i = (n); i >= (1); i -= (1)) {
if (i < n) rem[i] = min(rem[i], rem[i + 1]);
if (S[i - 1] == 'G') {
long long mx = (len[i] << 1);
if (rem[i] >= mx) {
tme -= len[i] * 4;
dif[i + 1] -= mx;
rem[i] -= mx;
} else {
tme -= rem[i] * 4 / 2;
dif[i + 1] -= rem[i];
rem[i] = 0;
}
}
}
for (int i = (1); i <= (n); i += (1)) {
dif[i] += dif[i - 1];
rem[i] += dif[i];
}
for (int i = (n); i >= (1); i -= (1)) {
if (i < n) rem[i] = min(rem[i], rem[i + 1]);
if (S[i - 1] == 'W') {
long long mx = (len[i] << 1);
if (rem[i] >= mx) {
tme -= len[i] * 2;
rem[i] -= mx;
} else {
tme -= rem[i] * 2 / 2;
rem[i] = 0;
}
}
}
printf("%lld\n", tme);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string str;
cin >> str;
int dp[n];
map<int, int> firstocc;
dp[0] = str[0] == '0' ? -1 : 1;
firstocc[dp[0]] = 0;
firstocc[0] = -1;
int ans = 0;
for (int i = 1; i < n; i++) {
dp[i] = str[i] == '0' ? dp[i - 1] - 1 : dp[i - 1] + 1;
if (!firstocc[dp[i]] && dp[i] != dp[0])
firstocc[dp[i]] = i;
else {
int check = i - firstocc[dp[i]];
ans = max(check, ans);
}
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
const long long INF = 1000000000000000;
int a[N];
int main() {
int t;
cin >> t;
for (int q = 0; q < t; q++) {
int n, k;
cin >> n >> k;
for (int j = 0; j < n; j++) {
cin >> a[j];
}
long long answer = -INF;
for (long long j = 1; j < n; j++) {
long long i = j - 1;
while (i >= 0 and (i + 1) * (j + 1) - k * a[j] > answer) {
long long result = (i + 1) * (j + 1) - k * (a[i] | a[j]);
answer = max(answer, result);
i--;
}
}
cout << answer << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int evn = 0, odd = 0;
while (n--) {
int x;
cin >> x;
if (x % 2)
odd++;
else
evn++;
}
if (odd % 2)
cout << odd;
else
cout << evn;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[200];
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) cin >> arr[i];
if (m < n)
cout << 0;
else if (m == n)
cout << 1;
else {
vector<int> v;
for (int i = 0; i < m; i++) {
bool ok = true;
for (int j = 0; j < v.size(); j++) {
if (arr[i] == v[j]) {
ok = false;
break;
}
}
if (ok) v.push_back(arr[i]);
}
vector<int> v1(v.size(), 0);
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < m; j++) {
if (v[i] == arr[j]) v1[i]++;
}
}
for (int i = m; i >= 1; i--) {
int res = 0;
for (int j = 0; j < v1.size(); j++) {
res += v1[j] / i;
}
if (res >= n) {
cout << i;
break;
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main() {
int testcase, num;
scanf("%d", &testcase);
for (int a = 0; a < testcase; a++) {
scanf("%d", &num);
int arr[num], total = 0;
for (int b = 0; b < num; b++) {
scanf("%d", &arr[b]);
}
int flag = 1;
for (int c = 1; c < num; c++) {
if (arr[c] != arr[c - 1]) flag = 0;
}
if (flag == 0)
printf("1\n");
else if (flag == 1)
printf("%d\n", num);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename S, typename T>
ostream& operator<<(ostream& out, pair<S, T> const& p) {
out << '(' << p.first << ", " << p.second << ')';
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
long long l = v.size();
for (long long i = 0; i < l - 1; i++) out << v[i] << ' ';
if (l > 0) out << v[l - 1];
return out;
}
template <typename T>
void trace(const char* name, T&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
trace(comma + 1, args...);
}
const long long N = 110;
const long long MOD = 1e6 + 3;
long long add(long long a, long long b) {
long long res = a + b;
if (res >= MOD) return res - MOD;
return res;
}
long long mul(long long a, long long b) {
long long res = a;
res *= b;
if (res >= MOD) return res % MOD;
return res;
}
struct matrix {
long long SZ;
vector<vector<long long>> arr;
matrix(long long s) {
SZ = s;
arr.resize(s, vector<long long>(s));
}
void reset() {
for (long long i = 0; i < SZ; i++) {
for (long long j = 0; j < SZ; j++) {
arr[i][j] = 0;
}
}
}
void makeiden() {
reset();
for (long long i = 0; i < SZ; i++) {
arr[i][i] = 1;
}
}
matrix operator+(const matrix& o) const {
matrix res(o.SZ);
for (long long i = 0; i < SZ; i++) {
for (long long j = 0; j < SZ; j++) {
res.arr[i][j] = add(arr[i][j], o.arr[i][j]);
}
}
return res;
}
matrix operator*(const matrix& o) const {
matrix res(o.SZ);
for (long long i = 0; i < SZ; i++) {
for (long long j = 0; j < SZ; j++) {
res.arr[i][j] = 0;
for (long long k = 0; k < SZ; k++) {
res.arr[i][j] = add(res.arr[i][j], mul(arr[i][k], o.arr[k][j]));
}
}
}
return res;
}
void print() {
for (long long i = 0; i < SZ; i++) {
for (long long j = 0; j < SZ; j++) {
cout << arr[i][j] << " ";
}
cout << "\n";
}
}
};
matrix power(matrix a, long long b) {
matrix res = a;
b--;
while (b) {
if (b & 1) {
res = res * a;
}
a = a * a;
b >>= 1;
}
return res;
}
vector<long long> multiply_vector(matrix& a, vector<long long>& b) {
long long SZ = a.SZ;
vector<long long> ans(a.SZ);
for (long long i = 0; i < SZ; i++) {
long long curr = 0;
for (long long j = 0; j < SZ; j++) {
curr = (curr + (b[j] * a.arr[i][j]) % MOD) % MOD;
}
ans[i] = curr;
}
return ans;
}
vector<long long> multiply_vector(vector<long long>& b, matrix& a) {
long long SZ = a.SZ;
vector<long long> ans(a.SZ);
for (long long i = 0; i < SZ; i++) {
long long curr = 0;
for (long long j = 0; j < SZ; j++) {
curr = add(curr, mul(a.arr[j][i], b[j]));
}
ans[i] = curr;
}
return ans;
}
long long c, w, h;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> c >> w >> h;
matrix m(N);
m.reset();
for (long long i = 0; i <= w; i++) {
m.arr[0][i] = 1;
}
for (long long i = 1; i <= w; i++) {
m.arr[i][i - 1] = h;
}
matrix res = power(m, c);
long long ans = 0;
for (long long i = 0; i <= w; i++) {
ans = (ans + res.arr[i][0]) % MOD;
}
cout << ans << "\n";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool found;
int n, x, nums[1000];
void Greedy() {
int median = (n + 1) / 2;
sort(nums, nums + n);
if (nums[--median] == x) {
printf("0");
return;
}
int smaller, larger;
if (found) {
int index = -1;
for (int i = 0; nums[i] <= x && i < n; i++)
if (x == nums[i] && abs(index - median) > abs(i - median)) index = i;
smaller = index;
larger = n - index - 1;
if (larger < smaller)
printf("%d", smaller - larger);
else
printf("%d", larger - smaller - 1);
} else {
for (smaller = 0; smaller < n && nums[smaller] < x; smaller++)
;
larger = n - smaller;
printf("%d", abs(smaller - larger) + (smaller >= larger));
}
}
int main() {
found = false;
scanf("%d %d", &n, &x);
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
found |= (nums[i] == x);
}
Greedy();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 26, MOD = 1e9 + 7;
int a[N][N], tmp[N], mx[N], sum[N], dp[1 << (N - 6)];
string s[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(dp, 63, sizeof(dp));
dp[0] = 0;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> a[i][j];
dp[(1 << i)] = min(dp[(1 << i)], a[i][j]);
for (int msk = 0; msk < (1 << n); msk++)
dp[msk | (1 << i)] = min(dp[msk | (1 << i)], a[i][j] + dp[msk]);
}
for (int j = 0; j < m; j++) {
memset(tmp, 0, sizeof(tmp));
memset(sum, 0, sizeof(sum));
memset(mx, 0, sizeof(mx));
for (int i = 0; i < n; i++) {
tmp[s[i][j] - 'a'] |= (1 << i);
sum[s[i][j] - 'a'] += a[i][j];
mx[s[i][j] - 'a'] = max(mx[s[i][j] - 'a'], a[i][j]);
}
for (int i = 0; i < 26; i++) {
if (!tmp[i]) continue;
int S = sum[i] - mx[i];
for (int msk = 0; msk < (1 << n); msk++)
dp[msk | tmp[i]] = min(dp[msk | tmp[i]], S + dp[msk]);
}
}
cout << dp[(1 << n) - 1];
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100005;
int son[MAX];
double ans[MAX];
double fact[MAX];
vector<int> graph[MAX], ng[MAX];
int getSon(int u, int dad) {
son[u] = 1;
for (int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if (v == dad) continue;
ng[u].push_back(v);
son[u] += getSon(v, u);
}
return son[u];
}
void solve(int u) {
double sum = son[u] - 1;
for (int i = 0; i < ng[u].size(); i++) {
int v = ng[u][i];
ans[v] += ans[u] + (sum - son[v]) * 0.5 + 1;
}
for (int i = 0; i < ng[u].size(); i++) {
int v = ng[u][i];
solve(v);
}
}
int main() {
int n, x;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d", &x);
x--;
graph[i].push_back(x);
graph[x].push_back(i);
}
double cur = 0;
fact[1] = 1;
fact[2] = 0.5;
for (int i = 3; i <= n; i++) {
fact[i] = 1.0 / i + (1.0 / i) * fact[i - 1];
}
getSon(0, 0);
ans[0] = 0.0;
solve(0);
for (int i = 0; i < n; i++) {
printf("%.1lf ", ans[i] + 1);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, q;
const long long nax = 2e5 + 4;
vector<long long> cnt;
long long a[nax];
void solve() {
cin >> n >> q;
cnt.resize(43);
for (long long i = 1; i <= n; ++i) {
cin >> a[i];
cnt[__builtin_ctz(a[i])]++;
}
sort(a + 1, a + n + 1);
for (long long i = 1; i <= q; ++i) {
long long b;
cin >> b;
vector<long long> temp = cnt;
bool ok = true;
for (long long i = 0; i <= 40; ++i) {
if ((1LL << i) & b) {
if (temp[i] == 0) {
ok = false;
cout << -1 << '\n';
break;
} else {
temp[i]--;
temp[i + 1] += temp[i] / 2;
temp[i] = 0;
}
} else {
temp[i + 1] += temp[i] / 2;
temp[i] = 0;
}
}
if (ok) {
long long cost = 0;
vector<long long> test = cnt;
for (long long i = 0; i <= 39; ++i) {
if ((1LL << i) & b) {
if (test[i] > 0) {
++cost;
--test[i];
} else {
long long delta = 1;
for (long long j = i - 1; j >= 0; --j) {
delta <<= 1;
if (test[j] > 0) {
long long lol = min(delta, test[j]);
test[j] -= lol;
delta -= lol;
cost += lol;
if (delta > 0) continue;
}
}
}
}
}
cout << cost << '\n';
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t = 1;
while (t--) {
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, a[201], i, k = 0, s = 0, j;
cin >> t;
for (i = 1; i <= t; i++) {
cin >> n;
for (j = 0; j <= n - 1; j++) {
cin >> a[j];
s = s + a[j];
}
for (j = 0; j <= n - 1; j++) {
if (a[j] == 0) {
k++;
}
}
if (s > 0) {
cout << k << "\n";
}
if (s == 0) {
if (k == 0) {
cout << 1 << "\n";
} else {
cout << k << "\n";
}
}
if (s < 0) {
if (k == 0) {
cout << 0 << "\n";
}
if (k > 0) {
if (0 - s == k) {
cout << k + 1 << "\n";
} else {
cout << k << "\n";
}
}
}
k = 0;
s = 0;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
short int q;
scanf("%hi", &q);
while (q--) {
short int n;
scanf("%hi", &n);
short int Cnt[2049] = {0};
for (short int i = 0; i < n; i++) {
int a;
scanf("%i", &a);
if (a < 2049) Cnt[a]++;
}
for (short int i = 1; i < 1025; i *= 2) {
Cnt[i * 2] += Cnt[i] / 2;
}
if (Cnt[2048])
printf("Yes\n");
else
printf("NO\n");
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
namespace Fast {
template <typename __VariableName>
inline __VariableName read() {
__VariableName __X = 0, __W = 1;
char __c = getchar();
while (__c < '0' || __c > '9') {
if (__c == '-') __W = -1;
__c = getchar();
}
while (__c >= '0' && __c <= '9') __X = __X * 10 + __c - '0', __c = getchar();
return __X * __W;
}
template <typename __VariableName>
inline void print(__VariableName __X) {
if (__X < 0) putchar('-'), __X = -__X;
if (__X > 9) print(__X / 10);
putchar(__X % 10 + '0');
}
}; // namespace Fast
using namespace Fast;
struct node {
int to, next;
};
inline void upmin(int &x, const int &y) {
if (y < x) x = y;
}
const int N = 100010;
int val[N << 1], n, m, q, tot;
multiset<int> st[N];
namespace TCD {
const int N = 200010;
node e[N << 1];
int h[N], sum = 0;
int fa[N], top[N], hson[N];
int size[N], dep[N];
int pos[N], tid[N], clk = 0;
int mn[N << 2];
void AddEdge(int u, int v) {
e[++sum].to = v;
e[sum].next = h[u];
h[u] = sum;
}
void add_edge(int u, int v) {
AddEdge(u, v);
AddEdge(v, u);
}
void dfs1(int u, int la) {
size[u] = 1;
int mx = 0;
for (int tmp = h[u]; tmp; tmp = e[tmp].next) {
int v = e[tmp].to;
if (v == la) continue;
dep[v] = dep[u] + 1;
dfs1(v, u);
fa[v] = u;
size[u] += size[v];
if (size[v] > mx) mx = size[v], hson[u] = v;
}
}
void dfs2(int u, int tp) {
top[u] = tp;
tid[pos[u] = ++clk] = u;
if (hson[u]) dfs2(hson[u], tp);
for (int tmp = h[u]; tmp; tmp = e[tmp].next)
if (e[tmp].to != fa[u] && e[tmp].to != hson[u]) dfs2(e[tmp].to, e[tmp].to);
}
inline void maintain(int o) { mn[o] = min(mn[o << 1], mn[o << 1 | 1]); }
void Build(int o, int l, int r) {
if (l == r) {
mn[o] = val[tid[l]];
return;
}
int mid = (l + r) / 2;
Build(o << 1, l, mid);
Build(o << 1 | 1, mid + 1, r);
maintain(o);
}
void Modify(int o, int l, int r, int k, int x) {
if (l == r) {
mn[o] = x;
return;
}
int mid = (l + r) / 2;
if (k <= mid)
Modify(o << 1, l, mid, k, x);
else
Modify(o << 1 | 1, mid + 1, r, k, x);
maintain(o);
}
int Query(int o, int l, int r, int nl, int nr) {
if (l >= nl && r <= nr) return mn[o];
int mid = (l + r) / 2, res = 0x7f7f7f7f;
if (nl <= mid) upmin(res, Query(o << 1, l, mid, nl, nr));
if (nr > mid) upmin(res, Query(o << 1 | 1, mid + 1, r, nl, nr));
return res;
}
int PathQuery(int u, int v) {
int res = 0x7f7f7f7f;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
upmin(res, Query(1, 1, tot, pos[top[u]], pos[u]));
u = fa[top[u]];
}
if (dep[u] > dep[v]) swap(u, v);
upmin(res, Query(1, 1, tot, pos[u], pos[v]));
if (u > n) upmin(res, val[fa[u]]);
return res;
}
} // namespace TCD
namespace Graph {
node e[N << 1];
int h[N], sum = 0;
int dfn[N], low[N], tim = 0;
stack<int> stk;
void add_edge(int u, int v) {
e[++sum].to = v;
e[sum].next = h[u];
h[u] = sum;
}
void Tarjan(int u) {
dfn[u] = low[u] = ++tim;
stk.push(u);
for (int tmp = h[u]; tmp; tmp = e[tmp].next) {
int v = e[tmp].to;
if (!dfn[v]) {
Tarjan(v);
upmin(low[u], low[v]);
if (low[v] >= dfn[u]) {
int o;
tot++;
do {
o = stk.top();
stk.pop();
TCD::add_edge(o, tot);
} while (o != v);
TCD::add_edge(u, tot);
}
} else
upmin(low[u], dfn[v]);
}
}
} // namespace Graph
int main() {
int x, y;
scanf("%d%d%d", &n, &m, &q);
tot = n;
for (int i = 1; i <= n; i++) val[i] = read<int>();
for (int i = 1; i <= m; i++) {
x = read<int>();
y = read<int>();
Graph::add_edge(x, y);
Graph::add_edge(y, x);
}
for (int i = 1; i <= n; i++)
if (!Graph::dfn[i]) Graph::Tarjan(i);
TCD::dfs1(1, 0);
TCD::dfs2(1, 1);
for (int i = 2; i <= n; i++) st[TCD::fa[i] - n].insert(val[i]);
for (int i = n + 1; i <= tot; i++)
val[i] = st[i - n].empty() ? 0x7f7f7f7f : *st[i - n].begin();
TCD::Build(1, 1, tot);
while (q--) {
char opt = getchar();
while (opt != 'C' && opt != 'A') opt = getchar();
x = read<int>();
y = read<int>();
if (opt == 'C') {
TCD::Modify(1, 1, tot, TCD::pos[x], y);
if (x == 1) {
val[x] = y;
continue;
}
int o = TCD::fa[x];
st[o - n].erase(st[o - n].find(val[x]));
st[o - n].insert(y);
int minv = *st[o - n].begin();
if (minv == val[o]) {
val[x] = y;
continue;
}
TCD::Modify(1, 1, tot, TCD::pos[o], minv);
val[o] = minv;
val[x] = y;
} else
printf("%d\n", TCD::PathQuery(x, y));
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int c=200002;
int w, n, d[c], ml[c], maxi, msp;
vector<int> sz[c];
bool v[c];
void dfs(int a) {
int ert=n;
v[a]=true;
if (sz[a].size()==1) {
ert=d[a];
}
for (int x:sz[a]) {
if (!v[x]) {
d[x]=d[a]+1;
dfs(x);
ml[a]=min(ml[a], ml[x]);
int y=ml[x]-d[a];
maxi=max(maxi, y);
if (a==1 && msp<y) msp=y;
else maxi=max(maxi, y+1);
ert=min(ert, ml[x]);
}
}
ml[a]=ert;
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> w;
while(w--) {
cin >> n;
for (int i=1; i<=n; i++) d[i]=0, ml[i]=n+1, v[i]=0, maxi=0, msp=0, sz[i].clear();
for (int i=1; i<n; i++) {
int a, b; cin >> a >> b;
sz[a].push_back(b), sz[b].push_back(a);
}
dfs(1);
cout << maxi << "\n";
}
return 0;
}
/*
1
6
1 2 2 3
1 4
1 5 5 6
*/
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int M = 2000010;
const int INF = 1e6;
int n, k, x;
vector<int> v[2];
int f[2][M];
bool flag = false;
inline void Min(int &x, int y) {
if (y < x) x = y;
}
bool pd[1010];
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++) {
scanf("%d", &x);
if (pd[x]) continue;
pd[x] = true;
x -= n;
if (x == 0) flag = true;
if (x <= 0)
v[0].push_back(-x);
else
v[1].push_back(x);
}
if (flag) {
puts("1");
return 0;
}
for (int i = 0; i < M; i++) f[0][i] = f[1][i] = INF;
f[0][0] = f[1][0] = 0;
int ans = INF;
for (int i = 0; i < M; i++) {
if (i > 0) ans = min(ans, f[0][i] + f[1][i]);
for (int j = 0; j < v[0].size(); j++) {
if (i + v[0][j] < M) Min(f[0][i + v[0][j]], f[0][i] + 1);
}
for (int j = 0; j < v[1].size(); j++) {
if (i + v[1][j] < M) Min(f[1][i + v[1][j]], f[1][i] + 1);
}
}
if (ans == INF)
puts("-1");
else
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
int main() {
int a, b, r;
scanf("%d %d %d", &a, &b, &r);
r *= 2;
if (r > a || r > b) {
printf("Second\n");
return 0;
}
printf("First\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000;
const int INF = 0x3f3f3f3f;
const int MOD = 1000003;
template <class T>
void Read(T &x) {
x = 0;
char c = getchar();
bool flag = 0;
while (c < '0' || '9' < c) {
if (c == '-') flag = 1;
c = getchar();
}
while ('0' <= c && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (flag) x = -x;
}
int dp[MAXN + 10][MAXN + 10];
int n;
char str[MAXN + 10];
bool sp[MAXN + 10];
int cnt;
inline int adjust(int v) {
if (v >= MOD)
return v - MOD;
else if (v < 0)
return v + MOD;
else
return v;
}
inline bool isop(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '\0';
}
inline bool check() {
if (isop(str[n])) return false;
for (int i = 1; i <= n; ++i)
if ((str[i] == '*' || str[i] == '/') && isop(str[i - 1])) return false;
return true;
}
int main() {
scanf("%s", str + 1);
n = strlen(str + 1);
if (!check())
puts("0");
else {
cnt = 0;
for (int i = 1; i <= n; ++i)
if (isop(str[i])) {
++cnt;
if (isop(str[i - 1])) sp[cnt] = true;
}
dp[0][0] = 1;
for (int i = 1; i <= cnt; ++i) {
if (sp[i])
for (int j = 1; j <= cnt; ++j) dp[i][j] = dp[i - 1][j - 1];
else
for (int j = 0; j <= cnt; ++j) dp[i][j] = dp[i - 1][j];
for (int j = 1; j <= cnt; ++j) dp[i][j] = adjust(dp[i][j] + dp[i][j - 1]);
for (int j = 0; j <= cnt - 1; ++j) dp[i][j] = dp[i][j + 1];
}
printf("%d\n", dp[cnt][0]);
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long d[200001];
int main() {
int n;
cin >> n;
if (n == 1)
cout << 1;
else {
for (int i = 2; i <= n; i++) cout << i << " ";
cout << 1;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b, c, d;
cin >> a >> b >> c >> d;
int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
if ((a.size() - 2) >= (b.size() - 2) * 2 &&
(a.size() - 2) >= (c.size() - 2) * 2 &&
(a.size() - 2) >= (d.size() - 2) * 2) {
c1 = 1;
} else if ((a.size() - 2) * 2 <= (b.size() - 2) &&
(a.size() - 2) * 2 <= (c.size() - 2) &&
(a.size() - 2) * 2 <= (d.size() - 2)) {
c1 = 1;
}
if ((b.size() - 2) >= (a.size() - 2) * 2 &&
(b.size() - 2) >= (c.size() - 2) * 2 &&
(b.size() - 2) >= (d.size() - 2) * 2) {
c2 = 1;
} else if ((b.size() - 2) * 2 <= (a.size() - 2) &&
(b.size() - 2) * 2 <= (c.size() - 2) &&
(b.size() - 2) * 2 <= (d.size() - 2)) {
c2 = 1;
}
if ((c.size() - 2) >= (a.size() - 2) * 2 &&
(c.size() - 2) >= (b.size() - 2) * 2 &&
(c.size() - 2) >= (d.size() - 2) * 2) {
c3 = 1;
} else if ((c.size() - 2) * 2 <= (a.size() - 2) &&
(c.size() - 2) * 2 <= (b.size() - 2) &&
(c.size() - 2) * 2 <= (d.size() - 2)) {
c3 = 1;
}
if ((d.size() - 2) >= (a.size() - 2) * 2 &&
(d.size() - 2) >= (b.size() - 2) * 2 &&
(d.size() - 2) >= (c.size() - 2) * 2) {
c4 = 1;
} else if ((d.size() - 2) * 2 <= (a.size() - 2) &&
(d.size() - 2) * 2 <= (b.size() - 2) &&
(d.size() - 2) * 2 <= (c.size() - 2)) {
c4 = 1;
}
if (c1 + c2 + c3 + c4 > 1 || c1 + c2 + c3 + c4 == 0)
cout << c[0] << endl;
else {
if (c1 == 1)
cout << a[0] << endl;
else if (c2 == 1)
cout << b[0] << endl;
else if (c3 == 1)
cout << c[0] << endl;
else if (c4 == 1)
cout << d[0] << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2, M = 2e5 + 2;
int a[N], lj[M], nxt[M], fir[N], dfn[N], top[N], hc[N], siz[N], f[N], dep[N],
st[N];
int n, m, q, i, x, y, c, bs, tp, ance, ans;
inline void read(int &x) {
c = getchar();
while ((c < 48) || (c > 57)) c = getchar();
x = c ^ 48;
c = getchar();
while ((c >= 48) && (c <= 57)) {
x = x * 10 + (c ^ 48);
c = getchar();
}
}
inline void add(int x, int y) {
lj[++bs] = y;
nxt[bs] = fir[x];
fir[x] = bs;
}
void dfs1(int x) {
siz[x] = 1;
int i;
for (i = fir[x]; i; i = nxt[i])
if (lj[i] != f[x]) {
dep[lj[i]] = dep[f[lj[i]] = x] + 1;
dfs1(lj[i]);
siz[x] += siz[lj[i]];
if (siz[hc[x]] < siz[lj[i]]) hc[x] = lj[i];
}
}
void dfs2(int x) {
dfn[x] = ++bs;
if (hc[x]) {
int i;
top[hc[x]] = top[x];
dfs2(hc[x]);
for (i = fir[x]; i; i = nxt[i])
if ((lj[i] != f[x]) && (lj[i] != hc[x])) dfs2(top[lj[i]] = lj[i]);
}
}
inline int lca(int x, int y) {
while (top[x] != top[y])
if (dep[top[x]] > dep[top[y]])
x = f[top[x]];
else
y = f[top[y]];
if (dep[x] < dep[y]) return x;
return y;
}
void qs(int l, int r) {
int i = l, j = r, m = dfn[a[l + r >> 1]];
while (i <= j) {
while (dfn[a[i]] < m) ++i;
while (dfn[a[j]] > m) --j;
if (i <= j) swap(a[i++], a[j--]);
}
if (i < r) qs(i, r);
if (l < j) qs(l, j);
}
inline void ins(int x) {
if (tp == 0) {
st[tp = 1] = x;
return;
}
ance = lca(st[tp], x);
while ((tp > 1) && (dep[ance] < dep[st[tp - 1]])) {
add(st[tp - 1], st[tp]);
--tp;
}
if (dep[ance] < dep[st[tp]]) add(ance, st[tp--]);
if ((!tp) || (st[tp] != ance)) st[++tp] = ance;
st[++tp] = x;
}
void dfs3(int x) {
int i;
if (siz[x])
for (i = fir[x]; i; i = nxt[i]) {
dfs3(lj[i]);
if (siz[lj[i]]) {
siz[lj[i]] = 0;
++ans;
}
}
else {
for (i = fir[x]; i; i = nxt[i]) {
dfs3(lj[i]);
siz[x] += siz[lj[i]];
siz[lj[i]] = 0;
}
if (siz[x] > 1) {
++ans;
siz[x] = 0;
}
}
fir[x] = 0;
}
int main() {
read(n);
for (i = 1; i < n; i++) {
read(x);
read(y);
add(x, y);
add(y, x);
}
bs = 0;
dfs1(dep[1] = 1);
dfs2(top[1] = 1);
memset(fir + 1, 0, n << 2);
memset(siz + 1, 0, n << 2);
read(q);
bs = 0;
while (q--) {
x = 1;
read(m);
for (i = 1; i <= m; i++) {
read(a[i]);
siz[a[i]] = 1;
}
for (i = 1; i <= m; i++)
if (siz[f[a[i]]]) {
puts("-1");
x = 0;
break;
}
if (!x) {
while (m) siz[a[m--]] = 0;
continue;
}
ans = 0;
qs(1, m);
if (a[1] != 1) st[tp = 1] = 1;
for (i = 1; i <= m; i++) ins(a[i]);
if (tp)
while (--tp) add(st[tp], st[tp + 1]);
dfs3(1);
siz[1] = bs = 0;
printf("%d\n", ans);
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
long long c = 0;
while (n > 0) {
n = n >> 1;
c++;
}
cout << c;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int maxN = 1100000;
int d[maxN];
int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
++d[u];
++d[v];
}
long long N = n;
long long total = N * (N - 1) * (N - 2) / 6LL;
long long add = 0;
for (int i = 1; i <= n; ++i) {
long long x = d[i];
long long y = n - 1 - x;
add += x * y;
}
cout << total - (add / 2) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, m = 0, k = 0, ds;
bool test = false, test1 = false;
long long const N = 1e5 + 7;
long long const oo = 1000000007;
long long nChoosek(long long n, long long k) {
if (k > n) return 0;
if (k * 2 > n) k = n - k;
if (k == 0) return 1;
long long result = n;
for (long long i = 2; i <= k; ++i) {
result *= (n - i + 1);
result /= i;
}
return result;
}
vector<long long> v, v1;
vector<pair<long long, long long> > vv;
map<long long, long long> my;
map<long long, long long> my1;
priority_queue<long long> q;
long long t[N];
long long t1[N];
long long dx[4] = {0, 1, 0, -1};
long long dy[4] = {1, 0, -1, 0};
bool in_da_prison(long long x, long long y) {
return (x < 9 && x >= 0 && y < 9 && y >= 0);
}
long long dp(long long i) {
if (i == 0) return 1;
if (i < 0) return 0;
if (t[i] != -1) return t[i];
long long choice1 = dp(i - 1) % oo;
long long choice2 = dp(i - k) % oo;
return t[i] = (choice1 % oo + choice2 % oo) % oo;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long i = 0, a = 0, b = 0, c = 0, d = 1e10, x = 0, y = 0, s = 0;
string s1, s2, s3, s4, s5;
long double j = 0.0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a;
my[a]++;
j -= (my[a - 1] - my[a + 1]);
j += a * i - a * (n - i - 1);
}
cout << fixed << setprecision(0) << j;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
inline int dcmp(double first) {
if (fabs(first) < eps)
return 0;
else
return first < 0 ? -1 : 1;
}
struct Point {
double first, second;
Point(double first = 0, double second = 0) : first(first), second(second) {}
void read() { scanf("%lf%lf", &first, &second); }
void write() { printf("%lf %lf", first, second); }
bool operator==(const Point& u) const {
return dcmp(first - u.first) == 0 && dcmp(second - u.second) == 0;
}
bool operator!=(const Point& u) const { return !(*this == u); }
bool operator<(const Point& u) const {
return dcmp(first - u.first) < 0 ||
(dcmp(first - u.first) == 0 && dcmp(second - u.second) < 0);
}
bool operator>(const Point& u) const { return u < *this; }
bool operator<=(const Point& u) const { return *this < u || *this == u; }
bool operator>=(const Point& u) const { return *this > u || *this == u; }
Point operator+(const Point& u) {
return Point(first + u.first, second + u.second);
}
Point operator-(const Point& u) {
return Point(first - u.first, second - u.second);
}
Point operator*(const double u) { return Point(first * u, second * u); }
Point operator/(const double u) { return Point(first / u, second / u); }
double operator*(const Point& u) {
return first * u.second - second * u.first;
}
};
double getCross(Point a, Point b) {
return a.first * b.second - a.second * b.first;
}
bool isPointInConvexPolygon(vector<Point>& poly, Point p) {
int n = poly.size(), lo = 1, hi = poly.size() - 1;
if (getCross(poly[1] - poly[0], p - poly[0]) <= 0) return 0;
if (getCross(poly[n - 1] - poly[0], p - poly[0]) >= 0) return 0;
while (hi - lo > 1) {
int mid = (lo + hi) / 2;
if (getCross(poly[mid] - poly[0], p - poly[0]) > 0)
lo = mid;
else
hi = mid;
}
return getCross(poly[hi] - poly[lo], p - poly[lo]) > 0;
}
int main() {
long long N, M;
vector<Point> A, B;
scanf("%lld", &N);
A.resize(N);
for (long long i = (0); i < (N); i++) A[i].read();
scanf("%lld", &M);
B.resize(M);
for (long long i = (0); i < (M); i++) B[i].read();
bool ans = true;
reverse(A.begin(), A.end());
for (long long i = (0); i < (M); i++) {
ans &= isPointInConvexPolygon(A, B[i]);
}
if (ans)
cout << "YES\n";
else
cout << "NO\n";
}
| 6 |
#include <bits/stdc++.h>
int main() {
int m, n;
int a, b;
int ans = 0;
scanf("%d%d", &n, &m);
a = 0;
while (a * a <= m) {
b = m - a * a;
if (b * b + a == n) ans++;
a++;
}
printf("%d", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, c, Q, sl, fh, fa[100010];
set<pair<int, int> > ex[100010];
set<int> edge[100010];
int rd() {
sl = 0;
fh = 1;
char ch = getchar();
while (ch < '0' || '9' < ch) {
if (ch == '-') fh = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar();
return sl * fh;
}
int getfa(int x) {
if (x != fa[x]) fa[x] = getfa(fa[x]);
return fa[x];
}
void conncet(int x, int y) {
static int fx, fy;
fx = getfa(x);
fy = getfa(y);
if (fx == fy) return;
if (edge[fx].size() < edge[fy].size()) swap(edge[fx], edge[fy]);
for (set<int>::iterator i = edge[fy].begin(); i != edge[fy].end(); ++i)
edge[fx].insert(*i);
edge[fy].clear();
fa[fy] = fx;
}
void add(int u, int v, int clr) {
edge[getfa(u)].insert(v);
set<pair<int, int> >::iterator i = ex[v].lower_bound(make_pair(clr, 0));
if (i == ex[v].end() || i->first != clr)
ex[v].insert(make_pair(clr, u));
else
conncet(u, i->second);
}
int main() {
n = rd();
m = rd();
c = rd();
Q = rd();
int x, y, z, fx, fy;
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1; i <= m; ++i)
x = rd(), y = rd(), z = rd(), add(x, y, z), add(y, x, z);
for (char s[2]; Q; --Q) {
scanf("%s", s);
if (s[0] == '+')
x = rd(), y = rd(), z = rd(), add(x, y, z), add(y, x, z);
else {
x = rd();
y = rd();
fx = getfa(x);
fy = getfa(y);
if (fx == fy)
puts("Yes");
else if (*edge[fx].lower_bound(y) == y)
puts("Yes");
else
puts("No");
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const double ep = 1e-12;
const double pi = acos(-1);
struct pnt {
double x, y;
pnt(double x = 0, double y = 0) : x(x), y(y) {}
inline pnt operator-(const pnt &a) const { return pnt(x - a.x, y - a.y); }
inline pnt operator+(const pnt &a) const { return pnt(x + a.x, y + a.y); }
inline pnt operator*(const double &a) const { return pnt(x * a, y * a); }
inline pnt operator/(const double &a) const { return pnt(x / a, y / a); }
inline double operator&(const pnt &a) const { return x * a.x + y * a.y; }
inline double operator^(const pnt &a) const { return x * a.y - y * a.x; }
inline void input() { x = y = 0, scanf("%lf %lf", &x, &y); }
void FastInput() {
int a, b;
scanf("%d %d", &a, &b);
x = 1.0 * a;
y = 1.0 * b;
}
inline void print() { printf("%lf %lf\n", x, y); }
inline bool operator<(const pnt &a) const {
if (fabs(x - a.x) > ep) return x < a.x;
return y < a.y;
}
inline bool operator==(const pnt &a) const {
return fabs(x - a.x) < ep && fabs(y - a.y) < ep;
}
inline pnt Rotate(const double &angle) {
pnt R = pnt(sin(angle), cos(angle)), C = *this;
return pnt(C ^ R, C & R);
}
inline double dist() { return sqrt(x * x + y * y); }
inline pnt norm() { return (*this) / dist(); }
} p[4000001];
inline double dist(pnt a) { return sqrt(a & a); }
inline pnt rot(pnt p) { return pnt(-p.y, p.x); }
int intersect_circles(pnt c, double r, pnt cc, double rr, pnt p[]) {
double d = dist(c - cc);
if (r + rr < d || r + d < rr || rr + d < r) return 0;
double x = (r * r - rr * rr + d * d) / (d * 2);
pnt cp = (c * (d - x) + cc * x) / d;
if (r + rr == d || r + d == rr || rr + d == r) {
p[0] = cp;
return 1;
}
double h = sqrt(r * r - x * x);
pnt dv = rot(cc - c) * h / d;
p[0] = cp - dv;
p[1] = cp + dv;
return 2;
}
double a[4000001], b[4000001], sto[4000001];
int id[4000001], bit[4000001];
bool cmp(int x, int y) {
if (b[x] - a[x] != b[y] - a[y]) return b[x] - a[x] < b[y] - a[y];
return 0;
}
bool common(double a, double b, double c, double d) {
if (a <= c && c <= b) return 1;
if (a <= d && d <= b) return 1;
if (c <= a && a <= d) return 1;
if (c <= b && b <= d) return 1;
a += 2 * pi, b += 2 * pi;
if (a <= c && c <= b) return 1;
if (a <= d && d <= b) return 1;
if (c <= a && a <= d) return 1;
if (c <= b && b <= d) return 1;
a -= 2 * pi, b -= 2 * pi;
c += 2 * pi, d += 2 * pi;
if (a <= c && c <= b) return 1;
if (a <= d && d <= b) return 1;
if (c <= a && a <= d) return 1;
if (c <= b && b <= d) return 1;
return 0;
}
void add(int x, int val) {
while (x < 4000001) {
bit[x] += val;
x += x & -x;
}
}
int calc(int x) {
int sum = 0;
while (x) {
sum += bit[x];
x -= x & -x;
}
return sum;
}
bool cmpequal(double a, double b) { return fabs(a - b) < ep; }
int main() {
int n, A;
scanf("%d %d", &n, &A);
for (int i = 0; i < n; i++) p[i].input();
pnt P = pnt(A, 0), Q = pnt(-A, 0);
double ans = dist(P - Q);
double st = 0, en = ans;
for (int step = 0; step < 50; step++) {
double mid = (en + st) / 2;
int gas = 0, num = 0;
for (int i = 0; i < n; i++) {
double r = dist(p[i] - Q);
pnt res[10];
int x = intersect_circles(P, mid, p[i], r, res);
if (x != 2) continue;
a[gas] = atan2(res[0].y - P.y, res[0].x - P.x);
b[gas] = atan2(res[1].y - P.y, res[1].x - P.x);
if (a[gas] < 0) a[gas] += 2 * pi;
if (b[gas] < 0) b[gas] += 2 * pi;
if (a[gas] > b[gas]) swap(a[gas], b[gas]);
sto[num++] = a[gas], sto[num++] = b[gas];
gas++;
}
for (int i = 0; i < gas; i++) id[i] = i;
sort(id, id + gas, cmp);
sort(sto, sto + num);
for (int i = 0; i < 4000001; i++) bit[i] = 0;
num = unique(sto, sto + num, cmpequal) - sto;
int ok = 0;
for (int i = 0; i < gas; i++) {
int x = lower_bound(sto, sto + num, a[id[i]] - ep) - sto + 1;
int y = lower_bound(sto, sto + num, b[id[i]] - ep) - sto + 1;
x++;
y++;
if (calc(y) > 0) ok = 1;
if (calc(x) > 0) ok = 1;
add(y + 1, -1);
add(x, 1);
}
if (ok)
en = mid;
else
st = mid;
}
printf("%.10lf\n", en);
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
long long a[N];
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%I64d", a + i);
}
long long ans = 0;
for (int i = 1; i <= n; ++i) {
ans += (a[i] - 1) * i + 1;
}
printf("%I64d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> m;
long long k, b, n, ans0, ans, x, sum[1000001], cnt[1000001];
int main() {
cin >> k >> b >> n;
if (!b) {
for (long long i = 1; i <= n; i++) {
cin >> x;
cnt[0] = (!x) ? (cnt[0] + 1) : 0;
ans += cnt[0];
}
cout << ans << endl;
} else {
--k, --b;
for (long long i = 1; i <= n; i++) {
cin >> x;
cnt[i] = (!x) ? (cnt[i - 1] + 1) : 0;
sum[i] = (sum[i - 1] + x) % k;
}
m[0] = 1;
for (long long i = 1; i <= n; i++) {
long long tmp = (sum[i] - b - 1 + k) % k;
ans += m[tmp];
m[sum[i]]++;
if (tmp == sum[i]) ans -= cnt[i];
}
cout << ans << endl;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count = 1;
cin >> n;
int x;
for (int i = 1; i <= n; i++) {
if (i == 1) {
cin >> x;
continue;
}
int y;
cin >> y;
if (y != x) count++;
x = y;
}
cout << count;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int c, d, n, m, k;
int dp[10010];
int cal(int rem) {
if (rem <= 0) return 0;
int &ret = dp[rem];
if (ret != -1) return ret;
ret = min(c + cal(rem - n), d + cal(rem - 1));
return ret;
}
int main() {
cin >> c >> d >> n >> m >> k;
for (int i = 0; i <= n * m; i++) dp[i] = -1;
cout << cal(n * m - k) << '\n';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 211111;
const long long mod = 998244353;
long long n, m, a[maxn], b[maxn], lst[maxn], res = 1;
map<long long, long long> mm;
int main() {
memset(lst, -1, sizeof(lst));
scanf("%lld%lld", &n, &m);
for (long long i = 0; i < n; i++) scanf("%lld", &a[i]);
for (long long i = 0; i < m; i++) scanf("%lld", &b[i]), mm[b[i]] = i;
for (long long i = 0; i < n; i++)
if (mm.count(a[i])) lst[mm[a[i]]] = i;
for (long long i = 0; i < m; i++)
if (lst[i] == -1) {
puts("0");
return 0;
}
long long j = 0;
for (; j < lst[0]; j++)
if (a[j] < b[0]) {
puts("0");
return 0;
}
for (long long i = 1; i < m; i++) {
long long k = j, l = j;
for (; j < lst[i]; j++)
if (a[j] < b[i]) k = j;
if (a[k] < b[i - 1]) {
puts("0");
return 0;
} else
res = (res * (lst[i] - k)) % mod;
}
for (; j < n; j++)
if (a[j] < b[m - 1]) {
puts("0");
return 0;
}
printf("%lld\n", res);
return 0;
}
| 6 |
#include <bits/stdc++.h>
int g[105][105], n, m, a, b, ans = 3000, Max, Min;
int down(int ii, int jj, int a, int b) {
int an = 0;
for (int i = ii; i <= ii + a; i++)
for (int j = jj; j <= jj + b; j++)
if (g[i][j]) an++;
return an;
}
int main() {
for (int i = 1; i <= 100; i++)
for (int j = 1; j <= 100; j++) g[i][j] = 1;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &g[i][j]);
scanf("%d%d", &a, &b);
a--, b--;
if (a > b)
Max = a, Min = b;
else
Max = b, Min = a;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ans = (ans < (down(i, j, a, b) < down(i, j, b, a) ? down(i, j, a, b)
: down(i, j, b, a))
? ans
: (down(i, j, a, b) < down(i, j, b, a) ? down(i, j, a, b)
: down(i, j, b, a)));
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, m;
cin >> n >> m;
map<long long int, long long int> mp;
for (long long int i = 0; i < m; i++) {
long long int h[4] = {0};
long long int x, y, z;
cin >> x >> y >> z;
if (i == 0) {
mp[x] = 1;
mp[y] = 2;
mp[z] = 3;
} else {
if (mp.find(x) != mp.end()) h[mp[x]] = 1;
if (mp.find(y) != mp.end()) h[mp[y]] = 1;
if (mp.find(z) != mp.end()) h[mp[z]] = 1;
if (mp.find(x) == mp.end()) {
if (h[1] == 0) {
mp[x] = 1;
h[1] = 1;
} else if (h[2] == 0) {
mp[x] = 2;
h[2] = 1;
} else if (h[3] == 0) {
mp[x] = 3;
h[3] = 1;
}
}
if (mp.find(y) == mp.end()) {
if (h[1] == 0) {
mp[y] = 1;
h[1] = 1;
} else if (h[2] == 0) {
mp[y] = 2;
h[2] = 1;
} else if (h[3] == 0) {
mp[y] = 3;
h[3] = 1;
}
}
if (mp.find(z) == mp.end()) {
if (h[1] == 0) {
mp[z] = 1;
h[1] = 1;
} else if (h[2] == 0) {
mp[z] = 2;
h[2] = 1;
} else if (h[3] == 0) {
mp[z] = 3;
h[3] = 1;
}
}
}
}
for (auto x : mp) cout << x.second << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long ans;
int a[100005];
int c[100005];
int d[100005];
int e[100005];
vector<int> b[100005];
int main() {
int n;
cin >> n;
int i;
for (i = 1; i <= n; i++) scanf("%d", &a[i]), b[a[i]].push_back(i);
a[0] = n + 1;
a[n + 1] = n + 1;
for (i = 1; i <= n; i++) {
for (auto &j : b[i]) {
if (a[j - 1] == i) e[i]++;
if (a[j - 1] < i) {
ans -= (long long)a[j - 1] * (n - i + 1);
}
c[i]++;
if (a[j + 1] == i) e[i]++;
if (a[j + 1] < i) ans -= (long long)a[j + 1] * (n - i + 1);
}
ans += (long long)i * (n - i + 1) * (c[i] - e[i] / 2);
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
long long permutation[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2},
{1, 2, 0}, {2, 0, 1}, {2, 1, 0}};
long long coord[8][3];
long long coord2[8][3];
bool chosen[8];
bool chosen2[8];
long long position[8];
bool recur3() {
for (long long k = 4; k < 8; ++k) {
chosen2[k] = false;
}
for (long long k = 0; k < 3; ++k) {
coord2[4][k] = (coord2[2][k] - coord2[0][k]) +
(coord2[3][k] - coord2[0][k]) + coord2[0][k];
}
for (long long k = 0; k < 3; ++k) {
coord2[5][k] = (coord2[3][k] - coord2[0][k]) +
(coord2[1][k] - coord2[0][k]) + coord2[0][k];
}
for (long long k = 0; k < 3; ++k) {
coord2[6][k] = (coord2[1][k] - coord2[0][k]) +
(coord2[2][k] - coord2[0][k]) + coord2[0][k];
}
for (long long k = 0; k < 3; ++k) {
coord2[7][k] = (coord2[1][k] - coord2[0][k]) +
(coord2[2][k] - coord2[0][k]) +
(coord2[3][k] - coord2[0][k]) + coord2[0][k];
}
for (long long k = 0; k < 8; ++k) {
if (!chosen[k]) {
bool good = false;
for (long long j = 4; j < 8; ++j) {
if (chosen2[j]) continue;
for (long long i = 0; i < 6; ++i) {
bool flag = true;
for (long long h = 0; h < 3; ++h) {
if (coord[k][h] != coord2[j][permutation[i][h]]) {
flag = false;
break;
}
}
if (flag) {
good = true;
break;
}
}
if (good) {
chosen2[j] = true;
position[k] = j;
break;
}
}
if (!good) {
return false;
}
}
}
return true;
}
long long dprod(long long *a, long long *b, long long *c) {
long long ans = 0;
for (long long k = 0; k < 3; ++k) {
ans += (a[k] - c[k]) * (b[k] - c[k]);
}
return ans;
}
bool recur2(long long level = 1, long long last = 0) {
if (level == 4) {
if (dprod(coord2[1], coord2[2], coord2[0]) ||
dprod(coord2[2], coord2[3], coord2[0]) ||
dprod(coord2[3], coord2[1], coord2[0]))
return false;
if (dprod(coord2[1], coord2[1], coord2[0]) !=
dprod(coord2[2], coord2[2], coord2[0]) ||
dprod(coord2[1], coord2[1], coord2[0]) !=
dprod(coord2[3], coord2[3], coord2[0]) ||
dprod(coord2[1], coord2[1], coord2[0]) == 0)
return false;
return recur3();
}
for (long long k = last + 1; k < 8; ++k) {
chosen[k] = true;
for (long long j = 0; j < 6; ++j) {
for (long long i = 0; i < 3; ++i) {
coord2[level][i] = coord[k][permutation[j][i]];
}
position[k] = level;
if (recur2(level + 1, k)) return true;
}
chosen[k] = false;
}
return false;
}
bool recur1() {
for (long long k = 0; k < 3; ++k) {
coord2[0][k] = coord[0][k];
}
chosen[0] = true;
position[0] = 0;
if (recur2()) return true;
chosen[0] = false;
return false;
}
int main() {
std::ios_base::sync_with_stdio(0);
for (int k = 0; k < 8; ++k) {
for (int j = 0; j < 3; ++j) {
std::cin >> coord[k][j];
}
}
if (recur1()) {
std::cout << "YES\n";
for (int k = 0; k < 8; ++k) {
for (int j = 0; j < 3; ++j) {
std::cout << coord2[position[k]][j] << ' ';
}
std::cout << '\n';
}
} else {
std::cout << "NO\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<string> Vect;
int main() {
ios::sync_with_stdio(0);
long long n;
cin >> n;
string s;
for (int i = 0; i < n; ++i) {
cin >> s;
Vect.push_back(s);
}
string res;
int size = Vect[0].size();
for (int j = 0; j < size; ++j) {
char d = '#';
for (int i = 0; i < n; ++i) {
if (d != '?') {
if (d == '#') {
if (Vect[i][j] != '?') d = Vect[i][j];
} else {
if (d != Vect[i][j] && Vect[i][j] != '?') {
d = '?';
}
}
}
}
if (d == '#') d = 'a';
res += d;
}
cout << res;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int v[10] = {2, 7, 2, 3, 3, 4, 2, 5, 1, 2};
int x, y, z;
while (cin >> z) {
x = z / 10;
y = z % 10;
cout << v[x] * v[y] << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 19;
long long dp[1 << N][N], ans[N][N];
long long sum;
int n, m;
bool adj[N][N];
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u][v] = adj[v][u] = true;
}
for (int num = 1; num < (1 << n); num++) {
int st = -1;
for (int i = 0; i < n; i++) {
if (num & (1 << i)) {
if (st == -1) {
st = i;
if ((num ^ (1 << i)) == 0) dp[num][i] = 1;
continue;
} else {
if (((num ^ (1 << st)) ^ (1 << i)) == 0) {
if (adj[i][st]) dp[num][i] = 1;
continue;
} else {
for (int j = st + 1; j < n; j++) {
if (num & (1 << j)) {
if (adj[i][j]) {
dp[num][i] += dp[num ^ (1 << i)][j];
ans[st][i] += dp[num ^ (1 << i)][j];
}
}
}
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (adj[i][j]) sum += ans[i][j];
}
}
cout << sum / 2 << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool combPossible(int n, int k, vector<int> group) {
int total4seaters = n;
int total2seaters = n * 2;
int singleSoilders = 0;
int fourSeatCount = 0;
for (int i = 0; i < k; i++) {
while (group[i] >= 4) {
total4seaters--;
group[i] -= 4;
}
if (group[i] == 3) {
total4seaters--;
}
if (group[i] == 2) {
total2seaters--;
}
if (group[i] == 1) {
singleSoilders++;
}
}
while (total2seaters > 0 && total4seaters < 0) {
total2seaters -= 2;
total4seaters++;
}
while (total4seaters > 0 && total2seaters < 0) {
total2seaters++;
total4seaters--;
if (singleSoilders > 0)
singleSoilders--;
else {
fourSeatCount++;
if (fourSeatCount % 2 == 0) total2seaters++;
}
}
while (singleSoilders > 0 && (total2seaters > 0 || total4seaters > 0)) {
while (total2seaters > 0) {
total2seaters--;
singleSoilders--;
}
while (total4seaters > 0) {
total4seaters--;
singleSoilders -= 2;
}
}
if (total4seaters < 0 || total2seaters < 0 || singleSoilders > 0)
return false;
else
return true;
}
int main() {
int n, k, solInGrp;
;
vector<int> group;
cin >> n >> k;
for (int i = 0; i < k; i++) {
cin >> solInGrp;
group.push_back(solInGrp);
}
if (combPossible(n, k, group))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a < b) ? a : b; }
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
string s;
cin >> s;
if (n < 11) {
cout << "No\n";
} else if (n == 11) {
if (s[0] == '8')
cout << "Yes\n";
else
cout << "No\n";
} else {
long long cnt = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == '8') {
break;
} else
cnt++;
}
if (cnt <= (n - 11)) {
cout << "Yes\n";
} else
cout << "No\n";
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int x, y;
cin >> x >> y;
long long int a, b;
cin >> a >> b;
long long int ans1 = b * min(x, y) + a * abs(x - y);
long long int ans2 = a * (x + y);
if (ans1 < ans2)
cout << ans1 << "\n";
else {
cout << ans2 << "\n";
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, m, c[maxn], w[maxn];
pair<int, int> ans[maxn];
struct node {
long long first;
int second;
node() {}
node(long long a, int b) { first = a, second = b; }
bool operator<(const node& a) const { return first > a.first; }
};
priority_queue<node> pq;
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
ans[i].first = c[i] / 100;
ans[i].second = c[i] % 100;
c[i] = ans[i].second;
}
for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
long long sum = 0;
for (int i = 1; i <= n; i++) {
if (ans[i].second == 0) continue;
m -= c[i];
pq.push(node(1ll * w[i] * (100 - c[i]), i));
if (m < 0) {
node tmp = pq.top();
pq.pop();
ans[tmp.second].first++;
ans[tmp.second].second = 0;
m += 100;
sum += tmp.first;
}
}
printf("%lld\n", sum);
for (int i = 1; i <= n; i++) printf("%d %d\n", ans[i].first, ans[i].second);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m, ch[13][13], a[13][13], ans;
const int w[4][3][3] = {{{1, 1, 1}, {0, 1, 0}, {0, 1, 0}},
{{0, 1, 0}, {0, 1, 0}, {1, 1, 1}},
{{1, 0, 0}, {1, 1, 1}, {1, 0, 0}},
{{0, 0, 1}, {1, 1, 1}, {0, 0, 1}}};
int dbg;
inline bool check(int x, int y, int i) {
if (y + 2 >= m or x + 2 >= n) return false;
for (int(u) = 0; (u) < (3); (u)++)
for (int(v) = 0; (v) < (3); (v)++)
if (w[i][u][v])
if (a[x + u][y + v]) return 0;
return 1;
}
void dfs(int x, int y, int cnt, int __free) {
dbg++;
if (__free <= (ans - cnt) * 6.69) return;
if (y + 2 >= m) {
dfs(x + 1, 0, cnt, __free);
return;
}
if (x + 2 >= n) {
if (cnt > ans) {
ans = cnt;
memcpy(ch, a, sizeof a);
}
return;
}
for (int(i) = 0; (i) < (4); (i)++)
if (check(x, y, i)) {
cnt++;
for (int(u) = 0; (u) < (3); (u)++)
for (int(v) = 0; (v) < (3); (v)++)
if (w[i][u][v]) a[x + u][y + v] = cnt;
dfs(x, y + 1, cnt, __free - 5 - !a[x][y]);
cnt--;
for (int(u) = 0; (u) < (3); (u)++)
for (int(v) = 0; (v) < (3); (v)++)
if (w[i][u][v]) a[x + u][y + v] = 0;
}
dfs(x, y + 1, cnt, __free - !a[x][y]);
}
void print() {
cout << ans << endl;
for (int(i) = 0; (i) < (n); (i)++) {
for (int(j) = 0; (j) < (m); (j)++)
cout.put(ch[i][j] ? 'A' + ch[i][j] - 1 : '.');
cout << endl;
}
exit(0);
}
int main() {
ios::sync_with_stdio(false);
cout.tie(0), cin.tie(0);
cin >> n >> m;
if (n < 3 or m < 3) print();
dfs(0, 0, 0, n * m);
cerr << "dfs called " << dbg << " times." << endl;
print();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1000000000;
const long long int mod = 1000000000 + 7;
inline void IO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
inline int dcmp(long double x) { return x < -1e-12 ? -1 : (x > 1e-12); }
template <class T>
inline int CHECK(T MASK, int i) {
return (MASK >> i) & 1;
}
template <class T>
inline T ON(T MASK, int i) {
return MASK | (T(1) << i);
}
template <class T>
inline T OFF(T MASK, int i) {
return MASK & (~(T(1) << i));
}
template <typename T>
inline int CNT(T MASK) {
if (numeric_limits<T>::digits <= numeric_limits<unsigned int>::digits)
return __builtin_popcount(MASK);
else
return __builtin_popcountll(MASK);
}
template <class T>
inline int RIGHT(T MASK) {
return log2(MASK & -MASK);
}
int dx4[] = {0, 0, -1, +1};
int dy4[] = {+1, -1, 0, 0};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
inline void I(int& a) { scanf("%d", &a); }
inline void I(long long int& a) { scanf("%I64d", &a); }
inline void I(unsigned long long int& a) { scanf("%I64u", &a); }
inline void I(char* a) { scanf("%s", a); }
char Iarr[2000010];
inline void I(string& a) {
scanf("%s", Iarr);
a = Iarr;
}
template <typename T, typename... Args>
void I(T& a, Args&... args) {
I(a);
I(args...);
}
inline void OUT(int a) { printf("%d", a); }
inline void OUT(long long int a) { printf("%I64d", a); }
inline void OUT(const char* a) { printf("%s", a); }
inline void OUT(char* a) { printf("%s", a); }
inline void OUT(bool a) { printf("%d", a); }
inline void OUT(string a) {
for (__typeof(a.end()) it = (a.begin()) - ((a.begin()) > (a.end()));
it != (a.end()) - ((a.begin()) > (a.end()));
it += 1 - 2 * ((a.begin()) > (a.end())))
printf("%c", *it);
}
inline void OUT(unsigned long long int a) { printf("%I64u", a); }
template <typename T, typename... Args>
void OUT(T a, Args... args) {
OUT(a);
OUT(" ");
OUT(args...);
}
template <typename... Args>
void O(Args... args) {
OUT(args...);
OUT("\n");
}
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, pair<T1, T2> p) {
os << "{" << p.first << ", " << p.second << "}";
return os;
}
template <typename T>
inline ostream& operator<<(ostream& os, vector<T>& a) {
os << "[";
for (int i = 0; i < (int)a.size(); i++) {
if (i) os << ", ";
os << a[i];
}
os << "]";
return os;
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
const long double PI = acos(-1.0);
class PT {
public:
long double x, y;
PT() {}
PT(long double x, long double y) : x(x), y(y) {}
PT(const PT& p) : x(p.x), y(p.y) {}
long double Magnitude() { return sqrt(x * x + y * y); }
bool operator==(const PT& u) const {
return dcmp(x - u.x) == 0 && dcmp(y - u.y) == 0;
}
bool operator!=(const PT& u) const { return !(*this == u); }
bool operator<(const PT& u) const {
return dcmp(x - u.x) < 0 || (dcmp(x - u.x) == 0 && dcmp(y - u.y) < 0);
}
bool operator>(const PT& u) const { return u < *this; }
bool operator<=(const PT& u) const { return *this < u || *this == u; }
bool operator>=(const PT& u) const { return *this > u || *this == u; }
PT operator+(const PT& u) const { return PT(x + u.x, y + u.y); }
PT operator-(const PT& u) const { return PT(x - u.x, y - u.y); }
PT operator*(const long double u) const { return PT(x * u, y * u); }
PT operator/(const long double u) const { return PT(x / u, y / u); }
long double operator*(const PT& u) const { return x * u.y - y * u.x; }
};
long double dot(PT p, PT q) { return p.x * q.x + p.y * q.y; }
long double dist2(PT p, PT q) { return dot(p - q, p - q); }
long double dist(PT p, PT q) { return sqrt(dist2(p, q)); }
long double cross(PT p, PT q) { return p.x * q.y - p.y * q.x; }
long double myAsin(long double val) {
if (val > 1) return PI * 0.5;
if (val < -1) return -PI * 0.5;
return asin(val);
}
long double myAcos(long double val) {
if (val > 1) return 0;
if (val < -1) return PI;
return acos(val);
}
ostream& operator<<(ostream& os, const PT& p) {
os << "(" << p.x << "," << p.y << ")";
}
istream& operator>>(istream& is, PT& p) {
is >> p.x >> p.y;
return is;
}
PT RotateCCW90(PT p) { return PT(-p.y, p.x); }
PT RotateCW90(PT p) { return PT(p.y, -p.x); }
PT RotateCCW(PT p, long double t) {
return PT(p.x * cos(t) - p.y * sin(t), p.x * sin(t) + p.y * cos(t));
}
PT RotateAroundPointCCW(PT p, PT pivot, long double t) {
PT trans = p - pivot;
PT ret = RotateCCW(trans, t);
ret = ret + pivot;
return ret;
}
PT ProjectPointLine(PT a, PT b, PT c) {
return a + (b - a) * dot(c - a, b - a) / dot(b - a, b - a);
}
long double DistancePointLine(PT a, PT b, PT c) {
return dist(c, ProjectPointLine(a, b, c));
}
PT ProjectPointSegment(PT a, PT b, PT c) {
long double r = dot(b - a, b - a);
if (fabs(r) < 1e-12) return a;
r = dot(c - a, b - a) / r;
if (r < 0) return a;
if (r > 1) return b;
return a + (b - a) * r;
}
long double DistancePointSegment(PT a, PT b, PT c) {
return sqrt(dist2(c, ProjectPointSegment(a, b, c)));
}
long double DistancePointPlane(long double x, long double y, long double z,
long double a, long double b, long double c,
long double d) {
return fabs(a * x + b * y + c * z - d) / sqrt(a * a + b * b + c * c);
}
bool LinesParallel(PT a, PT b, PT c, PT d) {
return dcmp(cross(b - a, c - d)) == 0;
}
bool LinesCollinear(PT a, PT b, PT c, PT d) {
return LinesParallel(a, b, c, d) && dcmp(cross(a - b, a - c)) == 0 &&
dcmp(cross(c - d, c - a)) == 0;
}
bool SegmentsIntersect(PT a, PT b, PT c, PT d) {
if (LinesCollinear(a, b, c, d)) {
if (dcmp(dist2(a, c)) == 0 || dcmp(dist2(a, d)) == 0 ||
dcmp(dist2(b, c)) == 0 || dcmp(dist2(b, d)) == 0)
return true;
if (dcmp(dot(c - a, c - b)) > 0 && dcmp(dot(d - a, d - b)) > 0 &&
dcmp(dot(c - b, d - b)) > 0)
return false;
return true;
}
if (dcmp(cross(d - a, b - a)) * dcmp(cross(c - a, b - a)) > 0) return false;
if (dcmp(cross(a - c, d - c)) * dcmp(cross(b - c, d - c)) > 0) return false;
return true;
}
PT ComputeLineIntersection(PT a, PT b, PT c, PT d) {
b = b - a;
d = c - d;
c = c - a;
assert(dot(b, b) > 1e-12 && dot(d, d) > 1e-12);
return a + b * cross(c, d) / cross(b, d);
}
PT ComputeCircleCenter(PT a, PT b, PT c) {
b = (a + b) / 2;
c = (a + c) / 2;
return ComputeLineIntersection(b, b + RotateCW90(a - b), c,
c + RotateCW90(a - c));
}
bool PointOnSegment(PT s, PT e, PT p) {
if (p == s || p == e) return 1;
return dcmp(cross(s - p, s - e)) == 0 &&
dcmp(dot(PT(s.x - p.x, s.y - p.y), PT(e.x - p.x, e.y - p.y))) < 0;
}
int PointInPolygon(vector<PT> p, PT q) {
int i, j, cnt = 0;
int n = p.size();
for (i = 0, j = n - 1; i < n; j = i++) {
if (PointOnSegment(p[i], p[j], q)) return 1;
if (p[i].y > q.y != p[j].y > q.y &&
q.x < (long double)(p[j].x - p[i].x) * (q.y - p[i].y) /
(long double)(p[j].y - p[i].y) +
p[i].x)
cnt++;
}
return cnt & 1;
}
bool PointOnPolygon(const vector<PT>& p, PT q) {
for (int i = 0; i < p.size(); i++)
if (dist2(ProjectPointSegment(p[i], p[(i + 1) % p.size()], q), q) < 1e-12)
return true;
return false;
}
vector<PT> CircleLineIntersection(PT a, PT b, PT c, long double r) {
vector<PT> ret;
b = b - a;
a = a - c;
long double A = dot(b, b);
long double B = dot(a, b);
long double C = dot(a, a) - r * r;
long double D = B * B - A * C;
if (D < -1e-12) return ret;
ret.push_back(c + a + b * (-B + sqrt(D + 1e-12)) / A);
if (D > 1e-12) ret.push_back(c + a + b * (-B - sqrt(D)) / A);
return ret;
}
vector<PT> CircleCircleIntersection(PT a, PT b, long double r, long double R) {
vector<PT> ret;
long double d = sqrt(dist2(a, b));
if (d > r + R || d + min(r, R) < max(r, R)) return ret;
long double x = (d * d - R * R + r * r) / (2 * d);
long double y = sqrt(r * r - x * x);
PT v = (b - a) / d;
ret.push_back(a + v * x + RotateCCW90(v) * y);
if (y > 0) ret.push_back(a + v * x - RotateCCW90(v) * y);
return ret;
}
long double ComputeSignedArea(const vector<PT>& p) {
long double area = 0;
for (int i = 0; i < p.size(); i++) {
int j = (i + 1) % p.size();
area += p[i].x * p[j].y - p[j].x * p[i].y;
}
return area / 2.0;
}
long double ComputeArea(const vector<PT>& p) {
return fabs(ComputeSignedArea(p));
}
long double ShoeLace(vector<PT>& vec) {
int i, n;
long double ans = 0;
n = vec.size();
for (i = 0; i < n; i++)
ans += vec[i].x * vec[((i + 1) % n)].y - vec[i].y * vec[((i + 1) % n)].x;
return fabs(ans) * 0.5;
}
PT ComputeCentroid(const vector<PT>& p) {
PT c(0, 0);
long double scale = 6.0 * ComputeSignedArea(p);
for (int i = 0; i < p.size(); i++) {
int j = (i + 1) % p.size();
c = c + (p[i] + p[j]) * (p[i].x * p[j].y - p[j].x * p[i].y);
}
return c / scale;
}
long double PAngle(PT a, PT b, PT c) {
PT temp1(a.x - b.x, a.y - b.y), temp2(c.x - b.x, c.y - b.y);
long double ans = myAsin((temp1.x * temp2.y - temp1.y * temp2.x) /
(temp1.Magnitude() * temp2.Magnitude()));
if ((ans < 0 && (temp1.x * temp2.x + temp1.y * temp2.y) < 0) ||
(ans >= 0 && (temp1.x * temp2.x + temp1.y * temp2.y) < 0))
ans = PI - ans;
ans = ans < 0 ? 2 * PI + ans : ans;
return ans;
}
long double SignedArea(PT a, PT b, PT c) {
PT temp1(b.x - a.x, b.y - a.y), temp2(c.x - a.x, c.y - a.y);
return 0.5 * (temp1.x * temp2.y - temp1.y * temp2.x);
}
bool XYasscending(PT a, PT b) {
if (abs(a.x - b.x) < 1e-12) return a.y < b.y;
return a.x < b.x;
}
void MakeConvexHull(vector<PT> given, vector<PT>& ans) {
int i, n = given.size(), j = 0, k = 0;
vector<PT> U, L;
ans.clear();
sort(given.begin(), given.end(), XYasscending);
for (i = 0; i < n; i++) {
while (true) {
if (j < 2) break;
if (SignedArea(L[j - 2], L[j - 1], given[i]) <= 1e-12)
j--;
else
break;
}
if (j == L.size()) {
L.push_back(given[i]);
j++;
} else {
L[j] = given[i];
j++;
}
}
for (i = n - 1; i >= 0; i--) {
while (1) {
if (k < 2) break;
if (SignedArea(U[k - 2], U[k - 1], given[i]) <= 1e-12)
k--;
else
break;
}
if (k == U.size()) {
U.push_back(given[i]);
k++;
} else {
U[k] = given[i];
k++;
}
}
for (i = 0; i < j - 1; i++) ans.push_back(L[i]);
for (i = 0; i < k - 1; i++) ans.push_back(U[i]);
}
struct DirLine {
PT p;
PT v;
long double ang;
DirLine() {}
DirLine(PT p, PT q) {
this->p = p;
this->v.x = q.x - p.x;
this->v.y = q.y - p.y;
ang = atan2(v.y, v.x);
}
bool operator<(const DirLine& u) const { return ang < u.ang; }
};
bool getIntersection(PT p, PT v, PT q, PT w, PT& o) {
if (dcmp(cross(v, w)) == 0) return false;
PT u = p - q;
long double k = cross(w, u) / cross(v, w);
o = p + v * k;
return true;
}
bool onLeft(DirLine l, PT p) { return dcmp(l.v * (p - l.p)) >= 0; }
int halfPlaneIntersection(DirLine* li, int n, vector<PT>& poly) {
sort(li, li + n);
int first, last;
PT* p = new PT[n];
DirLine* q = new DirLine[n];
q[first = last = 0] = li[0];
for (int i = 1; i < n; i++) {
while (first < last && !onLeft(li[i], p[last - 1])) last--;
while (first < last && !onLeft(li[i], p[first])) first++;
q[++last] = li[i];
if (dcmp(q[last].v * q[last - 1].v) == 0) {
last--;
if (onLeft(q[last], li[i].p)) q[last] = li[i];
}
if (first < last)
getIntersection(q[last - 1].p, q[last - 1].v, q[last].p, q[last].v,
p[last - 1]);
}
while (first < last && !onLeft(q[first], p[last - 1])) last--;
if (last - first <= 1) {
delete[] p;
delete[] q;
return 0;
}
getIntersection(q[last].p, q[last].v, q[first].p, q[first].v, p[last]);
poly.resize(last - first + 1);
int m = 0;
for (int i = first; i <= last; i++) poly[m++] = p[i];
delete[] p;
delete[] q;
return m;
}
long double CirclishArea(PT a, PT b, PT cen, long double r) {
long double ang =
fabs(atan2((a - cen).y, (a - cen).x) - atan2((b - cen).y, (b - cen).x));
if (ang > PI) ang = 2 * PI - ang;
return (ang * r * r) / 2.0;
}
long double CicleTriangleIntersectionArea(PT a, PT b, PT c,
long double radius) {
vector<PT> g = CircleLineIntersection(a, b, c, radius);
if (b < a) swap(a, b);
if (g.size() < 2)
return CirclishArea(a, b, c, radius);
else {
PT l = g[0], r = g[1];
if (r < l) swap(l, r);
if (b < l || r < a)
return CirclishArea(a, b, c, radius);
else if (a < l && b < r)
return fabs(SignedArea(c, b, l)) + CirclishArea(a, l, c, radius);
else if (r < b && l < a)
return fabs(SignedArea(a, c, r)) + CirclishArea(r, b, c, radius);
else if (a < l && r < b)
return fabs(SignedArea(c, l, r)) + CirclishArea(a, l, c, radius) +
CirclishArea(b, r, c, radius);
else
return fabs(SignedArea(a, b, c));
}
return 0;
}
long double CirclePolygonIntersectionArea(vector<PT>& p, PT c, long double r) {
int i, j, k, n = p.size();
long double sum = 0;
for (i = 0; i < p.size(); i++) {
long double temp =
CicleTriangleIntersectionArea(p[i], p[(i + 1) % n], c, r);
long double sign = SignedArea(c, p[i], p[(i + 1) % n]);
if (dcmp(sign) == 1)
sum += temp;
else if (dcmp(sign) == -1)
sum -= temp;
}
return sum;
}
vector<PT> CutPolygon(vector<PT>& u, PT a, PT b) {
vector<PT> ret;
int n = u.size();
for (int i = 0; i < n; i++) {
PT c = u[i], d = u[(i + 1) % n];
if (dcmp((b - a) * (c - a)) >= 0) ret.push_back(c);
if (ProjectPointLine(a, b, c) == c || ProjectPointLine(a, b, d) == d)
continue;
if (dcmp((b - a) * (d - c)) != 0) {
PT t;
getIntersection(a, b - a, c, d - c, t);
if (PointOnSegment(c, d, t)) ret.push_back(t);
}
}
return ret;
}
vector<PT> tanCP(PT c, long double r, PT p) {
long double x = dot(p - c, p - c);
long double d = x - r * r;
vector<PT> res;
if (d < -1e-12) return res;
if (d < 0) d = 0;
PT q1 = (p - c) * (r * r / x);
PT q2 = RotateCCW90((p - c) * (-r * sqrt(d) / x));
res.push_back(c + q1 - q2);
res.push_back(c + q1 + q2);
return res;
}
vector<pair<PT, PT> > tanCC(PT c1, long double r1, PT c2, long double r2) {
vector<pair<PT, PT> > res;
if (fabs(r1 - r2) < 1e-12) {
PT dir = c2 - c1;
dir = RotateCCW90(dir * (r1 / dir.Magnitude()));
res.push_back(pair<PT, PT>(c1 + dir, c2 + dir));
res.push_back(pair<PT, PT>(c1 - dir, c2 - dir));
} else {
PT p = ((c1 * -r2) + (c2 * r1)) / (r1 - r2);
vector<PT> ps = tanCP(c1, r1, p), qs = tanCP(c2, r2, p);
for (int i = 0; i < ps.size() && i < qs.size(); ++i) {
res.push_back(pair<PT, PT>(ps[i], qs[i]));
}
}
PT p = ((c1 * r2) + (c2 * r1)) / (r1 + r2);
vector<PT> ps = tanCP(c1, r1, p), qs = tanCP(c2, r2, p);
for (int i = 0; i < ps.size() && i < qs.size(); ++i) {
res.push_back(pair<PT, PT>(ps[i], qs[i]));
}
return res;
}
pair<PT, PT> MoveSegmentLeft(PT a, PT b, long double d) {
long double l = dist(a, b);
PT p = ((RotateCCW90(b - a) * d) / l) + a;
return make_pair(p, p + b - a);
}
void GetLineABC(PT A, PT B, long double& a, long double& b, long double& c) {
a = A.y - B.y;
b = B.x - A.x;
c = A.x * B.y - A.y * B.x;
}
long double Sector(long double r, long double alpha) {
return r * r * 0.5 * (alpha - sin(alpha));
}
long double CircleCircleIntersectionArea(long double r1, long double r2,
long double d) {
if (dcmp(d - r1 - r2) != -1) return 0;
if (dcmp(d + r1 - r2) != 1) return PI * r1 * r1;
if (dcmp(d + r2 - r1) != 1) return PI * r2 * r2;
long double ans =
Sector(r1, 2 * acos((r1 * r1 + d * d - r2 * r2) / (2.0 * r1 * d)));
ans += Sector(r2, 2 * acos((r2 * r2 + d * d - r1 * r1) / (2.0 * r2 * d)));
return ans;
}
long double PolygonStubbing(vector<PT>& p, PT s, PT t) {
int n = p.size();
long double sm = 0;
for (int i = 0; i < n; i++) sm += p[i] * p[(i + 1) % n];
if (dcmp(sm) == -1) reverse(p.begin(), p.end());
vector<pair<long double, int> > event;
for (int i = 0; i < n; i++) {
int lef = dcmp(cross(p[i] - s, t - s));
int rig = dcmp(cross(p[((i + 1) % n)] - s, t - s));
if (lef == rig) continue;
long double r = cross(p[((i + 1) % n)] - s, p[((i + 1) % n)] - p[i]) /
cross(t - s, p[((i + 1) % n)] - p[i]);
if (lef > rig)
event.push_back(make_pair(r, (!lef || !rig ? -1 : -2)));
else
event.push_back(make_pair(r, (!lef || !rig ? 1 : 2)));
}
sort(event.begin(), event.end());
int cnt = 0;
long double sum = 0, la = 0;
for (int i = 0; i < (int)event.size(); i++) {
if (cnt > 0) sum += event[i].first - la;
la = event[i].first;
cnt += event[i].second;
}
return sum * (t - s).Magnitude();
}
bool IsInCircle(pair<PT, long double> C, PT p) {
return dcmp(C.second - dist(C.first, p)) >= 0;
}
pair<PT, long double> MinimumEnclosingCircle2(vector<PT>& p, int m, int n) {
pair<PT, long double> D =
make_pair((p[m] + p[n]) / 2.0, dist(p[m], p[n]) / 2.0);
for (int i = 0; i < m; i++)
if (!IsInCircle(D, p[i])) {
D.first = ComputeCircleCenter(p[i], p[m], p[n]);
D.second = dist(D.first, p[i]);
}
return D;
}
pair<PT, long double> MinimumEnclosingCircle1(vector<PT>& p, int n) {
pair<PT, long double> D =
make_pair((p[0] + p[n]) / 2.0, dist(p[0], p[n]) / 2.0);
for (int i = 1; i < n; i++)
if (!IsInCircle(D, p[i])) {
D = MinimumEnclosingCircle2(p, i, n);
}
return D;
}
pair<PT, long double> MinimumEnclosingCircle(vector<PT> p) {
srand(time(NULL));
sort(p.begin(), p.end());
p.resize(distance(p.begin(), unique(p.begin(), p.end())));
random_shuffle(p.begin(), p.end());
if (p.size() == 1) return make_pair(p[0], 0);
pair<PT, long double> D =
make_pair((p[0] + p[1]) / 2.0, dist(p[0], p[1]) / 2.0);
for (int i = 2; i < p.size(); i++)
if (!IsInCircle(D, p[i])) {
D = MinimumEnclosingCircle1(p, i);
}
return D;
}
PT polygonCenter;
bool less_comp(PT a, PT b) {
if (dcmp(a.x - polygonCenter.x) != -1 && dcmp(b.x - polygonCenter.x) == -1)
return true;
if (dcmp(a.x - polygonCenter.x) == -1 && dcmp(b.x - polygonCenter.x) != -1)
return false;
if (dcmp(a.x - polygonCenter.x) == 0 && dcmp(b.x - polygonCenter.x) == 0) {
if (dcmp(a.y - polygonCenter.y) != -1 || dcmp(b.y - polygonCenter.y) != -1)
return a.y > b.y;
return b.y > a.y;
}
long double det = (a.x - polygonCenter.x) * (b.y - polygonCenter.y) -
(b.x - polygonCenter.x) * (a.y - polygonCenter.y);
if (dcmp(det) == -1) return true;
if (dcmp(det) == 1) return false;
long double d1 = (a.x - polygonCenter.x) * (a.x - polygonCenter.x) +
(a.y - polygonCenter.y) * (a.y - polygonCenter.y);
long double d2 = (b.x - polygonCenter.x) * (b.x - polygonCenter.x) +
(b.y - polygonCenter.y) * (b.y - polygonCenter.y);
return (d1 - d2) == 1;
}
void sortCW(vector<PT>& given) {
if ((int)given.size() < 3) return;
polygonCenter = MinimumEnclosingCircle(given).first;
sort(given.begin(), given.end(), less_comp);
}
void sortCCW(vector<PT>& given) {
sortCW(given);
reverse(given.begin(), given.end());
}
int PolygonPolygonIntersection(vector<PT> p1, vector<PT> p2, vector<PT>& poly) {
DirLine d[(int)p1.size() + (int)p2.size()];
for (int i = 0; i < (int)p1.size(); i++) {
d[i] = DirLine(p1[i], p1[(i + 1) % (int)p1.size()]);
}
for (int i = 0; i < (int)p2.size(); i++) {
d[i + (int)p1.size()] = DirLine(p2[i], p2[(i + 1) % (int)p2.size()]);
}
int n = halfPlaneIntersection(d, (int)p1.size() + (int)p2.size(), poly);
sortCCW(poly);
return n;
}
int pointAndConvexPolygonInLogn(const vector<PT>& poly, PT p) {
int n = (int)poly.size();
if (poly[0] == p)
return 0;
else if (onLeft(DirLine(poly[0], poly[1]), p) &&
onLeft(DirLine(poly[n - 1], poly[0]), p))
return 1;
int low = 1, high = n - 1;
while (low < high) {
int mid = (low + high + 1) / 2;
DirLine d(poly[0], poly[mid]);
if (onLeft(d, p) || dcmp(DistancePointLine(poly[0], poly[mid], p)) == 0) {
high = mid - 1;
} else {
low = mid;
}
}
if (low == 1) {
if (PointOnSegment(poly[0], poly[1], p))
return 0;
else if (onLeft(DirLine(poly[0], poly[1]), p))
return 1;
}
if (low == n - 1) {
return 1;
} else if (low == n - 2) {
if (PointOnSegment(poly[n - 2], poly[n - 1], p) ||
PointOnSegment(poly[0], poly[n - 1], p))
return 0;
else if (onLeft(DirLine(poly[n - 1], poly[n - 2]), p))
return -1;
else
return 1;
} else {
if (PointOnSegment(poly[low], poly[low + 1], p))
return 0;
else if (onLeft(DirLine(poly[low + 1], poly[low]), p))
return -1;
else
return 1;
}
}
const int M = 200010;
int main() {
IO();
long double x, y, r;
long double a, b, c;
cin >> x >> y >> r;
cin >> a >> b >> c;
cout << setprecision(20) << fixed
<< CircleCircleIntersectionArea(r, c, dist((PT){x, y}, (PT){a, b}))
<< endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a = 0, b;
cin >> n;
while (n > 0) {
if (n % 8 == 1) {
a++;
}
n /= 8;
}
cout << a;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long power(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret *= a;
a *= a;
if (ret >= MOD) ret %= MOD;
if (a >= MOD) a %= MOD;
b >>= 1;
}
return ret;
}
long long invmod(long long x) { return power(x, MOD - 2); }
int main() {
double n, i, a;
cin >> n >> a;
double o = 360.0 / n;
double mn = 1000000000;
long long x;
for (i = 2; i <= n - 1; i++) {
if (mn > abs(a - (180 - ((i)*o) / 2.0)))
x = i, mn = abs(a - (180 - ((i)*o) / 2.0));
}
cout << 1 << " " << x << " " << x + 1;
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
int main(){
int t;
scanf("%d",&t);
while(t--){
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
if(a+b+c<7){
printf("NO\n");
continue;
}
ll m = min(a,min(b,c));
ll s = a+b+c;
if(s%9==0&&s/9<=m){
printf("YES\n");
continue;
}
else{
printf("NO\n");
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2010;
const int maxm = 5e5 + 10;
int n, m, a[maxm], b[maxm];
bitset<maxn> g[maxn], C[maxn];
int main() {
int i, j;
scanf("%d%d", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%d%d", &a[i], &b[i]);
g[a[i]].set(b[i]);
}
for (i = 1; i <= n; i++) C[i].set(i);
for (i = 1; i <= n; i++) {
if (!g[i][i])
for (j = i + 1; j <= n; j++)
if (g[j][i]) swap(g[j], g[i]), swap(C[j], C[i]);
for (j = 1; j <= n; j++)
if (i != j && g[j][i]) g[j] ^= g[i], C[j] ^= C[i];
}
for (i = 1; i <= m; i++) puts(C[b[i]][a[i]] ? "NO" : "YES");
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int MN = 200005, MM = 3005, mod = 998244353;
int n, m, a[MN], w[MN], dp[MM];
int inv(int u) { return u == 1 ? 1 : LL(mod - mod / u) * inv(mod % u) % mod; }
int main() {
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin >> n >> m;
int S = 0, T = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> w[i];
if (a[i] == 1)
T += w[i];
else
S += w[i];
}
dp[0] = 1;
for (int i = 0; i < m; i++) {
int tmp = S + T - i;
for (int j = i; j >= 0 && S - i + j >= 0; j--) {
int invsum = inv(j * 2 + tmp);
dp[j + 1] = (dp[j + 1] + LL(T + j) * invsum % mod * dp[j]) % mod;
dp[j] = LL(S - i + j) * invsum % mod * dp[j] % mod;
}
}
int inc = dp[0], dec = dp[m];
for (int i = 1, tmp = 1; i <= m; i++) {
tmp = LL(1 + inv(T + i - 1)) * tmp % mod;
inc = (inc + (LL)tmp * dp[i]) % mod;
}
for (int i = 1, tmp = 1; i <= m && i <= S; i++) {
tmp = LL(1 - inv(S - i + 1) + mod) * tmp % mod;
dec = (dec + (LL)tmp * dp[m - i]) % mod;
}
for (int i = 1; i <= n; i++)
cout << w[i] * LL(a[i] == 1 ? inc : dec) % mod << "\n";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int d[65005];
int main() {
int n;
cin >> n;
string str;
cin >> str;
int answer = 0;
for (int i = 1; i <= n; i++) {
if ((str[i - 1] - '0') % 2 == 0) {
answer += i;
}
}
cout << answer;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(0.0) * 2.0;
const long double eps = 1e-10;
const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1},
{-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
template <class T>
inline T abs1(T a) {
return a < 0 ? -a : a;
}
template <typename t, typename t1>
t min1(t a, t1 b) {
return a < b ? a : b;
}
template <typename t, typename... arg>
t min1(t a, arg... arr) {
return min1(a, min1(arr...));
}
template <typename t, typename t1>
t max1(t a, t1 b) {
return a > b ? a : b;
}
template <typename t, typename... arg>
t max1(t a, arg... arr) {
return max1(a, max1(arr...));
}
inline int jud(double a, double b) {
if (abs(a) < eps && abs(b) < eps)
return 0;
else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
return 0;
if (a < b) return -1;
return 1;
}
template <typename t>
inline int jud(t a, t b) {
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
if (na == 0) return 0;
int be = 0, en = na - 1;
if (*a <= *(a + na - 1)) {
if (f_lb == 0)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != 1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != -1)
en = mid;
else
be = mid + 1;
}
if (f_small && jud(*(a + be), val) == 1) be--;
if (!f_small && jud(*(a + be), val) == -1) be++;
} else {
if (f_lb)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != -1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != 1)
en = mid;
else
be = mid + 1;
}
if (!f_small && jud(*(a + be), val) == -1) be--;
if (f_small && jud(*(a + be), val) == 1) be++;
}
return be;
}
template <class T>
inline T lowb(T num) {
return num & (-num);
}
inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(unsigned long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(unsigned int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(unsigned long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
inline int bitmaxl(long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
long long pow(long long n, long long m, long long mod = 0) {
if (m < 0) return 0;
long long ans = 1;
long long k = n;
while (m) {
if (m & 1) {
ans *= k;
if (mod) ans %= mod;
}
k *= k;
if (mod) k %= mod;
m >>= 1;
}
return ans;
}
template <class t1, class t2>
inline void add(t1& a, t2 b, int mod = -1) {
if (mod == -1) mod = 1000000007;
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
}
template <class t>
void output1(t arr) {
for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
cerr << endl;
}
template <class t>
void output2(t arr) {
for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
}
const int maxn = 1010;
char ans[maxn][maxn];
int n;
struct Range {
int from, to;
bool operator<(const Range& other) const {
if (to - from == other.to - other.from) return to < other.to;
return to - from > other.to - other.from;
}
bool operator==(const Range& other) const {
return to == other.to && from == other.from;
}
};
set<Range> s;
int main() {
ios_base::sync_with_stdio(0);
scanf("%d", &n);
s.insert({0, n + 1});
for (int i = 0; i < n; i++) {
int num;
scanf("%d", &num);
int empty = 1 + n - num;
int have = num;
auto add = [&](int j) {
if (empty) {
empty--;
ans[j][i] = '0';
} else {
have--;
ans[j][i] = '1';
};
};
if (s.size()) {
auto r = *s.begin();
s.erase(s.begin());
int rempty = empty;
for (int j = r.from; j < r.to - 1; j++) {
add(j);
}
have--;
ans[r.to - 1][i] = '1';
Range a = {r.from, min(r.from + rempty, r.to - 1)};
Range b = {a.to, r.to};
if (a.to - a.from > 1) s.insert(a);
if (b.to - b.from > 1) s.insert(b);
for (int j = 0; j < r.from; j++) {
add(j);
}
for (int j = r.to; j < n + 1; j++) {
add(j);
}
} else {
for (int j = 0; j < n + 1; j++) {
add(j);
}
}
assert(empty == 0 && have == 0);
}
assert(s.empty());
int cant = -1;
for (int i = 0; i < n + 1; i++) {
bool have = 0;
for (int j = 0; j < n; j++)
if (ans[i][j] == '1') {
have = 1;
}
if (!have) {
cant = i;
}
}
printf("%d\n", n + (cant == -1));
for (int i = 0; i < n + 1; i++)
if (cant != i) {
puts(ans[i]);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long n, x, y, nn;
void egcd(long long a, long long b, long long &i, long long &j, long long &d) {
if (b == 0)
d = a, i = 1, j = 0;
else {
egcd(b, a % b, j, i, d);
j -= a / b * i;
}
}
int main() {
scanf("%lld", &n);
nn = n;
for (long long i = 2; i * i <= n; i++)
if (n % i == 0) {
if (x == 0)
x = i;
else if (y == 0) {
y = i;
break;
}
while (n % i == 0) n /= i;
}
if (n != 1 && x && y == 0) y = n;
if (y == 0) {
puts("NO");
return 0;
}
long long i, j, d;
egcd(x, y, i, j, d);
n = nn;
if (i < 0 && n / y > j) {
puts("YES");
puts("2");
printf("%lld %lld\n", -i, n / x);
printf("%lld %lld\n", n / y - j, n / y);
} else if (j < 0 && n / x > i) {
puts("YES");
puts("2");
printf("%lld %lld\n", n / x - i, n / x);
printf("%lld %lld\n", -j, n / y);
} else
puts("NO");
return 0;
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.