solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <cstdio>
long long gcd(long long a,long long b)
{
return b?gcd(b,a%b):a;
}
int main()
{
long long n,x;
scanf("%lld%lld",&n,&x);
long long d=gcd(n,x);
printf("%lld\n",(n-d)*3);
return 0;
}
| 0 |
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const long long lim = 40000000;
const long long inf = 0x3f3f3f3f3f3f3f3f;
signed main(void)
{
int n, m, s; cin >> n >> m >> s;
vector<pair<pair<int, int>, int > > edge;
for (int i = 0, u, v, w; i < m; ++i)
{
cin >> u >> v >> w;
edge.push_back(make_pair(make_pair(u, v), w));
}
vector<long long> dis(n); for (auto &i : dis)i = inf; dis[s] = 0;
for (int i = 0; i < n; ++i)for (auto j : edge)
if (dis[j.first.second] > dis[j.first.first] + j.second)
dis[j.first.second] = dis[j.first.first] + j.second;
for (auto i : edge)if (dis[i.first.second] > dis[i.first.first] + i.second)
if (dis[i.first.second] <= lim)return cout << "NEGATIVE CYCLE" << endl, 0;
for (auto i : dis)if (i < lim)cout << i << endl; else cout << "INF" << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXK = 5000;
const int PRIMESIZE = 669;
int n;
vector<int> prime;
map<int, int> primeIndex;
int number[MAXK + 1];
bool isPrime[MAXK + 1];
vector<int> factor[MAXK + 1];
vector<int> power[MAXK + 1];
long long int dist[MAXK + 1];
int fPower[MAXK + 1][PRIMESIZE];
int fPowerIndex[MAXK + 1];
void prep();
void fastIO();
void sieve();
void init();
void read();
long long int answer();
long long int getSum();
int nextPrime(int);
void popLargestFactor(int);
int main() {
prep();
read();
cout << answer() << endl;
return 0;
}
void prep() {
fastIO();
sieve();
init();
}
void fastIO() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void sieve() {
for (int n = 2; n <= MAXK; n++) isPrime[n] = true;
int ind = 0;
for (int n = 0; n <= MAXK; n++) {
if (isPrime[n]) {
prime.push_back(n);
primeIndex[n] = ind++;
int m = n;
while (m <= MAXK) {
factor[m].push_back(n);
int c = 0;
int k = m;
while (k % n == 0) {
k /= n;
c++;
}
power[m].push_back(c);
isPrime[m] = false;
m += n;
}
}
}
}
void init() {
dist[0] = dist[1] = 0;
fPowerIndex[0] = -1;
fPowerIndex[1] = -1;
for (int n = 2; n <= MAXK; n++) {
dist[n] = dist[n - 1];
fPowerIndex[n] = fPowerIndex[n - 1];
for (int i = 0; i < PRIMESIZE; i++) fPower[n][i] = fPower[n - 1][i];
for (int i = 0; i < power[n].size(); i++) {
dist[n] += power[n][i];
int p = factor[n][i];
int ind = primeIndex[p];
fPower[n][ind] += power[n][i];
fPowerIndex[n] = max(ind, fPowerIndex[n]);
}
}
}
void read() {
cin >> n;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
number[k]++;
}
}
long long int answer() {
long long int sum = getSum();
bool first = true;
int l, r, cnt;
int lastFactor;
while (true) {
if (first) {
first = false;
l = r = cnt = -1;
for (int i = 0; i < prime.size(); i++) {
int p = prime[i];
int q = nextPrime(p);
int c = 0;
for (int j = p; j < q; j++) c += number[j];
if (c > cnt) {
l = p;
r = q;
lastFactor = i;
cnt = c;
}
}
} else {
int cnt1, cnt2;
cnt1 = cnt2 = 0;
int mid = r;
for (int i = l; i < r; i++) {
if (fPower[i][lastFactor] > 0) {
cnt2 += number[i];
if (mid == r) mid = i;
} else if (lastFactor > 0)
cnt1 += number[i];
}
if (cnt2 > cnt1) {
l = mid;
cnt = cnt2;
} else if (cnt1 > cnt2) {
r = mid;
cnt = cnt1;
lastFactor--;
} else
break;
}
if (cnt * 2 <= n) break;
sum -= cnt * 2 - n;
for (int i = l; i < r; i++) popLargestFactor(i);
}
return sum;
}
long long int getSum() {
long long int sum = 0;
for (int n = 0; n <= MAXK; n++) sum += number[n] * dist[n];
return sum;
}
int nextPrime(int p) {
int ind = primeIndex[p] + 1;
if (ind < prime.size())
return prime[ind];
else
return MAXK + 1;
}
void popLargestFactor(int m) {
if (fPower[m][fPowerIndex[m]] == 0) fPowerIndex[m]--;
fPower[m][fPowerIndex[m]]--;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int tt, cnt, tim, tp;
bool w[N];
vector<int> q[N];
int u[N], head[N], to[N], nxt[N], dfn[N], low[N], v[N], c[N];
inline int gi() {
int x = 0, o = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') o = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
inline void lnk(int x, int y) { to[++tt] = y, nxt[tt] = head[x], head[x] = tt; }
inline void dfs(int x) {
dfn[x] = low[x] = ++tim;
w[x] = 1, v[++tp] = x;
for (int i = head[x]; i; i = nxt[i]) {
int y = to[i];
if (!dfn[y]) {
dfs(y);
low[x] = min(low[x], low[y]);
} else if (w[y])
low[x] = min(low[x], dfn[y]);
}
if (dfn[x] == low[x]) {
int y;
++cnt;
do {
w[y = v[tp--]] = 0;
c[y] = cnt;
q[cnt].push_back(y);
} while (x != y);
}
}
inline bool cmp(const vector<int> &x, const vector<int> &y) {
return x.size() < y.size();
}
int main() {
int n, m, h, t = -1;
cin >> n >> m >> h;
for (int i = 1; i <= n; i++) u[i] = gi();
for (int i = 1; i <= m; i++) {
int x = gi(), y = gi();
if ((u[x] + 1) % h == u[y]) lnk(x, y);
if ((u[y] + 1) % h == u[x]) lnk(y, x);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) dfs(i);
for (int i = 1; i <= cnt; i++) {
bool flg = 1;
for (int j = 0; j < q[i].size(); j++) {
int x = q[i][j];
for (int k = head[x]; k; k = nxt[k])
if (c[to[k]] != i) {
flg = 0;
break;
}
if (!flg) break;
}
if (flg) {
if (t == -1)
t = i;
else if (q[i].size() < q[t].size())
t = i;
}
}
cout << q[t].size() << endl;
for (int i = 0; i < q[t].size(); i++) printf("%d ", q[t][i]);
return 0;
}
| 5 |
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
using namespace std;
int ModAtoi(string_view sv, int d) {
int r = 0;
for (char c : sv) {
r *= 2;
r %= d;
r += c - '0';
}
return r;
}
inline int popcount(int x) { return __builtin_popcount(x); }
int f(int x) { return x == 0 ? 0 : f(x % popcount(x)) + 1; }
int main() {
int n;
string s;
cin >> n >> s;
int pc0 = 0;
for (char c : s) pc0 += c - '0';
vector<int> ans(n);
for (int b : {0, 1}) {
const int pc = b == 0 ? pc0 + 1 : pc0 - 1;
if (pc <= 0) continue;
const int r0 = ModAtoi(s, pc); // X0 % popcount(X0)
int k = 1;
for (int i = n - 1; i >= 0; --i) {
const int x = s[i] - '0';
if (x == b) {
int r = r0;
if (b == 0) {
r = (r + k) % pc;
} else {
r = (r - k + pc) % pc;
}
ans[i] = f(r) + 1;
}
k = (2 * k) % pc;
}
}
for (int i = 0; i < n; i++) {
cout << ans[i] << endl;
}
return 0;
}
| 0 |
/*
* D.cpp
*
* Created on: 2012-8-25
* Author: Administrator
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int n, m;
int h, w;
struct page {
string name;
int bn;
int pt[105];
int x1[105];
int x2[105];
int y1[105];
int y2[105];
} site[105];
int buffer[10000];
string its[105];
int bfend;
int pointer;
int main() {
while (cin >> n && n) {
// memset(site, 0, sizeof(site));
map<string, int> ma;
cin >> h >> w;
int cp = 1;
string name;
int b;
while (n--) {
cin >> name >> b;
if (ma[name] == 0) {
its[cp] = name;
ma[name] = cp++;
}
int currpage = ma[name];
// cout << "currpage " << currpage << endl;
site[currpage].bn = b;
// cout << "b " << b << endl;
int x1, y1, x2, y2;
string pp;
for (int i = 0; i != b; i++) {
cin >> x1 >> y1 >> x2 >> y2 >> pp;
// cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" "<<pp<<endl;
if (ma[pp] == 0) {
its[cp] = pp;
ma[pp] = cp++;
}
int ipp = ma[pp];
site[currpage].x1[i] = x1;
site[currpage].x2[i] = x2;
site[currpage].y1[i] = y1;
site[currpage].y2[i] = y2;
site[currpage].pt[i] = ipp;
}
}
pointer = 0;
bfend = 0;
buffer[pointer] = 1;
cin >> m;
string command;
while (m--) {
// cout<<pointer<<endl;
cin >> command;
if (command == "show") {
cout << its[buffer[pointer]] << endl;
}
if (command == "forward") {
if (pointer < bfend)
pointer++;
}
if (command == "back") {
if (pointer > 0)
pointer--;
}
if (command == "click") {
int x, y;
cin >> x >> y;
int page = buffer[pointer];
int nextpage;
bool flag = false;
for (int i = 0; i != site[page].bn; i++) {
int x1 = site[page].x1[i];
int x2 = site[page].x2[i];
int y1 = site[page].y1[i];
int y2 = site[page].y2[i];
if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
nextpage = site[page].pt[i];
flag = true;
break;
}
}
if (flag) {
bfend = pointer + 1;
pointer++;
buffer[pointer] = nextpage;
}
}
}
}
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;cin>>n;
string s,t;
cin>>s;cin>>t;
for(int i=0;i<n;i++)
cout<<s[i]<<t[i];
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, ans[1010], cnt;
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
bool flag = 0;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (flag) continue;
for (int j = i; j <= n; j *= i) ans[++cnt] = j;
}
printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<string> a;
long long get(string s) {
long long pts = 0;
int nh = 0;
for (int i = ((int)(s).size()) - 1; i >= 0; i--) {
if (s[i] == 'h') {
nh++;
} else {
pts += nh;
}
}
return pts;
}
struct cmp {
inline bool operator()(const string& pa, const string& pb) {
return get(pa + pb) > get(pb + pa);
}
};
int main(int argc, char* argv[]) {
while ((cin >> n)) {
a.clear();
for (int i = 0; i < n; i++) {
string x;
cin >> x;
a.push_back(x);
}
vector<string> items;
for (int i = 0; i < n; i++) {
items.push_back(a[i]);
}
cerr << "bf sort" << endl;
if (((int)(items).size())) {
sort(items.begin(), items.end(), cmp());
}
cerr << "af sort" << endl;
long long pts = 0;
string res = "";
for (auto& p : items) {
res += p;
}
assert(((int)(res).size()) > 0);
vector<int> nh(((int)(res).size()));
nh[((int)(res).size()) - 1] = res.back() == 'h';
for (int i = ((int)(res).size()) - 2; i >= 0; i--) {
nh[i] = nh[i + 1] + (res[i] == 'h');
}
for (int i = 0; i < ((int)(res).size()); i++) {
if (res[i] == 's') {
pts += nh[i];
}
}
cerr << res << endl;
cout << pts << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
int n, m, g[maxn + 1][maxn + 1];
int number[maxn + 1], low[maxn + 1], parent[maxn + 1];
int dem;
void del(int x, int y) {
int i;
for (i = 1; i <= g[x][0]; i++)
if (g[x][i] == y) break;
if (g[x][i] == y) {
for (int j = i; j <= g[x][0]; j++) g[x][j] = g[x][j + 1];
g[x][0]--;
}
}
void dfs(int x) {
dem++;
number[x] = dem;
low[x] = n + 1;
for (int i = 1; i <= g[x][0]; i++) {
del(g[x][i], x);
if (parent[g[x][i]] == 0) {
parent[g[x][i]] = x;
dfs(g[x][i]);
if (low[x] > low[g[x][i]]) low[x] = low[g[x][i]];
} else if (low[x] > number[g[x][i]])
low[x] = number[g[x][i]];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
if (m != n - 1) {
cout << "no";
return 0;
}
for (int i = 1; i <= n; i++) g[i][0] = 0;
memset(number, 0, sizeof(number));
memset(parent, 0, sizeof(parent));
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[x][0]++;
g[x][g[x][0]] = y;
g[y][0]++;
g[y][g[y][0]] = x;
}
dem = 0;
parent[1] = -1;
dfs(1);
for (int i = 1; i <= n; i++)
if (parent[i] == 0) {
cout << "no";
return 0;
}
cout << "yes";
return 0;
}
| 3 |
#include <iostream>
using namespace std;
void solve()
{
int a, b;
while(cin >> a >> b)
{
if(!a && !b)
{
break;
}
int back = b - a;
int count1000 = back / 1000;
back %= 1000;
int count500 = back / 500;
back %= 500;
int count100 = back / 100;
cout << count100 << " " << count500 << " " << count1000 << endl;
}
}
int main()
{
solve();
return(0);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
double dp[2005][2005], cnt[2005][2005];
double p[2005], u[2005];
int main() {
int n, a, b;
cin >> n >> a >> b;
;
for (int i = 0; i < n; i++) cin >> *(p + 1 + i);
;
for (int i = 0; i < n; i++) cin >> *(u + 1 + i);
;
double lo = -4000.0, hi = 0.0;
int t = 100;
while (t--) {
double mid = (lo + hi) * 0.5;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= i; j++) dp[i][j] = -1e6;
dp[0][0] = 0.0;
memset(cnt, 0, sizeof cnt);
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= min(a, i); j++) {
double x = 1.0 - (1.0 - p[i]) * (1.0 - u[i]);
if (j < i) dp[i][j] = dp[i - 1][j];
cnt[i][j] = cnt[i - 1][j];
if (j > 0 && dp[i][j] < dp[i - 1][j - 1] + p[i])
dp[i][j] = dp[i - 1][j - 1] + p[i], cnt[i][j] = cnt[i - 1][j - 1];
if (j < i && dp[i][j] < dp[i - 1][j] + u[i] + mid)
dp[i][j] = dp[i - 1][j] + u[i] + mid, cnt[i][j] = cnt[i - 1][j] + 1;
if (j > 0 && dp[i][j] < dp[i - 1][j - 1] + x + mid)
dp[i][j] = dp[i - 1][j - 1] + x + mid,
cnt[i][j] = cnt[i - 1][j - 1] + 1;
}
}
if (cnt[n][a] < b)
lo = mid;
else
hi = mid;
}
cout << setprecision(10) << fixed << dp[n][a] - lo * b << " ";
;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, rt, d, dep[N], ans, p1, p2, c[N], len, maxd;
int head[N], numE = 0;
vector<int> g[N];
char s[10];
struct E {
int next, v;
} e[N << 1];
void inline add(int u, int v) {
e[++numE] = (E){head[u], v};
head[u] = numE;
}
void dfs(int u, int last) {
g[dep[u]].push_back(u);
maxd = max(maxd, dep[u]);
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if (v == last) continue;
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
bool inline check(int x) {
int p, dis;
printf("? %d ", (int)g[x].size());
for (int i = 0; i < g[x].size(); i++) {
printf("%d ", g[x][i]);
}
puts("");
fflush(stdout);
scanf("%d%d", &p, &dis);
if (dis == d) {
if (x > ans) ans = x, p1 = p;
return true;
} else
return false;
}
void dfs2(int u, int last, int dis) {
if (dis == d) c[++len] = u;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if (v == last) continue;
dfs2(v, u, dis + 1);
}
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
memset(head, 0, sizeof head);
scanf("%d", &n);
maxd = numE = ans = len = 0;
for (int i = 0; i < n; i++) g[i].clear();
for (int i = 1, u, v; i < n; i++) {
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
printf("? %d ", n);
for (int i = 1; i <= n; i++) printf("%d ", i);
puts("");
fflush(stdout);
scanf("%d%d", &rt, &d);
dep[rt] = 0;
dfs(rt, 0);
int l = (d & 1) ? d / 2 + 1 : d / 2, r = min(d, maxd);
if (l == r) check(r);
while (l < r) {
int mid = (l + r + 1) >> 1;
if (check(mid))
l = mid;
else
r = mid - 1;
}
if (ans != r) check(r);
dfs2(p1, 0, 0);
printf("? %d ", len);
for (int i = 1; i <= len; i++) printf("%d ", c[i]);
puts("");
fflush(stdout);
scanf("%d%d", &p2, &d);
printf("! %d %d\n", p1, p2);
fflush(stdout);
scanf("%s", s);
}
return 0;
}
| 6 |
#include <cstdio>
#include <algorithm>
#include <utility>
#include <queue>
using namespace std;
typedef long long int ll;
int N,M;
const int DATA_SIZE=100000;
const ll BIGNUM = 2000000000;
// 値段*美味しさ
pair<ll, ll> sweets[DATA_SIZE];
// 値段*個数
pair<ll,ll> friends[DATA_SIZE+1];
struct Sweet_ptr_comp{
// 美味しさを下降順
int operator()(pair<ll, ll> *a, pair<ll, ll> *b)
{
return a->second<=b->second;
}
};
//
typedef priority_queue<pair<ll, ll> *, vector<pair<ll, ll> *>, Sweet_ptr_comp> MaxQueue;
int main(){
scanf("%d%d",&N,&M);
for (int i=0;i<N;i++)
scanf("%lld%lld", &sweets[i].first, &sweets[i].second);
ll prev=BIGNUM;
for (int i=0;i<M;i++) {
scanf("%lld%lld", &friends[i].first, &friends[i].second);
friends[i].second=min(prev,friends[i].second);
prev = friends[i].second;
}
// 仮想の友達
friends[M]=pair<ll,ll>(BIGNUM,1);
// indexは探索したお菓子のインデクス
int index=0;
ll ans=0;
// 価値の小さいお菓子をいっぱい持つ
while (index<N&&sweets[index].first<friends[0].first)
ans+=sweets[index++].second;
// printf("%lld\n",ans);
MaxQueue Q;
// 全ての友達にたいする
for (int i=0;i<M;i++){
// 値段が今の友達と次の友達の間のお菓子を最大ヒープに追加
while (
index<N
&&sweets[index].first>=friends[i].first
&&sweets[index].first<friends[i+1].first)
Q.push(&sweets[index++]);
// 美味しさの高い奴から
for (int j=friends[i].second;j>friends[i+1].second&&!Q.empty();j--){
// printf("%lld %lld\n",Q.top()->first,Q.top()->second);
ans+=Q.top()->second;
Q.pop();
}
}
printf("%lld\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
const int maxn = 2e5+15;
int n,Q,a,b,c,d,cnt,head[maxn],ans[maxn];
struct edge{
int to,nxt,col,len;
}e[maxn<<1];
struct query{
int l,r,x,y,id;
}q[maxn<<1];
pii p[maxn];
inline void add(int u,int v,int c,int d){
e[++cnt] = (edge){v,head[u],c,d};
head[u] = cnt;
}
int dp[maxn][20],deep[maxn],st[maxn],ed[maxn],tot,A[maxn<<1];
void dfs(int x,int fa,int depth){
A[++tot] = x; st[x] = tot; deep[x] = depth;
for(int i = head[x];i > 0;i = e[i].nxt){
int v = e[i].to;
if(v == fa) continue;
p[v] = pii(e[i].col,e[i].len);
dp[v][0] = x; dfs(v,x,depth+1);
}
A[++tot] = x; ed[x] = tot;
}
void init(){
for(int i = 1;(1<<i)<=n; ++i)
for(int j = 1;j <= n; ++j)
dp[j][i] = dp[dp[j][i-1]][i-1];
}
int LCA(int u,int v){
if(deep[u] < deep[v]) swap(u,v);
int h = deep[u] - deep[v];
for(int i = 0;i < 20; ++i){
if(h&(1<<i)) u = dp[u][i];
}
if(u == v) return u;
for(int i = 19; ~i; --i){
if(dp[u][i] != dp[v][i]){
u = dp[u][i];
v = dp[v][i];
}
}
return dp[u][0];
}
int block,num[maxn],sum[maxn],vis[maxn],res;
bool cmp(query a,query b){
return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r);
}
void add(int v){
if(vis[v]) {num[p[v].fi]--;sum[p[v].fi]-=p[v].se;res-=p[v].se;}
else {num[p[v].fi]++;sum[p[v].fi]+=p[v].se;res+=p[v].se;}
vis[v] ^= 1;
}
void del(int v){
if(vis[v]) {num[p[v].fi]--;sum[p[v].fi]-=p[v].se;res-=p[v].se;}
else {num[p[v].fi]++;sum[p[v].fi]+=p[v].se;res+=p[v].se;}
vis[v] ^= 1;
}
int main(){
scanf("%d %d",&n,&Q); block = sqrt(n*1.0);
for(int i = 1;i < n; ++i){
scanf("%d %d %d %d",&a,&b,&c,&d);
add(a,b,c,d); add(b,a,c,d);
}
dfs(1,0,1); init();
for(int i = 0;i < Q; ++i){
scanf("%d %d %d %d",&a,&b,&c,&d);
if(st[c] > st[d]) swap(c,d);
if(LCA(c,d) == c) q[i] = (query){st[c]+1,st[d],a,b,i};
else q[i] = (query){ed[c],st[d],a,b,i};
}
sort(q,q+Q,cmp);
int L = 2,R = 1; res = 0;
for(int i = 0;i < Q; ++i){
while(R > q[i].r) del(A[R]),--R;
while(R < q[i].r) ++R,add(A[R]);
while(L < q[i].l) del(A[L]),++L;
while(L > q[i].l) --L,add(A[L]);
ans[q[i].id] = res-sum[q[i].x]+num[q[i].x]*q[i].y;
}
for(int i = 0;i < Q; ++i) printf("%d\n",ans[i]);
return 0;
} | 0 |
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e9;
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main(){
int n,m;
while(cin>>n>>m){
vi in(n);
rep(i,n)in[i]=i;
while(m--){
int a;cin>>a;
vvi out(2);
rep(i,n)out[(i<n/2?1:0)].pb(in[i]);
in=vi(0);
int t=0;
while(1){
rep(i,a){
if(out[t].size()==0)break;
in.pb(out[t][0]);
out[t].erase(out[t].begin());
}
t^=1;
if(in.size()==n)break;
}
}
cout<<in[in.size()-1]<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long n, c[N], ans[N], pw[64];
queue<long long> q[64];
int main() {
cin >> n;
pw[0] = 1;
for (int i = 1; i <= 60; i++) pw[i] = pw[i - 1] << 1;
for (int i = 1; i <= n; i++) {
cin >> c[i];
for (int j = 60; j >= 0; j--)
if (c[i] & pw[j]) {
q[j].push(c[i]);
break;
}
}
long long sum = 0;
for (int i = 1; i <= n; i++) {
int flag = 0;
for (int j = 0; j <= 60; j++)
if (!(sum & pw[j]) && !q[j].empty()) {
ans[i] = q[j].front(), sum ^= q[j].front(), q[j].pop();
flag = 1;
break;
}
if (!flag) {
puts("No");
return 0;
}
}
puts("Yes");
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define range(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,n) range(i,0,n)
using namespace std;
int main(void){
int ans=0;
while(1){
string s;
cin >> s;
if(cin.eof()) break;
int n=s.size();
int cur=0;
rep(i,n){
if(isdigit(s[i])){
cur=cur*10+(s[i]-'0');
}else{
ans+=cur;
cur=0;
}
}
ans+=cur;
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int tailleEnsemble[100 * 1000 + 3];
int dernierVu[100 * 1000 + 3], numero = 1;
int pere[100 * 1000 + 3];
map<long long, int> dictionnaire;
bool couleur[100 * 1000 + 3];
vector<int> frere[100 * 1000 + 3];
int trouverPere(int p) {
if (pere[p] == p) return p;
return pere[p] = trouverPere(pere[p]);
}
void echanger(int a) {
if (dernierVu[a] == numero) return;
couleur[a] ^= 1;
dernierVu[a] = numero;
for (int f : frere[a]) echanger(f);
}
long long lireChaine() {
long long reponse = 0;
char tmp = getchar();
for (int j = 0; 'a' <= tmp && tmp <= 'z'; j++) {
reponse += tmp;
reponse *= 26ll;
tmp = getchar();
}
return reponse;
}
int main() {
int nbNoeud, nbArete, nbQuestion;
scanf("%d%d%d\n", &nbNoeud, &nbArete, &nbQuestion);
for (int i = 0; i < nbNoeud; i++) {
pere[i] = i;
tailleEnsemble[i] = 1;
dictionnaire[lireChaine()] = i;
}
for (int i = 0; i < nbArete; i++) {
int choix = getchar() - '1';
getchar();
int a = dictionnaire[lireChaine()], b = dictionnaire[lireChaine()];
if (trouverPere(a) == trouverPere(b)) {
if ((couleur[a] == couleur[b]) == choix)
printf("NO\n");
else
printf("YES\n");
continue;
}
if ((couleur[a] == couleur[b]) == choix) {
if (tailleEnsemble[trouverPere(a)] < tailleEnsemble[trouverPere(b)])
swap(a, b);
numero++;
echanger(b);
}
frere[a].push_back(b);
frere[b].push_back(a);
a = trouverPere(a);
b = trouverPere(b);
pere[b] = a;
tailleEnsemble[a] += tailleEnsemble[b];
printf("YES\n");
}
for (int i = 0; i < nbQuestion; i++) {
int a = dictionnaire[lireChaine()], b = dictionnaire[lireChaine()];
if (trouverPere(a) != trouverPere(b))
printf("3\n");
else if (couleur[a] == couleur[b])
printf("1\n");
else
printf("2\n");
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int add(long long int x, long long int y) {
x += y;
while (x >= 1000000007) x -= 1000000007;
while (x < 0) x += 1000000007;
return x;
}
long long int mul(long long int x, long long int y) {
return (x * 1ll * y) % 1000000007;
}
long long int binpow(long long int x, long long int y) {
long long int z = 1;
while (y) {
if (y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
long long int inv(long long int x) { return binpow(x, 1000000007 - 2); }
long long int divide(long long int x, long long int y) {
return mul(x, inv(y));
}
bool collinear(long long int x1, long long int y1, long long int x2,
long long int y2, long long int x3, long long int y3) {
long long int a =
x1 * 1ll * (y2 - y3) + x2 * 1ll * (y3 - y1) + x3 * 1ll * (y1 - y2);
if (a == 0)
return true;
else
return false;
}
struct node {
int x, y, idx;
};
bool comp(node n1, node n2) {
if (n1.x != n2.x)
return n1.x < n2.x;
else
return n1.y < n2.y;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<node> v;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
v.push_back({x, y, i});
}
sort(v.begin(), v.end(), comp);
int prev = -1, prev1 = -1;
int i = 0, flag = 0;
while (i < n) {
int j = i + 1;
if (j < n && v[j].x == v[i].x) {
if (prev != -1 && prev1 == -1) {
cout << v[prev].idx + 1 << " " << v[i].idx + 1 << " " << v[j].idx + 1
<< endl;
return 0;
}
if (prev != -1 && prev1 != -1) {
cout << v[prev1].idx + 1 << " " << v[i].idx + 1 << " " << v[j].idx + 1
<< endl;
return 0;
}
flag = 0;
prev = i, prev1 = j;
while (j < n && v[j].x == v[i].x) j++;
} else {
if (prev != -1 && prev1 != -1 &&
(collinear(v[prev].x, v[prev].y, v[prev1].x, v[prev1].y, v[i].x,
v[i].y))) {
prev = prev1;
prev1 = i;
} else if (prev != -1 && prev1 != -1 &&
(collinear(v[prev].x, v[prev].y, v[prev1].x, v[prev1].y,
v[i].x, v[i].y)) == false) {
cout << v[prev].idx + 1 << " " << v[prev1].idx + 1 << " "
<< v[i].idx + 1 << endl;
return 0;
} else {
if (prev == -1)
prev = i;
else if (prev != -1)
prev1 = i;
}
}
i = j;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int isperfect(long long int n) {
if (sqrt(n) == floor(sqrt(n)))
return 1;
else
return 0;
}
int A[100001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int arr[n];
int max1 = INT_MIN;
for (int i = 0; i < n; i++) {
cin >> arr[i];
++A[arr[i]];
max1 = max(arr[i], max1);
}
if (A[max1] % 2 != 0) {
cout << "Conan";
return 0;
}
int f = 0;
for (int i = 0; i < n; i++) {
if (A[arr[i]] % 2 != 0) f = 1;
}
if (f)
cout << "Conan";
else
cout << "Agasa";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int N, M, a, b;
long long d[100010], pos[100010], H[100010], sum;
pair<long long, long long> tree[4 * 100010];
inline int Sx(int p) { return p << 1; }
inline int Dx(int p) { return (p << 1) + 1; }
inline long long Orario(int p) { return pos[p]; }
inline long long AntiOrario(int p) { return pos[N + 1] - pos[p]; }
void Combina(pair<long long, long long> sx, pair<long long, long long> dx,
pair<long long, long long>& ris) {
pair<long long, long long> prima =
(sx.first == sx.second)
? pair<long long, long long>(N + 2, N + 2)
: sx,
seconda =
(dx.first == dx.second)
? pair<long long, long long>(N + 2, N + 2)
: dx;
pair<long long, long long> terza = {sx.first, dx.second},
quarta = {sx.second, dx.first};
pair<long long, long long> quinta = {sx.first, dx.first},
sesta = {sx.second, dx.second};
if (terza.first == terza.second)
terza = pair<long long, long long>(N + 2, N + 2);
if (quarta.first == quarta.second)
quarta = pair<long long, long long>(N + 2, N + 2);
if (quinta.first == quinta.second)
quinta = pair<long long, long long>(N + 2, N + 2);
if (sesta.first == sesta.second)
sesta = pair<long long, long long>(N + 2, N + 2);
long long appo1 = Orario(prima.second) - Orario(prima.first) +
2 * (H[prima.first] + H[prima.second]);
long long appo2 = Orario(seconda.second) - Orario(seconda.first) +
2 * (H[seconda.first] + H[seconda.second]);
long long appo3 = Orario(terza.second) - Orario(terza.first) +
2 * (H[terza.first] + H[terza.second]);
long long appo4 = Orario(quarta.second) - Orario(quarta.first) +
2 * (H[quarta.first] + H[quarta.second]);
long long appo5 = Orario(quinta.second) - Orario(quinta.first) +
2 * (H[quinta.first] + H[quinta.second]);
long long appo6 = Orario(sesta.second) - Orario(sesta.first) +
2 * (H[sesta.first] + H[sesta.second]);
long long massimo =
max(appo1, max(appo2, max(appo3, max(appo4, max(appo5, appo6)))));
if (massimo == appo1)
ris = prima;
else if (massimo == appo2)
ris = seconda;
else if (massimo == appo3)
ris = terza;
else if (massimo == appo4)
ris = quarta;
else if (massimo == appo5)
ris = quinta;
else
ris = sesta;
}
void Build(int i, int j, int pos) {
if (i == j) {
tree[pos] = {i, i};
return;
}
Build(i, (i + j) / 2, Sx(pos));
Build((i + j) / 2 + 1, j, Dx(pos));
Combina(tree[Sx(pos)], tree[Dx(pos)], tree[pos]);
}
pair<long long, long long> Query(int i, int j, int da, int a, int pos) {
if (i > a || j < da) return {-1, -1};
if (i >= da && j <= a) return tree[pos];
pair<long long, long long> sx = Query(i, (i + j) / 2, da, a, Sx(pos));
pair<long long, long long> dx = Query((i + j) / 2 + 1, j, da, a, Dx(pos));
if (sx.first == -1)
return dx;
else if (dx.first == -1)
return sx;
else {
pair<long long, long long> ris;
Combina(sx, dx, ris);
return ris;
}
}
int main() {
scanf("%d %d", &N, &M);
for (int i = 1; i <= N; i++) scanf("%lld", &d[i]);
pos[1] = 0;
for (int i = 2; i < N + 2; i++) pos[i] = pos[i - 1] + d[i - 1];
for (int i = 1; i <= N; i++) scanf("%lld", &H[i]);
Build(1, N, 1);
for (int i = 0; i < M; i++) {
scanf("%d %d", &a, &b);
if (a <= b) {
pair<long long, long long> dx = (b < N)
? Query(1, N, b + 1, N, 1)
: pair<long long, long long>(-1, -1);
pair<long long, long long> sx = (a > 1)
? Query(1, N, 1, a - 1, 1)
: pair<long long, long long>(-1, -1);
if (sx.first == -1)
printf("%lld\n", 2 * (H[dx.first] + H[dx.second]) + Orario(dx.second) -
Orario(dx.first));
else if (dx.first == -1)
printf("%lld\n", 2 * (H[sx.first] + H[sx.second]) + Orario(sx.second) -
Orario(sx.first));
else {
pair<long long, long long> ind1 = sx, ind2 = dx,
ind3 = {dx.first, sx.second},
ind4 = {dx.first, sx.first},
ind5 = {dx.second, sx.first},
ind6 = {dx.second, sx.second};
if (ind1.first == ind1.second)
ind1 = pair<long long, long long>(N + 2, N + 2);
if (ind2.first == ind2.second)
ind2 = pair<long long, long long>(N + 2, N + 2);
if (ind3.first == ind3.second)
ind3 = pair<long long, long long>(N + 2, N + 2);
if (ind4.first == ind4.second)
ind4 = pair<long long, long long>(N + 2, N + 2);
if (ind5.first == ind5.second)
ind5 = pair<long long, long long>(N + 2, N + 2);
if (ind6.first == ind6.second)
ind6 = pair<long long, long long>(N + 2, N + 2);
long long appo1 = 2 * (H[ind1.first] + H[ind1.second]) +
Orario(ind1.second) - Orario(ind1.first);
long long appo2 = 2 * (H[ind2.first] + H[ind2.second]) +
Orario(ind2.second) - Orario(ind2.first);
long long appo3 = 2 * (H[ind3.first] + H[ind3.second]) +
Orario(ind3.second) + AntiOrario(ind3.first);
long long appo4 = 2 * (H[ind4.first] + H[ind4.second]) +
Orario(ind4.second) + AntiOrario(ind4.first);
long long appo5 = 2 * (H[ind5.first] + H[ind5.second]) +
Orario(ind5.second) + AntiOrario(ind5.first);
long long appo6 = 2 * (H[ind6.first] + H[ind6.second]) +
Orario(ind6.second) + AntiOrario(ind6.first);
printf(
"%lld\n",
max(appo1, max(appo2, max(appo3, max(appo4, max(appo5, appo6))))));
}
} else {
pair<long long, long long> ris = Query(1, N, b + 1, a - 1, 1);
printf("%lld\n", 2 * (H[ris.first] + H[ris.second]) + Orario(ris.second) -
Orario(ris.first));
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int menor[n];
for (int i = 0; i < n; i++) menor[i] = INT_MAX;
int e = 1;
while (e < n) e *= 2;
while (e > 1) {
e /= 2;
int a = 0, b = 0;
for (int i = 0; i < n; i++)
if (i & e)
a++;
else
b++;
cout << a << endl;
for (int i = 0; i < n; i++) {
if (i & e) {
cout << i + 1 << " ";
}
}
cout << endl;
fflush(stdout);
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
if (!(i & e)) menor[i] = min(tmp, menor[i]);
}
cout << b << endl;
for (int i = 0; i < n; i++) {
if (!(i & e)) {
cout << i + 1 << " ";
}
}
cout << endl;
fflush(stdout);
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
if (i & e) menor[i] = min(tmp, menor[i]);
}
}
cout << "-1" << endl;
for (int i = 0; i < n; i++) cout << menor[i] << " ";
cout << endl;
fflush(stdout);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long INF = 10000000000000;
int main(){
int N;
cin >> N;
vector<int> a(N);
for (int i = 0; i < N; i++){
cin >> a[i];
}
int V = N + 2;
vector<map<int, long long>> E(V);
long long ans = 0;
for (int i = 0; i < N; i++){
if (a[i] > 0){
ans += a[i];
E[N][i] = a[i];
E[i][N + 1] = 0;
} else {
E[N][i] = 0;
E[i][N + 1] = abs(a[i]);
}
}
for (int i = 1; i <= N; i++){
for (int j = i * 2; j <= N; j += i){
E[j - 1][i - 1] = INF;
}
}
while (1){
vector<long long> flow(V, INF);
vector<int> prev(V, -1);
prev[N] = N;
queue<int> Q;
Q.push(N);
while (!Q.empty()){
int v = Q.front();
Q.pop();
for (auto edge : E[v]){
int w = edge.first;
long long c = edge.second;
if (prev[w] == -1){
prev[w] = v;
flow[w] = min(flow[v], c);
Q.push(w);
}
}
}
if (prev[V - 1] == -1){
break;
}
long long f = flow[V - 1];
int v = V - 1;
while (v != N){
E[v][prev[v]] += f;
E[prev[v]][v] -= f;
if (E[prev[v]][v] == 0){
E[prev[v]].erase(v);
}
v = prev[v];
}
ans -= f;
}
cout << ans << endl;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
cout<<max(abs(a-b),max(abs(c-a),b-c))<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int MAXN = 1e9 + 1;
const double PI = 3.14159265359;
using namespace std;
int a[100001], q[100001];
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
q[a[i]] = i;
}
int z = 0, mx = 0;
for (int i = 1; i <= n; ++i) {
if (q[i] < q[i + 1]) {
++z;
} else {
mx = max(mx, z);
z = 0;
}
}
cout << n - max(mx, z) - 1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma optimise("O3")
using namespace std;
mt19937 rnumber((long long)new char);
typedef struct Treap* Arbore;
Arbore NIL;
struct Treap {
int move_st, move_dr;
int poz, poz_min, poz_max, w, g;
int prio;
long long suma;
Arbore st, dr;
Treap(int g, int poz)
: move_st(0),
move_dr(0),
poz(poz),
poz_min(poz),
poz_max(poz),
suma(g),
w(g),
prio(rnumber()),
st(NIL),
dr(NIL),
g(1) {}
inline void recalc() {
g = 1 + st->g + dr->g;
suma = w + st->suma + dr->suma;
poz_min = min(poz, st->poz_min);
poz_max = max(poz, dr->poz_max);
move_st =
(1LL * (poz - poz_min - st->g) * w +
1LL * (dr->poz_min - poz_min - st->g - 1) * (dr->suma % 1000000007) +
st->move_st + dr->move_st) %
1000000007;
move_dr =
(1LL * (poz_max - poz - dr->g) * w +
1LL * (poz_max - st->poz_max - dr->g - 1) * (st->suma % 1000000007) +
st->move_dr + dr->move_dr) %
1000000007;
}
};
Arbore join(Arbore a, Arbore b) {
if (a == NIL) return b;
if (b == NIL) return a;
if (a->prio >= b->prio) {
a->dr = join(a->dr, b);
a->recalc();
return a;
}
b->st = join(a, b->st);
b->recalc();
return b;
}
pair<Arbore, Arbore> split_by_number(Arbore a, int nr) {
if (a == NIL) return {NIL, NIL};
if (a->st->g >= nr) {
pair<Arbore, Arbore> s = split_by_number(a->st, nr);
a->st = NIL;
a->recalc();
return {s.first, join(s.second, a)};
}
pair<Arbore, Arbore> s = split_by_number(a->dr, nr - a->st->g - 1);
a->dr = NIL;
a->recalc();
return {join(a, s.first), s.second};
}
pair<Arbore, Arbore> split_by_value(Arbore a, long long nr) {
if (a == NIL) return {NIL, NIL};
if (a->st->suma >= nr) {
pair<Arbore, Arbore> s = split_by_value(a->st, nr);
a->st = NIL;
a->recalc();
return {s.first, join(s.second, a)};
}
pair<Arbore, Arbore> s = split_by_value(a->dr, nr - a->st->suma - a->w);
a->dr = NIL;
a->recalc();
return {join(a, s.first), s.second};
}
Arbore update(Arbore a, int poz, int w) {
pair<Arbore, Arbore> s1 = split_by_number(a, poz);
pair<Arbore, Arbore> s2 = split_by_number(s1.first, poz - 1);
s2.second->w = w;
s2.second->recalc();
return join(s2.first, join(s2.second, s1.second));
}
inline Arbore query(Arbore a, int st, int dr) {
pair<Arbore, Arbore> s1 = split_by_number(a, dr);
pair<Arbore, Arbore> s2 = split_by_number(s1.first, st - 1);
Arbore partea_st = s2.first, partea_dr = s1.second;
a = s2.second;
long long w = a->suma / 2;
pair<Arbore, Arbore> s = split_by_value(a, w);
long long ans = s.first->move_dr + s.second->move_st;
if (s.first == NIL || s.second == NIL)
;
else if (s.first->suma <= s.second->suma)
ans += (s.second->poz_min - s.first->poz_max - 1) *
(s.first->suma % 1000000007);
else
ans += (s.second->poz_min - s.first->poz_max - 1) *
(s.second->suma % 1000000007);
printf("%d\n", ans % 1000000007);
return join(join(partea_st, s.first), join(s.second, partea_dr));
}
int main() {
NIL = new Treap(0, 0);
NIL->g = 0;
NIL->poz_min = 1e9, NIL->poz_max = -1e9;
Arbore k = NIL;
int n, q;
scanf("%d%d", &n, &q);
vector<int> a(n), w(n);
for (auto& i : a) scanf("%d", &i);
for (auto& i : w) scanf("%d", &i);
for (int i = 0; i < n; i++) k = join(k, new Treap(w[i], a[i]));
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
if (a < 0)
k = update(k, -a, b);
else
k = query(k, a, b);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
std::istream& is = std::cin;
std::ostream& os = std::cout;
std::string s, t, u;
std::getline(is, s);
std::getline(is, t);
std::sort(s.begin(), s.end());
std::sort(t.begin(), t.end());
int n = s.length();
u.resize(n);
int sl = 0, sr = n / 2 + n % 2 - 1, tl = n / 2 + n % 2, tr = n - 1, ul = 0,
ur = n - 1;
for (int m = 0; m < n; m++) {
if (m % 2 == 0) {
if (s[sl] < t[tr])
u[ul++] = s[sl++];
else
u[ur--] = s[sr--];
} else {
if (t[tr] > s[sl])
u[ul++] = t[tr--];
else
u[ur--] = t[tl++];
}
}
os << u;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, i, j, x, y, t, ans = 0;
vector<long long> b1(200001, 2002), b2(200001, 2002);
int main() {
scanf("%I64d", &n);
for (i = 0; i < n; ++i) {
scanf("%I64d%I64d", &x, &y);
b1[i] = -x - y;
b2[i] = x - y;
}
sort(b1.begin(), b1.end());
sort(b2.begin(), b2.end());
for (i = 0; i < n - 1; ++i) {
t = b1[i];
for (j = i + 1; j < n && b1[j] == t; ++j) {
}
ans += (j - i - 1) * (j - i) / 2;
i = j - 1;
}
for (i = 0; i < n - 1; ++i) {
t = b2[i];
for (j = i + 1; j < n && b2[j] == t; ++j) {
}
ans += (j - i - 1) * (j - i) / 2;
i = j - 1;
}
printf("%I64d", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while (1) {
int N, M;
cin >> N >> M;
if (!N & !M) break;
unordered_map<int, int> DV;
for (int i = 1; i <= M; i++) {
DV[i] = 0;
}
for (int i = 0; i < N; i++) {
int d, v;
cin >> d >> v;
DV[d] = max(v, DV[d]);
}
int ans = 0;
for (int i = 1; i <= M; i++) {
ans += DV[i];
}
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double pi = acos(-1.0);
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
int a[105], dp[105][1 << 17], n, bit[105], path[105][1 << 17];
int solve(int pos, int mask) {
if (pos >= n) return 0;
int& ret = dp[pos][mask];
if (ret != -1) return ret;
ret = INF;
for (int i = 1; i < 62; i++) {
if ((bit[i] & mask) == 0) {
int val = solve(pos + 1, mask | bit[i]) + abs(a[pos] - i);
if (val < ret) {
ret = val;
path[pos][mask] = i;
}
}
}
return ret;
}
int main() {
std::ios_base::sync_with_stdio(false);
for (int i = 1; i < 62; i++) {
for (int j = 0; j < 16; j++) {
if (i % primes[j] == 0) bit[i] |= (1 << j);
}
}
while (cin >> n) {
for (int i = 0; i < n; i++) cin >> a[i];
memset(dp, (-1), sizeof(dp));
solve(0, 0);
int mask = 0;
for (int i = 0; i < n; i++) {
cout << path[i][mask] << " ";
mask |= bit[path[i][mask]];
}
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int E,Y;
cin>>E>>Y;
if(E==0){
if(Y>=1989&&Y<=2016)cout<<"H"<<Y-1988<<endl;
if(Y>=1926&&Y<=1988)cout<<"S"<<Y-1925<<endl;
if(Y>=1912&&Y<=1925)cout<<"T"<<Y-1911<<endl;
if(Y>=1868&&Y<=1911)cout<<"M"<<Y-1867<<endl;
}
else{
if(E==1)cout<<Y+1867<<endl;
if(E==2)cout<<Y+1911<<endl;
if(E==3)cout<<Y+1925<<endl;
if(E==4)cout<<Y+1988<<endl;
}
return 0;
}
| 0 |
#include <iostream>
#include <complex>
#include <cmath>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
const double EPS = 1e-12;
double cross(const xy_t &a, const xy_t &b){
return imag(conj(a) * b);
}
double dot(const xy_t &a, const xy_t &b){
return real(conj(a) * b);
}
xy_t projection(const line &l, const xy_t &p){
double t = dot(l.second - l.first, p - l.first) / norm(l.second - l.first);
return l.first + t * (l.second - l.first);
}
bool intersectSP(const line &l, const xy_t &p){
return abs(p - l.first) + abs(p - l.second) < abs(l.first - l.second) + EPS;
}
double distanceSP(const line &l, const xy_t &p){
xy_t r = projection(l, p);
if(intersectSP(l, r)) return abs(r - p);
else return min(abs(l.first - p), abs(l.second - p));
}
int ccw(xy_t a, xy_t b, xy_t c){
b -= a, c -= a;
if(cross(b, c) > 0) return 1;
if(cross(b, c) < 0) return -1;
if(dot(b, c) < 0) return 2;
if(norm(b) < norm(c)) return -2;
return 0;
}
xy_t p[3];
line l[3];
xy_t c;
double r;
int main(){
int x, y;
int c1, c2;
while(cin >> x>> y && (x || y)){
p[0] = xy_t(x, y);
for(int i = 1; i < 3; i++) {
cin >> x >> y;
p[i] = xy_t(x, y);
}
cin >> x >> y;
cin >> r;
c = xy_t(x, y);
rep(i, 3) l[i] = line(p[i], p[(i+1)%3]);
c1 = 0, c2 = 0;
int cnt = 0;
rep(i, 3){
int d = ccw(p[i], p[(i+1)%3], c);
if(d > 0) c1++;
if(d < 0) c2++;
}
bool circle_in[3];
rep(i, 3){
if(distanceSP(l[i], c) < r + EPS) cnt++;
if(distanceSP(l[i], c) > r - EPS){
circle_in[i] = true;
}else{
circle_in[i] = false;
}
}
bool t_in[3];
rep(i, 3){
if(abs(c - p[i]) < r + EPS){
t_in[i] = true;
cnt++;
}else{
t_in[i] = false;
}
}
if(circle_in[0] && circle_in[1] && circle_in[2] && max(c1, c2) == 3){
cout << 'a' << endl;
}else if(t_in[0] && t_in[1] && t_in[2]) {
cout << 'b' << endl;
}else if(cnt > 0){
cout << 'c' << endl;
}else{
cout << 'd' << endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct kraw {
int m, M, a, p;
};
vector<vector<int> > st;
int n;
int A = -1, B = -1;
void gen(vector<int>& a, int x, int nr, int max_dl, vector<int>& ogr) {
a[nr] = x;
if (nr == max_dl - 1) {
st.push_back(a);
return;
}
if (nr == max_dl - 2)
gen(a, -1, nr + 1, max_dl, ogr);
else
for (int i = 0; i <= ogr[nr + 1]; ++i) gen(a, i, nr + 1, max_dl, ogr);
}
void gen2(vector<int>& a, int x, int nr, int max_dl, vector<int>& dod, int S,
vector<kraw>& ogr, int nr_wie) {
a[nr] = x;
if (nr == max_dl - 1) {
int s = 0;
for (int i = 0; i < max_dl; ++i) s += a[i];
if (s != S) return;
for (int i = 0; i < max_dl; ++i) dod.push_back(a[i]);
return;
}
for (int i = ogr[nr_wie + nr + 2].m; i <= ogr[nr_wie + nr + 2].M; ++i)
gen2(a, i, nr + 1, max_dl, dod, S, ogr, nr_wie);
}
void dfs(int akt, vector<vector<kraw> >& G, vector<vector<int> >& rozk,
vector<int>& st) {
if (akt != 1) {
int st_we = 0;
for (int i = 1; i < akt; ++i) st_we += G[i][akt].p;
int x = st[akt] - st_we;
if (x >= G[0][akt].m && x <= G[0][akt].M)
G[0][akt].p = x;
else
return;
}
if (akt != n - 2) {
int il_nast = n - akt - 1;
for (int i = 0; i < rozk[akt].size(); i += il_nast) {
for (int j = 0; j < il_nast; ++j)
G[akt][akt + j + 1].p = rozk[akt][i + j];
dfs(akt + 1, G, rozk, st);
}
}
if (akt == n - 2) {
int s1 = 0, s2 = 0;
for (int i = 1; i <= n - 2; ++i) {
s1 += G[0][i].p;
s2 += G[i][n - 1].p;
}
if (s1 != s2) return;
int prze = 0, koszt = 0;
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (G[i][j].p != 0) koszt += G[i][j].p * G[i][j].p + G[i][j].a;
for (int i = 1; i < n; ++i) prze += G[0][i].p;
if (A == -1 || prze < A || (prze == A && koszt > B)) {
A = prze;
B = koszt;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
vector<vector<kraw> > G(n, vector<kraw>(n));
for (int i = 0; i < n * (n - 1) / 2; ++i) {
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
--a;
--b;
G[a][b] = {c, d, e, -1};
}
if (n == 2) {
if (G[0][1].m != 0)
cout << G[0][1].m << " " << G[0][1].a + G[0][1].m * G[0][1].m;
else
cout << "0 0";
return 0;
}
if (n == 3) {
int a = 0, b = 0;
int x = max(G[0][1].m, G[1][2].m);
int y = min(G[0][1].M, G[1][2].M);
if (x <= y) {
if (x != 0) {
a += x;
b += 2 * x * x + (G[0][1].a + G[1][2].a);
}
} else {
cout << "-1 -1";
return 0;
}
if (G[0][2].m != 0) {
a += G[0][2].m;
b += G[0][2].m * G[0][2].m + G[0][2].a;
}
cout << a << " " << b;
return 0;
}
vector<int> max_st(n);
max_st[0] = max_st[n - 1] = -1;
int l = 1, p = n - 2, s = 5;
while (l <= p) {
max_st[l] = max_st[p] = s;
s += 5;
++l;
--p;
}
vector<int> a(n);
gen(a, -1, 0, n, max_st);
for (int i = 0; i < st.size(); ++i) {
vector<vector<int> > rozk(n);
for (int j = 1; j <= n - 3; ++j) {
a.clear();
for (int k = 0; k < n - j - 1; ++k) a.push_back(0);
for (int k = G[j][j + 1].m; k <= G[j][j + 1].M; ++k)
gen2(a, k, 0, n - j - 1, rozk[j], st[i][j], G[j], j);
}
if (!(st[i][1] >= G[0][1].m && st[i][1] <= G[0][1].M &&
st[i][n - 2] >= G[n - 2][n - 1].m &&
st[i][n - 2] <= G[n - 2][n - 1].M))
continue;
G[0][1].p = st[i][1];
G[n - 2][n - 1].p = st[i][n - 2];
G[0][n - 1].p = G[0][n - 1].m;
dfs(1, G, rozk, st[i]);
}
cout << A << " " << B;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
inline void chkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
using ll = long long;
using ld = long double;
const string FILENAME = "input";
int main() {
ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
map<int, int> cnt;
for (int i = 0; i < k + 1; i++) {
vector<int> st;
for (int j = 0; j <= k; j++) {
if (j != i) {
st.push_back(j);
}
}
cout << '?';
for (auto x : st) {
cout << ' ' << x + 1;
}
cout << endl;
int pos, val;
cin >> pos >> val;
cnt[val]++;
}
for (auto x : cnt) {
cout << "!" << ' ' << k + 1 - x.second << endl;
break;
}
}
| 5 |
//ITP1_5_D
#include<iostream>
using namespace std;
int main()
{
int i,x,n;
cin>>n;
for(i=1;i<=n;i++){
if(0==i%3)
cout<<" "<<i;
else for(x=i;x!=0;x/=10)
{
if(3==x%10){
cout<<" "<<i;
break;
}
}
}
cout<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bitset<2000005> bs;
vector<int> p;
void build() {
bs[0] = bs[1] = 1;
for (int i = 2; i < 2000005; i++) {
if (!bs[i]) p.push_back(i);
for (int j = 0; i * p[j] < 2000005; j++) {
bs[i * p[j]] = 1;
if (i % p[j] == 0) break;
}
}
}
int in[1005];
bitset<1005> G[1005], now;
int main() {
{
cin.tie(0);
ios_base::sync_with_stdio(false);
};
int tt = clock();
build();
int n;
cin >> n;
vector<int> v;
for (int i = 0; i < n; i++) cin >> in[i], v.push_back(i);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j && !bs[in[i] + in[j]]) {
G[i][j] = G[j][i] = 1;
}
int ans = 0;
bitset<1005> best;
while ((clock() - tt) * 1.0 / CLOCKS_PER_SEC < 0.95) {
random_shuffle(v.begin(), v.end());
now.reset();
int r = 0;
for (int i : v)
if ((G[i] & now) == now) {
now[i] = 1;
r++;
}
if (ans < r) {
ans = r;
best = now;
}
}
cout << ans << '\n';
for (int i = 0; i < n; i++)
if (best[i]) cout << in[i] << ' ';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
const int inf = 4 * MAXN * MAXN;
int n, m;
int xp, yp, xk, yk;
char A[MAXN][MAXN];
int graf[inf][5];
int size[inf];
char delta[inf][4];
char temp[4];
void load() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", A[i]);
scanf("%d%d%d%d", &xp, &yp, &xk, &yk);
xp--;
yp--;
xk--;
yk--;
}
void spoji(int a, int b) { graf[a][size[a]++] = b; }
void initGraf() {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
int x = i * m + j;
switch (A[i][j]) {
case '+':
strcpy(delta[x], "1111");
break;
case '-':
strcpy(delta[x], "1010");
break;
case '|':
strcpy(delta[x], "0101");
break;
case '^':
strcpy(delta[x], "0001");
break;
case '>':
strcpy(delta[x], "1000");
break;
case 'v':
strcpy(delta[x], "0100");
break;
case '<':
strcpy(delta[x], "0010");
break;
case 'L':
strcpy(delta[x], "1101");
break;
case 'R':
strcpy(delta[x], "0111");
break;
case 'D':
strcpy(delta[x], "1011");
break;
case 'U':
strcpy(delta[x], "1110");
break;
case '*':
strcpy(delta[x], "0000");
break;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < n; j++)
for (int k = 0; k < m; k++) {
int x = j * m + k;
int y;
if (k + 1 != m) {
y = x + 1;
if (delta[x][0] == '1' && delta[y][2] == '1') {
spoji(x * 4 + i, y * 4 + i);
spoji(y * 4 + i, x * 4 + i);
}
}
if (j + 1 != n) {
y = x + m;
if (delta[x][1] == '1' && delta[y][3] == '1') {
spoji(x * 4 + i, y * 4 + i);
spoji(y * 4 + i, x * 4 + i);
}
}
}
for (int j = 0; j < n * m; j++) {
temp[0] = delta[j][3];
temp[1] = delta[j][0];
temp[2] = delta[j][1];
temp[3] = delta[j][2];
delta[j][0] = temp[0];
delta[j][1] = temp[1];
delta[j][2] = temp[2];
delta[j][3] = temp[3];
}
}
for (int i = 0; i < n * m; i++) {
spoji(i * 4 + 0, i * 4 + 1);
spoji(i * 4 + 1, i * 4 + 2);
spoji(i * 4 + 2, i * 4 + 3);
spoji(i * 4 + 3, i * 4 + 0);
}
}
bool bio[inf];
const int siz = 2 * inf;
struct queue {
int A[siz];
int lo, hi, vel;
queue(int pot) {
lo = 0, hi = 0;
A[0] = pot;
vel = 1;
}
queue() {
lo = 0, hi = -1;
vel = 0;
}
void push(int x) {
hi = (hi + 1) % siz;
A[hi] = x;
vel++;
}
int front() { return A[lo]; }
void pop() {
lo = (lo + 1) % siz;
vel--;
}
bool empty() { return vel == 0; }
} q;
int bfs() {
q.push((xp * m + yp) * 4);
q.push(0);
int cilj = xk * m + yk;
while (!q.empty()) {
int x = q.front();
q.pop();
int t = q.front();
q.pop();
if (bio[x]) continue;
if (x / 4 == cilj) return t;
bio[x] = true;
for (int i = 0; i < size[x]; i++) {
int sus = graf[x][i];
if (bio[sus]) continue;
q.push(sus);
q.push(t + 1);
}
}
return -1;
}
void solve() {
initGraf();
printf("%d\n", bfs());
}
int main() {
load();
solve();
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> pii;
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define itn int
ll powmod(ll a,ll b) {ll res=1; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, s = 0;
cin >> n;
int a[n], b[n];
for(int i=0;i<n;++i) cin >> a[i] >> b[i] ,s+=b[i], a[i]=2*a[i]-b[i];
int dp[n+1][200*n+1];
for(int i=0;i<=n;++i) for(int j=0;j<=200*n;++j) dp[i][j]=-1;
dp[0][0] = 0;
for(int i=0;i<n;++i) {
for(int l=i;l>=0;--l) {
for(int k=a[i];k<200*n+1;++k) {
if(dp[l][k-a[i]] != -1) {
dp[l+1][k] = max(dp[l][k-a[i]] + b[i], dp[l+1][k]);
}
}
}
}
double ans[n]; for(int i=0;i<n;++i) ans[i] = 0;
for(int l=1;l<=n;++l) {
for(int k=0;k<200*n+1;++k) {
if(dp[l][k] == -1) continue;
int r = dp[l][k] + min(s, k);
// cout << l << ' ' << k << ' ' << dp[l][k] << ' ';
if(r > ans[l-1]) ans[l-1] = r;
}
ans[l-1] /= 2;
}
for(int i=0;i<n;++i) cout << ans[i] << ' ' ;
cout << '\n';
return 0;
} | 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k, c = 1;
cin >> n >> k;
while (true) {
while (k - 3 >= 0 && (n - c) * 2 <= k - 3 && c <= n && c++) k -= 3;
while (k - 4 >= 0 && (n - c) * 2 <= k - 4 && c <= n && c++) k -= 4;
while (k - 5 >= 0 && (n - c) * 2 <= k - 5 && c <= n && c++) k -= 5;
break;
}
cout << n - c + 1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int inf = ~0U >> 1;
const int mod = 1e9 + 7;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int n, m, mat[N][N];
long long k;
bool ans[N][N];
inline int idx(int x, int y) { return x * m + y; }
inline bool inRange(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
struct DSU {
int p[N * N], cnt[N * N];
void init() {
memset(p, -1, sizeof(p));
memset(cnt, 0, sizeof(cnt));
}
int find_set(int x) {
if (p[x] == x) return x;
return p[x] = find_set(p[x]);
}
void merge(int x, int y) {
x = find_set(x), y = find_set(y);
if (x == y) return;
p[x] = y;
cnt[y] += cnt[x];
}
int get_cnt(pair<int, int> pt) {
return cnt[find_set(idx(pt.first, pt.second))];
}
void insert(pair<int, int> pt) {
int x = pt.first, y = pt.second;
p[idx(x, y)] = idx(x, y);
cnt[idx(x, y)] = 1;
for (int i = 0; i < 4; ++i) {
int ox = x + dx[i], oy = y + dy[i];
if (inRange(ox, oy) && p[idx(ox, oy)] != -1) {
merge(idx(x, y), idx(ox, oy));
}
}
}
} dsu;
vector<pair<int, pair<int, int> > > vec;
void dfs(int x, int y, int val, int& cnt) {
ans[x][y] = true;
cnt--;
for (int i = 0; i < 4; ++i) {
int ox = x + dx[i], oy = y + dy[i];
if (inRange(ox, oy) && mat[ox][oy] >= val && !ans[ox][oy] && cnt) {
dfs(ox, oy, val, cnt);
}
}
}
void print(int val) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
printf("%d%c", (ans[i][j] ? val : 0), (j == m - 1 ? '\n' : ' '));
}
int main() {
scanf("%d %d %I64d", &n, &m, &k);
for (int i = 0; i < n; ++i)
for (int j = 0, x; j < m; ++j) {
scanf("%d", &x);
mat[i][j] = x;
vec.push_back(make_pair(x, pair<int, int>(i, j)));
}
sort(vec.begin(), vec.end(), greater<pair<int, pair<int, int> > >());
dsu.init();
for (int i = 0; i < n * m; ++i) {
dsu.insert(vec[i].second);
if (k % vec[i].first == 0 &&
dsu.get_cnt(vec[i].second) >= k / vec[i].first) {
puts("YES");
int cnt = k / vec[i].first;
dfs(vec[i].second.first, vec[i].second.second, vec[i].first, cnt);
print(vec[i].first);
return 0;
}
}
puts("NO");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void show(const vector<T> &a) {
for (T x : a) cout << x << " ";
cout << '\n';
}
const long long N = 3e5 + 50, oo = 1e18 + 100, mod = 1e9 + 7;
const long double eps = 1e-9, PI = 2 * acos(0.0);
vector<long long> g[N];
vector<long long> visit(N, 0);
long long n, m, k;
long long cnt = 0;
signed main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin >> n;
for (long long i = 0; i < n; i++) {
long long b, p, f, h, c;
cin >> b >> p >> f >> h >> c;
cnt = 0;
b /= 2;
if (h > c) {
long long x = min(b, p);
b -= x;
cnt += x * h;
x = min(b, f);
cnt += x * c;
} else {
long long x = min(b, f);
b -= x;
cnt += x * c;
x = min(b, p);
cnt += x * h;
}
cout << cnt << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
std::vector<int> adj[100107];
bool mat[150][150];
std::vector<pair<int, int> > edge;
bool vis[100107];
int dist[100107], par[100107];
int n;
int ans = 1e9;
void bfs(int x, int y) {
for (int i = 0; i < n; i++) vis[i] = 0, dist[i] = 1e9, par[i] = -1;
dist[x] = 0;
queue<int> q;
q.push(x);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : adj[u]) {
if (vis[v] or (u == x and y == v)) continue;
vis[v] = 1;
dist[v] = dist[u] + 1;
if (mat[v][x] and dist[v] > 1) ans = min(ans, dist[v] + 1);
q.push(v);
}
}
}
int main() {
cin >> n;
long long arr[n + 5];
if (n < 3) return cout << "-1", 0;
map<int, int> mp;
bool f = 0;
int cnt = 0;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
if (!x) continue;
arr[cnt++] = x;
for (long long j = 0; j < 61; j++)
if (x & (1LL << j)) {
mp[j]++;
if (mp[j] >= 3) f = 1;
}
}
if (f) return cout << "3\n", 0;
n = cnt;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
for (int k = 0; k < 61; k++)
if (arr[i] & (1LL << k) and arr[j] & (1LL << k)) {
mat[i][j] = 1;
mat[j][i] = 1;
adj[i].push_back(j);
adj[j].push_back(i);
edge.push_back(pair<int, int>(i, j));
}
}
for (auto x : edge) {
int u = x.first;
int v = x.second;
bfs(u, v);
bfs(v, u);
}
if (ans > n + 100) ans = -1;
cout << ans << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 3e5 + 5;
int n;
int ans[max_n];
vector<int> v;
int rob(int pos) {
if (pos + 1 < 0 || pos + 1 >= n) return -1e9;
if (pos + 2 >= n) return v[pos + 1];
return min(max(v[pos], v[pos + 2]), v[pos + 1]);
}
int main() {
scanf("%d", &(n));
;
for (int i = (0); i < (n); ++i) {
int(a);
scanf("%d", &(a));
;
v.push_back(a);
}
if (n % 2 == 0) {
int best = v[0];
int x = n / 2;
ans[0] = max(v[x], v[x - 1]);
best = ans[0];
int p1 = x - 1, p2 = x;
for (int k = 2; k < n; k += 2) {
if (p1 - 1 >= 0) {
best = max(best, v[p1 - 1]);
}
if (p2 + 1 < n) {
best = max(best, v[p2 + 1]);
}
p1--;
p2++;
ans[k] = best;
}
if (n == 2)
ans[1] = max(v[0], v[1]);
else {
int x = n / 2;
int p1 = x - 2;
int p2 = x - 1;
ans[1] = max(rob(p1), rob(p2));
int best = ans[1];
for (int k = 3; k < n; k += 2) {
if (k == n - 1) break;
best = max(best, max(rob(p1 - 1), rob(p2 + 1)));
ans[k] = best;
p1--;
p2++;
}
ans[n - 1] = v[0];
for (int i = (0); i < (n); ++i) ans[n - 1] = max(ans[n - 1], v[i]);
}
} else {
if (n == 1)
ans[0] = v[0];
else {
int p1 = n / 2 - 1;
int p2 = n / 2 + 1;
ans[1] = max(max(v[p1], v[p1 + 1]), v[p2]);
int best = ans[1];
for (int k = 3; k < n; k += 2) {
if (p1 - 1 >= 0) {
best = max(best, v[p1 - 1]);
}
if (p2 + 1 < n) {
best = max(best, v[p2 + 1]);
}
ans[k] = best;
p1--;
p2++;
}
p1 = n / 2 - 1;
p2 = n / 2 - 1;
ans[0] = rob(p1);
best = ans[0];
for (int k = 2; k < n; k += 2) {
best = max(best, max(rob(p1 - 1), rob(p2 + 1)));
ans[k] = best;
p1--;
p2++;
}
ans[n - 1] = v[0];
for (int i = (0); i < (n); ++i) ans[n - 1] = max(ans[n - 1], v[i]);
}
}
for (int i = (0); i < (n); ++i) {
printf("%d ", ans[i]);
}
printf("\n");
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 * 1000 + 10;
int n, m, a[maxn], al[maxn], ar[maxn], tind[3 * maxn];
vector<int> tc[3 * maxn];
unordered_set<int> s;
int *ind = tind + maxn;
vector<int> *c = tc + maxn;
long long ans;
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a[i];
a[i]--;
}
if (n == 1) {
cout << 0;
return 0;
}
for (int i = 0; i < n; i++) {
ind[i] = i;
s.insert(i);
c[i].push_back(i);
}
for (int i = 0; i < m; i++) {
if (s.find(a[i] + i + 1) == s.end()) continue;
if (s.find(a[i] + i + 2) == s.end()) {
ind[a[i] + i + 2] = ind[a[i] + i + 1];
s.erase(a[i] + i + 1);
s.insert(a[i] + i + 2);
continue;
}
s.erase(a[i] + i + 1);
if ((((int)c[ind[a[i] + i + 1]].size())) <
(((int)c[ind[a[i] + i + 2]].size()))) {
for (auto &w : c[ind[a[i] + i + 1]]) c[ind[a[i] + i + 2]].push_back(w);
c[ind[a[i] + i + 1]].clear();
} else {
for (auto &w : c[ind[a[i] + i + 2]]) c[ind[a[i] + i + 1]].push_back(w);
c[ind[a[i] + i + 2]].clear();
ind[a[i] + i + 2] = ind[a[i] + i + 1];
}
}
for (auto &w : s) {
for (auto &g : c[ind[w]]) al[g] = max(0, w - (m + 1));
}
s.clear();
for (int i = -maxn; i < 2 * maxn; i++) {
c[i].clear();
ind[i] = i;
}
for (int i = 0; i < n; i++) {
c[i].push_back(i);
s.insert(i);
}
for (int i = 0; i < m; i++) {
if (s.find(a[i] - i - 1) == s.end()) continue;
if (s.find(a[i] - i - 2) == s.end()) {
ind[a[i] - i - 2] = ind[a[i] - i - 1];
s.erase(a[i] - i - 1);
s.insert(a[i] - i - 2);
continue;
}
s.erase(a[i] - i - 1);
if ((((int)c[ind[a[i] - i - 1]].size())) <
(((int)c[ind[a[i] - i - 2]].size()))) {
for (auto &w : c[ind[a[i] - i - 1]]) c[ind[a[i] - i - 2]].push_back(w);
c[ind[a[i] - i - 1]].clear();
} else {
for (auto &w : c[ind[a[i] - i - 2]]) c[ind[a[i] - i - 1]].push_back(w);
c[ind[a[i] - i - 2]].clear();
ind[a[i] - i - 2] = ind[a[i] - i - 1];
}
}
for (auto &w : s) {
for (auto &g : c[ind[w]]) {
ar[g] = min(n - 1, w + (m + 1));
ans += ar[g] - al[g] + 1;
}
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct block {
int x, y;
inline bool operator<(const block &cpr) const {
if (x != cpr.x) return x < cpr.x;
return y < cpr.y;
}
inline bool operator==(const block &cpr) const {
return ((x == cpr.x) && (y == cpr.y));
}
};
struct nblock {
int x, y, id;
inline bool operator<(const nblock &cpr) const {
if (y != cpr.y) return y < cpr.y;
return x < cpr.x;
}
};
block bl[100005];
nblock nbl[100005];
int vis[100005];
int n, m;
queue<int> pq;
int mov[8][2]{
1, 1, 1, 0, 1, -1, 0, -1, 0, 1, -1, 1, -1, 0, -1, -1,
};
int main() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d%d", &bl[i].x, &bl[i].y);
sort(bl + 1, bl + 1 + m);
for (int i = 1; i <= m; i++) {
nbl[i].x = bl[i].x;
nbl[i].y = bl[i].y;
nbl[i].id = i;
}
sort(nbl + 1, nbl + 1 + m);
for (int i = 1; i <= m; i++)
if (bl[i].x == n || bl[i].y == 1) {
vis[i] = 1;
pq.push(i);
}
while (pq.size()) {
block cur;
int cid;
cid = pq.front();
cur = bl[cid];
pq.pop();
if (cur.x > 1) {
block nw;
nw = cur;
nw.x--;
int top, bot, tr;
top = m;
bot = 1;
while (top > bot) {
tr = (top + bot + 1) / 2;
if (bl[tr] < nw)
bot = tr;
else
top = tr - 1;
}
if (bl[top].x == nw.x)
if (bl[top].y < nw.y)
if (vis[top] == 0) {
vis[top] = 1;
pq.push(top);
}
}
if (cur.y < n) {
nblock nw;
nw.x = cur.x;
nw.y = cur.y + 1;
int top, bot, tr;
top = m;
bot = 1;
while (top > bot) {
tr = (top + bot) / 2;
if (nw < nbl[tr])
top = tr;
else
bot = tr + 1;
}
if (nbl[top].y == nw.y)
if (nbl[top].x > nw.x)
if (vis[nbl[top].id] == 0) {
vis[nbl[top].id] = 1;
pq.push(nbl[top].id);
}
}
for (int dr = 0; dr < 8; dr++) {
block nw;
nw = cur;
nw.x += mov[dr][0];
nw.y += mov[dr][1];
int top, bot, tr;
top = m;
bot = 1;
while (top > bot) {
tr = (top + bot + 1) / 2;
if (nw < bl[tr])
top = tr - 1;
else
bot = tr;
}
if (nw == bl[top])
if (vis[top] == 0) {
vis[top] = 1;
pq.push(top);
}
}
}
for (int i = 1; i <= m; i++)
if (vis[i] == 1)
if (bl[i].x == 1 || bl[i].y == n) {
printf("-1\n");
return 0;
}
printf("%d\n", 2 * (n - 1));
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> v[n];
for (int i = 0; i < n; i++) {
string str;
cin >> str;
for (int j = 0; j < str.length(); j++) {
if (str[j] == '#') v[i].push_back(j);
}
}
int flag = 1;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int cmp = 0;
for (int k = 0; k < v[i].size(); k++) {
for (int l = 0; l < v[j].size(); l++) {
if (v[i][k] == v[j][l]) cmp++;
}
}
if (cmp == 0 || (v[i].size() == v[j].size() && cmp == v[i].size())) {
} else
flag = 0;
}
}
if (flag == 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m, tmp;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m)), b(m, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> a[i][j];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) cin >> b[i][j];
bool s = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == b[0][0]) {
for (int o = 0; o < n; o++) {
if (a[o][j] != b[0][o]) {
for (int u = 0; u < n; u++) {
if (a[o][j] == b[0][u]) {
swap(a[o], a[u]);
o = 0;
break;
}
}
}
}
s = true;
break;
}
}
if (s) break;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j] << ' ';
}
cout << '\n';
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10231;
int n, A, cf, cm;
long long m;
pair<int, int> t[N];
long long pref[N];
long long ok(int z, int w) {
int x = upper_bound(t + 1, t + n + 1, make_pair(z, -1)) - t - 1;
x = min(x, n - w);
return (long long)z * x - pref[x];
}
int ans[N];
int main() {
scanf("%d%d%d%d%lld", &n, &A, &cf, &cm, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &t[i].first);
t[i].second = i;
}
sort(t + 1, t + n + 1);
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1] + t[i].first;
}
long long best = -1;
pair<int, int> k = {0, 0};
for (int i = 0; i <= n; ++i) {
long long cost = (long long)A * i - (pref[n] - pref[n - i]);
cost = m - cost;
if (cost < 0) continue;
int Left = 0;
int Right = A;
while (Left < Right) {
int Mid = (Left + Right + 1) / 2;
if (ok(Mid, i) <= cost)
Left = Mid;
else
Right = Mid - 1;
}
long long val = (long long)i * cf + (long long)Left * cm;
if (val > best) {
best = val;
k = {i, Left};
}
}
printf("%lld\n", best);
for (int i = 1; i <= n; ++i) {
ans[t[i].second] = max(k.second, t[i].first);
if (n - i + 1 <= k.first) ans[t[i].second] = A;
}
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> goods, g;
vector<int> cost;
int n;
vector<vector<int>> d(105, vector<int>(100005, -1));
void bfs(vector<int>& nds, vector<int>& dis) {
queue<int> q;
for (auto& i : nds) {
q.push(i);
dis[i] = 0;
}
while (!q.empty()) {
int top = q.front();
q.pop();
for (auto& ch : g[top]) {
if (dis[ch] == -1) {
dis[ch] = dis[top] + 1;
q.push(ch);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, k, s;
cin >> n >> m >> k >> s;
goods = vector<vector<int>>(k);
g = vector<vector<int>>(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
x--;
goods[x].push_back(i);
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < k; i++) {
bfs(goods[i], d[i]);
}
for (int i = 0; i < n; i++) {
vector<int> v;
for (int j = 0; j < k; j++) {
v.push_back(d[j][i]);
}
sort(v.begin(), v.end());
cout << accumulate(v.begin(), v.begin() + s, 0) << " ";
}
return 0;
}
| 1 |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long ll ;
#define rep(i, a, b) for (int i = a; i <= b; ++ i)
const int N = 1e5 + 5 ;
using namespace std ;
int X, Y, Z, n, top, q[N] ; ll sum, f[N] ;
struct poi { int x, y, z ; } a[N] ;
bool cmp(poi a, poi b) { return a.x - a.y < b.x - b.y ; }
void push(int x) { sum -= x ; q[top ++] = x, push_heap(q, q + top) ; }
void pop() { sum += q[0] ; pop_heap(q, q + top), -- top ; }
int main() {
scanf("%d%d%d", &X, &Y, &Z) ;
n = X + Y + Z ;
rep(i, 1, n) scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z) ;
sort(a + 1, a + n + 1, cmp) ;
sum = 0 , top = 0 ;
rep(i, 1, Y) sum += a[i].z, push(a[i].z - a[i].y) ; f[Y] = sum ;
rep(i, Y + 1, n - X) {
sum += a[i].z ;
push(a[i].z - a[i].y), pop(), f[i] = sum ;
}
sum = 0, top = 0 ;
for (int i = n ; i > n - X ; -- i) sum += a[i].z, push(a[i].z - a[i].x) ; f[n - X] += sum ;
for (int i = n - X ; i > Y ; -- i) {
sum += a[i].z ;
push(a[i].z - a[i].x), pop(), f[i - 1] += sum ;
}
ll ans = 0 ;
rep(i, 1, n) ans = max(ans, f[i]) ;
printf("%lld\n", ans) ;
return 0 ;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int MAX = 2e5 + 5;
const long long MAX2 = 11;
const int MOD = 1000000000 + 7;
const long long INF = 20000;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const double pi = acos(-1);
const double EPS = 1e-9;
int n, x[MAX], mx;
stack<int> s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
if (n == 1) return cout << "YES", 0;
x[0] = x[n + 1] = 2e9;
for (int i = 1; i <= n; i++) {
cin >> x[i];
mx = max(mx, x[i]);
if (s.empty() || s.top() != x[i])
s.push(x[i]);
else
s.pop();
}
for (int i = 1; i <= n; i++)
if (x[i - 1] > x[i] && x[i + 1] > x[i]) return cout << "NO", 0;
if (s.empty() || (s.size() < 2 && s.top() == mx))
cout << "YES";
else
cout << "NO";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char a[100005];
int main() {
int n, x1, y1, x2, y2, n1, n2;
char ch1, ch2;
int t1, t2;
cin >> n >> x1 >> y1 >> x2 >> y2;
cin >> a;
if (x1 > x2) {
ch1 = 'W';
n1 = x1 - x2;
} else {
ch1 = 'E';
n1 = x2 - x1;
}
if (y1 > y2) {
ch2 = 'S';
n2 = y1 - y2;
} else {
ch2 = 'N';
n2 = y2 - y1;
}
int i, j;
if (n1 == 0) {
t1 = 0;
goto next;
}
for (i = 0; i < n; i++) {
if (a[i] == ch1) {
n1--;
}
if (n1 == 0) {
t1 = i + 1;
break;
}
}
next:
if (n2 == 0) {
t2 = 0;
goto mext;
}
for (j = 0; j < n; j++) {
if (a[j] == ch2) {
n2--;
}
if (n2 == 0) {
t2 = j + 1;
break;
}
}
mext:
if ((i == n) || (j == n)) {
cout << -1 << endl;
} else {
int ans = max(t1, t2);
cout << ans << endl;
}
}
| 2 |
#include <iostream>
#include <cstdio>
#include <vector>
#include <complex>
#include <cmath>
#include <algorithm>
using namespace std;
// * XYツ催?標
#define X real()
#define Y imag()
// * ツ点ツづ個表ツ個サ
typedef complex<double> P;
// * ツ仰鳴容ツつキツづゥツ古ォツ債キツε?
const double EPS = 1e-10;
// * Infinity
const double INF = 1e12;
// * ツ円ツ偲シツ猟ヲ
const double PI = acos(-1.0);
// * Infinity 2
const P INF_P(INF,INF);
// * complex<double> ツづ個渉?渉伉づーツ津ィツ義ツつキツづゥ
namespace std {
bool operator < (const P& a, const P& b) {
return (real(a) != real(b))? real(a) < real(b) : imag(a) < imag(b);
}
}
// * ツ禿?静?(dot product) : aツ・b = |a||b|cosツδヲ
double dot(P a, P b) {
return real( conj(a) * b );
}
// * ツ外ツ静?(cross product) : aツ×b = |a||b|sinツδヲ
double cross(P a, P b) {
return imag( conj(a) * b ) ;
}
int main(){
int n;
for(int t=1 ; scanf("%d", &n) , n ; t++ ){
vector<P> v;
for(int i=0 ; i < n ; i++ ){
int x, y;
scanf("%d %d", &x, &y);
v.push_back( P(x,y) );
}
double s = 0.0;
for(int i=0 ; i < v.size() ; i++ ) {
s += 0.5 * (v[(i+1)%v.size()].X - v[i].X) * (v[i].Y + v[(i+1)%v.size()].Y);
}
printf("%d %.1f\n", t , abs(s) );
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[100005];
int main() {
long long n, m;
while (scanf("%lld%lld", &n, &m) != EOF) {
memset(a, 0, sizeof(a));
long long sum = 0;
while (m--) {
long long x, d;
scanf("%lld%lld", &x, &d);
sum += n * x;
if (d > 0) {
sum += d * 0.5 * n * (n - 1);
} else {
if (n % 2 != 0) {
sum += d * (1 + (n - 1) / 2) * (n - 1) / 2;
} else {
sum += d * ((1 + (n - 2) / 2) * (n - 2) / 2 + n / 2);
}
}
}
double ans = 0.0;
ans = sum * 1.0 / n;
printf("%.15lf\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long s, a[200020], b, g, i, d, l, r, m, tmp, presum[200020];
vector<pair<long long, long long>> v;
cin >> s >> b;
for (i = 1; i <= s; i++) {
cin >> a[i];
}
for (i = 1; i <= b; i++) {
cin >> d >> g;
v.push_back({d, g});
}
sort(v.begin(), v.end());
for (i = 0; i < b; i++) {
if (i == 0) {
presum[0] = v[i].second;
} else {
presum[i] = presum[i - 1] + v[i].second;
}
}
for (i = 1; i <= s; i++) {
tmp = -1;
l = 0;
r = b - 1;
while (l <= r) {
m = (l + r) / 2;
if (v[m].first <= a[i]) {
tmp = m;
l = m + 1;
} else {
r = m - 1;
}
}
if (tmp == -1) {
cout << 0 << endl;
} else {
cout << presum[tmp] << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
string S;
cin>>S;
if(S=="hi"||S=="hihi"||S=="hihihi"||S=="hihihihi"||S=="hihihihihi"){cout<<"Yes"<<endl;}
else{cout<<"No"<<endl;}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct sta
{
int X,Y;
sta(){}
sta(int a,int b){X=a;Y=b;}
inline bool operator>(const sta& a) const
{
return X!=a.X?X>a.X:Y<a.Y;
}
};
#define P sta
const int MAXN=1e5+10,INF=1<<27;
int n,m,x,y,z,dist[MAXN];
set<int> s[MAXN];
vector<P> G[MAXN];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
G[x].push_back(P(y,z));G[y].push_back(P(x,z));
}
int oo,tmp;
priority_queue<P,vector<P>,greater<P> > q;
memset(dist,33,sizeof(dist));oo=*dist;
dist[1]=0;q.push(P(0,1));
while(!q.empty())
{
P t=q.top();q.pop();
int u=t.Y,cost=t.X;
if(cost>dist[u]) continue;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i].X,col=G[u][i].Y;
tmp=dist[u]+(!s[u].count(col));
if(tmp==dist[v]) s[v].insert(col);
else if(tmp<dist[v])
{
s[v].clear();s[v].insert(col);
dist[v]=tmp,q.push(P(dist[v],v));
}
}
}
printf("%d",(dist[n]==oo)?-1:dist[n]);
return 0;
} | 0 |
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll x,y,tmp;
inline ll gcd(ll x,ll y){
if(y==0)return x;
return gcd(y,x%y);
}
int main(){
cin >>x>>y;
ll g = gcd(x,y);
for(ll i=2; i<= g / i; i++)
if(g%i==0)
{
tmp++;
while(g%i == 0) g /= i;
}
if(g>1)tmp++;
cout<<tmp+1<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 13;
int n, L, a[1 << N], b[1 << N], c[1 << N], d[1 << N], val[1 << N], sum;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
L = 1 << n;
for (int i = 0; i < L; ++i)
cin >> val[i], sum ^= val[i], a[i] = b[i] = c[i] = d[i] = i;
if (sum) {
cout << "Fou\n";
return 0;
}
cout << "Shi\n";
for (int i = 0; i < L; ++i)
for (int j = i; (a[j] ^ b[j]) != val[j];) {
int k = c[val[j] ^ b[j]];
swap(a[j], a[k]);
swap(c[a[j]], c[a[k]]);
if (k > i) break;
swap(b[k], b[i + 1]);
swap(d[b[k]], d[b[i + 1]]);
j = k;
}
for (int i = 0; i < L; ++i) cout << a[i] << " \n"[i == L - 1];
for (int i = 0; i < L; ++i) cout << b[i] << " \n"[i == L - 1];
return 0;
}
| 5 |
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define forE(i,x) for(int i=head[x];i!=-1;i=ne[i])
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned u32;
typedef pair<int,int> pin;
#define mk(a,b) make_pair(a,b)
#define lowbit(x) ((x)&(-(x)))
#define sqr(a) ((a)*(a))
#define clr(a) (memset((a),0,sizeof(a)))
#define ls ((x)<<1)
#define rs (((x)<<1)|1)
#define mid (((l)+(r))>>1)
#define pb push_back
#define w1 first
#define w2 second
inline void read(int &x){
x=0;int f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
x*=f;
}
inline void judge(){
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
}
/*******************************head*******************************/
const int maxn=200005;
vector<int> ea[maxn],eb[maxn];
int st[maxn],ed[maxn],n,S,rt,ans,vis[maxn],fa[maxn];
int dep[maxn];
const int inf=1e9;
inline void dfs(int x,int f){
for(int w:eb[x]){
if(w==f)continue;
dep[w]=dep[x]+1;
fa[w]=x;
dfs(w,x);
}
}
inline void calc(int x,int f,int cur){
if(ans==inf)return;
if(dep[x]<cur)return;
if(dep[x]==cur){
ans=max(ans,cur<<1);
return;
}
if(vis[x]){
ans=inf;return;
}
for(int w:ea[x]){
if(w==f)continue;
calc(w,x,cur+1);
}
ans=max(ans,dep[x]<<1);
}
inline bool get(int x,int y){
if(dep[x]<dep[y])swap(x,y);
if(fa[x]==y)return 0;
if(fa[fa[x]]==y)return 0;
if(fa[x]==fa[y])return 0;
return 1;
}
int main(){
read(n);read(S);read(rt);
rep(i,1,n-1){
read(st[i]);read(ed[i]);
ea[st[i]].pb(ed[i]);
ea[ed[i]].pb(st[i]);
}
rep(i,1,n-1){
int x,y;read(x);read(y);
eb[x].pb(y);eb[y].pb(x);
}
dfs(rt,0);
rep(i,1,n-1)if(get(st[i],ed[i]))vis[st[i]]=vis[ed[i]]=1;
calc(S,0,0);
if(ans==inf)
puts("-1");
else
printf("%d\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int32_t main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
;
long long n, m, q;
cin >> n >> m >> q;
long long g = gcd(n, m);
long long x = n / g, y = m / g;
while (q--) {
long long a, b, c, d;
cin >> a >> b >> c >> d;
long long n1, n2;
if (a == 1)
n1 = (b - 1) / x;
else
n1 = (b - 1) / y;
if (c == 1)
n2 = (d - 1) / x;
else
n2 = (d - 1) / y;
if (n1 == n2) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int eps = 10e-13;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long double c;
cin >> c;
long double n = c;
map<long double, long double> m;
vector<long double> v;
long double sum = 0;
while (c--) {
long double e;
cin >> e;
m[e]++;
sum += e;
v.push_back(e);
}
long double mot = (long double)(sum / (long double)n);
long double ans = 0;
set<long double> st;
for (int i = 0; i < v.size(); i++) {
long double o =
(long double)((long double)(-1) * (mot * (n - 2) - sum + v[i]));
st.insert(o);
st.insert(v[i]);
if (m[o]) {
if (st.size() == 2)
ans += (m[o]);
else
ans += (m[o] - 1);
}
st.clear();
}
cout << (long long)ans / 2 << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = (0); i <= (n - 1); ++i) scanf("%d", &a[i]);
int l = 0, r = 1e9, ans = 0;
auto check = [&](int mid) -> int {
auto b(a);
for (int i = (n - 1); i >= (2); --i) {
if (b[i] < mid) return 0;
int t = min((b[i] - mid) / 3, a[i] / 3);
b[i - 1] += t;
b[i - 2] += t * 2;
b[i] -= t * 3;
}
return b[0] >= mid && b[1] >= mid;
};
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n];
long long int temp = 0;
long long int count = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] >= 0) {
temp += arr[i];
} else {
if (abs(arr[i]) <= temp) {
temp -= abs(arr[i]);
} else if (abs(arr[i]) > temp) {
count += (abs(arr[i]) - temp);
temp = 0;
}
}
}
cout << count << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long X;
long long Ans[8000010][2] = {{0}};
int Ansp = 0;
int main() {
cin >> X;
X *= 6;
for (long long i = 1;; i++) {
if (i * i * i > X) break;
long long div = i * (i + 1);
if (X % div == 0) {
long long MM = X / div;
long long M3 = MM + i - 1;
if (M3 % 3 != 0) continue;
if (M3 / 3 < i) break;
Ansp++;
Ans[Ansp][0] = i, Ans[Ansp][1] = M3 / 3;
}
}
for (int i = Ansp; i >= 1; i--) {
if (Ans[i][0] != Ans[i][1]) {
Ansp++;
Ans[Ansp][0] = Ans[i][1];
Ans[Ansp][1] = Ans[i][0];
}
}
cout << Ansp << endl;
for (int i = 1; i <= Ansp; i++) printf("%I64d %I64d\n", Ans[i][0], Ans[i][1]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct point{
int a,b;
point *l,*r,*pa;
point(){l=r=pa=NULL;}
point(int aa,int bb):a(aa),b(bb){l=r=pa=NULL;};
};
point *root;
point *rightRotate(point *t){
point *s=t->l;
t->l=s->r;
s->r=t;
return s;
}
point *leftRotate(point *t){
point *s=t->r;
t->r=s->l;
s->l=t;
return s;
}
point* insert(point *p,int a,int b){
if(p==NULL)return new point(a,b);
if(a==p->a)return p;
if(a<p->a){
p->l=insert(p->l,a,b);
if(p->b<p->l->b)
p=rightRotate(p);
}else{
p->r=insert(p->r,a,b);
if(p->b<p->r->b)
p=leftRotate(p);
}return p;
}
point *_del(point *p, int a);
point *del(point *p,int a){
if(p==NULL)return NULL;
if(a<p->a)p->l=del(p->l,a);
else if(a>p->a)p->r=del(p->r,a);
else return _del(p,a);
return p;
}
point *_del(point *p, int a){
if(p->l==NULL&&p->r==NULL)return NULL;
else if(p->l==NULL)p=leftRotate(p);
else if(p->r==NULL)p=rightRotate(p);
else{
if(p->l->b>p->r->b)p=rightRotate(p);
else p=leftRotate(p);
}return del(p,a);
}
bool find(int a){
point *p=root;
while(p){
if(a==p->a)return 1;
if(a>p->a)p=p->r;
else p=p->l;
}
return 0;
}
void dfs1(point *p){
if(!p)return;
dfs1(p->l);
printf(" %d",p->a);
dfs1(p->r);
}
void dfs2(point *p){
if(!p)return;
printf(" %d",p->a);
dfs2(p->l);
dfs2(p->r);
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int T;cin>>T;
int ta,tb;
string s;
while(cin>>s){
if(s=="insert"){
cin>>ta>>tb;
// if(!root)root=new point(ta,tb);
// else
root=insert(root,ta,tb);
}else if(s=="find"){
cin>>ta;
if(find(ta))printf("yes\n");
else printf("no\n");
}else if(s=="print"){
dfs1(root);
printf("\n");
dfs2(root);
printf("\n");
}else if(s=="delete"){
cin>>ta;
root=del(root,ta);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, p, t, ans, f[100010], g[100010];
struct node {
int x, y;
} A[100010];
inline void div(int a, int b, int &c, int &r) {
c = a / b;
r = a % b;
}
int main() {
scanf("%*d%d%d%d", &n, &p, &t);
for (int i = 1; i <= n; ++i) scanf("%d%d", &A[i].x, &A[i].y);
g[0] = -t;
for (int i = 1, k1 = 0, k2 = 0, mx = 0; i <= n; ++i) {
int h, v;
div(A[i].y - A[i].x, p, v, h);
g[i] = A[i].y - h;
while (g[k1] <= A[i].x - t) {
mx = max(mx, f[k1]);
++k1;
}
f[i] = mx + v;
k2 = k1;
while (k2 < i && g[k2] <= A[i].y - t) {
int hh, vv;
div(A[i].y - g[k2] - t, p, vv, hh);
if (f[k2] + vv > f[i] || (f[k2] + vv == f[i] && A[i].y - hh < g[i])) {
f[i] = f[k2] + vv;
g[i] = A[i].y - hh;
}
++k2;
}
ans = max(ans, f[i]);
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
map<long long, long long> mp;
long long p;
long long mul(long long x, long long y) { return (x * y) % p; }
long long val(long long x, long long k) {
long long f = mul(mul(x, x), mul(x, x));
long long s = mul(k, x);
f -= s;
f %= p;
f += p;
return f % p;
}
int main() {
long long n, k;
cin >> n >> p >> k;
long long ans = 0;
for (long long i = 0; i < n; i++) {
long long x;
cin >> x;
x = val(x, k);
ans += mp[x];
mp[x]++;
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
using namespace std;
const long long MOD = 1e9 + 7;
long long n, m, k, l, r;
long long q, y;
long long kla, klb, dp[1000001], ans[1000001], e[1000001];
long long a[1000001];
string s, d;
vector<pair<long long, long long> > v;
bool tt[1000001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
v.push_back(make_pair(0, 0));
for (int i = 1; i <= n; i++) {
cin >> a[i];
v.push_back(make_pair(a[i], i));
}
sort(v.begin(), v.end());
dp[0] = 0;
for (int i = 1; i <= n; i++) dp[i] = 1e18;
for (int i = 0; i < n; i++)
for (int j = 3; j < min(n - i + 1, 6ll); j++) {
if (dp[i + j] > dp[i] + v[i + j].first - v[i + 1].first) {
dp[i + j] = dp[i] + v[i + j].first - v[i + 1].first;
e[i + j] = j;
}
}
long long p = 0;
tt[n] = 1;
for (int i = n; i > 0; i--) {
if (tt[i]) p++, tt[i - e[i]] = 1;
ans[v[i].second] = p;
}
cout << dp[n] << " " << p << "\n";
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long N, a[26][26], min1[26][26], max1[26][26];
int main() {
long long i, j;
cin >> N;
for (j = N - 2; j >= 0; j--) {
for (i = 1; i < N; i++) {
a[i][j] = max1[i - 1][j + 1] - min1[i][j + 1] + 1;
min1[i][j] = min1[i][j + 1] + a[i][j];
}
max1[N - 1][j] = min1[N - 1][j];
for (i = N - 2; i >= 0; i--) {
max1[i][j] = max1[i + 1][j] + a[i][j];
}
}
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%lld ", a[i][j]);
}
fflush(stdout);
printf("\n");
}
long long Q;
cin >> Q;
while (Q--) {
long long X;
scanf("%lld", &X);
long long nowx = 0, nowy = 0;
while (1) {
printf("%lld %lld\n", nowx + 1, nowy + 1);
fflush(stdout);
if (nowx == N - 1 && nowy == N - 1) break;
if (nowx <= N - 2 && X >= min1[nowx + 1][nowy]) {
nowx++;
X -= a[nowx][nowy];
} else {
nowy++;
X -= a[nowx][nowy];
}
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int A,B,C;
cin >> A >> B >> C;
string ans="NO";
for(int i=1;i<10000000;i++){
if(i*A%B==C){
ans="YES";}
}
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string s;
cin >> s;
int res = 0, cur = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '(') {
++cur;
} else {
if (cur > 0) {
--cur;
res += 2;
}
}
}
cout << res << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 300 + 5;
const int MMAX = 3000 + 5;
const int INF = 1000000000;
const int BS = 1000000000 + 7;
const long long INFLL = 1000000000ll * 1000000000ll;
const long double eps = 1e-6;
int a[NMAX];
int c[NMAX];
map<int, int> dp[NMAX];
int gcd(int x, int y) {
if (x > y) return gcd(y, x);
if (0 == x || 0 == y) return x + y;
return gcd(y % x, x);
}
void update(int i, int j, int value) {
if (dp[i].find(j) == dp[i].end()) {
dp[i][j] = value;
} else {
dp[i][j] = min(dp[i][j], value);
}
}
int main(int argc, char** argv) {
if (argc > 1 && strcmp(argv[1], "seland") == 0) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 1; i <= n; i++) {
dp[i][a[i]] = c[i];
for (auto it : dp[i - 1]) {
update(i, it.first, it.second);
update(i, gcd(it.first, a[i]), it.second + c[i]);
}
}
if (dp[n].find(1) == dp[n].end()) {
cout << -1 << endl;
} else {
cout << dp[n][1] << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
int cnt, n, m;
long long d[100005];
int head[100005], vis[100005];
struct cmp {
bool operator()(const pair<long long, int> p1,
const pair<long long, int> p2) {
return p1.first > p2.first;
}
};
priority_queue<pair<long long, int>, vector<pair<long long, int> >, cmp> q;
struct node {
int x, y, wz;
} mp[100005];
struct pp {
int st, ed, v;
} edge[600500];
void add(int x, int y, int z) {
edge[++cnt].ed = y;
edge[cnt].v = z;
edge[cnt].st = head[x];
head[x] = cnt;
}
bool cmpx(node x, node y) { return x.x < y.x; }
bool cmpy(node x, node y) { return x.y < y.y; }
void dijkstr() {
for (int i = 0; i <= m + 2; i++) d[i] = inf;
d[1] = 0;
q.push(pair<long long, int>(0, 1));
while (!q.empty()) {
int id = q.top().second;
q.pop();
if (vis[id]) continue;
vis[id] = 1;
for (int i = head[id]; i; i = edge[i].st) {
int v = edge[i].ed;
long long w = edge[i].v;
if (!vis[v] && w + d[id] < d[v]) {
d[v] = w + d[id];
q.push(pair<long long, int>(d[v], v));
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
add(1, m + 2, abs(x1 - x2) + abs(y1 - y2));
for (int i = 2; i <= m + 1; i++)
scanf("%d%d", &mp[i].x, &mp[i].y),
mp[i].wz = i, add(1, i, min(abs(mp[i].x - x1), abs(mp[i].y - y1))),
add(i, m + 2, abs(mp[i].x - x2) + abs(mp[i].y - y2));
sort(mp + 2, mp + m + 2, cmpx);
for (int i = 2; i <= m + 1; i++)
if (i == 2)
add(mp[i].wz, mp[i + 1].wz, abs(mp[i].x - mp[i + 1].x));
else if (i == m + 1)
add(mp[i].wz, mp[i - 1].wz, abs(mp[i].x - mp[i - 1].x));
else
add(mp[i].wz, mp[i + 1].wz, abs(mp[i].x - mp[i + 1].x)),
add(mp[i].wz, mp[i - 1].wz, abs(mp[i].x - mp[i - 1].x));
sort(mp + 2, mp + m + 2, cmpy);
for (int i = 2; i <= m + 1; i++)
if (i == 2)
add(mp[i].wz, mp[i + 1].wz, abs(mp[i].y - mp[i + 1].y));
else if (i == m + 1)
add(mp[i].wz, mp[i - 1].wz, abs(mp[i].y - mp[i - 1].y));
else
add(mp[i].wz, mp[i + 1].wz, abs(mp[i].y - mp[i + 1].y)),
add(mp[i].wz, mp[i - 1].wz, abs(mp[i].y - mp[i - 1].y));
dijkstr();
printf("%lld\n", d[m + 2]);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long readint() {
long long x = 0, y = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') y = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * y;
}
long long n;
long long a[100010];
signed main() {
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long ans = 0;
for (long long i = 0; i < n; i++)
if (a[i] == a[2]) ans++;
if (a[2] == a[0])
ans = ans * (ans - 1) * (ans - 2) / 6;
else if (a[1] == a[2])
ans = ans * (ans - 1) / 2;
cout << ans << '\n';
return 0;
}
| 2 |
#include<iostream>
#include<string>
using namespace std;
int main()
{
string sentence;
string::size_type a, b, i;
int c, d;
getline(cin, sentence);
i = 0;
while (1) {
a = sentence.find("apple", i);
b = sentence.find("peach", i);
if( a==string::npos && b==string::npos ) break;
if (a<b) {
sentence.replace(a, 5, "peach");
i = a+5;
} else {
sentence.replace(b, 5, "apple");
i = b+5;
}
}
cout<<sentence<<endl;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,k;cin>>n>>k;
cout<<(((n-1)/(k-1))+((n-1)%(k-1)?1:0))<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using vi = vector<long long>;
template <typename T>
std::istream& operator>>(std::istream& input, std::pair<T, T>& data) {
input >> data.first >> data.second;
return input;
}
template <typename T>
std::istream& operator>>(std::istream& input, std::vector<T>& data) {
for (T& first : data) input >> first;
return input;
}
template <typename T>
std::ostream& operator<<(std::ostream& output, const pair<T, T>& data) {
output << "(" << data.first << "," << data.second << ")";
return output;
}
template <typename T>
std::ostream& operator<<(std::ostream& output, const std::vector<T>& data) {
for (const T& first : data) output << first << " ";
return output;
}
std::ostream& operator<<(std::ostream& output, const __int128& first) {
__int128 n = first;
if (n == 0) {
output << "0";
return output;
}
if (n < 0) {
n = -n;
output << "-";
}
string s;
while (n) {
s += '0' + (n % 10);
n /= 10;
}
reverse(s.begin(), s.end());
cout << s;
return output;
}
long long div_up(long long a, long long b) {
return a / b + ((a ^ b) > 0 && a % b);
}
long long div_down(long long a, long long b) {
return a / b - ((a ^ b) < 0 && a % b);
}
long long math_mod(long long a, long long b) { return a - b * div_down(a, b); }
template <class T>
using V = vector<T>;
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
long long gcd(long long a, long long b) {
while (b) {
tie(a, b) = make_pair(b, a % b);
}
return a;
}
long long Bit(long long mask, long long bit) { return (mask >> bit) & 1; }
const long long INF = 1e16 + 7, C = 1e9 + 7;
struct Dinic {
struct edge {
long long to, flow, cap;
};
const static long long N = 57 * 207 + 7;
vector<edge> e;
vector<long long> g[N + 7];
long long dp[N + 7];
long long ptr[N + 7];
void clear() {
for (long long i = 0; i < N + 7; i++) g[i].clear();
e.clear();
}
void addEdge(long long a, long long b, long long cap) {
g[a].push_back(e.size());
e.push_back({b, 0, cap});
g[b].push_back(e.size());
e.push_back({a, 0, 0});
}
long long minFlow, start, finish;
bool bfs() {
for (long long i = 0; i < N; i++) dp[i] = -1;
dp[start] = 0;
vector<long long> st;
long long uk = 0;
st.push_back(start);
while (uk < st.size()) {
long long v = st[uk++];
for (long long to : g[v]) {
auto ed = e[to];
if (ed.cap - ed.flow >= minFlow && dp[ed.to] == -1) {
dp[ed.to] = dp[v] + 1;
st.push_back(ed.to);
}
}
}
return dp[finish] != -1;
}
long long dfs(long long v, long long flow) {
if (v == finish) return flow;
for (; ptr[v] < g[v].size(); ptr[v]++) {
long long to = g[v][ptr[v]];
edge ed = e[to];
if (ed.cap - ed.flow >= minFlow && dp[ed.to] == dp[v] + 1) {
long long add = dfs(ed.to, min(flow, ed.cap - ed.flow));
if (add) {
e[to].flow += add;
e[to ^ 1].flow -= add;
return add;
}
}
}
return 0;
}
long long dinic(long long start, long long finish) {
Dinic::start = start;
Dinic::finish = finish;
long long flow = 0;
for (minFlow = (1ll << 60); minFlow; minFlow >>= 1) {
while (bfs()) {
for (long long i = 0; i < N; i++) ptr[i] = 0;
while (long long now = dfs(start, INF)) flow += now;
}
}
return flow;
}
} dinic;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin >> n >> m;
V<vi> f(n, vi(3));
cin >> f;
vi l(n), r(n);
vi start(n);
const long long S = dinic.N - 2, T = dinic.N - 1;
long long ptr = 0;
for (long long i = (0); i < (n); ++i) {
cin >> l[i] >> r[i];
start[i] = ptr;
ptr += r[i] - l[i] + 2;
dinic.addEdge(S, start[i], INF);
dinic.addEdge(start[i] + r[i] + 1 - l[i], T, INF);
for (long long first = l[i]; first <= r[i]; ++first) {
dinic.addEdge(start[i] + first - l[i], start[i] + first - l[i] + 1,
C - (f[i][0] * first * first + f[i][1] * first + f[i][2]));
}
}
for (long long _ = (0); _ < (m); ++_) {
long long u, v, d;
cin >> u >> v >> d;
u--;
v--;
for (long long first = l[u]; first <= r[u]; ++first) {
if (first - d <= l[v]) {
} else if (first - d <= r[v]) {
dinic.addEdge(start[u] + first - l[u], start[v] + (first - d) - l[v],
INF);
} else {
dinic.addEdge(start[u] + first - l[u], T, INF);
}
}
}
cout << n * C - dinic.dinic(S, T) << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int fib[51];
vector<long long int> calc(long long int n, long long int k) {
vector<long long int> res;
if (!n) return res;
if (n == 1) {
res.push_back(1);
return res;
}
if (k <= fib[n - 1]) {
res.push_back(1);
vector<long long int> v = calc(n - 1, k);
for (auto i : v) res.push_back(i + 1);
} else {
res.push_back(2);
res.push_back(1);
vector<long long int> v = calc(n - 2, k - fib[n - 1]);
for (auto i : v) res.push_back(i + 2);
}
return res;
}
void solve() {
fib[1] = 1, fib[2] = 2;
for (long long int i = 3; i <= 50; i++) fib[i] = fib[i - 1] + fib[i - 2];
long long int n, k;
cin >> n >> k;
vector<long long int> ans = calc(n, k);
for (auto i : ans) cout << i << " ";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cout.precision(20);
long long int t = 1;
for (long long int i = 1; i <= t; i++) {
if (0) cout << "Case #" << i << ": ";
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1000001;
int memo1[nmax];
int memo2[nmax];
int main() {
int m;
cin >> m;
int x1, y1, x2, y2, a1, a2;
long long h1, h2;
cin >> h1 >> a1;
cin >> x1 >> y1;
cin >> h2 >> a2;
cin >> x2 >> y2;
int ans1 = 0, ans2 = 0;
for (int i = 0; i < m; ++i) {
if (h1 == a1) {
break;
}
h1 = ((long long)h1 * x1 + y1) % m;
if (memo1[h1]) {
cout << "-1\n";
return 0;
}
++ans1;
memo1[h1] = 1;
}
for (int i = 0; i < m; ++i) {
if (h2 == a2) {
break;
}
h2 = ((long long)h2 * x2 + y2) % m;
if (memo2[h2]) {
cout << "-1\n";
return 0;
}
++ans2;
memo2[h2] = 1;
}
int l1 = 0, l2 = 0;
if (ans1 == ans2) {
cout << ans1 << "\n";
return 0;
} else {
if (ans1 < ans2) {
for (int i = ans1 + 1; i <= ans2; ++i) {
h1 = ((long long)h1 * x1 + y1) % m;
}
if (h1 == a1) {
cout << ans2 << "\n";
return 0;
}
} else {
for (int i = ans2 + 1; i <= ans1; ++i) {
h2 = ((long long)h2 * x2 + y2) % m;
}
if (h2 == a2) {
cout << ans1 << "\n";
return 0;
}
}
h1 = a1;
memset(memo1, 0, sizeof(memo1));
memset(memo2, 0, sizeof(memo2));
for (int i = 0; i < m; ++i) {
h1 = ((long long)h1 * x1 + y1) % m;
if (memo1[h1]) {
cout << "-1\n";
return 0;
}
++l1;
memo1[h1] = 1;
if (h1 == a1) {
break;
}
}
h2 = a2;
for (int i = 0; i < m; ++i) {
h2 = ((long long)h2 * x2 + y2) % m;
if (memo2[h2]) {
cout << "-1\n";
return 0;
}
++l2;
memo2[h2] = 1;
if (h2 == a2) {
break;
}
}
}
if (ans1 < ans2) {
swap(ans1, ans2);
swap(l1, l2);
}
int nr_cycles = 0;
for (;;) {
long long dist = (long long)nr_cycles * l1 + ans1 - ans2;
if (dist % l2 == 0) {
break;
}
++nr_cycles;
if (nr_cycles > 10000000) {
cout << "-1\n";
return 0;
}
}
long long ans = (long long)nr_cycles * l1 + ans1;
cout << ans << "\n";
return 0;
}
| 3 |
// AOJ q0040
// YAMADA Terushige
// 2013.5.6
#include<iostream>
#include<sstream>
#include<string>
#include<cctype>
using namespace std;
const int N = 26;
char decode(const char c,const int a,const int b){
if(islower(c)){
// int t = ((c - 'a') + N - b)%N;
int t = c - 'a' - b;
while(t%a != 0){
t += N;
}
return 'a' + ((t/a) % N) ;
}
return c;
}
int gcm(int a,int b){
if (b==0) return a;
else return gcm(b,a%b);
}
int main(){
string s,wd;
int n;
cin >> n;
getline(cin,s);
int a,b;
for(int i=0;i<n;i++){
getline(cin,s);
string ans = s;
istringstream istr;
istr.str(s);
while(istr >> wd){
if(wd.size()==4){
string dd = wd;
for(a=1;a<N;a++){
if(gcm(a,N) != 1) continue;
for(b=0;b<N;b++){
for(int j=0;j<4;j++)
dd[j] = decode(wd[j],a,b);
if(dd == "this" || dd == "that")
goto solved;
}
}
}
}
solved:
// istr.clear();
for(int k=0;k<s.size();k++)
ans[k]=decode(s[k],a,b);
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long p = (long long)1e9 + 7;
long long n, k;
long long bin_pow(long long a, long long n) {
if (!n) return 1;
if (n % 2)
return (bin_pow(a, n - 1) * a) % p;
else {
long long b = bin_pow(a, n / 2);
return (b * b) % p;
}
}
int main() {
cin >> n >> k;
cout << (bin_pow(k, k - 1) * bin_pow(n - k, n - k)) % p << endl;
return 0;
}
| 4 |
#include <iostream>
using namespace std;
int main(void){
int n;
cin >> n;
string name;
long m,a,r,c,h;
m=0;
a=0;
r=0;
c=0;
h=0;
for (int i = 0; i < n; i++)
{
cin >> name;
if (name[0] == 'M') m++;
if (name[0] == 'A') a++;
if (name[0] == 'R') r++;
if (name[0] == 'C') c++;
if (name[0] == 'H') h++;
}
cout << m*a*r + m*a*c + m*a*h + m*r*c + m*r*h + m*c*h + a*r*c + a*r*h + a*c*h +r*c*h << endl;
return 0;
}
| 0 |
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
while(cin >>s){
bool f = false;
int x = 0;
for(int i=0; i<s.size(); i++){
if(s[i] == '@'){f = true;}
else if(f && x == 0){x = s[i]-'0';}
else if(f){
for(int j=0; j<x; j++){cout <<s[i];}
f = false;
x = 0;
}
else cout <<s[i];
}
cout <<endl;
}
return 0;
} | 0 |
#include<cstdio>
#define RI register int
using namespace std;
typedef long long LL;
const int N=205;
struct Linear_Basis
{
LL p[63];
inline void clear(void)
{
for (RI i=0;i<63;++i) p[i]=0;
}
inline void insert(LL x)
{
for (RI i=62;~i;--i) if ((x>>i)&1)
{
if (!p[i]) return (void)(p[i]=x); x^=p[i];
}
}
inline bool exist(LL x)
{
for (RI i=62;~i;--i) if ((x^p[i])<x) x^=p[i]; return !x;
}
}l;
int t,n; LL a[N]; char s[N];
int main()
{
for (scanf("%d",&t);t;--t)
{
RI i; for (scanf("%d",&n),i=1;i<=n;++i) scanf("%lld",&a[i]);
scanf("%s",s+1); if (s[n]=='1') { puts("1"); continue; }
bool flag=0; for (l.clear(),i=n;i;--i) if (s[i]=='1')
{
if (!l.exist(a[i])) { flag=1; break; }
} else l.insert(a[i]);
puts(flag?"1":"0");
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MAXN = 83;
const int MAXM = 4007;
struct eT {
void setd(int _u, int _v, int _w, int _l) {
u = _u, v = _v, w = _w, last = _l;
}
int u, v, w, last;
} edge[MAXM];
int n, tk;
int m;
int ke, la[MAXN];
int dp[2][MAXN][MAXN][2];
void init();
void input();
void work();
int main() {
init();
input();
work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
scanf("%d%d", &n, &tk);
scanf("%d", &m);
int u, v, w;
ke = 0;
memset(la, -1, sizeof(la));
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &u, &v, &w);
edge[ke].setd(u, v, w, la[u]);
la[u] = ke++;
}
}
void work() {
memset(dp, 0x3f3f3f3f, sizeof(dp));
for (int i = 0; i <= n + 1; ++i)
for (int j = i; j <= n + 1; ++j) dp[1][i][j][0] = dp[1][i][j][1] = 0;
int t, now, v;
for (int nk = 2; nk <= tk; ++nk) {
t = nk & 1;
memset(dp[t], 0x3f3f3f3f, sizeof(dp[t]));
for (int l = 0; l <= n + 1; ++l) {
for (int r = l + 1; r <= n + 1; ++r) {
for (int d = 0; d < 2; ++d) {
now = d ? r : l;
for (int i = la[now]; ~i; i = edge[i].last) {
v = edge[i].v;
if (v > l && v < r) {
dp[t][l][r][d] =
min(dp[t][l][r][d],
edge[i].w + (((dp[t ^ 1][l][v][1]) < (dp[t ^ 1][v][r][0]))
? (dp[t ^ 1][l][v][1])
: (dp[t ^ 1][v][r][0])));
}
}
}
}
}
}
int ans = INF;
t = tk & 1;
for (int l = 0; l <= n + 1; ++l)
for (int r = l + 1; r <= n + 1; ++r)
for (int d = 0; d < 2; ++d)
ans = (((ans) < (dp[t][l][r][d])) ? (ans) : (dp[t][l][r][d]));
printf("%d\n", (ans < 1e9) ? ans : -1);
}
| 4 |
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
const int MOD=100000;
int main(){
int n,m,h,k;
while(cin>>n>>m>>h>>k&&(n||m||h||k)){
vector<int> V[h];
int p[n],a,b;
REP(i,n)cin>>p[i];
int ans=0,d[n],c[n];
REP(i,m){
cin>>a>>b;a--;b--;
V[b].PB(a);
}
REP(i,n)d[i]=c[i]=i;
REP(i,h)REP(j,V[i].size())swap(d[V[i][j]],d[V[i][j]+1]);
REP(i,n)if(d[i]<k)ans+=p[i];
for(int i=h-1;i>=0;i--){
REP(j,V[i].size()){
swap(d[V[i][j]],d[V[i][j]+1]);
int res=0;
REP(l,n)if(d[l]<k)res+=p[c[l]];
ans=min(ans,res);
swap(d[V[i][j]],d[V[i][j]+1]);
}
REP(j,V[i].size())swap(d[V[i][j]],d[V[i][j]+1]);
REP(j,V[i].size())swap(c[V[i][j]],c[V[i][j]+1]);
}
cout<<ans<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
const long long int MOD = 998244353;
long long int postsum[MAXN], fact[MAXN], inv_fact[MAXN];
long long int power(long long int a, long long int b)
{
if (b == 0)
{
return 1;
}
long long int ans = power(a, b/2);
ans = (ans * ans) % MOD;
if (b%2)
{
ans = (ans * a) % MOD;
}
return ans;
}
void precompute()
{
fact[0] = 1;
for (int i = 1; i < MAXN; ++i)
{
fact[i] = (fact[i - 1] * i) % MOD;
}
inv_fact[MAXN - 1] = power(fact[MAXN - 1], MOD - 2);
for (int i = MAXN - 2; i >= 0; --i)
{
inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
}
}
long long int comb(int n, int r)
{
// cerr<<n<<"\t"<<r<<":\t";
if (r > n)
{
// cerr<<0<<"\n";
return 0;
}
// cerr<<((((fact[n] * inv_fact[r]) % MOD) * inv_fact[n - r]) % MOD)<<"\n";
return ((((fact[n] * inv_fact[r]) % MOD) * inv_fact[n - r]) % MOD);
}
long long int f(int n, int m, int diff)
{
long long int ans = 0;
for (int div_col = 1; div_col < m; ++div_col)
{
for (int i = 0; i < MAXN; ++i)
{
postsum[i] = 0;
}
for (int i = n - 1; i >= 0; --i)
{
long long int temp = comb(m - (div_col + 1) + i, i) * comb(m - div_col + n - (i + 1), n - (i + 1));
postsum[i] = (postsum[i + 1] + temp) % MOD;
}
for (int i = 0; i < n; ++i)
{
long long int temp = comb(i + div_col, div_col) * comb(n - (i + 1) + div_col - 1, div_col - 1);
temp %= MOD;
ans = (ans + temp * postsum[i + 1 + diff]) % MOD;
}
}
return ans;
}
int main()
{
precompute();
int n, m;
cin>>n>>m;
long long int ans = f(n, m, 0) + f(m, n, 1);
cout<<(ans * 2) % MOD<<"\n";
return 0;
} | 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2007;
const double EPS = 1e-10;
double add(double a, double b) {
if (abs(a + b) < EPS * (abs(a) + abs(b))) return 0;
return a + b;
}
struct P {
double x, y;
P() {}
P(double x_, double y_) : x(x_), y(y_) {}
P operator+(P p) { return P(add(x, p.x), add(y, p.y)); }
P operator-(P p) { return P(add(x, -p.x), add(y, -p.y)); }
P operator*(double d) { return P(x * d, y * d); }
double dot(P p) { return add(x * p.x, y * p.y); }
double det(P p) { return add(x * p.y, -y * p.x); }
} s[MAXN];
bool on_seg(P p1, P p2, P q) {
return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}
double dist(P p1, P p2) {
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lf%lf", &s[i].x, &s[i].y);
}
for (int i = n + 1; i <= 2 * n; i++) {
s[i] = s[i - n];
}
double ans = 1e18;
for (int i = 1; i <= n; i++) {
double tmp = (s[i + 2] - s[i]).det(s[i + 1] - s[i]) / 2;
ans = min(ans, tmp / dist(s[i], s[i + 2]));
}
printf("%.10f\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long int n, k, p, c;
cin >> n >> k >> p;
if (n % 2 == 0) {
if (k > n / 2) {
unsigned long long int u = 2 * k - n;
unsigned long long int r = 2 * n - 2 * k;
for (long i = 1; i <= p; i++) {
cin >> c;
if (c <= r) {
if (c % 2 == 0)
cout << 'X';
else
cout << '.';
} else {
cout << 'X';
}
}
cout << endl;
return 0;
} else {
unsigned long long int h = (k - 1) * 2;
unsigned long long int f = n - h;
for (long i = 1; i <= p; i++) {
cin >> c;
if (c < f) cout << '.';
if (c >= f && c % 2 == 0) cout << 'X';
if (c >= f && c % 2 != 0) cout << '.';
}
cout << endl;
return 0;
}
}
unsigned long long int g = (n + 1) / 2;
if (k <= g) {
if (k == 1) {
for (long i = 1; i <= p; i++) {
cin >> c;
if (c == n)
cout << 'X';
else
cout << '.';
}
cout << endl;
return 0;
}
if (k == 0) {
for (long i = 1; i <= p; i++) {
cin >> c;
cout << '.';
}
cout << endl;
return 0;
}
unsigned long long int u = (k - 2) * 2;
unsigned long long int f = n - u - 1;
for (long i = 1; i <= p; i++) {
cin >> c;
if (c < f) cout << '.';
if (c >= f && c % 2 == 0 && c < n) cout << 'X';
if (c >= f && c % 2 != 0 && c < n) cout << '.';
if (c == n) cout << 'X';
}
cout << endl;
return 0;
}
unsigned long long int u = 2 * k - n;
unsigned long long int h = n - u;
h = h;
for (long i = 1; i <= p; i++) {
cin >> c;
if (c <= h) {
if (c % 2 == 0)
cout << 'X';
else
cout << '.';
} else {
cout << 'X';
}
}
cout << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int main() {
ios_base::sync_with_stdio(false);
int n, a, b, k;
cin >> n >> a >> b >> k;
string s;
cin >> s;
s += '1';
int cnt = 0, all = 0;
for (int i = 0; i <= n; i++) {
if (s[i] == '1') {
all += cnt / b;
cnt = 0;
} else
cnt++;
}
int answer = all - a + 1;
cout << answer << endl;
cnt = 0;
for (int i = 0; i <= n; i++) {
if (s[i] == '1')
cnt = 0;
else {
cnt++;
if (cnt % b == 0 and answer > 0) {
cout << i + 1 << " ";
answer--;
}
}
}
cout << endl;
}
| 2 |
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
map<string, bool>mp;
bool check(string s) {
if (s.empty())return true;
if (*s.begin() != 'm' || *s.rbegin() != 'w')return false;
if (mp.find(s) != mp.end())return mp[s];
for (int i = 1; i < s.size() - 1; i++) {
if (s[i] != 'e')continue;
if (check(s.substr(1, i - 1)) && check(s.substr(i + 1, s.size() - i - 2)))return mp[s] = true;
}
return mp[s] = false;
}
int main() {
string s; cin >> s;
puts(check(s) ? "Cat" : "Rabbit");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool comparator(pair<int, int> &ai, pair<int, int> &bi) {
return ai.second < bi.second;
}
int main() {
string sin, sout;
cin >> sin;
int i, n = sin.length();
vector<int> i_dot;
bool find_at = false;
for (i = 0; i <= n - 3;) {
if (sin.substr(i, 2) == string("at") && i != 0 && i != n - 2 &&
false == find_at) {
sout.append(1, '@');
find_at = true;
i = i + 2;
} else if (sin.substr(i, 3) == string("dot") && i != 0 && i != n - 3) {
sout.append(1, '.');
i = i + 3;
} else {
sout.append(1, sin[i]);
i++;
}
}
sout.append(sin.substr(i, n - i));
cout << sout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int k;
vector<int> ans;
int main() {
cin >> n >> m >> k;
for (int i = 0; i < k; i++) ans.push_back(1);
int r = n;
int qr = k;
while (r < n + m + 1) {
if (qr == k) {
ans.push_back(r);
qr = 1;
r++;
if (r < n + m + 1) {
bool was = false;
while (qr < k) {
was = true;
ans.push_back(r);
qr++;
}
if (!was) r--;
r = r + n - 1;
}
} else {
while (qr < k) {
ans.push_back(r);
qr++;
}
r = r + n - 1;
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
if (i) cout << " ";
cout << ans[i];
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long Maxn = 300010;
long long N, K;
pair<long long, long long> pr[Maxn];
priority_queue<pair<long long, long long> > Q;
long long z[Maxn];
int main() {
scanf("%lld%lld", &N, &K);
for (long long i = 1; i <= N; i++)
scanf("%lld", &pr[i].first), pr[i].second = i;
for (long long i = 1; i <= K; i++) Q.push(pr[i]);
long long ans = 0;
for (long long i = K + 1; i <= K + N; i++) {
if (i <= N) Q.push(pr[i]);
pair<long long, long long> x = Q.top();
ans += (i - x.second) * x.first;
z[x.second] = i;
Q.pop();
}
printf("%lld\n", ans);
for (long long i = 1; i < N; i++) printf("%lld ", z[i]);
printf("%lld\n", z[N]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int i, t, n, rs, nr, dp[205];
string a, b, nume[205];
void Normalizare(string &s) {
int i, n = s.length();
for (i = 0; i < n; ++i)
if (s[i] >= 'A' && s[i] <= 'Z') s[i] += 'a' - 'A';
}
int find(string s) {
for (int i = 1; i <= nr; ++i)
if (nume[i] == s) return i;
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
nume[++nr] = "polycarp";
dp[1] = 1;
for (cin >> t; t; --t) {
cin >> a >> nume[0] >> b;
Normalizare(a);
Normalizare(b);
if (!find(a)) nume[++nr] = a, dp[nr] = 1 + dp[find(b)];
rs = max(rs, dp[nr]);
}
cout << rs << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1, s2, s3, s4;
cin >> s1 >> s2 >> s3;
s4 = s2 + s1;
sort(s3.begin(), s3.end());
sort(s4.begin(), s4.end());
if (s3 == s4)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = (sizeof(long long) == 4 ? 1e9 : 2e18) + 1e5;
constexpr long long MOD = 1e9 + 7;
constexpr long long MAXN = 2e5 + 3;
void solve() {
long long n;
cin >> n;
vector<long long> arr(n + 1);
long long sum = 0;
for (long long i = 1; i <= n; ++i) cin >> arr[i], sum += arr[i];
if (sum % n != 0) {
cout << -1 << '\n';
return;
}
long long m = sum / n;
vector<tuple<long long, long long, long long> > ans;
for (long long i = 2; i <= n; ++i) {
long long v = (i - (arr[i] % i)) % i;
if (v > 0) {
ans.emplace_back(1, i, v);
arr[1] -= v;
arr[i] += v;
}
ans.emplace_back(i, 1, arr[i] / i);
arr[1] += arr[i];
arr[i] -= arr[i];
}
for (long long i = 2; i <= n; ++i) {
assert(arr[i] >= 0);
assert(arr[i] <= m);
if (arr[i] < m) {
ans.emplace_back(1, i, m - arr[i]);
arr[1] -= (m - arr[i]);
arr[i] += (m - arr[i]);
}
}
assert(ans.size() <= 3 * n);
cout << ans.size() << '\n';
for (auto [a, b, c] : ans) {
cout << a << ' ' << b << ' ' << c << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
long long t = 1;
cin >> t;
while (t--) solve();
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
map<pair<long long, long long>, int> F;
int calc(long long a, long long b) {
if (a > b) {
swap(a, b);
}
if (a == 0 || b == 0) {
return 2;
}
if (b % a == 0) {
return 1;
}
if (a <= 2000000000LL) {
long long d = a;
if (a % 2LL == 0LL) {
d *= (a + 1LL);
} else {
d *= (a - 1LL);
}
b -= ((b - a - 1LL) / d) * d;
if (b + 2LL * a >= a + d && a % 2 == 0LL) {
return 1;
}
}
b -= ((b - a - 1) / (2LL * a)) * (2LL * a);
if (F.count(make_pair(a, b))) {
return F[make_pair(a, b)];
}
if (calc(b % a, a) == 2) {
return F[make_pair(a, b)] = 1;
}
if (calc(b - a, a) == 2) {
return F[make_pair(a, b)] = 1;
}
return F[make_pair(a, b)] = 2;
}
map<pair<int, int>, int> M;
int triv(int a, int b) {
if (a > b) {
swap(a, b);
}
if (a == 0 || b == 0) {
return 2;
}
if (b % a == 0) {
return 1;
}
if (M.count(make_pair(a, b))) {
return M[make_pair(a, b)];
}
if (triv(a, b % a) == 2) {
return M[make_pair(a, b)] = 1;
}
long long value = a;
while ((long long)(b) >= value) {
if (triv(b - value, a) == 2) {
return M[make_pair(a, b)] = 1;
}
value *= a;
}
return M[make_pair(a, b)] = 2;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
long long a, b;
cin >> a >> b;
int res = calc(a, b);
if (res == 1) {
printf("First\n");
} else {
printf("Second\n");
}
F.clear();
}
return 0;
}
| 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.