code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
// CPP
// Mind_flayer404
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define len length()
#define vt vector
#define f(i,a,b) for(ll i=a; i<b; i++)
#define rf(i,a,b) for(ll i=b-1; i>=a; i--)
ll const mod = 1e9+7;
ll const mod1 = 998244353;
ll const N = 1e+6;
ll power(ll a,ll b,ll m)
{
ll res = 1;
while(b>0)
{
if(b&1)
{
res = ((res%m)*(a%m))%m;
}
a = ((a%m)*(a%m))%m;
b /= 2;
}
return res;
}
ll modi(ll a,ll p)
{
return power(a,p-2,p);
}
ll max(ll a,ll b)
{
return a>b?a:b;
}
ll min(ll a,ll b)
{
return a>b?b:a;
}
ll lcm(ll a,ll b)
{
return (a*b)/(__gcd(a,b));
}
ll bins(ll ar[],ll l,ll r,ll x)
{
if(l<=r)
{
ll mid = l + (r-1)/2;
if(ar[mid]==x)
return mid;
else if(ar[mid]>x)
{
bins(ar,l,mid-1,x);
}
else if(ar[mid]<x)
{
bins(ar,mid+1,r,x);
}
}
return -1;
}
ll get(ll n)
{
vt<ll> v;
while(n>0)
{
v.pb(n%10);
n /= 10;
}
ll a = 0,b = 0;
sort(all(v));
ll sz = v.size();
rf(i,0,sz)
{
a += v[i]*pow(10,i);
}
f(i,0,sz)
{
b += v[i]*pow(10,sz - i - 1);
}
return a - b;
}
int main()
{
fast;
ll t = 1;
//cin>>t;
while(t--)
{
ll n,k;
cin>>n>>k;
ll ar[k+1];
ar[0] = n;
f(i,1,k+1)
{
ar[i] = get(ar[i-1]);
}
cout<<ar[k]<<endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void run(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int karp(int n){
int f[11]={};
int z=1,y=1;
while(n>0){
f[n%10]++;
n/=10;
}
for(int i=9;i>=0;i--){
for(int j=0;j<f[i];j++){
y = y*10+i;
}
}
for(int i=0;i<=9;i++){
for(int j=0;j<f[i];j++){
z = z*10+i;
}
}
return (y-z);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
run();
int n,k; cin>>n>>k;
for(int i=0;i<k;i++){
n = karp(n);
}
cout<<n<<endl;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
// Policy based data structure
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long int
#define pii pair<ll,ll>
#define rep(i,st,en) for(ll i=st;i<en;i++)
#define vi vector<ll>
#define vii vector<pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define eb emplace_back
#define yes cout<<"YES"<<endl; return;
#define no cout<<"NO"<<endl; return;
#define flus fflush(stdout);
#define gin(x) cerr<<#x<<" : "<<x<<" ";
#define fin cerr<<endl;
#define F first
#define S second
#define np next_permutation
#define inf 1e18
#define mod 1000000007
#define N 200009
#define PI 3.14159265358979323846
#define minpq priority_queue <ll, vector<ll>, greater<ll>>
#define maxpq priority_queue<ll>
void sout(){
cout<<endl;
}
template <typename T,typename... Types>
void sout(T var1,Types... var2){
cout<<var1<<" ";
sout(var2...);
}
void solve(){
int a,b,w;
cin>>a>>b>>w;
int M=0,m=1000000000;
// ll m = inf;
for(int i=1;i<=1000000;i++){
if(a*i<=w*1000 && b*i>=w*1000){
m=min(m,i);
M=max(M,i);
}
}
if(M==0){
cout<<"UNSATISFIABLE";
return;}
else
cout<<m<<" "<<M;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
clock_t t1=clock();
int t;
// cin>>t;
t=1;
while(t--){
solve();
}
cerr<<"Time elapsed: "<<(double)(clock()-t1)/1000<<" s"<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int a, b, w, q, r;
cin >> a >> b >> w;
w *= 1000;
r = w / a;
if(w % b == 0) q = w / b;
else q = w / b + 1;
if(q > r) cout << "UNSATISFIABLE" << endl;
else{
cout << q << " " << r << endl;
}
} |
#include<cstdio>
int n,f,l;
struct node{
int rating,id;
}a[65537];
inline int read() {
int ln=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') {
ln=(ln<<3)+(ln<<1)+ch-'0';
ch=getchar();
}
return ln;
}
int main() {
l=1,f=2;
n=(1<<read());
for(int i=1;i<=n;i++) {
a[i].rating=read();
a[i].id=i;
}
while(f<n) {
for(int i=1;i<=n;i+=f) {
if(a[i].rating<a[i+l].rating) a[i]=a[i+l];
}
f<<=1;
l<<=1;
}
if(a[1].rating<a[1+l].rating) printf("%d",a[1].id);
else printf("%d",a[1+l].id);
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename T>bool chmin(T& x,const T&y) {if(x>y){x=y;return true;} else return false;}
template<typename T>bool chmax(T& x,const T&y) {if(x<y){x=y;return true;} else return false;}
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cout << (i*2) % N + 1 << ' ' << (i*2+1) % N + 1 << '\n';
}
return 0;
// 重複, 境界, 条件, ll (1e5のnCr), 0, -, 1i, for s&g, debug
} |
#include <bits/stdc++.h>
#define ll long long int
#define w(t) int t; cin>>t; while(t--)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define mii map<int,int>
#define sp(x,y) fixed<<setprecision(y)<<x
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin>>s;
int temp=0;
for(int i=0;i<s.size();i++){
if(s[i]!='.'&&!temp)cout<<s[i];
else if(s[i]=='.')temp=1;
}
return 0;
} | /*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
#define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
#define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1<<20;
int a[SIZE],b[SIZE];
int N;
void solve() {
W(max(0,*min_element(b,b+N)-*max_element(a,a+N)+1));
}
void input() {
R(N);
REP(i,N)R(a[i]);
REP(i,N)R(b[i]);
}
int main(){
input();
solve();
return 0;
}
|
// Jai Shree Ram
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key
#define fbo find_by_order
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ll long long
#define int long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define endl "\n"
#define x first
#define y second
#define gcd(a,b) __gcd(a,b)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define hell 1000000007
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;}
template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;}
const int N=1e9+5;
const int M=1e7;
struct node{
int l;int r;
int val;
int cnt;
}t[M];
int cnt=0;
int n;
void extend(int v){
if(t[v].l==0){
t[v].l=++cnt;
t[v].r=++cnt;
}
}
void pushup(int v){
t[v].val = t[t[v].l].val + t[t[v].r].val;
t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt;
}
void update(int v,int l,int r,pii val,int tl=0,int tr=1e9){
if(tl>r || l>tr)return;
if(l<=tl && tr<=r){
assert(tl == tr);
t[v].val += val.x;
t[v].cnt += val.y;
return;
}
extend(v);
int mid=(tl+tr)/2;
update(t[v].l,l,r,val,tl,mid);
update(t[v].r,l,r,val,mid+1,tr);
pushup(v);
}
pii query(int v,int l,int r,int tl=0,int tr=1e9){
if(tl>r || l>tr)return {0,0};
if(l<=tl && tr<=r){
return {t[v].val,t[v].cnt};
}
int mid=(tl+tr)/2;
extend(v);
pii a =query(t[v].l,l,r,tl,mid);
pii b =query(t[v].r,l,r,mid+1,tr);
return {a.x+b.x,a.y+b.y};
}
int solve(){
int n,m,q; cin >> n >> m >> q;
int ans = 0;
int roota = ++cnt;
int rootb = ++cnt;
vector<int>v1(n+1),v2(m+1);
update(roota,0,0,{0,n});
update(rootb,0,0,{0,m});
rep(i,0,q){
int t; cin >> t;
int x,y; cin >> x >> y;
if(t == 1){
if(y > v1[x]){
auto s = query(rootb,0,y);
ans += s.y*y;
s = query(rootb,v1[x]+1,y);
ans -= s.x;
s = query(rootb,0,v1[x]);
ans -= v1[x]*s.y;
}
else if(y < v1[x]){
auto s = query(rootb,0,v1[x]);
ans -= s.y*v1[x];
s = query(rootb,0,y);
ans += s.y*y;
s = query(rootb,y+1,v1[x]);
ans += s.x;
}
update(roota,v1[x],v1[x],{-v1[x],-1});
update(roota,y,y,{y,1});
v1[x] = y;
}
else{
if(y > v2[x]){
auto s = query(roota,0,y-1);
ans += s.y*y;
s = query(roota,0,v2[x]-1);
ans -= s.y*v2[x];
s = query(roota,v2[x],y-1);
ans -= s.x;
}
else if(y < v2[x]){
auto s = query(roota,0,v2[x]-1);
ans -= s.y*v2[x];
s = query(roota,0,y-1);
ans += s.y*y;
s = query(roota,y,v2[x]-1);
ans += s.x;
}
update(rootb,v2[x],v2[x],{-v2[x],-1});
update(rootb,y,y,{y,1});
v2[x] = y;
}
cout << ans << endl;
}
return 0;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
int t=1;//cin>>t;
while(t--){
solve();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1<<19;
int N, M, Q;
ll a[200005], b[200005];
vector<ll> v;
vector<pair<int, ll>> tree_a(MAXN), tree_b(MAXN); // cnt, sum
int qt[200005], qx[200005], qy[200005];
void update(vector<pair<int, ll>> &tree, int node, int s, int e, int idx, int d) {
if(idx < s || idx > e) return ;
if(s == idx && idx == e) {
tree[node].first += d;
tree[node].second = tree[node].first * v[idx];
return ;
}
int n1 = node << 1, n2 = node << 1 | 1;
int mid = s + e >> 1;
update(tree, n1, s, mid, idx, d);
update(tree, n2, mid+1, e, idx, d);
tree[node].first = tree[n1].first + tree[n2].first;
tree[node].second = tree[n1].second + tree[n2].second;
}
pair<int, ll> query(vector<pair<int, ll>> &tree, int node, int s, int e, int l, int r) {
if(l > e || r < s) return make_pair(0, 0);
if(l <= s && e <= r) return tree[node];
int n1 = node << 1, n2 = node << 1 | 1;
int mid = s + e >> 1;
pair<int, ll> p1 = query(tree, n1, s, mid, l, r);
pair<int, ll> p2 = query(tree, n2, mid+1, e, l, r);
return make_pair(p1.first + p2.first, p1.second + p2.second);
}
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
cin >> N >> M >> Q;
v.push_back(0);
for(int i=0;i<Q;++i) {
cin >> qt[i] >> qx[i] >> qy[i];
v.push_back(qy[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
ll sum = 0;
ll a_sum = 0, b_sum = 0;
update(tree_a, 1, 0, v.size()-1, 0, N);
update(tree_b, 1, 0, v.size()-1, 0, M);
for(int i=0;i<Q;++i) {
int idx = qx[i];
ll val = qy[i];
int q_idx = lower_bound(v.begin(), v.end(), val) - v.begin();
if(qt[i] == 1) {
int cur_idx = lower_bound(v.begin(), v.end(), a[idx]) - v.begin();
pair<int, ll> pre = query(tree_b, 1, 0, v.size()-1, 0, cur_idx);
int n = pre.first;
ll pre_sum = n*a[idx] + b_sum - pre.second;
a_sum -= a[idx];
sum -= pre_sum;
update(tree_a, 1, 0, v.size()-1, cur_idx, -1);
a[idx] = val;
a_sum += val;
update(tree_a, 1, 0, v.size()-1, q_idx, 1);
pair<int, ll> p = query(tree_b, 1, 0, v.size()-1, 0, q_idx);
sum += p.first * a[idx] + b_sum - p.second;
}
else {
int cur_idx = lower_bound(v.begin(), v.end(), b[idx]) - v.begin();
pair<int, ll> pre = query(tree_a, 1, 0, v.size()-1, 0, cur_idx);
int n = pre.first;
ll pre_sum = n*b[idx] + a_sum - pre.second;
b_sum -= b[idx];
sum -= pre_sum;
update(tree_b, 1, 0, v.size()-1, cur_idx, -1);
b[idx] = val;
b_sum += val;
update(tree_b, 1, 0, v.size()-1, q_idx, 1);
pair<int, ll> p = query(tree_a, 1, 0, v.size()-1, 0, q_idx);
sum += p.first * b[idx] + a_sum - p.second;
}
cout << sum << '\n';
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n) for(long long int i=a; i<n; i++)
#define loop(i,a,n) for(long long int i=a; i>=n; i--)
int main()
{
ios_base :: sync_with_stdio(false),cin.tie(NULL);
int a;
cin>>a;
vector<pair<double,double>> v;
forr(i, 0, a)
{
int x, y;
cin>>x>>y;
v.push_back({x, y});
}
int ans=0;
forr(i, 0, a)
{
forr(j, i+1, a)
{
double d=(v[j].second-v[i].second)/(v[j].first-v[i].first);
//cout<<v[i].first<<' '<<v[i].second<<' '<<v[j].first<<' '<<v[j].second<<' '<<d<<'\n';
if(d>=-1.0 and d<=1.0)
ans++;
}
}
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> x(n), y(n);
for(int i=0; i<n; i++){
cin >> x[i] >> y[i];
}
int ans = 0;
for(int i=0; i<n; i++){
for(int j=0; j<i; j++){
int dx = abs(x[i] - x[j]);
int dy = abs(y[i] - y[j]);
if(dx >= dy) ans += 1;
}
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#define int long long
using namespace std;
int nCr(int i,int j){
int sum=1;
for(int X=1;X<=j;X++){
sum*=(i-X+1);sum/=X;
}
return sum;
}
signed main(){
int A,B,K;cin>>A>>B>>K;
int now=K;
int NA=A,NB=B;
string S="";
for(int X=0;X<A+B;X++){
if(now>nCr(NA+NB-1,NB)){
now-=nCr(NA+NB-1,NB);
NB--;
S.push_back('b');
}
else{
NA--;
S.push_back('a');
}
}
cout<<S<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(int)n; ++i)
using ll=long long;
vector<vector<ll>> c;
string kth(int a,int b,ll k) {
if(a==0) return string(b, 'b');
if(b==0) return string(a, 'a');
if(k<=c[a-1][b]) return string("a")+kth(a-1,b,k);
else return string("b")+kth(a, b-1, k-c[a-1][b]);
}
int main() {
int a,b; ll k; cin >> a >> b >> k;
c.resize(a+1, vector<ll>(b+1));
c[0][0]=1;
rep(i,a+1) rep(j,b+1) { if(i>0) c[i][j]+=c[i-1][j]; if(j>0) c[i][j]+=c[i][j-1]; }
cout << kth(a,b,k) << endl;
} |
#include<bits/stdc++.h>
#define PI 3.141592653589793238462
#define eps 1e-20
#define fi first
#define se second
using namespace std;
using cd = complex<double>;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll a[105],d[100005];
int main(){
ll n,sum=0;cin>>n;
for(ll i=1;i<=n;i++){
cin>>a[i];sum+=a[i];
}
d[0]=1;
for(ll i=1;i<=n;i++){
for(ll j=sum;j>=a[i];j--){
d[j]|=d[j-a[i]];
}
}
ll ans=sum;
for(ll i=0;i<=sum;i++){
if(!d[i]) continue;
ans=min(ans,max(i,sum-i));
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
using Table = vector<vector<int>>;
template<typename T>
bool chmin(T &a, T b)
{
if (a > b) {
a = b;
return true;
}
return false;
}
int main()
{
int N;
cin >> N;
vector<int> T(N);
for (auto &Ti : T)
cin >> Ti;
constexpr int INF = 2e+5;
int max_T = *max_element(T.begin(), T.end());
Table dp(N + 1, vector<int>(max_T * N + 1, INF));
dp[0][0] = 0;
for (int i = 0; i < N; ++i) {
auto Ti = T.at(i);
for (int j = 0; j <= max_T * N; ++j) {
int k = Ti + j;
if (k <= max_T * N)
chmin(dp[i + 1][k], dp[i][j]);
chmin(dp[i + 1][j], dp[i][j] + Ti);
}
}
int min_cost = INF;
for (int j = 0; j <= max_T * N; ++j)
chmin(min_cost, max(j, dp[N][j]));
cout << min_cost << endl;
}
|
#include<cstdio>
int n;
int cnt[500];
int main(){
for(int i = 1; i <= 255; ++i) cnt[i] = cnt[i >> 1] + (i & 1);
scanf("%d", &n);
printf("%d\n", (1 << n) - 1);
for(int i = 1; i < (1 << n); ++i){
for(int t = 1; t <= (1 << n); ++t)
printf("%c", (cnt[t & i] & 1) ? 'A' : 'B');
puts("");
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
ll rd(){
int f = 1;
ll x = 0;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-') f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
ll ksm(ll a,ll b,ll m){
ll base = a;
ll ans = 1;
while(b){
if(b & 1) {
ans *= base;
ans %= m;
}
base *= base;
base %= m;
b >>= 1;
}
return ans;
}
int main(){
int n = rd();
vector<string> ans;
ans.push_back("AB");
for(int i = 2;i <= n;i++) {
for(int j = 0;j < (1 << (i - 1)) - 1;j++)
ans[j] += ans[j];
for(int j = 0;j < (1 << (i - 1)) - 1;j++){
string s = ans[j];
for(int k = (1 << (i - 1));k < (1 << i);k++)
s[k] ^= 'A' ^ 'B';
ans.push_back(s);
}
ans.push_back(string(1 << (i - 1),'A') + string(1 << (i - 1),'B'));
}
cout << ans.size() << '\n';
for(auto s:ans) cout << s << '\n';
}
|
#include <bits/stdc++.h>
int main(){
int N;
std::cin >> N;
std::vector< int > A(N);
for(int i=0; i<N; i++){
std::cin >> A[i];
}
int max = 0;
for(int i=0; i<N; i++){
int cur_min = 1e9;
int cur = 0;
for(int j=i; j<N; j++){
cur_min = std::min(cur_min, A[j]);
max = std::max(max, cur_min * (j - i + 1));
}
}
std::cout << max << std::endl;
return 0;
}
| // #define LOCAL
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++)
#define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
// #define M_PI 3.14159265358979323846 // CF
#define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI))
#define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank
#define endl "\n"
#define Case(x) printf("Case #%d: ", x); // gcj
typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<char> vch;
typedef vector<vector<char>> vvch;
constexpr ll LINF = 1001002003004005006ll;
constexpr int INF = 1002003004;
constexpr int n_max = 2e5+10;
template<class T>
inline bool chmax(T &a, T b) { if(a<b) { a=b; return true; } return false; };
template<class T>
inline bool chmin(T &a, T b) { if(a>b) { a=b; return true; } return false; };
template<class T, class U>
T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;};
// debug
template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';};
string to_string(const char c) {return to_string((string) &c);};
string to_string(bool b) {return (b ? "true" : "false");};
template <size_t N>
string to_string(bitset<N> v){
string res = "";
for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
return res;
};
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for(const auto &x : v) {
if(!first) res += ", ";
first = false; res += to_string(x);
}
res += "}";
return res;
};
template <typename A, typename B>
string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
void debug_out() {cerr << endl;};
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); };
void LINE_OUT() {
cout << "--------------" << endl;
};
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define LINE LINE_OUT();
#else
#define debug(...) 71
#define LINE 71;
#endif
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
cout << head;
if (sizeof...(tail) != 0) cout << " ";
print(forward<Tail>(tail)...);
};
template <class T>
void print(vector<T> &vec) {
for (auto& a : vec) {
cout << a;
if (&a != &vec.back()) cout << " ";
}
cout << endl;
};
template <class T>
void print(vector<vector<T>> &df) {
for (auto& vec : df) {
print(vec);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll N; cin >> N;
vl A(N); rep(i, 0, N) cin >> A[i];
A.pb(0);
vpl V;
ll ans = 0;
rep(i, 0, N+1) {
if (V.empty()) {
V.pb(make_pair(A[i], i));
continue;
}
if (V.back().first < A[i]) {
V.pb(make_pair(A[i], i));
continue;
} else if (V.back().first > A[i]) {
int target = i;
while (!V.empty() && V.back().first >= A[i]) {
auto pre = V.back(); V.pop_back();
ll area = pre.first * (i - pre.second);
chmax(ans, area);
target = pre.second;
}
V.pb(make_pair(A[i], target));
}
}
cout << ans << endl;
return 0;
};
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
#define MOD 998244353
struct UnionFind {
vector<int> data;
void init(int n) { data.assign(n, -1); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
bool same(int x, int y) { return root(x) == root(y); }
bool unite(int x, int y) {
x = root(x), y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y];
data[y] = x;
}
return x != y;
}
int size(int x) { return -data[root(x)]; }
};
// x^n
ll mod_pow(ll x, ll n) {
if (n == 0) return 1;
ll res = mod_pow(x * x % MOD, n / 2);
if (n & 1) res = res * x % MOD;
return res;
}
// x^{-1}
ll mod_inv(ll x) { return mod_pow(x, MOD - 2); }
int main() {
int n;
cin >> n;
UnionFind uf;
uf.init(n);
for (int i = 0; i < n; ++i) {
int f;
cin >> f;
f--;
uf.unite(i, f);
}
set<int> st;
for (int i = 0; i < n; ++i) {
st.insert(uf.root(i));
}
ll ans = mod_pow(2, (ll)st.size());
ans = (ans - 1 + MOD) % MOD;
cout << ans << endl;
return 0;
}
|
/**
* Dont raise your voice, improve your argument.
* --Desmond Tutu
*/
#include <bits/stdc++.h>
using namespace std;
const bool ready = [](){
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
return true;
}();
using ld=long double;
const ld PI = acos((ld)-1);
using ll= long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for(int i=0; i<int(n); i++)
#define cini(i) int i; cin>>i;
#define cins(s) string s; cin>>s;
#define cind(d) ld d; cin>>d;
#define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; }
using pii= pair<int, int>;
using pdd= pair<ld, ld>;
using vd= vector<ld>;
using vb= vector<bool>;
using vi= vector<int>;
using vvi= vector<vi>;
using vs= vector<string>;
#define endl "\n"
const int MOD=998244353;
int mul(const int v1, const int v2, int mod=MOD) {
return (int)((1LL * v1 * v2) % mod);
}
int pl(int v1, int v2, int mod=MOD) {
int res = v1 + v2;
while(res < 0)
res += mod;
while(res>=mod)
res-=mod;
return res;
}
/* int pow */
int toPower(int a, int p, int mod=MOD) {
int res = 1;
while (p != 0) {
if (p & 1)
res = mul(res, a, mod);
p >>= 1;
a = mul(a, a, mod);
}
return res;
}
int inv(const int x, const int mod=MOD) {
return toPower(x, mod - 2);
}
const int N=1e6;
vector<int> fac(N);
const bool initFac=[](const int mod) {
fac[1]=1;
for(int i=2; i<N; i++) {
fac[i]=mul(fac[i-1], i, mod);
}
return true;
}(MOD);
/** see https://cp-algorithms.com/combinatorics/binomial-coefficients.html */
int nCr(int n, int k) {
if(k==0 || k==n)
return 1;
if(k==1 || k==n-1)
return n;
if(k>n/2)
return nCr(n, n-k);
return mul(mul(fac[n], inv(fac[k])), inv(fac[n - k]));
}
/*
* Consider including i into the set, so f[i]
* must be included, too, and so on.
* So we need to find components in f[].
* Each complete component satisfies first rule.
*
* And such component allways satisfies the second rule, too.
*
* So we can put each combination of those components
* into the set. If c is count of components, then ans=c!
* ***
* No, but sample 3 makes not sense.
* How can answer be 7?
* There are 7 subsets, but the ones of size 2 do not work,
* the first rule is not satisfied :/
* ...It is! It is three independend components.
* ********************
* So, only circles can be included at all.
* Then each combination of those circles can be used.
* sum(nCr(c, 1..c));
* ************
* The circle detection is more complecated :/
* Starting search somehwere there are several cases:
* -simply a cicle
* -> increment circle count and mark all vertex seen
*
* -ends in previously seen something
* -> just mark visited
*
* -ends somewhere else
* -> increment cirlce count
*/
void solve() {
cini(n);
vi f(n+1);
for(int i=1; i<=n; i++)
cin>>f[i];
int ccnt=0;
vb vis1(n+1); /* vis1[i] -> i is checked */
for(int i=1; i<=n; i++) {
if(vis1[i])
continue;
bool isC=true;
vi nums;
vb vis2(n+1);
int v=i;
while(!vis2[v] && !vis1[v]) {
vis2[v]=true;
nums.push_back(v);
v=f[v];
if(vis1[v])
isC=false;
}
if(isC) /* found a circle */
ccnt++;
for(int j=0; j<nums.size(); j++)
vis1[nums[j]]=true;
}
//cerr<<"ccnt="<<ccnt<<endl;
if(ccnt==0) {
cout<<0<<endl;
return;
}
int ans=0;
for(int i=1; i<=ccnt; i++) {
ans=pl(ans, nCr(ccnt, i));
}
cout<<ans<<endl;
}
signed main() {
solve();
} |
#include<bits/stdc++.h>
#include<queue>
using namespace std;
int main () {
int N;
cin >> N;
string S, T;
cin >> S >> T;
int s = 0;
int t = 0;
for (int i = N - 1; i >= 0; i --) {
if (S[i] == '1') s ++;
if (T[i] == '1') t ++;
if (s < t) {
cout << -1 << endl;
return 0;
}
}
if ((s - t) % 2 != 0) {
cout << -1 << endl;
return 0;
}
int sp = -1;
queue<int>tp;
long long ans = 0;
for (int i = 0; i < N; i ++) {
if (T[i] == '1') {
tp.push(i);
}
if (S[i] == '1') {
if (sp != -1) {
ans += (long long)(i - sp);
sp = -1;
} else if (!tp.empty()) {
ans += (long long)(i - tp.front());
tp.pop();
} else {
sp = i;
}
}
//cout << sp << ' ' << tp << ' ' << ans << endl;
}
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
using LL = long long;
void no(){
cout << "-1";
exit(0);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N;
string S, T;
cin >> N >> S >> T;
vector<int> s, t;
for(int i = 0; i < N; i += 1) if(S[i] == '1') s.push_back(i);
for(int i = 0; i < N; i += 1) if(T[i] == '1') t.push_back(i);
LL ans = 0;
for(int i = 0, j = 0; i < s.size() or j < t.size();){
if(i == s.size()) no();
if(j == t.size() or s[i] < t[j]){
if(i + 1 == s.size()) no();
ans += s[i + 1] - s[i];
i += 2;
continue;
}
ans += s[i] - t[j];
i += 1;
j += 1;
}
cout << ans;
return 0;
} |
#include <iostream>
using namespace std;
int main()
{
int X;
cin >> X;
if (X % 100 == 0) {
X = 100;
} else {
X = 100 - X%100;
}
cout << X;
return 0;
}
| #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep_inv(i, n, m) for(int i = (int)(n); i > (int)(m); i--)
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
int main(){
ll X;
cin >> X;
X %= 100;
cout << 100 - X << "\n";
return 0;
}
|
#include <iostream>
#include<stdio.h>
using namespace std;
int a[1010];
int main()
{
int b;
int n,c=0;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&b);
if(b>n)
{
c=-1;
break;
}
a[b]++;
}
for(int i=1; i<=n; i++)
{
if(a[i]==0)
{
c=-1;
}
}
if(c==-1)
{
printf("No");
}
else printf("Yes");
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
const int N=2e5+6;
char c[N];
char l[N];
int len,top;
int main()
{
cin>>len;
scanf("%s",c+1);
int p=len;
int tot=1;
tot=0;
for(int i=1;i<=len;i++)
{
tot++;
l[tot]=c[i];
if(tot>=3&&l[tot]=='x'&&l[tot-1]=='o'&&l[tot-2]=='f')
p-=3,tot-=3;
}
cout<<p;
return 0;
} |
#include<functional>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<utility>
#include<string>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cmath>
#include<ctime>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
using namespace std;
const int inf=2147483647,dx[]={-1,0,1,0},dy[]={0,-1,0,1};// 上 左 下 右
const int N=100005,M=1000005,K=200005,mod=1000000007;
const long long llinf=9223372036854775807ll;
//int & long long
int a,b;
int main(){
cin>>a>>b;
cout<<(a+b)/2<<' '<<(a+b)/2-b;
return 0;
}
| #pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using vi = vector<int>;
constexpr char newl = '\n';
constexpr double eps = 1e-10;
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define fi first
#define se second
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
F0R(j, sz(v)) i >> v[j];
return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
F0R(i, v.size()) {
o << v[i] << ' ';
}
o << newl;
return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
for (auto& p : m) {
o << p.first << ": " << p.second << newl;
}
o << newl;
return o;
}
#if 1
// INSERT ABOVE HERE
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int a, b;
cin >> a >> b;
int y = (a - b) / 2;
int x = a - y;
cout << x << ' ' << y;
}
#endif
|
#include <bits/stdc++.h>
using namespace std;
/* ループ処理 0からnまで*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main()
{
int x, y;
cin >> x >> y;
if (x == y)
{
cout << x << endl;
return 0;
}
cout << 3 - (x + y) << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long
#define vi vector <int>
#define vll vector <long long int>
#define pb push_back
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define time cerr << "Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" sec"<<endl;
#define sp " "
#define chk cout << "Alhamdulillah" << endl;
#define nl endl
int main ()
{
FIO;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt", "w", stderr);
#endif
int x, y, z;
cin >> x >> y;
if(x == y) z = x;
else{
if(x + y == 3) z = 0;
if(x + y == 2) z = 1;
if(x + y == 1) z = 2;
}
cout << z << nl;
time;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, x;
cin >> n >> x;
string s;
cin >> s;
for(int i=0; i<s.size();++i){
if(s.at(i)=='o') x++;
else if(x > 0) x--;
}
cout << x << endl;
} | #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define sz(a) ((int)a.size())
#define re return
#define all(a) a.begin(),a.end()
#define int long long
#define rept(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,a) rept(i,0,a)
#define vi vector<int>
#define vvi(_Name,_Height,_Width) vector<vi>_Name(_Height,vi(_Width))
#define vvi_v(_Name,_Height,_Width,_Value) vector<vi>_Name(_Height,vi(_Width,_Value))
#define pii pair<int,int>
using namespace std;
void fail(string s){ cout<<s;exit(0); }
void fail(int s){ cout<<s;exit(0); }
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
vi v[2000];
vi use(2000);
int n,m;
int dfs(int u){
use[u]=1;
int ans=1;
for(int i:v[u]){
if(!use[i])
ans+=dfs(i);
}
re ans;
}
void run_test_case(){
cin>>n>>m;
rep(i,m){
int a,b;
cin>>a>>b;
v[a-1].pb(b-1);
}
int ans=0;
rep(i,n){
rep(_,n)use[_]=0;
ans+=dfs(i);
}
cout<<ans<<'\n';
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int test_cases=1;
// cin>>test_cases;
while(test_cases--)
run_test_case();
re 0;
}
|
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#define fs(i,x,y) for(int i=(x),_=(y);i<=_;++i)
#define fn(i,x,y) for(int i=(x),_=(y);i>=_;--i)
#define tor(i,v,x) for(int i=head[x],v=to[i];i;i=nxt[i],v=to[i])
#define Li inline
#define R register int
#define ls(x) ch[x][0]
#define rs(x) ch[x][1]
#define mpi(x,y) make_pair(x,y)
#define pi pair<int,int>
#define int ll
using namespace std;
typedef long long ll;
template<typename T>
void read(T &x){
x=0;char ch=getchar();bool f=0;
while(ch<'0'||ch>'9'){
if(ch=='-')f=1;
ch=getchar();
}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x=f?-x:x;
}
const int N=2e5+7;
int n,m;
int a[N];
int now;
bool cmp(int x,int y){
return x>y;
}
signed main(){
read(n);fs(i,1,n){
int x,y;read(x);read(y);
a[i]=x*2+y;now+=x;
}
sort(a+1,a+n+1,cmp);
fs(i,1,n){
now-=a[i];if(now<0){
cout<<i;return 0;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int N, M;
int a[MAXN][2];
vector <pair <int, int>> adj[MAXN];
int bio[MAXN], ans[MAXN];
int c[MAXN];
void load() {
scanf("%d%d", &N, &M);
for (int i = 0; i < M; i++)
for (int j = 0; j < 2; j++)
scanf("%d", a[i] + j);
for (int i = 1; i <= N; i++)
scanf("%d", c + i);
}
void dfs(int x, int p) {
bio[x] = 1;
for (auto it : adj[x]) {
int nxt = a[it.first][it.second];
if (nxt == p || bio[nxt] == 2)
continue;
ans[it.first] = it.second;
if (!bio[nxt])
dfs(nxt, x);
}
bio[x] = 2;
}
void solve() {
for (int i = 0; i < M; i++)
if (c[a[i][0]] != c[a[i][1]])
ans[i] = c[a[i][0]] > c[a[i][1]];
else
for (int j = 0; j < 2; j++)
adj[a[i][j]].push_back({i, j ^ 1});
for (int i = 1; i <= N; i++)
if (!bio[i])
dfs(i, 0);
for (int i = 0; i < M; i++)
printf("%s\n", ans[i] ? "->" : "<-");
}
int main() {
load();
solve();
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;
const int N = 1e3 + 5, inf = 1e9 + 7;
int n, m;
vector <pair <int, char>> adj[N];
int ck[N][N], dp[N][N];
queue <pii> qu;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen(".inp", "r", stdin);
// freopen(".out", "w", stdout);
cin >> n >> m;
ForE(i, 1, m){
int u, v; char c; cin >> u >> v >> c;
adj[u].emplace_back(v, c);
adj[v].emplace_back(u, c);
}
memset(dp, 0x3f, sizeof(dp));
ForE(i, 1, n){
dp[i][i] = 0;
qu.push(make_pair(i, i));
}
ForE(u, 1, n){
Fora(&edge, adj[u]){
int v = edge.fi, w = edge.se;
if (u >= v){
continue;
}
dp[u][v] = 1;
qu.push(make_pair(u, v));
}
}
while (!qu.empty()){
int u = qu.front().fi, v = qu.front().se; qu.pop();
if (ck[u][v]){
continue;
}
ck[u][v] = 1;
Fora(&edgeu, adj[u]){
Fora(&edgev, adj[v]){
int uv = edgeu.fi, uw = edgeu.se, vv = edgev.fi, vw = edgev.se;
if (uw != vw){
continue;
}
if (uv > vv){
swap(uv, vv);
}
if (dp[uv][vv] > dp[u][v] + 2){
dp[uv][vv] = dp[u][v] + 2;
qu.push(make_pair(uv, vv));
}
}
}
}
if (dp[1][n] == dp[N - 1][N - 1]){
cout << -1 << endl;
}
else{
cout << dp[1][n] << endl;
}
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/ | #pragma GCC optimize("Ofast")
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define mod (int)(1e9+7)
#define mod2 998244353
#define PI 3.1415926535897932384626433832795
void solve() {
int n; cin>>n;
string s,t; cin>>s>>t;
int x=0,y=0;
for(char ch:s)
x+=(ch=='0');
for(char ch:t)
y+=(ch=='0');
if(x!=y){
cout<<"-1\n";return;
}
int ans=x;
int prefs=0,preft=0;
for(int i=0;i<n;i++){
prefs+=(s[i]=='0');
preft+=(t[i]=='0');
if(s[i]==t[i]&&s[i]=='0'&&prefs==preft)ans--;
}
cout<<ans;
}
int32_t 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=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define LL long long
//#define MOD 1000000007
#define MOD 998244353
#define INF 1000000000000000000
#define VE vector
#define VL vector<LL>
#define VVL VE<VL>
#define VVVL VE<VVL>
#define LD long double
#define PB push_back
#define POB pop_back
#define FOR(i,a,k) for(LL i=a;i<k;i++)
#define rep(i,k) FOR(i,0,k)
#define ALL(a) a.begin(),a.end()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define coutl cout<<fixed<<setprecision(15)//
#define MI modint<MOD>
template<int mod>struct modint{
int x;
modint():x(0){}
modint(LL n):x(n>=0?n%mod:(mod-(-n)%mod)%mod){}
modint &operator+=(const modint &n){if((x+=n.x)>=mod)x-=mod;return *this;}
modint &operator-=(const modint &n){if((x+=mod-n.x)>=mod)x-=mod;return *this;}
modint &operator++(){*this+=1;return *this;}
modint &operator--(){*this-=1;return *this;}
modint &operator*=(const modint &n){x=(int)((LL)x*n.x%mod);return *this;}
modint &operator/=(const modint &n){*this*=n.inv();return *this;}
modint operator-()const{return modint(-x);}
modint operator+(const modint &n)const{return modint(*this)+=n;}
modint operator-(const modint &n)const{return modint(*this)-=n;}
modint operator++(int){modint ret(*this);*this+=1;return ret;}
modint operator--(int){modint ret(*this);*this-=1;return ret;}
modint operator*(const modint &n)const{return modint(*this)*=n;}
modint operator/(const modint &n)const{return modint(*this)/=n;}
bool operator<(const modint &n)const{return x<n.x;}
bool operator>(const modint &n)const{return x>n.x;}
bool operator<=(const modint &n)const{return x<=n.x;}
bool operator>=(const modint &n)const{return x>=n.x;}
bool operator!=(const modint &n)const{return x!=n.x;}
bool operator==(const modint &n)const{return x==n.x;}
friend istream &operator>>(istream &is,modint &n){LL l;is>>l;n=modint<mod>(l);return is;}
friend ostream &operator<<(ostream &os,const modint &n){return os<<n.x;}
int getmod(){return mod;}
modint inv()const{int a=x,b=mod,c=1,d=0,n;while(b){n=a/b;swap(a-=n*b,b);swap(c-=n*d,d);}return modint(c);}
modint pow(LL n)const{modint ret(1),m(x);while(n){if(n&1)ret*=m;m*=m;n>>=1;}return ret;}
};
VL yakusuu(LL N){
LL M=0;
VL ret;
for(LL i=0; i<N; i++){
M++;
if(M>N/M){
for(LL i=0; i<N; i++){
M--;
if(M==0){
return ret;
}
if(M*M==N){
continue;
}
if(N%M==0){
ret.PB(N/M);
}
}
}
if(N%M==0){
ret.PB(M);
}
}
return ret;
}
LL nanjo(LL a,LL n){//aのn乗 N=nanjo(a,n)A
LL ret=1;
for(LL i=0;i<n;i++){
ret*=a;
}
return ret;
}
void YesNo(bool f){
if(f)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
LL kakuketa(LL n){//各桁の和を求める
LL a=0;
while(n>0){
a=a+n%10;
n=n/10;
}
return a;
}
int main(){
LL X,Y,Z;
cin>>X>>Y>>Z;
LL ans=Y*Z/X;
if(ans*X==Y*Z){
ans--;
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<int,int,int>
using Graph = vector<vector<int>>;
int main(){
int N; cin >> N;
string S; cin >> S; int a[N+3];
rep(i,N+1){
cin >> a[i+1];
}
//a[0] = 0; a[N+2] = 0;
int k = 2000000000;
rep(i,N){
k = min(k,abs(a[i+1]-a[i+2]));
}
int c[N+1],d[N+1];
rep(i,N+1){
c[i] = a[i+1]/k; d[i] = a[i+1]%k;
}
cout << k << endl;
rep(i,k){
rep(j,N+1){
if(i<d[j]) cout << c[j]+1 << " ";
else cout << c[j] << " ";
}
cout << endl;
}
} |
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}
template<typename T>void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++){for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
template<typename T>void rearrange(vector<ll>&ord, vector<T>&v){
auto tmp = v;
for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];
}
template<typename Head, typename... Tail>void rearrange(vector<ll>&ord,Head&& head, Tail&&... tail){
rearrange(ord, head);
rearrange(ord, tail...);
}
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int popcount(ll x){return __builtin_popcountll(x);};
int poplow(ll x){return __builtin_ctzll(x);};
int pophigh(ll x){return 63 - __builtin_clzll(x);};
vector<ll> bfs(vector<vector<ll>>&g,ll k=0){
vector<ll>dist(g.size(),INF);
queue<P> q;
ll n=g.size();
q.emplace(k,0);
while(!q.empty()){
ll x=q.front().fi;
ll len=q.front().se;
if(dist[x]==INF){
dist[x]=len;
for(ll i=0;i<g[x].size();i++){
q.emplace(g[x][i],len+1);
}
}
q.pop();
}
return dist;
}
void check(vector<vector<ll>>g){
ll n=g.size();
ll mx=0;
rep(i,0,n){
auto d=bfs(g,i);
rep(j,0,n)chmax(mx,d[j]);
}
cout<<mx<<endl;
}
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll res=0,buf=0;
bool judge = true;
ll n;cin>>n;
vector<P>v(n);
vector<vector<ll>>g(n);
rep(i,1,n+1){
ll l=(2*(i-1))%n+1;
ll r=(2*(i-1)+1)%n+1;
cout<<l spa r<<endl;
g[i-1].PB(l-1);
g[i-1].PB(r-1);
}
//check(g);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define INF 1234567890
#define ll long long
int N;
vector<int> g[2020];
int dist[2020];
int main()
{
scanf("%d", &N);
for(int i=0; i<N/2; i++)
{
g[i].push_back(i*2);
g[i].push_back(i*2+1);
}
for(int i=N/2; i<N; i++)
{
g[i].push_back((i*2)%N);
g[i].push_back((i*2+1)%N);
}
// int mx = 0;
// for(int i=0; i<N; i++)
// {
// memset(dist, 0x3f, sizeof(dist));
// queue<int> q;
// q.push(i); dist[i] = 0;
// while(!q.empty())
// {
// int n = q.front(); q.pop();
// for(int next : g[n])
// {
// if (dist[next] != 0x3f3f3f3f) continue;
// q.push(next); dist[next] = dist[n] + 1;
// }
// }
// for(int j=0; j<N; j++)
// {
// //if (dist[j] > 10) printf("D[%d][%d] = %d\n", i, j, dist[j]);
// mx = max(mx, dist[j]);
// }
// }
// printf("mx : %d\n", mx);
for(int i=0; i<N; i++)
printf("%d %d\n", g[i][0]+1, g[i][1]+1);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define P pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
int main(){
ll n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
vector<bool> surplus(200, false);
vector<vector<ll>> sup_info(200);
ll cnt = min(n, (ll)8);
ll mod = 200;
drep(bit, 1, ((1<<cnt)-1)){
ll tmp = 0;
ll count = 0;
rep(i, n) if((bit>>i) & 1){
count++;
tmp += a[i];
tmp%=mod;
}
if(surplus[tmp] == false){
surplus[tmp] = true;
rep(i, n) if((bit>>i) & 1) sup_info[tmp].pb(i);
}else{
cout << "Yes" << endl;
ll size = sup_info[tmp].size();
cout << size;
rep(i, size) cout << " " << sup_info[tmp][i]+1;
cout << endl;
cout << count;
rep(i, n) if((bit>>i) & 1) cout << " " << i+1;
cout << endl;
return 0;
}
}
cout << "No" << endl;
} | #include<bits/stdc++.h>
#define inf 999999999
using namespace std;
const int maxn=1e5+10;
int n,sz[maxn],h[maxn],tot,f[maxn];
struct edge{int t,n;}e[maxn<<1];
void ins(int u,int v){e[++tot]=edge{v,h[u]};h[u]=tot;}
void dfs(int v,int fa)
{
int sum=0;
vector<int>seq;
for(int i=h[v];i;i=e[i].n)
{
if(e[i].t==fa)continue;
dfs(e[i].t,v);
sz[v]+=sz[e[i].t];
if(sz[e[i].t]%2)
{
seq.push_back(-f[e[i].t]);
}
else
{
if(f[e[i].t]<0)f[v]+=f[e[i].t];
else sum-=f[e[i].t];
}
}
sort(seq.begin(),seq.end(),greater<int>{});
seq.push_back(sum);
for(int i=0;i<(int)seq.size();i++)
{
if(i%2) f[v]+=seq[i];
else f[v]-=seq[i];
}
};
signed main()
{
cin>>n;
for(int i=2,t;i<=n;i++)
{
cin>>t;ins(i,t);ins(t,i);
}
for(int i=1;i<=n;i++)f[i]=sz[i]=1;
dfs(1,0);
cout<<(f[1]+n)/2<<endl;
return 0;
} |
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int A[n];
int B[n];
for (int i = 0; i < n; i++)
{
cin >> A[i] >> B[i];
}
int ans = 1000000;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j)
{
ans = min(ans, A[i] + B[j]);
}
else
{
ans = min(ans, max(A[i], B[j]));
}
}
}
cout << ans << "\n";
} | #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
int N;
int A[1011];
int B[1011];
void MAIN() {
scanf("%d", &N);
REP (i, N) scanf("%d%d", A+i, B+i);
int k = min_element(A, A+N) - A;
int ans = 1<<29;
REP (i, N) if (i != k) {
amin(ans, max(A[k], B[i]));
}
k = min_element(B, B+N) - B;
REP (i, N) if (i != k) {
amin(ans, max(A[i], B[k]));
}
REP (i, N) {
amin(ans, A[i] + B[i]);
}
printf("%d\n", ans);
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
set<char> s;
for(char c : s1) s.insert(c);
for(char c : s2) s.insert(c);
for(char c : s3) s.insert(c);
if(s.size() > 10) {
cout << "UNSOLVABLE" << endl;
return 0;
}
vector<int> ord(10);
iota(ord.begin(), ord.end(), 0);
auto len = [&](ll x) -> int {
return log10(x) + 1;
};
do {
vector<int> mp(26);
int it = 0;
for(char c : s) {
mp[c - 'a'] = ord[it];
++it;
}
ll a1 = 0, a2 = 0, a3 = 0;
for(char c : s1) a1 = a1 * 10 + mp[c - 'a'];
for(char c : s2) a2 = a2 * 10 + mp[c - 'a'];
for(char c : s3) a3 = a3 * 10 + mp[c - 'a'];
if(a1 + a2 == a3 && a1 && a2 && a3 && len(a1) == s1.size() && len(a2) == s2.size() && len(a3) == s3.size()) {
cout << a1 << endl;
cout << a2 << endl;
cout << a3 << endl;
exit(0);
}
}while(next_permutation(ord.begin(), ord.end()));
cout << "UNSOLVABLE" << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
#define F first
#define S second
#define YES(n)cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl
int main(){
vector<string> s(3);
map<char,ll> mp;
set<char> heads;
rep(i,3){
cin>>s[i];
heads.insert(s[i][0]);
reverse(all(s[i]));
ll co=1;
if(i==2)co=-1;
for(char c:s[i]){
mp[c]+=co;
co*=10;
}
reverse(all(s[i]));
}
if(mp.size()>10){
cout<<"UNSOLVABLE"<<endl;
return 0;
}
vector<int> p(10);
iota(all(p),0);
do{
ll tot=0;
int i=0;
for(auto x:mp){
char c=x.first;
ll co=x.second;
if(p[i]==0&&heads.count(c)) tot=1e18;
tot+=co*p[i];
i++;
}
if(tot==0){
i = 0;
vector<string> ans(3);
for(auto& x : mp){
x.second=p[i];
++i;
}
rep(i,3){
ll x=0;
for(char c:s[i]){
x=x*10+mp[c];
}
cout<<x<<endl;
}
return 0;
}
}while(next_permutation(all(p)));
cout<<"UNSOLVABLE"<<endl;
return 0;
} |
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define LL long long
#define int long long
#define ull unsigned long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<int, int>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)
constexpr int N = 1e6+9;
constexpr int M = 1e9+7;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif
signed main() {
fastio;
int n;
cin >> n;
vector<int> fa(n, -1);
vector<vector<int>> ch(n);
for (int i = 1; i < n; i++) {
cin >> fa[i];
fa[i]--;
ch[fa[i]].emplace_back(i);
}
vector<int> sz(n, 1);
// sz*2 + sz-1
vector<int> dp(n, 0);
function<void(int)> dfs = [&](int u) {
dp[u] = 1;
array<vector<pii>, 2> sub;
for (int v: ch[u]) {
dfs(v);
sz[u] += sz[v];
sub[sz[v]&1].emplace_back(dp[v], sz[v]-dp[v]);
}
sort(sub[0].begin(), sub[0].end(), [](const pii& p1, const pii& p2) {
return p1.fi-p1.se > p2.fi-p2.se;
});
sort(sub[1].begin(), sub[1].end(), [](const pii& p1, const pii& p2) {
return p1.fi-p1.se < p2.fi-p2.se;
});
while (!sub[0].empty() && sub[0].back().fi <= sub[0].back().se) {
dp[u] += sub[0].back().fi;
sub[0].pop_back();
}
while (!sub[0].empty()) {
if (SZ(sub[1]) & 1)
dp[u] += sub[0].back().se;
else
dp[u] += sub[0].back().fi;
sub[0].pop_back();
}
for (int i = 0; i < SZ(sub[1]); i++) {
if (i & 1) dp[u] += sub[1][i].se;
else dp[u] += sub[1][i].fi;
}
};
dfs(0);
cout << dp[0] << "\n";
return 0;
} | #pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
preprocess() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} ____;
#pragma endregion head
#pragma region library
#pragma endregion library
string inv(const string &s) {
string res = "";
rep(i, s.size()) {
if (s[i] == 'A') {
res += 'B';
} else {
res += 'A';
}
}
return res;
}
int main() {
int n;
cin >> n;
vector<string> ans = {"AB"};
rep(i, n - 1) {
vector<string> nxt;
rep(i, ans.size()) {
nxt.emplace_back(ans[i] + ans[i]);
nxt.emplace_back(ans[i] + inv(ans[i]));
}
nxt.emplace_back(string(ans[i].size(), 'A') + string(ans[i].size(), 'B'));
swap(nxt, ans);
}
cout << ans.size() << '\n';
rep(i, ans.size()) {
cout << ans[i] << '\n';
}
} |
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(Array) sort(Array.begin(), Array.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define out(ans) cout << ans << endl;
int MOD = 1000000007;
const int INF = 2147483647;
const ld PI = 3.14159265358979323846;
int main() {
string S; cin >> S;
int k = S.size();
int ans = INF;
for (int bit = 0; bit < (1<<k); ++bit) {
int sum = 0;
int ans_tmp = 0;
for (int i=0; i < k; ++i) {
if (bit & (1<<i)) {
ans_tmp++;
} else {
sum += S[i] - '0';
}
}
if (sum > 0 && sum % 3 == 0) ans = min(ans, ans_tmp);
}
if (ans == INF) ans = -1;
out(ans);
return 0;
}
| #include <iostream>
#include <vector>
using namespace std;
int n, ans = -1;
vector<int> nums, lst;
void dfs(int pos, int s) {
if (pos == n) {
if (lst.size() > 0 && s % 3 == 0) ans = max(ans, (int)lst.size());
return ;
}
lst.push_back(nums[pos]);
dfs(pos + 1, s + nums[pos]);
lst.pop_back();
dfs(pos + 1, s);
}
int main() {
long long tmp;
cin >> tmp;
while (tmp) {
nums.push_back(tmp % 10);
tmp /= 10;
}
n = nums.size();
dfs(0, 0);
if (ans == -1) cout << -1 << endl;
else cout << (int)nums.size() - ans << endl;
return 0;
} |
#include <iostream>
using namespace std;
typedef long long ll;
int main(){
string S;
ll a;
scanf("%lld",&a);
cin >> S;
for (ll i=0; i<a-2; i++) {
if (S[i]=='f' && S[i+1]=='o' && S[i+2]=='x') {
S.erase(i, 3);
i-=3;
}
}
cout << S.size() << endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#include<string.h>
#define pi 3.1415926535897932384626433832795028841971
#define fastttt std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
#define str_max_len 4294967295
#define mod 1000000007
#define max_ll LLONG_MAX
#define min_ll LLONG_MIN
#define cps CLOCKS_PER_SEC
#define pb push_back
#define fi first
#define se second
#define in insert
#define m_p make_pair
#define p_q priority_queue
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define vii vector <pair<ll,ll>>
#define pii pair<ll,ll>
#define vi vector <ll>
ll ll_max(ll a,ll b,ll c){return max(a,max(b,c));}
int int_max(int a,int b,int c){return max(a,max(b,c));}
ll ll_min(ll a,ll b,ll c){return min(a,min(b,c));}
int int_min(int a,int b,int c){return min(a,min(b,c));}
ll max(int a,ll b){ return max((ll)a,b);}
ll min(int a,ll b){ return min((ll)a,b);}
ll min(ll a,int b){ return min(a,(ll)b);}
ll max(ll a,int b){ return max(a,(ll)b);}
ll maximum(ll a[],ll n)
{
ll max1=min_ll;
fo(i,n)
{
max1=max(max1,a[i]);
}
return max1;
}
ll minimum(ll a[],ll n)
{
ll min1=max_ll;
fo(i,n)
{
min1=min(min1,a[i]);
}
return min1;
}
ll power(ll x,ll y,ll m){ //power(x,y)=x^y %m;
if(y==0)return 1;
ll a=power(x,y/2,m);
if(y%2){
return (a*((a*x)%m))%m;
}
else{
return (a*a)%m;
}
}
ll mod_inverse(ll x,ll m){
return power(x,m-2,m);
}
ll fact(ll n,ll m){
if(n<=1)return 1;
return (fact(n-1,m)*n)%m;
}
ll ncr(ll n,ll r,ll m){
if(r>n)return 0;
ll n1=1,d1=1,d2=1;
n1=fact(n,m);d1=fact(r,m);d2=fact(n-r,m);
ll ans=mod_inverse((d1*d2)%m,m);
ans=(ans*n1)%m;
return ans;
}
ll gcd(ll a, ll b){
if(a==0||b==0)return max(a,b);
if (a < b)return gcd(b, a);
if (a % b == 0) return b;
return gcd(b,a % b);}
ll ispal(string s){
ll len=s.size();
ll flag=1;
for(ll i=0;i<len;++i){
if(s[i]!=s[len-i-1]){
flag=0;break;
}
}
return flag;
}
bool sortbysec(const pair<ll,ll> &a,
const pair<ll,ll> &b)
{
return (a.second < b.second);
}
/***************************code begins here*****************************/
class solver{
public:
void solve()
{
ll n;cin>>n;
string s;cin>>s;ll ans=0;
stack <char> st;
if(n<=2)
{
cout<<n;return;
}
fo(i,n)
{
st.push(s[i]);
//cout<<st.size()<<" ";
if(st.size()>=3)
{
char a=st.top();st.pop();
char b=st.top();st.pop();
char c=st.top();st.pop();
if(a=='x' && b=='o'&& c=='f')
{
n++;n--;
}
else
{
st.push(c);
st.push(b);
st.push(a);
}
}
}
cout<<st.size();
}
};
int main(){
fastttt
// cout<<fixed<<setprecision(10);
int test=1,c=1;
// cin>>test;
while(test--){
//cout<<"Case #"<<c<<": ";c++;
solver o;
o.solve();
}
} |
#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 endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}
void solve() {
int N;
cin >> N;
vector<ll> A(N), T(N);
for (int i = 0; i < N; i++) {
cin >> A[i] >> T[i];
}
ll inf = 4e18;
ll lo = -inf, hi = inf;
ll sum = 0;
for (int i = 0; i < N; i++) {
if (T[i] == 1) {
sum += A[i];
lo += A[i];
hi += A[i];
}
if (T[i] == 2) {
lo = max(lo, A[i]);
hi = max(lo, hi);
}
if (T[i] == 3) {
hi = min(hi, A[i]);
lo = min(lo, hi);
}
}
ll ok_lo = lo - sum;
ll ok_hi = hi - sum;
int Q;
cin >> Q;
for (int i = 0; i < Q; i++) {
ll x;
cin >> x;
if (x > ok_hi) {
cout << hi << endl;
} else if (x < ok_lo) {
cout << lo << endl;
} else {
cout << x + sum << endl;
}
}
}
int main() {
#ifdef LOCAL_ENV
cin.exceptions(ios::failbit);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
solve();
} | #include<bits/stdc++.h>
using namespace std;
int n,a[105][105],b[105][105];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
b[i][i]=1;
for(int j=1;j<=n;j++)
if(s[j-1]=='1')
a[i][j]=b[j][i]=1;
}
for(int t=1;t<=n;t++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
b[i][j]=b[i][j]|(b[i][t]&b[t][j]);
double ans=0;
for(int i=1;i<=n;i++)
{
int cnt=0;
for(int j=1;j<=n;j++)
if(b[i][j]) cnt++;
ans+=1.0/cnt;
}
printf("%.10lf",ans);
}
|
#include "bits/stdc++.h"
using namespace std;
#define int long long int
int32_t main()
{
int n,m;
cin>>n>>m;
vector<int> a(n+1),b(m+1);
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=m;i++)
{
cin>>b[i];
}
vector<vector<int> > dp(n+1,vector<int>(m+1));
for(int i=1;i<=m;i++)
{
dp[0][i]=i;
}
for(int i=1;i<=n;i++)
{
dp[i][0]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
dp[i][j]=min({dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+((a[i]!=b[j])?1:0)});
}
}
cout<<dp[n][m]<<endl;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(),_obj.end()
#define F first
#define S second
#define pll pair<ll, ll>
#define vll vector<ll>
ll n,m,a,b,c,k,temp,x,y,INF=1e18;
const int MAXN=1e5+11,mod=1e9+7;
ll max(ll a,ll b) {return ((a>b)?a:b);}
ll min(ll a,ll b) {return ((a>b)?b:a);}
vll read(int n) {vll v(n);for (int i = 0; i < v.size(); i++)cin>>v[i];return v;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dp[1001][1001];
void sol(void)
{
cin>>n>>m;
vll v1=read(n),v2=read(m);
for(int i=0;i<=1000;i++)
for(int j=0;j<=1000;j++)
dp[i][j]=1e9;
for(int i=0;i<=1000;i++)
dp[i][m]=n+m,dp[n][i]=n+m;
for(int i=n-1;i>=0;i--)
{
for(int j=m-1;j>=0;j--)
{
if(v1[i]==v2[j])
dp[i][j]=min(dp[i][j],dp[i+1][j+1]-2);
dp[i][j]=min(dp[i][j],dp[i+1][j+1]-1);
dp[i][j]=min(dp[i][j],dp[i+1][j]);
dp[i][j]=min(dp[i][j],dp[i][j+1]);
}
}
cout<<dp[0][0];
return ;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
int test=1;
//cin>>test;
while(test--) sol();
}
|
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) cout<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define pw(b,p) pow(b,p) + 0.1
#define endl "\n"
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int32_t x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
char myHeap[200 * 1024 * 1024];
int sz = 0;
void assert_tle(bool q) { while (!q); }
void* operator new ( std::size_t count ) {
sz += count;
assert_tle(sz <= 200 * 1024 * 1024);
return myHeap + sz - count;
}
void operator delete (void *ptr) { }
void fastIO()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
struct djsets
{
vector<int> leader;
vector<int> rank;
void init(int n)
{
rank.assign(n,1);
for(int i=0;i<n;i++)
{
leader.push_back(i);
}
}
int retleader(int node)
{
if(leader[node]==node)
return node;
int p= retleader(leader[node]);
leader[node]=p;
return p;
}
void connect(int a,int b)
{
a=retleader(a);
b=retleader(b);
if(rank[a]>rank[b])
{
leader[b]=a;
}
else if(rank[b]>rank[a])
{
leader[a]=b;
}
else
{
leader[b]=a;
rank[a]++;
}
}
int retans()
{
int ans=0;
for(int i=0;i<leader.size();i++)
if(leader[i]==i&&rank[i]>1) ans++;
return ans-1;
}
};
void solve()
{
int h,w;
cin>>h>>w;
string str[h];
for(int i=0;i<h;i++)
{
cin>>str[i];
}
str[0][0]=str[h-1][0]=str[h-1][w-1]=str[0][w-1]='#';
djsets d;
d.init(h+w);
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
if(str[i][j]=='#')
{
d.connect(i,h+j);
}
}
}
int rows=0,col=0;
for(int i=0;i<h;i++)
{
if(d.rank[d.retleader(i)]==1) rows++;
}
for(int i=h;i<h+w;i++)
{
if(d.rank[d.retleader(i)]==1) col++;
}
cout<<d.retans()+min(rows,col);
}
int32_t main()
{
fastIO();
//w(t)
{
solve();
cout<<endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <=b ; ++i)
#define per(i, b, a) for(int i = b; i >= a; --i)
#define ll long long
#define pb push_back
using namespace std;
inline void rd(int &x){scanf("%d", &x);}
inline void rd(ll &x){scanf("%lld", &x);}
int n, m, k;
const int maxn = 1145;
int par[maxn << 1];
int findroot(int u)
{
return par[u] == u ? par[u] : (par[u] = findroot(par[u]));
}
void merge(int u, int v)
{
if(findroot(u) == findroot(v)) return;
par[findroot(u)] = v;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
rep(i, 1, n + m) par[i] = i;
rep(i, 1, n)
{
string s;
cin >> s;
rep(j, 1, m) if(s[j - 1] == '#') merge(i, n + j);
}
merge(1, n + 1), merge(1, n + m), merge(n, n + 1), merge(n, n + m);
int ans = 0;
unordered_set<int> r, c;
rep(i, 1, n) r.insert(findroot(i));
rep(j, 1, m) c.insert(findroot(j + n));
cout << min(r.size(), c.size()) - 1;
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
int main()
{
int v, t, s, d;
cin >> v >> t >> s >> d;
if (t * v <= d && d <= s * v)
{
cout << "No" << endl;
}
else
{
cout << "Yes" << endl;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define get(a) ll a ; cin >> a ;
#define show(a) cout << a << endl;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
get(v);
get(t);
get(s);
get(d);
float time = d/float(v);
if ( time < t or time > s)
cout << "Yes"<< endl;
else
cout<< "No" << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD=998244353;
LL n,a[105],sum,p[105],fac[105],dp[105][10005];
int main(){
scanf("%lld",&n);
for(LL i=1;i<=n;++i) scanf("%lld",&a[i]),sum+=a[i];
if(sum&1) return puts("0")&0;
for(LL i=1;i<=n;++i) ++p[a[i]];
fac[0]=1;
for(LL i=1;i<=100;++i) fac[i]=fac[i-1]*i%MOD;
dp[0][0]=1;
for(LL i=1;i<=n;++i) for(LL j=i-1;~j;--j) for(LL k=0;k<=sum;++k) (dp[j+1][k+a[i]]+=dp[j][k])%=MOD;
LL ans=dp[n][0];
for(LL i=1;i<=n;++i) (ans+=fac[n-i]*fac[i]%MOD*dp[i][sum>>1]%MOD)%=MOD;
printf("%lld",ans);
return 0;
} | //Date -> 27-06-2021
//Atcoder Grand Contest
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define all(x) (x).begin(),(x).end()
#define rep(i,a,b) for(ll i = a; i<b ; i++)
#define sz(x) (int)((x).size())
#define PI 3.141592653589
#define mod 998244353//1000000007
#define MAX 1000005 //1e6+5
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
long long const INF = 1e6;
ll power(ll a, ll b)
{ ll result =1;
while(b)
{ if(b%2) result=(result*a)%mod;
a=(a*a)%mod;
b/=2;}
return result;
}
/*
Let X = T(Takahashi's Score) — A(Aoki's score)
Initially, X = 0 The process is such that, at the first step, some w[i] will be added to X, then for the next
few steps 2,3,4... etc some w[i]'s shall be subtracted and finally the sum becomes 0, at the last step.
So, there are some k elements which are added to X, and some n — k elements which are subtracted from X,
so that X remains 0 finally, after the process.
Let us say we have already chosen these elements: We have some k elements if the W array,
whose sum is exactly equal to the remaining n — k elements.
The k elements will give positive contribution to X, and n — k will give negative contribution.
Once the elements are chosen, at the first step, the element kept shall be one of the k elements
(as first step is a positive contribution.Then, the next element must be from the
other group (with n — k elements), it can be chosen in (n — k) ways, if the sum is still > 0,
then again some element will be chosen from the 2nd (negative contribution group) group,
this time with (n — k — 1) possibilities... next maybe (n — k — 2) possibilities and so on,
until X becomes < 0, now out of the (k-1) elements of the first group, some will be chosen
(of course in k-1 ways..)
Basically, the above should kind of show that once k elements
with "positive role" and n — k with "negative role" are chosen,
then we can take them individually in any order.
I mean the elements of a particular group can be permuted with each other. So, (n — k)! * k! ways are there.*/
int n;
int a[102];
ll fact[104];
void sol()
{
cin>>n;
int sum =0;
rep(i,0,n) {cin>>a[i]; sum+=a[i];}
fact[0]=1;
for(int i=1;i<102;i++)
{
fact[i]=fact[i-1]*i;
fact[i]%=mod;
}
if(sum%2 != 0) {cout<<"0"; return;}
int req = sum/2;
ll dp[n+1][req+1][n+1]; //dp[n][x][k] is num of way making sum x using exactly k elemnts
memset(dp,0,sizeof(dp));
for(int i=0;i<=n;i++) dp[i][0][0] = 1; //making sum 0 with 0 elemnt
for(int i=1;i<=n;i++)
{
for(int j=0;j<=req;j++)
{
for(int k=1;k<=i;k++) //k<=i bcz we never get len more than i by using elemnt upto idx i-1
{
dp[i][j][k] = dp[i-1][j][k];
if(j-a[i-1]>=0) {dp[i][j][k] += dp[i-1][j-a[i-1]][k-1]; dp[i][j][k]%=mod;}
}
}
}
ll ans = 0;
for(int k=1;k<=n-1;k++)
{
ll curr = ((fact[k]*fact[n-k])%mod)*dp[n][req][k];
curr%=mod;
ans = ans + curr;
ans%=mod;
}
cout<<ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int T=1; //cin>>T;
while(T--) sol();
return 0;
} |
#include<cstdio>
#include<queue>
#define LL long long
using namespace std;
int read() {
int x=0,f=1;char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
priority_queue<LL>q;
const int N=4e5+5;
int n;
LL v[N],ans;
main() {
n=read();
for(int i=1;i<=2*n;++i)v[i]=read(),ans+=v[i];
for(int i=1,j=n,k=n+1;i<=n;++i) {
for(;j>=n-i+1;--j)q.push(-v[j]);
for(;k<=i+n;++k)q.push(-v[k]);
ans+=q.top();q.pop();
}
printf("%lld",ans);
return 0;
} | #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int n; cin >> n;
vector<int> a(2 * n);
rep(i, 2 * n)cin >> a[i];
multiset<int> st;
rep(i, n) {
st.insert(a[n - 1 - i]);
st.insert(a[n + i]);
st.erase(st.begin());
}
ll ans = 0;
for (int val : st)ans += val;
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//int t; cin >> t;rep(i,t)
solve();
return 0;
} |
// CODING BEAST
/*******************************************************************************************************************************************************/
#include<bits/stdc++.h>
using namespace std;
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define Precision(a) cout << fixed << setprecision(a)
using ll = long long;
ll inf = 1e18;
using vb = vector<bool>; using vc = vector<char>; using vd = vector<double>; using vi = vector<int>; using vvi = vector<vi>;
using vll = vector<ll>; using vvll = vector<vll>; using pi = pair<int, int>; using pll = pair<ll, ll>;
#define endl "\n"
#define pb push_back
#define mp make_pair
#define rep(i,n) for(ll i = 0; i < n; i++)
#define repc(i,a,n) for(ll i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define pow2(x) (1ll<<(x))
#define Blazing_fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define trav(a,v) for(auto &a:v)
// int a[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
// int b[8] = { -1, 0, 1, -1, 1, -1, 0, 1};
ll mod = 1e9;
// ll modx(ll a, ll b) {
// return (((a % b) + b) % b);
// }
// inline ll power(ll x, ll y)
// {
// ll temp;
// if ( y == 0)
// return 1;
// temp = power(x, y / 2) ;
// if (y % 2 == 0)
// return (temp * temp) % mod ;
// else
// return (((x * temp) % mod) * temp) % mod ;
// }
// bool cmp(pll & a, pll & b) {
// if (a.first == b.first) {
// return (a.second < b.second);
// }
// else {
// return (a.first > b.first);
// }
// }
// ll fact(ll n)
// {
// ll res = 1;
// for (ll i = 2; i <= n; i++)
// res = (res * i) % mod ;
// return res;
// }
// ll inv(ll a) {
// return power(a, mod - 2);
// }
// ll ncr(ll n, ll r)
// {
// ll x = fact(n);
// ll y = ((inv(fact(r)) % mod) * (inv(fact(n - r)))) % mod;
// return ((x % mod) * y) % mod;
// }
// bool isprime(ll n) {
// if (n <= 1) return false;
// if (n <= 3) return true;
// if ((n % 2 == 0) || (n % 3 == 0)) return false;
// for (ll i = 5; i * i <= n; i = i + 6)
// if ((n % i == 0) || (n % (i + 2) == 0)) return false;
// return true;
// }
/*********************************************************************************************************************************************************/
//whenever given equality try to rewrite it
//matrix remember about even odd or modularity
void solve() {
ll n, k;
cin >> n >> k;
ll ans = 0;
for (ll i = 1; i <= n; i++) {
ll p = i * 100;
for (ll j = 1; j <= k; j++) {
ans += p + j;
}
}
cout << ans << endl;
}
signed main() {
Blazing_fast
ll t = 1;
// ll t; cin >> t;
while (t--) {
solve();
}
}
| #include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const int MOD = 998244353;
const long double PI = 3.141592653589793238462643383279502884197;
ll fac[51] = {1}, inv[1] = {1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}
ll cmb(ll r, ll c) {return (c>r||c<0) ? 0:fac[r] * inv[c] % MOD * inv[r - c] % MOD;}
int dp[3001][3001];
int go(int n, int k) {
if (k > n) return 0;
if (k == 0) return n == 0;
if (dp[n][k] != -1) return dp[n][k];
ll ret = 0;
ret += go(n - 1, k - 1);
ret += go(n, k * 2);
return dp[n][k] = ret % MOD;
}
int main() {
memset(dp, -1, sizeof(dp));
int n, k;
scanf("%d %d", &n, &k);
printf("%d", go(n, k));
}
// author: rdd6584
|
#include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int maxn = 101, maxw = 101;
const i64 P = 998244353;
int a[maxn];
i64 dp[2][maxn][2 * maxn * maxw + 1];
i64 *pdp[2][maxn];
i64 fact[maxn];
void add(i64 &x, i64 y) {
x += y;
if (x >= P) x -= P;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "rt", stdin);
#endif
fact[0] = 1;
forn(i, maxn - 1) fact[i + 1] = fact[i] * (i + 1) % P;
forn(z, 2) forn(i, maxn) pdp[z][i] = dp[z][i] + maxn * maxw;
int n;
cin >> n;
pdp[0][0][0] = 1;
int S = 0;
forn(i, n) {
int x;
cin >> x;
forn(j, i + 1) fore(w, -S, S) {
if (!pdp[0][j][w]) continue;
add(pdp[1][j][w - x], pdp[0][j][w]);
add(pdp[1][j + 1][w + x], pdp[0][j][w]);
}
S += x;
forn(j, i + 2) fore(w, -S, S) {
pdp[0][j][w] = pdp[1][j][w];
pdp[1][j][w] = 0;
}
}
i64 ans = 0;
forn(j, n + 1) (ans += pdp[0][j][0] * fact[j] % P * fact[n - j]) %= P;
cout << ans << '\n';
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif
namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); }
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
template <class Con>
string concat(const Con &c, const string &spr){
stringstream ss;
typename Con::const_iterator it = c.begin(), en = c.end();
bool fst = true;
for(; it != en; ++it){
if(!fst){ ss << spr; }
fst = false;
ss << *it;
}
return ss.str();
}
template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
vector<typename Con::value_type> ret;
typename Con::const_iterator it = c.begin(), en = c.end();
for(; it != en; ++it){
if(f(*it)){
ret.emplace_back(*it);
}
}
return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
vector<decltype(f(*c.begin()))> ret;
ret.reserve(c.size());
for(const auto &x: c){
ret.emplace_back(f(x));
}
return ret;
}
#endif
#if __cplusplus >= 201402L
#define lambda(e) ([&](const auto &_){ return (e); })
#define lambda2(e) ([&](const auto &_a, const auto &_b){ return (e); })
#endif
#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()
#define tget(t,i) get<i>(t)
#define MOD 1000000007LL
#define EPS 1e-8
void mainmain(){
using D = double;
using C = complex<D>;
int n;
cin >> n;
D x0, y0, xh, yh;
cin >> x0 >> y0 >> xh >> yh;
C p0(x0, y0), ph(xh, yh);
D pi = acos(D(-1));
D dir0 = arg(ph - p0) - pi / 2 + pi / n;
D dir = dir0;
C p = p0;
for(int i = 0; i < n; i += 2){
p += polar(D(1), dir);
dir += 2 * pi / n;
}
D u = abs(ph - p0) / abs(p - p0);
C ans = p0 + polar(u, dir0);
cout << real(ans) << ' ' << imag(ans) << '\n';
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(4);
mainmain();
}
|
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <tuple>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include<iomanip>
using namespace std;
using ll = long long;
using ULL = unsigned long long;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vb = std::vector<bool>;
using db = double;
using vdb = std::vector<db>;
using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending
using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending
using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending
using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending
using tlll = tuple<ll, ll, ll> ;
template<class T>
using vv = std::vector<std::vector<T> >;
#define REPL(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FORL(i, 0, n)
#define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- )
#define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
#define INF pow(10, 10)
int main(void)
{
ll S, P, N, Q;
cin >>N;
vector<pll> Edges(N-1);
vv<ll> Graph(N);
vl adds(N, 0);
vl pars(N, -1);
vl sums(N, 0);
REP(i, N-1)
{
ll a, b;
cin >> a >> b;
a --; b --;
Edges[i] = {a, b};
Graph[a].push_back(b);
Graph[b].push_back(a);
}
cin >> Q;
vv<ll> Tasks(Q, vl(3) );
REP(i, Q)
{
ll t, e, x;
cin >> Tasks[i][0] >> Tasks[i][1] >> Tasks[i][2];
Tasks[i][1] --;
}
queue<ll> Que;
ll Parent_Tree = 0;
Que.push(Parent_Tree); // 0を頂点とする木
vb used(N);
while( !Que.empty() )
{
ll from = Que.front(); Que.pop();
used[from] = true;
REP(i, Graph[from].size() )
{
ll to = Graph[from][i];
if(used[to]) continue;
pars[to] = from;
Que.push(to);
}
}
REP(i, N)
{
used[i] = false;
}
REP(i, Q)
{
if(Tasks[i][0]==1)
{
ll v = Edges[ Tasks[i][1] ].first;
ll through = Edges[ Tasks[i][1] ].second;
if(pars[v]==through)
{
adds[v] += Tasks[i][2];
}
else
{
adds[Parent_Tree] += Tasks[i][2];
adds[through] -= Tasks[i][2];
}
}
else
{
ll through = Edges[ Tasks[i][1] ].first;
ll v = Edges[ Tasks[i][1] ].second;
if(pars[v]==through)
{
adds[v] += Tasks[i][2];
}
else
{
adds[Parent_Tree] += Tasks[i][2];
adds[through] -= Tasks[i][2];
}
}
}
Que.push(Parent_Tree);
while( !Que.empty() )
{
ll from = Que.front(); Que.pop();
used[from] = true;
sums[from] += adds[from];
REP(i, Graph[from].size() )
{
ll to = Graph[from][i];
// if(used[to] == true) continue;
if(pars[to] != from) continue;
adds[to] += sums[from];
Que.push(to);
}
}
REP(i, N)
{
cout << sums[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector <int> num, used, tin, tout, add, dist, num1;
vector <vector <int> > graph, up;
vector <pair <int, int> > smth;
map <int, int> kek;
int timer = 0, res = 0;
const int l = 27;
int dfs(int v, int par) {
used[v] = 1;
int size_of_now = 1;
timer++;
tin[v] = timer;
dist[v] = dist[par] + num[v];
up[v][0] = par;
for (int i = 1; i < l; i++) up[v][i] = up[up[v][i - 1]][i - 1];
for (auto to : graph[v]) {
if (!used[to]) size_of_now += dfs(to, v);
}
timer++;
tout[v] = timer;
smth.push_back({v, size_of_now});
kek[v] = n - (int)smth.size();
return size_of_now;
}
bool check(int a, int b) {
return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
int lca(int a, int b) {
if (check(a, b)) return a;
if (check(b, a)) return b;
for (int i = l - 1; i >= 0; i--) {
if (!check(up[a][i], b)) a = up[a][i];
}
return up[a][0];
}
void push(int v) {
add[v * 2] += add[v];
add[v * 2 + 1] += add[v];
add[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int k) {
if (r <= tl || l >= tr) return;
else if (l <= tl && r >= tr) add[v] += k;
else {
int tm = (tl + tr) / 2;
upd(v * 2, tl, tm, l, r, k);
upd(v * 2 + 1, tm, tr, l, r, k);
}
}
int get(int v, int tl, int tr, int pos) {
if (tl == tr - 1) {
return add[v];
}
else {
int tm = (tl + tr) / 2;
if (pos < tm) {
return add[v] + get(v * 2, tl, tm, pos);
}
else {
return add[v] + get(v * 2 + 1, tm, tr, pos);
}
}
}
void upd2(int v, int tl, int tr, int pos) {
if (tl + 1 == tr) {
add[v] = 0;
return;
}
else {
push(v);
int tm = (tl + tr) / 2;
if (pos < tm) upd2(v * 2, tl, tm, pos);
else upd2(v * 2 + 1, tm, tr, pos);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
num.resize(n);
used.resize(n);
tin.resize(n);
tout.resize(n);
graph.resize(n);
up.resize(n);
add.resize(4 * n);
dist.resize(n);
num1.resize(n);
for (int i = 0; i < n; i++) {
up[i] = vector <int> (l, 0);
}
vector <pair <int, int> > edges;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
edges.push_back({a, b});
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(0, 0);
reverse(smth.begin(), smth.end());
int sum = 0;
int q;
cin >> q;
while (q--) {
int req;
cin >> req;
int a, b;
if (req == 1) {
int x;
cin >> x;
x--;
a = edges[x].first;
b = edges[x].second;
}
else {
int x;
cin >> x;
x--;
a = edges[x].first;
b = edges[x].second;
swap(b, a);
}
int delta;
cin >> delta;
if (up[a][0] == b) {
int pos = kek[a];
int len = smth[pos].second;
upd(1, 0, n, pos, pos + len, delta);
}
else {
sum += delta;
int pos = kek[b];
int len = smth[pos].second;
upd(1, 0, n, pos, pos + len, -delta);
}
}
for (int i = 0; i < n; i++) {
int res = get(1, 0, n, kek[i]) + sum;
cout << res << "\n";
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define line cerr<<"--------------------\n";
#define see(x) cerr<<x<<" "
#define seeln(x) cerr<<x<<endl
#define out(x) cerr<<#x<<" = "<<x<<" "
#define outln(x) cerr<<#x<<" = "<<x<<endl
#define outarr(x,l,r) cerr<<#x"["<<l<<"-"<<r<<"] = "; for (int _i=l;_i<=r;++_i) cerr<<x[_i]<<" ";cerr<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define gc() getchar()
//char buf[1<<23],*p1=buf,*p2=buf;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x)
{
x=0; char c=gc(); int flag=0;
while (c<'0'||c>'9') flag|=(c=='-'),c=gc();
while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
if (flag) x=-x;
}
template <class T> inline T _max(T a,T b){return a>b ? a : b;}
template <class T> inline T _min(T a,T b){return a<b ? a : b;}
template <class T> inline bool checkmax(T &a,T b){return b>a ? a=b,1 : 0;}
template <class T> inline bool checkmin(T &a,T b){return b<a ? a=b,1 : 0;}
int n,k,mod;
int Add(int x,int y){x+=y; return x>=mod ? x-mod : x;}
int Sub(int x,int y){x-=y; return x<0 ? x+mod : x;}
int Mul(int x,int y){return (ll)x*y%mod;}
void add(int &x,int y){x=Add(x,y);}
void sub(int &x,int y){x=Sub(x,y);}
void mul(int &x,int y){x=Mul(x,y);}
int dp[102][128000];
void init()
{
read(n); read(k); read(mod);
dp[0][0]=1;
for (int i=1;i<=n;++i)
{
int tmp=_min(i,50);
for (int r=0;r<=k;++r)
{
for (int j=r*i;j<=tmp*(tmp+1)/2*k;++j)
{
add(dp[i][j],dp[i-1][j-r*i]);
}
}
}
}
int calc(int x)
{
int tmp=_min(x-1,n-x);
int ans=0;
for (int i=0;i<=tmp*(tmp+1)/2*k;++i)
{
if (i) add(ans,Mul(Mul(dp[x-1][i],dp[n-x][i]),k+1));
else add(ans,k);
}
return ans;
}
void solve()
{
for (int i=1;i<=n;++i)
{
printf("%d\n",calc(i));
}
}
int main()
{
init();
solve();
return 0;
}
| #include <iostream>
#include <vector>
using namespace std;
int main(){
int N, K, M; cin >> N >> K >> M;
vector<vector<int>> dp;
dp.push_back(vector<int>(1, 1));
for(int i=1;i<=N;i++){
int n = dp.back().size();
int m = min(n + i*K, 122501);
vector<int> v(m, 0);
for(int j=0;j<n;j++){
for(int k=j;k<=min(m-1, j+K*i);k+=i) v[k] = (v[k] + dp.back()[j]) % M;
}
dp.push_back(v);
}
for(int i=1;i<=N;i++){
const auto& a = dp[i-1];
const auto& b = dp[N-i];
const int m = min(a.size(), b.size());
long long res = 0;
for(int j=0;j<m;j++){
res += (long long)a[j] * b[j];
res %= M;
}
res = (res * (K+1)) % M;
res = (res + M - 1) % M;
cout << res << endl;
}
} |
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define PRINT(a) printf("%.15Lf\n",a);
#define YESNO(a) cout << (a ? "YES" : "NO") << endl
#define YesNo(a) cout << (a ? "Yes" : "No") << endl
#define yesno(a) cout << (a ? "yes" : "no") << endl
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
//priority_queue<int,vector<int>, greater<int> > q2;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
bool ok = 1;
int A, B, C, X, Y;
string S, T;
int D;
cin >> A >> B >> C >> D;
int ans = -INF;
eFOR(x, A, B) {
eFOR(y, C, D) {
chmax(ans, x - y);
}
}
cout << ans << endl;
return 0;
}
| // D - increment of coins
#include <bits/stdc++.h>
using namespace std;
#define N 100
double dp[N][N][N];
bool vis[N][N][N];
double f(int g, int s, int b){ // g=gold, s=silver, b=bronze
if(g==N || s==N || b==N) return 1;
if(vis[g][s][b]) return dp[g][s][b] + 1;
vis[g][s][b] = true;
int t = g + s + b;
dp[g][s][b] = f(g+1,s,b)*g/t + f(g,s+1,b)*s/t + f(g,s,b+1)*b/t;
return dp[g][s][b] + 1;
}
int main(){
int G, S, B; cin>>G>>S>>B;
cout<<setprecision(9)<<fixed<< f(G, S, B) - 1 <<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,i;
cin>>n;
long long a[n];
for(i=0;i<n;i++){cin>>a[i];}
unsigned long long int s=0,t=0;
sort(a,a+n);
for(i=1;i<n;i++)
{
t=t+(a[i]-a[i-1])*i;
s+=t;
}
cout<<s;
} | #include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
read(n);
readarr(a, n);
sort(a + 1, a + n + 1);
int pre[n + 1] = {}, sum = 0;
FOR (i, 1, n)
{
pre[i] = pre[i - 1] + a[i];
sum += i*a[i] - pre[i];
}
print(sum);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
unsigned long long int t,n,i,x,mx,mn,f,j,s,r,y;
cin>>x;
s=0;
n=999000000000;
if(x>999)s+=(x-999);
if(x>999999)s+=(x-999999);
if(x>999999999)s+=(x-999999999);
if(x>999999999999)s+=(x-999999999999);
if(x>999999999999999)s+=(x-999999999999999);
cout<<s<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long N;
cin >> N;
int x = (int)log10(N) + 1;
long long res = 0;
if (x >= 4) {
int y = x - 3;
rep(i, y) {
int z = i + 3;
if (i < y - 1)
res += ((long)pow(10, z) * 9) * ((int)z / 3);
else
res += (N - (long)pow(10, z) + 1) * ((int)z / 3);
}
}
cout << res << endl;
} |
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read()
{
int x=0,y=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*y;
}
int n,a[200010],b[200010],c[10][2000010],cnt[10];
int ans;
bool cmp(int x,int y)
{
return x>y;
}
signed main()
{
n=read();
for(int i=0;i<n;i++)
{
a[i]=read(),ans+=a[i];
}
for(int j=0;j<n;j++)
{
b[j]=read();
c[j%2][++cnt[j%2]]=b[j]-a[j];
}
int ji=ans;
sort(c[1]+1,c[1]+1+cnt[1],cmp);
sort(c[0]+1,c[0]+1+cnt[0],cmp);
for(int i=1;i<=cnt[0];i++)
{
// cout<<c[0][i]<<' '<<c[1][i]<<endl;
ji+=c[1][i]+c[0][i];
ans=max(ans,ji);
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;
template<class T>inline bool chmax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; }
template<class T>inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;
void solve() {
int N;
cin >> N;
vector<pair<int, int>> A(1<<N);
for (int i=0; i<(1<<N); ++i) {
cin >> A[i].first;
A[i].second = i+1;
}
vector<pair<int, int>> nA(1<<N);
for (int i=0; i<N-1; ++i) {
for (int j=0; j<(1<<(N-i-1)); ++j) nA[j] = max(A[2*j], A[2*j+1]);
A.swap(nA);
}
cout << min(A[0], A[1]).second << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
solve();
return 0;
}
|
#include<cstdio>
bool vis[15];
int a[15][15], ans, n, k;
void dfs(int now, int tot, int cs) {
vis[now] = 1;
// printf("%d %d %d\n", now, tot, cs);
if(cs == n + 1) {
if(tot == k) {
ans ++;
} return;
}
if(cs == n) dfs(1, tot + a[now][1], cs + 1);
else {
for(int i = 1; i <= n; i ++) {
if(i == now) continue;
if(vis[i]) continue;
vis[i] = 1;
dfs(i, tot + a[now][i], cs + 1);
vis[i] = 0;
} vis[now] = 0;
}
}
int main() {
scanf("%d %d", &n, &k);
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
scanf("%d", &a[i][j]);
}
} dfs(1, 0, 1);
printf("%d", ans);
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FIO ios::sync_with_stdio(false); cin.tie(nullptr)
#define TC(t) int t; cin >> t; for(int i = 1; i <= t; i++)
#define deb(x) cerr << #x << " = " << x << endl
#define deb2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl
#define f0(i, a, n) for(i = a; i < n; i++)
#define f1(i, a, n) for(i = a; i <= n; i++)
#define ini(x, y) memset(x, y, sizeof(x))
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define ll long long int
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define M 1000000007
#define endl '\n'
#define bits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define multi_ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
const int N = 1e5 + 1;
const int MX = 1e9;
const ll INF = 1e18;
using namespace std;
using namespace __gnu_pbds;
inline ll uceil(ll a,ll b) {return (a % b ? a / b + 1 : a / b);}
inline ll mod(ll x) {return ( (x % M + M) % M );}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {for(auto& x : v) os << x << " "; return os << '\n';}
template<typename... T> void in(T&... args) {((cin >> args), ...);}
template<typename... T> void out(T&&... args) {((cout << args << endl), ...);}
template<typename... T> void out2(T&&... args) {((cout << args << " "), ...);}
void solve() {
ll n, k, i, j;
cin >> n >> k;
vector<vector<ll>> a(n, vector<ll>(n));
f0(i, 0, n) {
f0(j, 0, n) {
cin >> a[i][j];
}
}
vector<ll> v(n);
f0(i, 1, n) {
v[i] = i;
}
ll ans = 0;
ll sum = 0;
sum += a[0][v[0]];
f0(i, 0, n-1) {
sum += a[v[i]][v[i+1]];
}
sum += a[v[i]][0];
if(sum == k) ans++;
while(next_permutation(all(v))) {
sum = 0;
sum += a[0][v[0]];
f0(i, 0, n-1) {
sum += a[v[i]][v[i+1]];
}
sum += a[v[i]][0];
if(sum == k) ans++;
}
out(ans/2);
}
int main () {
FIO;
// TC(t)
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define lowbit(x) (x&-x)
#define reg register
#define mkpr make_pair
#define fir first
#define sec second
typedef long long LL;
typedef unsigned long long uLL;
const LL INF=0x7f7f7f7f7f7f7f7f;
const int mo=998244353;
const int zero=500;
const LL jzm=2333;
const int iv2=499122177;
const double Pi=acos(-1.0);
typedef pair<int,int> pii;
const double PI=acos(-1.0);
template<typename _T>
_T Fabs(_T x){return x<0?-x:x;}
template<typename _T>
void read(_T &x){
_T f=1;x=0;char s=getchar();
while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}
while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}
x*=f;
}
template<typename _T>
void print(_T x){if(x<0){x=(~x)+1;putchar('-');}if(x>9)print(x/10);putchar(x%10+'0');}
int t;LL X,Y,P,Q,x[1005],y[1005],d[1005];
LL exgcd(LL a,LL b,LL &x,LL &y){
if(!b){x=1;y=0;return a;}
LL d=exgcd(b,a%b,y,x);y-=x*(a/b);return d;
}
signed main(){
read(t);
while(t--){
read(X);read(Y);read(P);read(Q);
LL A=P+Q,B=2ll*X+2ll*Y,res=INF;
for(LL i=-Q;i<=Y;i++)d[i+zero]=exgcd(A,B,x[i+zero],y[i+zero]);
for(LL i=P;i<P+Q;i++)
for(LL j=X;j<X+Y;j++){
LL t=j-i,d1=d[t+zero-X+P];if(t%d1!=0)continue;
LL ax=t/d1*x[t+zero-X+P],ay=i/d1*y[t+zero-X+P],a1=B/d1,b1=A/d1,k;
if(ax<0)k=(a1-ax)/a1,ax+=k*a1,ay-=k*b1;
if(ax>=0)k=(ax-1)/a1,ax-=k*a1,ay+=k*b1;
if(ay>0)k=(ay-1)/b1,ax+=k*a1,ay-=k*b1;
res=min(ax%a1*A+i,res);
}
for(LL i=-Q;i<=Y;i++)d[i+zero]=x[i+zero]=y[i+zero]=0;
if(res>INF-1)puts("infinity");else printf("%lld\n",res);
}
return 0;
}
/*
[(2X+2Y)n+X,(2X+2Y)n+X+Y)
[(P+Q)m+P,(P+Q)(m+1))
(X-P)<=(P+Q)m%(2X+2Y)<(X+Y-P)
(P+Q)x+(2X+2Y)y=+j-i
*/ | #include<bits/stdc++.h>
using namespace std;
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=1e9+7;
const ll mod=MOD;
const int MAX=200010;
int main(){
ll n;cin>>n;
map<ll,ll>ma;
ll b=1LL;
ll cnt=0;
while(b*3LL<inf){
b*=3LL;
cnt++;
ma[b]=cnt;
}
ll c=1LL;
bool f=0;
cnt=0;
pll ans;
while(c*5LL<=n){
cnt++;
c*=5LL;
if(ma.find(n-c)!=ma.end()){
ans.fi=ma[n-c];
ans.se=cnt;
f=1;
}
if(f)break;
}
if(f){
cout<<ans.fi<<' '<<ans.se<<endl;
}else{
cout<<-1<<endl;
}
} |
#include <bits/stdc++.h>
#include <iostream>
//#include <algorithm>
// #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
// template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
using namespace std;
const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);
int main() {
// std::cout << std::fixed << std::setprecision(10);
ll N, M;
scanf("%lld %lld", &N, &M);
vector<ll> alist;
for (ll i = 0; i < N; i++) {
ll v;
scanf("%lld", &v);
alist.emplace_back(v);
}
mll m;
ll ans;
ll mex = 0;
for (ll i = 0; i < M; i++) {
ll v = alist[i];
m[v]++;
if (v == mex) {
mex = v + 1;
for (;;) {
if (m[mex] == 0) {
break;
}
mex++;
}
}
}
ans = mex;
ll left = 0;
ll right = M - 1;
for (;;) {
if (right >= N - 1) {
break;
}
ll lost_value = alist[left];
ll new_value = alist[right + 1];
// cout << "lost_value:" << (lost_value) << ",new_value:" << (new_value) << endl;
m[lost_value]--;
m[new_value]++;
ll new_mex = mex;
if (lost_value == new_value) {
// NOP
} else if (m[lost_value] == 0 && lost_value < mex) {
new_mex = lost_value;
} else if (new_value == mex) {
new_mex = new_value + 1;
for (;;) {
if (m[new_mex] == 0) {
break;
}
new_mex++;
}
}
left++;
right++;
mex = new_mex;
chmin(ans, new_mex);
}
cout << ans << endl;
}
| //clear adj and visited vector declared globally after each test case
//check for long long overflow
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Incase of close mle change language to c++17 or c++14
/**#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops”)**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007ll //998244353ll
#define lld long double
#define mii map<int, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
int g=__gcd(a, b);
return a/g*b;
}
int power(int a, int b, int p)
{
if(a==0)
return 0;
int res=1;
a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
int32_t main()
{
IOS;
int n;
cin>>n;
string s;
cin>>s;
int ar[n+1];
rep(i,0,n+1)
cin>>ar[i];
int mi=1e6;
rep(i,1,n+1)
mi=min(mi, abs(ar[i]-ar[i-1]));
cout<<mi<<"\n";
int b[mi][n+1];
int sub=0;
rep(i,0,n+1)
{
int base=ar[i]/mi;
int lim=ar[i]%mi;
rep(j,0,lim)
b[j][i]=base+1;
rep(j,lim,mi)
b[j][i]=base;
}
print2d(b, mi-1, n);
} |
#include <bits/stdc++.h>
//#include <chrono>
#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);
template<class T> bool chmin(T &a, const T b){
if(a > b) {a = b; return true;}
else return false;
}
template<class T> bool chmax(T &a, const T b){
if(a < b) {a = b; return true;}
else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
if(!v.empty()){
for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
std::cout<<v.back();
}
if(endline) std::cout<<std::endl;
}
vec prime;
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
reps(i, 2, 51){
bool pr = true;
reps(j, 2, i) if(i%j == 0) pr = false;
if(pr) prime.push_back(i);
}
M = 15;
ll res = INF;
cin>>N;
vec a(N);
rep(i, N) cin>>a[i];
rep(s, 1<<M){
ll cmp = 1;
rep(i, M) if((s>>i)&1) cmp *= prime[i];
rep(i, N){
bool ok = false;
rep(j, M){
if(((s>>j)&1) && a[i]%prime[j] == 0){
ok = true;
break;
}
}
if(!ok){
cmp = -1;
break;
}
}
if(cmp != -1) chmin(res, cmp);
}
cout<<res<<endl;
} | #include<bits/stdc++.h>
using namespace std;
int n, x[50];
int prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
int gcd(long long a, long long b) {
return b ? gcd(b, a % b) : a;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> x[i];
long long ans = 0x3f3f3f3f3f3f3f3f;
for (int i = 0; i < (1<<15); i++) {
long long val = 1;
for (int j = 0; j < 15; j++)
if (i & (1<<j)) val *= prime[j];
bool flag = 1;
for (int j = 1; j <= n; j++)
if (gcd(val, x[j]) == 1) flag = 0;
if (flag) ans = min(ans, val);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0; i < (n);i++)
#define _GLIBCXX_DEBUG
typedef long long ll;
int main(){
int n;
cin >> n;
vector<int>x(n);
ll man = 0;
ll euc = 0;
int a = 0;
rep(i,n){
cin >> x[i];
x[i] = abs(x[i]);
man += x[i];
euc += ll(x[i])*x[i];
a = max(a,x[i]);
}
cout << man << endl;
cout << setprecision(20) << sqrt(euc) <<endl;
cout << a << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0); cin.tie(0);
long long n, m=0; cin >> n; double e=0;
vector<long long> x(n), c(n);
for(long long i=0;i<n;i++) cin >> x[i];
for(long long i=0;i<n;i++) m += abs(x[i]);
for(long long i=0;i<n;i++) e += pow(abs(x[i]), 2);
for(long long i=0;i<n;i++) c.push_back(abs(x[i]));
sort(c.begin(), c.end());
cout << setprecision(15) << endl;
cout << m << endl;
cout << sqrt(e) << endl;
cout << c[c.size()-1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
const int mu=1e9+7,N=100005;
int n,a[N];
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
std::sort(a+1,a+n+1);
n=std::unique(a+1,a+n+1)-a-1;
int p=1;
for (int i=1;i<=n;i++) p=(long long)p*(a[i]-a[i-1]+1)%mu;
printf("%d\n",p);
} | #include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
//問題文を理解できなかった
// しきりを入れて区間に分ける
// 区間内はor演算、区間をまたぐときはxor演算を全ての要素同士で先頭から順に
// しきりを入れるパターンは、2^n-1(しきりを入れるか入れないか)
// 計算する場所としきりの場所がずれている!!
// 区間をor演算し終わったもの同士でxor演算する
// A={5,3,7,2,8}のとき
// 0 1 2 3 4
// 0 5|3 7|2 8|
// i=0 0とA[0](5)をor演算、しきりがあるのでoredをxoredに保持。oredを0に
// i=1 0とA[1](3)をor演算
// i=2 oredとA[2](7)をor演算 区間全てor演算したoredと保持していたxoredでxor演算
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
int ans = 1 << 30; // 10^9x1.073 使いやすい! いいね!!
// int型は4byte(32bit)、1<<31で10^9x2.147になる
for (int bit = 0; bit < (1 << N - 1); ++bit) {
int ored = 0, xored = 0;
for (int i = 0; i < N; ++i) {
//計算回数はN回、0とA[i]を最初に計算
ored |= A[i];
if (bit >> i & 1) { //一致すれば区間をまたぐ
xored ^= ored;
// or演算し終わった区間同士をxor演算する
ored = 0;
}
}
xored ^= ored;
ans = min(ans, xored);
}
cout << ans << endl;
return 0;
}
// int main() {
// int N;
// cin >> N;
// vector<int> A(N);
// for (int i = 0; i < N; ++i) cin >> A[i];
// vector<int> a;
// for (int i = 0; i < N; i += 2) {
// int tmp = A[i] | A[i + 1];
// a.push_back(tmp);
// }
// int ans = a[0];
// for (int i = 1; i < a.size(); ++i) { ans ^= a[i]; }
// cout << ans << endl;
// // sort(all(A));
// // int tmp = 0;
// // for (int i = 0; i < N - 1; ++i) { tmp |= A[i]; }
// // tmp ^= A.back();
// // cout << tmp << endl;
// return 0;
// }
|
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
const int L=3e5;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N;
ll W;
cin>>N>>W;
vector<int> S(N),T(N);
vector<ll> P(N);
rep(i,N) cin>>S[i]>>T[i]>>P[i];
vector<ll> imos(L+10,0);
rep(i,N){
imos[S[i]]+=P[i];
imos[T[i]]-=P[i];
}
rep(i,L) imos[i+1]+=imos[i];
ll ma=*max_element(all(imos));
if(ma<=W) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
| #include <iostream>
#include <climits>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#define MAX 1000000007
using namespace std;
#define ll long long
#define dbg if(0)
#define ISRANGE(val,lo,hi) ((lo<=val)&&(val<hi))
int main(){
ll ans,a,b,c,d;
cin>>a>>b>>c>>d;
if(d*c-b>0){
ans=a/(d*c-b)+((a%(d*c-b)!=0)?(1):(0));
}else if(d*c-b==0 && a==0){
ans=0;
}else{
ans=-1;
}
cout <<ans<<endl;
return 0;
} |
#line 1 "Contests/AtCoder_abc200/abc200_b/main.cpp"
using namespace std;
#line 2 "library/bits/stdc++.h"
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#line 3 "Contests/AtCoder_abc200/abc200_b/main.cpp"
#define ll long long
void solve(){
// write your solution here.
}
int main() {
ll N;
int K;
cin >> N >> K;
for(int i=0; i<K; i++){
if(N%200==0) N/=200;
else{
N=N*1000+200;
}
}
cout << N << endl;
return 0;
}
| #include <iostream>
#include <string>
#define mod 3
using namespace std;
int n;
string s;
int dp[400000] = { 0 };
int f[400000] = { 0 };
int g[400000] = { 0 };
void compute()
{
dp[0] = 1;
f[0] = 0;
g[0] = 1;
for (int i = 1; i < n; ++i) {
f[i] += f[i - 1];
int j = i;
while (j % mod == 0) {
f[i]++;
j /= mod;
}
g[i] = (g[i - 1] * j) % mod;
}
for (int i = 1; i < n; ++i) {
if (f[n - 1] > f[i] + f[n - 1 - i]) {
dp[i] = 0;
} else {
dp[i] = g[n - 1] * g[n - 1 - i] * g[i];
dp[i] %= mod;
}
}
}
int convert(char c)
{
if (c == 'B')
return 0;
if (c == 'R')
return 1;
if (c == 'W')
return 2;
}
char revert(int num)
{
if (num == 0)
return 'B';
if (num == 1)
return 'R';
if (num == 2)
return 'W';
}
int main()
{
cin >> n >> s;
compute();
int res = 0;
for (int i = 0; i < n; ++i) {
res += dp[i] * (convert(s[i])) % mod;
}
if ((n - 1) % 2) {
res = -res;
}
res = ((res % mod) + mod) % mod;
cout << revert(res) << endl;
}
|
#include <bits/stdc++.h>
#define NIL (-1)
#define LL long long
using namespace std;
const int64_t MOD = 1e9 + 7;
const int INF = INT_MAX;
const double PI = acos(-1.0);
int main() {
int H, W;
cin >> H >> W;
vector<string> mass(H);
int top, bottom;
top = -1, bottom = -1;
for (int i = 0; i < H; i++) {
cin >> mass[i];
//cout << mass[i] << endl;
if (mass[i].find('#') != string::npos) {
if (top == -1) top = i;
bottom = i;
}
}
//cout << top << ' ' << bottom << endl;
int n = 0;
for (int i = 0; i < H - 1; i++) {
for (int j = 0; j < W - 1; j++) {
int cnt = 0;
if (mass[i][j] == '#') cnt++;
if (mass[i][j+1] == '#') cnt++;
if (mass[i+1][j] == '#') cnt++;
if (mass[i+1][j+1] == '#') cnt++;
if (cnt % 2) n++;
}
}
cout << n << endl;
} | //This Code was made by Chinese_zjc_.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <ctime>
// #include<windows.h>
#define int long long
#define double long double
using namespace std;
const double PI = acos(-1);
const double eps = 0.0000000001;
const int INF = 0x3fffffffffffffff;
int n, m, a[100005], b[100005], to[100005], lst, ans, v;
bool sid[100005];
signed main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
for (int i = 1; i <= n; ++i)
{
cin >> b[i];
}
a[n + 1] = m + 1;
a[n + 2] = INF;
for (int i = 1; i <= n; ++i)
{
while (a[lst + 1] + (i - lst - 1) <= b[i] && a[lst] + (i - lst) != b[i])
{
++lst;
}
while (a[lst + 1] + (i - lst - 1) == b[i] && abs(lst + 1 - i) < abs(lst - i))
{
++lst;
}
to[i] = lst;
if (a[lst] + (i - lst) != b[i])
{
cout << -1 << endl;
return 0;
}
}
to[0] = -1;
for (int i = 1; i <= n; ++i)
{
if (to[i] < i)
{
sid[i] = true;
}
}
for (int i = 1; i <= n; ++i)
{
if (to[i] == to[i - 1] && sid[i] == sid[i - 1])
{
v = max(v, abs(i - to[i]));
}
else
{
ans += v;
v = abs(i - to[i]);
}
}
ans += v;
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define INF (int)1e9
#define EPS (int)1e-9
#define ll long long
#define ld long double
#define FOR(i,m,n) for(int i=m; i<n; i++)
#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
//#define endl "\n"
void solve(){
ld n;
cin >> n;
vt<pair<ld,ld>> a(n);
vt<pair<ld,ld>> sums(n);
ld asum = 0, tsum = 0, ans = 0, ct = n-1;
FOR(i,0,n){
cin >> a[i].first >> a[i].second;
asum += a[i].first;
sums[i].first = a[i].first*2 + a[i].second;
sums[i].second = i;
}
sort(all(sums));
while(tsum<=asum){
tsum += sums[ct].first;
ans ++;
ct --;
}
cout << ans << "\n";
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tt;
//cin >> tt;
tt=1;
cout << setprecision(0) << fixed;
while (tt--){
solve();
}
}
| #include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = int64_t; using ull = uint64_t; const ll INF = 9e18;
void print() { cout << endl; }
template<typename Head,typename... Tail> void print(Head head,Tail... tail){cout<<head;if(sizeof...(Tail)>0)cout<<" ";print(tail...);}
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<head;print0(tail...);}
// clang-format on
struct person {
ll id;
ll a;
ll b;
};
ll check(ll N, ll start, ll end, vector<ll>& start_end, vector<ll>& start_fix, vector<ll>& end_fix) {
if (end > 2 * N) {
return 0;
}
if (start_fix[start] >= 0) {
return 2;
}
if (end_fix[end] >= 0) {
return 2;
}
if (start_end[start * 1000 + end] >= 0) {
return 2;
}
return 1;
}
bool recursive1(ll N, ll start, ll cnt, vector<ll>& start_end, vector<ll>& start_fix, vector<ll>& end_fix, ll anypt, vector<ll>& memo) {
if (cnt == N) {
return true;
}
if (start >= 2 * N) {
return false;
}
if (memo[start] >= anypt) {
return false;
}
memo[start] = anypt;
for (ll end = min(start + N, 2 * N); end >= start + 1; end--) {
bool success = true;
ll anyy = anypt;
for (ll ss = start; ss <= end - 1; ss++) {
ll ee = end - start + ss;
ll result = check(N, ss, ee, start_end, start_fix, end_fix);
if (result == 0) {
success = false;
break;
} else if (result == 1) {
anyy--;
if (anyy < 0) {
success = false;
break;
}
} else {
;
}
}
if (success) {
bool ok = recursive1(N, end + end - start, cnt + (end - start), start_end, start_fix, end_fix, anyy, memo);
if (ok) {
return true;
}
}
}
return false;
}
bool solve(ll N, vector<person>& persons) {
vector<ll> start_end(300000, -1);
vector<ll> start_fix(300, -1);
vector<ll> end_fix(300, -1);
ll anypt = 0;
for (auto p : persons) {
if (p.a == -1 && p.b == -1) {
anypt++;
}
if (p.a >= 0 && p.b == -1) {
start_fix[p.a] = p.id;
}
if (p.a == -1 && p.b >= 0) {
end_fix[p.b] = p.id;
}
if (p.a >= 0 && p.b >= 0) {
start_end[p.a * 1000 + p.b] = p.id;
}
}
vector<ll> memo(2 * N + 2, -1);
return recursive1(N, 1, 0, start_end, start_fix, end_fix, anypt, memo);
}
int main() {
ll N;
cin >> N;
vector<person> persons(N);
for (ll i = 0; i < N; i++) {
person p;
cin >> p.a >> p.b;
p.id = i;
persons[i] = p;
}
if (solve(N, persons)) {
print("Yes");
} else {
print("No");
}
}
|
#include <iostream>
using namespace std;
int main()
{
int a,b,sumA=0,sumB=0;
cin>>a>>b;
while(a!=0)
{
sumA+= a%10;
a/=10;
sumB+= b%10;
b/=10;
}
if(sumA>=sumB)
cout<<sumA;
else
cout<<sumB;
}
| #include <bits/stdc++.h>
#include <cctype>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//template<typename key, typename val>
//using ordered_tree =
// tree<key, val, std::less<>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
typedef complex<double> comp;
const double pi = 3.14159265358979323846;
const int inf = (int)1e+9 + 2;
const int64 inf64 = (int64)1e+18 + 2;
const double dinf = 1e+20;
const int mod = /*1'000'000'007;//*/998244353;
const int base = 2187;
const double eps = 1e-9;
const int N = 500'000;
const int LOGN = 19;
int n, m, k;
void solve(int test) {
int64 n;
cin >> n;
int a[10] = {};
int s = 0, t = 0;
for (; n; n /= 10, ++t) {
++a[n % 10 % 3];
s += n % 10;
}
if (s % 3 == 0) {
cout << "0\n";
return;
}
if (s % 3 == 1) {
if (a[1] && t > 1) {
cout << "1\n";
return;
}
if (a[2] >= 2 && t > 2) {
cout << "2\n";
return;
}
cout << "-1\n";
return;
}
if (a[2] && t > 1) {
cout << "1\n";
return;
}
if (a[1] >= 2 && t > 2) {
cout << "2\n";
return;
}
cout << "-1\n";
}
void precalc() {
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
precalc();
int test = 1;
//cin >> test;
//auto start = chrono::high_resolution_clock::now();
for (int i = 1; i <= test; ++i) {
solve(i);
}
//cerr << chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start).count() << '\n';
return 0;
}
|
//remove #define int long long in critical cases to avoid TLE
//always use ll with constants like (1ll)<<59 otherwise ans will be zero at higher cases
//while doing question on comparing, sort elements according to the difference they produce
//when elements are unique sorting elements after inserting in vector sometimes gives TLE but inserting in set
//can help
//prefer set over map for checking presence. Map may give TLE more earlier than Sets && set::find over set::count
//even if computation takes O(logn) time in sets prefer deleting elements as soon their use is over. Reduces chances of TLE
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ff first
#define ss second
#define vi vector <int>
#define pii pair<int,int>
#define f(x,y,z) for(x=y;x<z;x++)
#define pb push_back
#define mp make_pair
#define ld long double
#define ps(x,y) fixed << setprecision(y) << x
#define PI 3.1415926535
#define mod 1000000007
#define mods 998244353
#define setbits(x) __builtin_popcountll(x)
#define all(x) (x).begin(), (x).end()
#define fill(a,b) memset(a,b,sizeof(a))
#define INF INT_MAX
// AUTHOR ALOK KUMAR
int lcm(int a,int b)
{
return (a*b)/(__gcd(a,b));
}
int power(int a,int b,int p)
{
int c=1;
while(b)
{
if(b&1)
c*=a;
a*=a;
b>>=1;
c%=p;
a%=p;
b%=p;
}
return c%p;
}
int inverse_mod(int n, int p)
{
return power(n,p-2,p);
}
void seive()
{
int N;
int prime[N+1];
int i;
for(i=2;i*i<=N;i++)
{
if(!prime[i])
{
for(int j=i*i;j<=N;j+=i)
{
prime[j] = 1;
}
}
}
}
void solve()
{
char ch1,ch2,ch3;
cin>>ch1>>ch2>>ch3;
if(ch1==ch2&&ch2==ch3)
cout<<"Won\n";
else
cout<<"Lost\n";
}
int32_t main()
{
IOS
int t=1;
// cin>>t;
while(t--)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fast_io cin.tie(0);ios_base::sync_with_stdio(0);
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cout << endl; }
void show() { cout << endl; }
void pret() { cout << endl; exit(0);}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cout << " " << to_string(H); debug_out(T...);}
template <typename Head, typename... Tail> void show(Head H, Tail... T) {cout <<H<<" "; show(T...);}
template <typename Head, typename... Tail> void pret(Head H, Tail... T) {cout <<H<<" "; pret(T...);}
#define pr(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
typedef long long ll;
#define int ll
typedef long double ld;
typedef vector<int> vi;
#define disp(x) cout<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(int)b;i++)
#define fo(i,a,b) for(int i=a;i<=(int)b;i++)
#define rf(i,a,b) for(int i=a;i>=(int)b;i--)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
#define endl '\n'
//cout.setf(ios::fixed);cout.precision(18)
const int MOD = 1e9+7;
const int maxn = 300000+10;
int32_t main(){
fast_io;
string s;
cin >> s;
if(s[1] != s[0] or s[2] != s[0]){
pret("Lost");
}
else {
show("Won");
}
return 0;
} |
#line 1 "/home/anqooqie/.proconlib/tools/util.hpp"
#include <bits/stdc++.h>
using i64 = ::std::int_fast64_t;
using u64 = ::std::uint_fast64_t;
using i32 = ::std::int_fast32_t;
using u32 = ::std::uint_fast32_t;
#define all(x) ::std::begin((x)), ::std::end((x))
namespace tools {
template <typename T>
void read(::std::istream& is, ::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
vector.reserve(size);
::std::copy_n(::std::istream_iterator<T>(is), size, ::std::back_inserter(vector));
}
template <typename T>
void read(::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
::tools::read(::std::cin, vector, size);
}
template <typename T, ::std::size_t N>
void read(::std::istream& is, ::std::array<T, N>& array) {
::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
}
template <typename T, ::std::size_t N>
void read(::std::array<T, N>& array) {
::tools::read(::std::cin, array);
}
}
#line 2 "main.cpp"
int main() {
i64 n;
std::cin >> n;
i64 ng = 0;
i64 ok = 1414213561;
while (ok - ng > 1) {
const i64 center = (ng + ok) / 2;
if (center * (center + 3) >= 2 * n + 4) {
ok = center;
} else {
ng = center;
}
}
std::cout << std::max(n + 1 - ok, ok * (ok - 1) / 2) << '\n';
return 0;
}
| #include <bits/stdc++.h>
//#include <atcoder/modint>
//using namespace atcoder;
//#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<61);
template<class T> bool chmin(T &a, const T b){
if(a > b) {a = b; return true;}
else return false;
}
template<class T> bool chmax(T &a, const T b){
if(a < b) {a = b; return true;}
else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
if(!v.empty()){
for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
std::cout<<v.back();
}
if(endline) std::cout<<std::endl;
}
template <class T> class BIT {
//T has operator "+=" and can be initialized with 0.
int n, n_2k;
vector<T> bitree;
public:
//make [0, n) BIT
BIT(int _n) : bitree(_n + 1, 0) {
n = n_2k = _n;
while(n_2k & (n_2k - 1)) n_2k &= n_2k - 1;
}
//BIT[id] += x
void add(int id, T x) {
++id;
while (id <= n) {
bitree[id] += x;
id += id & -id;
}
}
//sum of BIT[0, id]
T sum(int id) {
++id;
T temp(0);
while (id > 0) {
temp += bitree[id];
id -= id & -id;
}
return temp;
}
//sum of BIT[l, r)
T range_sum(int l, int r) {
return sum(r-1) - sum(l-1);
}
//return i in [0, n]
//i = argmin_{j} (BIT[0, j] >= x)
//T should have bool operator '<'
int lower_bound(T x){
int id = n_2k;
T temp(0);
for(int plus = id; plus; id |= (plus>>=1)){
if(id <= n && temp + bitree[id] < x) temp += bitree[id];
else id ^= plus;
}
return id;
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin>>N;
vec a(N * 2);
rep(i, N * 2) cin>>a[i];
priority_queue<ll> pque;
ll res{};
rep(i, N){
pque.push(a[N * 2 - 1 - i]);
pque.push(a[i]);
ll nxt = pque.top();
pque.pop();
res += nxt;
}
cout<<res<<endl;
} |
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <climits>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
#define pb push_back
int ri() {
int n;
cin>>n;
return n;
}
void solve() {
int x = ri();
if(x>=0) {
cout<<x;
return;
}
cout<<0<<endl;
}
int32_t main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
while(t--) {
solve();
}
return 0;
}
| //#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (ll i = j; i <= n; i++)
#define per(i, j, n) for (ll i = j; i >= n; i--)
#define ll long long
#define ld long double
#define ld long double
#define ii pair<ll, ll>
#define vii vector<ii>
#define vi vector<ll>
#define adj unordered_map<ll, vii>
#define MOD 1000000007
#define PI 3.14159265359
#define db1(x) cout << #x " = " << x << endl;
inline ll add(ll a, ll b) { return ((a % MOD) + (b % MOD)) % MOD; }
inline ll mul(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }
inline ll sub(ll a, ll b) { return ((a % MOD) - (b % MOD) + MOD) % MOD; }
template <typename T> void i1d(vector<T> &v) {
for (auto &i : v)
cin >> i;
}
template <typename T> void p1d(const vector<T> &v) {
for (auto i : v)
cout << i << ' ';
cout << endl;
}
ll t = 1;
ll x;
void solve() {
// Code goes here
cin >> x;
if (x >= 0)
cout << x << '\n';
else
cout << 0 << '\n';
}
int main() {
ios::sync_with_stdio(false);
// cin >> t;
rep(i, 1, t) {
// cout<<"Case #" << i << ": ";
solve();
}
}
|
#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
ll INF = 1000000000000000;
struct segment_tree{
int n; vector<ll> seg;
segment_tree(vector<ll> v){
n = v.size(); seg.resize(2*n);
for(int i=0;i<n;i++) seg[i + n] = v[i];
for(int i=n - 1;i>0;i--) seg[i] = seg[i<<1] + seg[i<<1|1];
}
void update(int p,ll val){
for(seg[p += n] += val;p>1;p>>=1) seg[p>>1] = seg[p] + seg[p^1];
}
ll query(int l,int r){
ll res = 0;
for(l += n,r += n; l<r;l>>=1,r>>=1){
if(l&1) res += seg[l++];
if(r&1) res += seg[--r];
}
return res;
}
void clear(){for(int i=0;i<2*n;i++) seg[i] = 0;}
};
int t[200010],x[200010],y[200010];
int main(){
int i,n,m,q; cin >> n >> m >> q;
vector<int> a(n),b(m);
map<int,int> mp;
mp[0]++;
for(i=0;i<q;i++){
cin >> t[i] >> x[i] >> y[i];
t[i]--; x[i]--; mp[y[i]]++;
}
vector<ll> v,u;
for(auto x:mp) v.push_back(x.first);
for(i=0;i<v.size();i++) mp[v[i]] = i;
int k = v.size(); u.resize(k);
vector<segment_tree> seg(2,segment_tree(u)),seg_num(2,segment_tree(u));
for(i=0;i<n;i++) seg[0].update(mp[a[i]],a[i]),seg_num[0].update(mp[a[i]],1);
for(i=0;i<m;i++) seg[1].update(mp[b[i]],b[i]),seg_num[1].update(mp[b[i]],1);
ll ans = 0;
for(i=0;i<q;i++){
ll now;
int j = x[i];
if(t[i]==0) now = a[j];
else now = b[j];
ans -= seg_num[1 - t[i]].query(0,mp[now])*now + seg[1 - t[i]].query(mp[now],k);
if(t[i]==0){
seg[0].update(mp[a[j]],-a[j]); seg_num[0].update(mp[a[j]],-1); a[j] = y[i];
}else{
seg[1].update(mp[b[j]],-b[j]); seg_num[1].update(mp[b[j]],-1); b[j] = y[i];
}
seg[t[i]].update(mp[y[i]],y[i]); seg_num[t[i]].update(mp[y[i]],1);
ans += seg_num[1 - t[i]].query(0,mp[y[i]])*y[i] + seg[1 - t[i]].query(mp[y[i]],k);
cout << ans << "\n";
}
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define fo(i,a,b) for(int i = a; i<b ; i++)
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define M 1000000007
int mod(int x){
return ((x%M + M)%M);
}
int add(int a,int b){
return mod(mod(a)+mod(b));
}
int mul(int a,int b){
return mod(mod(a)*mod(b));
}
// ****************************************************************************
int n,m;
map<int,vector<pair<int,int>>>mp;
int dp[1<<(19)];
int rec(int mask){
if(mask==0)
return 1;
// now check the validity
if(dp[mask]!=-1)
return dp[mask];
int pref = n-__builtin_popcount(mask);
for(auto it : mp[pref]){
int val = it.first;
int maxnum = it.second;
int temp = 0;
for(int j=1;j<=n;j++){
if(!(mask&(1<<j)) && j<=val)
temp++;
}
if(temp>maxnum)
return 0;
}
// now try to put every value
int ans = 0;
for(int i=1;i<=n;i++){
if((mask&(1<<i))){
ans+=rec(mask^(1<<i));
}
}
return dp[mask] = ans;
}
void solve(){
cin>>n>>m;
while(m--){
int x,y,z;
cin>>x>>y>>z;
mp[x].pb({y,z});
}
memset(dp,-1,sizeof dp);
int mask = 1<<(n+1);
mask--;
mask^=1;
cout<<rec(mask);
}
signed main()
{
FIO
int t;
t=1;
// cin>>t;
while(t--)
{
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
constexpr int MOD = 1000000007;
constexpr int INF = 1001001001;
constexpr bool is_multiple = false;
i64 count_between(i64 a, i64 b) {
return b - a + 1;
}
void solve() {
i64 n;
cin >> n;
i64 res = 0;
i64 base = 1000;
i64 cnt = 1;
while (base <= n) {
res += cnt * count_between(base, min(base*1000 - 1, n));
base *= 1000;
cnt++;
}
cout << res << endl;
}
int main() {
int t = 1;
if (is_multiple) cin >> t;
while (t--) solve();
return 0;
}
| #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
ll n;
int main(void){
scanf("%lld",&n);
ll ans=0;
ll rest=n;
int dig=1;
ll now=9;
ll su=0;
while(rest>0LL){
if(rest>=now){
ans+=now*((dig-1)/3LL);
rest-=now;
}else{
ans+=rest*((dig-1)/3LL);
rest=0;
}
dig++;
now*=10LL;
}
printf("%lld\n",ans);
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
#include <iomanip>
#include <limits>
#include <random>
#include <unordered_map>
typedef int64_t ll;
typedef std::pair<int, int> Pii;
typedef std::pair<ll, ll> Pll;
typedef std::pair<double, double> Pdd;
#define rip(_i, _n, _s) for (int _i = (_s); _i < (int)(_n); _i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<
template<typename _T>
using MaxHeap = std::priority_queue<_T>;
template<typename _T>
using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>;
template<typename _T>
inline bool chmax(_T &_l, const _T _b) {
if (_l < _b) {
_l = _b;
return true;
}
return false;
}
template<typename _T>
inline bool chmin(_T &_l, const _T _b) {
if (_l > _b) {
_l = _b;
return true;
}
return false;
}
template<typename _T>
void vdeb(const std::vector<_T> &bb) {
for (unsigned int i = 0;i < bb.size();i++) {
if (i == bb.size() - 1) std::cout << bb[i];
else std::cout << bb[i] << ' ';
}
std::cout << '\n';
}
template<typename _T>
void vdeb(const std::vector<std::vector<_T>> &bb) {
for (unsigned int i = 0;i < bb.size();i++) {
std::cout << i << ' ';
vdeb(bb[i]);
}
std::cout << '\n';
}
using namespace std;
int main() {
int n; cin >> n;
vector<ll> da1(n), da2(n);
ll ans = 0;
rip(i,n,0) {
cin >> da1[i];
}
rip(i,n,0) cin >> da2[i];
reverse(all(da1));
MinHeap<ll> pq;
rip(i,n,0) {
pq.push(da1[i]);
pq.push(da2[i]);
ans += da1[i] + da2[i];
ans -= pq.top();
pq.pop();
}
cout << ans << endl;
} | #include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
typedef long long ll;
cs int N = 4e5 + 5;
int n, a[N], p[N], c[N];
ll ans;
#define mid ((l + r) >> 1)
cs int M = N << 2;
int mn[M], ta[M];
void up(int x) {
mn[x] = min(mn[x << 1], mn[x << 1 | 1]) + ta[x];
}
void build(int x, int l, int r) {
if(l == r) {
mn[x] = l;
return;
}
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
up(x);
}
void mdf(int x, int l, int r, int L, int R) {
if(L <= l && r <= R)
return mn[x] --, ta[x] --, void();
if(L <= mid) mdf(x << 1, l, mid, L, R);
if(R > mid) mdf(x << 1 | 1, mid + 1, r, L, R);
up(x);
}
int qry(int x, int l, int r, int L, int R) {
if(L <= l && r <= R)
return mn[x];
int ans = 1e9;
if(L <= mid) ans = min(ans, qry(x << 1, l, mid, L, R));
if(R > mid) ans = min(ans, qry(x << 1 | 1, mid + 1, r, L, R));
return ans + ta[x];
}
#undef mid
void ins(int x, int c) {
int z = qry(1, 1, n, x, n);
if(z) {
ans += c;
mdf(1, 1, n, x, n);
}
}
int main() {
#ifdef FSYo
freopen("1.in", "r", stdin);
#endif
cin >> n;
for(int i = 1; i <= n + n; i++)
scanf("%d", &a[i]), p[i] = i;
sort(p + 1, p + n + n + 1,
[](cs int &i, cs int &j) {
return a[i] > a[j];
});
build(1, 1, n);
for(int i = 1; i <= n + n; i++) {
int x = p[i];
if(x <= n) {
ins(n - x + 1, a[x]);
}
else {
ins(x - n, a[x]);
}
}
cout << ans;
return 0;
} |
#include<bits/stdc++.h>
#include<cmath>
#define SZ(x) ((int)x.size())
#include<vector>
#include<set>
#include<map>
#define ll long long
#define lli long long int
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#include<string>
#include<cctype>
#define F first
#define S second
#include<queue>
#define ii pair<int,int>
#include <sstream>
#define lli long long int
#define pairs pair<int,int>
#define ld long double
#define mod 1000000007
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
ll pows(ll a,ll n,ll m)
{
ll res=1;
while(n)
{
if(n%2!=0)
{
res=(res*a)%m;
n--;
}
else
{
a=(a*a)%m;
n=n/2;
}
}
return res%m;
}
ll gcd(ll a,ll b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
bool isprime(ll n)
{
if(n==1)
{
return false;
}
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
bool istrue(string s)
{
int i=0;
int j=s.size()-1;
while(i<j)
{
if(s[i]==s[j])
{
i++;
j--;
}
else
{
return false;
}
}
return true;
}
ll n,m;
vector<ll>a,b;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
ll x;
for(ll i=0;i<n;i++)
{
cin>>x;
a.pb(x);
}
for(ll i=0;i<m;i++)
{
cin>>x;
b.pb(x);
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
vector<ll> diff1(n/2+1),diff2(n/2+1);
for(ll i=0;i<n/2;i++)
{
diff1[i+1]=diff1[i]+abs(a[2*i+1]-a[2*i]);
diff2[i+1]=diff2[i]+abs(a[2*i+1]-a[2*i+2]);
}
ll ans=1e18;
for(ll i=0;i<n;i++)
{
ll temp=0;
auto it=lower_bound(b.begin(),b.end(),a[i]);
if(it==b.begin())
{
temp=temp+abs(*it-a[i]);
}
else if(it==b.end())
{
it--;
temp+=abs(*it-a[i]);
}
else
{
temp=abs(*it-a[i]);
it--;
temp=min(temp,abs(*it-a[i]));
}
if(i%2==0)
{
temp+=diff1[i/2]+diff2[n/2]-diff2[i/2];
}
else
{
temp+=diff1[i/2]+abs(a[i+1]-a[i-1])+diff2[n/2]-diff2[(i/2)+1];
}
ans=min(ans,temp);
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int,int> P;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long double PI = acos(-1.0);
// cout << fixed << setprecision(14);
signed main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> H(N), W(M+2);
rep(i,N){
cin >> H[i];
}
rep(i,M){
cin >> W[i];
}
W[M] = -INF;
W[M+1] = INF;
sort(H.begin(), H.end());
sort(W.begin(), W.end());
vector<int> dp1(N/2+1), dp2(N/2+1);
rep(i,N/2){
dp1[i+1] = H[i*2+1] - H[i*2] + dp1[i];
dp2[i+1] = H[N-1-i*2] - H[N-2-i*2] + dp2[i];
}
reverse(dp2.begin(), dp2.end());
int ans = INF;
rep(i,N/2+1){
int Z = dp1[i] + dp2[i];
int botti = H[i*2];
auto itr = lower_bound(W.begin(), W.end(), botti);
int X = *itr;
--itr;
int Y = *itr;
Z += min(abs(botti - X), abs(botti - Y));
// cout << Z << endl;
chmin(ans, Z);
}
cout << ans << endl;
/*
rep(i,N/2+1){
cout << dp1[i] << ' ' << dp2[i] << endl;
}
*/
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};
const ll INF = (1ll<<62);
// const ld EPS = 1e-10;
// const ld PI = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;
int main(){
ll A, B, C;
cin >> A >> B >> C;
auto dp = make_vec(101, 101, 101, (ld)(-1));
auto rec = [&](auto self, ll x, ll y, ll z)->ld{
if(dp[x][y][z] >= -0.5) return dp[x][y][z];
if(x == 100 or y == 100 or z == 100){
return 0;
}
ld res = 0;
res += ((ld)x/(x+y+z)) * (self(self, x+1, y, z) + 1.0);
res += ((ld)y/(x+y+z)) * (self(self, x, y+1, z) + 1.0);
res += ((ld)z/(x+y+z)) * (self(self, x, y, z+1) + 1.0);
dp[x][y][z] = res;
return res;
};
cout << rec(rec, A, B, C) << endl;
return 0;
} | #include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i=0;i<n;++i)
#define ren(i,n) for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define gc getchar
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array
using namespace std;
dd dp[105][105][105];
ll vis[105][105][105];
dd solve(ll x,ll y,ll z){
if(x==100 or y==100 or z==100)return 0.0;
if(vis[x][y][z])return dp[x][y][z];
vis[x][y][z]=1;
dd a=0.0,b=0.0,c=0.0;
if(x) a=1+solve(x+1,y,z);
if(y) b=1+solve(x,y+1,z);
if(z) c=1+solve(x,y,z+1);
dd fa=0.0;
dd den=(x*1.0+y*1.0+z*1.0);
fa=a*(x*1.0)/den;
fa+=b*(y*1.0)/den;
fa+=c*(z*1.00)/den;
return dp[x][y][z]=fa;
}
int main(){
ll a,b,c;
prec(20);
cin>>a>>b>>c;
cout<<solve(a,b,c);
}
|
#include <iostream>
#include <bits/stdc++.h>
#include <bitset>
#define ll long long int
#define pi pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vll vector<vl>
#define vpi vector<pi>
#define vpll vector<pll>
#define vb vector<bool>
#define vii vector<vi>
#define ALL(v) v.begin(), v.end()
#define st string
#define si(n) cin >> n
#define dout(n) cout << n << "\n"
#define mod 1000000007
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
bool yes = false;
st s1, s2;
ll max(ll a, ll b, ll c)
{
return max(a, max(b, c));
}
struct comp
{
bool operator()(const pi &a, const pi &b)
{
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
}
};
bool check_pal(st &s)
{
int i = 0, j = s.length() - 1;
while (i < j)
{
if (s[i] != s[j])
return false;
i++, j--;
}
return true;
}
void swap(char &a, char &b)
{
char ch = a;
a = b;
b = ch;
return;
}
int dfs(vii &g, vi &vis, vi &co, int pos)
{
int mn = co[pos];
vis[pos] = 1;
f(i, 0, g[pos].size())
{
if (!vis[g[pos][i]])
mn = min(dfs(g, vis, co, g[pos][i]), mn);
}
return mn;
}
void seive(int n, vector<bool> &isprime)
{
isprime[0] = true;
isprime[1] = true;
f(i, 2, n + 1)
{
if (!isprime[i])
{
for (int j = 2; (j * i) < n + 1; j++)
isprime[j * i] = true;
}
}
}
int findfact( int n , int&a , int &b){
for( int i =2 ;i<n ; i++){
if( n%i == 0 && i!=n/i) {
a=i;
b = n/i ;
return 0;
}
}
return -1;
}
ll ignoref(vi & v, int mx , int fmx , vi & con, int idx){
ll op=0;
int n = v.size();
int prev = idx == 0? v[1]:v[0];
if( fmx==1){
f(i,1,n){
if( v[i]!=mx){
op+= abs(v[i]-prev);
//cout<<op<<" ";
prev = v[i];
}
}
}
else {
int index;
int mxcon=INT_MIN;
f(i,0,n){
if(v[i]==mx){
if( mxcon <con[i]){
index =i;
mxcon = con[i];
}
}
}
f(i,1,n){
if( i!= index){
op+= abs(v[i]-prev)*1ll;
// cout<<op<<" "<<v[i]<<" "<<prev<<" \n";
prev = v[i];
}
}
}
return op;
}
int main()
{
int n;
cin>>n;
vi v;
f(i,0,n){
int a;
cin>>a;
v.pb(a);
}
vector<pair<ll,ll>>pssum(n);
ll msf=0,mx=0;
f(i,0,n){
msf+=v[i];
mx = max(mx,msf);
pssum[i].first = mx;
pssum[i].second = msf;
}
ll ans = 0;
msf =0;
f(i,0,n){
ans = max(msf+pssum[i].first, ans);
msf+=pssum[i].second;
}
ans = max(msf,ans);
cout<<ans<<"\n";
}
| #include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
LL maxx[400000], arr[400000];
int main (){
LL n;
cin >> n;
for (LL i = 1;i <= n; ++ i){
scanf("%lld", &arr[i]);
}
LL max_ = arr[1];
maxx[1] = max_;
for (LL i = 2;i <= n; ++ i){
arr[i] += arr[i - 1];
maxx[i] = max_ = max(max_, arr[i]);
// cout << arr[i]<<" "<<maxx[i]<<endl;
}
LL x = 0, ans = 0;
for (LL i = 1;i <= n; ++ i){
// cout << x;
ans = max(ans, x + maxx[i]);
x += arr[i];
}
cout << ans;
return 0;
} |
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <string>
#include <bitset>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
//infの設定
#define INF32 2147483647
#define INF64 9223372036854775807
#define PRIME 1000000007
#define PI 3.141592653589793238
//repマクロ
#define rep(i,m,n) for (int i=(m); (i)<(int)(n); ++(i))
#define rrep(i,m,n) for (int i=(m); (i)>(int)(n); --(i))
//その他
#define eb emplace_back
#define ALL(v) v.begin(), v.end()
typedef long long ll;
typedef pair<long long,long long> Pl;
//#define int long long
//負閉路を含まない重み付きグラフのある点からのGraph_distを返す関数(ダイクストラ法) ↑とセット
int bfs(int n,vector<vector<int>>& g){
queue<int> que;
vector<bool> seen(2020,false);
que.push(n);
int count=0;
while(!que.empty()){
int v=que.front();
que.pop();
if(seen[v]) continue;
seen[v]=true;
count++;
for(int ne:g[v]){
que.push(ne);
}
}
return count;
}
int main(){
int N,M;
cin>>N>>M;
vector<vector<int>> g(2020);
rep(i,0,M){
int a,b; cin>>a>>b;
g[a-1].eb(b-1);
}
ll ans=0;
rep(i,0,N){
ans+=bfs(i,g);
}
cout<<ans<<endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
#include <map>
#include <memory>
#include <list>
#include <deque>
#include <queue>
#include <iomanip>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#include <string.h>
#include <functional>
#include <sstream>
using namespace std;
#define REP(i,n) for (decltype(n) i = 0; i < n; i++)
#define THE_MOD 1'000'000'007
using ll = long long;
using ivec = vector<int>;
using lvec = vector<long>;
using ipair = pair<int, int>;
using llvec = vector<ll>;
using llpair = pair<ll, ll>;
#define umap unordered_map
#define uset unordered_set
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { auto n = v.size(); REP(i,n) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &e : v) is >> e; return is; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
void in() {} template <typename T, typename... Args> void in(T& t, Args& ...args) { cin >> t; in(args...); }
void out() { cout << endl; } template <typename T, typename... Args> void out(const T& t, const Args& ...args) { cout << t; if (sizeof...(args)) cout << " "; out(args...); }
constexpr ll LLINF = numeric_limits<ll>::max();
template<class T> void sort(T& v) { sort(v.begin(), v.end()); }
template<class T> void rsort(T& v) { sort(v.begin(), v.end(), greater<typename T::value_type>()); }
void YN(bool b) { cout << (b ? "Yes" : "No") << endl; }
template <typename T = ll, typename U = ll>
vector<T> Dijkstra(const vector<vector<pair<U,T>>>& edges, U start, U numNodes)
{
using Node = pair<U,T>;
auto compare = [](Node a, Node b){ return a.second > b.second;};
priority_queue<Node, vector<Node>, decltype(compare)> q{compare};
vector<T> costs(numNodes, numeric_limits<T>::max());
q.emplace(start, 0);
while (!q.empty()) {
auto [from, cost] = q.top();
q.pop();
if (cost > costs[from]) continue;
for (const auto& edge : edges[from]) {
auto to = edge.first;
auto newCost = cost + edge.second;
if (newCost < costs[to]) {
costs[to] = newCost;
q.emplace(to, newCost);
}
}
}
return costs;
}
struct Range {
ll x;
ll y;
Range(ll x, ll y) : x(x), y(y)
{
if (y > x) swap(x,y);
}
struct Iterator {
using difference_type = ptrdiff_t;
using value_type = ll;
using pointer = ll*;
using reference = ll&;
using iterator_category = std::random_access_iterator_tag;
Iterator() = default;
Iterator(ll n) : n(n) {}
ll n;
bool operator!=(const Iterator&rhs) const { return n != rhs.n; }
bool operator==(const Iterator&rhs) const { return n == rhs.n; }
Iterator& operator++() { n++; return *this; }
Iterator& operator--() { n--; return *this; }
Iterator& operator+=(ll x) { n+=x; return *this; }
ll operator*() const { return n; }
ptrdiff_t operator-(const Iterator&rhs) const { return n - rhs.n; }
};
Iterator begin() { return Iterator(x); }
Iterator end() { return Iterator(y); }
};
ll extgcd(ll a, ll b, ll &x, ll &y)
{
/*
a = pb*q; // p = a / b, q = a % b
ax + by
= pbx + qx + by
= b(px + y) + qx
= bY + qX // Y = px + y, X = x -> y = Y - px
*/
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll run()
{
ll a,b; double w;in(a,b,w);
w *= 1000;
ll m = ceil(w/b);
ll M = floor(w/a);
if (M < m) {
out("UNSATISFIABLE");
return 0;
} else {
out(m,M);
return 0;
}
return 0;
}
int main()
{
cout << fixed << setprecision(15);
auto r = run();
//YN(r);
//out(r);
//cout << r << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;
void Main() {
ll n;
R n;
n*=2;
vector<ll> a[3];
rep(i,n) {
ll x;
char c;
cin >> x >> c;
if(c=='R') a[0].pb(x);
else if(c=='B') a[1].pb(x);
else a[2].pb(x);
}
rep(i,3) sort(all(a[i]));
P c[3];
rep(i,3) c[i]=P(a[i].size()%2,i);
sort(c,c+3,greater<P>());
ll ans=MAXL;
if(c[0].F) {
ll x=c[0].S,y=c[1].S,z=c[2].S;
rep(i,a[x].size()) {
ll k=lower_bound(all(a[y]),a[x][i])-a[y].begin();
if(k<a[y].size()) ans=min(ans,abs(a[x][i]-a[y][k]));
if(k) ans=min(ans,abs(a[x][i]-a[y][k-1]));
}
vector<P> v[2];
rep(i,a[z].size()) {
ll k=lower_bound(all(a[x]),a[z][i])-a[x].begin();
if(k<a[x].size()) {
ll d=abs(a[z][i]-a[x][k]);
v[0].pb(P(d,i));
}
if(k) {
ll d=abs(a[z][i]-a[x][k-1]);
v[0].pb(P(d,i));
}
}
rep(i,a[z].size()) {
ll k=lower_bound(all(a[y]),a[z][i])-a[y].begin();
if(k<a[y].size()) {
ll d=abs(a[z][i]-a[y][k]);
v[1].pb(P(d,i));
}
if(k) {
ll d=abs(a[z][i]-a[y][k-1]);
v[1].pb(P(d,i));
}
}
rep(i,2) sort(all(v[i]));
rep(i,min(3,(int)v[0].size())) {
rep(j,min(3,(int)v[1].size())) {
if(v[0][i].S!=v[1][j].S) ans=min(ans,v[0][i].F+v[1][i].F);
}
}
} else ans=0;
pr(ans);
}
int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
| #include <bits/stdc++.h>
using namespace std;
//.define
// #define FILE_IN_OUT
#define RET(_x) cout << (_x) << '\n'; return;
#define all(_obj) (_obj).begin(), (_obj).end()
#define loop(_n) for (int i = 0; i < (_n); ++i)
#define sz(_obj) static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x) std::cerr << (#_x) << " = " << (_x) << '\n' << std::flush;
#else
#define debug(_x) {}
#endif
#define endl "\n"
template<typename P, typename Q>
inline P CeilDiv(P _dividend, Q _divider) {
return static_cast<P>((_dividend - 1LL + _divider) / _divider);
}
template<typename Tp>
inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") {
for (Tp current = _begin; current != _end; ++current) {
std::cout << *current << _delim;
}
std::cout << '\n';
}
//.constant
using ll = int64_t;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<int>(1e9 + 7);
//.data
const vector<int> primes{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
//.code
void SolveTask() {
ll a, b;
cin >> a >> b;
vector<int> arr(b - a + 1, 0);
for (ll i = a; i <= b; ++i) {
for (int j = 0; j < sz(primes); ++j) {
if (i % primes[j] == 0) {
arr[i - a] |= 1 << j;
}
}
}
const int full_mask = 1 << sz(primes);
vector<ll> dp(full_mask, 0);
dp[0] = 1;
loop(sz(arr)) {
for (int mask = 0; mask < full_mask; ++mask) {
if ((mask & arr[i]) == 0) {
dp[mask | arr[i]] += dp[mask];
}
}
}
cout << accumulate(all(dp), 0LL) << endl;
}
int main() {
#ifdef FILE_IN_OUT
std::ifstream cin ("input.txt", std::ios::in);
std::ofstream cout("output.txt", std::ios::out);
#else
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int yt = 1; yt <= test_cases; ++yt) {
SolveTask();
}
#ifdef FILE_IN_OUT
cin.close();
cout.close();
#endif
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template < typename T_container, typename T = typename enable_if < !is_same<T_container, string>::value, typename T_container::value_type >::type >
ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << "\b\b]\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ", "; dbg_out(T...);}
#define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)
const int N = 200;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
map <int, int> mp;
ll ans = 0;
for (int i = 0; i < n; ++i) {
int a; cin >> a; a %= N;
ans += mp[a];
mp[a]++;
}
cout << ans;
return 0;
}
| #include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <bitset>
#include <cmath>
#include <algorithm>
#include <climits>
using namespace std;
typedef long long ll;
//infの設定
#define INF32 2147483647
#define INF64 9223372036854775807
//repマクロ
#define rep(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
int main(){
ll N,A;
static ll a[202020];
cin>>N;
rep(i,0,N){
cin>>A;
a[i]=A%200;
//cout<<a[i]<<" ";
}
sort(a,a+N);
/*for(int i=0;i<N;i++){
cout<<a[i]<<" ";
}*/
//cout<<endl;
ll count=0;
ll num=0;
ll a0=a[0];
a[N]=201;
rep(i,1,N+1){
if(a[i]==a0){
num++;
//cout<<i<<" "<<a[i]<<endl;
}else{
count+=(num*(num+1))/2;
//cout<<a0<<" ga "<<num+1<<" ko"<<endl;
num=0;
a0=a[i];
}
}
cout<<count<<endl;
return 0;
} |
#include <bits/stdc++.h>
signed main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::string s;
std::cin >> n >> s;
int ans = 0;
for (int i = 0; i < n; i++) {
int at = 0, gc = 0;
for (int j = i; j < n; j++) {
if (s[j] == 'A') ++at;
else if (s[j] == 'T') --at;
else if (s[j] == 'G') ++gc;
else --gc;
ans += !(at || gc);
}
}
std::cout << ans;
return 0;
}
| #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<functional>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
bool isprime(int p) {
if (p == 1) return false;
for (int i = 2; i < p; i++) {
if (p % i == 0) return false;
}
return true;
}
ll gcd(ll a, ll b) {
if (a < b)swap(a, b);
if (a % b == 0)return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll keta(ll n) {
ll res = 0;
while (n >= 1) {
res += n % 10; n /= 10;
}
return res;
}
ll modpow(ll x, ll y) {
ll res = 1;
while (y) {
if (y % 2) { res *= x; res %= mod; }
x = x * x % mod; y /= 2;
}
return res;
}
ll nCk(ll n, ll k) {
ll a = 1, b = 1;
for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
return a * modpow(b, mod - 2) % mod;
}
//printf("%.10f\n", n);
string s, t;
ll n, m, cnt, ans;
ll ruia[123456], ruit[123456], ruic[123456], ruig[123456];
signed main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n >> s;
rep(i, n) {
ruia[i + 1] = ruia[i];
ruit[i + 1] = ruit[i];
ruic[i + 1] = ruic[i];
ruig[i + 1] = ruig[i];
if (s[i] == 'A') { ruia[i + 1]++; }
if (s[i] == 'T') { ruit[i + 1]++; }
if (s[i] == 'C') { ruic[i + 1]++; }
if (s[i] == 'G') { ruig[i + 1]++; }
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
ll a, t, c, g;
a = ruia[j] - ruia[i];
t = ruit[j] - ruit[i];
c = ruic[j] - ruic[i];
g = ruig[j] - ruig[i];
//cout << a << ' ' << t << ' ' << c << ' ' << g << endl;
if (a == t && c == g) {
ans++;
//cout << i << ' ' << j << endl;
}
}
}
cout << ans << endl;
return 0;
} |
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
//setfill - cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define rall(x) (x).rbegin, (x).rend() //reverse traversal
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
int main(){
fastio;
int t;
cin>>t;
cout<<((t+100-1)/100)<<endl;
} | #include<bits/stdc++.h>
using namespace std;
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define All(x) (x).begin(),(x).end()
#define mp make_pair
#define nl "\n"
typedef pair<int,int>ii;
typedef vector<int>vi;
typedef vector<ii>vii;
typedef vector<vi>vvi;
typedef vector<long long>vl;
typedef vector <vl>vvl;
template<class T>
void print(T& a)
{
for(auto x:a)
cout<<x<<" ";
cout<<"\n";
}
const int MX=2e5+5;
int to[MX];
int findto(int x)
{
if(to[x]==x)return x;
else to[x]=findto(to[x]);
return to[x];
}
int main()
{
FastIO
int n,ans=0; cin>>n;
int a[n];
for(int &x:a)cin>>x;
iota(to,to+MX,0);
for(int i=0; i<(n/2); i++)
{
int x=findto(a[i]);
int y=findto(a[n-1-i]);
if(x==y)continue;
else
{
ans++;
to[x]=y;
}
}
cout<<ans<<nl;
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define ALL(a) (a).begin(),(a).end()
typedef long long int ll;
typedef string str;
typedef pair<ll, ll> P;
using Graph = vector<vector<int>>;
const int dy[] = { 0, 0, 1, -1 }, dx[] = { 1, -1, 0, 0 };
const ll INFL = 1ll << 60, INF = 1 << 30, mod = 1e9 + 7;
ll h, w, a, b; ll sum = 0;
ll solve(ll temp, ll ya, ll xa, ll e, vector<vector<ll>> v) {
if (e == 0) { sum++; return 0; }
if (temp == 1) { v[ya][xa] = 1; v[ya][xa + 1] = 1; }
if (temp == 2) { v[ya][xa] = 1; v[ya + 1][xa] = 1; }
rep(i, h) {
rep(j, w) {
if (j + 1 < w)if ((v[i][j] | v[i][j + 1]) == 0)solve(1, i, j, e - 1, v);
if (i + 1 < h)if ((v[i][j] | v[i + 1][j]) == 0)solve(2, i, j, e - 1, v);
}
}
return 0;
}
int main() {
//Code Line
ll k=1;
cin >> h >> w >> a >> b;
vector<vector<ll>> v(h,vector<ll>(w,0));
solve(0,0,0,a,v);
srep(i, 1, a + 1)k *= i;
cout << sum / k << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
int h,w,a,b;
ll ans = 0;
vector < vector <int> > arr;
unordered_map <string, ll > mp;
int cal(int x,int y,int a,int b,vector < vector <int> > &arr, ll mask)
{
string s=to_string(x)+" "+to_string(y)+" "+to_string(a)+" "+to_string(b)+" "+to_string(mask);
//cout<<" AAO MERE GAO ME BHAI LOG "<<s<<endl;
int ans=0;
if(mp.find(s)!=mp.end())
return mp[s];
if(y==w)
return mp[s]=cal(x+1,0,a,b,arr,mask);
if(x==h)
{
if(a==b&&a==0)
return mp[s]=1;
return mp[s]=0;
}
if(arr[x][y]==1)
return mp[s]=cal(x,y+1,a,b,arr,mask);
//take one
if(b)
{
arr[x][y]=1;
ans+=cal(x,y+1,a,b-1,arr,(mask)|(1<<(x*w+y)));
arr[x][y]=0;
}
if(a&&y+1<w&&(arr[x][y+1]==0))
{
arr[x][y]=1;
arr[x][y+1]=1;
ans+=cal(x,y+1,a-1,b,arr,(mask)|(1<<(x*w+y))|(1<<(x*w+y+1)));
arr[x][y+1]=0;
arr[x][y]=0;
}
if(a&&x+1<h&&(arr[x+1][y]==0))
{
arr[x][y]=1;
arr[x+1][y]=1;
ans+=cal(x,y+1,a-1,b,arr,(mask)|(1<<(x*w+y))|(1<<(((x+1)*w)+y)));
arr[x+1][y]=0;
arr[x][y]=0;
}
mp[s]=ans;
// cout<<" anas is "<<ans<<endl;
return ans;
}
int main()
{
int t=1;
// cin>>t;
while(t--)
{
cin>>h>>w>>a>>b;
arr.resize(h,(vector <int> (w,0)));
cout<<cal(0,0,a,b,arr,0)<<endl;
//cout<<ans<<endl;
}
return 0;
}
|
//khodaya khodet komak kon
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define lc id << 1
# define rc id << 1 | 1
# define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 2e2 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const ld eps = 1e-15;
const int mod = 998244353;
const int base = 257;
ll n, f[xn], m, x, y, nn;
bool mark[xn];
vector <int> vec;
int main(){
InTheNameOfGod;
cin >> n, nn = n;
f[0] = f[1] = m = 1;
while (f[m] + f[m - 1] <= n)
++ m, f[m] = f[m - 1] + f[m - 2];
for (int i = m; i >= 0; -- i)
if (f[i] <= nn)
mark[i] = true, nn -= f[i];
m += m % 2;
for (int i = 0; i < m; ++ i){
if (i % 2 == 0){
if (mark[m - i])
vec.push_back(1), ++ x;
vec.push_back(4), y += x;
}
else{
if (mark[m - i])
vec.push_back(2), ++ y;
vec.push_back(3), x += y;
}
}
if (mark[0])
++ x, vec.push_back(1);
cout << SZ(vec) << endl;
for (int val : vec)
cout << val << endl;
if (x != n)
kill("FUCK");
return 0;
}
| #include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout << fixed << setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
// clang-format on
ll fibrest(ll x0, ll y0, ll n, bool isx) {
ll iter = 0;
vector<ll> fib = {x0, y0};
ll x = x0;
ll y = y0;
ll op = 0;
while (true) {
if (isx) {
x = x + y;
} else {
y = y + x;
}
if (x >= n) break;
op++;
isx = !isx;
}
return op;
}
void op(vector<ll> &ops, ll &x, ll &y, ll t) {
if (t == 1) {
x = x + 1;
}
if (t == 2) {
y = y + 1;
}
if (t == 3) {
x = x + y;
}
if (t == 4) {
y = y + x;
}
ops.push_back(t);
}
void output(vector<ll> &ops) {
ll k = ops.size();
print(ops.size());
for (auto ope : ops) {
print(ope);
}
}
int main() {
ioinit();
ll n;
cin >> n;
// while (fib.back() < n) {
// fib.push_back(fib[fib.size() - 1] + fib[fib.size() - 2]);
// }
// ll m = fib.size();
ll x = 0;
ll y = 0;
vector<ll> ops;
if (n < 120) {
while (x < n) {
op(ops, x, y, 1);
}
output(ops);
return 0;
}
op(ops, x, y, 2);
bool isx = true;
while (true) {
if (x + y > n) {
while (x < n) {
op(ops, x, y, 1);
}
break;
}
ll curop = fibrest(x, y, n, isx);
{
ll k = 0;
while (true) {
ll r = fibrest(x + k + 1, y, n, isx);
if (r != curop) break;
//perr(curop, r);
op(ops, x, y, 1);
k = k + 1;
}
}
if (isx) {
op(ops, x, y, 3);
} else {
op(ops, x, y, 4);
}
isx = !isx;
if (x >= n) break;
}
output(ops);
}
|
//ABC_196_E_
//
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define reps(i,s,n) for(int i=s;i<n;++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main()
{
int n,q;
cin >> n;
const ll INF = ll(1)<<60;
ll l = -INF, r = INF;
ll s = 0;
rep(i,n)
{
ll a,t;
cin >> a >> t;
if(t == 1)
{
l += a;
r += a;
s += a;
}
else if(t ==2)
{
l = max(l,a);
r = max(r,a);
}
else
{
l = min(l,a);
r = min(r,a);
}
}
cin >> q;
rep(i,q)
{
ll x;
cin >> x;
x += s;
ll ans = x;
if(x <= l) ans = l;
else if(r <= x) ans = r;
printf("%ld\n",ans);
}
return 0;
}
| #include<algorithm>
#include<iostream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define neinf 0xc0c0c0c0c0c0c0c0ll
#define inf 0x3f3f3f3f3f3f3f3fll
#define cap(i) ((i-1)%500+1)
#define cp(i) ((i-1)/500+1)
#define uint unsigned int
#define ull unsigned ll
#define ll long long
#define reg register
#define HgS 1000000007
#define db double
#define il inline
#define gc getchar
#define pc putchar
#define len 200000
#define siz 500
il ll rd()
{
reg ll res=0,lab=1;
reg char ch=gc();
while((ch<'0'||ch>'9')&&ch!=EOF)
{if(ch=='-')lab=-lab;ch=gc();}
while(ch>='0'&&ch<='9')
res=(res<<3)+(res<<1)+(ch&15),ch=gc();
return res*lab;
}
il void prt(ll x,char t='\n')
{
char ch[70];int tp=0;
if(!x){fputs("0",stdout);if(t)pc(t);return;}
if(x<0)pc('-'),x=-x;
while(x)ch[++tp]=(x%10)^48,x/=10;
while(tp)pc(ch[tp--]);
if(t)pc(t);
}
il ll umax(ll a,ll b){return a>b?a:b;}
il ll umin(ll a,ll b){return a<b?a:b;}
ll n,a[len+5],t[len+5],q,x[len+5];
ll mnx=inf,mxx=neinf,add;
int main()
{
//memset(mnx,0x3f,sizeof(mnx));memset(mxx,0xc0,sizeof(mxx));
n=rd();for(int i=1;i<=n;++i)a[i]=rd(),t[i]=rd();
q=rd();
for(int i=1;i<=q;++i)
x[i]=rd(),mnx=umin(mnx,x[i]),mxx=umax(mxx,x[i]);
for(int i=1;i<=n;++i)
{
switch(t[i])
{
case 1:
add+=a[i];mnx+=a[i];mxx+=a[i];break;
case 2:mnx=umax(mnx,a[i]);mxx=umax(mxx,a[i]);break;
case 3:mnx=umin(mnx,a[i]);mxx=umin(mxx,a[i]);break;
}
}
for(int i=1;i<=q;++i)prt(umin(mxx,umax(mnx,x[i]+add)));
return 0;
} |
//#pragma GCC target("avx2")
//#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr)
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
const ll MOD=1e9+7;
const ll LINF=1e18;
const int INF=0x3f3f3f3f;
const int MAXE=5000050;
const int MAXN=2000050;
const int bk_sz=3420;
mt19937 rnd(114514);
ll fac[MAXN],inv[MAXN];
ll mpow(ll a,ll n){
ll ans=1;
while(n){
if(n&1){
ans*=a;
ans%=MOD;
}
a*=a;
a%=MOD;
n>>=1;
}
return ans;
}
void prework(int n){
fac[1]=1;
fac[0]=1;
for(ll i=1;i<=n;i++){
fac[i]=fac[i-1]*i;
fac[i]%=MOD;
}
inv[0]=1;
inv[n]=mpow(fac[n],MOD-2);
for(ll i=n-1;i;i--){
inv[i]=inv[i+1]*(i+1)%MOD;
}
}
ll getC(int n,int m){
if(n<m||n<0||m<0)return 0ll;
return fac[n]*inv[m]%MOD*inv[n-m]%MOD;
}
void mainwork(int T){
ll n,m,k;
scanf("%lld%lld%lld",&n,&m,&k);
prework(n+m);
if(m<=n-k-1){
puts("0");
return;
}
ll ans=getC(n+m,m)-getC(n+m,m+k+1);
ans=(ans+MOD)%MOD;
printf("%lld\n",ans);
}
signed main(){
//IOS;
#ifndef ONLINE_JUDGE
freopen("Ranulx.in","r",stdin);
freopen("Ranulx.out","w",stdout);
#endif
int t=1;
//scanf("%d",&t);
for(int i=1;i<=t;i++){
mainwork(i);
}
} | #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iomanip>
#include <functional>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define REP(i,n) for (ll i = 1; i <= (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define fcout cout<<fixed<<setprecision(15)
#define PI 3.1415926s535897932384626433832795028841971693993751058209749445923078160628620899
const ll MOD = 1000000007;
const ll INF = 1000000000000000000;
ll mod(ll N, ll A) {
return (N + INF) % A;
}
ll modpow(ll N, ll P, ll M) {
if (P == 0) return 1;
if (P % 2 == 0) return modpow(N, P / 2, M) * modpow(N, P / 2 , M) % M;
return N * modpow(N, P - 1, M) % M;
}
bool isPrime(ll N) {
ll T = sqrt(N);
REP(i, T) {
if (N % i == 0) return false;
}
return true;
}
ll GCD(ll A, ll B) {
if (A % B == 0) return B;
return GCD(B, A % B);
}
ll LCM(ll A, ll B) {
return A / GCD(A, B) * B;
}
ll Digit(ll N) {
string S = to_string(N);
return S.size();
}
ll getSumDigit(ll N) {
string S = to_string(N);
ll A = 0;
rep(i, S.size()) A += S[i] - '0';
return A;
}
ll N, C, a[200010];
int main() {
cin >> N;
ll P = modpow(2, N, MOD);
ll M1 = 0;
rep(i, P) {
cin >> a[i];
M1 = max(a[i], M1);
}
ll K;
rep(i, P) if (M1 == a[i]) K = i;
if (0 <= K && K <= P / 2 - 1) {
ll M2 = 0;
for (ll i = P / 2; i < P; i++) M2 = max(a[i], M2);
for (ll i = P / 2; i < P; i++) if (M2 == a[i]) cout << i + 1 << endl;
}
else {
ll M2 = 0;
rep(i, P / 2) M2 = max(a[i], M2);
rep(i, P / 2) if (M2 == a[i]) cout << i + 1 << endl;
}
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pll = pair <ll, ll>;
#define FIO() ios_base::sync_with_stdio(0);cin.tie(NULL);
#define endl '\n'
int main() {
FIO();
ll r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
if (r1 == r2 and c1 == c2) {
cout << 0 << '\n';
return 0;
}
if (abs(r1 - r2) + abs(c1 - c2) <= 3) {
cout << 1 << '\n';
return 0;
}
if (r1 + c1 == r2 + c2) {
cout << 1 << '\n';
return 0;
}
if (r1 - c1 == r2 - c2) {
cout << 1 << '\n';
return 0;
}
if ((r1 + c1) % 2 == (r2 + c2) % 2) {
cout << 2 << '\n';
return 0;
}
ll ans = 9e18;
for (int x = -3; x <= 3; x++) {
for (int y = -3; y <= 3; y++) {
int rr = r1 + x, cc = c1 + y;
int d = abs(rr - r1) + abs(cc - c1);
if (d > 3) continue;
if ((rr + cc) % 2 != (r2 + c2) % 2) continue;
ll temp = 1;
if (abs(rr - r2) + abs(cc - c2) <= 3) {
temp += 1;
} else if (rr + cc == r2 + c2) {
temp += 1;
} else if (rr - cc == r2 - c2) {
temp += 1;
} else temp += 2;
ans = min(ans, temp);
}
}
cout << ans << '\n';
}
| #include <bits/stdc++.h>
#include <cstdint>
using namespace std;
int main() {
long long A, B, C, D;
cin >> A >> B;
cin >> C >> D;
C -= A;
D -= B;
C = abs(C);
D = abs(D);
long long x = max(C,D);
long long y = min(C,D);
C = x;
D = y;
if(x == 0 && y == 0) {
cout << "0";
}
else if(x==y || x+y <= 3) {
cout << "1";
}
else {
x = (C+D)/2;
y = (C+D)/2;
if((C==x&&D==y) || abs(C-x)+abs(D-y) <= 3 || abs(C-x)==abs(D-y)) {
cout << "2";
}
else {
cout << "3";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
int n; cin >> n;
unordered_map<int, bool> mp;
int current = 0;
for(int i = 0; i < n; ++i) {
int tmp; cin >> tmp;
mp[tmp] = true;
while(mp[current]) ++current;
printf("%d\n", current);
}
return 0;
} | #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#include<iomanip>
#include<list>
#include<deque>
using namespace std;
//#define MODE 1
#ifdef MODE
#define DEB(X) cout<< #X <<": "<<X<<" ";
#define DEB2(X) cout<<X<<" ";
#define END cout<<endl;
#else
#define DEB(X) {}
#define DEB2(X) {}
#define END {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
//typedef std::pair<int,int> P;
struct edge{int to,cost;};
const int INF=100000000000000000;
const int INF2=9223372036854775807;
const int MOD=1000000007;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define min(X,Y) (((int)(X)<(int)(Y))?(X):(Y))
#define max(X,Y) (((int)(X)>(int)(Y))?(X):(Y))
#define NP(X,Y) next_permutation(X,Y)
#define setdouble(X,Y) cout<<fixed<<setprecision(X)<<Y
int ceil2(int a,int b){if(a%b==0){return a/b;}else{return a/b+1;}}
int pow2(int a,int b){int r=1;for(int i=1;i<=b;i++){r*=a;}return r;}
int Log2(int a){int t=0;while(1){if(a==0||a==1){break;}a/=2;t++;}return t;}
int N;
int A[200010];
set<int> U;
signed main(){
cin>>N;
REP(i,N){
cin>>A[i];
}
for(int i=0;i<=200010;i++){
U.insert(i);
}
REP(i,N){
U.erase(A[i]);
cout<<*U.begin()<<endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int main()
{
char s, t;
cin >> s >> t;
if(s == 'Y') cout << char(t - 32) << endl;
else cout << t << endl;
}
| /*#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("inline")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")*/
#include <bits/stdc++.h>
#define int long long
#define double long double
#define x first
#define y second
#define yy cout<<"YES\n"
#define nn cout<<"NO\n"
#define rt return
#define ct continue
#define br break
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define mmod 1000000007
#define pmod 998244353
using namespace std;
int gcd(int a,int b){
while (b) b^=a^=b^=a%=b;
return a;
}
int lcm(int a,int b){
return a*b/gcd(a,b);
}
int bigmod(int b,int p,int m){
int ans=1;
b%=m;
if (!b) return 0;
while (p){
if (p&1) ans=(ans*b)%m;
p>>=1;
b=(b*b)%m;
}
return ans;
}
int ncr(int n,int r){
if (n<r) return 0ll;
int ans=1,k=min(n-r,r);
for (int i=1; i<=k; i++) ans=ans*(n-i+1)/i;
return ans;
}
double eu(int x1,int y1,int x2,int y2){
int dx=abs(x1-x2),dy=abs(y1-y2);
return sqrt(dx*dx+dy*dy);
}
int ham(string s,string t){
int ans=0;
for (int i=0; i<s.size(); i++) ans+=(s[i]!=t[i]);
return ans;
}
int ce(int a,int b){
if (a%b==0) return a/b;
else return a/b+1;
}
bool cmp(){
return 1;
}
void gen(){
}
void solve(){
char s,t;
cin>>s>>t;
if (s=='Y'){
cout<<char(t-'a'+'A');
} else {
cout<<t;
}
}
signed main(){
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while (t--) solve();
} |
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const int mod=1e9+7;
const int maxV=2e7+1;
const ll inf=1e18+1;
int main(){
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll n;
cin>>n;
ll p10=10;
int cnt=0;
for(int i=1;i<1e6;i++){
if(i==10 || i==100 || i==1000 || i==10000 || i==100000){
p10*=10;
}
if(i*p10+i<=n){
cnt++;
}
}
cout<<cnt<<"\n";
} | #include <iostream>
#include <string>
using namespace std;
using ll = long long;
int main(){
ll N;
cin >> N;
for(ll i = 1; ; i++) if(stoll(to_string(i) + to_string(i)) > N){
cout << i - 1 << endl;
return 0;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
typedef long long ll;
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const vector<T>&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.first>>v.second;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.first<<","<<v.second;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>T dup(T x, T y){return (x+y-1)/y;}
template<typename T>ll suma(const vector<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
int keta(ll n) { int ret = 0; while (n>0) { n/=10; ret++; } return ret; }
#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) { cerr << head; dump(); }
template <typename Head, typename... Tail> void dump(Head &&head, Tail &&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); }
#define debug(...) do { cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false)
#else
#define dump(...)
#define debug(...)
#endif
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
const ll LINF = 1LL << 60;
const int INF = 1001001001;
/////////////////////////////////////////////////////////////////////
void solve()
{
ll n,k; cin>>n>>k;
vector<vector<ll>> dp(3, vector<ll>(n*3+1));
for (int i=1; i<=n; i++) {
dp[0][i] = 1;
}
rep(i, 2) {
vector<ll> s = dp[i];
rep(j, 3*n) {
s[j+1] += s[j];
dp[i+1][j+1] = s[j] - s[max(0LL, j-n)];
}
}
vector<ll> s = dp[2];
rep(i, 3*n) s[i+1] += s[i];
auto it = lower_bound(s.begin(), s.end(), k);
ll v = it - s.begin();
ll u = k - s[v-1] - 1;
ll a = 1;
ll cnt = 0;
for (ll i=1; i<=min(v, n); i++) {
a = i;
ll t = v - i;
if (t < 2 || 2*n < t) continue;
ll g = min(2*n-t+1, t-1);
if (cnt + g > u) break;
cnt += g;
}
v -= a;
u -= cnt;
for (ll i=1; i<=n; i++) {
ll t = v - i;
if (t < 1 || n < t) continue;
if (u <= 0) {
printf("%lld %lld %lld\n", a, i, t);
break;
}
u--;
}
}
int main()
{
int t;
t = 1;
// cin>>t;
while (t--) solve();
return 0;
}
| #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int a[1001], b[1001], c[2001], p, k, j, n, m, i;
cin >> n >> m;
for (i = 0; i < n; i++)
cin >> a[i];
for (i = 0; i < m; i++)
cin >> b[i];
p = 0;
for (i = 0; i < n; i++)
{
k = 0;
for(j = 0; j < m; j++)
if(a[i] == b[j])
k++;
if (k==0)
{
c[p] = a[i];
p++;
}
}
for (i = 0; i < m; i++)
{
k = 0;
for(j =0; j< n; j++)
if(b[i] == a[j])
k++;
if (k==0)
{
c[p] = b[i];
p++;
}
}
sort(c, c + p);
for (i = 0; i < p; i++)
cout << c[i] << " ";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
// #define LOCAL
typedef long long ll;
#ifdef LOCAL
const int N = 105;
#else
const int N = 2e5 + 105;
#endif
#define mid (l + r >> 1)
#define ls (rt << 1)
#define rs (rt << 1 | 1)
const int M = N + N;
int a[N], b[N], n;
int tr[M << 2];
void init(){
set<int> se;
for(int i = 1; i <= n; ++i) cin >> a[i], se.insert(a[i] + i);
for(int i = 1; i <= n; ++i) cin >> b[i], se.insert(b[i] + i);
map<int, int> ma;
int tot = 0;
for(auto it : se){
ma[it] = ++tot;
}
for(int i = 1; i <= n; ++i) a[i] = ma[a[i] + i];
for(int i = 1; i <= n; ++i) b[i] = ma[b[i] + i];
}
set<int>pos[N + N];
int p[N];
void add(int rt, int l, int r, int idx){
if(l == r) { tr[rt]++; return; }
if(idx <= mid) add(ls, l, mid, idx);
else add(rs, mid + 1, r, idx);
tr[rt] = tr[ls] + tr[rs];
}
int qr(int rt, int l, int r, int L ,int R){
int res = 0;
if(L <= l && R >= r){ return tr[rt]; }
if(L <= mid) res += qr(ls, l, mid, L, R);
if(R > mid) res += qr(rs, mid + 1, r, L, R);
return res;
}
void solve() {
cin >> n;
init();
for(int i = 1; i <= n; ++i) pos[a[i]].insert(i);
for(int i = 1; i <= n; ++i){
if(pos[b[i]].size() == 0) {
cout << -1 << endl; return;
}
auto it = pos[b[i]].begin();
p[i] = *it;
pos[b[i]].erase(it);
}
ll ans = 0;
for(int i = 1; i <= n; ++i){
ans += qr(1, 1, n, p[i], n);
add(1, 1, n, p[i]);
}
cout << ans << endl;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
cout << "start Debug" << endl;
#endif
ios::sync_with_stdio(0), cin.tie(0);
int _ = 1;
// cin >> _;
for(int i = 1; i <= _; ++i) solve();
} | #include<iostream>
#include<list>
#include<map>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
auto itr=v.begin();
while(itr!=v.end()){
if(itr!=v.begin())cout << " ";
cout << *(itr++);
}
return os;
};
// pair 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const pair<T,S>& p){
cout << "(" << p.first << "," << p.second << ")";
return os;
}
// map 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const map<S,T>& mp){
for(const pair<S,T>& x: mp){
cout << "[" << x.first << "]=" << x.second << " ";
}
return os;
}
struct segtree{
int n;
vector<int> vec;
segtree(int nn){
n=nn;
vec.resize(2*n,0);
}
int get(int i){ return vec[i+n]; };
void set(li a,int i){
i+=n;
vec[i]=a;
while(i>1){
i/=2;
vec[i]=vec[i*2]+vec[i*2+1];
}
};
int sum(int i,int j){
// [i,j)
if(df)print(i,j);
int s=0;
i=i+n; j=j+n;
while(i<j){
if(i%2){ if(df) print(i); s+=vec[i]; i++; }
if(j%2){ j--; s+=vec[j]; if(df) print(j); }
i/=2; j/=2;
}
return s;
}
};
std::ostream& operator<<(std::ostream& os, const segtree& s){
rep(i,2*s.n) printf("%ld ",s.vec[i]);
return os;
}
int judge(map<li,int>& mpa,map<li,int>& mpb){
int f=1;
int t=0;
for(auto& x:mpa){
if(mpb[x.first]!=x.second) f=0;
if(df)print(x,mpb[x.first],"->",f);
}
return f;
}
int main(){
int n; cin >>n;
vector<li> a(n);
map<li,int> mpa;
rep(i,n){
li c; cin >>c;
a[i]=c+i;
mpa[a[i]]++;
}
if(df)print(mpa);
vector<li> b(n);
map<li,int> mpb;
rep(i,n){
li c; cin >>c;
b[i]=c+i;
mpb[b[i]]++;
}
if(df)print(mpb);
if(!judge(mpa,mpb)){
print(-1);
return 0;
}
map<li,list<int>> mp;
rep(i,n){
mp[b[i]].push_back(i);
}
if(df)print(mp);
rep(i,n){
int t=mp[a[i]].front();
mp[a[i]].pop_front();
a[i]=t;
}
if(df)print(a);
segtree sgt(n);
li ans=0;
rep(i,n){
ans+=sgt.sum(a[i],n);
sgt.set(1,a[i]);
if(df)print(sgt, sgt.sum(a[i],n));
}
print(ans);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define eps 1e-9
#define INF 2000000000 // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...) \
cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif
// ---------------- Utility ------------------
template <class T>
bool chmin(T &a, const T &b) {
if (a <= b) return false;
a = b;
return true;
}
template <class T>
bool chmax(T &a, const T &b) {
if (a >= b) return false;
a = b;
return true;
}
template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
vector<T> vect(int len, T elem) {
return vector<T>(len, elem);
}
// ----------------- Input -------------------
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
return is >> p.first >> p.second;
}
template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
for (int i = 0; i < vec.size(); i++) is >> vec[i];
return is;
}
// ----------------- Output ------------------
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << p.first << ',' << p.second;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &e : v) os << e << " ";
return os;
}
template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
for (const T &e : d) os << e << " ";
return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
os << "{ ";
for (const T &e : s) os << e << " ";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
os << "{ ";
for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
return os << "}";
}
template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
(..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}
template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
os << "(";
dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
return os << ")";
}
void dump_str_rec(ostringstream &) {}
template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
oss << ", " << head;
dump_str_rec(oss, forward<Tail>(tail)...);
}
template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
ostringstream oss;
oss << arg;
dump_str_rec(oss, forward<U>(args)...);
return oss.str();
}
// --------------- Fast I/O ------------------
void fastio() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
}
// ------------ End of template --------------
#define endl "\n"
bool check(int x) {
int tmp = x;
while (tmp) {
if (tmp % 10 == 7) return false;
tmp /= 10;
}
tmp = x;
while (tmp) {
if (tmp % 8 == 7) return false;
tmp /= 8;
}
return true;
}
void solve() {
int ans = 0;
int N;
cin >> N;
for (int i = 1; i <= N; i++) {
if (check(i)) ans++;
}
cout << ans << endl;
return;
}
int main() {
fastio();
solve();
// int t; cin >> t; while(t--)solve();
// int t; cin >> t;
// for(int i=1;i<=t;i++){
// cout << "Case #" << i << ": ";
// solve();
// }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int count = 0;
for (int i = 1; i <= n; i++)
{
int i10 = i, i8 = i;
bool flag = false;
while (i10 > 0)
{
if (i10 % 10 == 7)
{
flag = true;
}
i10 /= 10;
}
while (i8 > 0)
{
if (i8 % 8 == 7)
{
flag = true;
}
i8 /= 8;
}
if (flag)
{
count++;
}
}
cout << n - count << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define deb(...)
#endif
#define int long long int
#define endl '\n'
void solve() {
int n, k;
cin >> n >> k;
vector<pair<int, int>>v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].first >> v[i].second;
}
sort(v.begin(), v.end());
vector<pair<int, int>>req;
for (int i = 0; i < n; i++) {
int sum = 0;
int zx = v[i].first;
while (i < n && v[i].first == zx) {
sum += v[i].second;
i++;
}
i--;
req.push_back({zx, sum});
}
sort(req.begin(), req.end());
v.clear();
v = req;
n = v.size();
// deb(v)
int ans = 0;
if (k >= v[0].first) {
k -= v[0].first;
k += v[0].second;
for (int i = 1; i < n; i++) {
if (k >= (v[i].first - v[i - 1].first)) {
k -= (v[i].first - v[i - 1].first);
k += (v[i].second);
}
else {
cout << v[i - 1].first + k << endl;
return;
}
}
if (k >= 0) {
cout << v.back().first + k << endl;
}
}
else {
cout << k;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(void) {
int n;
long long k;
long long x, y;
vector<pair<long long, long long> >a;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> x >> y;
a.push_back({ x,y }); //a[i].first=A_i, a[i].second=B_i
}
sort(a.begin(), a.end());
for (int i = 0; i < n; i++) {
if (a[i].first > k)break;
k += a[i].second;
}
cout << k << endl;
return 0;
} |
#include <bits/stdc++.h>
#define PB push_back
#define MP(x,y) make_pair(x,y)
#define F first
#define S second
#define loop(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rloop(i,m,n) for(int i=(int)(m) ; i > (int) (n) ; --i )
#define lp(i,n) loop(i,0,n)
#define rlp(i,n) rloop(i,n,0)
#define mod 1000000007
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int N;
scanf("%d", &N);
vector<pair<int, lli> > edges[N];
int u, v;
lli w;
lp(i, N-1) {
scanf("%d%d%lld", &u, &v, &w);
--u; --v;
edges[u].PB(MP(v,w));
edges[v].PB(MP(u,w));
}
vector<lli> XOR(N);
vector<bool> visited(N, false);
queue<pair<int, lli> > q;
lli d;
q.push(MP(0, 0));
visited[0] = true;
while (!q.empty()) {
u = q.front().F;
d = q.front().S;
q.pop();
XOR[u] = d;
for(vector<pair<int, lli> >::iterator it = edges[u].begin(); it != edges[u].end(); ++it) {
v = it->F;
w = it->S;
if (visited[v]) continue;
visited[v] = true;
q.push(MP(v, d^w));
}
}
lli ans = 0, add, cnt;
lp(bit, 60) {
cnt = 0;
lp(i, N)
if (XOR[i]&(((lli)1)<<bit)) ++cnt;
add = ((((lli)1)<<bit)%mod)*cnt;
add %= mod;
add = add * (N-cnt);
add %= mod;
// printf("%d %lld %lld\n", bit, cnt, add);
ans += add%mod;
ans %= mod;
}
// lp(i, N) {
// loop(j, i+1, N) {
// ans += XOR[i]^XOR[j];
// ans %= mod;
// }
// }
printf("%lld", ans);
return 0;
}
/*
*
* 1 0 0 1 1 0 A
* 0 0 1 1 0 1 B
*
* 1 0 1 0 1 1 A ^ B
* 1 0 0 1 1 0 A
* 1 0 1 0 0 0 1 A + A^B
*
* 0 0 1 1 0 1 0 2B
*
* 1 0 0 1 1 0 0 2A
*
*
* 010
* 011
* 001
*
* 001
* 011
* 010
*
*/
| #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debugger.h>
#else
#define dbg(x...) {}
#define dbg_t() {}
#endif
using ll = long long;
using ld = long double;
const ll INF = 1e18;
const ll MOD = 1e9+7;
namespace mod_op
{
ll madd(ll a, ll b)
{
return (a + b) % MOD;
}
ll mmul(ll a, ll b)
{
return ((a % MOD) * (b % MOD)) % MOD;
}
ll msub(ll a, ll b)
{
return (((a - b) % MOD) + MOD) % MOD;
}
ll mpow(ll a, ll b)
{
a %= MOD;
ll res = 1;
while (b > 0)
{
if (b & 1)
res = mmul(res, a);
a = mmul(a, a);
b >>= 1;
}
return res;
}
ll minv(ll a)
{
return mpow(a, MOD - 2);
}
ll mdiv(ll a, ll b)
{
return mmul(a, minv(b));
}
}
using namespace mod_op;
void solve()
{
ll n; cin >> n;
vector<vector<array<ll, 2>>> graph(n);
for (ll i = 0; i < n - 1; i++)
{
ll u, v, w; cin >> u >> v >> w;
u--; v--;
graph[u].push_back({v, w});
graph[v].push_back({u, w});
}
vector<ll> c(n, 0);
function<void(ll, ll)> dfs = [&](ll v, ll p)
{
for (auto x: graph[v])
{
if (x[0] != p)
{
c[x[0]] ^= (x[1] ^ c[v]);
dfs(x[0], v);
}
}
};
dfs(0, -1);
ll ans = 0;
vector<vector<ll>> v(n, vector<ll>(62, 0));
for (ll i = 0; i < n; i++)
{
bitset<62> bs(c[i]);
for (ll j = 0 ; j < 62; j++)
{
v[i][j] = (bs[j] == 1);
if (i > 0)
{
v[i][j] += v[i - 1][j];
if (bs[j] == 1)
ans = madd(ans, mmul(i - v[i - 1][j], mpow(2, j)));
else
ans = madd(ans, mmul(v[i - 1][j], mpow(2, j)));
}
}
}
cout << ans << "\n";
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll _t = 1;
// cin >> _t;
for (ll i = 1; i <= _t; i++)
{
// cout << "Case #" << i << ": ";
solve();
}
dbg_t();
return 0;
}
|
#include <cstdio>
#define rep( i, a, b ) for( int (i) = (a) ; (i) <= (b) ; (i) ++ )
#define per( i, a, b ) for( int (i) = (a) ; (i) >= (b) ; (i) -- )
const int MAXN = 105;
template<typename _T>
void read( _T &x )
{
x = 0;char s = getchar();int f = 1;
while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();}
while( s >= '0' && 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 ) + 1; }
if( 9 < x ){ write( x / 10 ); }
putchar( x % 10 + '0' );
}
char typ[MAXN][MAXN];
char in[MAXN];
int N, K;
char Compete( const char a, const char b )
{
if( a == b ) return a;
if( a == 'P' ) return b == 'R' ? a : b;
if( a == 'S' ) return b == 'P' ? a : b;
if( a == 'R' ) return b == 'S' ? a : b;
}
int main()
{
read( N ), read( K );
scanf( "%s", in + 1 );
for( int i = 1 ; i <= N ; i ++ ) typ[i][0] = in[i];
int stp = 1;
for( int j = 1 ; j <= K ; j ++, stp = stp * 2 % N )
for( int i = 1 ; i <= N ; i ++ )
typ[i][j] = Compete( typ[i][j - 1], typ[( i + stp - 1 ) % N + 1][j - 1] );
putchar( typ[1][K] ), putchar( '\n' );
return 0;
} | /*
JAI JAGANNATH!
*/
//@Author : zanj0
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ff first
#define ss second
#define pb push_back
#define MOD 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define w(x) int x; cin>>x; while(x--)
#define endl "\n"
#define timetaken cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n"
void zanj0()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
const int N = 1e5 + 5;
vector <vector <int> > graph[N];
int n;
map <int, set <int> > divs;
int dijkstra(int src) {
priority_queue < pair <int, int>, vector < pair <int, int> >, greater < pair <int, int> > > pq;
pq.push({0, src});
vector <int> dist(n + 1, inf);
while (!pq.empty()) {
pair <int, int> node = pq.top();
pq.pop();
if (node.ff >= dist[node.ss]) continue;
dist[node.ss] = node.ff;
for (auto& it : graph[node.ss]) {
int nxt = it[0];
for (auto &it2 : divs[it.back()]) {
int val = it2 + 1;
int extra = it2 - dist[node.ss];
int ndist = it[1] + (it.back() / val) + extra + dist[node.ss];
if (extra < 0) {
ndist = it[1] + (it.back() / (dist[node.ss] + 1)) + dist[node.ss];
}
if (ndist < dist[nxt]) {
pq.push({ndist, nxt});
}
}
}
}
return dist.back();
}
set <int> getDivs(int x) {
set <int> ret;
ret.insert(sqrt(x));
ret.insert(sqrt(x) + 1);
if (sqrt(x) > 0) ret.insert((sqrt(x) - 1));
return ret;
}
void solve() {
cin >> n;
int m;
cin >> m;
for (int i = 0; i < m; i++) {
int u, v, x, y;
cin >> u >> v >> x >> y;
graph[u].pb({v, x, y});
graph[v].pb({u, x, y});
if (!divs.count(y)) divs[y] = getDivs(y);
}
int ret = dijkstra(1);
if (ret == inf) ret = -1;
cout << ret << endl;
}
int32_t main()
{
zanj0();
solve();
timetaken;
return 0;
} |
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main () {
std::ios::sync_with_stdio(false); std::cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<pair<int,int>>> g(n);
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
g[a].emplace_back(b, c);
}
for (int start = 0; start < n; start++) {
vector<int> dist(n, 1 << 30);
priority_queue<pair<int, int>> que;
que.emplace(0, start);
int ans = -1;
bool flag = false;
while (!que.empty()) {
auto [curd, cur] = que.top();
que.pop();
curd = -curd;
if (cur == start) {
if (flag) {
ans = curd;
break;
}
flag = true;
}
if (dist[cur] < curd) continue;
for (auto [nbr, d] : g[cur]) {
int nd = curd + d;
if (dist[nbr] > nd) {
if (nbr != start) {
dist[nbr] = nd;
}
que.emplace(-nd, nbr);
}
}
}
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i <= n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll INF = 1e18;
const ll M = 3 * 1e5 + 10, B = sqrt(M) + 10;
// バケット i は [i*B, (i+1)*B) の記録を持つ
// a_s,...,a_t の xor を返す
ll getxor(int s, int t, vll &a, vll &xors){
ll ret = 0;
int st = s / B, gt = t / B;
//... || ... s ... t ... || ...
//直接計算
if (st == gt){
for (int i = s; i <= t; i++) ret ^= a[i];
}
//... s ... || (被覆されるパケット) || ... t ...
else{
//端が属するパケット内は直接計算
for (int i = s; i < (st + 1) * B; i++) ret ^= a[i];
for (int i = gt * B; i <= t; i++) ret ^= a[i];
//被覆されるパケットはパケット単位で調査する
for (int i = st + 1; i < gt; i++) ret ^= xors[i];
}
return ret;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n, q;
cin >> n >> q;
vll a(n), xors(B);
REP(i, n) {
cin >> a[i];
xors[i / B] ^= a[i];
}
while (q--) {
ll t, x, y;
cin >> t >> x >> y;
if(t == 1){
x--;
a[x] ^= y;
xors[x / B] ^= y;
}
else{
x--, y--;
cout << getxor(x, y, a, xors) << endl;
}
}
return 0;
}
|
//Radhe Radhe
#include<bits/stdc++.h>
#define FASTIO_ ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define mod 1000000007
#define ld long double
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define itr(i,n) for(ll i=0; i<n; i++)
#define itr_ab(i,a,b) for(ll i=a; i<=b; i++)
#define itrV(i,n) for(ll i=0; i<v.size(); i++)
#define fixed cout.setf(ios::fixed);
#define precise cout.precision(15);
#define yes cout<<"Yes"<<'\n'
#define no cout<<"No"<<'\n'
#define en '\n'
#define Dont_Quit int main(void)
#define Avada_Kedavra return 0;
using namespace std;
Dont_Quit
{
FASTIO_
ll tc,n,m,x,i,j,d;
string s;
tc=1;
while(tc--)
{
ll a[3];
cin>>a[0]>>a[1]>>a[2];
sort(a,a+3);
if(a[2]-a[1]==a[1]-a[0])
yes;
else no;
}
Avada_Kedavra
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
int arr[]={a,b,c};
int count=0;
sort(arr,arr+3);
if(arr[1]-arr[0]==arr[2]-arr[1])count++;
if(count==1)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll mod=1e9+7;
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
ll n, q;
cin>>n>>q;
vector<ll> a(n+1, 0), c(n+1, 0);
for (int i = 1; i <= n; ++i)
{
cin>>a[i];
}
for (int i = 1; i <= n; ++i)
{
c[i] = a[i] - i;
}
for (int i = 0; i < q; ++i)
{
ll k;
cin>>k;
if (k > c[n]) {
cout << a[n] + k - c[n] << "\n";
continue;
}
ll l = 0, r = n;
while (r -l > 1) {
ll m = (l+r)/2;
if (c[m] < k) l = m;
else r = m;
}
cout << a[l] + k - c[l] << "\n";
}
} | #include <bits/stdc++.h>
//#define DEBUG
#define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++)
#define deb(x) std::cout << #x << " " << x << endl;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
template<typename... T>
void read(T& ... a){
((cin >> a),...);
}
template<typename... T>
void write(T... a){
((cout << a << "\n"),...);
}
template<typename... T>
void write_space(T... a){
((cout << a << " "),...);
}
void solve(){
ll x, y, a, b;
read(x,y, a, b);
ll count = 0;
while( x < (x + b) / a && x <= (y -1) / a ){
x *= a;
count++;
}
count += (y - 1 - x) / b;
cout << count << endl;
}
int main()
{
//making data IO Fast
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
/****************************/
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
string res = "{";
for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
cerr << " " << to_string(H);
debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
int main() {
int k;
scanf("%d", &k);
ll ans = 0;
for (int a = 1; a <= k; ++a) {
for (int ab = a; ab <= k; ab += a) {
int nc = k / ab;
ans += nc;
}
}
printf("%lld\n", ans);
}
| /*pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(firstsecond) '(' << first< ',' << second< ')'
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
ll INF = 1001001001001;
long double EPS = 1e-8;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T> &vector, unsigned int index)
{
vector.erase(vector.begin() + index);
}
using Graph = vector<vector<pair<ll,ll>>>;
// MOD確認
int main(){
ll K;
cin >> K;
ll ans = 0;
ll cnt = 0;
ll m = 0;
FOR(i,1,K+1){
FOR(j,1,K / i + 1){
ll l = 1,r = INF;
while(r-l > 1){
ll mid = (l+r)/2;
if(i * j * mid > K) r = mid;
else l = mid;
}
ans += l;
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i < (r); i++)
using namespace std;
typedef long long ll;
int N;
int f(int x) {
while (x > N - 1) {
int a = 1;
while (a * 2 <= x) a *= 2;
x -= a;
}
return x + 1;
}
int main() {
cin >> N;
rep(i, 0, N) {
cout << f(i * 2) << " " << f(i * 2 + 1) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t; cin >> t;
rep(_, t) {
ll l, r; cin >> l >> r;
if(2*l > r) cout << 0 << endk;
else cout << (-2*l+1)*(r-2*l+1) + r*(r+1)/2 - (2*l-1)*(2*l)/2 << endk;
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i < (r); i++)
using namespace std;
typedef long long ll;
int main() {
int N, M, maxw = 0, minv = 1e9, ans = 1e9;
cin >> N >> M;
vector<int> w(N), l(M), v(M), s(1 << N, 0), d(1 << N, 0), a(N);
rep(i, 0, N) {
cin >> w[i];
maxw = max(maxw, w[i]);
}
rep(i, 0, M) {
cin >> l[i] >> v[i];
minv = min(minv, v[i]);
}
if (maxw > minv) {
cout << -1 << endl;
return 0;
}
rep(i, 0, 1 << N) {
rep(j, 0, N) {
if ((i >> j) & 1) s[i] += w[j];
}
rep(j, 0, M) {
if (s[i] > v[j]) d[i] = max(d[i], l[j]);
}
//cout << i << " " << s[i] << " " << d[i] << endl;
}
rep(i, 0, N) a[i] = i;
do {
//rep(i, 0, N) cout << a[i]; cout << endl;
vector<int> b(N + 1, 0);
rep(i, 0, N) b[i + 1] = b[i] | (1 << a[i]);
//rep(i, 0, N + 1) cout << b[i] << " "; cout << endl;
vector<vector<int>> c(N, vector<int>(N));
rep(i, 0, N - 1) {
rep(j, i + 1, N) {
c[i][j] = d[b[i] ^ b[j + 1]];
//cout << "c[" << i << "][" << j << "]=" << c[i][j] << " ";
}
}
vector<int> dist(N, 0);
rep(i, 1, N) {
rep(j, 0, i) {
dist[i] = max(dist[i], dist[j] + c[j][i]);
}
}
ans = min(ans, dist[N - 1]);
//out << ans << endl;
} while (next_permutation(a.begin(), a.end()));
cout << ans << endl;
} | #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef __int128 int_;
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0'||c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c&&c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
inline void writeint(int_ x){
if(x > 9) writeint(x/10);
putchar((x-x/10*10)^48);
}
int_ exgcd(int_ a,int_ b,int_ &x,int_ &y){
if(!b){ x = 1, y = 0; return a; }
int_ d = exgcd(b,a%b,y,x);
y -= (a/b)*x; return d;
}
int main(){
int_ zxy, sxy;
for(int T=readint(); T; --T){
int_ ans = -1; // infinity
int x = readint(), y = readint();
int p = readint(), q = readint();
int T1 = 2*(x+y), T2 = p+q;
int_ lcm = exgcd(T1,T2,zxy,sxy);
lcm = 1ll*T1/lcm*T2;
rep(i,x,x+y-1) rep(j,p,p+q-1){
int_ a, b, d = exgcd(T1,T2,a,b);
if((j-i)%d) continue; // no solution
d = (j-i)/d; a *= d, b *= d;
int_ t = T1*a+i; // remainder
t = (t%lcm+lcm)%lcm; // be positive
if(ans == -1 || ans > t) ans = t;
}
if(ans == -1) puts("infinity");
else writeint(ans), putchar('\n');
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
unordered_map<ll,ll>dp;
ll n,m;
ll fun(ll x){
if(n>=x) return n-x;
if(dp[x]) return dp[x];
ll c=x-n;
c=min(c,fun(x/2)+1+x%2);
if(x%2) c=min(c,fun(x/2+1)+2);
return dp[x]=c;
}
int main(){
scanf("%lld%lld",&n,&m);
printf("%lld\n",fun(m));
return 0;
} | /* _
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||_ \
| | \\\ - /'| | |
| \_| `\`---'// |_/ |
\ .-\__ `-. -'__/-. /
___`. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' _> \"".
| | : `- \`. ;`. _/; .'/ / .' ; |
\ \ `-. \_\_`. _.'_/_/ -' _.' /
===========`-.`___`-.__\ \___ /__.-'_.'_.-'================
Please give me AC.
*/
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <string>
#include <sstream>
#include <complex>
#include <bitset>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <chrono>
#include <random>
using namespace std;
using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define printn(v) rep(i, (v).size() - 1) cout << (v)[i] << ' '; cout << (v)[n - 1] << '\n';
#ifdef ONLINE_JUDGE
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define dbg(vec)
#else
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define debug2(x, y) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << '\n'
#define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';}
#endif
// constant
const int INF = (1<<30) - 1;
const int64 INF64 = (1LL<<62) - 1;
template<typename T> T gcd(T a, T b) {
if (a < b) return gcd(b, a);
T r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
template<typename T> T lcm(const T a, const T b) {
return a / gcd(a, b) * b;
}
template<typename T> bool chmin(T& a, const T& b) {
if (a > b) return a = b, true; else return false;
}
template<typename T> bool chmax(T& a, const T& b) {
if (a < b) return a = b, true; else return false;
}
// End of template.
unordered_map<int64, int64> memo;
int64 f(int64 x, int64 y) {
auto it = memo.find(y);
if (it != memo.end()) return it->second;
if (x >= y) {
return x - y;
} else if (y & 1) {
return memo[y] = min(min(f(x, (y + 1) / 2), f(x, (y - 1) / 2)) + 2, abs(x - y));
} else {
return memo[y] = min(abs(x - y), f(x, y / 2) + 1);
}
}
int main() {
cout << fixed << setprecision(15);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int64 x, y;
cin >> x >> y;
print(f(x, y));
return 0;
}
/*
__
/ _| ___ _ __ _ __
| |_ / __| '_ \| '_ \
| _| (__| |_) | |_) |
|_|(_)___| .__/| .__/
|_| |_|
*/
|
#include<bits/stdc++.h>
#define LL long long
#define DB long double
using namespace std;
//#define P 998244353
//inline int ad(int x,int y){return (x+y>=P)?(x+y-P):(x+y);}
//inline int mt(int x,int y){return 1ll*x*y%P;}
//inline void mo(int &x){x=(x>=P)?(x-P):x; x=(x<0)?(x+P):x;}
template<typename T>inline void ckmx(T &x,T y){x=max(x,y);}
template<typename T>inline void ckmi(T &x,T y){x=min(x,y);}
template<typename T>void read(T &x){
x=0;int f=1;
char c=getchar();
for(;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
x*=f;
}
LL a[100010];
LL b[10];
int usd[10],n,T,bn;
int main(){
read(T);
while(T--){
read(n);
bn=0;
int tp=0;
for(int i=1;i<=n;++i) read(a[i]);
sort(a+1,a+1+n);
if(n&1) puts("Second");
else{
int la=0,f=1;
for(int i=1;i<=n;++i)
if (la==0) la=a[i];
else if(la==a[i]) la=0;
else f=0;
if(f==0) puts("First");
else puts("Second");
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define all(x) (x).begin(),(x).end()
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int ddx[]={0,0,1,-1,1,-1,1,-1};
int ddy[]={1,-1,0,0,-1,1,1,-1};
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//ll t;cin>>t;while(t--)
{
ll a,b,c,d,i,j,k,l,m,n,sum=0,temp=0,f=0,cnt=0;
ll wt;
cin>>a>>b>>wt;
wt*=1000;
ll p=-1,q=-1;
for(i=a;i<=b;i++)
{
if(wt%i==0 || (a<=(wt%i) && (wt%i)<=b))
{
if(wt%i==0)p=wt/i;
else p=(wt/i)+1;
break;
}
}
for(i=b;i>=a;i--)
{
if(wt%i==0 || (a<=(wt%i) && (wt%i)<=b))
{
if(wt%i==0)q=wt/i;
else q=(wt/i)+1;
break;
}
}
swap(p,q);
if(p==-1)cout<<"UNSATISFIABLE"<<endl;
else {
cout<<((wt/b)+((wt%b)!=0))<<" "<<((wt/a))<<endl;
}
}
}
|
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 2e5 + 5;
int n, a[MAXN];
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
int ans = a[0];
for(int i = 0; i < n; ++i) {
ans = __gcd(ans, a[i]);
}
// while(true) {
// int minVal = 2e9;
// for(int i = 0; i < n; ++i) {
// if (minVal > a[i])
// minVal = a[i];
// }
// for(int i = 0; i < n; ++i) {
// if(minVal < a[i]) {
// a[i] = a[i]%minVal;
// }
// }
// }
printf("%d\n", ans);
} |
//#pragma GCC target("avx2")
#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() {
cerr << endl;
}
template <typename H, typename... T>
void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T>
void print(T x) {
cout << x << "\n";
}
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;
ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n; cin >> n;
vi a(n);
REP(i, n) cin >> a[i];
int m = *min_element(ALL(a));
int ans = m;
REP(i, n) {
ans = __gcd(ans, a[i]);
}
print(ans);
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
typedef long double LD;
typedef pair<int, int> II;
const LD EPS = 1e-8;
bool solve(int N, vector<int> a, vector<int> b, vector<int> c, vector<int> d) {
if (N <= 1) return true;
LD dx = a[1] - a[0], dy = b[1] - b[0], r = sqrtl(dx * dx + dy * dy), deg = atan2l(dy, dx);
set<II> s;
for (int i = 0; i < N; ++i) {
s.insert(II(a[i] - a[0], b[i] - b[0]));
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (i == j) continue;
LD dx2 = c[j] - c[i], dy2 = d[j] - d[i], r2 = sqrtl(dx2 * dx2 + dy2 * dy2), offset = atan2l(dy2, dx2) - deg;
if (fabsl(r2 - r) > EPS) continue;
set<II> s2;
for (int k = 0; k < N; ++k) {
LD dx3 = c[k] - c[i], dy3 = d[k] - d[i], r3 = sqrtl(dx3 * dx3 + dy3 * dy3), deg3 = atan2l(dy3, dx3) - offset;
LD x = roundl(cosl(deg3) * r3), y = roundl(sinl(deg3) * r3);
LD r4 = sqrtl(x * x + y * y);
if (fabsl(r4 - r3) <= EPS) {
s2.insert(II(int(x), int(y)));
}
}
if (s == s2) {
return true;
}
}
}
return false;
}
int main() {
#if DEBUG || _DEBUG
freopen("in.txt", "r", stdin);
// freopen("in_1.txt", "r", stdin);
#endif
int N;
cin >> N;
vector<int> a(N), b(N), c(N), d(N);
for (int i = 0; i < N; i++) {
std::cin >> a[i] >> b[i];
}
for (int i = 0; i < N; i++) {
std::cin >> c[i] >> d[i];
}
cout << (solve(N, a, b, c, d) ? "Yes" : "No") << endl;
return 0;
}
| /*
Author: Nguyen Tan Bao
Status:
Idea:
*/
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x
using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;
const ll MODBASE = 1000000007LL;
const int MAXN = 110;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;
ll MOD = MODBASE;
ll binPowMod(ll a, ll b) {
a %= MOD;
ll res = 1;
while (b > 0) {
if (b & 1LL) res = res * a % MOD;
a = a * a % MOD;
b >>= 1LL;
}
return res;
}
ll add(ll a, ll b) {
return (a + b) % MOD;
}
ll sub(ll a, ll b) {
return (a - b + MOD) % MOD;
}
ll mul(ll a, ll b) {
return a * b % MOD;
}
ll inv(ll u) {
return binPowMod(u, MOD-2);
}
ll get(ll r) {
return mul(mul(r, r+1), inv(2));
}
ll getSum(ll l, ll r) {
if (r < l) return 0;
return sub(get(r), get(l-1));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
int t, n, a, b;
cin >> t;
while (t--) {
cin >> n >> a >> b;
ll res = mul(mul(n-a+1, n-a+1), mul(n-b+1, n-b+1));
if (a > b) swap(a, b);
int l = b, r = b+a-1;
int g = n-a+1;
r = min(r, g);
l = min(l, g);
// cout << l << ' ' << r << endl;
ll sum = mul(getSum(l, r-1), 2);
// cout << getSum(l, r-1) << endl;
ll remain = n-b+1 - (r - l) * 2;
sum = add(sum, mul(remain, r));
sum = mul(sum, sum);
res = sub(res, sum);
cout << res << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3f;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
ll n;
cin>>n;
set<ll> s;
for(ll i=2;i*i<=n;i++)
{
ll res=i;
for(int j=1;j<=35;j++)
{
res*=i;
if(res<=n) s.insert(res);
else break;
}
}
cout<<n-s.size()<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF=1e9+7;
const ll INFF=1e18+7;
#define nl '\n'
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char* x) { cout << '\"' << x << '\"'; }
void __print(const string& x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template<typename T, typename V>
void __print(const pair<T, V>& x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; }
template<typename T>
void __print(const T& x) { int f = 0; cout << '{'; for (auto& i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; }
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
int intlog(int base, int x) {
return (int)(log(x) / log(base));
}
void solve()
{
ll n,nr=0;
cin>>n;
ll a=2,b=2;
unordered_map<ll,int> d;
while(a*a<=n){
while(a*b<=n){
a*=b;
d[a]++;
if(d[a]==1) nr++;
}
b++;
a=b;
}
cout<<n-nr;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
// int t;
// cin >> t;
for(int tt=1; tt<=t; tt++){
// cout<<"#Case "<<t<<nl;
solve();
}
return 0;
} |
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_poizcy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define mem(a,h) memset(a, (h), sizeof(a))
#define por(a,b) (((a%MOD) * (b%MOD))%MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i+=c)
#define forr(i, b, e) for (ll i = b; i < e; i++)
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef vector<int> vi;
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
//typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define initseg ll new_nodo=(pnodo*2),mid=(iz+der)/2;
#define fi first
#define se second
#define ptr item*
const long long INF = 1e18;
const ll tam =1000100;
const ll MOD=998244353;
const ll cmplog=29;
int main()
{
ll n;
cin>>n;
ll iz=1,der=2;
ll mid;
while(der*(der+1)/2<=n+1)
der*=2;
while(iz+1<der)
{
mid=(iz+der)/2;
if (mid*(mid+1)/2<=n+1)
iz=mid;
else der=mid;
}
cout<<n-iz+1<<endl;
}
| // #include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <ctime>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cassert>
#include <bitset>
using namespace std;
int main() {
long long int N, ans=0;
long long int x;
string y;
cin >> N;
for(size_t i=1; i<1e+6; i++){
y = to_string(i);
y += to_string(i);
x = stoll(y);
if(x > N){
break;
}
else{
ans += 1;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
void solve(long long N, long long K, std::vector<std::vector<long long>> A){
// 中央値を ll 以下にすることが可能か
int ok = 1e9;
int ng = -1;
auto check = [&](int l) {
vector<vector<int>> cum1(N, vector<int>(N + 1));
rep(i, N) {
rep(j, N) {
cum1[i][j + 1] = cum1[i][j] + (A[i][j] <= l);
}
}
vector<vector<int>> cum2(N + 1, vector<int>(N + 1));
rep(i, N) {
rep(j, N + 1) {
cum2[i + 1][j] = cum2[i][j] + cum1[i][j];
}
}
// printf("l: %ld\n", l);
rep(i, N + 1) {
rep(j, N + 1) {
// printf("%ld ", cum2[i][j]);
}
// printf("\n");
}
rep(i, N - K + 1) {
rep(j, N - K + 1) {
if (
cum2[i + K][j + K]
- cum2[i + K][j]
- cum2[i][j + K]
+ cum2[i][j] >= (K * K + 1) / 2) {
// printf("OK\n");
return 1;
}
}
}
// printf("NG\n");
return 0;
};
while (ok - ng > 1) {
int l = (ok + ng) / 2;
(check(l) ? ok : ng) = l;
}
cout << ok << endl;
}
// Generated by 2.3.1 https://github.com/kyuridenamida/atcoder-tools
int main(){
long long N;
std::scanf("%lld", &N);
long long K;
std::scanf("%lld", &K);
std::vector<std::vector<long long>> A(N, std::vector<long long>(N));
for(int i = 0 ; i < N ; i++){
for(int j = 0 ; j < N ; j++){
std::scanf("%lld", &A[i][j]);
}
}
solve(N, K, std::move(A));
return 0;
}
| #include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using vi = vector<int>;
using si = set<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define P pair<int,int>
#define PLL pair<ll,ll>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)
#define reciv(v,n) vll (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) ll x;cin >> x
#define cci(x,y) ll x,y;cin >> x >> y
#define co(x) cout << x << endl
#define pb push_back
#define eb emplace_back
#define rz resize
#define pu push
#define sz(x) int(x.size())
#define vij v[i][j]
// ll p = 1e9+7;
// ll p = 998244353;
// n do -> n*pi/180
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}
ll x,y;
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){x=1;y=0;return a;}
ll d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int main(){
cii(t);
vll ans;
while(t--){
cii(n);
cci(s,k);
s=n-s;
ll g=__gcd(n,__gcd(s,k));
n/=g;
s/=g;
k/=g;
ll g2=__gcd(n,k);
if(g2!=1){ans.pb(-1);continue;}
exgcd(k,n,x,y);
(x+=n)%=n;
ans.pb(s*x%n);
}
for(ll d:ans)co(d);
} |
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T>& a) {
for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? " " : "") << a[i];
return os << '\n';
}
template <typename T>
std::ostream& operator<<(std::ostream& os, pair<T, T>& p) {
return os << "{ " << p.first << ", " << p.second << " }" << std::endl;
}
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& a) {
for (T& x : a) is >> x;
return is;
}
[[maybe_unused]] constexpr ll MOD = 1000000007;
[[maybe_unused]] constexpr int INF = 0x3fffffff;
[[maybe_unused]] constexpr ll INFL = 0x3fffffffffffffffLL;
class combination {
public:
combination(int n, uint64_t m)
: mod(m), fact(n + 1), inv(n + 1), finv(n + 1) {
fact[0] = finv[0] = inv[1] = 1;
// i * inv[i] = 1 mod p.
// p = p / i * i + p % i
// => p / i * i + p % i = 0 mod p
// => (p / i * -inv[p % i]) * i = 1 mod p
for (uint64_t i = 2; i <= n; ++i)
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
for (uint64_t i = 1; i <= n; ++i) {
fact[i] = fact[i - 1] * i % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
uint64_t operator()(int n, int k) const {
if (n < k || n < 0 || k < 0) return 0;
return fact[n] * finv[n - k] % mod * finv[k] % mod;
}
uint64_t modpow(uint64_t x, uint64_t e) {
uint64_t res = 1;
while (e > 0) {
if (e & 1) res = res * x % mod;
x = x * x % mod;
e >>= 1;
}
return res;
}
private:
uint64_t mod;
std::vector<uint64_t> fact, inv, finv;
};
int main() {
cin.tie(0)->sync_with_stdio(0);
ll n, m, k;
cin >> n >> m >> k;
combination c(n + m + 5, MOD);
if (n - m > k) {
cout << 0;
} else {
ll a = c(n + m, n);
ll b = c(n + m, n - k - 1);
cout << (MOD + (a - b) % MOD) % MOD;
}
return 0;
}
|
//It is good to start with zero//
// kyu @Aryabhatta//
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
typedef long long ll;
#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define repA(i, a, n) for(ll i = a; i <= (n); ++i)
#define repD(i, a, n) for(ll i = a; i >= (n); --i)
#define vec(i, a) for( auto i=a.begin();i != a.end(); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define fill(a) memset(a, 0, sizeof (a))
#define mp make_pair
#define pb push_back
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define in freopen("input.txt","r",stdin);
#define out freopen("output.txt","w",stdout);
#define sixfive in out
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
const ll MOD = 1e9 + 7;
ll n_bits(ll n ) { ll x= __builtin_popcount(n) ;return x ;}
int pow(int a, int b, int m)
{
int ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%m;
b/=2;
a=(a*a)%m;
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
sixfive
#endif
fast
ll t=1;
//cin>>t;
while(t--)
{
int a,b,c,d;
int sum=0;
cin>>a>>b>>c>>d;
if(a==b+c+d||b==c+d+a||c==a+b+d||d==a+b+c)
{
cout<<"Yes";
continue;
}
if(a+b==c+d||b+c==a+d||a+c==b+d||a+d==b+c)
{
cout<<"Yes";
continue;
}
cout<<"No";
}
return 0;
}
// sometimes I do it for her...// |
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define endl "\n"
using namespace std;
const int max_n = 1e5+100;
const int max_sum = 1e7+100;
const int max_len = 1e6+110;
const int offset = 1e5+10;
const int inf = 1e18;
int n;
vector<int> adj[max_n];
int st[max_n];
struct Node
{
int first,second,turn;
Node(){
first=0;second=0;turn=0;
}
};
Node dfs(int v){
st[v]=1;
vector<Node> c;
for(int x:adj[v]){
c.pb(dfs(x));
st[v]+=st[x];
}
sort(c.begin(),c.end(),[](const Node& a,const Node& b)->bool{
if(a.turn!=b.turn){
return a.turn<b.turn;
}else{
return (a.second-a.first)>(b.second-b.first);
}
});
Node ans;
ans.turn = (st[v]%2);
ans.first = 1;
for(Node x:c){
if(x.turn==0 && x.second>=x.first){
ans.second+=x.second;
ans.first+=x.first;
}
}
int t=0;
for(Node x:c){
if(x.turn==1){
if(t==0){
ans.second+=x.second;
ans.first+=x.first;
}else{
ans.second+=x.first;
ans.first+=x.second;
}
t = (t+1)%2;
}
}
for(Node x:c){
if(x.turn==0 && x.second<x.first){
if(t==1){
ans.first+=x.second;
ans.second+=x.first;
}else{
ans.first+=x.first;
ans.second+=x.second;
}
}
}
return ans;
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin>>n;
for(int i=1;i<n;i++){
int p;cin>>p;p--;
adj[p].pb(i);
}
Node ans = dfs(0);
cout<<ans.first<<endl;
} | //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef _DEBUG
#define debug(x) cout << #x << ": " << x << std::endl
#else
#define debug(x)
#pragma GCC target("avx")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); ++i)
#define all(x) (x).begin(), (x).end()
constexpr char enl = '\n';
constexpr int INF = 1000000000; // 1e9
constexpr ll llINF = 1000000000000000000LL; // 1e18
/*#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
*/
typedef struct node_ {
vector<int> child;
int invert;
int size;
int first;
node_() :
child(vector<int>(0)), invert(-1), size(1), first(-1) {}
} node;
void Main() {
int n;
cin >> n;
vector<node> tree(0);
tree.reserve(n);
rep(i, 0, n) tree.emplace_back();
rep(i, 1, n) {
int p;
cin >> p;
tree[p - 1].child.push_back(i);
//debug(tree[p - 1].child[tree[p - 1].child.size() - 1]);
}
auto get_inv = [&](auto self, int cn) -> pair<int, int> {
int res1 = 1, res2 = 1;
for (auto nn : tree[cn].child) {
auto [a, b] = self(self, nn);
res1 = (res1 + a) % 2;
res2 += b;
}
tree[cn].invert = res1;
tree[cn].size = res2;
return {res1, res2};
};
get_inv(get_inv, 0);
//rep(i, 0, n) debug(tree[i].size);
auto get_first = [&](auto self, int cn) -> int {
//debug(cn);
if (tree[cn].first != -1) return tree[cn].first;
int res = 0;
vector<int> minus(0), plus(0), inv(0);
for (auto nn : tree[cn].child) {
//debug(cn); debug(nn);
self(self, nn);
if (tree[nn].invert == 1) inv.push_back(nn);
else if (2 * tree[nn].first <= tree[nn].size)
minus.push_back(nn);
else
plus.push_back(nn);
}
sort(all(inv), [&](const auto &a, const auto &b) { return 2 * tree[a].first - tree[a].size < 2 * tree[b].first - tree[b].size; });
for (auto nn : minus) res += tree[nn].size - tree[nn].first;
if (inv.size() % 2)
for (auto nn : plus) res += tree[nn].first;
else
for (auto nn : plus) res += tree[nn].size - tree[nn].first;
rep(i, 0, inv.size()) res += i % 2 ? tree[inv[i]].first : tree[inv[i]].size - tree[inv[i]].first;
return tree[cn].first = tree[cn].size - res;
};
cout << get_first(get_first, 0) << endl;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(15);
Main();
} |
/*
author: Kamal Kant Bhardwaj
date:
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define N 20000006
#define F first
#define S second
#define test(x) int x; cin>>x; while(x--)
#define pb push_back
#define vi vector<int>
#define mapp map<int,int>
#define pqbig priority_queue<int>
#define pqsmall priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define Inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int32_t main() {
c_p_c();
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << (b - c) << endl;
return 0;
}
| #include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int t;scanf("%d",&t);
while(t--)
{
int n;scanf("%d%*s%*s%*s",&n);
for(int i=1;i<=n;i++)putchar('0');
for(int i=1;i<=n;i++)putchar('1');
puts("0");
}
return 0;
} |
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
#define rep(i,n) for (int i = 0; i < (int)(n); ++i)
int main(){
int n,m; cin >> n >> m;
cout << n/m << endl;
} | #include<bits/stdc++.h>
#define for_int(i,a,b) for(int i=(a);i<=(b);++i)
#define rep_int(i,a,b) for(int i=(a);i>=(b);--i)
#define for_ll(i,a,b) for(ll i=(a);i<=(b);++i)
#define rep_ll(i,a,b) for(ll i=(a);i>=(b);--i)
#define Ios ios::sync_with_stdio(false),cin.tie(0)
#define ull unsigned long long
#define ll long long
#define db double
#define HPMAX 1005
#define KMP_MAX 1005
#define prime_MAX 1000005
#define quick_pow_mod 1000000007
#define PII pair<int,int>
const int factor_N = 10000005;
const int tree_arr_N = 1000005;
using namespace std;
/*
1.注意边界情况
2.优先考虑时间复杂度
!!! 3.求最大值时,答案应至多初始化为INT_MIN;求最小值时,答案应至少初始化为INT_MAX
4.遇事不决先暴力
5.代码要写简洁
6.计数题要开long long
*/
//快读
int read() {
char c = getchar();
int x = 0, f = 1;
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
return x * f;
}
int n, m;
int main() {
Ios;
cin >> n >> m;
cout << n / m << endl;
return 0;
}
|
/*
-------------- | /
| | /
| | /
| * |/ | | ------ *
| | | | / \
| | |\ | | | |\ |
\ | | | \ | | | | \ |
\ | | | \ | | \ / \ |
V | | \ \__/| ----- \ |
*/
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "\e[1;31m" << #x << " = " << (x) << "\e[0m\n"
#define print(x) emilia_mata_tenshi(#x, begin(x), end(x))
template<typename T> void emilia_mata_tenshi(const char *s, T l, T r) {
cerr << "\e[1;33m" << s << " = [";
while(l != r) {
cerr << *l;
cerr << (++l == r ? ']' : ',');
}
cerr << "\e[0m\n";
}
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9+7;
/*--------------------------------------------------------------------------------------*/
signed main() {
int n;
cin >> n;
vector<vector<int>> edge(n + 1);
vector<int> in(n + 1), out(n + 1), dep(n + 1);
vector<vector<int>> d(n);
for(int i = 2; i <= n; i++) {
int x;
cin >> x;
edge[x].push_back(i);
}
int t = 0;
function<void(int)> dfs = [&](int u) {
d[dep[u]].push_back(t);
in[u] = t++;
for(int v : edge[u])
dep[v] = dep[u] + 1, dfs(v);
d[dep[u]].push_back(t);
out[u] = t++;
};
dfs(1);
int q;
cin >> q;
while(q--) {
int u, de;
cin >> u >> de;
cout << (upper_bound(d[de].begin(), d[de].end(), out[u]) - lower_bound(d[de].begin(), d[de].end(), in[u])) / 2 << '\n';
}
}
| #include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair
typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;
const int N = 200005;
vi g[N];
vi nodes[N];
int D[N];
int _tm;
int tin[N], tout[N];
void dfs(int c, int d) {
D[c] = d;
tin[c] = ++_tm;
nodes[d].pb(tin[c]);
rep(i,0,g[c].size()) {
dfs(g[c][i], d + 1);
}
tout[c] = ++_tm;
}
int bs(int d, int x) {
int lo = 0, hi = (int)nodes[d].size() - 1;
int ans = 0;
while(lo <= hi) {
int mi = (lo + hi) >> 1;
if(nodes[d][mi] <= x) {
ans = mi + 1;
lo = mi + 1;
} else {
hi = mi - 1;
}
}
return ans;
}
int main() {
int n;
S(n);
rep(i,2,n+1) {
int p;
S(p);
g[p].pb(i);
}
dfs(1, 0);
int q;
S(q);
while(q--) {
int u,d;
S2(u,d);
if(D[u] > d) {
P(0);
continue;
}
P(bs(d, tout[u]) - bs(d, tin[u] - 1));
}
return 0;
}
|
#include <iostream>
#include <unordered_map>
int main()
{
int n;
std::cin >> n;
std::unordered_map<int, int> table;
for (int i = 0; i < n; ++i)
{
int a;
std::cin >> a;
table[a] += 1;
}
int64_t total = 0;
int consumed = 0;
for (const auto &e : table)
{
const int c = e.second;
const int k = n - consumed - c;
total += static_cast<int64_t>(c) * k;
consumed += c;
}
std::cout << total << std::endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
// ---------- Debugger
#define TRACE
string to_string(string s) {
return '"' + s + '"';
}
string to_string(char c) {
return '\'' + string(1, c) + '\'';
}
string to_string(char * s) {
return to_string((string) s);
}
string to_string(const char * s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template < typename A >
string to_string(A v);
template < typename A, typename B >
string to_string(pair < A, B > p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template < typename C > struct rge { C b, e; };
template < typename C > rge<C> range(C i, C j) { return rge<C>{i, j}; }
template < typename C >
string to_string(rge<C> v) {
bool first = true;
string res = "[";
for(auto it = v.b; it != v.e; it++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(*it);
}
res += "]";
return res;
}
template < typename A >
string to_string(A v) {
return to_string(range(v.begin(), v.end()));
}
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template < typename Head >
void __f(const string names, Head arg1) {
cerr << names << " : " << to_string(arg1) << endl;
}
template < typename Head, typename...Tail >
void __f(const string names, Head arg1, Tail...args) {
int b = 0, comma = names.size();
for(int i=0;i<names.size();i++) {
if(names[i] == ',' && b == 0) {
comma = i;
break;
}
if(names[i] == '(' || names[i] == '{' || names[i] == '[' || names[i] == '<') b++;
else if(names[i] == ')' || names[i] == '}' || names[i] == ']' || names[i] == '>') b--;
}
// size_t comma = names.find(', ');
cerr << names.substr(0, comma) << " : " << to_string(arg1) << " | ";
__f(names.substr(comma + 1), args...);
}
#else
#define trace(...)
#endif
// ---------- Debugger
#define LL long long
#define FI ios_base::sync_with_stdio(false); cin.tie(NULL);
#define PREC cout << setprecision(10) << fixed;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 9;
const int MX = (int)2e5 + 5;
namespace modop {
const int mod = 998244353;
long long madd(long long a, long long b) {
return (a + b) % mod;
}
long long msub(long long a, long long b) {
return (((a - b) % mod) + mod) % mod;
}
long long mmul(long long a, long long b) {
return ((a % mod) * (b % mod)) % mod;
}
long long mpow(long long base, long long exp) {
long long res = 1;
while (exp) {
if (exp % 2 == 1){
res = (res * base) % mod;
}
exp >>= 1;
base = (base * base) % mod;
}
return res;
}
long long minv(long long base) {
return mpow(base, mod - 2);
}
long long mdiv(long long a, long long b) {
return mmul(a, minv(b));
}
}
using namespace modop;
int n;
int inp[MX];
int vis[MX], mark[MX];
int cyc;
void dfs(int node) {
vis[node] = 1;
mark[node] = 1;
int v = inp[node];
if(mark[v] == 1) {
cyc++;
}
if(!vis[v]) {
dfs(v);
}
mark[node] = 0;
}
int main() {
cin >> n;
for(int i=1;i<=n;i++) {
cin >> inp[i];
vis[i] = 0;
mark[i] = 0;
}
cyc = 0;
for(int i=1;i<=n;i++) {
if(!vis[i]) {
dfs(i);
}
}
int ans = msub(mpow(2, cyc),1);
cout << ans << endl;
return 0;
}
|
//@Author: KeinYukiyoshi
// clang-format off
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#define int long long
using namespace std;
#define stoi stoll
#define fi first
#define se second
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++)
#define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--)
#define FOR(i, a) for (auto &i: a)
#define ALL(obj) begin(obj), end(obj)
#define _max(x) *max_element(ALL(x))
#define _min(x) *min_element(ALL(x))
#define _sum(x) accumulate(ALL(x), 0LL)
const int MOD = 1e14;
// const int MOD = 998244353;
const int INF = (int)1e18;
const int INF2 = 10000000000007; // 1e13 + 7
const int INF3 = LLONG_MAX; // 9.2e18
const double EPS = 1e-8;
const double PI = 3.14159265358979;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T> using VVV = vector<vector<vector<T>>>;
template <class T, class S> using P = pair<T, S>;
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 _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); }
template<class T> T chmod(T &a, T mod=MOD) {a = a >= 0 ? a % mod : a - (mod * _ceil(a, mod)); return a;};
int _mod(int a, int mod=MOD) {return a >= 0 ? a % mod : a - (mod * _ceil(a, mod));}
int _pow(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;}
struct mint {long long x;mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) { (x *= a.x) %= MOD;return *this; }mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long t) const {if (!t) return 1;mint a = pow(t >> 1);a *= a;if (t & 1) a *= *this;return a;}mint inv() const { return pow(MOD - 2); }mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }};istream &operator>>(istream &is, mint &a) { return is >> a.x; }ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// clang-format on
class CDoubled {
public:
static void solve(istream &cin, ostream &cout) {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
int N;
cin >> N;
int ans = 0;
rep2(i, 1, 1000000 + 100) {
string I = to_string(i);
int ret = i + i * _pow(10, I.size());
if (ret <= N) ans++;
}
cout << ans << endl;
}
};
signed main() {
CDoubled solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| #include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;
const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<char> Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
int main()
{
cout << fixed << setprecision(10);
ll n;
cin >> n;
vector<ll> a(n) , b(n);
rep(i,n) cin >> a[i];
rep(i,n) cin >> b[i];
ll sum = 0;
rep(i,n)
{
sum += a[i] * b[i];
}
if(sum==0)
{
cout << "Yes" << endl;
}
else cout << "No" << endl;
return 0;
}
|
Subsets and Splits