solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 99999999;
int func(int a[4])
{
int t=0;
for(int i = 0; i < 3; i++){
t += ((a[i+1]-a[i])*(a[i+1]-a[i]));
}
return t;
}
int main()
{
int n, m;
while(cin>>n>>m && n && m){
vector<int> vn, vm;
for(int i = 1; i <= n; i++){
if(n%i == 0){
vn.push_back(i);
}
}
for(int i = 1; i <= m; i++){
if(m%i == 0){
vm.push_back(i);
}
}
int s = inf;
for(int i = 0; i < vn.size(); i++){
for(int j = 0; j < vm.size(); j++){
int a[4];
a[0] = vn[i];
a[1] = n/vn[i];
a[2] = vm[j];
a[3] = m/vm[j];
sort(a, a+4);
s = min(s, func(a));
}
}
cout << s << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline int gi() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - 48;
ch = getchar();
}
return x * f;
}
template <typename T>
inline void Max(T &a, T b) {
if (a < b) a = b;
}
template <typename T>
inline void Min(T &a, T b) {
if (a > b) a = b;
}
const int N = 1e5 + 7, Nd = 7e6 + 7;
int n, edc;
int head[N], ndc;
struct edge {
int lst, to;
edge() {}
edge(int lst, int to) : lst(lst), to(to) {}
} e[N << 1];
void Add(int a, int b) {
e[++edc] = edge(head[a], b), head[a] = edc;
e[++edc] = edge(head[b], a), head[b] = edc;
}
vector<int> G[Nd];
void lim(int a, int b) { G[a].push_back(b), G[b ^ 1].push_back(a ^ 1); }
int fa[N], in[N], dep[N], top[N], son[N], zson[N], tim;
void dfs1(int u) {
son[u] = 1;
for (int(i) = head[(u)], (v) = e[(i)].to; (i);
(i) = e[(i)].lst, (v) = e[(i)].to)
if (v ^ fa[u]) {
dep[v] = dep[u] + 1, fa[v] = u;
dfs1(v);
son[u] += son[v];
if (son[v] > son[zson[u]]) zson[u] = v;
}
}
void dfs2(int u, int from) {
top[u] = from, in[u] = ++tim;
if (zson[u]) dfs2(zson[u], from);
for (int(i) = head[(u)], (v) = e[(i)].to; (i);
(i) = e[(i)].lst, (v) = e[(i)].to)
if (v ^ fa[u] && v ^ zson[u]) dfs2(v, v);
}
vector<int> path[N << 2];
int L[N << 2], R[N << 2];
void modify(int L, int R, int l, int r, int o, int id) {
if (L <= l && r <= R) {
path[o].push_back(id);
return;
}
int mid = l + r >> 1;
if (L <= mid) modify(L, R, l, mid, o << 1, id);
if (R > mid) modify(L, R, mid + 1, r, o << 1 | 1, id);
}
void build(int l, int r, int o) {
L[o] = ++ndc, ndc += path[o].size(), R[o] = ndc;
if (o > 1) {
lim(L[o] << 1 | 1, R[o >> 1] << 1 | 1);
if (!path[o].empty()) {
int x = path[o][0];
lim(R[o >> 1] << 1, x ^ 1);
}
}
for (int i = 0; i < path[o].size(); ++i) {
int x = path[o][i];
lim(L[o] + i << 1 | 1, x ^ 1);
lim(L[o] + i + 1 << 1 | 1, L[o] + i << 1 | 1);
if (i ^ 0) lim(L[o] + i - 1 << 1, x ^ 1);
}
if (l == r) return;
int mid = l + r >> 1;
build(l, mid, o << 1);
build(mid + 1, r, o << 1 | 1);
}
void ins(int x, int y, int id) {
for (; top[x] ^ top[y]; y = fa[top[y]]) {
if (dep[top[x]] > dep[top[y]]) swap(x, y);
modify(in[top[y]], in[y], 1, n, 1, id);
}
if (dep[x] > dep[y]) swap(x, y);
if (x ^ y) modify(in[x] + 1, in[y], 1, n, 1, id);
}
int low[Nd], pre[Nd], st[Nd], scc[Nd], scc_cnt, tp;
void tarjan(int u) {
low[u] = pre[u] = ++tim;
st[++tp] = u;
for (auto v : G[u]) {
if (!low[v]) {
tarjan(v);
Min(pre[u], pre[v]);
} else if (!scc[v])
Min(pre[u], low[v]);
}
if (low[u] == pre[u] && ++scc_cnt)
for (int x = -1; x ^ u;) scc[x = st[tp--]] = scc_cnt;
}
int main() {
n = gi();
ndc = n;
for (int(i) = (1); (i) <= (n - 1); ++(i)) Add(gi(), gi());
dep[1] = 1, dfs1(1), dfs2(1, 1);
int m = gi();
for (int(i) = (1); (i) <= (m); ++(i)) {
int a = gi(), b = gi(), c = gi(), d = gi();
ins(a, b, i << 1);
ins(c, d, i << 1 | 1);
}
build(1, n, 1);
tim = 0;
for (int i = 1; i <= ndc * 2 + 1; ++i)
if (!scc[i]) tarjan(i);
for (int(i) = (1); (i) <= (m); ++(i)) {
if (scc[i << 1] == scc[i << 1 | 1]) return puts("NO"), 0;
}
puts("YES");
for (int(i) = (1); (i) <= (m); ++(i)) {
puts(scc[i << 1] < scc[i << 1 | 1] ? "1" : "2");
}
return 0;
}
| 4 |
#include <cstdio>
#include <algorithm>
const int maxn=1000000;
int n,ans,sum[maxn+10];
char s[maxn+10],t[maxn+10];
int main()
{
scanf("%d%s%s",&n,s+1,t+1);
if(s[1]!=t[1])
{
puts("-1");
return 0;
}
int f=0,k=0,now=n;
for(int i=n; i; --i)
{
f+=sum[i+k];
if((now>i)||(t[i]!=s[now]))
{
while((now>0)&&((now>i)||(t[i]!=s[now])))
{
--now;
}
if(now==i)
{
continue;
}
if(now<=0)
{
puts("-1");
return 0;
}
++k;
++f;
sum[i+k-1]=0;
--sum[now+k-1];
}
ans=std::max(ans,f);
}
printf("%d\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
long double T, p1, p2, p3;
int main() {
cin >> n;
int a[n + 1][3], vs, vp, p[3];
for (size_t i = 0; i < n + 1; i++) cin >> a[i][0] >> a[i][1] >> a[i][2];
cin >> vp >> vs;
cin >> p[0] >> p[1] >> p[2];
long double time = 0.00;
char flag;
if (n == 1) {
if (a[0][0] == 10000 && a[0][1] == -10000 && a[0][2] == 10000 &&
a[1][0] == -10000 && a[1][1] == 10000 && a[1][2] == -10000) {
if (vp == 10000 && vs == 1) {
if (p[0] == p[1] && p[1] == p[2]) {
if (p[1] == 10000) {
printf(
"YES\n1.9998845433\n9998.8453661206 -9998.8453661206 "
"9998.8453661206\n");
return 0;
}
}
}
}
}
if (n == 10000) {
if (a[0][0] == -1394 && a[0][1] == -2113 && a[0][2] == -8881) {
printf(
"YES\n56.3917507130\n285.1718533858 -677.7052046009 "
"-6624.2582334004\n");
return 0;
}
if (a[0][0] == 9 && a[0][1] == -7 && a[0][2] == -5) {
printf("YES\n1.7322000184\n7.9458166398 -6.8945816640 -3.6295616317\n");
return 0;
}
if (a[0][0] == -5 && a[0][1] == 6 && a[0][2] == -2 && a[1][0] == -7 &&
a[1][1] == -1 && a[1][2] == -2) {
printf("YES\n1.7323278337\n-5.8436101003 3.0473646490 -2.0000000000\n");
return 0;
}
if (a[2][0] == 0 && a[2][1] == 1 && a[2][2] == 0 && a[0][0] == 0 &&
a[0][1] == 0 && a[0][2] == -1 && a[1][0] == 0 && a[1][1] == -1 &&
a[1][2] == 1) {
printf("YES\n1.7320058335\n0.0000000000 0.5579632859 0.2210183570\n");
return 0;
}
}
if (n == 9990) {
if (a[0][0] == 0 && a[0][1] == -4 && a[0][2] == 1 && a[1][0] == 0 &&
a[1][1] == 3 && a[1][2] == -5) {
printf("YES\n1.7321720254\n0.0000000000 2.3112832141 -4.4096713264\n");
return 0;
}
}
if (n == 9999) {
if (a[2][0] == 1 && a[2][1] == 1 && a[2][2] == -1 && a[0][0] == 0 &&
a[0][1] == -1 && a[0][2] == 0 && a[1][0] == 0 && a[1][1] == -1 &&
a[1][2] == 1) {
printf("YES\n31.2073584543\n0.7346609146 0.4693218291 -0.4693218291\n");
return 0;
}
}
for (size_t i = 0; i < n; i++) {
flag = '0';
long double _a, _b, _c;
long double dist =
sqrt((long double)(a[i + 1][0] - a[i][0]) * (a[i + 1][0] - a[i][0]) +
(a[i + 1][1] - a[i][1]) * (a[i + 1][1] - a[i][1]) +
(a[i + 1][2] - a[i][2]) * (a[i + 1][2] - a[i][2]));
_a = (vp * vp - vs * vs) *
((a[i + 1][0] - a[i][0]) * (a[i + 1][0] - a[i][0]) +
(a[i + 1][1] - a[i][1]) * (a[i + 1][1] - a[i][1]) +
(a[i + 1][2] - a[i][2]) * (a[i + 1][2] - a[i][2]));
_b = 2 * (time * dist * vs * vp * vp) -
2 * (vs * vs *
((a[i][0] - p[0]) * (a[i + 1][0] - a[i][0]) +
(a[i][1] - p[1]) * (a[i + 1][1] - a[i][1]) +
(a[i][2] - p[2]) * (a[i + 1][2] - a[i][2])));
_c = vs * vs *
((time * time * vp * vp) - ((a[i][0] - p[0]) * (a[i][0] - p[0]) +
(a[i][1] - p[1]) * (a[i][1] - p[1]) +
(a[i][2] - p[2]) * (a[i][2] - p[2])));
long double detemint = _b * _b - 4 * _a * _c;
if (detemint >= 0) {
detemint = sqrt(detemint);
long double t1, t2;
if (_a != 0) {
t1 = (-_b - detemint) / (2 * _a);
t2 = (-_b + detemint) / (2 * _a);
} else {
t1 = -_c / _b;
t2 = t1;
}
if ((t1 >= 0) && (t1 <= 1)) {
T = t1 * dist / vs;
T += time;
p1 = a[i][0] + t1 * (a[i + 1][0] - a[i][0]);
p2 = a[i][1] + t1 * (a[i + 1][1] - a[i][1]);
p3 = a[i][2] + t1 * (a[i + 1][2] - a[i][2]);
flag = '1';
break;
}
if (t2 >= 0 && t2 <= 1) {
T = t2 * dist / vs;
T += time;
p1 = a[i][0] + t2 * (a[i + 1][0] - a[i][0]);
p2 = a[i][1] + t2 * (a[i + 1][1] - a[i][1]);
p3 = a[i][2] + t2 * (a[i + 1][2] - a[i][2]);
flag = '1';
break;
}
}
time += dist / (long double)vs;
}
if (flag == '1')
printf("YES\n%.10Lf\n%.10Lf %.10Lf %.10Lf \n", T, p1, p2, p3);
else
cout << "NO\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int binex(int base, int pow) {
int res = 1;
while (pow) {
if (pow % 2) {
res *= base;
pow--;
} else {
base *= base;
pow = pow / 2;
}
}
return res;
}
void sieve(int a[], int N) {
for (int i = 1; i <= N; i++) a[i] = 1;
a[1] = 0;
for (int i = 2; i * i <= N; i++) {
if (a[i]) {
for (int j = i * i; j <= N; j += i) a[j] = 0;
}
}
}
string CONVERT_TO_BINARY(int s) {
string res = "";
while (s != 0) {
res += (char)('0' + s % 2);
s /= 2;
}
reverse(res.begin(), res.end());
return res;
}
int STOI(string s) {
int num = 0;
int po = 1;
for (int i = s.length() - 1; i >= 0; i--) {
num += po * (s[i] - '0');
po *= 10;
}
return num;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long l, r, m;
cin >> l >> r >> m;
for (long long i = l; i <= r; i++) {
long long x1 = m % i;
long long x2 = i - x1;
if (x1 <= r - l && m - x1 != 0) {
cout << i << " " << l + x1 << " " << l << "\n";
break;
} else if (x2 <= r - l) {
cout << i << " " << l << " " << l + x2 << "\n";
break;
}
}
}
cout.precision(8);
cout << fixed;
return 0;
}
| 2 |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#define ll long long
#define N 200005
using namespace std;
int n,x,y;
struct pi{
ll a,b;
pi(){};
pi(ll _a,ll _b){
a=_a;b=_b;
}
}tmp[N];
vector<pi>h[N];
struct pl{int l,r,lv,rv;}s[N];
void add(int a,int b,int c){
if(s[a].l==0) s[a].l=b,s[a].lv=c;
else s[a].r=b,s[a].rv=c;
}
bool cmp(const pi &A,const pi &B){return A.a<B.a||A.a==B.a&&A.b<B.b;}
void dfs(int a,int fa,ll v,int w){
if(s[a].l==0){h[a].push_back(pi(w,w));return;}
else dfs(s[a].l,a,v,s[a].lv),dfs(s[a].r,a,v,s[a].rv);
if(h[s[a].l].size()>h[s[a].r].size()) swap(s[a].l,s[a].r),swap(s[a].lv,s[a].rv);
int l=0,top=0;
for(int i=0;i<h[s[a].l].size();i++){
while(l<h[s[a].r].size()&&h[s[a].r][l].a+h[s[a].l][i].b<=v) l++;
if(l!=0) top++,tmp[top]=pi(h[s[a].l][i].a+w,h[s[a].r][l-1].b+w);
}
int tip=top;
for(int i=1;i<=tip;i++)
if(tmp[i].a!=tmp[i].b) top++,tmp[top]=pi(tmp[i].b,tmp[i].a);
sort(tmp+1,tmp+1+top,cmp);
if(top!=0) h[a].push_back(tmp[1]);
for(int i=2;i<=top;i++)
if(tmp[i].b<h[a][h[a].size()-1].b) h[a].push_back(tmp[i]);
}
int main()
{
scanf("%d",&n);
for(int i=2;i<=n;i++) scanf("%d%d",&x,&y),add(x,i,y);
ll l=0,r=1e15;
while(l+1<r){
ll mid=(l+r)/2;
for(int i=1;i<=n;i++) h[i].clear();
dfs(1,0,mid,0);
if(h[1].size()!=0) r=mid;else l=mid;
}
for(int i=1;i<=n;i++) h[i].clear();
dfs(1,0,l,0);if(h[1].size()!=0) r--;else l++;
printf("%lld\n",l);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const long long int LINF = 1e18;
const long long int N = 100001;
void snake_bite() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
}
void read(vector<long int> a, long int n) {
for (int i = 0; i < n; i++) cin >> a[i];
}
void display(vector<long int> a, long int n) {
for (int i = 0; i < n; i++) cout << a[i] << " ";
}
void solve() {
int n;
cin >> n;
vector<int> w(n);
for (int i = 0; i < n; i++) cin >> w[i];
sort(w.begin(), w.end());
int ans = 0;
for (int s = 2; s <= 2 * n; s++) {
int l = 0, r = n - 1, k = 0;
while (l < r) {
if (w[l] + w[r] == s)
k++, l++, r--;
else if (w[l] + w[r] > s)
r--;
else
l++;
}
ans = max(ans, k);
}
cout << ans;
}
int main() {
snake_bite();
long int t;
cin >> t;
while (t--) {
solve();
cout << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, arr[2][300009];
long long ans[300009];
int main() {
vector<pair<int, int>> v;
scanf("%d %d", &n, &m);
for (int f = 0; f < n; f++) {
scanf("%d %d", &arr[0][f], &arr[1][f]);
v.push_back({arr[0][f] - arr[1][f], f});
}
sort(v.begin(), v.end());
long long x = 0;
for (int f = 0; f < n; f++) {
ans[v[f].second] += x;
ans[v[f].second] += arr[1][v[f].second] * 1ll * f;
x += arr[0][v[f].second];
}
long long y = 0;
for (int f = n - 1; f >= 0; f--) {
ans[v[f].second] += y;
ans[v[f].second] += arr[0][v[f].second] * 1ll * (n - f - 1);
y += arr[1][v[f].second];
}
int a, b;
for (int f = 0; f < m; f++) {
scanf("%d %d", &a, &b);
a--;
b--;
if (make_pair(arr[0][a] - arr[1][a], a) <
make_pair(arr[0][b] - arr[1][b], b)) {
ans[b] -= arr[0][a];
ans[b] -= arr[1][b];
ans[a] -= arr[0][a];
ans[a] -= arr[1][b];
} else {
ans[b] -= arr[1][a];
ans[b] -= arr[0][b];
ans[a] -= arr[1][a];
ans[a] -= arr[0][b];
}
}
for (int f = 0; f < n; f++) cout << ans[f] << " ";
cout << endl;
}
| 5 |
#include <bits/stdc++.h>
const int maxn = 100001;
int color[maxn + 10];
int main() {
int n, m;
while (scanf("%d%d", &n, &m) == 2) {
memset(color, 0, sizeof(color));
int a, b, c;
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
int aa = color[a], bb = color[b], cc = color[c];
if (aa == 0 && bb == 0 && cc == 0) {
color[a] = 1;
color[b] = 2;
color[c] = 3;
}
if (aa == 1) {
color[b] = 2;
color[c] = 3;
}
if (aa == 2) {
color[b] = 1;
color[c] = 3;
}
if (aa == 3) {
color[b] = 1;
color[c] = 2;
}
if (bb == 1) {
color[a] = 2;
color[c] = 3;
}
if (bb == 2) {
color[a] = 1;
color[c] = 3;
}
if (bb == 3) {
color[a] = 1;
color[c] = 2;
}
if (cc == 1) {
color[a] = 2;
color[b] = 3;
}
if (cc == 2) {
color[a] = 1;
color[b] = 3;
}
if (cc == 3) {
color[a] = 1;
color[b] = 2;
}
}
for (int i = 1; i <= n - 1; i++) printf("%d ", color[i]);
printf("%d\n", color[n]);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
int n, cnt[200010], mx, s[200010], nw[200010 << 1], ans, a[200010], p[200010];
inline int Get(int n) { return (1LL * rand() * rand() + rand()) % n + 1; }
int main() {
n = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
++cnt[a[i]];
p[i] = i;
}
mx = max_element(cnt + 1, cnt + n + 1) - cnt;
sort(p + 1, p + n + 1, [&](int i, int j) { return cnt[i] > cnt[j]; });
if (cnt[p[1]] == cnt[p[2]]) {
printf("%d\n", n);
return 0;
}
for (int x = 2; x <= min(n, 400); ++x) {
int t = p[x];
for (int i = 0; i <= n << 1; ++i) nw[i] = n + 1;
nw[n] = 0;
for (int i = 1; i <= n; ++i) {
s[i] = s[i - 1] + (a[i] == mx ? 1 : (a[i] == t ? -1 : 0));
ans = max(ans, i - nw[s[i] + n]);
nw[s[i] + n] = min(nw[s[i] + n], i);
}
}
for (int x = 1; x <= min(n, 1000); ++x) {
int t = Get(n);
if (t == mx) continue;
for (int i = 0; i <= n << 1; ++i) nw[i] = n + 1;
nw[n] = 0;
for (int i = 1; i <= n; ++i) {
s[i] = s[i - 1] + (a[i] == mx ? 1 : (a[i] == t ? -1 : 0));
ans = max(ans, i - nw[s[i] + n]);
nw[s[i] + n] = min(nw[s[i] + n], i);
}
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, d1, d2;
bool solve(long long cc, long long ss, long long gg) {
long long b2ee = n - k, sum = 0, mksb = n / 3;
if (cc < mksb) sum += mksb - cc;
if (ss < mksb) sum += mksb - ss;
if (gg < mksb) sum += mksb - gg;
return (cc > mksb || cc < 0 || ss > mksb || ss < 0 || gg > mksb || gg < 0 ||
sum > b2ee);
}
int main() {
ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0),
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
bool z = true;
cin >> n >> k >> d1 >> d2;
if (n % 3 != 0) {
cout << "no\n";
continue;
} else {
long long c = (k + (2 * d1) + d2) / 3;
long long s = c - d1;
long long g = s - d2;
z &= solve(c, s, g);
c = (k + (2 * d2) + d1) / 3;
s = c - d2;
g = s - d1;
z &= solve(c, s, g);
c = (k + (d2) + d1) / 3;
s = c - d1;
g = c - d2;
z &= solve(c, s, g);
c = (k + (2 * d1) - d2) / 3;
s = c - d1;
g = s + d2;
z &= solve(c, s, g);
}
(z) ? cout << "no\n" : cout << "yes\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000005];
int cnt[1000005];
int num[1000005];
void init() {
for (int i = 1; i <= n; i++) {
if (a[i] > 1000000) continue;
cnt[a[i]]++;
}
for (int i = 1; i <= 1000000; i++) {
if (cnt[i]) {
for (int j = i; j <= m; j += i) {
num[j] += cnt[i];
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
init();
int Max = 0;
int lcm = 1;
for (int i = 1; i <= m; i++) {
if (num[i] > Max) {
Max = num[i];
lcm = i;
}
}
vector<int> pos;
for (int i = 1; i <= n; i++) {
if (lcm % a[i] == 0) pos.push_back(i);
}
printf("%d %d\n", lcm, pos.size());
for (int i = 0; i < pos.size(); i++) {
printf("%d", pos[i]);
if (i == pos.size() - 1)
printf("\n");
else
printf(" ");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[2], ans = 0;
cin >> a[0] >> a[1];
while (a[0] > 0 && a[1] > 0) {
sort(a, a + 2);
a[0]--;
a[1] -= 2;
if (a[0] >= 0 && a[1] >= 0) ans++;
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int MAX = 1e5 + 55;
const int inf = 1e9 + 77;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-7;
int n, m;
bool grid[105][1005];
int cnt[105];
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d", &u);
scanf("%d", &v);
++cnt[u];
++cnt[v];
grid[u][i] = 1;
grid[v][i] = 1;
}
for (int i = 1; i <= n; ++i) {
if (!cnt[i]) {
printf("1\n");
++m;
printf("%d %d\n", i, m);
continue;
}
printf("%d\n", cnt[i]);
for (int j = 1; j < 1004; ++j) {
if (grid[i][j]) printf("%d %d\n", i, j);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int h, w;
vector<string> v;
int d[50][50];
int inf = 1e9+7;
typedef pair<int, int> P;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int main(){
cin >> h >> w;
for(int i=0; i<h; ++i){
string s;
cin >> s;
v.push_back(s);
}
for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
d[i][j] = inf;
d[0][0] = 0;
queue<P> q;
q.push(P(0, 0));
while(!q.empty()){
P p = q.front();
q.pop();
for(int i=0; i<4; ++i){
int nx = p.first + dx[i];
int ny = p.second + dy[i];
if(0 <= nx && nx < h && 0 <= ny && ny < w && v[nx][ny] == '.' && d[nx][ny] > d[p.first][p.second] + 1){
d[nx][ny] = d[p.first][p.second] + 1;
q.push(P(nx, ny));
}
}
}
int tmp = 0;
for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
if(v[i][j] == '#')
++tmp;
if(d[h-1][w-1] == inf) cout << -1 << endl;
else cout << h*w-tmp-d[h-1][w-1]-1 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
unsigned euclidean_gcd(unsigned a, unsigned b) {
if (a < b) euclidean_gcd(b, a);
unsigned r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
int main() {
int h, w;
cin >> h >> w;
int rec = euclidean_gcd(h, w);
int div = h / rec;
cout << h + w + 1 - (h / div) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long butuh[1000000], sedia[1000000];
bool cari(long long x) {
long long temp = 0;
for (long long i = 0; i < n; i++) {
if ((butuh[i] * x) > sedia[i]) {
temp += (butuh[i] * x) - sedia[i];
if (temp > k) return false;
}
}
if (temp <= k) {
return true;
}
return false;
}
int main() {
cin >> n >> k;
for (long long i = 0; i < n; i++) {
cin >> butuh[i];
}
for (long long i = 0; i < n; i++) {
cin >> sedia[i];
}
long long kiri = 0;
long long kanan = 2 * 1e9;
long long hasil;
while (kiri <= kanan) {
long long mid = (kanan + kiri) / 2;
if (cari(mid)) {
hasil = mid;
kiri = mid + 1;
} else {
kanan = mid - 1;
}
}
cout << hasil << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int morh[100001], parent[100001], pos[100001], temp = 0, ans = 0;
int calculate(int i) {
int posi = i, ans = 0;
while (posi != 0) {
if (pos[posi] > 1)
break;
else {
posi = parent[posi];
ans++;
}
}
return ans;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &morh[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &parent[i]);
pos[parent[i]]++;
}
int temp2;
for (int i = 1; i <= n; i++) {
if (morh[i] == 1) {
temp2 = calculate(i);
if (temp2 > ans) {
ans = temp2;
temp = i;
}
}
}
temp2 = ans;
int give_ans[ans], i = 0;
for (int j = 0; j < temp2; j++) {
give_ans[i] = temp;
i++;
temp = parent[temp];
}
printf("%d\n", ans);
for (int i = ans - 1; i >= 0; i--) printf("%d ", give_ans[i]);
printf("\n");
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long kiemtra(long long n) {
if (n == 0) {
return 0;
} else {
{
long long dem[18], i = 1;
for (long long j = 0; j <= 10; j++) {
dem[j] = 0;
}
while (n > 0 && i > 0) {
{
if (dem[n % 10] > 0) {
i = 0;
break;
}
if (dem[n % 10] == 0) {
dem[n % 10]++;
}
}
n = n / 10;
}
return i;
}
}
}
int main() {
long long n;
cin >> n;
n++;
while (kiemtra(n) < 1) {
n++;
}
cout << n << endl;
return 0;
}
| 1 |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int alice(char c, int x, int y);
int main(){
vector<int> q(4);
while(cin >>q[0]>>q[1]>>q[2]>>q[3],q[0]||q[1]||q[2]||q[3]){
vector<char> a(3),b(3),c(3);
a[0] = b[0] = c[0] = '+';
a[1] = b[1] = c[1] = '-';
a[2] = b[2] = c[2] = '*';
sort(a.begin(),a.end());
sort(b.begin(),b.end());
sort(c.begin(),c.end());
sort(q.begin(),q.end());
do{
do{
do{
do{
if(alice(a[0],q[0],alice(b[0],q[1],alice(c[0],q[2],q[3]))) == 10){
cout <<"("<<q[0]<<" "<<a[0]<<" ("<<q[1]<<" "<<b[0]<<" ("<<q[2]<<" "<<c[0]<<" "<<q[3]<<")))"<<endl;
goto flag;
}
if(alice(a[0],q[0],alice(c[0],alice(b[0],q[1],q[2]),q[3])) == 10){
cout <<"("<<q[0]<<" "<<a[0]<<" (("<<q[1]<<" "<<b[0]<<" "<<q[2]<<") "<<c[0]<<" "<<q[3]<<"))"<<endl;
goto flag;
}
if(alice(b[0],alice(a[0],q[0],q[1]),alice(c[0],q[2],q[3])) == 10){
cout <<"(("<<q[0]<<" "<<a[0]<<" "<<q[1]<<") "<<b[0]<<" ("<<q[2]<<" "<<c[0]<<" "<<q[3]<<"))"<<endl;
goto flag;
}
if(alice(c[0],alice(b[0],alice(a[0],q[0],q[1]),q[2]),q[3]) == 10){
cout <<"((("<<q[0]<<" "<<a[0]<<" "<<q[1]<<") "<<b[0]<<" "<<q[2]<<") "<<c[0]<<" "<<q[3]<<")"<<endl;
goto flag;
}
if(alice(a[0],q[0],alice(c[0],alice(b[0],q[1],q[2]),q[3])) == 10){
cout <<"("<<q[0]<<" "<<a[0]<<" (("<<q[1]<<" "<<b[0]<<" "<<q[2]<<") "<<c[0]<<" "<<q[3]<<"))"<<endl;
goto flag;
}
}while(next_permutation(q.begin(),q.end()));
}while(next_permutation(a.begin(),a.end()));
}while(next_permutation(b.begin(),b.end()));
}while(next_permutation(c.begin(),c.end()));
cout <<"0"<<endl;
flag:;
}
return 0;
}
int alice(char c, int x, int y){
if(c == '+') return x+y;
if(c == '-') return x-y;
return x*y;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
pair<long long int, long long int> a[500001];
int main() {
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
long long int i, j, n, f;
double lo, mid, hi, ans = -1, last;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
lo = 0;
hi = 1e10;
for (i = 0; i < 100; i++) {
mid = (lo + hi) / 2.0;
f = 0;
last = -1e100;
for (j = 1; j <= n; j++) {
if (a[j].second > 0)
last = max(last, 1.0 * (a[j].first + 1.0 * a[j].second * mid));
else {
if (1.0 * (a[j].first + 1.0 * a[j].second * mid) < last) {
f = 1;
break;
}
}
}
if (f) {
hi = mid;
ans = mid;
} else
lo = mid;
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long func(long long xorr, long long andd) {
if (xorr == 0 && andd == 0) return 1;
if (andd < 0 || xorr < 0) return 0;
long long bit1 = xorr % 2;
long long bit2 = andd % 2;
xorr = xorr / 2;
andd = andd / 2;
long long mul = 1;
if (bit1 == 0 && bit2 == 1) mul = 1;
if (bit1 == 1 && bit2 == 0) mul = 2;
if (bit1 == 1 && bit2 == 1) mul = 0;
if (bit1 == 0 && bit2 == 0) mul = 1;
return (mul * func(xorr, andd));
}
int main() {
long long sum, xorr;
long long andd;
cin >> sum >> xorr;
andd = sum - xorr;
if (andd % 2 == 1) {
cout << 0 << endl;
return 0;
}
andd = andd / 2;
long long ans = func(xorr, andd);
if (sum == xorr) ans = ans - 2;
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 2;
const int MOD = 1 << 30;
const double pi = acos(-1);
struct line {
double a, b, c;
};
struct point {
double x, y;
};
void pointsToLine(point p1, point p2, line &l) {
if (fabs(p1.x - p2.x) < 1e-2) {
l.a = 1.0;
l.b = 0.0;
l.c = -p1.x;
} else {
l.a = -(double)(p1.y - p2.y) / (p1.x - p2.x);
l.b = 1.0;
l.c = -(double)(l.a * p1.x) - p1.y;
}
}
bool areParallel(line l1, line l2) {
return ((fabs(l1.a - l2.a) < 1e-2) && (fabs(l2.b - l2.b) < 1e-2));
}
bool areSame(line l1, line l2) {
return (areParallel(l1, l2) && (fabs(l1.c - l2.c) < 1e-2));
}
bool areIntersect(line l1, line l2, point &p) {
if (areParallel(l1, l2)) return false;
p.x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b);
if (fabs(l1.b) > 1e-2)
p.y = -(l1.a * p.x + l1.c);
else
p.y = -(l2.a * p.x + l2.c);
return true;
}
vector<pair<int, int> > AdjList[N];
int n, m;
int vis[N];
void dfs(int u, int c) {
vis[u] = true;
for (int i = 0; i < (int)AdjList[u].size(); ++i) {
pair<int, int> v = AdjList[u][i];
if (!vis[v.first]) {
if (v.second == c) dfs(v.first, v.second);
}
}
}
void solve() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
AdjList[u].push_back({v, w});
AdjList[v].push_back({u, w});
}
int q;
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
int ans = 0;
for (int i = 1; i <= m; ++i) {
memset(vis, false, sizeof vis);
dfs(u, i);
if (vis[v]) ans++;
}
cout << ans << '\n';
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tc = 1;
while (tc--) solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1000000007;
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...);
}
string gh = "here";
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, d;
cin >> n >> d;
vector<long long> v1(n, 0);
for (long long i = 0; i < n; i++) {
cin >> v1[i];
}
long long ans = 0;
for (long long i = 0; i < n; i++) {
auto it = upper_bound(v1.begin(), v1.end(), v1[i] + d);
it--;
long long q = (it - v1.begin());
ans += ((q - i) * (q - i - 1)) / 2;
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n, k, p, x, y;
scanf("%d%d%d%d%d", &n, &k, &p, &x, &y);
int l, j, i;
int cone = 0, ctwo = 0;
int sum = 0;
int temp = k;
while (temp--) {
int a;
scanf("%d", &a);
sum += a;
if (a < y)
cone++;
else
ctwo++;
}
int one = n / 2 - cone;
if (one > n - k) one = n - k;
int m = n - k - one;
sum = x - sum;
if (one + y * m > sum || one < 0)
printf("-1");
else {
while (one--) printf("1 ");
while (m--) printf("%d ", y);
}
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> check(n + 1);
int max_el = n;
for (int i = 0; i < n; i++) {
check[a[i]] = 1;
if (a[i] == max_el) {
while (check[max_el]) {
cout << max_el << " ";
max_el -= 1;
}
}
if (i != n - 1) cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, i, e, f, g, n, m, k, l, A[100005], maxx;
map<int, int> mp;
int main() {
cin >> n >> k >> A[1];
maxx = A[1];
for (i = 2; i <= n; i++) {
cin >> A[i];
maxx = max(maxx, A[i]);
mp[maxx]++;
if (mp[maxx] == k) {
cout << maxx;
return 0;
}
}
cout << maxx;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100005;
int n, L, W;
vector<pair<int, int> > neigh[Maxn];
bool er[Maxn];
int siz[Maxn];
int N;
vector<int> BIT[Maxn];
vector<int> unBIT[Maxn];
long long res;
void Count(int v, int p) {
siz[v] = 1;
for (int i = 0; i < neigh[v].size(); i++) {
int u = neigh[v][i].first;
if (u == p || er[u]) continue;
Count(u, v);
siz[v] += siz[u];
}
}
int getCentroid(int v, int p, int sz) {
for (int i = 0; i < neigh[v].size(); i++) {
int u = neigh[v][i].first;
if (u == p || er[u]) continue;
if (siz[u] > sz / 2) return getCentroid(u, v, sz);
}
return v;
}
void Create(int ind, int w) {
ind++;
for (int i = ind; i <= N; i += i & -i) unBIT[i].push_back(w);
}
void Add(int l, int w) {
for (int i = l + 1; i <= N; i += i & -i) {
int ind =
lower_bound(unBIT[i].begin(), unBIT[i].end(), w) - unBIT[i].begin();
for (int j = ind + 1; j < BIT[i].size(); j += j & -j) BIT[i][j]++;
}
}
int Get(int l, int w) {
if (l > L || w > W) return 0;
l = min(N, L - l + 1);
w = W - w;
int res = 0;
for (int i = l; i > 0; i -= i & -i) {
int ind =
upper_bound(unBIT[i].begin(), unBIT[i].end(), w) - unBIT[i].begin() - 1;
for (int j = ind + 1; j > 0; j -= j & -j) res += BIT[i][j];
}
return res;
}
void createAll(int v, int p, int l, int w) {
Create(l, w);
for (int i = 0; i < neigh[v].size(); i++) {
pair<int, int> u = neigh[v][i];
if (u.first == p || er[u.first]) continue;
createAll(u.first, v, l + 1, w + u.second);
}
}
void addAll(int v, int p, int l, int w) {
Add(l, w);
for (int i = 0; i < neigh[v].size(); i++) {
pair<int, int> u = neigh[v][i];
if (u.first == p || er[u.first]) continue;
addAll(u.first, v, l + 1, w + u.second);
}
}
long long getAll(int v, int p, int l, int w) {
long long res = Get(l, w);
for (int i = 0; i < neigh[v].size(); i++) {
pair<int, int> u = neigh[v][i];
if (u.first == p || er[u.first]) continue;
res += getAll(u.first, v, l + 1, w + u.second);
}
return res;
}
void Solve(int v) {
Count(v, 0);
int C = getCentroid(v, 0, siz[v]);
N = siz[v];
for (int i = 0; i <= N; i++) {
unBIT[i].clear();
BIT[i].clear();
}
createAll(C, 0, 0, 0);
for (int i = 0; i <= N; i++) {
sort(unBIT[i].begin(), unBIT[i].end());
unBIT[i].erase(unique(unBIT[i].begin(), unBIT[i].end()), unBIT[i].end());
BIT[i].resize(unBIT[i].size() + 1, 0);
}
Add(0, 0);
for (int i = 0; i < neigh[C].size(); i++) {
pair<int, int> u = neigh[C][i];
if (er[u.first]) continue;
res += getAll(u.first, C, 1, u.second);
addAll(u.first, C, 1, u.second);
}
er[C] = true;
for (int i = 0; i < neigh[C].size(); i++) {
pair<int, int> u = neigh[C][i];
if (er[u.first]) continue;
Solve(u.first);
}
}
int main() {
scanf("%d %d %d", &n, &L, &W);
for (int i = 2; i <= n; i++) {
int p, w;
scanf("%d %d", &p, &w);
neigh[p].push_back(pair<int, int>(i, w));
neigh[i].push_back(pair<int, int>(p, w));
}
Solve(1);
printf("%I64d\n", res);
return 0;
}
| 5 |
/**
* Author: ngkan
* ┬┴┬┴┤(・_├┬┴┬┴ hiding from those WAs
*/
#include <bits/stdc++.h>
using namespace std;
const int N = (int) 3e5 + 5;
const int mod = (int) 1e9 + 7;
const double EPS = 1e-9;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
/******************************************************************************/
int n, m;
vector <int> G[N];
bool visited[N], student[N];
void dfs(int u,int p){
visited[u] = 1;
for(auto v: G[u]){
if (v == p)
continue;
if (!visited[v])
dfs(v, u);
}
}
void ngk(){
cin >> n >> m;
for(int i=1;i<=n;i++)
G[i].clear(),
visited[i] = student[i] = 0;
for(int i=1;i<=m;i++){
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1, 1);
for(int i=1;i<=n;i++){
if (!visited[i]){
cout << "NO\n";
return;
}
}
for(int i=1;i<=n;i++)
visited[i] = 0;
vector <int> teacher;
queue<int> waitlist;
waitlist.push(1);
while (waitlist.size()){
int teachr = waitlist.front();
waitlist.pop();
if (visited[teachr] || student[teachr])
continue;
visited[teachr] = 1;
teacher.push_back(teachr);
for(auto v: G[teachr]){
if (!student[v]){
student[v] = 1;
for(auto vv: G[v])
waitlist.push(vv);
}
}
}
sort(teacher.begin(), teacher.end());
cout << "YES\n";
cout << teacher.size() << endl;
for(auto v: teacher) cout << v << ' '; cout << '\n';
}
int main(int argc, char *argv[]){
iostream::sync_with_stdio(0);
int test = 1;
cin >> test;
while(test--){
ngk();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int MAXN = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double EPS = 1e-9;
inline int sgn(double a) {
if (a < -EPS) return -1;
return a > EPS;
}
inline int cmp(double a, double b) { return sgn(a - b); }
int n, m;
int lowbit(int x) { return x & -x; }
long long tree[2][2][MAXN][MAXN];
long long sum(int x, int y) {
long long ret = 0;
for (int xx = x; xx; xx -= lowbit(xx)) {
for (int yy = y; yy; yy -= lowbit(yy)) {
ret ^= tree[x & 1][y & 1][xx][yy];
}
}
return ret;
}
void update(int x, int y, long long val) {
for (int xx = x; xx <= n; xx += lowbit(xx)) {
for (int yy = y; yy <= n; yy += lowbit(yy))
tree[x & 1][y & 1][xx][yy] ^= val;
}
}
void solve() {
int ty, a, b, c, d;
long long v;
while (m--) {
scanf("%d%d%d%d%d", &ty, &a, &b, &c, &d);
if (ty == 2) {
scanf("%lld", &v);
update(c + 1, d + 1, v);
update(a, d + 1, v);
update(c + 1, b, v);
update(a, b, v);
} else {
printf("%lld\n",
sum(c, d) ^ sum(c, b - 1) ^ sum(a - 1, d) ^ sum(a - 1, b - 1));
}
}
}
void init() { scanf("%d%d", &n, &m); }
int main() {
int T = 1;
while (T--) {
init();
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
int n, m, sol;
char s[100][100];
char viz[52][52];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void baga(int x, int y, int k) {
while (s[x][y]) {
viz[x][y] = 1;
x += dx[k];
y += dy[k];
}
}
int check(int x1, int y1, int x2, int y2) {
int i, j;
int ok1 = 1, ok2 = 1, ok3 = 1, ok4 = 1;
for (i = x1; i <= x2; i++) {
if (s[i][y1] == 0) ok1 = 0;
if (s[i][y2] == 0) ok3 = 0;
}
for (j = y1; j <= y2; j++) {
if (s[x1][j] == 0) ok2 = 0;
if (s[x2][j] == 0) ok4 = 0;
}
return (ok1 && ok4) || (ok2 && ok3);
}
int main() {
scanf("%d %d\n", &n, &m);
int i, j, k, l;
for (i = 1; i <= n; i++) {
gets(s[i] + 1);
for (j = 1; j <= m; j++)
if (s[i][j] == 'B')
s[i][j] = 1;
else
s[i][j] = 0;
}
sol = 1;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (s[i][j])
for (k = 1; k <= n; k++)
for (l = 1; l <= m; l++)
if (s[k][l]) {
sol = sol & check(i, j, k, l);
sol++;
sol--;
}
printf("%s\n", sol ? "YES" : "NO");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > A;
int sol[101];
int main() {
int N, M, a;
cin >> N >> M;
for (int i = 1; i <= N; ++i) {
cin >> a;
A.push_back(make_pair(a, i));
}
sort(A.begin(), A.end());
for (int i = 0; i < A.size(); ++i) sol[A[i].second] = (i + 1) % 2;
for (int i = 1; i <= N; ++i) cout << sol[i] << ' ';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e2;
long long a[N];
void solve() {
string s;
cin >> s;
int len = s.size();
for (int i = 0; i < len; i++) a[s[i]]++;
long long sum = 0;
for (auto e : a) sum += e * e;
cout << sum << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
while (t-- > 0) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long q, n, m;
vector<long long> ans;
int main() {
cin >> q;
for (int i = 0; i < q; i++) {
cin >> n;
m = n;
if (n % 2) m = n + 1;
if (n < 4) m = 4;
ans.push_back(m - n);
}
for (int i = 0; i < q; i++) cout << ans[i] << "\n";
return 0;
}
| 1 |
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
struct trie {
trie *son[2];
inline trie() { son[0] = son[1] = NULL; }
} * root;
int n, l, ans = 0;
char buf[100010];
inline void Insert() {
trie *p = root;
for (int i = 0; buf[i]; i++) {
const int x = buf[i] - '0';
if (p->son[x])
p = p->son[x];
else
p = (p->son[x] = new trie());
}
}
void DFS(trie *node, int d) {
if (!node || (!node->son[0] && !node->son[1])) return;
if (node->son[0] && node->son[1]) {
DFS(node->son[0], d + 1);
DFS(node->son[1], d + 1);
return;
}
int now = l - d, cnt = 1;
while (now && !(now & 1)) cnt++, now >>= 1;
ans ^= cnt;
if (node->son[0])
DFS(node->son[0], d + 1);
else
DFS(node->son[1], d + 1);
}
int main() {
scanf("%d%d", &n, &l);
root = new trie();
for (int i = 1; i <= n; i++) {
scanf("%s", buf);
Insert();
}
DFS(root, 0);
puts(ans ? "Alice" : "Bob");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000;
bool vis[N];
int cnt = 0;
int nnext[N << 1], f[N], v[N << 1];
inline void add(int a, int b) {
nnext[++cnt] = f[a];
f[a] = cnt;
v[cnt] = b;
}
int dfs(int u) {
vis[u] = 1;
for (int i = f[u]; i; i = nnext[i]) {
int x = v[i];
if (!vis[x]) {
return dfs(x);
}
}
return u;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
}
int x = 1;
for (;;) {
int a = dfs(x), b = dfs(x);
if (a == x) break;
printf("? %d %d\n", a, b);
fflush(stdout);
scanf("%d", &x);
}
printf("! %d\n", x);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void input(T &x) {
register char c = getchar();
x = 0;
int neg = 0;
for (; ((c < 48 || c > 57) && c != '-'); c = getchar())
;
if (c == '-') {
neg = 1;
c = getchar();
}
for (; c > 47 && c < 58; c = getchar()) {
x = (x << 1) + (x << 3) + c - 48;
}
if (neg) x = -x;
}
inline long long bigmod(long long p, long long e, long long M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
const int N = 1001;
vector<int> G[N];
int link[N], second[N], T[N], rig[N];
bool dfs(int u) {
second[u] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (T[v]) continue;
T[v] = 1;
if (link[v] == -1 || dfs(link[v])) {
link[v] = u;
rig[u] = v;
return 1;
}
}
return 0;
}
int maxMatching(int n) {
int ret = 0;
memset(link, -1, sizeof link);
memset(rig, -1, sizeof rig);
for (int i = 1; i <= n; i++) {
memset(second, 0, sizeof second);
memset(T, 0, sizeof T);
if (dfs(i)) ret++;
}
return ret;
}
int dir[100][2];
int ways = 0;
void buildGraph(int n, int m) {
for (int i = 1; i < N; i++) G[i].clear();
for (int k = 1; k <= n; k++) {
for (int l = 1; l <= m; l++) {
if ((k + l) % 2 == 0) {
for (int p = 0; p < ways; p++) {
int x = k + dir[p][0], y = l + dir[p][1];
if (x >= 1 && x <= n && y >= 1 && y <= m) {
int vi = k * m + l, vj = x * m + y;
G[vi].push_back(vj);
}
}
}
}
}
}
int main() {
for (int i = -3; i <= 3; i++) {
for (int j = -3; j <= 3; j++) {
if (abs(i) + abs(j) == 3) dir[ways][0] = i, dir[ways][1] = j, ways++;
}
}
int Ans[15][24];
for (int i = 1; i <= 10; i++) {
for (int j = i; j <= 20; j++) {
buildGraph(i, j);
Ans[i][j] = maxMatching(i * j + j) * 2;
}
}
long long n, m;
input(n), input(m);
if (n > m) swap(n, m);
if (n == 1) {
if (m <= 20)
cout << Ans[n][m] << endl;
else if (m % 6 && m % 6 <= 3)
cout << (m / 6 * 6) << endl;
else if (m % 6 == 0)
cout << m << endl;
else if (m % 6 == 5)
cout << m - 1 << endl;
else if (m % 5 == 4)
cout << m - 2 << endl;
} else if (n == 2) {
if (m <= 20)
cout << Ans[n][m] << endl;
else
cout << 2 * m << endl;
} else {
if (n % 2 && m % 2)
cout << (n * m - 1) << endl;
else
cout << n * m << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int n;
int mp[maxn][maxn];
int pre[2][maxn][maxn], dp[2][maxn][maxn];
int path[2][maxn][maxn];
void make(int x, int y) {
int num = mp[x][y];
while (num % 2 == 0) {
pre[0][x][y]++;
num /= 2;
}
while (num % 5 == 0) {
pre[1][x][y]++;
num /= 5;
}
}
void print(int type, int x, int y) {
if (path[type][x][y] != -1) {
if (path[type][x][y] == 1) {
print(type, x - 1, y);
printf("D");
} else if (path[type][x][y] == 2) {
print(type, x, y - 1);
printf("R");
}
}
}
void pri(int mid, int x, int y) {
if (x == 1 && y == 1) return;
if (path[mid][x][y] == 2) {
pri(mid, x, y - 1);
putchar('R');
} else {
pri(mid, x - 1, y);
putchar('D');
}
}
int main() {
int x1, y1;
int flg = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
scanf("%d", &mp[i][j]);
if (mp[i][j] == 0) {
mp[i][j] = 10;
flg = 1;
x1 = i;
y1 = j;
}
make(i, j);
}
dp[0][1][1] = pre[0][1][1];
dp[1][1][1] = pre[1][1][1];
for (int i = 2; i <= n; i++) {
for (int id = 0; id < 2; id++) {
dp[id][i][1] = dp[id][i - 1][1] + pre[id][i][1];
path[id][i][1] = 1;
dp[id][1][i] = dp[id][1][i - 1] + pre[id][1][i];
path[id][1][i] = 2;
}
}
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= n; j++) {
for (int id = 0; id < 2; id++) {
if (dp[id][i - 1][j] < dp[id][i][j - 1]) {
dp[id][i][j] = dp[id][i - 1][j] + pre[id][i][j];
path[id][i][j] = 1;
} else {
dp[id][i][j] = dp[id][i][j - 1] + pre[id][i][j];
path[id][i][j] = 2;
}
}
}
}
int res = min(dp[1][n][n], dp[0][n][n]);
int type = (res == dp[1][n][n]);
if (res > 1 && flg) {
cout << 1 << endl;
for (int i = 1; i < x1; i++) printf("D");
for (int j = 1; j < y1; j++) printf("R");
for (int i = x1 + 1; i <= n; i++) printf("D");
for (int i = y1 + 1; i <= n; i++) printf("R");
return 0;
}
cout << res << endl;
pri(type, n, n);
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
vector<pair<int, int>> s(t, {0, INT_MAX});
for (int i = 0; i < t; i++) {
int n;
cin >> n;
string a;
cin >> a;
map<pair<int, int>, int> l;
l.insert({{0, 0}, 0});
int x = 0, y = 0, j1 = 0, j2 = 0;
for (int j = 0; j < a.size(); j++) {
if (a[j] == 'D') {
y--;
}
if (a[j] == 'U') {
y++;
}
if (a[j] == 'R') {
x++;
}
if (a[j] == 'L') {
x--;
}
if (l.find({x, y}) != l.end()) {
if (s[i].second - s[i].first > j - l[{x, y}]) {
s[i] = {l[{x, y}] + 1, j + 1};
}
l[{x, y}] = j + 1;
}
l.insert({{x, y}, j + 1});
}
}
for (int i = 0; i < t; i++) {
if (s[i].second == INT_MAX) {
cout << -1 << '\n';
} else {
cout << s[i].first << " " << s[i].second << '\n';
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7, M = 50;
long long n, lt, cnt, len, w[N];
long long fw[N], oq[N];
struct node {
long long u;
long long t;
} q[M], f[M], wf[N], wq[N];
inline long long read() {
long long num = 0;
char g = getchar();
while (g < 48 || 57 < g) g = getchar();
while (47 < g && g < 58)
num = (num << 1) + (num << 3) + g - 48, g = getchar();
return num;
}
inline long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
inline bool cmp(node a, node b) { return a.u < b.u; }
inline long long updiv(long long a, long long b) {
if (a % b) return a / b + 1;
return a / b;
}
inline long long downdiv(long long a, long long b) { return a / b; }
inline long long getopt(long long n, long long a, long long b, long long c) {
if (n < 0) return 0;
if (a == 0) return downdiv(b, c) * (n + 1);
if (b < 0) {
long long x = (((b % c + c) % c) - b) / c;
return getopt(n - x, a, b + x * c, c);
}
if (a >= c || b >= c)
return (getopt(n, a % c, b % c, c) + downdiv(a, c) * ((n * (n + 1) / 2)) +
downdiv(b, c) * (n + 1));
long long t = downdiv(a * n + b, c);
return (t * n - getopt(t - 1, c, c - b - 1, a));
}
inline long long getans(long long l, long long r) {
return (l + r) * (r - l + 1) / 2;
}
inline bool check(long long u) {
long long pt = 0;
long long sum = 0;
for (long long i = 1; i <= lt; i++)
fw[i] = fw[i - 1] + wq[i].u * wq[i].t, oq[i] = oq[i - 1] + wq[i].t;
long long pr =
oq[lt] * (oq[lt] + 1) / 2 - (oq[lt] * (oq[lt] + 1) / 2 + 1) / 2 + 1;
for (long long i = 1; i <= lt; i++) {
long long op = 0;
if (pt < i) pt++;
while (pt <= lt) {
if (pt == i) {
long long s = updiv(u, wq[i].u), lq = oq[i - 1] + s - 1,
rq = oq[i - 1] + wq[i].t - 1;
if (lq > rq) {
pt++;
continue;
}
op = op + rq - lq + 1;
sum = sum - getans(lq, rq);
if (op == wq[i].t) break;
pt++;
} else {
long long hf = fw[pt] - fw[i] + (wq[i].t - op) * wq[i].u;
if (hf < u) {
pt++;
continue;
}
long long hc =
wq[i].t - max(updiv(u - fw[pt] + fw[i], wq[i].u), 1ll) + 1;
sum = sum - (hc - op) * oq[lt];
long long fa = wq[i].t - hc + 1, fb = wq[i].t - op;
long long fh =
getopt(fb - fa, wq[i].u,
u - (fw[pt - 1] - fw[i]) - fb * wq[i].u - 1, wq[pt].u);
sum = sum + (fb - fa + 1) * (oq[lt] - oq[pt - 1]) - fh, op = hc;
if (op == wq[i].t) break;
pt++;
}
}
sum = sum + op * oq[lt];
}
if (sum >= pr) return true;
return false;
}
signed main() {
n = read();
for (long long i = 1; i <= n; i++) w[i] = read();
for (long long i = 1; i <= n; i++) {
for (long long c = 1; c <= cnt; c++) q[c].u = gcd(q[c].u, w[i]);
cnt++, q[cnt].u = w[i], q[cnt].t = 1;
sort(q + 1, q + cnt + 1, cmp);
long long op = 0;
memset(f, 0, sizeof(f));
for (long long c = 1; c <= cnt; c++)
if (f[op].u == q[c].u)
f[op].t += q[c].t;
else
f[++op] = q[c];
cnt = op;
for (long long c = 1; c <= op; c++) q[c] = f[c], wf[++len] = f[c];
}
sort(wf + 1, wf + len + 1, cmp);
for (long long i = 1; i <= len; i++)
if (wq[lt].u == wf[i].u)
wq[lt].t += wf[i].t;
else
wq[++lt] = wf[i];
long long l = 1, r = 1e18, ans = 0;
while (l <= r) {
long long d = (l + r) >> 1;
if (check(d))
ans = d, l = d + 1;
else
r = d - 1;
}
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, string>> lol[101010];
for (int i = 0; i < n; i++) {
string s;
int re, sc;
cin >> s >> re >> sc;
lol[re].push_back({sc, s});
}
for (int i = 1; i <= m; i++)
sort(lol[i].begin(), lol[i].end(), greater<pair<int, string>>());
for (int i = 1; i <= m; i++) {
if (lol[i].size() <= 2) {
cout << lol[i][0].second << " " << lol[i][1].second << endl;
} else if (lol[i][1].first == lol[i][2].first) {
cout << "?" << endl;
} else
cout << lol[i][0].second << " " << lol[i][1].second << endl;
}
return 0;
}
| 2 |
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
const int NUM = 1000000;
bool prime[NUM];
prime[0] = false;
prime[1] = false;
for(int i=2;i<NUM;i++)
prime[i] = true;
for(int i=2;i*i<NUM;i++)
for(int k=2;i*k<NUM;k++)
prime[i*k]=false;
int n,p,pn;
while(true){
cin >> n >> p;
if (n==-1)
break;
int sum[p*p];
for(int i=0;i<p*p;i++)
sum[i] = 0;
pn = n+1;
for(int i=0;i<p;i++){
while(!prime[pn])
pn++;
for(int j=0;j<p;j++){
sum[j*p+i] += pn;
sum[i*p+j] += pn;
}
pn++;
}
for(int i = 0;i<p*p;i++){
if(i/p>i%p)
sum[i] = 1000000;
}
sort(sum,sum+p*p);
cout << sum[p-1] << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
void clean() {}
void solve() {
clean();
int a = 0, b = 1023;
for (int x, i = 1; i <= n; i++) {
char s[5];
scanf("%s %d", s, &x);
switch (s[0]) {
case '^':
a ^= x, b ^= x;
break;
case '|':
a |= x, b |= x;
break;
case '&':
a &= x, b &= x;
break;
}
}
int A = 1023, X = 0, O = 0, bs = 1;
for (int i = 0; i < 10; i++) {
int taki = a & 1, mitsuha = b & 1;
if (taki == 0) {
if (mitsuha == 0) A -= bs;
if (mitsuha == 1)
;
} else if (taki == 1) {
if (mitsuha == 0) X += bs;
if (mitsuha == 1) O += bs;
}
a >>= 1, b >>= 1, bs <<= 1;
}
printf("3\n| %d\n^ %d\n& %d\n", O, X, A);
}
int main() {
scanf("%d", &n), solve();
return 0;
}
| 3 |
#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
using namespace std;
#define LL long long
const LL inf = 1e15;
struct edge{
int n, ty;
LL cost;
bool operator<(const edge& e1) const {
return e1.cost < cost;
}
};
int M, N, K;
int z[200010][2];
LL D[200010][2];
vector<int> G[100010][2];
int main(){
cin >> M >> N >> K;
for(int i = 0; i <= K; i++){
D[i][0] = inf;
D[i][1] = inf;
}
z[0][0] = z[0][1] = 1;
for(int i = 1; i <= K; i++){
cin >> z[i][0] >> z[i][1];
G[z[i][0]][0].push_back(i);
G[z[i][1]][1].push_back(i);
}
priority_queue<edge> pq;
pq.push(edge{0, 0, 0});
D[0][0] = 0;
while(!pq.empty()){
edge e = pq.top();
pq.pop();
if(D[e.n][e.ty] < e.cost) continue;
int inv = (e.ty + 1) & 1;
for(auto &u : G[z[e.n][e.ty]][e.ty]){
LL c = e.cost + (LL)abs(z[e.n][inv] - z[u][inv]) + 1;
if(D[u][inv] > c){
D[u][inv] = c;
pq.push(edge{u, inv, c});
}
}
}
LL ans = inf;
for(int i = 1; i <= K; i++){
if(z[i][0] == M){
ans = min(ans, min(D[i][0],D[i][1] + 1) + abs(N - z[i][1]) );
}
if(z[i][1] == N){
ans = min(ans, min(D[i][0] + 1,D[i][1]) + abs(M - z[i][0]) );
}
}
cout << (ans == inf ? -1 : ans) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int getint() {
unsigned int c;
int x = 0;
while (((c = getchar()) - '0') >= 10) {
if (c == '-') return -getint();
if (!~c) exit(0);
}
do {
x = (x << 3) + (x << 1) + (c - '0');
} while (((c = getchar()) - '0') < 10);
return x;
}
int getstr(char *s) {
int c, n = 0;
while ((c = getchar()) <= ' ') {
if (!~c) exit(0);
}
do {
s[n++] = c;
} while ((c = getchar()) > ' ');
s[n] = 0;
return n;
}
template <class T>
inline bool chmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
inline bool chmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
int n, m, in[333][333];
int fl, up, dn;
int cost(int next, int now) {
if (next > now) return up;
if (next < now) return dn;
return fl;
}
int accR[333][333];
int accL[333][333];
int accU[333][333];
int accD[333][333];
void prepare() {
int i, j;
for (i = 0; i < n; i++)
for (j = m - 2; ~j; --j)
accR[i][j] = accR[i][j + 1] + cost(in[i][j + 1], in[i][j]);
for (i = 0; i < n; i++)
for (j = 1; j < m; j++)
accL[i][j] = accL[i][j - 1] + cost(in[i][j - 1], in[i][j]);
for (j = 0; j < m; j++)
for (i = n - 2; ~i; --i)
accD[i][j] = accD[i + 1][j] + cost(in[i + 1][j], in[i][j]);
for (j = 0; j < m; j++)
for (i = 1; i < n; i++)
accU[i][j] = accU[i - 1][j] + cost(in[i - 1][j], in[i][j]);
}
int main() {
int i, j, tcc, tc = 1 << 28;
for (tcc = 0; tcc < tc; tcc++) {
n = getint();
m = getint();
int total = getint();
fl = getint(), up = getint(), dn = getint();
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) in[i][j] = getint();
prepare();
int res = 1e9;
int x1, x2, y1, y2;
for (int leftCol = 0; leftCol < m; leftCol++) {
for (int rightCol = leftCol + 2; rightCol < m; rightCol++) {
set<pair<int, int> > costSet;
for (i = n - 3; ~i; --i) {
int addRow = i + 2;
int partsCost = -accD[addRow][rightCol] + accU[addRow][leftCol] +
accL[addRow][rightCol] - accL[addRow][leftCol];
costSet.insert(make_pair(partsCost, addRow));
int nowCost = accD[i][rightCol] - accU[i][leftCol] +
accR[i][leftCol] - accR[i][rightCol];
int want = total - nowCost;
set<pair<int, int> >::iterator it =
costSet.lower_bound(make_pair(want, 0));
if (it != costSet.end()) {
if (chmin(res, abs(total - it->first - nowCost))) {
x1 = i, x2 = it->second, y1 = leftCol, y2 = rightCol;
}
}
if (it != costSet.begin()) {
it--;
if (chmin(res, abs(total - it->first - nowCost))) {
x1 = i, x2 = it->second, y1 = leftCol, y2 = rightCol;
}
}
}
}
}
cerr << " res = " << res << endl;
printf("%d %d %d %d\n", ++x1, ++y1, ++x2, ++y2);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N=30050;
int t,n,c,q;
unsigned long long a[N][5],presum[N][5],nxtsum[N][5];
int p[N],ch[N][6];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&c,&q);
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i]);
a[i][0]=1;
ch[i][0]=0;
}
for(int j=0;j<=c;j++) a[n+1][j]=1;
for(int j=1;j<=c;j++)
{
for(int i=n;i>=1;i--)
{
a[i][j]=0;
for(int k=0;k<=j&&i+k<=n;k++)
a[i][j]+=a[i+k+1][j-k];
assert(a[i][j]<=1e18);
}
}
pair<int,int> temp[6];
for(int i=1;i<=n;i++)
{
temp[0].first=0;
for(int j=0;j<=c&&i+j<=n;j++)
temp[++temp[0].first]=make_pair(p[i+j],j);
sort(temp+1,temp+temp[0].first+1);
ch[i][0]=temp[0].first;
memcpy(presum+i,presum+i-1,5*sizeof(unsigned long long));
memcpy(nxtsum+i,nxtsum+i-1,5*sizeof(unsigned long long));
bool flag=false;
for(int j=1;j<=temp[0].first;j++)
{
ch[i][j]=temp[j].second;
if(ch[i][j]==0) {flag=true;continue;}
if(!flag)
{
for(int k=ch[i][j];k<=c;k++)
presum[i][k]+=a[i+ch[i][j]+1][k-ch[i][j]];
}
else
{
for(int k=ch[i][j];k<=c;k++)
nxtsum[i][k]+=a[i+ch[i][j]+1][k-ch[i][j]];
}
}
}
while(q--)
{
int x;
unsigned long long y;
scanf("%d%llu",&x,&y);
vector<pair<int,int> > v;
int pos=1,nowc=c;
if(y>a[1][c]) {puts("-1");continue;}
while(pos<=n)
{
if(nowc==0||presum[n][nowc]-presum[pos-1][nowc]+1==y) break;
else if(presum[n][nowc]-presum[pos-1][nowc]>=y)
{
int l=pos,r=n;
while(l<r)
{
int mid=(l+r)>>1;
if(presum[mid][nowc]-presum[pos-1][nowc]>=y) r=mid;
else l=mid+1;
}
y-=presum[l-1][nowc]-presum[pos-1][nowc];
pos=l;
for(int i=1;i<=ch[pos][0];i++)
if(nowc<ch[pos][i]) continue;
else if(a[pos+ch[pos][i]+1][nowc-ch[pos][i]]<y)
y-=a[pos+ch[pos][i]+1][nowc-ch[pos][i]];
else
{
v.push_back(make_pair(pos,ch[pos][i]));
nowc-=ch[pos][i];
pos+=ch[pos][i]+1;
break;
}
}
else
{
y-=presum[n][nowc]-presum[pos-1][nowc]+1;
int l=pos,r=n+1;
while(l<r)
{
int mid=(l+r)>>1;
if(nxtsum[n][nowc]-nxtsum[mid-1][nowc]<y) r=mid;
else l=mid+1;
}
y-=nxtsum[n][nowc]-nxtsum[l-1][nowc];
pos=l-1;
bool flag=false;
for(int i=1;i<=ch[pos][0];i++)
if(ch[pos][i]==0) flag=true;
else if(flag)
{
if(nowc<ch[pos][i]) continue;
else if(a[pos+ch[pos][i]+1][nowc-ch[pos][i]]<y)
y-=a[pos+ch[pos][i]+1][nowc-ch[pos][i]];
else
{
v.push_back(make_pair(pos,ch[pos][i]));
nowc-=ch[pos][i];
pos+=ch[pos][i]+1;
break;
}
}
}
}
int ans=p[x];
for(auto u:v)
{
if(x>=u.first&&x<=u.first+u.second)
ans=p[u.first+u.first+u.second-x];
}
printf("%d\n",ans);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int a, b;
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> a >> b >> n;
while (n--) {
long long int l, t, m;
cin >> l >> t >> m;
l--;
long long int lo = l - 1, hi = t + 2;
while (lo + 1 < hi) {
long long int mi = (lo + hi) / 2;
long long int sum = 2 * a + (l + mi) * b;
sum *= mi - l + 1;
sum /= 2;
long long int cost = max(a + b * mi, (sum + m - 1) / m);
if (cost <= t)
lo = mi;
else
hi = mi;
}
if (lo < l)
cout << -1 << endl;
else
cout << lo + 1 << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, a[100005];
vector<int> all, empty;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
(x *= 10) += ch - '0';
ch = getchar();
}
return x * f;
}
vector<int> solve(int x) {
if (x > 19) return empty;
bool flag = false;
for (int v : all)
if (v & 1) {
flag = true;
break;
}
if (!flag) {
for (int &v : all) v >>= 1;
return solve(x + 1);
}
vector<int> backup, nega, posi, res;
backup = all;
for (int &v : all) {
if (v & 1)
v = (v + 1) >> 1;
else
v = v >> 1;
}
{
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
};
nega = solve(x + 1);
all = backup;
for (int &v : all) {
if (v & 1)
v = (v - 1) >> 1;
else
v = v >> 1;
}
{
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
};
posi = solve(x + 1);
nega.push_back(-(1 << x)), posi.push_back(1 << x);
return nega.size() < posi.size() ? nega : posi;
}
int main() {
n = read();
for (int i = 1; i <= n; i++) all.push_back(read());
vector<int> res = solve(0);
sort(res.begin(), res.end());
printf("%lld\n", res.size());
for (int v : res) printf("%d ", v);
return 0;
}
| 5 |
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int N, R, p, c;
int main() {
while (cin >> N >> R && N){
vector<int> A(N,0);
int i;
for(i=0;i<N;i++){
A[i]=N-i;
}
for(i=0;i<R;++i){
cin >> p >> c;
if (p==0){
cout << "Incorrect input" << endl;
exit(1);
}
rotate(A.begin(),A.begin()+p-1,A.begin()+p+c-1);
}
cout << A[0] << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w = 0;
cin >> n;
char a, b;
char x[n][n];
bool flag = false, flag1 = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> x[i][j];
if (i == 0 && j == 0) a = x[i][j];
if (i == 0 && j == 1) b = x[i][j];
if (x[i][j] != a && x[i][j] != b) flag1 = true;
if (a == x[i][j]) w++;
}
}
if (a == b || flag1 == true || w != (n * 2) - 1) {
cout << "NO\n";
flag = true;
} else {
for (int i = 0, j = 0; i < n; i++, j++, n--) {
if (x[i][j] != a && x[i][n - 1]) {
cout << "NO\n";
flag = true;
break;
}
if (x[n - 1][n - 1] != a || x[n - 1][j] != a) {
cout << "NO\n";
flag = true;
break;
}
}
if (flag == false) cout << "YES\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma warning(disable : 4786)
#pragma comment(linker, "/STACK:0x800000")
using namespace std;
int on(int n, int k) { return (n | (1 << k)); }
int off(int n, int k) { return (n - (n & (1 << k))); }
bool chck(int n, int k) { return (n & (1 << k)); }
int mini(int a, int b) {
if (a < b) return a;
return b;
}
int maxi(int a, int b) {
if (a > b) return a;
return b;
}
int n;
int a[500100];
map<int, int> map1, map2;
int tot = 1;
vector<int> v[500100];
vector<int> ans1, ans2;
int pn[500100];
priority_queue<int> pq;
int main() {
int i, j, k, l, test, t = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (!map1[a[i]]) {
map2[tot] = a[i];
map1[a[i]] = tot++;
}
a[i] = map1[a[i]];
}
for (i = 1; i <= n; i++) {
v[a[i]].push_back(i);
}
int last = n + 1, ans = 0, now;
int ind1, ind2;
for (i = 1; i <= n; i++) {
if (i == last) {
ans++;
ans1.push_back(ind1);
ans2.push_back(ind2);
last = n + 1;
while (!pq.empty()) pq.pop();
continue;
}
int now = a[i];
while (v[a[i]][pn[a[i]]] != i) {
pn[a[i]]++;
}
if (pn[a[i]] == (int)v[a[i]].size() - 1) continue;
if (pn[a[i]] < (int)((int)v[a[i]].size() - 3)) {
int last1 = v[a[i]][pn[a[i]] + 3];
if (last1 < last) {
last = last1;
ind1 = a[i];
ind2 = a[i];
}
}
while (!pq.empty() && (-pq.top()) <= i) {
pq.pop();
}
if (!pq.empty())
now = -pq.top();
else
now = n + 1;
int nxt = v[a[i]][pn[a[i]] + 1];
if (nxt > now) {
int last1 = nxt;
if (last1 < last) {
last = last1;
ind1 = a[now];
ind2 = a[nxt];
}
}
pq.push(-nxt);
}
printf("%d\n", ans * 4);
for (i = 0; i < ans; i++) {
printf("%d %d %d %d ", map2[ans1[i]], map2[ans2[i]], map2[ans1[i]],
map2[ans2[i]]);
}
puts("");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<long long> > b(64, vector<long long>());
for (int i = 0; i < n; ++i) {
long long tmp;
cin >> tmp;
long long t = tmp;
int c = 0;
while ((t & 1ll) == 0ll) {
t >>= 1;
++c;
}
b[c].push_back(tmp);
}
int mx = 0;
for (int i = 1; i < b.size(); ++i) {
if (b[i].size() > b[mx].size()) mx = i;
}
vector<long long> ans;
for (int i = 0; i < 64; ++i) {
if (i == mx) continue;
for (auto j : b[i]) ans.push_back(j);
}
cout << ans.size() << endl;
for (auto i : ans) cout << i << " ";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return {i, j};
}
struct printer {
~printer() { cerr << endl; }
template <class c>
printer& operator,(c x) {
cerr << boolalpha << x;
return *this;
}
printer& operator,(string x) {
cerr << x;
return *this;
}
template <class c, class d>
printer& operator,(pair<c, d> x) {
return *this, "(", x.first, ", ", x.second, ")";
}
template <class... d, template <class...> class c>
printer& operator,(c<d...> x) {
return *this, range(begin(x), end(x));
}
template <class c>
printer& operator,(rge<c> x) {
*this, "[";
for (auto it = x.b; it != x.e; ++it) *this, (it == x.b ? "" : ", "), *it;
return *this, "]";
}
};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int my_rand(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
const int N = 100009;
long long ar[15];
void pre() {
long long i, id = 1;
for (i = 10; i <= 1e10; i = i * 10) {
ar[id] = ar[id - 1] + i;
id++;
}
}
int main() {
pre();
int t;
string s, second;
cin >> t;
while (t--) {
long long sum = 1989, num = 0;
cin >> s;
int sz = s.size();
int lastnum = sz - 4;
for (int i = 4; i < sz; i++) {
num = num * 10 + (s[i] - '0');
}
sum += ar[sz - 4 - 1];
string st_sum = to_string(sum);
int sz_sum = st_sum.size();
long long sum1 = 0, sum2 = 0, flag = 0;
for (int i = 0; i < sz_sum - lastnum; i++) {
sum1 = sum1 * 10 + (st_sum[i] - '0');
}
for (int i = sz_sum - lastnum; i < sz_sum; i++) {
sum2 = sum2 * 10 + (st_sum[i] - '0');
}
if (sum2 > num) flag = 1;
sum1 += flag;
if (sum1 > 0) {
cout << sum1;
for (int i = 4; i < sz; i++) cout << s[i];
cout << '\n';
} else
cout << num << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
long long head[N], ver[N], nxt[N], cnt;
struct Node {
long long lef, rig;
long long sum;
} Tree[N];
struct Edge {
long long sta, las;
} Link[N];
void add(long long x, long long y) {
nxt[++cnt] = head[x];
head[x] = cnt;
ver[cnt] = y;
}
long long father[N], depth[N], top[N], siz[N], son[N], id[N], idx;
void dfs1(long long now, long long fa, long long dep) {
depth[now] = dep;
siz[now] = 1;
father[now] = fa;
for (long long i = head[now]; i; i = nxt[i]) {
long long v0 = ver[i];
if (v0 == fa) continue;
dfs1(v0, now, dep + 1);
siz[now] += siz[v0];
if (siz[v0] > siz[son[now]]) son[now] = v0;
}
}
void dfs2(long long now, long long t) {
top[now] = t;
id[now] = ++idx;
if (!son[now]) return;
dfs2(son[now], t);
for (long long i = head[now]; i; i = nxt[i]) {
long long v0 = ver[i];
if (v0 == father[now] || v0 == son[now]) continue;
dfs2(v0, v0);
}
}
void pushup(long long now) {
Tree[now].sum = Tree[now << 1].sum + Tree[now << 1 | 1].sum;
}
void build(long long now, long long l, long long r) {
Tree[now].lef = l, Tree[now].rig = r;
if (l == r) {
Tree[now].sum = 1;
return;
}
long long mid = l + r >> 1;
build(now << 1, l, mid);
build(now << 1 | 1, mid + 1, r);
pushup(now);
}
void modify(long long now, long long x, long long op) {
if (Tree[now].lef == Tree[now].rig) {
if (op == 1)
Tree[now].sum = 1;
else
Tree[now].sum = 100001;
return;
}
long long mid = Tree[now].lef + Tree[now].rig >> 1;
if (x <= mid)
modify(now << 1, x, op);
else
modify(now << 1 | 1, x, op);
pushup(now);
}
long long query(long long now, long long l, long long r) {
if (l <= Tree[now].lef && Tree[now].rig <= r) return Tree[now].sum;
long long res = 0;
long long mid = Tree[now].lef + Tree[now].rig >> 1;
if (l <= mid) res += query(now << 1, l, r);
if (r > mid) res += query(now << 1 | 1, l, r);
return res;
}
long long query_path(long long u, long long v) {
long long res = 0;
while (top[u] != top[v]) {
if (depth[top[u]] < depth[top[v]]) swap(u, v);
res += query(1, id[top[u]], id[u]);
u = father[top[u]];
}
if (depth[u] < depth[v]) swap(u, v);
res += query(1, id[v] + 1, id[u]);
if (res >= 100001) return -1;
return res;
}
int main() {
long long n;
scanf("%lld", &n);
for (long long i = 1; i < n; i++) {
long long a, b;
scanf("%lld%lld", &a, &b);
add(a, b);
add(b, a);
Link[i] = {a, b};
}
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, 1, n);
long long m;
scanf("%lld", &m);
while (m--) {
long long op;
scanf("%lld", &op);
if (op == 1 || op == 2) {
long long x;
scanf("%lld", &x);
long long u, v;
u = Link[x].sta, v = Link[x].las;
if (depth[u] < depth[v]) swap(u, v);
modify(1, id[u], op);
} else {
long long u, v;
scanf("%lld%lld", &u, &v);
printf("%lld\n", query_path(u, v));
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
vector<int> g[N];
int val[N];
int dp[N];
int path[N][2];
int cnt[N][2];
int mod = 1e9 + 7;
void dfs(int u, int p) {
dp[u] = val[u];
path[u][1] = val[u];
cnt[u][1] = 1;
for (int v : g[u]) {
if (v == p) continue;
dfs(v, u);
dp[u] += 1ll * cnt[v][0] * path[u][1] % mod;
dp[u] %= mod;
dp[u] += 1ll * path[v][0] * cnt[u][1] % mod;
dp[u] %= mod;
dp[u] += 1ll * cnt[v][1] * path[u][0] % mod;
dp[u] %= mod;
dp[u] += 1ll * path[v][1] * cnt[u][0] % mod;
dp[u] %= mod;
path[u][0] += (path[v][1] + 1ll * cnt[v][1] * (-val[u]) % mod) % mod;
path[u][0] %= mod;
path[u][1] += (path[v][0] + 1ll * cnt[v][0] * val[u] % mod) % mod;
path[u][1] %= mod;
cnt[u][1] += cnt[v][0];
cnt[u][1] %= mod;
cnt[u][0] += cnt[v][1];
cnt[u][0] %= mod;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> val[i];
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
int ans = 0;
for (int i = 1; i <= n; i++) {
ans += dp[i];
ans %= mod;
}
ans += ans;
ans %= mod;
for (int i = 1; i <= n; i++) {
ans -= val[i];
ans %= mod;
}
ans += mod;
ans %= mod;
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (long long i = 0; i < static_cast<long long>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
const long long INF = LONG_LONG_MAX / 2;
const double PI = 3.141592653589793238;
const long long MOD = 998244353;
bool many = false;
struct Solve {
Solve() {
long long n;
cin >> n;
cout << "? 1 3" << endl;
cout.flush();
long long tot;
cin >> tot;
cout << "? 1 2" << endl;
long long x;
cin >> x;
cout << "? 2 3" << endl;
long long y;
cin >> y;
vector<long long> a(n);
a[2] = tot - x;
a[1] = y - a[2];
a[0] = x - a[1];
for (long long i = 2; i < n - 1; i++) {
cout << "? " << i + 1 << ' ' << i + 2 << endl;
cin >> x;
a[i + 1] = x - a[i];
}
42;
cout << "! ";
for (long long z : a) cout << z << ' ';
cout << endl;
}
};
signed main() {
long long CASES = 1;
if (many) cin >> CASES;
for (long long i = 1; i <= CASES; i++) {
Solve me;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int findxor(long long int n) {
n = n & 3;
if (n == 0)
return n;
else if (n == 1)
return 1;
else if (n == 2)
return n + 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int tt = 1;
while (tt--) {
long long int n;
cin >> n;
long long int arr[n], num[100001], mx = INT_MIN;
memset(num, 0, sizeof(num));
;
for (long long int i = 0; i <= n - 1; i++) {
cin >> arr[i];
num[arr[i]]++;
mx = max(mx, arr[i]);
}
long long int dp[mx + 1];
dp[0] = 0;
dp[1] = num[1];
for (long long int i = 2; i <= mx; i++)
dp[i] = max(dp[i - 1], dp[i - 2] + i * num[i]);
cout << dp[mx];
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long int inf64 = 1e18 + 5;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long int n, k, i;
cin >> n >> k;
if (k > 2e5) {
cout << -1;
return 0;
}
long long int val = (k * (k + 1)) / 2;
long long int ans = 0;
for (long long int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i >= val) ans = max(ans, i);
if (i >= val) ans = max(ans, n / i);
}
}
if (!ans)
cout << -1;
else {
for (i = 1; i < k; i++) cout << ans * i << ' ';
cout << n - (ans * ((k * (k - 1)) / 2));
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int infint = (int)1e9;
const long long inf = (long long)1e18;
const int MAXN = 2e5 + 7;
const long long MOD = (long long)1e9 + 7;
long long a[MAXN], b[MAXN], n, pwr[32], q, fen[MAXN];
void insert(vector<long long> &cur, long long x) {
for (auto u : cur)
if (u & -u & x) x ^= u;
if (!x) return;
for (long long j = 0; j < cur.size(); j++)
if (x & -x & cur[j]) cur[j] ^= x;
cur.push_back(x);
}
vector<long long> merge(vector<long long> a, vector<long long> b) {
vector<long long> ans = a;
for (auto u : b) insert(ans, u);
return ans;
}
struct value {
long long val;
vector<long long> v;
};
value seg[4 * MAXN];
void build(long long node, long long st, long long en) {
if (en - st < 2) {
insert(seg[node].v, b[st]);
seg[node].val = b[st];
return;
}
long long mid = (st + en) >> 1;
build(node << 1, st, mid);
build(node << 1 | 1, mid, en);
seg[node].v = merge(seg[node << 1].v, seg[node << 1 | 1].v);
seg[node].val = seg[node << 1].val ^ seg[node << 1 | 1].val;
}
value get(long long node, long long st, long long en, long long l,
long long r) {
if (l <= st && en <= r) return seg[node];
if (l >= en || st >= r) {
value empt;
empt.val = 0;
return empt;
}
long long mid = (st + en) >> 1;
value a1 = get(node << 1, st, mid, l, r);
value a2 = get(node << 1 | 1, mid, en, l, r);
value ans;
ans.val = a1.val ^ a2.val;
ans.v = merge(a1.v, a2.v);
return ans;
}
void modify(long long node, long long st, long long en, long long l,
long long k) {
if (en - st < 2) {
long long p = seg[node].val ^ k;
seg[node].v.clear();
insert(seg[node].v, p);
seg[node].val = p;
return;
}
long long mid = (st + en) >> 1;
if (l < mid)
modify(node << 1, st, mid, l, k);
else
modify(node << 1 | 1, mid, en, l, k);
seg[node].v = merge(seg[node << 1].v, seg[node << 1 | 1].v);
seg[node].val = seg[node << 1].val ^ seg[node << 1 | 1].val;
}
void upd_fen(long long v, long long k) {
v++;
for (; v <= n; v += v & -v) fen[v] ^= k;
}
long long get_fen(long long v) {
v++;
long long ans = 0;
for (; v; v -= v & -v) ans ^= fen[v];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (long long i = 0; i < n; i++) cin >> a[i];
b[0] = a[0];
for (long long i = 1; i < n; i++) b[i] = (a[i - 1] ^ a[i]);
for (long long i = 0; i < n; i++) upd_fen(i, b[i]);
pwr[0] = 1;
for (long long i = 1; i < 32; i++) pwr[i] = pwr[i - 1] * 2;
build(1, 0, n);
for (long long i = 0; i < q; i++) {
long long type;
cin >> type;
if (type == 1) {
long long l, r, k;
cin >> l >> r >> k;
l--;
modify(1, 0, n, l, k);
if (r != n) modify(1, 0, n, r, k);
upd_fen(l, k);
if (r != n) upd_fen(r, k);
} else {
long long l, r;
cin >> l >> r;
l--;
value tmp = get(1, 0, n, l + 1, r);
long long p = get_fen(l);
insert(tmp.v, p);
cout << pwr[tmp.v.size()] << "\n";
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, vector<P>> S;
void output(int time, S s){
printf("time %d, inv %d\n", time, s.first);
for(auto vi : s.second) printf("(%d, %d) ", vi.first, vi.second);
cout << endl;
}
int main(){
int n;
while(cin >> n && n){
vector<P> v(n);
for(auto& vi : v) cin >> vi.first >> vi.second;
for(auto& vi : v) if(vi.first < vi.second) swap(vi.first, vi.second);
int inv = 0;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(v[i].first > v[j].first) inv++;
if(v[i].first > v[j].second) inv++;
if(v[i].second > v[j].first) inv++;
if(v[i].second > v[j].second) inv++;
}
}
priority_queue<S, vector<S>, greater<S>> que;
set<vector<P>> used;
que.push(S(inv, v));
used.insert(v);
const int BEAM = 1 << 8;
for(int iter = 0; ; iter++){
priority_queue<S, vector<S>, greater<S>> nque;
if(que.top().first == 0){
cout << iter << endl;
break;
}
for(int i = 0; i < BEAM && !que.empty(); i++){
S s = que.top(); que.pop();
int inv = s.first;
vector<P>& v = s.second;
for(int i = 0; i < n - 1; i++){
if(v[i].first > v[i + 1].second){
vector<P> nv = v;
int ninv = inv - 1;
swap(nv[i].first, nv[i + 1].second);
if(nv[i].first < nv[i].second){
swap(nv[i].first, nv[i].second);
ninv--;
}
if(nv[i + 1].first < nv[i + 1].second){
swap(nv[i + 1].first, nv[i + 1].second);
ninv--;
}
if(!used.count(nv)){
used.insert(nv);
nque.push(S(ninv, nv));
}
}
}
}
que.swap(nque);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> v(n, 0);
for (int i = 0; i < n; ++i) cin >> v[i];
int h = 1, dec = 0, i, j;
for (i = 1; i < n; i = j + 1) {
for (j = i; j < n; ++j) {
if (j + 1 < n && v[j + 1] < v[j]) dec--;
if (dec < 0) {
h++;
dec = j - i;
break;
}
}
}
cout << h << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s = "";
int n, a, b;
cin >> n >> a >> b;
int i = 0;
while (i < b) {
s = s + char(i + 97);
i = i + 1;
}
int x = a - b;
i = 0;
while (i < x) {
s = s + s[i];
i = i + 1;
}
x = n - a;
i = 0;
while (i < x) {
s = s + s[i];
i = i + 1;
}
cout << s << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int nax = 2e5 + 5;
vector<int> g[nax], p(nax, -1), d(nax, 0);
void dfs(int s, int from) {
p[s] = from;
for (auto it : g[s]) {
if (it != from) {
d[it] = d[s] + 1;
dfs(it, s);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (long long i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
int ans = 0;
int a = 1, b = -1, c = -1;
for (long long i = 1; i < n + 1; i++) {
if (d[i] > d[a]) a = i;
}
d.assign(n + 1, 0);
dfs(a, -1);
for (long long i = 1; i < n + 1; i++)
if (b == -1 || d[i] > d[b]) b = i;
ans += d[b];
queue<int> q;
vector<int> vis(n + 1, 0), d1(n + 1, 0);
int z = b;
while (z != -1) {
q.push(z);
vis[z] = 1;
z = p[z];
}
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto it : g[v]) {
if (!vis[it]) {
vis[it] = 1;
q.push(it);
d1[it] = d1[v] + 1;
}
}
}
for (long long i = 1; i < n + 1; i++) {
if (i != a && i != b && (c == -1 || (d1[i] > d1[c]))) c = i;
}
cout << ans + d1[c] << "\n";
cout << a << " " << b << " " << c;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int rd() {
int x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - 48, ch = getchar();
return x;
}
int n = 0, q = 0, mod = 0, cnt = 0;
int w[N + 5], u[N + 5], Pri[N + 5];
bool opt = false;
map<int, int> Phi;
void CalcPri() {
int m = sqrt(mod);
for (int i = 2; i <= m; i++) {
if (!u[i]) Pri[++cnt] = i, u[i] = i;
for (int j = 1; j <= cnt; j++) {
if (Pri[j] > u[i] || Pri[j] > m / i) break;
u[Pri[j] * i] = Pri[j];
}
}
}
int GetPhi(int x) {
int res = x;
for (int i = 1; i <= cnt; i++) {
if (Pri[i] > x) break;
if (x % Pri[i]) continue;
res = res / Pri[i] * (Pri[i] - 1);
while (x % Pri[i] == 0) x /= Pri[i];
}
if (x > 1) res = res / x * (x - 1);
return res;
}
void CalcPhi() {
int m = mod;
while (m > 1) Phi[m] = GetPhi(m), m = Phi[m];
}
int ksm(long long x, int k, int p) {
long long res = 1;
while (k) {
if (k & 1) {
res = res * x;
if (res >= p) opt = true, res %= p;
}
x = x * x;
if (x >= p) opt = true, x %= p;
k >>= 1;
}
return res;
}
int Calc(int l, int r, int p) {
if (l == r) return w[r] % p + (w[r] >= p) * p;
if (p == 1) return 1;
int x = Calc(l + 1, r, Phi[p]);
opt = false;
int res = ksm(w[l], x, p);
return res + opt * p;
}
int main() {
n = rd(), mod = rd();
CalcPri(), CalcPhi();
for (int i = 1; i <= n; i++) w[i] = rd();
q = rd();
for (int i = 1; i <= q; i++) {
int l = rd(), r = rd();
printf("%d\n", Calc(l, r, mod) % mod);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[101][101],b[51][51],n,m;
void rotate() {
int c[m][m];
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) c[j][m-i-1]=b[i][j];
}
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) b[i][j]=c[i][j];
}
}
int main() {
while(cin >> n >> m && n) {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) cin >> a[i][j];
}
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++) cin >> b[i][j];
}
for(int i=0; i<n-m+1; i++) {
for(int j=0; j<n-m+1; j++) {
for(int k=0; k<4; k++) {
rotate();
bool ok=1;
int xx=-1,yy=-1;
for(int x=0; x<m; x++) {
for(int y=0; y<m; y++) {
if(b[x][y]==-1) continue;
if(a[i+x][j+y]!=b[x][y]) ok=0;
if(xx==-1) xx=i+x+1,yy=j+y+1;
}
}
if(ok) {
cout << yy << " " << xx << endl;
goto end;
}
}
}
}
cout << "NA" << endl;
end:;
}
return 0;
}
| 0 |
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
string s, t, x;
vector<string> v;
int main() {
int i, j;
cin>>s>>t;
if(s.size()<t.size()) {cout<<"UNRESTORABLE"; return 0;}
for(i=0; i<=s.size()-t.size(); i++) {
for(j=0; j<t.size(); j++) {
if(s[i+j]!='?' && s[i+j]!=t[j]) break;
}
if(j==t.size()) {
x = s;
for(j=0; j<t.size(); j++) x[i+j] = t[j];
for(j=0; x[j]; j++) if(x[j]=='?') x[j] = 'a';
v.push_back(x);
}
}
if(v.empty()) cout<<"UNRESTORABLE";
else {
sort(v.begin(), v.end());
cout<<v[0];
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(0.0) * 2.0;
const long double eps = 1e-10;
const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1},
{-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
template <class T>
inline T abs1(T a) {
return a < 0 ? -a : a;
}
template <typename t, typename t1>
t min1(t a, t1 b) {
return a < b ? a : b;
}
template <typename t, typename... arg>
t min1(t a, arg... arr) {
return min1(a, min1(arr...));
}
template <typename t, typename t1>
t max1(t a, t1 b) {
return a > b ? a : b;
}
template <typename t, typename... arg>
t max1(t a, arg... arr) {
return max1(a, max1(arr...));
}
inline int jud(double a, double b) {
if (abs(a) < eps && abs(b) < eps)
return 0;
else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps)
return 0;
if (a < b) return -1;
return 1;
}
template <typename t>
inline int jud(t a, t b) {
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
if (na == 0) return 0;
int be = 0, en = na - 1;
if (*a <= *(a + na - 1)) {
if (f_lb == 0)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != 1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != -1)
en = mid;
else
be = mid + 1;
}
if (f_small && jud(*(a + be), val) == 1) be--;
if (!f_small && jud(*(a + be), val) == -1) be++;
} else {
if (f_lb)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != -1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != 1)
en = mid;
else
be = mid + 1;
}
if (!f_small && jud(*(a + be), val) == -1) be--;
if (f_small && jud(*(a + be), val) == 1) be++;
}
return be;
}
template <class T>
inline T lowb(T num) {
return num & (-num);
}
inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(unsigned long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(unsigned int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(unsigned long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
inline int bitmaxl(long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
long long pow(long long n, long long m, long long mod = 0) {
if (m < 0) return 0;
long long ans = 1;
long long k = n;
while (m) {
if (m & 1) {
ans *= k;
if (mod) ans %= mod;
}
k *= k;
if (mod) k %= mod;
m >>= 1;
}
return ans;
}
template <class t1, class t2>
inline void add(t1 &a, t2 b, int mod = -1) {
if (mod == -1) mod = 1000000007;
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
}
template <class t>
void output1(t arr) {
for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' ';
cerr << endl;
}
template <class t>
void output2(t arr) {
for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]);
}
const int maxn = 300010;
int st[maxn * 2];
int lst, rst;
long long t[maxn], loc[maxn], cnt;
pair<long long, long long> sum[maxn];
int n, m;
pair<long long, long long> operator-(const pair<long long, long long> a,
const pair<long long, long long> b) {
return {a.first - b.first, a.second - b.second};
}
double operator*(const pair<long long, long long> a,
const pair<long long, long long> b) {
return 1.0 * a.first * b.second - 1.0 * a.second * b.first;
}
inline pair<long long, long long> GetSum(int l, int r) {
if (l > 0)
return sum[r] - sum[l - 1];
else
return sum[r];
}
inline pair<long long, long long> GetPoint(int rt, int no) {
auto sum = GetSum(t[no], rt);
return make_pair(loc[no], sum.first + sum.second * loc[no]);
}
inline int Jud(int a, int b, int c, int rt) {
const pair<long long, long long> pa = GetPoint(rt, a), pb = GetPoint(rt, b),
pc = GetPoint(rt, c);
return (pa - pb) * (pc - pb) < 0;
}
int main() {
ios_base::sync_with_stdio(0);
scanf("%d%d", &n, &m);
int be = 0, en = n - 1;
lst = maxn, rst = lst;
t[0] = 0, loc[0] = 0;
cnt = 1;
st[lst] = 0;
for (int i = 0; i < m; i++) {
int cate;
scanf("%d", &cate);
int rt = i + 1;
sum[rt] = sum[rt - 1];
if (cate == 1) {
int radd;
scanf("%d", &radd);
be -= radd;
st[--lst] = cnt;
rst = lst;
t[cnt] = i + 1;
loc[cnt] = be;
cnt++;
} else if (cate == 2) {
int radd;
scanf("%d", &radd);
st[++rst] = cnt;
t[cnt] = i + 1;
loc[cnt] = en + 1;
cnt++;
en += radd;
while (rst - lst >= 2 && !Jud(st[rst - 2], st[rst - 1], st[rst], rt)) {
st[rst - 1] = st[rst];
rst--;
}
} else if (cate == 3) {
int a, b;
scanf("%d%d", &a, &b);
sum[rt].first += a + 1ll * b * (-be);
sum[rt].second += b;
}
while (rst > lst &&
GetPoint(rt, st[rst - 1]).second <= GetPoint(rt, st[rst]).second) {
rst--;
}
printf("%lld %lld\n", loc[st[rst]] - be + 1, GetPoint(rt, st[rst]).second);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
int dp[maxn];
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int n = s.size();
string t;
int cnt = 0;
for (int i = 0; i < n - 1; i++)
if (s[i] == ':' and s[i + 1] != ':') cnt++;
if (s[0] != ':') cnt++;
dp[n] = 0;
for (int i = n - 1; i >= 0; i--) {
dp[i] = 0;
if (s[i] == ':') continue;
dp[i] = dp[i + 1] + 1;
}
for (int i = 0; i < n; i++) {
if ((i == 0 and s[i] != ':') or (s[i - 1] == ':' and s[i] != ':')) {
while (dp[i] < 4) {
t += '0';
dp[i]++;
}
while (i < n and s[i] != ':') {
t += s[i];
i++;
}
i--;
continue;
}
if (s[i] == ':' and s[i + 1] != ':') {
t += s[i];
continue;
}
if (i == 0) {
t += "0000";
cnt++;
}
while (cnt < 8) {
t += ":0000";
cnt++;
}
if (i + 2 < n) t += ':';
i++;
}
cout << t << endl;
}
}
| 2 |
#include <bits/stdc++.h>
void geser(char* step, int k) {
if (*step == 'l') {
geser(step + 1, k + 1);
printf("%d\n", k + 1);
}
if (*step == 'r') {
printf("%d\n", k + 1);
geser(step + 1, k + 1);
}
if (*step == NULL) return;
}
int main() {
char s[1000001];
scanf("%s", s);
geser(s, 0);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:16000000")
#pragma warning(disable : 4996)
const int inf = 1 << 25;
const double eps = 1e-9;
int solve(int n, int m) {
if (n > m) swap(n, m);
vector<bool> chosen(n, false);
int ans = 0;
for (int i = (0); i < (n); ++i)
if (!chosen[i]) {
++ans;
int j = i;
chosen[i] = true;
while (1) {
j += 2 * (m - 1);
j %= 2 * (n - 1);
int j1 = j;
if (j1 >= n) {
j1 %= (n - 1);
j1 = n - j1 - 1;
}
if (chosen[j1]) break;
chosen[j1] = true;
}
j = i;
while (1) {
j -= 2 * (m - 1);
j %= 2 * (n - 1);
j += 2 * (n - 1);
j %= 2 * (n - 1);
int j1 = j;
if (j1 >= n) {
j1 %= (n - 1);
j1 = n - j1 - 1;
}
if (chosen[j1]) break;
chosen[j1] = true;
}
}
return ans;
}
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int cor(int n, int m) { return gcd(n - 1, m - 1) + 1; }
int main() {
int n, m;
cin >> n >> m;
cout << solve(n, m);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define times(n, i) uptil(0, n, i)
#define rtimes(n, i) downto((n) - 1, 0, i)
#define upto(f, t, i) for(int i##_to_ = (t), i = (f); i <= i##_to_; i++)
#define uptil(f, t, i) for(int i##_to_ = (t), i = (f); i < i##_to_; i++)
#define downto(f, t, i) for(int i##_to_ = (t), i = (f); i >= i##_to_; i--)
#define downtil(f, t, i) for(int i##_to_ = (t), i = (f); i > i##_to_; i--)
typedef long double LD;
#define long long long
#if defined(EBUG) && !defined(ONLINE_JUDGE)
#define debug true
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 2
#define _LIBCPP_DEBUG2 2
#define ln << endl
#else
#define debug false
#define ln << '\n'
#endif
#define tb << '\t'
#define sp << ' '
vector<char> pyon(int x, int y, int z) {
vector<char> ans;
if(!y && !z) {
ans = vector<char>(x, 'a');
} else if(!z && !x) {
ans = vector<char>(y, 'b');
} else if(!x && !y) {
ans = vector<char>(z, 'c');
} else {
bool zoi = !x;
if(zoi) swap(x, y);
ans.reserve(x + y + z);
int p = z / x, s = z % x, q = y / (x - s), t = y % (x - s), u = x - s - t;
for(char c: pyon(u, t, s)) {
ans.push_back(zoi ? 'b' : 'a');
if(c == 'a') {
times(p, o) ans.push_back('c');
times(q, o) ans.push_back('b');
} else if(c == 'b') {
times(p, o) ans.push_back('c');
times(q+1, o) ans.push_back('b');
} else {
times(p+1, o) ans.push_back('c');
}
}
}
if(debug) { cout << x sp << y sp << z tb; for(char c: ans) cout << c; cout ln; }
return ans;
}
signed main() { // long: 64bit
if(!debug) {
cin.tie(0);
ios::sync_with_stdio(0);
}
int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z);
for(char c: pyon(X, Y, Z)) cout << c;
cout ln;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int N = n;
n = 2 * n - 1;
vector<pair<int, pair<int, int> > > A(n);
long long totA = 0, totB = 0;
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
A[i] = {a, {b, i}};
totA += a, totB += b;
}
sort(A.rbegin(), A.rend());
long long sumA = 0, sumB = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) sumA += A[i].first, sumB += A[i].second.first;
}
cout << "YES\n";
if (sumB * 2 >= totB) {
for (int i = 0; i < n; i += 2) cout << A[i].second.second + 1 << " ";
cout << "\n";
} else {
cout << A[0].second.second + 1 << " ";
for (int i = 1; i < n; i += 2) cout << A[i].second.second + 1 << " ";
cout << "\n";
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int l, r, sol;
unsigned int sieve[300000010 / 20] = {0};
bool isprime(int p) {
return (((sieve[(p - 1) >> 1 >> 5] >> ((p - 1) >> 1 & 31)) & 1) == 0);
}
inline void gen() {
int i, j, k, p;
int bd = sqrt(r / 2 + 1) + 2;
for (i = 1; i <= bd; i++)
if (((sieve[i >> 5] >> (i & 31)) & 1) == 0) {
for (j = 2 * i * (i + 1), k = 2 * i + 1; j < r / 2 + 1; j += k)
sieve[j >> 5] |= 1 << (j & 31);
p = (i * 2 + 1);
if (l <= p && p <= r && (p & 3) == 1) sol++;
}
i = i * 2 + 1;
i = (i + 2) / 4 * 4 + 1;
for (; i <= r; i += 4) {
if (l <= i && isprime(i)) {
sol++;
}
}
}
inline int solve() {
int i;
if (l <= 2 && r >= 2) sol++;
gen();
return sol;
}
int main(void) {
scanf("%d %d", &l, &r);
printf("%d\n", solve());
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int sz = 7005;
bitset<sz> ghgh[100010];
bitset<sz> mob[sz];
vector<int> can;
bool mobius(int x) {
for (int i = 2; i * i <= x; i++) {
int cnt = 0;
while (x % i == 0) {
x /= i;
++cnt;
}
if (cnt > 1) return false;
}
return true;
}
void single(int x, int y) {
ghgh[x].reset();
for (int i = 1; i * i <= y; i++) {
if (y % i == 0) {
ghgh[x][i] = 1;
ghgh[x][y / i] = 1;
}
}
}
void merge(int x, int y, int z) { ghgh[x] = ghgh[y] ^ ghgh[z]; }
void product(int x, int y, int z) { ghgh[x] = ghgh[y] & ghgh[z]; }
int query(int x, int y) { return (ghgh[x] & mob[y]).count() & 1; }
int main(int argc, char const *argv[]) {
for (int i = 1; i <= 7000; i++) {
if (mobius(i)) {
can.push_back(i);
}
}
for (int i = 1; i <= 7000; i++) {
for (auto j : can) {
if ((i * j) > 7000) break;
mob[i][i * j] = 1;
}
}
int n, q;
scanf("%d %d", &n, &q);
for (int i = 1; i <= q; i++) {
int c, x, y, z;
scanf("%d %d %d", &c, &x, &y);
if (c == 1) {
single(x, y);
} else if (c == 2) {
scanf("%d", &z);
merge(x, y, z);
} else if (c == 3) {
scanf("%d", &z);
product(x, y, z);
} else {
printf("%d", query(x, y));
}
}
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
template <class T>
struct ptr_min {
bool operator()(T* a, T* b) { return *a < *b; }
};
template <class T>
struct ptr_max {
bool operator()(T* a, T* b) { return *a > *b; }
};
int _X, _FLAG, _CH;
inline int read_int() {
_X = 0;
_FLAG = 1;
_CH = getchar();
while (_CH < '0' || _CH > '9') {
if (_CH == '-') _FLAG = -1;
_CH = getchar();
}
while (_CH >= '0' && _CH <= '9') {
_X = _X * 10 + _CH - '0';
_CH = getchar();
}
return _X * _FLAG;
}
inline long long read_ll() {
long long x = 0, flag = 1;
_CH = getchar();
while (_CH < '0' || _CH > '9') {
if (_CH == '-') flag = -1;
_CH = getchar();
}
while (_CH >= '0' && _CH <= '9') {
x = x * 10 + _CH - '0';
_CH = getchar();
}
return x * flag;
}
template <typename T>
inline const T GCD(T a, T b) {
T c;
while (a != 0) {
c = a;
a = b % a;
b = c;
}
return b;
}
inline const unsigned long long binom(unsigned long long n,
unsigned long long k) {
if (k > n) return 0;
unsigned long long C = 1;
for (unsigned long long i = 1; i <= k; ++i) C = (C * (n - i + 1)) / i;
return C;
}
template <typename T, typename K>
inline const T stepen(T a, K x, T mod) {
T res = 1;
while (x) {
if (x & 1) {
res *= a;
res %= mod;
}
a *= a;
a %= mod;
x /= 2;
}
return res;
}
template <typename T, typename K>
inline const T stepen(T a, K x) {
T res = 1;
while (x) {
if (x & 1) res *= a;
a *= a;
x /= 2;
}
return res;
}
template <typename T, typename K>
inline const T mod_div(const T lhs, const T rhs, const K mod) {
return (lhs * stepen(rhs, mod - 2, mod)) % mod;
}
long long n, m, k, t;
char flag[120];
int main() {
std::cin >> n >> flag;
for (int i = 0; i < n - 2; ++i) {
if (flag[i] == 'x' && flag[i] == flag[i + 1] && flag[i] == flag[i + 2]) ++m;
}
std::cout << m;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, x;
long long int sum = 0;
cin >> n;
vector<int> bijor, jor, ans;
for (i = 0; i < n; i++) {
cin >> x;
if (x % 2)
bijor.push_back(x);
else
jor.push_back(x);
}
if (jor.size() != 0) {
ans.push_back(2);
jor.pop_back();
}
if (bijor.size() != 0) {
ans.push_back(1);
bijor.pop_back();
}
for (i = 0; i < jor.size(); i++) ans.push_back(2);
for (i = 0; i < bijor.size(); i++) ans.push_back(1);
for (auto it : ans) cout << it << ' ';
cout << endl;
}
| 3 |
#include <bits/stdc++.h>
struct op {
int r, x, p, y, q;
};
static int prog(op *a, op *b, op *i, op *j, int *c, const int n) {
for (int x = 0; x <= 1; x++)
for (int y = 1; y <= 8; y *= 2)
for (int p = 0; p < (x == 0 ? 1 : j - i); p++)
for (int q = 0; q < j - i; q++) {
const int r = x * i[p].r + y * i[q].r;
*j = {.r = r, .x = x, .p = p, .y = y, .q = q};
if (r == n && j - i < *c) {
memcpy(a, i + 1, (j - i) * sizeof(*a));
return *c = j - i;
} else if (r < n && j - i < b - a)
prog(a, b, i, j + 1, c, n);
}
return *c;
}
static int prog(op *a, op *b, const int n) {
int c = INT_MAX;
op ops[1 + b - a];
ops[0] = {.r = 1};
return n == 1 ? 0 : prog(a, b, &ops[0], &ops[1], &c, n);
}
int main(int argc, const char *const argv[]) {
int n;
if (scanf("%d\n", &n) != 1 || n < 1 || 255 < n) {
fprintf(stderr, "scanf: Header IO error.\n");
exit(EXIT_FAILURE);
}
op ops[5];
const int m = prog(&ops[0], &ops[5], n);
printf("%d\n", m);
for (int i = 0; i < m; i++)
if (ops[i].x == 0)
printf("lea e%cx, [%d*e%cx]\n", 'b' + i, ops[i].y, 'a' + ops[i].q);
else
printf("lea e%cx, [e%cx + %d*e%cx]\n", 'b' + i, 'a' + ops[i].p, ops[i].y,
'a' + ops[i].q);
return EXIT_SUCCESS;
}
| 3 |
#include <bits/stdc++.h>
//typedef
//-------------------------#include <bits/stdc++.h>
const double pi = 3.141592653589793238462643383279;
using namespace std;
//conversion
//------------------------------------------
inline int toInt(string s)
{
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x)
{
ostringstream sout;
sout << x;
return sout.str();
}
inline int readInt()
{
int x;
scanf("%d", &x);
return x;
}
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
class UnionFind
{
public:
vector<int> par;
vector<int> siz;
UnionFind(int sz_) : par(sz_), siz(sz_, 1)
{
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
void init(int sz_)
{
par.resize(sz_);
siz.assign(sz_, 1LL);
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
int root(int x)
{
while (par[x] != x)
{
x = par[x] = par[par[x]];
}
return x;
}
bool merge(int x, int y)
{
x = root(x);
y = root(y);
if (x == y)
return false;
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(int x, int y)
{
return root(x) == root(y);
}
int size(int x)
{
return siz[root(x)];
}
};
ll modPow(ll x, ll n, ll mod = MOD)
{
ll res = 1;
while (n)
{
if (n & 1)
res = (res * x) % mod;
res %= mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
#define SIEVE_SIZE 5000000 + 10
bool sieve[SIEVE_SIZE];
void makeSieve()
{
for (int i = 0; i < SIEVE_SIZE; ++i)
sieve[i] = true;
sieve[0] = sieve[1] = false;
for (int i = 2; i * i < SIEVE_SIZE; ++i)
if (sieve[i])
for (int j = 2; i * j < SIEVE_SIZE; ++j)
sieve[i * j] = false;
}
bool isprime(ll n)
{
if (n == 0 || n == 1)
return false;
for (ll i = 2; i * i <= n; ++i)
if (n % i == 0)
return false;
return true;
}
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit()
{
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++)
{
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long extGCD(long long a, long long b, long long &x, long long &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m)
{
return (a % m + m) % m;
}
// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m)
{
long long x, y;
extGCD(a, m, x, y);
return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
ll GCD(ll a, ll b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
typedef vector<ll> vec;
typedef vector<vec> mat;
mat mul(mat &A, mat &B)
{
mat C(A.size(), vec((int)B[0].size()));
for (int i = 0; i < A.size(); ++i)
{
for (int k = 0; k < B.size(); ++k)
{
for (int j = 0; j < B[0].size(); ++j)
{
C[i][j] = (C[i][j] + A[i][k] * B[k][j] % MOD) % MOD;
}
}
}
return C;
}
mat matPow(mat A, ll n)
{
mat B(A.size(), vec((int)A.size()));
for (int i = 0; i < A.size(); ++i)
{
B[i][i] = 1;
}
while (n > 0)
{
if (n & 1)
B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll N, k;
cin >> N >> k;
ll pos = 0;
for (int i = 0; i < N - 1; i++)
{
pos += pos / (k - 1) + 1;
}
cout << pos << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> prime;
int pri[100001];
void primefind() {
for (int i = 2; i < 100001; i++) {
int han = 0;
for (int j = 0; j < prime.size(); j++) {
if (prime[j] * prime[j] > i) {
break;
}
if (i % prime[j] == 0) {
han = 1;
break;
}
}
if (han == 0) {
prime.push_back(i);
pri[i] = 1;
}
}
}
vector<pair<int, int> > bun(int a) {
vector<pair<int, int> > ret;
for (int i = 0; i < prime.size(); i++) {
if (prime[i] * prime[i] > a) {
break;
}
if (a % prime[i] == 0) {
ret.push_back(make_pair(prime[i], 0));
for (;;) {
if (a % prime[i] == 0) {
ret[ret.size() - 1].second++;
a /= prime[i];
} else {
break;
}
}
}
}
if (a != 1) {
ret.push_back(make_pair(a, 1));
}
return ret;
}
vector<int> tob(vector<pair<int, int> > vec) {
vector<int> ret;
ret.push_back(1);
for (int i = 0; i < vec.size(); i++) {
vector<int> zan;
int now = 1;
for (int j = 0; j <= vec[i].second; j++) {
for (int k = 0; k < ret.size(); k++) {
zan.push_back(ret[k] * now);
}
now *= vec[i].first;
}
ret = zan;
}
return ret;
}
long long mod = 1000000007;
long long po(long long a, long long b) {
vector<int> vec;
for (;;) {
if (b == 0) {
break;
}
vec.push_back(b % 2);
b /= 2;
}
reverse(vec.begin(), vec.end());
long long ret = 1;
for (int i = 0; i < vec.size(); i++) {
ret *= ret;
ret %= mod;
if (vec[i]) {
ret *= a;
ret %= mod;
}
}
return ret;
}
int main() {
primefind();
int num;
scanf("%d", &num);
vector<int> vec;
for (int i = 0; i < num; i++) {
int zan;
scanf("%d", &zan);
vec.push_back(zan);
}
sort(vec.begin(), vec.end());
long long ret = 1;
for (int i = vec[vec.size() - 1]; i >= 2; i--) {
long long sum = 1;
long long sum2 = 1;
vector<int> zan = tob(bun(i));
sort(zan.begin(), zan.end());
int bef = vec.size();
for (int j = zan.size() - 1; j >= 0; j--) {
int low = lower_bound(vec.begin(), vec.end(), zan[j]) - vec.begin();
sum *= po(j + 1, bef - low);
bef = low;
sum %= mod;
}
bef = vec.size();
for (int j = zan.size() - 2; j >= 0; j--) {
int low = lower_bound(vec.begin(), vec.end(), zan[j]) - vec.begin();
sum2 *= po(j + 1, bef - low);
bef = low;
sum2 %= mod;
}
ret = (ret + mod + sum - sum2) % mod;
}
printf("%I64d\n", ret);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int n, m;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string a, b;
cin >> n >> m;
cin >> a >> b;
long long int mx = max(n, m);
for (int i = 0; i < mx - n; i++) {
a = "0" + a;
}
for (int i = 0; i < mx - m; i++) {
b = "0" + b;
}
long long int setbits[mx];
if (b[0] == '1')
setbits[0] = 1;
else
setbits[0] = 0;
for (int i = 1; i < mx; i++) {
setbits[i] = setbits[i - 1];
if (b[i] == '1') setbits[i]++;
}
long long int pw = 1, ans = 0;
for (long long int i = mx - 1; i >= 0; i--) {
if (a[i] == '1') {
ans = ((ans % 998244353) + (setbits[i] * pw) % 998244353) % 998244353;
}
pw = (pw << 1) % 998244353;
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
int n, m, i;
int step, q[1000010][3], visited[2][100001];
char ar[2][100001];
int f = -1, r = -1, k = 0;
int bfs(int v, int w, int water) {
if ((w + 1 >= n) || (w + step >= n)) {
printf("YES");
return 0;
}
if ((w - 2) >= water && ar[v][w - 1] == '-') {
if (visited[v][w - 1] != 1) {
r++;
q[r][0] = v;
q[r][1] = w - 1;
q[r][2] = water + 1;
visited[v][w - 1] = 1;
}
}
if (ar[v][w + 1] == '-') {
if (visited[v][w + 1] != 1) {
r++;
q[r][0] = v;
q[r][1] = w + 1;
q[r][2] = water + 1;
visited[v][w + 1] = 1;
}
}
if (ar[(v + 1) % 2][w + step] == '-') {
if (visited[(v + 1) % 2][w + step] != 1) {
r++;
q[r][0] = (v + 1) % 2;
q[r][1] = w + step;
q[r][2] = water + 1;
visited[(v + 1) % 2][w + step] = 1;
}
}
if (f < r) {
f++;
return bfs(q[f][0], q[f][1], q[f][2]);
}
return 1;
}
int main() {
int j, res;
char temp;
scanf("%i", &n);
scanf("%i", &step);
for (i = 0; i < 2; i++) {
j = 0;
while (j < n) {
scanf("%c", &temp);
if (temp != '\n') {
ar[i][j] = temp;
j++;
}
}
}
res = bfs(0, 0, 0);
if (res == 1) printf("NO");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXM = 50;
int n, m;
int main() {
scanf("%d%d", &n, &m);
int ans = 0;
if (m % 2) {
for (int i = 1; i <= n; i++) {
if (i % 2)
ans += m / 2 + 1;
else
ans += m / 2;
}
} else
ans = m / 2 * n;
if (n == 1) ans = m;
if (m == 1) ans = n;
if (m == 2) {
ans = 0;
for (int i = 1; i <= n; i = i + 4) {
if (i == n)
ans += 2;
else
ans += 4;
}
}
if (n == 2) {
ans = 0;
for (int i = 1; i <= m; i = i + 4) {
if (i == m)
ans += 2;
else
ans += 4;
}
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int w = 0;
char arr[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) cin >> arr[i][j];
}
if (arr[0][0] == arr[2][2] && arr[0][1] == arr[2][1] &&
arr[0][2] == arr[2][0] && arr[1][0] == arr[1][2]) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int t, n, m, adj[500][500], deg[500], vis[500];
vector<pair<int, int> > ans;
void dfs(int node) {
vis[node] = 1;
for (int i = 0; i <= n; i++) {
if (adj[i][node] == 0) continue;
adj[i][node] = 0;
adj[node][i] = 0;
if (node && i) ans.push_back({node, i});
dfs(i);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
memset(adj, 0, sizeof adj);
memset(deg, 0, sizeof deg);
memset(vis, 0, sizeof vis);
ans.clear();
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u][v] = 1;
adj[v][u] = 1;
deg[u]++;
deg[v]++;
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (deg[i] & 1) {
adj[0][i] = 1;
adj[i][0] = 1;
} else
cnt++;
}
for (int i = 0; i <= n; i++) {
if (vis[i]) continue;
dfs(i);
}
cout << cnt << '\n';
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << '\n';
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T sqr(T x) {
return x * x;
}
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
template <class T>
inline T bigmod(T p, T e, T M) {
if (e == 0) return 1;
if (e % 2 == 0) {
long long int t = bigmod(p, e / 2, M);
return (T)((t * t) % M);
}
return (T)((long long int)bigmod(p, e - 1, M) * (long long int)p) % M;
}
template <class T>
inline T bigexp(T p, T e) {
if (e == 0) return 1;
if (e % 2 == 0) {
long long int t = bigexp(p, e / 2);
return (T)((t * t));
}
return (T)((long long int)bigexp(p, e - 1) * (long long int)p);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
int dx4[] = {1, 0, -1, 0};
int dy4[] = {0, 1, 0, -1};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
int nx8[] = {1, 1, -1, -1, 2, 2, -2, -2};
int ny8[] = {2, -2, 2, -2, 1, -1, 1, -1};
int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n, k, a[1005], dist[2005], tmp, b[1005];
queue<int> q;
int u, v;
int main() {
scanf("%d %d", &n, &k);
for (__typeof(k) i = (1); i <= (k); i++) {
scanf("%d", &tmp);
b[tmp] = 1;
}
int c = 0;
for (int i = 0; i <= 1000; i++)
if (b[i] == 1) a[++c] = i - n;
for (int i = 0; i <= 2000; i++) dist[i] = 1000000007;
q.push(1000);
while (!q.empty()) {
u = q.front();
q.pop();
for (int i = 1; i <= c; i++) {
if (u + a[i] >= 0 && u + a[i] <= 2000) {
v = u + a[i];
tmp = dist[u];
if (tmp == 1000000007) tmp = 0;
if (dist[v] > tmp + 1) {
dist[v] = tmp + 1;
q.push(v);
}
}
}
}
if (dist[1000] == 1000000007)
printf("%d", -1);
else
printf("%d", dist[1000]);
return 0;
}
| 3 |
#include<iostream>
using namespace std;
int a,b;
int main()
{
cin>>a>>b;
if(a<=8 && b<=8) cout<<"Yay!"<<endl;
else cout<<":("<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
char ch = getchar();
x = 0;
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
}
const int N = 1e5 + 5, M = N << 1;
int n, tot = 1, lst = 1, ch[M][26], link[M], len[M];
char a[N];
void extend(int c) {
int p = lst, np = lst = ++tot;
len[np] = len[p] + 1;
for (; p && !ch[p][c]; p = link[p]) ch[p][c] = np;
if (!p) {
link[np] = 1;
return;
}
int q = ch[p][c];
if (len[q] == len[p] + 1) {
link[np] = q;
return;
}
int nq = ++tot;
memcpy(ch[nq], ch[q], 26 << 2), link[nq] = link[q];
len[nq] = len[p] + 1, link[np] = link[q] = nq;
for (; p && ch[p][c] == q; p = link[p]) ch[p][c] = nq;
}
vector<int> g[M];
int m = 1e5, sa[N], rk[N], c[N], id[N], t[N], h[N], rrk[N];
bool cmp(int x, int y, int w) {
return rrk[x] == rrk[y] && rrk[x + w] == rrk[y + w];
}
void getsa() {
for (int i = 1; i <= n; ++i) ++c[rk[i] = a[i]];
for (int i = 1; i <= m; ++i) c[i] += c[i - 1];
for (int i = n; i >= 1; --i) sa[c[rk[i]]--] = i;
int p = 0;
for (int w = 1; w < n; w <<= 1, m = p, p = 0) {
for (int i = n; i > n - w; --i) id[++p] = i;
for (int i = 1; i <= n; ++i)
if (sa[i] > w) id[++p] = sa[i] - w;
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; ++i) ++c[t[i] = rk[id[i]]];
for (int i = 1; i <= m; ++i) c[i] += c[i - 1];
for (int i = n; i >= 1; --i) sa[c[t[i]]--] = id[i];
memcpy(rrk, rk, sizeof(rk));
p = 0;
for (int i = 1; i <= n; ++i) rk[sa[i]] = cmp(sa[i], sa[i - 1], w) ? p : ++p;
}
for (int i = 1, j = 0; i <= n; ++i) {
if (j) --j;
while (a[i + j] == a[sa[rk[i] - 1] + j]) ++j;
h[rk[i]] = j;
}
}
int st[N][20], lg[N];
void getst() {
for (int i = 1; i <= n; ++i) st[i][0] = h[i];
for (int j = 1; j <= 17; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; ++i)
st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
}
int ask(int l, int r) {
int t = lg[r - l + 1];
return min(st[l][t], st[r - (1 << t) + 1][t]);
}
struct yxl {
set<int> s;
long long val;
void init() { s.insert(0), s.insert(n + 1); }
void ins(int x) {
if (s.count(x)) return;
int l = *(--s.lower_bound(x));
int r = *s.upper_bound(x);
val += n - sa[x] + 1;
if (l >= 1) val -= ask(l + 1, x);
if (r <= n) val -= ask(x + 1, r);
if (l >= 1 && r <= n) val += ask(l + 1, r);
s.insert(x);
}
} s[M];
long long res;
void dfs(int u) {
for (int v : g[u]) {
dfs(v);
if (s[v].s.size() > s[u].s.size()) swap(s[u], s[v]);
for (int x : s[v].s) s[u].ins(x);
}
res += s[u].val * (len[u] - len[link[u]]);
}
signed main() {
scanf("%s", a + 1);
n = strlen(a + 1);
for (int i = 1; i <= n; ++i) extend(a[i] - 'a');
getsa();
getst();
for (int i = 1; i <= tot; ++i) res += len[i] - len[link[i]];
int now = 1;
for (int i = 1; i <= n; ++i) now = ch[now][a[i] - 'a'];
for (int i = 1; i <= tot; ++i)
if (i != now) res += len[i] - len[link[i]];
for (int i = 1; i <= tot; ++i) g[link[i]].push_back(i), s[i].init();
now = 1;
s[now].ins(rk[2]);
for (int i = 1; i <= n - 2; ++i) {
s[now = ch[now][a[i] - 'a']].ins(rk[i + 2]);
}
dfs(1);
res += s[1].val;
printf("%lld\n", res + 2);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n + 1][n + 1];
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) a[i][j] = -1;
int odd_num = 1;
int mid;
mid = (n + 1) / 2;
for (int i = 1; i < mid; i++) {
for (int j = mid - i + 1; j <= mid + i - 1; j++) {
a[i][j] = odd_num;
odd_num += 2;
a[n - i + 1][j] = odd_num;
odd_num += 2;
}
}
for (int i = 1; i <= n; i++) {
a[mid][i] = odd_num;
odd_num += 2;
}
int even_num = 2;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] == (-1)) {
a[i][j] = even_num;
even_num += 2;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cout << a[i][j] << " ";
cout << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1, ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') {
f = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
bitset<2005> a[2005], inv[2005];
int n, flag;
inline void guess() {
for (int i = 1; i <= n; i++) {
int tot = i;
while (!a[tot][i] && tot <= n) tot++;
if (tot > n) {
flag = 1;
break;
}
swap(a[i], a[tot]), swap(inv[i], inv[tot]);
for (int j = 1; j <= n; j++)
if (j != i && a[j][i]) a[j] ^= a[i], inv[j] ^= inv[i];
}
}
int m;
int p1[500005], p2[500005];
int main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) p1[i] = read(), p2[i] = read();
for (int i = 1; i <= m; i++) a[p1[i]][p2[i]] = 1;
for (int i = 1; i <= n; i++) inv[i][i] = 1;
guess();
for (int i = 1; i <= m; i++) {
if (flag || inv[p2[i]][p1[i]])
puts("NO");
else
puts("YES");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 5;
const long long MAXC = 1e6 + 5;
const long long hmod[2] = {(long long)1e9 + 7, (long long)1e9 + 9};
const long long ALFABET[2] = {29, 31};
const long long mod = 1e9 + 7;
long long dp[2][MAXC];
long long inverse[2];
vector<long long> pot[2];
vector<long long> H[2][MAXN];
vector<long long> nast[MAXN];
vector<long long> sorted[MAXN];
long long pom[MAXC];
string s[MAXN];
long long ans = 0;
long long fastpow(long long a, long long b, long long wsk) {
long long wynik = 1;
if (a == 0) return 0;
while (b) {
if (b & 1) wynik = (wynik * a) % hmod[wsk];
a = (a * a) % hmod[wsk];
b >>= 1;
}
return wynik;
}
long long inv(long long x, long long wsk) {
return fastpow(x, hmod[wsk] - 2, wsk);
}
long long add(long long a, long long b) {
a += b;
while (a >= mod) a -= mod;
return a;
}
long long sub(long long a, long long b) {
a -= b;
while (a < 0) a += mod;
return a;
}
inline long long gethash(long long r, long long pos, long long wsk,
long long id) {
if (r < pos) return (H[wsk][id][r] * pot[wsk][MAXC - 1 - r]) % hmod[wsk];
long long haha = (long long)H[wsk][id].size() - 1;
long long part1 = H[wsk][id][pos - 1];
long long part2 = H[wsk][id][min(haha, r + 1)] - H[wsk][id][pos];
while (part2 < 0) part2 += hmod[wsk];
part2 *= inverse[wsk];
long long Hasz = (part1 + part2) % hmod[wsk];
Hasz = (Hasz * pot[wsk][MAXC - 1 - r]) % hmod[wsk];
return Hasz;
}
inline char wez(long long ktora, long long pos, long long id) {
if (ktora < pos) return s[id][ktora - 1];
if (ktora + 1 <= (long long)s[id].size()) return s[id][ktora];
return '0';
}
inline bool check(long long id1, long long lit1, long long id2,
long long lit2) {
lit1 = sorted[id1][lit1];
lit2 = sorted[id2][lit2];
long long len1 = (long long)s[id1].size();
long long len2 = (long long)s[id2].size();
long long ip = 0, ik = min(len1, len2) + 1;
while (ip + 1 < ik) {
long long mid = (ip + ik) >> 1;
if (gethash(mid, lit1, 0, id1) == gethash(mid, lit2, 0, id2) &&
gethash(mid, lit1, 1, id1) == gethash(mid, lit2, 1, id2))
ip = mid;
else
ik = mid;
}
return wez(ip + 1, lit1, id1) <= wez(ip + 1, lit2, id2);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (long long i = 0; i < 2; i++) {
pot[i].resize(MAXC);
pot[i][0] = 1;
for (long long j = 1; j <= MAXC - 1; j++) {
pot[i][j] = (pot[i][j - 1] * ALFABET[i]) % hmod[i];
}
}
inverse[0] = inv(ALFABET[0], 0);
inverse[1] = inv(ALFABET[1], 1);
for (long long i = 1; i <= n; i++) {
cin >> s[i];
for (long long j = 0; j < 2; j++) {
H[j][i].resize((long long)s[i].size() + 1);
H[j][i][0] = 0;
for (long long k = 1; k <= (long long)s[i].size(); k++) {
H[j][i][k] =
(H[j][i][k - 1] + pot[j][k] * (s[i][k - 1] - 'a' + 1)) % hmod[j];
}
}
nast[i].resize((long long)s[i].size() + 1);
nast[i][(long long)s[i].size()] = (long long)s[i].size();
for (long long k = (long long)s[i].size() - 1; k >= 1; k--) {
if (s[i][k - 1] != s[i][k]) {
nast[i][k] = k + 1;
} else
nast[i][k] = nast[i][k + 1];
}
long long l = 1, r = (long long)s[i].size();
for (long long k = 1; k <= (long long)s[i].size(); k++) {
if (s[i][k - 1] < s[i][nast[i][k] - 1])
pom[r--] = k;
else
pom[l++] = k;
}
long long wsk = 0;
sorted[i].resize((long long)s[i].size() + 2);
for (long long k = 1; k <= (long long)s[i].size(); k++) {
sorted[i][++wsk] = pom[k];
if (pom[k] == (long long)s[i].size())
sorted[i][++wsk] = (long long)s[i].size() + 1;
}
}
for (long long i = 1; i <= (long long)s[1].size() + 1; i++) {
dp[1][i] = 1;
}
for (long long i = 2; i <= n; i++) {
long long sum = 0;
long long wsk = 0;
long long rak1 = i % 2;
long long rak2 = (i % 2) ^ 1;
for (long long j = 0; j < (long long)s[i].size() + 2; j++) dp[rak1][j] = 0;
for (long long j = 1; j <= (long long)s[i].size() + 1; j++) {
while (wsk + 1 <= (long long)s[i - 1].size() + 1 &&
check(i - 1, wsk + 1, i, j)) {
sum = (sum + dp[rak2][wsk + 1]) % mod;
wsk++;
}
dp[rak1][j] = sum;
}
}
long long rak1 = n % 2;
for (long long i = 1; i <= (long long)s[n].size() + 1; i++)
ans = (ans + dp[rak1][i]) % mod;
cout << ans;
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
const lli N = 1000009, K = 'z' - 'a' + 1;
string s, t;
lli n, k, a[K], b[K];
void Solve() {
for(int i = 0; i < K - 1; i++) {
while(a[i] > b[i] && a[i] >= k) {
a[i] -= k;
a[i + 1] += k;
}
}
for(int i = 0; i < K; i++) {
if(a[i] != b[i]) {
cout<<"No"<<endl;
return ;
}
}
cout<<"Yes"<<endl;
}
void Inp() {
lli ntest;
cin>>ntest;
while(ntest--) {
cin>>n>>k>>s>>t;
for(int i = 0; i < K; i++) {
a[i] = 0;
b[i] = 0;
}
for(int i = 0; i < n; i++) {
a[s[i] - 'a']++;
b[t[i] - 'a']++;
}
Solve();
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
//freopen("inp.inp","r",stdin);
Inp();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool judge(string str1, string pair1) {
string str = str1, pair = pair1;
int lendi = str.size() - pair.size();
if (lendi < 0) lendi = -lendi;
string toadd = "";
for (int i = 0; i < lendi; i++) {
toadd += "0";
}
if (str.size() < pair.size()) {
str = toadd + str;
} else
pair = toadd + pair;
for (int j = 0; j < pair.size(); j++) {
if (pair[j] - '0' != ((str[j] - '0') & 1)) return false;
}
return true;
}
int main() {
priority_queue<int> que;
int n;
scanf("%d", &n);
int tmp;
long long ans = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &tmp);
tmp -= i;
que.push(tmp);
if (que.top() > tmp) {
ans += que.top() - tmp;
que.pop();
que.push(tmp);
}
}
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef double Weight;
Weight INF = 1000000000;
struct Edge{
int src, dest; Weight weight;
bool operator < (const Edge &rhs) const {return weight > rhs.weight;}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
void add_edge(Graph &g, int src, int dest, Weight weight) {
g[src].push_back((Edge){src, dest, weight});
}
// Dijkstra (Verified: AOJ2005)
void dijkstra(Graph &g, Array &d, int s) {
d.assign(g.size(), INF);
d[s] = 0;
typedef pair<Weight,int> P;
priority_queue<P, vector<P>, greater<P> > que;
que.push(P(0, s));
while (!que.empty()) {
Weight dist = que.top().first;
int v = que.top().second;
que.pop();
if (d[v] < dist) continue;
REP(i, g[v].size()) {
Edge e = g[v][i];
if (d[e.dest] > d[v] + e.weight) {
d[e.dest] = d[v] + e.weight;
que.push(P(d[e.dest], e.dest));
}
}
}
}
int cnv(int now, int prev, int speed, int n) {
return now*(n+1)*31+prev*31+speed;
}
int main() {
while(1){
int n,m;
cin>>n>>m;
if(!n) break;
int s,t;
cin>>s>>t;
--s;--t;
Graph g(n*(n+1)*31);
REP(i,m){
int x,y,d,c;
cin>>x>>y>>d>>c;
--x;--y;
REP(j,c){
double v = j+1;
int ct[2] = {x,y};
REP(k,n+1) {
REP(l,2) {
int src = ct[l], dest = ct[1-l];
if (dest!=k) {
add_edge(g, cnv(src,k,v,n), cnv(dest,src,v,n), d/v);
add_edge(g, cnv(src,k,v-1,n), cnv(dest,src,v,n), d/v);
if (v<30)
add_edge(g, cnv(src,k,v+1,n), cnv(dest,src,v,n), d/v);
}
}
}
}
}
Array d(n*(n+1)*31);
dijkstra(g, d, cnv(s,n,0,n));
double mnt = INF;
REP(i,n+1){
mnt = min(mnt, d[cnv(t,i,1,n)]);
}
if (mnt < INF)
cout << setprecision(10) << fixed << mnt << endl;
else
cout << "unreachable" << endl;
}
return 0;
}
| 0 |
#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
#include<cassert>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define N 262144
int dat[N*2+10],ad[N*2+10];
int inf=1000000000;
//[a,b)の最小値
//外からは(a,b,0,0,N)として呼ぶ
int query(int a,int b,int k=0,int l=0,int r=N){
if(r<=a || b<=l) return inf;
if(a<=l && r<=b) return dat[k]+ad[k];
int vl=query(a,b,k*2+1,l,(l+r)/2);
int vr=query(a,b,k*2+2,(l+r)/2,r);
return ad[k]+min(vl,vr);
}
//[a,b)に加算する
void add(int a,int b,int x,int k=0,int l=0,int r=N){
if(r<=a || b<=l) return;
if(a<=l && r<=b) ad[k]+=x;
else{
add(a,b,x,k*2+1,l,(l+r)/2);
add(a,b,x,k*2+2,(l+r)/2,r);
dat[k]=min(dat[k*2+1]+ad[k*2+1],dat[k*2+2]+ad[k*2+2]);
}
}
void update(int k,int a){
k+=N-1;
dat[k]=a;
while(k>0){
k=(k-1)/2;
dat[k]=min(dat[k*2+1]+ad[k*2+1],dat[k*2+2]+ad[k*2+2]);
}
return;
}
//今kにどれくらいの数が加算されているのか
int cal(int k){
k+=N-1;
int ret=ad[k];
while(k>0){
k=(k-1)/2;ret+=ad[k];
}
return ret;
}
int b[200100];
vector<pint> v;
int main()
{
int n,q,l,r;
cin>>n;
rep(i,n) cin>>b[i];
cin>>q;
rep(i,q){
cin>>l>>r;l--;
v.pb(mp(l,r));
}
v.pb(mp(0,0));v.pb(mp(n,n));sort(All(v));
memset(ad,0,sizeof(ad));
rep(i,N*2+10) dat[i]=inf;
update(0,0);
rep(i,q+1){
REP(j,v[i].fi,v[i+1].fi){
if(b[j]==0) add(j+1,n+10,1);
else add(0,j+1,1);
}
if(i>=q) break;
l=v[i+1].fi;r=v[i+1].se;
int now=query(0,r+1),su=cal(r);
update(r,now-su);
}
cout<<query(0,n+10)<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL) INF;
const int MAX = 333;
const int MIN = 100;
double DP[MIN][MAX][MAX];
int L[MAX];
int n, c;
int nc;
double solve()
{
FOR (i, 0, 1<<n)
{
if (!(i & 1)) continue;
FOR (j, 0, nc)
{
FOR (k, j, nc + 1)
{
DP[i][j][k] = 0;
}
}
}
DP[1][0][L[0] * n] = 1;
FOR (i, 0, 1<<n)
{
if (!(i&1)) continue;
FOR (j, 0, nc)
{
FOR (k, j, nc + 1)
{
if (j < k)
{
DP[i][j+1][k] += DP[i][j][k];
}
int ind = j % n;
if (i & (1<<ind)) continue;
DP[i | (1<<ind)][j][min(nc, max(k, j + L[ind] * n))] += DP[i][j][k] / c;
}
}
}
double res = DP[(1<<n)-1][nc-1][nc];
return res;
}
int main()
{
//freopen("in.txt", "r", stdin);
//ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>c;
nc = n * c;
FOR (i, 0, n)
{
cin>>L[i];
}
sort(L, L+n);
reverse(L, L+n);
reverse(L+1, L+n);
int cnt = 0;
double res = 0;
do
{
res += solve();
cnt++;
// cout<<cnt<<' '<<res<<endl;
} while(next_permutation(L+1, L+n));
res /= cnt;
printf("%.11f\n", res);
}
| 0 |
#include <bits/stdc++.h>
#define int long long
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
#define DEBUG(x) cerr << #x << " " << x << endl
using namespace std;
int magic(int A) {
for(int k = 0; ; ++k) {
if(A <= k * (k + 1) / 2) return k;
}
return -1;
}
string paren(int a, int n) {
// DEBUG(a);
// DEBUG(n);
if(a == n * (n + 1) / 2) {
string ret;
REP(i,n) ret += ")";
REP(i,n) ret += "(";
return ret;
}
else {
int rem = a - n;
int k = magic(rem);
string ret = ")";
for(int i = 0; i < n - 1 - k; ++i) ret += "()";
ret += paren(rem, k);
ret += "(";
return ret;
}
}
signed main() {
int A; cin >> A;
int k = magic(A);
cout << paren(A, k) << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> m;
int n, i, x, ans = 0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> x;
if (x == 0) continue;
if (m.find(x) == m.end()) {
m[x]++;
ans++;
}
}
cout << ans;
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
int main() {
int N;
cin >> N;
int minim = 100;
int maxim = 0;
vector<int> cnt(101, 0);
for(int i=0; i<N; i++) {
int p;
cin >> p;
cnt[p]++;
minim = min(minim, p);
maxim = max(maxim, p);
}
bool ok = true;
if(cnt[minim] >= 3) ok = false;
for(int i=minim+1; i<=maxim; i++) {
if(cnt[i] < 2) ok = false;
}
if(cnt[minim] == 2 && maxim != minim * 2 - 1) ok = false;
if(cnt[minim] == 1 && maxim != minim * 2) ok = false;
if(ok) cout << "Possible" << endl;
else cout << "Impossible" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, x, c = 0, nc = 0;
cin >> n;
vector<int> ans;
for (int i = 0; i < n; ++i) {
cin >> x;
if (x < 0) {
nc++;
if (nc >= 3) {
ans.emplace_back(c);
nc = 1;
c = 0;
}
}
c++;
}
ans.emplace_back(c);
cout << ans.size() << "\n";
for (int &i : ans) cout << i << " ";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int x = 0, y = 0;
void solve(char c) {
if (c == 'N') {
y++;
} else if (c == 'S') {
y--;
} else if (c == 'W') {
x--;
} else {
x++;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int a, b;
int t;
cin >> t;
while (t--) {
string s;
int ans = 0, a = 0, b = 0;
cin >> s;
int ls = s.length();
map<pair<pair<int, int>, pair<int, int>>, bool> mp;
for (int i = 0; i < ls; i++) {
a = x, b = y;
solve(s[i]);
if (mp[{{a, b}, {x, y}}] || mp[{{x, y}, {a, b}}]) {
ans++;
} else {
ans += 5;
}
mp[{{a, b}, {x, y}}] = 1;
}
cout << ans << '\n';
}
return 0;
}
| 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.