solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
bool comp[1001], mark[1001];
char in[1001], out[1001];
int vet[26], primes[1001], pc, len;
void sieve() {
int lim = sqrt(1001) + 1;
for (int i = 2; i <= lim; ++i) {
if (comp[i]) continue;
for (int j = i * i; j < 1001; j += i) comp[j] = true;
}
for (int i = 2; i < 1001; ++i)
if (!comp[i]) primes[pc++] = i;
}
int main() {
scanf("%s", in);
sieve();
for (int i = 0; in[i]; ++i, ++len) vet[in[i] - 'a']++;
if (len == 1) {
printf("YES\n");
printf("%s\n", in);
} else {
int fp;
for (int i = 1; i < pc; ++i) {
if (primes[i] * 2 - 1 >= len) {
fp = i - 1;
break;
}
}
int max = 0;
char c;
for (int i = 0; i < 26; ++i) {
if (vet[i] > max) {
c = i;
max = vet[i];
}
}
int nc = 0;
for (int i = 0; i <= fp; ++i) {
for (int j = 1; j * primes[i] - 1 < len; ++j) {
if (!mark[j * primes[i] - 1]) {
mark[j * primes[i] - 1] = 1;
out[j * primes[i] - 1] = c + 'a';
vet[c]--;
++nc;
}
}
}
if (max >= nc) {
printf("YES\n");
char d = 0;
for (int i = 0; i < len; ++i) {
if (mark[i]) continue;
while (!vet[d]) ++d;
if (vet[d]) {
vet[d]--;
out[i] = d + 'a';
}
}
out[len] = '\0';
printf("%s\n", out);
} else {
printf("NO\n");
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int r, c, rS, cS, rE, cE, rez;
char s[1005][1005];
int m[1005][1005], f[1005][1005];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
queue<int> qer, qec;
int main() {
int i, j, rr, cc, nr, nc;
scanf("%d %d\n", &r, &c);
for (i = 0; i < r; ++i) scanf("%s", s[i]);
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
if (s[i][j] == 'S') {
rS = i;
cS = j;
}
if (s[i][j] == 'E') {
rE = i;
cE = j;
}
}
m[rE][cE] = 0;
f[rE][cE] = 1;
qer.push(rE);
qec.push(cE);
while (!qer.empty()) {
rr = qer.front();
cc = qec.front();
qer.pop();
qec.pop();
for (i = 0; i < 4; ++i) {
nr = rr + dx[i];
nc = cc + dy[i];
if (nr < 0 || nr >= r || nc < 0 || nc >= c) continue;
if (s[nr][nc] == 'T') continue;
if (f[nr][nc]) continue;
f[nr][nc] = 1;
m[nr][nc] = m[rr][cc] + 1;
qer.push(nr);
qec.push(nc);
}
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
if (s[i][j] >= '1' && s[i][j] <= '9' && f[i][j] && m[i][j] <= m[rS][cS])
rez += s[i][j] - '0';
printf("%d", rez);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
const double eps = 1e-9;
string t;
char c[N];
double f[N][2];
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * f;
}
int main() {
scanf("%s", c + 1);
int n = strlen(c + 1);
for (int i = 1; i <= n; i++) {
t += c[i];
if (c[i] != 'X' && i != n && c[i] == c[i + 1]) t += 'X';
}
if (c[1] != 'X' && c[1] == c[n]) {
if (c[1] == 'L')
t += 'X';
else
t = "X" + t;
}
int len = t.size();
double l = 0, r = 1;
while (r - l > eps) {
double mid = (l + r) / 2;
f[0][0] = -0;
f[0][1] = -mid;
for (int i = 0; i < len; i++) {
f[i + 1][0] = f[i][1] - mid;
f[i + 1][1] = f[i][0] - mid;
if (t[i] != 'X') {
if (t[i] == 'L')
f[i + 1][1] = max(f[i + 1][1], f[i][0] + 1 - mid);
else
f[i + 1][0] = max(f[i + 1][0], f[i][1] + 1 - mid);
}
f[i + 1][1] = max(f[i + 1][1], f[i + 1][0] - mid);
f[i + 1][0] = max(f[i + 1][0], f[i + 1][1] - mid);
}
if (f[len][0] > 0)
l = mid;
else
r = mid;
}
int x = (l + eps) * 1e8;
printf("%d.%06d", x / 1000000, x % 1000000);
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, x, c = 0;
char v;
int s[26] = {0};
cin >> n;
if (n > 26) {
cout << "-1" << endl;
return 0;
} else {
for (i = 0; i < n; i++) {
cin >> v;
x = v - 97;
s[x]++;
}
for (j = 0; j < 26; j++) {
if (s[j] > 1) {
c += (s[j] - 1);
}
}
}
cout << c << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, q;
vector<int> adj[maxn];
int par[maxn][20];
int sum[maxn][20];
vector<int> roots;
bool mark[maxn];
int starttime[maxn], finishtime[maxn], h[maxn];
int timer;
void dfs(int v) {
mark[v] = 1;
starttime[v] = timer++;
for (int i = 1; i < 20; i++) {
par[v][i] = par[par[v][i - 1]][i - 1];
sum[v][i] = sum[v][i - 1] + sum[par[v][i - 1]][i - 1];
}
for (auto u : adj[v]) {
if (!mark[u]) {
h[u] = h[v] + 1;
dfs(u);
}
}
finishtime[v] = timer++;
}
bool isAncestor(int u, int v) {
return starttime[u] <= starttime[v] && finishtime[u] >= finishtime[v];
}
int LCA(int u, int v) {
if (isAncestor(u, v)) return u;
if (isAncestor(v, u)) return v;
for (int i = 19; i >= 0; i--)
if (!isAncestor(par[u][i], v)) u = par[u][i];
return par[u][0];
}
bool isSpecialCase(int v, int u) {
if (!isAncestor(u, v)) return false;
if (sum[v][19] - sum[u][19] == 0) return true;
return false;
}
bool isPartOf(int v, int u) {
if (!isAncestor(u, v)) return false;
if (sum[v][19] - sum[u][19] == h[v] - h[u]) return true;
return false;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
if (a == -1) {
roots.push_back(i);
par[i][0] = i;
sum[i][0] = 0;
} else {
adj[i].push_back(a);
adj[a].push_back(i);
par[i][0] = a;
sum[i][0] = b;
}
}
for (int i = 0; i < roots.size(); i++) {
h[roots[i]] = 0;
dfs(roots[i]);
}
cin >> q;
while (q--) {
int type;
cin >> type;
int u, v;
cin >> u >> v;
if (type == 1) {
if (u == v)
cout << "NO" << endl;
else if (isSpecialCase(v, u))
cout << "YES" << endl;
else
cout << "NO" << endl;
} else {
int w = LCA(u, v);
if (u == v || !isAncestor(w, u) || !isAncestor(w, v) || isAncestor(v, u))
cout << "NO" << endl;
else if (isPartOf(v, w) && isSpecialCase(u, w))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, a[300000][2], fa[300000], sz[300000], top[300000], d[300000],
prf[300000], f1[300000], q1[300000], q2[300000];
vector<int> g[300000], b[300000];
map<int, map<int, int> > f2[300000];
long long ans;
void dfs1(int u) {
d[u] = d[fa[u]] + 1;
sz[u] = 1;
for (int v : g[u])
if (v != fa[u]) {
fa[v] = u;
dfs1(v);
sz[u] += sz[v];
}
}
void dfs2(int u) {
if (!top[u]) top[u] = u;
b[top[u]].push_back(u);
int t = 0;
for (int v : g[u])
if (v != fa[u] && sz[v] > sz[t]) t = v;
if (!t) return;
prf[u] = t;
top[t] = top[u];
dfs2(t);
for (int v : g[u])
if (v != fa[u] && v != t) dfs2(v);
}
int lca(int x, int y) {
for (; top[x] != top[y]; x = fa[top[x]])
if (d[top[x]] < d[top[y]]) swap(x, y);
return d[x] < d[y] ? x : y;
}
int go(int x, int k) {
int len = d[x] - d[top[x]];
if (len < k) return go(fa[top[x]], k - len - 1);
return b[top[x]][len - k];
}
int add1(int x, int y) {
int len = d[x] - d[y];
if (len < k) return x;
int z = go(x, len - k + 1);
++f1[x];
--f1[z];
return z;
}
void add2(int u1, int u2, int v1, int v2) {
if (top[u1] > top[v1]) swap(u1, v1), swap(u2, v2);
if (d[u1] > d[u2]) swap(u1, u2);
++f2[top[u1]][top[v1]][d[u1]];
--f2[top[u1]][top[v1]][d[u2] + 1];
}
void add(int x, int y) {
int z = lca(x, y);
x = add1(x, z);
y = add1(y, z);
int u = x, v = y;
int n1 = 0, n2 = 0;
for (; top[u] != top[z]; u = fa[top[u]]) q1[++n1] = u;
for (; top[v] != top[z]; v = fa[top[v]]) q2[++n2] = v;
if (u != z) q1[++n1] = u;
if (v != z) q2[++n2] = v;
for (int i1 = 1, i2 = n2; i1 <= n1; ++i1) {
int u1 = q1[i1], u2 = top[u1];
if (d[u2] <= d[z]) u2 = prf[z];
for (; i2; --i2) {
int v1 = q2[i2], v2 = top[v1];
if (d[v2] <= d[z]) v2 = prf[z];
if (d[u1] + d[v1] - d[z] * 2 < k) continue;
int len = d[u1] + d[v1] - d[z] * 2;
int w1 = go(v1, len - k), w2 = 0;
len = d[u2] + d[v1] - d[z] * 2;
if (len >= k) {
w2 = go(v1, len - k);
add2(u1, u2, w1, w2);
break;
}
len = d[u1] + d[v1] - d[z] * 2;
w2 = go(u1, len - k);
add2(u1, w2, w1, v1);
u1 = fa[w2];
}
}
}
long long C(long long x) { return x * (x - 1) / 2; }
void dfs3(int u, int t) {
for (int v : g[u])
if (v != fa[u]) {
dfs3(v, t);
f1[u] += f1[v];
}
ans += C(f1[u]) * t;
}
void calc(int t) {
for (int i = 1; i <= n; ++i) {
for (auto j : f2[i]) {
long long sum = 0, lst = 0;
for (auto k : j.second) {
ans += (k.first - lst) * C(sum) * t;
lst = k.first;
sum += k.second;
}
}
f2[i].clear();
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i < n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs1(1);
dfs2(1);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", a[i] + 0, a[i] + 1);
add(a[i][0], a[i][1]);
}
dfs3(1, 1);
calc(1);
memset(f1, 0, sizeof f1);
++k;
for (int i = 1; i <= m; ++i) add(a[i][0], a[i][1]);
dfs3(1, -1);
calc(-1);
printf("%lld\n", ans);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 14, lg = 18;
int n, l_tbl[lg][maxn * 2], r_tbl[lg][maxn * 2], ans[maxn];
template <class comp>
int query(int *tbl, int l, int r) {
int ans = max(INT_MIN, INT_MAX, comp());
for (l += 2 * n, r += 2 * n; l < r; l >>= 1, r >>= 1) {
if (l % 2) ans = min(ans, tbl[l++], comp());
if (r % 2) ans = min(ans, tbl[--r], comp());
}
return ans;
}
void make_tbl(int k) {
for (int i = 2 * n - 1; i > 0; --i) {
l_tbl[k][i] = min(l_tbl[k][i * 2], l_tbl[k][i * 2 + 1]);
r_tbl[k][i] = max(r_tbl[k][i * 2], r_tbl[k][i * 2 + 1]);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
int re;
cin >> re;
l_tbl[0][2 * n + i] = max(0, i - re);
r_tbl[0][2 * n + i] = i + re + 1;
l_tbl[0][3 * n + i] = i + n - re;
r_tbl[0][3 * n + i] = min(2 * n, i + n + re + 1);
}
make_tbl(0);
for (int k = 0; k + 1 < lg; ++k) {
for (int i = 0; i < 2 * n; ++i) {
l_tbl[k + 1][2 * n + i] =
query<less<int>>(l_tbl[k], l_tbl[k][2 * n + i], r_tbl[k][2 * n + i]);
r_tbl[k + 1][2 * n + i] = query<greater<int>>(
r_tbl[k], l_tbl[k][2 * n + i], r_tbl[k][2 * n + i]);
}
make_tbl(k + 1);
}
for (int i = 0; i < 2 * n; ++i) {
int l = i, r = i + 1;
for (int j = lg - 1; j >= 0; --j) {
int nxl = query<less<int>>(l_tbl[j], l, r),
nxr = query<greater<int>>(r_tbl[j], l, r);
if (nxr - nxl < n) {
ans[i] += 1 << j;
l = nxl;
r = nxr;
}
}
}
for (int i = 0; i < n; ++i) {
cout << (n == 1 ? 0 : min(ans[i], ans[i + n]) + 1) << ' ';
}
cout << '\n';
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int x;
int y;
} p[8];
bool cmp(node a, node b) {
if (a.x < b.x) return true;
if (a.x == b.x && a.y < b.y) return true;
return false;
}
int main() {
for (int i = 0; i < 8; i++) scanf("%d%d", &p[i].x, &p[i].y);
sort(p, p + 8, cmp);
if (p[0].x == p[1].x && p[1].x == p[2].x && p[3].x == p[4].x &&
p[5].x == p[6].x && p[6].x == p[7].x && p[0].y == p[3].y &&
p[3].y == p[5].y && p[1].y == p[6].y && p[2].y == p[4].y &&
p[4].y == p[7].y && p[0].x != p[3].x && p[3].x != p[5].x &&
p[0].y != p[1].y && p[1].y != p[2].y)
printf("respectable\n");
else
printf("ugly\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void RD(int &x) { scanf("%d", &x); }
void RD(long long &x) { scanf("%I64d", &x); }
void RD(double &x) { scanf("%lf", &x); }
void RD(int &x, int &y) { scanf("%d%d", &x, &y); }
void RD(long long &x, long long &y) { scanf("%I64d%I64d", &x, &y); }
void RD(double &x, double &y) { scanf("%lf%lf", &x, &y); }
void RD(char *s) { scanf("%s", s); }
void RD(char &s) { scanf("%c", &s); }
void RD(string &s) { cin >> s; }
void PR(int x) { printf("%d\n", x); }
void PR(int x, int y) { printf("%d %d\n", x, y); }
void PR(long long x) { printf("%I64d\n", x); }
void PR(char x) { printf("%c\n", x); }
void PR(char *x) { printf("%s\n", x); }
void PR(string x) { cout << x << endl; }
const long long inf = 1;
const long long mod = 1LL;
vector<int> v;
int main() {
int n, m, i, j, k, a, b;
while (scanf("%d", &n) == 1) {
(v.clear());
a = b = 0;
while (n--) {
RD(m);
for ((i) = 1; (i) <= (int)(m / 2); (i)++) RD(j), a += j;
if (m & 1) RD(j), v.push_back(j);
for ((i) = 1; (i) <= (int)(m / 2); (i)++) RD(j), b += j;
}
sort(v.rbegin(), v.rend());
k = (int)v.size();
for ((i) = 0; (i) < (int)(k); (i)++) {
if (i & 1)
b += v[i];
else
a += v[i];
}
printf("%d %d\n", a, b);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int a = (n - k) / 2;
for (int i = 0; i < n; i++) {
cout << ((i + 1) % (a + 1) == 0);
}
cout << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5 + 10;
bool win[mx], lose[mx];
int trie[mx][26];
int n, k, res = 1;
string s;
void insert() {
int node = 0;
for (auto c : s) {
if (!trie[node][c - 'a']) trie[node][c - 'a'] = res++;
node = trie[node][c - 'a'];
}
}
void search(int val) {
bool flag = 1;
for (int i = 0; i < 26; i++) {
if (trie[val][i]) {
search(trie[val][i]);
flag = 0;
if (!win[trie[val][i]]) win[val] = true;
if (!lose[trie[val][i]]) lose[val] = true;
}
}
lose[val] |= flag;
}
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> s;
insert();
}
search(0);
if (!win[0])
cout << "Second" << endl;
else if (lose[0] || k & 1)
cout << "First" << endl;
else
cout << "Second" << endl;
}
| 5 |
/*jai_ganeshdeva*/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pu push // adds the value to the last of the queue
#define lld long long int
#define ins insert /// used in set to insert the values
#define adv advance /// used to increment the iterator
#define mp make_pair
#define fi first
#define se second
#define all(c) c.begin(),c.end()
#define PI 3.1415926
#define INF (lld)1e16
#define vl vector<long long int >
#define vpll vector< pair<lld ,lld> >
#define vvl vector<vector<lld> >
#define pll pair <lld,lld>
lld p1=1e9 + 7,p2=998244353;
void judge(){
#ifndef ONLINE_JUDGE
// for getting input from input.
freopen("input.txt", "r", stdin);
// for writing output to output.txt
// freopen("output.txt", "w", stdout);
#endif
}
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
long long int modp(lld a,lld p,lld n)
{
lld d[100];
lld i,j,k,l;
if(n==0) return 1;
for(i=0;n>=1;i++)
{
d[i]=n%2;
n/=2;
}
l=i;
long long int e=1;
if(d[0]==1) e*=(a%p);
for(i=1;i<l;i++)
{
a*=a;
a=a%p;
if(d[i]==1)
{
e*=a;
e=e%p;
}
}
return e%p;
}
lld modInverse(lld n, lld p)
{
return modp(n, p, p-2);
}
// lld fac[500005];
// void getfac(lld p)
// {
// fac[0] = 1;
// for (lld i=1 ; i<=500005; i++)
// fac[i] = fac[i-1]*i%p;
// }
// long long int nCrmodp(lld n, lld r,lld p)
// {
// // Base case
// if (r==0)
// return 1;
// return (fac[n]* modInverse(fac[r], p) % p *
// modInverse(fac[n-r], p) % p) % p;
// }
lld l,r,mid,ans=0;
lld n,i,j,k,g,m;
lld x,y,n1,n2,h,z,c;
void solve2(){
cin>>n>>m;
vector<lld> v;
map<int,int> mp;
lld ans=0;
lld yoyo=-1;
for(i=0;i<n;i++)
{
cin>>l;
lld val=1;
for(j=2;j*j<=l;j++)
{
int y=0;
while(l%j==0)
{
l/=j;
y+=1;
}
if(y%2==1) val*=j;
if(l==1) break;
}
if(l) val*=l;
// cout<<"\nval == "<<val<<" ";
if(mp.count(val))
{
ans+=1;
mp.clear();
// cout<<i<<" ";
}
mp[val]=1;
}
if(!mp.empty())
{
ans+=1;
}
cout<<ans;
}
int main()
{
judge();
fastio();
lld t=1;
cin>>t;
// lld p1 = 1e9 + 7;
// getfac(p1);
for(int i=0;i<t;i++)
{
// cout<<"Case #"<<i+1<<": ";
// cout<<" ----------------\n";
solve2();
cout<<"\n";
}
cerr << "Time : " << ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
Node() {}
Node(int a, char b) {
to = a;
c = b;
}
int to;
char c;
};
vector<Node> v[100005];
int n, m;
int vis[100005];
vector<int> ans1, ans2;
vector<int> p[3];
bool dfs(int i, int k, char c) {
if (vis[i]) {
return vis[i] == k;
}
vis[i] = k;
p[k].push_back(i);
for (int j = 0; j < v[i].size(); j++) {
if (dfs(v[i][j].to, v[i][j].c == c ? k : k ^ 3, c) == false) return false;
}
return true;
}
int solve(char ch, vector<int> &ans) {
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) {
p[1].clear();
p[2].clear();
if (dfs(i, 1, ch) == false) {
return 0;
}
if (p[1].size() < p[2].size())
ans.insert(ans.end(), p[1].begin(), p[1].end());
else
ans.insert(ans.end(), p[2].begin(), p[2].end());
}
}
return 1;
}
void print(vector<int> &p) {
cout << p.size() << endl;
if (p.size()) printf("%d", p[0]);
for (int i = 1; i < p.size(); i++) printf(" %d", p[i]);
}
int main() {
while (cin >> n >> m) {
int a, b;
char c;
ans1.clear();
ans2.clear();
for (int i = 1; i <= n; i++) v[i].clear();
for (int i = 1; i <= m; i++) {
scanf("%d %d %c", &a, &b, &c);
v[a].push_back(Node(b, c));
v[b].push_back(Node(a, c));
}
int k1 = n, k2 = n;
if (solve('B', ans1)) k1 = ans1.size();
if (solve('R', ans2)) k2 = ans2.size();
if (k2 < k1) {
print(ans2);
} else if (k1 != n && k1 <= k2) {
print(ans1);
} else
cout << -1 << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long a[300009], ans, o[2];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
ans += a[i] / 2;
if (a[i] % 2) {
o[i % 2]++;
}
}
cout << ans + min(o[0], o[1]);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
if (n < 2 * k + 1) {
cout << "-1";
return 0;
}
long long ans = 0;
cout << n * k << '\n';
for (long long(i) = (0); (i) < (n); ++(i)) {
for (long long(j) = (1); (j) < (k + 1); ++(j)) {
cout << i + 1 << " " << (i + j) % n + 1 << '\n';
}
}
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
//#define ll int
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pu push
#define db double
#define m_p make_pair
ll mod = 1e9 + 7;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
t=1;
cin>>t;
ll t1=t;
while(t1--)
{
///your code goes here/////
ll n;
cin>>n;
ll arr[n];
for(ll i=0;i<n;i++)
cin>>arr[i];
ll prev=0;
cout<<prev<<" ";
for(ll i=1;i<n;i++)
{
ll cur=(prev^arr[i-1]);
ll per=arr[i];
bitset<30> a(cur);
bitset<30> b(per);
// cout<<a[0]<<" "<<a[1]<<" "<<a[29]<<" "<<a[28]<<" "<<a[27]<<endl;
ll num=0;
for(ll i=0;i<30;i++)
{
if(a[i]==b[i])
continue;
if(a[i])
{
num+=pow(2,i);
}
}
cout<<num<<" ";
prev=num;
}
cout<<endl;
///code end here/////////////
}
return 0;
} | 2 |
#include <bits/stdc++.h>
using namespace std;
int countOnes(int q) {
int count = 0;
while (q != 0) {
q = q & (q - 1);
count++;
}
return count;
}
int main() {
int countOnes(int q);
int n, m, k, sum = 0;
cin >> n >> m >> k;
int arr[m + 1];
for (int x = 0; x < m + 1; x++) {
cin >> arr[x];
}
for (int x = 0; x < m; x++) {
int a = arr[m] ^ arr[x];
int b = countOnes(a);
if (b <= k) {
++sum;
}
}
cout << sum;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n == 2) cout << "1\n", exit(0);
if (n == 3) cout << "7\n", exit(0);
if (n == 4) cout << "11\n", exit(0);
cout << (n % 2 != 0 ? "7" + string((n - 3) / 2, '1') : string(n / 2, '1'))
<< endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int pref[n];
pref[0] = 0;
for (int i = 1; i < n; i++) {
pref[i] = a[i] - a[i - 1];
}
int start = 0, pos = 0, temp = INT_MAX, ans = 0;
for (int i = 1; i < n; i++) {
if (pref[i] == temp) {
ans = max(ans, i - start);
start = pos;
}
if (i == n - 1) {
if (pref[i] == temp)
ans = max(ans, i - start);
else
ans = max(ans, i - start + 1);
} else if (pref[i] == 1 || pref[i] == -1) {
pos = i;
temp = pref[i];
}
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, ans, x, y;
int a[1000000], p[1000000];
vector<int> b;
int main() {
cin >> n >> m;
scanf("%d", &x);
for (i = 0; i < n - 1; i++) {
scanf("%d", &y);
a[i] = y - x;
x = y;
}
scanf("%d", &x);
for (i = 0; i < m - 1; i++) {
scanf("%d", &y);
b.push_back(y - x);
x = y;
}
n--, m--;
b.push_back(1000000000);
for (i = 0; i < n; i++) b.push_back(a[i]);
for (i = 1; i < b.size(); i++) {
while (k > 0 && b[i] != b[k]) k = p[k - 1];
if (b[i] == b[k]) k++;
p[i] = k;
}
int ans = 0;
for (i = m; i < b.size(); i++) {
if (p[i] >= m) ans++;
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1000000007;
const int N = 200000 + 7;
pair<int, int> arr[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second;
sort(arr, arr + n);
int f1 = -1, f2 = -1;
for (int i = 0; i < n; i++) {
if (arr[i].first > f1)
f1 = arr[i].second;
else if (arr[i].first > f2)
f2 = arr[i].second;
else {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, v, u;
cin >> n >> v;
long long steps = 0;
for (int i = 1; i < n; i++) {
cin >> u;
if (u < v) steps += v - u;
v = u;
}
cout << steps << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, count, sum = 0, j;
cin >> n >> m;
int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
count = 0;
for (i = 0; i < n; i++) {
if (a[i] == m) {
count++;
} else {
sum = 0;
j = i;
while (sum <= m && j < n) {
sum += a[j];
j++;
}
if (j == n) {
if (sum <= m)
i = j;
else {
j--;
sum -= a[j];
i = j - 1;
}
} else {
j--;
sum -= a[j];
j--;
i = j;
}
count++;
}
}
cout << count;
return 0;
}
| 1 |
#include <bits/stdc++.h>
namespace OI {
template <class T>
T Fabs(T x) {
return x < 0 ? -x : x;
}
template <class T>
void rd(T &x) {
x = 0;
int f = 1;
char c;
while (!isdigit(c = getchar()))
if (c == '-') f = -1;
do {
x = (x << 3) + (x << 1) + (c ^ 48);
} while (isdigit(c = getchar()));
x *= f;
}
template <class T>
void pt(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) pt(x / 10);
putchar(x % 10 ^ 48);
}
} // namespace OI
using namespace OI;
using namespace std;
const int N = 2005;
int n, m, a[N], b[N], kind;
long long sa, sb;
vector<pair<int, pair<int, int> > > suma, sumb;
pair<int, int> ans[2];
int main() {
rd(n);
for (int i = 1; i <= n; i++) rd(a[i]), sa += a[i];
rd(m);
for (int i = 1; i <= m; i++) rd(b[i]), sb += b[i];
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++) suma.push_back({a[i] + a[j], {i, j}});
for (int i = 1; i < m; i++)
for (int j = i + 1; j <= m; j++) sumb.push_back({b[i] + b[j], {i, j}});
sort(suma.begin(), suma.end());
sort(sumb.begin(), sumb.end());
long long minx = Fabs(sa - sb);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
long long tmp = Fabs(sa - sb + 2ll * b[j] - 2ll * a[i]);
if (tmp < minx) {
minx = tmp;
kind = 1;
ans[0] = {i, j};
}
}
for (int i = 0, j = 0; i < (int)suma.size() && j < (int)sumb.size();) {
long long tmp = (sa - sb + 2ll * sumb[j].first - 2ll * suma[i].first);
if (Fabs(tmp) < minx) {
minx = Fabs(tmp);
kind = 2;
ans[0] = {suma[i].second.first, sumb[j].second.first};
ans[1] = {suma[i].second.second, sumb[j].second.second};
}
if (tmp > 0)
i++;
else
j++;
}
pt(minx), puts(""), pt(kind), puts("");
for (int i = 0; i < kind; i++)
pt(ans[i].first), putchar(' '), pt(ans[i].second), puts("");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)2e9 + 7;
long long MIN1 = -INF, MIN2 = -INF, MAX1 = INF, MAX2 = INF, n, m, ans = -INF;
int main() {
ios ::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
long long li, ri;
cin >> li >> ri;
MAX1 = min(MAX1, ri);
MIN2 = max(MIN2, li);
}
cin >> m;
for (int i = 1; i <= m; ++i) {
long long li, ri;
cin >> li >> ri;
MIN1 = max(MIN1, li);
MAX2 = min(MAX2, ri);
}
if (MIN1 > MAX1)
ans = max(ans, MIN1 - MAX1);
else
ans = max(ans, (long long)0);
if (MIN2 > MAX2)
ans = max(ans, MIN2 - MAX2);
else
ans = max(ans, (long long)0);
cout << ans;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct po {
int to, next, w;
} e[4000005];
int n, m, st, en, a, b, c, d, cnt = 1, cur[4000005], head[4000005],
deep[4000005];
int xb[10005], sr[10005], g, ans = 0;
void add(int x, int y, int z) {
e[++cnt].to = y, e[cnt].w = z, e[cnt].next = head[x], head[x] = cnt;
e[++cnt].to = x, e[cnt].w = 0, e[cnt].next = head[y], head[y] = cnt;
}
bool bfs() {
queue<int> q;
while (!q.empty()) q.pop();
memset(deep, -1, sizeof(deep));
deep[st] = 0, q.push(st);
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = head[now]; i; i = e[i].next)
if (deep[e[i].to] == -1 && e[i].w)
deep[e[i].to] = deep[now] + 1, q.push(e[i].to);
}
return (deep[en] != -1);
}
long long dfs(int x, long long w) {
if (x == en || !w) return w;
long long pro, flow = 0;
for (int &i = cur[x]; i && flow < w; i = e[i].next)
if (deep[e[i].to] == deep[x] + 1 && e[i].w) {
if ((pro = dfs(e[i].to, min(w - flow, (long long)e[i].w))) > 0)
flow += pro, e[i].w -= pro, e[i ^ 1].w += pro;
if (flow == w) break;
}
if (!flow) deep[x] = -1;
return flow;
}
int main() {
scanf("%d%d%d", &n, &m, &g), st = 0, en = n * 2 + m * 2 + 1;
for (int i = 1; i <= n; i++) scanf("%d", &xb[i]);
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
if (xb[i])
add(i, en, a);
else
add(st, i, a);
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &a, &b, &c), ans += b;
for (int j = 1; j <= c; j++) scanf("%d", &sr[j]);
scanf("%d", &d);
for (int j = 1; j <= c; j++) {
if (a)
add(sr[j], n + i, 0x3f3f3f3f);
else
add(i + n, sr[j], 0x3f3f3f3f);
}
if (a)
add(n + i, en, b + d * g);
else
add(st, i + n, b + d * g);
}
int pro;
while (bfs()) {
memcpy(cur, head, sizeof(head));
while ((pro = dfs(st, 0x3f3f3f3f)) > 0) ans -= pro;
}
printf("%d", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long sum = 0;
long long x = 0;
while (n--) {
long long s;
cin >> s;
sum += s;
x ^= s;
}
cout << 2 << "\n";
cout << x << " " << sum + x << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long int mod = 1e9 + 7;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T& x) {
int f = 0;
cerr << '{';
for (auto& i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
template <typename T>
void print(vector<T> v) {
for (T i : v) cout << i << " ";
cout << '\n';
}
template <typename T>
void print(vector<vector<T>>& v) {
for (vector<T>& vv : v) {
for (T& i : vv) cout << i << " ";
cout << '\n';
}
}
template <typename T>
void read(vector<T>& v) {
for (T& i : v) cin >> i;
}
template <typename T>
void read(T&& t) {
cin >> t;
}
template <typename T, typename... Args>
void read(T&& t, Args&&... args) {
cin >> t;
read(forward<Args>(args)...);
}
template <typename T>
void print(T&& t) {
cout << t << '\n';
}
template <typename T, typename... Args>
void print(T&& t, Args&&... args) {
cout << t << " ";
print(forward<Args>(args)...);
}
bool checkPrime(ll n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
ll binpow(ll a, ll b, ll mod) {
a %= mod;
ll res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
ll to_int(string str) {
ll n = str.size(), num = 0;
for (int i = 0; i < n; i++) {
num = num * 10 + (str[i] - '0');
}
return num;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a / gcd(a, b) * b); }
ll sumofDigits(ll n) {
ll sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool isPalindrome(string& str) {
int n = str.size();
for (int i = 0; i < n; i++) {
if (str[i] != str[n - i - 1]) {
return false;
}
}
return true;
}
string toBinary(ll n) {
string str = "";
while (n > 0) {
str += n % 2 + '0';
n /= 2;
}
reverse(str.begin(), str.end());
return str;
}
ll toDecimal(string str) {
ll n = str.size(), ans = 0;
for (ll i = n - 1; i >= 0; i--) {
if (str[i] == '1') {
ans += pow(2, n - i - 1);
}
}
return ans;
}
ll modInverse(ll n, ll mod) { return binpow(n, mod - 2, mod); }
ll C(ll n, ll r, ll p) {
if (r == 0 or n == r) return 1;
ll fac[n + 1];
fac[0] = 1;
for (ll i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i;
fac[i] %= p;
}
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
p;
}
set<int> storePrimes;
void seive() {
map<int, int> is_prime;
for (int i = 2; i * i <= 1000000; i++) {
if (is_prime[i] == 0)
for (int j = i * i; j <= 100000; j += i) {
is_prime[j] = 1;
}
}
for (int i = 2; i <= 1000000; i++) {
if (is_prime[i] == 0) {
storePrimes.insert(i);
}
}
}
bool isPowerof2(ll n) {
if ((n & (n - 1)) == 0) return true;
return false;
}
ll ceil(ll a, ll b) { return (a + b - 1) / b; }
vector<ll> rotateArray(vector<ll>& arr, ll d) {
vector<ll> tmp;
ll n = arr.size();
for (ll i = 0; i < n; i++) {
tmp[i] = arr[(i + d) % n];
tmp[i] = arr[(n + i - d) % n];
}
return tmp;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
;
ll n;
cin >> n;
bool can = 0;
vector<ll> ans;
for (int i = n; i >= max(0ll, n - 100000); i--) {
;
if ((i + sumofDigits(i)) == n) {
ans.push_back(i);
can = 1;
}
}
sort(ans.begin(), ans.end());
if (can) {
print(ans.size());
for (ll i : ans) {
print(i);
}
} else {
print(0);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string s;
string gen(int x) {
string ret;
while (x--) ret += "1";
return ret;
}
int main() {
cin >> s;
string ans;
int cnt1 = 0;
for (char c : s) {
if (c == '1') {
cnt1++;
continue;
}
ans += c;
}
if (ans.empty()) {
ans = gen(cnt1);
} else {
if (ans[0] == '2') {
ans = gen(cnt1) + ans;
} else {
int ptr = 0;
int len = ans.length();
while (ptr < len and ans[ptr] == '0') ptr++;
ans = ans.substr(0, ptr) + gen(cnt1) + ans.substr(ptr, len);
}
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int p = 1e9 + 7;
const int lim = 1e6;
bool check(int elem) { return true; }
void bsearch() {
int lo = 0, hi = 1e9, res = -1, mid;
while (lo <= hi) {
mid = lo + (hi - lo) / 2;
if (check(mid)) {
res = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
}
struct Node {};
struct Compare {
bool operator()(const Node &a, const Node &b) { return true; }
};
bool compare(const Node &a, const Node &b) { return true; }
void pre_io() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int32_t main() {
pre_io();
int test;
cin >> test;
while (test--) {
int n;
cin >> n;
int pref[2][n + 2], v[2][n + 1];
for (int i = 0; i < n; i++) cin >> v[0][i];
for (int i = 0; i < n; i++) cin >> v[1][i];
pref[0][n + 1] = 0;
for (int i = n; i >= 1; i--) pref[0][i] = pref[0][i + 1] + v[0][i - 1];
pref[1][0] = 0;
for (int i = 1; i <= n; i++) pref[1][i] = pref[1][i - 1] + v[1][i - 1];
int res = INT_MAX;
for (int i = 1; i <= n; i++) {
res = min(res, max(pref[1][i - 1], pref[0][i + 1]));
}
cout << res << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n + 1];
a[1] = 0;
int p;
for (int i = 2; i <= n; ++i) {
cin >> a[i];
}
p = n;
vector<int> ans;
while (p) {
ans.push_back(p);
p = a[p];
}
for (int i = ans.size() - 1; i >= 0; --i) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2000;
int t[MAX_N + 1];
int m[MAX_N + 1];
int h[MAX_N + 1];
int eaten[MAX_N + 1];
int main() {
int n, x;
cin >> n >> x;
for (int i = 1; i <= n; ++i) cin >> t[i] >> h[i] >> m[i];
int answer = 0;
int curr_max = 0;
int curr_t = 0;
int ht = x;
for (int i = 1; i <= n; ++i) {
int best = 0;
for (int j = 1; j <= n; ++j) {
if (!eaten[j] && ht >= h[j] && curr_t == t[j] && m[best] < m[j]) {
best = j;
}
}
if (best == 0) break;
ht += m[best];
eaten[best] = 1;
curr_t = 1 - curr_t;
++curr_max;
}
answer = curr_max;
curr_max = 0;
curr_t = 1;
for (int i = 1; i <= n; ++i) eaten[i] = 0;
ht = x;
for (int i = 1; i <= n; ++i) {
int best = 0;
for (int j = 1; j <= n; ++j) {
if (!eaten[j] && ht >= h[j] && curr_t == t[j] && m[best] < m[j]) {
best = j;
}
}
if (best == 0) break;
ht += m[best];
eaten[best] = 1;
curr_t = 1 - curr_t;
++curr_max;
}
cout << max(answer, curr_max) << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
struct Point {
double x, y;
Point(double a = 0, double b = 0) : x(a), y(b) {}
inline void read() { scanf("%lf%lf", &x, &y); }
inline void print() { printf("%.2lf %.2lf\n", x, y); }
inline Point operator+(const Point &b) { return Point(x + b.x, y + b.y); }
inline Point operator-(const Point &b) { return Point(x - b.x, y - b.y); }
inline Point operator*(const double p) { return Point(x * p, y * p); }
inline Point operator/(const double p) { return Point(x / p, y / p); }
inline Point operator==(const Point &b) {
return fabs(x - b.x) < 1e-10 && fabs(y - b.y) < 1e-10;
}
inline Point operator<(const Point &b) {
return x < b.x || (x == b.x && y < b.y);
}
inline double ang() { return atan2(y, x); }
} A, B, C;
struct Interval {
double l, r;
Interval(double a = 0, double b = 0) : l(a), r(b) {}
inline bool In_Interval(double x) { return x >= l && x <= r; }
};
struct Circle {
Point O;
double r;
Circle() {}
Circle(Point O, double r) : O(O), r(r) {}
inline Point point(double rad) {
return Point(O.x + r * cos(rad), O.y + r * sin(rad));
}
};
double t1, t2, L1, L2;
inline double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; }
inline double Lenth(Point A) { return sqrt(Dot(A, A)); }
Interval Circle_Jiao(Circle c1, Circle c2) {
double a = c1.r;
double b = c2.r;
double c = Lenth(c1.O - c2.O);
double temp = (c1.O - c2.O).ang();
double delta = acos((a * a + c * c - b * b) / (2 * a * c));
return Interval(temp - delta, temp + delta);
}
bool Separate(Circle c1, Circle c2) {
return Lenth(c1.O - c2.O) > c1.r + c2.r + 1e-10;
}
bool Contain(Circle c1, Circle c2) {
return Lenth(c1.O - c2.O) < fabs(c1.r - c2.r);
}
bool Interval_Jiao(Interval l1, Interval l2) {
return l1.In_Interval(l2.l) || l1.In_Interval(l2.r) || l2.In_Interval(l1.l) ||
l2.In_Interval(l1.r);
}
bool Judge(Circle c1, Circle c2, Circle c3) {
Interval l1 = Circle_Jiao(c1, c2);
Interval l2 = Circle_Jiao(c1, c3);
if (Interval_Jiao(l1, l2)) return 1;
return 0;
}
bool check(double mid) {
Circle c1(A, mid);
Circle c2(B, L1 - mid);
Circle c3(C, L2 - Lenth(B - C) - mid);
if (Separate(c1, c2) || Separate(c1, c3) || Separate(c2, c3)) return 0;
if (Contain(c1, c2) || Contain(c1, c3) || Contain(c2, c3)) return 1;
if (Judge(c1, c2, c3) || Judge(c2, c1, c3) || Judge(c3, c1, c2)) return 1;
return 0;
}
void solve() {
double l = 0, r = min(L1, L2 - Lenth(B - C));
while (l + 1e-10 < r) {
double mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}
printf("%.10lf\n", check(r) ? r : l);
}
int main() {
scanf("%lf%lf", &t2, &t1);
A.read();
B.read();
C.read();
L1 = t1 + Lenth(A - B);
L2 = t2 + Lenth(A - C) + Lenth(B - C);
if (L1 >= Lenth(A - C) + Lenth(B - C))
printf("%.10lf\n", min(L1, L2));
else
solve();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
struct Order {
int a, b, idx;
};
int n, p, k;
Order orders[100010];
char used[100010];
struct s1cmp {
bool operator()(const int &a, const int &b) {
return orders[a].a == orders[b].a ? orders[a].b < orders[b].b
: orders[a].a < orders[b].a;
}
};
struct s2cmp {
bool operator()(const int &a, const int &b) {
return orders[a].b == orders[b].b ? orders[a].a < orders[b].a
: orders[a].b < orders[b].b;
}
};
multiset<int, s1cmp> s1;
multiset<int, s2cmp> s2;
bool ocmp(const Order &a, const Order &b) {
return a.b == b.b ? a.a > b.a : a.b < b.b;
}
int main() {
scanf("%d %d %d", &n, &p, &k);
for (int i = 0; i < n; i++) {
scanf("%d %d", &orders[i].a, &orders[i].b);
orders[i].idx = i;
}
sort(orders, orders + n, ocmp);
int added = 0;
int removed = 0;
for (int i = p - k; i < n; i++) {
s1.insert(i);
added++;
if (s1.size() > k) {
s1.erase(s1.begin());
removed++;
}
}
memset(used, 0, sizeof(used));
int minb = 1e9 + 1;
int minv = 1e9 + 1;
for (typeof(s1.begin()) it = s1.begin(); it != s1.end(); it++) {
int i = *it;
if (orders[i].b < minb) {
minb = orders[i].b;
minv = orders[i].a;
}
used[i] = 1;
printf("%d ", orders[i].idx + 1);
}
for (int i = 0; i < n; i++) {
if (used[i]) continue;
if (orders[i].b < minb || (orders[i].b == minb && orders[i].a >= minv)) {
s2.insert(i);
if (s2.size() > p - k) s2.erase(s2.begin());
}
}
for (typeof(s2.begin()) it = s2.begin(); it != s2.end(); it++) {
int i = *it;
printf("%d ", orders[i].idx + 1);
}
printf("\n");
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
string s;
struct gg {
int a, b, c;
} g[120000];
int cmp(gg x, gg y) {
if (x.a != y.a)
return x.a < y.a;
else
return x.b < y.b;
}
int p[120000];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> g[i].a >> g[i].b;
g[i].c = i;
}
sort(g, g + n, cmp);
int x, pp = g[0].b, falg = 0;
for (int i = 1; i < n; i++) {
if (g[i].a > pp) {
x = i;
falg = 1;
break;
}
pp = max(pp, g[i].b);
}
if (!falg)
cout << "-1" << endl;
else {
for (int i = 0; i < x; i++) {
p[g[i].c] = 1;
}
for (int i = x; i < n; i++) {
p[g[i].c] = 2;
}
for (int i = 0; i < n; i++) {
cout << p[i];
if (i == n - 1)
cout << endl;
else
cout << " ";
}
}
}
int main() {
ios::sync_with_stdio(0);
int t;
cin >> t;
while (t--) solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c <= '9' && c >= '0') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return w == 1 ? x : -x;
}
const int N = 5e2 + 10;
int n;
int a[N], vis[N];
int main() {
int _ = read();
while (_--) {
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
int base = 1;
vector<int> ans;
for (int k = 1; k < n - 1; ++k) {
int mi = INT_MAX, id = 0;
for (int i = base; i <= n; ++i) {
if (a[i] < mi) mi = a[i], id = i;
}
while (id >= base + 2) {
ans.push_back(id - 2);
int tmp = a[id];
a[id] = a[id - 1];
a[id - 1] = a[id - 2];
a[id - 2] = tmp;
id = id - 2;
}
if (id == base + 1) {
ans.push_back(base);
ans.push_back(base);
int tmp = a[id];
a[id] = a[id + 1];
a[id + 1] = a[base];
a[base] = tmp;
}
base++;
}
int flag = 1;
for (int i = 1; i + 1 <= n; ++i)
if (a[i + 1] < a[i]) flag = 0;
if (flag) {
printf("%d\n", ans.size());
for (int v : ans) printf("%d ", v);
puts("");
} else {
int f = 0, mx = 0;
for (int i = 1; i <= n; i++) mx = max(mx, a[i]);
for (int i = 0; i <= mx; i++) vis[i] = 0;
for (int i = 1; i <= n; i++) vis[a[i]]++;
for (int i = 1; i <= n; i++)
if (vis[a[i]] >= 2) f = 1;
if (f) {
int id = n;
while (id - 1 >= 1 && a[id] < a[id - 1]) {
ans.push_back(id - 2);
int tmp = a[id];
a[id] = a[id - 1];
a[id - 1] = a[id - 2];
a[id - 2] = tmp;
id--;
}
printf("%d\n", ans.size());
for (int v : ans) printf("%d ", v);
puts("");
} else
puts("-1");
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
void chkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
void chkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
inline void debug(int x) { fprintf(stderr, "ycx has aked ioi %d times\n", x); }
namespace fastio {
char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf;
inline char getc() {
return p1 == p2 &&
(p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2)
? -1
: *p1++;
}
inline void putc(char x) { (*p3++ = x); }
template <typename T>
void read(T &x) {
x = 0;
char c = getchar();
T neg = 0;
while (!isdigit(c)) neg |= !(c ^ '-'), c = getchar();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
if (neg) x = (~x) + 1;
}
template <typename T>
void recursive_print(T x) {
if (!x) return;
recursive_print(x / 10);
putc(x % 10 ^ 48);
}
template <typename T>
void print(T x) {
if (!x) putc('0');
if (x < 0) putc('-'), x = ~x + 1;
recursive_print(x);
}
template <typename T>
void print(T x, char c) {
if (!x) putc('0');
if (x < 0) putc('-'), x = ~x + 1;
recursive_print(x);
putc(c);
}
void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); }
} // namespace fastio
const int MAXN = 1.5e5;
int n, a[MAXN + 5], p[MAXN + 5];
struct node {
int l, r, len, slen, mx, smx, mxc, tg, stg;
long long sum;
} s[MAXN * 4 + 5];
void build(int k, int l, int r) {
s[k].l = l;
s[k].r = r;
if (l == r) return;
int mid = l + r >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
}
void clear(int k) {
s[k].len = s[k].slen = s[k].mx = s[k].smx = s[k].mxc = s[k].tg = s[k].stg =
s[k].sum = 0;
if (s[k].l == s[k].r)
return;
else
clear(k << 1), clear(k << 1 | 1);
}
void pushup(int k) {
s[k].sum = s[k << 1].sum + s[k << 1 | 1].sum;
s[k].mx = max(s[k << 1].mx, s[k << 1 | 1].mx);
if (s[k << 1].mx > s[k << 1 | 1].mx) {
s[k].len = s[k << 1].len;
s[k].smx = max(s[k << 1].smx, s[k << 1 | 1].mx);
s[k].slen = s[k << 1].slen + s[k << 1 | 1].len + s[k << 1 | 1].slen;
} else if (s[k << 1].mx < s[k << 1 | 1].mx) {
s[k].len = s[k << 1 | 1].len;
s[k].smx = max(s[k << 1 | 1].smx, s[k << 1].mx);
s[k].slen = s[k << 1 | 1].slen + s[k << 1].len + s[k << 1].slen;
} else {
s[k].len = s[k << 1].len + s[k << 1 | 1].len;
s[k].slen = s[k << 1].slen + s[k << 1 | 1].slen;
s[k].smx = max(s[k << 1].smx, s[k << 1 | 1].smx);
}
}
void pushtag(int k, int t1, int t2, bool is) {
if (!is) t1 = t2;
s[k].mx += t1;
s[k].tg += t1;
s[k].smx += t2;
s[k].stg += t2;
s[k].sum += 1ll * t1 * s[k].len + 1ll * t2 * s[k].slen;
}
void pushdown(int k) {
bool tmp = s[k << 1 | 1].mx >= s[k << 1].mx;
pushtag(k << 1, s[k].tg, s[k].stg, s[k << 1].mx >= s[k << 1 | 1].mx);
pushtag(k << 1 | 1, s[k].tg, s[k].stg, tmp);
s[k].tg = s[k].stg = 0;
}
int add(int k, int l, int r) {
if (l > r) return 0;
if (l <= s[k].l && s[k].r <= r)
return pushtag(k, 1, 1, 1), s[k].len + s[k].slen;
int mid = (pushdown(k), s[k].l + s[k].r >> 1), ret;
if (r <= mid)
ret = add(k << 1, l, r);
else if (l > mid)
ret = add(k << 1 | 1, l, r);
else
ret = add(k << 1, l, mid) + add(k << 1 | 1, mid + 1, r);
return pushup(k), ret;
}
void turn(int k, int x, int v) {
if (s[k].l == s[k].r) return s[k].sum = s[k].mx = v, s[k].len = 1, void();
int mid = (pushdown(k), s[k].l + s[k].r >> 1);
((x <= mid) ? turn(k << 1, x, v) : turn(k << 1 | 1, x, v)), pushup(k);
}
void modify(int k, int l, int r, int v) {
if (l > r || v >= s[k].mx) return;
if (l <= s[k].l && s[k].r <= r) {
if (v > s[k].smx)
return pushtag(k, v - s[k].mx, 0, 1), void();
else {
int mid = (pushdown(k), l + r >> 1);
return modify(k << 1, l, mid, v), modify(k << 1 | 1, mid + 1, r, v),
pushup(k), void();
}
}
int mid = (pushdown(k), s[k].l + s[k].r >> 1);
if (r <= mid)
modify(k << 1, l, r, v);
else if (l > mid)
modify(k << 1 | 1, l, r, v);
else
modify(k << 1, l, mid, v), modify(k << 1 | 1, mid + 1, r, v);
pushup(k);
}
long long ans[MAXN + 5];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i;
build(1, 1, n);
for (int ycxakioi = 1; ycxakioi <= 2; ycxakioi++) {
clear(1);
for (int i = 1, x; i <= n; i++) {
x = add(1, p[i] + 1, n);
turn(1, p[i], i + 1);
modify(1, 1, p[i] - 1, i - x);
ans[i] += s[1].sum;
}
for (int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
}
for (int i = 1; i <= n; i++) printf("%lld\n", ans[i] - 1ll * i * (i + 2));
return 0;
}
| 12 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const int mod = (int)1e9 + 7;
const int N = 1e5 + 10;
const long long inf = 1e11;
long long nxt[N][26];
const long long maxm = 250005;
long long fact[maxm];
void solve() {
long long n, m;
cin >> n >> m;
fact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = i * fact[i - 1], fact[i] = fact[i] % m;
long long ans = 0;
for (long long i = 1; i <= n; i++) {
long long curr = (fact[i] * fact[n - i + 1]) % m * (n - i + 1LL);
ans += curr;
ans = ans % m;
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
tuple<T...> const &t) {
os << (n == 0 ? "" : ", ") << get<n>(t);
_ot<n + 1>(os, t);
}
template <class... T>
ostream &operator<<(ostream &o, tuple<T...> const &t) {
o << "(";
_ot<0>(o, t);
o << ")";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, pair<T, U> const &p) {
o << "(" << p.first << ", " << p.second << ")";
return o;
}
template <class T>
ostream &operator<<(ostream &o, const stack<T> &a) {
o << "{";
for (auto tmp = a; tmp.size(); tmp.pop())
o << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
o << "}";
return o;
}
template <class T,
class = typename iterator_traits<typename T::iterator>::value_type,
class = typename enable_if<!is_same<T, string>::value>::type>
ostream &operator<<(ostream &o, const T &a) {
for (auto ite = a.begin(); ite != a.end(); ++ite)
o << (ite == a.begin() ? "" : " ") << *ite;
return o;
}
const int N = 1e5;
ll n, t;
int a[N], b[N];
using D = double;
D p[N];
template <class T = long long, class Comp = less<T> >
struct DynamicCHT {
static T INF;
static Comp comp;
private:
struct Line {
T a, b;
Line(const T &a, const T &b) : a(a), b(b) {}
bool operator<(const Line &rhs) const {
return a != rhs.a ? comp(rhs.a, a) : comp(b, rhs.b);
}
};
struct CP {
T numer, denom;
Line p;
CP(const T &n) : numer(n), denom(1), p(0, 0) {}
CP(const Line &p1, const Line &p2) : p(p2) {
if (p1.a == INF || p1.a == -INF)
numer = -INF, denom = 1;
else if (p2.a == INF || p2.a == -INF)
numer = INF, denom = 1;
else {
numer = p1.b - p2.b, denom = p2.a - p1.a;
if (denom < 0) numer = -numer, denom = -denom;
}
}
bool operator<(const CP &rhs) const {
if (numer == INF || rhs.numer == -INF) return 0;
if (numer == -INF || rhs.numer == INF) return 1;
return numer * rhs.denom < rhs.numer * denom;
}
};
set<Line> lines;
set<CP> cps;
public:
DynamicCHT() {
lines.insert({Line(INF, 0), Line(-INF, 0)});
cps.insert(CP(Line(INF, 0), Line(-INF, 0)));
}
void add(const T &a, const T &b) {
const Line p(a, b);
typename set<Line>::iterator pos = lines.insert(p).first;
if (check(*prev(pos), p, *next(pos))) {
lines.erase(pos);
return;
}
cps.erase(CP(*prev(pos), *next(pos)));
{
typename set<Line>::iterator it = prev(pos);
while (it != lines.begin() && check(*prev(it), *it, p)) --it;
eraseRange(it, prev(pos));
lines.erase(++it, pos);
pos = lines.find(p);
}
{
typename set<Line>::iterator it = next(pos);
while (next(it) != lines.end() && check(p, *it, *next(it))) ++it;
eraseRange(++pos, it);
lines.erase(pos, it);
pos = lines.find(p);
}
cps.insert(CP(*prev(pos), *pos));
cps.insert(CP(*pos, *next(pos)));
}
T query(const T &x) const {
const Line &p = (--cps.lower_bound(CP(x)))->p;
return p.a * x + p.b;
}
tuple<T, T> get(const T &x) const {
const Line &p = (--cps.lower_bound(CP(x)))->p;
return make_tuple(p.a, p.b);
}
friend ostream &operator<<(ostream &os, const DynamicCHT &a) {
os << "\n";
os << "lines : " << a.lines.size() << "\n";
for (auto &p : a.lines)
os << "(" << p.a << ", " << p.b << ")"
<< "\n";
os << "cross points : " << a.cps.size() << "\n";
for (auto &p : a.cps)
os << "(x = " << p.numer << "/" << p.denom << "; " << p.p.a << ", "
<< p.p.b << ")"
<< "\n";
return os;
}
private:
void eraseRange(typename set<Line>::iterator a,
typename set<Line>::iterator b) {
for (typename set<Line>::iterator it = a; it != b; ++it)
cps.erase(CP(*it, *next(it)));
}
bool check(const Line &p1, const Line &p2, const Line &p3) {
if (p1.a == p2.a) return 1;
if (p1.a == INF || p1.a == -INF || p3.a == INF || p3.a == -INF) return 0;
return (p2.a - p1.a) * (p3.b - p2.b) + (1e-19) >=
(p2.b - p1.b) * (p3.a - p2.a);
}
};
template <class T, class Comp>
T DynamicCHT<T, Comp>::INF = numeric_limits<T>::has_infinity
? numeric_limits<T>::infinity()
: numeric_limits<T>::max();
template <class T, class Comp>
Comp DynamicCHT<T, Comp>::comp = Comp();
template <class T>
vector<vector<T> > mult(vector<vector<T> > a, vector<vector<T> > b) {
assert(a[0].size() == b.size());
vector<vector<T> > res(a.size(), vector<T>(b[0].size(), 0));
for (size_t i = 0; i < a.size(); i++) {
for (size_t j = 0; j < b[0].size(); j++) {
for (size_t k = 0; k < b.size(); k++) {
res[i][j] += a[i][k] * b[k][j];
}
}
}
return res;
}
template <class T>
vector<vector<T> > makeE(int n) {
vector<vector<T> > r(n, vector<T>(n, 0));
for (int i = 0; i < n; i++) r[i][i] = T(1);
return r;
}
template <class T>
vector<vector<T> > pow(vector<vector<T> > a, ll k) {
assert(a.size() == a[0].size());
vector<vector<T> > r = makeE<T>(a.size());
while (k) {
if (k & 1) r = mult(r, a);
a = mult(a, a);
k >>= 1;
}
return r;
}
using Vec = vector<D>;
using Mat = vector<Vec>;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0);
cin >> n >> t;
D m = 0;
DynamicCHT<D, greater<D> > cht;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i] >> p[i];
m = max(m, b[i] * p[i]);
cht.add(p[i], p[i] * a[i]);
}
(42);
cout << fixed << setprecision(12);
ll le = 1;
Mat v{{cht.query(0)}, {1}, {1}};
while (le != t) {
D xle = m * le - v[0][0];
auto g = cht.get(xle);
(42);
(42);
ll f = t - le;
ll h = 1;
vector<Mat> mats;
Mat A(3, Vec(3));
A[0][0] = 1 - get<0>(g);
A[0][1] = get<0>(g) * m;
A[0][2] = get<1>(g);
A[1][1] = A[1][2] = A[2][2] = 1;
mats.push_back(A);
while ((1ll << h) - 1 < f) {
A = mult(A, A);
mats.push_back(A);
h++;
}
ll now = 0;
for (int k = h - 1; k >= 0; k--) {
ll j = 1ll << k;
if (now + j > f) continue;
auto tv = mult(mats[k], v);
auto tdp = tv[0][0];
ll tt = le + now + j;
D x = m * tt - tdp;
(42);
(42);
(42);
if (g == cht.get(x)) {
v = tv;
now += j;
}
}
if (now + 1 <= f) {
v = mult(mats[0], v);
now++;
}
(42);
le += now;
}
cout << v[0][0] << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
const int N = 7e5, MASK = 1 << 30;
char s[N];
int kmp[N] = {-1};
std::vector<int> loss[N];
struct bigInt {
const static int base = 10;
std::vector<long long> v;
void reduce() {
for (int i = 0; v[i] >= base; ++i) {
if (i == v.size() - 1) v.push_back(0);
v[i + 1] += v[i] / base;
v[i] %= base;
}
}
bigInt(long long x) {
v.clear();
v.push_back(x);
reduce();
}
bigInt& operator+=(long long x) {
v[0] += x;
reduce();
return *this;
}
void write() {
for (auto it = v.rbegin(); it != v.rend(); ++it) putchar(*it + '0');
putchar(' ');
}
};
int main() {
int n;
scanf("%d", &n);
long long res = 0;
int sum26 = 0, sum_mask = 0;
bigInt sum(0);
std::map<int, std::pair<long long, int>, std::greater<int>> map;
for (int i = 1; i <= n; ++i) {
char c;
int w;
scanf(" %c%d", &c, &w);
s[i] = (c - 'a' + sum26) % 26 + 'a';
std::pair<long long, int> cur(w ^ sum_mask & MASK - 1, 1);
while (!map.empty()) {
std::pair<long long, int> prv = map.begin()->second;
if (prv.first >= cur.first) {
res -= prv.first * prv.second;
cur.second += prv.second;
map.erase(map.begin());
} else
break;
}
res += cur.first * cur.second;
map[i] = cur;
kmp[i] = kmp[i - 1];
while (kmp[i] >= 0 && s[kmp[i] + 1] != s[i]) {
loss[i].push_back(kmp[i]);
kmp[i] = kmp[kmp[i]];
}
for (int x : loss[++kmp[i]]) loss[i].push_back(x);
for (int x : loss[i]) {
std::pair<long long, int>& p = std::prev(map.upper_bound(i - x))->second;
res -= p.first;
--p.second;
}
sum26 = (sum26 + res % 26) % 26;
sum_mask = (sum_mask + res % MASK) % MASK;
(sum += res).write();
printf("\n");
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int solve(string &kind, int slide, int hcost, int wcost, int maxt) {
int n = kind.size();
int result = 0;
{
int res = 0, i = 0, takes = 0;
do {
if (i != 0) takes += slide;
takes += kind[i] == 'h' ? hcost : wcost;
++res;
if (takes <= maxt) result = max(result, res);
i = (i + 1) % n;
} while (i != 0);
}
{
int res = 0, i = 0, j = n - 1, takes = 0;
do {
if (i != 0) takes += 2 * slide;
takes += kind[i] == 'h' ? hcost : wcost;
++res;
while (j != i &&
takes + slide + (kind[j] == 'h' ? hcost : wcost) <= maxt) {
takes += slide + (kind[j] == 'h' ? hcost : wcost);
++res;
j = (j - 1 + n) % n;
}
while (j != n - 1 && takes > maxt) {
j = (j + 1) % n;
--res;
takes -= slide + (kind[j] == 'h' ? hcost : wcost);
}
if (takes <= maxt) result = max(result, res);
if (i == j && (i + 1) % n != 0) {
j = (j + 1) % n;
--res;
takes -= slide + (kind[j] == 'h' ? hcost : wcost);
}
i = (i + 1) % n;
} while (i != 0);
}
return result;
}
int main() {
int n, slide, rot, maxt;
scanf("%d %d %d %d", &n, &slide, &rot, &maxt);
string kind(n, ' ');
scanf("%s", &kind[0]);
int result = 0;
for (int rev = 0; rev < 2; ++rev) {
result = max(result, solve(kind, slide, 1, 1 + rot, maxt));
reverse(kind.begin(), kind.end());
rotate(kind.begin(), kind.end() - 1, kind.end());
}
printf("%d\n", result);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int H = 16;
const int N = 1 << 18;
int p;
int fpow(int x, int y = p - 2) {
if (!y) return 1;
long long z = fpow(x, y >> 1);
z = z * z % p;
if (y & 1) z = z * x % p;
return z;
}
int l[2], a[2][N];
void dfs(int k, int u, int t, int e) {
u = (u + p) % p;
a[e][l[e]++] = u;
if (k == H) return;
k++;
if (t != 2) dfs(k, u + 1, 1, e);
if (t != 1) dfs(k, u - 1, 2, e);
if (t != 3) dfs(k, fpow(u), 3, e);
}
int f[2];
vector<int> v[2];
void getans(int k, int u, int t, int e, int j) {
u = (u + p) % p;
if (!f[e]) v[e].push_back(t);
if (!f[e] && u == j) {
f[e] = 1;
return;
}
if (k == H) {
if (!f[e]) v[e].pop_back();
return;
}
k++;
if (t != 2) getans(k, u + 1, 1, e, j);
if (t != 1) getans(k, u - 1, 2, e, j);
if (t != 3) getans(k, fpow(u), 3, e, j);
if (!f[e]) v[e].pop_back();
}
int main() {
int i, j, x, y, z;
cin >> x >> y >> p;
dfs(0, x, 0, 0), dfs(0, y, 0, 1);
sort(a[0], a[0] + l[0]);
for (i = 0; i < l[1]; i = i + 1) {
j = lower_bound(a[0], a[0] + l[0], a[1][i]) - a[0];
if (j < l[0] && a[0][j] - a[1][i] <= 160) break;
}
z = a[0][j] - a[1][i];
getans(0, x, 0, 0, a[0][j]), getans(0, y, 0, 1, a[1][i]);
cout << v[0].size() - 1 + z + v[1].size() - 1 << endl;
for (i = 1; i < v[0].size(); i = i + 1) cout << v[0][i] << ' ';
for (i = 0; i < z; i = i + 1) cout << 2 << ' ';
for (i = v[1].size() - 1; i > 0; i = i - 1) {
if (v[1][i] < 3) v[1][i] = 3 - v[1][i];
cout << v[1][i] << ' ';
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using std::map;
using std::pair;
using std::string;
using std::vector;
map<string, vector<pair<bool, bool>>> SampleInput();
map<string, vector<pair<bool, bool>>> ReadInput(
std::istream& in_stream = std::cin);
void WriteAnswer(const vector<bool>& min, const vector<bool>& max,
std::ostream& out_stream = std::cout);
pair<vector<bool>, vector<bool>> BestSums(
const map<string, vector<pair<bool, bool>>>& variables);
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
auto input = ReadInput();
auto result = BestSums(input);
WriteAnswer(result.first, result.second);
return 0;
}
pair<vector<bool>, vector<bool>> BestSums(
const map<string, vector<pair<bool, bool>>>& variables) {
size_t bit_size = variables.at("?").size();
vector<pair<size_t, size_t>> count_sum(bit_size);
for (auto var : variables) {
if (var.first != "?") {
const auto& variants = var.second;
for (size_t pos = 0; pos < bit_size; ++pos) {
count_sum[pos].first += variants[pos].first;
count_sum[pos].second += variants[pos].second;
}
}
}
vector<bool> min, max;
for (auto bit_count : count_sum) {
min.push_back(bit_count.second < bit_count.first);
max.push_back(bit_count.second > bit_count.first);
}
return std::make_pair(min, max);
}
vector<pair<bool, bool>> Pure(const string& bit_mask);
vector<pair<bool, bool>> Pure(const vector<bool>& bit_mask);
vector<pair<bool, bool>> And(const vector<pair<bool, bool>>& fst,
const vector<pair<bool, bool>>& snd);
vector<pair<bool, bool>> Or(const vector<pair<bool, bool>>& fst,
const vector<pair<bool, bool>>& snd);
vector<pair<bool, bool>> Xor(const vector<pair<bool, bool>>& fst,
const vector<pair<bool, bool>>& snd);
map<string, vector<pair<bool, bool>>> SampleInput() {
size_t bit_size = 3;
map<string, vector<pair<bool, bool>>> variables;
variables["?"] = vector<pair<bool, bool>>(bit_size, std::make_pair(0, 1));
variables["a"] = Pure(vector<bool>{1, 0, 1});
variables["b"] = Pure(vector<bool>{0, 1, 1});
variables["c"] = Xor(variables["b"], variables["?"]);
return variables;
}
pair<string, vector<pair<bool, bool>>> ParseString(
string& line, const map<string, vector<pair<bool, bool>>>& varianles);
map<string, vector<pair<bool, bool>>> ReadInput(std::istream& in_stream) {
size_t variables_size, bit_size;
in_stream >> variables_size >> bit_size;
map<string, vector<pair<bool, bool>>> variables;
variables["?"] = vector<pair<bool, bool>>(bit_size, std::make_pair(0, 1));
in_stream.get();
for (size_t var = 0; var < variables_size; ++var) {
string line;
getline(in_stream, line);
variables.insert(ParseString(line, variables));
}
return variables;
}
enum Operation { NONE, AND, OR, XOR };
pair<string, vector<pair<bool, bool>>> ParseString(
string& line, const map<string, vector<pair<bool, bool>>>& varianles) {
while (line.back() == ' ') {
line.pop_back();
}
size_t finished_lexems = 0;
string name, fst = "", snd = "";
vector<pair<bool, bool>> variants;
Operation operation = NONE;
for (auto charapter : line) {
if (charapter != ' ') {
if ('A' <= charapter && charapter <= 'Z') {
if (operation == NONE) {
switch (charapter) {
case 'X':
operation = XOR;
break;
case 'O':
operation = OR;
break;
default:
operation = AND;
break;
}
}
} else if (('a' <= charapter && charapter <= 'z') || charapter == '?' ||
'0' == charapter || '1' == charapter) {
if (finished_lexems == 0) {
name += charapter;
} else if (finished_lexems <= 2) {
fst += charapter;
} else {
snd += charapter;
}
}
} else {
++finished_lexems;
}
}
switch (operation) {
case NONE:
variants = Pure(fst);
break;
case AND:
variants = And(varianles.at(fst), varianles.at(snd));
break;
case OR:
variants = Or(varianles.at(fst), varianles.at(snd));
break;
case XOR:
variants = Xor(varianles.at(fst), varianles.at(snd));
break;
default:
variants = Pure(fst);
break;
}
return std::make_pair(name, variants);
}
vector<pair<bool, bool>> Pure(const string& str) {
vector<pair<bool, bool>> result;
for (auto charapter : str) {
bool bit = charapter == '1';
result.push_back(std::make_pair(bit, bit));
}
return result;
}
vector<pair<bool, bool>> Pure(const vector<bool>& bit_mask) {
vector<pair<bool, bool>> result;
for (auto bit : bit_mask) {
result.push_back(std::make_pair(bit, bit));
}
return result;
}
vector<pair<bool, bool>> And(const vector<pair<bool, bool>>& fst,
const vector<pair<bool, bool>>& snd) {
vector<pair<bool, bool>> result;
for (auto it_1 = fst.begin(), it_2 = snd.begin();
it_1 != fst.end() && it_2 != snd.end(); ++it_1, ++it_2) {
result.push_back(std::make_pair(it_1->first && it_2->first,
it_1->second && it_2->second));
}
return result;
}
vector<pair<bool, bool>> Or(const vector<pair<bool, bool>>& fst,
const vector<pair<bool, bool>>& snd) {
vector<pair<bool, bool>> result;
for (auto it_1 = fst.begin(), it_2 = snd.begin();
it_1 != fst.end() && it_2 != snd.end(); ++it_1, ++it_2) {
result.push_back(std::make_pair(it_1->first || it_2->first,
it_1->second || it_2->second));
}
return result;
}
vector<pair<bool, bool>> Xor(const vector<pair<bool, bool>>& fst,
const vector<pair<bool, bool>>& snd) {
vector<pair<bool, bool>> result;
for (auto it_1 = fst.begin(), it_2 = snd.begin();
it_1 != fst.end() && it_2 != snd.end(); ++it_1, ++it_2) {
result.push_back(
std::make_pair(it_1->first ^ it_2->first, it_1->second ^ it_2->second));
}
return result;
}
void WriteAnswer(const vector<bool>& min, const vector<bool>& max,
std::ostream& out_stream) {
for (auto digit : min) {
out_stream << digit;
}
out_stream << std::endl;
for (auto digit : max) {
out_stream << digit;
}
out_stream << std::endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
float n;
cin >> n;
long long int flag = 0;
for (float i = 3; i < 1000; i++) {
float x = (i - 2) * (180 / i);
if (x == n) {
flag = 1;
}
}
if (flag)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000007;
long long INFLL = (long long)INF * (long long)INF;
long double EPS = 10e-9;
long double pi = 2 * acos(0.0);
void solve() {
long long n;
cin >> n;
if (n <= 2) {
cout << n << "\n";
return;
}
long long temp = (n) / 2;
if (n % 2 == 0) {
cout << temp * (temp - 1ll) + (n) << "\n";
} else
cout << temp * temp + (n) << "\n";
}
int32_t main() {
clock_t start, end;
start = clock();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
end = clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
cerr << "\nTime taken by program is : " << fixed << time_taken
<< setprecision(5);
cerr << " sec "
<< "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct tr {
int a, b, c, d;
};
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool b[1000][1000];
int spd[11];
int ans[11];
vector<pair<int, int> > st[11];
string s[1000];
int main() {
int n, m, p;
cin >> n >> m >> p;
for (int i = 1; i <= p; i++) cin >> spd[i];
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int j = 0; s[i][j]; j++) {
char x = s[i][j];
if (x >= '1' && x <= '9') {
b[i][j] = true;
st[x - '0'].push_back({i, j});
ans[x - '0']++;
} else if (x == '#')
b[i][j] = true;
}
}
queue<tr> qe;
for (int i = 1; i <= p; i++) {
for (auto x : st[i]) qe.push({i, x.first, x.second, spd[i]});
}
while (!qe.empty()) {
tr x = qe.front();
qe.pop();
queue<tr> nq;
nq.push(x);
while (!qe.empty() && qe.front().a == x.a) {
nq.push(qe.front());
qe.pop();
}
while (!nq.empty()) {
tr xx = nq.front();
nq.pop();
int r = xx.b, c = xx.c, cn = xx.d;
if (cn) {
for (int i = 0; i < 4; i++) {
int rx = dx[i], ry = dy[i];
if (r + rx < n && r + rx >= 0 && c + ry < m && c + ry >= 0 &&
!b[r + rx][c + ry]) {
b[r + rx][c + ry] = true;
ans[xx.a]++;
if (cn == 1)
qe.push({xx.a, r + rx, c + ry, spd[xx.a]});
else
nq.push({xx.a, r + rx, c + ry, cn - 1});
}
}
}
}
}
for (int i = 1; i <= p; i++) cout << ans[i] << " ";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long mult(long long a, long long b, long long p = (long long)(998244353)) {
return ((a % p) * (b % p)) % p;
}
long long add(long long a, long long b, long long p = (long long)(998244353)) {
return (a % p + b % p) % p;
}
long long neg(long long a, long long p = (long long)(998244353)) {
return (p - (a % p)) % p;
}
long long sub(long long a, long long b, long long p = (long long)(998244353)) {
return add(a, neg(b));
}
long long fpow(long long x, long long y) {
long long res = 1;
x = x % (long long)(998244353);
if (x == 0) return 0;
while (y > 0) {
if (y & 1LL) res = (res * x) % (long long)(998244353);
y = y >> 1LL;
x = (x * x) % (long long)(998244353);
}
return res;
}
long long inv(long long a, long long p = (long long)(998244353)) {
return fpow(a, p - 2);
}
bool sa(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
bool fd(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.first > b.first);
}
bool sd(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second > b.second);
}
long long dx[4] = {0, 0, 1, -1};
long long dy[4] = {1, -1, 0, 0};
bool valid(long long x, long long y, long long n, long long m) {
if (x < 0 || y < 0) {
return false;
} else if (x >= n || y >= m) {
return false;
} else
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long T;
T = 1;
for (long long t = 1; t < T + 1; t++) {
long long n, m, ans, val, x, y;
cin >> n >> m;
long long topic[n + 5], s[n + 5];
vector<long long> adj[n + 5];
unordered_map<long long, long long> mpp;
vector<pair<long long, long long> > v;
for (long long i = 0; i < m; i++) {
cin >> x >> y;
adj[x].emplace_back(y);
adj[y].emplace_back(x);
}
for (long long i = 1; i < n + 1; i++) {
s[i] = 0;
cin >> topic[i];
v.emplace_back(make_pair(topic[i], i));
}
for (long long i = 1; i < n + 1; i++) {
for (auto next : adj[i]) {
if (topic[next] > topic[i]) {
continue;
} else {
if (mpp[topic[next]] == 0) {
s[i] += topic[next];
mpp[topic[next]] = 1;
}
}
}
mpp.clear();
}
for (long long i = 1; i < n + 1; i++) {
val = topic[i] - 1;
ans = (val * (val + 1)) / 2;
if (s[i] != ans) {
cout << "-1";
return 0;
}
}
sort(v.begin(), v.end());
for (long long i = 0; i < (long long)v.size(); i++) {
cout << v[i].second << " ";
}
cout << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
int id[N][N], n, m, t, mo;
int fa[N * N], rn[N * N], t1;
int gf(int x) { return fa[x] == x ? x : fa[x] = gf(fa[x]); }
int ty[N][N];
long long c[N][N], cnt, ans;
int fail;
void merge(int x, int y) {
if (gf(x) == gf(y)) fail = 1;
fa[gf(x)] = gf(y);
}
long long power(long long a, long long b = mo - 2) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mo;
a = a * a % mo;
b >>= 1;
}
return ans;
}
int ths[N], used[N];
void solve(int n) {
if (n > 200) return;
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= n; j++) c[i][j] %= mo, sum += c[i][j];
c[i][i] = (c[i][i] - sum + mo) % mo;
}
n--;
for (int i = 1; i <= n; i++) {
int k = 0;
for (int j = 1; j <= n; j++) {
if (!used[j] && c[j][i]) {
k = j;
break;
}
}
if (!k) return;
ths[i] = k;
used[k] = 1;
long long z = power(c[k][i]);
for (int j = 1; j <= n; j++) {
if (!used[j] && c[j][i]) {
long long p = mo - z * c[j][i] % mo;
for (int u = 1; u <= n; u++) c[j][u] = (c[j][u] + p * c[k][u]) % mo;
}
}
}
long long prd = 1;
for (int i = 1; i <= n; i++) prd = prd * c[ths[i]][i] * -1 % mo;
for (int i = 1; i <= n; i++)
for (int j = 1; j < i; j++)
if (ths[i] < ths[j]) prd *= -1;
ans = (ans + prd + mo) % mo;
}
int main() {
cin >> n >> m >> mo;
for (int i = 1; i <= n + 1; i++)
for (int j = 1; j <= m + 1; j++) id[i][j] = ++t, fa[t] = t;
for (int i = 1; i <= n; i++) {
scanf("\n");
for (int j = 1; j <= m; j++) {
ty[i][j] = getchar();
if (ty[i][j] == '\\') merge(id[i][j], id[i + 1][j + 1]);
if (ty[i][j] == '/') merge(id[i + 1][j], id[i][j + 1]);
}
}
for (int i = 1; i <= n + 1; i++)
for (int j = 1; j <= m + 1; j++) {
if ((i + j) & 1) continue;
if (fa[id[i][j]] == id[i][j]) rn[id[i][j]] = ++t1;
t1 = min(t1, 201);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (ty[i][j] == '*')
;
else
continue;
if ((i + j) & 1) {
c[rn[gf(id[i + 1][j])]][rn[gf(id[i][j + 1])]]++;
c[rn[gf(id[i][j + 1])]][rn[gf(id[i + 1][j])]]++;
} else {
c[rn[gf(id[i + 1][j + 1])]][rn[gf(id[i][j])]]++;
c[rn[gf(id[i][j])]][rn[gf(id[i + 1][j + 1])]]++;
}
}
solve(t1);
memset(c, 0, sizeof c);
memset(ths, 0, sizeof ths);
memset(used, 0, sizeof used);
t1 = 0;
for (int i = 1; i <= n + 1; i++)
for (int j = 1; j <= m + 1; j++) {
if ((i + j) & 1)
;
else
continue;
if (fa[id[i][j]] == id[i][j]) rn[id[i][j]] = ++t1;
t1 = min(t1, 201);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (ty[i][j] == '*')
;
else
continue;
if ((i + j) & 1) {
c[rn[gf(id[i + 1][j + 1])]][rn[gf(id[i][j])]]++;
c[rn[gf(id[i][j])]][rn[gf(id[i + 1][j + 1])]]++;
} else {
c[rn[gf(id[i + 1][j])]][rn[gf(id[i][j + 1])]]++;
c[rn[gf(id[i][j + 1])]][rn[gf(id[i + 1][j])]]++;
}
}
solve(t1);
cout << ans << endl;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int a[55 + 5];
int len;
long long t;
int n;
struct Edge {
int x_1, y_1, x_2, y_2;
} edge[305];
int e_len;
Edge make_edge(int x_1, int y_1, int x_2, int y_2) {
static Edge ans;
ans.x_1 = x_1;
ans.x_2 = x_2;
ans.y_1 = y_1;
ans.y_2 = y_2;
return ans;
}
int x;
void del_1() { edge[++e_len] = make_edge(x - 1, x + 1, x, x + 1); }
void del_2() { edge[++e_len] = make_edge(x + 1, x - 1, x + 1, x); }
void del_3() { edge[++e_len] = make_edge(x - 1, x + 2, x, x + 2); }
void del_4() { edge[++e_len] = make_edge(x + 2, x - 1, x + 2, x); }
int main() {
cin >> t;
while (t) {
a[++len] = t % 6ll;
t /= 6ll;
}
n = (len << 1) + 2;
edge[++e_len] = make_edge(1, 2, 2, 2);
edge[++e_len] = make_edge(2, 1, 2, 2);
for (int i = 2, j = 6; i < n && j < n; i += 2, j += 2) {
edge[++e_len] = make_edge(i, j, i + 1, j);
edge[++e_len] = make_edge(i, j + 1, i + 1, j + 1);
}
if (n > 4) {
edge[++e_len] = make_edge(n - 4, n, n - 3, n);
}
for (int i = 6, j = 2; i < n && j < n; i += 2, j += 2) {
edge[++e_len] = make_edge(i, j, i, j + 1);
edge[++e_len] = make_edge(i + 1, j, i + 1, j + 1);
}
if (n > 4) {
edge[++e_len] = make_edge(n, n - 4, n, n - 3);
}
for (int i = 2; i + 2 < n; i += 2) {
edge[++e_len] = make_edge(i + 2, i, i + 3, i);
edge[++e_len] = make_edge(i + 2, i + 1, i + 3, i + 1);
edge[++e_len] = make_edge(i, i + 2, i, i + 3);
edge[++e_len] = make_edge(i + 1, i + 2, i + 1, i + 3);
}
x = 2;
for (int i = len; i > 0; i--) {
if (a[i] == 0) {
del_1();
del_2();
del_3();
del_4();
} else if (a[i] == 1) {
del_1();
del_2();
del_3();
} else if (a[i] == 2) {
del_1();
del_2();
} else if (a[i] == 3) {
del_2();
del_3();
del_4();
} else if (a[i] == 4) {
del_2();
del_4();
} else {
del_2();
}
x += 2;
}
printf("%d %d\n", n, n);
printf("%d\n", e_len);
for (int i = 1; i <= e_len; i++) {
printf("%d %d %d %d\n", edge[i].x_1, edge[i].y_1, edge[i].x_2, edge[i].y_2);
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n;
int sum, C[4], re[4], A[100050];
void dfs(int z) {
if (!C[z]) return;
A[++n] = z, C[z]--;
if (z == 0)
dfs(1);
else if (z == 1)
dfs(C[0] ? 0 : 2);
else if (z == 2)
dfs(C[3] ? 3 : 1);
else
dfs(2);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for (int i = 0; i < 4; i++) {
cin >> C[i];
re[i] = C[i];
}
sum = C[0] + C[1] + C[2] + C[3];
dfs(C[0] == 0 ? 2 : 0);
if (n != sum) {
n = 0;
for (int i = 0; i < 4; i++) C[i] = re[i];
dfs(C[3] == 0 ? 1 : 3);
if (n != sum)
cout << "NO" << endl;
else {
cout << "YES" << endl;
for (int i = 1; i <= n; i++) cout << A[i] << " ";
cout << endl;
}
} else {
cout << "YES" << endl;
for (int i = 1; i <= n; i++) cout << A[i] << " ";
cout << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "URDL";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
assert(y >= 0);
x += y;
if (x >= mod) x -= mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
struct Point {
long long x, y;
Point() {}
Point(long long x, long long y) : x(x), y(y) {}
long double abs() const { return hypot(x, y); }
long double arg() const { return atan2(y, x); }
Point operator*(double o) const { return Point(x * o, y * o); }
Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); }
Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); }
Point operator/(double o) const { return Point(x / o, y / o); }
bool operator<(const Point& o) const {
return x < o.x || (x == o.x && y < o.y);
}
friend bool operator==(const Point& r1, const Point& r2) {
return r1.x == r2.x && r1.y == r2.y;
}
Point scale(double o) const { return *this * (o / abs()); }
Point rotY() const { return Point(-y, x); }
Point rotX() const { return Point(y, -x); }
long long cross(Point b) const { return x * b.y - b.x * y; }
long long dot(Point b) const { return x * b.x + y * b.y; }
void readin() { scanf("%lld%lld", &x, &y); }
};
Point p[60];
vector<long long> vp;
long long T;
long long f(long long t) {
vector<long long> y;
for (int(i) = 0; (i) < (int)(n); (i)++) {
y.push_back(p[i].y - t);
y.push_back(p[i].y + t + 1);
}
sort(y.begin(), y.end());
long long ss = 0;
for (int(i) = 0; (i) < (int)(n + n); (i)++) {
long long L = -(1LL << 60);
for (int(j) = 0; (j) < (int)(n); (j)++) {
if (p[j].y - t > y[i] || p[j].y + t + 1 <= y[i]) continue;
if (p[j].x + t <= L) continue;
ss += (y[i + 1] - y[i]) * (p[j].x + t - max(L, p[j].x - t - 1));
L = p[j].x + t;
}
}
return ss % 998244353;
}
int i2, i6;
long long s2(long long x) {
if (x <= 0) return 0;
return x % 998244353 * (x + 1) % 998244353 * i2 % 998244353;
}
long long s3(long long x) {
if (x <= 0) return 0;
return x % 998244353 * ((x + 1) % 998244353) % 998244353 *
((x + x + 1) % 998244353) % 998244353 * i6 % 998244353;
}
long long b[5];
void fmain(int tid) {
i2 = fastPow(2, 998244353 - 2);
i6 = fastPow(6, 998244353 - 2);
scanf("%d%lld", &n, &T);
for (int(i) = 0; (i) < (int)(n); (i)++) p[i].readin();
sort(p, p + n);
for (int(i) = 0; (i) < (int)(n); (i)++)
for (int j = i + 1; j < n; j++) {
Point tt = p[i] - p[j];
vp.push_back((max(abs(tt.x), abs(tt.y)) + 1) / 2);
}
vp.push_back(T);
vp.push_back(0);
sort(vp.begin(), vp.end());
int ans = 0;
for (int i = 0; vp[i] < T; i++) {
long long dis = vp[i + 1] - vp[i];
for (int(j) = 0; (j) < (int)(min(3LL, dis)); (j)++) b[j] = f(vp[i] + j);
if (dis <= 3)
for (int(j) = 0; (j) < (int)(dis); (j)++) addmod(ans, b[j]);
else {
addmod(ans, b[0] * dis % 998244353);
long long B = 998244353 - 3 * b[0] * i2 % 998244353 + 2 * b[1] +
998244353 - b[2] * i2 % 998244353;
long long A = b[0] * i2 + 998244353 - b[1] + b[2] * i2 % 998244353;
A %= 998244353;
B %= 998244353;
addmod(ans, A * (dis - 1) % 998244353 * dis % 998244353 * (2 * dis - 1) %
998244353 * i6 % 998244353);
addmod(ans, B * (dis - 1) % 998244353 * dis % 998244353 * i2 % 998244353);
}
}
long long A = T * f(T) % 998244353 + 998244353 - ans;
A %= 998244353;
printf("%lld\n", A);
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
long long int myfind(long long int l, long long int h, long long int x) {
long long int m, ans;
while (l <= h) {
m = ((h - l) >> 1) + l;
if (((m * (m + 1)) >> 1) >= x) {
ans = m;
h = m - 1;
} else
l = m + 1;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int x, t, j, ans;
cin >> x;
x = abs(x);
t = myfind(0ll, INT_MAX, x);
j = (t * (t + 1)) >> 1;
if ((j - x) & 1)
ans = t + 2;
else
ans = t;
j = ((t + 1) * (t + 2)) >> 1;
if ((j - x) & 1)
ans = min(ans, t + 3);
else {
if (((j - x) >> 1) < (t + 1)) ans = min(ans, t + 1);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
long long n, m, i, j, a[N];
set<long long> st;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (0)
;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
st.insert(a[i]);
}
vector<long long> ans;
for (i = 0; i < n; i++) {
ans.clear();
ans.push_back(a[i]);
for (j = 1; j < n; j++) {
long long tail = *(--(ans.end()));
if (tail % 3 == 0 && st.count(tail / 3)) {
ans.push_back(tail / 3);
} else if (st.count(tail * 2)) {
ans.push_back(tail * 2);
} else
break;
}
if (j == n) {
for (i = 0; i < n; i++) {
i == 0 ? cout << ans[i] : cout << " " << ans[i];
}
cout << endl;
break;
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3fffffff;
const int Maxn = 205;
vector<int> edge[Maxn];
pair<int, int> ver[Maxn], cut;
struct Node {
int len, ver;
};
Node dfs(int u, int fa) {
Node res;
res.ver = u, res.len = 0;
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
if (v != fa && make_pair(u, edge[u][i]) != cut &&
make_pair(edge[u][i], u) != cut) {
Node tmp = dfs(v, u);
if (tmp.len + 1 > res.len) {
res.len = tmp.len + 1;
res.ver = tmp.ver;
}
}
}
return res;
}
int foo(int u) {
Node tmp, res;
tmp = dfs(u, -1);
res = dfs(tmp.ver, -1);
return res.len;
}
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) edge[i].clear();
int u, v;
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
ver[i] = make_pair(u, v);
edge[u].push_back(v);
edge[v].push_back(u);
}
int ans = 0;
for (int i = 1; i < n; i++) {
cut = ver[i];
int a = foo(cut.first);
int b = foo(cut.second);
ans = max(ans, a * b);
}
printf("%d\n", ans);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
vector<int> ar(n);
for (auto &i : ar) {
cin >> i;
sum += i;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (!((sum - ar[i]) & 1)) {
++ans;
}
}
cout << ans << '\n';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool div2(T a) {
return !(a & 1);
}
template <class T>
T mod(T a) {
if (a < 0) {
a += 1000000007;
}
return a % 1000000007;
}
template <class T>
T addmod(T a, T b) {
return (mod(a) + mod(b)) % 1000000007;
}
template <class T>
T prodmod(T a, T b) {
return (mod(a) * mod(b)) % 1000000007;
}
template <class T>
T e(T a, T b) {
T res = 1;
T temp = 1;
if (b <= 1) return a;
if (b % 2 == 0)
res = e(a * a, b / 2);
else {
res *= a;
temp = e(a, --b);
}
return res * temp;
}
template <class T>
T emod(T a, T b) {
T res = 1;
T temp = 1;
if (b <= 1) return a;
if (b % 2 == 0)
res = emod(prodmod(a, a), b / 2);
else {
res = prodmod(a, res);
temp = e(a, --b);
}
return prodmod(res, temp);
}
template <class T>
int digits(T a) {
vector<long long> dig = {0,
9,
99,
999,
9999,
99999,
999999,
9999999,
99999999,
999999999,
9999999999,
99999999999,
999999999999,
9999999999999,
99999999999999,
999999999999999,
9999999999999999,
99999999999999999,
999999999999999999,
INT64_MAX};
auto it = lower_bound(dig.begin(), dig.end(), a);
int idx = (it - dig.begin());
return idx;
}
template <class T>
int sumAllInts(T n) {
int a = 1;
int su = 0;
while (n) {
su += n % 10;
n /= 10;
}
return su;
}
template <class T>
void split(char c, T s) {
int n = s.size();
int i = 0;
while ((cout << s[i], i += 1) && (i < n) && (cout << c << ' '))
;
}
template <class T>
long long int fib(T a) {
return ((a * (a + 1)) / 2);
}
template <class T>
long long int nC2(T a) {
return ((a * (a - 1)) / 2);
}
template <class T>
vector<T> &divisors(T n) {
vector<T> *res = new vector<T>;
vector<T> &ret = *res;
int R = sqrt(n);
ret.resize(R);
int start = 0;
int end = R - 1;
for (int i = 2; i <= R; i++) {
if (n % i == 0) {
ret[start] = i;
start++;
T etc = n / i;
if (etc != i) {
ret[end] = etc;
end--;
}
}
}
int s = INT_MAX;
int e = -1;
for (int i = 0; i < R; i++) {
if (!ret[i]) {
s = min(s, i);
e = i;
}
}
if (e - s >= 0) ret.erase(ret.begin() + s, ret.begin() + e + 1);
return ret;
}
template <class T>
void pvec(vector<T> &vec) {
for (auto c : vec) cout << c << " ";
}
bool isPalindrome(string s) {
int n = s.size();
int sum = n - 1;
int mid = n / 2 - 1;
for (int i = 0; i < (int)mid + 1; i++) {
if (s[i] != s[sum - i]) {
return 0;
}
}
return 1;
}
void solve(int &test) {
int k;
string s;
cin >> s;
cin >> k;
int n = (int)s.size();
int f = n / k;
if (n % k != 0) {
cout << "No";
return;
}
for (int i = 0; i < n; i += f) {
for (int j = 0; j < f / 2; j++)
if (s[i + j] != s[i + f - j - 1]) {
cout << "NO";
return;
}
}
cout << "Yes";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int test;
test = 0;
if (test)
cin >> test;
else
test = 1;
while (test--) {
solve(test);
cout << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int MAX = 30;
long long x, num;
long long a[40];
bool check(long long x) {
for (int i = 1; i < MAX; ++i) {
if (a[i] == x) return 1;
}
return 0;
}
vector<int> ans;
int main() {
long long x = 1;
for (int i = 1; i < MAX; ++i) {
x <<= 1;
a[i] = x - 1;
}
scanf("%lld", &x);
while (check(x) == 0) {
int i;
num++;
for (i = MAX - 1; i >= 0; --i) {
if (x >> i) break;
}
x ^= a[i + 1];
ans.push_back(i + 1);
if (check(x)) break;
num++;
x++;
}
printf("%d\n", num);
for (auto i : ans) printf("%d ", i);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, par[1000009], val[1000009], rem[1000009];
tuple<string, int, int> x[1000009];
int dfs(int pos) {
if (get<0>(x[pos]) == "IN") {
return val[pos];
}
if (get<0>(x[pos]) == "NOT") {
int v1 = dfs(get<1>(x[pos]));
val[pos] = (1 ^ v1);
}
if (get<0>(x[pos]) == "AND") {
int v1 = dfs(get<1>(x[pos]));
int v2 = dfs(get<2>(x[pos]));
val[pos] = (v1 & v2);
}
if (get<0>(x[pos]) == "OR") {
int v1 = dfs(get<1>(x[pos]));
int v2 = dfs(get<2>(x[pos]));
val[pos] = (v1 | v2);
}
if (get<0>(x[pos]) == "XOR") {
int v1 = dfs(get<1>(x[pos]));
int v2 = dfs(get<2>(x[pos]));
val[pos] = (v1 ^ v2);
}
return val[pos];
}
void dfs2(int pos) {
if (pos == 1) {
rem[pos] = (1 ^ val[pos]);
} else {
int r = par[pos];
if (get<0>(x[r]) == "NOT") {
rem[pos] = rem[r];
}
if (get<0>(x[r]) == "AND") {
int res = 1;
if (get<1>(x[r]) != pos)
res &= val[get<1>(x[r])];
else
res &= (1 ^ val[get<1>(x[r])]);
if (get<2>(x[r]) != pos)
res &= val[get<2>(x[r])];
else
res &= (1 ^ val[get<2>(x[r])]);
if (res == val[r])
rem[pos] = val[1];
else
rem[pos] = rem[r];
}
if (get<0>(x[r]) == "OR") {
int res = 0;
if (get<1>(x[r]) != pos)
res |= val[get<1>(x[r])];
else
res |= (1 ^ val[get<1>(x[r])]);
if (get<2>(x[r]) != pos)
res |= val[get<2>(x[r])];
else
res |= (1 ^ val[get<2>(x[r])]);
if (res == val[r])
rem[pos] = val[1];
else
rem[pos] = rem[r];
}
if (get<0>(x[r]) == "XOR") {
int res = 0;
if (get<1>(x[r]) != pos)
res ^= val[get<1>(x[r])];
else
res ^= (1 ^ val[get<1>(x[r])]);
if (get<2>(x[r]) != pos)
res ^= val[get<2>(x[r])];
else
res ^= (1 ^ val[get<2>(x[r])]);
if (res == val[r])
rem[pos] = val[1];
else
rem[pos] = rem[r];
}
}
if (get<0>(x[pos]) != "IN") dfs2(get<1>(x[pos]));
if (get<0>(x[pos]) == "AND" || get<0>(x[pos]) == "OR" ||
get<0>(x[pos]) == "XOR")
dfs2(get<2>(x[pos]));
}
int main() {
cin >> n;
par[1] = -1;
vector<int> vec;
for (int i = 1; i <= n; i++) {
string s;
int px, py;
cin >> s;
if (s == "IN") {
cin >> px;
val[i] = px;
vec.push_back(i);
x[i] = make_tuple(s, -1, -1);
}
if (s == "NOT") {
cin >> px;
x[i] = make_tuple(s, px, -1);
par[px] = i;
}
if (s == "AND" || s == "OR" || s == "XOR") {
cin >> px >> py;
x[i] = make_tuple(s, px, py);
par[px] = i;
par[py] = i;
}
}
dfs(1);
dfs2(1);
for (int i = 0; i < vec.size(); i++) cout << rem[vec[i]];
cout << endl;
return 0;
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,pos=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') pos=0;
for(;isdigit(ch);ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return pos?x:-x;
}
const int N = 320005;
int n,m,q;
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define ROF(i,a,b) for(int i=a;i>=b;--i)
#define ll long long
ll sum[N],tot[N];
ll a[N],c[N];
int fa[N][21],vis[N];
int main(){
int q=read(),a0=read(),c0=read();
sum[1]=a0,tot[1]=1ll*a0*c0;
a[1]=a0,c[1]=c0;
vis[0]=1;
FOR(i,1,q){
int opt=read();
if(opt==1){
int p=read();p++;
int now=i+1;
a[now]=read(),c[now]=read();
fa[now][0]=p;sum[now]=sum[p]+a[now];tot[now]=tot[p]+1ll*a[now]*c[now];
FOR(j,1,20){
fa[now][j]=fa[fa[now][j-1]][j-1];
}
}else{
int p=read();p++;int w=read();
int pr=p;
ROF(j,20,0){
if(!vis[fa[pr][j]]) pr=fa[pr][j];
}
if(vis[pr]){
printf("0 0\n");fflush(stdout);continue;
}
ll ns=sum[pr]-a[pr],nt=tot[pr]-1ll*a[pr]*c[pr];
int nx=p;
ROF(j,20,0){
if(sum[fa[nx][j]]-ns>=w) nx=fa[nx][j];
}
if(sum[nx]-ns<=w){
printf("%lld %lld\n",sum[nx]-ns,tot[nx]-nt);
while(!vis[nx]){
vis[nx]=1;nx=fa[nx][0];
}
}else{
printf("%d %lld\n",w,tot[nx]-nt-1ll*c[nx]*(sum[nx]-ns-w));
a[nx]=(sum[nx]-ns-w);
nx=fa[nx][0];
while(!vis[nx]){
vis[nx]=1;nx=fa[nx][0];
}
}
fflush(stdout);
}
}
return 0;
} | 7 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long F[51];
int X[51];
int Y[51];
int k, x, n, m;
char A[2000];
char B[2000];
int main() {
scanf("%d %d %d %d", &k, &x, &n, &m);
for (int a1 = 0; a1 < 3; ++a1) {
for (int an = 0; an < 3; ++an) {
if (n > 1 || a1 == an) {
for (int b1 = 0; b1 < 3; ++b1) {
for (int bm = 0; bm < 3; ++bm) {
if (m > 1 || b1 == bm) {
for (int a = 0; a <= n / 2; ++a) {
for (int b = 0; b <= m / 2; ++b) {
if (n == 2 && a1 == 0 && an == 2 && a == 0) continue;
if (m == 2 && b1 == 0 && bm == 2 && b == 0) continue;
if (2 * a == n && (a1 != 0 || an != 2)) continue;
if (2 * b == m && (b1 != 0 || bm != 2)) continue;
if (n > 1 && 2 * a + 1 == n && a1 != 0 && an != 2) continue;
if (m > 1 && 2 * b + 1 == m && b1 != 0 && bm != 2) continue;
F[1] = a;
F[2] = b;
X[1] = a1, Y[1] = an;
X[2] = b1, Y[2] = bm;
for (int i = 3; i <= k; ++i) {
F[i] =
F[i - 2] + F[i - 1] + (Y[i - 2] == 0 && X[i - 1] == 2);
Y[i] = Y[i - 1];
X[i] = X[i - 2];
}
if (F[k] == x) {
for (int i = 0; i < n; ++i) A[i] = 'B';
for (int i = 0; i < m; ++i) B[i] = 'B';
A[0] = 'A' + a1;
A[n - 1] = 'A' + an;
B[0] = 'A' + b1;
B[m - 1] = 'A' + bm;
for (int i = 0; i < a; ++i)
A[2 * i + (a1 != 0)] = 'A',
A[2 * i + (a1 != 0) + 1] = 'C';
for (int i = 0; i < b; ++i)
B[2 * i + (b1 != 0)] = 'A',
B[2 * i + (b1 != 0) + 1] = 'C';
A[n] = 0, B[m] = 0;
printf("%s\n%s\n", A, B);
return 0;
}
}
}
}
}
}
}
}
}
printf("Happy new year!\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
inline long long mod(long long a, long long m) { return (a % m + m) % m; }
struct Fp {
long long val;
Fp() : val(0) {}
Fp(long long val_) { this->val = mod(val_, MOD); }
Fp operator=(long long val_) {
this->val = mod(val_, MOD);
return *this;
}
inline Fp operator-() { return mod(-val, MOD); }
inline const Fp &operator+=(const Fp &x);
inline const Fp &operator-=(const Fp &x);
inline const Fp &operator*=(const Fp &x);
inline const Fp &operator/=(const Fp &x);
};
ostream &operator<<(ostream &os, Fp x) { return os << x.val; }
istream &operator>>(istream &is, Fp &x) {
is >> x;
return is;
}
bool operator==(Fp x, Fp y) { return mod(x.val, MOD) == mod(y.val, MOD); }
inline Fp operator+(Fp x, Fp y) { return mod(x.val + y.val, MOD); }
inline Fp operator-(Fp x, Fp y) { return mod(x.val - y.val, MOD); }
inline Fp operator*(Fp x, Fp y) { return mod(x.val * y.val, MOD); }
inline Fp operator/(Fp x, Fp y) {
long long a = y.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return x * u;
}
inline Fp power(Fp a, long long n) {
if (n == 0) return Fp(1);
Fp t = power(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
inline const Fp &Fp::operator+=(const Fp &x) {
*this = *this + x;
return *this;
}
inline const Fp &Fp::operator-=(const Fp &x) {
*this = *this - x;
return *this;
}
inline const Fp &Fp::operator*=(const Fp &x) {
*this = *this * x;
return *this;
}
inline const Fp &Fp::operator/=(const Fp &x) {
*this = *this / x;
return *this;
}
int n;
string str;
int main() {
while (cin >> n >> str) {
int a[4];
for (int i = 0; i < 4; ++i) a[i] = 0;
for (int i = 0; i < n; ++i) {
if (str[i] == 'A') ++a[0];
if (str[i] == 'G') ++a[1];
if (str[i] == 'C') ++a[2];
if (str[i] == 'T') ++a[3];
}
Fp res = 1;
sort(a, a + 4);
int num = 1;
if (a[3] == a[2]) ++num;
if (a[3] == a[1]) ++num;
if (a[3] == a[0]) ++num;
res = power(Fp(num), n);
cout << res.val << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline string get() {
char ch[1000010];
scanf("%s", ch);
return ch;
}
const int MAX_LG = 60;
const long long maxn = 1e6 + 100;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long INF = 1e18 + 100;
vector<pair<long long, long long> > g[maxn];
bool mark[maxn];
long long pre[maxn];
long long c[maxn], v[maxn], u[maxn];
long long res = INF;
pair<long long, long long> edges;
pair<long long, long long> fedges;
long long forb;
inline void dfs(long long node, long long preId = -1) {
mark[node] = true;
pre[node] = preId;
for (auto u : g[node]) {
if (mark[u.first]) continue;
dfs(u.first, u.second);
}
}
inline void dfs4(long long node) {
mark[node] = true;
for (auto u : g[node]) {
if (u.second == forb) continue;
if (mark[u.first]) continue;
dfs4(u.first);
}
}
long long cur = 0;
long long l[maxn], h[maxn];
long long cut[maxn];
inline void dfs2(long long node, long long preId = -1) {
h[node] = l[node] = cur++;
mark[node] = true;
for (auto u : g[node]) {
if (u.second == preId || u.second == forb) continue;
if (mark[u.first]) {
l[node] = min(l[node], h[u.first]);
if (l[u.first] > h[node]) cut[u.second] = true;
} else {
dfs2(u.first, u.second);
l[node] = min(l[node], l[u.first]);
if (l[u.first] > h[node]) {
cut[u.second] = true;
}
}
}
}
bool mark2[maxn][2];
long long di[maxn][2];
inline void bfs(long long node, long long fl = 0, long long h = 0) {
mark2[node][fl] = true;
di[node][fl] = 0;
queue<long long> q;
q.push(node);
while (q.empty() == false) {
long long v = q.front();
q.pop();
for (auto u : g[v]) {
if (mark2[u.first][fl]) continue;
if (u.second == forb) continue;
mark2[u.first][fl] = true;
di[u.first][fl] = di[v][fl] + 1;
q.push(u.first);
}
}
}
inline bool check(long long s, long long t, long long v, long long u) {
if (di[v][0] == di[u][0] + 1 && di[u][1] == di[v][1] + 1) return 1;
return 0;
}
int32_t main() {
long long n = in(), m = in();
long long s = in(), t = in();
for (long long i = 0; i < m; i++) {
v[i] = in(), u[i] = in(), c[i] = in();
g[v[i]].push_back({u[i], i});
g[u[i]].push_back({v[i], i});
}
dfs(s);
if (!mark[t]) {
return cout << 0 << "\n" << 0 << "\n", 0;
}
long long node = t;
while (node != s) {
long long edgeId = pre[node];
long long node2;
if (v[edgeId] == node)
node2 = u[edgeId];
else
node2 = v[edgeId];
long long cost = c[edgeId];
edges.first = edgeId;
edges.second = -1;
forb = edgeId;
for (long long i = 0; i <= n; i++)
mark[i] = mark2[i][0] = mark2[i][1] = false;
dfs4(s);
if (!mark[t]) {
if (res > cost) res = cost, fedges = edges;
} else {
cur = 0;
for (long long i = 0; i <= n; i++)
mark[i] = mark2[i][0] = mark2[i][1] = false;
for (long long i = 0; i < m; i++) cut[i] = false;
dfs2(s);
bfs(s, 0);
bfs(t, 1);
for (long long i = 0; i < m; i++) {
if (((check(s, t, v[i], u[i])) || check(s, t, u[i], v[i])) && cut[i] &&
i != edgeId) {
long long cost2 = cost + c[i];
if (cost2 < res) {
edges.second = i;
res = cost2, fedges = edges;
}
}
}
}
node = node2;
}
if (res == INF) {
cout << -1 << "\n";
} else {
cout << res << "\n";
if (fedges.second == -1)
cout << 1 << "\n";
else
cout << 2 << "\n";
cout << fedges.first + 1 << " ";
if (fedges.second != -1) cout << fedges.second + 1;
cout << "\n";
}
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t = 1;
while (t--) {
int n, m;
cin >> n >> m;
int i;
while (true) {
for (i = 1; i <= n; i++) {
if (m >= i)
m -= i;
else
break;
}
if (i <= n) break;
}
cout << m << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s, t;
cin >> s >> t;
long long count = 0;
long long n1 = s.length();
long long n2 = t.length();
if (n1 != n2)
cout << "NO"
<< "\n";
else {
for (long long i = 0; i < n1; i++)
if (s[i] == t[n1 - 1 - i]) count++;
if (count == n1)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 13e5 + 1000, oo = 1e9;
const long long M = 998244353;
long long last;
long long two(long long v) {
long long l = 1, r = v;
while (r - l > 1) {
long long m = (l + r) >> 1;
if (m > (v + m - 1) / m || m * m > v)
r = m;
else
l = m;
}
last = r;
return l;
}
long long three(long long v) {
long long l = 1, r = last;
while (r - l > 1) {
long long m = (l + r) >> 1;
if (m * m > (v + m - 1) / m || m * m * m > v)
r = m;
else
l = m;
}
last = r;
return l;
}
set<long long> used;
set<long long> should;
map<long long, int> cnt;
long long gcd(long long a, long long b) {
if (!b)
return a;
else
return gcd(b, a % b);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<long long> vl(n);
for (int i = 0; i < n; ++i) cin >> vl[i];
for (int i = 0; i < n; ++i) {
long long v2 = two(vl[i]);
long long v3 = three(vl[i]);
long long v4 = two(v2);
if (v4 * v4 * v4 * v4 == vl[i]) {
vl[i] = 1;
cnt[v4] += 4;
should.insert(v4);
} else if (v2 * v2 == vl[i]) {
vl[i] = 1;
cnt[v2] += 2;
should.insert(v2);
} else if (v3 * v3 * v3 == vl[i]) {
vl[i] = 1;
cnt[v3] += 3;
should.insert(v3);
}
}
used = should;
while (should.size()) {
long long d = *should.begin();
if (d == 1) {
should.erase(d);
continue;
}
for (int z = 0; z < n; ++z) {
if (vl[z] % d == 0) {
vl[z] /= d;
cnt[d]++;
if (!used.count(vl[z])) {
should.insert(vl[z]);
used.insert(vl[z]);
}
}
}
should.erase(d);
}
long long ans = 1;
for (int i = 0; i < n; ++i) {
if (vl[i] == 1) continue;
for (int j = i + 1; j < n; ++j) {
if (vl[j] == vl[i] || vl[j] == 1) continue;
if (gcd(vl[i], vl[j]) != 1) {
long long v = gcd(vl[i], vl[j]);
should.insert(v);
used.insert(v);
while (should.size()) {
long long d = *should.begin();
if (d == 1) {
should.erase(d);
continue;
}
for (int z = 0; z < n; ++z) {
if (vl[z] % d == 0) {
vl[z] /= d;
cnt[d]++;
if (!used.count(vl[z])) {
should.insert(vl[z]);
used.insert(vl[z]);
}
}
}
should.erase(d);
}
}
}
}
stable_sort(vl.begin(), vl.end());
for (int i = 0; i < n; ++i) {
if (i + 1 == n || vl[i + 1] != vl[i]) {
if (vl[i] == 1) continue;
int cnt = 2;
for (int j = i - 1; j >= 0; --j)
if (vl[j] == vl[i]) cnt++;
ans = ((ans * cnt) % M * cnt) % M;
}
}
for (auto p : cnt) ans = (ans * (p.second + 1)) % M;
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
int n;
scanf("%d\n", &n);
char c[5200];
scanf("%s", c);
string s(c);
int pt = 0;
int cnt = 0;
std::vector<int> t;
for (int i = 0; i < n; i++)
if (i == 0 || s[i] != s[i - 1]) {
cnt++;
t.push_back(int(s[i] - 'a'));
}
std::vector<std::vector<int> > d(26, std::vector<int>(n + 1, 0));
std::vector<std::vector<int> > last(26, std::vector<int>(n + 1, 0));
for (int i = 0; i < n + 1; i++) last[t[0]][i] = mod - 1;
for (int i = 0; i < n + 1; i++) {
d[t[0]][i] = 1;
}
for (int sym = 1; sym < cnt; sym++) {
int cur = t[sym];
std::vector<int> cum(26, 0);
for (int i = 1; i < n + 1; i++) {
long long cand = 1;
for (int j = 0; j < 26; j++) {
if (j != cur) {
cand += cum[j];
cum[j] += d[j][i];
if (cum[j] >= mod) cum[j] -= mod;
}
}
cand %= mod;
d[cur][i] += cand + last[cur][i];
if (d[cur][i] >= mod) d[cur][i] -= mod;
last[cur][i] = mod - d[cur][i];
cum[cur] += d[cur][i];
cum[cur] %= mod;
}
}
long long ans = 0;
for (int i = 0; i < 26; i++) ans += d[i][n];
cout << ans % mod;
}
| 8 |
#include <bits/stdc++.h>
int main() {
long long n, x, a, b, c, d;
scanf("%lld", &n);
a = (n / 2 + n / 3 + n / 5 + n / 7);
b = (n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35);
c = (n / 30 + n / 42 + n / 70 + n / 105);
d = n / 210;
x = n - a + b - c + d;
printf("%lld", x);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int t;
string n;
const long long P2LIM = (long long)1e18;
int check(string sour, string tar) {
int token = 0;
int ss = 0;
int tt = 0;
while (ss < sour.size() && tt < tar.size()) {
if (sour[ss] == tar[tt]) {
token++;
ss++;
tt++;
} else {
ss++;
}
}
return sour.size() + tar.size() - 2 * token;
}
int main() {
cin >> t;
vector<string> nums;
for (long long i = 1; i < P2LIM; i *= 2) {
nums.push_back(to_string(i));
}
while (t--) {
cin >> n;
int res = n.size() + 1;
for (string d : nums) {
res = min(res, check(n, d));
}
cout << res << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, s, p = 1;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> x >> y;
s += ((x - p) % m) + (y - x + 1);
p = y + 1;
}
cout << s << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string s;
bool samoglasnik(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
int main(void) {
cin >> s;
for (int i = 0; i < s.size(); ++i) {
if (i + 2 < s.size() && !samoglasnik(s[i]) && !samoglasnik(s[i + 1]) &&
!samoglasnik(s[i + 2]) && (s[i] != s[i + 1] || s[i] != s[i + 2])) {
cout << s[i] << s[i + 1] << " ";
i++;
} else {
cout << s[i];
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
int a[N], n;
long long f[N];
long long sub(int l, int r) { return f[r + 1] - f[l]; }
double cc(int l, int x) {
return 1.0 * (sub(l - x, l) + sub(n - x, n - 1) - 1LL * a[l] * (2 * x + 1)) /
(2 * x + 1);
}
pair<double, int> cal(int pos) {
int l = 1, r = min(pos, n - 1 - pos);
while (l < r - 3) {
int mid = (l + r) >> 1;
int midmid = (l + mid) >> 1;
if (cc(pos, midmid) < cc(pos, mid))
l = midmid;
else
r = mid;
}
pair<double, int> p = make_pair(cc(pos, l), l);
for (int i = l; i <= r; i++) p = max(p, make_pair(cc(pos, i), i));
return p;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", a + i);
sort(a, a + n);
if (n <= 2) {
printf("%d\n", n);
for (int i = 0; i < n; i++) printf("%d ", a[i]);
return 0;
}
for (int i = 0; i < n; i++) f[i + 1] = 1LL * a[i] + f[i];
pair<double, int> ans(0, -1);
int pos;
for (int i = 1; i < n - 1; i++) {
pair<double, int> t = cal(i);
if (t > ans) {
pos = i;
ans = t;
}
}
if (ans.first == 0.0) {
puts("1");
printf("%d\n", a[0]);
} else {
int x = ans.second;
printf("%d\n", 2 * ans.second + 1);
for (int i = pos - x; i <= pos; i++) printf("%d ", a[i]);
for (int i = n - x; i < n; i++) printf("%d ", a[i]);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
i = 1;
cout << n;
while (i <= n - 1) {
cout << " " << i;
i++;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int deg[maxn], xx[maxn];
queue<int> Q;
bool mark[maxn];
vector<pair<int, int> > v;
void add(int fi, int se) { v.push_back(make_pair(fi, se)); }
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> deg[i] >> xx[i];
if (deg[i] == 1) {
Q.push(i);
mark[i] = true;
}
}
while (Q.size()) {
int x = Q.front();
Q.pop();
if (deg[x] == 0) continue;
add(x, xx[x]);
deg[xx[x]]--;
xx[xx[x]] ^= x;
deg[x]--;
if (deg[xx[x]] == 1 and !mark[xx[x]]) {
Q.push(xx[x]);
mark[xx[x]] = true;
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++)
cout << v[i].first << " " << v[i].second << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000000 + 7;
long long powe(long long x, long long y) {
x = x % mod, y = y % (mod - 1);
long long ans = 1;
while (y > 0) {
if (y & 1) {
ans = (1ll * x * ans) % mod;
}
y >>= 1;
x = (1ll * x * x) % mod;
}
return ans;
}
void fun() {}
long long dp[4][200002];
long long n;
set<long long> s1, s2, s3;
long long fun(long long sel, long long ind) {
if (ind > n) return 0;
if (dp[sel][ind] != -1) return dp[sel][ind];
long long mx = INT_MAX;
if (sel == 1) {
long long cnt = 0;
if (s1.find(ind) == s1.end()) cnt++;
mx = min(mx, cnt + fun(1, ind + 1));
mx = min(mx, cnt + fun(2, ind + 1));
mx = min(mx, cnt + fun(3, ind + 1));
}
if (sel == 2) {
long long cnt = 0;
if (s2.find(ind) == s2.end()) cnt++;
mx = min(mx, cnt + fun(2, ind + 1));
mx = min(mx, cnt + fun(3, ind + 1));
}
if (sel == 3) {
long long cnt = 0;
if (s3.find(ind) == s3.end()) cnt++;
mx = min(mx, cnt + fun(3, ind + 1));
}
return dp[sel][ind] = mx;
}
signed main() {
fun();
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long k1, k2, k3;
cin >> k1 >> k2 >> k3;
n = k1 + k2 + k3;
memset(dp, -1, sizeof(dp));
long long num;
for (long long i = 0; i < k1; i++) {
cin >> num;
s1.insert(num);
}
for (long long i = 0; i < k2; i++) {
cin >> num;
s2.insert(num);
}
for (long long i = 0; i < k3; i++) {
cin >> num;
s3.insert(num);
}
long long ans = fun(1, 1);
ans = min(ans, fun(2, 1));
ans = min(ans, fun(3, 1));
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long MAXN = 1e6 + 7;
long long n, m, a, b;
vector<long long> fact(MAXN);
vector<long long> rfact(MAXN);
inline long long pw(long long a, long long n);
inline long long sum(long long x, long long y);
inline long long mult(long long x, long long y);
inline long long A(long long n, long long k);
inline long long F(long long x, long long y);
inline long long C(long long n, long long k);
inline long long pw(long long a, long long n) {
long long res = 1;
while (n) {
if (n % 2 == 0) {
a = mult(a, a);
n >>= 1;
} else {
res = mult(res, a);
n--;
}
}
return res;
}
inline long long sum(long long x, long long y) {
x += y;
if (x < 0) x += M;
if (x >= M) x -= M;
return x;
}
inline long long mult(long long x, long long y) { return (1LL * x * y) % M; }
inline long long A(long long n, long long k) {
if (k < 0 || n < k) return 0;
return mult(fact[n], rfact[n - k]);
}
inline long long F(long long x, long long y) {
return mult(y, pw(x, x - y - 1 + M - 1));
}
inline long long C(long long n, long long k) {
if (k < 0 || n < k) return 0;
return mult(fact[n], mult(rfact[k], rfact[n - k]));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
fact[0] = 1;
for (long long i = 1; i < MAXN; ++i) fact[i] = mult(fact[i - 1], i);
rfact[MAXN - 1] = pw(fact[MAXN - 1], M - 2);
for (long long i = MAXN - 2; i >= 0; --i)
rfact[i] = mult(rfact[i + 1], i + 1);
cin >> n >> m >> a >> b;
long long ans = 0;
for (int i = 1; i < n && i <= m; ++i) {
long long res = mult(A(n - 2, i - 1), F(n, i + 1));
res = mult(res, C(m - 1, i - 1));
res = mult(res, pw(m, n - i - 1));
ans = sum(ans, res);
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 3e5 + 5, maxK = 3e5 + 5, INF = 0x3f3f3f3f;
int n, k, fa[2 * maxN], cnt[2 * maxN];
char str[maxN];
vector<int> light[maxN];
int Get(int now) {
if (fa[now] == now) return now;
return fa[now] = Get(fa[now]);
}
int GetMin(int u) { return min(cnt[Get(u)], cnt[Get(u + k)]); }
void Merge(int u, int v) {
u = Get(u), v = Get(v);
if (u != v) {
fa[u] = v;
cnt[v] += cnt[u];
}
}
int main() {
scanf("%d%d%s", &n, &k, str + 1);
for (int i = 1; i <= k; ++i) {
int nb;
scanf("%d", &nb);
while (nb--) {
int id;
scanf("%d", &id);
light[id].push_back(i);
}
fa[i] = i;
fa[i + k] = i + k;
cnt[i] = 1;
}
int inf = 2 * k + 1;
fa[inf] = inf;
cnt[inf] = INF;
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (light[i].size() == 1) {
ans -= GetMin(light[i][0]);
Merge(light[i][0] + k * (str[i] == '0'), inf);
ans += GetMin(light[i][0]);
} else if (light[i].size() == 2) {
if (str[i] == '1') {
if (Get(light[i][0]) != Get(light[i][1])) {
ans -= GetMin(light[i][0]) + GetMin(light[i][1]);
Merge(light[i][0], light[i][1]);
Merge(light[i][0] + k, light[i][1] + k);
ans += GetMin(light[i][0]);
}
} else {
if (Get(light[i][0]) != Get(light[i][1] + k)) {
ans -= GetMin(light[i][0]) + GetMin(light[i][1]);
Merge(light[i][0], light[i][1] + k);
Merge(light[i][0] + k, light[i][1]);
ans += GetMin(light[i][0]);
}
}
}
printf("%d\n", ans);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i = 0, may = 0, cont = 0;
cin >> n;
int a[n + 1];
for (int i = 1; i <= n; i++) cin >> a[i];
i = 1;
while (i <= n) {
if (a[i] > may) may = a[i];
if (i >= may) {
cont++;
i++;
} else
i++;
}
cout << cont << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int a[100];
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x % m == 0)
a[i] = (x / m) - 1;
else
a[i] = (x / m);
}
int ans = 0, pos = 0;
for (int i = 0; i < n; i++)
if (a[i] >= ans) {
ans = a[i];
pos = i + 1;
}
cout << pos << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int countCandy[5010][5010];
int minCandy[5010];
int endTime[5010];
int main(void) {
int N, M;
scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++) {
int s, e;
scanf("%d %d", &s, &e);
s--;
e--;
countCandy[s][e]++;
}
for (int i = 0; i < N; i++) {
minCandy[i] = -1;
for (int j = 0; j < N; j++) {
if (countCandy[i][(i + j) % N]) {
minCandy[i] = j;
break;
}
}
int sum = 0;
for (int j = 0; j < N; j++) sum += countCandy[i][(i + j) % N];
if (minCandy[i] >= 0) endTime[i] = (sum - 1) * N + minCandy[i];
}
for (int i = 0; i < N; i++) {
int maxTime = 0;
for (int j = 0; j < N; j++) {
int total = endTime[(i + j) % N] + j;
if (!endTime[(i + j) % N]) continue;
if (total > maxTime) maxTime = total;
}
printf("%d ", maxTime);
}
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
set<long long> st[30];
long long Ans;
int main() {
string s;
cin >> s;
s = "*" + s;
for (int i = 1; i < s.size(); i++) {
st[s[i] - 'a'].insert(i);
}
long long q;
cin >> q;
while (q--) {
long long t;
scanf("%I64d ", &t);
if (t == 1ll) {
long long pos;
char c;
scanf("%I64d %c", &pos, &c);
st[s[pos] - 'a'].erase(pos);
st[c - 'a'].insert(pos);
s[pos] = c;
} else {
long long l, r;
scanf("%I64d%I64d", &l, &r);
Ans = 0;
for (int i = 0; i < 26; i++) {
auto p = st[i].lower_bound(l);
if (p == st[i].end()) continue;
if (*p <= r) {
Ans++;
}
}
printf("%I64d\n", Ans);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#pragma GCC target("popcnt")
using namespace std;
int n, S;
long long mu[3000010], F[3000010], ans = 0;
const long long p = 1e9 + 7;
bool np[3000010];
vector<int> pr;
template <class T>
void read(T &x) {
char ch = x = 0;
bool fl = false;
while (!isdigit(ch)) fl |= ch == '-', ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x = fl ? -x : x;
}
long long pw(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % p;
a = a * a % p, b >>= 1;
}
return ret;
}
void sieve() {
mu[1] = 1;
for (int i = 2; i <= S; i++) {
if (!np[i]) pr.push_back(i), mu[i] = p - 1;
for (int j = 0; j < pr.size(); j++) {
if (i * pr[j] > S) break;
np[i * pr[j]] = true;
if (i % pr[j] == 0) break;
mu[i * pr[j]] = p - mu[i];
}
}
for (int i = 1; i <= S; i++) {
mu[i] = (mu[i] + mu[i - 1]) % p;
}
}
long long f(int m) {
if (m <= S) return mu[m];
if (F[n / m]) return F[n / m];
long long ret = 1;
for (int i = 2, pos; i <= m; i = pos + 1) {
pos = m / (m / i);
ret = (ret - 1LL * (pos - i + 1) * f(m / i) % p + p) % p;
}
return F[n / m] = ret;
}
int main() {
read(n), S = max((int)pow(n, 2.0 / 3), 1), sieve();
for (int i = 2, pos; i <= n; i = pos + 1) {
pos = n / (n / i);
ans = (ans + (f(pos) - f(i - 1) + p) * (n / i) % p * pw(n - n / i, p - 2)) %
p;
}
printf("%lld\n", (1 - ans + p) % p);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first != b.first) {
return a.first < b.first;
} else {
return a.second < b.second;
}
}
bool is_vowel(char c) {
if (c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u')
return true;
else
return false;
}
bool is_prime(long long n) {
if (n == 1) return false;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
long long min(long long a, long long b) {
if (a < b)
return a;
else
return b;
}
void solve() {
int n;
cin >> n;
bool flag = true;
int x, f, l;
for (int i = 0; i < n; i++) {
cin >> x;
if (i == n - 1) l = x;
if (i == 0) f = x;
}
if (f > l) flag = false;
if (flag)
cout << "yes";
else
cout << "no";
}
int main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
cout << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int N, A[101010];
template <typename T>
struct segtree {
public:
segtree() : N(0), data(nullptr) {}
segtree(int len) {
for (int i = 0;; ++i) {
if (len <= (1 << i)) {
N = 1 << i;
break;
}
}
data = new T[2 * N];
for (int i = 0; i < 2 * N; ++i) data[i] = T();
}
T at(int p) { return data[p + N]; }
T query(int L, int R) {
T left = T();
T right = T();
L += N;
R += N;
while (L < R) {
if (L & 1) left = left + data[L++];
if (R & 1) right = data[--R] + right;
L >>= 1;
R >>= 1;
}
return left + right;
}
void update(int p, const T &val) {
p += N;
data[p] = val;
p >>= 1;
while (p) {
data[p] = data[p * 2] + data[p * 2 + 1];
p >>= 1;
}
}
T top() { return data[1]; }
private:
int N;
T *data;
};
struct Pat {
long long lo, loo, o, ol, ool, loool;
Pat() : lo(0), loo(0), o(0), ol(0), ool(0), loool(0) {}
Pat(long long lo, long long ol)
: lo(lo), loo(0), o(1), ol(ol), ool(0), loool(0) {}
};
inline Pat operator+(const Pat &l, const Pat &r) {
Pat ret;
ret.lo = (l.lo + r.lo) % 1000000007;
ret.loo = (l.loo + l.lo * r.o + r.loo) % 1000000007;
ret.o = (l.o + r.o) % 1000000007;
ret.ol = (l.ol + r.ol) % 1000000007;
ret.ool = (r.ool + l.o * r.ol + l.ool) % 1000000007;
ret.loool = (l.loool + r.loool + l.lo * r.ool + l.loo * r.ol) % 1000000007;
return ret;
}
segtree<Pat> ent[101010];
int idx[101010], totcnt[101010];
int lcomp[101010], rcomp[101010];
void pset(int p) { ent[A[p]].update(idx[p], Pat(lcomp[p], rcomp[p])); }
void unset(int p) { ent[A[p]].update(idx[p], Pat()); }
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i) scanf("%d", A + i);
vector<int> As;
for (int i = 0; i < N; ++i) As.push_back(A[i]);
sort(As.begin(), As.end());
As.erase(unique(As.begin(), As.end()), As.end());
for (int i = 0; i < N; ++i)
A[i] = lower_bound(As.begin(), As.end(), A[i]) - As.begin();
{
segtree<int> seg(N);
for (int i = 0; i < N; ++i) {
lcomp[i] = seg.query(0, A[i] + 1);
seg.update(A[i], seg.at(A[i]) + 1);
}
}
{
segtree<int> seg(N);
for (int i = N - 1; i >= 0; --i) {
rcomp[i] = seg.query(0, A[i] + 1);
seg.update(A[i], seg.at(A[i]) + 1);
}
}
for (int i = 0; i < N; ++i) totcnt[i] = 0;
for (int i = 0; i < N; ++i) {
idx[i] = totcnt[A[i]]++;
}
for (int i = 0; i < N; ++i)
if (totcnt[i] >= 1) {
ent[i] = segtree<Pat>(totcnt[i]);
}
for (int i = 0; i < N; ++i) pset(i);
long long ans = 0;
for (int i = 0; i < As.size(); ++i)
((ans) = ((ans) + (ent[i].top().loool) % 1000000007) % 1000000007);
int M;
scanf("%d", &M);
for (; M--;) {
int t, x;
scanf("%d%d", &t, &x);
--x;
((ans) = ((ans) + (1000000007 - ent[A[x]].top().loool) % 1000000007) %
1000000007);
if (t == 1)
unset(x);
else
pset(x);
((ans) = ((ans) + (ent[A[x]].top().loool) % 1000000007) % 1000000007);
printf("%lld\n", ans);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
std::ios::sync_with_stdio(false);
long long n, m;
cin >> n >> m;
if (n == 0 and m == 0) {
cout << 0 << " " << 0;
} else if (n == 0 and m != 0) {
cout << "Impossible";
} else {
long long nim = 0;
long long xam = 0;
if (m == 0) {
cout << n << " " << n;
} else {
xam = (n - 1) + m;
if (n >= m) {
nim = n;
} else if (n < m) {
nim = m;
}
cout << nim << " " << xam;
}
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
const ll INF=1e18;
int t,n,dep[N],fa[N];
ll a[N],dp[N],mn[N],mx[N],Min[N],Max[N],levelMin[N],levelMax[N];
vector<int>e[N];
void dfs(int u,int d){
levelMin[d]=min(levelMin[d],a[u]);
levelMax[d]=max(levelMax[d],a[u]);
dep[u]=d;
for(auto &v:e[u]){
Min[u]=min(Min[u],a[v]);
Max[u]=max(Max[u],a[v]);
dfs(v,d+1);
}
}
void bfs(int u){
ll ans=0;
queue<int>q;
q.push(u);
while(!q.empty()){
int x=q.front();q.pop();
dp[x]=max(dp[x],mn[dep[x]-1]-a[x]);
dp[x]=max(dp[x],mx[dep[x]-1]+a[x]);
if(fa[x]){
dp[x]=max(dp[x],dp[fa[x]]+levelMin[dep[x]]-a[x]);
dp[x]=max(dp[x],dp[fa[x]]-levelMin[dep[x]]+a[x]);
dp[x]=max(dp[x],dp[fa[x]]+levelMax[dep[x]]-a[x]);
dp[x]=max(dp[x],dp[fa[x]]-levelMin[dep[x]]+a[x]);
}
//printf("x:%d dp:%lld\n",x,dp[x]);
ans=max(ans,dp[x]);
mn[dep[x]]=max(mn[dep[x]],dp[x]+Min[x]);
mx[dep[x]]=max(mx[dep[x]],dp[x]-Min[x]);
mn[dep[x]]=max(mn[dep[x]],dp[x]+Max[x]);
mx[dep[x]]=max(mx[dep[x]],dp[x]-Max[x]);
for(auto &y:e[x]){
q.push(y);
}
}
printf("%lld\n",ans);
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<=n;++i){
e[i].clear();
mn[i]=mx[i]=-INF;
levelMin[i]=Min[i]=INF;
levelMax[i]=Max[i]=0;
dp[i]=0;
}
for(int i=2;i<=n;++i){
scanf("%d",&fa[i]);
e[fa[i]].push_back(i);
}
for(int i=2;i<=n;++i){
scanf("%lld",&a[i]);
}
dfs(1,1);
bfs(1);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 2e5 + 5;
int a[maxn];
int cnt[maxn];
int pre[maxn];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
cnt[a[i]]++;
}
sort(a + 1, a + 1 + n);
pre[0] = 0;
for (int i = 1; i <= 2e5; i++) {
pre[i] = pre[i - 1] + cnt[i];
}
int j = 1;
int ans = 0, ansl = 0, ansr = 0;
for (int i = 1; i <= 2e5; i++) {
if (cnt[i] == 0) continue;
if (j < i) j = i;
if (j == i) {
while (cnt[j + 1] > 1) {
j++;
}
if (cnt[j + 1] > 0) {
j++;
}
} else {
if (cnt[j] == 1) {
} else {
while (cnt[j + 1] > 1) {
j++;
}
if (cnt[j + 1] > 0) {
j++;
}
}
}
int num = pre[j] - pre[i - 1];
if (num > ans) {
ans = num;
ansl = i, ansr = j;
}
}
cout << ans << endl;
vector<int> ret;
if (ansl == ansr) {
for (int i = 1; i <= cnt[ansl]; i++) {
ret.push_back(ansl);
}
} else {
for (int i = 1; i <= cnt[ansl]; i++) {
ret.push_back(ansl);
}
for (int i = ansl + 1; i < ansr; i++) {
for (int j = 2; j <= cnt[i]; j++) {
ret.push_back(i);
}
}
for (int i = 1; i <= cnt[ansr]; i++) {
ret.push_back(ansr);
}
for (int i = ansr - 1; i > ansl; i--) {
ret.push_back(i);
}
}
for (int i = 0; i < ret.size(); i++) {
printf("%d%c", ret[i], i == ret.size() - 1 ? '\n' : ' ');
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long dp[300010][41];
int main() {
int N, A[300010];
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", A + i);
}
for (int i = 0; i <= N; i++)
for (int j = 0; j <= 40; j++) dp[i][j] = 2000000000000000000LL;
dp[0][0] = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j <= 40; j++) {
int nextj = min(40, j + A[i] / 1000);
dp[i + 1][nextj] = min(dp[i + 1][nextj], dp[i][j] + A[i]);
for (int k = 1; k <= min(j, A[i] / 100); k++) {
dp[i + 1][j - k] = min(dp[i + 1][j - k], dp[i][j] + A[i] - k * 100);
}
}
}
long long ans = 2000000000000000000LL;
for (int i = 0; i <= 40; i++) {
ans = min(ans, dp[N][i]);
}
printf("%lld\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
const double PI = 3.14159;
long long n, m, k, a[N], c, t, ans;
string s, x;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m >> k;
while (k--) {
ans += n * 2;
ans += (m - 2) * 2;
m -= 4;
n -= 4;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[500010];
vector<int> vec[500010];
int p[500010];
vector<int> e[500010];
bool vis[500010];
bool ok[500010];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
vec[a[i]].push_back(i);
}
int N = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < vec[i].size(); j++) {
int u = vec[i][j];
int cnt = 0;
for (int k = 0; k < e[u].size(); k++) {
int v = e[u][k];
if (vis[v] && !ok[a[v]]) {
ok[a[v]] = true;
cnt++;
}
}
if (cnt != i - 1 || ok[a[u]]) {
printf("-1\n");
return 0;
}
for (int k = 0; k < e[u].size(); k++) {
int v = e[u][k];
ok[a[v]] = false;
}
p[++N] = u;
vis[u] = true;
}
}
for (int i = 1; i <= n; i++) printf("%d ", p[i]);
printf("\n");
return 0;
}
| 4 |
#include <iostream>
using namespace std;
void solve(){
int n, k;
cin >> n >> k;
int x_cord[n], y_cord[n];
for(int i = 0; i < n; i++)
{
cin >> x_cord[i] >> y_cord[i];
}
int ans = -1;
for(int i = 0; i < n; i++)
{
bool flag = true;
for(int j = 0; j < n; j++){
if(j != i && abs(x_cord[i] - x_cord[j]) + abs(y_cord[i] - y_cord[j]) > k)
{
flag = false;
break;
}
}
if(flag)
{
ans = 1;
break;
}
}
cout << ans << endl;
}
int main() {
int t;
cin >> t;
while(t--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool cmp_second(const pair<T, U> &a, const pair<T, U> &b) {
return a.second < b.second;
}
pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {
return make_pair(a.first + b.first, a.second + b.second);
}
pair<int, int> operator-(const pair<int, int> &a, const pair<int, int> &b) {
return make_pair(a.first - b.first, a.second - b.second);
}
pair<int, int> &operator+=(pair<int, int> &a, const pair<int, int> &b) {
a.first += b.first;
a.second += b.second;
return a;
}
pair<int, int> &operator-=(pair<int, int> &a, const pair<int, int> &b) {
a.first -= b.first;
a.second -= b.second;
return a;
}
inline int sg(int x) { return x ? (x > 0 ? 1 : -1) : 0; }
class Segment {
public:
int r1, r2, c1, c2;
void input() {
scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
if (r1 > r2) swap(r1, r2);
if (c1 > c2) swap(c1, c2);
}
};
int rn, cn, k;
vector<Segment> rseg, cseg;
bool cmpr(Segment a, Segment b) {
if (a.r1 != b.r1) return a.r1 < b.r1;
return a.c1 < b.c1;
}
bool cmpc(Segment a, Segment b) {
if (a.c1 != b.c1) return a.c1 < b.c1;
return a.r1 < b.r1;
}
const int inf = 1000000500;
inline int cut(vector<pair<int, int> > segs, int todo = inf) {
int rb = 0, ret = 0;
for (int i = 0; i < segs.size(); i++) {
int a = max(segs[i].first, rb) - rb;
if (ret + a >= todo) {
return rb + (todo - ret);
}
ret += a;
rb = max(rb, segs[i].second);
}
return ret;
}
inline int getnim(int ans = -1) {
int nim = 0;
vector<int> rseen, cseen;
for (int i = 0, j = 0; i < rseg.size(); i = j) {
vector<pair<int, int> > segs;
for (j = i; j < rseg.size() && rseg[i].r1 == rseg[j].r1; j++)
segs.push_back(make_pair(rseg[j].c1, rseg[j].c2));
segs.push_back(make_pair(cn, cn));
int totl = cut(segs);
if (ans >= 0 && (totl ^ ans) < totl) {
int todo = totl - (totl ^ ans);
int x = cut(segs, todo);
printf("%d %d %d %d\n", rseg[i].r1, 0, rseg[i].r1, x);
return -1;
}
nim ^= totl;
rseen.push_back(rseg[i].r1);
}
for (int i = 0, j = 0; i < cseg.size(); i = j) {
vector<pair<int, int> > segs;
for (j = i; j < cseg.size() && cseg[i].c1 == cseg[j].c1; j++)
segs.push_back(make_pair(cseg[j].r1, cseg[j].r2));
segs.push_back(make_pair(rn, rn));
int totl = cut(segs);
if (ans >= 0 && (totl ^ ans) < totl) {
int todo = totl - (totl ^ ans);
int x = cut(segs, todo);
printf("%d %d %d %d\n", 0, cseg[i].c1, x, cseg[i].c1);
return -1;
}
nim ^= totl;
cseen.push_back(cseg[i].c1);
}
if ((rn - 1 - rseen.size()) & 1) nim ^= cn;
if ((cn - 1 - cseen.size()) & 1) nim ^= rn;
if (ans >= 0 && rseen.size() < rn - 1 && (cn ^ ans) < cn) {
int todo = cn - (cn ^ ans);
int z;
for (z = 1; z <= rseen.size(); z++)
if (rseen[z - 1] != z) break;
printf("%d %d %d %d\n", z, 0, z, todo);
return -1;
}
if (ans >= 0 && cseen.size() < cn - 1 && (rn ^ ans) < rn) {
int todo = rn - (rn ^ ans);
int z;
for (z = 1; z <= cseen.size(); z++)
if (cseen[z - 1] != z) break;
printf("%d %d %d %d\n", 0, z, todo, z);
return -1;
}
return nim;
}
bool solve() {
sort(rseg.begin(), rseg.end(), cmpr);
sort(cseg.begin(), cseg.end(), cmpc);
int nim = getnim();
if (!nim) return 0;
puts("FIRST");
getnim(nim);
return 1;
}
int main(void) {
scanf("%d %d %d", &rn, &cn, &k);
for (int i = 0; i < k; i++) {
Segment s;
s.input();
if (s.r1 == s.r2)
rseg.push_back(s);
else
cseg.push_back(s);
}
if (!solve()) puts("SECOND");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<int> a;
vector<int> del;
int x, y;
int i, j, k;
int64_t z, sum;
z = -1e12;
cin >> n;
a.resize(n + 1);
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 1; i < n; i++) {
for (j = n; j > i; j--) {
if (a[i] != a[j]) continue;
sum = 0;
sum += a[i];
sum += a[j];
for (k = i + 1; k < j; k++) {
if (a[k] > 0) {
sum += a[k];
}
}
if (sum > z) {
z = sum;
x = i;
y = j;
}
}
}
for (i = 1; i < x; i++) {
del.push_back(i);
}
for (i = y + 1; i <= n; i++) {
del.push_back(i);
}
for (i = x + 1; i < y; i++) {
if (a[i] < 0) {
del.push_back(i);
}
}
cout << z << " " << del.size() << endl;
for (i = 0; i < del.size(); i++) {
if (i == del.size() - 1)
printf("%d\n", del[i]);
else
printf("%d ", del[i]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int N, l[300001], r[300001];
long long K;
struct segment {
int l, r, time;
inline bool operator<(const segment &b) const { return l < b.l; }
};
std::set<segment> S;
std::map<int, int> _op[300001];
std::vector<std::pair<int, int> > op[300001];
struct ans_t {
long long count, sum;
inline ans_t operator+(const ans_t &b) const {
return {count + b.count, sum + b.sum};
}
};
long long add_tag[300001];
ans_t check(long long mid) {
for (int i = 1; i <= N; i++) add_tag[i] = 0;
ans_t res = {0, 0}, cur = {0, 0};
long long this_ans = 0;
for (int i = 1; i <= N; i++) {
for (auto &e : op[i]) {
cur.sum += std::min<long long>(cur.count, e.first) * e.second;
add_tag[e.first] += e.second;
if (e.first > cur.count) this_ans += e.second;
}
while (this_ans >= mid) {
cur.sum += this_ans;
this_ans -= add_tag[++cur.count];
}
res = res + cur;
}
return res;
}
int main() {
scanf("%d%lld", &N, &K);
for (int i = 1; i <= N; i++) scanf("%d%d", l + i, r + i);
S.insert({1, 1000000000, 0});
for (int i = 1; i <= N; i++) {
std::set<segment>::iterator L = S.lower_bound({l[i] + 1, 0, 0}),
R = S.lower_bound({r[i], 0, 0});
L--;
std::vector<segment> inside(L, R);
S.erase(L, R);
for (auto &e : inside)
if (e.time) _op[i][e.time] -= e.r - e.l;
if (inside.front().l < l[i]) {
S.insert({inside.front().l, l[i], inside.front().time});
if (inside.front().time)
_op[i][inside.front().time] += l[i] - inside.front().l;
}
if (inside.back().r > r[i]) {
S.insert({r[i], inside.back().r, inside.back().time});
if (inside.back().time)
_op[i][inside.back().time] += inside.back().r - r[i];
}
S.insert({l[i], r[i], i});
_op[i][i] += r[i] - l[i];
}
for (int i = 1; i <= N; i++) op[i].assign(_op[i].begin(), _op[i].end());
int l = 1, r = 1000000000;
ans_t ans;
while (l < r) {
int m = l + r + 1 >> 1;
ans = check(m);
if (ans.count >= K)
l = m;
else
r = m - 1;
}
ans = check(l);
printf("%lld\n", ans.sum - (ans.count - K) * l);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
struct ww {
double x, y, z;
inline void read() { scanf("%lf%lf%lf", &x, &y, &z); }
inline void print() { printf("%.12lf %.12lf %.12lf\n", x, y, z); }
inline ww operator-(const ww &A) { return (ww){x - A.x, y - A.y, z - A.z}; }
inline ww operator+(const ww &A) { return (ww){x + A.x, y + A.y, z + A.z}; }
inline void operator/=(const double &A) { x /= A, y /= A, z /= A; }
inline void operator*=(const double &A) { x *= A, y *= A, z *= A; }
inline double com() { return x * x + y * y + z * z; }
inline double suan(const ww &A) {
ww B = {x - A.x, y - A.y, z - A.z};
return sqrt(B.com());
}
} a[10010], O, W;
int i, j, k, n, m;
double vp, vs, ti;
inline bool ju(int x, double L, double R) {
ww A = W;
A *= R;
A = A + a[x];
double t = O.suan(A) / vp;
return t - (1e-10) < L + R;
}
inline void work(int x, double L, double R) {
W = a[x + 1] - a[x];
W /= R;
double l = L, r = L + R, mid;
for (; l + 1e-10 < r;) {
mid = (l + r) / 2;
if (ju(x, L, mid - L))
r = mid;
else
l = mid;
}
printf("YES\n%.12lf\n", r);
W *= r - L;
W = W + a[x];
W.print();
exit(0);
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n + 1; i++) a[i].read();
scanf("%lf%lf", &vp, &vs);
O.read();
for (i = 1; i <= n; i++) {
double t = a[i].suan(a[i + 1]) / vs;
double r = O.suan(a[i + 1]) / vp;
if (r - (1e-10) < ti + t) work(i, ti, t);
ti += t;
}
printf("NO\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, x, y, s;
cin >> n;
s = 0;
for (i = 0; i < n; i++) {
scanf("%d %d", &x, &y);
if (s + x <= 500) {
s += x;
printf("A");
} else {
s -= y;
printf("G");
}
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<long long> v[2];
long long n;
string s;
cin >> n >> s;
for (int i = 0; i < n; i++) {
v[s[i] - 'A'].push_back(i);
}
long long ans = 0;
for (int i = 0; i < v[0].size(); i++) {
if (i == 0) {
if (s[i] == 'B') {
ans += v[0][i];
}
} else {
ans += (v[0][i] - v[0][i - 1] - 1);
}
}
for (int i = 0; i < v[1].size(); i++) {
if (i == 0) {
if (s[i] == 'A') {
ans += v[1][i];
}
} else {
ans += (v[1][i] - v[1][i - 1] - 1);
}
}
for (int i = 0; i < v[0].size(); i++) {
if (i == v[0].size() - 1) {
if (s[n - 1] == 'B') {
ans += n - v[0][i] - 1;
}
} else {
ans += (v[0][i + 1] - v[0][i] - 1);
}
}
for (int i = 0; i < v[1].size(); i++) {
if (i == v[1].size() - 1) {
if (s[n - 1] == 'A') {
ans += n - v[1][i] - 1;
}
} else {
ans += (v[1][i + 1] - v[1][i] - 1);
}
}
for (int i = 0; i < n - 1; i++) {
if (s[i] != s[i + 1]) {
ans--;
}
}
long long res = ((n) * (n - 1)) / 2 - ans;
cout << res << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long Rnumbs, Lnumbs;
int L[100001], R[100001];
int n;
bool loop() {
Lnumbs = 0;
Rnumbs = 0;
for (int i = 1; i <= n; i++) {
cin >> L[i] >> R[i];
Lnumbs += L[i];
Rnumbs += R[i];
}
int k = 0;
long long maxs = abs(Lnumbs - Rnumbs);
for (int i = 1; i <= n; i++) {
if (maxs < abs((Lnumbs - L[i] + R[i]) - (Rnumbs - R[i] + L[i]))) {
k = i;
maxs = abs((Lnumbs - L[i] + R[i]) - (Rnumbs - R[i] + L[i]));
}
}
cout << k << endl;
return true;
}
int main() {
cin.tie(0);
cin.sync_with_stdio(false);
while (cin >> n) loop();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
char ch = getchar();
long long f = 1, x = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return f * x;
}
const long long maxn = 1e5 + 10;
long long n, h[maxn], cnt, m;
struct edge {
long long v, next;
} e[maxn << 1];
void addedge(long long u, long long v) {
e[++cnt].v = v;
e[cnt].next = h[u];
h[u] = cnt;
}
void insert(long long u, long long v) {
addedge(u, v);
addedge(v, u);
}
stack<long long> g[maxn];
struct answers {
long long a, b, c;
} ans[maxn];
long long vis[maxn], tot;
bool no;
void dfs(long long u, long long fa) {
vis[u] = 1;
for (long long i = h[u]; i; i = e[i].next) {
long long v = e[i].v;
if (!vis[v])
dfs(v, u);
else if (v != fa) {
g[v].push(u);
}
}
while (g[u].size() >= 2) {
ans[++tot].a = g[u].top();
ans[tot].b = u;
g[u].pop();
ans[tot].c = g[u].top();
g[u].pop();
}
if (g[u].size() == 1) {
if (fa == 0) {
no = 1;
return;
}
long long x = g[u].top();
ans[++tot].a = x;
ans[tot].b = u;
ans[tot].c = fa;
g[u].pop();
} else {
g[fa].push(u);
}
}
signed main() {
n = read();
m = read();
if (m & 1) {
cout << "No solution\n";
return 0;
}
for (long long i = 1; i <= m; i++) {
insert(read(), read());
}
dfs(1, 0);
if (no) {
cout << "No solution\n";
return 0;
}
for (long long i = 1; i <= tot; i++) {
cout << ans[i].a << " " << ans[i].b << " " << ans[i].c << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
bool adj[18][18];
i64 dp[1 << 18][18];
i64 lenCnt[19][1 << 18];
template <typename T>
void subset_transform(int N, T a[1 << 18]) {
int L = 1 << N;
for (int k = 1; k < L; k *= 2) {
for (int i = 0; i < L; i += 2 * k) {
for (int j = 0; j < k; j++) {
a[i + j + k] += a[i + j];
}
}
}
}
template <typename T>
void inverse_superset_transform(int N, T a[1 << 18]) {
int L = 1 << N;
for (int k = 1; k < L; k *= 2) {
for (int i = 0; i < L; i += 2 * k) {
for (int j = 0; j < k; j++) {
a[i + j] -= a[i + j + k];
}
}
}
}
i64 ans[1 << 17];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int N;
cin >> N;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
for (int j = 0; j < N; j++) {
adj[i][j] = s[j] - '0';
}
}
for (int i = 0; i < N; i++) {
dp[1 << i][i] = 1;
}
for (int m = 1; m < (1 << N); m++) {
for (int i = 0; i < N; i++) {
if (!(m & (1 << i))) continue;
for (int j = 0; j < N; j++) {
if (m & (1 << j)) continue;
if (adj[i][j]) {
dp[m | (1 << j)][j] += dp[m][i];
}
}
}
}
for (int m = 1; m < (1 << N); m++) {
int pc = __builtin_popcount(m);
for (int i = 0; i < N; i++) {
lenCnt[pc][m] += dp[m][i];
}
}
for (int l = 1; l <= N; l++) {
subset_transform(N, lenCnt[l]);
}
map<vector<int>, i64> memo;
for (int p = 0; p < (1 << (N - 1)); p++) {
int cnt = 0;
vector<int> partition;
for (int i = 0; i < N; i++) {
cnt++;
if (!(p & (1 << i))) {
partition.push_back(cnt);
cnt = 0;
}
}
sort(partition.begin(), partition.end());
if (!memo.count(partition)) {
i64 res = 0;
for (int m = 1; m < (1 << N); m++) {
i64 v = 1;
for (int l : partition) {
v *= lenCnt[l][m];
}
if ((N - __builtin_popcount(m)) % 2 == 0) {
res += v;
} else {
res -= v;
}
}
memo[partition] = res;
}
ans[p] = memo[partition];
}
inverse_superset_transform(N - 1, ans);
for (int p = 0; p < (1 << (N - 1)); p++) {
cout << ans[p] << " \n"[p == (1 << (N - 1)) - 1];
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m, x;
cin >> n >> m >> x;
vector<vector<long long>> g(n, vector<long long>(m));
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) cin >> g[i][j];
long long ansv = 0, ansc = 0;
for (long long i = 0; i < m; i++) {
long long ind = 0, mxv = 0, mxc = 0;
while (ind < n and g[ind][i] == 0) ind++;
for (long long j = ind; j < min(ind + x, n); j++) mxv += (g[j][i] == 1);
for (long long j = 0; j < n; j++) {
long long v = 0, c = 0;
for (long long k = 0; k <= j; k++) c += (g[k][i] == 1);
long long kx = j + 1;
while (kx < n and g[kx][i] == 0) kx++;
for (long long k = kx; k < min(kx + x, n); k++) v += (g[k][i] == 1);
if (v > mxv) {
mxv = v;
mxc = c;
} else if (v == mxv)
mxc = min(mxc, c);
}
ansv += mxv;
ansc += mxc;
}
cout << ansv << " " << ansc << "\n";
return 0;
}
| 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.