solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};
constexpr int inf = 1e9 + 10;
vector<string> g;
vector<vector<int>> d;
int n, m, k;
bool valid(int x, int y) {
return x >= 0 && y >= 0 && x < n && y < m && g[x][y] == '.';
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> k;
g.resize(n);
d.resize(n, vector<int>(m, inf));
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--, x2--, y2--;
for (string &s : g) {
cin >> s;
}
queue<pair<int, int>> q;
q.emplace(x1, y1);
d[x1][y1] = 0;
while (q.size()) {
int x, y;
tie(x, y) = q.front();
q.pop();
for (int dir = 0; dir < 4; ++dir) {
int tx = x, ty = y;
for (int i = 1; i <= k; ++i) {
tx += dx[dir], ty += dy[dir];
if (valid(tx, ty) && d[tx][ty] == inf) {
d[tx][ty] = d[x][y] + 1;
q.emplace(tx, ty);
} else if (valid(tx, ty) && d[tx][ty] == d[x][y] + 1) {
continue;
} else {
break;
}
}
}
}
int ans = d[x2][y2];
if (ans >= inf)
ans = -1;
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long a, b;
scanf("%lld%lld", &a, &b);
long long ans = min(a, b / 2);
a -= ans; b -= ans * 2;
printf("%lld\n", ans + b / 4);
} | 0 |
#include<cstdio>
#include<algorithm>
#include<vector>
#define N_ 201000
using namespace std;
int A[N_], B[N_], DegA[N_], DegB[N_], Q[N_], head, tail, vis[N_], CA, CB;
int n, Num[N_][2];
vector<int>GA[N_], GB[N_];
#define N_ 300100
#define M_ 2001000
struct MaxFlow {
vector<int>G[N_];
struct Edge {
int b, e, f;
}E[M_];
int EC, flow, PV[N_], Q[N_], Level[N_], n, source, sink, INF = 1e9;
void init(int N, int S, int T) {
source = S, sink = T, flow = EC = 0;
n = N;
for (int i = 0; i <= n; i++)G[i].clear();
}
void Add_Edge(int a, int b, int f) {
G[a].push_back(EC);
G[b].push_back(EC + 1);
E[EC++] = { a,b,f };
E[EC++] = { b,a,0 };
}
int BlockFlow(int a, int f) {
if (a == sink)return f;
for (int &i = PV[a]; i >= 0; i--) {
int t = G[a][i];
if (E[t].f && Level[E[t].e] == Level[a] + 1) {
int ff = BlockFlow(E[t].e, min(E[t].f, f));
if (ff) {
E[t].f -= ff;
E[t ^ 1].f += ff;
return ff;
}
}
}
return 0;
}
bool GetLevel() {
int head = 0, tail = 0, i;
for (i = 1; i <= n; i++)Level[i] = -1;
Q[++tail] = source; Level[source] = 0;
while (head < tail) {
int x = Q[++head];
for (auto &t : G[x]) {
if (E[t].f && Level[E[t].e] == -1) {
Level[E[t].e] = Level[x] + 1;
Q[++tail] = E[t].e;
}
}
}
return Level[sink] != -1;
}
void Dinic() {
int i, t;
flow = 0;
while (GetLevel()) {
for (i = 1; i <= n; i++)PV[i] = G[i].size() - 1;
while (t = BlockFlow(source, INF)) {
flow += t;
}
}
}
}G1;
int main() {
int i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &A[i]);
DegA[A[i]]++;
}
for (i = 0; i < n; i++) {
scanf("%d", &B[i]);
DegB[B[i]]++;
}
for (i = 0; i < n; i++) {
if (!DegA[i])Q[++tail] = i;
}
while (head < tail) {
int x = Q[++head];
DegA[A[x]]--;
if (!DegA[A[x]])Q[++tail] = A[x];
}
for (i = 0; i < n; i++) {
if (DegA[i] && !vis[i]) {
int x = i;
vector<int>T;
while (!vis[x]) {
vis[x] = 1;
T.push_back(x);
x = A[x];
}
if (T.size() == 1)continue;
GA[++CA] = T;
for (auto &t : T)Num[t][0] = CA;
}
}
for (i = 0; i < n; i++)vis[i] = 0;
head = tail = 0;
for (i = 0; i < n; i++) {
if (!DegB[i])Q[++tail] = i;
}
while (head < tail) {
int x = Q[++head];
DegB[B[x]]--;
if (!DegB[B[x]])Q[++tail] = B[x];
}
for (i = 0; i < n; i++) {
if (DegB[i] && !vis[i]) {
int x = i;
vector<int>T;
while (!vis[x]) {
vis[x] = 1;
T.push_back(x);
x = B[x];
}
if (T.size() == 1)continue;
GB[++CB] = T;
for (auto &t : T)Num[t][1] = CB;
}
}
long long res = 0;
G1.init(CA + CB + 2, CA + CB + 1, CA + CB + 2);
for(i=1;i<=CA;i++){
G1.Add_Edge(G1.source, i, (int)GA[i].size());
res += GA[i].size();
}
for (i = 1; i <= CB; i++) {
G1.Add_Edge(CA + i, G1.sink, (int)GB[i].size());
res += GB[i].size();
}
for (i = 0; i < n; i++) {
if (Num[i][0] && Num[i][1]) {
G1.Add_Edge(Num[i][0], CA + Num[i][1], 1);
if (A[i] == B[i]) {
G1.Add_Edge(Num[i][0], CA + Num[i][1], 1);
}
}
}
G1.Dinic();
printf("%lld\n", res - G1.flow);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
string s, t, c;
int main() {
cin >> s >> t;
c = s;
int pos = s.size() - 1;
while (pos >= 0) {
if (s[pos] != 'z') {
c[pos] = s[pos] + 1;
break;
} else
c[pos] = 'a';
pos--;
}
if (c == t)
cout << "No such string";
else
cout << c;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = int(5e5) + 10;
const int K = int(2e6) + 10;
const int MOD = int(1e9) + 7;
const int INF = int(1e9) + 5;
const long long INF64 = 1e18;
int t[N], a[N];
int n;
void update(int x, int d = 1) {
for (; x <= n; x += x & (-x)) {
t[x] += d;
}
return;
}
int query(int k) {
int ret = 0;
for (; k > 0; k -= k & (-k)) {
ret += t[k];
}
return ret;
}
void solve() {
int k, d;
cin >> n >> k >> d;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + 1 + n);
for (int i = k; i <= n; i++) {
int mid = lower_bound(a + 1, a + i, a[i] - d) - a;
if (i - mid + 1 < k) continue;
if (mid == 1)
update(i);
else {
int tmp = query(i - k) - query(mid - 2);
if (tmp) update(i);
}
}
int ans = query(n) - query(n - 1);
if (ans)
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool dbg = 0;
clock_t start_time = clock();
void bad(string mes = "Impossible") {
cout << mes;
exit(0);
}
template <typename T>
string bin(T x, int st = 2) {
string ans = "";
while (x > 0) {
ans += char('0' + x % st);
x /= st;
}
reverse(ans.begin(), ans.end());
return ans.empty() ? "0" : ans;
}
template <typename T>
T input() {
T ans = 0, m = 1;
char c = ' ';
while (!((c >= '0' && c <= '9') || c == '-')) {
c = getchar();
}
if (c == '-') m = -1, c = getchar();
while (c >= '0' && c <= '9') {
ans = ans * 10 + (c - '0'), c = getchar();
}
return ans * m;
}
template <typename T>
void read(T& a) {
a = input<T>();
}
template <typename T>
void read(T& a, T& b) {
read(a), read(b);
}
template <typename T>
void read(T& a, T& b, T& c) {
read(a, b), read(c);
}
template <typename T>
void read(T& a, T& b, T& c, T& d) {
read(a, b), read(c, d);
}
void reads(string& second) {
string ans = "";
char c = '\n';
while (c == '\n' || c == ' ') c = getchar();
while (c != '\n' && c != ' ' && c) ans += c, c = getchar();
second = ans;
}
const int inf = 1e9;
const long double eps = 1e-12;
const int maxn = 1e6 + 10, base = 1e9 + 7;
const long long llinf = 4e18 + 5;
template <typename T>
T binpow(T n, T second) {
if (second <= 0) return 1LL;
if (second % 2 == 0) {
T b = binpow(n, second / 2);
return (1LL * b * b) % base;
} else {
return (1LL * binpow(n, second - 1) * n) % base;
}
}
int main() {
string s1, s2, s3, s4;
cin >> s1 >> s2 >> s3 >> s4;
string a, b;
if (s1[0] != 'X') a.push_back(s1[0]);
if (s1[1] != 'X') a.push_back(s1[1]);
if (s2[1] != 'X') a.push_back(s2[1]);
if (s2[0] != 'X') a.push_back(s2[0]);
if (s3[0] != 'X') b.push_back(s3[0]);
if (s3[1] != 'X') b.push_back(s3[1]);
if (s4[1] != 'X') b.push_back(s4[1]);
if (s4[0] != 'X') b.push_back(s4[0]);
if (a == b) return cout << "YES", 0;
a.push_back(a[0]);
a.erase(a.begin());
if (a == b) return cout << "YES", 0;
a.push_back(a[0]);
a.erase(a.begin());
if (a == b) return cout << "YES", 0;
a.push_back(a[0]);
a.erase(a.begin());
if (a == b) return cout << "YES", 0;
cout << "NO";
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int ab[50000],ac[50000],bc[50000];
bool check(char i,char j){
if(i=='?'||j=='?'||i==j) return 1;
return 0;
}
int main(){
string a,b,c;
cin>>a>>b>>c;
int la=a.size(),lb=b.size(),lc=c.size();
for(int i=0;i<la;i++){
for(int j=0;j<lb;j++){
if(!check(a[i],b[j]))ab[i-j+10000]=1;
}
for(int j=0;j<lc;j++){
if(!check(a[i],c[j])) ac[i-j+10000]=1;
}
}
for(int i=0;i<lb;i++){
for(int j=0;j<lc;j++){
if(!check(b[i],c[j])) bc[i-j+10000]=1;
}
}
int ans=1e9;
for(int i=-4000;i<=4000;i++){
for(int j=-4000;j<=4000;j++){
if(!ab[10000+i]&&!ac[10000+j]&&!bc[j-i+10000]){
int l=min(0,min(i,j));
int r=max(la,max(lb+i,lc+j));
ans=min(r-l,ans);
}
}
}
cout<<ans<<'\n';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool IsFail = false;
vector<int> answer;
vector<int> parent;
vector<int> r;
vector<vector<int>> smej;
vector<bool> usd;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> a(n);
vector<int> b(n);
int max = 0;
int index = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] >= 0)
b[i] = -a[i] - 1;
else
b[i] = a[i];
}
int index_max = 0;
if (n % 2 != 0) {
for (int i = 1; i < n; i++)
if (fabs(b[index_max]) < fabs(b[i])) index_max = i;
b[index_max] = -b[index_max] - 1;
}
for (int i = 0; i < n; i++) cout << b[i] << " ";
}
| 2 |
#include<bits/stdc++.h>
int main() {
int a,b;
scanf("%d%d",&a,&b);
printf("%d",(3*a+b)/2);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 5, MAXM = 1e5 + 5;
const int MAXS = 5e2 + 5;
template <typename _T>
void read(_T &x) {
x = 0;
char s = getchar();
int f = 1;
while (s < '0' || '9' < s) {
f = 1;
if (s == '-') f = -1;
s = getchar();
}
while ('0' <= s && s <= '9') {
x = (x << 3) + (x << 1) + (s - '0'), s = getchar();
}
x *= f;
}
template <typename _T>
void write(_T x) {
if (x < 0) putchar('-'), x = -x;
if (9 < x) write(x / 10);
putchar(x % 10 + '0');
}
template <typename _T>
_T MIN(const _T a, const _T b) {
return a < b ? a : b;
}
struct Edge {
int to, nxt, c;
} Graph[MAXM << 1];
int q[MAXN];
vector<int> G[MAXS];
int head[MAXN], dep[MAXN], cur[MAXN];
int N, M, cnt = 1, tot;
bool con[MAXS][MAXS];
void AddEdge(const int from, const int to, const int C) {
Graph[++cnt].to = to, Graph[cnt].nxt = head[from];
Graph[cnt].c = C, head[from] = cnt;
}
void AddE(const int from, const int to, const int C) {
AddEdge(from, to, C), AddEdge(to, from, 0);
}
bool BFS(const int S, const int T) {
int h = 1, t = 0, u, v;
for (int i = 1; i <= tot; i++) dep[i] = INF;
dep[q[++t] = S] = 0;
while (h <= t) {
u = q[h++];
for (int i = head[u]; i; i = Graph[i].nxt)
if (Graph[i].c && dep[v = Graph[i].to] > dep[u] + 1)
dep[q[++t] = v] = dep[u] + 1;
}
return dep[T] < INF;
}
int DFS(const int u, const int lin, const int T) {
if (u == T) return lin;
int used = 0, ret, v, c;
for (int &i = cur[u]; i; i = Graph[i].nxt) {
v = Graph[i].to, c = Graph[i].c;
if (dep[v] == dep[u] + 1 && c && (ret = DFS(v, MIN(lin - used, c), T))) {
used += ret, Graph[i].c -= ret, Graph[i ^ 1].c += ret;
if (used == lin) break;
}
}
if (used < lin) dep[u] = INF;
return used;
}
int Dinic(const int S, const int T) {
int f = 0;
while (BFS(S, T)) {
for (int i = 1; i <= tot; i++) cur[i] = head[i];
f += DFS(S, INF, T);
}
return f;
}
void Clean() {
cnt = 1, tot = 2 * N;
for (int i = 1; i <= tot + 2; i++) head[i] = cur[i] = dep[i] = 0;
}
int main() {
read(N), read(M);
for (int i = 1, a, b; i <= M; i++)
read(a), read(b), G[a].push_back(b), con[a][b] = true;
int ans = INF;
for (int i = 1; i <= N; i++) {
int have = 0;
for (int j = 1; j <= N; j++) have += con[i][j] + con[j][i] * (i != j);
Clean();
const int s = ++tot, t = ++tot;
for (int a = 1; a <= N; a++)
if (a ^ i) {
AddE(s, a, 1), AddE(a + N, t, 1);
for (int k = 0; k < (int)G[a].size(); k++)
if (G[a][k] ^ i) AddE(a, G[a][k] + N, 1);
}
int flow = Dinic(s, t);
ans = MIN(ans, 2 * N - 1 - have + M - have - flow + N - 1 - flow);
}
write(ans), putchar('\n');
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const short inf = 1 << 14;
const double pi = acos(-1);
const double eps = 1e-9;
const int mod = 1e9 + 7;
const int mf[] = {0, 0, 1, -1}, mc[] = {1, -1, 0, 0};
const int N = 1005;
mt19937 rng(
(unsigned int)chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
static T randint(T lo, T hi) {
return uniform_int_distribution<T>(lo, hi)(rng);
}
template <typename T>
static T randreal(T lo, T hi) {
return uniform_real_distribution<T>(lo, hi)(rng);
}
int main() {
rng.seed(time(0));
vector<long long> a(4);
cin >> a[1] >> a[2] >> a[3];
vector<int> id(4);
iota(id.begin(), id.end(), 0);
sort(id.begin(), id.end(), [&](int i, int j) { return a[i] < a[j]; });
cout << "First" << endl;
long long y = a[id[3]] * 2ll - a[id[2]] - a[id[1]];
cout << y << endl;
int x;
cin >> x;
if (x == 0) return 0;
if (x == id[1]) {
y = a[id[3]] - a[id[2]];
cout << y << endl;
cin >> x;
assert(x == 0);
return 0;
}
if (x == id[2]) {
y = a[id[3]] - a[id[1]];
cout << y << endl;
cin >> x;
assert(x == 0);
return 0;
}
a[id[3]] += y;
y = a[id[3]] * 2ll - a[id[2]] - a[id[1]];
cout << y << endl;
cin >> x;
if (x == 0) return 0;
if (x == id[1]) {
y = a[id[3]] - a[id[2]];
cout << y << endl;
cin >> x;
assert(x == 0);
return 0;
}
if (x == id[2]) {
y = a[id[3]] - a[id[1]];
cout << y << endl;
cin >> x;
assert(x == 0);
return 0;
}
assert(false);
return 0;
}
| 6 |
#include <iostream>
using namespace std;
typedef long long ll;
ll n,m,MOD=1e9+7;
ll f(ll x,ll y){
if(y==1)return x;
if(y%2==0)return f(x,y/2)*f(x,y/2)%MOD;
return x*f(x,y-1)%MOD;
}
int main(void){
cin>>n>>m;
cout<<f(n,m)<<endl;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int n,k,val[100005],l[100005],r[100005];
vector<int> g[100005];
void die()
{
cout<<"No"<<endl;
exit(0);
}
void solve(int v,int p)
{
l[v]=-1e9-1;
r[v]=1e9+1;
if(val[v]!=-1) l[v]=r[v]=val[v];
for(int i=0;i<g[v].size();i++)
{
int to=g[v][i];
if(to==p)
continue;
solve(to,v);
if(l[to]<-1e8)
continue;
if(l[v]<-1e8)
{
l[v]=l[to]-1;
r[v]=r[to]+1;
}
else
{
if(abs(l[v])%2==abs(l[to])%2)
die();
l[v]=max(l[v],l[to]-1);
r[v]=min(r[v],r[to]+1);
}
}
if(l[v]>r[v])
die();
}
void make(int v,int p)
{
for(int i=0;i<g[v].size();i++)
{
int to=g[v][i];
if(to==p)
continue;
if(val[to]==-1)
{
if(l[to]<=val[v]-1&&val[v]-1<=r[to]) val[to]=val[v]-1;
else val[to]=val[v]+1;
}
make(to,v);
}
}
int main()
{
cin>>n;
for(int i=1;i<=n-1;i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
memset(val,-1,sizeof(val));
cin>>k;
for(int i=1;i<=k;i++)
{
int v,p;
cin>>v>>p;
val[v]=p;
}
solve(1,-1);
cout<<"Yes"<<endl;
val[1]=l[1];
make(1,-1);
for(int i=1;i<=n;i++)
cout<<val[i]<<endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
int n,m;
string s;
vector<int> p;
vector<int> ans;
int win(int a,int b){
int d=abs(a-b);
if(s[d-1]=='1') return max(a,b);
else return min(a,b);
}
void solve(int pos,int k,vector<int> v){
int len=(int)v.size();
if(v.size()==1){
ans[pos]=v[0];
return ;
}
vector<int> l,r;
// l
{
rep(i,v.size()/2){
l.push_back(win(v[2*i],v[2*i+1]));
}
}
{
rep(i,v.size()/2){
r.push_back(win(v[(2*i+1)%len],v[(2*i+2)%len]));
}
}
solve(pos,k+1,l);
solve(pos+(1<<k),k+1,r);
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cin>>n;
int m=(1<<n);
cin>>s;
p.resize(m);ans.resize(m);
rep(i,m) cin>>p[i];
solve(0,0,p);
rep(i,m) cout<<ans[i]<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve();
const int MAX_N = 100001;
const int MOD = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const long long LINF = 1ll * 1000000 * 1000000 * 1000000;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
for (int i = 0; i < t; ++i) {
solve();
cout << "\n";
}
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs"
<< "\n";
return 0;
}
void solve() {
int n;
cin >> n;
vector<int> a(n, 0);
long long sum = 0, a_xor = 0;
for (auto &i : a) {
cin >> i;
sum += i;
a_xor ^= i;
}
cout << 2 << "\n";
cout << a_xor << " " << sum + a_xor;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
const int LG = 26;
int _w;
int n, dat[N], discre[N], discre_tot;
namespace BIT {
int tree[N], n;
void init(int _n) {
n = _n;
for (int i = 1; i <= _n + 1; ++i) tree[i] = 0;
}
void add(int i, int v) {
while (i <= n) tree[i] += v, i += (i & (-i));
}
int sum(int i) {
int res = 0;
while (i) res += tree[i], i -= (i & (-i));
return res;
}
} // namespace BIT
int main(void) {
_w = scanf("%d", &n);
for (int i = 1; i <= n; ++i) _w = scanf("%d", dat + i);
int ans = 0;
for (int k = 0, cnt, mask; k < LG; ++k) {
cnt = 0, mask = ((1 << (k + 1)) - 1);
for (int i = 1; i <= n; ++i) discre[i] = (dat[i] & mask);
sort(discre + 1, discre + 1 + n);
discre_tot = unique(discre + 1, discre + 1 + n) - discre - 1;
BIT::init(discre_tot);
for (int i = n, L, R; i; --i) {
L = max(0, ((1 << k) - (dat[i] & mask)));
R = mask - (dat[i] & mask);
L = lower_bound(discre + 1, discre + 1 + discre_tot, L) - discre;
R = upper_bound(discre + 1, discre + 1 + discre_tot, R) - discre - 1;
cnt ^= ((BIT::sum(R) - BIT::sum(L - 1)) & 1);
if ((dat[i] >> k) & 1) {
L = ((1 << (k + 1)) + (1 << k)) - (dat[i] & mask);
R = ((1 << (k + 1)) + mask) - (dat[i] & mask);
L = lower_bound(discre + 1, discre + 1 + discre_tot, L) - discre;
R = upper_bound(discre + 1, discre + 1 + discre_tot, R) - discre - 1;
cnt ^= ((BIT::sum(R) - BIT::sum(L - 1)) & 1);
}
BIT::add(
lower_bound(discre + 1, discre + 1 + discre_tot, (dat[i] & mask)) -
discre,
1);
}
if (cnt & 1) ans |= (1 << k);
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
map<long long, long long> m;
string s;
map<string, long long> x;
string h(long long n) {
string t = "";
while (n) {
if ((n % 10) % 2 == 0)
t += "0";
else
t += "1";
n /= 10;
}
while (t.length() < 20) t += "0";
reverse(t.begin(), t.end());
return t;
}
int main() {
long long int n, i, k;
char c;
string temp;
cin >> n;
while (n--) {
cin >> c;
if (c == '+') {
cin >> k;
m[k]++;
x[h(k)]++;
} else if (c == '-') {
cin >> k;
m[k]--;
x[h(k)]--;
} else {
cin >> s;
temp = "";
if (s.size() < 20) {
for (i = 0; i < 20 - s.size(); i++) temp += "0";
temp += s;
}
cout << x[temp] << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
set<int> ans;
vector<char*> vec;
void getDif(int i) {
int pos = 0;
int dif = strcmp(vec[i] + pos, vec[i + 1] + pos);
while (dif > 0) {
while (vec[i][pos] == vec[i + 1][pos]) pos++;
ans.insert(pos);
pos++;
dif = strcmp(vec[i] + pos, vec[i + 1] + pos);
}
}
int main() {
cin >> n >> m;
for (int i = (0); i < (n); i++) vec.push_back(NULL);
char cad[100];
for (int i = (0); i < (n); i++) {
cin >> cad;
vec[i] = new char[100];
strcpy(vec[i], cad);
}
int res = 0;
int dif = 1;
while (dif != 0) {
dif = 0;
for (int i = 0; i < n - 1; i++) {
if (strcmp(vec[i], vec[i + 1]) > 0) {
getDif(i);
dif++;
}
}
for (int i = 0; i < n; i++) {
string s = "";
for (int j = 0; j < m; j++) {
if (ans.count(j) == 0) s += vec[i][j];
}
strcpy(vec[i], s.c_str());
}
res += ans.size();
ans.clear();
}
cout << res << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define MOD 1000000007
#define MAX_N 200005
ll inv[MAX_N], fact[MAX_N], ifact[MAX_N];
int C(int n, int k){
if(n<k) return 0;
return fact[n]*ifact[k]%MOD*ifact[n-k]%MOD;
}
void prepare(){
inv[1]=fact[0]=fact[1]=ifact[0]=ifact[1]=1;
for(int i=2;i<MAX_N;i++){
inv[i]=inv[MOD%i]*(MOD-MOD/i)%MOD;
fact[i]=fact[i-1]*i%MOD;
ifact[i]=ifact[i-1]*inv[i]%MOD;
}
}
int main(){
prepare();
int h,w,a,b,x;
cin>>h>>w>>a>>b;
w--,h--;
ll ans=C(w+h,w);
for(int i=0;i<b;i++){
ll tmp=C((h-a)+i,i);
ll tmp2=C((w-i)+(a-1), a-1);
ans-=tmp*tmp2%MOD;
if(ans<0) ans+=MOD;
}
cout<<ans<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int M = 1e2 + 5;
const int inf = 2000000000;
const int md = 1e9 + 7;
const long double eps = 0.000000001;
long long n, m, x, i;
char c;
string s;
multiset<long long> st;
map<long long, int> mp;
long long get(long long x) {
int f = 1;
int ans = 0;
while (x > 0) {
ans = ans + ((x % 2) * f);
x /= 10;
f *= 2;
}
return ans;
}
long long str(string s) {
long long f = 1;
long long ans = 0;
reverse(s.begin(), s.end());
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '1') ans += f;
f *= 2;
}
return ans;
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> n;
for (i = 1; i <= n; ++i) {
cin >> c;
if (c == '+') {
cin >> x;
m = get(x);
st.insert(x);
++mp[m];
} else if (c == '-') {
cin >> x;
m = get(x);
st.erase(st.find(x));
--mp[m];
} else {
cin >> s;
x = str(s);
cout << mp[x] << '\n';
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int main() {
ll N, H;
cin >> N >> H;
ll ng = 10000000000, ok = 0;
while (abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if (mid * (mid + 1) / 2 <= N) {
ok = mid;
} else
ng = mid;
}
if (ok <= H) {
if (N - (ok * (ok + 1) / 2) > 0) ok++;
cout << ok << endl;
return 0;
}
ng = 10000000000, ok = 0;
while (abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if (mid * mid - H * (H - 1) / 2 <= N) {
ok = mid;
} else
ng = mid;
}
ll tmp = N - ok * ok + H * (H - 1) / 2;
ll ans = (tmp + ok - 1) / ok;
ans += (ok * 2 - H);
cout << ans << endl;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int fa[N];
int num[N];
int getfa(int x)
{
return fa[x]==x?x:(fa[x]=getfa(fa[x]));
}
int main()
{
int n,m,x,y,fx,fy,res=1;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)fa[i]=i,num[i]=1;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
fx=getfa(x),fy=getfa(y);
if(fx!=fy)
{
num[fx]+=num[fy];
res=max(res,num[fx]);
fa[fy]=fx;
}
}
printf("%d\n",res);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int N, K, L;
int x[30], a[110];
int dist[10010];
int graph[30][30];
int dp[(1 << 20)];
queue<int> q;
void add(int pos, int d) {
if (d < dist[pos]) {
dist[pos] = d;
q.push(pos);
}
}
void bfs(int start) {
int i;
for ((i) = 0; (i) < (int)(N + 1); (i)++) dist[i] = (1 << 29);
add(start, 0);
while (!q.empty()) {
int pos = q.front();
q.pop();
for ((i) = 0; (i) < (int)(L); (i)++) {
if (pos - a[i] >= 0) add(pos - a[i], dist[pos] + 1);
if (pos + a[i] <= N) add(pos + a[i], dist[pos] + 1);
}
}
}
int main(void) {
int mask, i, j;
cin >> N >> K >> L;
for ((i) = 0; (i) < (int)(K); (i)++) {
cin >> x[i];
x[i]--;
}
for ((i) = 0; (i) < (int)(L); (i)++) cin >> a[i];
for ((i) = 0; (i) < (int)(K); (i)++) x[K + i] = x[i] + 1;
K *= 2;
for ((i) = 0; (i) < (int)(K); (i)++) {
bfs(x[i]);
for ((j) = 0; (j) < (int)(K); (j)++) graph[i][j] = dist[x[j]];
}
for ((i) = 0; (i) < (int)((1 << K)); (i)++) dp[i] = (1 << 29);
dp[0] = 0;
for ((mask) = 0; (mask) < (int)((1 << K)); (mask)++)
if (mask != 0) {
for ((i) = 0; (i) < (int)(K); (i)++)
if (mask & (1 << i)) break;
for ((j) = 0; (j) < (int)(K); (j)++)
if (mask & (1 << j))
if (i != j)
dp[mask] =
min(dp[mask], dp[mask ^ (1 << i) ^ (1 << j)] + graph[i][j]);
}
int ans = dp[(1 << K) - 1];
if (ans == (1 << 29))
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3 * 1e5 + 1;
bool dead[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
int mx = 3 * n;
for (int i = 1; i < mx + 1; i++) dead[i] = 0;
vector<int> ind;
vector<int> mat;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
if (!dead[x] and !dead[y]) {
dead[x] = 1;
dead[y] = 1;
mat.push_back(i + 1);
}
}
for (int i = 1; i <= mx; i++) {
if (!dead[i]) {
dead[i] = 1;
ind.push_back(i);
}
}
if (ind.size() >= n) {
cout << "IndSet\n";
for (int i = 0; i < n; i++) {
cout << ind[i] << " ";
}
cout << "\n";
continue;
}
if (mat.size() >= n) {
cout << "Matching\n";
for (int i = 0; i < n; i++) {
cout << mat[i] << " ";
}
cout << "\n";
continue;
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int x1, x2, y1, y2;
long long int k;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 == x2 || y1 == y2)
k = abs(x1 - x2) + abs(y1 - y2);
else
k = 2 + abs(x1 - x2) + abs(y1 - y2);
cout << k << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int N = 4e2+9;
const ll INF = 1LL<<60;
int n;
ll dp[N][N];
ll a[N];
ll sum[N];
ll rec(int l, int r) {
if(dp[l][r]) return dp[l][r];
if(l == r) return dp[l][r] = 0;
ll res = INF;
for(int i = l; i < r; i++)
res = min(res, rec(l, i) + rec(i+1, r) + sum[r] - sum[l-1]);
return dp[l][r] = res;
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i-1] + a[i];
}
cout << rec(1, n) << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a = 0, b;
cin >> b;
b++;
int m;
cin >> m;
for (int i = 0; i < m; i++) {
string s1, s2, s3, s4;
int k;
cin >> s1 >> s2 >> s3 >> s4 >> k;
if (s3[0] == 'l' && k < b) b = k;
if (s3[0] == 'r' && k > a) a = k;
}
if (b - a > 1)
cout << b - a - 1 << endl;
else
cout << -1 << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n >> x;
map<int, long long> s;
vector<int> a(n);
long long counter = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
int to = a[i] ^ x;
if (s.find(to) != s.end()) {
counter += s[to];
}
s[a[i]]++;
}
cout << counter << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int N = 500500;
const int Q = 1 << 21;
const long long mod = 998244353;
using namespace std;
int n;
int m;
int a[N];
int b[N];
vector<int> g[N];
vector<int> v[N];
bool can(int x) {
for (int i = 0; i < N; i++) {
v[i].clear();
}
for (int i = 1; i <= n; i++) {
b[i] = a[i];
int mx = 0;
for (int y : g[i]) {
if (y <= x) {
mx = max(mx, y);
}
}
v[mx].push_back(i);
}
int money = 0;
for (int i = 1; i <= x; i++) {
money++;
for (int y : v[i]) {
int z = min(money, b[y]);
money -= z;
b[y] -= z;
}
}
for (int i = 1; i <= n; i++) {
money -= b[i] * 2;
}
return money >= 0;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[y].push_back(x);
}
int l = 1, r = 4e5 + 100;
while (l < r) {
int mid = (l + r) / 2;
if (can(mid))
r = mid;
else
l = mid + 1;
}
cout << l;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
char a[maxn];
int wa[maxn], wb[maxn], wv[maxn], wts[maxn];
int cmp(int *r, int a, int b, int l) {
return r[a] == r[b] && r[a + l] == r[b + l];
}
void init_sa(char *r, int *sa, int n, int m) {
int i, j, *x = wa, *y = wb, *t;
int p;
for (i = 0; i < m; i++) wts[i] = 0;
for (i = 0; i < n; i++) wts[x[i] = r[i]]++;
for (i = 0; i < m; i++) wts[i] += wts[i - 1];
for (i = n - 1; i >= 0; --i) sa[--wts[x[i]]] = i;
for (j = 1, p = 1; p < n; j <<= 1, m = p) {
for (p = 0, i = n - j; i < n; i++) y[p++] = i;
for (i = 0; i < n; i++)
if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = 0; i < n; i++) wv[i] = x[y[i]];
for (i = 0; i < m; i++) wts[i] = 0;
for (i = 0; i < n; i++) wts[wv[i]]++;
for (i = 1; i < m; i++) wts[i] += wts[i - 1];
for (i = n - 1; i >= 0; i--) sa[--wts[wv[i]]] = y[i];
for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
}
return;
}
int rnk[maxn], height[maxn];
int d[maxn][20], inx[maxn];
void init_inx() {
inx[0] = -1;
for (int i = 1; i < maxn; i++)
inx[i] = (((i) & (i - 1)) ? 0 : 1) + inx[i - 1];
}
void init_rmq(int *r, int n) {
for (int i = 0; i < n; i++) d[i][0] = height[i];
for (int j = 1; j < 20; ++j) {
int k = 1 << (j - 1);
for (int i = 0; i + k * 2 - 1 < n; ++i)
d[i][j] = min(d[i][j - 1], d[i + k][j - 1]);
}
}
int query(int l, int r) {
int k = inx[r - l + 1];
return min(d[l][k], d[r - (1 << k) + 1][k]);
}
void calheight(char *r, int *sa, int n) {
int i, j, k = 0;
for (i = 1; i < n; i++) rnk[sa[i]] = i;
for (i = 0; i < n - 1; height[rnk[i++]] = k)
for (k ? k-- : 0, j = sa[rnk[i] - 1]; r[i + k] == r[j + k]; k++)
;
}
int sa[maxn];
int stk[maxn], tot = 0;
int lp[maxn], rp[maxn];
void solve() {
scanf("%s", a);
int len = strlen(a);
a[len] = '$';
len++;
init_sa(a, sa, len, 256);
calheight(a, sa, len);
tot = 0;
height[0] = height[len] = 0;
for (int i = 1; i < len; i++) {
while (tot > 0 && height[stk[tot - 1]] >= height[i]) tot--;
lp[i] = tot == 0 ? 0 : stk[tot - 1];
stk[tot++] = i;
}
tot = 0;
for (int i = len - 1; i >= 1; i--) {
while (tot > 0 && height[stk[tot - 1]] >= height[i]) tot--;
rp[i] = tot == 0 ? len : stk[tot - 1];
stk[tot++] = i;
}
long long ans = 0;
priority_queue<pair<int, int>> que;
for (int i = 1; i < len; i++) que.push({height[i], i});
int prenum = -1, prehi = -1;
for (int i = 1; i < len; i++)
ans += 1ll * (len - 1 - sa[i]) - max(height[i], height[i + 1]);
while (!que.empty()) {
pair<int, int> u = que.top();
que.pop();
int hei = u.first, st = u.second;
if (hei == prehi && lp[st] == lp[prenum]) continue;
ans += 1ll * (rp[st] - lp[st] + 1) * (rp[st] - lp[st]) / 2 *
(height[st] - max(height[lp[st]], height[rp[st]]));
prehi = hei, prenum = st;
}
printf("%lld\n", ans);
}
int main(int argc, char const *argv[]) {
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 2e5 + 17;
char s[Nmax], t[Nmax];
bool v[Nmax] = {0};
int a[256] = {0};
int main(void) {
scanf("%s%s", &s, &t);
for (int i = 0; t[i]; i++) a[t[i]]++;
int x = 0, y = 0;
for (int i = 0; s[i]; i++)
if (a[s[i]]) {
a[s[i]]--;
v[i] = 1;
x++;
}
for (int i = 0; s[i]; i++)
if (v[i] == 0) {
if (s[i] >= 'A' && s[i] <= 'Z') {
if (a[s[i] - 'A' + 'a']) {
a[s[i] - 'A' + 'a']--;
y++;
}
} else {
if (a[s[i] - 'a' + 'A']) {
a[s[i] - 'a' + 'A']--;
y++;
}
}
}
cout << x << " " << y;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int speed, ram, hdd, pri, id;
} COM;
COM lap[102], lap2[102];
int dump[102];
bool cmp(COM a, COM b) { return a.pri < b.pri; }
int main(void) {
int n, i, j, cnt, flag, ansc;
while (scanf("%d", &n) == 1) {
for (i = 0; i < n; ++i) {
scanf("%d%d%d%d", &lap[i].speed, &lap[i].ram, &lap[i].hdd, &lap[i].pri);
lap[i].id = i + 1;
dump[i] = 0;
}
for (i = cnt = 0; i < n; ++i) {
for (j = flag = 0; j < n; ++j) {
if (i != j) {
if (lap[i].speed < lap[j].speed && lap[i].ram < lap[j].ram &&
lap[i].hdd < lap[j].hdd) {
flag = 1;
break;
}
}
}
if (flag) dump[i] = 1;
}
for (i = ansc = 0; i < n; ++i) {
if (!dump[i]) lap2[ansc++] = lap[i];
}
sort(lap2, lap2 + ansc, cmp);
printf("%d\n", lap2[0].id);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
struct data{
int t;
ll p,f;
data(){}
data(int tt,ll pp,ll ff){
t=tt;
p=pp;
f=ff;
}
bool operator<(const data &d)const{
if(f==d.f)return p<d.p;
return f<d.f;
}
};
struct CHTrick{
deque<P> lines;
bool isMonoticX=true;
CHTrick(bool flag=true){
isMonoticX=flag;
}
bool check(P l1,P l2,P l3){
return (l3.second-l2.second)*(l2.first-l1.first)>=(l2.second-l1.second)*(l3.first-l2.first);
}
void add(ll a,ll b){
P line(a,b);
while(lines.size()>=2 && check(*(lines.end()-2),lines.back(),line)){
lines.pop_back();
}
lines.push_back(line);
}
ll f(int i,ll x){
return lines[i].first*x+lines[i].second;
}
ll f(P line,ll x){
return line.first*x+line.second;
}
bool comp(ll lv,ll rv){
return lv<=rv;
}
ll get(ll x){
if(isMonoticX){
while(lines.size()>=2 && comp(f(0,x),f(1,x))){
lines.pop_front();
}
return f(0,x);
}else{
int low=-1,high=lines.size()-1;
while(high-low>1){
int mid=(high+low)/2;
if(comp(f(mid,x),f(mid+1,x)))low=mid;
else high=mid;
}
return f(high,x);
}
}
};
int n,T;
data d[4005];
CHTrick dp[4005];
int main(void){
scanf("%d%d",&n,&T);
for(int i=0;i<n;i++){
scanf("%d%lld%lld",&d[i].t,&d[i].p,&d[i].f);
}
sort(d,d+n);
dp[0].add(0,0);
ll ans=0;
for(int i=0;i<n;i++){
for(int j=T-d[i].t;j>=0;j--){
if(dp[j].lines.size()==0)continue;
ll v=dp[j].get(d[i].f)+d[i].p-d[i].f*d[i].f;
if(j==0){
v+=d[i].f*d[i].f;
}
ans=max(v,ans);
dp[j+d[i].t].add(2LL*d[i].f,v-d[i].f*d[i].f);
}
}
printf("%lld\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long INF = 1e18;
int ptrl = 1;
int ptrr = 0;
long long curval = 0;
int a[100011];
int cnt[100011];
long long dp[2][100011];
void go(int l, int r) {
while (ptrl > l) {
ptrl--;
curval += cnt[a[ptrl]];
cnt[a[ptrl]]++;
}
while (ptrr < r) {
ptrr++;
curval += cnt[a[ptrr]];
cnt[a[ptrr]]++;
}
while (ptrl < l) {
cnt[a[ptrl]]--;
curval -= cnt[a[ptrl]];
ptrl++;
}
while (ptrr > r) {
cnt[a[ptrr]]--;
curval -= cnt[a[ptrr]];
ptrr--;
}
}
void compute(int k, int l, int r, int opl, int opr) {
if (l > r) return;
int mid = (l + r) / 2;
dp[k][mid] = INF;
int op;
for (int i = opl; i <= min(opr, mid); i++) {
go(i, mid);
if (dp[k][mid] > dp[k ^ 1][i - 1] + curval) {
dp[k][mid] = dp[k ^ 1][i - 1] + curval;
op = i;
}
}
compute(k, l, mid - 1, opl, op);
compute(k, mid + 1, r, op, opr);
}
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int j = 0; j < n + 1; j++) {
dp[0][j] = INF;
}
dp[0][0] = 0;
for (int ki = 1; ki <= k; ki++) {
compute(ki % 2, 1, n, 1, n);
}
cout << dp[k % 2][n];
}
| 6 |
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n / 10 == 9 || n % 10 == 9 ? "Yes" : "No") << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const double eps = 1e-9;
const int INF = inf;
const double EPS = eps;
struct __timestamper {};
const int PER = 60;
class segm_tree {
struct Data {
int toadd[PER];
Data() { fill(toadd, toadd + PER, 0); }
Data(int x) {
assert(2 <= x && PER % x == 0);
fill(toadd, toadd + PER, 1);
for (int i = 0; i < PER; i += x) toadd[i]++;
}
int &operator[](int x) { return toadd[x]; }
int operator[](int x) const { return toadd[x]; }
friend Data operator+(const Data &a, const Data &b) {
Data res;
for (int st = 0; st < PER; st++) {
res[st] = a[st] + b[(st + a[st]) % PER];
}
return res;
}
};
int off;
vector<Data> tr;
public:
segm_tree(vector<int> &as) {
off = 1;
while (off < ((int)(as).size())) off <<= 1;
tr = vector<Data>(2 * off);
for (int i = 0; i < (((int)(as).size())); i++) tr[off + i] = as[i];
for (int i = off - 1; i >= 1; i--) tr[i] = tr[2 * i] + tr[2 * i + 1];
}
void set(int x, int v) {
tr[x += off] = v;
for (x >>= 1; x >= 1; x >>= 1) tr[x] = tr[2 * x] + tr[2 * x + 1];
}
int get(int l, int r) {
Data resl, resr;
l += off;
r += off;
while (l <= r) {
if (l & 1) resl = resl + tr[l++];
if (!(r & 1)) resr = tr[r--] + resr;
l >>= 1;
r >>= 1;
}
Data res = resl + resr;
return res[0];
}
};
int main() {
int n;
while (scanf("%d", &n) == 1) {
vector<int> as(n);
for (int i = 0; i < (n); i++) scanf("%d", &as[i]);
segm_tree tr(as);
int q;
scanf("%d", &q);
while (q-- > 0) {
char op;
scanf(" %c", &op);
if (op == 'A') {
int l, r;
scanf("%d%d", &l, &r), l--, r -= 2;
printf("%d\n", tr.get(l, r));
} else if (op == 'C') {
int x, v;
scanf("%d%d", &x, &v), x--;
tr.set(x, v);
} else {
assert(false);
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
constexpr double EPS = 1e-14;
struct vec2 {
double x, y;
vec2 operator+(const vec2 rhs) {
return {x + rhs.x, y + rhs.y};
}
vec2 operator-(const vec2 rhs) {
return {x - rhs.x, y - rhs.y};
}
vec2 operator*(const double k) {
return {x * k, y * k};
}
vec2 operator/(const double k) {
return {x / k, y / k};
}
};
void printvec2(vec2 p, int precision) {
cout << setprecision(precision) << fixed << p.x << " " << p.y << endl;
}
double dot(vec2 a, vec2 b) {
return a.x * b.x + a.y * b.y;
};
double norm2(vec2 a) {
return dot(a, a);
}
struct line {
vec2 p, l;
};
line make_line(vec2 a, vec2 b) {
return {a, b - a};
}
double cross(vec2 a, vec2 b) {
return a.x * b.y - a.y * b.x;
}
bool is_parallel(vec2 a, vec2 b) {
return abs(cross(a, b)) < EPS;
}
bool is_parallel(line a, line b) {
return is_parallel(a.l, b.l);
}
double operator/(vec2 a, vec2 b) {
assert(is_parallel(a, b));
return abs(b.x) < EPS ? a.y / b.y : a.x / b.x;
}
vec2 intersection(line l1, line l2) {
assert(!is_parallel(l1, l2));
double a = l1.p.x, b = l1.p.y, c = l1.l.x, d = l1.l.y;
double e = l2.p.x, f = l2.p.y, g = l2.l.x, h = l2.l.y;
double k = (h * (a - e) - g * (b - f)) / (g * d - c * h);
return {
a + k * c,
b + k * d
};
}
vec2 get_normal(vec2 p) {
return {-p.y, p.x};
}
vec2 foot(vec2 p, line l) {
line normal = {p, get_normal(l.l)};
return intersection(normal, l);
}
vec2 reflect(vec2 p, line l) {
vec2 f = foot(p, l);
return {2 * f.x - p.x, 2 * f.y - p.y};
}
double dist2(vec2 p, line l) {
return norm2(foot(p, l) - p);
}
double dist2(vec2 p, vec2 q) {
return norm2(q - p);
}
int sgn(double a) {
if (a < -EPS) return -1;
if (a > EPS) return 1;
return 0;
}
struct segment {
vec2 a, b;
};
segment make_segment(vec2 a, vec2 b) {
return {a, b};
}
double dist2(vec2 p, segment s) {
vec2 l = s.a, r = s.b;
for (int i = 0; i < 100; i++) {
vec2 ml = (l + r) / 3;
vec2 mr = (l + r) * 2 / 3;
if (dist2(p, ml) > dist2(p, mr)) {
l = ml;
} else {
r = mr;
}
}
return dist2(p, l);
}
template<class T>
struct ext {
double val;
T x, y;
};
template<class T>
ext<T> ternary_2d(function<double(T, T)> f, T xl, T xr, T yl, T yr, bool maximum = true) {
if (!maximum) {
function<double(T, T)> g = [&](T a, T b){return -f(a, b);};
ext<T> ret = ternary_2d(g, xl, xr, yl, yr, true);
return {-ret.val, ret.x, ret.y};
}
// fix y
function<ext<T>(T, T, T)> fx = [&](T y, T l, T r) {
for (int i = 0; i < 100; i++) {
T ml = (l * 2 + r) / 3.0;
T mr = (l + r * 2) / 3.0;
if (f(ml, y) < f(mr, y)) {
l = ml;
} else {
r = mr;
}
}
ext<T> ret;
ret.x = l;
ret.y = y;
ret.val = f(l, y);
return ret;
};
for (int i = 0; i < 100; i++) {
T ml = (yl * 2 + yr) / 3.0;
T mr = (yl + yr * 2) / 3.0;
if (f(fx(ml, xl, xr).x, ml) < f(fx(mr, xl, xr).x, mr)) {
yl = ml;
} else {
yr = mr;
}
}
ext<T> ret;
ret.x = fx(yl, xl, xr).x;
ret.y = yl;
ret.val = f(ret.x, ret.y);
return ret;
}
double dist2(segment s, segment t) {
ext<vec2> ans;
function<double(vec2, vec2)> d = [&](vec2 a, vec2 b) {
return norm2(b - a);
};
ans = ternary_2d(d, s.a, s.b, t.a, t.b, false);
return ans.val;
}
vec2 intersection(segment s, segment t) {
ext<vec2> ans;
function<double(vec2, vec2)> d = [&](vec2 a, vec2 b) {
return norm2(b - a);
};
ans = ternary_2d(d, s.a, s.b, t.a, t.b, false);
assert(ans.val < EPS);
return {ans.x.x, ans.x.y};
}
struct triangle {
// counter clockwise
vec2 a, b, c;
};
double area(triangle t) {
return cross(t.b - t.a, t.c - t.a) / 2;
}
// counter clockwise
using polygon = vector<vec2>;
double area(polygon p) {
double ret = 0;
assert(p.size() >= 3);
for (int i = 1; i < p.size() - 1; i++) {
triangle t = {p.front(), p[i], p[i + 1]};
ret += area(t);
}
return ret;
}
int ccw(vec2 a, vec2 b, vec2 c) {
b = b - a;
c = c - a;
int s = sgn(cross(b, c));
if (s == -1) {
// CLOCKWISE
return -1;
} else if (s == 1) {
// COUNTER_CLOCKWISE
return 1;
} else {
double k = c / b;
if (k < -EPS) {
// ONLINE_BACK
return -2;
} else if (k > 1 + EPS) {
// ONLINE_FRONT
return 2;
} else {
// ON_SEGMENT
return 0;
}
}
}
bool is_convex(polygon p, bool strict = true) {
int n = p.size();
assert(p.size() >= 3);
p.push_back(p[0]);
p.push_back(p[1]);
for (int i = 0; i < n; i++) {
if (strict) {
if (ccw(p[i], p[i + 1], p[i + 2]) != 1) {
return false;
}
} else {
if (ccw(p[i], p[i + 1], p[i + 2]) == -1) {
return false;
}
}
}
return true;
}
int main() {
int n;
cin >> n;
polygon p;
for (int i = 0; i < n; i++) {
double x, y;
cin >> x >> y;
p.push_back({x, y});
}
cout << (is_convex(p, false) ? 1 : 0) << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, h;
while (cin >> n >> m >> h) {
vector<int> a, b;
vector<vector<int> > d;
a.resize(m);
b.resize(n);
d.resize(n);
for (int i = 0; i < m; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
d[i].resize(m);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> d[i][j];
if (d[i][j]) {
d[i][j] = min(a[j], b[i]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << d[i][j] << ' ';
}
cout << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int l, r, k;
cin >> l >> r >> k;
k %= (r - l + 1);
rotate(s.begin() + l - 1, s.begin() + r - k, s.begin() + r);
}
cout << s << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int N = 1e5;
void outp() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
}
void solve() {
long long n;
cin >> n;
if (n > 0) {
cout << n;
} else {
long long m = n / 100;
n = -1 * (n % 100);
if (n / 10 > n % 10) {
n = -1 * (n % 10);
} else {
n = -1 * (n / 10);
}
cout << m * 10 + n;
}
}
int main() {
outp();
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 74;
struct P {
int x, y, z;
} pre[N][N][N], p;
int n, d[N][N][N];
char t[N][N];
vector<int> g[N][144];
int main() {
scanf("%d %d %d %d", &n, &p.x, &p.y, &p.z);
memset(d, -1, sizeof(d));
d[p.x][p.y][p.z] = 0;
queue<P> q;
q.push(p);
for (int i = 1; i <= n; i++) {
scanf("%s", t[i] + 1);
for (int j = 1; j <= n; j++) {
if (i == j) continue;
g[i][t[i][j]].push_back(j);
}
}
P z = {-1, -1, -1};
while (!q.empty()) {
p = q.front();
q.pop();
int w = d[p.x][p.y][p.z];
if (p.x + p.y + p.z == 6) {
z = p;
break;
}
for (int v : g[p.x][t[p.y][p.z]]) {
if (v == p.y || v == p.z || d[v][p.y][p.z] != -1) continue;
d[v][p.y][p.z] = w + 1;
pre[v][p.y][p.z] = p;
q.push(P{v, p.y, p.z});
}
for (int v : g[p.y][t[p.x][p.z]]) {
if (v == p.x || v == p.z || d[p.x][v][p.z] != -1) continue;
d[p.x][v][p.z] = w + 1;
pre[p.x][v][p.z] = p;
q.push(P{p.x, v, p.z});
}
for (int v : g[p.z][t[p.y][p.x]]) {
if (v == p.y || v == p.x || d[p.x][p.y][v] != -1) continue;
d[p.x][p.y][v] = w + 1;
pre[p.x][p.y][v] = p;
q.push(P{p.x, p.y, v});
}
}
int ans = -1;
vector<pair<int, int> > v;
if (z.x != -1) {
ans = d[z.x][z.y][z.z];
for (int i = z.x, j = z.y, k = z.z;;) {
P pr = pre[i][j][k];
if (pr.x == 0) break;
if (i != pr.x)
v.push_back(pair<int, int>(pr.x, i));
else if (j != pr.y)
v.push_back(pair<int, int>(pr.y, j));
else
v.push_back(pair<int, int>(pr.z, k));
i = pr.x;
j = pr.y;
k = pr.z;
}
}
printf("%d\n", ans);
reverse(v.begin(), v.end());
for (pair<int, int> pa : v) printf("%d %d\n", pa.first, pa.second);
}
| 4 |
#include<stdio.h>
int main(){
int x,y;
scanf("%d%d",&x,&y);
printf("%d",x+y/2);
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<max((a+b),max((a-b),(a*b)));
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long m = 1000000007) {
if (b == 0) return 1;
if (b == 1) return a;
long long res = power(a, b / 2, m);
res = (res * res) % m;
if (b & 1) res = (res * a) % m;
return res;
}
long long modinv(long long a, long long m = 1000000007) {
return power(a, m - 2, m);
}
long long add(long long a, long long b, long long m = 1000000007) {
long long c = (a % m + b % m);
if (c >= m) c -= m;
return c;
}
long long sub(long long a, long long b, long long m = 1000000007) {
long long c = (a % m - b % m);
if (c < 0) c += m;
return c;
}
long long mul(long long a, long long b, long long m = 1000000007) {
return (a * b) % m;
}
void solve() {
long long n, a, b;
cin >> n >> a >> b;
long long ar[n + 1], i, x, y;
map<long long, long long> mp;
for (i = 1; i <= n; i++) {
cin >> ar[i];
mp[ar[i]] = 1;
}
vector<long long> v;
for (i = 1; i <= n; i++) {
x = a - ar[i];
if (mp.find(x) == mp.end()) {
v.push_back(ar[i]);
mp[ar[i]] = 2;
}
}
for (i = 0; i < v.size(); i++) {
x = b - v[i];
if (mp.find(x) == mp.end()) {
cout << "NO"
<< "\n";
return;
}
if (mp[x] == 2) continue;
mp[x] = 2;
y = a - x;
v.push_back(y);
}
cout << "YES"
<< "\n";
for (i = 1; i <= n; i++) cout << mp[ar[i]] - 1 << " ";
cout << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
for (long long i = 1; i <= t; i++) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, current, last = 0, rest = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> current;
if ((current == last) || (current == 0)) {
rest++;
current = 0;
}
if (current == 3) {
if (last == 1) {
last = 2;
} else if (last == 2) {
last = 1;
} else {
last = 0;
}
} else {
last = current;
}
}
cout << rest;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> A[4], B[4];
long long int Kierunek(const pair<int, int>& a, const pair<int, int>& b,
const pair<int, int>& c) {
pair<int, int> p1 = make_pair(b.first - a.first, b.second - a.second);
pair<int, int> p2 = make_pair(c.first - a.first, c.second - a.second);
return p1.first * p2.second - p1.second * p2.first;
}
bool NaOdcinku(const pair<int, int>& a, const pair<int, int>& b,
const pair<int, int>& c) {
if (Kierunek(a, b, c) != 0) return false;
if (c.first < min(a.first, b.first)) return false;
if (c.second < min(a.second, b.second)) return false;
if (c.first > max(a.first, b.first)) return false;
if (c.second > max(a.second, b.second)) return false;
return true;
}
bool Przeciecie(const pair<int, int>& a, const pair<int, int>& b,
const pair<int, int>& c, const pair<int, int>& d) {
if (NaOdcinku(a, b, c)) return true;
if (NaOdcinku(a, b, d)) return true;
if (NaOdcinku(c, d, a)) return true;
if (NaOdcinku(c, d, b)) return true;
int znak1 = Kierunek(a, b, c) > 0 ? 1 : -1;
int znak2 = Kierunek(a, b, d) > 0 ? 1 : -1;
if (znak1 * znak2 > 0) return false;
int znak3 = Kierunek(c, d, a) > 0 ? 1 : -1;
int znak4 = Kierunek(c, d, b) > 0 ? 1 : -1;
if (znak3 * znak4 > 0) return false;
return true;
}
bool Pomiedzy(int min_x, int max_x, int x) {
return (x >= min_x) && (x <= max_x);
}
int main() {
for (int i = 0; i < 4; i++) cin >> A[i].first >> A[i].second;
for (int i = 0; i < 4; i++) cin >> B[i].first >> B[i].second;
bool answer = false;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (Przeciecie(A[i], A[(i + 1) % 4], B[j], B[(j + 1) % 4])) answer = true;
int min_x = min(min(A[0].first, A[1].first), min(A[2].first, A[3].first));
int max_x = max(max(A[0].first, A[1].first), max(A[2].first, A[3].first));
int min_y = min(min(A[0].second, A[1].second), min(A[2].second, A[3].second));
int max_y = max(max(A[0].second, A[1].second), max(A[2].second, A[3].second));
for (int i = 0; i < 4; i++)
if ((Pomiedzy(min_x, max_x, B[i].first)) &&
(Pomiedzy(min_y, max_y, B[i].second)))
answer = true;
for (int i = 0; i < 4; i++) {
bool was_neg_det = false;
bool was_pos_det = false;
for (int j = 0; j < 4; j++) {
int det = Kierunek(B[j], B[(j + 1) % 4], A[i]);
if (det < 0) was_neg_det = true;
if (det > 0) was_pos_det = true;
}
if (was_neg_det && (!was_pos_det)) answer = true;
if (was_pos_det && (!was_neg_det)) answer = true;
}
if (answer)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
queue<int>q;
map<int,int>::iterator mt;
void init(){
mt=mp.end();
mt--;
}
bool erase(int u,int v){
if(!mp[u])return 0;
q.push(u);
mp[u]--;
if(!mp[v])return 0;
q.push(v);
mp[v]--;
while((*mt).second==0 and mt!=mp.begin()){
// cerr<<(*mt).first<<endl;
mt--;
}
return 1;
}
void add(){
while(!q.empty()){
mp[q.front()]++;
q.pop();
}
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[2*n+5],m=0;
for(int i=1;i<=2*n;i++){
cin>>a[i];
mp[a[i]]++;
}
bool ok=0;
for(auto it:mp){
init();
int x=(*mt).first,t=1;
if(!erase(it.first,(*mt).first)){
add(); continue;}
// cerr<<x+it.first<<endl;
for(int i=1;i<n;i++){
// cerr<<"x"<<x<<endl;
int p=x;
x=(*mt).first;
if(!erase((*mt).first,p-(*mt).first)){
t=0;
break;
}
}
if(t){
ok=1;
break;
}
add();
}
if(ok){
cout<<"YES"<<endl;
int u=q.front();
q.pop();
cout<<u+q.front()<<endl;
cout<<u<<' '<<q.front()<<endl;
q.pop();
while(!q.empty()){
u=q.front();
q.pop();
cout<<u<<' '<<q.front()<<endl;
q.pop();
}
}else cout<<"NO"<<endl;
mp.clear();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int fa = s[0] == 'a';
int n = s.size();
cout << "0"
<< " ";
for (int i = 1; i < n; i++) {
if (fa && s[i] == 'b' && i < n - 1 && s[i + 1] == 'a') {
fa = !fa;
cout << "1"
<< " ";
} else if (!fa && s[i] == 'a' && (i == n - 1 || s[i + 1] == 'b')) {
cout << "1"
<< " ";
fa = !fa;
} else {
cout << "0"
<< " ";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
scanf("%lld", &t);
while (t--) {
long long x, y;
scanf("%lld %lld", &x, &y);
if (x == y)
cout << "YES\n";
else if (x % 2 != 0) {
if (x <= 3) {
if (y > x)
cout << "NO\n";
else
cout << "YES\n";
} else
cout << "YES\n";
} else if (x % 2 == 0) {
if (x == 2) {
if (y > x && y != 3)
cout << "NO\n";
else
cout << "YES\n";
} else
cout << "YES\n";
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin >> a;
int i = 1;
bool open = 0;
while (i <= 5) {
cin >> b;
if (b[0] == a[0] or b[1] == a[1]) {
open = 1;
break;
}
i++;
}
if (open == 1)
cout << "Yes";
else
cout << "No";
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T>
inline void scan_d(T &ret) {
char c;
int flag = 0;
ret = 0;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
if (c == '-') {
flag = 1;
c = getchar();
}
while (c >= '0' && c <= '9') ret = ret * 10 + (c - '0'), c = getchar();
if (flag) ret = -ret;
}
const int maxn = 200000 + 10;
int st[maxn][2], a[maxn];
struct node {
int val, ran;
bool operator<(const node &t) const { return val > t.val; }
} no[maxn];
int l[maxn], r[maxn];
int ans[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
no[i].val = a[i];
}
int top = 0;
st[0][0] = st[0][1] = 0;
for (int i = 1; i <= n; i++) {
while (a[i] <= st[top][0]) top--;
l[i] = st[top][1] + 1;
st[++top][0] = a[i];
st[top][1] = i;
}
top = 0;
st[0][0] = 0;
st[0][1] = n + 1;
for (int i = n; i >= 1; i--) {
while (a[i] <= st[top][0]) top--;
r[i] = st[top][1] - 1;
st[++top][0] = a[i];
st[top][1] = i;
}
for (int i = 1; i <= n; i++) {
no[i].ran = r[i] - l[i] + 1;
}
sort(no + 1, no + 1 + n);
int p = 1;
for (int i = 1; i <= n; i++) {
if (no[i].ran < p) continue;
while (p <= no[i].ran) ans[p++] = no[i].val;
}
for (int i = 1; i <= n; i++) printf("%d%c", ans[i], (i == n) ? '\n' : ' ');
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T parse(const string &s) {
T x;
stringstream ss(s);
ss >> x;
return x;
}
const double EPS = 1e-12;
const int INF = 1000 * 1000 * 1000;
const long long LINF = INF * 1ll * INF;
const double DINF = 1e200;
const double PI = 3.1415926535897932384626433832795l;
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long gcdex(long long a, long long b, long long &x, long long &y) {
if (!a) {
x = 0, y = 1;
return b;
}
long long k = b / a;
long long g = gcdex(b - k * a, a, y, x);
x -= k * y;
return g;
}
long long inv(long long a, long long m) {
assert(m > 1);
long long x, y, g;
g = gcdex(a, m, x, y);
return (x % (m / g) + m / g) % m / g;
}
long long crt(long long a1, long long m1, long long a2, long long m2) {
long long a = (a2 - a1 % m2 + m2) % m2;
long long x, y, g;
g = gcdex(m1, m2, x, y);
if (a % g) return -1;
long long m = m1 / g * m2;
assert(x + m2 >= 0);
x = a / g * (x + m2) % m2;
long long r = (a1 + x * m1) % m;
assert(r % m1 == a1 && r % m2 == a2);
return r;
}
long long powmod(long long a, long long p, long long m) {
assert(p >= 0);
long long r = 1;
while (p) {
if (p & 1) r = r * a % m;
p >>= 1;
a = a * a % m;
}
return r;
}
bool isprime(long long a) {
if (a <= 1) return false;
for (long long i = 2; i * i <= a; ++i) {
if (a % i == 0) return false;
}
return true;
}
long long sqrtup(long long a) {
if (!a) return 0;
long long x = max(0ll, (long long)sqrt((double)a));
while (x * x >= a) --x;
while ((x + 1) * (x + 1) < a) ++x;
return x + 1;
}
long long isqrt(long long a) {
if (a <= 0) {
assert(!a);
return 0;
}
long long x = (long long)sqrt((double)a);
while (sqr(x + 1) <= a) ++x;
while (x * x > a) --x;
return x;
}
long long sgn(long long x) { return x < 0 ? -1 : x > 0 ? 1 : 0; }
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v);
template <class A, class B>
ostream &operator<<(ostream &s, const pair<A, B> &p);
template <class K, class V>
ostream &operator<<(ostream &s, const map<K, V> &m);
template <class T>
ostream &operator<<(ostream &s, const set<T> &m);
template <class T, size_t N>
ostream &operator<<(ostream &s, const array<T, N> &a);
template <class... T>
ostream &operator<<(ostream &s, const tuple<T...> &t);
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v) {
s << '[';
for (int i = 0; i < (((int)(v).size())); ++i) {
if (i) s << ',';
s << v[i];
}
s << ']';
return s;
}
template <class A, class B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
s << "(" << p.first << "," << p.second << ")";
return s;
}
template <class K, class V>
ostream &operator<<(ostream &s, const map<K, V> &m) {
s << "{";
bool f = false;
for (const auto &it : m) {
if (f) s << ",";
f = true;
s << it.first << ": " << it.second;
}
s << "}";
return s;
}
template <class T>
ostream &operator<<(ostream &s, const set<T> &m) {
s << "{";
bool f = false;
for (const auto &it : m) {
if (f) s << ",";
f = true;
s << it;
}
s << "}";
return s;
}
template <class T>
ostream &operator<<(ostream &s, const multiset<T> &m) {
s << "{";
bool f = false;
for (const auto &it : m) {
if (f) s << ",";
f = true;
s << it;
}
s << "}";
return s;
}
template <class T, class V, class C>
ostream &operator<<(ostream &s, const priority_queue<T, V, C> &q) {
auto a = q;
s << "{";
bool f = false;
while (!a.empty()) {
if (f) s << ",";
f = true;
s << a.top();
a.pop();
}
return s << "}";
}
template <class T, size_t N>
ostream &operator<<(ostream &s, const array<T, N> &a) {
s << '[';
for (int i = 0; i < (((int)(a).size())); ++i) {
if (i) s << ',';
s << a[i];
}
s << ']';
return s;
}
template <class T>
ostream &operator<<(ostream &s, const deque<T> &a) {
s << '[';
for (int i = 0; i < (((int)(a).size())); ++i) {
if (i) s << ',';
s << a[i];
}
s << ']';
return s;
}
template <size_t n, class... T>
struct put1 {
static ostream &put(ostream &s, const tuple<T...> &t) {
s << get<sizeof...(T) - n>(t);
if (n > 1) s << ',';
return put1<n - 1, T...>::put(s, t);
}
};
template <class... T>
struct put1<0, T...> {
static ostream &put(ostream &s, const tuple<T...> &t) { return s; }
};
template <class... T>
ostream &operator<<(ostream &s, const tuple<T...> &t) {
s << "(";
put1<sizeof...(T), T...>::put(s, t);
s << ")";
return s;
}
ostream &put3(ostream &s, const char *, bool) { return s; }
template <class U, class... T>
ostream &put3(ostream &s, const char *f, bool fs, U &&u, T &&...t) {
while (*f == ' ') ++f;
if (!fs) s << ", ";
auto nf = f;
int d = 0;
while (*nf && (*nf != ',' || d)) {
if (*nf == '(')
++d;
else if (*nf == ')')
--d;
++nf;
}
auto nf2 = nf;
while (nf2 > f && *(nf2 - 1) == ' ') --nf;
fs = *f == '"';
if (!fs) {
s.write(f, nf2 - f);
s << "=";
};
s << u;
if (fs) s << ' ';
if (*nf) ++nf;
return put3(s, nf, fs, forward<T>(t)...);
}
int main(int argc, char **argv) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.precision(20);
srand(time(0));
int n;
cin >> n;
map<long long, int> R;
vector<long long> A;
for (int qq = 0; qq < (n); ++qq) {
long long a;
cin >> a;
long long s2 = isqrt(a);
if (s2 * s2 == a) {
long long s4 = isqrt(s2);
if (s4 * s4 == s2) {
if (isprime(s4)) {
R[s4] += 4;
continue;
}
} else if (isprime(s2)) {
R[s2] += 2;
continue;
}
} else {
long long s3 = (long long)pow((long double)a, 1 / 3.l);
while (s3 * s3 * s3 < a) ++s3;
while (s3 * s3 * s3 > a) --s3;
if (s3 * s3 * s3 == a && isprime(s3)) {
R[s3] += 3;
continue;
}
}
for (int i = 0; i < (((int)(A).size())); ++i) {
long long g = gcd(a, A[i]);
if (g != 1 && g != A[i]) R[g] += 0;
}
A.push_back(a);
}
for (int i = 0; i < (((int)(A).size())); ++i) {
long long a = A[i];
bool f = false;
for (auto p : R) {
if (a % p.first == 0) {
f = true;
++R[p.first];
++R[a / p.first];
break;
}
}
if (f) {
swap(A[i], A.back());
A.pop_back();
--i;
}
}
long long res = 1;
const long long mod = 998244353;
for (auto p : R) {
(res *= p.second + 1) %= mod;
}
sort((A).begin(), (A).end());
for (int i = 0; i < ((int)(A).size());) {
int ii = i + 1;
while (ii < ((int)(A).size()) && A[ii] == A[i]) ++ii;
(res *= ii - i + 1) %= mod;
(res *= ii - i + 1) %= mod;
i = ii;
}
cout << res;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(int argv, char** argc) {
cin.sync_with_stdio(0);
int n;
cin >> n;
int nums[n];
for (typeof(n) i = 0; i < n; i++) {
cin >> nums[i];
}
for (typeof(n) i = 0; i < n; i++) {
int min_dist = INT_MAX;
if (i > 0) min_dist = nums[i] - nums[i - 1];
if (i < n - 1) min_dist = min(min_dist, nums[i + 1] - nums[i]);
int max_dist = max(nums[i] - nums[0], nums[n - 1] - nums[i]);
printf("%d %d\n", min_dist, max_dist);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long powCount(long long a, long long b) {
long long count = 0;
while (a % b == 0) {
a /= b;
count++;
}
return count;
}
long long power(long long a, long long b) {
long long res = 1;
while (b--) {
res *= a;
}
return res;
}
int main() {
long long n, k;
cin >> n >> k;
long long powTwo = powCount(n, 2);
long long powFive = powCount(n, 5);
long long rem = n / power(2, powTwo) / power(5, powFive);
long long newPowTwo = (powTwo - k > 0) ? powTwo - k : 0;
long long newPowFive = (powFive - k > 0) ? powFive - k : 0;
long long result =
power(2, newPowTwo) * power(5, newPowFive) * power(10, k) * rem;
cout << result;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 10;
const long long mod = 3e18;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m, i, j, k;
cin >> n >> m;
set<int> s;
for (i = 0; i < n; i++) {
cin >> k;
s.insert(k);
}
long long cost = 0;
vector<int> ans;
for (i = 1; i <= 2e5 && cost < m; ++i) {
if (s.find(i) == s.end() && (cost + i) <= m) {
cost += i;
ans.push_back(i);
}
}
cout << ans.size() << endl;
for (auto x : ans) cout << x << " ";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
inline int D() {
int m;
cin >> m;
return m;
}
inline long long lD() {
long long m;
cin >> m;
return m;
}
inline double dD() {
double m;
cin >> m;
return m;
}
int a[100006];
int main() {
fast();
int t = D();
while (t--) {
int n = D();
int ans = 0;
for (int i = 0; i < n; ++i) {
a[i] = D();
if (a[i] == 0) {
ans++;
a[i]++;
}
}
long long sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
}
if (sum == 0)
cout << ans + 1 << endl;
else
cout << ans << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int x, y, a, b;
char A[1005], B[1005];
int main() {
scanf("%s%s", A, B);
a = strlen(A);
b = strlen(B);
for (int i = 0; i < a; ++i)
if (A[i] == '1') ++x;
for (int i = 0; i < b; ++i)
if (B[i] == '1') ++y;
if (x & 1) {
if (y <= x + 1)
puts("YES");
else
puts("NO");
} else if (y <= x)
puts("YES");
else
puts("NO");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5005;
const long long mod = 998244353;
long long n, a[maxn], c[maxn];
long long sum[maxn], dp[maxn][maxn];
long long find(long long x) {
long long l = 1, r = x, res = 0;
while (l <= r) {
long long mid = (l + r) >> 1;
if (2 * a[mid] <= a[x]) {
res = mid;
l = mid + 1;
} else
r = mid - 1;
}
return res;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (long long i = 1; i <= n; ++i) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
c[1] = 0;
for (long long l = 0, r = 2; r <= n;) {
while (2 * a[l + 1] <= a[r]) ++l;
c[r] = l;
++r;
}
for (long long i = 1; i <= n; ++i) {
dp[1][i] = 1;
(sum[i] = sum[i - 1] + dp[1][i]) %= mod;
}
for (long long i = 2; i <= n; ++i) {
for (long long j = 1; j <= n; ++j) {
long long tmp = 0;
(tmp += sum[c[j]]) %= mod;
(tmp += dp[i - 1][j] * (c[j] - i + 2)) %= mod;
(dp[i][j] += tmp) %= mod;
}
for (long long j = 1; j <= n; ++j) {
(sum[j] = sum[j - 1] + dp[i][j]) %= mod;
}
}
long long ans = 0;
for (long long i = 1; i <= n; ++i) ans += dp[n][i];
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int AKA = 1e6 + 6;
int a[AKA];
int SD() {
int x;
scanf("%d", &x);
return x;
}
int main() {
int n = SD(), m = SD(), k = SD(), ans = 0;
for (int i = 1; i <= m; ++i) a[i] = 1e9;
for (int i = 0; i < n; ++i) {
int x = SD(), y = SD();
a[x] = min(a[x], y);
}
for (int i = 1; i <= m; ++i) {
ans += a[i];
}
printf("%d", min(k, ans));
}
| 1 |
#include <cstdio>
#include <algorithm>
using namespace std;
int v[200010], ap[200010];
int cover[200010];
int main ()
{
// freopen ("file.in", "r", stdin);
int n, m;
scanf ("%d %d", &n, &m);
for (int i = 1; i <= n; ++i)
{
scanf ("%d", &v[i]);
++ap[v[i]];
}
for (int i = 1; i <= n; ++i)
for (int j = i - ap[i]; j < i; ++j)
++cover[max (0, j)];
int nr = 0;
for (int i = 0; i < n; ++i)
if (!cover[i]) ++nr;
for (; m; --m)
{
int x, y;
scanf ("%d %d", &x, &y);
int ex = v[x];
v[x] = y;
++ap[y];
if ((++cover[max (0, y - ap[y])]) == 1) --nr;
if ((--cover[max (0, ex - ap[ex])]) == 0) ++nr;
--ap[ex];
printf ("%d\n", nr);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int h,n,a[1001],b[1001],dp[10001],ans=1e9;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>h>>n;
for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
for(int i=1;i<=h;i++) dp[i]=1e9;
for(int i=0;i<=h;i++)
for(int j=1;j<=n;j++){
if(i+a[j]>=h){ ans=min(ans,dp[i]+b[j]); continue; }
dp[i+a[j]]=min(dp[i+a[j]],dp[i]+b[j]);
}
cout<<ans;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long N = 1e5 + 10;
vector<pair<long long, long long> > g[N];
vector<pair<pair<long long, long long>, long long> > edge;
long long dis[N];
void dijkstra(long long src) {
memset(dis, 63, sizeof dis);
set<pair<long long, long long> > st;
st.insert({0, src});
dis[src] = 0;
while (st.size()) {
pair<long long, long long> p = *st.begin();
st.erase(p);
long long v = p.second;
for (auto e : g[v]) {
long long u = e.first, w = e.second;
if (dis[u] > dis[v] + w) {
st.erase({dis[u], u});
dis[u] = dis[v] + w;
st.insert({dis[u], u});
}
}
}
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long n, m, s;
cin >> n >> m >> s;
for (long long i = 0; i < m; i++) {
long long u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
edge.push_back({{u, v}, w});
}
long long l;
cin >> l;
dijkstra(s);
long long ans = 0;
for (long long i = 1; i <= n; i++)
if (dis[i] == l) ans++;
for (auto e : edge) {
long long u = e.first.first, v = e.first.second, w = e.second;
if (dis[v] < l && dis[u] < l && dis[v] + dis[u] + w == 2 * l) {
ans++;
continue;
}
if (l - dis[u] < w && l > dis[u] && w - l + dis[u] + dis[v] > l) ans++;
if (l - dis[v] < w && l > dis[v] && w - l + dis[u] + dis[v] > l) ans++;
}
cout << ans << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int as[100005], flag = 0, flag2 = 0;
string arr;
cin >> arr;
if (arr[0] == 'a')
as[0] = 1;
else
as[0] = 0;
for (int i = 1; i < arr.size(); i++) {
if (arr[i] == 'a')
as[i] = as[i - 1] + 1;
else
as[i] = as[i - 1];
}
int i;
for (i = 0; i < arr.size(); i++) {
if (as[i] == as[arr.size() - 1]) {
if (i + 1 - as[i] == arr.size() - i - 1) {
for (int j = 0, k = i + 1; j <= i && k < arr.size();) {
if (arr[j] == 'a')
j++;
else {
if (arr[j] != arr[k]) {
flag++;
break;
} else {
j++;
k++;
continue;
}
}
}
flag2++;
break;
}
}
}
if (flag2 == 0 || flag != 0)
cout << ":(";
else
for (int j = 0; j <= i; j++) cout << arr[j];
cout << endl;
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define M_PI 3.14159265358979323846
#define Speed_UP ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#warning Remember to change t
inline void setIO();
using namespace std;
typedef long long ll;
typedef long double ld;
int solve(){
ll n,k;
cin >> n >> k;
ll maxPos = (n-1LL)/2;
if(k>maxPos){
cout<<"-1\n";
}else{
vector<ll>ans(n);
ll c=0;
for(int i=0;i<n;i++){
ans[i]=i+1;
}
ll i = n-1;;
while(c!=k){
if(i-1>=0){
swap(ans[i],ans[i-1]);
i=i-2;
c++;
}
}
for(auto u:ans)
cout<<u<<" ";
cout<<"\n";
}
return 0;
}
int main(){
Speed_UP
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
ll t = 1;
cin >> t;
while(t--){
solve();
}
}
inline void setIO(string name="") {
#ifndef ONLINE_JUDGE
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
#endif
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
using namespace std;
long long poww(long long a, long long b, long long md) {
return (!b ? 1
: (b & 1 ? a * poww(a * a % md, b / 2, md) % md
: poww(a * a % md, b / 2, md) % md));
}
const int maxn = 300 + 5;
const long long inf = 9223372036854775807;
const long long mod = 1e9 + 7;
const int lg = 20;
struct ch {
int pos = 0, npos = 0;
pair<long double, long double> pt[maxn], npt[maxn];
ch() {
for (int i = 0; i < maxn; i++) {
pt[i] = npt[i] = {0.00000, 0.00000};
}
}
pair<double, double> cut(pair<double, double> p1, pair<double, double> p2,
double a, double b) {
double k = (p1.first - p2.first) / (p1.second - p2.second);
double m = p1.first - k * p1.second;
double rx = (a - m) / (k - b + a);
double ry = k * rx + m;
return make_pair(ry, rx);
}
void add(int a, int b) {
if (pos == 0 || (a >= pt[0].first && b >= pt[pos - 1].first)) {
pos = 2;
pt[0] = make_pair(a, 0);
pt[1] = make_pair(b, 1);
return;
}
bool lw = a < pt[0].first;
if (lw) {
npt[0] = pt[0];
} else {
npt[0] = make_pair(a, 0);
}
npos = 1;
for (int i = 1; i < pos; i++) {
double h = pt[i].second * (b - a) + a;
bool now = h < pt[i].first;
if (lw) {
if (!now) {
npt[npos] = cut(pt[i - 1], pt[i], a, b);
npos++;
lw = false;
} else {
npt[npos] = pt[i];
npos++;
}
} else {
if (now) {
npt[npos] = cut(pt[i - 1], pt[i], a, b);
npos++;
lw = true;
npt[npos] = pt[i];
npos++;
}
}
}
if (!lw) {
npt[npos] = make_pair(b, 1);
npos++;
}
for (int i = 0; i < npos; i++) pt[i] = npt[i];
pos = npos;
}
double get() {
double cur = 0;
for (int i = 0; i < pos - 1; i++) {
cur +=
(pt[i].first + pt[i + 1].first) * (pt[i + 1].second - pt[i].second);
}
return cur / 2.0;
}
};
ch c[maxn];
int n, k, x[maxn][maxn];
long double ans[maxn], s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n >> k;
cout << fixed << setprecision(5);
for (int i = 1; i <= n; i++) {
cin >> x[i][0];
for (int j = 1; j <= k; j++) {
s = c[j].get();
cin >> x[i][j];
c[j].add(x[i][j - 1], x[i][j]);
ans[i] += c[j].get() - s;
}
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " " << endl;
}
}
| 5 |
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while (t--) {
ll n;
cin>>n;
ll r[n];
vector<pair<ll, ll>> path(n+1);
path[0] = {1, 1};
for (int i = 0; i < n; i++) {
cin>>r[i];
}
for (int i = 1; i <= n; i++) {
ll c;
cin>>c;
path[i] = {r[i-1], c};
}
sort(path.begin(), path.end());
ll cost = 0;
for (int i = 1; i < path.size(); i++) {
ll left = (path[i].first - path[i].second) - (path[i-1].first - path[i-1].second);
ll free = path[i-1].first+path[i-1].second;
if (free%2 == 0 && left == 0) {
cost += path[i].first - path[i-1].first;
}
else if (free%2 == 1 && left == 0) {
cost += 0;
}
else if (free%2 == 0) {
cost += left/2;
}
else {
cost += (left+1)/2;
}
}
cout<<cost<<"\n";
}
}
| 6 |
#include <bits/stdc++.h>
int main() {
int n, m;
scanf("%d %d", &n, &m);
int a[n][m];
int x[2500], y[2500], x1 = 0, y1 = 0, YES = 0;
for (int i = 0; i < 2500; i++) {
x[i] = 51;
y[i] = 51;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
if (a[i][j] == 1) {
x[x1] = i;
y[y1] = j;
x1++;
y1++;
}
}
}
for (int i = 0; i < 2450; i++) {
if (x[i] == 0 || x[i] == n - 1 || y[i] == 0 || y[i] == m - 1) {
YES = 1;
break;
}
}
if (YES == 1)
printf("2");
else
printf("4");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int cont;
vector<vector<int> > adj, adj2;
vector<int> vis, dfsnum, dfslow, dualdfsnum, comp;
vector<pair<int, int> > bridgeslist;
queue<pair<int, int> > qu;
set<pair<int, int> > st;
int bridges(int cur, int pater) {
vis[cur] = 1;
dfsnum[cur] = dfslow[cur] = ++cont;
dualdfsnum[cont] = cur;
for (int i = 0; i < adj[cur].size(); i++) {
if (adj[cur][i] == pater) continue;
if (!vis[adj[cur][i]]) {
dfslow[cur] = min(dfslow[cur], bridges(adj[cur][i], cur));
if (dfslow[adj[cur][i]] > dfsnum[cur])
bridgeslist.push_back(pair<int, int>(adj[cur][i], cur));
} else
dfslow[cur] = min(dfslow[cur], dfsnum[adj[cur][i]]);
}
return dfslow[cur];
}
int main() {
if (fopen("input.txt", "r")) freopen("input.txt", "r", stdin);
int n, m, x, y;
scanf("%d %d ", &n, &m);
adj.assign(n + 1, vector<int>());
for (int i = 0; i < m; i++) {
scanf("%d %d ", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
vis.assign(n + 1, 0);
dfsnum.assign(n + 1, 0);
dfslow.assign(n + 1, 0);
dualdfsnum.assign(n + 1, 0);
cont = 0;
bridges(1, -1);
for (int i = 0; i < bridgeslist.size(); i++) {
st.insert(pair<int, int>(bridgeslist[i].first, bridgeslist[i].second));
st.insert(pair<int, int>(bridgeslist[i].second, bridgeslist[i].first));
}
comp.assign(n + 1, 0);
vis.assign(n + 1, 0);
for (int i = 1; i < n + 1; i++) {
if (comp[i]) continue;
qu.push(pair<int, int>(i, i));
while (!qu.empty()) {
pair<int, int> u = qu.front();
qu.pop();
if (vis[u.first]) continue;
vis[u.first] = 1;
comp[u.first] = u.second;
for (int j = 0; j < adj[u.first].size(); j++)
if (!vis[adj[u.first][j]] &&
st.find(pair<int, int>(u.first, adj[u.first][j])) == st.end())
qu.push(pair<int, int>(adj[u.first][j], u.second));
}
}
adj2.assign(n + 1, vector<int>());
for (int i = 0; i < bridgeslist.size(); i++) {
x = comp[bridgeslist[i].first];
y = comp[bridgeslist[i].second];
adj2[x].push_back(y);
adj2[y].push_back(x);
}
int last = 1;
qu.push(pair<int, int>(1, 0));
vis.assign(n + 1, 0);
while (!qu.empty()) {
pair<int, int> u = qu.front();
qu.pop();
if (vis[u.first]) continue;
vis[u.first] = 1;
last = u.first;
for (int i = 0; i < adj2[u.first].size(); i++)
if (!vis[adj2[u.first][i]]) qu.push(pair<int, int>(adj2[u.first][i], 0));
}
qu.push(pair<int, int>(last, 0));
vis.assign(n + 1, -1);
while (!qu.empty()) {
pair<int, int> u = qu.front();
qu.pop();
if (vis[u.first] != -1) continue;
vis[u.first] = u.second;
last = u.first;
for (int i = 0; i < adj2[u.first].size(); i++)
if (vis[adj2[u.first][i]] == -1)
qu.push(pair<int, int>(adj2[u.first][i], u.second + 1));
}
printf("%d", vis[last]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m = 999999999, minimum = 999999999;
int newa, newb;
cin >> n;
long long a = 0, b = 1;
for (a = 1; a <= n; ++a) {
if (n % a == 0) {
if (a <= n / a) {
if (abs(b - a) < m) {
b = n / a;
newa = a;
m = abs(b - a);
}
}
}
}
cout << newa << " " << b;
return 0;
}
| 1 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 3e5 + 5;
int mx[MAX_N << 2], tg[MAX_N << 2];
#define lson (o << 1)
#define rson (o << 1 | 1)
void puttag(int o, int v) { mx[o] += v, tg[o] += v; }
void pushup(int o) { mx[o] = max(mx[lson], mx[rson]); }
void pushdown(int o) {
if (!tg[o]) return ;
puttag(lson, tg[o]), puttag(rson, tg[o]);
tg[o] = 0;
}
void modify(int o, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) return puttag(o, v);
pushdown(o);
int mid = (l + r) >> 1;
if (ql <= mid) modify(lson, l, mid, ql, qr, v);
if (qr > mid) modify(rson, mid + 1, r, ql, qr, v);
pushup(o);
}
struct Node { int x, y; } a[MAX_N];
int stk1[MAX_N], stk2[MAX_N], top1, top2;
int N, H, W, ans = 0;
void solve() {
sort(&a[1], &a[N + 1], [&](const Node &l, const Node &r) { return l.x < r.x; } );
memset(mx, 0, sizeof(mx));
memset(tg, 0, sizeof(tg));
modify(1, 1, N, 1, N, H);
top1 = top2 = 0;
stk1[0] = stk2[0] = 1;
for (int i = 2; i <= N; i++) {
modify(1, 1, N, 1, i - 1, a[i].x - a[i - 1].x);
ans = max(ans, mx[1]);
if (a[i].y >= H >> 1) {
modify(1, 1, N, stk1[top1], i - 1, a[i].y - H);
while (top1 && a[stk1[top1]].y > a[i].y) {
modify(1, 1, N, stk1[top1 - 1], stk1[top1] - 1, a[i].y - a[stk1[top1]].y);
--top1;
}
stk1[++top1] = i;
} else {
modify(1, 1, N, stk2[top2], i - 1, -a[i].y);
while (top2 && a[stk2[top2]].y < a[i].y) {
modify(1, 1, N, stk2[top2 - 1], stk2[top2] - 1, a[stk2[top2]].y - a[i].y);
--top2;
}
stk2[++top2] = i;
}
}
}
int main () {
W = gi(), H = gi(), N = gi() + 1;
ans = max(W, H) + 1;
for (int i = 1; i < N; i++) a[i] = (Node){gi(), gi()};
a[N] = (Node){W, H}; a[++N] = (Node){0, 0};
solve();
for (int i = 1; i <= N; i++) swap(a[i].x, a[i].y);
swap(W, H);
solve();
printf("%d\n", ans << 1);
return 0;
} | 0 |
#include <iostream>
using namespace std;
int euler_phi(int n){
int res = n;
for(int i=2;i*i<=n;i++){
if(n % i == 0){
res = res / i * (i - 1);
for(;n%i==0;n/=i);
}
}
if(n != 1) res = res / n * (n - 1);
return res;
}
const int N = 1000000;
long long dat[N+1], t, n;
int main(){
dat[0] = 1;
for(int i=1;i<=N;i++) dat[i] = dat[i-1] + euler_phi(i);
cin >> t;
for(int i=0;i<t;i++){
cin >> n;
cout << dat[n] << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long extgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
long long g = extgcd(b, a % b, y, x);
y -= (a / b) * x;
return g;
}
long long chinese_remainder(long long cnt, long long a[], long long m[]) {
long long mc = 1;
for (int i = 0; i < cnt; i++) mc *= m[i];
long long ans = 0, x, y;
for (int i = 0; i < cnt; i++) {
extgcd(m[i], mc / m[i], x, y);
y = (y + m[i]) % m[i];
ans += a[i] * mc / m[i] % mc * y % mc;
ans %= mc;
}
return ans;
}
long long powmod(long long a, long long b, long long c) {
long long ans = 1;
while (b) {
if (b & 1) ans *= a, ans %= c;
b >>= 1;
a *= a;
a %= c;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
long long a, b, p, d;
cin >> a >> b >> p >> d;
long long i, j, x, y;
long long ans = 0;
long long t1[2], t2[2];
long long t = p * (p - 1);
t2[0] = p - 1, t2[1] = p;
for (i = 0; i < p - 1; i++) {
extgcd(p, powmod(a, i, p), x, y);
y = (y + p) % p;
j = b * y % p;
t1[0] = i, t1[1] = j;
long long tmp = chinese_remainder(2, t1, t2);
ans += d / t + (d % t >= tmp);
}
cout << ans << endl;
}
| 5 |
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
struct timer{
time_t start;
timer(){start=clock();}
~timer(){cerr<<1.*(clock()-start)/CLOCKS_PER_SEC<<" secs"<<endl;}
};
typedef istringstream iss;
typedef long long ll;
typedef pair<int,int> pi;
typedef stringstream sst;
typedef vector<int> vi;
#define MN 210
int N_;
ll A[MN][MN];
ll B[MN][MN];
ll work[MN][MN];
const ll LIM=LLONG_MAX-(MOD-1)*(MOD-1);
// a := a*b
void matmul(ll a[][MN], ll b[][MN]){
memset(work,0,sizeof(work));
rep(i,N_)rep(k,N_)rep(j,N_){
work[i][j]+=a[i][k]*b[k][j];
if(work[i][j]>LIM)work[i][j]%=MOD;
}
rep(i,N_)rep(j,N_)a[i][j]=work[i][j]%MOD;
}
// a := b*a
void matmulleft(ll a[][MN], ll b[][MN]){
memset(work,0,sizeof(work));
rep(i,N_)rep(k,N_)rep(j,N_){
work[i][j]+=b[i][k]*a[k][j];
if(work[i][j]>LIM)work[i][j]%=MOD;
}
rep(i,N_)rep(j,N_)a[i][j]=work[i][j]%MOD;
}
// a := a^2
void matsq(ll a[][MN]){
memset(work,0,sizeof(work));
rep(i,N_)rep(k,N_)rep(j,N_){
work[i][j]+=a[i][k]*a[k][j];
if(work[i][j]>LIM)work[i][j]%=MOD;
}
rep(i,N_)rep(j,N_)a[i][j]=work[i][j]%MOD;
}
// A := A^n
void matpow(ll a[][MN],ll n){
memset(B,0,sizeof(B));
rep(i,N_)B[i][i]=1;
while(n){
if(n&1)matmul(B,A);
matsq(A);
n>>=1;
}
rep(i,N_)rep(j,N_)A[i][j]=B[i][j];
}
ll fib(ll n){
n+=1;
N_=2;
A[0][0]=1;A[0][1]=1;
A[1][0]=1;A[1][1]=0;
matpow(A,n);
return A[1][0];
}
ll N;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>N;
ll lo=0,hi=INF*2;
while(lo+1<hi){
ll mi=lo+hi>>1;
ll m=(mi+1)/2;
ll sum=m*(m+1)/2*2;
if(mi%2){
sum-=m;
}
//cout<<mi<<" "<<sum<<endl;
if(sum>=N)hi=mi;
else lo=mi;
}
ll M=hi;
ll m=(M+1)/2;
ll sum=m*(m+1)/2*2;
if(M%2){
sum-=m;
}
ll no=m-(sum-N);
//cout<<M<<" "<<no<<endl;
ll a;
if(no <= (m+1)/2) a = no*2-1;
else{
if(m%2) a = m-1 - (no-(m+1)/2-1)*2;
else a = m - (no-m/2-1)*2;
}
ll b = M+1-a;
//cout<<a<<" "<<b<<endl;
cout<<fib(a)*fib(b)%MOD<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int i, j, k, m, n, ans = 0, cnt = 0;
cin >> n;
string s;
cin >> s;
long long int sum = 0;
for (int i = 0; i < s.length(); i++) {
sum += (s[i] - '0');
}
long long int dif = n - sum;
bool flag = 1;
for (int i = 0; i < s.length(); i++) {
if (s[i] != '9') flag = 0;
}
if (flag) {
cout << 0 << endl;
} else {
sort(s.begin(), s.end());
for (int i = 0; i < s.length(); i++) {
if (dif <= 0) {
break;
} else {
dif -= (9 - (s[i] - '0'));
cnt++;
}
}
cout << cnt << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
const int N = 3e5 + 3;
const int mod = 1e9 + 7;
int n;
ll a[N];
template <class T>
struct FT {
vector<T> tree;
int sz;
FT(int n) { sz = n, tree.resize(n + 1); }
T get(int i) {
if (i <= 0 || i > sz) return T(0);
T sum = T(0);
for (; i; i -= (i & -i)) sum += tree[i];
return sum;
}
void upd(int i, T val) {
for (; i <= sz; i += (i & -i)) tree[i] += val;
}
void init(T *arr, int n) {
sz = n;
for (int i = 1; i <= n; i++) upd(i, arr[i - 1]);
}
};
ll ans[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
FT<ll> tree1(N);
FT<ll> tree2(N);
ll sum = 0;
for (int i = 0; i < n; i++) {
ans[i] = 1ll * i * a[i] + sum;
ans[i] -= tree1.get(a[i]);
sum += a[i];
int mul = a[i];
while (mul < N) {
tree1.upd(mul, 1ll * a[i]);
mul += a[i];
}
mul = (300000 / a[i]) * a[i];
ll cnt = 0;
while (mul != 0) {
ll cur = i - tree2.get(mul - 1);
ll how_many = cur - cnt;
ans[i] -= 1ll * mul * how_many;
cnt = cur;
mul -= a[i];
}
tree2.upd(a[i], 1ll);
}
for (int i = 0; i < n; i++) {
printf("%lld ", ans[i]);
ans[i + 1] += ans[i];
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
char a[N], b[N], ans[N];
int main() {
scanf("%s", a);
scanf("%s", b);
int n = strlen(a);
sort(a, a + n);
sort(b, b + n);
reverse(b, b + n);
int la = 0, lb = (n + 1) >> 1;
int ra = 0, rb = n >> 1;
int l = 0, r = n;
for (int i = 0; i < n; i++) {
if (i & 1) {
if (a[la] < b[ra])
ans[l++] = b[ra++];
else
ans[--r] = b[--rb];
} else {
if (a[la] < b[ra])
ans[l++] = a[la++];
else
ans[--r] = a[--lb];
}
}
ans[n] = 0;
puts(ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
string s;
string arr[20], mark[20];
void solve() {
int n;
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
mp[arr[i]]++;
}
for (int i = 1; i <= n; i++) {
if (mp[arr[i]] != 1) {
ans++;
for (int k = 0; k < 4; k++) {
string s = arr[i];
char j = '0';
while (mp[s] && j <= '9') {
s[k] = j++;
}
if (!mp[s]) {
mp[s]++;
mark[i] = s;
break;
}
}
mp[arr[i]]--;
} else {
mark[i] = arr[i];
}
}
cout << ans << endl;
for (int i = 1; i <= n; i++) {
cout << mark[i] << endl;
}
mp.clear();
}
int main() {
ios::sync_with_stdio(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
inline int max(int x, int y) { return x > y ? x : y; }
int V, E, k, l, r, o, u, v, w, x, G[8 + 1][300 + 1][300 + 1],
F[2][300 + 1][300 + 1];
int main() {
scanf("%d %d", &V, &E);
for (k = 0; (1 << k) <= V; ++k)
for (u = 1; u <= V; ++u)
for (v = 1; v <= V; ++v) G[k][u][v] = -0x3F3F3F3F;
while (E--) scanf("%d %d", &u, &v), scanf("%d %d", &G[0][u][v], &G[0][v][u]);
for (k = 1; (1 << k) <= V; ++k) {
for (u = 1; u <= V; ++u)
for (v = 1; v <= V; ++v) G[k][u][v] = G[k - 1][u][v];
for (u = 1; u <= V; ++u)
for (v = 1; v <= V; ++v)
for (x = 1; x <= V; ++x)
G[k][u][v] = max(G[k][u][v], G[k - 1][u][x] + G[k - 1][x][v]);
}
for (u = 1; u <= V; ++u)
for (v = 1; v <= V; ++v) F[0][u][v] = u == v ? 0 : -0x3F3F3F3F;
r = 0;
for (--k; k >= 0; --k) {
for (u = 1; u <= V; ++u)
for (v = 1; v <= V; ++v) F[1][u][v] = -0x3F3F3F3F;
for (u = 1; u <= V; ++u)
for (v = 1; v <= V; ++v)
for (x = 1; x <= V; ++x)
F[1][u][v] = max(F[1][u][v], F[0][u][x] + G[k][x][v]);
for (u = 1; u <= V && F[1][u][u] <= 0; ++u)
;
if (u <= V) {
r = w + (1 << k);
continue;
}
w += 1 << k;
for (u = 1; u <= V; ++u)
for (v = 1; v <= V; ++v) F[0][u][v] = F[1][u][v];
}
printf("%d\n", r);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct Martix {
long long a[100][100];
};
Martix unit;
Martix operator*(Martix a, Martix b) {
Martix ans;
memset(ans.a, 0, sizeof(ans.a));
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
long long r = 0;
for (int k = 0; k < 2; k++) {
r += (a.a[i][k] * b.a[k][j] + 1000000007) % 1000000007;
}
ans.a[i][j] = (r + 1000000007) % 1000000007;
}
}
return ans;
}
void init() {
memset(unit.a, 0, sizeof(unit.a));
for (int i = 0; i < 100; i++) unit.a[i][i] = 1;
}
Martix qpow_mod(Martix a, long long n) {
Martix ret = unit;
while (n) {
if (n & 1) ret = ret * a;
n >>= 1;
a = a * a;
}
return ret;
}
int main() {
Martix p, ans;
long long n, x, y;
while (scanf("%I64d%I64d", &x, &y) != EOF) {
init();
scanf("%I64d", &n);
memset(p.a, 0, sizeof(p.a));
p.a[0][0] = 1, p.a[0][1] = -1;
p.a[1][0] = 1;
p.a[1][1] = 0;
if (n > 2) {
ans = qpow_mod(p, n - 2);
long long ret =
ans.a[0][0] * y % 1000000007 + ans.a[0][1] * x % 1000000007;
ret = (ret % 1000000007 + 1000000007) % 1000000007;
printf("%I64d\n", ret);
} else {
if (n == 1)
printf("%I64d\n", (x + 1000000007) % 1000000007);
else
printf("%I64d\n", (y + 1000000007) % 1000000007);
}
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#ifdef BTK
#include<dvector.h>
#define DEBUG if(1)
#else
#define CIN_ONLY if(1)
struct cww{cww(){
CIN_ONLY{
ios::sync_with_stdio(false);cin.tie(0);
}
}}star;
#define DEBUG if(0)
#endif
#define fin "\n"
#define FOR(i,bg,ed) for(int i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define REC(ret, ...) std::function<ret (__VA_ARGS__)>
template <typename T>inline bool chmin(T &l,T r)
{bool a=l>r;if(a)l=r;return a;}
template <typename T>inline bool chmax(T &l,T r)
{bool a=l<r;if(a)l=r;return a;}
template <typename T>
istream& operator>>(istream &is,vector<T> &v){
for(auto &it:v)is>>it;
return is;
}
typedef __int128 INT;
int main(){
int N;LL T;
cin>>N>>T;
LL a = 0;
LL b = 0;
REP(i,N){
LL h;
cin>>h;
auto check = [&](INT t){
return T<a*t+b+h;
};
chmax(a,h);
LL lb = -1e15;
LL ub = 1e15;
while(ub-lb>1){
const LL mid= (lb+ub)/2;
if(check(mid))ub=mid;
else lb=mid;
}
cout<<max(1ll,ub+1)<<endl;
b+=h;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int dp[55][55][55];
char dr[55][55];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%s", dr[i] + 1);
memset(dp, 63, sizeof(dp));
for (int i = 1; i <= n; ++i)
for (int j = i; j <= n; ++j) dp[i][j][n + 1] = 0;
for (int i = n; i >= 1; --i)
for (int h = n; h >= 1; --h)
for (int j = i; j <= n; ++j) {
for (int k = i; k < j; ++k)
dp[i][j][h] = min(dp[i][j][h], dp[i][k][h] + dp[k + 1][j][h]);
int mx1 = 0, mx2 = 1e9, mx3 = 0, mx4 = 1e9;
for (int k = h + 1; k <= n + 1; ++k) {
for (int l = i; l <= j; ++l)
if (dr[k - 1][l] == '#')
mx1 = max(mx1, l), mx2 = min(mx2, l), mx3 = max(mx3, k + 1),
mx4 = min(mx4, k + 1);
if (!mx1)
dp[i][j][h] = min(dp[i][j][h], dp[i][j][k]);
else
dp[i][j][h] = min(dp[i][j][h],
dp[i][j][k] + max(mx1 - mx2 + 1, mx3 - mx4 + 1));
}
}
printf("%d", dp[1][n][1]);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, 1, 0, -1};
void solve() {
long long a, b;
cin >> a >> b;
map<char, long long> mp;
string s = to_string(a);
string t = to_string(b);
for (long long i = 0; i < s.size(); i++) mp[s[i]]++;
if (t.size() > s.size()) {
sort(s.rbegin(), s.rend());
cout << s << "\n";
return;
}
string now;
for (long long i = 0; i < t.size(); i++) {
if (mp[t[i]]) {
mp[t[i]]--;
now.push_back(t[i]);
} else {
long long flag = 0;
for (long long j = t[i] - '0' - 1; j >= 0; j--) {
if (mp[j + '0']) {
flag = 1;
now.push_back('0' + j);
mp[j + '0']--;
string bache;
for (auto x : mp) {
long long p = x.second;
while (p--) bache.push_back(x.first);
}
sort(bache.rbegin(), bache.rend());
now += bache;
cout << now << "\n";
return;
}
}
if (!flag) {
while (now.size()) {
char cur = now.back();
mp[cur]++;
now.pop_back();
for (long long j = cur - '0' - 1; j >= 0; j--) {
if (mp[j + '0']) {
mp[j + '0']--;
now.push_back('0' + j);
string bache;
for (auto x : mp) {
long long p = x.second;
while (p--) bache.push_back(x.first);
}
sort(bache.rbegin(), bache.rend());
now += bache;
cout << now << "\n";
return;
}
}
}
}
}
}
cout << now << "\n";
return;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
| 3 |
#include<bits/stdc++.h>
#define LL long long
static const LL INF = 1<<50;
using namespace std;
int n,k,ans;
LL s[10001];
string num[11];
int t=0;
void cal(string sum,int depth,int bit){
if(depth==k){
int num;
num=atoi(sum.data());
s[t]=num;
t++;
return;
}
for(int i=1;i<=n;++i){
int b=1<<i;
if(bit&b){
int k=bit-b;
cal(sum+num[i],depth+1,k);
}
}
}
int main(){
while(1){
t=0;
for(int i=0;i<11;++i)num[i]='0';
memset(s,0,sizeof(s));
int p=1;
cin>>n>>k;
if(n==0&&k==0)break;
string a;
for(int i=1;i<=n;++i){
p+=pow(2,i);
}
for(int i=1;i<=n;++i){
cin>>num[i];
}
cal(a,0,p);
sort(s,s+t);
for(int i=0;i<t;++i){
if(s[i]&&s[i]!=s[i+1]){
ans++;
}
}
cout<<ans<<endl;
ans=0;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
class Graph {
public:
int n;
vector<vector<int> > adj;
vector<int> a;
vector<bool> dp;
Graph(int n) {
this->n = n;
a.resize(n + 1);
adj.resize(n + 1);
dp.resize(n + 1, false);
}
void add_edge(int u, int v) { adj[u].push_back(v); }
void create_tree() {
for (int i = 1; i <= n; i++) {
for (int m = (1 - i) / a[i]; m <= (n - i) / a[i]; m++) {
if (a[i + m * a[i]] > a[i]) adj[i].push_back(i + m * a[i]);
}
}
}
void printGraph() {
for (size_t i = 0; i < n; i++) {
cout << i + 1 << " - > ";
for (int j = 0; j < adj[i + 1].size(); j++) {
cout << adj[i + 1][j] << " ";
}
cout << "\n";
}
}
void dfs(int u, vector<bool>& visited) {
visited[u] = true;
dp[u] = true;
for (auto v : adj[u]) {
if (!visited[v]) dfs(v, visited);
dp[u] = dp[u] && (!dp[v]);
}
}
void solve() {
vector<bool> visited(n + 1, false);
create_tree();
for (int i = 1; i <= n; i++) {
if (!visited[i]) dfs(i, visited);
}
for (int i = 1; i <= n; i++) cout << (dp[i] ? "B" : "A");
cout << "\n";
}
};
int main() {
int n;
cin >> n;
Graph g(n);
for (int i = 1; i <= n; i++) cin >> g.a[i];
g.solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
scanf("%d%d", &n, &k);
printf("%d\n", ((n - 1) * 6 + 5) * k);
for (int i = 0; i < n; i++)
printf("%d %d %d %d\n", (6 * i + 1) * k, (6 * i + 2) * k, (i * 6 + 3) * k,
(i * 6 + 5) * k);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool uax(T &x, T y) {
return (y > x) ? x = y, true : false;
}
template <typename T>
inline bool uin(T &x, T y) {
return (y < x) ? x = y, true : false;
}
void err(istream_iterator<string>) {}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> buf) {
return os << "(" << buf.first << ": " << buf.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, vector<vector<T>> &A) {
for (auto &B : A) os << '\n' << B;
return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &A) {
bool f = false;
os << "{";
for (const auto e : A) {
if (f) os << ", ";
os << e;
f = true;
}
return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &A) {
bool f = false;
os << "{";
for (const auto e : A) {
if (f) os << ", ";
os << e;
f = true;
}
return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, multiset<T> &A) {
bool f = false;
os << "{";
for (const auto e : A) {
if (f) os << ", ";
os << e;
f = true;
}
return os << "}";
}
template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> &A) {
bool f = false;
os << "{";
for (auto &e : A) {
if (f) os << ", ";
os << e;
f = true;
}
return os << "}";
}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " =: " << a << endl;
err(++it, args...);
}
template <typename T>
void kek(T ans) {
cout << ans << endl;
exit(0);
}
int const MOD = 1e9 + 7;
long long const INF = 1e18;
int grundy(int x) {
if (x == 1) return 1;
if (x == 2) return 0;
if (x == 3) return 1;
if (x & 1) return 0;
int b = (1 << grundy(x - 1)) | (1 << grundy(x >> 1));
for (int i = 0;; ++i)
if (!(b >> i & 1)) return i;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int &z : a) cin >> z;
int g = 0;
for (int z : a) {
if (k & 1) {
g ^= grundy(z);
} else {
if (z < 3)
g ^= z;
else
g ^= z % 2 == 0;
}
}
kek(g ? "Kevin" : "Nicky");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long num[1010], ans;
void ok(long long x, long long y, long long n, long long m) {
for (long long i = 0; i < 10; i++) num[i] = 0;
if (!n) num[0]++;
if (!m) num[0]++;
while (n) {
num[x % 7]++;
x /= 7;
n /= 7;
}
while (m) {
num[y % 7]++;
y /= 7;
m /= 7;
}
for (int i = 0; i < 10; i++)
if (num[i] > 1) return;
ans++;
}
int main() {
long long n, m;
scanf("%lld%lld", &n, &m);
if (1ll * n * m > 1e7) {
puts("0");
return 0;
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) ok(i, j, n - 1, m - 1);
}
printf("%lld\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string S;
getline(cin, S);
long long tam = S.size();
long long e;
sort(S.begin(), S.end());
long long total = 0;
for (long long i = 0; i < tam; i++) {
e = count(S.begin(), S.end(), S[i]);
total += pow(e, 2);
i = S.find_last_of(S[i]);
}
cout << total;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, q, cnt = 0, pre[100005] = {0}, l[100005], r[100005],
ok[100005] = {0}, sum[100005];
vector<int> v[100005];
void dfs(int now) {
++cnt;
l[now] = cnt;
sum[now] = 1;
for (int i = 0; i < v[now].size(); i++) {
int t = v[now][i];
dfs(t);
sum[now] += sum[t];
}
r[now] = cnt;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m >> k >> q;
while (k--) {
int x, y;
cin >> x >> y;
if (pre[y] != 0) {
v[pre[y]].push_back(x);
ok[x] = 1;
}
pre[y] = x;
}
for (int i = 1; i <= n; i++) {
if (!ok[i]) dfs(i);
}
while (q--) {
int x, y;
cin >> x >> y;
if (pre[y] && l[x] <= l[pre[y]] && r[pre[y]] <= r[x])
cout << sum[x] << endl;
else
cout << 0 << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int mod;
struct Matrix {
long long a[2][2];
Matrix operator*(const Matrix& t) const {
Matrix c;
memset(c.a, 0, sizeof(c.a));
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
c.a[i][j] += a[i][k] * t.a[k][j];
c.a[i][j] %= mod;
}
}
}
return c;
}
};
Matrix gao(long long n) {
Matrix res = {1, 0, 0, 1};
Matrix base = {1, 1, 1, 0};
while (n > 0) {
if (n & 1) res = res * base;
n >>= 1;
base = base * base;
}
Matrix f = {1, 1, 0, 0};
return f * res;
}
long long quick(long long n) {
long long res = 1, base = 2;
while (n > 0) {
if (n & 1) res = (res * base) % mod;
n >>= 1;
base = (base * base) % mod;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
long long n, k, l;
while (cin >> n >> k >> l >> mod) {
long long res = 1, t = gao(n).a[0][0];
long long all = quick(n);
for (int i = 0; i < l; i++) {
if (k & 1) {
res = res * (all - t + mod) % mod;
} else {
res = res * t % mod;
}
k >>= 1;
}
if (k) res = 0;
cout << res % mod << "\n";
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
vector<ll> v(n+1);
v[0]=-1;
for(int i=1;i<=n;i++)
cin>>v[i];
ll ans=0;
for(ll i=1;i<=n;i++)
{
for(ll j=v[i]-i;j<=n;j+=v[i])
{
if(j>i && v[j]*v[i]==i+j)
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
| 2 |
#include <cstdio>
#include <algorithm>
#define LL long long
#define INF 2000000000
#define maxn 1010
using namespace std;
int n,m;
int k[][2]={{-1,0},{1,0},{0,-1},{0,1}};//L,R,U,D
int border[maxn][maxn][4];//边界
int vis[maxn][maxn],qx[maxn*maxn],qy[maxn*maxn];
LL ans;
struct node{
int a,b,c;
}f[maxn],g[maxn];
int X[maxn],Y[maxn],XN,YN;
int findX(int v){
return lower_bound(X,X+XN,v)-X;
}
int findY(int v){
return lower_bound(Y,Y+YN,v)-Y;
}
void bfs(int xx,int yy){
int i,h,t,nx,ny,x,y;
h=t=0;
qx[t]=xx,qy[t]=yy,t++,vis[xx][yy]=1;
while(h<t){
x=qx[h],y=qy[h],h++;
ans+=(LL)(X[x+1]-X[x])*(Y[y+1]-Y[y]);
for(i=0;i<4;i++){
if(border[x][y][i])continue;
nx=x+k[i][0],ny=y+k[i][1];
if(nx==0||nx==XN-1||ny==0||ny==YN-1){
ans=-1;
return;
}
if(vis[nx][ny])continue;
vis[nx][ny]=1,qx[t]=nx,qy[t]=ny,t++;
}
}
}
void init(){
int i,x,y,r,j;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)scanf("%d%d%d",&f[i].a,&f[i].b,&f[i].c),X[XN++]=f[i].c;
X[XN++]=-INF,X[XN++]=INF;
sort(X,X+XN);//自小到大排序
XN=unique(X,X+XN)-X;//去重
for(i=0;i<m;i++)scanf("%d%d%d",&g[i].a,&g[i].b,&g[i].c),Y[YN++]=g[i].a;
Y[YN++]=-INF,Y[YN++]=INF;
sort(Y,Y+YN);//自小到大排序
YN=unique(Y,Y+YN)-Y;
for(i=0;i<n;i++){
x=findX(f[i].c);
r=findY(f[i].b);
if(Y[r]!=f[i].b)r--;
for(j=findY(f[i].a);j<r;j++)
border[x-1][j][1]=border[x][j][0]=1;//边界设定
}
for(i=0;i<m;i++){
y=findY(g[i].a);
r=findX(g[i].c);
if(X[r]!=g[i].c)r--;
for(j=findX(g[i].b);j<r;j++)
border[j][y-1][3]=border[j][y][2]=1;//边界设定
}
}
int main(){
init();
bfs(findX(0)-1,findY(0)-1);
if(ans==-1)printf("INF\n");
else printf("%lld\n",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
if (s[0] >= 'a' && s[0] <= 'z') s[0] -= 32;
cout << s;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10;
const long long MOD = 1e9 + 7;
const long double EPS = 1e-9;
const int N = 81;
const int M = 1e3 + 10;
int d[N][N][N][2];
int a[N][N];
int main() {
ios_base::sync_with_stdio(false);
int n, k1;
cin >> n >> k1;
int m;
cin >> m;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) a[i][j] = INF;
for (int i = 0; i < m; ++i) {
int v, u, w;
cin >> v >> u >> w;
v--, u--;
a[v][u] = min(a[v][u], w);
}
for (int i1 = 0; i1 < n; ++i1)
for (int i2 = 0; i2 < n; ++i2)
for (int i3 = 0; i3 < n; ++i3)
for (int i4 = 0; i4 < 2; ++i4) d[i1][i2][i3][i4] = INF;
for (int l = 0; l < n; ++l)
for (int r = l; r < n; ++r)
for (int v = l; v < r + 1; ++v) d[l][r][v][0] = 0;
for (int k = 1; k < k1; ++k) {
for (int i1 = 0; i1 < n; ++i1)
for (int i2 = 0; i2 < n; ++i2)
for (int i3 = 0; i3 < n; ++i3) d[i1][i2][i3][k % 2] = INF;
for (int l = 0; l < n; ++l)
for (int r = l; r < n; ++r)
for (int v = l; v < r + 1; ++v)
for (int t = l; t < r + 1; ++t)
if (t != v) {
if (t < v)
d[l][r][v][k % 2] = min(d[l][r][v][k % 2],
d[l][v - 1][t][(k + 1) % 2] + a[v][t]);
else if (t > v)
d[l][r][v][k % 2] = min(d[l][r][v][k % 2],
d[v + 1][r][t][(k + 1) % 2] + a[v][t]);
}
}
int ans = INF;
for (int i = 0; i < n; ++i) ans = min(ans, d[0][n - 1][i][(k1 + 1) % 2]);
if (ans == INF)
cout << -1;
else
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 18;
int it[maxn], m;
long long a[maxn];
long long b[maxn];
void read() { cin >> m; }
void update_(int pos, int k) {
while (pos) {
it[pos] += k;
pos -= pos & (-pos);
}
}
void update(int pos, int k) {
while (pos < maxn) {
it[pos] += k;
pos += pos & (-pos);
}
}
int ask(int l, int r) {
int sum = 0;
while (r > 0) {
sum += it[r];
r -= r & (-r);
}
l;
while (l > 0) {
sum -= it[l];
l -= l & (l);
}
cout << sum << endl;
}
void solve() {
long long sz = 1;
long long x, y, z;
double ans;
long long sum = 0LL;
for (int i = 0; i < m; ++i) {
scanf("%lld", &x);
if (x == 1) {
scanf("%lld %lld", &y, &z);
sum += (y * z);
b[y] += z;
} else if (x == 2) {
scanf("%lld", &y);
sum += y;
++sz;
a[sz] = y;
} else if (x == 3) {
sum -= (a[sz] + b[sz]);
b[sz - 1] += b[sz];
a[sz] = 0;
b[sz] = 0;
--sz;
}
ans = (double)sum / (double)sz;
printf("%.6lf\n", ans);
}
}
int main() {
read();
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:268435456")
using namespace std;
template <typename T>
inline T abs(T a) {
return ((a < 0) ? -a : a);
}
template <typename T>
inline T sqr(T a) {
return a * a;
}
template <class T>
T gcd(T a, T b) {
return a ? gcd(b % a, a) : b;
}
template <class T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <class T>
T sign(T a) {
return a > 0 ? 1 : (a < 0 ? -1 : 0);
}
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int dxK[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dyK[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dxKn[] = {-2, -1, 1, 2, 2, 1, -1, -2};
const int dyKn[] = {1, 2, 2, 1, -1, -2, -2, -1};
const int N = int(1e5) + 9;
const int M = int(3e3) + 9;
const int LOGN = 22;
const int SQN = 350;
const int MOD = int(1e9) + 7;
const int INF = 1e9 + 100;
const long long INF64 = 2e18;
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-9;
int n, m;
vector<pair<int, int> > g1[N];
set<int> g2[N];
int col[N], cntc;
int col2[N];
void dfs1(int v) {
col[v] = cntc;
for (int i = 0; i < (int)((int)(g1[v].size())); ++i) {
int to = g1[v][i].first, c = g1[v][i].second;
if (c == 1) {
if (col[to] == 0) dfs1(to);
assert(col[to] == col[v]);
} else {
if (col[to] != 0) {
if (col[to] == col[v]) {
cout << 0;
exit(0);
}
g2[col[v]].insert(col[to]), g2[col[to]].insert(col[v]);
}
}
}
}
void dfs2(int v, int p = -1, int cc = 1) {
col[v] = 1;
col2[v] = cc;
for (set<int>::iterator it = g2[v].begin(); it != g2[v].end(); ++it) {
int to = *it;
if (to == p) continue;
if (col[to]) {
if (col[to] == 1 && col2[to] == col2[v]) {
cout << 0;
exit(0);
}
} else if (col[to] == 0)
dfs2(to, v, 3 - cc);
}
col[v] = 2;
}
void solve() {
cin >> n >> m;
for (int i = 0; i < (int)(m); ++i) {
int a, b, c;
cin >> a >> b >> c;
--a, --b;
g1[a].push_back(make_pair(b, c));
g1[b].push_back(make_pair(a, c));
}
for (int i = 0; i < (int)(n); ++i)
if (col[i] == 0) {
++cntc;
dfs1(i);
}
n = 0;
memset(col, 0, sizeof col);
for (int i = 1; i <= cntc; ++i)
if (col[i] == 0) dfs2(i), ++n;
int res = 1;
for (int i = 1; i < (int)(n); ++i) res = (res * 1LL * 2) % MOD;
cout << res;
}
int main() {
srand(time(NULL));
cout << setprecision(10) << fixed;
cerr << setprecision(10) << fixed;
ios_base::sync_with_stdio(0);
solve();
return 0;
}
| 5 |
#include<bits/stdc++.h>
#define Min(a,b,c) min(a,min(b,c))
#define Max(a,b,c) max(a,max(b,c))
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const double pi = 3.141592653589793;
const double eps = 1e-8;
ll nxt[20000010];
void init()
{
nxt[1] = 1;
for (int i = 2; i <= 20000002; i++)
{
if (nxt[i]) continue;
nxt[i] = i;
for (ll j = i; j * i <= 20000002; j++)
{
if (nxt[i * j] == 0) nxt[i * j] = i;
}
}
}
ll solve(int r)
{
ll p = 1;
while(r!=1)
{
ll div = nxt[r];
p*=2;
//if(div==0){cout<<r;exit(0);};
while(r % div == 0)
{
r /= div;
}
}
return p;
}
int main()
{
init();
int t;
cin >> t;
while (t--)
{
int c, d, x;
scanf("%d%d%d", &c, &d, &x);
vector<int> v;
for (int i = 1; i * i <= x; i++)
{
if (x % i == 0)
{
v.push_back(i);
if (i * i != x)
v.push_back(x / i);
}
}
ll ans = 0;
for (int i = 0; i < v.size(); i++)
{
int k2 = v[i];
if (((ll)d * k2 + x) % c == 0)
{
ll k1 = ((ll)d * k2 + x) / c;
if (k1 % k2 != 0) continue;
ll r = k1 / k2;
ans += solve(r);
}
}
printf("%lld\n", ans);
}
//system("pause");
return 0;
}
| 4 |
#include<iostream>
using namespace std;
typedef long long ll;
int main(){
int n;
cin >> n;
ll a[n], sum = 0;
for(int i = 0; i < n; i++){
cin >> a[i];
sum += a[i];
}
ll ans = 1ll<<60, tmp = 0;
for(int i = 0; i+1 < n; i++){
tmp += a[i];
ans = min(ans, abs(sum-tmp-tmp));
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool f[5050][5050], fl[5050];
int pre[5050][5050], n, m, a[5050], aim, sum, to, tof, inf = 1e9;
int main() {
cin >> n >> m >> aim;
f[0][0] = 1;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
sum += a[i];
int x = a[i] % m;
for (int j = 0; j < m; ++j) f[i][j] = f[i - 1][j], pre[i][j] = j;
for (int j = 0; j < m; ++j) {
if (j + x >= m) x -= m;
if (f[i - 1][j] && !f[i][j + x]) {
f[i][j + x] = 1;
pre[i][j + x] = j;
}
if (x < 0) x += m;
}
}
int las = sum - aim;
if (las < 0 || !f[n][las % m]) return 0 * puts("NO");
puts("YES");
int now = las % m;
for (int i = n; i; --i) {
if (pre[i][now] != now)
fl[i] = 1, tof = i;
else
to = i;
now = pre[i][now];
}
now = a[to];
for (int i = 1; i <= n; ++i) {
if (i == to || i == tof) continue;
printf("%d %d %d\n", inf, i, fl[i] ? tof : to);
now += a[i] * !fl[i];
}
if (!to) to = tof - 1 ? tof - 1 : tof + 1;
if (!tof) tof = to - 1 ? to - 1 : to + 1;
if (now < aim)
printf("%d %d %d\n", (aim - now) / m, tof, to);
else if (now > aim)
printf("%d %d %d\n", (now - aim) / m, to, tof);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long a[300005];
int main() {
int n;
cin >> n;
for (int i = (1); i <= (n); i++) cin >> a[i];
sort(a + 1, a + n + 1);
long long idx = 1, ans = 0;
for (int i = (1); i <= (n); i++) {
ans += abs(a[i] - idx);
idx++;
}
cout << ans << endl;
return 0;
}
| 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.